[whatwg/fetch] idea: Accept a iterator as a body (#809)

I where just thinking, what if `Response` and `Request` where able to accept a `@@asyncIterator` and a `@@iterator`? 

That is the minium you need to create a producer that don't consume lot of RAM and are able to generate lot of data.

Node.js and the the browser have different streaming api's but what they both will have in common is the `@@asyncIterator` that yields a `Uint8array` chunks. 

Node's streaming api can be exported to browsers.
And whatwg-streams can be polyfilled in Node.

But having to handle both in a uniformed way is tough and requires a large overhead and a dependency of the one or the other. A iterator can easily be created with neither of them and works in both the browser and Node.js 

```js
// if using Node:
// const { Request, Response } = require('node-fetch')

async function* iterator() {
  yield Uint8Array.from([97, 98, 99])
}

await new Response(iterator()).text() // abc
await new Request(iterator()).text() // abc

// With either Node-stream or whatwg-stream you would be able to do:
await new Response(stream).text()
await new Request(stream).text()
// ...since it would be able to detect the `Symbol.asyncIterator`

```

There are already many modules out there that already uses node streaming api but none of them can be used by `window.fetch` together with Node's stream api unless they are converted to a `Blob` or a whatwg stream first

Taking jsZip, WebTorrent and socket.io-stream as an example, they all utilize Nodes streaming api. But the fetch api can't handle them cuz it's a different streaming api (unless you are using node-fetch which is the complete opposite since they can't handle whatwg-streams)


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

Received on Friday, 14 September 2018 15:00:09 UTC