Sukima
Sukima

Reputation: 10084

How can I chain streams internally within a custom through2 stream

I'm writing my own through stream in Node which takes in a text stream and outputs an object per line of text. This is what the end result should look like:

fs.createReadStream('foobar')
  .pipe(myCustomPlugin());

The implementation would use through2 and event-stream to make things easy:

var es = require('event-stream');
var through = require('through2');
module.exports = function myCustomPlugin() {
  var parse = through.obj(function(chunk, enc, callback) {
    this.push({description: chunk});
    callback();
  });
  return es.split().pipe(parse);
};

However, if I were to pull this apart essentially what I did was:

fs.createReadStream('foobar')
  .pipe(
    es.split()
      .pipe(parse)
  );

Which is incorrect. Is there a better way? Can I inherit es.split() instead of use it inside the implementation? Is there an easy way to implement splits on lines without event-stream or similar? Would a different pattern work better?

NOTE: I'm intentionally doing the chaining inside the function as the myCustomPlugin() is the API interface I'm attempting to expose.

Upvotes: 2

Views: 2034

Answers (2)

user657199
user657199

Reputation:

Based on the link in the previously accepted answer that put me on the right googling track, here's a shorter version if you don't mind another module: stream-combiner (read the code to convince yourself of what's going on!)

var combiner = require('stream-combiner')
  , through = require('through2')
  , split = require('split2')

function MyCustomPlugin() {
  var parse = through(...)

  return combine( split(), parse )
}

Upvotes: 1

Kenny Au
Kenny Au

Reputation: 16

I'm working on something similar. See this solution: Creating a Node.js stream from two piped streams

var outstream = through2().on('pipe', function(source) {
    source.unpipe(this);
    this.transformStream = source.pipe(stream1).pipe(stream2);
});
outstream.pipe = function(destination, options) {
    return this.transformStream.pipe(destination, options);
};
return outstream;

Upvotes: 0

Related Questions