【问题标题】:NodeJS streams - a leaky pipe?NodeJS 流 - 一个泄漏的管道?
【发布时间】:2023-03-19 20:30:02
【问题描述】:

我目前正在使用 NodeJS 流做很多工作。

我发现自己需要一个“漏水管道”。

stream.PassThrough 类似,但仅当(且仅当)它无处可发送数据时才会丢弃数据。

这样的东西已经存在了吗?

有没有办法找出(在stream.Transform 内)连接了多少下游管道?

【问题讨论】:

    标签: node.js stream pipe memory-leaks


    【解决方案1】:

    我最终想出了一个解决方案:

    LeakyTransform.prototype._transform = function(chunk, encoding, done) {
      if (this._readableState.pipesCount > 0) {
        this.push(chunk);
      }
      done();
    }
    

    【讨论】:

      【解决方案2】:

      PassThrough 实现 _transfrom 如下:

      PassThrough.prototype._transform = function(chunk, encoding, cb) {
        cb(null, chunk);
      };
      

      我认为你需要的可以这样实现:

      Leak.prototype._transform = function(chunk, encoding, cb) {
      };
      

      即无操作

      【讨论】:

      • 谢谢,但是当“泄漏的管道”被输送到某物时,它应该不再泄漏。我不是在寻找/dev/null
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-22
      • 2017-04-21
      相关资源
      最近更新 更多