A stream is used in many parts of the Nodejs, and the flow is a very common concept, an HTTP request, and the console input and output forms are streams. The flow can be divided into three types:
- Readable stream
- Writable stream
- Able to read and write
The third stream can be divided into full-duplex stream duplex and conversion stream transform, in addition, all flows are eventemitter instances, that is, the ability to send events and handle events.
Readable stream readable stream
Readable streams can output data, and common readable streams are:
- HTTP requests and Responses
- Read the file
- Compression decompression
- Encrypted decryption
- TCP Sockect
- Process input
The readable stream is divided into two modes: streaming and non-streaming, the difference being that the former will ensure that the data is available as soon as possible, while the latter is only available until you have actively invoked stream.read ().
As I said, all flows are instances of eventemitter, and there are also some built-in events, Readablestream events include:
- Readable: The data in the stream is ready
- Data: Flowing mode is available.
- End: There's no more data.
- Close: triggered when a resource such as file descriptor is closed
- Error: A failure occurred while receiving data
There are also some method interfaces:
- Read ([size]): The active pull of certain data, if there is no data, then returns NULL, if the size does not pass, then all available data is returned. This method can only be called in non-flowing mode
- Setencoding (Encoding): What encoding format to use for parsing
- Pause (): Stops sending the data event, and the new information is persisted to the internal buffer
- Resume (): For the last event, restore the data event sent
- Pipe (destination, [option]): Pulls the data from the stream and writes it to the destination stream, because the destination stream is returned, so chained operations can be used; By default, the write stream is closed after the read stream is closed
- Unpipe ([destination]): Removes the Write rule set by the pipe method and removes all streams if not passed in destination
- Unshift (chunk): it can be imagined that the spit out of the thing to eat again, here is to read out some or all of the content into the readable stream again
- Wrap (Stream): wrapping old-fashioned streams
Writable Stream writable Stream
Similarly, a writable stream has two parts, an event and a method, which include:
- Drain: Indicates that the data is not finished, the Write method call returns false
- Finish: All the data is written
- Pipe: When a read stream calls the pipe method to point to the current write stream
- Unpipe: When a read stream calls the Unpipe method to revoke the current write stream
- Error: When writing errors occurred
Method:
- Write (Chunk,[encoding],[callback]): Writes the data, returns False when the data must be buffered internally
- End ([Chunk],[encoding],[callback]): After writing, the new write is terminated
Read/write Stream
Duplex: Full-duplex stream, readable and writable
Transform: Reads stream data from input and writes to the output stream after processing
Nodejs in the stream