Node.js v26.9.0 documentation
- Node.js v26.9.0
- Table of contents
- Web Streams API
- Overview
- API
ReadableStreamTee(stream[, cloneForBranch2])- Class:
ReadableStreamnew ReadableStream([underlyingSource [, strategy]])readableStream.lockedreadableStream.cancel([reason])readableStream.getReader([options])readableStream.pipeThrough(transform[, options])readableStream.pipeTo(destination[, options])readableStream.tee()readableStream.values([options])- Async Iteration
- Transferring with
postMessage()
ReadableStream.from(iterable)- Class:
ReadableStreamDefaultReader - Class:
ReadableStreamBYOBReader - Class:
ReadableStreamDefaultController - Class:
ReadableByteStreamController - Class:
ReadableStreamBYOBRequest - Class:
WritableStream - Class:
WritableStreamDefaultWriternew WritableStreamDefaultWriter(stream)writableStreamDefaultWriter.abort([reason])writableStreamDefaultWriter.close()writableStreamDefaultWriter.closedwritableStreamDefaultWriter.desiredSizewritableStreamDefaultWriter.readywritableStreamDefaultWriter.releaseLock()writableStreamDefaultWriter.write([chunk])
- Class:
WritableStreamDefaultController - Class:
TransformStream - Class:
TransformStreamDefaultController - Class:
ByteLengthQueuingStrategy - Class:
CountQueuingStrategy - Class:
TextEncoderStream - Class:
TextDecoderStream - Class:
CompressionStream - Class:
DecompressionStream - Utility Consumers
- Web Streams API
- Index
- About this documentation
- Usage and example
- Assertion testing
- Asynchronous context tracking
- Async hooks
- Benchmark runner
- Buffer
- C++ addons
- C/C++ addons with Node-API
- C++ embedder API
- Child processes
- Cluster
- Command-line options
- Console
- Crypto
- Debugger
- Deprecated APIs
- Diagnostics Channel
- DNS
- Domain
- Environment Variables
- Errors
- Events
- File system
- FFI
- Globals
- HTTP
- HTTP/2
- HTTPS
- Inspector
- Internationalization
- Iterable Streams API
- Modules: CommonJS modules
- Modules: ECMAScript modules
- Modules:
node:moduleAPI - Modules: Packages
- Modules: TypeScript
- Net
- OS
- Path
- Performance hooks
- Permissions
- Process
- Punycode
- Query strings
- Readline
- REPL
- Report
- Single executable applications
- SQLite
- Stream
- String decoder
- Test runner
- Timers
- TLS/SSL
- Trace events
- TTY
- UDP/datagram
- URL
- Utilities
- V8
- Virtual File System
- VM
- WASI
- Web Crypto API
- Web Streams API
- Worker threads
- Zlib
- Other versions
- Options
Web Streams API#
Stability: 2 - Stable
An implementation of the WHATWG Streams Standard.
Overview#
The WHATWG Streams Standard (or "web streams") defines an API for handling streaming data. It is similar to the Node.js Streams API but emerged later and has become the "standard" API for streaming data across many JavaScript environments.
There are three primary types of objects:
ReadableStream- Represents a source of streaming data.WritableStream- Represents a destination for streaming data.TransformStream- Represents an algorithm for transforming streaming data.
Example ReadableStream#
This example creates a simple ReadableStream that pushes the current
performance.now() timestamp once every second forever. An async iterable
is used to read the data from the stream.
import { ReadableStream, } from 'node:stream/web'; import { setInterval as every, } from 'node:timers/promises'; import { performance, } from 'node:perf_hooks'; const SECOND = 1000; const stream = new ReadableStream({ async start(controller) { for await (const _ of every(SECOND)) controller.enqueue(performance.now()); }, }); for await (const value of stream) console.log(value);const { ReadableStream, } = require('node:stream/web'); const { setInterval: every, } = require('node:timers/promises'); const { performance, } = require('node:perf_hooks'); const SECOND = 1000; const stream = new ReadableStream({ async start(controller) { for await (const _ of every(SECOND)) controller.enqueue(performance.now()); }, }); (async () => { for await (const value of stream) console.log(value); })();
Node.js streams interoperability#
Node.js streams can be converted to web streams and vice versa via the toWeb and fromWeb methods present on stream.Readable, stream.Writable and stream.Duplex objects.
For more details refer to the relevant documentation:
API#
ReadableStreamTee(stream[, cloneForBranch2])#
Stability: 1 - Experimental
stream<ReadableStream>cloneForBranch2<boolean>Whentrue, chunks enqueued into the second branch are cloned from chunks enqueued into the first branch. Default:false.- Returns:
<ReadableStream>[] Two<ReadableStream>branches.
Runs the WHATWG ReadableStreamTee abstract operation on stream.
This differs from readableStream.tee() only when cloneForBranch2 is
true. The tee() method always passes false, while other web platform
specifications, such as Fetch body cloning, pass true so that the second
branch receives cloned chunks and consumption of one branch cannot mutate chunks
seen by the other.
Class: ReadableStream#
new ReadableStream([underlyingSource [, strategy]])#
underlyingSource<Object>start<Function>A user-defined function that is invoked immediately when theReadableStreamis created.controller<ReadableStreamDefaultController>|<ReadableByteStreamController>- Returns:
undefinedor a promise fulfilled withundefined.
pull<Function>A user-defined function that is called repeatedly when theReadableStreaminternal queue is not full. The operation may be sync or async. If async, the function will not be called again until the previously returned promise is fulfilled.controller<ReadableStreamDefaultController>|<ReadableByteStreamController>- Returns: A promise fulfilled with
undefined.
cancel<Function>A user-defined function that is called when theReadableStreamis canceled.reason<any>- Returns: A promise fulfilled with
undefined.
type<string>Must be'bytes'orundefined.autoAllocateChunkSize<number>Used only whentypeis equal to'bytes'. When set to a non-zero value a view buffer is automatically allocated toReadableByteStreamController.byobRequest. When not set one must use stream's internal queues to transfer data via the default readerReadableStreamDefaultReader.
strategy<Object>highWaterMark<number>The maximum internal queue size before backpressure is applied.size<Function>A user-defined function used to identify the size of each chunk of data.
readableStream.locked#
- Type:
<boolean>Set totrueif there is an active reader for this<ReadableStream>.
The readableStream.locked property is false by default, and is
switched to true while there is an active reader consuming the
stream's data.
readableStream.cancel([reason])#
reason<any>- Returns: A promise fulfilled with
undefinedonce cancelation has been completed.
readableStream.getReader([options])#
options<Object>mode<string>'byob'orundefined
- Returns:
<ReadableStreamDefaultReader>|<ReadableStreamBYOBReader>
import { ReadableStream } from 'node:stream/web'; const stream = new ReadableStream(); const reader = stream.getReader(); console.log(await reader.read());const { ReadableStream } = require('node:stream/web'); const stream = new ReadableStream(); const reader = stream.getReader(); reader.read().then(console.log);
Causes the readableStream.locked to be true.
readableStream.pipeThrough(transform[, options])#
transform<Object>readable<ReadableStream>TheReadableStreamto whichtransform.writablewill push the potentially modified data it receives from thisReadableStream.writable<WritableStream>TheWritableStreamto which thisReadableStream's data will be written.
options<Object>preventAbort<boolean>Whentrue, errors in thisReadableStreamwill not causetransform.writableto be aborted.preventCancel<boolean>Whentrue, errors in the destinationtransform.writabledo not cause thisReadableStreamto be canceled.preventClose<boolean>Whentrue, closing thisReadableStreamdoes not causetransform.writableto be closed.signal<AbortSignal>Allows the transfer of data to be canceled using an<AbortController>.
- Returns:
<ReadableStream>Fromtransform.readable.
Connects this <ReadableStream> to the pair of