ReadableStream
The ReadableStream API represents a readable stream or readable end. It is designed based on the standard Web API ReadableStream.
Note:
A
ReadableStream
object cannot be constructed directly. You can use TransformStream to construct a ReadableStream
object.Overview
// Use TransformStream to construct a ReadableStream object.const { readable } = new TransformStream();
Attributes
// readable.lockedreadonly locked: boolean;
The locked attribute indicates whether a stream is locked.
Note:
A stream is locked in the following scenarios:
The stream has no more than one activated
reader
. Before the reader
calls the releaseLock()
method, the stream is locked. The stream is in being piped. The stream is locked until the piping ends.
Methods
Note:
Before you use any of the following methods, make sure that the stream is not locked. Otherwise, an exception is returned.
getReader
readable.getReader(options?: ReaderOptions): ReadableStreamDefaultReader | ReadableStreamBYOBReader;
The getReader() method creates a
reader
and locks the current stream until the reader
calls the releaseLock()
method.Parameters
Parameter | Type | Required | Description |
options | Yes | The configuration items for generating the reader. |
ReaderOptions
The following table describes the parameters of the
ReaderOptions
object.Parameter | Type | Required | Description |
mode | string | No | Reader The type of the `reader`. Default value: undefined. Valid values:undefined byob |
pipeThrough
readable.pipeThrough(transformStream: TransformStream, options?: PipeToOptions): ReadableStream;
The pipeThrough() method pipes the data of the current readable stream to the writable side of the
transformStream
and returns the readable side of the transformStream
.Note:
During the piping, the
writable
side of the current stream is locked.Parameters
Parameter | Type | Required | Description |
transformStream | Yes | The destination to which the current stream is piped. | |
options | Yes | The configuration items for piping the stream. |
PipeToOptions
The following table describes the configuration items for piping the stream.
Parameter | Type | Required | Description |
preventClose | boolean | No | If the value is true, the writable stream is not closed along with the readable stream. |
preventAbort | boolean | No | If the value is true, the writable stream is not stopped when an exception occurs on the readable stream. |
preventCancel | boolean | No | If the value is true, the writable stream is not closed when the readable stream is incorrect. |
signal | No | If `signal` is stopped, ongoing pipe operations are stopped. |
pipeTo
readable.pipeTo(destination: WritableStream, options?: PipeToOptions): Promise<void>;
The pipeTo() method pipes the current readable stream to the
destination
writable stream.Note:
During the piping, the
destination
of the current stream is locked.Parameters
Parameter | Type | Required | Description |
destination | Yes | The writable stream. | |
options | Yes | The configuration items for piping the stream. |
tee
readable.tee(): [ReadableStream, ReadableStream];
The tee() method tees the current readable stream and returns two independent branches.
cancel
readable.cancel(reason?: string): Promise<string>;
The cancel() method ends the current stream.