From 03df76cdec7939ac1e1d26086f8cda350084a0aa Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Wed, 1 Jan 2025 11:44:30 -0800 Subject: [PATCH] doc: add example for piping ReadableStream MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/nodejs/node/issues/56297 PR-URL: https://github.com/nodejs/node/pull/56415 Reviewed-By: Luigi Pinca Reviewed-By: Chemi Atlow Reviewed-By: Ulises Gascón --- doc/api/webstreams.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/doc/api/webstreams.md b/doc/api/webstreams.md index a5089796c219ad..f5b2496036fe7c 100644 --- a/doc/api/webstreams.md +++ b/doc/api/webstreams.md @@ -436,6 +436,41 @@ 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()); + +await 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()); + +(async () => { + await stream.pipeTo(createWritableStreamSomehow()); +})(); +``` + ### Class: `ReadableStreamDefaultReader`