事件:'readable'


¥Event: 'readable'

当有数据可从流中读取时,将触发 'readable' 事件,直至配置的高水位标记 (state.highWaterMark)。实际上,它表示流在缓冲区内有新信息。如果此缓冲区中有数据,则可以调用 stream.read() 来检索该数据。此外,当到达流的末尾时,也可能触发 'readable' 事件。

¥The 'readable' event is emitted when there is data available to be read from the stream, up to the configured high water mark (state.highWaterMark). Effectively, it indicates that the stream has new information within the buffer. If data is available within this buffer, stream.read() can be called to retrieve that data. Additionally, the 'readable' event may also be emitted when the end of the stream has been reached.

const readable = getReadableStreamSomehow();
readable.on('readable', function() {
  // There is some data to read now.
  let data;

  while ((data = this.read()) !== null) {
    console.log(data);
  }
}); 

如果已经到达流的末尾,则调用 stream.read() 将返回 null 并触发 'end' 事件。如果从未读取任何数据,则也是如此。例如,在以下示例中,foo.txt 是一个空文件:

¥If the end of the stream has been reached, calling stream.read() will return null and trigger the 'end' event. This is also true if there never was any data to be read. For instance, in the following example, foo.txt is an empty file:

const fs = require('node:fs');
const rr = fs.createReadStream('foo.txt');
rr.on('readable', () => {
  console.log(`readable: ${rr.read()}`);
});
rr.on('end', () => {
  console.log('end');
}); 

运行此脚本的输出是:

¥The output of running this script is:

$ node test.js
readable: null
end 

在某些情况下,为 'readable' 事件绑定监听器会导致一些数据被读入内部缓冲区。

¥In some cases, attaching a listener for the 'readable' event will cause some amount of data to be read into an internal buffer.

一般来说,readable.pipe()'data' 事件机制比 'readable' 事件更容易理解。但是,处理 'readable' 可能会导致吞吐量增加。

¥In general, the readable.pipe() and 'data' event mechanisms are easier to understand than the 'readable' event. However, handling 'readable' might result in increased throughput.

如果同时使用 'readable''data',则 'readable' 优先控制流,即只有在调用 stream.read() 时才会触发 'data'readableFlowing 属性将变为 false。如果在删除 'readable' 时有 'data' 监听器,流将开始流动,即 'data' 事件将在不调用 .resume() 的情况下触发。

¥If both 'readable' and 'data' are used at the same time, 'readable' takes precedence in controlling the flow, i.e. 'data' will be emitted only when stream.read() is called. The readableFlowing property would become false. If there are 'data' listeners when 'readable' is removed, the stream will start flowing, i.e. 'data' events will be emitted without calling .resume().