Support ReadableStreamBYOBReader.prototype.read(view, { min })
#7091
Labels
ReadableStreamBYOBReader.prototype.read(view, { min })
#7091
What is the problem this feature would solve?
Currently, reading from a readable byte stream with a BYOB reader can be a bit inefficient if the byte stream's controller does not make an effort to fill the given view as much as possible. In the worst case, it may only write a single byte into the
Uint8Array
on each call tobyobReader.read(view)
, which could cause a lot of overhead for the reader.For example, many wires formats encode arbitrary byte sequences as a fixed-size
length
field, followed bylength
bytes (e.g. a length-prefixed record in a protocol buffer, or a box in an MP4 file). At the moment, decoding such a sequence from a readable byte stream requires looping, as shown in thisreadInto()
example.What is the feature you are proposing to solve the problem?
The Streams specification has been updated to add an optional
min
option tobyobReader.read(view)
. When themin
option is given, the read will only be fulfilled as soon asmin
number of elements are available in the stream.This allows the reader to wait for a larger number of bytes to be available, even if the stream's controller is writing them in smaller chunks. For example, to read a payload with a 32-bit length prefix, you could now write:
Specification: https://streams.spec.whatwg.org/#byob-reader-read
Specification change: whatwg/streams#1145
Tests: web-platform-tests/wpt#29723
Since Bun is powered by JavaScriptCore, you may also want to collaborate with the WebKit team: https://bugs.webkit.org/show_bug.cgi?id=264731
What alternatives have you considered?
No response
The text was updated successfully, but these errors were encountered: