Creating transform streams
Streams allow for asynchronous programming. The most common stream is the transform stream; it's a black box that takes input and produces output asynchronously.
In this recipe, we'll look at creating a transform stream with the through2
module. In the There's more section, we'll look at how to create streams with the core stream
module.
Getting ready
Let's create a folder called through-streams
with an index.js
, initialize the folder as a package, and install through2
:
$ mkdir through-streams $ cd through-streams $ npm init -y $ npm install through2 $ touch index.js
Note
Why the 2?
The through2
module is a successor to the through
module. The through
module was built against an earlier Node core streams API (retrospectively called Streams 1 API). Later versions of Node introduced Streams 2 (and indeed 3). The through2
module was written to use the superior Streams 2 API (and is still relevant for the Streams 3 API; there's no need for a through3
!). In fact, any...