Use pumpify to expose pipelines

When writing pipelines, especially as part of a module, we might want to expose these pipelines to a user as a single entity.

So how do we do that? As described in the main recipe, a pipeline consists of a series of transform streams. We write data to the first stream in the pipeline and the data flows through it until it is written to the final stream.

Let's consider the following:

  pump(stream1, stream2, stream3) 

If we were to expose the preceding pipeline to a user, we would need to both return stream1 and stream3; stream1 is the stream a user should write the pipeline data to and stream3 is the stream the user should read the pipeline results from.

Since we only need to write to stream1 and only read from stream3, we could just combine two streams into a new duplex stream that would then represent the entire pipeline.

The npm module pumpify does exactly this.

Let's create a folder called pumpified-pipeline, initialize it as a package, install pumpify, base64-encode-stream, and create an index.js:

$ mkdir pumpified-pipeline 
$ cd pumpified-pipeline
$ npm init -y
$ npm install --save pumpify base64-encode-stream
$ touch index.js

At the top of index.js, we'll write:

const { createGzip } = require('zlib') 
const { createCipher } = require('crypto')
const pumpify = require('pumpify')
const base64 = require('base64-encode-stream')

function pipeline () {
const stream1 = createGzip()
const stream2 = createCipher('aes192', 'secretz')
const stream3 = base64()
return pumpify(stream1, stream2, stream3)
}

Now we'll use our pipeline. At the end of index.js we add:

const pipe = pipeline() 

pipe.end('written to stream1')

pipe.on('data', (data) => {
console.log('stream3 says: ', data.toString())
})

pipe.on('finish', () => {
console.log('all data was successfully flushed to stream3')
})
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset