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

Teeing a readable byte stream #1111

Closed
MattiasBuelens opened this issue Mar 19, 2021 · 1 comment
Closed

Teeing a readable byte stream #1111

MattiasBuelens opened this issue Mar 19, 2021 · 1 comment

Comments

@MattiasBuelens
Copy link
Collaborator

Currently, teeing a readable byte stream always returns two "default" readable streams. This prevents developers from using a BYOB reader on a tee'd byte stream.

I think we should change ReadableStreamTee to return two readable byte streams when given a readable byte stream as its input. This should also help with e.g. whatwg/fetch#267: when we make Response.body a readable byte stream, we need to make sure that Response.clone() creates two new byte streams as well.

An easy solution would be to start from the current ReadableStreamTee abstract op, and adapt it a bit:

  • Use CreateReadableByteStream instead of CreateReadableStream for creating the two branches. (Note that this abstract op was removed in Rewrite to use Web IDL, and generally modernize #1035 because it was unused, but it shouldn't be difficult to bring it back.)
  • Use the ReadableByteStreamController and its abstract ops instead of a ReadableStreamDefaultController.
  • Clone every read chunk, and enqueue the original chunk and the cloned chunk to the two branches. (If one of the two branches is already cancelled, we don't need to clone the chunk.)

Note that such an implementation would still use a ReadableStreamDefaultReader to read the byte chunks. This means any BYOB request from a tee'd stream cannot reach the original stream, so the original stream must always allocate a new buffer for every chunk and cannot make an informed decision on how big that buffer should be. It may be worth investigating if we can switch to a ReadableStreamBYOBReader inside the teeing logic whenever a tee'd branch has a pending BYOB request.

@domenic
Copy link
Member

domenic commented Mar 19, 2021

Right, I think the blocking question here has always been whether we want to do the easy solution or come up with a harder solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants