It is very common to consume a ReadableStream into a text, TypedArray, Blob, etc. In the fetch spec, there is the Body structure that exposes methods like text(), json(), blob(), etc for efficiently consuming the Request or Response payload. Unfortunately, for other types of ReadableStreams it's not so efficient and generally requires a fair amount of boilerplate. It would be nice to simplify.
async function text(readable) {
let text = '';
const dec = new TextDecoder();
for await (const chunk of readable) {
text += dec.decode(chunk, { stream: true });
}
text += dec.decode();
return text;
}
What solutions exist today?
The Response and Request objects have methods like async text(), async json(), etc ...
Node.js has a require('stream/adapters') API that implements these methods for ReadableStream and Node.js streams.
How would you solve it?
This proposal is to introduce a new ReadableStreamBodyReader to the streams spec with the definition. Like ReaableStreamBYOBReader, the ReadableStreamBodyReader would only be used with bytes type streams.
What problem are you trying to solve?
It is very common to consume a
ReadableStream
into a text, TypedArray, Blob, etc. In the fetch spec, there is theBody
structure that exposes methods liketext()
,json()
,blob()
, etc for efficiently consuming theRequest
orResponse
payload. Unfortunately, for other types ofReadableStream
s it's not so efficient and generally requires a fair amount of boilerplate. It would be nice to simplify.What solutions exist today?
The
Response
andRequest
objects have methods likeasync text()
,async json()
, etc ...Node.js has a
require('stream/adapters')
API that implements these methods forReadableStream
and Node.js streams.How would you solve it?
This proposal is to introduce a new
ReadableStreamBodyReader
to the streams spec with the definition. LikeReaableStreamBYOBReader
, theReadableStreamBodyReader
would only be used withbytes
type streams.Example use:
This model would allow implementations to optimize the read operation internally as much as possible and eliminate a fair amount of boilerplate.
Anything else?
No response