Using the pipe method
A pipe is used to connect streams together. DOS and Unix-like shells use the vertical bar (|
) to pipe the output of one program to another; we chain several pipes together to process and massage data in a number of ways.
Likewise, the Streams API affords us the pipe
method to channel data through multiple streams. Every readable stream has a pipe method that expects a writable stream (the destination) as its first parameter.
In this recipe, we're going to pipe several streams together.
Getting ready
Let's create a folder called piper
, initialize it as a package, install tar-map-stream
, and create an index.js
file:
$ mkdir piper $ cd piper $ npm init -y $ npm install tar-map-stream $ touch index.js
How to do it...
In our index.js
file, let's begin by requiring the dependencies we'll be using to create various streams:
const zlib = require('zlib') const map = require('tar-map-stream')
Let's imagine we want to take the gzipped tarball of the very first available version...