RadleyMith
RadleyMith

Reputation: 1343

How to have a writable stream write to nowhere?

My issue here is that I have a stream pipeline set up like.

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

I don't really want the writable stream to write anywhere, I'm just using it so that the buffer of my transform stream doesn't get backed up. How could I make the writable stream write to an empty destination?

Upvotes: 12

Views: 6226

Answers (2)

Ray Foss
Ray Foss

Reputation: 3883

There are lots of ways... some of them will crash node and cause the node bug reporter to crash... To avoid this, don't use PassThrough without a corresponding reader, be sure to call the callback.

// 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()})()
}

Upvotes: 8

Yuri Zarubin
Yuri Zarubin

Reputation: 11677

You can write it to /dev/null, there's an npm package for that https://www.npmjs.com/package/dev-null

Upvotes: 3

Related Questions