[whatwg/streams] BYOB with "fill at least" semantics (#1175)

The BYOB reader can be a bit inefficient if the controller does not make an effort to fill the given view as much as possible. For the Cloudflare implementation, we're looking at options for a stream consumer to instruct the stream to fill a view with a specified minimum number of bytes before returning. For instance, "here's a 100 byte view, don't resolve the read until you've got at least 50 bytes in it".

In the current API, there are several ways we could achieve this:

1. We could add a new non-standard `readAtLeast()` method to the existing `ReadableStreamBYOBReader` interface, e.g. `const reader = stream.getReader({ mode: 'byob' }); await reader.readAtLeast(50, new Uint8Array(100));`. This is obviously not ideal unless the method became part of the standard interface.
2. We could create a new kind of reader such that, `const reader = new ReadableStreamAtLeastReader(stream); await reader.read(50, new Uint8Array(100));`. This avoids stepping on the standard but creates additional complexities on the implementation side.
3. Coupled with #2, we could introduce a new mode, `const reader = stream.getReader({ mode: 'at-least' })` that would return the new reader type from #2.
4. We could add an optional `options` to the existing `ReadableStreamBYOBReader`'s `read` method, e.g. `const reader = stream.getReader({ mode: 'byob'}); await reader.read(new Uint8Array(100), { atLeast: 50 });`, but again, this steps on the standard interface and isn't portable.

A key part of this is that we want the stream *consumer* to be able to indicate the minimum number of bytes they wish filled, and most of the time those consumers will not have any visibility to the underlying source that is filling the view (that is, there won't be any other way to independently tell the underlying source to fill the minimal number of bytes).

Before we choose a direction, I'd like to get a sense from the spec authors which direction they feel is most appropriate.

-- 
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/1175

Received on Wednesday, 13 October 2021 20:21:33 UTC