readable.push(chunk[, encoding])
chunk
<Buffer> | <Uint8Array> | <string> | <null> | <any> 要推入读取队列的数据块。 对于不在对象模式下操作的流,chunk
必须是字符串、Buffer
或Uint8Array
。 对于对象模式的流,chunk
可以是任何 JavaScript 值。encoding
<string> 字符串块的编码。 必须是有效的Buffer
编码,例如'utf8'
或'ascii'
。- 返回: <boolean> 如果可以继续推送额外的数据块,则为
true
;否则为false
。
当 chunk
为 Buffer
、Uint8Array
或 string
时,数据的 chunk
将被添加到内部队列中供流的用户消费。
将 chunk
作为 null
传递信号表示流结束 (EOF),之后不能再写入数据。
当 Readable
处于暂停模式时,在 'readable'
事件触发时调用 readable.read()
方法可以读出添加了 readable.push()
的数据。
当 Readable
工作在流动模式时,添加了 readable.push()
的数据将通过触发 'data'
事件来传递。
readable.push()
方法设计得尽可能灵活。
例如,当封装提供某种形式的暂停/恢复机制和数据回调的低层源时,低层源可以由自定义 Readable
实例封装:
// `_source` 是一个具有 readStop() 和 readStart() 方法的对象,
// 当有数据时调用 `ondata` 成员,
// 当数据结束时调用 `onend` 成员。
class SourceWrapper extends Readable {
constructor(options) {
super(options);
this._source = getLowLevelSourceObject();
// 每次有数据时,将其推入内部缓冲区。
this._source.ondata = (chunk) => {
// 如果 push() 返回 false,则停止从源读取。
if (!this.push(chunk))
this._source.readStop();
};
// 当源结束时,推送 EOF 信令 `null` 块。
this._source.onend = () => {
this.push(null);
};
}
// 当流想要拉入更多数据时将调用 _read() 。
// 在这种情况下,会忽略建议的大小参数。
_read(size) {
this._source.readStart();
}
}
readable.push()
方法用于将内容推送到内部缓冲区中。
它可以由 readable._read()
方法驱动。
对于非对象模式的流,如果 readable.push()
的 chunk
参数为 undefined
,它将被视为空字符串或缓冲区。
有关详细信息,请参阅 readable.push('')
。
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
orUint8Array
. For object mode streams,chunk
may be any JavaScript value.encoding
<string> Encoding of string chunks. Must be a validBuffer
encoding, such as'utf8'
or'ascii'
.- Returns: <boolean>
true
if additional chunks of data may continue to be pushed;false
otherwise.
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.
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.
When the Readable
is operating in flowing mode, the data added with
readable.push()
will be delivered by emitting a 'data'
event.
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();
}
}
The readable.push()
method is used to push the content
into the internal buffer. It can be driven by the readable._read()
method.
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.