How to have a writable stream write to nowhere?
Asked Answered
A

2

11

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?

Alcaraz answered 2/9, 2015 at 17:25 Comment(1)
What's the point of using a Transform stream if you're going to ignore all of the output? Why not just use a Writable stream in the first place?Aara
S
3

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

Sonar answered 2/9, 2015 at 17:35 Comment(1)
the package looks dormant and does not support typesBowery
A
6

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()})()
}
Astrid answered 28/5, 2021 at 8:19 Comment(0)
S
3

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

Sonar answered 2/9, 2015 at 17:35 Comment(1)
the package looks dormant and does not support typesBowery

© 2022 - 2024 — McMap. All rights reserved.