-
Notifications
You must be signed in to change notification settings - Fork 161
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
ReadableStream.from(X) #1018
Comments
I wonder if we want to omit (4) for future-proofing, in case there are other idiomatic types we want to add to this. (3) is also somewhat dubious as it's not much value over What are code examples where this would be most helpful? |
The main motivating example is this from the CompressionStream explainer: async function compressArrayBuffer(in) {
const cs = new CompressionStream('deflate');
const writer = cs.writable.getWriter();
writer.write(in);
writer.close();
const out = [];
const reader = cs.readable.getReader();
let totalSize = 0;
while (true) {
const { value, done } = await reader.read();
if (done)
break;
out.push(value);
totalSize += value.byteLength;
}
const concatenated = new Uint8Array(totalSize);
let offset = 0;
for (const array of out) {
concatenated.set(array, offset);
offset += array.byteLength;
}
return concatenated;
} where it took 3 lines just to create a stream containing a single ArrayBuffer chunk, something that feels like it should be a one-liner. This is also a possible solution to whatwg/fetch#809. Edit: By "three lines" I meant turning const writer = cs.writable.getWriter();
writer.write(in);
writer.close(); into ReadableStream.from(in).pipeTo(cs.writable); |
I just realised that ReadableStream.from([in]).pipeTo(cs.writable); would also work, is only two more characters, and requires less DWIM. |
Awesome. Yeah just doing (1) and (2) seems totally uncontroversial (although it'll need a bit of Web IDL work, which I'm happy to do). |
like this proposal. this could make the conversion between node/whatwg stream easy if both have @@asyncIterator btw, An idea that poped up in whatwg/fetch#809 is if we maybe could have something like PS. have BYOB reader in mind also when designing this? |
That's not that far off from new Response(in).body.pipeTo(cs.writable); ...exacpt that Response can't handle iterators but it can handle a lot of other things like strings, FormData, blobs, typed arrays... and you can only get a byte stream back |
In the future, we could consider calling
You wouldn't benefit much from having a BYOB reader. We'll be reading chunks with That said, it would still be nice if there were an easy way to convert any stream with byte chunks into a proper byte stream, even if it incurred extra copies. Something like this could work: ReadableStream.from(x, { type: "bytes" }) This would be roughly equivalent to a "special" identity transform: ReadableStream.from(x).pipeThrough(new TransformStream({ readableType: "bytes" })) Note: this does not work yet. This assumes that |
Fwiw, I agree with @domenic. Allow 1 & 2, throw otherwise. |
Actually, thinking about it more: ReadableStream.from(uint8Array); Seems a bit odd that this creates a stream of numbers. Maybe we should special-case buffer source types, which would create a readable byte stream. Or have an opt-in like @MattiasBuelens suggests. ReadableStream.from(uint8Array, { type: 'bytes' }); Or, since the option impacts both the accepted input and produced output: ReadableStream.fromBytes(uint8Array); |
It would behave the same as |
I prefer this to making Is there a way to express |
Looks like a variadic operation is a thing in WebIDL, so yes. |
Thanks! |
I saw that node have already added Readable.from maybe would be grate if they could be similar (isomorphic)? |
If I'm reading the doc correctly, it will be identical, except we probably won't need an options parameter. |
If this is added it would be really nice if const chunks = ReadableStream.from(
chunkGenerator,
new CountQueueingStrategy({ highWaterMark: 10 }), // Try to have chunks available immediately when possible
); |
@Jamesernator I agree, that would be a good addition. We're still discussing whether we want to add that now, or leave it for a future extension. Feel free to chime in. 🙂 |
It would be good to have a convenience method to create a ReadableStream in a DWIM-ish fashion from something else.
Logic being something like
The text was updated successfully, but these errors were encountered: