从两个管道流创建Node.js流

从两个管道流创建Node.js流,第1张

从两个管道流创建Node.js流

您可以查看要传送到流中的内容,然后将其传送到您

unpipe
感兴趣的流中:

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);

您可以将此功能提取到自己的可构造流类中:

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);


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5614884.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-15
下一篇2022-12-15

发表评论

登录后才能评论

评论列表(0条)

    保存