Skip to content
Closed
Changes from all commits
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
85 changes: 85 additions & 0 deletions doc/api/webstreams.md
Original file line number Diff line number Diff line change
Expand Up @@ -1695,3 +1695,88 @@ text(readable).then((data) => {

[Streams]: stream.md
[WHATWG Streams Standard]: https://streams.spec.whatwg.org/

## .toWeb() and .fromWeb()

The `.toWeb()` and `.fromWeb()` methods on `Readable` and `Writable` streams allow you to convert a Node.js stream to a web stream and vice versa.

```
The `.toWeb()` and `.fromWeb()` methods are new in Node.js 17 and require the
`node:stream/web` module to be imported.

See: https://nodejs.org/docs/latest-v17.x/api/node:stream/web.html
```

### .toWeb()

The `.toWeb()` method converts a Node.js stream to a web stream. It takes a Node.js stream as input and returns a `ReadableStream` or `WritableStream` object.

The following example shows how to convert a Node.js `Readable` stream to a web `ReadableStream` object:

```js
const { Readable } = require('stream');
const { ReadableStream } = require('node:stream/web');

const nodeStream = new Readable();

const webStream = nodeStream.toWeb();

// Consume the web stream using the async iterator protocol.
for await (const chunk of webStream) {
console.log(chunk);
}
```

The following example shows how to convert a Node.js `Writable` stream to a web `WritableStream` object:

```js
const { Writable } = require('stream');
const { WritableStream } = require('node:stream/web');

const nodeStream = new Writable();

const webStream = nodeStream.toWeb();

// Write data to the web stream using the async iterator protocol.
await webStream.write('Hello, world!');

// Close the web stream.
await webStream.close();
```

### .fromWeb()

The `.fromWeb()` method converts a web stream to a Node.js stream. It takes a `ReadableStream` or `WritableStream` object as input and returns a Node.js `Readable` or `Writable` stream.

The following example shows how to convert a web `ReadableStream` object to a Node.js `Readable` stream:
Comment on lines +1749 to +1751

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
The `.fromWeb()` method converts a web stream to a Node.js stream. It takes a `ReadableStream` or `WritableStream` object as input and returns a Node.js `Readable` or `Writable` stream.
The following example shows how to convert a web `ReadableStream` object to a Node.js `Readable` stream:
The `.fromWeb()` method converts a web stream to a Node.js stream by taking a `ReadableStream` or `WritableStream` object as input and returning a Node.js `Readable` or `Writable` stream.
The following example demonstrates how to convert a web `ReadableStream` object into a Node.js `Readable` stream:


```js
const { Readable } = require('stream');
const { ReadableStream } = require('node:stream/web');

const webStream = new ReadableStream();

const nodeStream = webStream.fromWeb();

// Consume the Node.js stream using the async iterator protocol.
for await (const chunk of nodeStream) {
console.log(chunk);
}
```

The following example shows how to convert a web `WritableStream` object to a Node.js `Writable` stream:

```js
const { Writable } = require('stream');
const { WritableStream } = require('node:stream/web');

const webStream = new WritableStream();

const nodeStream = webStream.fromWeb();

// Write data to the Node.js stream using the async iterator protocol.
await nodeStream.write('Hello, world!');

// Close the Node.js stream.
await nodeStream.end();
```