Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ReadableStreamBYOBReader.prototype.read(view, { min }) #7091

Open
MattiasBuelens opened this issue Nov 13, 2023 · 0 comments
Open

Support ReadableStreamBYOBReader.prototype.read(view, { min }) #7091

MattiasBuelens opened this issue Nov 13, 2023 · 0 comments
Labels
enhancement New feature or request web-api Something that relates to a standard Web API

Comments

@MattiasBuelens
Copy link

MattiasBuelens commented Nov 13, 2023

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 to byobReader.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 by length 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 this readInto() example.

What is the feature you are proposing to solve the problem?

The Streams specification has been updated to add an optional min option to byobReader.read(view). When the min option is given, the read will only be fulfilled as soon as min 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:

const reader = stream.getReader({ mode: "byob" });
const lengthResult = await reader.read(new Uint32Array(1));
if (lengthResult.done) throw new Error("unexpected end of file");
const length = lengthResult.value[0];
const payloadResult = await reader.read(new Uint8Array(length), { min: length });
if (payloadResult.done) throw new Error("unexpected end of file");
return payloadResult.value;

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

@MattiasBuelens MattiasBuelens added the enhancement New feature or request label Nov 13, 2023
@Electroid Electroid added the web-api Something that relates to a standard Web API label Nov 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request web-api Something that relates to a standard Web API
Projects
None yet
Development

No branches or pull requests

2 participants