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.20.1"> 7 <title>Web Streams API | Node.js v18.20.1 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.20.1 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.20.1</li> 115 116 <li class="picker-header"> 117 <a href="#"> 118 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</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-bytelengthqueuingstrategyinit"><code>new ByteLengthQueuingStrategy(init)</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-countqueuingstrategyinit"><code>new CountQueuingStrategy(init)</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">►</span><span class="expanded-arrow">▼</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">►</span><span class="expanded-arrow">▼</span> 383 Other versions 384 </a> 385 <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/webstreams.html">21.x</a></li> 386<li><a href="https://nodejs.org/docs/latest-v20.x/api/webstreams.html">20.x <b>LTS</b></a></li> 387<li><a href="https://nodejs.org/docs/latest-v19.x/api/webstreams.html">19.x</a></li> 388<li><a href="https://nodejs.org/docs/latest-v18.x/api/webstreams.html">18.x <b>LTS</b></a></li> 389<li><a href="https://nodejs.org/docs/latest-v17.x/api/webstreams.html">17.x</a></li> 390<li><a href="https://nodejs.org/docs/latest-v16.x/api/webstreams.html">16.x</a></li></ol></div> 391 </li> 392 393 <li class="picker-header"> 394 <a href="#"> 395 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 396 Options 397 </a> 398 399 <div class="picker"> 400 <ul> 401 <li> 402 <a href="all.html">View on single page</a> 403 </li> 404 <li> 405 <a href="webstreams.json">View as JSON</a> 406 </li> 407 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/webstreams.md">Edit on GitHub</a></li> 408 </ul> 409 </div> 410 </li> 411 </ul> 412 </div> 413 <hr> 414 </header> 415 416 <details id="toc" open><summary>Table of contents</summary><ul> 417<li><span class="stability_1"><a href="#web-streams-api">Web Streams API</a></span> 418<ul> 419<li><a href="#overview">Overview</a> 420<ul> 421<li><a href="#example-readablestream">Example <code>ReadableStream</code></a></li> 422</ul> 423</li> 424<li><a href="#api">API</a> 425<ul> 426<li><a href="#class-readablestream">Class: <code>ReadableStream</code></a> 427<ul> 428<li><a href="#new-readablestreamunderlyingsource--strategy"><code>new ReadableStream([underlyingSource [, strategy]])</code></a></li> 429<li><a href="#readablestreamlocked"><code>readableStream.locked</code></a></li> 430<li><a href="#readablestreamcancelreason"><code>readableStream.cancel([reason])</code></a></li> 431<li><a href="#readablestreamgetreaderoptions"><code>readableStream.getReader([options])</code></a></li> 432<li><a href="#readablestreampipethroughtransform-options"><code>readableStream.pipeThrough(transform[, options])</code></a></li> 433<li><a href="#readablestreampipetodestination-options"><code>readableStream.pipeTo(destination[, options])</code></a></li> 434<li><a href="#readablestreamtee"><code>readableStream.tee()</code></a></li> 435<li><a href="#readablestreamvaluesoptions"><code>readableStream.values([options])</code></a></li> 436<li><a href="#async-iteration">Async Iteration</a></li> 437<li><a href="#transferring-with-postmessage">Transferring with <code>postMessage()</code></a></li> 438</ul> 439</li> 440<li><a href="#class-readablestreamdefaultreader">Class: <code>ReadableStreamDefaultReader</code></a> 441<ul> 442<li><a href="#new-readablestreamdefaultreaderstream"><code>new ReadableStreamDefaultReader(stream)</code></a></li> 443<li><a href="#readablestreamdefaultreadercancelreason"><code>readableStreamDefaultReader.cancel([reason])</code></a></li> 444<li><a href="#readablestreamdefaultreaderclosed"><code>readableStreamDefaultReader.closed</code></a></li> 445<li><a href="#readablestreamdefaultreaderread"><code>readableStreamDefaultReader.read()</code></a></li> 446<li><a href="#readablestreamdefaultreaderreleaselock"><code>readableStreamDefaultReader.releaseLock()</code></a></li> 447</ul> 448</li> 449<li><a href="#class-readablestreambyobreader">Class: <code>ReadableStreamBYOBReader</code></a> 450<ul> 451<li><a href="#new-readablestreambyobreaderstream"><code>new ReadableStreamBYOBReader(stream)</code></a></li> 452<li><a href="#readablestreambyobreadercancelreason"><code>readableStreamBYOBReader.cancel([reason])</code></a></li> 453<li><a href="#readablestreambyobreaderclosed"><code>readableStreamBYOBReader.closed</code></a></li> 454<li><a href="#readablestreambyobreaderreadview"><code>readableStreamBYOBReader.read(view)</code></a></li> 455<li><a href="#readablestreambyobreaderreleaselock"><code>readableStreamBYOBReader.releaseLock()</code></a></li> 456</ul> 457</li> 458<li><a href="#class-readablestreamdefaultcontroller">Class: <code>ReadableStreamDefaultController</code></a> 459<ul> 460<li><a href="#readablestreamdefaultcontrollerclose"><code>readableStreamDefaultController.close()</code></a></li> 461<li><a href="#readablestreamdefaultcontrollerdesiredsize"><code>readableStreamDefaultController.desiredSize</code></a></li> 462<li><a href="#readablestreamdefaultcontrollerenqueuechunk"><code>readableStreamDefaultController.enqueue([chunk])</code></a></li> 463<li><a href="#readablestreamdefaultcontrollererrorerror"><code>readableStreamDefaultController.error([error])</code></a></li> 464</ul> 465</li> 466<li><a href="#class-readablebytestreamcontroller">Class: <code>ReadableByteStreamController</code></a> 467<ul> 468<li><a href="#readablebytestreamcontrollerbyobrequest"><code>readableByteStreamController.byobRequest</code></a></li> 469<li><a href="#readablebytestreamcontrollerclose"><code>readableByteStreamController.close()</code></a></li> 470<li><a href="#readablebytestreamcontrollerdesiredsize"><code>readableByteStreamController.desiredSize</code></a></li> 471<li><a href="#readablebytestreamcontrollerenqueuechunk"><code>readableByteStreamController.enqueue(chunk)</code></a></li> 472<li><a href="#readablebytestreamcontrollererrorerror"><code>readableByteStreamController.error([error])</code></a></li> 473</ul> 474</li> 475<li><a href="#class-readablestreambyobrequest">Class: <code>ReadableStreamBYOBRequest</code></a> 476<ul> 477<li><a href="#readablestreambyobrequestrespondbyteswritten"><code>readableStreamBYOBRequest.respond(bytesWritten)</code></a></li> 478<li><a href="#readablestreambyobrequestrespondwithnewviewview"><code>readableStreamBYOBRequest.respondWithNewView(view)</code></a></li> 479<li><a href="#readablestreambyobrequestview"><code>readableStreamBYOBRequest.view</code></a></li> 480</ul> 481</li> 482<li><a href="#class-writablestream">Class: <code>WritableStream</code></a> 483<ul> 484<li><a href="#new-writablestreamunderlyingsink-strategy"><code>new WritableStream([underlyingSink[, strategy]])</code></a></li> 485<li><a href="#writablestreamabortreason"><code>writableStream.abort([reason])</code></a></li> 486<li><a href="#writablestreamclose"><code>writableStream.close()</code></a></li> 487<li><a href="#writablestreamgetwriter"><code>writableStream.getWriter()</code></a></li> 488<li><a href="#writablestreamlocked"><code>writableStream.locked</code></a></li> 489<li><a href="#transferring-with-postmessage_1">Transferring with postMessage()</a></li> 490</ul> 491</li> 492<li><a href="#class-writablestreamdefaultwriter">Class: <code>WritableStreamDefaultWriter</code></a> 493<ul> 494<li><a href="#new-writablestreamdefaultwriterstream"><code>new WritableStreamDefaultWriter(stream)</code></a></li> 495<li><a href="#writablestreamdefaultwriterabortreason"><code>writableStreamDefaultWriter.abort([reason])</code></a></li> 496<li><a href="#writablestreamdefaultwriterclose"><code>writableStreamDefaultWriter.close()</code></a></li> 497<li><a href="#writablestreamdefaultwriterclosed"><code>writableStreamDefaultWriter.closed</code></a></li> 498<li><a href="#writablestreamdefaultwriterdesiredsize"><code>writableStreamDefaultWriter.desiredSize</code></a></li> 499<li><a href="#writablestreamdefaultwriterready"><code>writableStreamDefaultWriter.ready</code></a></li> 500<li><a href="#writablestreamdefaultwriterreleaselock"><code>writableStreamDefaultWriter.releaseLock()</code></a></li> 501<li><a href="#writablestreamdefaultwriterwritechunk"><code>writableStreamDefaultWriter.write([chunk])</code></a></li> 502</ul> 503</li> 504<li><a href="#class-writablestreamdefaultcontroller">Class: <code>WritableStreamDefaultController</code></a> 505<ul> 506<li><a href="#writablestreamdefaultcontrollererrorerror"><code>writableStreamDefaultController.error([error])</code></a></li> 507<li><a href="#writablestreamdefaultcontrollersignal"><code>writableStreamDefaultController.signal</code></a></li> 508</ul> 509</li> 510<li><a href="#class-transformstream">Class: <code>TransformStream</code></a> 511<ul> 512<li><a href="#new-transformstreamtransformer-writablestrategy-readablestrategy"><code>new TransformStream([transformer[, writableStrategy[, readableStrategy]]])</code></a></li> 513<li><a href="#transformstreamreadable"><code>transformStream.readable</code></a></li> 514<li><a href="#transformstreamwritable"><code>transformStream.writable</code></a></li> 515<li><a href="#transferring-with-postmessage_2">Transferring with postMessage()</a></li> 516</ul> 517</li> 518<li><a href="#class-transformstreamdefaultcontroller">Class: <code>TransformStreamDefaultController</code></a> 519<ul> 520<li><a href="#transformstreamdefaultcontrollerdesiredsize"><code>transformStreamDefaultController.desiredSize</code></a></li> 521<li><a href="#transformstreamdefaultcontrollerenqueuechunk"><code>transformStreamDefaultController.enqueue([chunk])</code></a></li> 522<li><a href="#transformstreamdefaultcontrollererrorreason"><code>transformStreamDefaultController.error([reason])</code></a></li> 523<li><a href="#transformstreamdefaultcontrollerterminate"><code>transformStreamDefaultController.terminate()</code></a></li> 524</ul> 525</li> 526<li><a href="#class-bytelengthqueuingstrategy">Class: <code>ByteLengthQueuingStrategy</code></a> 527<ul> 528<li><a href="#new-bytelengthqueuingstrategyinit"><code>new ByteLengthQueuingStrategy(init)</code></a></li> 529<li><a href="#bytelengthqueuingstrategyhighwatermark"><code>byteLengthQueuingStrategy.highWaterMark</code></a></li> 530<li><a href="#bytelengthqueuingstrategysize"><code>byteLengthQueuingStrategy.size</code></a></li> 531</ul> 532</li> 533<li><a href="#class-countqueuingstrategy">Class: <code>CountQueuingStrategy</code></a> 534<ul> 535<li><a href="#new-countqueuingstrategyinit"><code>new CountQueuingStrategy(init)</code></a></li> 536<li><a href="#countqueuingstrategyhighwatermark"><code>countQueuingStrategy.highWaterMark</code></a></li> 537<li><a href="#countqueuingstrategysize"><code>countQueuingStrategy.size</code></a></li> 538</ul> 539</li> 540<li><a href="#class-textencoderstream">Class: <code>TextEncoderStream</code></a> 541<ul> 542<li><a href="#new-textencoderstream"><code>new TextEncoderStream()</code></a></li> 543<li><a href="#textencoderstreamencoding"><code>textEncoderStream.encoding</code></a></li> 544<li><a href="#textencoderstreamreadable"><code>textEncoderStream.readable</code></a></li> 545<li><a href="#textencoderstreamwritable"><code>textEncoderStream.writable</code></a></li> 546</ul> 547</li> 548<li><a href="#class-textdecoderstream">Class: <code>TextDecoderStream</code></a> 549<ul> 550<li><a href="#new-textdecoderstreamencoding-options"><code>new TextDecoderStream([encoding[, options]])</code></a></li> 551<li><a href="#textdecoderstreamencoding"><code>textDecoderStream.encoding</code></a></li> 552<li><a href="#textdecoderstreamfatal"><code>textDecoderStream.fatal</code></a></li> 553<li><a href="#textdecoderstreamignorebom"><code>textDecoderStream.ignoreBOM</code></a></li> 554<li><a href="#textdecoderstreamreadable"><code>textDecoderStream.readable</code></a></li> 555<li><a href="#textdecoderstreamwritable"><code>textDecoderStream.writable</code></a></li> 556</ul> 557</li> 558<li><a href="#class-compressionstream">Class: <code>CompressionStream</code></a> 559<ul> 560<li><a href="#new-compressionstreamformat"><code>new CompressionStream(format)</code></a></li> 561<li><a href="#compressionstreamreadable"><code>compressionStream.readable</code></a></li> 562<li><a href="#compressionstreamwritable"><code>compressionStream.writable</code></a></li> 563</ul> 564</li> 565<li><a href="#class-decompressionstream">Class: <code>DecompressionStream</code></a> 566<ul> 567<li><a href="#new-decompressionstreamformat"><code>new DecompressionStream(format)</code></a></li> 568<li><a href="#decompressionstreamreadable"><code>decompressionStream.readable</code></a></li> 569<li><a href="#decompressionstreamwritable"><code>decompressionStream.writable</code></a></li> 570</ul> 571</li> 572<li><a href="#utility-consumers">Utility Consumers</a> 573<ul> 574<li><a href="#streamconsumersarraybufferstream"><code>streamConsumers.arrayBuffer(stream)</code></a></li> 575<li><a href="#streamconsumersblobstream"><code>streamConsumers.blob(stream)</code></a></li> 576<li><a href="#streamconsumersbufferstream"><code>streamConsumers.buffer(stream)</code></a></li> 577<li><a href="#streamconsumersjsonstream"><code>streamConsumers.json(stream)</code></a></li> 578<li><a href="#streamconsumerstextstream"><code>streamConsumers.text(stream)</code></a></li> 579</ul> 580</li> 581</ul> 582</li> 583</ul> 584</li> 585</ul></details> 586 587 <div id="apicontent"> 588 <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> 589 590<div class="api_metadata"> 591<details class="changelog"><summary>History</summary> 592<table> 593<tbody><tr><th>Version</th><th>Changes</th></tr> 594<tr><td>v18.0.0</td> 595<td><p>Use of this API no longer emit a runtime warning.</p></td></tr> 596<tr><td>v16.5.0</td> 597<td><p><span>Added in: v16.5.0</span></p></td></tr> 598</tbody></table> 599</details> 600</div> 601<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental.</div><p></p> 602<p>An implementation of the <a href="https://streams.spec.whatwg.org/">WHATWG Streams Standard</a>.</p> 603<section><h3>Overview<span><a class="mark" href="#overview" id="overview">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_overview"></a></h3> 604<p>The <a href="https://streams.spec.whatwg.org/">WHATWG Streams Standard</a> (or "web streams") defines an API for handling 605streaming data. It is similar to the Node.js <a href="stream.html">Streams</a> API but emerged later 606and has become the "standard" API for streaming data across many JavaScript 607environments.</p> 608<p>There are three primary types of objects:</p> 609<ul> 610<li><code>ReadableStream</code> - Represents a source of streaming data.</li> 611<li><code>WritableStream</code> - Represents a destination for streaming data.</li> 612<li><code>TransformStream</code> - Represents an algorithm for transforming streaming data.</li> 613</ul> 614<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> 615<p>This example creates a simple <code>ReadableStream</code> that pushes the current 616<code>performance.now()</code> timestamp once every second forever. An async iterable 617is used to read the data from the stream.</p> 618 619<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> { 620 <span class="hljs-title class_">ReadableStream</span>, 621} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:stream/web'</span>; 622 623<span class="hljs-keyword">import</span> { 624 <span class="hljs-built_in">setInterval</span> <span class="hljs-keyword">as</span> every, 625} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:timers/promises'</span>; 626 627<span class="hljs-keyword">import</span> { 628 performance, 629} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:perf_hooks'</span>; 630 631<span class="hljs-keyword">const</span> <span class="hljs-variable constant_">SECOND</span> = <span class="hljs-number">1000</span>; 632 633<span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ReadableStream</span>({ 634 <span class="hljs-keyword">async</span> <span class="hljs-title function_">start</span>(<span class="hljs-params">controller</span>) { 635 <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>)) 636 controller.<span class="hljs-title function_">enqueue</span>(performance.<span class="hljs-title function_">now</span>()); 637 }, 638}); 639 640<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) 641 <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> { 642 <span class="hljs-title class_">ReadableStream</span>, 643} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:stream/web'</span>); 644 645<span class="hljs-keyword">const</span> { 646 <span class="hljs-attr">setInterval</span>: every, 647} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:timers/promises'</span>); 648 649<span class="hljs-keyword">const</span> { 650 performance, 651} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:perf_hooks'</span>); 652 653<span class="hljs-keyword">const</span> <span class="hljs-variable constant_">SECOND</span> = <span class="hljs-number">1000</span>; 654 655<span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ReadableStream</span>({ 656 <span class="hljs-keyword">async</span> <span class="hljs-title function_">start</span>(<span class="hljs-params">controller</span>) { 657 <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>)) 658 controller.<span class="hljs-title function_">enqueue</span>(performance.<span class="hljs-title function_">now</span>()); 659 }, 660}); 661 662(<span class="hljs-keyword">async</span> () => { 663 <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) 664 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value); 665})();</code><button class="copy-button">copy</button></pre> 666</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> 667<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> 668<div class="api_metadata"> 669<details class="changelog"><summary>History</summary> 670<table> 671<tbody><tr><th>Version</th><th>Changes</th></tr> 672<tr><td>v18.0.0</td> 673<td><p>This class is now exposed on the global object.</p></td></tr> 674<tr><td>v16.5.0</td> 675<td><p><span>Added in: v16.5.0</span></p></td></tr> 676</tbody></table> 677</details> 678</div> 679<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> 680<div class="api_metadata"> 681<span>Added in: v16.5.0</span> 682</div> 683<!--lint disable maximum-line-length remark-lint--> 684<ul> 685<li><code>underlyingSource</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 686<ul> 687<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> A user-defined function that is invoked immediately when 688the <code>ReadableStream</code> is created. 689<ul> 690<li><code>controller</code> <a href="webstreams.html#class-readablestreamdefaultcontroller" class="type"><ReadableStreamDefaultController></a> | <a href="webstreams.html#class-readablebytestreamcontroller" class="type"><ReadableByteStreamController></a></li> 691<li>Returns: <code>undefined</code> or a promise fulfilled with <code>undefined</code>.</li> 692</ul> 693</li> 694<li><code>pull</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> A user-defined function that is called repeatedly when the 695<code>ReadableStream</code> internal queue is not full. The operation may be sync or 696async. If async, the function will not be called again until the previously 697returned promise is fulfilled. 698<ul> 699<li><code>controller</code> <a href="webstreams.html#class-readablestreamdefaultcontroller" class="type"><ReadableStreamDefaultController></a> | <a href="webstreams.html#class-readablebytestreamcontroller" class="type"><ReadableByteStreamController></a></li> 700<li>Returns: A promise fulfilled with <code>undefined</code>.</li> 701</ul> 702</li> 703<li><code>cancel</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> A user-defined function that is called when the 704<code>ReadableStream</code> is canceled. 705<ul> 706<li><code>reason</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 707<li>Returns: A promise fulfilled with <code>undefined</code>.</li> 708</ul> 709</li> 710<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Must be <code>'bytes'</code> or <code>undefined</code>.</li> 711<li><code>autoAllocateChunkSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Used only when <code>type</code> is equal to 712<code>'bytes'</code>. When set to a non-zero value a view buffer is automatically 713allocated to <code>ReadableByteStreamController.byobRequest</code>. When not set 714one must use stream's internal queues to transfer data via the default 715reader <code>ReadableStreamDefaultReader</code>.</li> 716</ul> 717</li> 718<li><code>strategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 719<ul> 720<li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The maximum internal queue size before backpressure 721is applied.</li> 722<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> A user-defined function used to identify the size of each 723chunk of data. 724<ul> 725<li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 726<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 727</ul> 728</li> 729</ul> 730</li> 731</ul> 732<!--lint enable maximum-line-length remark-lint--> 733<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> 734<div class="api_metadata"> 735<span>Added in: v16.5.0</span> 736</div> 737<ul> 738<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Set to <code>true</code> if there is an active reader for this 739<a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a>.</li> 740</ul> 741<p>The <code>readableStream.locked</code> property is <code>false</code> by default, and is 742switched to <code>true</code> while there is an active reader consuming the 743stream's data.</p> 744<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> 745<div class="api_metadata"> 746<span>Added in: v16.5.0</span> 747</div> 748<ul> 749<li><code>reason</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 750<li>Returns: A promise fulfilled with <code>undefined</code> once cancelation has 751been completed.</li> 752</ul> 753<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> 754<div class="api_metadata"> 755<span>Added in: v16.5.0</span> 756</div> 757<ul> 758<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 759<ul> 760<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <code>'byob'</code> or <code>undefined</code></li> 761</ul> 762</li> 763<li>Returns: <a href="webstreams.html#class-readablestreamdefaultreader" class="type"><ReadableStreamDefaultReader></a> | <a href="webstreams.html#class-readablestreambyobreader" class="type"><ReadableStreamBYOBReader></a></li> 764</ul> 765 766<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>; 767 768<span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ReadableStream</span>(); 769 770<span class="hljs-keyword">const</span> reader = stream.<span class="hljs-title function_">getReader</span>(); 771 772<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>); 773 774<span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ReadableStream</span>(); 775 776<span class="hljs-keyword">const</span> reader = stream.<span class="hljs-title function_">getReader</span>(); 777 778reader.<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> 779<p>Causes the <code>readableStream.locked</code> to be <code>true</code>.</p> 780<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> 781<div class="api_metadata"> 782<span>Added in: v16.5.0</span> 783</div> 784<ul> 785<li><code>transform</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 786<ul> 787<li><code>readable</code> <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> The <code>ReadableStream</code> to which 788<code>transform.writable</code> will push the potentially modified data 789is receives from this <code>ReadableStream</code>.</li> 790<li><code>writable</code> <a href="webstreams.html#class-writablestream" class="type"><WritableStream></a> The <code>WritableStream</code> to which this 791<code>ReadableStream</code>'s data will be written.</li> 792</ul> 793</li> 794<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 795<ul> 796<li><code>preventAbort</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, errors in this <code>ReadableStream</code> 797will not cause <code>transform.writable</code> to be aborted.</li> 798<li><code>preventCancel</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, errors in the destination 799<code>transform.writable</code> do not cause this <code>ReadableStream</code> to be 800canceled.</li> 801<li><code>preventClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, closing this <code>ReadableStream</code> 802does not cause <code>transform.writable</code> to be closed.</li> 803<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows the transfer of data to be canceled 804using an <a href="globals.html#class-abortcontroller" class="type"><AbortController></a>.</li> 805</ul> 806</li> 807<li>Returns: <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> From <code>transform.readable</code>.</li> 808</ul> 809<p>Connects this <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> to the pair of <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> and 810<a href="webstreams.html#class-writablestream" class="type"><WritableStream></a> provided in the <code>transform</code> argument such that the 811data from this <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> is written in to <code>transform.writable</code>, 812possibly transformed, then pushed to <code>transform.readable</code>. Once the 813pipeline is configured, <code>transform.readable</code> is returned.</p> 814<p>Causes the <code>readableStream.locked</code> to be <code>true</code> while the pipe operation 815is active.</p> 816 817<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> { 818 <span class="hljs-title class_">ReadableStream</span>, 819 <span class="hljs-title class_">TransformStream</span>, 820} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:stream/web'</span>; 821 822<span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ReadableStream</span>({ 823 <span class="hljs-title function_">start</span>(<span class="hljs-params">controller</span>) { 824 controller.<span class="hljs-title function_">enqueue</span>(<span class="hljs-string">'a'</span>); 825 }, 826}); 827 828<span class="hljs-keyword">const</span> transform = <span class="hljs-keyword">new</span> <span class="hljs-title class_">TransformStream</span>({ 829 <span class="hljs-title function_">transform</span>(<span class="hljs-params">chunk, controller</span>) { 830 controller.<span class="hljs-title function_">enqueue</span>(chunk.<span class="hljs-title function_">toUpperCase</span>()); 831 }, 832}); 833 834<span class="hljs-keyword">const</span> transformedStream = stream.<span class="hljs-title function_">pipeThrough</span>(transform); 835 836<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) 837 <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> { 838 <span class="hljs-title class_">ReadableStream</span>, 839 <span class="hljs-title class_">TransformStream</span>, 840} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:stream/web'</span>); 841 842<span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ReadableStream</span>({ 843 <span class="hljs-title function_">start</span>(<span class="hljs-params">controller</span>) { 844 controller.<span class="hljs-title function_">enqueue</span>(<span class="hljs-string">'a'</span>); 845 }, 846}); 847 848<span class="hljs-keyword">const</span> transform = <span class="hljs-keyword">new</span> <span class="hljs-title class_">TransformStream</span>({ 849 <span class="hljs-title function_">transform</span>(<span class="hljs-params">chunk, controller</span>) { 850 controller.<span class="hljs-title function_">enqueue</span>(chunk.<span class="hljs-title function_">toUpperCase</span>()); 851 }, 852}); 853 854<span class="hljs-keyword">const</span> transformedStream = stream.<span class="hljs-title function_">pipeThrough</span>(transform); 855 856(<span class="hljs-keyword">async</span> () => { 857 <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) 858 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(chunk); 859})();</code><button class="copy-button">copy</button></pre> 860<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> 861<div class="api_metadata"> 862<span>Added in: v16.5.0</span> 863</div> 864<ul> 865<li><code>destination</code> <a href="webstreams.html#class-writablestream" class="type"><WritableStream></a> A <a href="webstreams.html#class-writablestream" class="type"><WritableStream></a> to which this 866<code>ReadableStream</code>'s data will be written.</li> 867<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 868<ul> 869<li><code>preventAbort</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, errors in this <code>ReadableStream</code> 870will not cause <code>destination</code> to be aborted.</li> 871<li><code>preventCancel</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, errors in the <code>destination</code> 872will not cause this <code>ReadableStream</code> to be canceled.</li> 873<li><code>preventClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, closing this <code>ReadableStream</code> 874does not cause <code>destination</code> to be closed.</li> 875<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows the transfer of data to be canceled 876using an <a href="globals.html#class-abortcontroller" class="type"><AbortController></a>.</li> 877</ul> 878</li> 879<li>Returns: A promise fulfilled with <code>undefined</code></li> 880</ul> 881<p>Causes the <code>readableStream.locked</code> to be <code>true</code> while the pipe operation 882is active.</p> 883<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> 884<div class="api_metadata"> 885<details class="changelog"><summary>History</summary> 886<table> 887<tbody><tr><th>Version</th><th>Changes</th></tr> 888<tr><td>v18.10.0</td> 889<td><p>Support teeing a readable byte stream.</p></td></tr> 890<tr><td>v16.5.0</td> 891<td><p><span>Added in: v16.5.0</span></p></td></tr> 892</tbody></table> 893</details> 894</div> 895<ul> 896<li>Returns: <a href="webstreams.html#class-readablestream" class="type"><ReadableStream[]></a></li> 897</ul> 898<p>Returns a pair of new <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> instances to which this 899<code>ReadableStream</code>'s data will be forwarded. Each will receive the 900same data.</p> 901<p>Causes the <code>readableStream.locked</code> to be <code>true</code>.</p> 902<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> 903<div class="api_metadata"> 904<span>Added in: v16.5.0</span> 905</div> 906<ul> 907<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 908<ul> 909<li><code>preventCancel</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, prevents the <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> 910from being closed when the async iterator abruptly terminates. 911<strong>Default</strong>: <code>false</code>.</li> 912</ul> 913</li> 914</ul> 915<p>Creates and returns an async iterator usable for consuming this 916<code>ReadableStream</code>'s data.</p> 917<p>Causes the <code>readableStream.locked</code> to be <code>true</code> while the async iterator 918is active.</p> 919<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>; 920 921<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>()); 922 923<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> })) 924 <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> 925<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> 926<p>The <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> object supports the async iterator protocol using 927<code>for await</code> syntax.</p> 928<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>; 929 930<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>()); 931 932<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) 933 <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> 934<p>The async iterator will consume the <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> until it terminates.</p> 935<p>By default, if the async iterator exits early (via either a <code>break</code>, 936<code>return</code>, or a <code>throw</code>), the <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> will be closed. To prevent 937automatic closing of the <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a>, use the <code>readableStream.values()</code> 938method to acquire the async iterator and set the <code>preventCancel</code> option to 939<code>true</code>.</p> 940<p>The <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> must not be locked (that is, it must not have an existing 941active reader). During the async iteration, the <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> will be locked.</p> 942<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> 943<p>A <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> instance can be transferred using a <a href="worker_threads.html#class-messageport" class="type"><MessagePort></a>.</p> 944<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>()); 945 946<span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); 947 948port1.<span class="hljs-property">onmessage</span> = <span class="hljs-function">(<span class="hljs-params">{ data }</span>) =></span> { 949 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> { 950 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(chunk); 951 }); 952}; 953 954port2.<span class="hljs-title function_">postMessage</span>(stream, [stream]);</code> <button class="copy-button">copy</button></pre> 955<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> 956<div class="api_metadata"> 957<details class="changelog"><summary>History</summary> 958<table> 959<tbody><tr><th>Version</th><th>Changes</th></tr> 960<tr><td>v18.0.0</td> 961<td><p>This class is now exposed on the global object.</p></td></tr> 962<tr><td>v16.5.0</td> 963<td><p><span>Added in: v16.5.0</span></p></td></tr> 964</tbody></table> 965</details> 966</div> 967<p>By default, calling <code>readableStream.getReader()</code> with no arguments 968will return an instance of <code>ReadableStreamDefaultReader</code>. The default 969reader treats the chunks of data passed through the stream as opaque 970values, which allows the <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> to work with generally any 971JavaScript value.</p> 972<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> 973<div class="api_metadata"> 974<span>Added in: v16.5.0</span> 975</div> 976<ul> 977<li><code>stream</code> <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a></li> 978</ul> 979<p>Creates a new <a href="webstreams.html#class-readablestreamdefaultreader" class="type"><ReadableStreamDefaultReader></a> that is locked to the 980given <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a>.</p> 981<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> 982<div class="api_metadata"> 983<span>Added in: v16.5.0</span> 984</div> 985<ul> 986<li><code>reason</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 987<li>Returns: A promise fulfilled with <code>undefined</code>.</li> 988</ul> 989<p>Cancels the <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> and returns a promise that is fulfilled 990when the underlying stream has been canceled.</p> 991<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> 992<div class="api_metadata"> 993<span>Added in: v16.5.0</span> 994</div> 995<ul> 996<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfilled with <code>undefined</code> when the associated 997<a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> is closed or rejected if the stream errors or the reader's 998lock is released before the stream finishes closing.</li> 999</ul> 1000<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> 1001<div class="api_metadata"> 1002<span>Added in: v16.5.0</span> 1003</div> 1004<ul> 1005<li>Returns: A promise fulfilled with an object: 1006<ul> 1007<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a></li> 1008<li><code>done</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1009</ul> 1010</li> 1011</ul> 1012<p>Requests the next chunk of data from the underlying <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> 1013and returns a promise that is fulfilled with the data once it is 1014available.</p> 1015<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> 1016<div class="api_metadata"> 1017<span>Added in: v16.5.0</span> 1018</div> 1019<p>Releases this reader's lock on the underlying <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a>.</p> 1020<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> 1021<div class="api_metadata"> 1022<details class="changelog"><summary>History</summary> 1023<table> 1024<tbody><tr><th>Version</th><th>Changes</th></tr> 1025<tr><td>v18.0.0</td> 1026<td><p>This class is now exposed on the global object.</p></td></tr> 1027<tr><td>v16.5.0</td> 1028<td><p><span>Added in: v16.5.0</span></p></td></tr> 1029</tbody></table> 1030</details> 1031</div> 1032<p>The <code>ReadableStreamBYOBReader</code> is an alternative consumer for 1033byte-oriented <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a>s (those that are created with 1034<code>underlyingSource.type</code> set equal to <code>'bytes'</code> when the 1035<code>ReadableStream</code> was created).</p> 1036<p>The <code>BYOB</code> is short for "bring your own buffer". This is a 1037pattern that allows for more efficient reading of byte-oriented 1038data that avoids extraneous copying.</p> 1039<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { 1040 open, 1041} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>; 1042 1043<span class="hljs-keyword">import</span> { 1044 <span class="hljs-title class_">ReadableStream</span>, 1045} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:stream/web'</span>; 1046 1047<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>; 1048 1049<span class="hljs-keyword">class</span> <span class="hljs-title class_">Source</span> { 1050 type = <span class="hljs-string">'bytes'</span>; 1051 autoAllocateChunkSize = <span class="hljs-number">1024</span>; 1052 1053 <span class="hljs-keyword">async</span> <span class="hljs-title function_">start</span>(<span class="hljs-params">controller</span>) { 1054 <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>)); 1055 <span class="hljs-variable language_">this</span>.<span class="hljs-property">controller</span> = controller; 1056 } 1057 1058 <span class="hljs-keyword">async</span> <span class="hljs-title function_">pull</span>(<span class="hljs-params">controller</span>) { 1059 <span class="hljs-keyword">const</span> view = controller.<span class="hljs-property">byobRequest</span>?.<span class="hljs-property">view</span>; 1060 <span class="hljs-keyword">const</span> { 1061 bytesRead, 1062 } = <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>({ 1063 <span class="hljs-attr">buffer</span>: view, 1064 <span class="hljs-attr">offset</span>: view.<span class="hljs-property">byteOffset</span>, 1065 <span class="hljs-attr">length</span>: view.<span class="hljs-property">byteLength</span>, 1066 }); 1067 1068 <span class="hljs-keyword">if</span> (bytesRead === <span class="hljs-number">0</span>) { 1069 <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>(); 1070 <span class="hljs-variable language_">this</span>.<span class="hljs-property">controller</span>.<span class="hljs-title function_">close</span>(); 1071 } 1072 controller.<span class="hljs-property">byobRequest</span>.<span class="hljs-title function_">respond</span>(bytesRead); 1073 } 1074} 1075 1076<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>()); 1077 1078<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>) { 1079 <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> }); 1080 1081 <span class="hljs-keyword">const</span> chunks = []; 1082 <span class="hljs-keyword">let</span> result; 1083 <span class="hljs-keyword">do</span> { 1084 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>)); 1085 <span class="hljs-keyword">if</span> (result.<span class="hljs-property">value</span> !== <span class="hljs-literal">undefined</span>) 1086 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>)); 1087 } <span class="hljs-keyword">while</span> (!result.<span class="hljs-property">done</span>); 1088 1089 <span class="hljs-keyword">return</span> <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">concat</span>(chunks); 1090} 1091 1092<span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> <span class="hljs-title function_">read</span>(stream); 1093<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> 1094<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> 1095<div class="api_metadata"> 1096<span>Added in: v16.5.0</span> 1097</div> 1098<ul> 1099<li><code>stream</code> <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a></li> 1100</ul> 1101<p>Creates a new <code>ReadableStreamBYOBReader</code> that is locked to the 1102given <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a>.</p> 1103<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> 1104<div class="api_metadata"> 1105<span>Added in: v16.5.0</span> 1106</div> 1107<ul> 1108<li><code>reason</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1109<li>Returns: A promise fulfilled with <code>undefined</code>.</li> 1110</ul> 1111<p>Cancels the <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> and returns a promise that is fulfilled 1112when the underlying stream has been canceled.</p> 1113<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> 1114<div class="api_metadata"> 1115<span>Added in: v16.5.0</span> 1116</div> 1117<ul> 1118<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfilled with <code>undefined</code> when the associated 1119<a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> is closed or rejected if the stream errors or the reader's 1120lock is released before the stream finishes closing.</li> 1121</ul> 1122<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> 1123<div class="api_metadata"> 1124<span>Added in: v16.5.0</span> 1125</div> 1126<ul> 1127<li><code>view</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 1128<li>Returns: A promise fulfilled with an object: 1129<ul> 1130<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a></li> 1131<li><code>done</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1132</ul> 1133</li> 1134</ul> 1135<p>Requests the next chunk of data from the underlying <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> 1136and returns a promise that is fulfilled with the data once it is 1137available.</p> 1138<p>Do not pass a pooled <a href="buffer.html#class-buffer" class="type"><Buffer></a> object instance in to this method. 1139Pooled <code>Buffer</code> objects are created using <code>Buffer.allocUnsafe()</code>, 1140or <code>Buffer.from()</code>, or are often returned by various <code>node:fs</code> module 1141callbacks. These types of <code>Buffer</code>s use a shared underlying 1142<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> object that contains all of the data from all of 1143the 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"><TypedArray></a>, 1144or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> is passed in to <code>readableStreamBYOBReader.read()</code>, 1145the view's underlying <code>ArrayBuffer</code> is <em>detached</em>, invalidating 1146all existing views that may exist on that <code>ArrayBuffer</code>. This 1147can have disastrous consequences for your application.</p> 1148<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> 1149<div class="api_metadata"> 1150<span>Added in: v16.5.0</span> 1151</div> 1152<p>Releases this reader's lock on the underlying <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a>.</p> 1153<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> 1154<div class="api_metadata"> 1155<span>Added in: v16.5.0</span> 1156</div> 1157<p>Every <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> has a controller that is responsible for 1158the internal state and management of the stream's queue. The 1159<code>ReadableStreamDefaultController</code> is the default controller 1160implementation for <code>ReadableStream</code>s that are not byte-oriented.</p> 1161<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> 1162<div class="api_metadata"> 1163<span>Added in: v16.5.0</span> 1164</div> 1165<p>Closes the <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> to which this controller is associated.</p> 1166<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> 1167<div class="api_metadata"> 1168<span>Added in: v16.5.0</span> 1169</div> 1170<ul> 1171<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1172</ul> 1173<p>Returns the amount of data remaining to fill the <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a>'s 1174queue.</p> 1175<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> 1176<div class="api_metadata"> 1177<span>Added in: v16.5.0</span> 1178</div> 1179<ul> 1180<li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1181</ul> 1182<p>Appends a new chunk of data to the <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a>'s queue.</p> 1183<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> 1184<div class="api_metadata"> 1185<span>Added in: v16.5.0</span> 1186</div> 1187<ul> 1188<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1189</ul> 1190<p>Signals an error that causes the <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> to error and close.</p> 1191<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> 1192<div class="api_metadata"> 1193<details class="changelog"><summary>History</summary> 1194<table> 1195<tbody><tr><th>Version</th><th>Changes</th></tr> 1196<tr><td>v18.10.0</td> 1197<td><p>Support handling a BYOB pull request from a released reader.</p></td></tr> 1198<tr><td>v16.5.0</td> 1199<td><p><span>Added in: v16.5.0</span></p></td></tr> 1200</tbody></table> 1201</details> 1202</div> 1203<p>Every <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> has a controller that is responsible for 1204the internal state and management of the stream's queue. The 1205<code>ReadableByteStreamController</code> is for byte-oriented <code>ReadableStream</code>s.</p> 1206<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> 1207<div class="api_metadata"> 1208<span>Added in: v16.5.0</span> 1209</div> 1210<ul> 1211<li>Type: <a href="webstreams.html#class-readablestreambyobrequest" class="type"><ReadableStreamBYOBRequest></a></li> 1212</ul> 1213<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> 1214<div class="api_metadata"> 1215<span>Added in: v16.5.0</span> 1216</div> 1217<p>Closes the <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> to which this controller is associated.</p> 1218<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> 1219<div class="api_metadata"> 1220<span>Added in: v16.5.0</span> 1221</div> 1222<ul> 1223<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1224</ul> 1225<p>Returns the amount of data remaining to fill the <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a>'s 1226queue.</p> 1227<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> 1228<div class="api_metadata"> 1229<span>Added in: v16.5.0</span> 1230</div> 1231<ul> 1232<li><code>chunk</code>: <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 1233</ul> 1234<p>Appends a new chunk of data to the <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a>'s queue.</p> 1235<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> 1236<div class="api_metadata"> 1237<span>Added in: v16.5.0</span> 1238</div> 1239<ul> 1240<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1241</ul> 1242<p>Signals an error that causes the <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> to error and close.</p> 1243<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> 1244<div class="api_metadata"> 1245<details class="changelog"><summary>History</summary> 1246<table> 1247<tbody><tr><th>Version</th><th>Changes</th></tr> 1248<tr><td>v18.0.0</td> 1249<td><p>This class is now exposed on the global object.</p></td></tr> 1250<tr><td>v16.5.0</td> 1251<td><p><span>Added in: v16.5.0</span></p></td></tr> 1252</tbody></table> 1253</details> 1254</div> 1255<p>When using <code>ReadableByteStreamController</code> in byte-oriented 1256streams, and when using the <code>ReadableStreamBYOBReader</code>, 1257the <code>readableByteStreamController.byobRequest</code> property 1258provides access to a <code>ReadableStreamBYOBRequest</code> instance 1259that represents the current read request. The object 1260is used to gain access to the <code>ArrayBuffer</code>/<code>TypedArray</code> 1261that has been provided for the read request to fill, 1262and provides methods for signaling that the data has 1263been provided.</p> 1264<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> 1265<div class="api_metadata"> 1266<span>Added in: v16.5.0</span> 1267</div> 1268<ul> 1269<li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1270</ul> 1271<p>Signals that a <code>bytesWritten</code> number of bytes have been written 1272to <code>readableStreamBYOBRequest.view</code>.</p> 1273<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> 1274<div class="api_metadata"> 1275<span>Added in: v16.5.0</span> 1276</div> 1277<ul> 1278<li><code>view</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 1279</ul> 1280<p>Signals that the request has been fulfilled with bytes written 1281to a new <code>Buffer</code>, <code>TypedArray</code>, or <code>DataView</code>.</p> 1282<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> 1283<div class="api_metadata"> 1284<span>Added in: v16.5.0</span> 1285</div> 1286<ul> 1287<li>Type: <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 1288</ul> 1289<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> 1290<div class="api_metadata"> 1291<details class="changelog"><summary>History</summary> 1292<table> 1293<tbody><tr><th>Version</th><th>Changes</th></tr> 1294<tr><td>v18.0.0</td> 1295<td><p>This class is now exposed on the global object.</p></td></tr> 1296<tr><td>v16.5.0</td> 1297<td><p><span>Added in: v16.5.0</span></p></td></tr> 1298</tbody></table> 1299</details> 1300</div> 1301<p>The <code>WritableStream</code> is a destination to which stream data is sent.</p> 1302<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { 1303 <span class="hljs-title class_">WritableStream</span>, 1304} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:stream/web'</span>; 1305 1306<span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WritableStream</span>({ 1307 <span class="hljs-title function_">write</span>(<span class="hljs-params">chunk</span>) { 1308 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(chunk); 1309 }, 1310}); 1311 1312<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> 1313<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> 1314<div class="api_metadata"> 1315<span>Added in: v16.5.0</span> 1316</div> 1317<ul> 1318<li><code>underlyingSink</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1319<ul> 1320<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> A user-defined function that is invoked immediately when 1321the <code>WritableStream</code> is created. 1322<ul> 1323<li><code>controller</code> <a href="webstreams.html#class-writablestreamdefaultcontroller" class="type"><WritableStreamDefaultController></a></li> 1324<li>Returns: <code>undefined</code> or a promise fulfilled with <code>undefined</code>.</li> 1325</ul> 1326</li> 1327<li><code>write</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> A user-defined function that is invoked when a chunk of 1328data has been written to the <code>WritableStream</code>. 1329<ul> 1330<li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1331<li><code>controller</code> <a href="webstreams.html#class-writablestreamdefaultcontroller" class="type"><WritableStreamDefaultController></a></li> 1332<li>Returns: A promise fulfilled with <code>undefined</code>.</li> 1333</ul> 1334</li> 1335<li><code>close</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> A user-defined function that is called when the 1336<code>WritableStream</code> is closed. 1337<ul> 1338<li>Returns: A promise fulfilled with <code>undefined</code>.</li> 1339</ul> 1340</li> 1341<li><code>abort</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> A user-defined function that is called to abruptly close 1342the <code>WritableStream</code>. 1343<ul> 1344<li><code>reason</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1345<li>Returns: A promise fulfilled with <code>undefined</code>.</li> 1346</ul> 1347</li> 1348<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> The <code>type</code> option is reserved for future use and <em>must</em> be 1349undefined.</li> 1350</ul> 1351</li> 1352<li><code>strategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1353<ul> 1354<li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The maximum internal queue size before backpressure 1355is applied.</li> 1356<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> A user-defined function used to identify the size of each 1357chunk of data. 1358<ul> 1359<li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1360<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1361</ul> 1362</li> 1363</ul> 1364</li> 1365</ul> 1366<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> 1367<div class="api_metadata"> 1368<span>Added in: v16.5.0</span> 1369</div> 1370<ul> 1371<li><code>reason</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1372<li>Returns: A promise fulfilled with <code>undefined</code>.</li> 1373</ul> 1374<p>Abruptly terminates the <code>WritableStream</code>. All queued writes will be 1375canceled with their associated promises rejected.</p> 1376<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> 1377<div class="api_metadata"> 1378<span>Added in: v16.5.0</span> 1379</div> 1380<ul> 1381<li>Returns: A promise fulfilled with <code>undefined</code>.</li> 1382</ul> 1383<p>Closes the <code>WritableStream</code> when no additional writes are expected.</p> 1384<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> 1385<div class="api_metadata"> 1386<span>Added in: v16.5.0</span> 1387</div> 1388<ul> 1389<li>Returns: <a href="webstreams.html#class-writablestreamdefaultwriter" class="type"><WritableStreamDefaultWriter></a></li> 1390</ul> 1391<p>Creates and returns a new writer instance that can be used to write 1392data into the <code>WritableStream</code>.</p> 1393<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> 1394<div class="api_metadata"> 1395<span>Added in: v16.5.0</span> 1396</div> 1397<ul> 1398<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1399</ul> 1400<p>The <code>writableStream.locked</code> property is <code>false</code> by default, and is 1401switched to <code>true</code> while there is an active writer attached to this 1402<code>WritableStream</code>.</p> 1403<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> 1404<p>A <a href="webstreams.html#class-writablestream" class="type"><WritableStream></a> instance can be transferred using a <a href="worker_threads.html#class-messageport" class="type"><MessagePort></a>.</p> 1405<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>()); 1406 1407<span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); 1408 1409port1.<span class="hljs-property">onmessage</span> = <span class="hljs-function">(<span class="hljs-params">{ data }</span>) =></span> { 1410 data.<span class="hljs-title function_">getWriter</span>().<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello'</span>); 1411}; 1412 1413port2.<span class="hljs-title function_">postMessage</span>(stream, [stream]);</code> <button class="copy-button">copy</button></pre> 1414<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> 1415<div class="api_metadata"> 1416<details class="changelog"><summary>History</summary> 1417<table> 1418<tbody><tr><th>Version</th><th>Changes</th></tr> 1419<tr><td>v18.0.0</td> 1420<td><p>This class is now exposed on the global object.</p></td></tr> 1421<tr><td>v16.5.0</td> 1422<td><p><span>Added in: v16.5.0</span></p></td></tr> 1423</tbody></table> 1424</details> 1425</div> 1426<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> 1427<div class="api_metadata"> 1428<span>Added in: v16.5.0</span> 1429</div> 1430<ul> 1431<li><code>stream</code> <a href="webstreams.html#class-writablestream" class="type"><WritableStream></a></li> 1432</ul> 1433<p>Creates a new <code>WritableStreamDefaultWriter</code> that is locked to the given 1434<code>WritableStream</code>.</p> 1435<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> 1436<div class="api_metadata"> 1437<span>Added in: v16.5.0</span> 1438</div> 1439<ul> 1440<li><code>reason</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1441<li>Returns: A promise fulfilled with <code>undefined</code>.</li> 1442</ul> 1443<p>Abruptly terminates the <code>WritableStream</code>. All queued writes will be 1444canceled with their associated promises rejected.</p> 1445<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> 1446<div class="api_metadata"> 1447<span>Added in: v16.5.0</span> 1448</div> 1449<ul> 1450<li>Returns: A promise fulfilled with <code>undefined</code>.</li> 1451</ul> 1452<p>Closes the <code>WritableStream</code> when no additional writes are expected.</p> 1453<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> 1454<div class="api_metadata"> 1455<span>Added in: v16.5.0</span> 1456</div> 1457<ul> 1458<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfilled with <code>undefined</code> when the associated 1459<a href="webstreams.html#class-writablestream" class="type"><WritableStream></a> is closed or rejected if the stream errors or the writer's 1460lock is released before the stream finishes closing.</li> 1461</ul> 1462<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> 1463<div class="api_metadata"> 1464<span>Added in: v16.5.0</span> 1465</div> 1466<ul> 1467<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1468</ul> 1469<p>The amount of data required to fill the <a href="webstreams.html#class-writablestream" class="type"><WritableStream></a>'s queue.</p> 1470<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> 1471<div class="api_metadata"> 1472<span>Added in: v16.5.0</span> 1473</div> 1474<ul> 1475<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfilled with <code>undefined</code> when the writer is ready 1476to be used.</li> 1477</ul> 1478<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> 1479<div class="api_metadata"> 1480<span>Added in: v16.5.0</span> 1481</div> 1482<p>Releases this writer's lock on the underlying <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a>.</p> 1483<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> 1484<div class="api_metadata"> 1485<span>Added in: v16.5.0</span> 1486</div> 1487<ul> 1488<li><code>chunk</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1489<li>Returns: A promise fulfilled with <code>undefined</code>.</li> 1490</ul> 1491<p>Appends a new chunk of data to the <a href="webstreams.html#class-writablestream" class="type"><WritableStream></a>'s queue.</p> 1492<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> 1493<div class="api_metadata"> 1494<details class="changelog"><summary>History</summary> 1495<table> 1496<tbody><tr><th>Version</th><th>Changes</th></tr> 1497<tr><td>v18.0.0</td> 1498<td><p>This class is now exposed on the global object.</p></td></tr> 1499<tr><td>v16.5.0</td> 1500<td><p><span>Added in: v16.5.0</span></p></td></tr> 1501</tbody></table> 1502</details> 1503</div> 1504<p>The <code>WritableStreamDefaultController</code> manage's the <a href="webstreams.html#class-writablestream" class="type"><WritableStream></a>'s 1505internal state.</p> 1506<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> 1507<div class="api_metadata"> 1508<span>Added in: v16.5.0</span> 1509</div> 1510<ul> 1511<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1512</ul> 1513<p>Called by user-code to signal that an error has occurred while processing 1514the <code>WritableStream</code> data. When called, the <a href="webstreams.html#class-writablestream" class="type"><WritableStream></a> will be aborted, 1515with currently pending writes canceled.</p> 1516<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> 1517<ul> 1518<li>Type: <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> An <code>AbortSignal</code> that can be used to cancel pending 1519write or close operations when a <a href="webstreams.html#class-writablestream" class="type"><WritableStream></a> is aborted.</li> 1520</ul> 1521<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> 1522<div class="api_metadata"> 1523<details class="changelog"><summary>History</summary> 1524<table> 1525<tbody><tr><th>Version</th><th>Changes</th></tr> 1526<tr><td>v18.0.0</td> 1527<td><p>This class is now exposed on the global object.</p></td></tr> 1528<tr><td>v16.5.0</td> 1529<td><p><span>Added in: v16.5.0</span></p></td></tr> 1530</tbody></table> 1531</details> 1532</div> 1533<p>A <code>TransformStream</code> consists of a <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> and a <a href="webstreams.html#class-writablestream" class="type"><WritableStream></a> that 1534are connected such that the data written to the <code>WritableStream</code> is received, 1535and potentially transformed, before being pushed into the <code>ReadableStream</code>'s 1536queue.</p> 1537<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { 1538 <span class="hljs-title class_">TransformStream</span>, 1539} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:stream/web'</span>; 1540 1541<span class="hljs-keyword">const</span> transform = <span class="hljs-keyword">new</span> <span class="hljs-title class_">TransformStream</span>({ 1542 <span class="hljs-title function_">transform</span>(<span class="hljs-params">chunk, controller</span>) { 1543 controller.<span class="hljs-title function_">enqueue</span>(chunk.<span class="hljs-title function_">toUpperCase</span>()); 1544 }, 1545}); 1546 1547<span class="hljs-keyword">await</span> <span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">all</span>([ 1548 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>), 1549 transform.<span class="hljs-property">readable</span>.<span class="hljs-title function_">getReader</span>().<span class="hljs-title function_">read</span>(), 1550]);</code> <button class="copy-button">copy</button></pre> 1551<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> 1552<div class="api_metadata"> 1553<span>Added in: v16.5.0</span> 1554</div> 1555<ul> 1556<li><code>transformer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1557<ul> 1558<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> A user-defined function that is invoked immediately when 1559the <code>TransformStream</code> is created. 1560<ul> 1561<li><code>controller</code> <a href="webstreams.html#class-transformstreamdefaultcontroller" class="type"><TransformStreamDefaultController></a></li> 1562<li>Returns: <code>undefined</code> or a promise fulfilled with <code>undefined</code></li> 1563</ul> 1564</li> 1565<li><code>transform</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> A user-defined function that receives, and 1566potentially modifies, a chunk of data written to <code>transformStream.writable</code>, 1567before forwarding that on to <code>transformStream.readable</code>. 1568<ul> 1569<li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1570<li><code>controller</code> <a href="webstreams.html#class-transformstreamdefaultcontroller" class="type"><TransformStreamDefaultController></a></li> 1571<li>Returns: A promise fulfilled with <code>undefined</code>.</li> 1572</ul> 1573</li> 1574<li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> A user-defined function that is called immediately before 1575the writable side of the <code>TransformStream</code> is closed, signaling the end of 1576the transformation process. 1577<ul> 1578<li><code>controller</code> <a href="webstreams.html#class-transformstreamdefaultcontroller" class="type"><TransformStreamDefaultController></a></li> 1579<li>Returns: A promise fulfilled with <code>undefined</code>.</li> 1580</ul> 1581</li> 1582<li><code>readableType</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> the <code>readableType</code> option is reserved for future use 1583and <em>must</em> be <code>undefined</code>.</li> 1584<li><code>writableType</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> the <code>writableType</code> option is reserved for future use 1585and <em>must</em> be <code>undefined</code>.</li> 1586</ul> 1587</li> 1588<li><code>writableStrategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1589<ul> 1590<li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The maximum internal queue size before backpressure 1591is applied.</li> 1592<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> A user-defined function used to identify the size of each 1593chunk of data. 1594<ul> 1595<li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1596<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1597</ul> 1598</li> 1599</ul> 1600</li> 1601<li><code>readableStrategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1602<ul> 1603<li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The maximum internal queue size before backpressure 1604is applied.</li> 1605<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> A user-defined function used to identify the size of each 1606chunk of data. 1607<ul> 1608<li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1609<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1610</ul> 1611</li> 1612</ul> 1613</li> 1614</ul> 1615<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> 1616<div class="api_metadata"> 1617<span>Added in: v16.5.0</span> 1618</div> 1619<ul> 1620<li>Type: <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a></li> 1621</ul> 1622<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> 1623<div class="api_metadata"> 1624<span>Added in: v16.5.0</span> 1625</div> 1626<ul> 1627<li>Type: <a href="webstreams.html#class-writablestream" class="type"><WritableStream></a></li> 1628</ul> 1629<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> 1630<p>A <a href="webstreams.html#class-transformstream" class="type"><TransformStream></a> instance can be transferred using a <a href="worker_threads.html#class-messageport" class="type"><MessagePort></a>.</p> 1631<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>(); 1632 1633<span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); 1634 1635port1.<span class="hljs-property">onmessage</span> = <span class="hljs-function">(<span class="hljs-params">{ data }</span>) =></span> { 1636 <span class="hljs-keyword">const</span> { writable, readable } = data; 1637 <span class="hljs-comment">// ...</span> 1638}; 1639 1640port2.<span class="hljs-title function_">postMessage</span>(stream, [stream]);</code> <button class="copy-button">copy</button></pre> 1641<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> 1642<div class="api_metadata"> 1643<details class="changelog"><summary>History</summary> 1644<table> 1645<tbody><tr><th>Version</th><th>Changes</th></tr> 1646<tr><td>v18.0.0</td> 1647<td><p>This class is now exposed on the global object.</p></td></tr> 1648<tr><td>v16.5.0</td> 1649<td><p><span>Added in: v16.5.0</span></p></td></tr> 1650</tbody></table> 1651</details> 1652</div> 1653<p>The <code>TransformStreamDefaultController</code> manages the internal state 1654of the <code>TransformStream</code>.</p> 1655<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> 1656<div class="api_metadata"> 1657<span>Added in: v16.5.0</span> 1658</div> 1659<ul> 1660<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1661</ul> 1662<p>The amount of data required to fill the readable side's queue.</p> 1663<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> 1664<div class="api_metadata"> 1665<span>Added in: v16.5.0</span> 1666</div> 1667<ul> 1668<li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1669</ul> 1670<p>Appends a chunk of data to the readable side's queue.</p> 1671<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> 1672<div class="api_metadata"> 1673<span>Added in: v16.5.0</span> 1674</div> 1675<ul> 1676<li><code>reason</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1677</ul> 1678<p>Signals to both the readable and writable side that an error has occurred 1679while processing the transform data, causing both sides to be abruptly 1680closed.</p> 1681<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> 1682<div class="api_metadata"> 1683<span>Added in: v16.5.0</span> 1684</div> 1685<p>Closes the readable side of the transport and causes the writable side 1686to be abruptly closed with an error.</p> 1687<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> 1688<div class="api_metadata"> 1689<details class="changelog"><summary>History</summary> 1690<table> 1691<tbody><tr><th>Version</th><th>Changes</th></tr> 1692<tr><td>v18.0.0</td> 1693<td><p>This class is now exposed on the global object.</p></td></tr> 1694<tr><td>v16.5.0</td> 1695<td><p><span>Added in: v16.5.0</span></p></td></tr> 1696</tbody></table> 1697</details> 1698</div> 1699<h5><code>new ByteLengthQueuingStrategy(init)</code><span><a class="mark" href="#new-bytelengthqueuingstrategyinit" id="new-bytelengthqueuingstrategyinit">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_new_bytelengthqueuingstrategy_init"></a></h5> 1700<div class="api_metadata"> 1701<span>Added in: v16.5.0</span> 1702</div> 1703<ul> 1704<li><code>init</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1705<ul> 1706<li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1707</ul> 1708</li> 1709</ul> 1710<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> 1711<div class="api_metadata"> 1712<span>Added in: v16.5.0</span> 1713</div> 1714<ul> 1715<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1716</ul> 1717<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> 1718<div class="api_metadata"> 1719<span>Added in: v16.5.0</span> 1720</div> 1721<ul> 1722<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 1723<ul> 1724<li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1725<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1726</ul> 1727</li> 1728</ul> 1729<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> 1730<div class="api_metadata"> 1731<details class="changelog"><summary>History</summary> 1732<table> 1733<tbody><tr><th>Version</th><th>Changes</th></tr> 1734<tr><td>v18.0.0</td> 1735<td><p>This class is now exposed on the global object.</p></td></tr> 1736<tr><td>v16.5.0</td> 1737<td><p><span>Added in: v16.5.0</span></p></td></tr> 1738</tbody></table> 1739</details> 1740</div> 1741<h5><code>new CountQueuingStrategy(init)</code><span><a class="mark" href="#new-countqueuingstrategyinit" id="new-countqueuingstrategyinit">#</a></span><a aria-hidden="true" class="legacy" id="webstreams_new_countqueuingstrategy_init"></a></h5> 1742<div class="api_metadata"> 1743<span>Added in: v16.5.0</span> 1744</div> 1745<ul> 1746<li><code>init</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1747<ul> 1748<li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1749</ul> 1750</li> 1751</ul> 1752<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> 1753<div class="api_metadata"> 1754<span>Added in: v16.5.0</span> 1755</div> 1756<ul> 1757<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1758</ul> 1759<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> 1760<div class="api_metadata"> 1761<span>Added in: v16.5.0</span> 1762</div> 1763<ul> 1764<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 1765<ul> 1766<li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1767<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1768</ul> 1769</li> 1770</ul> 1771<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> 1772<div class="api_metadata"> 1773<details class="changelog"><summary>History</summary> 1774<table> 1775<tbody><tr><th>Version</th><th>Changes</th></tr> 1776<tr><td>v18.0.0</td> 1777<td><p>This class is now exposed on the global object.</p></td></tr> 1778<tr><td>v16.6.0</td> 1779<td><p><span>Added in: v16.6.0</span></p></td></tr> 1780</tbody></table> 1781</details> 1782</div> 1783<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> 1784<div class="api_metadata"> 1785<span>Added in: v16.6.0</span> 1786</div> 1787<p>Creates a new <code>TextEncoderStream</code> instance.</p> 1788<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> 1789<div class="api_metadata"> 1790<span>Added in: v16.6.0</span> 1791</div> 1792<ul> 1793<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1794</ul> 1795<p>The encoding supported by the <code>TextEncoderStream</code> instance.</p> 1796<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> 1797<div class="api_metadata"> 1798<span>Added in: v16.6.0</span> 1799</div> 1800<ul> 1801<li>Type: <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a></li> 1802</ul> 1803<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> 1804<div class="api_metadata"> 1805<span>Added in: v16.6.0</span> 1806</div> 1807<ul> 1808<li>Type: <a href="webstreams.html#class-writablestream" class="type"><WritableStream></a></li> 1809</ul> 1810<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> 1811<div class="api_metadata"> 1812<details class="changelog"><summary>History</summary> 1813<table> 1814<tbody><tr><th>Version</th><th>Changes</th></tr> 1815<tr><td>v18.0.0</td> 1816<td><p>This class is now exposed on the global object.</p></td></tr> 1817<tr><td>v16.6.0</td> 1818<td><p><span>Added in: v16.6.0</span></p></td></tr> 1819</tbody></table> 1820</details> 1821</div> 1822<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> 1823<div class="api_metadata"> 1824<span>Added in: v16.6.0</span> 1825</div> 1826<ul> 1827<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Identifies the <code>encoding</code> that this <code>TextDecoder</code> instance 1828supports. <strong>Default:</strong> <code>'utf-8'</code>.</li> 1829<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1830<ul> 1831<li><code>fatal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if decoding failures are fatal.</li> 1832<li><code>ignoreBOM</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, the <code>TextDecoderStream</code> will include the 1833byte order mark in the decoded result. When <code>false</code>, the byte order mark 1834will be removed from the output. This option is only used when <code>encoding</code> is 1835<code>'utf-8'</code>, <code>'utf-16be'</code>, or <code>'utf-16le'</code>. <strong>Default:</strong> <code>false</code>.</li> 1836</ul> 1837</li> 1838</ul> 1839<p>Creates a new <code>TextDecoderStream</code> instance.</p> 1840<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> 1841<div class="api_metadata"> 1842<span>Added in: v16.6.0</span> 1843</div> 1844<ul> 1845<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1846</ul> 1847<p>The encoding supported by the <code>TextDecoderStream</code> instance.</p> 1848<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> 1849<div class="api_metadata"> 1850<span>Added in: v16.6.0</span> 1851</div> 1852<ul> 1853<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1854</ul> 1855<p>The value will be <code>true</code> if decoding errors result in a <code>TypeError</code> being 1856thrown.</p> 1857<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> 1858<div class="api_metadata"> 1859<span>Added in: v16.6.0</span> 1860</div> 1861<ul> 1862<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1863</ul> 1864<p>The value will be <code>true</code> if the decoding result will include the byte order 1865mark.</p> 1866<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> 1867<div class="api_metadata"> 1868<span>Added in: v16.6.0</span> 1869</div> 1870<ul> 1871<li>Type: <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a></li> 1872</ul> 1873<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> 1874<div class="api_metadata"> 1875<span>Added in: v16.6.0</span> 1876</div> 1877<ul> 1878<li>Type: <a href="webstreams.html#class-writablestream" class="type"><WritableStream></a></li> 1879</ul> 1880<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> 1881<div class="api_metadata"> 1882<details class="changelog"><summary>History</summary> 1883<table> 1884<tbody><tr><th>Version</th><th>Changes</th></tr> 1885<tr><td>v18.0.0</td> 1886<td><p>This class is now exposed on the global object.</p></td></tr> 1887<tr><td>v17.0.0</td> 1888<td><p><span>Added in: v17.0.0</span></p></td></tr> 1889</tbody></table> 1890</details> 1891</div> 1892<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> 1893<div class="api_metadata"> 1894<span>Added in: v17.0.0</span> 1895</div> 1896<ul> 1897<li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> One of either <code>'deflate'</code> or <code>'gzip'</code>.</li> 1898</ul> 1899<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> 1900<div class="api_metadata"> 1901<span>Added in: v17.0.0</span> 1902</div> 1903<ul> 1904<li>Type: <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a></li> 1905</ul> 1906<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> 1907<div class="api_metadata"> 1908<span>Added in: v17.0.0</span> 1909</div> 1910<ul> 1911<li>Type: <a href="webstreams.html#class-writablestream" class="type"><WritableStream></a></li> 1912</ul> 1913<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> 1914<div class="api_metadata"> 1915<details class="changelog"><summary>History</summary> 1916<table> 1917<tbody><tr><th>Version</th><th>Changes</th></tr> 1918<tr><td>v18.0.0</td> 1919<td><p>This class is now exposed on the global object.</p></td></tr> 1920<tr><td>v17.0.0</td> 1921<td><p><span>Added in: v17.0.0</span></p></td></tr> 1922</tbody></table> 1923</details> 1924</div> 1925<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> 1926<div class="api_metadata"> 1927<span>Added in: v17.0.0</span> 1928</div> 1929<ul> 1930<li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> One of either <code>'deflate'</code> or <code>'gzip'</code>.</li> 1931</ul> 1932<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> 1933<div class="api_metadata"> 1934<span>Added in: v17.0.0</span> 1935</div> 1936<ul> 1937<li>Type: <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a></li> 1938</ul> 1939<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> 1940<div class="api_metadata"> 1941<span>Added in: v17.0.0</span> 1942</div> 1943<ul> 1944<li>Type: <a href="webstreams.html#class-writablestream" class="type"><WritableStream></a></li> 1945</ul> 1946<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> 1947<div class="api_metadata"> 1948<span>Added in: v16.7.0</span> 1949</div> 1950<p>The utility consumer functions provide common options for consuming 1951streams.</p> 1952<p>They are accessed using:</p> 1953 1954<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> { 1955 arrayBuffer, 1956 blob, 1957 buffer, 1958 json, 1959 text, 1960} <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> { 1961 arrayBuffer, 1962 blob, 1963 buffer, 1964 json, 1965 text, 1966} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:stream/consumers'</span>);</code><button class="copy-button">copy</button></pre> 1967<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> 1968<div class="api_metadata"> 1969<span>Added in: v16.7.0</span> 1970</div> 1971<ul> 1972<li><code>stream</code> <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> | <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a> | <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type"><AsyncIterator></a></li> 1973<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with an <code>ArrayBuffer</code> containing the full 1974contents of the stream.</li> 1975</ul> 1976 1977<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>; 1978<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>; 1979<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>; 1980 1981<span class="hljs-keyword">const</span> encoder = <span class="hljs-keyword">new</span> <span class="hljs-title class_">TextEncoder</span>(); 1982<span class="hljs-keyword">const</span> dataArray = encoder.<span class="hljs-title function_">encode</span>(<span class="hljs-string">'hello world from consumers!'</span>); 1983 1984<span class="hljs-keyword">const</span> readable = <span class="hljs-title class_">Readable</span>.<span class="hljs-title function_">from</span>(dataArray); 1985<span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> <span class="hljs-title function_">arrayBuffer</span>(readable); 1986<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>); 1987<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>); 1988<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>); 1989 1990<span class="hljs-keyword">const</span> encoder = <span class="hljs-keyword">new</span> <span class="hljs-title class_">TextEncoder</span>(); 1991<span class="hljs-keyword">const</span> dataArray = encoder.<span class="hljs-title function_">encode</span>(<span class="hljs-string">'hello world from consumers!'</span>); 1992<span class="hljs-keyword">const</span> readable = <span class="hljs-title class_">Readable</span>.<span class="hljs-title function_">from</span>(dataArray); 1993<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> { 1994 <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>); 1995});</code><button class="copy-button">copy</button></pre> 1996<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> 1997<div class="api_metadata"> 1998<span>Added in: v16.7.0</span> 1999</div> 2000<ul> 2001<li><code>stream</code> <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> | <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a> | <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type"><AsyncIterator></a></li> 2002<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with a <a href="buffer.html#class-blob" class="type"><Blob></a> containing the full contents 2003of the stream.</li> 2004</ul> 2005 2006<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>; 2007 2008<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>]); 2009 2010<span class="hljs-keyword">const</span> readable = dataBlob.<span class="hljs-title function_">stream</span>(); 2011<span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> <span class="hljs-title function_">blob</span>(readable); 2012<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>); 2013 2014<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>]); 2015 2016<span class="hljs-keyword">const</span> readable = dataBlob.<span class="hljs-title function_">stream</span>(); 2017<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> { 2018 <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>); 2019});</code><button class="copy-button">copy</button></pre> 2020<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> 2021<div class="api_metadata"> 2022<span>Added in: v16.7.0</span> 2023</div> 2024<ul> 2025<li><code>stream</code> <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> | <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a> | <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type"><AsyncIterator></a></li> 2026<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with a <a href="buffer.html#class-buffer" class="type"><Buffer></a> containing the full 2027contents of the stream.</li> 2028</ul> 2029 2030<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>; 2031<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>; 2032<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>; 2033 2034<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>); 2035 2036<span class="hljs-keyword">const</span> readable = <span class="hljs-title class_">Readable</span>.<span class="hljs-title function_">from</span>(dataBuffer); 2037<span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> <span class="hljs-title function_">buffer</span>(readable); 2038<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>); 2039<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>); 2040<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>); 2041 2042<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>); 2043 2044<span class="hljs-keyword">const</span> readable = <span class="hljs-title class_">Readable</span>.<span class="hljs-title function_">from</span>(dataBuffer); 2045<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> { 2046 <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>); 2047});</code><button class="copy-button">copy</button></pre> 2048<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> 2049<div class="api_metadata"> 2050<span>Added in: v16.7.0</span> 2051</div> 2052<ul> 2053<li><code>stream</code> <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> | <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a> | <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type"><AsyncIterator></a></li> 2054<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with the contents of the stream parsed as a 2055UTF-8 encoded string that is then passed through <code>JSON.parse()</code>.</li> 2056</ul> 2057 2058<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>; 2059<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>; 2060 2061<span class="hljs-keyword">const</span> items = <span class="hljs-title class_">Array</span>.<span class="hljs-title function_">from</span>( 2062 { 2063 <span class="hljs-attr">length</span>: <span class="hljs-number">100</span>, 2064 }, 2065 <span class="hljs-function">() =></span> ({ 2066 <span class="hljs-attr">message</span>: <span class="hljs-string">'hello world from consumers!'</span>, 2067 }), 2068); 2069 2070<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)); 2071<span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> <span class="hljs-title function_">json</span>(readable); 2072<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>); 2073<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>); 2074 2075<span class="hljs-keyword">const</span> items = <span class="hljs-title class_">Array</span>.<span class="hljs-title function_">from</span>( 2076 { 2077 <span class="hljs-attr">length</span>: <span class="hljs-number">100</span>, 2078 }, 2079 <span class="hljs-function">() =></span> ({ 2080 <span class="hljs-attr">message</span>: <span class="hljs-string">'hello world from consumers!'</span>, 2081 }), 2082); 2083 2084<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)); 2085<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> { 2086 <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>); 2087});</code><button class="copy-button">copy</button></pre> 2088<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> 2089<div class="api_metadata"> 2090<span>Added in: v16.7.0</span> 2091</div> 2092<ul> 2093<li><code>stream</code> <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a> | <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a> | <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type"><AsyncIterator></a></li> 2094<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with the contents of the stream parsed as a 2095UTF-8 encoded string.</li> 2096</ul> 2097 2098<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>; 2099<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>; 2100 2101<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>); 2102<span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> <span class="hljs-title function_">text</span>(readable); 2103<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>); 2104<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>); 2105 2106<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>); 2107<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> { 2108 <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>); 2109});</code><button class="copy-button">copy</button></pre></section> 2110 <!-- API END --> 2111 </div> 2112 </div> 2113 </div> 2114</body> 2115</html> 2116