ReadableStreamBYOBRequest
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 inWeb Workers.
TheReadableStreamBYOBRequest
interface of theStreams API represents a "pull request" for data from an underlying source that will made as a zero-copy transfer to a consumer (bypassing the stream's internal queues).
ReadableStreamBYOBRequest
objects are created in "BYOB mode" when a consumer makes a request for data and the stream's internal queue isempty.(The stream will resolve the consumer's request directly if it already has buffered data).An underlying byte source can access active BYOB requests through its controller'sReadableByteStreamController.byobRequest
property, which will be set tonull
if there is no outstanding request.
An underlying source that supports "BYOB mode" should check forReadableByteStreamController.byobRequest
and must use it for transferring data, if present.If data arrives from the underlying source whenReadableByteStreamController.byobRequest
isnull
, it can be queued usingReadableByteStreamController.enqueue()
.This might happen when an underlying push source receives new data when the stream's internal buffers are not empty.
An underlying source uses the request by writing data to the BYOB request'sview
and then callingrespond()
, or by callingrespondWithNewView()
and passing a new view as an argument.Note that the "new view" must actually be a view over thesame buffer as the originalview
, starting at the same offset.This might be used to return a shorter buffer if the underlying source is unable to fill the entire original view.
Note that aReadableByteStreamController
is only created for underlying sources whentype="bytes"
is specified for the source in theReadableStream()
constructor."BYOB mode" is enabled when eitherautoAllocateChunkSize
is specified in theReadableController()
constructor or when using aReadableStreamBYOBReader
(typically constructed by callingReadableStream.getReader()
with the argument{ mode: 'byob' }
).
In this article
Constructor
None.ReadableStreamBYOBRequest
instance is created automatically byReadableByteStreamController
as needed.
Instance properties
ReadableStreamBYOBRequest.view
Read onlyReturns the current view.This is a view on a buffer that will be transferred to the consumer when
ReadableStreamBYOBRequest.respond()
is called.
Instance methods
ReadableStreamBYOBRequest.respond()
Signals the associated readable byte stream that the specified number of bytes were written into the current
view
, which then causes the pending request from the consumer to be resolved.Note that after this method is called theview
is transferred and no longer modifiable.ReadableStreamBYOBRequest.respondWithNewView()
Signals to the associated readable byte stream view passed as an argument should be transferred to the consumer of the readable byte stream.This new view must use the same buffer as the original
view
, start at the same offset, and be the same length or shorter.Note that after this method is called theview
is transferred and no longer modifiable.
Examples
The following code is taken from the live example inUsing readable byte streams > Creating a readable socket push byte stream.
A push underlying byte source with data to transfer should first check thatcontroller.byobRequest
is non-null
. PulA pull underlying byte source would only need this check if auto chunk allocation was not enabled and it was used with a default reader.
if (controller.byobRequest) { /* code to transfer data */}
There are two ways to read data into aReadableStreamBYOBRequest
and then transfer it.The first is to write the data into theReadableStreamBYOBRequest.view
property and then callReadableStreamBYOBRequest.respond()
to indicate the amount of data to be transferred.After the operation thebyobRequest.view
is detached and the request should be discarded.
The code below shows this case using a hypotheticalreadInto()
method to copy data into the view:
const v = controller.byobRequest.view;bytesRead = socket.readInto(v.buffer, v.byteOffset, v.byteLength);controller.byobRequest.respond(bytesRead);
The other approach is to callReadableStreamBYOBRequest.respondWithNewView()
passing your own view on the same underlying backing data.Note that this just another way of specifying the range of the underlying buffer/memory backing that is actually transferred.TherespondWithNewView
equivalent to the code above would be:
const v = controller.byobRequest.view;bytesRead = socket.readInto(v.buffer, v.byteOffset, v.byteLength);const newView = new Uint8Array(v.buffer, v.byteOffset, bytesRead);controller.byobRequest.respondWithNewView(newView);
Specifications
Specification |
---|
Streams> # rs-byob-request-class> |
Browser compatibility
Loading…