【问题标题】:How to have a writable stream write to nowhere?如何让可写流写入无处?
【发布时间】:2015-11-28 08:04:48
【问题描述】:

我的问题是我设置了一个流管道。

readableStream.pipe(transformStream).pipe(writableStream);

我真的不希望可写流在任何地方写入,我只是使用它以便我的转换流的缓冲区不会得到备份。我怎样才能让可写流写入一个空的目的地?

【问题讨论】:

  • 如果您要忽略所有输出,那么使用转换流有什么意义?为什么不首先使用 Writable 流?

标签: javascript node.js stream


【解决方案1】:

你可以把它写到 /dev/null,有一个 npm 包对应 https://www.npmjs.com/package/dev-null

【讨论】:

    【解决方案2】:

    有很多方法...其中一些会导致节点崩溃并导致您报告的错误异常...不要在没有相应阅读器的情况下使用 PassThrough。并且一定要调用回调。

    // hellSpawn: 268MiB/s. RSS 298
    // write2Null: 262MiB/s. RSS 138
    // passThrough: 259MiB/s. RSS 2667
    // noCb: 256MiB/s. RSS 2603
    // fancy: 256MiB/s. RSS 106
    
    {
          hellSpawn: (childProcess.spawn('sh', ['-c', 'cat > /dev/null'])).stdin,
          write2Null: fs.createWriteStream('/dev/null'),
          passThrough: new PassThrough(),
          noCb: new (class extends Writable {_write = () => {}})(),
          fancy: new  Writable ({write: (a,b,cb) => cb()})()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-30
      • 2021-09-15
      • 2013-10-03
      • 2014-03-13
      • 1970-01-01
      • 2014-02-24
      • 2016-07-30
      相关资源
      最近更新 更多