...

/

Implementing Transform Streams

Implementing Transform Streams

Learn about the implementation of Transform streams and the use of simplified construction for transform streams.

Transform streams

Transform streams are a special kind of Duplex stream that are specifically designed to handle data transformations. For example, the functions zlib.createGzip() and crypto.createCipheriv() create Transform streams for compression and encryption, respectively.
In a simple Duplex stream, there’s no immediate relationship between the data read from the stream and the data written into it (at least, the stream is agnostic to such a relationship). Think about a TCP socket, which just sends and receives data to and from the remote peer; the socket is not aware of any relationship between the input and output. The illustration given below shows the data flow in a Duplex stream.

Press + to interact
Duplex stream schematic representation
Duplex stream schematic representation

On the other hand, Transform streams apply some kind of transformation to each chunk of data that they receive from their Writable side, and then make the transformed data available on their Readable side. The illustration given below shows how the data flows in a Transform stream.

Press + to interact
 Transform stream schematic representation
Transform stream schematic representation

From the outside, the interface of a Transform ...