Thursday, February 17, 2022

Streams - Readable, Writable

Streams are one of the most important components of Node.js 

For example, the fs module has createReadStream() for reading from a file and createWriteStream() for writing to a file, the HTTP request and response objects are essentially streams, the zlib module allows compress and decompress data using a streaming interface and, crypto module exposes streaming primitives like createCipheriv and createDecipheriv.

Every stream in Node.js is an implementation of one of the four base abstract classes. Writable, Readable, Duplex, Transform. Each stream class is also an instance of EventEmitter.

Streams support two operating modes:

  • Binary mode - buffers or strings
  • Object mode: objects

Readable streams

A Readable stream represents a source of data. There are two approaches to receive the data from a Readable stream: non-flowing (or paused) and flowing. 

Paused mode

In paused mode, you need to call read() on the stream instance repeatedly until every chunk of data has been read.

The highWaterMark property, passed as an option to fs.createReadStream, determines how much data buffers inside the stream.

All Readable streams begin in paused mode but can be switched to flowing mode in one of the following ways: 

  • Adding a 'data' event handler.
  • Calling the stream.resume() method.
  • Calling the stream.pipe() method to send the data to a Writable.

Flowing mode

You can call readable.pause() and readable.resume()

Readable streams from iterables

You can create Readable stream instances from arrays or other iterable objects using the Readable.from()

Writable streams

A Writable stream represents a data destination. The example bellow shows an elegant way to create a gziped copy of a file.

In the next post, I am going to look at Transform streams.

No comments:

Post a Comment