whatwg / streams

Streams Standard
https://streams.spec.whatwg.org/
Other
1.34k stars 155 forks source link

"read all bytes" algorithm support for stringifiers #1312

Closed jasnell closed 2 months ago

jasnell commented 2 months ago

What problem are you trying to solve?

The read all bytes algorithm currently defined in the spec is currently limited to Uint8Array values:

image

It would be helpful if this algorithm allowed for values with stringifiers to be used as well. Specifically, if the value has a stringifier then the value would be converted to a string then UTF-8 encoded for the purpose of the read all bytes algorithm. This would allow, for instance, a stream of strings or various types of objects to be read:

const rs = ReadableStream.from(['hello', ' ', {toString() { return 'world'; }}]);
const res = new Response(rs);
await res.text(); // 'hello world'

What solutions exist today?

No response

How would you solve it?

No response

Anything else?

No response

domenic commented 2 months ago

I'm pretty firmly against this. Byte streams should be limited to streams of Uint8Arrays. Use a transform stream if you're getting the data in the wrong format. Then you can clearly choose the correct encoding, for example, instead of assuming UTF-8.