Re: [whatwg/streams] Add support for ReadableStream "transfer" type (PR #1271)

> I really like the syntactic sugar personally, and I think being a different `type` of stream is a good enough excuse to use it. I'd like to get other people's input on this because there may be footguns I'm overlooking.

I would be most comfortable with requiring `enqueue(videoFrame, { transfer: [videoFrame] })`. That is the most like other web platform APIs that do transferring, such as `structuredClone()` and `postMessage()`. (Note: the `{ transfer: [x] }` form is the modern version that replaces `[x]`, so indeed, @MattiasBuelens's suggestion in https://github.com/whatwg/streams/pull/1271#issuecomment-1504867016 is a good one.)

However, I recognize it is verbose. So we might want to blaze a new path here and have `enqueue()` be the first sort of "auto-transfer" API. We should think carefully about how that works, and what pattern we might be setting up for future such APIs.

> For testing, it seems we use node.js with WPT and the ref implementation.
> Are there any transferable object like MessagePort supported in node.js?
> Ditto for serializable, with say VideoFrame?

You might run up against the limits of the reference implementation here, for which I apologize. It appears that for transferrable streams https://github.com/whatwg/streams/pull/1053 we did not make any reference implementation changes and just skipped running the relevant tests against the reference implementation. But that was mostly fine because it didn't change the streams API itself. This change is more intrusive to the streams API so it would be a real shame to let the reference implementation get out of sync.

Your main tools for trying to tackle this are:

- Patching the `window` in which the reference implementation and test runs: https://github.com/whatwg/streams/blob/main/reference-implementation/run-web-platform-tests.js#L48 . In particular you could put things from the Node.js environment in here.
- Node.js does support `globalThis.structuredClone`: https://nodejs.org/api/all.html#all_globals_structuredclonevalue-options . I think that might be enough to implement what you need, with some creativity.
- ArrayBuffers are transferable, and plain JS objects are cloneable. So if you put those in the tests you write, it should work in any environment. You probably also want to add separate WPTs for VideoFrame specifically, since that's an important use case, but keep those in a separate file from the generic clone/transfer ones, so that we can exclude them when running reference implementation tests in Node.

-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/pull/1271#issuecomment-1506547031
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/streams/pull/1271/c1506547031@github.com>

Received on Thursday, 13 April 2023 08:18:25 UTC