• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html>
2<html lang="en">
3<head>
4  <meta charset="utf-8">
5  <meta name="viewport" content="width=device-width">
6  <meta name="nodejs.org:node-version" content="v18.18.2">
7  <title>Web Streams API | Node.js v18.18.2 Documentation</title>
8  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
9  <link rel="stylesheet" href="assets/style.css">
10  <link rel="stylesheet" href="assets/hljs.css">
11  <link rel="canonical" href="https://nodejs.org/api/webstreams.html">
12  <script async defer src="assets/api.js" type="text/javascript"></script>
13  <style>@media(max-width:358px){.with-17-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:654px){.with-54-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:334px){.with-14-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:678px){.with-57-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:622px){.with-50-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:638px){.with-52-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}</style>
14</head>
15<body class="alt apidoc" id="api-section-webstreams">
16  <div id="content" class="clearfix">
17    <div id="column2" class="interior">
18      <div id="intro" class="interior">
19        <a href="/" title="Go back to the home page">
20          Node.js
21        </a>
22      </div>
23      <ul>
24<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
25<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
26</ul>
27<hr class="line">
28<ul>
29<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
30<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
31<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
32<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
33<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
34<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
35<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
36<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
37<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
38<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
39<li><a href="console.html" class="nav-console">Console</a></li>
40<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
41<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
42<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
43<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
44<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
45<li><a href="dns.html" class="nav-dns">DNS</a></li>
46<li><a href="domain.html" class="nav-domain">Domain</a></li>
47<li><a href="errors.html" class="nav-errors">Errors</a></li>
48<li><a href="events.html" class="nav-events">Events</a></li>
49<li><a href="fs.html" class="nav-fs">File system</a></li>
50<li><a href="globals.html" class="nav-globals">Globals</a></li>
51<li><a href="http.html" class="nav-http">HTTP</a></li>
52<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
53<li><a href="https.html" class="nav-https">HTTPS</a></li>
54<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
55<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
56<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
57<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
58<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
59<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
60<li><a href="net.html" class="nav-net">Net</a></li>
61<li><a href="os.html" class="nav-os">OS</a></li>
62<li><a href="path.html" class="nav-path">Path</a></li>
63<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
64<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
65<li><a href="process.html" class="nav-process">Process</a></li>
66<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
67<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
68<li><a href="readline.html" class="nav-readline">Readline</a></li>
69<li><a href="repl.html" class="nav-repl">REPL</a></li>
70<li><a href="report.html" class="nav-report">Report</a></li>
71<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
72<li><a href="stream.html" class="nav-stream">Stream</a></li>
73<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
74<li><a href="test.html" class="nav-test">Test runner</a></li>
75<li><a href="timers.html" class="nav-timers">Timers</a></li>
76<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
77<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
78<li><a href="tty.html" class="nav-tty">TTY</a></li>
79<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
80<li><a href="url.html" class="nav-url">URL</a></li>
81<li><a href="util.html" class="nav-util">Utilities</a></li>
82<li><a href="v8.html" class="nav-v8">V8</a></li>
83<li><a href="vm.html" class="nav-vm">VM</a></li>
84<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
85<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
86<li><a href="webstreams.html" class="nav-webstreams active">Web Streams API</a></li>
87<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
88<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
89</ul>
90<hr class="line">
91<ul>
92<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
93</ul>
94    </div>
95
96    <div id="column1" data-id="webstreams" class="interior">
97      <header class="header">
98        <div class="header-container">
99          <h1>Node.js v18.18.2 documentation</h1>
100          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
101            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
102              <path fill="none" d="M0 0h24v24H0z" />
103              <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/>
104              <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/>
105            </svg>
106            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
107              <path d="M0 0h24v24H0z" fill="none" />
108              <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/>
109            </svg>
110          </button>
111        </div>
112        <div id="gtoc">
113          <ul>
114            <li class="pinned-header">Node.js v18.18.2</li>
115
116    <li class="picker-header">
117      <a href="#">
118        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
119        Table of contents
120      </a>
121
122      <div class="picker"><div class="toc"><ul>
123<li><span class="stability_1"><a href="#web-streams-api">Web Streams API</a></span>
124<ul>
125<li><a href="#overview">Overview</a>
126<ul>
127<li><a href="#example-readablestream">Example <code>ReadableStream</code></a></li>
128</ul>
129</li>
130<li><a href="#api">API</a>
131<ul>
132<li><a href="#class-readablestream">Class: <code>ReadableStream</code></a>
133<ul>
134<li><a href="#new-readablestreamunderlyingsource--strategy"><code>new ReadableStream([underlyingSource [, strategy]])</code></a></li>
135<li><a href="#readablestreamlocked"><code>readableStream.locked</code></a></li>
136<li><a href="#readablestreamcancelreason"><code>readableStream.cancel([reason])</code></a></li>
137<li><a href="#readablestreamgetreaderoptions"><code>readableStream.getReader([options])</code></a></li>
138<li><a href="#readablestreampipethroughtransform-options"><code>readableStream.pipeThrough(transform[, options])</code></a></li>
139<li><a href="#readablestreampipetodestination-options"><code>readableStream.pipeTo(destination[, options])</code></a></li>
140<li><a href="#readablestreamtee"><code>readableStream.tee()</code></a></li>
141<li><a href="#readablestreamvaluesoptions"><code>readableStream.values([options])</code></a></li>
142<li><a href="#async-iteration">Async Iteration</a></li>
143<li><a href="#transferring-with-postmessage">Transferring with <code>postMessage()</code></a></li>
144</ul>
145</li>
146<li><a href="#class-readablestreamdefaultreader">Class: <code>ReadableStreamDefaultReader</code></a>
147<ul>
148<li><a href="#new-readablestreamdefaultreaderstream"><code>new ReadableStreamDefaultReader(stream)</code></a></li>
149<li><a href="#readablestreamdefaultreadercancelreason"><code>readableStreamDefaultReader.cancel([reason])</code></a></li>
150<li><a href="#readablestreamdefaultreaderclosed"><code>readableStreamDefaultReader.closed</code></a></li>
151<li><a href="#readablestreamdefaultreaderread"><code>readableStreamDefaultReader.read()</code></a></li>
152<li><a href="#readablestreamdefaultreaderreleaselock"><code>readableStreamDefaultReader.releaseLock()</code></a></li>
153</ul>
154</li>
155<li><a href="#class-readablestreambyobreader">Class: <code>ReadableStreamBYOBReader</code></a>
156<ul>
157<li><a href="#new-readablestreambyobreaderstream"><code>new ReadableStreamBYOBReader(stream)</code></a></li>
158<li><a href="#readablestreambyobreadercancelreason"><code>readableStreamBYOBReader.cancel([reason])</code></a></li>
159<li><a href="#readablestreambyobreaderclosed"><code>readableStreamBYOBReader.closed</code></a></li>
160<li><a href="#readablestreambyobreaderreadview"><code>readableStreamBYOBReader.read(view)</code></a></li>
161<li><a href="#readablestreambyobreaderreleaselock"><code>readableStreamBYOBReader.releaseLock()</code></a></li>
162</ul>
163</li>
164<li><a href="#class-readablestreamdefaultcontroller">Class: <code>ReadableStreamDefaultController</code></a>
165<ul>
166<li><a href="#readablestreamdefaultcontrollerclose"><code>readableStreamDefaultController.close()</code></a></li>
167<li><a href="#readablestreamdefaultcontrollerdesiredsize"><code>readableStreamDefaultController.desiredSize</code></a></li>
168<li><a href="#readablestreamdefaultcontrollerenqueuechunk"><code>readableStreamDefaultController.enqueue([chunk])</code></a></li>
169<li><a href="#readablestreamdefaultcontrollererrorerror"><code>readableStreamDefaultController.error([error])</code></a></li>
170</ul>
171</li>
172<li><a href="#class-readablebytestreamcontroller">Class: <code>ReadableByteStreamController</code></a>
173<ul>
174<li><a href="#readablebytestreamcontrollerbyobrequest"><code>readableByteStreamController.byobRequest</code></a></li>
175<li><a href="#readablebytestreamcontrollerclose"><code>readableByteStreamController.close()</code></a></li>
176<li><a href="#readablebytestreamcontrollerdesiredsize"><code>readableByteStreamController.desiredSize</code></a></li>
177<li><a href="#readablebytestreamcontrollerenqueuechunk"><code>readableByteStreamController.enqueue(chunk)</code></a></li>
178<li><a href="#readablebytestreamcontrollererrorerror"><code>readableByteStreamController.error([error])</code></a></li>
179</ul>
180</li>
181<li><a href="#class-readablestreambyobrequest">Class: <code>ReadableStreamBYOBRequest</code></a>
182<ul>
183<li><a href="#readablestreambyobrequestrespondbyteswritten"><code>readableStreamBYOBRequest.respond(bytesWritten)</code></a></li>
184<li><a href="#readablestreambyobrequestrespondwithnewviewview"><code>readableStreamBYOBRequest.respondWithNewView(view)</code></a></li>
185<li><a href="#readablestreambyobrequestview"><code>readableStreamBYOBRequest.view</code></a></li>
186</ul>
187</li>
188<li><a href="#class-writablestream">Class: <code>WritableStream</code></a>
189<ul>
190<li><a href="#new-writablestreamunderlyingsink-strategy"><code>new WritableStream([underlyingSink[, strategy]])</code></a></li>
191<li><a href="#writablestreamabortreason"><code>writableStream.abort([reason])</code></a></li>
192<li><a href="#writablestreamclose"><code>writableStream.close()</code></a></li>
193<li><a href="#writablestreamgetwriter"><code>writableStream.getWriter()</code></a></li>
194<li><a href="#writablestreamlocked"><code>writableStream.locked</code></a></li>
195<li><a href="#transferring-with-postmessage_1">Transferring with postMessage()</a></li>
196</ul>
197</li>
198<li><a href="#class-writablestreamdefaultwriter">Class: <code>WritableStreamDefaultWriter</code></a>
199<ul>
200<li><a href="#new-writablestreamdefaultwriterstream"><code>new WritableStreamDefaultWriter(stream)</code></a></li>
201<li><a href="#writablestreamdefaultwriterabortreason"><code>writableStreamDefaultWriter.abort([reason])</code></a></li>
202<li><a href="#writablestreamdefaultwriterclose"><code>writableStreamDefaultWriter.close()</code></a></li>
203<li><a href="#writablestreamdefaultwriterclosed"><code>writableStreamDefaultWriter.closed</code></a></li>
204<li><a href="#writablestreamdefaultwriterdesiredsize"><code>writableStreamDefaultWriter.desiredSize</code></a></li>
205<li><a href="#writablestreamdefaultwriterready"><code>writableStreamDefaultWriter.ready</code></a></li>
206<li><a href="#writablestreamdefaultwriterreleaselock"><code>writableStreamDefaultWriter.releaseLock()</code></a></li>
207<li><a href="#writablestreamdefaultwriterwritechunk"><code>writableStreamDefaultWriter.write([chunk])</code></a></li>
208</ul>
209</li>
210<li><a href="#class-writablestreamdefaultcontroller">Class: <code>WritableStreamDefaultController</code></a>
211<ul>
212<li><a href="#writablestreamdefaultcontrollererrorerror"><code>writableStreamDefaultController.error([error])</code></a></li>
213<li><a href="#writablestreamdefaultcontrollersignal"><code>writableStreamDefaultController.signal</code></a></li>
214</ul>
215</li>
216<li><a href="#class-transformstream">Class: <code>TransformStream</code></a>
217<ul>
218<li><a href="#new-transformstreamtransformer-writablestrategy-readablestrategy"><code>new TransformStream([transformer[, writableStrategy[, readableStrategy]]])</code></a></li>
219<li><a href="#transformstreamreadable"><code>transformStream.readable</code></a></li>
220<li><a href="#transformstreamwritable"><code>transformStream.writable</code></a></li>
221<li><a href="#transferring-with-postmessage_2">Transferring with postMessage()</a></li>
222</ul>
223</li>
224<li><a href="#class-transformstreamdefaultcontroller">Class: <code>TransformStreamDefaultController</code></a>
225<ul>
226<li><a href="#transformstreamdefaultcontrollerdesiredsize"><code>transformStreamDefaultController.desiredSize</code></a></li>
227<li><a href="#transformstreamdefaultcontrollerenqueuechunk"><code>transformStreamDefaultController.enqueue([chunk])</code></a></li>
228<li><a href="#transformstreamdefaultcontrollererrorreason"><code>transformStreamDefaultController.error([reason])</code></a></li>
229<li><a href="#transformstreamdefaultcontrollerterminate"><code>transformStreamDefaultController.terminate()</code></a></li>
230</ul>
231</li>
232<li><a href="#class-bytelengthqueuingstrategy">Class: <code>ByteLengthQueuingStrategy</code></a>
233<ul>
234<li><a href="#new-bytelengthqueuingstrategyoptions"><code>new ByteLengthQueuingStrategy(options)</code></a></li>
235<li><a href="#bytelengthqueuingstrategyhighwatermark"><code>byteLengthQueuingStrategy.highWaterMark</code></a></li>
236<li><a href="#bytelengthqueuingstrategysize"><code>byteLengthQueuingStrategy.size</code></a></li>
237</ul>
238</li>
239<li><a href="#class-countqueuingstrategy">Class: <code>CountQueuingStrategy</code></a>
240<ul>
241<li><a href="#new-countqueuingstrategyoptions"><code>new CountQueuingStrategy(options)</code></a></li>
242<li><a href="#countqueuingstrategyhighwatermark"><code>countQueuingStrategy.highWaterMark</code></a></li>
243<li><a href="#countqueuingstrategysize"><code>countQueuingStrategy.size</code></a></li>
244</ul>
245</li>
246<li><a href="#class-textencoderstream">Class: <code>TextEncoderStream</code></a>
247<ul>
248<li><a href="#new-textencoderstream"><code>new TextEncoderStream()</code></a></li>
249<li><a href="#textencoderstreamencoding"><code>textEncoderStream.encoding</code></a></li>
250<li><a href="#textencoderstreamreadable"><code>textEncoderStream.readable</code></a></li>
251<li><a href="#textencoderstreamwritable"><code>textEncoderStream.writable</code></a></li>
252</ul>
253</li>
254<li><a href="#class-textdecoderstream">Class: <code>TextDecoderStream</code></a>
255<ul>
256<li><a href="#new-textdecoderstreamencoding-options"><code>new TextDecoderStream([encoding[, options]])</code></a></li>
257<li><a href="#textdecoderstreamencoding"><code>textDecoderStream.encoding</code></a></li>
258<li><a href="#textdecoderstreamfatal"><code>textDecoderStream.fatal</code></a></li>
259<li><a href="#textdecoderstreamignorebom"><code>textDecoderStream.ignoreBOM</code></a></li>
260<li><a href="#textdecoderstreamreadable"><code>textDecoderStream.readable</code></a></li>
261<li><a href="#textdecoderstreamwritable"><code>textDecoderStream.writable</code></a></li>
262</ul>
263</li>
264<li><a href="#class-compressionstream">Class: <code>CompressionStream</code></a>
265<ul>
266<li><a href="#new-compressionstreamformat"><code>new CompressionStream(format)</code></a></li>
267<li><a href="#compressionstreamreadable"><code>compressionStream.readable</code></a></li>
268<li><a href="#compressionstreamwritable"><code>compressionStream.writable</code></a></li>
269</ul>
270</li>
271<li><a href="#class-decompressionstream">Class: <code>DecompressionStream</code></a>
272<ul>
273<li><a href="#new-decompressionstreamformat"><code>new DecompressionStream(format)</code></a></li>
274<li><a href="#decompressionstreamreadable"><code>decompressionStream.readable</code></a></li>
275<li><a href="#decompressionstreamwritable"><code>decompressionStream.writable</code></a></li>
276</ul>
277</li>
278<li><a href="#utility-consumers">Utility Consumers</a>
279<ul>
280<li><a href="#streamconsumersarraybufferstream"><code>streamConsumers.arrayBuffer(stream)</code></a></li>
281<li><a href="#streamconsumersblobstream"><code>streamConsumers.blob(stream)</code></a></li>
282<li><a href="#streamconsumersbufferstream"><code>streamConsumers.buffer(stream)</code></a></li>
283<li><a href="#streamconsumersjsonstream"><code>streamConsumers.json(stream)</code></a></li>
284<li><a href="#streamconsumerstextstream"><code>streamConsumers.text(stream)</code></a></li>
285</ul>
286</li>
287</ul>
288</li>
289</ul>
290</li>
291</ul></div></div>
292    </li>
293
294
295    <li class="picker-header">
296      <a href="#">
297        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
298        Index
299      </a>
300
301      <div class="picker"><ul>
302<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
303<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
304
305      <li>
306        <a href="index.html">Index</a>
307      </li>
308    </ul>
309
310<hr class="line">
311<ul>
312<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
313<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
314<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
315<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
316<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
317<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
318<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
319<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
320<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
321<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
322<li><a href="console.html" class="nav-console">Console</a></li>
323<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
324<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
325<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
326<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
327<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
328<li><a href="dns.html" class="nav-dns">DNS</a></li>
329<li><a href="domain.html" class="nav-domain">Domain</a></li>
330<li><a href="errors.html" class="nav-errors">Errors</a></li>
331<li><a href="events.html" class="nav-events">Events</a></li>
332<li><a href="fs.html" class="nav-fs">File system</a></li>
333<li><a href="globals.html" class="nav-globals">Globals</a></li>
334<li><a href="http.html" class="nav-http">HTTP</a></li>
335<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
336<li><a href="https.html" class="nav-https">HTTPS</a></li>
337<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
338<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
339<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
340<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
341<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
342<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
343<li><a href="net.html" class="nav-net">Net</a></li>
344<li><a href="os.html" class="nav-os">OS</a></li>
345<li><a href="path.html" class="nav-path">Path</a></li>
346<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
347<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
348<li><a href="process.html" class="nav-process">Process</a></li>
349<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
350<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
351<li><a href="readline.html" class="nav-readline">Readline</a></li>
352<li><a href="repl.html" class="nav-repl">REPL</a></li>
353<li><a href="report.html" class="nav-report">Report</a></li>
354<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
355<li><a href="stream.html" class="nav-stream">Stream</a></li>
356<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
357<li><a href="test.html" class="nav-test">Test runner</a></li>
358<li><a href="timers.html" class="nav-timers">Timers</a></li>
359<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
360<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
361<li><a href="tty.html" class="nav-tty">TTY</a></li>
362<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
363<li><a href="url.html" class="nav-url">URL</a></li>
364<li><a href="util.html" class="nav-util">Utilities</a></li>
365<li><a href="v8.html" class="nav-v8">V8</a></li>
366<li><a href="vm.html" class="nav-vm">VM</a></li>
367<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
368<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
369<li><a href="webstreams.html" class="nav-webstreams active">Web Streams API</a></li>
370<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
371<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
372</ul>
373<hr class="line">
374<ul>
375<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
376</ul></div>
377    </li>
378
379
380    <li class="picker-header">
381      <a href="#">
382        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
383        Other versions
384      </a>
385      <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v20.x/api/webstreams.html">20.x</a></li>
386<li><a href="https://nodejs.org/docs/latest-v19.x/api/webstreams.html">19.x</a></li>
387<li><a href="https://nodejs.org/docs/latest-v18.x/api/webstreams.html">18.x <b>LTS</b></a></li>
388<li><a href="https://nodejs.org/docs/latest-v17.x/api/webstreams.html">17.x</a></li>
389<li><a href="https://nodejs.org/docs/latest-v16.x/api/webstreams.html">16.x</a></li></ol></div>
390    </li>
391
392            <li class="picker-header">
393              <a href="#">
394                <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
395                Options
396              </a>
397
398              <div class="picker">
399                <ul>
400                  <li>
401                    <a href="all.html">View on single page</a>
402                  </li>
403                  <li>
404                    <a href="webstreams.json">View as JSON</a>
405                  </li>
406                  <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/webstreams.md">Edit on GitHub</a></li>
407                </ul>
408              </div>
409            </li>
410          </ul>
411        </div>
412        <hr>
413      </header>
414
415      <details id="toc" open><summary>Table of contents</summary><ul>
416<li><span class="stability_1"><a href="#web-streams-api">Web Streams API</a></span>
417<ul>
418<li><a href="#overview">Overview</a>
419<ul>
420<li><a href="#example-readablestream">Example <code>ReadableStream</code></a></li>
421</ul>
422</li>
423<li><a href="#api">API</a>
424<ul>
425<li><a href="#class-readablestream">Class: <code>ReadableStream</code></a>
426<ul>
427<li><a href="#new-readablestreamunderlyingsource--strategy"><code>new ReadableStream([underlyingSource [, strategy]])</code></a></li>
428<li><a href="#readablestreamlocked"><code>readableStream.locked</code></a></li>
429<li><a href="#readablestreamcancelreason"><code>readableStream.cancel([reason])</code></a></li>
430<li><a href="#readablestreamgetreaderoptions"><code>readableStream.getReader([options])</code></a></li>
431<li><a href="#readablestreampipethroughtransform-options"><code>readableStream.pipeThrough(transform[, options])</code></a></li>
432<li><a href="#readablestreampipetodestination-options"><code>readableStream.pipeTo(destination[, options])</code></a></li>
433<li><a href="#readablestreamtee"><code>readableStream.tee()</code></a></li>
434<li><a href="#readablestreamvaluesoptions"><code>readableStream.values([options])</code></a></li>
435<li><a href="#async-iteration">Async Iteration</a></li>
436<li><a href="#transferring-with-postmessage">Transferring with <code>postMessage()</code></a></li>
437</ul>
438</li>
439<li><a href="#class-readablestreamdefaultreader">Class: <code>ReadableStreamDefaultReader</code></a>
440<ul>
441<li><a href="#new-readablestreamdefaultreaderstream"><code>new ReadableStreamDefaultReader(stream)</code></a></li>
442<li><a href="#readablestreamdefaultreadercancelreason"><code>readableStreamDefaultReader.cancel([reason])</code></a></li>
443<li><a href="#readablestreamdefaultreaderclosed"><code>readableStreamDefaultReader.closed</code></a></li>
444<li><a href="#readablestreamdefaultreaderread"><code>readableStreamDefaultReader.read()</code></a></li>
445<li><a href="#readablestreamdefaultreaderreleaselock"><code>readableStreamDefaultReader.releaseLock()</code></a></li>
446</ul>
447</li>
448<li><a href="#class-readablestreambyobreader">Class: <code>ReadableStreamBYOBReader</code></a>
449<ul>
450<li><a href="#new-readablestreambyobreaderstream"><code>new ReadableStreamBYOBReader(stream)</code></a></li>
451<li><a href="#readablestreambyobreadercancelreason"><code>readableStreamBYOBReader.cancel([reason])</code></a></li>
452<li><a href="#readablestreambyobreaderclosed"><code>readableStreamBYOBReader.closed</code></a></li>
453<li><a href="#readablestreambyobreaderreadview"><code>readableStreamBYOBReader.read(view)</code></a></li>
454<li><a href="#readablestreambyobreaderreleaselock"><code>readableStreamBYOBReader.releaseLock()</code></a></li>
455</ul>
456</li>
457<li><a href="#class-readablestreamdefaultcontroller">Class: <code>ReadableStreamDefaultController</code></a>
458<ul>
459<li><a href="#readablestreamdefaultcontrollerclose"><code>readableStreamDefaultController.close()</code></a></li>
460<li><a href="#readablestreamdefaultcontrollerdesiredsize"><code>readableStreamDefaultController.desiredSize</code></a></li>
461<li><a href="#readablestreamdefaultcontrollerenqueuechunk"><code>readableStreamDefaultController.enqueue([chunk])</code></a></li>
462<li><a href="#readablestreamdefaultcontrollererrorerror"><code>readableStreamDefaultController.error([error])</code></a></li>
463</ul>
464</li>
465<li><a href="#class-readablebytestreamcontroller">Class: <code>ReadableByteStreamController</code></a>
466<ul>
467<li><a href="#readablebytestreamcontrollerbyobrequest"><code>readableByteStreamController.byobRequest</code></a></li>
468<li><a href="#readablebytestreamcontrollerclose"><code>readableByteStreamController.close()</code></a></li>
469<li><a href="#readablebytestreamcontrollerdesiredsize"><code>readableByteStreamController.desiredSize</code></a></li>
470<li><a href="#readablebytestreamcontrollerenqueuechunk"><code>readableByteStreamController.enqueue(chunk)</code></a></li>
471<li><a href="#readablebytestreamcontrollererrorerror"><code>readableByteStreamController.error([error])</code></a></li>
472</ul>
473</li>
474<li><a href="#class-readablestreambyobrequest">Class: <code>ReadableStreamBYOBRequest</code></a>
475<ul>
476<li><a href="#readablestreambyobrequestrespondbyteswritten"><code>readableStreamBYOBRequest.respond(bytesWritten)</code></a></li>
477<li><a href="#readablestreambyobrequestrespondwithnewviewview"><code>readableStreamBYOBRequest.respondWithNewView(view)</code></a></li>
478<li><a href="#readablestreambyobrequestview"><code>readableStreamBYOBRequest.view</code></a></li>
479</ul>
480</li>
481<li><a href="#class-writablestream">Class: <code>WritableStream</code></a>
482<ul>
483<li><a href="#new-writablestreamunderlyingsink-strategy"><code>new WritableStream([underlyingSink[, strategy]])</code></a></li>
484<li><a href="#writablestreamabortreason"><code>writableStream.abort([reason])</code></a></li>
485<li><a href="#writablestreamclose"><code>writableStream.close()</code></a></li>
486<li><a href="#writablestreamgetwriter"><code>writableStream.getWriter()</code></a></li>
487<li><a href="#writablestreamlocked"><code>writableStream.locked</code></a></li>
488<li><a href="#transferring-with-postmessage_1">Transferring with postMessage()</a></li>
489</ul>
490</li>
491<li><a href="#class-writablestreamdefaultwriter">Class: <code>WritableStreamDefaultWriter</code></a>
492<ul>
493<li><a href="#new-writablestreamdefaultwriterstream"><code>new WritableStreamDefaultWriter(stream)</code></a></li>
494<li><a href="#writablestreamdefaultwriterabortreason"><code>writableStreamDefaultWriter.abort([reason])</code></a></li>
495<li><a href="#writablestreamdefaultwriterclose"><code>writableStreamDefaultWriter.close()</code></a></li>
496<li><a href="#writablestreamdefaultwriterclosed"><code>writableStreamDefaultWriter.closed</code></a></li>
497<li><a href="#writablestreamdefaultwriterdesiredsize"><code>writableStreamDefaultWriter.desiredSize</code></a></li>
498<li><a href="#writablestreamdefaultwriterready"><code>writableStreamDefaultWriter.ready</code></a></li>
499<li><a href="#writablestreamdefaultwriterreleaselock"><code>writableStreamDefaultWriter.releaseLock()</code></a></li>
500<li><a href="#writablestreamdefaultwriterwritechunk"><code>writableStreamDefaultWriter.write([chunk])</code></a></li>
501</ul>
502</li>
503<li><a href="#class-writablestreamdefaultcontroller">Class: <code>WritableStreamDefaultController</code></a>
504<ul>
505<li><a href="#writablestreamdefaultcontrollererrorerror"><code>writableStreamDefaultController.error([error])</code></a></li>
506<li><a href="#writablestreamdefaultcontrollersignal"><code>writableStreamDefaultController.signal</code></a></li>
507</ul>
508</li>
509<li><a href="#class-transformstream">Class: <code>TransformStream</code></a>
510<ul>
511<li><a href="#new-transformstreamtransformer-writablestrategy-readablestrategy"><code>new TransformStream([transformer[, writableStrategy[, readableStrategy]]])</code></a></li>
512<li><a href="#transformstreamreadable"><code>transformStream.readable</code></a></li>
513<li><a href="#transformstreamwritable"><code>transformStream.writable</code></a></li>
514<li><a href="#transferring-with-postmessage_2">Transferring with postMessage()</a></li>
515</ul>
516</li>
517<li><a href="#class-transformstreamdefaultcontroller">Class: <code>TransformStreamDefaultController</code></a>
518<ul>
519<li><a href="#transformstreamdefaultcontrollerdesiredsize"><code>transformStreamDefaultController.desiredSize</code></a></li>
520<li><a href="#transformstreamdefaultcontrollerenqueuechunk"><code>transformStreamDefaultController.enqueue([chunk])</code></a></li>
521<li><a href="#transformstreamdefaultcontrollererrorreason"><code>transformStreamDefaultController.error([reason])</code></a></li>
522<li><a href="#transformstreamdefaultcontrollerterminate"><code>transformStreamDefaultController.terminate()</code></a></li>
523</ul>
524</li>
525<li><a href="#class-bytelengthqueuingstrategy">Class: <code>ByteLengthQueuingStrategy</code></a>
526<ul>
527<li><a href="#new-bytelengthqueuingstrategyoptions"><code>new ByteLengthQueuingStrategy(options)</code></a></li>
528<li><a href="#bytelengthqueuingstrategyhighwatermark"><code>byteLengthQueuingStrategy.highWaterMark</code></a></li>
529<li><a href="#bytelengthqueuingstrategysize"><code>byteLengthQueuingStrategy.size</code></a></li>
530</ul>
531</li>
532<li><a href="#class-countqueuingstrategy">Class: <code>CountQueuingStrategy</code></a>
533<ul>
534<li><a href="#new-countqueuingstrategyoptions"><code>new CountQueuingStrategy(options)</code></a></li>
535<li><a href="#countqueuingstrategyhighwatermark"><code>countQueuingStrategy.highWaterMark</code></a></li>
536<li><a href="#countqueuingstrategysize"><code>countQueuingStrategy.size</code></a></li>
537</ul>
538</li>
539<li><a href="#class-textencoderstream">Class: <code>TextEncoderStream</code></a>
540<ul>
541<li><a href="#new-textencoderstream"><code>new TextEncoderStream()</code></a></li>
542<li><a href="#textencoderstreamencoding"><code>textEncoderStream.encoding</code></a></li>
543<li><a href="#textencoderstreamreadable"><code>textEncoderStream.readable</code></a></li>
544<li><a href="#textencoderstreamwritable"><code>textEncoderStream.writable</code></a></li>
545</ul>
546</li>
547<li><a href="#class-textdecoderstream">Class: <code>TextDecoderStream</code></a>
548<ul>
549<li><a href="#new-textdecoderstreamencoding-options"><code>new TextDecoderStream([encoding[, options]])</code></a></li>
550<li><a href="#textdecoderstreamencoding"><code>textDecoderStream.encoding</code></a></li>
551<li><a href="#textdecoderstreamfatal"><code>textDecoderStream.fatal</code></a></li>
552<li><a href="#textdecoderstreamignorebom"><code>textDecoderStream.ignoreBOM</code></a></li>
553<li><a href="#textdecoderstreamreadable"><code>textDecoderStream.readable</code></a></li>
554<li><a href="#textdecoderstreamwritable"><code>textDecoderStream.writable</code></a></li>
555</ul>
556</li>
557<li><a href="#class-compressionstream">Class: <code>CompressionStream</code></a>
558<ul>
559<li><a href="#new-compressionstreamformat"><code>new CompressionStream(format)</code></a></li>
560<li><a href="#compressionstreamreadable"><code>compressionStream.readable</code></a></li>
561<li><a href="#compressionstreamwritable"><code>compressionStream.writable</code></a></li>
562</ul>
563</li>
564<li><a href="#class-decompressionstream">Class: <code>DecompressionStream</code></a>
565<ul>
566<li><a href="#new-decompressionstreamformat"><code>new DecompressionStream(format)</code></a></li>
567<li><a href="#decompressionstreamreadable"><code>decompressionStream.readable</code></a></li>
568<li><a href="#decompressionstreamwritable"><code>decompressionStream.writable</code></a></li>
569</ul>
570</li>
571<li><a href="#utility-consumers">Utility Consumers</a>
572<ul>
573<li><a href="#streamconsumersarraybufferstream"><code>streamConsumers.arrayBuffer(stream)</code></a></li>
574<li><a href="#streamconsumersblobstream"><code>streamConsumers.blob(stream)</code></a></li>
575<li><a href="#streamconsumersbufferstream"><code>streamConsumers.buffer(stream)</code></a></li>
576<li><a href="#streamconsumersjsonstream"><code>streamConsumers.json(stream)</code></a></li>
577<li><a href="#streamconsumerstextstream"><code>streamConsumers.text(stream)</code></a></li>
578</ul>
579</li>
580</ul>
581</li>
582</ul>
583</li>
584</ul></details>
585
586      <div id="apicontent">
587        <h2>Web Streams API<span><a class="mark" href="#web-streams-api" id="web-streams-api">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_web_streams_api"></a></h2>
588
589<div class="api_metadata">
590<details class="changelog"><summary>History</summary>
591<table>
592<tbody><tr><th>Version</th><th>Changes</th></tr>
593<tr><td>v18.0.0</td>
594<td><p>Use of this API no longer emit a runtime warning.</p></td></tr>
595<tr><td>v16.5.0</td>
596<td><p><span>Added in: v16.5.0</span></p></td></tr>
597</tbody></table>
598</details>
599</div>
600<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental.</div><p></p>
601<p>An implementation of the <a href="https://streams.spec.whatwg.org/">WHATWG Streams Standard</a>.</p>
602<section><h3>Overview<span><a class="mark" href="#overview" id="overview">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_overview"></a></h3>
603<p>The <a href="https://streams.spec.whatwg.org/">WHATWG Streams Standard</a> (or "web streams") defines an API for handling
604streaming data. It is similar to the Node.js <a href="stream.html">Streams</a> API but emerged later
605and has become the "standard" API for streaming data across many JavaScript
606environments.</p>
607<p>There are three primary types of objects:</p>
608<ul>
609<li><code>ReadableStream</code> - Represents a source of streaming data.</li>
610<li><code>WritableStream</code> - Represents a destination for streaming data.</li>
611<li><code>TransformStream</code> - Represents an algorithm for transforming streaming data.</li>
612</ul>
613<h4>Example <code>ReadableStream</code><span><a class="mark" href="#example-readablestream" id="example-readablestream">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_example_readablestream"></a></h4>
614<p>This example creates a simple <code>ReadableStream</code> that pushes the current
615<code>performance.now()</code> timestamp once every second forever. An async iterable
616is used to read the data from the stream.</p>
617
618<pre class="with-17-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> {
619  <span class="hljs-title class_">ReadableStream</span>,
620} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:stream/web'</span>;
621
622<span class="hljs-keyword">import</span> {
623  <span class="hljs-built_in">setInterval</span> <span class="hljs-keyword">as</span> every,
624} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:timers/promises'</span>;
625
626<span class="hljs-keyword">import</span> {
627  performance,
628} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:perf_hooks'</span>;
629
630<span class="hljs-keyword">const</span> <span class="hljs-variable constant_">SECOND</span> = <span class="hljs-number">1000</span>;
631
632<span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ReadableStream</span>({
633  <span class="hljs-keyword">async</span> <span class="hljs-title function_">start</span>(<span class="hljs-params">controller</span>) {
634    <span class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</span> _ <span class="hljs-keyword">of</span> <span class="hljs-title function_">every</span>(<span class="hljs-variable constant_">SECOND</span>))
635      controller.<span class="hljs-title function_">enqueue</span>(performance.<span class="hljs-title function_">now</span>());
636  },
637});
638
639<span class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</span> value <span class="hljs-keyword">of</span> stream)
640  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> {
641  <span class="hljs-title class_">ReadableStream</span>,
642} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:stream/web'</span>);
643
644<span class="hljs-keyword">const</span> {
645  <span class="hljs-attr">setInterval</span>: every,
646} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:timers/promises'</span>);
647
648<span class="hljs-keyword">const</span> {
649  performance,
650} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:perf_hooks'</span>);
651
652<span class="hljs-keyword">const</span> <span class="hljs-variable constant_">SECOND</span> = <span class="hljs-number">1000</span>;
653
654<span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ReadableStream</span>({
655  <span class="hljs-keyword">async</span> <span class="hljs-title function_">start</span>(<span class="hljs-params">controller</span>) {
656    <span class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</span> _ <span class="hljs-keyword">of</span> <span class="hljs-title function_">every</span>(<span class="hljs-variable constant_">SECOND</span>))
657      controller.<span class="hljs-title function_">enqueue</span>(performance.<span class="hljs-title function_">now</span>());
658  },
659});
660
661(<span class="hljs-keyword">async</span> () => {
662  <span class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</span> value <span class="hljs-keyword">of</span> stream)
663    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value);
664})();</code><button class="copy-button">copy</button></pre>
665</section><section><h3>API<span><a class="mark" href="#api" id="api">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_api"></a></h3>
666<h4>Class: <code>ReadableStream</code><span><a class="mark" href="#class-readablestream" id="class-readablestream">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_class_readablestream"></a></h4>
667<div class="api_metadata">
668<details class="changelog"><summary>History</summary>
669<table>
670<tbody><tr><th>Version</th><th>Changes</th></tr>
671<tr><td>v18.0.0</td>
672<td><p>This class is now exposed on the global object.</p></td></tr>
673<tr><td>v16.5.0</td>
674<td><p><span>Added in: v16.5.0</span></p></td></tr>
675</tbody></table>
676</details>
677</div>
678<h5><code>new ReadableStream([underlyingSource [, strategy]])</code><span><a class="mark" href="#new-readablestreamunderlyingsource--strategy" id="new-readablestreamunderlyingsource--strategy">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_new_readablestream_underlyingsource_strategy"></a></h5>
679<div class="api_metadata">
680<span>Added in: v16.5.0</span>
681</div>
682<!--lint disable maximum-line-length remark-lint-->
683<ul>
684<li><code>underlyingSource</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
685<ul>
686<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> A user-defined function that is invoked immediately when
687the <code>ReadableStream</code> is created.
688<ul>
689<li><code>controller</code> <a href="webstreams.html#class-readablestreamdefaultcontroller" class="type">&#x3C;ReadableStreamDefaultController></a> | <a href="webstreams.html#class-readablebytestreamcontroller" class="type">&#x3C;ReadableByteStreamController></a></li>
690<li>Returns: <code>undefined</code> or a promise fulfilled with <code>undefined</code>.</li>
691</ul>
692</li>
693<li><code>pull</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> A user-defined function that is called repeatedly when the
694<code>ReadableStream</code> internal queue is not full. The operation may be sync or
695async. If async, the function will not be called again until the previously
696returned promise is fulfilled.
697<ul>
698<li><code>controller</code> <a href="webstreams.html#class-readablestreamdefaultcontroller" class="type">&#x3C;ReadableStreamDefaultController></a> | <a href="webstreams.html#class-readablebytestreamcontroller" class="type">&#x3C;ReadableByteStreamController></a></li>
699<li>Returns: A promise fulfilled with <code>undefined</code>.</li>
700</ul>
701</li>
702<li><code>cancel</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> A user-defined function that is called when the
703<code>ReadableStream</code> is canceled.
704<ul>
705<li><code>reason</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
706<li>Returns: A promise fulfilled with <code>undefined</code>.</li>
707</ul>
708</li>
709<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Must be <code>'bytes'</code> or <code>undefined</code>.</li>
710<li><code>autoAllocateChunkSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Used only when <code>type</code> is equal to
711<code>'bytes'</code>. When set to a non-zero value a view buffer is automatically
712allocated to <code>ReadableByteStreamController.byobRequest</code>. When not set
713one must use stream's internal queues to transfer data via the default
714reader <code>ReadableStreamDefaultReader</code>.</li>
715</ul>
716</li>
717<li><code>strategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
718<ul>
719<li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The maximum internal queue size before backpressure
720is applied.</li>
721<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> A user-defined function used to identify the size of each
722chunk of data.
723<ul>
724<li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
725<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
726</ul>
727</li>
728</ul>
729</li>
730</ul>
731<!--lint enable maximum-line-length remark-lint-->
732<h5><code>readableStream.locked</code><span><a class="mark" href="#readablestreamlocked" id="readablestreamlocked">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_readablestream_locked"></a></h5>
733<div class="api_metadata">
734<span>Added in: v16.5.0</span>
735</div>
736<ul>
737<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Set to <code>true</code> if there is an active reader for this
738<a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a>.</li>
739</ul>
740<p>The <code>readableStream.locked</code> property is <code>false</code> by default, and is
741switched to <code>true</code> while there is an active reader consuming the
742stream's data.</p>
743<h5><code>readableStream.cancel([reason])</code><span><a class="mark" href="#readablestreamcancelreason" id="readablestreamcancelreason">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_readablestream_cancel_reason"></a></h5>
744<div class="api_metadata">
745<span>Added in: v16.5.0</span>
746</div>
747<ul>
748<li><code>reason</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
749<li>Returns: A promise fulfilled with <code>undefined</code> once cancelation has
750been completed.</li>
751</ul>
752<h5><code>readableStream.getReader([options])</code><span><a class="mark" href="#readablestreamgetreaderoptions" id="readablestreamgetreaderoptions">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_readablestream_getreader_options"></a></h5>
753<div class="api_metadata">
754<span>Added in: v16.5.0</span>
755</div>
756<ul>
757<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
758<ul>
759<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <code>'byob'</code> or <code>undefined</code></li>
760</ul>
761</li>
762<li>Returns: <a href="webstreams.html#class-readablestreamdefaultreader" class="type">&#x3C;ReadableStreamDefaultReader></a> | <a href="webstreams.html#class-readablestreambyobreader" class="type">&#x3C;ReadableStreamBYOBReader></a></li>
763</ul>
764
765<pre class="with-54-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">ReadableStream</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:stream/web'</span>;
766
767<span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ReadableStream</span>();
768
769<span class="hljs-keyword">const</span> reader = stream.<span class="hljs-title function_">getReader</span>();
770
771<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-keyword">await</span> reader.<span class="hljs-title function_">read</span>());</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">ReadableStream</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:stream/web'</span>);
772
773<span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ReadableStream</span>();
774
775<span class="hljs-keyword">const</span> reader = stream.<span class="hljs-title function_">getReader</span>();
776
777reader.<span class="hljs-title function_">read</span>().<span class="hljs-title function_">then</span>(<span class="hljs-variable language_">console</span>.<span class="hljs-property">log</span>);</code><button class="copy-button">copy</button></pre>
778<p>Causes the <code>readableStream.locked</code> to be <code>true</code>.</p>
779<h5><code>readableStream.pipeThrough(transform[, options])</code><span><a class="mark" href="#readablestreampipethroughtransform-options" id="readablestreampipethroughtransform-options">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_readablestream_pipethrough_transform_options"></a></h5>
780<div class="api_metadata">
781<span>Added in: v16.5.0</span>
782</div>
783<ul>
784<li><code>transform</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
785<ul>
786<li><code>readable</code> <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> The <code>ReadableStream</code> to which
787<code>transform.writable</code> will push the potentially modified data
788is receives from this <code>ReadableStream</code>.</li>
789<li><code>writable</code> <a href="webstreams.html#class-writablestream" class="type">&#x3C;WritableStream></a> The <code>WritableStream</code> to which this
790<code>ReadableStream</code>'s data will be written.</li>
791</ul>
792</li>
793<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
794<ul>
795<li><code>preventAbort</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, errors in this <code>ReadableStream</code>
796will not cause <code>transform.writable</code> to be aborted.</li>
797<li><code>preventCancel</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, errors in the destination
798<code>transform.writable</code> do not cause this <code>ReadableStream</code> to be
799canceled.</li>
800<li><code>preventClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, closing this <code>ReadableStream</code>
801does not cause <code>transform.writable</code> to be closed.</li>
802<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type">&#x3C;AbortSignal></a> Allows the transfer of data to be canceled
803using an <a href="globals.html#class-abortcontroller" class="type">&#x3C;AbortController></a>.</li>
804</ul>
805</li>
806<li>Returns: <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> From <code>transform.readable</code>.</li>
807</ul>
808<p>Connects this <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> to the pair of <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> and
809<a href="webstreams.html#class-writablestream" class="type">&#x3C;WritableStream></a> provided in the <code>transform</code> argument such that the
810data from this <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> is written in to <code>transform.writable</code>,
811possibly transformed, then pushed to <code>transform.readable</code>. Once the
812pipeline is configured, <code>transform.readable</code> is returned.</p>
813<p>Causes the <code>readableStream.locked</code> to be <code>true</code> while the pipe operation
814is active.</p>
815
816<pre class="with-17-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> {
817  <span class="hljs-title class_">ReadableStream</span>,
818  <span class="hljs-title class_">TransformStream</span>,
819} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:stream/web'</span>;
820
821<span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ReadableStream</span>({
822  <span class="hljs-title function_">start</span>(<span class="hljs-params">controller</span>) {
823    controller.<span class="hljs-title function_">enqueue</span>(<span class="hljs-string">'a'</span>);
824  },
825});
826
827<span class="hljs-keyword">const</span> transform = <span class="hljs-keyword">new</span> <span class="hljs-title class_">TransformStream</span>({
828  <span class="hljs-title function_">transform</span>(<span class="hljs-params">chunk, controller</span>) {
829    controller.<span class="hljs-title function_">enqueue</span>(chunk.<span class="hljs-title function_">toUpperCase</span>());
830  },
831});
832
833<span class="hljs-keyword">const</span> transformedStream = stream.<span class="hljs-title function_">pipeThrough</span>(transform);
834
835<span class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</span> chunk <span class="hljs-keyword">of</span> transformedStream)
836  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(chunk);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> {
837  <span class="hljs-title class_">ReadableStream</span>,
838  <span class="hljs-title class_">TransformStream</span>,
839} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:stream/web'</span>);
840
841<span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ReadableStream</span>({
842  <span class="hljs-title function_">start</span>(<span class="hljs-params">controller</span>) {
843    controller.<span class="hljs-title function_">enqueue</span>(<span class="hljs-string">'a'</span>);
844  },
845});
846
847<span class="hljs-keyword">const</span> transform = <span class="hljs-keyword">new</span> <span class="hljs-title class_">TransformStream</span>({
848  <span class="hljs-title function_">transform</span>(<span class="hljs-params">chunk, controller</span>) {
849    controller.<span class="hljs-title function_">enqueue</span>(chunk.<span class="hljs-title function_">toUpperCase</span>());
850  },
851});
852
853<span class="hljs-keyword">const</span> transformedStream = stream.<span class="hljs-title function_">pipeThrough</span>(transform);
854
855(<span class="hljs-keyword">async</span> () => {
856  <span class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</span> chunk <span class="hljs-keyword">of</span> transformedStream)
857    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(chunk);
858})();</code><button class="copy-button">copy</button></pre>
859<h5><code>readableStream.pipeTo(destination[, options])</code><span><a class="mark" href="#readablestreampipetodestination-options" id="readablestreampipetodestination-options">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_readablestream_pipeto_destination_options"></a></h5>
860<div class="api_metadata">
861<span>Added in: v16.5.0</span>
862</div>
863<ul>
864<li><code>destination</code> <a href="webstreams.html#class-writablestream" class="type">&#x3C;WritableStream></a> A <a href="webstreams.html#class-writablestream" class="type">&#x3C;WritableStream></a> to which this
865<code>ReadableStream</code>'s data will be written.</li>
866<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
867<ul>
868<li><code>preventAbort</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, errors in this <code>ReadableStream</code>
869will not cause <code>destination</code> to be aborted.</li>
870<li><code>preventCancel</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, errors in the <code>destination</code>
871will not cause this <code>ReadableStream</code> to be canceled.</li>
872<li><code>preventClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, closing this <code>ReadableStream</code>
873does not cause <code>destination</code> to be closed.</li>
874<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type">&#x3C;AbortSignal></a> Allows the transfer of data to be canceled
875using an <a href="globals.html#class-abortcontroller" class="type">&#x3C;AbortController></a>.</li>
876</ul>
877</li>
878<li>Returns: A promise fulfilled with <code>undefined</code></li>
879</ul>
880<p>Causes the <code>readableStream.locked</code> to be <code>true</code> while the pipe operation
881is active.</p>
882<h5><code>readableStream.tee()</code><span><a class="mark" href="#readablestreamtee" id="readablestreamtee">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_readablestream_tee"></a></h5>
883<div class="api_metadata">
884<details class="changelog"><summary>History</summary>
885<table>
886<tbody><tr><th>Version</th><th>Changes</th></tr>
887<tr><td>v18.10.0</td>
888<td><p>Support teeing a readable byte stream.</p></td></tr>
889<tr><td>v16.5.0</td>
890<td><p><span>Added in: v16.5.0</span></p></td></tr>
891</tbody></table>
892</details>
893</div>
894<ul>
895<li>Returns: <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream[]></a></li>
896</ul>
897<p>Returns a pair of new <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> instances to which this
898<code>ReadableStream</code>'s data will be forwarded. Each will receive the
899same data.</p>
900<p>Causes the <code>readableStream.locked</code> to be <code>true</code>.</p>
901<h5><code>readableStream.values([options])</code><span><a class="mark" href="#readablestreamvaluesoptions" id="readablestreamvaluesoptions">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_readablestream_values_options"></a></h5>
902<div class="api_metadata">
903<span>Added in: v16.5.0</span>
904</div>
905<ul>
906<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
907<ul>
908<li><code>preventCancel</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, prevents the <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a>
909from being closed when the async iterator abruptly terminates.
910<strong>Default</strong>: <code>false</code>.</li>
911</ul>
912</li>
913</ul>
914<p>Creates and returns an async iterator usable for consuming this
915<code>ReadableStream</code>'s data.</p>
916<p>Causes the <code>readableStream.locked</code> to be <code>true</code> while the async iterator
917is active.</p>
918<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
919
920<span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ReadableStream</span>(<span class="hljs-title function_">getSomeSource</span>());
921
922<span class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</span> chunk <span class="hljs-keyword">of</span> stream.<span class="hljs-title function_">values</span>({ <span class="hljs-attr">preventCancel</span>: <span class="hljs-literal">true</span> }))
923  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(chunk).<span class="hljs-title function_">toString</span>());</code> <button class="copy-button">copy</button></pre>
924<h5>Async Iteration<span><a class="mark" href="#async-iteration" id="async-iteration">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_async_iteration"></a></h5>
925<p>The <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> object supports the async iterator protocol using
926<code>for await</code> syntax.</p>
927<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
928
929<span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ReadableStream</span>(<span class="hljs-title function_">getSomeSource</span>());
930
931<span class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</span> chunk <span class="hljs-keyword">of</span> stream)
932  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(chunk).<span class="hljs-title function_">toString</span>());</code> <button class="copy-button">copy</button></pre>
933<p>The async iterator will consume the <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> until it terminates.</p>
934<p>By default, if the async iterator exits early (via either a <code>break</code>,
935<code>return</code>, or a <code>throw</code>), the <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> will be closed. To prevent
936automatic closing of the <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a>, use the <code>readableStream.values()</code>
937method to acquire the async iterator and set the <code>preventCancel</code> option to
938<code>true</code>.</p>
939<p>The <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> must not be locked (that is, it must not have an existing
940active reader). During the async iteration, the <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> will be locked.</p>
941<h5>Transferring with <code>postMessage()</code><span><a class="mark" href="#transferring-with-postmessage" id="transferring-with-postmessage">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_transferring_with_postmessage"></a></h5>
942<p>A <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> instance can be transferred using a <a href="worker_threads.html#class-messageport" class="type">&#x3C;MessagePort></a>.</p>
943<pre><code class="language-js"><span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ReadableStream</span>(<span class="hljs-title function_">getReadableSourceSomehow</span>());
944
945<span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>();
946
947port1.<span class="hljs-property">onmessage</span> = <span class="hljs-function">(<span class="hljs-params">{ data }</span>) =></span> {
948  data.<span class="hljs-title function_">getReader</span>().<span class="hljs-title function_">read</span>().<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">chunk</span>) =></span> {
949    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(chunk);
950  });
951};
952
953port2.<span class="hljs-title function_">postMessage</span>(stream, [stream]);</code> <button class="copy-button">copy</button></pre>
954<h4>Class: <code>ReadableStreamDefaultReader</code><span><a class="mark" href="#class-readablestreamdefaultreader" id="class-readablestreamdefaultreader">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_class_readablestreamdefaultreader"></a></h4>
955<div class="api_metadata">
956<details class="changelog"><summary>History</summary>
957<table>
958<tbody><tr><th>Version</th><th>Changes</th></tr>
959<tr><td>v18.0.0</td>
960<td><p>This class is now exposed on the global object.</p></td></tr>
961<tr><td>v16.5.0</td>
962<td><p><span>Added in: v16.5.0</span></p></td></tr>
963</tbody></table>
964</details>
965</div>
966<p>By default, calling <code>readableStream.getReader()</code> with no arguments
967will return an instance of <code>ReadableStreamDefaultReader</code>. The default
968reader treats the chunks of data passed through the stream as opaque
969values, which allows the <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> to work with generally any
970JavaScript value.</p>
971<h5><code>new ReadableStreamDefaultReader(stream)</code><span><a class="mark" href="#new-readablestreamdefaultreaderstream" id="new-readablestreamdefaultreaderstream">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_new_readablestreamdefaultreader_stream"></a></h5>
972<div class="api_metadata">
973<span>Added in: v16.5.0</span>
974</div>
975<ul>
976<li><code>stream</code> <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a></li>
977</ul>
978<p>Creates a new <a href="webstreams.html#class-readablestreamdefaultreader" class="type">&#x3C;ReadableStreamDefaultReader></a> that is locked to the
979given <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a>.</p>
980<h5><code>readableStreamDefaultReader.cancel([reason])</code><span><a class="mark" href="#readablestreamdefaultreadercancelreason" id="readablestreamdefaultreadercancelreason">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_readablestreamdefaultreader_cancel_reason"></a></h5>
981<div class="api_metadata">
982<span>Added in: v16.5.0</span>
983</div>
984<ul>
985<li><code>reason</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
986<li>Returns: A promise fulfilled with <code>undefined</code>.</li>
987</ul>
988<p>Cancels the <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> and returns a promise that is fulfilled
989when the underlying stream has been canceled.</p>
990<h5><code>readableStreamDefaultReader.closed</code><span><a class="mark" href="#readablestreamdefaultreaderclosed" id="readablestreamdefaultreaderclosed">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_readablestreamdefaultreader_closed"></a></h5>
991<div class="api_metadata">
992<span>Added in: v16.5.0</span>
993</div>
994<ul>
995<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfilled with <code>undefined</code> when the associated
996<a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> is closed or rejected if the stream errors or the reader's
997lock is released before the stream finishes closing.</li>
998</ul>
999<h5><code>readableStreamDefaultReader.read()</code><span><a class="mark" href="#readablestreamdefaultreaderread" id="readablestreamdefaultreaderread">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_readablestreamdefaultreader_read"></a></h5>
1000<div class="api_metadata">
1001<span>Added in: v16.5.0</span>
1002</div>
1003<ul>
1004<li>Returns: A promise fulfilled with an object:
1005<ul>
1006<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a></li>
1007<li><code>done</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1008</ul>
1009</li>
1010</ul>
1011<p>Requests the next chunk of data from the underlying <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a>
1012and returns a promise that is fulfilled with the data once it is
1013available.</p>
1014<h5><code>readableStreamDefaultReader.releaseLock()</code><span><a class="mark" href="#readablestreamdefaultreaderreleaselock" id="readablestreamdefaultreaderreleaselock">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_readablestreamdefaultreader_releaselock"></a></h5>
1015<div class="api_metadata">
1016<span>Added in: v16.5.0</span>
1017</div>
1018<p>Releases this reader's lock on the underlying <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a>.</p>
1019<h4>Class: <code>ReadableStreamBYOBReader</code><span><a class="mark" href="#class-readablestreambyobreader" id="class-readablestreambyobreader">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_class_readablestreambyobreader"></a></h4>
1020<div class="api_metadata">
1021<details class="changelog"><summary>History</summary>
1022<table>
1023<tbody><tr><th>Version</th><th>Changes</th></tr>
1024<tr><td>v18.0.0</td>
1025<td><p>This class is now exposed on the global object.</p></td></tr>
1026<tr><td>v16.5.0</td>
1027<td><p><span>Added in: v16.5.0</span></p></td></tr>
1028</tbody></table>
1029</details>
1030</div>
1031<p>The <code>ReadableStreamBYOBReader</code> is an alternative consumer for
1032byte-oriented <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a>s (those that are created with
1033<code>underlyingSource.type</code> set equal to <code>'bytes'</code> when the
1034<code>ReadableStream</code> was created).</p>
1035<p>The <code>BYOB</code> is short for "bring your own buffer". This is a
1036pattern that allows for more efficient reading of byte-oriented
1037data that avoids extraneous copying.</p>
1038<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> {
1039  open,
1040} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
1041
1042<span class="hljs-keyword">import</span> {
1043  <span class="hljs-title class_">ReadableStream</span>,
1044} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:stream/web'</span>;
1045
1046<span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
1047
1048<span class="hljs-keyword">class</span> <span class="hljs-title class_">Source</span> {
1049  type = <span class="hljs-string">'bytes'</span>;
1050  autoAllocateChunkSize = <span class="hljs-number">1024</span>;
1051
1052  <span class="hljs-keyword">async</span> <span class="hljs-title function_">start</span>(<span class="hljs-params">controller</span>) {
1053    <span class="hljs-variable language_">this</span>.<span class="hljs-property">file</span> = <span class="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-keyword">import</span>.<span class="hljs-property">meta</span>.<span class="hljs-property">url</span>));
1054    <span class="hljs-variable language_">this</span>.<span class="hljs-property">controller</span> = controller;
1055  }
1056
1057  <span class="hljs-keyword">async</span> <span class="hljs-title function_">pull</span>(<span class="hljs-params">controller</span>) {
1058    <span class="hljs-keyword">const</span> view = controller.<span class="hljs-property">byobRequest</span>?.<span class="hljs-property">view</span>;
1059    <span class="hljs-keyword">const</span> {
1060      bytesRead,
1061    } = <span class="hljs-keyword">await</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">file</span>.<span class="hljs-title function_">read</span>({
1062      <span class="hljs-attr">buffer</span>: view,
1063      <span class="hljs-attr">offset</span>: view.<span class="hljs-property">byteOffset</span>,
1064      <span class="hljs-attr">length</span>: view.<span class="hljs-property">byteLength</span>,
1065    });
1066
1067    <span class="hljs-keyword">if</span> (bytesRead === <span class="hljs-number">0</span>) {
1068      <span class="hljs-keyword">await</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">file</span>.<span class="hljs-title function_">close</span>();
1069      <span class="hljs-variable language_">this</span>.<span class="hljs-property">controller</span>.<span class="hljs-title function_">close</span>();
1070    }
1071    controller.<span class="hljs-property">byobRequest</span>.<span class="hljs-title function_">respond</span>(bytesRead);
1072  }
1073}
1074
1075<span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ReadableStream</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Source</span>());
1076
1077<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">read</span>(<span class="hljs-params">stream</span>) {
1078  <span class="hljs-keyword">const</span> reader = stream.<span class="hljs-title function_">getReader</span>({ <span class="hljs-attr">mode</span>: <span class="hljs-string">'byob'</span> });
1079
1080  <span class="hljs-keyword">const</span> chunks = [];
1081  <span class="hljs-keyword">let</span> result;
1082  <span class="hljs-keyword">do</span> {
1083    result = <span class="hljs-keyword">await</span> reader.<span class="hljs-title function_">read</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">100</span>));
1084    <span class="hljs-keyword">if</span> (result.<span class="hljs-property">value</span> !== <span class="hljs-literal">undefined</span>)
1085      chunks.<span class="hljs-title function_">push</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(result.<span class="hljs-property">value</span>));
1086  } <span class="hljs-keyword">while</span> (!result.<span class="hljs-property">done</span>);
1087
1088  <span class="hljs-keyword">return</span> <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">concat</span>(chunks);
1089}
1090
1091<span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> <span class="hljs-title function_">read</span>(stream);
1092<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(data).<span class="hljs-title function_">toString</span>());</code> <button class="copy-button">copy</button></pre>
1093<h5><code>new ReadableStreamBYOBReader(stream)</code><span><a class="mark" href="#new-readablestreambyobreaderstream" id="new-readablestreambyobreaderstream">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_new_readablestreambyobreader_stream"></a></h5>
1094<div class="api_metadata">
1095<span>Added in: v16.5.0</span>
1096</div>
1097<ul>
1098<li><code>stream</code> <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a></li>
1099</ul>
1100<p>Creates a new <code>ReadableStreamBYOBReader</code> that is locked to the
1101given <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a>.</p>
1102<h5><code>readableStreamBYOBReader.cancel([reason])</code><span><a class="mark" href="#readablestreambyobreadercancelreason" id="readablestreambyobreadercancelreason">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_readablestreambyobreader_cancel_reason"></a></h5>
1103<div class="api_metadata">
1104<span>Added in: v16.5.0</span>
1105</div>
1106<ul>
1107<li><code>reason</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
1108<li>Returns: A promise fulfilled with <code>undefined</code>.</li>
1109</ul>
1110<p>Cancels the <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> and returns a promise that is fulfilled
1111when the underlying stream has been canceled.</p>
1112<h5><code>readableStreamBYOBReader.closed</code><span><a class="mark" href="#readablestreambyobreaderclosed" id="readablestreambyobreaderclosed">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_readablestreambyobreader_closed"></a></h5>
1113<div class="api_metadata">
1114<span>Added in: v16.5.0</span>
1115</div>
1116<ul>
1117<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfilled with <code>undefined</code> when the associated
1118<a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> is closed or rejected if the stream errors or the reader's
1119lock is released before the stream finishes closing.</li>
1120</ul>
1121<h5><code>readableStreamBYOBReader.read(view)</code><span><a class="mark" href="#readablestreambyobreaderreadview" id="readablestreambyobreaderreadview">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_readablestreambyobreader_read_view"></a></h5>
1122<div class="api_metadata">
1123<span>Added in: v16.5.0</span>
1124</div>
1125<ul>
1126<li><code>view</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
1127<li>Returns: A promise fulfilled with an object:
1128<ul>
1129<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a></li>
1130<li><code>done</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1131</ul>
1132</li>
1133</ul>
1134<p>Requests the next chunk of data from the underlying <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a>
1135and returns a promise that is fulfilled with the data once it is
1136available.</p>
1137<p>Do not pass a pooled <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> object instance in to this method.
1138Pooled <code>Buffer</code> objects are created using <code>Buffer.allocUnsafe()</code>,
1139or <code>Buffer.from()</code>, or are often returned by various <code>node:fs</code> module
1140callbacks. These types of <code>Buffer</code>s use a shared underlying
1141<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a> object that contains all of the data from all of
1142the pooled <code>Buffer</code> instances. When a <code>Buffer</code>, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a>,
1143or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> is passed in to <code>readableStreamBYOBReader.read()</code>,
1144the view's underlying <code>ArrayBuffer</code> is <em>detached</em>, invalidating
1145all existing views that may exist on that <code>ArrayBuffer</code>. This
1146can have disastrous consequences for your application.</p>
1147<h5><code>readableStreamBYOBReader.releaseLock()</code><span><a class="mark" href="#readablestreambyobreaderreleaselock" id="readablestreambyobreaderreleaselock">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_readablestreambyobreader_releaselock"></a></h5>
1148<div class="api_metadata">
1149<span>Added in: v16.5.0</span>
1150</div>
1151<p>Releases this reader's lock on the underlying <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a>.</p>
1152<h4>Class: <code>ReadableStreamDefaultController</code><span><a class="mark" href="#class-readablestreamdefaultcontroller" id="class-readablestreamdefaultcontroller">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_class_readablestreamdefaultcontroller"></a></h4>
1153<div class="api_metadata">
1154<span>Added in: v16.5.0</span>
1155</div>
1156<p>Every <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> has a controller that is responsible for
1157the internal state and management of the stream's queue. The
1158<code>ReadableStreamDefaultController</code> is the default controller
1159implementation for <code>ReadableStream</code>s that are not byte-oriented.</p>
1160<h5><code>readableStreamDefaultController.close()</code><span><a class="mark" href="#readablestreamdefaultcontrollerclose" id="readablestreamdefaultcontrollerclose">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_readablestreamdefaultcontroller_close"></a></h5>
1161<div class="api_metadata">
1162<span>Added in: v16.5.0</span>
1163</div>
1164<p>Closes the <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> to which this controller is associated.</p>
1165<h5><code>readableStreamDefaultController.desiredSize</code><span><a class="mark" href="#readablestreamdefaultcontrollerdesiredsize" id="readablestreamdefaultcontrollerdesiredsize">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_readablestreamdefaultcontroller_desiredsize"></a></h5>
1166<div class="api_metadata">
1167<span>Added in: v16.5.0</span>
1168</div>
1169<ul>
1170<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1171</ul>
1172<p>Returns the amount of data remaining to fill the <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a>'s
1173queue.</p>
1174<h5><code>readableStreamDefaultController.enqueue([chunk])</code><span><a class="mark" href="#readablestreamdefaultcontrollerenqueuechunk" id="readablestreamdefaultcontrollerenqueuechunk">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_readablestreamdefaultcontroller_enqueue_chunk"></a></h5>
1175<div class="api_metadata">
1176<span>Added in: v16.5.0</span>
1177</div>
1178<ul>
1179<li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
1180</ul>
1181<p>Appends a new chunk of data to the <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a>'s queue.</p>
1182<h5><code>readableStreamDefaultController.error([error])</code><span><a class="mark" href="#readablestreamdefaultcontrollererrorerror" id="readablestreamdefaultcontrollererrorerror">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_readablestreamdefaultcontroller_error_error"></a></h5>
1183<div class="api_metadata">
1184<span>Added in: v16.5.0</span>
1185</div>
1186<ul>
1187<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
1188</ul>
1189<p>Signals an error that causes the <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> to error and close.</p>
1190<h4>Class: <code>ReadableByteStreamController</code><span><a class="mark" href="#class-readablebytestreamcontroller" id="class-readablebytestreamcontroller">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_class_readablebytestreamcontroller"></a></h4>
1191<div class="api_metadata">
1192<details class="changelog"><summary>History</summary>
1193<table>
1194<tbody><tr><th>Version</th><th>Changes</th></tr>
1195<tr><td>v18.10.0</td>
1196<td><p>Support handling a BYOB pull request from a released reader.</p></td></tr>
1197<tr><td>v16.5.0</td>
1198<td><p><span>Added in: v16.5.0</span></p></td></tr>
1199</tbody></table>
1200</details>
1201</div>
1202<p>Every <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> has a controller that is responsible for
1203the internal state and management of the stream's queue. The
1204<code>ReadableByteStreamController</code> is for byte-oriented <code>ReadableStream</code>s.</p>
1205<h5><code>readableByteStreamController.byobRequest</code><span><a class="mark" href="#readablebytestreamcontrollerbyobrequest" id="readablebytestreamcontrollerbyobrequest">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_readablebytestreamcontroller_byobrequest"></a></h5>
1206<div class="api_metadata">
1207<span>Added in: v16.5.0</span>
1208</div>
1209<ul>
1210<li>Type: <a href="webstreams.html#class-readablestreambyobrequest" class="type">&#x3C;ReadableStreamBYOBRequest></a></li>
1211</ul>
1212<h5><code>readableByteStreamController.close()</code><span><a class="mark" href="#readablebytestreamcontrollerclose" id="readablebytestreamcontrollerclose">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_readablebytestreamcontroller_close"></a></h5>
1213<div class="api_metadata">
1214<span>Added in: v16.5.0</span>
1215</div>
1216<p>Closes the <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> to which this controller is associated.</p>
1217<h5><code>readableByteStreamController.desiredSize</code><span><a class="mark" href="#readablebytestreamcontrollerdesiredsize" id="readablebytestreamcontrollerdesiredsize">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_readablebytestreamcontroller_desiredsize"></a></h5>
1218<div class="api_metadata">
1219<span>Added in: v16.5.0</span>
1220</div>
1221<ul>
1222<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1223</ul>
1224<p>Returns the amount of data remaining to fill the <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a>'s
1225queue.</p>
1226<h5><code>readableByteStreamController.enqueue(chunk)</code><span><a class="mark" href="#readablebytestreamcontrollerenqueuechunk" id="readablebytestreamcontrollerenqueuechunk">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_readablebytestreamcontroller_enqueue_chunk"></a></h5>
1227<div class="api_metadata">
1228<span>Added in: v16.5.0</span>
1229</div>
1230<ul>
1231<li><code>chunk</code>: <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
1232</ul>
1233<p>Appends a new chunk of data to the <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a>'s queue.</p>
1234<h5><code>readableByteStreamController.error([error])</code><span><a class="mark" href="#readablebytestreamcontrollererrorerror" id="readablebytestreamcontrollererrorerror">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_readablebytestreamcontroller_error_error"></a></h5>
1235<div class="api_metadata">
1236<span>Added in: v16.5.0</span>
1237</div>
1238<ul>
1239<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
1240</ul>
1241<p>Signals an error that causes the <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> to error and close.</p>
1242<h4>Class: <code>ReadableStreamBYOBRequest</code><span><a class="mark" href="#class-readablestreambyobrequest" id="class-readablestreambyobrequest">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_class_readablestreambyobrequest"></a></h4>
1243<div class="api_metadata">
1244<details class="changelog"><summary>History</summary>
1245<table>
1246<tbody><tr><th>Version</th><th>Changes</th></tr>
1247<tr><td>v18.0.0</td>
1248<td><p>This class is now exposed on the global object.</p></td></tr>
1249<tr><td>v16.5.0</td>
1250<td><p><span>Added in: v16.5.0</span></p></td></tr>
1251</tbody></table>
1252</details>
1253</div>
1254<p>When using <code>ReadableByteStreamController</code> in byte-oriented
1255streams, and when using the <code>ReadableStreamBYOBReader</code>,
1256the <code>readableByteStreamController.byobRequest</code> property
1257provides access to a <code>ReadableStreamBYOBRequest</code> instance
1258that represents the current read request. The object
1259is used to gain access to the <code>ArrayBuffer</code>/<code>TypedArray</code>
1260that has been provided for the read request to fill,
1261and provides methods for signaling that the data has
1262been provided.</p>
1263<h5><code>readableStreamBYOBRequest.respond(bytesWritten)</code><span><a class="mark" href="#readablestreambyobrequestrespondbyteswritten" id="readablestreambyobrequestrespondbyteswritten">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_readablestreambyobrequest_respond_byteswritten"></a></h5>
1264<div class="api_metadata">
1265<span>Added in: v16.5.0</span>
1266</div>
1267<ul>
1268<li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1269</ul>
1270<p>Signals that a <code>bytesWritten</code> number of bytes have been written
1271to <code>readableStreamBYOBRequest.view</code>.</p>
1272<h5><code>readableStreamBYOBRequest.respondWithNewView(view)</code><span><a class="mark" href="#readablestreambyobrequestrespondwithnewviewview" id="readablestreambyobrequestrespondwithnewviewview">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_readablestreambyobrequest_respondwithnewview_view"></a></h5>
1273<div class="api_metadata">
1274<span>Added in: v16.5.0</span>
1275</div>
1276<ul>
1277<li><code>view</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
1278</ul>
1279<p>Signals that the request has been fulfilled with bytes written
1280to a new <code>Buffer</code>, <code>TypedArray</code>, or <code>DataView</code>.</p>
1281<h5><code>readableStreamBYOBRequest.view</code><span><a class="mark" href="#readablestreambyobrequestview" id="readablestreambyobrequestview">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_readablestreambyobrequest_view"></a></h5>
1282<div class="api_metadata">
1283<span>Added in: v16.5.0</span>
1284</div>
1285<ul>
1286<li>Type: <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
1287</ul>
1288<h4>Class: <code>WritableStream</code><span><a class="mark" href="#class-writablestream" id="class-writablestream">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_class_writablestream"></a></h4>
1289<div class="api_metadata">
1290<details class="changelog"><summary>History</summary>
1291<table>
1292<tbody><tr><th>Version</th><th>Changes</th></tr>
1293<tr><td>v18.0.0</td>
1294<td><p>This class is now exposed on the global object.</p></td></tr>
1295<tr><td>v16.5.0</td>
1296<td><p><span>Added in: v16.5.0</span></p></td></tr>
1297</tbody></table>
1298</details>
1299</div>
1300<p>The <code>WritableStream</code> is a destination to which stream data is sent.</p>
1301<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> {
1302  <span class="hljs-title class_">WritableStream</span>,
1303} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:stream/web'</span>;
1304
1305<span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WritableStream</span>({
1306  <span class="hljs-title function_">write</span>(<span class="hljs-params">chunk</span>) {
1307    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(chunk);
1308  },
1309});
1310
1311<span class="hljs-keyword">await</span> stream.<span class="hljs-title function_">getWriter</span>().<span class="hljs-title function_">write</span>(<span class="hljs-string">'Hello World'</span>);</code> <button class="copy-button">copy</button></pre>
1312<h5><code>new WritableStream([underlyingSink[, strategy]])</code><span><a class="mark" href="#new-writablestreamunderlyingsink-strategy" id="new-writablestreamunderlyingsink-strategy">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_new_writablestream_underlyingsink_strategy"></a></h5>
1313<div class="api_metadata">
1314<span>Added in: v16.5.0</span>
1315</div>
1316<ul>
1317<li><code>underlyingSink</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
1318<ul>
1319<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> A user-defined function that is invoked immediately when
1320the <code>WritableStream</code> is created.
1321<ul>
1322<li><code>controller</code> <a href="webstreams.html#class-writablestreamdefaultcontroller" class="type">&#x3C;WritableStreamDefaultController></a></li>
1323<li>Returns: <code>undefined</code> or a promise fulfilled with <code>undefined</code>.</li>
1324</ul>
1325</li>
1326<li><code>write</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> A user-defined function that is invoked when a chunk of
1327data has been written to the <code>WritableStream</code>.
1328<ul>
1329<li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
1330<li><code>controller</code> <a href="webstreams.html#class-writablestreamdefaultcontroller" class="type">&#x3C;WritableStreamDefaultController></a></li>
1331<li>Returns: A promise fulfilled with <code>undefined</code>.</li>
1332</ul>
1333</li>
1334<li><code>close</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> A user-defined function that is called when the
1335<code>WritableStream</code> is closed.
1336<ul>
1337<li>Returns: A promise fulfilled with <code>undefined</code>.</li>
1338</ul>
1339</li>
1340<li><code>abort</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> A user-defined function that is called to abruptly close
1341the <code>WritableStream</code>.
1342<ul>
1343<li><code>reason</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
1344<li>Returns: A promise fulfilled with <code>undefined</code>.</li>
1345</ul>
1346</li>
1347<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> The <code>type</code> option is reserved for future use and <em>must</em> be
1348undefined.</li>
1349</ul>
1350</li>
1351<li><code>strategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
1352<ul>
1353<li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The maximum internal queue size before backpressure
1354is applied.</li>
1355<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> A user-defined function used to identify the size of each
1356chunk of data.
1357<ul>
1358<li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
1359<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1360</ul>
1361</li>
1362</ul>
1363</li>
1364</ul>
1365<h5><code>writableStream.abort([reason])</code><span><a class="mark" href="#writablestreamabortreason" id="writablestreamabortreason">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_writablestream_abort_reason"></a></h5>
1366<div class="api_metadata">
1367<span>Added in: v16.5.0</span>
1368</div>
1369<ul>
1370<li><code>reason</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
1371<li>Returns: A promise fulfilled with <code>undefined</code>.</li>
1372</ul>
1373<p>Abruptly terminates the <code>WritableStream</code>. All queued writes will be
1374canceled with their associated promises rejected.</p>
1375<h5><code>writableStream.close()</code><span><a class="mark" href="#writablestreamclose" id="writablestreamclose">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_writablestream_close"></a></h5>
1376<div class="api_metadata">
1377<span>Added in: v16.5.0</span>
1378</div>
1379<ul>
1380<li>Returns: A promise fulfilled with <code>undefined</code>.</li>
1381</ul>
1382<p>Closes the <code>WritableStream</code> when no additional writes are expected.</p>
1383<h5><code>writableStream.getWriter()</code><span><a class="mark" href="#writablestreamgetwriter" id="writablestreamgetwriter">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_writablestream_getwriter"></a></h5>
1384<div class="api_metadata">
1385<span>Added in: v16.5.0</span>
1386</div>
1387<ul>
1388<li>Returns: <a href="webstreams.html#class-writablestreamdefaultwriter" class="type">&#x3C;WritableStreamDefaultWriter></a></li>
1389</ul>
1390<p>Creates and creates a new writer instance that can be used to write
1391data into the <code>WritableStream</code>.</p>
1392<h5><code>writableStream.locked</code><span><a class="mark" href="#writablestreamlocked" id="writablestreamlocked">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_writablestream_locked"></a></h5>
1393<div class="api_metadata">
1394<span>Added in: v16.5.0</span>
1395</div>
1396<ul>
1397<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1398</ul>
1399<p>The <code>writableStream.locked</code> property is <code>false</code> by default, and is
1400switched to <code>true</code> while there is an active writer attached to this
1401<code>WritableStream</code>.</p>
1402<h5>Transferring with postMessage()<span><a class="mark" href="#transferring-with-postmessage_1" id="transferring-with-postmessage_1">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_transferring_with_postmessage_1"></a></h5>
1403<p>A <a href="webstreams.html#class-writablestream" class="type">&#x3C;WritableStream></a> instance can be transferred using a <a href="worker_threads.html#class-messageport" class="type">&#x3C;MessagePort></a>.</p>
1404<pre><code class="language-js"><span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WritableStream</span>(<span class="hljs-title function_">getWritableSinkSomehow</span>());
1405
1406<span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>();
1407
1408port1.<span class="hljs-property">onmessage</span> = <span class="hljs-function">(<span class="hljs-params">{ data }</span>) =></span> {
1409  data.<span class="hljs-title function_">getWriter</span>().<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello'</span>);
1410};
1411
1412port2.<span class="hljs-title function_">postMessage</span>(stream, [stream]);</code> <button class="copy-button">copy</button></pre>
1413<h4>Class: <code>WritableStreamDefaultWriter</code><span><a class="mark" href="#class-writablestreamdefaultwriter" id="class-writablestreamdefaultwriter">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_class_writablestreamdefaultwriter"></a></h4>
1414<div class="api_metadata">
1415<details class="changelog"><summary>History</summary>
1416<table>
1417<tbody><tr><th>Version</th><th>Changes</th></tr>
1418<tr><td>v18.0.0</td>
1419<td><p>This class is now exposed on the global object.</p></td></tr>
1420<tr><td>v16.5.0</td>
1421<td><p><span>Added in: v16.5.0</span></p></td></tr>
1422</tbody></table>
1423</details>
1424</div>
1425<h5><code>new WritableStreamDefaultWriter(stream)</code><span><a class="mark" href="#new-writablestreamdefaultwriterstream" id="new-writablestreamdefaultwriterstream">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_new_writablestreamdefaultwriter_stream"></a></h5>
1426<div class="api_metadata">
1427<span>Added in: v16.5.0</span>
1428</div>
1429<ul>
1430<li><code>stream</code> <a href="webstreams.html#class-writablestream" class="type">&#x3C;WritableStream></a></li>
1431</ul>
1432<p>Creates a new <code>WritableStreamDefaultWriter</code> that is locked to the given
1433<code>WritableStream</code>.</p>
1434<h5><code>writableStreamDefaultWriter.abort([reason])</code><span><a class="mark" href="#writablestreamdefaultwriterabortreason" id="writablestreamdefaultwriterabortreason">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_writablestreamdefaultwriter_abort_reason"></a></h5>
1435<div class="api_metadata">
1436<span>Added in: v16.5.0</span>
1437</div>
1438<ul>
1439<li><code>reason</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
1440<li>Returns: A promise fulfilled with <code>undefined</code>.</li>
1441</ul>
1442<p>Abruptly terminates the <code>WritableStream</code>. All queued writes will be
1443canceled with their associated promises rejected.</p>
1444<h5><code>writableStreamDefaultWriter.close()</code><span><a class="mark" href="#writablestreamdefaultwriterclose" id="writablestreamdefaultwriterclose">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_writablestreamdefaultwriter_close"></a></h5>
1445<div class="api_metadata">
1446<span>Added in: v16.5.0</span>
1447</div>
1448<ul>
1449<li>Returns: A promise fulfilled with <code>undefined</code>.</li>
1450</ul>
1451<p>Closes the <code>WritableStream</code> when no additional writes are expected.</p>
1452<h5><code>writableStreamDefaultWriter.closed</code><span><a class="mark" href="#writablestreamdefaultwriterclosed" id="writablestreamdefaultwriterclosed">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_writablestreamdefaultwriter_closed"></a></h5>
1453<div class="api_metadata">
1454<span>Added in: v16.5.0</span>
1455</div>
1456<ul>
1457<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfilled with <code>undefined</code> when the associated
1458<a href="webstreams.html#class-writablestream" class="type">&#x3C;WritableStream></a> is closed or rejected if the stream errors or the writer's
1459lock is released before the stream finishes closing.</li>
1460</ul>
1461<h5><code>writableStreamDefaultWriter.desiredSize</code><span><a class="mark" href="#writablestreamdefaultwriterdesiredsize" id="writablestreamdefaultwriterdesiredsize">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_writablestreamdefaultwriter_desiredsize"></a></h5>
1462<div class="api_metadata">
1463<span>Added in: v16.5.0</span>
1464</div>
1465<ul>
1466<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1467</ul>
1468<p>The amount of data required to fill the <a href="webstreams.html#class-writablestream" class="type">&#x3C;WritableStream></a>'s queue.</p>
1469<h5><code>writableStreamDefaultWriter.ready</code><span><a class="mark" href="#writablestreamdefaultwriterready" id="writablestreamdefaultwriterready">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_writablestreamdefaultwriter_ready"></a></h5>
1470<div class="api_metadata">
1471<span>Added in: v16.5.0</span>
1472</div>
1473<ul>
1474<li>type: A promise that is fulfilled with <code>undefined</code> when the
1475writer is ready to be used.</li>
1476</ul>
1477<h5><code>writableStreamDefaultWriter.releaseLock()</code><span><a class="mark" href="#writablestreamdefaultwriterreleaselock" id="writablestreamdefaultwriterreleaselock">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_writablestreamdefaultwriter_releaselock"></a></h5>
1478<div class="api_metadata">
1479<span>Added in: v16.5.0</span>
1480</div>
1481<p>Releases this writer's lock on the underlying <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a>.</p>
1482<h5><code>writableStreamDefaultWriter.write([chunk])</code><span><a class="mark" href="#writablestreamdefaultwriterwritechunk" id="writablestreamdefaultwriterwritechunk">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_writablestreamdefaultwriter_write_chunk"></a></h5>
1483<div class="api_metadata">
1484<span>Added in: v16.5.0</span>
1485</div>
1486<ul>
1487<li><code>chunk</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
1488<li>Returns: A promise fulfilled with <code>undefined</code>.</li>
1489</ul>
1490<p>Appends a new chunk of data to the <a href="webstreams.html#class-writablestream" class="type">&#x3C;WritableStream></a>'s queue.</p>
1491<h4>Class: <code>WritableStreamDefaultController</code><span><a class="mark" href="#class-writablestreamdefaultcontroller" id="class-writablestreamdefaultcontroller">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_class_writablestreamdefaultcontroller"></a></h4>
1492<div class="api_metadata">
1493<details class="changelog"><summary>History</summary>
1494<table>
1495<tbody><tr><th>Version</th><th>Changes</th></tr>
1496<tr><td>v18.0.0</td>
1497<td><p>This class is now exposed on the global object.</p></td></tr>
1498<tr><td>v16.5.0</td>
1499<td><p><span>Added in: v16.5.0</span></p></td></tr>
1500</tbody></table>
1501</details>
1502</div>
1503<p>The <code>WritableStreamDefaultController</code> manage's the <a href="webstreams.html#class-writablestream" class="type">&#x3C;WritableStream></a>'s
1504internal state.</p>
1505<h5><code>writableStreamDefaultController.error([error])</code><span><a class="mark" href="#writablestreamdefaultcontrollererrorerror" id="writablestreamdefaultcontrollererrorerror">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_writablestreamdefaultcontroller_error_error"></a></h5>
1506<div class="api_metadata">
1507<span>Added in: v16.5.0</span>
1508</div>
1509<ul>
1510<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
1511</ul>
1512<p>Called by user-code to signal that an error has occurred while processing
1513the <code>WritableStream</code> data. When called, the <a href="webstreams.html#class-writablestream" class="type">&#x3C;WritableStream></a> will be aborted,
1514with currently pending writes canceled.</p>
1515<h5><code>writableStreamDefaultController.signal</code><span><a class="mark" href="#writablestreamdefaultcontrollersignal" id="writablestreamdefaultcontrollersignal">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_writablestreamdefaultcontroller_signal"></a></h5>
1516<ul>
1517<li>Type: <a href="globals.html#class-abortsignal" class="type">&#x3C;AbortSignal></a> An <code>AbortSignal</code> that can be used to cancel pending
1518write or close operations when a <a href="webstreams.html#class-writablestream" class="type">&#x3C;WritableStream></a> is aborted.</li>
1519</ul>
1520<h4>Class: <code>TransformStream</code><span><a class="mark" href="#class-transformstream" id="class-transformstream">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_class_transformstream"></a></h4>
1521<div class="api_metadata">
1522<details class="changelog"><summary>History</summary>
1523<table>
1524<tbody><tr><th>Version</th><th>Changes</th></tr>
1525<tr><td>v18.0.0</td>
1526<td><p>This class is now exposed on the global object.</p></td></tr>
1527<tr><td>v16.5.0</td>
1528<td><p><span>Added in: v16.5.0</span></p></td></tr>
1529</tbody></table>
1530</details>
1531</div>
1532<p>A <code>TransformStream</code> consists of a <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> and a <a href="webstreams.html#class-writablestream" class="type">&#x3C;WritableStream></a> that
1533are connected such that the data written to the <code>WritableStream</code> is received,
1534and potentially transformed, before being pushed into the <code>ReadableStream</code>'s
1535queue.</p>
1536<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> {
1537  <span class="hljs-title class_">TransformStream</span>,
1538} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:stream/web'</span>;
1539
1540<span class="hljs-keyword">const</span> transform = <span class="hljs-keyword">new</span> <span class="hljs-title class_">TransformStream</span>({
1541  <span class="hljs-title function_">transform</span>(<span class="hljs-params">chunk, controller</span>) {
1542    controller.<span class="hljs-title function_">enqueue</span>(chunk.<span class="hljs-title function_">toUpperCase</span>());
1543  },
1544});
1545
1546<span class="hljs-keyword">await</span> <span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">all</span>([
1547  transform.<span class="hljs-property">writable</span>.<span class="hljs-title function_">getWriter</span>().<span class="hljs-title function_">write</span>(<span class="hljs-string">'A'</span>),
1548  transform.<span class="hljs-property">readable</span>.<span class="hljs-title function_">getReader</span>().<span class="hljs-title function_">read</span>(),
1549]);</code> <button class="copy-button">copy</button></pre>
1550<h5><code>new TransformStream([transformer[, writableStrategy[, readableStrategy]]])</code><span><a class="mark" href="#new-transformstreamtransformer-writablestrategy-readablestrategy" id="new-transformstreamtransformer-writablestrategy-readablestrategy">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_new_transformstream_transformer_writablestrategy_readablestrategy"></a></h5>
1551<div class="api_metadata">
1552<span>Added in: v16.5.0</span>
1553</div>
1554<ul>
1555<li><code>transformer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
1556<ul>
1557<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> A user-defined function that is invoked immediately when
1558the <code>TransformStream</code> is created.
1559<ul>
1560<li><code>controller</code> <a href="webstreams.html#class-transformstreamdefaultcontroller" class="type">&#x3C;TransformStreamDefaultController></a></li>
1561<li>Returns: <code>undefined</code> or a promise fulfilled with <code>undefined</code></li>
1562</ul>
1563</li>
1564<li><code>transform</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> A user-defined function that receives, and
1565potentially modifies, a chunk of data written to <code>transformStream.writable</code>,
1566before forwarding that on to <code>transformStream.readable</code>.
1567<ul>
1568<li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
1569<li><code>controller</code> <a href="webstreams.html#class-transformstreamdefaultcontroller" class="type">&#x3C;TransformStreamDefaultController></a></li>
1570<li>Returns: A promise fulfilled with <code>undefined</code>.</li>
1571</ul>
1572</li>
1573<li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> A user-defined function that is called immediately before
1574the writable side of the <code>TransformStream</code> is closed, signaling the end of
1575the transformation process.
1576<ul>
1577<li><code>controller</code> <a href="webstreams.html#class-transformstreamdefaultcontroller" class="type">&#x3C;TransformStreamDefaultController></a></li>
1578<li>Returns: A promise fulfilled with <code>undefined</code>.</li>
1579</ul>
1580</li>
1581<li><code>readableType</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> the <code>readableType</code> option is reserved for future use
1582and <em>must</em> be <code>undefined</code>.</li>
1583<li><code>writableType</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> the <code>writableType</code> option is reserved for future use
1584and <em>must</em> be <code>undefined</code>.</li>
1585</ul>
1586</li>
1587<li><code>writableStrategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
1588<ul>
1589<li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The maximum internal queue size before backpressure
1590is applied.</li>
1591<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> A user-defined function used to identify the size of each
1592chunk of data.
1593<ul>
1594<li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
1595<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1596</ul>
1597</li>
1598</ul>
1599</li>
1600<li><code>readableStrategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
1601<ul>
1602<li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The maximum internal queue size before backpressure
1603is applied.</li>
1604<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> A user-defined function used to identify the size of each
1605chunk of data.
1606<ul>
1607<li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
1608<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1609</ul>
1610</li>
1611</ul>
1612</li>
1613</ul>
1614<h5><code>transformStream.readable</code><span><a class="mark" href="#transformstreamreadable" id="transformstreamreadable">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_transformstream_readable"></a></h5>
1615<div class="api_metadata">
1616<span>Added in: v16.5.0</span>
1617</div>
1618<ul>
1619<li>Type: <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a></li>
1620</ul>
1621<h5><code>transformStream.writable</code><span><a class="mark" href="#transformstreamwritable" id="transformstreamwritable">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_transformstream_writable"></a></h5>
1622<div class="api_metadata">
1623<span>Added in: v16.5.0</span>
1624</div>
1625<ul>
1626<li>Type: <a href="webstreams.html#class-writablestream" class="type">&#x3C;WritableStream></a></li>
1627</ul>
1628<h5>Transferring with postMessage()<span><a class="mark" href="#transferring-with-postmessage_2" id="transferring-with-postmessage_2">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_transferring_with_postmessage_2"></a></h5>
1629<p>A <a href="webstreams.html#class-transformstream" class="type">&#x3C;TransformStream></a> instance can be transferred using a <a href="worker_threads.html#class-messageport" class="type">&#x3C;MessagePort></a>.</p>
1630<pre><code class="language-js"><span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class="hljs-title class_">TransformStream</span>();
1631
1632<span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>();
1633
1634port1.<span class="hljs-property">onmessage</span> = <span class="hljs-function">(<span class="hljs-params">{ data }</span>) =></span> {
1635  <span class="hljs-keyword">const</span> { writable, readable } = data;
1636  <span class="hljs-comment">// ...</span>
1637};
1638
1639port2.<span class="hljs-title function_">postMessage</span>(stream, [stream]);</code> <button class="copy-button">copy</button></pre>
1640<h4>Class: <code>TransformStreamDefaultController</code><span><a class="mark" href="#class-transformstreamdefaultcontroller" id="class-transformstreamdefaultcontroller">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_class_transformstreamdefaultcontroller"></a></h4>
1641<div class="api_metadata">
1642<details class="changelog"><summary>History</summary>
1643<table>
1644<tbody><tr><th>Version</th><th>Changes</th></tr>
1645<tr><td>v18.0.0</td>
1646<td><p>This class is now exposed on the global object.</p></td></tr>
1647<tr><td>v16.5.0</td>
1648<td><p><span>Added in: v16.5.0</span></p></td></tr>
1649</tbody></table>
1650</details>
1651</div>
1652<p>The <code>TransformStreamDefaultController</code> manages the internal state
1653of the <code>TransformStream</code>.</p>
1654<h5><code>transformStreamDefaultController.desiredSize</code><span><a class="mark" href="#transformstreamdefaultcontrollerdesiredsize" id="transformstreamdefaultcontrollerdesiredsize">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_transformstreamdefaultcontroller_desiredsize"></a></h5>
1655<div class="api_metadata">
1656<span>Added in: v16.5.0</span>
1657</div>
1658<ul>
1659<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1660</ul>
1661<p>The amount of data required to fill the readable side's queue.</p>
1662<h5><code>transformStreamDefaultController.enqueue([chunk])</code><span><a class="mark" href="#transformstreamdefaultcontrollerenqueuechunk" id="transformstreamdefaultcontrollerenqueuechunk">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_transformstreamdefaultcontroller_enqueue_chunk"></a></h5>
1663<div class="api_metadata">
1664<span>Added in: v16.5.0</span>
1665</div>
1666<ul>
1667<li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
1668</ul>
1669<p>Appends a chunk of data to the readable side's queue.</p>
1670<h5><code>transformStreamDefaultController.error([reason])</code><span><a class="mark" href="#transformstreamdefaultcontrollererrorreason" id="transformstreamdefaultcontrollererrorreason">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_transformstreamdefaultcontroller_error_reason"></a></h5>
1671<div class="api_metadata">
1672<span>Added in: v16.5.0</span>
1673</div>
1674<ul>
1675<li><code>reason</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
1676</ul>
1677<p>Signals to both the readable and writable side that an error has occurred
1678while processing the transform data, causing both sides to be abruptly
1679closed.</p>
1680<h5><code>transformStreamDefaultController.terminate()</code><span><a class="mark" href="#transformstreamdefaultcontrollerterminate" id="transformstreamdefaultcontrollerterminate">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_transformstreamdefaultcontroller_terminate"></a></h5>
1681<div class="api_metadata">
1682<span>Added in: v16.5.0</span>
1683</div>
1684<p>Closes the readable side of the transport and causes the writable side
1685to be abruptly closed with an error.</p>
1686<h4>Class: <code>ByteLengthQueuingStrategy</code><span><a class="mark" href="#class-bytelengthqueuingstrategy" id="class-bytelengthqueuingstrategy">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_class_bytelengthqueuingstrategy"></a></h4>
1687<div class="api_metadata">
1688<details class="changelog"><summary>History</summary>
1689<table>
1690<tbody><tr><th>Version</th><th>Changes</th></tr>
1691<tr><td>v18.0.0</td>
1692<td><p>This class is now exposed on the global object.</p></td></tr>
1693<tr><td>v16.5.0</td>
1694<td><p><span>Added in: v16.5.0</span></p></td></tr>
1695</tbody></table>
1696</details>
1697</div>
1698<h5><code>new ByteLengthQueuingStrategy(options)</code><span><a class="mark" href="#new-bytelengthqueuingstrategyoptions" id="new-bytelengthqueuingstrategyoptions">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_new_bytelengthqueuingstrategy_options"></a></h5>
1699<div class="api_metadata">
1700<span>Added in: v16.5.0</span>
1701</div>
1702<ul>
1703<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
1704<ul>
1705<li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1706</ul>
1707</li>
1708</ul>
1709<h5><code>byteLengthQueuingStrategy.highWaterMark</code><span><a class="mark" href="#bytelengthqueuingstrategyhighwatermark" id="bytelengthqueuingstrategyhighwatermark">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_bytelengthqueuingstrategy_highwatermark"></a></h5>
1710<div class="api_metadata">
1711<span>Added in: v16.5.0</span>
1712</div>
1713<ul>
1714<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1715</ul>
1716<h5><code>byteLengthQueuingStrategy.size</code><span><a class="mark" href="#bytelengthqueuingstrategysize" id="bytelengthqueuingstrategysize">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_bytelengthqueuingstrategy_size"></a></h5>
1717<div class="api_metadata">
1718<span>Added in: v16.5.0</span>
1719</div>
1720<ul>
1721<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
1722<ul>
1723<li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
1724<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1725</ul>
1726</li>
1727</ul>
1728<h4>Class: <code>CountQueuingStrategy</code><span><a class="mark" href="#class-countqueuingstrategy" id="class-countqueuingstrategy">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_class_countqueuingstrategy"></a></h4>
1729<div class="api_metadata">
1730<details class="changelog"><summary>History</summary>
1731<table>
1732<tbody><tr><th>Version</th><th>Changes</th></tr>
1733<tr><td>v18.0.0</td>
1734<td><p>This class is now exposed on the global object.</p></td></tr>
1735<tr><td>v16.5.0</td>
1736<td><p><span>Added in: v16.5.0</span></p></td></tr>
1737</tbody></table>
1738</details>
1739</div>
1740<h5><code>new CountQueuingStrategy(options)</code><span><a class="mark" href="#new-countqueuingstrategyoptions" id="new-countqueuingstrategyoptions">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_new_countqueuingstrategy_options"></a></h5>
1741<div class="api_metadata">
1742<span>Added in: v16.5.0</span>
1743</div>
1744<ul>
1745<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
1746<ul>
1747<li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1748</ul>
1749</li>
1750</ul>
1751<h5><code>countQueuingStrategy.highWaterMark</code><span><a class="mark" href="#countqueuingstrategyhighwatermark" id="countqueuingstrategyhighwatermark">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_countqueuingstrategy_highwatermark"></a></h5>
1752<div class="api_metadata">
1753<span>Added in: v16.5.0</span>
1754</div>
1755<ul>
1756<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1757</ul>
1758<h5><code>countQueuingStrategy.size</code><span><a class="mark" href="#countqueuingstrategysize" id="countqueuingstrategysize">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_countqueuingstrategy_size"></a></h5>
1759<div class="api_metadata">
1760<span>Added in: v16.5.0</span>
1761</div>
1762<ul>
1763<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
1764<ul>
1765<li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
1766<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1767</ul>
1768</li>
1769</ul>
1770<h4>Class: <code>TextEncoderStream</code><span><a class="mark" href="#class-textencoderstream" id="class-textencoderstream">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_class_textencoderstream"></a></h4>
1771<div class="api_metadata">
1772<details class="changelog"><summary>History</summary>
1773<table>
1774<tbody><tr><th>Version</th><th>Changes</th></tr>
1775<tr><td>v18.0.0</td>
1776<td><p>This class is now exposed on the global object.</p></td></tr>
1777<tr><td>v16.6.0</td>
1778<td><p><span>Added in: v16.6.0</span></p></td></tr>
1779</tbody></table>
1780</details>
1781</div>
1782<h5><code>new TextEncoderStream()</code><span><a class="mark" href="#new-textencoderstream" id="new-textencoderstream">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_new_textencoderstream"></a></h5>
1783<div class="api_metadata">
1784<span>Added in: v16.6.0</span>
1785</div>
1786<p>Creates a new <code>TextEncoderStream</code> instance.</p>
1787<h5><code>textEncoderStream.encoding</code><span><a class="mark" href="#textencoderstreamencoding" id="textencoderstreamencoding">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_textencoderstream_encoding"></a></h5>
1788<div class="api_metadata">
1789<span>Added in: v16.6.0</span>
1790</div>
1791<ul>
1792<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1793</ul>
1794<p>The encoding supported by the <code>TextEncoderStream</code> instance.</p>
1795<h5><code>textEncoderStream.readable</code><span><a class="mark" href="#textencoderstreamreadable" id="textencoderstreamreadable">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_textencoderstream_readable"></a></h5>
1796<div class="api_metadata">
1797<span>Added in: v16.6.0</span>
1798</div>
1799<ul>
1800<li>Type: <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a></li>
1801</ul>
1802<h5><code>textEncoderStream.writable</code><span><a class="mark" href="#textencoderstreamwritable" id="textencoderstreamwritable">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_textencoderstream_writable"></a></h5>
1803<div class="api_metadata">
1804<span>Added in: v16.6.0</span>
1805</div>
1806<ul>
1807<li>Type: <a href="webstreams.html#class-writablestream" class="type">&#x3C;WritableStream></a></li>
1808</ul>
1809<h4>Class: <code>TextDecoderStream</code><span><a class="mark" href="#class-textdecoderstream" id="class-textdecoderstream">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_class_textdecoderstream"></a></h4>
1810<div class="api_metadata">
1811<details class="changelog"><summary>History</summary>
1812<table>
1813<tbody><tr><th>Version</th><th>Changes</th></tr>
1814<tr><td>v18.0.0</td>
1815<td><p>This class is now exposed on the global object.</p></td></tr>
1816<tr><td>v16.6.0</td>
1817<td><p><span>Added in: v16.6.0</span></p></td></tr>
1818</tbody></table>
1819</details>
1820</div>
1821<h5><code>new TextDecoderStream([encoding[, options]])</code><span><a class="mark" href="#new-textdecoderstreamencoding-options" id="new-textdecoderstreamencoding-options">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_new_textdecoderstream_encoding_options"></a></h5>
1822<div class="api_metadata">
1823<span>Added in: v16.6.0</span>
1824</div>
1825<ul>
1826<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Identifies the <code>encoding</code> that this <code>TextDecoder</code> instance
1827supports. <strong>Default:</strong> <code>'utf-8'</code>.</li>
1828<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
1829<ul>
1830<li><code>fatal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <code>true</code> if decoding failures are fatal.</li>
1831<li><code>ignoreBOM</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, the <code>TextDecoderStream</code> will include the
1832byte order mark in the decoded result. When <code>false</code>, the byte order mark
1833will be removed from the output. This option is only used when <code>encoding</code> is
1834<code>'utf-8'</code>, <code>'utf-16be'</code>, or <code>'utf-16le'</code>. <strong>Default:</strong> <code>false</code>.</li>
1835</ul>
1836</li>
1837</ul>
1838<p>Creates a new <code>TextDecoderStream</code> instance.</p>
1839<h5><code>textDecoderStream.encoding</code><span><a class="mark" href="#textdecoderstreamencoding" id="textdecoderstreamencoding">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_textdecoderstream_encoding"></a></h5>
1840<div class="api_metadata">
1841<span>Added in: v16.6.0</span>
1842</div>
1843<ul>
1844<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1845</ul>
1846<p>The encoding supported by the <code>TextDecoderStream</code> instance.</p>
1847<h5><code>textDecoderStream.fatal</code><span><a class="mark" href="#textdecoderstreamfatal" id="textdecoderstreamfatal">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_textdecoderstream_fatal"></a></h5>
1848<div class="api_metadata">
1849<span>Added in: v16.6.0</span>
1850</div>
1851<ul>
1852<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1853</ul>
1854<p>The value will be <code>true</code> if decoding errors result in a <code>TypeError</code> being
1855thrown.</p>
1856<h5><code>textDecoderStream.ignoreBOM</code><span><a class="mark" href="#textdecoderstreamignorebom" id="textdecoderstreamignorebom">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_textdecoderstream_ignorebom"></a></h5>
1857<div class="api_metadata">
1858<span>Added in: v16.6.0</span>
1859</div>
1860<ul>
1861<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1862</ul>
1863<p>The value will be <code>true</code> if the decoding result will include the byte order
1864mark.</p>
1865<h5><code>textDecoderStream.readable</code><span><a class="mark" href="#textdecoderstreamreadable" id="textdecoderstreamreadable">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_textdecoderstream_readable"></a></h5>
1866<div class="api_metadata">
1867<span>Added in: v16.6.0</span>
1868</div>
1869<ul>
1870<li>Type: <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a></li>
1871</ul>
1872<h5><code>textDecoderStream.writable</code><span><a class="mark" href="#textdecoderstreamwritable" id="textdecoderstreamwritable">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_textdecoderstream_writable"></a></h5>
1873<div class="api_metadata">
1874<span>Added in: v16.6.0</span>
1875</div>
1876<ul>
1877<li>Type: <a href="webstreams.html#class-writablestream" class="type">&#x3C;WritableStream></a></li>
1878</ul>
1879<h4>Class: <code>CompressionStream</code><span><a class="mark" href="#class-compressionstream" id="class-compressionstream">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_class_compressionstream"></a></h4>
1880<div class="api_metadata">
1881<details class="changelog"><summary>History</summary>
1882<table>
1883<tbody><tr><th>Version</th><th>Changes</th></tr>
1884<tr><td>v18.0.0</td>
1885<td><p>This class is now exposed on the global object.</p></td></tr>
1886<tr><td>v17.0.0</td>
1887<td><p><span>Added in: v17.0.0</span></p></td></tr>
1888</tbody></table>
1889</details>
1890</div>
1891<h5><code>new CompressionStream(format)</code><span><a class="mark" href="#new-compressionstreamformat" id="new-compressionstreamformat">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_new_compressionstream_format"></a></h5>
1892<div class="api_metadata">
1893<span>Added in: v17.0.0</span>
1894</div>
1895<ul>
1896<li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> One of either <code>'deflate'</code> or <code>'gzip'</code>.</li>
1897</ul>
1898<h5><code>compressionStream.readable</code><span><a class="mark" href="#compressionstreamreadable" id="compressionstreamreadable">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_compressionstream_readable"></a></h5>
1899<div class="api_metadata">
1900<span>Added in: v17.0.0</span>
1901</div>
1902<ul>
1903<li>Type: <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a></li>
1904</ul>
1905<h5><code>compressionStream.writable</code><span><a class="mark" href="#compressionstreamwritable" id="compressionstreamwritable">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_compressionstream_writable"></a></h5>
1906<div class="api_metadata">
1907<span>Added in: v17.0.0</span>
1908</div>
1909<ul>
1910<li>Type: <a href="webstreams.html#class-writablestream" class="type">&#x3C;WritableStream></a></li>
1911</ul>
1912<h4>Class: <code>DecompressionStream</code><span><a class="mark" href="#class-decompressionstream" id="class-decompressionstream">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_class_decompressionstream"></a></h4>
1913<div class="api_metadata">
1914<details class="changelog"><summary>History</summary>
1915<table>
1916<tbody><tr><th>Version</th><th>Changes</th></tr>
1917<tr><td>v18.0.0</td>
1918<td><p>This class is now exposed on the global object.</p></td></tr>
1919<tr><td>v17.0.0</td>
1920<td><p><span>Added in: v17.0.0</span></p></td></tr>
1921</tbody></table>
1922</details>
1923</div>
1924<h5><code>new DecompressionStream(format)</code><span><a class="mark" href="#new-decompressionstreamformat" id="new-decompressionstreamformat">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_new_decompressionstream_format"></a></h5>
1925<div class="api_metadata">
1926<span>Added in: v17.0.0</span>
1927</div>
1928<ul>
1929<li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> One of either <code>'deflate'</code> or <code>'gzip'</code>.</li>
1930</ul>
1931<h5><code>decompressionStream.readable</code><span><a class="mark" href="#decompressionstreamreadable" id="decompressionstreamreadable">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_decompressionstream_readable"></a></h5>
1932<div class="api_metadata">
1933<span>Added in: v17.0.0</span>
1934</div>
1935<ul>
1936<li>Type: <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a></li>
1937</ul>
1938<h5><code>decompressionStream.writable</code><span><a class="mark" href="#decompressionstreamwritable" id="decompressionstreamwritable">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_decompressionstream_writable"></a></h5>
1939<div class="api_metadata">
1940<span>Added in: v17.0.0</span>
1941</div>
1942<ul>
1943<li>Type: <a href="webstreams.html#class-writablestream" class="type">&#x3C;WritableStream></a></li>
1944</ul>
1945<h4>Utility Consumers<span><a class="mark" href="#utility-consumers" id="utility-consumers">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_utility_consumers"></a></h4>
1946<div class="api_metadata">
1947<span>Added in: v16.7.0</span>
1948</div>
1949<p>The utility consumer functions provide common options for consuming
1950streams.</p>
1951<p>They are accessed using:</p>
1952
1953<pre class="with-14-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> {
1954  arrayBuffer,
1955  blob,
1956  buffer,
1957  json,
1958  text,
1959} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:stream/consumers'</span>;</code><code class="language-js cjs"><span class="hljs-keyword">const</span> {
1960  arrayBuffer,
1961  blob,
1962  buffer,
1963  json,
1964  text,
1965} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:stream/consumers'</span>);</code><button class="copy-button">copy</button></pre>
1966<h5><code>streamConsumers.arrayBuffer(stream)</code><span><a class="mark" href="#streamconsumersarraybufferstream" id="streamconsumersarraybufferstream">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_streamconsumers_arraybuffer_stream"></a></h5>
1967<div class="api_metadata">
1968<span>Added in: v16.7.0</span>
1969</div>
1970<ul>
1971<li><code>stream</code> <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> | <a href="stream.html#class-streamreadable" class="type">&#x3C;stream.Readable></a> | <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type">&#x3C;AsyncIterator></a></li>
1972<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with an <code>ArrayBuffer</code> containing the full
1973contents of the stream.</li>
1974</ul>
1975
1976<pre class="with-57-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { arrayBuffer } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:stream/consumers'</span>;
1977<span class="hljs-keyword">import</span> { <span class="hljs-title class_">Readable</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:stream'</span>;
1978<span class="hljs-keyword">import</span> { <span class="hljs-title class_">TextEncoder</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:util'</span>;
1979
1980<span class="hljs-keyword">const</span> encoder = <span class="hljs-keyword">new</span> <span class="hljs-title class_">TextEncoder</span>();
1981<span class="hljs-keyword">const</span> dataArray = encoder.<span class="hljs-title function_">encode</span>(<span class="hljs-string">'hello world from consumers!'</span>);
1982
1983<span class="hljs-keyword">const</span> readable = <span class="hljs-title class_">Readable</span>.<span class="hljs-title function_">from</span>(dataArray);
1984<span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> <span class="hljs-title function_">arrayBuffer</span>(readable);
1985<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`from readable: <span class="hljs-subst">${data.byteLength}</span>`</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { arrayBuffer } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:stream/consumers'</span>);
1986<span class="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:stream'</span>);
1987<span class="hljs-keyword">const</span> { <span class="hljs-title class_">TextEncoder</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>);
1988
1989<span class="hljs-keyword">const</span> encoder = <span class="hljs-keyword">new</span> <span class="hljs-title class_">TextEncoder</span>();
1990<span class="hljs-keyword">const</span> dataArray = encoder.<span class="hljs-title function_">encode</span>(<span class="hljs-string">'hello world from consumers!'</span>);
1991<span class="hljs-keyword">const</span> readable = <span class="hljs-title class_">Readable</span>.<span class="hljs-title function_">from</span>(dataArray);
1992<span class="hljs-title function_">arrayBuffer</span>(readable).<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">data</span>) =></span> {
1993  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`from readable: <span class="hljs-subst">${data.byteLength}</span>`</span>);
1994});</code><button class="copy-button">copy</button></pre>
1995<h5><code>streamConsumers.blob(stream)</code><span><a class="mark" href="#streamconsumersblobstream" id="streamconsumersblobstream">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_streamconsumers_blob_stream"></a></h5>
1996<div class="api_metadata">
1997<span>Added in: v16.7.0</span>
1998</div>
1999<ul>
2000<li><code>stream</code> <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> | <a href="stream.html#class-streamreadable" class="type">&#x3C;stream.Readable></a> | <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type">&#x3C;AsyncIterator></a></li>
2001<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with a <a href="buffer.html#class-blob" class="type">&#x3C;Blob></a> containing the full contents
2002of the stream.</li>
2003</ul>
2004
2005<pre class="with-50-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { blob } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:stream/consumers'</span>;
2006
2007<span class="hljs-keyword">const</span> dataBlob = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Blob</span>([<span class="hljs-string">'hello world from consumers!'</span>]);
2008
2009<span class="hljs-keyword">const</span> readable = dataBlob.<span class="hljs-title function_">stream</span>();
2010<span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> <span class="hljs-title function_">blob</span>(readable);
2011<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`from readable: <span class="hljs-subst">${data.size}</span>`</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { blob } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:stream/consumers'</span>);
2012
2013<span class="hljs-keyword">const</span> dataBlob = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Blob</span>([<span class="hljs-string">'hello world from consumers!'</span>]);
2014
2015<span class="hljs-keyword">const</span> readable = dataBlob.<span class="hljs-title function_">stream</span>();
2016<span class="hljs-title function_">blob</span>(readable).<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">data</span>) =></span> {
2017  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`from readable: <span class="hljs-subst">${data.size}</span>`</span>);
2018});</code><button class="copy-button">copy</button></pre>
2019<h5><code>streamConsumers.buffer(stream)</code><span><a class="mark" href="#streamconsumersbufferstream" id="streamconsumersbufferstream">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_streamconsumers_buffer_stream"></a></h5>
2020<div class="api_metadata">
2021<span>Added in: v16.7.0</span>
2022</div>
2023<ul>
2024<li><code>stream</code> <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> | <a href="stream.html#class-streamreadable" class="type">&#x3C;stream.Readable></a> | <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type">&#x3C;AsyncIterator></a></li>
2025<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with a <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> containing the full
2026contents of the stream.</li>
2027</ul>
2028
2029<pre class="with-52-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { buffer } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:stream/consumers'</span>;
2030<span class="hljs-keyword">import</span> { <span class="hljs-title class_">Readable</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:stream'</span>;
2031<span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
2032
2033<span class="hljs-keyword">const</span> dataBuffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'hello world from consumers!'</span>);
2034
2035<span class="hljs-keyword">const</span> readable = <span class="hljs-title class_">Readable</span>.<span class="hljs-title function_">from</span>(dataBuffer);
2036<span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> <span class="hljs-title function_">buffer</span>(readable);
2037<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`from readable: <span class="hljs-subst">${data.length}</span>`</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { buffer } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:stream/consumers'</span>);
2038<span class="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:stream'</span>);
2039<span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
2040
2041<span class="hljs-keyword">const</span> dataBuffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'hello world from consumers!'</span>);
2042
2043<span class="hljs-keyword">const</span> readable = <span class="hljs-title class_">Readable</span>.<span class="hljs-title function_">from</span>(dataBuffer);
2044<span class="hljs-title function_">buffer</span>(readable).<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">data</span>) =></span> {
2045  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`from readable: <span class="hljs-subst">${data.length}</span>`</span>);
2046});</code><button class="copy-button">copy</button></pre>
2047<h5><code>streamConsumers.json(stream)</code><span><a class="mark" href="#streamconsumersjsonstream" id="streamconsumersjsonstream">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_streamconsumers_json_stream"></a></h5>
2048<div class="api_metadata">
2049<span>Added in: v16.7.0</span>
2050</div>
2051<ul>
2052<li><code>stream</code> <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> | <a href="stream.html#class-streamreadable" class="type">&#x3C;stream.Readable></a> | <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type">&#x3C;AsyncIterator></a></li>
2053<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with the contents of the stream parsed as a
2054UTF-8 encoded string that is then passed through <code>JSON.parse()</code>.</li>
2055</ul>
2056
2057<pre class="with-50-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { json } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:stream/consumers'</span>;
2058<span class="hljs-keyword">import</span> { <span class="hljs-title class_">Readable</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:stream'</span>;
2059
2060<span class="hljs-keyword">const</span> items = <span class="hljs-title class_">Array</span>.<span class="hljs-title function_">from</span>(
2061  {
2062    <span class="hljs-attr">length</span>: <span class="hljs-number">100</span>,
2063  },
2064  <span class="hljs-function">() =></span> ({
2065    <span class="hljs-attr">message</span>: <span class="hljs-string">'hello world from consumers!'</span>,
2066  }),
2067);
2068
2069<span class="hljs-keyword">const</span> readable = <span class="hljs-title class_">Readable</span>.<span class="hljs-title function_">from</span>(<span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(items));
2070<span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> <span class="hljs-title function_">json</span>(readable);
2071<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`from readable: <span class="hljs-subst">${data.length}</span>`</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { json } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:stream/consumers'</span>);
2072<span class="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:stream'</span>);
2073
2074<span class="hljs-keyword">const</span> items = <span class="hljs-title class_">Array</span>.<span class="hljs-title function_">from</span>(
2075  {
2076    <span class="hljs-attr">length</span>: <span class="hljs-number">100</span>,
2077  },
2078  <span class="hljs-function">() =></span> ({
2079    <span class="hljs-attr">message</span>: <span class="hljs-string">'hello world from consumers!'</span>,
2080  }),
2081);
2082
2083<span class="hljs-keyword">const</span> readable = <span class="hljs-title class_">Readable</span>.<span class="hljs-title function_">from</span>(<span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(items));
2084<span class="hljs-title function_">json</span>(readable).<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">data</span>) =></span> {
2085  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`from readable: <span class="hljs-subst">${data.length}</span>`</span>);
2086});</code><button class="copy-button">copy</button></pre>
2087<h5><code>streamConsumers.text(stream)</code><span><a class="mark" href="#streamconsumerstextstream" id="streamconsumerstextstream">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_streamconsumers_text_stream"></a></h5>
2088<div class="api_metadata">
2089<span>Added in: v16.7.0</span>
2090</div>
2091<ul>
2092<li><code>stream</code> <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a> | <a href="stream.html#class-streamreadable" class="type">&#x3C;stream.Readable></a> | <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type">&#x3C;AsyncIterator></a></li>
2093<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with the contents of the stream parsed as a
2094UTF-8 encoded string.</li>
2095</ul>
2096
2097<pre class="with-50-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { text } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:stream/consumers'</span>;
2098<span class="hljs-keyword">import</span> { <span class="hljs-title class_">Readable</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:stream'</span>;
2099
2100<span class="hljs-keyword">const</span> readable = <span class="hljs-title class_">Readable</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'Hello world from consumers!'</span>);
2101<span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> <span class="hljs-title function_">text</span>(readable);
2102<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`from readable: <span class="hljs-subst">${data.length}</span>`</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { text } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:stream/consumers'</span>);
2103<span class="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:stream'</span>);
2104
2105<span class="hljs-keyword">const</span> readable = <span class="hljs-title class_">Readable</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'Hello world from consumers!'</span>);
2106<span class="hljs-title function_">text</span>(readable).<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">data</span>) =></span> {
2107  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`from readable: <span class="hljs-subst">${data.length}</span>`</span>);
2108});</code><button class="copy-button">copy</button></pre></section>
2109        <!-- API END -->
2110      </div>
2111    </div>
2112  </div>
2113</body>
2114</html>
2115