readable.push(chunk[, encoding])


chunk<Buffer><TypedArray><DataView><string> 时,chunk 条数据将被添加到内部队列中供流的用户消费。将 chunk 作为 null 传递表示流结束 (EOF),之后无法写入更多数据。

¥When chunk is a <Buffer>, <TypedArray>, <DataView> or <string>, the chunk of data will be added to the internal queue for users of the stream to consume. Passing chunk as null signals the end of the stream (EOF), after which no more data can be written.

Readable 运行在 paused 模式时,在 'readable' 事件触发时调用 readable.read() 方法可以读出 readable.push() 添加的数据。

¥When the Readable is operating in paused mode, the data added with readable.push() can be read out by calling the readable.read() method when the 'readable' event is emitted.

Readable 在流动模式下运行时,添加了 readable.push() 的数据将通过触发 'data' 事件来传递。

¥When the Readable is operating in flowing mode, the data added with readable.push() will be delivered by emitting a 'data' event.

readable.push() 方法被设计为尽可能灵活。例如,当封装提供某种形式的暂停/恢复机制和数据回调的底层源时,底层源可以由自定义 Readable 实例封装:

¥The readable.push() method is designed to be as flexible as possible. For example, when wrapping a lower-level source that provides some form of pause/resume mechanism, and a data callback, the low-level source can be wrapped by the custom Readable instance:

// `_source` is an object with readStop() and readStart() methods,
// and an `ondata` member that gets called when it has data, and
// an `onend` member that gets called when the data is over.

class SourceWrapper extends Readable {
  constructor(options) {
    super(options);

    this._source = getLowLevelSourceObject();

    // Every time there's data, push it into the internal buffer.
    this._source.ondata = (chunk) => {
      // If push() returns false, then stop reading from source.
      if (!this.push(chunk))
        this._source.readStop();
    };

    // When the source ends, push the EOF-signaling `null` chunk.
    this._source.onend = () => {
      this.push(null);
    };
  }
  // _read() will be called when the stream wants to pull more data in.
  // The advisory size argument is ignored in this case.
  _read(size) {
    this._source.readStart();
  }
} 

readable.push() 方法用于将内容推入内部缓冲区。可以用 readable._read() 方法驱动。

¥The readable.push() method is used to push the content into the internal buffer. It can be driven by the readable._read() method.

对于非对象模式运行的流,如果 readable.push()chunk 参数为 undefined,将被视为空字符串或缓冲区。有关详细信息,请参阅 readable.push('')

¥For streams not operating in object mode, if the chunk parameter of readable.push() is undefined, it will be treated as empty string or buffer. See readable.push('') for more information.