Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make response body streams readable byte streams #1246

Closed
wants to merge 5 commits into from

Conversation

domenic
Copy link
Member

@domenic domenic commented Jun 2, 2021

Depends on whatwg/streams#1130. Closes #267.

  • At least two implementers are interested (and none opposed):
    • I think most implementers have expressed interest, but I'm not sure it's on anyone's near-term plan to implement this, so we may want to leave this PR open for a while...
  • Tests are written and can be reviewed and commented upon at:
    • TODO
  • Implementation bugs are filed:
    • Chrome: TODO
    • Firefox: TODO
    • Safari: TODO

(See WHATWG Working Mode: Changes for more details.)


Preview | Diff

@domenic domenic added addition/proposal New features or enhancements topic: streams labels Jun 2, 2021
@MattiasBuelens
Copy link

We're also blocked by whatwg/streams#1114. When "clone a body" tees a stream, the result must also be a readable byte stream.

fetch.bs Outdated
<li><p><a for=ReadableStream>Enqueue</a> a {{Uint8Array}} wrapping an {{ArrayBuffer}}
containing <var>bytes</var> into <var>stream</var>.
<li><p><a for=ReadableStream>Enqueue bytes</a> into <var>stream</var> given
<var>bytes</var>.
Copy link
Member

Choose a reason for hiding this comment

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

There is an existing problem that this needs to queue a task. I can PR that, but this will have to be rebased then.

Copy link
Member Author

Choose a reason for hiding this comment

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

Re-reading this section it feels like most of the section needs to take place in a task. (I.e., everything dealing with stream.) Just the reading of the bytes from the network needs to be done in parallel.

@annevk annevk force-pushed the readable-byte-streams branch from fcf2e88 to 3f03a81 Compare July 19, 2021 12:53
Copy link
Member

@annevk annevk left a comment

Choose a reason for hiding this comment

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

I personally prefer not using commas before and if there are only two "simple" things.

What I don't understand here is that you still get a fresh view if the BYOB view cannot be written into due to its size. Wouldn't an error or request for a new view be more appropriate? If the idea is to avoid allocations presumably you would not want such allocations to nevertheless happen silently sometimes?

<li><p>Let <var>view</var> be null.

<li><p>If <var>stream</var>'s <a for=ReadableStream>current BYOB request view</a> is
non-null, and <var>bytes</var>'s <a for="byte sequence">length</a> is less than
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
non-null, and <var>bytes</var>'s <a for="byte sequence">length</a> is less than
non-null and <var>bytes</var>'s <a for="byte sequence">length</a> is less than

non-null, and <var>bytes</var>'s <a for="byte sequence">length</a> is less than
<var>stream</var>'s <a for=ReadableStream>current BYOB request view</a>'s
<a for=BufferSource>byte length</a>, then set <var>view</var> to <var>stream</var>'s
<a for=ReadableStream>current BYOB request view</a>, and <a for=ArrayBufferView>write</a>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<a for=ReadableStream>current BYOB request view</a>, and <a for=ArrayBufferView>write</a>
<a for=ReadableStream>current BYOB request view</a> and <a for=ArrayBufferView>write</a>

@domenic
Copy link
Member Author

domenic commented Jul 19, 2021

What I don't understand here is that you still get a fresh view if the BYOB view cannot be written into due to its size. Wouldn't an error or request for a new view be more appropriate? If the idea is to avoid allocations presumably you would not want such allocations to nevertheless happen silently sometimes?

Interesting. This is the first time we're hearing this feedback.

I'm not sure how these ideas would work. If you ask for a 1 KiB buffer, and Fetch is unable to restrict what it pulls from the network to less than 16 KiB, you'd get an error... and then what would you do? At that point Fetch has 16 KiB in memory in a new buffer. The only things we can do are:

  • Give you the first 1 KiB of the 16 KiB buffer, and next time you ask give you (the requested portion of) the remaining 15 KiB; or
  • Give you the entire 16 KiB buffer anyway.

The first option is more friendly toward people who are using BYOB readers to read specific numbers of bytes, e.g. when parsing a protocol, and so is what we chose.

I don't quite understand what you mean by "request for a new view" as opposed to error.

@annevk
Copy link
Member

annevk commented Jul 20, 2021

We discussed this further in https://matrixlogs.bakkot.com/WHATWG/2021-07-19#L105-L119. I'm a bit surprised at all the transfers, but I suppose that is to avoid exposing races.

annevk added a commit that referenced this pull request Jul 20, 2021
Helps with #1246.

The main problem with this PR (which is why I marked it WIP) is that the stream itself is created in parallel and I don't see a good way to avoid that. If we had low-level streams this would be different.
@annevk annevk mentioned this pull request Jul 20, 2021
3 tasks
@domenic
Copy link
Member Author

domenic commented Mar 24, 2023

Superseded by #1593; somehow I forgot this existed when reviewing that :)

@domenic domenic closed this Mar 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition/proposal New features or enhancements topic: streams
Development

Successfully merging this pull request may close these issues.

Use a ReadableStream with byte source (formerly called ReadableByteStream) for .body
3 participants