-
Notifications
You must be signed in to change notification settings - Fork 332
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
Use a ReadableStream with byte source (formerly called ReadableByteStream) for .body #267
Comments
Currently the fetch spec allows constructing a Response with any ReadableStream, which can thus be enqueued with ArrayBuffers, Uint8Arrays or potatoes. Restricting the Response body to a ReadableStream with a byte source would remove the possibility to enqueue potatoes, leaving space for ArrayBuffer and ArrayBufferView. But enqueuing something else than Uint8Array would still lead to TypeError when consuming the body, since "read all bytes" requires Uint8Array objects. |
I thought we wanted to require Uint8Array? |
Yeah, a while ago we discussed whether we should perform some auto-conversion from array buffer views or blobs or strings, and decided to settle on being conservative and just allowing Uint8Array. I think the discussion was at yutakahirano/fetch-with-streams#53. |
"read all bytes" is not really doing any conversion, it merely appends a Uint8Array to a byte sequence. Any BufferSource would be as easily appendable there. I do not see what is gained with this restriction. yutakahirano/fetch-with-streams#53 seems mostly about upload. |
Yeah, though a |
@yutakahirano is this fixed? |
Not yet. |
This might not happen quickly, as we want to run an experiment in Chrome to ensure it is web-compatible to use a byte stream. In theory there is no problem, but AFAIK it's never been tested and it's not something we can afford to break. |
It seems we should do this before upload streams though. No reason not to have the best-in-class there. |
How will This might also affect other parts of the specification. For example, in this suggestion on #1144, we'd also want to clone a |
I don't understand what is proposed initially and why it blocks upload streams.
|
I'm not sure if I understand the comment, but a streaming request body containing a non- |
@yoichio if |
I understood
However I still don't understand this behavior. |
Sorry the part is underspecified. We use "create a proxy" in the spec, but it doesn't match our mental mode perfectly. The request and response body transferring between the page and the service worker should reject chunks other than Uint8Arrays, and it doesn't have to preserve the chunk boundary. |
@yutakahirano good point, it seems that should be somewhat straightforward to fix with the new "read incrementally" operation. |
It should be done by changing "Let requestForServiceWorker be a clone of request." part in HTTP fetch to something having chunk type checking? |
I suppose it's really only observable there, yeah. The network side already does the type check. In retrospect it would have been nicer if we could have done this closer to the source for both |
I was thinking about creating a TransformStream whose transformer
It may be good to talk with @ricea. |
I've thought the same thing. The question I got stuck on in the past is: do we want to define a "reasonable" chunk size to split/combine to? |
Personally I think it's fine to leave that |
This change addresses the inconsistency discussed in #267 that a chunk type in a stream body on uploading to the network is limited to Uint8Array, but passing it to a service worker is not restricted. This change limits the latter as well. Tests: web-platform-tests/wpt#28203.
Can we unblock this from upload-streaming-blocking following #1199 (comment) ? |
I think so, but it would be great if @domenic @MattiasBuelens or @ricea could elaborate on what BYOB stream adoption would mean for Fetch first and how feasible it is to get there if we ship things as-is (i.e., without BYOB). |
On a spec level, we're trying to tackle whatwg/streams#1121 to let the spec infrastructure create readable byte streams. The upgrade path from vanilla readable streams to byte readable streams is seamless: before, |
Can that be done currently on Chrome ?
|
No. First, the specification needs to be updated, that's what this issue is about. Afterwards, Chrome can implement the spec change and make it available to its users.
Note that a BYOB reader doesn't guarantee that it'll fill your entire 64 KB view on every |
Thank you @MattiasBuelens for the reply and the helpful suggestions. You just saved my day. |
2022 update: Firefox shipped byte stream as of version 102, and it shipped Fetch with byte stream (which was not really intentional but accidental IMO, given that there is still no relevant test). And thus this has been working on Firefox for months: new Response(new Uint8Array([1,2])).body.getReader({mode: 'byob'}) |
That's great news! It implies that it is web-compatible. We are currently evaluating the priority of implementing it in Chrome. |
Given #267 (comment) is the only thing that's missing here WPT tests or do we need a specification change still? |
whatwg/streams#1130 added "set up with byte reading support" algorithm, so I guess another work item here is to use that. |
Shall I interpret #267 (comment) as a support from Google? I can write a PR in that case. |
Yes. We strongly support using byte streams for |
Tests: web-platform-tests/wpt#37910. Fixes #267.
/cc @yutakahirano
ReadableByteStream has been merged into ReadableStream interface. It's time to update the ReadableStream section of the Fetch spec (https://fetch.spec.whatwg.org/#readablestream) to adopt the updated ReadableStream and construct a ReadableStream with the byte handling feature (i.e. getReader() returns a ReadableStreamBYOBReader when a certain option is passed).
In the first step, we don't need to fully use the BYOB responding API but may just call the same enqueue() method.
The text was updated successfully, but these errors were encountered: