Skip to content

Conversation

harrishancock
Copy link
Contributor

This compatibility flag is new as of this week.

Note that the text references a relatively new, non-standard method that we added to ReadableStream: readAtLeast(). Unfortunately, this method is not yet documented.

/cc @jasnell

…flag

This compatibility flag is new as of this week.

Note that the text references a relatively new, non-standard method that we added to ReadableStream: `readAtLeast()`. Unfortunately, this method is not yet documented.

Originally, the Workers runtime did not detach the `ArrayBuffer`s from user-provided TypedArrays when using the [BYOB reader's `read()` method](/runtime-apis/streams/readablestreambyobreader#methods), as required by the Streams spec, meaning it was possible to inadvertently reuse the same buffer for multiple `read()` calls. This change brings us in line with the spec.

User code should never try to reuse an `ArrayBuffer` that has been passed into a [BYOB reader's `read()` method](/runtime-apis/streams/readablestreambyobreader#methods). The more recently added extension method `readAtLeast()` will always detach the `ArrayBuffer` and is unaffected by this feature flag setting.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can reuse the buffer, just not under the same reference? That is, when read() returns, it gives you a new reference to the buffer, which you can then use in the next call. This seems like the whole point of BYOB, to avoid allocation overhead, so we should probably be a bit clearer here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a little example.

@kentonv
Copy link
Member

kentonv commented Nov 22, 2021

Sorry for failing to see this earlier, I have this repo muted because it was sending me too many notifications that weren't relevant to me... but that means I also missed a code review I was actually tagged on. :(

@harrishancock harrishancock force-pushed the harris/add-streams-byob-reader-detaches-buffer-compatflag branch from a3d1b4b to 67b5c61 Compare November 25, 2021 16:54
@harrishancock harrishancock force-pushed the harris/add-streams-byob-reader-detaches-buffer-compatflag branch from 67b5c61 to e9f0839 Compare November 25, 2021 16:55
@kentonv
Copy link
Member

kentonv commented Nov 29, 2021

LGTM but I guess my approval is not good enough as I'm not an owner.

@kentonv kentonv merged commit d30cd80 into production Dec 1, 2021
@kentonv kentonv deleted the harris/add-streams-byob-reader-detaches-buffer-compatflag branch December 1, 2021 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

5 participants