From c9f1861c8e28536fee64460d17ab07a1199aa341 Mon Sep 17 00:00:00 2001 From: Keskar Date: Sun, 22 Oct 2023 21:09:34 +0530 Subject: [PATCH] Fixes: https://github.com/nodejs/node/issues/45381 --- doc/api/webstreams.md | 85 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/doc/api/webstreams.md b/doc/api/webstreams.md index dd6e46c3274..69240182c44 100644 --- a/doc/api/webstreams.md +++ b/doc/api/webstreams.md @@ -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: + +```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(); +```