我正在Node中编写自己的直通流,它接收文本流并输出每一行文本的对象。最终结果应该是这样的:
fs.createReadStream('foobar')
.pipe(myCustomPlugin());该实现将使用through2和event-stream来简化操作:
var es = require('event-stream');
var through = require('through2');
module.exports = function myCustomPlugin() {
var parse = through.obj(function(chunk, enc, callback) {
this.push({description: chunk});
callback();
});
return es.split().pipe(parse);
};但是,如果我要把它拆开的话,我所做的就是:
fs.createReadStream('foobar')
.pipe(
es.split()
.pipe(parse)
);这是不正确的。有更好的办法吗?我可以继承es.split()而不是在实现中使用它吗?是否有一种简单的方法可以在没有事件流或类似事件的情况下在线实现拆分?另一种模式会更好吗?
注意:我有意在函数中进行链接,因为myCustomPlugin()是我试图公开的API接口。
发布于 2014-11-17 17:11:23
我也在做类似的事情。请参阅此解决方案:从两个管道流创建Node.js流
var outstream = through2().on('pipe', function(source) {
source.unpipe(this);
this.transformStream = source.pipe(stream1).pipe(stream2);
});
outstream.pipe = function(destination, options) {
return this.transformStream.pipe(destination, options);
};
return outstream;发布于 2015-07-16 11:42:47
https://stackoverflow.com/questions/26856874
复制相似问题