-
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.prototype.arrayBuffer() #1019
Comments
A more generic method might be If we had that, then we'd just need a way of turning an array of Uint8Arrays into a single Uint8Array. Surprisingly I can't find any easy way to do this, especially not performantly. So, maybe that is not the right path. |
Probably also want to consider an equivalent API where the buffer is already allocated (similar to |
I like the idea of a method that fills up an entire It would be different from |
How about:
Maybe we can look at |
The It would be nice to have all array/blob/text/arrayBuffer - hopefully node streams would follow suit than you could use express.js to do stuff like app.post('route', (req, res) => {
await req.arrayBuffer()
})
Not sure what you mean by this Blob constructor can take any kind of object you throw at it. The default fallback behavior is to cast anything it can't handle into a string... |
That would have delayed shipping fetch and therefore service workers for many years. Also, the methods on
Yeah, agreed. My intention was: Chunks can be anything that can appear in the sequence used in the blob constructor. As you say, almost everything is acceptable, with a few exceptions like detached buffers. |
FWIW, +1 to a readFully method on BYOB readers. Maybe a new issue for it? |
An interesting use of See https://stackoverflow.com/questions/67362823/progress-for-a-fetch-blob-javascript/67364010#67364010 for an example we could make much cleaner. |
Looks like Node has just implemented this for arrayBuffer, blob, json, and text, but the API is a bit different. |
Any chance of this ever landing? It's amazing to me that there's no "stream.collect" equivalent. |
just figured if this could be part of something like iterator helpers |
As mentioned above, iterator helpers have a And iterator helpers are intended as fully general utilities, so they're probably not the right place to have a method specifically for producers which yield chunks of bytes. That's a pretty uncommon thing to do with iterators anyway; the ReadableStream machinery is much better suited to handle such producers efficiently. |
I believe that is native streaming instruments had better api they would be used more, which should not be a controversial opinion.. |
Related: #1238 |
We would like to implement this in Bun, along with We also would be interested in implementing |
Can I suggest instead having a |
I'm happy with this approach also (in contrast to what I suggest in #1238). A key challenge I have with having these methods directly on
const chunks = Array.fromAsync(readable);
Honestly, the more I think about it, the more I'd really prefer the const u8 = await Uint8Array.fromAsync(iterable); // iterable must provide BufferSources
const str = await String.fromAsync(iterable); // iterable must provide stringifiable chunks
const val = await JSON.fromAsync(iterable); // iterable must provide stringifiable chunks
// etc For text encoding, the pattern is pretty natural: const str = await String.fromAsync(readable.pipeThrough(new TextDecoderStream("..."))); |
If the streams are passing strings that should be fine too for
From an implementer perspective, it sounds nice. From a JS developer perspective, I have trouble imagining developers prefer this over My gut here is that most JS developers would prefer the idea of both, but probably 98% of all usage would be one of People are used to This chain of reasoning is more complicated:
Compared to:
Isn't there precedent for defaulting to UTF-8? Or is that Windows-1252?
I think this pattern composes well, but would only feel natural to developers who understand a lot of streams and spec authors. It would feel complicated for less experienced developers |
I would guess that something like |
There is precedent for enforcing UTF-8 for |
It looks like we have a plan here, and we just need someone to do the work. I don't have time to commit to this in the near future, but if someone has time to draft a PR, I'd be happy to look at it. I believe we have rough consensus on the following issues:
|
I think it's worth considering doing |
I like |
|
No concrete plans, but PRs will be accepted. |
All SGTM! |
I have whatwg/fetch#1732 but there's no expressed implementer interest yet. |
Maybe I'm missing something but I don't see this part in this thread, has there been discussion about this somewhere else? |
This was based on @jimmywarting However, on re-reading I realised that @jakearchibald was only saying this should work for the Anyway, it looks like we don't have consensus on this item, so let's discuss it a bit more. First question: should all of Arguments for:
Arguments against:
Anything else? |
I vote yes. My second question: do we really want to accept everything possible in non-byte ReadableStream instead of simply requiring ArrayBufferView as ReadableByteStreamControllerEnqueue does? If yes, should we also accept everything possible in the byte stream controller? |
Is there an equivalent for getting the If I had a vote I'd say "yes, but only buffer sources". Allowing Blobs and strings in a constructor is not the same as allowing them in a stream. |
I've made a first attempt at drafting some spec around this in #1311 ... it intentionally limits to just text, Uint8Array, and Blob, ignoring |
#1311 allows cancellation of |
Yeah, definitely worth talking about that point. I had considered make it so that it would only cancel the read loop and not the underlying stream. A |
It would be good to have a convenience method to concatenate the contents of a ReadableStream into a (promise for a) Uint8Array.
It is unclear what should happen if the stream contains chunks that are not ArrayBuffer or ArrayBufferViews.
Other similar methods we might consider are
.blob()
and.text()
.The text was updated successfully, but these errors were encountered: