On this page

Consumers

History
M

array

History
array(source, options?): Promise
Attributes
whose chunks must be Uint8Array[]
options:Object
limit:number
Maximum number of bytes to consume. If the total bytes collected exceeds limit, an ERR_OUT_OF_RANGE error is thrown
Returns:Promise
Fulfills with an array of Uint8Array objects.

Collect all chunks as an array of Uint8Array values (without concatenating).

M

arrayBuffer

History
arrayBuffer(source, options?): Promise
Attributes
whose chunks must be Uint8Array[]
options:Object
limit:number
Maximum number of bytes to consume. If the total bytes collected exceeds limit, an ERR_OUT_OF_RANGE error is thrown
Returns:Promise
Fulfills with an ArrayBuffer object.

Collect all bytes into an ArrayBuffer.

M

arrayBufferSync

History
arrayBufferSync(source, options?): ArrayBuffer
Attributes
source:Iterable
whose chunks must be Uint8Array[]
options:Object
limit:number
Maximum number of bytes to consume. If the total bytes collected exceeds limit, an ERR_OUT_OF_RANGE error is thrown
Returns:ArrayBuffer

Synchronous version of arrayBuffer().

M

arraySync

History
arraySync(source, options?): Uint8Array[]
Attributes
source:Iterable
whose chunks must be Uint8Array[]
options:Object
limit:number
Maximum number of bytes to consume. If the total bytes collected exceeds limit, an ERR_OUT_OF_RANGE error is thrown
Returns:Uint8Array[]

Synchronous version of array().

M

bytes

History
bytes(source, options?): Promise
Attributes
whose chunks must be Uint8Array[]
options:Object
limit:number
Maximum number of bytes to consume. If the total bytes collected exceeds limit, an ERR_OUT_OF_RANGE error is thrown
Returns:Promise
Fulfills with an Uint8Array 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);
M

bytesSync

History
bytesSync(source, options?): Uint8Array
Attributes
source:Iterable
whose chunks must be Uint8Array[]
options:Object
limit:number
Maximum number of bytes to consume. If the total bytes collected exceeds limit, an ERR_OUT_OF_RANGE error is thrown
Returns:Uint8Array

Synchronous version of bytes().

M

dump

History
dump(source, options?): Promise
Attributes
whose chunks must be Uint8Array[]
options:Object
limit:number
Maximum number of bytes to consume. If the total bytes read exceeds limit, an ERR_OUT_OF_RANGE error is thrown
Returns:Promise
Fulfills with undefined.

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);
M

dumpSync

History
dumpSync(source, options?): undefined
Attributes
source:Iterable
whose chunks must be Uint8Array[]
options:Object
limit:number
Maximum number of bytes to consume. If the total bytes read exceeds limit, an ERR_OUT_OF_RANGE error is thrown
Returns:undefined

Synchronous version of dump(). Throws ERR_INVALID_ARG_TYPE if source is not synchronously iterable.

M

text

History
text(source, options?): Promise
Attributes
whose chunks must be Uint8Array[]
options:Object
encoding?:string
Text encoding. Default: 'utf-8'.
limit:number
Maximum number of bytes to consume. If the total bytes collected exceeds limit, an ERR_OUT_OF_RANGE error is thrown
Returns:Promise
Fulfills with a string.

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);
M

textSync

History
textSync(source, options?): string
Attributes
source:Iterable
whose chunks must be Uint8Array[]
options:Object
encoding?:string
Default: 'utf-8'.
limit:number
Maximum number of bytes to consume. If the total bytes collected exceeds limit, an ERR_OUT_OF_RANGE error is thrown
Returns:string

Synchronous version of text().