[whatwg/streams] Efficient mapping to Unix-style APIs with pollable readers and writers (Issue #1265)

When cross-compiling code written for native platforms to WebAssembly developers need to implement the `poll`, `read` and `write` system calls on top of a `ReadableStream` or `WritableStream`. If we make the simplifying assumption that only file descriptors opened in non-blocking mode need to be supported then we need a way to register a callback for when a stream is readable or writable and to issue a read or write which will only do as much work as can be completed synchronously.

While this behavior can mostly be polyfilled (for example, by calling `read()` when a stream is polled and marking it readable when the read completes) I have not figured out a way to preserve the copy-minimizing benefits of BYOB readers because there's no way to guarantee that data is written to the BYOB buffer synchronously and so an intermediate buffer is required.

This could be resolved by adding two new options to the `ReadableStreamBYOBReader` interface (and similar changes for writable streams):

* a new `peek()` method which returns a `Promise` that resolves when there is data in the internal queue or the underlying source has indicated that there is data available.
* a `sync: true` option to `read()` which will cause it to return a `ReadableStreamReadResult` rather than a `Promise<ReadableStreamReadResult>` if there was data in the internal queue or the underlying source responded to `pull()` by enqueuing a buffer synchronously. Otherwise it would return `null` or some other sentinel value.

Synchronous reads probably have to be signaled to the underlying source as well because changing the value of `byobRequest` midway through a `read()` operation would likely cause problems.

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

Message ID: <whatwg/streams/issues/1265@github.com>

Received on Wednesday, 5 April 2023 00:29:29 UTC