首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从两个管道流创建一个Node.js流

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

Stack Overflow用户
提问于 2013-07-04 21:32:25
回答 2查看 9.7K关注 0票数 20

如果可能的话,我想通过管道将两个Node.js流合并为一个流。我使用的是Transform streams。

换句话说,我希望我的库返回myStream供人们使用。例如,他们可以这样写:

代码语言:javascript
复制
process.stdin.pipe(myStream).pipe(process.stdout);

在内部,我使用了一个插入到myInternalStream中我自己的逻辑中的第三方vendorStream来完成一些工作。因此,上面的内容将转化为:

代码语言:javascript
复制
process.stdin.pipe(vendorStream).pipe(myInternalStream).pipe(process.stdout);

我能做这样的事吗?我尝试过var myStream = vendorStream.pipe(myInternalStream),但显然不起作用。

为了与bash进行类比,假设我想编写一个程序来检查某个流(tail -n 1 | grep h)的最后一行中是否存在字母h,我可以创建一个shell脚本:

代码语言:javascript
复制
# myscript.sh
tail -n 1 | grep h

如果人们这样做了:

代码语言:javascript
复制
$ printf "abc\ndef\nghi" | . myscript.sh

它就是这样的。

这就是我到目前为止所知道的:

代码语言:javascript
复制
// Combine a pipe of two streams into one stream

var util = require('util')
  , Transform = require('stream').Transform;

var chunks1 = [];
var stream1 = new Transform();
var soFar = '';
stream1._transform = function(chunk, encoding, done) {
  chunks1.push(chunk.toString());
  var pieces = (soFar + chunk).split('\n');
  soFar = pieces.pop();
  for (var i = 0; i < pieces.length; i++) {
    var piece = pieces[i];
    this.push(piece);
  }
  return done();
};

var chunks2 = [];
var count = 0;
var stream2 = new Transform();
stream2._transform = function(chunk, encoding, done) {
  chunks2.push(chunk.toString());
  count = count + 1;
  this.push(count + ' ' + chunk.toString() + '\n');
  done();
};

var stdin = process.stdin;
var stdout = process.stdout;

process.on('exit', function () {
    console.error('chunks1: ' + JSON.stringify(chunks1));
    console.error('chunks2: ' + JSON.stringify(chunks2));
});
process.stdout.on('error', process.exit);


// stdin.pipe(stream1).pipe(stream2).pipe(stdout);

// $ (printf "abc\nd"; sleep 1; printf "ef\nghi\n") | node streams-combine.js
// Outputs:
// 1 abc
// 2 def
// 3 ghi
// chunks1: ["abc\nd","ef\nghi\n"]
// chunks2: ["abc","def","ghi"]

// Best working solution I could find
var stream3 = function(src) {
  return src.pipe(stream1).pipe(stream2);
};
stream3(stdin).pipe(stdout);

// $ (printf "abc\nd"; sleep 1; printf "ef\nghi\n") | node streams-combine.js
// Outputs:
// 1 abc
// 2 def
// 3 ghi
// chunks1: ["abc\nd","ef\nghi\n"]
// chunks2: ["abc","def","ghi"]

这有可能吗?如果我要做的事情不清楚,请让我知道。

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-05 02:48:34

您可以查看要通过管道传输到流的内容,然后使用unpipe将其传输到您感兴趣的流:

代码语言:javascript
复制
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);

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

代码语言:javascript
复制
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);
票数 28
EN

Stack Overflow用户

发布于 2016-08-05 18:35:05

一种选择可能是使用multipipe,它允许您将多个转换链接在一起,包装为单个转换流:

代码语言:javascript
复制
// my-stream.js
var multipipe = require('multipipe');

module.exports = function createMyStream() {
  return multipipe(vendorStream, myInternalStream);
};

然后您可以执行以下操作:

代码语言:javascript
复制
var createMyStream = require('./my-stream');

var myStream = createMyStream();

process.stdin.pipe(myStream).pipe(process.stdout);

澄清:这使标准输入经过vendorStream,然后是myInternalStream,最后是标准输出。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17471659

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档