a readable stream emitting lines (from files, streams)


[Node.js] ReadableStream of lines

$ npm install linestream


$ git clone git://
var stream = require('linestream').create(filename, {bufferSize: 300});

stream.on('data', function(line, isEnd) {
  console.log(line); // each line comes here
  console.log(isEnd); // if it is the end of data or not.

stream.on('end', function() { // emitted at the end of file

stream.on('error', function(e) { // emitted when an error occurred
var https = require('https');
var req = https.request({host: ''}, function(response) {
  var stream = require('linestream').create(response);

  stream.on('data', function(line) {
    console.log(line); // each line comes here

LineStream extends ReadableStream.

See Node.js#Stream for ReadableStream's API.

  • LineStream.create(source, options)
  • on "data"
  • LineStream.tsv(source, [options], fn)
  • stream.after(rstream1, rstream2, ...)

Creates an instance of LineStream.

source is the one of the followings.

  • (string) filename. Then reads the file and emit each lines.
  • "-". Then resumes process.stdin and reads from it.
  • (ReadableStream) stream. Then reads lines from the stream.

(Object) options is optional.

key type description example
separator string line separator. "\n" by default.
trim boolean If true, separator are not appended in the line. true by default.
filter function filter function before emitting lines.
each line is passed to the function as the first argument.
function (line) { return line.length }
comment string Registers the marks of one-line comment. If the mark comes in the first position of a line, the line is filtered. "#"
fieldSep string A field separator. It is used with fieldNum options. "\t"
fieldNum string the required number of the fields.
If not matched, the line is filtered.
empty boolean If true, empty lines (after trimmed) are filtered. true

Other options are passed to fs.createReadStream(filename, options) if the first argument is a string.

See fs.createReadStream()

Data event of LineStream. Two arguments are passed.

  • line (string) each line
  • isEnd (boolean) whether the line is final or not.


stream.on("data", function(line, isEnd) {
  console.log([line, isEnd].join('\t'));

Creates an instance of LineStream, with field separated by "\t".

source and options are the same as LineStream.create(source, options).

fn is called on "data" event. Three arguments are passed.

  • data (Array)
  • line (string)
  • isEnd (boolean)

data is equivalent to line.split("\t")

Other arguments are the same as original "data" event.

Returns an instance of LineStream.

Pauses the stream until all passed readable streams come to an end.

var ids = {};
var comingLines = LineStream.tsv('-', function(data, line, isEnd) { // reads from process.stdin
  var id = data[0];
  ids[id] = true;

LineStream.tsv('file1', function(data, line, isEnd) { // reads from "file1"
  var id = data[0];
  if (ids[id]) console.log(line);
.after(comingLines); // resumes after comingLines finished,