Skip to main content
The Streams API provides interfaces for creating, composing, and consuming streams of data. Streams enable efficient processing of large data sets without loading everything into memory.

ReadableStream

Represents a readable stream of data.

Constructor

new ReadableStream<R = any>(
  underlyingSource?: UnderlyingSource<R>,
  strategy?: QueuingStrategy<R>
)
underlyingSource
UnderlyingSource<R>
Object defining the stream’s data source:
  • start(controller): Called immediately when the stream is constructed
  • pull(controller): Called when the consumer requests more data
  • cancel(reason): Called when the stream is cancelled
  • type: Either "bytes" for a byte stream or omit for a default stream
  • autoAllocateChunkSize: For byte streams, the size of automatically allocated buffers
strategy
QueuingStrategy<R>
Strategy for managing the stream’s internal queue:
  • highWaterMark: Maximum size of the internal queue
  • size(chunk): Function to compute the size of each chunk

Properties

locked
boolean
True if the stream is locked to a reader.

Methods

getReader()
method
Acquire a reader for the stream.
getReader(options?: { mode: 'byob' }): ReadableStreamReader
pipeTo()
method
Pipe the stream to a WritableStream.
pipeTo(
  dest: WritableStream<R>,
  options?: PipeToOptions
): Promise<void>
pipeThrough()
method
Pipe the stream through a TransformStream.
pipeThrough<T>(
  transform: { writable: WritableStream<R>; readable: ReadableStream<T> },
  options?: PipeToOptions
): ReadableStream<T>
tee()
method
Split the stream into two branches.
tee(): [ReadableStream<R>, ReadableStream<R>]
cancel()
method
Cancel the stream.
cancel(reason?: any): Promise<void>
values()
method
Get an async iterator for the stream.
values(options?: { preventCancel?: boolean }): AsyncIterableIterator<R>

ReadableStreamDefaultReader

Reader for ReadableStream.

Methods

read()
method
Read the next chunk from the stream.
read(): Promise<ReadableStreamReadResult<R>>
Returns:
  • { done: false, value: R } when data is available
  • { done: true, value: undefined } when the stream is closed
releaseLock()
method
Release the reader’s lock on the stream.
releaseLock(): void
cancel()
method
Cancel the stream.
cancel(reason?: any): Promise<void>

WritableStream

Represents a writable stream of data.

Constructor

new WritableStream<W = any>(
  underlyingSink?: UnderlyingSink<W>,
  strategy?: QueuingStrategy<W>
)
underlyingSink
UnderlyingSink<W>
Object defining the stream’s destination:
  • start(controller): Called immediately when the stream is constructed
  • write(chunk, controller): Called when a new chunk is ready to be written
  • close(): Called when the stream is closed
  • abort(reason): Called when the stream is aborted
strategy
QueuingStrategy<W>
Strategy for managing the stream’s internal queue.

Properties

locked
boolean
True if the stream is locked to a writer.

Methods

getWriter()
method
Acquire a writer for the stream.
getWriter(): WritableStreamDefaultWriter<W>
abort()
method
Abort the stream.
abort(reason?: any): Promise<void>
close()
method
Close the stream.
close(): Promise<void>

WritableStreamDefaultWriter

Writer for WritableStream.

Properties

desiredSize
number | null
The desired size of the stream’s internal queue.
ready
Promise<undefined>
Promise that resolves when the writer is ready to accept writes.
closed
Promise<undefined>
Promise that resolves when the stream is closed.

Methods

write()
method
Write a chunk to the stream.
write(chunk: W): Promise<void>
close()
method
Close the writer.
close(): Promise<void>
abort()
method
Abort the stream.
abort(reason?: any): Promise<void>
releaseLock()
method
Release the writer’s lock on the stream.
releaseLock(): void

TransformStream

Represents a transform stream that modifies data as it passes through.

Constructor

new TransformStream<I = any, O = any>(
  transformer?: Transformer<I, O>,
  writableStrategy?: QueuingStrategy<I>,
  readableStrategy?: QueuingStrategy<O>
)
transformer
Transformer<I, O>
Object defining the transformation:
  • start(controller): Called when the stream is constructed
  • transform(chunk, controller): Called for each chunk
  • flush(controller): Called when all chunks have been written

Properties

readable
ReadableStream<O>
The readable side of the transform stream.
writable
WritableStream<I>
The writable side of the transform stream.

Compression streams

CompressionStream
constructor
Transform stream that compresses data.
new CompressionStream(format: 'gzip' | 'deflate' | 'deflate-raw')
DecompressionStream
constructor
Transform stream that decompresses data.
new DecompressionStream(format: 'gzip' | 'deflate' | 'deflate-raw')

Text encoding streams

TextEncoderStream
constructor
Transform stream that encodes strings to UTF-8 bytes.
new TextEncoderStream()
TextDecoderStream
constructor
Transform stream that decodes UTF-8 bytes to strings.
new TextDecoderStream(label?: string, options?: TextDecoderOptions)

Queuing strategies

ByteLengthQueuingStrategy
constructor
Queuing strategy that uses byte length for sizing.
new ByteLengthQueuingStrategy({ highWaterMark: number })
CountQueuingStrategy
constructor
Queuing strategy that counts chunks.
new CountQueuingStrategy({ highWaterMark: number })

Example

// Creating a readable stream
const stream = new ReadableStream({
  start(controller) {
    controller.enqueue('chunk 1');
    controller.enqueue('chunk 2');
    controller.close();
  },
});

// Reading from a stream
const reader = stream.getReader();
while (true) {
  const { done, value } = await reader.read();
  if (done) break;
  console.log(value);
}

// Transform stream
const transformStream = new TransformStream({
  transform(chunk, controller) {
    controller.enqueue(chunk.toUpperCase());
  },
});

// Piping streams
await readableStream
  .pipeThrough(transformStream)
  .pipeTo(writableStream);

// Compression
const compressed = readableStream
  .pipeThrough(new CompressionStream('gzip'));

// Text encoding
const encoded = readableStream
  .pipeThrough(new TextEncoderStream());

// Async iteration
for await (const chunk of stream) {
  console.log(chunk);
}

Build docs developers (and LLMs) love