Creating a Node.js stream from two piped streams

后端 未结 2 2023
礼貌的吻别
礼貌的吻别 2020-12-08 15:29

I\'d like to combine two Node.js streams into one by piping them, if possible. I\'m using Transform streams.

In other words, I\'d like my library to return myS

相关标签:
2条回答
  • 2020-12-08 16:29

    One option is perhaps to use multipipe which lets you chain multiple transforms together, wrapped as a single transform stream:

    // my-stream.js
    var multipipe = require('multipipe');
    
    module.exports = function createMyStream() {
      return multipipe(vendorStream, myInternalStream);
    };
    

    Then you can do:

    var createMyStream = require('./my-stream');
    
    var myStream = createMyStream();
    
    process.stdin.pipe(myStream).pipe(process.stdout);
    

    Clarification: This makes stdin go through vendorStream, then myInternalStream and finally stdout.

    0 讨论(0)
  • 2020-12-08 16:31

    You can watch for something to be piped to your stream, and then unpipe it and pipe it to the streams you're interested in:

    var PassThrough = require('stream').PassThrough;
    
    var stream3 = new PassThrough();
    
    // When a source stream is piped to us, undo that pipe, and save
    // off the source stream piped into our internally managed streams.
    stream3.on('pipe', function(source) {
      source.unpipe(this);
      this.transformStream = source.pipe(stream1).pipe(stream2);
    });
    
    // When we're piped to another stream, instead pipe our internal
    // transform stream to that destination.
    stream3.pipe = function(destination, options) {
      return this.transformStream.pipe(destination, options);
    };
    
    stdin.pipe(stream3).pipe(stdout);
    

    You can extract this functionality into your own constructable stream class:

    var util = require('util');
    var PassThrough = require('stream').PassThrough;
    
    var StreamCombiner = function() {
      this.streams = Array.prototype.slice.apply(arguments);
    
      this.on('pipe', function(source) {
        source.unpipe(this);
        for(i in this.streams) {
          source = source.pipe(this.streams[i]);
        }
        this.transformStream = source;
      });
    };
    
    util.inherits(StreamCombiner, PassThrough);
    
    StreamCombiner.prototype.pipe = function(dest, options) {
      return this.transformStream.pipe(dest, options);
    };
    
    var stream3 = new StreamCombiner(stream1, stream2);
    stdin.pipe(stream3).pipe(stdout);
    
    0 讨论(0)
提交回复
热议问题