- Node Cookbook(Third Edition)
- David Mark Clements Matthias Buus Matteo Collina Peter Elger
- 233字
- 2024-10-29 20:27:03
Types of stream
If we want to make a stream that provides data for other users to read, we need to make a readable stream. An example of a readable stream could be a stream that reads data from a file stored on disk.
If we want to make a stream that other users can write data to, we need to make a writable stream. An example of a writable stream could be a stream that writes data to a file stored on disk.
Node core provides base implementations of all these variations of streams that we can extend to support various use cases. We can use node -p require('stream') as a convenient way to take look at available stream implementations.
Sometimes you want to make a stream that is both readable and writable at the same time. We call these duplex streams. An example of a duplex stream could be a TCP network stream that both allows us to read data from the network and write data back at the same time.
A special case of a duplex stream is a stream that transforms the data being written to it and makes the transformed data available to read out of the stream. We call these transform streams. An example of a transform stream could be a GZIP stream that compresses the input data written to it.