ReadableByteStreamController
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Note: This feature is available in Web Workers.
The ReadableByteStreamController
interface of the Streams API represents a controller for a readable byte stream.
It allows control of the state and internal queue of a ReadableStream
with an underlying byte source, and enables efficient zero-copy transfer of data from the underlying source to a consumer when the stream's internal queue is empty.
An instance of this controller type is created if an underlyingSource
object with the property type="bytes"
is passed as an argument to the ReadableStream()
constructor.
The underlyingSource
object may also define start()
and pull()
callback functions.
These are called with the controller as a parameter, in order to set up the underlying source, and request data when needed.
The underlying source uses the controller to supply data to the stream via its byobRequest
property or enqueue()
method.
byobRequest
is a ReadableStreamBYOBRequest
object that represents a pending request from a consumer to make a zero-copy transfer of data direct to a consumer.
byobRequest
must be used to copy data if it exists (do not use enqueue()
in this case)!
If the underlying source needs to pass data to the stream and byobRequest
is null
then the source can call enqueue()
to add the data to the stream's internal queues.
Note that the byobRequest
is only created in "BYOB mode" when there is a request from a reader and the stream's internal queue is empty.
"BYOB mode" is enabled when using a ReadableStreamBYOBReader
(typically constructed by calling ReadableStream.getReader()
with the argument { mode: 'byob' }
).
It is also enabled when using a default reader and autoAllocateChunkSize
is specified in the ReadableController()
constructor.
An underlying byte source can also use the controller to close()
the stream when all the data has been sent and report errors from the underlying source using error()
.
The controller's desiredSize
property is used to apply "backpressure", informing the underlying source of the size of the internal queue (small values indicate that the queue is filling up, hinting to the underlying source that it is be desirable to pause or throttle the inflow).
Note that even though the controller is primarily used by the underlying byte source, there is no reason it cannot be stored used by other parts of the system to signal the stream.
Constructor
None. ReadableByteStreamController
instances are automatically created if an underlyingSource
with the property type="bytes"
is passed to the ReadableStream()
constructor.
Instance properties
ReadableByteStreamController.byobRequest
Read only-
Returns the current BYOB pull request, or
null
if there no outstanding request. ReadableByteStreamController.desiredSize
Read only-
Returns the desired size required to fill the stream's internal queue.
Instance methods
ReadableByteStreamController.close()
-
Closes the associated stream.
ReadableByteStreamController.enqueue()
-
Enqueues a given chunk in the associated stream.
ReadableByteStreamController.error()
-
Causes any future interactions with the associated stream to error.
Examples
The controller is used by an underlying source to transfer or enqueue data, to signal that the stream has no more data (has closed) or has errored. It is also used to signal the underlying source from "upstream" of the desired data rate, using desiredSize
.
The example in Using readable byte streams, in particular Creating a readable socket push byte stream, show most of these cases.
Specifications
Specification |
---|
Streams Standard # rbs-controller-class |
Browser compatibility
BCD tables only load in the browser
See also
- Streams API concepts
- Using readable byte streams
ReadableStream
- WHATWG Stream Visualizer, for a basic visualization of readable, writable, and transform streams.
- Web-streams-polyfill or sd-streams - polyfills