-
Notifications
You must be signed in to change notification settings - Fork 337
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
Allow request take AsyncIterable body? #1291
Comments
Maybe. However, we are going to add ReadableStream.from() to the Streams Standard which will at least make it trivial to construct a body from an |
Remember asking the exact same question a long time ago, but it got rejected by ReadableStream.from() |
@jimmywarting I don't think it got rejected, but you suggested to close it in favor of that API. Or am I missing something? |
yea, pretty much |
I really think this should be part of the standard. It makes Response so much easier to work with in Node.js, as we need to maintain backward compatibility with Node streams without incurring in the overhead of wrapping one stream type in another. (Node.js ships this exactly for this reason). |
If this change is welcomed, I’m happy to champion this change and send a PR for it. |
In principle I'm supportive of this. The part that seems most tricky is how to make this work IDL-wise. |
Some guidance on this would be fantastic. |
I have opened a PR to Web IDL with an attempt for how to make this work IDL wise: whatwg/webidl#1397 |
We shipped a version of this in the wild as an experiment in node compat, and discovered the following issue that the spec will have to deal with that relates to boxed strings: new Response(new String("hello world")) Currently, boxed strings are cast to I have two proposed solutions:
Node does not seem to exhibit this behaviour because Node does not allow |
Treating string objects and values in the same way whenever possible seems preferable. Which argues for 1 as otherwise each API would have to deal with this anew. (Also, thanks for experimenting!) |
I had missed it but in Node.js' implementation of
It's a bit unfortunate that this ended up being shipped in Node.js prior to there being support defined in the standard but it is what it is. Unfortunately we (the workers runtime) are starting to have folks request this behavior in our implementation of fetch in the runtime in order to be compatible with Node.js. See cloudflare/workerd#2746 Specifically, users want to be able to pass a Node.js This is all a long-winded way of say that I think it would be worthwhile to go ahead and allow |
@jasnell, for a purely historical context, we borrowed that behavior from |
Bun also supports this non-standard behavior, and by way of documentation (not calling this out as a non-standard feature), potentially implicitly endorsing this pattern. Also, when I have an const stream = generateZipStream()
const buffer = await new Response(stream).arrayBuffer() Admittedly the above code looks a bit cursed, but given that By the way, I later discovered that as of now, the above code does not work in Deno and in browsers. So I had to change it to this, but then compatibility with Node 20 is broken: const stream = generateZipStream()
const buffer = await new Blob(await Array.fromAsync(stream)).arrayBuffer() To test if a runtime already supports this feature: console.log(await new Response((async function* () { yield "Hello, "; yield "world!" })()).text())
|
Would be possible/make sense to add
AsyncIterable
to the extract body algorithm? This would help with interopt with Node streams when implementing fetch in Node without violating the spec.The text was updated successfully, but these errors were encountered: