[英]NodeJS streams - a leaky pipe?
I'm doing a lot of work with NodeJS streams at the moment. 目前,我正在处理NodeJS流。
One thing I find myself needing is a 'leaky pipe'. 我发现自己需要的一件事是“漏水的管道”。
Like stream.PassThrough
, but which just drops data if (and only if) it has no where to send it. 像stream.PassThrough
一样,但仅在(且仅当)无处发送数据时才丢弃数据。
Does such a thing already exist? 这样的事情已经存在了吗?
Is there a way of finding out (within a stream.Transform
) how many downstream pipes are connected? 有没有办法找出(在stream.Transform
)连接了多少个下游管道?
I eventually came up with a solution: 我最终想出了一个解决方案:
LeakyTransform.prototype._transform = function(chunk, encoding, done) {
if (this._readableState.pipesCount > 0) {
this.push(chunk);
}
done();
}
PassThrough implements _transfrom as following: PassThrough实现_transfrom的方法如下:
PassThrough.prototype._transform = function(chunk, encoding, cb) {
cb(null, chunk);
};
I think what you need can be implemented like this: 我认为您需要的可以这样实现:
Leak.prototype._transform = function(chunk, encoding, cb) {
};
ie no-op 即无操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.