[whatwg/streams] Proposal: ReadableStreamBodyReader (Issue #1238)

Consuming a `ReadableStream` as either an `ArrayBuffer`, `Blob`, `FormData`, JSON, or text is a common use case. We can see this in `Body` mixin from fetch and the methods implemented by `Request` and `Response`, as well as in utilities such as Node.js' custom "stream consumers" documented here: https://nodejs.org/dist/latest-v18.x/docs/api/webstreams.html#utility-consumers

The pattern is so common, it would be helpful to be able to standardize the pattern with a new type of standard reader type... Specifically something like:

```
[Exposed=(Window,Worker)]
interface ReadableStreamBodyReader {
  constructor(ReadableStream stream);

  readonly attribute boolean bodyUsed;
  [NewObject] Promise<ArrayBuffer> arrayBuffer();
  [NewObject] Promise<Blob> blob();
  [NewObject] Promise<FormData> formData();
  [NewObject] Promise<any> json();
  [NewObject] Promise<USVString> text();

  undefined releaseLock();
}
ReadableStreamBodyReader includes ReadableStreamGenericReader;
```

Use example:

```js
const readable = getReadableStreamSomehow();
const reader = readable.getReader({ mode: 'body' });
await reader.arrayBuffer();
// or
await reader.text();
// or
await reader.json();
// or
await reader.formData();
// or
await reader.blob();
```

Note that the `ReadableStreamGenericReader` could but does not just include the `Body` mixin. This is because the `Body` mixin includes the `body` getter that really wouldn't make sense in this case. We *could* use it, however, if that is the preference.

The key benefit of adding this is that it would standardize what is becoming a common pattern.

-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/issues/1238
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/streams/issues/1238@github.com>

Received on Tuesday, 5 July 2022 16:42:50 UTC