Creating a Node.js stream from two piped streams

后端 未结 2 2025
礼貌的吻别
礼貌的吻别 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.

提交回复
热议问题