【发布时间】:2023-03-19 20:30:02
【问题描述】:
我目前正在使用 NodeJS 流做很多工作。
我发现自己需要一个“漏水管道”。
与stream.PassThrough 类似,但仅当(且仅当)它无处可发送数据时才会丢弃数据。
这样的东西已经存在了吗?
有没有办法找出(在stream.Transform 内)连接了多少下游管道?
【问题讨论】:
标签: node.js stream pipe memory-leaks
我目前正在使用 NodeJS 流做很多工作。
我发现自己需要一个“漏水管道”。
与stream.PassThrough 类似,但仅当(且仅当)它无处可发送数据时才会丢弃数据。
这样的东西已经存在了吗?
有没有办法找出(在stream.Transform 内)连接了多少下游管道?
【问题讨论】:
标签: node.js stream pipe memory-leaks
我最终想出了一个解决方案:
LeakyTransform.prototype._transform = function(chunk, encoding, done) {
if (this._readableState.pipesCount > 0) {
this.push(chunk);
}
done();
}
【讨论】:
PassThrough 实现 _transfrom 如下:
PassThrough.prototype._transform = function(chunk, encoding, cb) {
cb(null, chunk);
};
我认为你需要的可以这样实现:
Leak.prototype._transform = function(chunk, encoding, cb) {
};
即无操作
【讨论】:
/dev/null。