如何将可写流写入无处?

How to have a writable stream write to nowhere?

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

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

我真的不想让可写流写到任何地方,我只是在使用它,这样我的转换流的缓冲区就不会得到备份。如何让可写流写入空目的地?

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

有很多方法...其中一些会导致节点崩溃并导致节点错误报告器崩溃...为避免这种情况,请不要在没有相应 reader 的情况下使用 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()})()
}