11cb0ef41Sopenharmony_ci# Web Streams API 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci<!--introduced_in=v16.5.0--> 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci<!-- YAML 61cb0ef41Sopenharmony_ciadded: v16.5.0 71cb0ef41Sopenharmony_cichanges: 81cb0ef41Sopenharmony_ci - version: v18.0.0 91cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/42225 101cb0ef41Sopenharmony_ci description: Use of this API no longer emit a runtime warning. 111cb0ef41Sopenharmony_ci--> 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ci> Stability: 1 - Experimental. 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ciAn implementation of the [WHATWG Streams Standard][]. 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci## Overview 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ciThe [WHATWG Streams Standard][] (or "web streams") defines an API for handling 201cb0ef41Sopenharmony_cistreaming data. It is similar to the Node.js [Streams][] API but emerged later 211cb0ef41Sopenharmony_ciand has become the "standard" API for streaming data across many JavaScript 221cb0ef41Sopenharmony_cienvironments. 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ciThere are three primary types of objects: 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci* `ReadableStream` - Represents a source of streaming data. 271cb0ef41Sopenharmony_ci* `WritableStream` - Represents a destination for streaming data. 281cb0ef41Sopenharmony_ci* `TransformStream` - Represents an algorithm for transforming streaming data. 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci### Example `ReadableStream` 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ciThis example creates a simple `ReadableStream` that pushes the current 331cb0ef41Sopenharmony_ci`performance.now()` timestamp once every second forever. An async iterable 341cb0ef41Sopenharmony_ciis used to read the data from the stream. 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci```mjs 371cb0ef41Sopenharmony_ciimport { 381cb0ef41Sopenharmony_ci ReadableStream, 391cb0ef41Sopenharmony_ci} from 'node:stream/web'; 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ciimport { 421cb0ef41Sopenharmony_ci setInterval as every, 431cb0ef41Sopenharmony_ci} from 'node:timers/promises'; 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ciimport { 461cb0ef41Sopenharmony_ci performance, 471cb0ef41Sopenharmony_ci} from 'node:perf_hooks'; 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ciconst SECOND = 1000; 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ciconst stream = new ReadableStream({ 521cb0ef41Sopenharmony_ci async start(controller) { 531cb0ef41Sopenharmony_ci for await (const _ of every(SECOND)) 541cb0ef41Sopenharmony_ci controller.enqueue(performance.now()); 551cb0ef41Sopenharmony_ci }, 561cb0ef41Sopenharmony_ci}); 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_cifor await (const value of stream) 591cb0ef41Sopenharmony_ci console.log(value); 601cb0ef41Sopenharmony_ci``` 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci```cjs 631cb0ef41Sopenharmony_ciconst { 641cb0ef41Sopenharmony_ci ReadableStream, 651cb0ef41Sopenharmony_ci} = require('node:stream/web'); 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ciconst { 681cb0ef41Sopenharmony_ci setInterval: every, 691cb0ef41Sopenharmony_ci} = require('node:timers/promises'); 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ciconst { 721cb0ef41Sopenharmony_ci performance, 731cb0ef41Sopenharmony_ci} = require('node:perf_hooks'); 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ciconst SECOND = 1000; 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ciconst stream = new ReadableStream({ 781cb0ef41Sopenharmony_ci async start(controller) { 791cb0ef41Sopenharmony_ci for await (const _ of every(SECOND)) 801cb0ef41Sopenharmony_ci controller.enqueue(performance.now()); 811cb0ef41Sopenharmony_ci }, 821cb0ef41Sopenharmony_ci}); 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ci(async () => { 851cb0ef41Sopenharmony_ci for await (const value of stream) 861cb0ef41Sopenharmony_ci console.log(value); 871cb0ef41Sopenharmony_ci})(); 881cb0ef41Sopenharmony_ci``` 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ci## API 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci### Class: `ReadableStream` 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ci<!-- YAML 951cb0ef41Sopenharmony_ciadded: v16.5.0 961cb0ef41Sopenharmony_cichanges: 971cb0ef41Sopenharmony_ci - version: v18.0.0 981cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/42225 991cb0ef41Sopenharmony_ci description: This class is now exposed on the global object. 1001cb0ef41Sopenharmony_ci--> 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci#### `new ReadableStream([underlyingSource [, strategy]])` 1031cb0ef41Sopenharmony_ci 1041cb0ef41Sopenharmony_ci<!-- YAML 1051cb0ef41Sopenharmony_ciadded: v16.5.0 1061cb0ef41Sopenharmony_ci--> 1071cb0ef41Sopenharmony_ci 1081cb0ef41Sopenharmony_ci<!--lint disable maximum-line-length remark-lint--> 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_ci* `underlyingSource` {Object} 1111cb0ef41Sopenharmony_ci * `start` {Function} A user-defined function that is invoked immediately when 1121cb0ef41Sopenharmony_ci the `ReadableStream` is created. 1131cb0ef41Sopenharmony_ci * `controller` {ReadableStreamDefaultController|ReadableByteStreamController} 1141cb0ef41Sopenharmony_ci * Returns: `undefined` or a promise fulfilled with `undefined`. 1151cb0ef41Sopenharmony_ci * `pull` {Function} A user-defined function that is called repeatedly when the 1161cb0ef41Sopenharmony_ci `ReadableStream` internal queue is not full. The operation may be sync or 1171cb0ef41Sopenharmony_ci async. If async, the function will not be called again until the previously 1181cb0ef41Sopenharmony_ci returned promise is fulfilled. 1191cb0ef41Sopenharmony_ci * `controller` {ReadableStreamDefaultController|ReadableByteStreamController} 1201cb0ef41Sopenharmony_ci * Returns: A promise fulfilled with `undefined`. 1211cb0ef41Sopenharmony_ci * `cancel` {Function} A user-defined function that is called when the 1221cb0ef41Sopenharmony_ci `ReadableStream` is canceled. 1231cb0ef41Sopenharmony_ci * `reason` {any} 1241cb0ef41Sopenharmony_ci * Returns: A promise fulfilled with `undefined`. 1251cb0ef41Sopenharmony_ci * `type` {string} Must be `'bytes'` or `undefined`. 1261cb0ef41Sopenharmony_ci * `autoAllocateChunkSize` {number} Used only when `type` is equal to 1271cb0ef41Sopenharmony_ci `'bytes'`. When set to a non-zero value a view buffer is automatically 1281cb0ef41Sopenharmony_ci allocated to `ReadableByteStreamController.byobRequest`. When not set 1291cb0ef41Sopenharmony_ci one must use stream's internal queues to transfer data via the default 1301cb0ef41Sopenharmony_ci reader `ReadableStreamDefaultReader`. 1311cb0ef41Sopenharmony_ci* `strategy` {Object} 1321cb0ef41Sopenharmony_ci * `highWaterMark` {number} The maximum internal queue size before backpressure 1331cb0ef41Sopenharmony_ci is applied. 1341cb0ef41Sopenharmony_ci * `size` {Function} A user-defined function used to identify the size of each 1351cb0ef41Sopenharmony_ci chunk of data. 1361cb0ef41Sopenharmony_ci * `chunk` {any} 1371cb0ef41Sopenharmony_ci * Returns: {number} 1381cb0ef41Sopenharmony_ci 1391cb0ef41Sopenharmony_ci<!--lint enable maximum-line-length remark-lint--> 1401cb0ef41Sopenharmony_ci 1411cb0ef41Sopenharmony_ci#### `readableStream.locked` 1421cb0ef41Sopenharmony_ci 1431cb0ef41Sopenharmony_ci<!-- YAML 1441cb0ef41Sopenharmony_ciadded: v16.5.0 1451cb0ef41Sopenharmony_ci--> 1461cb0ef41Sopenharmony_ci 1471cb0ef41Sopenharmony_ci* Type: {boolean} Set to `true` if there is an active reader for this 1481cb0ef41Sopenharmony_ci {ReadableStream}. 1491cb0ef41Sopenharmony_ci 1501cb0ef41Sopenharmony_ciThe `readableStream.locked` property is `false` by default, and is 1511cb0ef41Sopenharmony_ciswitched to `true` while there is an active reader consuming the 1521cb0ef41Sopenharmony_cistream's data. 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_ci#### `readableStream.cancel([reason])` 1551cb0ef41Sopenharmony_ci 1561cb0ef41Sopenharmony_ci<!-- YAML 1571cb0ef41Sopenharmony_ciadded: v16.5.0 1581cb0ef41Sopenharmony_ci--> 1591cb0ef41Sopenharmony_ci 1601cb0ef41Sopenharmony_ci* `reason` {any} 1611cb0ef41Sopenharmony_ci* Returns: A promise fulfilled with `undefined` once cancelation has 1621cb0ef41Sopenharmony_ci been completed. 1631cb0ef41Sopenharmony_ci 1641cb0ef41Sopenharmony_ci#### `readableStream.getReader([options])` 1651cb0ef41Sopenharmony_ci 1661cb0ef41Sopenharmony_ci<!-- YAML 1671cb0ef41Sopenharmony_ciadded: v16.5.0 1681cb0ef41Sopenharmony_ci--> 1691cb0ef41Sopenharmony_ci 1701cb0ef41Sopenharmony_ci* `options` {Object} 1711cb0ef41Sopenharmony_ci * `mode` {string} `'byob'` or `undefined` 1721cb0ef41Sopenharmony_ci* Returns: {ReadableStreamDefaultReader|ReadableStreamBYOBReader} 1731cb0ef41Sopenharmony_ci 1741cb0ef41Sopenharmony_ci```mjs 1751cb0ef41Sopenharmony_ciimport { ReadableStream } from 'node:stream/web'; 1761cb0ef41Sopenharmony_ci 1771cb0ef41Sopenharmony_ciconst stream = new ReadableStream(); 1781cb0ef41Sopenharmony_ci 1791cb0ef41Sopenharmony_ciconst reader = stream.getReader(); 1801cb0ef41Sopenharmony_ci 1811cb0ef41Sopenharmony_ciconsole.log(await reader.read()); 1821cb0ef41Sopenharmony_ci``` 1831cb0ef41Sopenharmony_ci 1841cb0ef41Sopenharmony_ci```cjs 1851cb0ef41Sopenharmony_ciconst { ReadableStream } = require('node:stream/web'); 1861cb0ef41Sopenharmony_ci 1871cb0ef41Sopenharmony_ciconst stream = new ReadableStream(); 1881cb0ef41Sopenharmony_ci 1891cb0ef41Sopenharmony_ciconst reader = stream.getReader(); 1901cb0ef41Sopenharmony_ci 1911cb0ef41Sopenharmony_cireader.read().then(console.log); 1921cb0ef41Sopenharmony_ci``` 1931cb0ef41Sopenharmony_ci 1941cb0ef41Sopenharmony_ciCauses the `readableStream.locked` to be `true`. 1951cb0ef41Sopenharmony_ci 1961cb0ef41Sopenharmony_ci#### `readableStream.pipeThrough(transform[, options])` 1971cb0ef41Sopenharmony_ci 1981cb0ef41Sopenharmony_ci<!-- YAML 1991cb0ef41Sopenharmony_ciadded: v16.5.0 2001cb0ef41Sopenharmony_ci--> 2011cb0ef41Sopenharmony_ci 2021cb0ef41Sopenharmony_ci* `transform` {Object} 2031cb0ef41Sopenharmony_ci * `readable` {ReadableStream} The `ReadableStream` to which 2041cb0ef41Sopenharmony_ci `transform.writable` will push the potentially modified data 2051cb0ef41Sopenharmony_ci is receives from this `ReadableStream`. 2061cb0ef41Sopenharmony_ci * `writable` {WritableStream} The `WritableStream` to which this 2071cb0ef41Sopenharmony_ci `ReadableStream`'s data will be written. 2081cb0ef41Sopenharmony_ci* `options` {Object} 2091cb0ef41Sopenharmony_ci * `preventAbort` {boolean} When `true`, errors in this `ReadableStream` 2101cb0ef41Sopenharmony_ci will not cause `transform.writable` to be aborted. 2111cb0ef41Sopenharmony_ci * `preventCancel` {boolean} When `true`, errors in the destination 2121cb0ef41Sopenharmony_ci `transform.writable` do not cause this `ReadableStream` to be 2131cb0ef41Sopenharmony_ci canceled. 2141cb0ef41Sopenharmony_ci * `preventClose` {boolean} When `true`, closing this `ReadableStream` 2151cb0ef41Sopenharmony_ci does not cause `transform.writable` to be closed. 2161cb0ef41Sopenharmony_ci * `signal` {AbortSignal} Allows the transfer of data to be canceled 2171cb0ef41Sopenharmony_ci using an {AbortController}. 2181cb0ef41Sopenharmony_ci* Returns: {ReadableStream} From `transform.readable`. 2191cb0ef41Sopenharmony_ci 2201cb0ef41Sopenharmony_ciConnects this {ReadableStream} to the pair of {ReadableStream} and 2211cb0ef41Sopenharmony_ci{WritableStream} provided in the `transform` argument such that the 2221cb0ef41Sopenharmony_cidata from this {ReadableStream} is written in to `transform.writable`, 2231cb0ef41Sopenharmony_cipossibly transformed, then pushed to `transform.readable`. Once the 2241cb0ef41Sopenharmony_cipipeline is configured, `transform.readable` is returned. 2251cb0ef41Sopenharmony_ci 2261cb0ef41Sopenharmony_ciCauses the `readableStream.locked` to be `true` while the pipe operation 2271cb0ef41Sopenharmony_ciis active. 2281cb0ef41Sopenharmony_ci 2291cb0ef41Sopenharmony_ci```mjs 2301cb0ef41Sopenharmony_ciimport { 2311cb0ef41Sopenharmony_ci ReadableStream, 2321cb0ef41Sopenharmony_ci TransformStream, 2331cb0ef41Sopenharmony_ci} from 'node:stream/web'; 2341cb0ef41Sopenharmony_ci 2351cb0ef41Sopenharmony_ciconst stream = new ReadableStream({ 2361cb0ef41Sopenharmony_ci start(controller) { 2371cb0ef41Sopenharmony_ci controller.enqueue('a'); 2381cb0ef41Sopenharmony_ci }, 2391cb0ef41Sopenharmony_ci}); 2401cb0ef41Sopenharmony_ci 2411cb0ef41Sopenharmony_ciconst transform = new TransformStream({ 2421cb0ef41Sopenharmony_ci transform(chunk, controller) { 2431cb0ef41Sopenharmony_ci controller.enqueue(chunk.toUpperCase()); 2441cb0ef41Sopenharmony_ci }, 2451cb0ef41Sopenharmony_ci}); 2461cb0ef41Sopenharmony_ci 2471cb0ef41Sopenharmony_ciconst transformedStream = stream.pipeThrough(transform); 2481cb0ef41Sopenharmony_ci 2491cb0ef41Sopenharmony_cifor await (const chunk of transformedStream) 2501cb0ef41Sopenharmony_ci console.log(chunk); 2511cb0ef41Sopenharmony_ci``` 2521cb0ef41Sopenharmony_ci 2531cb0ef41Sopenharmony_ci```cjs 2541cb0ef41Sopenharmony_ciconst { 2551cb0ef41Sopenharmony_ci ReadableStream, 2561cb0ef41Sopenharmony_ci TransformStream, 2571cb0ef41Sopenharmony_ci} = require('node:stream/web'); 2581cb0ef41Sopenharmony_ci 2591cb0ef41Sopenharmony_ciconst stream = new ReadableStream({ 2601cb0ef41Sopenharmony_ci start(controller) { 2611cb0ef41Sopenharmony_ci controller.enqueue('a'); 2621cb0ef41Sopenharmony_ci }, 2631cb0ef41Sopenharmony_ci}); 2641cb0ef41Sopenharmony_ci 2651cb0ef41Sopenharmony_ciconst transform = new TransformStream({ 2661cb0ef41Sopenharmony_ci transform(chunk, controller) { 2671cb0ef41Sopenharmony_ci controller.enqueue(chunk.toUpperCase()); 2681cb0ef41Sopenharmony_ci }, 2691cb0ef41Sopenharmony_ci}); 2701cb0ef41Sopenharmony_ci 2711cb0ef41Sopenharmony_ciconst transformedStream = stream.pipeThrough(transform); 2721cb0ef41Sopenharmony_ci 2731cb0ef41Sopenharmony_ci(async () => { 2741cb0ef41Sopenharmony_ci for await (const chunk of transformedStream) 2751cb0ef41Sopenharmony_ci console.log(chunk); 2761cb0ef41Sopenharmony_ci})(); 2771cb0ef41Sopenharmony_ci``` 2781cb0ef41Sopenharmony_ci 2791cb0ef41Sopenharmony_ci#### `readableStream.pipeTo(destination[, options])` 2801cb0ef41Sopenharmony_ci 2811cb0ef41Sopenharmony_ci<!-- YAML 2821cb0ef41Sopenharmony_ciadded: v16.5.0 2831cb0ef41Sopenharmony_ci--> 2841cb0ef41Sopenharmony_ci 2851cb0ef41Sopenharmony_ci* `destination` {WritableStream} A {WritableStream} to which this 2861cb0ef41Sopenharmony_ci `ReadableStream`'s data will be written. 2871cb0ef41Sopenharmony_ci* `options` {Object} 2881cb0ef41Sopenharmony_ci * `preventAbort` {boolean} When `true`, errors in this `ReadableStream` 2891cb0ef41Sopenharmony_ci will not cause `destination` to be aborted. 2901cb0ef41Sopenharmony_ci * `preventCancel` {boolean} When `true`, errors in the `destination` 2911cb0ef41Sopenharmony_ci will not cause this `ReadableStream` to be canceled. 2921cb0ef41Sopenharmony_ci * `preventClose` {boolean} When `true`, closing this `ReadableStream` 2931cb0ef41Sopenharmony_ci does not cause `destination` to be closed. 2941cb0ef41Sopenharmony_ci * `signal` {AbortSignal} Allows the transfer of data to be canceled 2951cb0ef41Sopenharmony_ci using an {AbortController}. 2961cb0ef41Sopenharmony_ci* Returns: A promise fulfilled with `undefined` 2971cb0ef41Sopenharmony_ci 2981cb0ef41Sopenharmony_ciCauses the `readableStream.locked` to be `true` while the pipe operation 2991cb0ef41Sopenharmony_ciis active. 3001cb0ef41Sopenharmony_ci 3011cb0ef41Sopenharmony_ci#### `readableStream.tee()` 3021cb0ef41Sopenharmony_ci 3031cb0ef41Sopenharmony_ci<!-- YAML 3041cb0ef41Sopenharmony_ciadded: v16.5.0 3051cb0ef41Sopenharmony_cichanges: 3061cb0ef41Sopenharmony_ci - version: v18.10.0 3071cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/44505 3081cb0ef41Sopenharmony_ci description: Support teeing a readable byte stream. 3091cb0ef41Sopenharmony_ci--> 3101cb0ef41Sopenharmony_ci 3111cb0ef41Sopenharmony_ci* Returns: {ReadableStream\[]} 3121cb0ef41Sopenharmony_ci 3131cb0ef41Sopenharmony_ciReturns a pair of new {ReadableStream} instances to which this 3141cb0ef41Sopenharmony_ci`ReadableStream`'s data will be forwarded. Each will receive the 3151cb0ef41Sopenharmony_cisame data. 3161cb0ef41Sopenharmony_ci 3171cb0ef41Sopenharmony_ciCauses the `readableStream.locked` to be `true`. 3181cb0ef41Sopenharmony_ci 3191cb0ef41Sopenharmony_ci#### `readableStream.values([options])` 3201cb0ef41Sopenharmony_ci 3211cb0ef41Sopenharmony_ci<!-- YAML 3221cb0ef41Sopenharmony_ciadded: v16.5.0 3231cb0ef41Sopenharmony_ci--> 3241cb0ef41Sopenharmony_ci 3251cb0ef41Sopenharmony_ci* `options` {Object} 3261cb0ef41Sopenharmony_ci * `preventCancel` {boolean} When `true`, prevents the {ReadableStream} 3271cb0ef41Sopenharmony_ci from being closed when the async iterator abruptly terminates. 3281cb0ef41Sopenharmony_ci **Default**: `false`. 3291cb0ef41Sopenharmony_ci 3301cb0ef41Sopenharmony_ciCreates and returns an async iterator usable for consuming this 3311cb0ef41Sopenharmony_ci`ReadableStream`'s data. 3321cb0ef41Sopenharmony_ci 3331cb0ef41Sopenharmony_ciCauses the `readableStream.locked` to be `true` while the async iterator 3341cb0ef41Sopenharmony_ciis active. 3351cb0ef41Sopenharmony_ci 3361cb0ef41Sopenharmony_ci```mjs 3371cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer'; 3381cb0ef41Sopenharmony_ci 3391cb0ef41Sopenharmony_ciconst stream = new ReadableStream(getSomeSource()); 3401cb0ef41Sopenharmony_ci 3411cb0ef41Sopenharmony_cifor await (const chunk of stream.values({ preventCancel: true })) 3421cb0ef41Sopenharmony_ci console.log(Buffer.from(chunk).toString()); 3431cb0ef41Sopenharmony_ci``` 3441cb0ef41Sopenharmony_ci 3451cb0ef41Sopenharmony_ci#### Async Iteration 3461cb0ef41Sopenharmony_ci 3471cb0ef41Sopenharmony_ciThe {ReadableStream} object supports the async iterator protocol using 3481cb0ef41Sopenharmony_ci`for await` syntax. 3491cb0ef41Sopenharmony_ci 3501cb0ef41Sopenharmony_ci```mjs 3511cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer'; 3521cb0ef41Sopenharmony_ci 3531cb0ef41Sopenharmony_ciconst stream = new ReadableStream(getSomeSource()); 3541cb0ef41Sopenharmony_ci 3551cb0ef41Sopenharmony_cifor await (const chunk of stream) 3561cb0ef41Sopenharmony_ci console.log(Buffer.from(chunk).toString()); 3571cb0ef41Sopenharmony_ci``` 3581cb0ef41Sopenharmony_ci 3591cb0ef41Sopenharmony_ciThe async iterator will consume the {ReadableStream} until it terminates. 3601cb0ef41Sopenharmony_ci 3611cb0ef41Sopenharmony_ciBy default, if the async iterator exits early (via either a `break`, 3621cb0ef41Sopenharmony_ci`return`, or a `throw`), the {ReadableStream} will be closed. To prevent 3631cb0ef41Sopenharmony_ciautomatic closing of the {ReadableStream}, use the `readableStream.values()` 3641cb0ef41Sopenharmony_cimethod to acquire the async iterator and set the `preventCancel` option to 3651cb0ef41Sopenharmony_ci`true`. 3661cb0ef41Sopenharmony_ci 3671cb0ef41Sopenharmony_ciThe {ReadableStream} must not be locked (that is, it must not have an existing 3681cb0ef41Sopenharmony_ciactive reader). During the async iteration, the {ReadableStream} will be locked. 3691cb0ef41Sopenharmony_ci 3701cb0ef41Sopenharmony_ci#### Transferring with `postMessage()` 3711cb0ef41Sopenharmony_ci 3721cb0ef41Sopenharmony_ciA {ReadableStream} instance can be transferred using a {MessagePort}. 3731cb0ef41Sopenharmony_ci 3741cb0ef41Sopenharmony_ci```js 3751cb0ef41Sopenharmony_ciconst stream = new ReadableStream(getReadableSourceSomehow()); 3761cb0ef41Sopenharmony_ci 3771cb0ef41Sopenharmony_ciconst { port1, port2 } = new MessageChannel(); 3781cb0ef41Sopenharmony_ci 3791cb0ef41Sopenharmony_ciport1.onmessage = ({ data }) => { 3801cb0ef41Sopenharmony_ci data.getReader().read().then((chunk) => { 3811cb0ef41Sopenharmony_ci console.log(chunk); 3821cb0ef41Sopenharmony_ci }); 3831cb0ef41Sopenharmony_ci}; 3841cb0ef41Sopenharmony_ci 3851cb0ef41Sopenharmony_ciport2.postMessage(stream, [stream]); 3861cb0ef41Sopenharmony_ci``` 3871cb0ef41Sopenharmony_ci 3881cb0ef41Sopenharmony_ci### Class: `ReadableStreamDefaultReader` 3891cb0ef41Sopenharmony_ci 3901cb0ef41Sopenharmony_ci<!-- YAML 3911cb0ef41Sopenharmony_ciadded: v16.5.0 3921cb0ef41Sopenharmony_cichanges: 3931cb0ef41Sopenharmony_ci - version: v18.0.0 3941cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/42225 3951cb0ef41Sopenharmony_ci description: This class is now exposed on the global object. 3961cb0ef41Sopenharmony_ci--> 3971cb0ef41Sopenharmony_ci 3981cb0ef41Sopenharmony_ciBy default, calling `readableStream.getReader()` with no arguments 3991cb0ef41Sopenharmony_ciwill return an instance of `ReadableStreamDefaultReader`. The default 4001cb0ef41Sopenharmony_cireader treats the chunks of data passed through the stream as opaque 4011cb0ef41Sopenharmony_civalues, which allows the {ReadableStream} to work with generally any 4021cb0ef41Sopenharmony_ciJavaScript value. 4031cb0ef41Sopenharmony_ci 4041cb0ef41Sopenharmony_ci#### `new ReadableStreamDefaultReader(stream)` 4051cb0ef41Sopenharmony_ci 4061cb0ef41Sopenharmony_ci<!-- YAML 4071cb0ef41Sopenharmony_ciadded: v16.5.0 4081cb0ef41Sopenharmony_ci--> 4091cb0ef41Sopenharmony_ci 4101cb0ef41Sopenharmony_ci* `stream` {ReadableStream} 4111cb0ef41Sopenharmony_ci 4121cb0ef41Sopenharmony_ciCreates a new {ReadableStreamDefaultReader} that is locked to the 4131cb0ef41Sopenharmony_cigiven {ReadableStream}. 4141cb0ef41Sopenharmony_ci 4151cb0ef41Sopenharmony_ci#### `readableStreamDefaultReader.cancel([reason])` 4161cb0ef41Sopenharmony_ci 4171cb0ef41Sopenharmony_ci<!-- YAML 4181cb0ef41Sopenharmony_ciadded: v16.5.0 4191cb0ef41Sopenharmony_ci--> 4201cb0ef41Sopenharmony_ci 4211cb0ef41Sopenharmony_ci* `reason` {any} 4221cb0ef41Sopenharmony_ci* Returns: A promise fulfilled with `undefined`. 4231cb0ef41Sopenharmony_ci 4241cb0ef41Sopenharmony_ciCancels the {ReadableStream} and returns a promise that is fulfilled 4251cb0ef41Sopenharmony_ciwhen the underlying stream has been canceled. 4261cb0ef41Sopenharmony_ci 4271cb0ef41Sopenharmony_ci#### `readableStreamDefaultReader.closed` 4281cb0ef41Sopenharmony_ci 4291cb0ef41Sopenharmony_ci<!-- YAML 4301cb0ef41Sopenharmony_ciadded: v16.5.0 4311cb0ef41Sopenharmony_ci--> 4321cb0ef41Sopenharmony_ci 4331cb0ef41Sopenharmony_ci* Type: {Promise} Fulfilled with `undefined` when the associated 4341cb0ef41Sopenharmony_ci {ReadableStream} is closed or rejected if the stream errors or the reader's 4351cb0ef41Sopenharmony_ci lock is released before the stream finishes closing. 4361cb0ef41Sopenharmony_ci 4371cb0ef41Sopenharmony_ci#### `readableStreamDefaultReader.read()` 4381cb0ef41Sopenharmony_ci 4391cb0ef41Sopenharmony_ci<!-- YAML 4401cb0ef41Sopenharmony_ciadded: v16.5.0 4411cb0ef41Sopenharmony_ci--> 4421cb0ef41Sopenharmony_ci 4431cb0ef41Sopenharmony_ci* Returns: A promise fulfilled with an object: 4441cb0ef41Sopenharmony_ci * `value` {ArrayBuffer} 4451cb0ef41Sopenharmony_ci * `done` {boolean} 4461cb0ef41Sopenharmony_ci 4471cb0ef41Sopenharmony_ciRequests the next chunk of data from the underlying {ReadableStream} 4481cb0ef41Sopenharmony_ciand returns a promise that is fulfilled with the data once it is 4491cb0ef41Sopenharmony_ciavailable. 4501cb0ef41Sopenharmony_ci 4511cb0ef41Sopenharmony_ci#### `readableStreamDefaultReader.releaseLock()` 4521cb0ef41Sopenharmony_ci 4531cb0ef41Sopenharmony_ci<!-- YAML 4541cb0ef41Sopenharmony_ciadded: v16.5.0 4551cb0ef41Sopenharmony_ci--> 4561cb0ef41Sopenharmony_ci 4571cb0ef41Sopenharmony_ciReleases this reader's lock on the underlying {ReadableStream}. 4581cb0ef41Sopenharmony_ci 4591cb0ef41Sopenharmony_ci### Class: `ReadableStreamBYOBReader` 4601cb0ef41Sopenharmony_ci 4611cb0ef41Sopenharmony_ci<!-- YAML 4621cb0ef41Sopenharmony_ciadded: v16.5.0 4631cb0ef41Sopenharmony_cichanges: 4641cb0ef41Sopenharmony_ci - version: v18.0.0 4651cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/42225 4661cb0ef41Sopenharmony_ci description: This class is now exposed on the global object. 4671cb0ef41Sopenharmony_ci--> 4681cb0ef41Sopenharmony_ci 4691cb0ef41Sopenharmony_ciThe `ReadableStreamBYOBReader` is an alternative consumer for 4701cb0ef41Sopenharmony_cibyte-oriented {ReadableStream}s (those that are created with 4711cb0ef41Sopenharmony_ci`underlyingSource.type` set equal to `'bytes'` when the 4721cb0ef41Sopenharmony_ci`ReadableStream` was created). 4731cb0ef41Sopenharmony_ci 4741cb0ef41Sopenharmony_ciThe `BYOB` is short for "bring your own buffer". This is a 4751cb0ef41Sopenharmony_cipattern that allows for more efficient reading of byte-oriented 4761cb0ef41Sopenharmony_cidata that avoids extraneous copying. 4771cb0ef41Sopenharmony_ci 4781cb0ef41Sopenharmony_ci```mjs 4791cb0ef41Sopenharmony_ciimport { 4801cb0ef41Sopenharmony_ci open, 4811cb0ef41Sopenharmony_ci} from 'node:fs/promises'; 4821cb0ef41Sopenharmony_ci 4831cb0ef41Sopenharmony_ciimport { 4841cb0ef41Sopenharmony_ci ReadableStream, 4851cb0ef41Sopenharmony_ci} from 'node:stream/web'; 4861cb0ef41Sopenharmony_ci 4871cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer'; 4881cb0ef41Sopenharmony_ci 4891cb0ef41Sopenharmony_ciclass Source { 4901cb0ef41Sopenharmony_ci type = 'bytes'; 4911cb0ef41Sopenharmony_ci autoAllocateChunkSize = 1024; 4921cb0ef41Sopenharmony_ci 4931cb0ef41Sopenharmony_ci async start(controller) { 4941cb0ef41Sopenharmony_ci this.file = await open(new URL(import.meta.url)); 4951cb0ef41Sopenharmony_ci this.controller = controller; 4961cb0ef41Sopenharmony_ci } 4971cb0ef41Sopenharmony_ci 4981cb0ef41Sopenharmony_ci async pull(controller) { 4991cb0ef41Sopenharmony_ci const view = controller.byobRequest?.view; 5001cb0ef41Sopenharmony_ci const { 5011cb0ef41Sopenharmony_ci bytesRead, 5021cb0ef41Sopenharmony_ci } = await this.file.read({ 5031cb0ef41Sopenharmony_ci buffer: view, 5041cb0ef41Sopenharmony_ci offset: view.byteOffset, 5051cb0ef41Sopenharmony_ci length: view.byteLength, 5061cb0ef41Sopenharmony_ci }); 5071cb0ef41Sopenharmony_ci 5081cb0ef41Sopenharmony_ci if (bytesRead === 0) { 5091cb0ef41Sopenharmony_ci await this.file.close(); 5101cb0ef41Sopenharmony_ci this.controller.close(); 5111cb0ef41Sopenharmony_ci } 5121cb0ef41Sopenharmony_ci controller.byobRequest.respond(bytesRead); 5131cb0ef41Sopenharmony_ci } 5141cb0ef41Sopenharmony_ci} 5151cb0ef41Sopenharmony_ci 5161cb0ef41Sopenharmony_ciconst stream = new ReadableStream(new Source()); 5171cb0ef41Sopenharmony_ci 5181cb0ef41Sopenharmony_ciasync function read(stream) { 5191cb0ef41Sopenharmony_ci const reader = stream.getReader({ mode: 'byob' }); 5201cb0ef41Sopenharmony_ci 5211cb0ef41Sopenharmony_ci const chunks = []; 5221cb0ef41Sopenharmony_ci let result; 5231cb0ef41Sopenharmony_ci do { 5241cb0ef41Sopenharmony_ci result = await reader.read(Buffer.alloc(100)); 5251cb0ef41Sopenharmony_ci if (result.value !== undefined) 5261cb0ef41Sopenharmony_ci chunks.push(Buffer.from(result.value)); 5271cb0ef41Sopenharmony_ci } while (!result.done); 5281cb0ef41Sopenharmony_ci 5291cb0ef41Sopenharmony_ci return Buffer.concat(chunks); 5301cb0ef41Sopenharmony_ci} 5311cb0ef41Sopenharmony_ci 5321cb0ef41Sopenharmony_ciconst data = await read(stream); 5331cb0ef41Sopenharmony_ciconsole.log(Buffer.from(data).toString()); 5341cb0ef41Sopenharmony_ci``` 5351cb0ef41Sopenharmony_ci 5361cb0ef41Sopenharmony_ci#### `new ReadableStreamBYOBReader(stream)` 5371cb0ef41Sopenharmony_ci 5381cb0ef41Sopenharmony_ci<!-- YAML 5391cb0ef41Sopenharmony_ciadded: v16.5.0 5401cb0ef41Sopenharmony_ci--> 5411cb0ef41Sopenharmony_ci 5421cb0ef41Sopenharmony_ci* `stream` {ReadableStream} 5431cb0ef41Sopenharmony_ci 5441cb0ef41Sopenharmony_ciCreates a new `ReadableStreamBYOBReader` that is locked to the 5451cb0ef41Sopenharmony_cigiven {ReadableStream}. 5461cb0ef41Sopenharmony_ci 5471cb0ef41Sopenharmony_ci#### `readableStreamBYOBReader.cancel([reason])` 5481cb0ef41Sopenharmony_ci 5491cb0ef41Sopenharmony_ci<!-- YAML 5501cb0ef41Sopenharmony_ciadded: v16.5.0 5511cb0ef41Sopenharmony_ci--> 5521cb0ef41Sopenharmony_ci 5531cb0ef41Sopenharmony_ci* `reason` {any} 5541cb0ef41Sopenharmony_ci* Returns: A promise fulfilled with `undefined`. 5551cb0ef41Sopenharmony_ci 5561cb0ef41Sopenharmony_ciCancels the {ReadableStream} and returns a promise that is fulfilled 5571cb0ef41Sopenharmony_ciwhen the underlying stream has been canceled. 5581cb0ef41Sopenharmony_ci 5591cb0ef41Sopenharmony_ci#### `readableStreamBYOBReader.closed` 5601cb0ef41Sopenharmony_ci 5611cb0ef41Sopenharmony_ci<!-- YAML 5621cb0ef41Sopenharmony_ciadded: v16.5.0 5631cb0ef41Sopenharmony_ci--> 5641cb0ef41Sopenharmony_ci 5651cb0ef41Sopenharmony_ci* Type: {Promise} Fulfilled with `undefined` when the associated 5661cb0ef41Sopenharmony_ci {ReadableStream} is closed or rejected if the stream errors or the reader's 5671cb0ef41Sopenharmony_ci lock is released before the stream finishes closing. 5681cb0ef41Sopenharmony_ci 5691cb0ef41Sopenharmony_ci#### `readableStreamBYOBReader.read(view)` 5701cb0ef41Sopenharmony_ci 5711cb0ef41Sopenharmony_ci<!-- YAML 5721cb0ef41Sopenharmony_ciadded: v16.5.0 5731cb0ef41Sopenharmony_ci--> 5741cb0ef41Sopenharmony_ci 5751cb0ef41Sopenharmony_ci* `view` {Buffer|TypedArray|DataView} 5761cb0ef41Sopenharmony_ci* Returns: A promise fulfilled with an object: 5771cb0ef41Sopenharmony_ci * `value` {ArrayBuffer} 5781cb0ef41Sopenharmony_ci * `done` {boolean} 5791cb0ef41Sopenharmony_ci 5801cb0ef41Sopenharmony_ciRequests the next chunk of data from the underlying {ReadableStream} 5811cb0ef41Sopenharmony_ciand returns a promise that is fulfilled with the data once it is 5821cb0ef41Sopenharmony_ciavailable. 5831cb0ef41Sopenharmony_ci 5841cb0ef41Sopenharmony_ciDo not pass a pooled {Buffer} object instance in to this method. 5851cb0ef41Sopenharmony_ciPooled `Buffer` objects are created using `Buffer.allocUnsafe()`, 5861cb0ef41Sopenharmony_cior `Buffer.from()`, or are often returned by various `node:fs` module 5871cb0ef41Sopenharmony_cicallbacks. These types of `Buffer`s use a shared underlying 5881cb0ef41Sopenharmony_ci{ArrayBuffer} object that contains all of the data from all of 5891cb0ef41Sopenharmony_cithe pooled `Buffer` instances. When a `Buffer`, {TypedArray}, 5901cb0ef41Sopenharmony_cior {DataView} is passed in to `readableStreamBYOBReader.read()`, 5911cb0ef41Sopenharmony_cithe view's underlying `ArrayBuffer` is _detached_, invalidating 5921cb0ef41Sopenharmony_ciall existing views that may exist on that `ArrayBuffer`. This 5931cb0ef41Sopenharmony_cican have disastrous consequences for your application. 5941cb0ef41Sopenharmony_ci 5951cb0ef41Sopenharmony_ci#### `readableStreamBYOBReader.releaseLock()` 5961cb0ef41Sopenharmony_ci 5971cb0ef41Sopenharmony_ci<!-- YAML 5981cb0ef41Sopenharmony_ciadded: v16.5.0 5991cb0ef41Sopenharmony_ci--> 6001cb0ef41Sopenharmony_ci 6011cb0ef41Sopenharmony_ciReleases this reader's lock on the underlying {ReadableStream}. 6021cb0ef41Sopenharmony_ci 6031cb0ef41Sopenharmony_ci### Class: `ReadableStreamDefaultController` 6041cb0ef41Sopenharmony_ci 6051cb0ef41Sopenharmony_ci<!-- YAML 6061cb0ef41Sopenharmony_ciadded: v16.5.0 6071cb0ef41Sopenharmony_ci--> 6081cb0ef41Sopenharmony_ci 6091cb0ef41Sopenharmony_ciEvery {ReadableStream} has a controller that is responsible for 6101cb0ef41Sopenharmony_cithe internal state and management of the stream's queue. The 6111cb0ef41Sopenharmony_ci`ReadableStreamDefaultController` is the default controller 6121cb0ef41Sopenharmony_ciimplementation for `ReadableStream`s that are not byte-oriented. 6131cb0ef41Sopenharmony_ci 6141cb0ef41Sopenharmony_ci#### `readableStreamDefaultController.close()` 6151cb0ef41Sopenharmony_ci 6161cb0ef41Sopenharmony_ci<!-- YAML 6171cb0ef41Sopenharmony_ciadded: v16.5.0 6181cb0ef41Sopenharmony_ci--> 6191cb0ef41Sopenharmony_ci 6201cb0ef41Sopenharmony_ciCloses the {ReadableStream} to which this controller is associated. 6211cb0ef41Sopenharmony_ci 6221cb0ef41Sopenharmony_ci#### `readableStreamDefaultController.desiredSize` 6231cb0ef41Sopenharmony_ci 6241cb0ef41Sopenharmony_ci<!-- YAML 6251cb0ef41Sopenharmony_ciadded: v16.5.0 6261cb0ef41Sopenharmony_ci--> 6271cb0ef41Sopenharmony_ci 6281cb0ef41Sopenharmony_ci* Type: {number} 6291cb0ef41Sopenharmony_ci 6301cb0ef41Sopenharmony_ciReturns the amount of data remaining to fill the {ReadableStream}'s 6311cb0ef41Sopenharmony_ciqueue. 6321cb0ef41Sopenharmony_ci 6331cb0ef41Sopenharmony_ci#### `readableStreamDefaultController.enqueue([chunk])` 6341cb0ef41Sopenharmony_ci 6351cb0ef41Sopenharmony_ci<!-- YAML 6361cb0ef41Sopenharmony_ciadded: v16.5.0 6371cb0ef41Sopenharmony_ci--> 6381cb0ef41Sopenharmony_ci 6391cb0ef41Sopenharmony_ci* `chunk` {any} 6401cb0ef41Sopenharmony_ci 6411cb0ef41Sopenharmony_ciAppends a new chunk of data to the {ReadableStream}'s queue. 6421cb0ef41Sopenharmony_ci 6431cb0ef41Sopenharmony_ci#### `readableStreamDefaultController.error([error])` 6441cb0ef41Sopenharmony_ci 6451cb0ef41Sopenharmony_ci<!-- YAML 6461cb0ef41Sopenharmony_ciadded: v16.5.0 6471cb0ef41Sopenharmony_ci--> 6481cb0ef41Sopenharmony_ci 6491cb0ef41Sopenharmony_ci* `error` {any} 6501cb0ef41Sopenharmony_ci 6511cb0ef41Sopenharmony_ciSignals an error that causes the {ReadableStream} to error and close. 6521cb0ef41Sopenharmony_ci 6531cb0ef41Sopenharmony_ci### Class: `ReadableByteStreamController` 6541cb0ef41Sopenharmony_ci 6551cb0ef41Sopenharmony_ci<!-- YAML 6561cb0ef41Sopenharmony_ciadded: v16.5.0 6571cb0ef41Sopenharmony_cichanges: 6581cb0ef41Sopenharmony_ci - version: v18.10.0 6591cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/44702 6601cb0ef41Sopenharmony_ci description: Support handling a BYOB pull request from a released reader. 6611cb0ef41Sopenharmony_ci--> 6621cb0ef41Sopenharmony_ci 6631cb0ef41Sopenharmony_ciEvery {ReadableStream} has a controller that is responsible for 6641cb0ef41Sopenharmony_cithe internal state and management of the stream's queue. The 6651cb0ef41Sopenharmony_ci`ReadableByteStreamController` is for byte-oriented `ReadableStream`s. 6661cb0ef41Sopenharmony_ci 6671cb0ef41Sopenharmony_ci#### `readableByteStreamController.byobRequest` 6681cb0ef41Sopenharmony_ci 6691cb0ef41Sopenharmony_ci<!-- YAML 6701cb0ef41Sopenharmony_ciadded: v16.5.0 6711cb0ef41Sopenharmony_ci--> 6721cb0ef41Sopenharmony_ci 6731cb0ef41Sopenharmony_ci* Type: {ReadableStreamBYOBRequest} 6741cb0ef41Sopenharmony_ci 6751cb0ef41Sopenharmony_ci#### `readableByteStreamController.close()` 6761cb0ef41Sopenharmony_ci 6771cb0ef41Sopenharmony_ci<!-- YAML 6781cb0ef41Sopenharmony_ciadded: v16.5.0 6791cb0ef41Sopenharmony_ci--> 6801cb0ef41Sopenharmony_ci 6811cb0ef41Sopenharmony_ciCloses the {ReadableStream} to which this controller is associated. 6821cb0ef41Sopenharmony_ci 6831cb0ef41Sopenharmony_ci#### `readableByteStreamController.desiredSize` 6841cb0ef41Sopenharmony_ci 6851cb0ef41Sopenharmony_ci<!-- YAML 6861cb0ef41Sopenharmony_ciadded: v16.5.0 6871cb0ef41Sopenharmony_ci--> 6881cb0ef41Sopenharmony_ci 6891cb0ef41Sopenharmony_ci* Type: {number} 6901cb0ef41Sopenharmony_ci 6911cb0ef41Sopenharmony_ciReturns the amount of data remaining to fill the {ReadableStream}'s 6921cb0ef41Sopenharmony_ciqueue. 6931cb0ef41Sopenharmony_ci 6941cb0ef41Sopenharmony_ci#### `readableByteStreamController.enqueue(chunk)` 6951cb0ef41Sopenharmony_ci 6961cb0ef41Sopenharmony_ci<!-- YAML 6971cb0ef41Sopenharmony_ciadded: v16.5.0 6981cb0ef41Sopenharmony_ci--> 6991cb0ef41Sopenharmony_ci 7001cb0ef41Sopenharmony_ci* `chunk`: {Buffer|TypedArray|DataView} 7011cb0ef41Sopenharmony_ci 7021cb0ef41Sopenharmony_ciAppends a new chunk of data to the {ReadableStream}'s queue. 7031cb0ef41Sopenharmony_ci 7041cb0ef41Sopenharmony_ci#### `readableByteStreamController.error([error])` 7051cb0ef41Sopenharmony_ci 7061cb0ef41Sopenharmony_ci<!-- YAML 7071cb0ef41Sopenharmony_ciadded: v16.5.0 7081cb0ef41Sopenharmony_ci--> 7091cb0ef41Sopenharmony_ci 7101cb0ef41Sopenharmony_ci* `error` {any} 7111cb0ef41Sopenharmony_ci 7121cb0ef41Sopenharmony_ciSignals an error that causes the {ReadableStream} to error and close. 7131cb0ef41Sopenharmony_ci 7141cb0ef41Sopenharmony_ci### Class: `ReadableStreamBYOBRequest` 7151cb0ef41Sopenharmony_ci 7161cb0ef41Sopenharmony_ci<!-- YAML 7171cb0ef41Sopenharmony_ciadded: v16.5.0 7181cb0ef41Sopenharmony_cichanges: 7191cb0ef41Sopenharmony_ci - version: v18.0.0 7201cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/42225 7211cb0ef41Sopenharmony_ci description: This class is now exposed on the global object. 7221cb0ef41Sopenharmony_ci--> 7231cb0ef41Sopenharmony_ci 7241cb0ef41Sopenharmony_ciWhen using `ReadableByteStreamController` in byte-oriented 7251cb0ef41Sopenharmony_cistreams, and when using the `ReadableStreamBYOBReader`, 7261cb0ef41Sopenharmony_cithe `readableByteStreamController.byobRequest` property 7271cb0ef41Sopenharmony_ciprovides access to a `ReadableStreamBYOBRequest` instance 7281cb0ef41Sopenharmony_cithat represents the current read request. The object 7291cb0ef41Sopenharmony_ciis used to gain access to the `ArrayBuffer`/`TypedArray` 7301cb0ef41Sopenharmony_cithat has been provided for the read request to fill, 7311cb0ef41Sopenharmony_ciand provides methods for signaling that the data has 7321cb0ef41Sopenharmony_cibeen provided. 7331cb0ef41Sopenharmony_ci 7341cb0ef41Sopenharmony_ci#### `readableStreamBYOBRequest.respond(bytesWritten)` 7351cb0ef41Sopenharmony_ci 7361cb0ef41Sopenharmony_ci<!-- YAML 7371cb0ef41Sopenharmony_ciadded: v16.5.0 7381cb0ef41Sopenharmony_ci--> 7391cb0ef41Sopenharmony_ci 7401cb0ef41Sopenharmony_ci* `bytesWritten` {number} 7411cb0ef41Sopenharmony_ci 7421cb0ef41Sopenharmony_ciSignals that a `bytesWritten` number of bytes have been written 7431cb0ef41Sopenharmony_cito `readableStreamBYOBRequest.view`. 7441cb0ef41Sopenharmony_ci 7451cb0ef41Sopenharmony_ci#### `readableStreamBYOBRequest.respondWithNewView(view)` 7461cb0ef41Sopenharmony_ci 7471cb0ef41Sopenharmony_ci<!-- YAML 7481cb0ef41Sopenharmony_ciadded: v16.5.0 7491cb0ef41Sopenharmony_ci--> 7501cb0ef41Sopenharmony_ci 7511cb0ef41Sopenharmony_ci* `view` {Buffer|TypedArray|DataView} 7521cb0ef41Sopenharmony_ci 7531cb0ef41Sopenharmony_ciSignals that the request has been fulfilled with bytes written 7541cb0ef41Sopenharmony_cito a new `Buffer`, `TypedArray`, or `DataView`. 7551cb0ef41Sopenharmony_ci 7561cb0ef41Sopenharmony_ci#### `readableStreamBYOBRequest.view` 7571cb0ef41Sopenharmony_ci 7581cb0ef41Sopenharmony_ci<!-- YAML 7591cb0ef41Sopenharmony_ciadded: v16.5.0 7601cb0ef41Sopenharmony_ci--> 7611cb0ef41Sopenharmony_ci 7621cb0ef41Sopenharmony_ci* Type: {Buffer|TypedArray|DataView} 7631cb0ef41Sopenharmony_ci 7641cb0ef41Sopenharmony_ci### Class: `WritableStream` 7651cb0ef41Sopenharmony_ci 7661cb0ef41Sopenharmony_ci<!-- YAML 7671cb0ef41Sopenharmony_ciadded: v16.5.0 7681cb0ef41Sopenharmony_cichanges: 7691cb0ef41Sopenharmony_ci - version: v18.0.0 7701cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/42225 7711cb0ef41Sopenharmony_ci description: This class is now exposed on the global object. 7721cb0ef41Sopenharmony_ci--> 7731cb0ef41Sopenharmony_ci 7741cb0ef41Sopenharmony_ciThe `WritableStream` is a destination to which stream data is sent. 7751cb0ef41Sopenharmony_ci 7761cb0ef41Sopenharmony_ci```mjs 7771cb0ef41Sopenharmony_ciimport { 7781cb0ef41Sopenharmony_ci WritableStream, 7791cb0ef41Sopenharmony_ci} from 'node:stream/web'; 7801cb0ef41Sopenharmony_ci 7811cb0ef41Sopenharmony_ciconst stream = new WritableStream({ 7821cb0ef41Sopenharmony_ci write(chunk) { 7831cb0ef41Sopenharmony_ci console.log(chunk); 7841cb0ef41Sopenharmony_ci }, 7851cb0ef41Sopenharmony_ci}); 7861cb0ef41Sopenharmony_ci 7871cb0ef41Sopenharmony_ciawait stream.getWriter().write('Hello World'); 7881cb0ef41Sopenharmony_ci``` 7891cb0ef41Sopenharmony_ci 7901cb0ef41Sopenharmony_ci#### `new WritableStream([underlyingSink[, strategy]])` 7911cb0ef41Sopenharmony_ci 7921cb0ef41Sopenharmony_ci<!-- YAML 7931cb0ef41Sopenharmony_ciadded: v16.5.0 7941cb0ef41Sopenharmony_ci--> 7951cb0ef41Sopenharmony_ci 7961cb0ef41Sopenharmony_ci* `underlyingSink` {Object} 7971cb0ef41Sopenharmony_ci * `start` {Function} A user-defined function that is invoked immediately when 7981cb0ef41Sopenharmony_ci the `WritableStream` is created. 7991cb0ef41Sopenharmony_ci * `controller` {WritableStreamDefaultController} 8001cb0ef41Sopenharmony_ci * Returns: `undefined` or a promise fulfilled with `undefined`. 8011cb0ef41Sopenharmony_ci * `write` {Function} A user-defined function that is invoked when a chunk of 8021cb0ef41Sopenharmony_ci data has been written to the `WritableStream`. 8031cb0ef41Sopenharmony_ci * `chunk` {any} 8041cb0ef41Sopenharmony_ci * `controller` {WritableStreamDefaultController} 8051cb0ef41Sopenharmony_ci * Returns: A promise fulfilled with `undefined`. 8061cb0ef41Sopenharmony_ci * `close` {Function} A user-defined function that is called when the 8071cb0ef41Sopenharmony_ci `WritableStream` is closed. 8081cb0ef41Sopenharmony_ci * Returns: A promise fulfilled with `undefined`. 8091cb0ef41Sopenharmony_ci * `abort` {Function} A user-defined function that is called to abruptly close 8101cb0ef41Sopenharmony_ci the `WritableStream`. 8111cb0ef41Sopenharmony_ci * `reason` {any} 8121cb0ef41Sopenharmony_ci * Returns: A promise fulfilled with `undefined`. 8131cb0ef41Sopenharmony_ci * `type` {any} The `type` option is reserved for future use and _must_ be 8141cb0ef41Sopenharmony_ci undefined. 8151cb0ef41Sopenharmony_ci* `strategy` {Object} 8161cb0ef41Sopenharmony_ci * `highWaterMark` {number} The maximum internal queue size before backpressure 8171cb0ef41Sopenharmony_ci is applied. 8181cb0ef41Sopenharmony_ci * `size` {Function} A user-defined function used to identify the size of each 8191cb0ef41Sopenharmony_ci chunk of data. 8201cb0ef41Sopenharmony_ci * `chunk` {any} 8211cb0ef41Sopenharmony_ci * Returns: {number} 8221cb0ef41Sopenharmony_ci 8231cb0ef41Sopenharmony_ci#### `writableStream.abort([reason])` 8241cb0ef41Sopenharmony_ci 8251cb0ef41Sopenharmony_ci<!-- YAML 8261cb0ef41Sopenharmony_ciadded: v16.5.0 8271cb0ef41Sopenharmony_ci--> 8281cb0ef41Sopenharmony_ci 8291cb0ef41Sopenharmony_ci* `reason` {any} 8301cb0ef41Sopenharmony_ci* Returns: A promise fulfilled with `undefined`. 8311cb0ef41Sopenharmony_ci 8321cb0ef41Sopenharmony_ciAbruptly terminates the `WritableStream`. All queued writes will be 8331cb0ef41Sopenharmony_cicanceled with their associated promises rejected. 8341cb0ef41Sopenharmony_ci 8351cb0ef41Sopenharmony_ci#### `writableStream.close()` 8361cb0ef41Sopenharmony_ci 8371cb0ef41Sopenharmony_ci<!-- YAML 8381cb0ef41Sopenharmony_ciadded: v16.5.0 8391cb0ef41Sopenharmony_ci--> 8401cb0ef41Sopenharmony_ci 8411cb0ef41Sopenharmony_ci* Returns: A promise fulfilled with `undefined`. 8421cb0ef41Sopenharmony_ci 8431cb0ef41Sopenharmony_ciCloses the `WritableStream` when no additional writes are expected. 8441cb0ef41Sopenharmony_ci 8451cb0ef41Sopenharmony_ci#### `writableStream.getWriter()` 8461cb0ef41Sopenharmony_ci 8471cb0ef41Sopenharmony_ci<!-- YAML 8481cb0ef41Sopenharmony_ciadded: v16.5.0 8491cb0ef41Sopenharmony_ci--> 8501cb0ef41Sopenharmony_ci 8511cb0ef41Sopenharmony_ci* Returns: {WritableStreamDefaultWriter} 8521cb0ef41Sopenharmony_ci 8531cb0ef41Sopenharmony_ciCreates and returns a new writer instance that can be used to write 8541cb0ef41Sopenharmony_cidata into the `WritableStream`. 8551cb0ef41Sopenharmony_ci 8561cb0ef41Sopenharmony_ci#### `writableStream.locked` 8571cb0ef41Sopenharmony_ci 8581cb0ef41Sopenharmony_ci<!-- YAML 8591cb0ef41Sopenharmony_ciadded: v16.5.0 8601cb0ef41Sopenharmony_ci--> 8611cb0ef41Sopenharmony_ci 8621cb0ef41Sopenharmony_ci* Type: {boolean} 8631cb0ef41Sopenharmony_ci 8641cb0ef41Sopenharmony_ciThe `writableStream.locked` property is `false` by default, and is 8651cb0ef41Sopenharmony_ciswitched to `true` while there is an active writer attached to this 8661cb0ef41Sopenharmony_ci`WritableStream`. 8671cb0ef41Sopenharmony_ci 8681cb0ef41Sopenharmony_ci#### Transferring with postMessage() 8691cb0ef41Sopenharmony_ci 8701cb0ef41Sopenharmony_ciA {WritableStream} instance can be transferred using a {MessagePort}. 8711cb0ef41Sopenharmony_ci 8721cb0ef41Sopenharmony_ci```js 8731cb0ef41Sopenharmony_ciconst stream = new WritableStream(getWritableSinkSomehow()); 8741cb0ef41Sopenharmony_ci 8751cb0ef41Sopenharmony_ciconst { port1, port2 } = new MessageChannel(); 8761cb0ef41Sopenharmony_ci 8771cb0ef41Sopenharmony_ciport1.onmessage = ({ data }) => { 8781cb0ef41Sopenharmony_ci data.getWriter().write('hello'); 8791cb0ef41Sopenharmony_ci}; 8801cb0ef41Sopenharmony_ci 8811cb0ef41Sopenharmony_ciport2.postMessage(stream, [stream]); 8821cb0ef41Sopenharmony_ci``` 8831cb0ef41Sopenharmony_ci 8841cb0ef41Sopenharmony_ci### Class: `WritableStreamDefaultWriter` 8851cb0ef41Sopenharmony_ci 8861cb0ef41Sopenharmony_ci<!-- YAML 8871cb0ef41Sopenharmony_ciadded: v16.5.0 8881cb0ef41Sopenharmony_cichanges: 8891cb0ef41Sopenharmony_ci - version: v18.0.0 8901cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/42225 8911cb0ef41Sopenharmony_ci description: This class is now exposed on the global object. 8921cb0ef41Sopenharmony_ci--> 8931cb0ef41Sopenharmony_ci 8941cb0ef41Sopenharmony_ci#### `new WritableStreamDefaultWriter(stream)` 8951cb0ef41Sopenharmony_ci 8961cb0ef41Sopenharmony_ci<!-- YAML 8971cb0ef41Sopenharmony_ciadded: v16.5.0 8981cb0ef41Sopenharmony_ci--> 8991cb0ef41Sopenharmony_ci 9001cb0ef41Sopenharmony_ci* `stream` {WritableStream} 9011cb0ef41Sopenharmony_ci 9021cb0ef41Sopenharmony_ciCreates a new `WritableStreamDefaultWriter` that is locked to the given 9031cb0ef41Sopenharmony_ci`WritableStream`. 9041cb0ef41Sopenharmony_ci 9051cb0ef41Sopenharmony_ci#### `writableStreamDefaultWriter.abort([reason])` 9061cb0ef41Sopenharmony_ci 9071cb0ef41Sopenharmony_ci<!-- YAML 9081cb0ef41Sopenharmony_ciadded: v16.5.0 9091cb0ef41Sopenharmony_ci--> 9101cb0ef41Sopenharmony_ci 9111cb0ef41Sopenharmony_ci* `reason` {any} 9121cb0ef41Sopenharmony_ci* Returns: A promise fulfilled with `undefined`. 9131cb0ef41Sopenharmony_ci 9141cb0ef41Sopenharmony_ciAbruptly terminates the `WritableStream`. All queued writes will be 9151cb0ef41Sopenharmony_cicanceled with their associated promises rejected. 9161cb0ef41Sopenharmony_ci 9171cb0ef41Sopenharmony_ci#### `writableStreamDefaultWriter.close()` 9181cb0ef41Sopenharmony_ci 9191cb0ef41Sopenharmony_ci<!-- YAML 9201cb0ef41Sopenharmony_ciadded: v16.5.0 9211cb0ef41Sopenharmony_ci--> 9221cb0ef41Sopenharmony_ci 9231cb0ef41Sopenharmony_ci* Returns: A promise fulfilled with `undefined`. 9241cb0ef41Sopenharmony_ci 9251cb0ef41Sopenharmony_ciCloses the `WritableStream` when no additional writes are expected. 9261cb0ef41Sopenharmony_ci 9271cb0ef41Sopenharmony_ci#### `writableStreamDefaultWriter.closed` 9281cb0ef41Sopenharmony_ci 9291cb0ef41Sopenharmony_ci<!-- YAML 9301cb0ef41Sopenharmony_ciadded: v16.5.0 9311cb0ef41Sopenharmony_ci--> 9321cb0ef41Sopenharmony_ci 9331cb0ef41Sopenharmony_ci* Type: {Promise} Fulfilled with `undefined` when the associated 9341cb0ef41Sopenharmony_ci {WritableStream} is closed or rejected if the stream errors or the writer's 9351cb0ef41Sopenharmony_ci lock is released before the stream finishes closing. 9361cb0ef41Sopenharmony_ci 9371cb0ef41Sopenharmony_ci#### `writableStreamDefaultWriter.desiredSize` 9381cb0ef41Sopenharmony_ci 9391cb0ef41Sopenharmony_ci<!-- YAML 9401cb0ef41Sopenharmony_ciadded: v16.5.0 9411cb0ef41Sopenharmony_ci--> 9421cb0ef41Sopenharmony_ci 9431cb0ef41Sopenharmony_ci* Type: {number} 9441cb0ef41Sopenharmony_ci 9451cb0ef41Sopenharmony_ciThe amount of data required to fill the {WritableStream}'s queue. 9461cb0ef41Sopenharmony_ci 9471cb0ef41Sopenharmony_ci#### `writableStreamDefaultWriter.ready` 9481cb0ef41Sopenharmony_ci 9491cb0ef41Sopenharmony_ci<!-- YAML 9501cb0ef41Sopenharmony_ciadded: v16.5.0 9511cb0ef41Sopenharmony_ci--> 9521cb0ef41Sopenharmony_ci 9531cb0ef41Sopenharmony_ci* Type: {Promise} Fulfilled with `undefined` when the writer is ready 9541cb0ef41Sopenharmony_ci to be used. 9551cb0ef41Sopenharmony_ci 9561cb0ef41Sopenharmony_ci#### `writableStreamDefaultWriter.releaseLock()` 9571cb0ef41Sopenharmony_ci 9581cb0ef41Sopenharmony_ci<!-- YAML 9591cb0ef41Sopenharmony_ciadded: v16.5.0 9601cb0ef41Sopenharmony_ci--> 9611cb0ef41Sopenharmony_ci 9621cb0ef41Sopenharmony_ciReleases this writer's lock on the underlying {ReadableStream}. 9631cb0ef41Sopenharmony_ci 9641cb0ef41Sopenharmony_ci#### `writableStreamDefaultWriter.write([chunk])` 9651cb0ef41Sopenharmony_ci 9661cb0ef41Sopenharmony_ci<!-- YAML 9671cb0ef41Sopenharmony_ciadded: v16.5.0 9681cb0ef41Sopenharmony_ci--> 9691cb0ef41Sopenharmony_ci 9701cb0ef41Sopenharmony_ci* `chunk`: {any} 9711cb0ef41Sopenharmony_ci* Returns: A promise fulfilled with `undefined`. 9721cb0ef41Sopenharmony_ci 9731cb0ef41Sopenharmony_ciAppends a new chunk of data to the {WritableStream}'s queue. 9741cb0ef41Sopenharmony_ci 9751cb0ef41Sopenharmony_ci### Class: `WritableStreamDefaultController` 9761cb0ef41Sopenharmony_ci 9771cb0ef41Sopenharmony_ci<!-- YAML 9781cb0ef41Sopenharmony_ciadded: v16.5.0 9791cb0ef41Sopenharmony_cichanges: 9801cb0ef41Sopenharmony_ci - version: v18.0.0 9811cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/42225 9821cb0ef41Sopenharmony_ci description: This class is now exposed on the global object. 9831cb0ef41Sopenharmony_ci--> 9841cb0ef41Sopenharmony_ci 9851cb0ef41Sopenharmony_ciThe `WritableStreamDefaultController` manage's the {WritableStream}'s 9861cb0ef41Sopenharmony_ciinternal state. 9871cb0ef41Sopenharmony_ci 9881cb0ef41Sopenharmony_ci#### `writableStreamDefaultController.error([error])` 9891cb0ef41Sopenharmony_ci 9901cb0ef41Sopenharmony_ci<!-- YAML 9911cb0ef41Sopenharmony_ciadded: v16.5.0 9921cb0ef41Sopenharmony_ci--> 9931cb0ef41Sopenharmony_ci 9941cb0ef41Sopenharmony_ci* `error` {any} 9951cb0ef41Sopenharmony_ci 9961cb0ef41Sopenharmony_ciCalled by user-code to signal that an error has occurred while processing 9971cb0ef41Sopenharmony_cithe `WritableStream` data. When called, the {WritableStream} will be aborted, 9981cb0ef41Sopenharmony_ciwith currently pending writes canceled. 9991cb0ef41Sopenharmony_ci 10001cb0ef41Sopenharmony_ci#### `writableStreamDefaultController.signal` 10011cb0ef41Sopenharmony_ci 10021cb0ef41Sopenharmony_ci* Type: {AbortSignal} An `AbortSignal` that can be used to cancel pending 10031cb0ef41Sopenharmony_ci write or close operations when a {WritableStream} is aborted. 10041cb0ef41Sopenharmony_ci 10051cb0ef41Sopenharmony_ci### Class: `TransformStream` 10061cb0ef41Sopenharmony_ci 10071cb0ef41Sopenharmony_ci<!-- YAML 10081cb0ef41Sopenharmony_ciadded: v16.5.0 10091cb0ef41Sopenharmony_cichanges: 10101cb0ef41Sopenharmony_ci - version: v18.0.0 10111cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/42225 10121cb0ef41Sopenharmony_ci description: This class is now exposed on the global object. 10131cb0ef41Sopenharmony_ci--> 10141cb0ef41Sopenharmony_ci 10151cb0ef41Sopenharmony_ciA `TransformStream` consists of a {ReadableStream} and a {WritableStream} that 10161cb0ef41Sopenharmony_ciare connected such that the data written to the `WritableStream` is received, 10171cb0ef41Sopenharmony_ciand potentially transformed, before being pushed into the `ReadableStream`'s 10181cb0ef41Sopenharmony_ciqueue. 10191cb0ef41Sopenharmony_ci 10201cb0ef41Sopenharmony_ci```mjs 10211cb0ef41Sopenharmony_ciimport { 10221cb0ef41Sopenharmony_ci TransformStream, 10231cb0ef41Sopenharmony_ci} from 'node:stream/web'; 10241cb0ef41Sopenharmony_ci 10251cb0ef41Sopenharmony_ciconst transform = new TransformStream({ 10261cb0ef41Sopenharmony_ci transform(chunk, controller) { 10271cb0ef41Sopenharmony_ci controller.enqueue(chunk.toUpperCase()); 10281cb0ef41Sopenharmony_ci }, 10291cb0ef41Sopenharmony_ci}); 10301cb0ef41Sopenharmony_ci 10311cb0ef41Sopenharmony_ciawait Promise.all([ 10321cb0ef41Sopenharmony_ci transform.writable.getWriter().write('A'), 10331cb0ef41Sopenharmony_ci transform.readable.getReader().read(), 10341cb0ef41Sopenharmony_ci]); 10351cb0ef41Sopenharmony_ci``` 10361cb0ef41Sopenharmony_ci 10371cb0ef41Sopenharmony_ci#### `new TransformStream([transformer[, writableStrategy[, readableStrategy]]])` 10381cb0ef41Sopenharmony_ci 10391cb0ef41Sopenharmony_ci<!-- YAML 10401cb0ef41Sopenharmony_ciadded: v16.5.0 10411cb0ef41Sopenharmony_ci--> 10421cb0ef41Sopenharmony_ci 10431cb0ef41Sopenharmony_ci* `transformer` {Object} 10441cb0ef41Sopenharmony_ci * `start` {Function} A user-defined function that is invoked immediately when 10451cb0ef41Sopenharmony_ci the `TransformStream` is created. 10461cb0ef41Sopenharmony_ci * `controller` {TransformStreamDefaultController} 10471cb0ef41Sopenharmony_ci * Returns: `undefined` or a promise fulfilled with `undefined` 10481cb0ef41Sopenharmony_ci * `transform` {Function} A user-defined function that receives, and 10491cb0ef41Sopenharmony_ci potentially modifies, a chunk of data written to `transformStream.writable`, 10501cb0ef41Sopenharmony_ci before forwarding that on to `transformStream.readable`. 10511cb0ef41Sopenharmony_ci * `chunk` {any} 10521cb0ef41Sopenharmony_ci * `controller` {TransformStreamDefaultController} 10531cb0ef41Sopenharmony_ci * Returns: A promise fulfilled with `undefined`. 10541cb0ef41Sopenharmony_ci * `flush` {Function} A user-defined function that is called immediately before 10551cb0ef41Sopenharmony_ci the writable side of the `TransformStream` is closed, signaling the end of 10561cb0ef41Sopenharmony_ci the transformation process. 10571cb0ef41Sopenharmony_ci * `controller` {TransformStreamDefaultController} 10581cb0ef41Sopenharmony_ci * Returns: A promise fulfilled with `undefined`. 10591cb0ef41Sopenharmony_ci * `readableType` {any} the `readableType` option is reserved for future use 10601cb0ef41Sopenharmony_ci and _must_ be `undefined`. 10611cb0ef41Sopenharmony_ci * `writableType` {any} the `writableType` option is reserved for future use 10621cb0ef41Sopenharmony_ci and _must_ be `undefined`. 10631cb0ef41Sopenharmony_ci* `writableStrategy` {Object} 10641cb0ef41Sopenharmony_ci * `highWaterMark` {number} The maximum internal queue size before backpressure 10651cb0ef41Sopenharmony_ci is applied. 10661cb0ef41Sopenharmony_ci * `size` {Function} A user-defined function used to identify the size of each 10671cb0ef41Sopenharmony_ci chunk of data. 10681cb0ef41Sopenharmony_ci * `chunk` {any} 10691cb0ef41Sopenharmony_ci * Returns: {number} 10701cb0ef41Sopenharmony_ci* `readableStrategy` {Object} 10711cb0ef41Sopenharmony_ci * `highWaterMark` {number} The maximum internal queue size before backpressure 10721cb0ef41Sopenharmony_ci is applied. 10731cb0ef41Sopenharmony_ci * `size` {Function} A user-defined function used to identify the size of each 10741cb0ef41Sopenharmony_ci chunk of data. 10751cb0ef41Sopenharmony_ci * `chunk` {any} 10761cb0ef41Sopenharmony_ci * Returns: {number} 10771cb0ef41Sopenharmony_ci 10781cb0ef41Sopenharmony_ci#### `transformStream.readable` 10791cb0ef41Sopenharmony_ci 10801cb0ef41Sopenharmony_ci<!-- YAML 10811cb0ef41Sopenharmony_ciadded: v16.5.0 10821cb0ef41Sopenharmony_ci--> 10831cb0ef41Sopenharmony_ci 10841cb0ef41Sopenharmony_ci* Type: {ReadableStream} 10851cb0ef41Sopenharmony_ci 10861cb0ef41Sopenharmony_ci#### `transformStream.writable` 10871cb0ef41Sopenharmony_ci 10881cb0ef41Sopenharmony_ci<!-- YAML 10891cb0ef41Sopenharmony_ciadded: v16.5.0 10901cb0ef41Sopenharmony_ci--> 10911cb0ef41Sopenharmony_ci 10921cb0ef41Sopenharmony_ci* Type: {WritableStream} 10931cb0ef41Sopenharmony_ci 10941cb0ef41Sopenharmony_ci#### Transferring with postMessage() 10951cb0ef41Sopenharmony_ci 10961cb0ef41Sopenharmony_ciA {TransformStream} instance can be transferred using a {MessagePort}. 10971cb0ef41Sopenharmony_ci 10981cb0ef41Sopenharmony_ci```js 10991cb0ef41Sopenharmony_ciconst stream = new TransformStream(); 11001cb0ef41Sopenharmony_ci 11011cb0ef41Sopenharmony_ciconst { port1, port2 } = new MessageChannel(); 11021cb0ef41Sopenharmony_ci 11031cb0ef41Sopenharmony_ciport1.onmessage = ({ data }) => { 11041cb0ef41Sopenharmony_ci const { writable, readable } = data; 11051cb0ef41Sopenharmony_ci // ... 11061cb0ef41Sopenharmony_ci}; 11071cb0ef41Sopenharmony_ci 11081cb0ef41Sopenharmony_ciport2.postMessage(stream, [stream]); 11091cb0ef41Sopenharmony_ci``` 11101cb0ef41Sopenharmony_ci 11111cb0ef41Sopenharmony_ci### Class: `TransformStreamDefaultController` 11121cb0ef41Sopenharmony_ci 11131cb0ef41Sopenharmony_ci<!-- YAML 11141cb0ef41Sopenharmony_ciadded: v16.5.0 11151cb0ef41Sopenharmony_cichanges: 11161cb0ef41Sopenharmony_ci - version: v18.0.0 11171cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/42225 11181cb0ef41Sopenharmony_ci description: This class is now exposed on the global object. 11191cb0ef41Sopenharmony_ci--> 11201cb0ef41Sopenharmony_ci 11211cb0ef41Sopenharmony_ciThe `TransformStreamDefaultController` manages the internal state 11221cb0ef41Sopenharmony_ciof the `TransformStream`. 11231cb0ef41Sopenharmony_ci 11241cb0ef41Sopenharmony_ci#### `transformStreamDefaultController.desiredSize` 11251cb0ef41Sopenharmony_ci 11261cb0ef41Sopenharmony_ci<!-- YAML 11271cb0ef41Sopenharmony_ciadded: v16.5.0 11281cb0ef41Sopenharmony_ci--> 11291cb0ef41Sopenharmony_ci 11301cb0ef41Sopenharmony_ci* Type: {number} 11311cb0ef41Sopenharmony_ci 11321cb0ef41Sopenharmony_ciThe amount of data required to fill the readable side's queue. 11331cb0ef41Sopenharmony_ci 11341cb0ef41Sopenharmony_ci#### `transformStreamDefaultController.enqueue([chunk])` 11351cb0ef41Sopenharmony_ci 11361cb0ef41Sopenharmony_ci<!-- YAML 11371cb0ef41Sopenharmony_ciadded: v16.5.0 11381cb0ef41Sopenharmony_ci--> 11391cb0ef41Sopenharmony_ci 11401cb0ef41Sopenharmony_ci* `chunk` {any} 11411cb0ef41Sopenharmony_ci 11421cb0ef41Sopenharmony_ciAppends a chunk of data to the readable side's queue. 11431cb0ef41Sopenharmony_ci 11441cb0ef41Sopenharmony_ci#### `transformStreamDefaultController.error([reason])` 11451cb0ef41Sopenharmony_ci 11461cb0ef41Sopenharmony_ci<!-- YAML 11471cb0ef41Sopenharmony_ciadded: v16.5.0 11481cb0ef41Sopenharmony_ci--> 11491cb0ef41Sopenharmony_ci 11501cb0ef41Sopenharmony_ci* `reason` {any} 11511cb0ef41Sopenharmony_ci 11521cb0ef41Sopenharmony_ciSignals to both the readable and writable side that an error has occurred 11531cb0ef41Sopenharmony_ciwhile processing the transform data, causing both sides to be abruptly 11541cb0ef41Sopenharmony_ciclosed. 11551cb0ef41Sopenharmony_ci 11561cb0ef41Sopenharmony_ci#### `transformStreamDefaultController.terminate()` 11571cb0ef41Sopenharmony_ci 11581cb0ef41Sopenharmony_ci<!-- YAML 11591cb0ef41Sopenharmony_ciadded: v16.5.0 11601cb0ef41Sopenharmony_ci--> 11611cb0ef41Sopenharmony_ci 11621cb0ef41Sopenharmony_ciCloses the readable side of the transport and causes the writable side 11631cb0ef41Sopenharmony_cito be abruptly closed with an error. 11641cb0ef41Sopenharmony_ci 11651cb0ef41Sopenharmony_ci### Class: `ByteLengthQueuingStrategy` 11661cb0ef41Sopenharmony_ci 11671cb0ef41Sopenharmony_ci<!-- YAML 11681cb0ef41Sopenharmony_ciadded: v16.5.0 11691cb0ef41Sopenharmony_cichanges: 11701cb0ef41Sopenharmony_ci - version: v18.0.0 11711cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/42225 11721cb0ef41Sopenharmony_ci description: This class is now exposed on the global object. 11731cb0ef41Sopenharmony_ci--> 11741cb0ef41Sopenharmony_ci 11751cb0ef41Sopenharmony_ci#### `new ByteLengthQueuingStrategy(init)` 11761cb0ef41Sopenharmony_ci 11771cb0ef41Sopenharmony_ci<!-- YAML 11781cb0ef41Sopenharmony_ciadded: v16.5.0 11791cb0ef41Sopenharmony_ci--> 11801cb0ef41Sopenharmony_ci 11811cb0ef41Sopenharmony_ci* `init` {Object} 11821cb0ef41Sopenharmony_ci * `highWaterMark` {number} 11831cb0ef41Sopenharmony_ci 11841cb0ef41Sopenharmony_ci#### `byteLengthQueuingStrategy.highWaterMark` 11851cb0ef41Sopenharmony_ci 11861cb0ef41Sopenharmony_ci<!-- YAML 11871cb0ef41Sopenharmony_ciadded: v16.5.0 11881cb0ef41Sopenharmony_ci--> 11891cb0ef41Sopenharmony_ci 11901cb0ef41Sopenharmony_ci* Type: {number} 11911cb0ef41Sopenharmony_ci 11921cb0ef41Sopenharmony_ci#### `byteLengthQueuingStrategy.size` 11931cb0ef41Sopenharmony_ci 11941cb0ef41Sopenharmony_ci<!-- YAML 11951cb0ef41Sopenharmony_ciadded: v16.5.0 11961cb0ef41Sopenharmony_ci--> 11971cb0ef41Sopenharmony_ci 11981cb0ef41Sopenharmony_ci* Type: {Function} 11991cb0ef41Sopenharmony_ci * `chunk` {any} 12001cb0ef41Sopenharmony_ci * Returns: {number} 12011cb0ef41Sopenharmony_ci 12021cb0ef41Sopenharmony_ci### Class: `CountQueuingStrategy` 12031cb0ef41Sopenharmony_ci 12041cb0ef41Sopenharmony_ci<!-- YAML 12051cb0ef41Sopenharmony_ciadded: v16.5.0 12061cb0ef41Sopenharmony_cichanges: 12071cb0ef41Sopenharmony_ci - version: v18.0.0 12081cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/42225 12091cb0ef41Sopenharmony_ci description: This class is now exposed on the global object. 12101cb0ef41Sopenharmony_ci--> 12111cb0ef41Sopenharmony_ci 12121cb0ef41Sopenharmony_ci#### `new CountQueuingStrategy(init)` 12131cb0ef41Sopenharmony_ci 12141cb0ef41Sopenharmony_ci<!-- YAML 12151cb0ef41Sopenharmony_ciadded: v16.5.0 12161cb0ef41Sopenharmony_ci--> 12171cb0ef41Sopenharmony_ci 12181cb0ef41Sopenharmony_ci* `init` {Object} 12191cb0ef41Sopenharmony_ci * `highWaterMark` {number} 12201cb0ef41Sopenharmony_ci 12211cb0ef41Sopenharmony_ci#### `countQueuingStrategy.highWaterMark` 12221cb0ef41Sopenharmony_ci 12231cb0ef41Sopenharmony_ci<!-- YAML 12241cb0ef41Sopenharmony_ciadded: v16.5.0 12251cb0ef41Sopenharmony_ci--> 12261cb0ef41Sopenharmony_ci 12271cb0ef41Sopenharmony_ci* Type: {number} 12281cb0ef41Sopenharmony_ci 12291cb0ef41Sopenharmony_ci#### `countQueuingStrategy.size` 12301cb0ef41Sopenharmony_ci 12311cb0ef41Sopenharmony_ci<!-- YAML 12321cb0ef41Sopenharmony_ciadded: v16.5.0 12331cb0ef41Sopenharmony_ci--> 12341cb0ef41Sopenharmony_ci 12351cb0ef41Sopenharmony_ci* Type: {Function} 12361cb0ef41Sopenharmony_ci * `chunk` {any} 12371cb0ef41Sopenharmony_ci * Returns: {number} 12381cb0ef41Sopenharmony_ci 12391cb0ef41Sopenharmony_ci### Class: `TextEncoderStream` 12401cb0ef41Sopenharmony_ci 12411cb0ef41Sopenharmony_ci<!-- YAML 12421cb0ef41Sopenharmony_ciadded: v16.6.0 12431cb0ef41Sopenharmony_cichanges: 12441cb0ef41Sopenharmony_ci - version: v18.0.0 12451cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/42225 12461cb0ef41Sopenharmony_ci description: This class is now exposed on the global object. 12471cb0ef41Sopenharmony_ci--> 12481cb0ef41Sopenharmony_ci 12491cb0ef41Sopenharmony_ci#### `new TextEncoderStream()` 12501cb0ef41Sopenharmony_ci 12511cb0ef41Sopenharmony_ci<!-- YAML 12521cb0ef41Sopenharmony_ciadded: v16.6.0 12531cb0ef41Sopenharmony_ci--> 12541cb0ef41Sopenharmony_ci 12551cb0ef41Sopenharmony_ciCreates a new `TextEncoderStream` instance. 12561cb0ef41Sopenharmony_ci 12571cb0ef41Sopenharmony_ci#### `textEncoderStream.encoding` 12581cb0ef41Sopenharmony_ci 12591cb0ef41Sopenharmony_ci<!-- YAML 12601cb0ef41Sopenharmony_ciadded: v16.6.0 12611cb0ef41Sopenharmony_ci--> 12621cb0ef41Sopenharmony_ci 12631cb0ef41Sopenharmony_ci* Type: {string} 12641cb0ef41Sopenharmony_ci 12651cb0ef41Sopenharmony_ciThe encoding supported by the `TextEncoderStream` instance. 12661cb0ef41Sopenharmony_ci 12671cb0ef41Sopenharmony_ci#### `textEncoderStream.readable` 12681cb0ef41Sopenharmony_ci 12691cb0ef41Sopenharmony_ci<!-- YAML 12701cb0ef41Sopenharmony_ciadded: v16.6.0 12711cb0ef41Sopenharmony_ci--> 12721cb0ef41Sopenharmony_ci 12731cb0ef41Sopenharmony_ci* Type: {ReadableStream} 12741cb0ef41Sopenharmony_ci 12751cb0ef41Sopenharmony_ci#### `textEncoderStream.writable` 12761cb0ef41Sopenharmony_ci 12771cb0ef41Sopenharmony_ci<!-- YAML 12781cb0ef41Sopenharmony_ciadded: v16.6.0 12791cb0ef41Sopenharmony_ci--> 12801cb0ef41Sopenharmony_ci 12811cb0ef41Sopenharmony_ci* Type: {WritableStream} 12821cb0ef41Sopenharmony_ci 12831cb0ef41Sopenharmony_ci### Class: `TextDecoderStream` 12841cb0ef41Sopenharmony_ci 12851cb0ef41Sopenharmony_ci<!-- YAML 12861cb0ef41Sopenharmony_ciadded: v16.6.0 12871cb0ef41Sopenharmony_cichanges: 12881cb0ef41Sopenharmony_ci - version: v18.0.0 12891cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/42225 12901cb0ef41Sopenharmony_ci description: This class is now exposed on the global object. 12911cb0ef41Sopenharmony_ci--> 12921cb0ef41Sopenharmony_ci 12931cb0ef41Sopenharmony_ci#### `new TextDecoderStream([encoding[, options]])` 12941cb0ef41Sopenharmony_ci 12951cb0ef41Sopenharmony_ci<!-- YAML 12961cb0ef41Sopenharmony_ciadded: v16.6.0 12971cb0ef41Sopenharmony_ci--> 12981cb0ef41Sopenharmony_ci 12991cb0ef41Sopenharmony_ci* `encoding` {string} Identifies the `encoding` that this `TextDecoder` instance 13001cb0ef41Sopenharmony_ci supports. **Default:** `'utf-8'`. 13011cb0ef41Sopenharmony_ci* `options` {Object} 13021cb0ef41Sopenharmony_ci * `fatal` {boolean} `true` if decoding failures are fatal. 13031cb0ef41Sopenharmony_ci * `ignoreBOM` {boolean} When `true`, the `TextDecoderStream` will include the 13041cb0ef41Sopenharmony_ci byte order mark in the decoded result. When `false`, the byte order mark 13051cb0ef41Sopenharmony_ci will be removed from the output. This option is only used when `encoding` is 13061cb0ef41Sopenharmony_ci `'utf-8'`, `'utf-16be'`, or `'utf-16le'`. **Default:** `false`. 13071cb0ef41Sopenharmony_ci 13081cb0ef41Sopenharmony_ciCreates a new `TextDecoderStream` instance. 13091cb0ef41Sopenharmony_ci 13101cb0ef41Sopenharmony_ci#### `textDecoderStream.encoding` 13111cb0ef41Sopenharmony_ci 13121cb0ef41Sopenharmony_ci<!-- YAML 13131cb0ef41Sopenharmony_ciadded: v16.6.0 13141cb0ef41Sopenharmony_ci--> 13151cb0ef41Sopenharmony_ci 13161cb0ef41Sopenharmony_ci* Type: {string} 13171cb0ef41Sopenharmony_ci 13181cb0ef41Sopenharmony_ciThe encoding supported by the `TextDecoderStream` instance. 13191cb0ef41Sopenharmony_ci 13201cb0ef41Sopenharmony_ci#### `textDecoderStream.fatal` 13211cb0ef41Sopenharmony_ci 13221cb0ef41Sopenharmony_ci<!-- YAML 13231cb0ef41Sopenharmony_ciadded: v16.6.0 13241cb0ef41Sopenharmony_ci--> 13251cb0ef41Sopenharmony_ci 13261cb0ef41Sopenharmony_ci* Type: {boolean} 13271cb0ef41Sopenharmony_ci 13281cb0ef41Sopenharmony_ciThe value will be `true` if decoding errors result in a `TypeError` being 13291cb0ef41Sopenharmony_cithrown. 13301cb0ef41Sopenharmony_ci 13311cb0ef41Sopenharmony_ci#### `textDecoderStream.ignoreBOM` 13321cb0ef41Sopenharmony_ci 13331cb0ef41Sopenharmony_ci<!-- YAML 13341cb0ef41Sopenharmony_ciadded: v16.6.0 13351cb0ef41Sopenharmony_ci--> 13361cb0ef41Sopenharmony_ci 13371cb0ef41Sopenharmony_ci* Type: {boolean} 13381cb0ef41Sopenharmony_ci 13391cb0ef41Sopenharmony_ciThe value will be `true` if the decoding result will include the byte order 13401cb0ef41Sopenharmony_cimark. 13411cb0ef41Sopenharmony_ci 13421cb0ef41Sopenharmony_ci#### `textDecoderStream.readable` 13431cb0ef41Sopenharmony_ci 13441cb0ef41Sopenharmony_ci<!-- YAML 13451cb0ef41Sopenharmony_ciadded: v16.6.0 13461cb0ef41Sopenharmony_ci--> 13471cb0ef41Sopenharmony_ci 13481cb0ef41Sopenharmony_ci* Type: {ReadableStream} 13491cb0ef41Sopenharmony_ci 13501cb0ef41Sopenharmony_ci#### `textDecoderStream.writable` 13511cb0ef41Sopenharmony_ci 13521cb0ef41Sopenharmony_ci<!-- YAML 13531cb0ef41Sopenharmony_ciadded: v16.6.0 13541cb0ef41Sopenharmony_ci--> 13551cb0ef41Sopenharmony_ci 13561cb0ef41Sopenharmony_ci* Type: {WritableStream} 13571cb0ef41Sopenharmony_ci 13581cb0ef41Sopenharmony_ci### Class: `CompressionStream` 13591cb0ef41Sopenharmony_ci 13601cb0ef41Sopenharmony_ci<!-- YAML 13611cb0ef41Sopenharmony_ciadded: v17.0.0 13621cb0ef41Sopenharmony_cichanges: 13631cb0ef41Sopenharmony_ci - version: v18.0.0 13641cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/42225 13651cb0ef41Sopenharmony_ci description: This class is now exposed on the global object. 13661cb0ef41Sopenharmony_ci--> 13671cb0ef41Sopenharmony_ci 13681cb0ef41Sopenharmony_ci#### `new CompressionStream(format)` 13691cb0ef41Sopenharmony_ci 13701cb0ef41Sopenharmony_ci<!-- YAML 13711cb0ef41Sopenharmony_ciadded: v17.0.0 13721cb0ef41Sopenharmony_ci--> 13731cb0ef41Sopenharmony_ci 13741cb0ef41Sopenharmony_ci* `format` {string} One of either `'deflate'` or `'gzip'`. 13751cb0ef41Sopenharmony_ci 13761cb0ef41Sopenharmony_ci#### `compressionStream.readable` 13771cb0ef41Sopenharmony_ci 13781cb0ef41Sopenharmony_ci<!-- YAML 13791cb0ef41Sopenharmony_ciadded: v17.0.0 13801cb0ef41Sopenharmony_ci--> 13811cb0ef41Sopenharmony_ci 13821cb0ef41Sopenharmony_ci* Type: {ReadableStream} 13831cb0ef41Sopenharmony_ci 13841cb0ef41Sopenharmony_ci#### `compressionStream.writable` 13851cb0ef41Sopenharmony_ci 13861cb0ef41Sopenharmony_ci<!-- YAML 13871cb0ef41Sopenharmony_ciadded: v17.0.0 13881cb0ef41Sopenharmony_ci--> 13891cb0ef41Sopenharmony_ci 13901cb0ef41Sopenharmony_ci* Type: {WritableStream} 13911cb0ef41Sopenharmony_ci 13921cb0ef41Sopenharmony_ci### Class: `DecompressionStream` 13931cb0ef41Sopenharmony_ci 13941cb0ef41Sopenharmony_ci<!-- YAML 13951cb0ef41Sopenharmony_ciadded: v17.0.0 13961cb0ef41Sopenharmony_cichanges: 13971cb0ef41Sopenharmony_ci - version: v18.0.0 13981cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/42225 13991cb0ef41Sopenharmony_ci description: This class is now exposed on the global object. 14001cb0ef41Sopenharmony_ci--> 14011cb0ef41Sopenharmony_ci 14021cb0ef41Sopenharmony_ci#### `new DecompressionStream(format)` 14031cb0ef41Sopenharmony_ci 14041cb0ef41Sopenharmony_ci<!-- YAML 14051cb0ef41Sopenharmony_ciadded: v17.0.0 14061cb0ef41Sopenharmony_ci--> 14071cb0ef41Sopenharmony_ci 14081cb0ef41Sopenharmony_ci* `format` {string} One of either `'deflate'` or `'gzip'`. 14091cb0ef41Sopenharmony_ci 14101cb0ef41Sopenharmony_ci#### `decompressionStream.readable` 14111cb0ef41Sopenharmony_ci 14121cb0ef41Sopenharmony_ci<!-- YAML 14131cb0ef41Sopenharmony_ciadded: v17.0.0 14141cb0ef41Sopenharmony_ci--> 14151cb0ef41Sopenharmony_ci 14161cb0ef41Sopenharmony_ci* Type: {ReadableStream} 14171cb0ef41Sopenharmony_ci 14181cb0ef41Sopenharmony_ci#### `decompressionStream.writable` 14191cb0ef41Sopenharmony_ci 14201cb0ef41Sopenharmony_ci<!-- YAML 14211cb0ef41Sopenharmony_ciadded: v17.0.0 14221cb0ef41Sopenharmony_ci--> 14231cb0ef41Sopenharmony_ci 14241cb0ef41Sopenharmony_ci* Type: {WritableStream} 14251cb0ef41Sopenharmony_ci 14261cb0ef41Sopenharmony_ci### Utility Consumers 14271cb0ef41Sopenharmony_ci 14281cb0ef41Sopenharmony_ci<!-- YAML 14291cb0ef41Sopenharmony_ciadded: v16.7.0 14301cb0ef41Sopenharmony_ci--> 14311cb0ef41Sopenharmony_ci 14321cb0ef41Sopenharmony_ciThe utility consumer functions provide common options for consuming 14331cb0ef41Sopenharmony_cistreams. 14341cb0ef41Sopenharmony_ci 14351cb0ef41Sopenharmony_ciThey are accessed using: 14361cb0ef41Sopenharmony_ci 14371cb0ef41Sopenharmony_ci```mjs 14381cb0ef41Sopenharmony_ciimport { 14391cb0ef41Sopenharmony_ci arrayBuffer, 14401cb0ef41Sopenharmony_ci blob, 14411cb0ef41Sopenharmony_ci buffer, 14421cb0ef41Sopenharmony_ci json, 14431cb0ef41Sopenharmony_ci text, 14441cb0ef41Sopenharmony_ci} from 'node:stream/consumers'; 14451cb0ef41Sopenharmony_ci``` 14461cb0ef41Sopenharmony_ci 14471cb0ef41Sopenharmony_ci```cjs 14481cb0ef41Sopenharmony_ciconst { 14491cb0ef41Sopenharmony_ci arrayBuffer, 14501cb0ef41Sopenharmony_ci blob, 14511cb0ef41Sopenharmony_ci buffer, 14521cb0ef41Sopenharmony_ci json, 14531cb0ef41Sopenharmony_ci text, 14541cb0ef41Sopenharmony_ci} = require('node:stream/consumers'); 14551cb0ef41Sopenharmony_ci``` 14561cb0ef41Sopenharmony_ci 14571cb0ef41Sopenharmony_ci#### `streamConsumers.arrayBuffer(stream)` 14581cb0ef41Sopenharmony_ci 14591cb0ef41Sopenharmony_ci<!-- YAML 14601cb0ef41Sopenharmony_ciadded: v16.7.0 14611cb0ef41Sopenharmony_ci--> 14621cb0ef41Sopenharmony_ci 14631cb0ef41Sopenharmony_ci* `stream` {ReadableStream|stream.Readable|AsyncIterator} 14641cb0ef41Sopenharmony_ci* Returns: {Promise} Fulfills with an `ArrayBuffer` containing the full 14651cb0ef41Sopenharmony_ci contents of the stream. 14661cb0ef41Sopenharmony_ci 14671cb0ef41Sopenharmony_ci```mjs 14681cb0ef41Sopenharmony_ciimport { arrayBuffer } from 'node:stream/consumers'; 14691cb0ef41Sopenharmony_ciimport { Readable } from 'node:stream'; 14701cb0ef41Sopenharmony_ciimport { TextEncoder } from 'node:util'; 14711cb0ef41Sopenharmony_ci 14721cb0ef41Sopenharmony_ciconst encoder = new TextEncoder(); 14731cb0ef41Sopenharmony_ciconst dataArray = encoder.encode('hello world from consumers!'); 14741cb0ef41Sopenharmony_ci 14751cb0ef41Sopenharmony_ciconst readable = Readable.from(dataArray); 14761cb0ef41Sopenharmony_ciconst data = await arrayBuffer(readable); 14771cb0ef41Sopenharmony_ciconsole.log(`from readable: ${data.byteLength}`); 14781cb0ef41Sopenharmony_ci``` 14791cb0ef41Sopenharmony_ci 14801cb0ef41Sopenharmony_ci```cjs 14811cb0ef41Sopenharmony_ciconst { arrayBuffer } = require('node:stream/consumers'); 14821cb0ef41Sopenharmony_ciconst { Readable } = require('node:stream'); 14831cb0ef41Sopenharmony_ciconst { TextEncoder } = require('node:util'); 14841cb0ef41Sopenharmony_ci 14851cb0ef41Sopenharmony_ciconst encoder = new TextEncoder(); 14861cb0ef41Sopenharmony_ciconst dataArray = encoder.encode('hello world from consumers!'); 14871cb0ef41Sopenharmony_ciconst readable = Readable.from(dataArray); 14881cb0ef41Sopenharmony_ciarrayBuffer(readable).then((data) => { 14891cb0ef41Sopenharmony_ci console.log(`from readable: ${data.byteLength}`); 14901cb0ef41Sopenharmony_ci}); 14911cb0ef41Sopenharmony_ci``` 14921cb0ef41Sopenharmony_ci 14931cb0ef41Sopenharmony_ci#### `streamConsumers.blob(stream)` 14941cb0ef41Sopenharmony_ci 14951cb0ef41Sopenharmony_ci<!-- YAML 14961cb0ef41Sopenharmony_ciadded: v16.7.0 14971cb0ef41Sopenharmony_ci--> 14981cb0ef41Sopenharmony_ci 14991cb0ef41Sopenharmony_ci* `stream` {ReadableStream|stream.Readable|AsyncIterator} 15001cb0ef41Sopenharmony_ci* Returns: {Promise} Fulfills with a {Blob} containing the full contents 15011cb0ef41Sopenharmony_ci of the stream. 15021cb0ef41Sopenharmony_ci 15031cb0ef41Sopenharmony_ci```mjs 15041cb0ef41Sopenharmony_ciimport { blob } from 'node:stream/consumers'; 15051cb0ef41Sopenharmony_ci 15061cb0ef41Sopenharmony_ciconst dataBlob = new Blob(['hello world from consumers!']); 15071cb0ef41Sopenharmony_ci 15081cb0ef41Sopenharmony_ciconst readable = dataBlob.stream(); 15091cb0ef41Sopenharmony_ciconst data = await blob(readable); 15101cb0ef41Sopenharmony_ciconsole.log(`from readable: ${data.size}`); 15111cb0ef41Sopenharmony_ci``` 15121cb0ef41Sopenharmony_ci 15131cb0ef41Sopenharmony_ci```cjs 15141cb0ef41Sopenharmony_ciconst { blob } = require('node:stream/consumers'); 15151cb0ef41Sopenharmony_ci 15161cb0ef41Sopenharmony_ciconst dataBlob = new Blob(['hello world from consumers!']); 15171cb0ef41Sopenharmony_ci 15181cb0ef41Sopenharmony_ciconst readable = dataBlob.stream(); 15191cb0ef41Sopenharmony_ciblob(readable).then((data) => { 15201cb0ef41Sopenharmony_ci console.log(`from readable: ${data.size}`); 15211cb0ef41Sopenharmony_ci}); 15221cb0ef41Sopenharmony_ci``` 15231cb0ef41Sopenharmony_ci 15241cb0ef41Sopenharmony_ci#### `streamConsumers.buffer(stream)` 15251cb0ef41Sopenharmony_ci 15261cb0ef41Sopenharmony_ci<!-- YAML 15271cb0ef41Sopenharmony_ciadded: v16.7.0 15281cb0ef41Sopenharmony_ci--> 15291cb0ef41Sopenharmony_ci 15301cb0ef41Sopenharmony_ci* `stream` {ReadableStream|stream.Readable|AsyncIterator} 15311cb0ef41Sopenharmony_ci* Returns: {Promise} Fulfills with a {Buffer} containing the full 15321cb0ef41Sopenharmony_ci contents of the stream. 15331cb0ef41Sopenharmony_ci 15341cb0ef41Sopenharmony_ci```mjs 15351cb0ef41Sopenharmony_ciimport { buffer } from 'node:stream/consumers'; 15361cb0ef41Sopenharmony_ciimport { Readable } from 'node:stream'; 15371cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer'; 15381cb0ef41Sopenharmony_ci 15391cb0ef41Sopenharmony_ciconst dataBuffer = Buffer.from('hello world from consumers!'); 15401cb0ef41Sopenharmony_ci 15411cb0ef41Sopenharmony_ciconst readable = Readable.from(dataBuffer); 15421cb0ef41Sopenharmony_ciconst data = await buffer(readable); 15431cb0ef41Sopenharmony_ciconsole.log(`from readable: ${data.length}`); 15441cb0ef41Sopenharmony_ci``` 15451cb0ef41Sopenharmony_ci 15461cb0ef41Sopenharmony_ci```cjs 15471cb0ef41Sopenharmony_ciconst { buffer } = require('node:stream/consumers'); 15481cb0ef41Sopenharmony_ciconst { Readable } = require('node:stream'); 15491cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer'); 15501cb0ef41Sopenharmony_ci 15511cb0ef41Sopenharmony_ciconst dataBuffer = Buffer.from('hello world from consumers!'); 15521cb0ef41Sopenharmony_ci 15531cb0ef41Sopenharmony_ciconst readable = Readable.from(dataBuffer); 15541cb0ef41Sopenharmony_cibuffer(readable).then((data) => { 15551cb0ef41Sopenharmony_ci console.log(`from readable: ${data.length}`); 15561cb0ef41Sopenharmony_ci}); 15571cb0ef41Sopenharmony_ci``` 15581cb0ef41Sopenharmony_ci 15591cb0ef41Sopenharmony_ci#### `streamConsumers.json(stream)` 15601cb0ef41Sopenharmony_ci 15611cb0ef41Sopenharmony_ci<!-- YAML 15621cb0ef41Sopenharmony_ciadded: v16.7.0 15631cb0ef41Sopenharmony_ci--> 15641cb0ef41Sopenharmony_ci 15651cb0ef41Sopenharmony_ci* `stream` {ReadableStream|stream.Readable|AsyncIterator} 15661cb0ef41Sopenharmony_ci* Returns: {Promise} Fulfills with the contents of the stream parsed as a 15671cb0ef41Sopenharmony_ci UTF-8 encoded string that is then passed through `JSON.parse()`. 15681cb0ef41Sopenharmony_ci 15691cb0ef41Sopenharmony_ci```mjs 15701cb0ef41Sopenharmony_ciimport { json } from 'node:stream/consumers'; 15711cb0ef41Sopenharmony_ciimport { Readable } from 'node:stream'; 15721cb0ef41Sopenharmony_ci 15731cb0ef41Sopenharmony_ciconst items = Array.from( 15741cb0ef41Sopenharmony_ci { 15751cb0ef41Sopenharmony_ci length: 100, 15761cb0ef41Sopenharmony_ci }, 15771cb0ef41Sopenharmony_ci () => ({ 15781cb0ef41Sopenharmony_ci message: 'hello world from consumers!', 15791cb0ef41Sopenharmony_ci }), 15801cb0ef41Sopenharmony_ci); 15811cb0ef41Sopenharmony_ci 15821cb0ef41Sopenharmony_ciconst readable = Readable.from(JSON.stringify(items)); 15831cb0ef41Sopenharmony_ciconst data = await json(readable); 15841cb0ef41Sopenharmony_ciconsole.log(`from readable: ${data.length}`); 15851cb0ef41Sopenharmony_ci``` 15861cb0ef41Sopenharmony_ci 15871cb0ef41Sopenharmony_ci```cjs 15881cb0ef41Sopenharmony_ciconst { json } = require('node:stream/consumers'); 15891cb0ef41Sopenharmony_ciconst { Readable } = require('node:stream'); 15901cb0ef41Sopenharmony_ci 15911cb0ef41Sopenharmony_ciconst items = Array.from( 15921cb0ef41Sopenharmony_ci { 15931cb0ef41Sopenharmony_ci length: 100, 15941cb0ef41Sopenharmony_ci }, 15951cb0ef41Sopenharmony_ci () => ({ 15961cb0ef41Sopenharmony_ci message: 'hello world from consumers!', 15971cb0ef41Sopenharmony_ci }), 15981cb0ef41Sopenharmony_ci); 15991cb0ef41Sopenharmony_ci 16001cb0ef41Sopenharmony_ciconst readable = Readable.from(JSON.stringify(items)); 16011cb0ef41Sopenharmony_cijson(readable).then((data) => { 16021cb0ef41Sopenharmony_ci console.log(`from readable: ${data.length}`); 16031cb0ef41Sopenharmony_ci}); 16041cb0ef41Sopenharmony_ci``` 16051cb0ef41Sopenharmony_ci 16061cb0ef41Sopenharmony_ci#### `streamConsumers.text(stream)` 16071cb0ef41Sopenharmony_ci 16081cb0ef41Sopenharmony_ci<!-- YAML 16091cb0ef41Sopenharmony_ciadded: v16.7.0 16101cb0ef41Sopenharmony_ci--> 16111cb0ef41Sopenharmony_ci 16121cb0ef41Sopenharmony_ci* `stream` {ReadableStream|stream.Readable|AsyncIterator} 16131cb0ef41Sopenharmony_ci* Returns: {Promise} Fulfills with the contents of the stream parsed as a 16141cb0ef41Sopenharmony_ci UTF-8 encoded string. 16151cb0ef41Sopenharmony_ci 16161cb0ef41Sopenharmony_ci```mjs 16171cb0ef41Sopenharmony_ciimport { text } from 'node:stream/consumers'; 16181cb0ef41Sopenharmony_ciimport { Readable } from 'node:stream'; 16191cb0ef41Sopenharmony_ci 16201cb0ef41Sopenharmony_ciconst readable = Readable.from('Hello world from consumers!'); 16211cb0ef41Sopenharmony_ciconst data = await text(readable); 16221cb0ef41Sopenharmony_ciconsole.log(`from readable: ${data.length}`); 16231cb0ef41Sopenharmony_ci``` 16241cb0ef41Sopenharmony_ci 16251cb0ef41Sopenharmony_ci```cjs 16261cb0ef41Sopenharmony_ciconst { text } = require('node:stream/consumers'); 16271cb0ef41Sopenharmony_ciconst { Readable } = require('node:stream'); 16281cb0ef41Sopenharmony_ci 16291cb0ef41Sopenharmony_ciconst readable = Readable.from('Hello world from consumers!'); 16301cb0ef41Sopenharmony_citext(readable).then((data) => { 16311cb0ef41Sopenharmony_ci console.log(`from readable: ${data.length}`); 16321cb0ef41Sopenharmony_ci}); 16331cb0ef41Sopenharmony_ci``` 16341cb0ef41Sopenharmony_ci 16351cb0ef41Sopenharmony_ci[Streams]: stream.md 16361cb0ef41Sopenharmony_ci[WHATWG Streams Standard]: https://streams.spec.whatwg.org/ 1637