Node stream has 4 types: 1. Readable (readable stream). The "_read" method needs to be implemented to return content; 2. Writable (writable stream), the "_write" method needs to be implemented to accept content; 3. Duplex (readable and writable stream), the "_read" and "_write" methods need to be implemented To accept and return content; 4. Transform (conversion stream), you need to implement the "_transform" method to convert the received content and return the content.
The operating environment of this tutorial: Windows 7 system, nodejs version 16, DELL G3 computer.
Stream is a very basic concept in Nodejs. Many basic modules are implemented based on streams and play a very important role. At the same time, flow is also a very difficult concept to understand. This is mainly due to the lack of relevant documentation. For NodeJs beginners, it often takes a lot of time to understand flow before they can truly master this concept. Fortunately, for most NodeJs, it is For users, it is only used to develop Web applications. Insufficient understanding of streams does not affect their use. However, understanding streams can lead to a better understanding of other modules in NodeJs, and in some cases, using streams to process data will have better results.
Stream is an abstract interface for processing streaming data in Node.js. Stream is not an actual interface, but a general term for all streams. The actual interfaces are ReadableStream, WritableStream, and ReadWriteStream.
interface ReadableStream extends EventEmitter { readable: boolean; read(size?: number): string | Buffer; setEncoding(encoding: BufferEncoding): this; pause(): this; resume(): this; isPaused(): boolean; pipe<T extends WritableStream>(destination: T, options?: { end?: boolean | undefined; }): T; unpipe(destination?: WritableStream): this; unshift(chunk: string | Uint8Array, encoding?: BufferEncoding): void; wrap(oldStream: ReadableStream): this; [Symbol.asyncIterator](): AsyncIterableIterator<string | Buffer>; } interface WritableStream extends EventEmitter { writable: boolean; write(buffer: Uint8Array | string, cb?: (err?: Error | null) => void): boolean; write(str: string, encoding?: BufferEncoding, cb?: (err?: Error | null) => void): boolean; end(cb?: () => void): this; end(data: string | Uint8Array, cb?: () => void): this; end(str: string, encoding?: BufferEncoding, cb?: () => void): this; } interface ReadWriteStream extends ReadableStream, WritableStream { }
It can be seen that ReadableStream and WritableStream are both interfaces that inherit the EventEmitter class (interfaces in ts can inherit classes, because they are only merging types).
The implementation classes corresponding to the above interfaces are Readable, Writable and Duplex respectively
There are 4 types of streams in NodeJs:
Readable readable stream (implements ReadableStream)
Writable writable stream (implements WritableStream)
Duplex readable and writable stream (inherits Implement WritableStream after Readable)
Transform conversion stream (inherit Duplex)
They all have methods to be implemented:
Readable needs to implement the _read method to return the content
Writable needs to implement the _write method to accept the content
Duplex needs Implement the _read and _write methods to accept and return content
Transform needs to implement the _transform method to convert the received content and return it
Readable stream (Readable) is a type of stream. It has two modes and three states
Two reading modes:
Flow mode: Data will be read and written from the underlying system to the buffer. When the buffer is full, the data will automatically be passed to the registered event handler as quickly as possible through EventEmitter
Pause mode: In this mode, EventEmitter will not be actively triggered to transmit data. The Readable.read()
method must be explicitly called to read data from the buffer. read will trigger a response to EventEmitter event.
Three states:
readableFlowing === null (initial state)
readableFlowing === false (pause mode)
readableFlowing === true (flowing mode)
Initially flowing readable. readableFlowing
becomes true after adding data event for null
. When pause()
, unpipe()
is called, or back pressure is received or a readable
event is added, readableFlowing
will be set to false ,In this state, binding a listener to the data event will not switch readableFlowing to true.
Call resume()
to switch the readableFlowing
of the readable stream to true
Remove all readable events to enable readableFlowing The only way to become null.
Event name | Description |
---|---|
Triggered when there is new readable data in the buffer (triggered every time a node is inserted into the cache pool) | |
It will be triggered every time after consuming data Triggered, the parameter is the data consumed this time | |
Triggered when the stream is closed | |
Triggered when an error occurs in the stream |
Description | |
---|---|
Consume data with a length of size. Return null to indicate that the current data is less than size. Otherwise, return the data consumed this time. When size is not passed, it means consuming all the data in the cache pool |