doc: add example for piping ReadableStream

When piping a `ReadableStream` created from an `Iterable` into a
`WritableStream`, the sequence of objects in the `Iterable` must
consist of either `Buffer`s, `TypedArray`s, or `DataView`s.

Re: https://github.com/nodejs/node/issues/56297
PR-URL: https://github.com/nodejs/node/pull/56415
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Ulises Gascón <ulisesgascongonzalez@gmail.com>
pull/56417/head
Gabriel Schulhof 2025-01-01 11:44:30 -08:00 committed by GitHub
parent 35742a2d0b
commit 9731d64f13
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 35 additions and 0 deletions

View File

@ -436,6 +436,41 @@ async function* asyncIterableGenerator() {
})();
```
To pipe the resulting {ReadableStream} into a {WritableStream} the {Iterable}
should yield a sequence of {Buffer}, {TypedArray}, or {DataView} objects.
```mjs
import { ReadableStream } from 'node:stream/web';
import { Buffer } from 'node:buffer';
async function* asyncIterableGenerator() {
yield Buffer.from('a');
yield Buffer.from('b');
yield Buffer.from('c');
}
const stream = ReadableStream.from(asyncIterableGenerator());
await stream.pipeTo(createWritableStreamSomehow());
```
```cjs
const { ReadableStream } = require('node:stream/web');
const { Buffer } = require('node:buffer');
async function* asyncIterableGenerator() {
yield Buffer.from('a');
yield Buffer.from('b');
yield Buffer.from('c');
}
const stream = ReadableStream.from(asyncIterableGenerator());
(async () => {
await stream.pipeTo(createWritableStreamSomehow());
})();
```
### Class: `ReadableStreamDefaultReader`
<!-- YAML