Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: add example for piping ReadableStream #56415

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
gabrielschulhof marked this conversation as resolved.
Show resolved Hide resolved
```

```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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this promise is not awaited, then something else needs to make sure there is enough i/o scheduled on the event loop to ensure the process does not close before the pipe completes.
 

Suggested change
stream.pipeTo(createWritableStreamSomehow());
stream.pipeTo(createWritableStreamSomehow());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasnell I put this in an async IIFE and awaited the promise.

```

### Class: `ReadableStreamDefaultReader`

<!-- YAML
Expand Down
Loading