ReadableStream
ReadableStream represents a stream of data that can be read incrementally rather than all at once.
In Scripting, ReadableStream<Data> is used in various scenarios, including:
- Handling streaming HTTP responses (e.g.,
Response.body) - Chunked file downloads or large data transfers
- Real-time data streams such as logs, AI model output, or event streams
It follows the same behavior as the standard Web Streams API, allowing asynchronous iteration (for await...of) and manual reading via a ReadableStreamDefaultReader.
Definition
Overview
- A
ReadableStreamrepresents a data source that can be consumed asynchronously. - Instead of loading the entire payload into memory, data is read in chunks as it becomes available.
- Only one active reader can consume a stream at a time. When locked (
locked = true), no other consumer can access it until released or canceled.
Properties
locked: boolean
Indicates whether the stream is currently locked to a reader.
If true, you must release the lock or cancel the stream before another reader can be created.
Example
Methods
getReader(): ReadableStreamDefaultReader<T>
Returns a ReadableStreamDefaultReader that allows manual, incremental reading of stream data.
Each call to reader.read() returns a Promise resolving to an object { value, done }.
Example
cancel(reason?: any): Promise<void>
Cancels reading from the stream.
Optionally provide a reason describing why the operation was aborted.
Example
tee(): [ReadableStream<T>, ReadableStream<T>]
Splits a stream into two identical branches that can be consumed independently.
Example
ReadableStreamDefaultReader
When you obtain a reader using getReader(), you can manually control the reading process.
Definition
Method Descriptions
Example — Reading Stream Data
Integration with Response
The Response.body property is a ReadableStream<Data> that allows streaming response content.
Example — Handling Streaming Network Response
This method enables real-time data processing before the full response is received, ideal for:
- Real-time log streaming
- Progressive file downloads
- AI or LLM text generation (token streaming)
Integration with Data
In Scripting, each stream chunk (value) is a Data object.
You can use the Data APIs such as .toRawString() or .toUint8Array() to inspect or transform the content.
Example — Save Stream Data to a File
Using Async Iteration
ReadableStream supports async iteration (for await...of), simplifying data consumption syntax:
This approach automatically handles the done condition for you, resulting in cleaner code.
Common Use Cases
Notes
- Single-reader rule: Only one reader can consume a
ReadableStreamat a time. - Memory efficiency: Streaming avoids loading large payloads entirely into memory.
- Error handling: Reading may reject on errors (e.g., network failure). Use
try...catchto handle exceptions safely. - Chunk type: For
Response.body, each chunk is aDataobject — not plain text or byte arrays.
Summary
ReadableStream is a core component of Scripting’s streaming data architecture, providing efficient and flexible data handling capabilities.
Key Features
- Asynchronous, incremental data reading
- Seamless integration with
fetch(),Response, andData - Supports real-time processing and large data transfers
- Fully compatible with the standard Web Streams API
