readable[Symbol.for('Stream.toAsyncStreamable')]()
- 返回:<AsyncIterable> 一个
AsyncIterable<Uint8Array[]>,可以从流中生成批量块。
当启用 --experimental-stream-iter 标志时,Readable 流实现 Stream.toAsyncStreamable 协议,从而支持 stream/iter API 的高效使用。
🌐 When the --experimental-stream-iter flag is enabled, Readable streams
implement the Stream.toAsyncStreamable protocol, enabling efficient
consumption by the stream/iter API.
这提供了一个批量异步迭代器,将流的内部缓冲区排入 Uint8Array[] 批次中,从而分摊标准 Symbol.asyncIterator 路径的每块 Promise 开销。对于字节模式流,块将直接作为 Buffer 实例(它们是 Uint8Array 子类)输出。对于对象模式或编码流,每个块在批处理前都将归一化为 Uint8Array。
🌐 This provides a batched async iterator that drains the stream's internal
buffer into Uint8Array[] batches, amortizing the per-chunk Promise overhead
of the standard Symbol.asyncIterator path. For byte-mode streams, chunks
are yielded directly as Buffer instances (which are Uint8Array subclasses).
For object-mode or encoded streams, each chunk is normalized to Uint8Array
before batching.
返回的迭代器被标记为已验证的来源,因此 from() 会直接使用它,而无需额外的规范化。
🌐 The returned iterator is tagged as a validated source, so from()
passes it through without additional normalization.
import { Readable } from 'node:stream';
import { text, from } from 'node:stream/iter';
const readable = new Readable({
read() { this.push('hello'); this.push(null); },
});
// Readable is automatically consumed via toAsyncStreamable
console.log(await text(from(readable))); // 'hello'const { Readable } = require('node:stream');
const { text, from } = require('node:stream/iter');
async function run() {
const readable = new Readable({
read() { this.push('hello'); this.push(null); },
});
console.log(await text(from(readable))); // 'hello'
}
run().catch(console.error);如果没有 --experimental-stream-iter 标志,调用此方法会抛出 ERR_STREAM_ITER_MISSING_FLAG。
🌐 Without the --experimental-stream-iter flag, calling this method throws
ERR_STREAM_ITER_MISSING_FLAG.