Using through2

In notebook:
FrontEndMasters Networking and Streams
Created at:
backend Node JS JavaScript libraries

through2 vs stream.Transform

you can use either version:

  • through2(opts={...}, write, end) or
  • new Transform({ transform: write, flush: end, ... })

through(write, end)

With through there are 2 parameters: write and end. Both are optional.

  • function write (buf, enc, next) {}
  • function end () {}

Call next() when you're ready for the next chunk. If you don't call next(), your stream will hang!

Call this.push(VALUE) inside the callback to put VALUE into the stream's output.

Use a VALUE of null to end the stream.

about the next function:

Not every piece of data might correspond to an output

In this case you would do:


For example with gzip compression you want to buffer up stuff to the buffer size. So you call next() a couple of times, until you have enough data.

To understand better:

next(null, buf.toString().toUpperCase())
// is the same as ↴

So push provides output. If you push(null) or next(null), you send an end signal. You're telling the downstream consumers that your stream is finished.


If you don't give through any arguments, these are the default values for write and end:

  • function write (buf, enc, next) { this.push(buf); next() }
  • function end () { this.push(null) }

This means that through() with no arguments will pass everything written as input directly through to its output.

You would use it with an API that expects a stream and you need to pass your data through.