mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
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>
This commit is contained in:
1 parent
8dc39e5e2e
commit
03df76cdec
1 file changed
+35
@@ -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
|
||||
|
||||
Reference in new issue
Block a user