Consumers
History
array(source, options?): Promise
AsyncIterable | IterableUint8Array[]ObjectAbortSignalnumberERR_OUT_OF_RANGE error is thrownPromiseUint8Array objects.Collect all chunks as an array of Uint8Array values (without concatenating).
arrayBuffer(source, options?): Promise
AsyncIterable | IterableUint8Array[]ObjectAbortSignalnumberERR_OUT_OF_RANGE error is thrownPromiseArrayBuffer object.Collect all bytes into an ArrayBuffer.
arrayBufferSync(source, options?): ArrayBuffer
IterableUint8Array[]ObjectnumberERR_OUT_OF_RANGE error is thrownArrayBufferSynchronous version of arrayBuffer().
arraySync(source, options?): Uint8Array[]
IterableUint8Array[]ObjectnumberERR_OUT_OF_RANGE error is thrownUint8Array[]Synchronous version of array().
bytes(source, options?): Promise
AsyncIterable | IterableUint8Array[]ObjectAbortSignalnumberERR_OUT_OF_RANGE error is thrownPromiseUint8Array object.Collect all bytes from a stream into a single Uint8Array.
import { from, bytes } from 'node:stream/iter'; const data = await bytes(from('hello')); console.log(data); // Uint8Array(5) [ 104, 101, 108, 108, 111 ]
const { from, bytes } = require('node:stream/iter'); async function run() { const data = await bytes(from('hello')); console.log(data); // Uint8Array(5) [ 104, 101, 108, 108, 111 ] } run().catch(console.error);
bytesSync(source, options?): Uint8Array
IterableUint8Array[]ObjectnumberERR_OUT_OF_RANGE error is thrownUint8ArraySynchronous version of bytes().
dump(source, options?): Promise
AsyncIterable | IterableUint8Array[]ObjectAbortSignalnumberERR_OUT_OF_RANGE error is thrownPromiseundefined.Read a source to completion, discarding every chunk. Unlike the other
consumers, dump() retains nothing. Memory tops-out at a single batch no
matter how much data the source produces.
Use this to consume a stream when the content doesn't matter. For example, A QUIC stream only returns flow-control credit to the peer as its data is consumed, so a receiver that does not want the payload must still read it to completion.
If the source errors part-way through, the returned promise rejects with that error.
There is no default limit. dump() reads until the source is exhausted
unless a limit is specified. When a limit is configured and the source exceeds
it, the promise rejects and the source is cancelled. A partial dump is never
reported as success.
import { dump, from, pull, tap } from 'node:stream/iter'; // Count the bytes flowing through a stream without retaining any of them. let bytesSeen = 0; const counter = tap((chunks) => { for (const chunk of chunks) bytesSeen += chunk.byteLength; }); await dump(pull(from('hello world'), counter)); console.log(bytesSeen); // 11
const { dump, from, pull, tap } = require('node:stream/iter'); async function run() { // Count the bytes flowing through a stream without retaining any of them. let bytesSeen = 0; const counter = tap((chunks) => { for (const chunk of chunks) bytesSeen += chunk.byteLength; }); await dump(pull(from('hello world'), counter)); console.log(bytesSeen); // 11 } run().catch(console.error);
dumpSync(source, options?): undefined
IterableUint8Array[]ObjectnumberERR_OUT_OF_RANGE error is thrownundefinedSynchronous version of dump(). Throws ERR_INVALID_ARG_TYPE if source
is not synchronously iterable.
text(source, options?): Promise
AsyncIterable | IterableUint8Array[]Objectstring'utf-8'.AbortSignalnumberERR_OUT_OF_RANGE error is thrownPromisestring.Collect all bytes and decode as text.
import { from, text } from 'node:stream/iter'; console.log(await text(from('hello'))); // 'hello'
const { from, text } = require('node:stream/iter'); async function run() { console.log(await text(from('hello'))); // 'hello' } run().catch(console.error);
textSync(source, options?): string
Synchronous version of text().