A ReadableStream is returned by the readable property inside TransformStream.
lockedboolean- A Boolean value that indicates if the readable stream is locked to a reader.
-
pipeTo(destinationWritableStream, optionsPipeToOptions): Promise<void>- Pipes the readable stream to a given writable stream
destinationand returns a promise that is fulfilled when thewriteoperation succeeds or rejects it if the operation fails.
- Pipes the readable stream to a given writable stream
-
pipeThrough(transformStream, optionsPipeToOptions): ReadableStream- Pipes the readable stream to the writable side of a given
TransformStreamand returns the transform's readable side, so that calls can be chained.optionsaccepts the same values aspipeTo().
- Pipes the readable stream to the writable side of a given
-
getReader(optionsObject): ReadableStreamDefaultReader- Gets an instance of
ReadableStreamDefaultReaderand locks theReadableStreamto that reader instance. This method accepts an object argument indicating options. The only supported option ismode, which can be set tobyobto create aReadableStreamBYOBReader, as shown here:
- Gets an instance of
let reader = readable.getReader({ mode: 'byob' });-
cancel(reasonstringoptional): Promise<void>- Cancels the stream.
reasonis an optional human-readable string indicating the reason for cancellation.reasonwill be passed to the underlying source’s cancel algorithm. Any data not yet read is lost.
- Cancels the stream.
-
tee(): [ReadableStream, ReadableStream]- Locks the stream and returns an array of two new
ReadableStreaminstances, each of which reads the same data as the original stream. Backpressure to the underlying source follows the branch with the most unread data, which avoids unbounded buffering when one branch reads more slowly than the other, as long as the underlying source responds to backpressure. Refer to workerd's streams documentation ↗ for implementation details.
- Locks the stream and returns an array of two new
-
values(optionsObject): AsyncIterableIterator- Returns an async iterator that reads and consumes the chunks of the stream. This method accepts an object argument indicating options. The only supported option is
preventCancel, which, whentrue, prevents the stream from being canceled when the iterator exits early (for example, from abreakstatement). AReadableStreamis also async iterable directly:
- Returns an async iterator that reads and consumes the chunks of the stream. This method accepts an object argument indicating options. The only supported option is
for await (const chunk of readable) {
console.log(chunk);
}for await (const chunk of readable) {
console.log(chunk);
}-
preventClosebool- When
true, closure of the sourceReadableStreamwill not cause the destinationWritableStreamto be closed.
- When
-
preventAbortbool- When
true, errors in the sourceReadableStreamwill no longer abort the destinationWritableStream.pipeTowill return a rejected promise with the error from the source or any error that occurred while aborting the destination.
- When
-
ReadableStream.from(asyncIterable): ReadableStream- Creates a new
ReadableStreamwhose chunks are the values yielded byasyncIterable, which may be any iterable or async iterable, including an async generator.
- Creates a new
const stream = ReadableStream.from(
(async function* () {
yield "hello ";
yield "world";
})(),
);const stream = ReadableStream.from(
(async function* () {
yield 'hello ';
yield 'world';
})()
);