Re: [whatwg/fetch] Allow request take AsyncIterable body? (#1291)

I had missed it but in Node.js' implementation of `Request` they do already support passing an async iterator as the body of a request. 

```
async function* foo() { yield 'hello'; }
const req = new Request('http://example.org', { method: 'POST', duplex: 'half', body: foo() } );
console.log(await req.text());
```

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 https://github.com/cloudflare/workerd/issues/2746

Specifically, users want to be able to pass a Node.js `stream.Readable` as the body. Since Node.js `stream.Readable` can be consumed as an AsyncIterable, it works with Node.js' extended behavior. Yes, using `ReadableStream.from(nodeReadable)` works but is less ergonomic and, unfortunately there are already people in the ecosystem making use of `body: nodeReadable` as opposed to `body: ReadableStream.from(nodeReadable)`.

This is all a long-winded way of say that I think it would be worthwhile to go ahead and allow `body` to be an `AsyncIterable` or `Iterable`. 

-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/fetch/issues/1291#issuecomment-2405433410
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/fetch/issues/1291/2405433410@github.com>

Received on Thursday, 10 October 2024 15:26:42 UTC