Streams
The ByteLengthQueuingStrategy interface of the Streams API provides a built-in byte length queuing strategy that can be used when constructing streams.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 52 | 16 | 65 | 10.1 | 52 | 10.3 | |
| The ByteLengthQueuingStrategy() constructor creates and returns a ByteLengthQueuingStrategy object instance. | 52 | 16 | 65 | 10.1 | 52 | 10.3 |
| The read-only ByteLengthQueuingStrategy.highWaterMark property returns the total number of bytes that can be contained in the internal queue before backpressure is applied. | 52 | 16 | 65 | 10.1 | 52 | 10.3 |
| The size() method of the ByteLengthQueuingStrategy interface returns the given chunk's byteLength property. | 52 | 16 | 65 | 10.1 | 52 | 10.3 |
| The CountQueuingStrategy interface of the Streams API provides a built-in chunk counting queuing strategy that can be used when constructing streams. | 52 | 16 | 65 | 10.1 | 52 | 10.3 |
| The CountQueuingStrategy() constructor creates and returns a CountQueuingStrategy object instance. | 52 | 16 | 65 | 10.1 | 52 | 10.3 |
| The read-only CountQueuingStrategy.highWaterMark property returns the total number of chunks that can be contained in the internal queue before backpressure is applied. | 52 | 16 | 65 | 10.1 | 52 | 10.3 |
| The size() method of the CountQueuingStrategy interface always returns 1, so that the total queue size is a count of the number of chunks in the queue. | 52 | 16 | 65 | 10.1 | 52 | 10.3 |
| The ReadableStream interface of the Streams API represents a readable stream of byte data. The Fetch API offers a concrete instance of a ReadableStream through the Response.body property of a Response object. | 43 | 14 | 65 | 10.1 | 43 | 10.3 |
| The cancel() method of the ReadableStream interface returns a Promise that resolves when the stream is canceled. | 43 | 14 | 65 | 10.1 | 43 | 10.3 |
| The getReader() method of the ReadableStream interface creates a reader and locks the stream to it. While the stream is locked, no other reader can be acquired until this one is released. | 43 | 14 | 65 | 10.1 | 43 | 10.3 |
| The locked read-only property of the ReadableStream interface returns whether or not the readable stream is locked to a reader. | 52 | 14 | 65 | 10.1 | 52 | 10.3 |
| The pipeThrough() method of the ReadableStream interface provides a chainable way of piping the current stream through a transform stream or any other writable/readable pair. | 59 | 79 | 102 | 10.1 | 59 | 10.3 |
| The pipeTo() method of the ReadableStream interface pipes the current ReadableStream to a given WritableStream and returns a Promise that fulfills when the piping process completes successfully, or rejects if any errors were encountered. | 59 | 79 | 100 | 10.1 | 59 | 10.3 |
| The ReadableStream() constructor creates and returns a readable stream object from the given handlers. | 52 | 79 | 65 | 10.1 | 52 | 10.3 |
| The tee() method of the ReadableStream interface tees the current readable stream, returning a two-element array containing the two resulting branches as new ReadableStream instances. | 52 | 79 | 65 | 10.1 | 52 | 10.3 |
| The ReadableStreamDefaultController interface of the Streams API represents a controller allowing control of a ReadableStream's state and internal queue. Default controllers are for streams that are not byte streams. | 52 | 79 | 65 | 10 | 52 | 10 |
| The close() method of the ReadableStreamDefaultController interface closes the associated stream. | 52 | 79 | 65 | 10 | 52 | 10 |
| The desiredSize read-only property of the ReadableStreamDefaultController interface returns the desired size required to fill the stream's internal queue. | 52 | 79 | 65 | 10 | 52 | 10 |
| The enqueue() method of the ReadableStreamDefaultController interface enqueues a given chunk in the associated stream. | 52 | 79 | 65 | 10 | 52 | 10 |
| The error() method of the ReadableStreamDefaultController interface causes any future interactions with the associated stream to error. | 52 | 79 | 65 | 10 | 52 | 10 |
| The ReadableStreamDefaultReader interface of the Streams API represents a default reader that can be used to read stream data supplied from a network (such as a fetch request). | 43 | 79 | 65 | 10 | 43 | 10 |
| The cancel() method of the ReadableStreamDefaultReader interface returns a Promise that resolves when the stream is canceled. Calling this method signals a loss of interest in the stream by a consumer. | 43 | 79 | 65 | 10 | 43 | 10 |
| The closed read-only property of the ReadableStreamDefaultReader interface returns a Promise that fulfills when the stream closes, or rejects if the stream throws an error or the reader's lock is released. This property enables you to write code that responds to an end to the streaming process. | 43 | 79 | 65 | 10 | 43 | 10 |
| The read() method of the ReadableStreamDefaultReader interface returns a Promise providing access to the next chunk in the stream's internal queue. | 43 | 79 | 65 | 10 | 43 | 10 |
| The ReadableStreamDefaultReader() constructor creates and returns a ReadableStreamDefaultReader object instance. | 78 | 79 | 100 | 10 | 78 | 10 |
| The releaseLock() method of the ReadableStreamDefaultReader interface releases the reader's lock on the stream. | 43 | 79 | 65 | 10 | 43 | 10 |
releaseLock (reject pending read request) `releaseLock()` rejects pending read requests | 105 | 105 | 102 | 17 | 105 | 17 |
| The TransformStream interface of the Streams API represents a concrete implementation of the pipe chain transform stream concept. | 67 | 79 | 102 | 14.1 | 67 | 14.5 |
| The readable read-only property of the TransformStream interface returns the ReadableStream instance controlled by this TransformStream. This stream emits the transformed output data. | 67 | 79 | 102 | 14.1 | 67 | 14.5 |
| The TransformStream() constructor creates a new TransformStream object which represents a pair of streams: a WritableStream representing the writable side, and a ReadableStream representing the readable side. | 67 | 79 | 102 | 14.1 | 67 | 14.5 |
| The writable read-only property of the TransformStream interface returns the WritableStream instance controlled by this TransformStream. This stream accepts input data that will be transformed and emitted to the readable stream. | 67 | 79 | 102 | 14.1 | 67 | 14.5 |
| The TransformStreamDefaultController interface of the Streams API provides methods to manipulate the associated ReadableStream and WritableStream. | 67 | 79 | 102 | 14.1 | 67 | 14.5 |
| The desiredSize read-only property of the TransformStreamDefaultController interface returns the desired size to fill the queue of the associated ReadableStream. | 67 | 79 | 102 | 14.1 | 67 | 14.5 |
| The enqueue() method of the TransformStreamDefaultController interface enqueues the given chunk in the readable side of the stream. | 67 | 79 | 102 | 14.1 | 67 | 14.5 |
| The error() method of the TransformStreamDefaultController interface errors both sides of the stream. Any further interactions with it will fail with the given error message, and any chunks in the queue will be discarded. | 67 | 79 | 102 | 14.1 | 67 | 14.5 |
| The terminate() method of the TransformStreamDefaultController interface closes the readable side and errors the writable side of the stream. | 67 | 79 | 102 | 14.1 | 67 | 14.5 |
| The WritableStream interface of the Streams API provides a standard abstraction for writing streaming data to a destination, known as a sink. This object comes with built-in backpressure and queuing. | 59 | 16 | 100 | 14.1 | 59 | 14.5 |
| The abort() method of the WritableStream interface aborts the stream, signaling that the producer can no longer successfully write to the stream and it is to be immediately moved to an error state, with any queued writes discarded. | 59 | 16 | 100 | 14.1 | 59 | 14.5 |
| The close() method of the WritableStream interface closes the associated stream. All chunks written before this method is called are sent before the returned promise is fulfilled. | 81 | 81 | 100 | 14.1 | 81 | 14.5 |
| The getWriter() method of the WritableStream interface returns a new instance of WritableStreamDefaultWriter and locks the stream to that instance. While the stream is locked, no other writer can be acquired until this one is released. | 59 | 16 | 100 | 14.1 | 59 | 14.5 |
| The locked read-only property of the WritableStream interface returns a boolean indicating whether the WritableStream is locked to a writer. | 59 | 16 | 100 | 14.1 | 59 | 14.5 |
| The WritableStream() constructor creates a new WritableStream object instance. | 59 | 16 | 100 | 14.1 | 59 | 14.5 |
| The WritableStreamDefaultController interface of the Streams API represents a controller allowing control of a WritableStream's state. When constructing a WritableStream, the underlying sink is given a corresponding WritableStreamDefaultController instance to manipulate. | 59 | 16 | 100 | 14.1 | 59 | 14.5 |
| The error() method of the WritableStreamDefaultController interface causes any future interactions with the associated stream to error. | 59 | 16 | 100 | 14.1 | 59 | 14.5 |
| The read-only signal property of the WritableStreamDefaultController interface returns the AbortSignal associated with the controller. | 98 | 98 | 100 | 16.4 | 98 | 16.4 |
| The WritableStreamDefaultWriter interface of the Streams API is the object returned by WritableStream.getWriter() and once created locks the writer to the WritableStream ensuring that no other streams can write to the underlying sink. | 59 | 16 | 100 | 14.1 | 59 | 14.5 |
| The abort() method of the WritableStreamDefaultWriter interface aborts the stream, signaling that the producer can no longer successfully write to the stream and it is to be immediately moved to an error state, with any queued writes discarded. | 59 | 16 | 100 | 14.1 | 59 | 14.5 |
| The close() method of the WritableStreamDefaultWriter interface closes the associated writable stream. | 59 | 16 | 100 | 14.1 | 59 | 14.5 |
| The closed read-only property of the WritableStreamDefaultWriter interface returns a Promise that fulfills if the stream becomes closed, or rejects if the stream errors or the writer's lock is released. | 59 | 16 | 100 | 14.1 | 59 | 14.5 |
| The desiredSize read-only property of the WritableStreamDefaultWriter interface returns the desired size required to fill the stream's internal queue. | 59 | 16 | 100 | 14.1 | 59 | 14.5 |
| The ready read-only property of the WritableStreamDefaultWriter interface returns a Promise that resolves when the desired size of the stream's internal queue transitions from non-positive to positive, signaling that it is no longer applying backpressure. | 59 | 16 | 100 | 14.1 | 59 | 14.5 |
| The releaseLock() method of the WritableStreamDefaultWriter interface releases the writer's lock on the corresponding stream. After the lock is released, the writer is no longer active. If the associated stream is errored when the lock is released, the writer will appear errored in the same way from now on; otherwise, the writer will appear closed. | 59 | 16 | 100 | 14.1 | 59 | 14.5 |
| The WritableStreamDefaultWriter() constructor creates a new WritableStreamDefaultWriter object instance. | 78 | 79 | 100 | 14.1 | 78 | 14.5 |
| The write() method of the WritableStreamDefaultWriter interface writes a passed chunk of data to a WritableStream and its underlying sink, then returns a Promise that resolves to indicate the success or failure of the write operation. | 59 | 16 | 100 | 14.1 | 59 | 14.5 |
- Before version 105, `releaseLock()` throws instead of rejecting.
- Before version 105, `releaseLock()` throws instead of rejecting.
- Before version 105, `releaseLock()` throws instead of rejecting.
Syntax
const queueingStrategy = new ByteLengthQueuingStrategy({ highWaterMark: 1024 });
const readableStream = new ReadableStream(
{
start(controller) {
// …
},
pull(controller) {
// …
},
cancel(err) {
console.log("stream error:", err);
},
},
queueingStrategy,
);
const size = queueingStrategy.size(chunk); Use cases
-
Using Streams
The ByteLengthQueuingStrategy interface of the Streams API provides a built-in byte length queuing strategy that can be used when constructing streams.
Cautions
- May not be supported in older browsers.
Implementation notes
- Some APIs require a secure context (HTTPS) or user activation. Check the requirements before use.