readable.push(chunk[, encoding])


  • chunk <Buffer> | <Uint8Array> | <string> | <null> | <any> 要推入读取队列的数据块。对于不在对象模式下操作的流,chunk 必须是字符串、BufferUint8Array。对于对象模式流,chunk 可以是任何 JavaScript 值。

    ¥chunk <Buffer> | <Uint8Array> | <string> | <null> | <any> Chunk of data to push into the read queue. For streams not operating in object mode, chunk must be a string, Buffer or Uint8Array. For object mode streams, chunk may be any JavaScript value.

  • encoding <string> 字符串块的编码。必须是有效的 Buffer 编码,例如 'utf8''ascii'

    ¥encoding <string> Encoding of string chunks. Must be a valid Buffer encoding, such as 'utf8' or 'ascii'.

  • 返回:<boolean> true 如果可以继续推送额外的数据块;false 否则。

    ¥Returns: <boolean> true if additional chunks of data may continue to be pushed; false otherwise.

chunkBufferUint8Arraystring 时,会将数据的 chunk 加入内部队列,供流用户消费。将 chunk 作为 null 传递表示流结束 (EOF),之后无法写入更多数据。

¥When chunk is a Buffer, Uint8Array, 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.