From c9f7ecfcfbe9393f773fc466341c16bd7d3f5aa6 Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Mon, 30 Dec 2024 11:28:54 -0800 Subject: [PATCH 1/2] doc: add example for piping ReadableStream 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 --- doc/api/webstreams.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/doc/api/webstreams.md b/doc/api/webstreams.md index a5089796c219ad..3c94158b0ce6be 100644 --- a/doc/api/webstreams.md +++ b/doc/api/webstreams.md @@ -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`