缓冲


【Buffering】

WritableReadable 流都会将数据存储在内部缓冲区中。

【Both Writable and Readable streams will store data in an internal buffer.】

潜在缓存的数据量取决于传递给流构造函数的 highWaterMark 选项。对于普通流,highWaterMark 选项指定一个 字节总数。对于以对象模式运行的流,highWaterMark 指定对象的总数量。对于处理(但不解码)字符串的流,highWaterMark 指定 UTF-16 代码单元的总数量。

【The amount of data potentially buffered depends on the highWaterMark option passed into the stream's constructor. For normal streams, the highWaterMark option specifies a total number of bytes. For streams operating in object mode, the highWaterMark specifies a total number of objects. For streams operating on (but not decoding) strings, the highWaterMark specifies a total number of UTF-16 code units.】

当实现调用 stream.push(chunk) 时,数据会在 Readable 流中被缓冲。如果流的使用者不调用 stream.read(),数据将一直保存在内部队列中,直到被消费。

【Data is buffered in Readable streams when the implementation calls stream.push(chunk). If the consumer of the Stream does not call stream.read(), the data will sit in the internal queue until it is consumed.】

一旦内部读取缓冲区的总大小达到 highWaterMark 指定的阈值,流将暂时停止从底层资源读取数据,直到当前缓冲的数据被消耗(也就是说,流将停止调用用于填充读取缓冲区的内部 readable._read() 方法)。

【Once the total size of the internal read buffer reaches the threshold specified by highWaterMark, the stream will temporarily stop reading data from the underlying resource until the data currently buffered can be consumed (that is, the stream will stop calling the internal readable._read() method that is used to fill the read buffer).】

当反复调用 [writable.write(chunk)][stream-write] 方法时,数据会被缓存在 Writable 流中。当内部写入缓冲区的总大小低于 highWaterMark 设置的阈值时,调用 writable.write() 将返回 true。一旦内部缓冲区的大小达到或超过 highWaterMark,则会返回 false

【Data is buffered in Writable streams when the writable.write(chunk) method is called repeatedly. While the total size of the internal write buffer is below the threshold set by highWaterMark, calls to writable.write() will return true. Once the size of the internal buffer reaches or exceeds the highWaterMark, false will be returned.】

stream API 的一个主要目标,特别是 stream.pipe() 方法,是将数据缓冲限制在可接受的水平,以便不同速度的来源和目的地不会超出可用内存的承载能力。

【A key goal of the stream API, particularly the stream.pipe() method, is to limit the buffering of data to acceptable levels such that sources and destinations of differing speeds will not overwhelm the available memory.】

highWaterMark 选项是一个阈值,而不是限制:它决定了流在停止请求更多数据之前缓存的数据量。它通常不会强制执行严格的内存限制。具体的流实现可能会选择执行更严格的限制,但这是可选的。

【The highWaterMark option is a threshold, not a limit: it dictates the amount of data that a stream buffers before it stops asking for more data. It does not enforce a strict memory limitation in general. Specific stream implementations may choose to enforce stricter limits but doing so is optional.】

因为 DuplexTransform 流都是 ReadableWritable,每个都维护着两个独立的内部缓冲区,用于读取和写入,这使得每一侧可以独立于另一侧操作,同时保持适当且高效的数据流。例如,net.Socket 实例是 Duplex 流,它的 Readable 端允许消费从套接字接收的数据,而它的 Writable 端允许向套接字写入数据。由于写入套接字的数据速率可能比接收数据的速率快或慢,每一侧都应当独立于另一侧进行操作(并缓冲)。

【Because Duplex and Transform streams are both Readable and Writable, each maintains two separate internal buffers used for reading and writing, allowing each side to operate independently of the other while maintaining an appropriate and efficient flow of data. For example, net.Socket instances are Duplex streams whose Readable side allows consumption of data received from the socket and whose Writable side allows writing data to the socket. Because data may be written to the socket at a faster or slower rate than data is received, each side should operate (and buffer) independently of the other.】

内部缓冲的机制是内部实现细节,可能会随时更改。然而,对于某些高级实现,可以使用 writable.writableBufferreadable.readableBuffer 来获取内部缓冲。不建议使用这些未记录的属性。

【The mechanics of the internal buffering are an internal implementation detail and may be changed at any time. However, for certain advanced implementations, the internal buffers can be retrieved using writable.writableBuffer or readable.readableBuffer. Use of these undocumented properties is discouraged.】