The Stream docs state that Duplex Streams "are streams that implement both the Readable and Writable interfaces" and Transform Streams "are Duplex streams where the output is in some way computed from the input." Unfortunately, the docs do not describe what Transform streams provide above and beyond Duplex streams.
Are there any differences between the two? When would you use one over the other?
The difference is just syntactic sugar. Transform streams are full duplex streams but rather than implementing
_write
and_read
methods, you are asked to implement just the_transform
method. You can read more about streams on the excellent substack's streams guide or from Isaacs's readable-stream repo.A Duplex stream can be thought of a readable stream with a writable stream. Both are independent and each have separate internal buffer. The reads and writes events happen independently.
A Transform stream is a duplex where the read and write takes place in a causal way. The end points of the duplex stream are linked via some transform. Read requires write to have occurred.
If you read the API for stream implementors section of the docs, they state that some possible use cases for Duplex and Transform streams are "Reading and writing" and "Operate on written data, then read the result" respectively.
Simply put the Transform stream lets you implement the
_transform
method that takes some input and returns the output after some operation has been done on the data, and can be used for things like compressing or hashing, whereas the Duplex stream can be used for things like a TCP socket connection where you just send and receive data.