Skip to content

Commit

Permalink
doc: add example for piping ReadableStream
Browse files Browse the repository at this point in the history
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: nodejs#56297
  • Loading branch information
gabrielschulhof committed Dec 30, 2024
1 parent b3f82fe commit c9f7ecf
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions doc/api/webstreams.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,39 @@ 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());

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());

stream.pipeTo(createWritableStreamSomehow());
```

### Class: `ReadableStreamDefaultReader`

<!-- YAML
Expand Down

0 comments on commit c9f7ecf

Please sign in to comment.