Search code examples
node.jsnode.js-stream

Node.js. How to pass a several streams (array or chain) into pipe?


I have a function returning a several streams:

// function 
var s1 = through.obj({highWaterMark: 2}, function(data, enc, next) {
    console.log( "A: "+data );
    next(null, "A");
});

var s2 = through.obj({highWaterMark: 2}, function(data, enc, next) {
    console.log( "B: "+data );
    next(null, "B");
});

return [s1, s2]; 
// or chain:
// s1.pipe(s2);
// return s1;

I want to pass the result of function into pipe:

gulp.src(...).pipe(...).pipe( getStreams() ).pipe(gulp.dest...);

Is there way to do such?

Or maybe I can do it somethig like the next?

var s1 = getStreams();
gulp.src(...).pipe(...).pipe( s1 )
s1.getLastStreamOfChain().pipe(gulp.dest...);

Solution

  • I found a few ways to do this. 1)

    s1.pipe(s2);
    s1.lastStreamOfChain = s2;
    
    stream.pipe(s1).lastStreamOfChain.pipe(s3); // Of course it is not good
    

    2) Use the stream-combiner2.

    combine.obj(s1, s2);
    

    is there any way to reuse a chain of pipe transformations in NodeJS?