mirror of https://github.com/nodejs/node.git
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
parent
35742a2d0b
commit
9731d64f13
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue