GeezerVison is showing github.com — doc: add example for piping ReadableStream · nodejs/node@03df76c
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: doc/api/webstreams.md +35Lines changed: 35 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -436,6 +436,41 @@ async function* asyncIterableGenerator() { |
436 | 436 | })(); |
437 | 437 | ``` |
438 | 438 |
|
| 439 | +To pipe the resulting {ReadableStream} into a {WritableStream} the {Iterable} |
| 440 | +should yield a sequence of {Buffer}, {TypedArray}, or {DataView} objects. |
| 441 | + |
| 442 | +```mjs |
| 443 | +import { ReadableStream } from 'node:stream/web'; |
| 444 | +import { Buffer } from 'node:buffer'; |
| 445 | + |
| 446 | +async function* asyncIterableGenerator() { |
| 447 | + yield Buffer.from('a'); |
| 448 | + yield Buffer.from('b'); |
| 449 | + yield Buffer.from('c'); |
| 450 | +} |
| 451 | + |
| 452 | +const stream = ReadableStream.from(asyncIterableGenerator()); |
| 453 | + |
| 454 | +await stream.pipeTo(createWritableStreamSomehow()); |
| 455 | +``` |
| 456 | + |
| 457 | +```cjs |
| 458 | +const { ReadableStream } = require('node:stream/web'); |
| 459 | +const { Buffer } = require('node:buffer'); |
| 460 | + |
| 461 | +async function* asyncIterableGenerator() { |
| 462 | + yield Buffer.from('a'); |
| 463 | + yield Buffer.from('b'); |
| 464 | + yield Buffer.from('c'); |
| 465 | +} |
| 466 | + |
| 467 | +const stream = ReadableStream.from(asyncIterableGenerator()); |
| 468 | + |
| 469 | +(async () => { |
| 470 | + await stream.pipeTo(createWritableStreamSomehow()); |
| 471 | +})(); |
| 472 | +``` |
| 473 | + |
439 | 474 | ### Class: `ReadableStreamDefaultReader` |
440 | 475 |
|
441 | 476 | <!-- YAML |
|