-
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
idea: Accept a iterator as a body #809
Comments
Would a constructor that takes an iterator and returns a ReadableStream of
those values fix this problem?
…On Fri, 14 Sep 2018, 15:59 Jimmy Wärting, ***@***.***> wrote:
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
// if using Node:// const { Request, Response } = require('node-fetch')
async function* iterator() {
yield Uint8Array.from([97, 98, 99])
}
await new Response(iterator()).text() // abcawait 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, view it on GitHub
<#809>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAFtmkATU9CAk1Blr3tMNwWGsijXFngHks5ua8RZgaJpZM4WpbrV>
.
|
I guess... I guess it could be useful to have a iterator to ReadableStream constructor converter for other purposes too, but then i would wish for this to be included in the steps of creating a body in the Request/Response constructor
But then I think we are digging a little bit into this issue also: w3c/FileAPI#40
so if all iterators had // make up some symbol name
Symbol.toStream = Symbol('toStream')
// polyfill iterator to have `Symbol.toStream`
// Not sure if i'm doing this correctly
Object.getPrototypeOf(generator())[Symbol.toStream] = function(){
// quick and dirty not optional hack to convert iterator to ReadableStream
// Should use ReadableStream + pull + this.next() instead...
return new Response([...this].join('')).body
}
// sample iterator
function* generator() {
yield 'abc'
yield 'def'
}
var iterator = generator()
var stream = iterator[Symbol.toStream]()
new Response(stream).text().then(console.log) // abcdef
// or just simply iterator if the spec understood Symbol.toStream
new Response(iterator).text().then(console.log) So instead of having a constructor to convert a iterable you can work with Symbol.toStream I'm digging the Symbol approach, would like to see that on the Blob prototype too |
ofc, this would be nice too: new ReadableStream({
pull: sync_or_async_iterator_or_generator
})
// Node
new Readable({
read: sync_or_async_iterator_or_generator
}) |
I feel like this proposal could definitely improve interoperability between the two most popular stream implementations at the moment: Node.js streams and WHATWG streams. Arguably, we could probably use iterators though as the foundation — pass in a stream and internally the logic is to actually call @jimmywarting shouldn't it be |
don't mather so much now, The blob implementation never got something like |
Not the only one that thought of it.
ref: |
close in favor of whatwg/streams#1018 ? |
Sure thing. |
I where just thinking, what if
Response
andRequest
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 yieldsUint8array
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
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 aBlob
or a whatwg stream firstTaking 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)
The text was updated successfully, but these errors were encountered: