[whatwg/streams] Teeing a readable byte stream (#1111)

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](https://streams.spec.whatwg.org/commit-snapshots/4b5b3cdb0f22a791fc1a8e734296a32a94ad753a/#readable-stream-tee), and adapt it a bit:
* Use `CreateReadableByteStream` instead of `CreateReadableStream` for creating the two branches. (Note that this abstract op was removed in #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.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/issues/1111

Received on Friday, 19 March 2021 16:07:12 UTC