node:stream/web
Node-compatible WHATWG Streams API (node:stream/web).
Available in both vjs and vjs-lite runtimes. Enabled by default unless
--no-node-compat is passed.
import ... from "node:stream/web"
node-compat (enabled by default; disable with --no-node-compat)Example
Section titled “Example”import { ReadableStream, TransformStream } from "node:stream/web";
const stream = new ReadableStream<string>({ start(controller) { controller.enqueue("hello"); controller.close(); },});for await (const chunk of stream) { console.log(chunk);}ReadableStreamDefaultControllerv0.0.0-alpha.2UnderlyingSource methods on a ReadableStream.ReadableStreamDefaultReaderv0.0.0-alpha.2ReadableStream.getReader().ReadableStreamv0.0.0-alpha.2ReadableStream implementation.WritableStreamDefaultControllerv0.0.0-alpha.2UnderlyingSink methods on a WritableStream.WritableStreamDefaultWriterv0.0.0-alpha.2WritableStream.getWriter().WritableStreamv0.0.0-alpha.2WritableStream implementation.TransformStreamDefaultControllerv0.0.0-alpha.2Transformer methods on a TransformStream.TransformStreamv0.0.0-alpha.2TransformStream pairing a WritableStream and ReadableStream.ByteLengthQueuingStrategyv0.0.0-alpha.2chunk.byteLength.CountQueuingStrategyv0.0.0-alpha.21.AbortSignalLikev0.0.0-alpha.2QueuingStrategyv0.0.0-alpha.2UnderlyingSourcev0.0.0-alpha.2ReadableStream.UnderlyingSinkv0.0.0-alpha.2WritableStream.Transformerv0.0.0-alpha.2TransformStream.StreamPipeOptionsv0.0.0-alpha.2ReadableStream.pipeTo() and ReadableStream.pipeThrough().Classes
Section titled “Classes”ReadableStreamDefaultController
Section titled “ReadableStreamDefaultController”export class ReadableStreamDefaultController<R = any> {
readonly desiredSize: number | null;
close(): void;
enqueue(chunk?: R): void;
error(e?: any): void;
}
Controller passed to UnderlyingSource methods on a ReadableStream.
| Property | Type | Description | Since |
|---|---|---|---|
desiredSize readonly | number | null | Desired queue size to fill up to the high-water mark (null if closed/errored). | v0.0.0-alpha.2 |
ReadableStreamDefaultController.close
Section titled “ReadableStreamDefaultController.close”close(): void;
Close the controlled ReadableStream.
voidReadableStreamDefaultController.enqueue
Section titled “ReadableStreamDefaultController.enqueue”enqueue(chunk?: R): void;
Enqueue chunk into the stream’s internal queue.
| Name | Type | Description |
|---|---|---|
chunk optional | R | — |
voidReadableStreamDefaultController.error
Section titled “ReadableStreamDefaultController.error”error(e?: any): void;
Transition the controlled ReadableStream into an errored state.
| Name | Type | Description |
|---|---|---|
e optional | any | — |
voidReadableStreamDefaultReader
Section titled “ReadableStreamDefaultReader”export class ReadableStreamDefaultReader<R = any> {
readonly closed: Promise<void>;
constructor(stream: ReadableStream<R>);
cancel(reason?: any): Promise<void>;
read(): Promise<ReadableStreamReadResult<R>>;
releaseLock(): void;
}
Default reader returned by ReadableStream.getReader().
constructor(stream: ReadableStream<R>);
Construct a reader locked to stream.
| Name | Type | Description |
|---|---|---|
stream | ReadableStream<R> | — |
| Property | Type | Description | Since |
|---|---|---|---|
closed readonly | Promise<void> | Promise resolving when the stream closes or rejecting if it errors. | v0.0.0-alpha.2 |
ReadableStreamDefaultReader.cancel
Section titled “ReadableStreamDefaultReader.cancel”cancel(reason?: any): Promise<void>;
Cancel the underlying stream with reason.
| Name | Type | Description |
|---|---|---|
reason optional | any | — |
Promise<void>ReadableStreamDefaultReader.read
Section titled “ReadableStreamDefaultReader.read”read(): Promise<ReadableStreamReadResult<R>>;
Read the next chunk from the stream.
ReadableStreamDefaultReader.releaseLock
Section titled “ReadableStreamDefaultReader.releaseLock”releaseLock(): void;
Release the reader’s lock on the stream.
voidReadableStream
Section titled “ReadableStream”export class ReadableStream<R = any> {
readonly locked: boolean;
constructor(
underlyingSource?: UnderlyingSource<R>,
strategy?: QueuingStrategy<R>,
);
cancel(reason?: any): Promise<void>;
getReader(): ReadableStreamDefaultReader<R>;
pipeThrough<T>(
transform: { readable: ReadableStream<T>; writable: WritableStream<R> },
options?: StreamPipeOptions,
): ReadableStream<T>;
pipeTo(destination: WritableStream<R>, options?: StreamPipeOptions): Promise<void>;
tee(): [ReadableStream<R>, ReadableStream<R>];
values(options?: { preventCancel?: boolean }): AsyncIterableIterator<R>;
[Symbol.asyncIterator](): AsyncIterableIterator<R>;
static from<T>(iterable: Iterable<T> | AsyncIterable<T>): ReadableStream<T>;
}
WHATWG ReadableStream implementation.
constructor(
underlyingSource?: UnderlyingSource<R>,
strategy?: QueuingStrategy<R>,
);
Construct a ReadableStream.
| Name | Type | Description |
|---|---|---|
underlyingSource optional | UnderlyingSource<R> | Optional source callbacks. |
strategy optional | QueuingStrategy<R> | Optional queuing strategy. |
| Property | Type | Description | Since |
|---|---|---|---|
locked readonly | boolean | Whether the stream is currently locked to a reader. | v0.0.0-alpha.2 |
ReadableStream.cancel
Section titled “ReadableStream.cancel”cancel(reason?: any): Promise<void>;
Cancel the stream with optional reason.
| Name | Type | Description |
|---|---|---|
reason optional | any | — |
Promise<void>ReadableStream.getReader
Section titled “ReadableStream.getReader”getReader(): ReadableStreamDefaultReader<R>;
Acquire a reader and lock the stream.
ReadableStream.pipeThrough
Section titled “ReadableStream.pipeThrough”pipeThrough<T>(
transform: { readable: ReadableStream<T>; writable: WritableStream<R> },
options?: StreamPipeOptions,
): ReadableStream<T>;
Pipe chunks through a readable/writable transform pair.
| Name | Type | Description |
|---|---|---|
transform | { readable: ReadableStream<T>; writable: WritableStream<R> } | — |
options optional | StreamPipeOptions | — |
ReadableStream.pipeTo
Section titled “ReadableStream.pipeTo”pipeTo(destination: WritableStream<R>, options?: StreamPipeOptions): Promise<void>;
Pipe all chunks to destination.
| Name | Type | Description |
|---|---|---|
destination | WritableStream<R> | — |
options optional | StreamPipeOptions | — |
Promise<void>ReadableStream.tee
Section titled “ReadableStream.tee”tee(): [ReadableStream<R>, ReadableStream<R>];
Split the stream into two independent readable branches.
[ReadableStream<R>, ReadableStream<R>]ReadableStream.values
Section titled “ReadableStream.values”values(options?: { preventCancel?: boolean }): AsyncIterableIterator<R>;
Return an async iterator over chunks in the stream.
| Name | Type | Description |
|---|---|---|
options optional | { preventCancel?: boolean } | — |
ReadableStream.[Symbol.asyncIterator]
Section titled “ReadableStream.[Symbol.asyncIterator]”[Symbol.asyncIterator](): AsyncIterableIterator<R>;
Default async iterator yielding chunks from the stream.
ReadableStream.from
Section titled “ReadableStream.from”static from<T>(iterable: Iterable<T> | AsyncIterable<T>): ReadableStream<T>;
Create a ReadableStream from an iterable or async iterable.
| Name | Type | Description |
|---|---|---|
iterable | Iterable<T> | AsyncIterable<T> | — |
WritableStreamDefaultController
Section titled “WritableStreamDefaultController”export class WritableStreamDefaultController {
readonly signal: AbortSignalLike;
error(e?: any): void;
}
Controller passed to UnderlyingSink methods on a WritableStream.
| Property | Type | Description | Since |
|---|---|---|---|
signal readonly | AbortSignalLike | Abort signal aborted when the stream is aborted. | v0.0.0-alpha.2 |
WritableStreamDefaultController.error
Section titled “WritableStreamDefaultController.error”error(e?: any): void;
Error the controlled WritableStream.
| Name | Type | Description |
|---|---|---|
e optional | any | — |
voidWritableStreamDefaultWriter
Section titled “WritableStreamDefaultWriter”export class WritableStreamDefaultWriter<W = any> {
readonly closed: Promise<void>;
readonly desiredSize: number | null;
readonly ready: Promise<void>;
constructor(stream: WritableStream<W>);
abort(reason?: any): Promise<void>;
close(): Promise<void>;
releaseLock(): void;
write(chunk?: W): Promise<void>;
}
Writer returned by WritableStream.getWriter().
constructor(stream: WritableStream<W>);
Construct a writer locked to stream.
| Name | Type | Description |
|---|---|---|
stream | WritableStream<W> | — |
| Property | Type | Description | Since |
|---|---|---|---|
closed readonly | Promise<void> | Promise resolving when the stream finishes closing. | v0.0.0-alpha.2 |
desiredSize readonly | number | null | Desired queue size before backpressure applies (null if closed/errored). | v0.0.0-alpha.2 |
ready readonly | Promise<void> | Promise resolving when backpressure clears. | v0.0.0-alpha.2 |
WritableStreamDefaultWriter.abort
Section titled “WritableStreamDefaultWriter.abort”abort(reason?: any): Promise<void>;
Abort the stream with reason.
| Name | Type | Description |
|---|---|---|
reason optional | any | — |
Promise<void>WritableStreamDefaultWriter.close
Section titled “WritableStreamDefaultWriter.close”close(): Promise<void>;
Close the writable stream after pending writes complete.
Promise<void>WritableStreamDefaultWriter.releaseLock
Section titled “WritableStreamDefaultWriter.releaseLock”releaseLock(): void;
Release the writer’s lock on the stream.
voidWritableStreamDefaultWriter.write
Section titled “WritableStreamDefaultWriter.write”write(chunk?: W): Promise<void>;
Write chunk to the stream.
| Name | Type | Description |
|---|---|---|
chunk optional | W | — |
Promise<void>WritableStream
Section titled “WritableStream”export class WritableStream<W = any> {
readonly locked: boolean;
constructor(
underlyingSink?: UnderlyingSink<W>,
strategy?: QueuingStrategy<W>,
);
abort(reason?: any): Promise<void>;
close(): Promise<void>;
getWriter(): WritableStreamDefaultWriter<W>;
}
WHATWG WritableStream implementation.
constructor(
underlyingSink?: UnderlyingSink<W>,
strategy?: QueuingStrategy<W>,
);
Construct a WritableStream.
| Name | Type | Description |
|---|---|---|
underlyingSink optional | UnderlyingSink<W> | Optional sink callbacks. |
strategy optional | QueuingStrategy<W> | Optional queuing strategy. |
| Property | Type | Description | Since |
|---|---|---|---|
locked readonly | boolean | Whether the stream is currently locked to a writer. | v0.0.0-alpha.2 |
WritableStream.abort
Section titled “WritableStream.abort”abort(reason?: any): Promise<void>;
Abort the stream with optional reason.
| Name | Type | Description |
|---|---|---|
reason optional | any | — |
Promise<void>WritableStream.close
Section titled “WritableStream.close”close(): Promise<void>;
Close the stream.
Promise<void>WritableStream.getWriter
Section titled “WritableStream.getWriter”getWriter(): WritableStreamDefaultWriter<W>;
Acquire a writer and lock the stream.
TransformStreamDefaultController
Section titled “TransformStreamDefaultController”export class TransformStreamDefaultController<O = any> {
readonly desiredSize: number | null;
enqueue(chunk?: O): void;
error(reason?: any): void;
terminate(): void;
}
Controller passed to Transformer methods on a TransformStream.
| Property | Type | Description | Since |
|---|---|---|---|
desiredSize readonly | number | null | Desired queue size on the readable side (null if closed/errored). | v0.0.0-alpha.2 |
TransformStreamDefaultController.enqueue
Section titled “TransformStreamDefaultController.enqueue”enqueue(chunk?: O): void;
Enqueue chunk to the readable side.
| Name | Type | Description |
|---|---|---|
chunk optional | O | — |
voidTransformStreamDefaultController.error
Section titled “TransformStreamDefaultController.error”error(reason?: any): void;
Error both sides of the TransformStream.
| Name | Type | Description |
|---|---|---|
reason optional | any | — |
voidTransformStreamDefaultController.terminate
Section titled “TransformStreamDefaultController.terminate”terminate(): void;
Close the readable side and error the writable side.
voidTransformStream
Section titled “TransformStream”export class TransformStream<I = any, O = any> {
readonly readable: ReadableStream<O>;
readonly writable: WritableStream<I>;
constructor(
transformer?: Transformer<I, O>,
writableStrategy?: QueuingStrategy<I>,
readableStrategy?: QueuingStrategy<O>,
);
}
WHATWG TransformStream pairing a WritableStream and ReadableStream.
constructor(
transformer?: Transformer<I, O>,
writableStrategy?: QueuingStrategy<I>,
readableStrategy?: QueuingStrategy<O>,
);
Construct a TransformStream.
| Name | Type | Description |
|---|---|---|
transformer optional | Transformer<I, O> | Optional transform callbacks. |
writableStrategy optional | QueuingStrategy<I> | Optional writable-side queuing strategy. |
readableStrategy optional | QueuingStrategy<O> | Optional readable-side queuing strategy. |
| Property | Type | Description | Since |
|---|---|---|---|
readable readonly | ReadableStream<O> | Readable output side of the transform stream. | v0.0.0-alpha.2 |
writable readonly | WritableStream<I> | Writable input side of the transform stream. | v0.0.0-alpha.2 |
ByteLengthQueuingStrategy
Section titled “ByteLengthQueuingStrategy”export class ByteLengthQueuingStrategy {
readonly highWaterMark: number;
constructor(init: { highWaterMark: number });
size(chunk: ArrayBufferView): number;
}
Queuing strategy measuring chunk size via chunk.byteLength.
constructor(init: { highWaterMark: number });
Construct a ByteLengthQueuingStrategy.
| Name | Type | Description |
|---|---|---|
init | { highWaterMark: number } | Configuration containing highWaterMark. |
| Property | Type | Description | Since |
|---|---|---|---|
highWaterMark readonly | number | Desired byte high-water mark. | v0.0.0-alpha.2 |
ByteLengthQueuingStrategy.size
Section titled “ByteLengthQueuingStrategy.size”size(chunk: ArrayBufferView): number;
Return chunk.byteLength.
| Name | Type | Description |
|---|---|---|
chunk | ArrayBufferView | — |
numberCountQueuingStrategy
Section titled “CountQueuingStrategy”export class CountQueuingStrategy {
readonly highWaterMark: number;
constructor(init: { highWaterMark: number });
size(): 1;
}
Queuing strategy counting each chunk as size 1.
constructor(init: { highWaterMark: number });
Construct a CountQueuingStrategy.
| Name | Type | Description |
|---|---|---|
init | { highWaterMark: number } | Configuration containing highWaterMark. |
| Property | Type | Description | Since |
|---|---|---|---|
highWaterMark readonly | number | Desired chunk count high-water mark. | v0.0.0-alpha.2 |
CountQueuingStrategy.size
Section titled “CountQueuingStrategy.size”size(): 1;
Return 1 for every chunk.
1Interfaces
Section titled “Interfaces”AbortSignalLike
Section titled “AbortSignalLike”export interface AbortSignalLike {
readonly aborted: boolean;
readonly reason?: unknown;
addEventListener(type: string, listener: (...args: any[]) => void, options?: { once?: boolean }): void;
removeEventListener?(type: string, listener: (...args: any[]) => void): void;
}
Minimal abort-signal contract exposed on stream controllers.
| Property | Type | Description | Since |
|---|---|---|---|
aborted readonly | boolean | Whether the signal has already been aborted. | v0.0.0-alpha.2 |
reason readonly optional | unknown | Optional abort reason value. | v0.0.0-alpha.2 |
AbortSignalLike.addEventListener
Section titled “AbortSignalLike.addEventListener”addEventListener(type: string, listener: (...args: any[]) => void, options?: { once?: boolean }): void;
Register an "abort" listener.
| Name | Type | Description |
|---|---|---|
type | string | Event type ("abort"). |
listener | (...args: any[]) => void | Callback invoked on abort. |
options optional | { once?: boolean } | Optional listener options. |
voidAbortSignalLike.removeEventListener
Section titled “AbortSignalLike.removeEventListener”removeEventListener?(type: string, listener: (...args: any[]) => void): void;
Unregister an "abort" listener.
| Name | Type | Description |
|---|---|---|
type | string | Event type ("abort"). |
listener | (...args: any[]) => void | Callback to remove. |
voidQueuingStrategy
Section titled “QueuingStrategy”export interface QueuingStrategy<T = any> {
highWaterMark?: number;
size?: (chunk: T) => number;
}
Queuing strategy contract measuring chunk size and high-water mark.
| Property | Type | Description | Since |
|---|---|---|---|
highWaterMark optional | number | Desired buffer size before applying backpressure. | v0.0.0-alpha.2 |
size optional | (chunk: T) => number | Compute the byte or unit size of a single chunk. | v0.0.0-alpha.2 |
UnderlyingSource
Section titled “UnderlyingSource”export interface UnderlyingSource<R = any> {
start?: (controller: ReadableStreamDefaultController<R>) => void | PromiseLike<void>;
pull?: (controller: ReadableStreamDefaultController<R>) => void | PromiseLike<void>;
cancel?: (reason?: any) => void | PromiseLike<void>;
type?: undefined;
}
Underlying source callbacks for constructing a ReadableStream.
| Property | Type | Description | Since |
|---|---|---|---|
start optional | (controller: ReadableStreamDefaultController<R>) => void | PromiseLike<void> | Invoked immediately upon stream construction. | v0.0.0-alpha.2 |
pull optional | (controller: ReadableStreamDefaultController<R>) => void | PromiseLike<void> | Invoked when the internal queue drops below highWaterMark. | v0.0.0-alpha.2 |
cancel optional | (reason?: any) => void | PromiseLike<void> | Invoked when the stream is canceled. | v0.0.0-alpha.2 |
type optional | undefined | Stream source type (undefined for default). | v0.0.0-alpha.2 |
UnderlyingSink
Section titled “UnderlyingSink”export interface UnderlyingSink<W = any> {
start?: (controller: WritableStreamDefaultController) => void | PromiseLike<void>;
write?: (chunk: W, controller: WritableStreamDefaultController) => void | PromiseLike<void>;
close?: () => void | PromiseLike<void>;
abort?: (reason?: any) => void | PromiseLike<void>;
}
Underlying sink callbacks for constructing a WritableStream.
| Property | Type | Description | Since |
|---|---|---|---|
start optional | (controller: WritableStreamDefaultController) => void | PromiseLike<void> | Invoked immediately upon stream construction. | v0.0.0-alpha.2 |
write optional | (chunk: W, controller: WritableStreamDefaultController) => void | PromiseLike<void> | Invoked for each chunk written to the stream. | v0.0.0-alpha.2 |
close optional | () => void | PromiseLike<void> | Invoked when the writer closes the stream. | v0.0.0-alpha.2 |
abort optional | (reason?: any) => void | PromiseLike<void> | Invoked when the stream aborts. | v0.0.0-alpha.2 |
Transformer
Section titled “Transformer”export interface Transformer<I = any, O = any> {
start?: (controller: TransformStreamDefaultController<O>) => void | PromiseLike<void>;
transform?: (chunk: I, controller: TransformStreamDefaultController<O>) => void | PromiseLike<void>;
flush?: (controller: TransformStreamDefaultController<O>) => void | PromiseLike<void>;
}
Transformer callbacks for constructing a TransformStream.
| Property | Type | Description | Since |
|---|---|---|---|
start optional | (controller: TransformStreamDefaultController<O>) => void | PromiseLike<void> | Invoked immediately upon stream construction. | v0.0.0-alpha.2 |
transform optional | (chunk: I, controller: TransformStreamDefaultController<O>) => void | PromiseLike<void> | Transform one incoming chunk chunk. | v0.0.0-alpha.2 |
flush optional | (controller: TransformStreamDefaultController<O>) => void | PromiseLike<void> | Flush any buffered output before closing the readable side. | v0.0.0-alpha.2 |
StreamPipeOptions
Section titled “StreamPipeOptions”export interface StreamPipeOptions {
preventClose?: boolean;
preventAbort?: boolean;
preventCancel?: boolean;
signal?: AbortSignalLike;
}
Options accepted by ReadableStream.pipeTo() and ReadableStream.pipeThrough().
| Property | Type | Description | Since |
|---|---|---|---|
preventClose optional | boolean | Keep the destination open when the source closes. | v0.0.0-alpha.2 |
preventAbort optional | boolean | Do not abort the destination when the source errors. | v0.0.0-alpha.2 |
preventCancel optional | boolean | Do not cancel the source when the destination errors. | v0.0.0-alpha.2 |
signal optional | AbortSignalLike | Optional abort signal cancelling the pipe operation. | v0.0.0-alpha.2 |
Type Aliases
Section titled “Type Aliases”ReadableStreamReadResult
Section titled “ReadableStreamReadResult”export type ReadableStreamReadResult<T> =
| { value: T; done: false }
| { value: undefined; done: true };
Result yielded by ReadableStreamDefaultReader.read().
Default Export
Section titled “Default Export”default export
Section titled “default export”const _default: {
/** WHATWG `ReadableStream` class. @since 0.0.0-alpha.2 */
ReadableStream: typeof ReadableStream;
/** WHATWG `ReadableStreamDefaultReader` class. @since 0.0.0-alpha.2 */
ReadableStreamDefaultReader: typeof ReadableStreamDefaultReader;
/** WHATWG `ReadableStreamDefaultController` class. @since 0.0.0-alpha.2 */
ReadableStreamDefaultController: typeof ReadableStreamDefaultController;
/** WHATWG `WritableStream` class. @since 0.0.0-alpha.2 */
WritableStream: typeof WritableStream;
/** WHATWG `WritableStreamDefaultWriter` class. @since 0.0.0-alpha.2 */
WritableStreamDefaultWriter: typeof WritableStreamDefaultWriter;
/** WHATWG `WritableStreamDefaultController` class. @since 0.0.0-alpha.2 */
WritableStreamDefaultController: typeof WritableStreamDefaultController;
/** WHATWG `TransformStream` class. @since 0.0.0-alpha.2 */
TransformStream: typeof TransformStream;
/** WHATWG `TransformStreamDefaultController` class. @since 0.0.0-alpha.2 */
TransformStreamDefaultController: typeof TransformStreamDefaultController;
/** Byte-length queuing strategy class. @since 0.0.0-alpha.2 */
ByteLengthQueuingStrategy: typeof ByteLengthQueuingStrategy;
/** Chunk-count queuing strategy class. @since 0.0.0-alpha.2 */
CountQueuingStrategy: typeof CountQueuingStrategy;
};
export default _default;
Default export object providing all node:stream/web classes.
