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>Buffer | 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/buffer.html"> 12 <script async defer src="assets/api.js" type="text/javascript"></script> 13 <style>@media(max-width:558px){.with-42-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:718px){.with-62-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:646px){.with-53-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}</style> 14</head> 15<body class="alt apidoc" id="api-section-buffer"> 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 active">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">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="buffer" 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_2"><a href="#buffer">Buffer</a></span> 124<ul> 125<li><a href="#buffers-and-character-encodings">Buffers and character encodings</a></li> 126<li><a href="#buffers-and-typedarrays">Buffers and TypedArrays</a></li> 127<li><a href="#buffers-and-iteration">Buffers and iteration</a></li> 128<li><a href="#class-blob">Class: <code>Blob</code></a> 129<ul> 130<li><a href="#new-bufferblobsources-options"><code>new buffer.Blob([sources[, options]])</code></a></li> 131<li><a href="#blobarraybuffer"><code>blob.arrayBuffer()</code></a></li> 132<li><a href="#blobsize"><code>blob.size</code></a></li> 133<li><a href="#blobslicestart-end-type"><code>blob.slice([start[, end[, type]]])</code></a></li> 134<li><a href="#blobstream"><code>blob.stream()</code></a></li> 135<li><a href="#blobtext"><code>blob.text()</code></a></li> 136<li><a href="#blobtype"><code>blob.type</code></a></li> 137<li><a href="#blob-objects-and-messagechannel"><code>Blob</code> objects and <code>MessageChannel</code></a></li> 138</ul> 139</li> 140<li><a href="#class-buffer">Class: <code>Buffer</code></a> 141<ul> 142<li><a href="#static-method-bufferallocsize-fill-encoding">Static method: <code>Buffer.alloc(size[, fill[, encoding]])</code></a></li> 143<li><a href="#static-method-bufferallocunsafesize">Static method: <code>Buffer.allocUnsafe(size)</code></a></li> 144<li><a href="#static-method-bufferallocunsafeslowsize">Static method: <code>Buffer.allocUnsafeSlow(size)</code></a></li> 145<li><a href="#static-method-bufferbytelengthstring-encoding">Static method: <code>Buffer.byteLength(string[, encoding])</code></a></li> 146<li><a href="#static-method-buffercomparebuf1-buf2">Static method: <code>Buffer.compare(buf1, buf2)</code></a></li> 147<li><a href="#static-method-bufferconcatlist-totallength">Static method: <code>Buffer.concat(list[, totalLength])</code></a></li> 148<li><a href="#static-method-buffercopybytesfromview-offset-length">Static method: <code>Buffer.copyBytesFrom(view[, offset[, length]])</code></a></li> 149<li><a href="#static-method-bufferfromarray">Static method: <code>Buffer.from(array)</code></a></li> 150<li><a href="#static-method-bufferfromarraybuffer-byteoffset-length">Static method: <code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a></li> 151<li><a href="#static-method-bufferfrombuffer">Static method: <code>Buffer.from(buffer)</code></a></li> 152<li><a href="#static-method-bufferfromobject-offsetorencoding-length">Static method: <code>Buffer.from(object[, offsetOrEncoding[, length]])</code></a></li> 153<li><a href="#static-method-bufferfromstring-encoding">Static method: <code>Buffer.from(string[, encoding])</code></a></li> 154<li><a href="#static-method-bufferisbufferobj">Static method: <code>Buffer.isBuffer(obj)</code></a></li> 155<li><a href="#static-method-bufferisencodingencoding">Static method: <code>Buffer.isEncoding(encoding)</code></a></li> 156<li><a href="#class-property-bufferpoolsize">Class property: <code>Buffer.poolSize</code></a></li> 157<li><a href="#bufindex"><code>buf[index]</code></a></li> 158<li><a href="#bufbuffer"><code>buf.buffer</code></a></li> 159<li><a href="#bufbyteoffset"><code>buf.byteOffset</code></a></li> 160<li><a href="#bufcomparetarget-targetstart-targetend-sourcestart-sourceend"><code>buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])</code></a></li> 161<li><a href="#bufcopytarget-targetstart-sourcestart-sourceend"><code>buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])</code></a></li> 162<li><a href="#bufentries"><code>buf.entries()</code></a></li> 163<li><a href="#bufequalsotherbuffer"><code>buf.equals(otherBuffer)</code></a></li> 164<li><a href="#buffillvalue-offset-end-encoding"><code>buf.fill(value[, offset[, end]][, encoding])</code></a></li> 165<li><a href="#bufincludesvalue-byteoffset-encoding"><code>buf.includes(value[, byteOffset][, encoding])</code></a></li> 166<li><a href="#bufindexofvalue-byteoffset-encoding"><code>buf.indexOf(value[, byteOffset][, encoding])</code></a></li> 167<li><a href="#bufkeys"><code>buf.keys()</code></a></li> 168<li><a href="#buflastindexofvalue-byteoffset-encoding"><code>buf.lastIndexOf(value[, byteOffset][, encoding])</code></a></li> 169<li><a href="#buflength"><code>buf.length</code></a></li> 170<li><span class="stability_0"><a href="#bufparent"><code>buf.parent</code></a></span></li> 171<li><a href="#bufreadbigint64beoffset"><code>buf.readBigInt64BE([offset])</code></a></li> 172<li><a href="#bufreadbigint64leoffset"><code>buf.readBigInt64LE([offset])</code></a></li> 173<li><a href="#bufreadbiguint64beoffset"><code>buf.readBigUInt64BE([offset])</code></a></li> 174<li><a href="#bufreadbiguint64leoffset"><code>buf.readBigUInt64LE([offset])</code></a></li> 175<li><a href="#bufreaddoublebeoffset"><code>buf.readDoubleBE([offset])</code></a></li> 176<li><a href="#bufreaddoubleleoffset"><code>buf.readDoubleLE([offset])</code></a></li> 177<li><a href="#bufreadfloatbeoffset"><code>buf.readFloatBE([offset])</code></a></li> 178<li><a href="#bufreadfloatleoffset"><code>buf.readFloatLE([offset])</code></a></li> 179<li><a href="#bufreadint8offset"><code>buf.readInt8([offset])</code></a></li> 180<li><a href="#bufreadint16beoffset"><code>buf.readInt16BE([offset])</code></a></li> 181<li><a href="#bufreadint16leoffset"><code>buf.readInt16LE([offset])</code></a></li> 182<li><a href="#bufreadint32beoffset"><code>buf.readInt32BE([offset])</code></a></li> 183<li><a href="#bufreadint32leoffset"><code>buf.readInt32LE([offset])</code></a></li> 184<li><a href="#bufreadintbeoffset-bytelength"><code>buf.readIntBE(offset, byteLength)</code></a></li> 185<li><a href="#bufreadintleoffset-bytelength"><code>buf.readIntLE(offset, byteLength)</code></a></li> 186<li><a href="#bufreaduint8offset"><code>buf.readUInt8([offset])</code></a></li> 187<li><a href="#bufreaduint16beoffset"><code>buf.readUInt16BE([offset])</code></a></li> 188<li><a href="#bufreaduint16leoffset"><code>buf.readUInt16LE([offset])</code></a></li> 189<li><a href="#bufreaduint32beoffset"><code>buf.readUInt32BE([offset])</code></a></li> 190<li><a href="#bufreaduint32leoffset"><code>buf.readUInt32LE([offset])</code></a></li> 191<li><a href="#bufreaduintbeoffset-bytelength"><code>buf.readUIntBE(offset, byteLength)</code></a></li> 192<li><a href="#bufreaduintleoffset-bytelength"><code>buf.readUIntLE(offset, byteLength)</code></a></li> 193<li><a href="#bufsubarraystart-end"><code>buf.subarray([start[, end]])</code></a></li> 194<li><span class="stability_0"><a href="#bufslicestart-end"><code>buf.slice([start[, end]])</code></a></span></li> 195<li><a href="#bufswap16"><code>buf.swap16()</code></a></li> 196<li><a href="#bufswap32"><code>buf.swap32()</code></a></li> 197<li><a href="#bufswap64"><code>buf.swap64()</code></a></li> 198<li><a href="#buftojson"><code>buf.toJSON()</code></a></li> 199<li><a href="#buftostringencoding-start-end"><code>buf.toString([encoding[, start[, end]]])</code></a></li> 200<li><a href="#bufvalues"><code>buf.values()</code></a></li> 201<li><a href="#bufwritestring-offset-length-encoding"><code>buf.write(string[, offset[, length]][, encoding])</code></a></li> 202<li><a href="#bufwritebigint64bevalue-offset"><code>buf.writeBigInt64BE(value[, offset])</code></a></li> 203<li><a href="#bufwritebigint64levalue-offset"><code>buf.writeBigInt64LE(value[, offset])</code></a></li> 204<li><a href="#bufwritebiguint64bevalue-offset"><code>buf.writeBigUInt64BE(value[, offset])</code></a></li> 205<li><a href="#bufwritebiguint64levalue-offset"><code>buf.writeBigUInt64LE(value[, offset])</code></a></li> 206<li><a href="#bufwritedoublebevalue-offset"><code>buf.writeDoubleBE(value[, offset])</code></a></li> 207<li><a href="#bufwritedoublelevalue-offset"><code>buf.writeDoubleLE(value[, offset])</code></a></li> 208<li><a href="#bufwritefloatbevalue-offset"><code>buf.writeFloatBE(value[, offset])</code></a></li> 209<li><a href="#bufwritefloatlevalue-offset"><code>buf.writeFloatLE(value[, offset])</code></a></li> 210<li><a href="#bufwriteint8value-offset"><code>buf.writeInt8(value[, offset])</code></a></li> 211<li><a href="#bufwriteint16bevalue-offset"><code>buf.writeInt16BE(value[, offset])</code></a></li> 212<li><a href="#bufwriteint16levalue-offset"><code>buf.writeInt16LE(value[, offset])</code></a></li> 213<li><a href="#bufwriteint32bevalue-offset"><code>buf.writeInt32BE(value[, offset])</code></a></li> 214<li><a href="#bufwriteint32levalue-offset"><code>buf.writeInt32LE(value[, offset])</code></a></li> 215<li><a href="#bufwriteintbevalue-offset-bytelength"><code>buf.writeIntBE(value, offset, byteLength)</code></a></li> 216<li><a href="#bufwriteintlevalue-offset-bytelength"><code>buf.writeIntLE(value, offset, byteLength)</code></a></li> 217<li><a href="#bufwriteuint8value-offset"><code>buf.writeUInt8(value[, offset])</code></a></li> 218<li><a href="#bufwriteuint16bevalue-offset"><code>buf.writeUInt16BE(value[, offset])</code></a></li> 219<li><a href="#bufwriteuint16levalue-offset"><code>buf.writeUInt16LE(value[, offset])</code></a></li> 220<li><a href="#bufwriteuint32bevalue-offset"><code>buf.writeUInt32BE(value[, offset])</code></a></li> 221<li><a href="#bufwriteuint32levalue-offset"><code>buf.writeUInt32LE(value[, offset])</code></a></li> 222<li><a href="#bufwriteuintbevalue-offset-bytelength"><code>buf.writeUIntBE(value, offset, byteLength)</code></a></li> 223<li><a href="#bufwriteuintlevalue-offset-bytelength"><code>buf.writeUIntLE(value, offset, byteLength)</code></a></li> 224<li><span class="stability_0"><a href="#new-bufferarray"><code>new Buffer(array)</code></a></span></li> 225<li><span class="stability_0"><a href="#new-bufferarraybuffer-byteoffset-length"><code>new Buffer(arrayBuffer[, byteOffset[, length]])</code></a></span></li> 226<li><span class="stability_0"><a href="#new-bufferbuffer"><code>new Buffer(buffer)</code></a></span></li> 227<li><span class="stability_0"><a href="#new-buffersize"><code>new Buffer(size)</code></a></span></li> 228<li><span class="stability_0"><a href="#new-bufferstring-encoding"><code>new Buffer(string[, encoding])</code></a></span></li> 229</ul> 230</li> 231<li><span class="stability_1"><a href="#class-file">Class: <code>File</code></a></span> 232<ul> 233<li><a href="#new-bufferfilesources-filename-options"><code>new buffer.File(sources, fileName[, options])</code></a></li> 234<li><a href="#filename"><code>file.name</code></a></li> 235<li><a href="#filelastmodified"><code>file.lastModified</code></a></li> 236</ul> 237</li> 238<li><a href="#nodebuffer-module-apis"><code>node:buffer</code> module APIs</a> 239<ul> 240<li><span class="stability_3"><a href="#bufferatobdata"><code>buffer.atob(data)</code></a></span></li> 241<li><span class="stability_3"><a href="#bufferbtoadata"><code>buffer.btoa(data)</code></a></span></li> 242<li><a href="#bufferisasciiinput"><code>buffer.isAscii(input)</code></a></li> 243<li><a href="#bufferisutf8input"><code>buffer.isUtf8(input)</code></a></li> 244<li><a href="#bufferinspect_max_bytes"><code>buffer.INSPECT_MAX_BYTES</code></a></li> 245<li><a href="#bufferkmaxlength"><code>buffer.kMaxLength</code></a></li> 246<li><a href="#bufferkstringmaxlength"><code>buffer.kStringMaxLength</code></a></li> 247<li><span class="stability_1"><a href="#bufferresolveobjecturlid"><code>buffer.resolveObjectURL(id)</code></a></span></li> 248<li><a href="#buffertranscodesource-fromenc-toenc"><code>buffer.transcode(source, fromEnc, toEnc)</code></a></li> 249<li><span class="stability_0"><a href="#class-slowbuffer">Class: <code>SlowBuffer</code></a></span> 250<ul> 251<li><span class="stability_0"><a href="#new-slowbuffersize"><code>new SlowBuffer(size)</code></a></span></li> 252</ul> 253</li> 254<li><a href="#buffer-constants">Buffer constants</a> 255<ul> 256<li><a href="#bufferconstantsmax_length"><code>buffer.constants.MAX_LENGTH</code></a></li> 257<li><a href="#bufferconstantsmax_string_length"><code>buffer.constants.MAX_STRING_LENGTH</code></a></li> 258</ul> 259</li> 260</ul> 261</li> 262<li><a href="#bufferfrom-bufferalloc-and-bufferallocunsafe"><code>Buffer.from()</code>, <code>Buffer.alloc()</code>, and <code>Buffer.allocUnsafe()</code></a> 263<ul> 264<li><a href="#the---zero-fill-buffers-command-line-option">The <code>--zero-fill-buffers</code> command-line option</a></li> 265<li><a href="#what-makes-bufferallocunsafe-and-bufferallocunsafeslow-unsafe">What makes <code>Buffer.allocUnsafe()</code> and <code>Buffer.allocUnsafeSlow()</code> "unsafe"?</a></li> 266</ul> 267</li> 268</ul> 269</li> 270</ul></div></div> 271 </li> 272 273 274 <li class="picker-header"> 275 <a href="#"> 276 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 277 Index 278 </a> 279 280 <div class="picker"><ul> 281<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 282<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 283 284 <li> 285 <a href="index.html">Index</a> 286 </li> 287 </ul> 288 289<hr class="line"> 290<ul> 291<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 292<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 293<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 294<li><a href="buffer.html" class="nav-buffer active">Buffer</a></li> 295<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 296<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 297<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 298<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 299<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 300<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 301<li><a href="console.html" class="nav-console">Console</a></li> 302<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 303<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 304<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 305<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 306<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 307<li><a href="dns.html" class="nav-dns">DNS</a></li> 308<li><a href="domain.html" class="nav-domain">Domain</a></li> 309<li><a href="errors.html" class="nav-errors">Errors</a></li> 310<li><a href="events.html" class="nav-events">Events</a></li> 311<li><a href="fs.html" class="nav-fs">File system</a></li> 312<li><a href="globals.html" class="nav-globals">Globals</a></li> 313<li><a href="http.html" class="nav-http">HTTP</a></li> 314<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 315<li><a href="https.html" class="nav-https">HTTPS</a></li> 316<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 317<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 318<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 319<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 320<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 321<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 322<li><a href="net.html" class="nav-net">Net</a></li> 323<li><a href="os.html" class="nav-os">OS</a></li> 324<li><a href="path.html" class="nav-path">Path</a></li> 325<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 326<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 327<li><a href="process.html" class="nav-process">Process</a></li> 328<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 329<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 330<li><a href="readline.html" class="nav-readline">Readline</a></li> 331<li><a href="repl.html" class="nav-repl">REPL</a></li> 332<li><a href="report.html" class="nav-report">Report</a></li> 333<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 334<li><a href="stream.html" class="nav-stream">Stream</a></li> 335<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 336<li><a href="test.html" class="nav-test">Test runner</a></li> 337<li><a href="timers.html" class="nav-timers">Timers</a></li> 338<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 339<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 340<li><a href="tty.html" class="nav-tty">TTY</a></li> 341<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 342<li><a href="url.html" class="nav-url">URL</a></li> 343<li><a href="util.html" class="nav-util">Utilities</a></li> 344<li><a href="v8.html" class="nav-v8">V8</a></li> 345<li><a href="vm.html" class="nav-vm">VM</a></li> 346<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 347<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 348<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 349<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 350<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 351</ul> 352<hr class="line"> 353<ul> 354<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 355</ul></div> 356 </li> 357 358 359 <li class="picker-header"> 360 <a href="#"> 361 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 362 Other versions 363 </a> 364 <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/buffer.html">21.x</a></li> 365<li><a href="https://nodejs.org/docs/latest-v20.x/api/buffer.html">20.x <b>LTS</b></a></li> 366<li><a href="https://nodejs.org/docs/latest-v19.x/api/buffer.html">19.x</a></li> 367<li><a href="https://nodejs.org/docs/latest-v18.x/api/buffer.html">18.x <b>LTS</b></a></li> 368<li><a href="https://nodejs.org/docs/latest-v17.x/api/buffer.html">17.x</a></li> 369<li><a href="https://nodejs.org/docs/latest-v16.x/api/buffer.html">16.x</a></li> 370<li><a href="https://nodejs.org/docs/latest-v15.x/api/buffer.html">15.x</a></li> 371<li><a href="https://nodejs.org/docs/latest-v14.x/api/buffer.html">14.x</a></li> 372<li><a href="https://nodejs.org/docs/latest-v13.x/api/buffer.html">13.x</a></li> 373<li><a href="https://nodejs.org/docs/latest-v12.x/api/buffer.html">12.x</a></li> 374<li><a href="https://nodejs.org/docs/latest-v11.x/api/buffer.html">11.x</a></li> 375<li><a href="https://nodejs.org/docs/latest-v10.x/api/buffer.html">10.x</a></li> 376<li><a href="https://nodejs.org/docs/latest-v9.x/api/buffer.html">9.x</a></li> 377<li><a href="https://nodejs.org/docs/latest-v8.x/api/buffer.html">8.x</a></li> 378<li><a href="https://nodejs.org/docs/latest-v7.x/api/buffer.html">7.x</a></li> 379<li><a href="https://nodejs.org/docs/latest-v6.x/api/buffer.html">6.x</a></li> 380<li><a href="https://nodejs.org/docs/latest-v5.x/api/buffer.html">5.x</a></li> 381<li><a href="https://nodejs.org/docs/latest-v4.x/api/buffer.html">4.x</a></li> 382<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/buffer.html">0.12.x</a></li> 383<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/buffer.html">0.10.x</a></li></ol></div> 384 </li> 385 386 <li class="picker-header"> 387 <a href="#"> 388 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 389 Options 390 </a> 391 392 <div class="picker"> 393 <ul> 394 <li> 395 <a href="all.html">View on single page</a> 396 </li> 397 <li> 398 <a href="buffer.json">View as JSON</a> 399 </li> 400 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/buffer.md">Edit on GitHub</a></li> 401 </ul> 402 </div> 403 </li> 404 </ul> 405 </div> 406 <hr> 407 </header> 408 409 <details id="toc" open><summary>Table of contents</summary><ul> 410<li><span class="stability_2"><a href="#buffer">Buffer</a></span> 411<ul> 412<li><a href="#buffers-and-character-encodings">Buffers and character encodings</a></li> 413<li><a href="#buffers-and-typedarrays">Buffers and TypedArrays</a></li> 414<li><a href="#buffers-and-iteration">Buffers and iteration</a></li> 415<li><a href="#class-blob">Class: <code>Blob</code></a> 416<ul> 417<li><a href="#new-bufferblobsources-options"><code>new buffer.Blob([sources[, options]])</code></a></li> 418<li><a href="#blobarraybuffer"><code>blob.arrayBuffer()</code></a></li> 419<li><a href="#blobsize"><code>blob.size</code></a></li> 420<li><a href="#blobslicestart-end-type"><code>blob.slice([start[, end[, type]]])</code></a></li> 421<li><a href="#blobstream"><code>blob.stream()</code></a></li> 422<li><a href="#blobtext"><code>blob.text()</code></a></li> 423<li><a href="#blobtype"><code>blob.type</code></a></li> 424<li><a href="#blob-objects-and-messagechannel"><code>Blob</code> objects and <code>MessageChannel</code></a></li> 425</ul> 426</li> 427<li><a href="#class-buffer">Class: <code>Buffer</code></a> 428<ul> 429<li><a href="#static-method-bufferallocsize-fill-encoding">Static method: <code>Buffer.alloc(size[, fill[, encoding]])</code></a></li> 430<li><a href="#static-method-bufferallocunsafesize">Static method: <code>Buffer.allocUnsafe(size)</code></a></li> 431<li><a href="#static-method-bufferallocunsafeslowsize">Static method: <code>Buffer.allocUnsafeSlow(size)</code></a></li> 432<li><a href="#static-method-bufferbytelengthstring-encoding">Static method: <code>Buffer.byteLength(string[, encoding])</code></a></li> 433<li><a href="#static-method-buffercomparebuf1-buf2">Static method: <code>Buffer.compare(buf1, buf2)</code></a></li> 434<li><a href="#static-method-bufferconcatlist-totallength">Static method: <code>Buffer.concat(list[, totalLength])</code></a></li> 435<li><a href="#static-method-buffercopybytesfromview-offset-length">Static method: <code>Buffer.copyBytesFrom(view[, offset[, length]])</code></a></li> 436<li><a href="#static-method-bufferfromarray">Static method: <code>Buffer.from(array)</code></a></li> 437<li><a href="#static-method-bufferfromarraybuffer-byteoffset-length">Static method: <code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a></li> 438<li><a href="#static-method-bufferfrombuffer">Static method: <code>Buffer.from(buffer)</code></a></li> 439<li><a href="#static-method-bufferfromobject-offsetorencoding-length">Static method: <code>Buffer.from(object[, offsetOrEncoding[, length]])</code></a></li> 440<li><a href="#static-method-bufferfromstring-encoding">Static method: <code>Buffer.from(string[, encoding])</code></a></li> 441<li><a href="#static-method-bufferisbufferobj">Static method: <code>Buffer.isBuffer(obj)</code></a></li> 442<li><a href="#static-method-bufferisencodingencoding">Static method: <code>Buffer.isEncoding(encoding)</code></a></li> 443<li><a href="#class-property-bufferpoolsize">Class property: <code>Buffer.poolSize</code></a></li> 444<li><a href="#bufindex"><code>buf[index]</code></a></li> 445<li><a href="#bufbuffer"><code>buf.buffer</code></a></li> 446<li><a href="#bufbyteoffset"><code>buf.byteOffset</code></a></li> 447<li><a href="#bufcomparetarget-targetstart-targetend-sourcestart-sourceend"><code>buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])</code></a></li> 448<li><a href="#bufcopytarget-targetstart-sourcestart-sourceend"><code>buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])</code></a></li> 449<li><a href="#bufentries"><code>buf.entries()</code></a></li> 450<li><a href="#bufequalsotherbuffer"><code>buf.equals(otherBuffer)</code></a></li> 451<li><a href="#buffillvalue-offset-end-encoding"><code>buf.fill(value[, offset[, end]][, encoding])</code></a></li> 452<li><a href="#bufincludesvalue-byteoffset-encoding"><code>buf.includes(value[, byteOffset][, encoding])</code></a></li> 453<li><a href="#bufindexofvalue-byteoffset-encoding"><code>buf.indexOf(value[, byteOffset][, encoding])</code></a></li> 454<li><a href="#bufkeys"><code>buf.keys()</code></a></li> 455<li><a href="#buflastindexofvalue-byteoffset-encoding"><code>buf.lastIndexOf(value[, byteOffset][, encoding])</code></a></li> 456<li><a href="#buflength"><code>buf.length</code></a></li> 457<li><span class="stability_0"><a href="#bufparent"><code>buf.parent</code></a></span></li> 458<li><a href="#bufreadbigint64beoffset"><code>buf.readBigInt64BE([offset])</code></a></li> 459<li><a href="#bufreadbigint64leoffset"><code>buf.readBigInt64LE([offset])</code></a></li> 460<li><a href="#bufreadbiguint64beoffset"><code>buf.readBigUInt64BE([offset])</code></a></li> 461<li><a href="#bufreadbiguint64leoffset"><code>buf.readBigUInt64LE([offset])</code></a></li> 462<li><a href="#bufreaddoublebeoffset"><code>buf.readDoubleBE([offset])</code></a></li> 463<li><a href="#bufreaddoubleleoffset"><code>buf.readDoubleLE([offset])</code></a></li> 464<li><a href="#bufreadfloatbeoffset"><code>buf.readFloatBE([offset])</code></a></li> 465<li><a href="#bufreadfloatleoffset"><code>buf.readFloatLE([offset])</code></a></li> 466<li><a href="#bufreadint8offset"><code>buf.readInt8([offset])</code></a></li> 467<li><a href="#bufreadint16beoffset"><code>buf.readInt16BE([offset])</code></a></li> 468<li><a href="#bufreadint16leoffset"><code>buf.readInt16LE([offset])</code></a></li> 469<li><a href="#bufreadint32beoffset"><code>buf.readInt32BE([offset])</code></a></li> 470<li><a href="#bufreadint32leoffset"><code>buf.readInt32LE([offset])</code></a></li> 471<li><a href="#bufreadintbeoffset-bytelength"><code>buf.readIntBE(offset, byteLength)</code></a></li> 472<li><a href="#bufreadintleoffset-bytelength"><code>buf.readIntLE(offset, byteLength)</code></a></li> 473<li><a href="#bufreaduint8offset"><code>buf.readUInt8([offset])</code></a></li> 474<li><a href="#bufreaduint16beoffset"><code>buf.readUInt16BE([offset])</code></a></li> 475<li><a href="#bufreaduint16leoffset"><code>buf.readUInt16LE([offset])</code></a></li> 476<li><a href="#bufreaduint32beoffset"><code>buf.readUInt32BE([offset])</code></a></li> 477<li><a href="#bufreaduint32leoffset"><code>buf.readUInt32LE([offset])</code></a></li> 478<li><a href="#bufreaduintbeoffset-bytelength"><code>buf.readUIntBE(offset, byteLength)</code></a></li> 479<li><a href="#bufreaduintleoffset-bytelength"><code>buf.readUIntLE(offset, byteLength)</code></a></li> 480<li><a href="#bufsubarraystart-end"><code>buf.subarray([start[, end]])</code></a></li> 481<li><span class="stability_0"><a href="#bufslicestart-end"><code>buf.slice([start[, end]])</code></a></span></li> 482<li><a href="#bufswap16"><code>buf.swap16()</code></a></li> 483<li><a href="#bufswap32"><code>buf.swap32()</code></a></li> 484<li><a href="#bufswap64"><code>buf.swap64()</code></a></li> 485<li><a href="#buftojson"><code>buf.toJSON()</code></a></li> 486<li><a href="#buftostringencoding-start-end"><code>buf.toString([encoding[, start[, end]]])</code></a></li> 487<li><a href="#bufvalues"><code>buf.values()</code></a></li> 488<li><a href="#bufwritestring-offset-length-encoding"><code>buf.write(string[, offset[, length]][, encoding])</code></a></li> 489<li><a href="#bufwritebigint64bevalue-offset"><code>buf.writeBigInt64BE(value[, offset])</code></a></li> 490<li><a href="#bufwritebigint64levalue-offset"><code>buf.writeBigInt64LE(value[, offset])</code></a></li> 491<li><a href="#bufwritebiguint64bevalue-offset"><code>buf.writeBigUInt64BE(value[, offset])</code></a></li> 492<li><a href="#bufwritebiguint64levalue-offset"><code>buf.writeBigUInt64LE(value[, offset])</code></a></li> 493<li><a href="#bufwritedoublebevalue-offset"><code>buf.writeDoubleBE(value[, offset])</code></a></li> 494<li><a href="#bufwritedoublelevalue-offset"><code>buf.writeDoubleLE(value[, offset])</code></a></li> 495<li><a href="#bufwritefloatbevalue-offset"><code>buf.writeFloatBE(value[, offset])</code></a></li> 496<li><a href="#bufwritefloatlevalue-offset"><code>buf.writeFloatLE(value[, offset])</code></a></li> 497<li><a href="#bufwriteint8value-offset"><code>buf.writeInt8(value[, offset])</code></a></li> 498<li><a href="#bufwriteint16bevalue-offset"><code>buf.writeInt16BE(value[, offset])</code></a></li> 499<li><a href="#bufwriteint16levalue-offset"><code>buf.writeInt16LE(value[, offset])</code></a></li> 500<li><a href="#bufwriteint32bevalue-offset"><code>buf.writeInt32BE(value[, offset])</code></a></li> 501<li><a href="#bufwriteint32levalue-offset"><code>buf.writeInt32LE(value[, offset])</code></a></li> 502<li><a href="#bufwriteintbevalue-offset-bytelength"><code>buf.writeIntBE(value, offset, byteLength)</code></a></li> 503<li><a href="#bufwriteintlevalue-offset-bytelength"><code>buf.writeIntLE(value, offset, byteLength)</code></a></li> 504<li><a href="#bufwriteuint8value-offset"><code>buf.writeUInt8(value[, offset])</code></a></li> 505<li><a href="#bufwriteuint16bevalue-offset"><code>buf.writeUInt16BE(value[, offset])</code></a></li> 506<li><a href="#bufwriteuint16levalue-offset"><code>buf.writeUInt16LE(value[, offset])</code></a></li> 507<li><a href="#bufwriteuint32bevalue-offset"><code>buf.writeUInt32BE(value[, offset])</code></a></li> 508<li><a href="#bufwriteuint32levalue-offset"><code>buf.writeUInt32LE(value[, offset])</code></a></li> 509<li><a href="#bufwriteuintbevalue-offset-bytelength"><code>buf.writeUIntBE(value, offset, byteLength)</code></a></li> 510<li><a href="#bufwriteuintlevalue-offset-bytelength"><code>buf.writeUIntLE(value, offset, byteLength)</code></a></li> 511<li><span class="stability_0"><a href="#new-bufferarray"><code>new Buffer(array)</code></a></span></li> 512<li><span class="stability_0"><a href="#new-bufferarraybuffer-byteoffset-length"><code>new Buffer(arrayBuffer[, byteOffset[, length]])</code></a></span></li> 513<li><span class="stability_0"><a href="#new-bufferbuffer"><code>new Buffer(buffer)</code></a></span></li> 514<li><span class="stability_0"><a href="#new-buffersize"><code>new Buffer(size)</code></a></span></li> 515<li><span class="stability_0"><a href="#new-bufferstring-encoding"><code>new Buffer(string[, encoding])</code></a></span></li> 516</ul> 517</li> 518<li><span class="stability_1"><a href="#class-file">Class: <code>File</code></a></span> 519<ul> 520<li><a href="#new-bufferfilesources-filename-options"><code>new buffer.File(sources, fileName[, options])</code></a></li> 521<li><a href="#filename"><code>file.name</code></a></li> 522<li><a href="#filelastmodified"><code>file.lastModified</code></a></li> 523</ul> 524</li> 525<li><a href="#nodebuffer-module-apis"><code>node:buffer</code> module APIs</a> 526<ul> 527<li><span class="stability_3"><a href="#bufferatobdata"><code>buffer.atob(data)</code></a></span></li> 528<li><span class="stability_3"><a href="#bufferbtoadata"><code>buffer.btoa(data)</code></a></span></li> 529<li><a href="#bufferisasciiinput"><code>buffer.isAscii(input)</code></a></li> 530<li><a href="#bufferisutf8input"><code>buffer.isUtf8(input)</code></a></li> 531<li><a href="#bufferinspect_max_bytes"><code>buffer.INSPECT_MAX_BYTES</code></a></li> 532<li><a href="#bufferkmaxlength"><code>buffer.kMaxLength</code></a></li> 533<li><a href="#bufferkstringmaxlength"><code>buffer.kStringMaxLength</code></a></li> 534<li><span class="stability_1"><a href="#bufferresolveobjecturlid"><code>buffer.resolveObjectURL(id)</code></a></span></li> 535<li><a href="#buffertranscodesource-fromenc-toenc"><code>buffer.transcode(source, fromEnc, toEnc)</code></a></li> 536<li><span class="stability_0"><a href="#class-slowbuffer">Class: <code>SlowBuffer</code></a></span> 537<ul> 538<li><span class="stability_0"><a href="#new-slowbuffersize"><code>new SlowBuffer(size)</code></a></span></li> 539</ul> 540</li> 541<li><a href="#buffer-constants">Buffer constants</a> 542<ul> 543<li><a href="#bufferconstantsmax_length"><code>buffer.constants.MAX_LENGTH</code></a></li> 544<li><a href="#bufferconstantsmax_string_length"><code>buffer.constants.MAX_STRING_LENGTH</code></a></li> 545</ul> 546</li> 547</ul> 548</li> 549<li><a href="#bufferfrom-bufferalloc-and-bufferallocunsafe"><code>Buffer.from()</code>, <code>Buffer.alloc()</code>, and <code>Buffer.allocUnsafe()</code></a> 550<ul> 551<li><a href="#the---zero-fill-buffers-command-line-option">The <code>--zero-fill-buffers</code> command-line option</a></li> 552<li><a href="#what-makes-bufferallocunsafe-and-bufferallocunsafeslow-unsafe">What makes <code>Buffer.allocUnsafe()</code> and <code>Buffer.allocUnsafeSlow()</code> "unsafe"?</a></li> 553</ul> 554</li> 555</ul> 556</li> 557</ul></details> 558 559 <div id="apicontent"> 560 <h2>Buffer<span><a class="mark" href="#buffer" id="buffer">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer"></a></h2> 561 562<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 563<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/buffer.js">lib/buffer.js</a></p> 564<p><code>Buffer</code> objects are used to represent a fixed-length sequence of bytes. Many 565Node.js APIs support <code>Buffer</code>s.</p> 566<p>The <code>Buffer</code> class is a subclass of JavaScript's <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a> class and 567extends it with methods that cover additional use cases. Node.js APIs accept 568plain <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a>s wherever <code>Buffer</code>s are supported as well.</p> 569<p>While the <code>Buffer</code> class is available within the global scope, it is still 570recommended to explicitly reference it via an import or require statement.</p> 571 572<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 573 574<span class="hljs-comment">// Creates a zero-filled Buffer of length 10.</span> 575<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">10</span>); 576 577<span class="hljs-comment">// Creates a Buffer of length 10,</span> 578<span class="hljs-comment">// filled with bytes which all have the value `1`.</span> 579<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">10</span>, <span class="hljs-number">1</span>); 580 581<span class="hljs-comment">// Creates an uninitialized buffer of length 10.</span> 582<span class="hljs-comment">// This is faster than calling Buffer.alloc() but the returned</span> 583<span class="hljs-comment">// Buffer instance might contain old data that needs to be</span> 584<span class="hljs-comment">// overwritten using fill(), write(), or other functions that fill the Buffer's</span> 585<span class="hljs-comment">// contents.</span> 586<span class="hljs-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">10</span>); 587 588<span class="hljs-comment">// Creates a Buffer containing the bytes [1, 2, 3].</span> 589<span class="hljs-keyword">const</span> buf4 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]); 590 591<span class="hljs-comment">// Creates a Buffer containing the bytes [1, 1, 1, 1] – the entries</span> 592<span class="hljs-comment">// are all truncated using `(value & 255)` to fit into the range 0–255.</span> 593<span class="hljs-keyword">const</span> buf5 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">257</span>, <span class="hljs-number">257.5</span>, -<span class="hljs-number">255</span>, <span class="hljs-string">'1'</span>]); 594 595<span class="hljs-comment">// Creates a Buffer containing the UTF-8-encoded bytes for the string 'tést':</span> 596<span class="hljs-comment">// [0x74, 0xc3, 0xa9, 0x73, 0x74] (in hexadecimal notation)</span> 597<span class="hljs-comment">// [116, 195, 169, 115, 116] (in decimal notation)</span> 598<span class="hljs-keyword">const</span> buf6 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'tést'</span>); 599 600<span class="hljs-comment">// Creates a Buffer containing the Latin-1 bytes [0x74, 0xe9, 0x73, 0x74].</span> 601<span class="hljs-keyword">const</span> buf7 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'tést'</span>, <span class="hljs-string">'latin1'</span>);</code><code class="language-js cjs"><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>); 602 603<span class="hljs-comment">// Creates a zero-filled Buffer of length 10.</span> 604<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">10</span>); 605 606<span class="hljs-comment">// Creates a Buffer of length 10,</span> 607<span class="hljs-comment">// filled with bytes which all have the value `1`.</span> 608<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">10</span>, <span class="hljs-number">1</span>); 609 610<span class="hljs-comment">// Creates an uninitialized buffer of length 10.</span> 611<span class="hljs-comment">// This is faster than calling Buffer.alloc() but the returned</span> 612<span class="hljs-comment">// Buffer instance might contain old data that needs to be</span> 613<span class="hljs-comment">// overwritten using fill(), write(), or other functions that fill the Buffer's</span> 614<span class="hljs-comment">// contents.</span> 615<span class="hljs-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">10</span>); 616 617<span class="hljs-comment">// Creates a Buffer containing the bytes [1, 2, 3].</span> 618<span class="hljs-keyword">const</span> buf4 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]); 619 620<span class="hljs-comment">// Creates a Buffer containing the bytes [1, 1, 1, 1] – the entries</span> 621<span class="hljs-comment">// are all truncated using `(value & 255)` to fit into the range 0–255.</span> 622<span class="hljs-keyword">const</span> buf5 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">257</span>, <span class="hljs-number">257.5</span>, -<span class="hljs-number">255</span>, <span class="hljs-string">'1'</span>]); 623 624<span class="hljs-comment">// Creates a Buffer containing the UTF-8-encoded bytes for the string 'tést':</span> 625<span class="hljs-comment">// [0x74, 0xc3, 0xa9, 0x73, 0x74] (in hexadecimal notation)</span> 626<span class="hljs-comment">// [116, 195, 169, 115, 116] (in decimal notation)</span> 627<span class="hljs-keyword">const</span> buf6 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'tést'</span>); 628 629<span class="hljs-comment">// Creates a Buffer containing the Latin-1 bytes [0x74, 0xe9, 0x73, 0x74].</span> 630<span class="hljs-keyword">const</span> buf7 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'tést'</span>, <span class="hljs-string">'latin1'</span>);</code><button class="copy-button">copy</button></pre> 631<section><h3>Buffers and character encodings<span><a class="mark" href="#buffers-and-character-encodings" id="buffers-and-character-encodings">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffers_and_character_encodings"></a></h3> 632<div class="api_metadata"> 633<details class="changelog"><summary>History</summary> 634<table> 635<tbody><tr><th>Version</th><th>Changes</th></tr> 636<tr><td>v15.7.0, v14.18.0</td> 637<td><p>Introduced <code>base64url</code> encoding.</p></td></tr> 638<tr><td>v6.4.0</td> 639<td><p>Introduced <code>latin1</code> as an alias for <code>binary</code>.</p></td></tr> 640<tr><td>v5.0.0</td> 641<td><p>Removed the deprecated <code>raw</code> and <code>raws</code> encodings.</p></td></tr> 642</tbody></table> 643</details> 644</div> 645<p>When converting between <code>Buffer</code>s and strings, a character encoding may be 646specified. If no character encoding is specified, UTF-8 will be used as the 647default.</p> 648 649<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 650 651<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'hello world'</span>, <span class="hljs-string">'utf8'</span>); 652 653<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>)); 654<span class="hljs-comment">// Prints: 68656c6c6f20776f726c64</span> 655<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'base64'</span>)); 656<span class="hljs-comment">// Prints: aGVsbG8gd29ybGQ=</span> 657 658<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>(<span class="hljs-string">'fhqwhgads'</span>, <span class="hljs-string">'utf8'</span>)); 659<span class="hljs-comment">// Prints: <Buffer 66 68 71 77 68 67 61 64 73></span> 660<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>(<span class="hljs-string">'fhqwhgads'</span>, <span class="hljs-string">'utf16le'</span>)); 661<span class="hljs-comment">// Prints: <Buffer 66 00 68 00 71 00 77 00 68 00 67 00 61 00 64 00 73 00></span></code><code class="language-js cjs"><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>); 662 663<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'hello world'</span>, <span class="hljs-string">'utf8'</span>); 664 665<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>)); 666<span class="hljs-comment">// Prints: 68656c6c6f20776f726c64</span> 667<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'base64'</span>)); 668<span class="hljs-comment">// Prints: aGVsbG8gd29ybGQ=</span> 669 670<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>(<span class="hljs-string">'fhqwhgads'</span>, <span class="hljs-string">'utf8'</span>)); 671<span class="hljs-comment">// Prints: <Buffer 66 68 71 77 68 67 61 64 73></span> 672<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>(<span class="hljs-string">'fhqwhgads'</span>, <span class="hljs-string">'utf16le'</span>)); 673<span class="hljs-comment">// Prints: <Buffer 66 00 68 00 71 00 77 00 68 00 67 00 61 00 64 00 73 00></span></code><button class="copy-button">copy</button></pre> 674<p>Node.js buffers accept all case variations of encoding strings that they 675receive. For example, UTF-8 can be specified as <code>'utf8'</code>, <code>'UTF8'</code>, or <code>'uTf8'</code>.</p> 676<p>The character encodings currently supported by Node.js are the following:</p> 677<ul> 678<li> 679<p><code>'utf8'</code> (alias: <code>'utf-8'</code>): Multi-byte encoded Unicode characters. Many web 680pages and other document formats use <a href="https://en.wikipedia.org/wiki/UTF-8">UTF-8</a>. This is the default character 681encoding. When decoding a <code>Buffer</code> into a string that does not exclusively 682contain valid UTF-8 data, the Unicode replacement character <code>U+FFFD</code> � will be 683used to represent those errors.</p> 684</li> 685<li> 686<p><code>'utf16le'</code> (alias: <code>'utf-16le'</code>): Multi-byte encoded Unicode characters. 687Unlike <code>'utf8'</code>, each character in the string will be encoded using either 2 688or 4 bytes. Node.js only supports the <a href="https://en.wikipedia.org/wiki/Endianness">little-endian</a> variant of 689<a href="https://en.wikipedia.org/wiki/UTF-16">UTF-16</a>.</p> 690</li> 691<li> 692<p><code>'latin1'</code>: Latin-1 stands for <a href="https://en.wikipedia.org/wiki/ISO-8859-1">ISO-8859-1</a>. This character encoding only 693supports the Unicode characters from <code>U+0000</code> to <code>U+00FF</code>. Each character is 694encoded using a single byte. Characters that do not fit into that range are 695truncated and will be mapped to characters in that range.</p> 696</li> 697</ul> 698<p>Converting a <code>Buffer</code> into a string using one of the above is referred to as 699decoding, and converting a string into a <code>Buffer</code> is referred to as encoding.</p> 700<p>Node.js also supports the following binary-to-text encodings. For 701binary-to-text encodings, the naming convention is reversed: Converting a 702<code>Buffer</code> into a string is typically referred to as encoding, and converting a 703string into a <code>Buffer</code> as decoding.</p> 704<ul> 705<li> 706<p><code>'base64'</code>: <a href="https://en.wikipedia.org/wiki/Base64">Base64</a> encoding. When creating a <code>Buffer</code> from a string, 707this encoding will also correctly accept "URL and Filename Safe Alphabet" as 708specified in <a href="https://tools.ietf.org/html/rfc4648#section-5">RFC 4648, Section 5</a>. Whitespace characters such as spaces, 709tabs, and new lines contained within the base64-encoded string are ignored.</p> 710</li> 711<li> 712<p><code>'base64url'</code>: <a href="https://tools.ietf.org/html/rfc4648#section-5">base64url</a> encoding as specified in 713<a href="https://tools.ietf.org/html/rfc4648#section-5">RFC 4648, Section 5</a>. When creating a <code>Buffer</code> from a string, this 714encoding will also correctly accept regular base64-encoded strings. When 715encoding a <code>Buffer</code> to a string, this encoding will omit padding.</p> 716</li> 717<li> 718<p><code>'hex'</code>: Encode each byte as two hexadecimal characters. Data truncation 719may occur when decoding strings that do not exclusively consist of an even 720number of hexadecimal characters. See below for an example.</p> 721</li> 722</ul> 723<p>The following legacy character encodings are also supported:</p> 724<ul> 725<li> 726<p><code>'ascii'</code>: For 7-bit <a href="https://en.wikipedia.org/wiki/ASCII">ASCII</a> data only. When encoding a string into a 727<code>Buffer</code>, this is equivalent to using <code>'latin1'</code>. When decoding a <code>Buffer</code> 728into a string, using this encoding will additionally unset the highest bit of 729each byte before decoding as <code>'latin1'</code>. 730Generally, there should be no reason to use this encoding, as <code>'utf8'</code> 731(or, if the data is known to always be ASCII-only, <code>'latin1'</code>) will be a 732better choice when encoding or decoding ASCII-only text. It is only provided 733for legacy compatibility.</p> 734</li> 735<li> 736<p><code>'binary'</code>: Alias for <code>'latin1'</code>. 737The name of this encoding can be very misleading, as all of the 738encodings listed here convert between strings and binary data. For converting 739between strings and <code>Buffer</code>s, typically <code>'utf8'</code> is the right choice.</p> 740</li> 741<li> 742<p><code>'ucs2'</code>, <code>'ucs-2'</code>: Aliases of <code>'utf16le'</code>. UCS-2 used to refer to a variant 743of UTF-16 that did not support characters that had code points larger than 744U+FFFF. In Node.js, these code points are always supported.</p> 745</li> 746</ul> 747 748<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 749 750<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'1ag123'</span>, <span class="hljs-string">'hex'</span>); 751<span class="hljs-comment">// Prints <Buffer 1a>, data truncated when first non-hexadecimal value</span> 752<span class="hljs-comment">// ('g') encountered.</span> 753 754<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'1a7'</span>, <span class="hljs-string">'hex'</span>); 755<span class="hljs-comment">// Prints <Buffer 1a>, data truncated when data ends in single digit ('7').</span> 756 757<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'1634'</span>, <span class="hljs-string">'hex'</span>); 758<span class="hljs-comment">// Prints <Buffer 16 34>, all data represented.</span></code><code class="language-js cjs"><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>); 759 760<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'1ag123'</span>, <span class="hljs-string">'hex'</span>); 761<span class="hljs-comment">// Prints <Buffer 1a>, data truncated when first non-hexadecimal value</span> 762<span class="hljs-comment">// ('g') encountered.</span> 763 764<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'1a7'</span>, <span class="hljs-string">'hex'</span>); 765<span class="hljs-comment">// Prints <Buffer 1a>, data truncated when data ends in single digit ('7').</span> 766 767<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'1634'</span>, <span class="hljs-string">'hex'</span>); 768<span class="hljs-comment">// Prints <Buffer 16 34>, all data represented.</span></code><button class="copy-button">copy</button></pre> 769<p>Modern Web browsers follow the <a href="https://encoding.spec.whatwg.org/">WHATWG Encoding Standard</a> which aliases 770both <code>'latin1'</code> and <code>'ISO-8859-1'</code> to <code>'win-1252'</code>. This means that while doing 771something like <code>http.get()</code>, if the returned charset is one of those listed in 772the WHATWG specification it is possible that the server actually returned 773<code>'win-1252'</code>-encoded data, and using <code>'latin1'</code> encoding may incorrectly decode 774the characters.</p> 775</section><section><h3>Buffers and TypedArrays<span><a class="mark" href="#buffers-and-typedarrays" id="buffers-and-typedarrays">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffers_and_typedarrays"></a></h3> 776<div class="api_metadata"> 777<details class="changelog"><summary>History</summary> 778<table> 779<tbody><tr><th>Version</th><th>Changes</th></tr> 780<tr><td>v3.0.0</td> 781<td><p>The <code>Buffer</code>s class now inherits from <code>Uint8Array</code>.</p></td></tr> 782</tbody></table> 783</details> 784</div> 785<p><code>Buffer</code> instances are also JavaScript <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> 786instances. All <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> methods are available on <code>Buffer</code>s. There are, 787however, subtle incompatibilities between the <code>Buffer</code> API and the 788<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> API.</p> 789<p>In particular:</p> 790<ul> 791<li>While <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/slice"><code>TypedArray.prototype.slice()</code></a> creates a copy of part of the <code>TypedArray</code>, 792<a href="#bufslicestart-end"><code>Buffer.prototype.slice()</code></a> creates a view over the existing <code>Buffer</code> 793without copying. This behavior can be surprising, and only exists for legacy 794compatibility. <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/subarray"><code>TypedArray.prototype.subarray()</code></a> can be used to achieve 795the behavior of <a href="#bufslicestart-end"><code>Buffer.prototype.slice()</code></a> on both <code>Buffer</code>s 796and other <code>TypedArray</code>s and should be preferred.</li> 797<li><a href="#buftostringencoding-start-end"><code>buf.toString()</code></a> is incompatible with its <code>TypedArray</code> equivalent.</li> 798<li>A number of methods, e.g. <a href="#bufindexofvalue-byteoffset-encoding"><code>buf.indexOf()</code></a>, support additional arguments.</li> 799</ul> 800<p>There are two ways to create new <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> instances from a <code>Buffer</code>:</p> 801<ul> 802<li>Passing a <code>Buffer</code> to a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> constructor will copy the <code>Buffer</code>s 803contents, interpreted as an array of integers, and not as a byte sequence 804of the target type.</li> 805</ul> 806 807<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 808 809<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]); 810<span class="hljs-keyword">const</span> uint32array = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint32Array</span>(buf); 811 812<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(uint32array); 813 814<span class="hljs-comment">// Prints: Uint32Array(4) [ 1, 2, 3, 4 ]</span></code><code class="language-js cjs"><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>); 815 816<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]); 817<span class="hljs-keyword">const</span> uint32array = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint32Array</span>(buf); 818 819<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(uint32array); 820 821<span class="hljs-comment">// Prints: Uint32Array(4) [ 1, 2, 3, 4 ]</span></code><button class="copy-button">copy</button></pre> 822<ul> 823<li>Passing the <code>Buffer</code>s underlying <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> will create a 824<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> that shares its memory with the <code>Buffer</code>.</li> 825</ul> 826 827<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 828 829<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'hello'</span>, <span class="hljs-string">'utf16le'</span>); 830<span class="hljs-keyword">const</span> uint16array = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint16Array</span>( 831 buf.<span class="hljs-property">buffer</span>, 832 buf.<span class="hljs-property">byteOffset</span>, 833 buf.<span class="hljs-property">length</span> / <span class="hljs-title class_">Uint16Array</span>.<span class="hljs-property">BYTES_PER_ELEMENT</span>); 834 835<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(uint16array); 836 837<span class="hljs-comment">// Prints: Uint16Array(5) [ 104, 101, 108, 108, 111 ]</span></code><code class="language-js cjs"><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>); 838 839<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'hello'</span>, <span class="hljs-string">'utf16le'</span>); 840<span class="hljs-keyword">const</span> uint16array = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint16Array</span>( 841 buf.<span class="hljs-property">buffer</span>, 842 buf.<span class="hljs-property">byteOffset</span>, 843 buf.<span class="hljs-property">length</span> / <span class="hljs-title class_">Uint16Array</span>.<span class="hljs-property">BYTES_PER_ELEMENT</span>); 844 845<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(uint16array); 846 847<span class="hljs-comment">// Prints: Uint16Array(5) [ 104, 101, 108, 108, 111 ]</span></code><button class="copy-button">copy</button></pre> 848<p>It is possible to create a new <code>Buffer</code> that shares the same allocated 849memory as a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> instance by using the <code>TypedArray</code> object's 850<code>.buffer</code> property in the same way. <a href="#static-method-bufferfromarraybuffer-byteoffset-length"><code>Buffer.from()</code></a> 851behaves like <code>new Uint8Array()</code> in this context.</p> 852 853<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 854 855<span class="hljs-keyword">const</span> arr = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint16Array</span>(<span class="hljs-number">2</span>); 856 857arr[<span class="hljs-number">0</span>] = <span class="hljs-number">5000</span>; 858arr[<span class="hljs-number">1</span>] = <span class="hljs-number">4000</span>; 859 860<span class="hljs-comment">// Copies the contents of `arr`.</span> 861<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arr); 862 863<span class="hljs-comment">// Shares memory with `arr`.</span> 864<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arr.<span class="hljs-property">buffer</span>); 865 866<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 867<span class="hljs-comment">// Prints: <Buffer 88 a0></span> 868<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2); 869<span class="hljs-comment">// Prints: <Buffer 88 13 a0 0f></span> 870 871arr[<span class="hljs-number">1</span>] = <span class="hljs-number">6000</span>; 872 873<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 874<span class="hljs-comment">// Prints: <Buffer 88 a0></span> 875<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2); 876<span class="hljs-comment">// Prints: <Buffer 88 13 70 17></span></code><code class="language-js cjs"><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>); 877 878<span class="hljs-keyword">const</span> arr = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint16Array</span>(<span class="hljs-number">2</span>); 879 880arr[<span class="hljs-number">0</span>] = <span class="hljs-number">5000</span>; 881arr[<span class="hljs-number">1</span>] = <span class="hljs-number">4000</span>; 882 883<span class="hljs-comment">// Copies the contents of `arr`.</span> 884<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arr); 885 886<span class="hljs-comment">// Shares memory with `arr`.</span> 887<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arr.<span class="hljs-property">buffer</span>); 888 889<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 890<span class="hljs-comment">// Prints: <Buffer 88 a0></span> 891<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2); 892<span class="hljs-comment">// Prints: <Buffer 88 13 a0 0f></span> 893 894arr[<span class="hljs-number">1</span>] = <span class="hljs-number">6000</span>; 895 896<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 897<span class="hljs-comment">// Prints: <Buffer 88 a0></span> 898<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2); 899<span class="hljs-comment">// Prints: <Buffer 88 13 70 17></span></code><button class="copy-button">copy</button></pre> 900<p>When creating a <code>Buffer</code> using a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>'s <code>.buffer</code>, it is 901possible to use only a portion of the underlying <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> by passing in 902<code>byteOffset</code> and <code>length</code> parameters.</p> 903 904<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 905 906<span class="hljs-keyword">const</span> arr = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint16Array</span>(<span class="hljs-number">20</span>); 907<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arr.<span class="hljs-property">buffer</span>, <span class="hljs-number">0</span>, <span class="hljs-number">16</span>); 908 909<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>); 910<span class="hljs-comment">// Prints: 16</span></code><code class="language-js cjs"><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>); 911 912<span class="hljs-keyword">const</span> arr = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint16Array</span>(<span class="hljs-number">20</span>); 913<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arr.<span class="hljs-property">buffer</span>, <span class="hljs-number">0</span>, <span class="hljs-number">16</span>); 914 915<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>); 916<span class="hljs-comment">// Prints: 16</span></code><button class="copy-button">copy</button></pre> 917<p>The <code>Buffer.from()</code> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from"><code>TypedArray.from()</code></a> have different signatures and 918implementations. Specifically, the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> variants accept a second 919argument that is a mapping function that is invoked on every element of the 920typed array:</p> 921<ul> 922<li><code>TypedArray.from(source[, mapFn[, thisArg]])</code></li> 923</ul> 924<p>The <code>Buffer.from()</code> method, however, does not support the use of a mapping 925function:</p> 926<ul> 927<li><a href="#static-method-bufferfromarray"><code>Buffer.from(array)</code></a></li> 928<li><a href="#static-method-bufferfrombuffer"><code>Buffer.from(buffer)</code></a></li> 929<li><a href="#static-method-bufferfromarraybuffer-byteoffset-length"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a></li> 930<li><a href="#static-method-bufferfromstring-encoding"><code>Buffer.from(string[, encoding])</code></a></li> 931</ul> 932</section><section><h3>Buffers and iteration<span><a class="mark" href="#buffers-and-iteration" id="buffers-and-iteration">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffers_and_iteration"></a></h3> 933<p><code>Buffer</code> instances can be iterated over using <code>for..of</code> syntax:</p> 934 935<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 936 937<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]); 938 939<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> b <span class="hljs-keyword">of</span> buf) { 940 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b); 941} 942<span class="hljs-comment">// Prints:</span> 943<span class="hljs-comment">// 1</span> 944<span class="hljs-comment">// 2</span> 945<span class="hljs-comment">// 3</span></code><code class="language-js cjs"><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>); 946 947<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]); 948 949<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> b <span class="hljs-keyword">of</span> buf) { 950 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b); 951} 952<span class="hljs-comment">// Prints:</span> 953<span class="hljs-comment">// 1</span> 954<span class="hljs-comment">// 2</span> 955<span class="hljs-comment">// 3</span></code><button class="copy-button">copy</button></pre> 956<p>Additionally, the <a href="#bufvalues"><code>buf.values()</code></a>, <a href="#bufkeys"><code>buf.keys()</code></a>, and 957<a href="#bufentries"><code>buf.entries()</code></a> methods can be used to create iterators.</p> 958</section><section><h3>Class: <code>Blob</code><span><a class="mark" href="#class-blob" id="class-blob">#</a></span><a aria-hidden="true" class="legacy" id="buffer_class_blob"></a></h3> 959<div class="api_metadata"> 960<details class="changelog"><summary>History</summary> 961<table> 962<tbody><tr><th>Version</th><th>Changes</th></tr> 963<tr><td>v18.0.0</td> 964<td><p>No longer experimental.</p></td></tr> 965<tr><td>v15.7.0, v14.18.0</td> 966<td><p><span>Added in: v15.7.0, v14.18.0</span></p></td></tr> 967</tbody></table> 968</details> 969</div> 970<p>A <a href="https://developer.mozilla.org/en-US/docs/Web/API/Blob"><code>Blob</code></a> encapsulates immutable, raw data that can be safely shared across 971multiple worker threads.</p> 972<h4><code>new buffer.Blob([sources[, options]])</code><span><a class="mark" href="#new-bufferblobsources-options" id="new-bufferblobsources-options">#</a></span><a aria-hidden="true" class="legacy" id="buffer_new_buffer_blob_sources_options"></a></h4> 973<div class="api_metadata"> 974<details class="changelog"><summary>History</summary> 975<table> 976<tbody><tr><th>Version</th><th>Changes</th></tr> 977<tr><td>v16.7.0</td> 978<td><p>Added the standard <code>endings</code> option to replace line-endings, and removed the non-standard <code>encoding</code> option.</p></td></tr> 979<tr><td>v15.7.0, v14.18.0</td> 980<td><p><span>Added in: v15.7.0, v14.18.0</span></p></td></tr> 981</tbody></table> 982</details> 983</div> 984<ul> 985<li><code>sources</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer[]></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> | <a href="buffer.html#class-blob" class="type"><Blob[]></a> An 986array of string, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></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>, or <a href="buffer.html#class-blob" class="type"><Blob></a> objects, 987or any mix of such objects, that will be stored within the <code>Blob</code>.</li> 988<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 989<ul> 990<li><code>endings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> One of either <code>'transparent'</code> or <code>'native'</code>. When set 991to <code>'native'</code>, line endings in string source parts will be converted to 992the platform native line-ending as specified by <code>require('node:os').EOL</code>.</li> 993<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The Blob content-type. The intent is for <code>type</code> to convey 994the MIME media type of the data, however no validation of the type format 995is performed.</li> 996</ul> 997</li> 998</ul> 999<p>Creates a new <code>Blob</code> object containing a concatenation of the given sources.</p> 1000<p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></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>, and <a href="buffer.html#class-buffer" class="type"><Buffer></a> sources are copied into 1001the 'Blob' and can therefore be safely modified after the 'Blob' is created.</p> 1002<p>String sources are encoded as UTF-8 byte sequences and copied into the Blob. 1003Unmatched surrogate pairs within each string part will be replaced by Unicode 1004U+FFFD replacement characters.</p> 1005<h4><code>blob.arrayBuffer()</code><span><a class="mark" href="#blobarraybuffer" id="blobarraybuffer">#</a></span><a aria-hidden="true" class="legacy" id="buffer_blob_arraybuffer"></a></h4> 1006<div class="api_metadata"> 1007<span>Added in: v15.7.0, v14.18.0</span> 1008</div> 1009<ul> 1010<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 1011</ul> 1012<p>Returns a promise that fulfills with an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> containing a copy of 1013the <code>Blob</code> data.</p> 1014<h4><code>blob.size</code><span><a class="mark" href="#blobsize" id="blobsize">#</a></span><a aria-hidden="true" class="legacy" id="buffer_blob_size"></a></h4> 1015<div class="api_metadata"> 1016<span>Added in: v15.7.0, v14.18.0</span> 1017</div> 1018<p>The total size of the <code>Blob</code> in bytes.</p> 1019<h4><code>blob.slice([start[, end[, type]]])</code><span><a class="mark" href="#blobslicestart-end-type" id="blobslicestart-end-type">#</a></span><a aria-hidden="true" class="legacy" id="buffer_blob_slice_start_end_type"></a></h4> 1020<div class="api_metadata"> 1021<span>Added in: v15.7.0, v14.18.0</span> 1022</div> 1023<ul> 1024<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The starting index.</li> 1025<li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The ending index.</li> 1026<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The content-type for the new <code>Blob</code></li> 1027</ul> 1028<p>Creates and returns a new <code>Blob</code> containing a subset of this <code>Blob</code> objects 1029data. The original <code>Blob</code> is not altered.</p> 1030<h4><code>blob.stream()</code><span><a class="mark" href="#blobstream" id="blobstream">#</a></span><a aria-hidden="true" class="legacy" id="buffer_blob_stream"></a></h4> 1031<div class="api_metadata"> 1032<span>Added in: v16.7.0</span> 1033</div> 1034<ul> 1035<li>Returns: <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a></li> 1036</ul> 1037<p>Returns a new <code>ReadableStream</code> that allows the content of the <code>Blob</code> to be read.</p> 1038<h4><code>blob.text()</code><span><a class="mark" href="#blobtext" id="blobtext">#</a></span><a aria-hidden="true" class="legacy" id="buffer_blob_text"></a></h4> 1039<div class="api_metadata"> 1040<span>Added in: v15.7.0, v14.18.0</span> 1041</div> 1042<ul> 1043<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 1044</ul> 1045<p>Returns a promise that fulfills with the contents of the <code>Blob</code> decoded as a 1046UTF-8 string.</p> 1047<h4><code>blob.type</code><span><a class="mark" href="#blobtype" id="blobtype">#</a></span><a aria-hidden="true" class="legacy" id="buffer_blob_type"></a></h4> 1048<div class="api_metadata"> 1049<span>Added in: v15.7.0, v14.18.0</span> 1050</div> 1051<ul> 1052<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1053</ul> 1054<p>The content-type of the <code>Blob</code>.</p> 1055<h4><code>Blob</code> objects and <code>MessageChannel</code><span><a class="mark" href="#blob-objects-and-messagechannel" id="blob-objects-and-messagechannel">#</a></span><a aria-hidden="true" class="legacy" id="buffer_blob_objects_and_messagechannel"></a></h4> 1056<p>Once a <a href="buffer.html#class-blob" class="type"><Blob></a> object is created, it can be sent via <code>MessagePort</code> to multiple 1057destinations without transferring or immediately copying the data. The data 1058contained by the <code>Blob</code> is copied only when the <code>arrayBuffer()</code> or <code>text()</code> 1059methods are called.</p> 1060 1061<pre class="with-62-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_">Blob</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1062<span class="hljs-keyword">import</span> { <span class="hljs-built_in">setTimeout</span> <span class="hljs-keyword">as</span> delay } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:timers/promises'</span>; 1063 1064<span class="hljs-keyword">const</span> blob = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Blob</span>([<span class="hljs-string">'hello there'</span>]); 1065 1066<span class="hljs-keyword">const</span> mc1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); 1067<span class="hljs-keyword">const</span> mc2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); 1068 1069mc1.<span class="hljs-property">port1</span>.<span class="hljs-property">onmessage</span> = <span class="hljs-keyword">async</span> ({ data }) => { 1070 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-keyword">await</span> data.<span class="hljs-title function_">arrayBuffer</span>()); 1071 mc1.<span class="hljs-property">port1</span>.<span class="hljs-title function_">close</span>(); 1072}; 1073 1074mc2.<span class="hljs-property">port1</span>.<span class="hljs-property">onmessage</span> = <span class="hljs-keyword">async</span> ({ data }) => { 1075 <span class="hljs-keyword">await</span> <span class="hljs-title function_">delay</span>(<span class="hljs-number">1000</span>); 1076 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-keyword">await</span> data.<span class="hljs-title function_">arrayBuffer</span>()); 1077 mc2.<span class="hljs-property">port1</span>.<span class="hljs-title function_">close</span>(); 1078}; 1079 1080mc1.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(blob); 1081mc2.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(blob); 1082 1083<span class="hljs-comment">// The Blob is still usable after posting.</span> 1084blob.<span class="hljs-title function_">text</span>().<span class="hljs-title function_">then</span>(<span class="hljs-variable language_">console</span>.<span class="hljs-property">log</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Blob</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>); 1085<span class="hljs-keyword">const</span> { <span class="hljs-attr">setTimeout</span>: delay } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:timers/promises'</span>); 1086 1087<span class="hljs-keyword">const</span> blob = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Blob</span>([<span class="hljs-string">'hello there'</span>]); 1088 1089<span class="hljs-keyword">const</span> mc1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); 1090<span class="hljs-keyword">const</span> mc2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); 1091 1092mc1.<span class="hljs-property">port1</span>.<span class="hljs-property">onmessage</span> = <span class="hljs-keyword">async</span> ({ data }) => { 1093 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-keyword">await</span> data.<span class="hljs-title function_">arrayBuffer</span>()); 1094 mc1.<span class="hljs-property">port1</span>.<span class="hljs-title function_">close</span>(); 1095}; 1096 1097mc2.<span class="hljs-property">port1</span>.<span class="hljs-property">onmessage</span> = <span class="hljs-keyword">async</span> ({ data }) => { 1098 <span class="hljs-keyword">await</span> <span class="hljs-title function_">delay</span>(<span class="hljs-number">1000</span>); 1099 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-keyword">await</span> data.<span class="hljs-title function_">arrayBuffer</span>()); 1100 mc2.<span class="hljs-property">port1</span>.<span class="hljs-title function_">close</span>(); 1101}; 1102 1103mc1.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(blob); 1104mc2.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(blob); 1105 1106<span class="hljs-comment">// The Blob is still usable after posting.</span> 1107blob.<span class="hljs-title function_">text</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> 1108</section><section><h3>Class: <code>Buffer</code><span><a class="mark" href="#class-buffer" id="class-buffer">#</a></span><a aria-hidden="true" class="legacy" id="buffer_class_buffer"></a></h3> 1109<p>The <code>Buffer</code> class is a global type for dealing with binary data directly. 1110It can be constructed in a variety of ways.</p> 1111<h4>Static method: <code>Buffer.alloc(size[, fill[, encoding]])</code><span><a class="mark" href="#static-method-bufferallocsize-fill-encoding" id="static-method-bufferallocsize-fill-encoding">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_alloc_size_fill_encoding"></a></h4> 1112<div class="api_metadata"> 1113<details class="changelog"><summary>History</summary> 1114<table> 1115<tbody><tr><th>Version</th><th>Changes</th></tr> 1116<tr><td>v20.0.0</td> 1117<td><p>Throw ERR_INVALID_ARG_TYPE or ERR_OUT_OF_RANGE instead of ERR_INVALID_ARG_VALUE for invalid input arguments.</p></td></tr> 1118<tr><td>v15.0.0</td> 1119<td><p>Throw ERR_INVALID_ARG_VALUE instead of ERR_INVALID_OPT_VALUE for invalid input arguments.</p></td></tr> 1120<tr><td>v10.0.0</td> 1121<td><p>Attempting to fill a non-zero length buffer with a zero length buffer triggers a thrown exception.</p></td></tr> 1122<tr><td>v10.0.0</td> 1123<td><p>Specifying an invalid string for <code>fill</code> triggers a thrown exception.</p></td></tr> 1124<tr><td>v8.9.3</td> 1125<td><p>Specifying an invalid string for <code>fill</code> now results in a zero-filled buffer.</p></td></tr> 1126<tr><td>v5.10.0</td> 1127<td><p><span>Added in: v5.10.0</span></p></td></tr> 1128</tbody></table> 1129</details> 1130</div> 1131<ul> 1132<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The desired length of the new <code>Buffer</code>.</li> 1133<li><code>fill</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type"><Uint8Array></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> A value to pre-fill the new <code>Buffer</code> 1134with. <strong>Default:</strong> <code>0</code>.</li> 1135<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> If <code>fill</code> is a string, this is its encoding. 1136<strong>Default:</strong> <code>'utf8'</code>.</li> 1137</ul> 1138<p>Allocates a new <code>Buffer</code> of <code>size</code> bytes. If <code>fill</code> is <code>undefined</code>, the 1139<code>Buffer</code> will be zero-filled.</p> 1140 1141<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1142 1143<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">5</span>); 1144 1145<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 1146<span class="hljs-comment">// Prints: <Buffer 00 00 00 00 00></span></code><code class="language-js cjs"><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>); 1147 1148<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">5</span>); 1149 1150<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 1151<span class="hljs-comment">// Prints: <Buffer 00 00 00 00 00></span></code><button class="copy-button">copy</button></pre> 1152<p>If <code>size</code> is larger than 1153<a href="#bufferconstantsmax_length"><code>buffer.constants.MAX_LENGTH</code></a> or smaller than 0, <a href="errors.html#err_out_of_range"><code>ERR_OUT_OF_RANGE</code></a> 1154is thrown.</p> 1155<p>If <code>fill</code> is specified, the allocated <code>Buffer</code> will be initialized by calling 1156<a href="#buffillvalue-offset-end-encoding"><code>buf.fill(fill)</code></a>.</p> 1157 1158<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1159 1160<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">5</span>, <span class="hljs-string">'a'</span>); 1161 1162<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 1163<span class="hljs-comment">// Prints: <Buffer 61 61 61 61 61></span></code><code class="language-js cjs"><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>); 1164 1165<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">5</span>, <span class="hljs-string">'a'</span>); 1166 1167<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 1168<span class="hljs-comment">// Prints: <Buffer 61 61 61 61 61></span></code><button class="copy-button">copy</button></pre> 1169<p>If both <code>fill</code> and <code>encoding</code> are specified, the allocated <code>Buffer</code> will be 1170initialized by calling <a href="#buffillvalue-offset-end-encoding"><code>buf.fill(fill, encoding)</code></a>.</p> 1171 1172<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1173 1174<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">11</span>, <span class="hljs-string">'aGVsbG8gd29ybGQ='</span>, <span class="hljs-string">'base64'</span>); 1175 1176<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 1177<span class="hljs-comment">// Prints: <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64></span></code><code class="language-js cjs"><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>); 1178 1179<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">11</span>, <span class="hljs-string">'aGVsbG8gd29ybGQ='</span>, <span class="hljs-string">'base64'</span>); 1180 1181<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 1182<span class="hljs-comment">// Prints: <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64></span></code><button class="copy-button">copy</button></pre> 1183<p>Calling <a href="#static-method-bufferallocsize-fill-encoding"><code>Buffer.alloc()</code></a> can be measurably slower than the alternative 1184<a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> but ensures that the newly created <code>Buffer</code> instance 1185contents will never contain sensitive data from previous allocations, including 1186data that might not have been allocated for <code>Buffer</code>s.</p> 1187<p>A <code>TypeError</code> will be thrown if <code>size</code> is not a number.</p> 1188<h4>Static method: <code>Buffer.allocUnsafe(size)</code><span><a class="mark" href="#static-method-bufferallocunsafesize" id="static-method-bufferallocunsafesize">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_allocunsafe_size"></a></h4> 1189<div class="api_metadata"> 1190<details class="changelog"><summary>History</summary> 1191<table> 1192<tbody><tr><th>Version</th><th>Changes</th></tr> 1193<tr><td>v20.0.0</td> 1194<td><p>Throw ERR_INVALID_ARG_TYPE or ERR_OUT_OF_RANGE instead of ERR_INVALID_ARG_VALUE for invalid input arguments.</p></td></tr> 1195<tr><td>v15.0.0</td> 1196<td><p>Throw ERR_INVALID_ARG_VALUE instead of ERR_INVALID_OPT_VALUE for invalid input arguments.</p></td></tr> 1197<tr><td>v7.0.0</td> 1198<td><p>Passing a negative <code>size</code> will now throw an error.</p></td></tr> 1199<tr><td>v5.10.0</td> 1200<td><p><span>Added in: v5.10.0</span></p></td></tr> 1201</tbody></table> 1202</details> 1203</div> 1204<ul> 1205<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The desired length of the new <code>Buffer</code>.</li> 1206</ul> 1207<p>Allocates a new <code>Buffer</code> of <code>size</code> bytes. If <code>size</code> is larger than 1208<a href="#bufferconstantsmax_length"><code>buffer.constants.MAX_LENGTH</code></a> or smaller than 0, <a href="errors.html#err_out_of_range"><code>ERR_OUT_OF_RANGE</code></a> 1209is thrown.</p> 1210<p>The underlying memory for <code>Buffer</code> instances created in this way is <em>not 1211initialized</em>. The contents of the newly created <code>Buffer</code> are unknown and 1212<em>may contain sensitive data</em>. Use <a href="#static-method-bufferallocsize-fill-encoding"><code>Buffer.alloc()</code></a> instead to initialize 1213<code>Buffer</code> instances with zeroes.</p> 1214 1215<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1216 1217<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">10</span>); 1218 1219<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 1220<span class="hljs-comment">// Prints (contents may vary): <Buffer a0 8b 28 3f 01 00 00 00 50 32></span> 1221 1222buf.<span class="hljs-title function_">fill</span>(<span class="hljs-number">0</span>); 1223 1224<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 1225<span class="hljs-comment">// Prints: <Buffer 00 00 00 00 00 00 00 00 00 00></span></code><code class="language-js cjs"><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>); 1226 1227<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">10</span>); 1228 1229<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 1230<span class="hljs-comment">// Prints (contents may vary): <Buffer a0 8b 28 3f 01 00 00 00 50 32></span> 1231 1232buf.<span class="hljs-title function_">fill</span>(<span class="hljs-number">0</span>); 1233 1234<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 1235<span class="hljs-comment">// Prints: <Buffer 00 00 00 00 00 00 00 00 00 00></span></code><button class="copy-button">copy</button></pre> 1236<p>A <code>TypeError</code> will be thrown if <code>size</code> is not a number.</p> 1237<p>The <code>Buffer</code> module pre-allocates an internal <code>Buffer</code> instance of 1238size <a href="#class-property-bufferpoolsize"><code>Buffer.poolSize</code></a> that is used as a pool for the fast allocation of new 1239<code>Buffer</code> instances created using <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a>, <a href="#static-method-bufferfromarray"><code>Buffer.from(array)</code></a>, 1240and <a href="#static-method-bufferconcatlist-totallength"><code>Buffer.concat()</code></a> only when <code>size</code> is less than or equal to 1241<code>Buffer.poolSize >> 1</code> (floor of <a href="#class-property-bufferpoolsize"><code>Buffer.poolSize</code></a> divided by two).</p> 1242<p>Use of this pre-allocated internal memory pool is a key difference between 1243calling <code>Buffer.alloc(size, fill)</code> vs. <code>Buffer.allocUnsafe(size).fill(fill)</code>. 1244Specifically, <code>Buffer.alloc(size, fill)</code> will <em>never</em> use the internal <code>Buffer</code> 1245pool, while <code>Buffer.allocUnsafe(size).fill(fill)</code> <em>will</em> use the internal 1246<code>Buffer</code> pool if <code>size</code> is less than or equal to half <a href="#class-property-bufferpoolsize"><code>Buffer.poolSize</code></a>. The 1247difference is subtle but can be important when an application requires the 1248additional performance that <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> provides.</p> 1249<h4>Static method: <code>Buffer.allocUnsafeSlow(size)</code><span><a class="mark" href="#static-method-bufferallocunsafeslowsize" id="static-method-bufferallocunsafeslowsize">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_allocunsafeslow_size"></a></h4> 1250<div class="api_metadata"> 1251<details class="changelog"><summary>History</summary> 1252<table> 1253<tbody><tr><th>Version</th><th>Changes</th></tr> 1254<tr><td>v20.0.0</td> 1255<td><p>Throw ERR_INVALID_ARG_TYPE or ERR_OUT_OF_RANGE instead of ERR_INVALID_ARG_VALUE for invalid input arguments.</p></td></tr> 1256<tr><td>v15.0.0</td> 1257<td><p>Throw ERR_INVALID_ARG_VALUE instead of ERR_INVALID_OPT_VALUE for invalid input arguments.</p></td></tr> 1258<tr><td>v5.12.0</td> 1259<td><p><span>Added in: v5.12.0</span></p></td></tr> 1260</tbody></table> 1261</details> 1262</div> 1263<ul> 1264<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The desired length of the new <code>Buffer</code>.</li> 1265</ul> 1266<p>Allocates a new <code>Buffer</code> of <code>size</code> bytes. If <code>size</code> is larger than 1267<a href="#bufferconstantsmax_length"><code>buffer.constants.MAX_LENGTH</code></a> or smaller than 0, <a href="errors.html#err_out_of_range"><code>ERR_OUT_OF_RANGE</code></a> 1268is thrown. A zero-length <code>Buffer</code> is created if <code>size</code> is 0.</p> 1269<p>The underlying memory for <code>Buffer</code> instances created in this way is <em>not 1270initialized</em>. The contents of the newly created <code>Buffer</code> are unknown and 1271<em>may contain sensitive data</em>. Use <a href="#buffillvalue-offset-end-encoding"><code>buf.fill(0)</code></a> to initialize 1272such <code>Buffer</code> instances with zeroes.</p> 1273<p>When using <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> to allocate new <code>Buffer</code> instances, 1274allocations under 4 KiB are sliced from a single pre-allocated <code>Buffer</code>. This 1275allows applications to avoid the garbage collection overhead of creating many 1276individually allocated <code>Buffer</code> instances. This approach improves both 1277performance and memory usage by eliminating the need to track and clean up as 1278many individual <code>ArrayBuffer</code> objects.</p> 1279<p>However, in the case where a developer may need to retain a small chunk of 1280memory from a pool for an indeterminate amount of time, it may be appropriate 1281to create an un-pooled <code>Buffer</code> instance using <code>Buffer.allocUnsafeSlow()</code> and 1282then copying out the relevant bits.</p> 1283 1284<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1285 1286<span class="hljs-comment">// Need to keep around a few small chunks of memory.</span> 1287<span class="hljs-keyword">const</span> store = []; 1288 1289socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 1290 <span class="hljs-keyword">let</span> data; 1291 <span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (data = readable.<span class="hljs-title function_">read</span>())) { 1292 <span class="hljs-comment">// Allocate for retained data.</span> 1293 <span class="hljs-keyword">const</span> sb = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafeSlow</span>(<span class="hljs-number">10</span>); 1294 1295 <span class="hljs-comment">// Copy the data into the new allocation.</span> 1296 data.<span class="hljs-title function_">copy</span>(sb, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">10</span>); 1297 1298 store.<span class="hljs-title function_">push</span>(sb); 1299 } 1300});</code><code class="language-js cjs"><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>); 1301 1302<span class="hljs-comment">// Need to keep around a few small chunks of memory.</span> 1303<span class="hljs-keyword">const</span> store = []; 1304 1305socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 1306 <span class="hljs-keyword">let</span> data; 1307 <span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (data = readable.<span class="hljs-title function_">read</span>())) { 1308 <span class="hljs-comment">// Allocate for retained data.</span> 1309 <span class="hljs-keyword">const</span> sb = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafeSlow</span>(<span class="hljs-number">10</span>); 1310 1311 <span class="hljs-comment">// Copy the data into the new allocation.</span> 1312 data.<span class="hljs-title function_">copy</span>(sb, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">10</span>); 1313 1314 store.<span class="hljs-title function_">push</span>(sb); 1315 } 1316});</code><button class="copy-button">copy</button></pre> 1317<p>A <code>TypeError</code> will be thrown if <code>size</code> is not a number.</p> 1318<h4>Static method: <code>Buffer.byteLength(string[, encoding])</code><span><a class="mark" href="#static-method-bufferbytelengthstring-encoding" id="static-method-bufferbytelengthstring-encoding">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_bytelength_string_encoding"></a></h4> 1319<div class="api_metadata"> 1320<details class="changelog"><summary>History</summary> 1321<table> 1322<tbody><tr><th>Version</th><th>Changes</th></tr> 1323<tr><td>v7.0.0</td> 1324<td><p>Passing invalid input will now throw an error.</p></td></tr> 1325<tr><td>v5.10.0</td> 1326<td><p>The <code>string</code> parameter can now be any <code>TypedArray</code>, <code>DataView</code> or <code>ArrayBuffer</code>.</p></td></tr> 1327<tr><td>v0.1.90</td> 1328<td><p><span>Added in: v0.1.90</span></p></td></tr> 1329</tbody></table> 1330</details> 1331</div> 1332<ul> 1333<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <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> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer" class="type"><SharedArrayBuffer></a> A 1334value to calculate the length of.</li> 1335<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> If <code>string</code> is a string, this is its encoding. 1336<strong>Default:</strong> <code>'utf8'</code>.</li> 1337<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The number of bytes contained within <code>string</code>.</li> 1338</ul> 1339<p>Returns the byte length of a string when encoded using <code>encoding</code>. 1340This is not the same as <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length"><code>String.prototype.length</code></a>, which does not account 1341for the encoding that is used to convert the string into bytes.</p> 1342<p>For <code>'base64'</code>, <code>'base64url'</code>, and <code>'hex'</code>, this function assumes valid input. 1343For strings that contain non-base64/hex-encoded data (e.g. whitespace), the 1344return value might be greater than the length of a <code>Buffer</code> created from the 1345string.</p> 1346 1347<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1348 1349<span class="hljs-keyword">const</span> str = <span class="hljs-string">'\u00bd + \u00bc = \u00be'</span>; 1350 1351<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${str}</span>: <span class="hljs-subst">${str.length}</span> characters, `</span> + 1352 <span class="hljs-string">`<span class="hljs-subst">${Buffer.byteLength(str, <span class="hljs-string">'utf8'</span>)}</span> bytes`</span>); 1353<span class="hljs-comment">// Prints: ½ + ¼ = ¾: 9 characters, 12 bytes</span></code><code class="language-js cjs"><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>); 1354 1355<span class="hljs-keyword">const</span> str = <span class="hljs-string">'\u00bd + \u00bc = \u00be'</span>; 1356 1357<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${str}</span>: <span class="hljs-subst">${str.length}</span> characters, `</span> + 1358 <span class="hljs-string">`<span class="hljs-subst">${Buffer.byteLength(str, <span class="hljs-string">'utf8'</span>)}</span> bytes`</span>); 1359<span class="hljs-comment">// Prints: ½ + ¼ = ¾: 9 characters, 12 bytes</span></code><button class="copy-button">copy</button></pre> 1360<p>When <code>string</code> is a <code>Buffer</code>/<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView"><code>DataView</code></a>/<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>/<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a>/ 1361<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"><code>SharedArrayBuffer</code></a>, the byte length as reported by <code>.byteLength</code> 1362is returned.</p> 1363<h4>Static method: <code>Buffer.compare(buf1, buf2)</code><span><a class="mark" href="#static-method-buffercomparebuf1-buf2" id="static-method-buffercomparebuf1-buf2">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_compare_buf1_buf2"></a></h4> 1364<div class="api_metadata"> 1365<details class="changelog"><summary>History</summary> 1366<table> 1367<tbody><tr><th>Version</th><th>Changes</th></tr> 1368<tr><td>v8.0.0</td> 1369<td><p>The arguments can now be <code>Uint8Array</code>s.</p></td></tr> 1370<tr><td>v0.11.13</td> 1371<td><p><span>Added in: v0.11.13</span></p></td></tr> 1372</tbody></table> 1373</details> 1374</div> 1375<ul> 1376<li><code>buf1</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/Uint8Array" class="type"><Uint8Array></a></li> 1377<li><code>buf2</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/Uint8Array" class="type"><Uint8Array></a></li> 1378<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Either <code>-1</code>, <code>0</code>, or <code>1</code>, depending on the result of the 1379comparison. See <a href="#bufcomparetarget-targetstart-targetend-sourcestart-sourceend"><code>buf.compare()</code></a> for details.</li> 1380</ul> 1381<p>Compares <code>buf1</code> to <code>buf2</code>, typically for the purpose of sorting arrays of 1382<code>Buffer</code> instances. This is equivalent to calling 1383<a href="#bufcomparetarget-targetstart-targetend-sourcestart-sourceend"><code>buf1.compare(buf2)</code></a>.</p> 1384 1385<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1386 1387<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'1234'</span>); 1388<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'0123'</span>); 1389<span class="hljs-keyword">const</span> arr = [buf1, buf2]; 1390 1391<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(arr.<span class="hljs-title function_">sort</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-property">compare</span>)); 1392<span class="hljs-comment">// Prints: [ <Buffer 30 31 32 33>, <Buffer 31 32 33 34> ]</span> 1393<span class="hljs-comment">// (This result is equal to: [buf2, buf1].)</span></code><code class="language-js cjs"><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>); 1394 1395<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'1234'</span>); 1396<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'0123'</span>); 1397<span class="hljs-keyword">const</span> arr = [buf1, buf2]; 1398 1399<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(arr.<span class="hljs-title function_">sort</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-property">compare</span>)); 1400<span class="hljs-comment">// Prints: [ <Buffer 30 31 32 33>, <Buffer 31 32 33 34> ]</span> 1401<span class="hljs-comment">// (This result is equal to: [buf2, buf1].)</span></code><button class="copy-button">copy</button></pre> 1402<h4>Static method: <code>Buffer.concat(list[, totalLength])</code><span><a class="mark" href="#static-method-bufferconcatlist-totallength" id="static-method-bufferconcatlist-totallength">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_concat_list_totallength"></a></h4> 1403<div class="api_metadata"> 1404<details class="changelog"><summary>History</summary> 1405<table> 1406<tbody><tr><th>Version</th><th>Changes</th></tr> 1407<tr><td>v8.0.0</td> 1408<td><p>The elements of <code>list</code> can now be <code>Uint8Array</code>s.</p></td></tr> 1409<tr><td>v0.7.11</td> 1410<td><p><span>Added in: v0.7.11</span></p></td></tr> 1411</tbody></table> 1412</details> 1413</div> 1414<ul> 1415<li><code>list</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/Uint8Array" class="type"><Uint8Array[]></a> List of <code>Buffer</code> or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a> 1416instances to concatenate.</li> 1417<li><code>totalLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Total length of the <code>Buffer</code> instances in <code>list</code> 1418when concatenated.</li> 1419<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 1420</ul> 1421<p>Returns a new <code>Buffer</code> which is the result of concatenating all the <code>Buffer</code> 1422instances in the <code>list</code> together.</p> 1423<p>If the list has no items, or if the <code>totalLength</code> is 0, then a new zero-length 1424<code>Buffer</code> is returned.</p> 1425<p>If <code>totalLength</code> is not provided, it is calculated from the <code>Buffer</code> instances 1426in <code>list</code> by adding their lengths.</p> 1427<p>If <code>totalLength</code> is provided, it is coerced to an unsigned integer. If the 1428combined length of the <code>Buffer</code>s in <code>list</code> exceeds <code>totalLength</code>, the result is 1429truncated to <code>totalLength</code>.</p> 1430 1431<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1432 1433<span class="hljs-comment">// Create a single `Buffer` from a list of three `Buffer` instances.</span> 1434 1435<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">10</span>); 1436<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">14</span>); 1437<span class="hljs-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">18</span>); 1438<span class="hljs-keyword">const</span> totalLength = buf1.<span class="hljs-property">length</span> + buf2.<span class="hljs-property">length</span> + buf3.<span class="hljs-property">length</span>; 1439 1440<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(totalLength); 1441<span class="hljs-comment">// Prints: 42</span> 1442 1443<span class="hljs-keyword">const</span> bufA = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">concat</span>([buf1, buf2, buf3], totalLength); 1444 1445<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(bufA); 1446<span class="hljs-comment">// Prints: <Buffer 00 00 00 00 ...></span> 1447<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(bufA.<span class="hljs-property">length</span>); 1448<span class="hljs-comment">// Prints: 42</span></code><code class="language-js cjs"><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>); 1449 1450<span class="hljs-comment">// Create a single `Buffer` from a list of three `Buffer` instances.</span> 1451 1452<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">10</span>); 1453<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">14</span>); 1454<span class="hljs-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">18</span>); 1455<span class="hljs-keyword">const</span> totalLength = buf1.<span class="hljs-property">length</span> + buf2.<span class="hljs-property">length</span> + buf3.<span class="hljs-property">length</span>; 1456 1457<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(totalLength); 1458<span class="hljs-comment">// Prints: 42</span> 1459 1460<span class="hljs-keyword">const</span> bufA = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">concat</span>([buf1, buf2, buf3], totalLength); 1461 1462<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(bufA); 1463<span class="hljs-comment">// Prints: <Buffer 00 00 00 00 ...></span> 1464<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(bufA.<span class="hljs-property">length</span>); 1465<span class="hljs-comment">// Prints: 42</span></code><button class="copy-button">copy</button></pre> 1466<p><code>Buffer.concat()</code> may also use the internal <code>Buffer</code> pool like 1467<a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> does.</p> 1468<h4>Static method: <code>Buffer.copyBytesFrom(view[, offset[, length]])</code><span><a class="mark" href="#static-method-buffercopybytesfromview-offset-length" id="static-method-buffercopybytesfromview-offset-length">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_copybytesfrom_view_offset_length"></a></h4> 1469<div class="api_metadata"> 1470<span>Added in: v18.16.0</span> 1471</div> 1472<ul> 1473<li><code>view</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> The <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> to copy.</li> 1474<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The starting offset within <code>view</code>. <strong>Default:</strong>: <code>0</code>.</li> 1475<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The number of elements from <code>view</code> to copy. 1476<strong>Default:</strong> <code>view.length - offset</code>.</li> 1477</ul> 1478<p>Copies the underlying memory of <code>view</code> into a new <code>Buffer</code>.</p> 1479<pre><code class="language-js"><span class="hljs-keyword">const</span> u16 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint16Array</span>([<span class="hljs-number">0</span>, <span class="hljs-number">0xffff</span>]); 1480<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">copyBytesFrom</span>(u16, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>); 1481u16[<span class="hljs-number">1</span>] = <span class="hljs-number">0</span>; 1482<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>); <span class="hljs-comment">// 2</span> 1483<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf[<span class="hljs-number">0</span>]); <span class="hljs-comment">// 255</span> 1484<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf[<span class="hljs-number">1</span>]); <span class="hljs-comment">// 255</span></code> <button class="copy-button">copy</button></pre> 1485<h4>Static method: <code>Buffer.from(array)</code><span><a class="mark" href="#static-method-bufferfromarray" id="static-method-bufferfromarray">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_from_array"></a></h4> 1486<div class="api_metadata"> 1487<span>Added in: v5.10.0</span> 1488</div> 1489<ul> 1490<li><code>array</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer[]></a></li> 1491</ul> 1492<p>Allocates a new <code>Buffer</code> using an <code>array</code> of bytes in the range <code>0</code> – <code>255</code>. 1493Array entries outside that range will be truncated to fit into it.</p> 1494 1495<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1496 1497<span class="hljs-comment">// Creates a new Buffer containing the UTF-8 bytes of the string 'buffer'.</span> 1498<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x62</span>, <span class="hljs-number">0x75</span>, <span class="hljs-number">0x66</span>, <span class="hljs-number">0x66</span>, <span class="hljs-number">0x65</span>, <span class="hljs-number">0x72</span>]);</code><code class="language-js cjs"><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>); 1499 1500<span class="hljs-comment">// Creates a new Buffer containing the UTF-8 bytes of the string 'buffer'.</span> 1501<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x62</span>, <span class="hljs-number">0x75</span>, <span class="hljs-number">0x66</span>, <span class="hljs-number">0x66</span>, <span class="hljs-number">0x65</span>, <span class="hljs-number">0x72</span>]);</code><button class="copy-button">copy</button></pre> 1502<p>If <code>array</code> is an <code>Array</code>-like object (that is, one with a <code>length</code> property of 1503type <code>number</code>), it is treated as if it is an array, unless it is a <code>Buffer</code> or 1504a <code>Uint8Array</code>. This means all other <code>TypedArray</code> variants get treated as an 1505<code>Array</code>. To create a <code>Buffer</code> from the bytes backing a <code>TypedArray</code>, use 1506<a href="#static-method-buffercopybytesfromview-offset-length"><code>Buffer.copyBytesFrom()</code></a>.</p> 1507<p>A <code>TypeError</code> will be thrown if <code>array</code> is not an <code>Array</code> or another type 1508appropriate for <code>Buffer.from()</code> variants.</p> 1509<p><code>Buffer.from(array)</code> and <a href="#static-method-bufferfromstring-encoding"><code>Buffer.from(string)</code></a> may also use the internal 1510<code>Buffer</code> pool like <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> does.</p> 1511<h4>Static method: <code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code><span><a class="mark" href="#static-method-bufferfromarraybuffer-byteoffset-length" id="static-method-bufferfromarraybuffer-byteoffset-length">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_from_arraybuffer_byteoffset_length"></a></h4> 1512<div class="api_metadata"> 1513<span>Added in: v5.10.0</span> 1514</div> 1515<ul> 1516<li><code>arrayBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer" class="type"><SharedArrayBuffer></a> An <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a>, 1517<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"><code>SharedArrayBuffer</code></a>, for example the <code>.buffer</code> property of a 1518<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>.</li> 1519<li><code>byteOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Index of first byte to expose. <strong>Default:</strong> <code>0</code>.</li> 1520<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to expose. 1521<strong>Default:</strong> <code>arrayBuffer.byteLength - byteOffset</code>.</li> 1522</ul> 1523<p>This creates a view of the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> without copying the underlying 1524memory. For example, when passed a reference to the <code>.buffer</code> property of a 1525<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> instance, the newly created <code>Buffer</code> will share the same 1526allocated memory as the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>'s underlying <code>ArrayBuffer</code>.</p> 1527 1528<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1529 1530<span class="hljs-keyword">const</span> arr = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint16Array</span>(<span class="hljs-number">2</span>); 1531 1532arr[<span class="hljs-number">0</span>] = <span class="hljs-number">5000</span>; 1533arr[<span class="hljs-number">1</span>] = <span class="hljs-number">4000</span>; 1534 1535<span class="hljs-comment">// Shares memory with `arr`.</span> 1536<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arr.<span class="hljs-property">buffer</span>); 1537 1538<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 1539<span class="hljs-comment">// Prints: <Buffer 88 13 a0 0f></span> 1540 1541<span class="hljs-comment">// Changing the original Uint16Array changes the Buffer also.</span> 1542arr[<span class="hljs-number">1</span>] = <span class="hljs-number">6000</span>; 1543 1544<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 1545<span class="hljs-comment">// Prints: <Buffer 88 13 70 17></span></code><code class="language-js cjs"><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>); 1546 1547<span class="hljs-keyword">const</span> arr = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint16Array</span>(<span class="hljs-number">2</span>); 1548 1549arr[<span class="hljs-number">0</span>] = <span class="hljs-number">5000</span>; 1550arr[<span class="hljs-number">1</span>] = <span class="hljs-number">4000</span>; 1551 1552<span class="hljs-comment">// Shares memory with `arr`.</span> 1553<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arr.<span class="hljs-property">buffer</span>); 1554 1555<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 1556<span class="hljs-comment">// Prints: <Buffer 88 13 a0 0f></span> 1557 1558<span class="hljs-comment">// Changing the original Uint16Array changes the Buffer also.</span> 1559arr[<span class="hljs-number">1</span>] = <span class="hljs-number">6000</span>; 1560 1561<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 1562<span class="hljs-comment">// Prints: <Buffer 88 13 70 17></span></code><button class="copy-button">copy</button></pre> 1563<p>The optional <code>byteOffset</code> and <code>length</code> arguments specify a memory range within 1564the <code>arrayBuffer</code> that will be shared by the <code>Buffer</code>.</p> 1565 1566<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1567 1568<span class="hljs-keyword">const</span> ab = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>(<span class="hljs-number">10</span>); 1569<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(ab, <span class="hljs-number">0</span>, <span class="hljs-number">2</span>); 1570 1571<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>); 1572<span class="hljs-comment">// Prints: 2</span></code><code class="language-js cjs"><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>); 1573 1574<span class="hljs-keyword">const</span> ab = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>(<span class="hljs-number">10</span>); 1575<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(ab, <span class="hljs-number">0</span>, <span class="hljs-number">2</span>); 1576 1577<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>); 1578<span class="hljs-comment">// Prints: 2</span></code><button class="copy-button">copy</button></pre> 1579<p>A <code>TypeError</code> will be thrown if <code>arrayBuffer</code> is not an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> or a 1580<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"><code>SharedArrayBuffer</code></a> or another type appropriate for <code>Buffer.from()</code> 1581variants.</p> 1582<p>It is important to remember that a backing <code>ArrayBuffer</code> can cover a range 1583of memory that extends beyond the bounds of a <code>TypedArray</code> view. A new 1584<code>Buffer</code> created using the <code>buffer</code> property of a <code>TypedArray</code> may extend 1585beyond the range of the <code>TypedArray</code>:</p> 1586 1587<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1588 1589<span class="hljs-keyword">const</span> arrA = <span class="hljs-title class_">Uint8Array</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x63</span>, <span class="hljs-number">0x64</span>, <span class="hljs-number">0x65</span>, <span class="hljs-number">0x66</span>]); <span class="hljs-comment">// 4 elements</span> 1590<span class="hljs-keyword">const</span> arrB = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>(arrA.<span class="hljs-property">buffer</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>); <span class="hljs-comment">// 2 elements</span> 1591<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(arrA.<span class="hljs-property">buffer</span> === arrB.<span class="hljs-property">buffer</span>); <span class="hljs-comment">// true</span> 1592 1593<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arrB.<span class="hljs-property">buffer</span>); 1594<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 1595<span class="hljs-comment">// Prints: <Buffer 63 64 65 66></span></code><code class="language-js cjs"><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>); 1596 1597<span class="hljs-keyword">const</span> arrA = <span class="hljs-title class_">Uint8Array</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x63</span>, <span class="hljs-number">0x64</span>, <span class="hljs-number">0x65</span>, <span class="hljs-number">0x66</span>]); <span class="hljs-comment">// 4 elements</span> 1598<span class="hljs-keyword">const</span> arrB = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>(arrA.<span class="hljs-property">buffer</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>); <span class="hljs-comment">// 2 elements</span> 1599<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(arrA.<span class="hljs-property">buffer</span> === arrB.<span class="hljs-property">buffer</span>); <span class="hljs-comment">// true</span> 1600 1601<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arrB.<span class="hljs-property">buffer</span>); 1602<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 1603<span class="hljs-comment">// Prints: <Buffer 63 64 65 66></span></code><button class="copy-button">copy</button></pre> 1604<h4>Static method: <code>Buffer.from(buffer)</code><span><a class="mark" href="#static-method-bufferfrombuffer" id="static-method-bufferfrombuffer">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_from_buffer"></a></h4> 1605<div class="api_metadata"> 1606<span>Added in: v5.10.0</span> 1607</div> 1608<ul> 1609<li><code>buffer</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/Uint8Array" class="type"><Uint8Array></a> An existing <code>Buffer</code> or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a> from 1610which to copy data.</li> 1611</ul> 1612<p>Copies the passed <code>buffer</code> data onto a new <code>Buffer</code> instance.</p> 1613 1614<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1615 1616<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>); 1617<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(buf1); 1618 1619buf1[<span class="hljs-number">0</span>] = <span class="hljs-number">0x61</span>; 1620 1621<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>()); 1622<span class="hljs-comment">// Prints: auffer</span> 1623<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>()); 1624<span class="hljs-comment">// Prints: buffer</span></code><code class="language-js cjs"><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>); 1625 1626<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>); 1627<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(buf1); 1628 1629buf1[<span class="hljs-number">0</span>] = <span class="hljs-number">0x61</span>; 1630 1631<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>()); 1632<span class="hljs-comment">// Prints: auffer</span> 1633<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>()); 1634<span class="hljs-comment">// Prints: buffer</span></code><button class="copy-button">copy</button></pre> 1635<p>A <code>TypeError</code> will be thrown if <code>buffer</code> is not a <code>Buffer</code> or another type 1636appropriate for <code>Buffer.from()</code> variants.</p> 1637<h4>Static method: <code>Buffer.from(object[, offsetOrEncoding[, length]])</code><span><a class="mark" href="#static-method-bufferfromobject-offsetorencoding-length" id="static-method-bufferfromobject-offsetorencoding-length">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_from_object_offsetorencoding_length"></a></h4> 1638<div class="api_metadata"> 1639<span>Added in: v8.2.0</span> 1640</div> 1641<ul> 1642<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> An object supporting <code>Symbol.toPrimitive</code> or <code>valueOf()</code>.</li> 1643<li><code>offsetOrEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A byte-offset or encoding.</li> 1644<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> A length.</li> 1645</ul> 1646<p>For objects whose <code>valueOf()</code> function returns a value not strictly equal to 1647<code>object</code>, returns <code>Buffer.from(object.valueOf(), offsetOrEncoding, length)</code>.</p> 1648 1649<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1650 1651<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>(<span class="hljs-string">'this is a test'</span>)); 1652<span class="hljs-comment">// Prints: <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74></span></code><code class="language-js cjs"><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>); 1653 1654<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>(<span class="hljs-string">'this is a test'</span>)); 1655<span class="hljs-comment">// Prints: <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74></span></code><button class="copy-button">copy</button></pre> 1656<p>For objects that support <code>Symbol.toPrimitive</code>, returns 1657<code>Buffer.from(object[Symbol.toPrimitive]('string'), offsetOrEncoding)</code>.</p> 1658 1659<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1660 1661<span class="hljs-keyword">class</span> <span class="hljs-title class_">Foo</span> { 1662 [<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">toPrimitive</span>]() { 1663 <span class="hljs-keyword">return</span> <span class="hljs-string">'this is a test'</span>; 1664 } 1665} 1666 1667<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Foo</span>(), <span class="hljs-string">'utf8'</span>); 1668<span class="hljs-comment">// Prints: <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74></span></code><code class="language-js cjs"><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>); 1669 1670<span class="hljs-keyword">class</span> <span class="hljs-title class_">Foo</span> { 1671 [<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">toPrimitive</span>]() { 1672 <span class="hljs-keyword">return</span> <span class="hljs-string">'this is a test'</span>; 1673 } 1674} 1675 1676<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Foo</span>(), <span class="hljs-string">'utf8'</span>); 1677<span class="hljs-comment">// Prints: <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74></span></code><button class="copy-button">copy</button></pre> 1678<p>A <code>TypeError</code> will be thrown if <code>object</code> does not have the mentioned methods or 1679is not of another type appropriate for <code>Buffer.from()</code> variants.</p> 1680<h4>Static method: <code>Buffer.from(string[, encoding])</code><span><a class="mark" href="#static-method-bufferfromstring-encoding" id="static-method-bufferfromstring-encoding">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_from_string_encoding"></a></h4> 1681<div class="api_metadata"> 1682<span>Added in: v5.10.0</span> 1683</div> 1684<ul> 1685<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A string to encode.</li> 1686<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The encoding of <code>string</code>. <strong>Default:</strong> <code>'utf8'</code>.</li> 1687</ul> 1688<p>Creates a new <code>Buffer</code> containing <code>string</code>. The <code>encoding</code> parameter identifies 1689the character encoding to be used when converting <code>string</code> into bytes.</p> 1690 1691<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1692 1693<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'this is a tést'</span>); 1694<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'7468697320697320612074c3a97374'</span>, <span class="hljs-string">'hex'</span>); 1695 1696<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>()); 1697<span class="hljs-comment">// Prints: this is a tést</span> 1698<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>()); 1699<span class="hljs-comment">// Prints: this is a tést</span> 1700<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'latin1'</span>)); 1701<span class="hljs-comment">// Prints: this is a tést</span></code><code class="language-js cjs"><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>); 1702 1703<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'this is a tést'</span>); 1704<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'7468697320697320612074c3a97374'</span>, <span class="hljs-string">'hex'</span>); 1705 1706<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>()); 1707<span class="hljs-comment">// Prints: this is a tést</span> 1708<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>()); 1709<span class="hljs-comment">// Prints: this is a tést</span> 1710<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'latin1'</span>)); 1711<span class="hljs-comment">// Prints: this is a tést</span></code><button class="copy-button">copy</button></pre> 1712<p>A <code>TypeError</code> will be thrown if <code>string</code> is not a string or another type 1713appropriate for <code>Buffer.from()</code> variants.</p> 1714<h4>Static method: <code>Buffer.isBuffer(obj)</code><span><a class="mark" href="#static-method-bufferisbufferobj" id="static-method-bufferisbufferobj">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_isbuffer_obj"></a></h4> 1715<div class="api_metadata"> 1716<span>Added in: v0.1.101</span> 1717</div> 1718<ul> 1719<li><code>obj</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 1720<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1721</ul> 1722<p>Returns <code>true</code> if <code>obj</code> is a <code>Buffer</code>, <code>false</code> otherwise.</p> 1723 1724<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1725 1726<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">10</span>)); <span class="hljs-comment">// true</span> 1727<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'foo'</span>)); <span class="hljs-comment">// true</span> 1728<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>(<span class="hljs-string">'a string'</span>); <span class="hljs-comment">// false</span> 1729<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>([]); <span class="hljs-comment">// false</span> 1730<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>(<span class="hljs-number">1024</span>)); <span class="hljs-comment">// false</span></code><code class="language-js cjs"><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>); 1731 1732<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">10</span>)); <span class="hljs-comment">// true</span> 1733<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'foo'</span>)); <span class="hljs-comment">// true</span> 1734<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>(<span class="hljs-string">'a string'</span>); <span class="hljs-comment">// false</span> 1735<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>([]); <span class="hljs-comment">// false</span> 1736<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>(<span class="hljs-number">1024</span>)); <span class="hljs-comment">// false</span></code><button class="copy-button">copy</button></pre> 1737<h4>Static method: <code>Buffer.isEncoding(encoding)</code><span><a class="mark" href="#static-method-bufferisencodingencoding" id="static-method-bufferisencodingencoding">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_isencoding_encoding"></a></h4> 1738<div class="api_metadata"> 1739<span>Added in: v0.9.1</span> 1740</div> 1741<ul> 1742<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A character encoding name to check.</li> 1743<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1744</ul> 1745<p>Returns <code>true</code> if <code>encoding</code> is the name of a supported character encoding, 1746or <code>false</code> otherwise.</p> 1747 1748<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1749 1750<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_">isEncoding</span>(<span class="hljs-string">'utf8'</span>)); 1751<span class="hljs-comment">// Prints: true</span> 1752 1753<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_">isEncoding</span>(<span class="hljs-string">'hex'</span>)); 1754<span class="hljs-comment">// Prints: true</span> 1755 1756<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_">isEncoding</span>(<span class="hljs-string">'utf/8'</span>)); 1757<span class="hljs-comment">// Prints: false</span> 1758 1759<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_">isEncoding</span>(<span class="hljs-string">''</span>)); 1760<span class="hljs-comment">// Prints: false</span></code><code class="language-js cjs"><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>); 1761 1762<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_">isEncoding</span>(<span class="hljs-string">'utf8'</span>)); 1763<span class="hljs-comment">// Prints: true</span> 1764 1765<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_">isEncoding</span>(<span class="hljs-string">'hex'</span>)); 1766<span class="hljs-comment">// Prints: true</span> 1767 1768<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_">isEncoding</span>(<span class="hljs-string">'utf/8'</span>)); 1769<span class="hljs-comment">// Prints: false</span> 1770 1771<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_">isEncoding</span>(<span class="hljs-string">''</span>)); 1772<span class="hljs-comment">// Prints: false</span></code><button class="copy-button">copy</button></pre> 1773<h4>Class property: <code>Buffer.poolSize</code><span><a class="mark" href="#class-property-bufferpoolsize" id="class-property-bufferpoolsize">#</a></span><a aria-hidden="true" class="legacy" id="buffer_class_property_buffer_poolsize"></a></h4> 1774<div class="api_metadata"> 1775<span>Added in: v0.11.3</span> 1776</div> 1777<ul> 1778<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>8192</code></li> 1779</ul> 1780<p>This is the size (in bytes) of pre-allocated internal <code>Buffer</code> instances used 1781for pooling. This value may be modified.</p> 1782<h4><code>buf[index]</code><span><a class="mark" href="#bufindex" id="bufindex">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_index"></a></h4> 1783<ul> 1784<li><code>index</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1785</ul> 1786<p>The index operator <code>[index]</code> can be used to get and set the octet at position 1787<code>index</code> in <code>buf</code>. The values refer to individual bytes, so the legal value 1788range is between <code>0x00</code> and <code>0xFF</code> (hex) or <code>0</code> and <code>255</code> (decimal).</p> 1789<p>This operator is inherited from <code>Uint8Array</code>, so its behavior on out-of-bounds 1790access is the same as <code>Uint8Array</code>. In other words, <code>buf[index]</code> returns 1791<code>undefined</code> when <code>index</code> is negative or greater or equal to <code>buf.length</code>, and 1792<code>buf[index] = value</code> does not modify the buffer if <code>index</code> is negative or 1793<code>>= buf.length</code>.</p> 1794 1795<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1796 1797<span class="hljs-comment">// Copy an ASCII string into a `Buffer` one byte at a time.</span> 1798<span class="hljs-comment">// (This only works for ASCII-only strings. In general, one should use</span> 1799<span class="hljs-comment">// `Buffer.from()` to perform this conversion.)</span> 1800 1801<span class="hljs-keyword">const</span> str = <span class="hljs-string">'Node.js'</span>; 1802<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(str.<span class="hljs-property">length</span>); 1803 1804<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i < str.<span class="hljs-property">length</span>; i++) { 1805 buf[i] = str.<span class="hljs-title function_">charCodeAt</span>(i); 1806} 1807 1808<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'utf8'</span>)); 1809<span class="hljs-comment">// Prints: Node.js</span></code><code class="language-js cjs"><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>); 1810 1811<span class="hljs-comment">// Copy an ASCII string into a `Buffer` one byte at a time.</span> 1812<span class="hljs-comment">// (This only works for ASCII-only strings. In general, one should use</span> 1813<span class="hljs-comment">// `Buffer.from()` to perform this conversion.)</span> 1814 1815<span class="hljs-keyword">const</span> str = <span class="hljs-string">'Node.js'</span>; 1816<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(str.<span class="hljs-property">length</span>); 1817 1818<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i < str.<span class="hljs-property">length</span>; i++) { 1819 buf[i] = str.<span class="hljs-title function_">charCodeAt</span>(i); 1820} 1821 1822<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'utf8'</span>)); 1823<span class="hljs-comment">// Prints: Node.js</span></code><button class="copy-button">copy</button></pre> 1824<h4><code>buf.buffer</code><span><a class="mark" href="#bufbuffer" id="bufbuffer">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_buffer"></a></h4> 1825<ul> 1826<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> The underlying <code>ArrayBuffer</code> object based on which this <code>Buffer</code> 1827object is created.</li> 1828</ul> 1829<p>This <code>ArrayBuffer</code> is not guaranteed to correspond exactly to the original 1830<code>Buffer</code>. See the notes on <code>buf.byteOffset</code> for details.</p> 1831 1832<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1833 1834<span class="hljs-keyword">const</span> arrayBuffer = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>(<span class="hljs-number">16</span>); 1835<span class="hljs-keyword">const</span> buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arrayBuffer); 1836 1837<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buffer.<span class="hljs-property">buffer</span> === arrayBuffer); 1838<span class="hljs-comment">// Prints: true</span></code><code class="language-js cjs"><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>); 1839 1840<span class="hljs-keyword">const</span> arrayBuffer = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>(<span class="hljs-number">16</span>); 1841<span class="hljs-keyword">const</span> buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arrayBuffer); 1842 1843<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buffer.<span class="hljs-property">buffer</span> === arrayBuffer); 1844<span class="hljs-comment">// Prints: true</span></code><button class="copy-button">copy</button></pre> 1845<h4><code>buf.byteOffset</code><span><a class="mark" href="#bufbyteoffset" id="bufbyteoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_byteoffset"></a></h4> 1846<ul> 1847<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The <code>byteOffset</code> of the <code>Buffer</code>s underlying <code>ArrayBuffer</code> object.</li> 1848</ul> 1849<p>When setting <code>byteOffset</code> in <code>Buffer.from(ArrayBuffer, byteOffset, length)</code>, 1850or sometimes when allocating a <code>Buffer</code> smaller than <code>Buffer.poolSize</code>, the 1851buffer does not start from a zero offset on the underlying <code>ArrayBuffer</code>.</p> 1852<p>This can cause problems when accessing the underlying <code>ArrayBuffer</code> directly 1853using <code>buf.buffer</code>, as other parts of the <code>ArrayBuffer</code> may be unrelated 1854to the <code>Buffer</code> object itself.</p> 1855<p>A common issue when creating a <code>TypedArray</code> object that shares its memory with 1856a <code>Buffer</code> is that in this case one needs to specify the <code>byteOffset</code> correctly:</p> 1857 1858<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1859 1860<span class="hljs-comment">// Create a buffer smaller than `Buffer.poolSize`.</span> 1861<span class="hljs-keyword">const</span> nodeBuffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>]); 1862 1863<span class="hljs-comment">// When casting the Node.js Buffer to an Int8Array, use the byteOffset</span> 1864<span class="hljs-comment">// to refer only to the part of `nodeBuffer.buffer` that contains the memory</span> 1865<span class="hljs-comment">// for `nodeBuffer`.</span> 1866<span class="hljs-keyword">new</span> <span class="hljs-title class_">Int8Array</span>(nodeBuffer.<span class="hljs-property">buffer</span>, nodeBuffer.<span class="hljs-property">byteOffset</span>, nodeBuffer.<span class="hljs-property">length</span>);</code><code class="language-js cjs"><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>); 1867 1868<span class="hljs-comment">// Create a buffer smaller than `Buffer.poolSize`.</span> 1869<span class="hljs-keyword">const</span> nodeBuffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>]); 1870 1871<span class="hljs-comment">// When casting the Node.js Buffer to an Int8Array, use the byteOffset</span> 1872<span class="hljs-comment">// to refer only to the part of `nodeBuffer.buffer` that contains the memory</span> 1873<span class="hljs-comment">// for `nodeBuffer`.</span> 1874<span class="hljs-keyword">new</span> <span class="hljs-title class_">Int8Array</span>(nodeBuffer.<span class="hljs-property">buffer</span>, nodeBuffer.<span class="hljs-property">byteOffset</span>, nodeBuffer.<span class="hljs-property">length</span>);</code><button class="copy-button">copy</button></pre> 1875<h4><code>buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])</code><span><a class="mark" href="#bufcomparetarget-targetstart-targetend-sourcestart-sourceend" id="bufcomparetarget-targetstart-targetend-sourcestart-sourceend">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_compare_target_targetstart_targetend_sourcestart_sourceend"></a></h4> 1876<div class="api_metadata"> 1877<details class="changelog"><summary>History</summary> 1878<table> 1879<tbody><tr><th>Version</th><th>Changes</th></tr> 1880<tr><td>v8.0.0</td> 1881<td><p>The <code>target</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 1882<tr><td>v5.11.0</td> 1883<td><p>Additional parameters for specifying offsets are supported now.</p></td></tr> 1884<tr><td>v0.11.13</td> 1885<td><p><span>Added in: v0.11.13</span></p></td></tr> 1886</tbody></table> 1887</details> 1888</div> 1889<ul> 1890<li><code>target</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/Uint8Array" class="type"><Uint8Array></a> A <code>Buffer</code> or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a> with which to 1891compare <code>buf</code>.</li> 1892<li><code>targetStart</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The offset within <code>target</code> at which to begin 1893comparison. <strong>Default:</strong> <code>0</code>.</li> 1894<li><code>targetEnd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The offset within <code>target</code> at which to end comparison 1895(not inclusive). <strong>Default:</strong> <code>target.length</code>.</li> 1896<li><code>sourceStart</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The offset within <code>buf</code> at which to begin comparison. 1897<strong>Default:</strong> <code>0</code>.</li> 1898<li><code>sourceEnd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The offset within <code>buf</code> at which to end comparison 1899(not inclusive). <strong>Default:</strong> <a href="#buflength"><code>buf.length</code></a>.</li> 1900<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1901</ul> 1902<p>Compares <code>buf</code> with <code>target</code> and returns a number indicating whether <code>buf</code> 1903comes before, after, or is the same as <code>target</code> in sort order. 1904Comparison is based on the actual sequence of bytes in each <code>Buffer</code>.</p> 1905<ul> 1906<li><code>0</code> is returned if <code>target</code> is the same as <code>buf</code></li> 1907<li><code>1</code> is returned if <code>target</code> should come <em>before</em> <code>buf</code> when sorted.</li> 1908<li><code>-1</code> is returned if <code>target</code> should come <em>after</em> <code>buf</code> when sorted.</li> 1909</ul> 1910 1911<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1912 1913<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'ABC'</span>); 1914<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'BCD'</span>); 1915<span class="hljs-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'ABCD'</span>); 1916 1917<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf1)); 1918<span class="hljs-comment">// Prints: 0</span> 1919<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf2)); 1920<span class="hljs-comment">// Prints: -1</span> 1921<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf3)); 1922<span class="hljs-comment">// Prints: -1</span> 1923<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">compare</span>(buf1)); 1924<span class="hljs-comment">// Prints: 1</span> 1925<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">compare</span>(buf3)); 1926<span class="hljs-comment">// Prints: 1</span> 1927<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>([buf1, buf2, buf3].<span class="hljs-title function_">sort</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-property">compare</span>)); 1928<span class="hljs-comment">// Prints: [ <Buffer 41 42 43>, <Buffer 41 42 43 44>, <Buffer 42 43 44> ]</span> 1929<span class="hljs-comment">// (This result is equal to: [buf1, buf3, buf2].)</span></code><code class="language-js cjs"><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>); 1930 1931<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'ABC'</span>); 1932<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'BCD'</span>); 1933<span class="hljs-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'ABCD'</span>); 1934 1935<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf1)); 1936<span class="hljs-comment">// Prints: 0</span> 1937<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf2)); 1938<span class="hljs-comment">// Prints: -1</span> 1939<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf3)); 1940<span class="hljs-comment">// Prints: -1</span> 1941<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">compare</span>(buf1)); 1942<span class="hljs-comment">// Prints: 1</span> 1943<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">compare</span>(buf3)); 1944<span class="hljs-comment">// Prints: 1</span> 1945<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>([buf1, buf2, buf3].<span class="hljs-title function_">sort</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-property">compare</span>)); 1946<span class="hljs-comment">// Prints: [ <Buffer 41 42 43>, <Buffer 41 42 43 44>, <Buffer 42 43 44> ]</span> 1947<span class="hljs-comment">// (This result is equal to: [buf1, buf3, buf2].)</span></code><button class="copy-button">copy</button></pre> 1948<p>The optional <code>targetStart</code>, <code>targetEnd</code>, <code>sourceStart</code>, and <code>sourceEnd</code> 1949arguments can be used to limit the comparison to specific ranges within <code>target</code> 1950and <code>buf</code> respectively.</p> 1951 1952<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1953 1954<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>]); 1955<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]); 1956 1957<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf2, <span class="hljs-number">5</span>, <span class="hljs-number">9</span>, <span class="hljs-number">0</span>, <span class="hljs-number">4</span>)); 1958<span class="hljs-comment">// Prints: 0</span> 1959<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf2, <span class="hljs-number">0</span>, <span class="hljs-number">6</span>, <span class="hljs-number">4</span>)); 1960<span class="hljs-comment">// Prints: -1</span> 1961<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf2, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">5</span>)); 1962<span class="hljs-comment">// Prints: 1</span></code><code class="language-js cjs"><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>); 1963 1964<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>]); 1965<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]); 1966 1967<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf2, <span class="hljs-number">5</span>, <span class="hljs-number">9</span>, <span class="hljs-number">0</span>, <span class="hljs-number">4</span>)); 1968<span class="hljs-comment">// Prints: 0</span> 1969<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf2, <span class="hljs-number">0</span>, <span class="hljs-number">6</span>, <span class="hljs-number">4</span>)); 1970<span class="hljs-comment">// Prints: -1</span> 1971<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf2, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">5</span>)); 1972<span class="hljs-comment">// Prints: 1</span></code><button class="copy-button">copy</button></pre> 1973<p><a href="errors.html#err_out_of_range"><code>ERR_OUT_OF_RANGE</code></a> is thrown if <code>targetStart < 0</code>, <code>sourceStart < 0</code>, 1974<code>targetEnd > target.byteLength</code>, or <code>sourceEnd > source.byteLength</code>.</p> 1975<h4><code>buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])</code><span><a class="mark" href="#bufcopytarget-targetstart-sourcestart-sourceend" id="bufcopytarget-targetstart-sourcestart-sourceend">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_copy_target_targetstart_sourcestart_sourceend"></a></h4> 1976<div class="api_metadata"> 1977<span>Added in: v0.1.90</span> 1978</div> 1979<ul> 1980<li><code>target</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/Uint8Array" class="type"><Uint8Array></a> A <code>Buffer</code> or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a> to copy into.</li> 1981<li><code>targetStart</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The offset within <code>target</code> at which to begin 1982writing. <strong>Default:</strong> <code>0</code>.</li> 1983<li><code>sourceStart</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The offset within <code>buf</code> from which to begin copying. 1984<strong>Default:</strong> <code>0</code>.</li> 1985<li><code>sourceEnd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The offset within <code>buf</code> at which to stop copying (not 1986inclusive). <strong>Default:</strong> <a href="#buflength"><code>buf.length</code></a>.</li> 1987<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The number of bytes copied.</li> 1988</ul> 1989<p>Copies data from a region of <code>buf</code> to a region in <code>target</code>, even if the <code>target</code> 1990memory region overlaps with <code>buf</code>.</p> 1991<p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/set"><code>TypedArray.prototype.set()</code></a> performs the same operation, and is available 1992for all TypedArrays, including Node.js <code>Buffer</code>s, although it takes 1993different function arguments.</p> 1994 1995<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1996 1997<span class="hljs-comment">// Create two `Buffer` instances.</span> 1998<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">26</span>); 1999<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">26</span>).<span class="hljs-title function_">fill</span>(<span class="hljs-string">'!'</span>); 2000 2001<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">26</span>; i++) { 2002 <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> 2003 buf1[i] = i + <span class="hljs-number">97</span>; 2004} 2005 2006<span class="hljs-comment">// Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`.</span> 2007buf1.<span class="hljs-title function_">copy</span>(buf2, <span class="hljs-number">8</span>, <span class="hljs-number">16</span>, <span class="hljs-number">20</span>); 2008<span class="hljs-comment">// This is equivalent to:</span> 2009<span class="hljs-comment">// buf2.set(buf1.subarray(16, 20), 8);</span> 2010 2011<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'ascii'</span>, <span class="hljs-number">0</span>, <span class="hljs-number">25</span>)); 2012<span class="hljs-comment">// Prints: !!!!!!!!qrst!!!!!!!!!!!!!</span></code><code class="language-js cjs"><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>); 2013 2014<span class="hljs-comment">// Create two `Buffer` instances.</span> 2015<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">26</span>); 2016<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">26</span>).<span class="hljs-title function_">fill</span>(<span class="hljs-string">'!'</span>); 2017 2018<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">26</span>; i++) { 2019 <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> 2020 buf1[i] = i + <span class="hljs-number">97</span>; 2021} 2022 2023<span class="hljs-comment">// Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`.</span> 2024buf1.<span class="hljs-title function_">copy</span>(buf2, <span class="hljs-number">8</span>, <span class="hljs-number">16</span>, <span class="hljs-number">20</span>); 2025<span class="hljs-comment">// This is equivalent to:</span> 2026<span class="hljs-comment">// buf2.set(buf1.subarray(16, 20), 8);</span> 2027 2028<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'ascii'</span>, <span class="hljs-number">0</span>, <span class="hljs-number">25</span>)); 2029<span class="hljs-comment">// Prints: !!!!!!!!qrst!!!!!!!!!!!!!</span></code><button class="copy-button">copy</button></pre> 2030 2031<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 2032 2033<span class="hljs-comment">// Create a `Buffer` and copy data from one region to an overlapping region</span> 2034<span class="hljs-comment">// within the same `Buffer`.</span> 2035 2036<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">26</span>); 2037 2038<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">26</span>; i++) { 2039 <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> 2040 buf[i] = i + <span class="hljs-number">97</span>; 2041} 2042 2043buf.<span class="hljs-title function_">copy</span>(buf, <span class="hljs-number">0</span>, <span class="hljs-number">4</span>, <span class="hljs-number">10</span>); 2044 2045<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>()); 2046<span class="hljs-comment">// Prints: efghijghijklmnopqrstuvwxyz</span></code><code class="language-js cjs"><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>); 2047 2048<span class="hljs-comment">// Create a `Buffer` and copy data from one region to an overlapping region</span> 2049<span class="hljs-comment">// within the same `Buffer`.</span> 2050 2051<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">26</span>); 2052 2053<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">26</span>; i++) { 2054 <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> 2055 buf[i] = i + <span class="hljs-number">97</span>; 2056} 2057 2058buf.<span class="hljs-title function_">copy</span>(buf, <span class="hljs-number">0</span>, <span class="hljs-number">4</span>, <span class="hljs-number">10</span>); 2059 2060<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>()); 2061<span class="hljs-comment">// Prints: efghijghijklmnopqrstuvwxyz</span></code><button class="copy-button">copy</button></pre> 2062<h4><code>buf.entries()</code><span><a class="mark" href="#bufentries" id="bufentries">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_entries"></a></h4> 2063<div class="api_metadata"> 2064<span>Added in: v1.1.0</span> 2065</div> 2066<ul> 2067<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> 2068</ul> 2069<p>Creates and returns an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols">iterator</a> of <code>[index, byte]</code> pairs from the contents 2070of <code>buf</code>.</p> 2071 2072<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 2073 2074<span class="hljs-comment">// Log the entire contents of a `Buffer`.</span> 2075 2076<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>); 2077 2078<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> pair <span class="hljs-keyword">of</span> buf.<span class="hljs-title function_">entries</span>()) { 2079 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(pair); 2080} 2081<span class="hljs-comment">// Prints:</span> 2082<span class="hljs-comment">// [0, 98]</span> 2083<span class="hljs-comment">// [1, 117]</span> 2084<span class="hljs-comment">// [2, 102]</span> 2085<span class="hljs-comment">// [3, 102]</span> 2086<span class="hljs-comment">// [4, 101]</span> 2087<span class="hljs-comment">// [5, 114]</span></code><code class="language-js cjs"><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>); 2088 2089<span class="hljs-comment">// Log the entire contents of a `Buffer`.</span> 2090 2091<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>); 2092 2093<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> pair <span class="hljs-keyword">of</span> buf.<span class="hljs-title function_">entries</span>()) { 2094 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(pair); 2095} 2096<span class="hljs-comment">// Prints:</span> 2097<span class="hljs-comment">// [0, 98]</span> 2098<span class="hljs-comment">// [1, 117]</span> 2099<span class="hljs-comment">// [2, 102]</span> 2100<span class="hljs-comment">// [3, 102]</span> 2101<span class="hljs-comment">// [4, 101]</span> 2102<span class="hljs-comment">// [5, 114]</span></code><button class="copy-button">copy</button></pre> 2103<h4><code>buf.equals(otherBuffer)</code><span><a class="mark" href="#bufequalsotherbuffer" id="bufequalsotherbuffer">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_equals_otherbuffer"></a></h4> 2104<div class="api_metadata"> 2105<details class="changelog"><summary>History</summary> 2106<table> 2107<tbody><tr><th>Version</th><th>Changes</th></tr> 2108<tr><td>v8.0.0</td> 2109<td><p>The arguments can now be <code>Uint8Array</code>s.</p></td></tr> 2110<tr><td>v0.11.13</td> 2111<td><p><span>Added in: v0.11.13</span></p></td></tr> 2112</tbody></table> 2113</details> 2114</div> 2115<ul> 2116<li><code>otherBuffer</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/Uint8Array" class="type"><Uint8Array></a> A <code>Buffer</code> or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a> with which to 2117compare <code>buf</code>.</li> 2118<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2119</ul> 2120<p>Returns <code>true</code> if both <code>buf</code> and <code>otherBuffer</code> have exactly the same bytes, 2121<code>false</code> otherwise. Equivalent to 2122<a href="#bufcomparetarget-targetstart-targetend-sourcestart-sourceend"><code>buf.compare(otherBuffer) === 0</code></a>.</p> 2123 2124<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 2125 2126<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'ABC'</span>); 2127<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'414243'</span>, <span class="hljs-string">'hex'</span>); 2128<span class="hljs-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'ABCD'</span>); 2129 2130<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">equals</span>(buf2)); 2131<span class="hljs-comment">// Prints: true</span> 2132<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">equals</span>(buf3)); 2133<span class="hljs-comment">// Prints: false</span></code><code class="language-js cjs"><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>); 2134 2135<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'ABC'</span>); 2136<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'414243'</span>, <span class="hljs-string">'hex'</span>); 2137<span class="hljs-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'ABCD'</span>); 2138 2139<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">equals</span>(buf2)); 2140<span class="hljs-comment">// Prints: true</span> 2141<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">equals</span>(buf3)); 2142<span class="hljs-comment">// Prints: false</span></code><button class="copy-button">copy</button></pre> 2143<h4><code>buf.fill(value[, offset[, end]][, encoding])</code><span><a class="mark" href="#buffillvalue-offset-end-encoding" id="buffillvalue-offset-end-encoding">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_fill_value_offset_end_encoding"></a></h4> 2144<div class="api_metadata"> 2145<details class="changelog"><summary>History</summary> 2146<table> 2147<tbody><tr><th>Version</th><th>Changes</th></tr> 2148<tr><td>v11.0.0</td> 2149<td><p>Throws <code>ERR_OUT_OF_RANGE</code> instead of <code>ERR_INDEX_OUT_OF_RANGE</code>.</p></td></tr> 2150<tr><td>v10.0.0</td> 2151<td><p>Negative <code>end</code> values throw an <code>ERR_INDEX_OUT_OF_RANGE</code> error.</p></td></tr> 2152<tr><td>v10.0.0</td> 2153<td><p>Attempting to fill a non-zero length buffer with a zero length buffer triggers a thrown exception.</p></td></tr> 2154<tr><td>v10.0.0</td> 2155<td><p>Specifying an invalid string for <code>value</code> triggers a thrown exception.</p></td></tr> 2156<tr><td>v5.7.0</td> 2157<td><p>The <code>encoding</code> parameter is supported now.</p></td></tr> 2158<tr><td>v0.5.0</td> 2159<td><p><span>Added in: v0.5.0</span></p></td></tr> 2160</tbody></table> 2161</details> 2162</div> 2163<ul> 2164<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type"><Uint8Array></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The value with which to fill <code>buf</code>. 2165Empty value (string, Uint8Array, Buffer) is coerced to <code>0</code>.</li> 2166<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to fill <code>buf</code>. 2167<strong>Default:</strong> <code>0</code>.</li> 2168<li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Where to stop filling <code>buf</code> (not inclusive). <strong>Default:</strong> 2169<a href="#buflength"><code>buf.length</code></a>.</li> 2170<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The encoding for <code>value</code> if <code>value</code> is a string. 2171<strong>Default:</strong> <code>'utf8'</code>.</li> 2172<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> A reference to <code>buf</code>.</li> 2173</ul> 2174<p>Fills <code>buf</code> with the specified <code>value</code>. If the <code>offset</code> and <code>end</code> are not given, 2175the entire <code>buf</code> will be filled:</p> 2176 2177<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 2178 2179<span class="hljs-comment">// Fill a `Buffer` with the ASCII character 'h'.</span> 2180 2181<span class="hljs-keyword">const</span> b = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">50</span>).<span class="hljs-title function_">fill</span>(<span class="hljs-string">'h'</span>); 2182 2183<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">toString</span>()); 2184<span class="hljs-comment">// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh</span> 2185 2186<span class="hljs-comment">// Fill a buffer with empty string</span> 2187<span class="hljs-keyword">const</span> c = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">5</span>).<span class="hljs-title function_">fill</span>(<span class="hljs-string">''</span>); 2188 2189<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(c.<span class="hljs-title function_">fill</span>(<span class="hljs-string">''</span>)); 2190<span class="hljs-comment">// Prints: <Buffer 00 00 00 00 00></span></code><code class="language-js cjs"><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>); 2191 2192<span class="hljs-comment">// Fill a `Buffer` with the ASCII character 'h'.</span> 2193 2194<span class="hljs-keyword">const</span> b = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">50</span>).<span class="hljs-title function_">fill</span>(<span class="hljs-string">'h'</span>); 2195 2196<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">toString</span>()); 2197<span class="hljs-comment">// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh</span> 2198 2199<span class="hljs-comment">// Fill a buffer with empty string</span> 2200<span class="hljs-keyword">const</span> c = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">5</span>).<span class="hljs-title function_">fill</span>(<span class="hljs-string">''</span>); 2201 2202<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(c.<span class="hljs-title function_">fill</span>(<span class="hljs-string">''</span>)); 2203<span class="hljs-comment">// Prints: <Buffer 00 00 00 00 00></span></code><button class="copy-button">copy</button></pre> 2204<p><code>value</code> is coerced to a <code>uint32</code> value if it is not a string, <code>Buffer</code>, or 2205integer. If the resulting integer is greater than <code>255</code> (decimal), <code>buf</code> will be 2206filled with <code>value & 255</code>.</p> 2207<p>If the final write of a <code>fill()</code> operation falls on a multi-byte character, 2208then only the bytes of that character that fit into <code>buf</code> are written:</p> 2209 2210<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 2211 2212<span class="hljs-comment">// Fill a `Buffer` with character that takes up two bytes in UTF-8.</span> 2213 2214<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_">allocUnsafe</span>(<span class="hljs-number">5</span>).<span class="hljs-title function_">fill</span>(<span class="hljs-string">'\u0222'</span>)); 2215<span class="hljs-comment">// Prints: <Buffer c8 a2 c8 a2 c8></span></code><code class="language-js cjs"><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>); 2216 2217<span class="hljs-comment">// Fill a `Buffer` with character that takes up two bytes in UTF-8.</span> 2218 2219<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_">allocUnsafe</span>(<span class="hljs-number">5</span>).<span class="hljs-title function_">fill</span>(<span class="hljs-string">'\u0222'</span>)); 2220<span class="hljs-comment">// Prints: <Buffer c8 a2 c8 a2 c8></span></code><button class="copy-button">copy</button></pre> 2221<p>If <code>value</code> contains invalid characters, it is truncated; if no valid 2222fill data remains, an exception is thrown:</p> 2223 2224<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 2225 2226<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">5</span>); 2227 2228<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">fill</span>(<span class="hljs-string">'a'</span>)); 2229<span class="hljs-comment">// Prints: <Buffer 61 61 61 61 61></span> 2230<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">fill</span>(<span class="hljs-string">'aazz'</span>, <span class="hljs-string">'hex'</span>)); 2231<span class="hljs-comment">// Prints: <Buffer aa aa aa aa aa></span> 2232<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">fill</span>(<span class="hljs-string">'zz'</span>, <span class="hljs-string">'hex'</span>)); 2233<span class="hljs-comment">// Throws an exception.</span></code><code class="language-js cjs"><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>); 2234 2235<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">5</span>); 2236 2237<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">fill</span>(<span class="hljs-string">'a'</span>)); 2238<span class="hljs-comment">// Prints: <Buffer 61 61 61 61 61></span> 2239<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">fill</span>(<span class="hljs-string">'aazz'</span>, <span class="hljs-string">'hex'</span>)); 2240<span class="hljs-comment">// Prints: <Buffer aa aa aa aa aa></span> 2241<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">fill</span>(<span class="hljs-string">'zz'</span>, <span class="hljs-string">'hex'</span>)); 2242<span class="hljs-comment">// Throws an exception.</span></code><button class="copy-button">copy</button></pre> 2243<h4><code>buf.includes(value[, byteOffset][, encoding])</code><span><a class="mark" href="#bufincludesvalue-byteoffset-encoding" id="bufincludesvalue-byteoffset-encoding">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_includes_value_byteoffset_encoding"></a></h4> 2244<div class="api_metadata"> 2245<span>Added in: v5.3.0</span> 2246</div> 2247<ul> 2248<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type"><Uint8Array></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> What to search for.</li> 2249<li><code>byteOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Where to begin searching in <code>buf</code>. If negative, then 2250offset is calculated from the end of <code>buf</code>. <strong>Default:</strong> <code>0</code>.</li> 2251<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> If <code>value</code> is a string, this is its encoding. 2252<strong>Default:</strong> <code>'utf8'</code>.</li> 2253<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if <code>value</code> was found in <code>buf</code>, <code>false</code> otherwise.</li> 2254</ul> 2255<p>Equivalent to <a href="#bufindexofvalue-byteoffset-encoding"><code>buf.indexOf() !== -1</code></a>.</p> 2256 2257<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 2258 2259<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'this is a buffer'</span>); 2260 2261<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-string">'this'</span>)); 2262<span class="hljs-comment">// Prints: true</span> 2263<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-string">'is'</span>)); 2264<span class="hljs-comment">// Prints: true</span> 2265<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'a buffer'</span>))); 2266<span class="hljs-comment">// Prints: true</span> 2267<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-number">97</span>)); 2268<span class="hljs-comment">// Prints: true (97 is the decimal ASCII value for 'a')</span> 2269<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'a buffer example'</span>))); 2270<span class="hljs-comment">// Prints: false</span> 2271<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'a buffer example'</span>).<span class="hljs-title function_">slice</span>(<span class="hljs-number">0</span>, <span class="hljs-number">8</span>))); 2272<span class="hljs-comment">// Prints: true</span> 2273<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-string">'this'</span>, <span class="hljs-number">4</span>)); 2274<span class="hljs-comment">// Prints: false</span></code><code class="language-js cjs"><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>); 2275 2276<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'this is a buffer'</span>); 2277 2278<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-string">'this'</span>)); 2279<span class="hljs-comment">// Prints: true</span> 2280<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-string">'is'</span>)); 2281<span class="hljs-comment">// Prints: true</span> 2282<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'a buffer'</span>))); 2283<span class="hljs-comment">// Prints: true</span> 2284<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-number">97</span>)); 2285<span class="hljs-comment">// Prints: true (97 is the decimal ASCII value for 'a')</span> 2286<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'a buffer example'</span>))); 2287<span class="hljs-comment">// Prints: false</span> 2288<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'a buffer example'</span>).<span class="hljs-title function_">slice</span>(<span class="hljs-number">0</span>, <span class="hljs-number">8</span>))); 2289<span class="hljs-comment">// Prints: true</span> 2290<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-string">'this'</span>, <span class="hljs-number">4</span>)); 2291<span class="hljs-comment">// Prints: false</span></code><button class="copy-button">copy</button></pre> 2292<h4><code>buf.indexOf(value[, byteOffset][, encoding])</code><span><a class="mark" href="#bufindexofvalue-byteoffset-encoding" id="bufindexofvalue-byteoffset-encoding">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_indexof_value_byteoffset_encoding"></a></h4> 2293<div class="api_metadata"> 2294<details class="changelog"><summary>History</summary> 2295<table> 2296<tbody><tr><th>Version</th><th>Changes</th></tr> 2297<tr><td>v8.0.0</td> 2298<td><p>The <code>value</code> can now be a <code>Uint8Array</code>.</p></td></tr> 2299<tr><td>v5.7.0, v4.4.0</td> 2300<td><p>When <code>encoding</code> is being passed, the <code>byteOffset</code> parameter is no longer required.</p></td></tr> 2301<tr><td>v1.5.0</td> 2302<td><p><span>Added in: v1.5.0</span></p></td></tr> 2303</tbody></table> 2304</details> 2305</div> 2306<ul> 2307<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type"><Uint8Array></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> What to search for.</li> 2308<li><code>byteOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Where to begin searching in <code>buf</code>. If negative, then 2309offset is calculated from the end of <code>buf</code>. <strong>Default:</strong> <code>0</code>.</li> 2310<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> If <code>value</code> is a string, this is the encoding used to 2311determine the binary representation of the string that will be searched for in 2312<code>buf</code>. <strong>Default:</strong> <code>'utf8'</code>.</li> 2313<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The index of the first occurrence of <code>value</code> in <code>buf</code>, or 2314<code>-1</code> if <code>buf</code> does not contain <code>value</code>.</li> 2315</ul> 2316<p>If <code>value</code> is:</p> 2317<ul> 2318<li>a string, <code>value</code> is interpreted according to the character encoding in 2319<code>encoding</code>.</li> 2320<li>a <code>Buffer</code> or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a>, <code>value</code> will be used in its entirety. 2321To compare a partial <code>Buffer</code>, use <a href="#bufsubarraystart-end"><code>buf.subarray</code></a>.</li> 2322<li>a number, <code>value</code> will be interpreted as an unsigned 8-bit integer 2323value between <code>0</code> and <code>255</code>.</li> 2324</ul> 2325 2326<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 2327 2328<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'this is a buffer'</span>); 2329 2330<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'this'</span>)); 2331<span class="hljs-comment">// Prints: 0</span> 2332<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'is'</span>)); 2333<span class="hljs-comment">// Prints: 2</span> 2334<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">indexOf</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'a buffer'</span>))); 2335<span class="hljs-comment">// Prints: 8</span> 2336<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">indexOf</span>(<span class="hljs-number">97</span>)); 2337<span class="hljs-comment">// Prints: 8 (97 is the decimal ASCII value for 'a')</span> 2338<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">indexOf</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'a buffer example'</span>))); 2339<span class="hljs-comment">// Prints: -1</span> 2340<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">indexOf</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'a buffer example'</span>).<span class="hljs-title function_">slice</span>(<span class="hljs-number">0</span>, <span class="hljs-number">8</span>))); 2341<span class="hljs-comment">// Prints: 8</span> 2342 2343<span class="hljs-keyword">const</span> utf16Buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'\u039a\u0391\u03a3\u03a3\u0395'</span>, <span class="hljs-string">'utf16le'</span>); 2344 2345<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'\u03a3'</span>, <span class="hljs-number">0</span>, <span class="hljs-string">'utf16le'</span>)); 2346<span class="hljs-comment">// Prints: 4</span> 2347<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'\u03a3'</span>, -<span class="hljs-number">4</span>, <span class="hljs-string">'utf16le'</span>)); 2348<span class="hljs-comment">// Prints: 6</span></code><code class="language-js cjs"><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>); 2349 2350<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'this is a buffer'</span>); 2351 2352<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'this'</span>)); 2353<span class="hljs-comment">// Prints: 0</span> 2354<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'is'</span>)); 2355<span class="hljs-comment">// Prints: 2</span> 2356<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">indexOf</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'a buffer'</span>))); 2357<span class="hljs-comment">// Prints: 8</span> 2358<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">indexOf</span>(<span class="hljs-number">97</span>)); 2359<span class="hljs-comment">// Prints: 8 (97 is the decimal ASCII value for 'a')</span> 2360<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">indexOf</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'a buffer example'</span>))); 2361<span class="hljs-comment">// Prints: -1</span> 2362<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">indexOf</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'a buffer example'</span>).<span class="hljs-title function_">slice</span>(<span class="hljs-number">0</span>, <span class="hljs-number">8</span>))); 2363<span class="hljs-comment">// Prints: 8</span> 2364 2365<span class="hljs-keyword">const</span> utf16Buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'\u039a\u0391\u03a3\u03a3\u0395'</span>, <span class="hljs-string">'utf16le'</span>); 2366 2367<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'\u03a3'</span>, <span class="hljs-number">0</span>, <span class="hljs-string">'utf16le'</span>)); 2368<span class="hljs-comment">// Prints: 4</span> 2369<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'\u03a3'</span>, -<span class="hljs-number">4</span>, <span class="hljs-string">'utf16le'</span>)); 2370<span class="hljs-comment">// Prints: 6</span></code><button class="copy-button">copy</button></pre> 2371<p>If <code>value</code> is not a string, number, or <code>Buffer</code>, this method will throw a 2372<code>TypeError</code>. If <code>value</code> is a number, it will be coerced to a valid byte value, 2373an integer between 0 and 255.</p> 2374<p>If <code>byteOffset</code> is not a number, it will be coerced to a number. If the result 2375of coercion is <code>NaN</code> or <code>0</code>, then the entire buffer will be searched. This 2376behavior matches <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf"><code>String.prototype.indexOf()</code></a>.</p> 2377 2378<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 2379 2380<span class="hljs-keyword">const</span> b = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'abcdef'</span>); 2381 2382<span class="hljs-comment">// Passing a value that's a number, but not a valid byte.</span> 2383<span class="hljs-comment">// Prints: 2, equivalent to searching for 99 or 'c'.</span> 2384<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">indexOf</span>(<span class="hljs-number">99.9</span>)); 2385<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">indexOf</span>(<span class="hljs-number">256</span> + <span class="hljs-number">99</span>)); 2386 2387<span class="hljs-comment">// Passing a byteOffset that coerces to NaN or 0.</span> 2388<span class="hljs-comment">// Prints: 1, searching the whole buffer.</span> 2389<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'b'</span>, <span class="hljs-literal">undefined</span>)); 2390<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'b'</span>, {})); 2391<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'b'</span>, <span class="hljs-literal">null</span>)); 2392<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'b'</span>, []));</code><code class="language-js cjs"><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>); 2393 2394<span class="hljs-keyword">const</span> b = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'abcdef'</span>); 2395 2396<span class="hljs-comment">// Passing a value that's a number, but not a valid byte.</span> 2397<span class="hljs-comment">// Prints: 2, equivalent to searching for 99 or 'c'.</span> 2398<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">indexOf</span>(<span class="hljs-number">99.9</span>)); 2399<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">indexOf</span>(<span class="hljs-number">256</span> + <span class="hljs-number">99</span>)); 2400 2401<span class="hljs-comment">// Passing a byteOffset that coerces to NaN or 0.</span> 2402<span class="hljs-comment">// Prints: 1, searching the whole buffer.</span> 2403<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'b'</span>, <span class="hljs-literal">undefined</span>)); 2404<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'b'</span>, {})); 2405<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'b'</span>, <span class="hljs-literal">null</span>)); 2406<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'b'</span>, []));</code><button class="copy-button">copy</button></pre> 2407<p>If <code>value</code> is an empty string or empty <code>Buffer</code> and <code>byteOffset</code> is less 2408than <code>buf.length</code>, <code>byteOffset</code> will be returned. If <code>value</code> is empty and 2409<code>byteOffset</code> is at least <code>buf.length</code>, <code>buf.length</code> will be returned.</p> 2410<h4><code>buf.keys()</code><span><a class="mark" href="#bufkeys" id="bufkeys">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_keys"></a></h4> 2411<div class="api_metadata"> 2412<span>Added in: v1.1.0</span> 2413</div> 2414<ul> 2415<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> 2416</ul> 2417<p>Creates and returns an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols">iterator</a> of <code>buf</code> keys (indices).</p> 2418 2419<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 2420 2421<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>); 2422 2423<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> key <span class="hljs-keyword">of</span> buf.<span class="hljs-title function_">keys</span>()) { 2424 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(key); 2425} 2426<span class="hljs-comment">// Prints:</span> 2427<span class="hljs-comment">// 0</span> 2428<span class="hljs-comment">// 1</span> 2429<span class="hljs-comment">// 2</span> 2430<span class="hljs-comment">// 3</span> 2431<span class="hljs-comment">// 4</span> 2432<span class="hljs-comment">// 5</span></code><code class="language-js cjs"><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>); 2433 2434<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>); 2435 2436<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> key <span class="hljs-keyword">of</span> buf.<span class="hljs-title function_">keys</span>()) { 2437 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(key); 2438} 2439<span class="hljs-comment">// Prints:</span> 2440<span class="hljs-comment">// 0</span> 2441<span class="hljs-comment">// 1</span> 2442<span class="hljs-comment">// 2</span> 2443<span class="hljs-comment">// 3</span> 2444<span class="hljs-comment">// 4</span> 2445<span class="hljs-comment">// 5</span></code><button class="copy-button">copy</button></pre> 2446<h4><code>buf.lastIndexOf(value[, byteOffset][, encoding])</code><span><a class="mark" href="#buflastindexofvalue-byteoffset-encoding" id="buflastindexofvalue-byteoffset-encoding">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_lastindexof_value_byteoffset_encoding"></a></h4> 2447<div class="api_metadata"> 2448<details class="changelog"><summary>History</summary> 2449<table> 2450<tbody><tr><th>Version</th><th>Changes</th></tr> 2451<tr><td>v8.0.0</td> 2452<td><p>The <code>value</code> can now be a <code>Uint8Array</code>.</p></td></tr> 2453<tr><td>v6.0.0</td> 2454<td><p><span>Added in: v6.0.0</span></p></td></tr> 2455</tbody></table> 2456</details> 2457</div> 2458<ul> 2459<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type"><Uint8Array></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> What to search for.</li> 2460<li><code>byteOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Where to begin searching in <code>buf</code>. If negative, then 2461offset is calculated from the end of <code>buf</code>. <strong>Default:</strong> 2462<code>buf.length - 1</code>.</li> 2463<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> If <code>value</code> is a string, this is the encoding used to 2464determine the binary representation of the string that will be searched for in 2465<code>buf</code>. <strong>Default:</strong> <code>'utf8'</code>.</li> 2466<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The index of the last occurrence of <code>value</code> in <code>buf</code>, or 2467<code>-1</code> if <code>buf</code> does not contain <code>value</code>.</li> 2468</ul> 2469<p>Identical to <a href="#bufindexofvalue-byteoffset-encoding"><code>buf.indexOf()</code></a>, except the last occurrence of <code>value</code> is found 2470rather than the first occurrence.</p> 2471 2472<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 2473 2474<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'this buffer is a buffer'</span>); 2475 2476<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'this'</span>)); 2477<span class="hljs-comment">// Prints: 0</span> 2478<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'buffer'</span>)); 2479<span class="hljs-comment">// Prints: 17</span> 2480<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>))); 2481<span class="hljs-comment">// Prints: 17</span> 2482<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-number">97</span>)); 2483<span class="hljs-comment">// Prints: 15 (97 is the decimal ASCII value for 'a')</span> 2484<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'yolo'</span>))); 2485<span class="hljs-comment">// Prints: -1</span> 2486<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'buffer'</span>, <span class="hljs-number">5</span>)); 2487<span class="hljs-comment">// Prints: 5</span> 2488<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'buffer'</span>, <span class="hljs-number">4</span>)); 2489<span class="hljs-comment">// Prints: -1</span> 2490 2491<span class="hljs-keyword">const</span> utf16Buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'\u039a\u0391\u03a3\u03a3\u0395'</span>, <span class="hljs-string">'utf16le'</span>); 2492 2493<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'\u03a3'</span>, <span class="hljs-literal">undefined</span>, <span class="hljs-string">'utf16le'</span>)); 2494<span class="hljs-comment">// Prints: 6</span> 2495<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'\u03a3'</span>, -<span class="hljs-number">5</span>, <span class="hljs-string">'utf16le'</span>)); 2496<span class="hljs-comment">// Prints: 4</span></code><code class="language-js cjs"><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>); 2497 2498<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'this buffer is a buffer'</span>); 2499 2500<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'this'</span>)); 2501<span class="hljs-comment">// Prints: 0</span> 2502<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'buffer'</span>)); 2503<span class="hljs-comment">// Prints: 17</span> 2504<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>))); 2505<span class="hljs-comment">// Prints: 17</span> 2506<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-number">97</span>)); 2507<span class="hljs-comment">// Prints: 15 (97 is the decimal ASCII value for 'a')</span> 2508<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'yolo'</span>))); 2509<span class="hljs-comment">// Prints: -1</span> 2510<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'buffer'</span>, <span class="hljs-number">5</span>)); 2511<span class="hljs-comment">// Prints: 5</span> 2512<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'buffer'</span>, <span class="hljs-number">4</span>)); 2513<span class="hljs-comment">// Prints: -1</span> 2514 2515<span class="hljs-keyword">const</span> utf16Buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'\u039a\u0391\u03a3\u03a3\u0395'</span>, <span class="hljs-string">'utf16le'</span>); 2516 2517<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'\u03a3'</span>, <span class="hljs-literal">undefined</span>, <span class="hljs-string">'utf16le'</span>)); 2518<span class="hljs-comment">// Prints: 6</span> 2519<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'\u03a3'</span>, -<span class="hljs-number">5</span>, <span class="hljs-string">'utf16le'</span>)); 2520<span class="hljs-comment">// Prints: 4</span></code><button class="copy-button">copy</button></pre> 2521<p>If <code>value</code> is not a string, number, or <code>Buffer</code>, this method will throw a 2522<code>TypeError</code>. If <code>value</code> is a number, it will be coerced to a valid byte value, 2523an integer between 0 and 255.</p> 2524<p>If <code>byteOffset</code> is not a number, it will be coerced to a number. Any arguments 2525that coerce to <code>NaN</code>, like <code>{}</code> or <code>undefined</code>, will search the whole buffer. 2526This behavior matches <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf"><code>String.prototype.lastIndexOf()</code></a>.</p> 2527 2528<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 2529 2530<span class="hljs-keyword">const</span> b = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'abcdef'</span>); 2531 2532<span class="hljs-comment">// Passing a value that's a number, but not a valid byte.</span> 2533<span class="hljs-comment">// Prints: 2, equivalent to searching for 99 or 'c'.</span> 2534<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-number">99.9</span>)); 2535<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-number">256</span> + <span class="hljs-number">99</span>)); 2536 2537<span class="hljs-comment">// Passing a byteOffset that coerces to NaN.</span> 2538<span class="hljs-comment">// Prints: 1, searching the whole buffer.</span> 2539<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'b'</span>, <span class="hljs-literal">undefined</span>)); 2540<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'b'</span>, {})); 2541 2542<span class="hljs-comment">// Passing a byteOffset that coerces to 0.</span> 2543<span class="hljs-comment">// Prints: -1, equivalent to passing 0.</span> 2544<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'b'</span>, <span class="hljs-literal">null</span>)); 2545<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'b'</span>, []));</code><code class="language-js cjs"><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>); 2546 2547<span class="hljs-keyword">const</span> b = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'abcdef'</span>); 2548 2549<span class="hljs-comment">// Passing a value that's a number, but not a valid byte.</span> 2550<span class="hljs-comment">// Prints: 2, equivalent to searching for 99 or 'c'.</span> 2551<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-number">99.9</span>)); 2552<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-number">256</span> + <span class="hljs-number">99</span>)); 2553 2554<span class="hljs-comment">// Passing a byteOffset that coerces to NaN.</span> 2555<span class="hljs-comment">// Prints: 1, searching the whole buffer.</span> 2556<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'b'</span>, <span class="hljs-literal">undefined</span>)); 2557<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'b'</span>, {})); 2558 2559<span class="hljs-comment">// Passing a byteOffset that coerces to 0.</span> 2560<span class="hljs-comment">// Prints: -1, equivalent to passing 0.</span> 2561<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'b'</span>, <span class="hljs-literal">null</span>)); 2562<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'b'</span>, []));</code><button class="copy-button">copy</button></pre> 2563<p>If <code>value</code> is an empty string or empty <code>Buffer</code>, <code>byteOffset</code> will be returned.</p> 2564<h4><code>buf.length</code><span><a class="mark" href="#buflength" id="buflength">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_length"></a></h4> 2565<div class="api_metadata"> 2566<span>Added in: v0.1.90</span> 2567</div> 2568<ul> 2569<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 2570</ul> 2571<p>Returns the number of bytes in <code>buf</code>.</p> 2572 2573<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 2574 2575<span class="hljs-comment">// Create a `Buffer` and write a shorter string to it using UTF-8.</span> 2576 2577<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">1234</span>); 2578 2579<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>); 2580<span class="hljs-comment">// Prints: 1234</span> 2581 2582buf.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some string'</span>, <span class="hljs-number">0</span>, <span class="hljs-string">'utf8'</span>); 2583 2584<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>); 2585<span class="hljs-comment">// Prints: 1234</span></code><code class="language-js cjs"><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>); 2586 2587<span class="hljs-comment">// Create a `Buffer` and write a shorter string to it using UTF-8.</span> 2588 2589<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">1234</span>); 2590 2591<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>); 2592<span class="hljs-comment">// Prints: 1234</span> 2593 2594buf.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some string'</span>, <span class="hljs-number">0</span>, <span class="hljs-string">'utf8'</span>); 2595 2596<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>); 2597<span class="hljs-comment">// Prints: 1234</span></code><button class="copy-button">copy</button></pre> 2598<h4><code>buf.parent</code><span><a class="mark" href="#bufparent" id="bufparent">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_parent"></a></h4> 2599<div class="api_metadata"> 2600<span>Deprecated since: v8.0.0</span> 2601</div> 2602<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#bufbuffer"><code>buf.buffer</code></a> instead.</div><p></p> 2603<p>The <code>buf.parent</code> property is a deprecated alias for <code>buf.buffer</code>.</p> 2604<h4><code>buf.readBigInt64BE([offset])</code><span><a class="mark" href="#bufreadbigint64beoffset" id="bufreadbigint64beoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readbigint64be_offset"></a></h4> 2605<div class="api_metadata"> 2606<span>Added in: v12.0.0, v10.20.0</span> 2607</div> 2608<ul> 2609<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to read. Must 2610satisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 2611<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 2612</ul> 2613<p>Reads a signed, big-endian 64-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> 2614<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed 2615values.</p> 2616<h4><code>buf.readBigInt64LE([offset])</code><span><a class="mark" href="#bufreadbigint64leoffset" id="bufreadbigint64leoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readbigint64le_offset"></a></h4> 2617<div class="api_metadata"> 2618<span>Added in: v12.0.0, v10.20.0</span> 2619</div> 2620<ul> 2621<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to read. Must 2622satisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 2623<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 2624</ul> 2625<p>Reads a signed, little-endian 64-bit integer from <code>buf</code> at the specified 2626<code>offset</code>.</p> 2627<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed 2628values.</p> 2629<h4><code>buf.readBigUInt64BE([offset])</code><span><a class="mark" href="#bufreadbiguint64beoffset" id="bufreadbiguint64beoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readbiguint64be_offset"></a></h4> 2630<div class="api_metadata"> 2631<details class="changelog"><summary>History</summary> 2632<table> 2633<tbody><tr><th>Version</th><th>Changes</th></tr> 2634<tr><td>v14.10.0, v12.19.0</td> 2635<td><p>This function is also available as <code>buf.readBigUint64BE()</code>.</p></td></tr> 2636<tr><td>v12.0.0, v10.20.0</td> 2637<td><p><span>Added in: v12.0.0, v10.20.0</span></p></td></tr> 2638</tbody></table> 2639</details> 2640</div> 2641<ul> 2642<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to read. Must 2643satisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 2644<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 2645</ul> 2646<p>Reads an unsigned, big-endian 64-bit integer from <code>buf</code> at the specified 2647<code>offset</code>.</p> 2648<p>This function is also available under the <code>readBigUint64BE</code> alias.</p> 2649 2650<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 2651 2652<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x00</span>, <span class="hljs-number">0x00</span>, <span class="hljs-number">0x00</span>, <span class="hljs-number">0x00</span>, <span class="hljs-number">0xff</span>, <span class="hljs-number">0xff</span>, <span class="hljs-number">0xff</span>, <span class="hljs-number">0xff</span>]); 2653 2654<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readBigUInt64BE</span>(<span class="hljs-number">0</span>)); 2655<span class="hljs-comment">// Prints: 4294967295n</span></code><code class="language-js cjs"><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>); 2656 2657<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x00</span>, <span class="hljs-number">0x00</span>, <span class="hljs-number">0x00</span>, <span class="hljs-number">0x00</span>, <span class="hljs-number">0xff</span>, <span class="hljs-number">0xff</span>, <span class="hljs-number">0xff</span>, <span class="hljs-number">0xff</span>]); 2658 2659<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readBigUInt64BE</span>(<span class="hljs-number">0</span>)); 2660<span class="hljs-comment">// Prints: 4294967295n</span></code><button class="copy-button">copy</button></pre> 2661<h4><code>buf.readBigUInt64LE([offset])</code><span><a class="mark" href="#bufreadbiguint64leoffset" id="bufreadbiguint64leoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readbiguint64le_offset"></a></h4> 2662<div class="api_metadata"> 2663<details class="changelog"><summary>History</summary> 2664<table> 2665<tbody><tr><th>Version</th><th>Changes</th></tr> 2666<tr><td>v14.10.0, v12.19.0</td> 2667<td><p>This function is also available as <code>buf.readBigUint64LE()</code>.</p></td></tr> 2668<tr><td>v12.0.0, v10.20.0</td> 2669<td><p><span>Added in: v12.0.0, v10.20.0</span></p></td></tr> 2670</tbody></table> 2671</details> 2672</div> 2673<ul> 2674<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to read. Must 2675satisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 2676<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 2677</ul> 2678<p>Reads an unsigned, little-endian 64-bit integer from <code>buf</code> at the specified 2679<code>offset</code>.</p> 2680<p>This function is also available under the <code>readBigUint64LE</code> alias.</p> 2681 2682<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 2683 2684<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x00</span>, <span class="hljs-number">0x00</span>, <span class="hljs-number">0x00</span>, <span class="hljs-number">0x00</span>, <span class="hljs-number">0xff</span>, <span class="hljs-number">0xff</span>, <span class="hljs-number">0xff</span>, <span class="hljs-number">0xff</span>]); 2685 2686<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readBigUInt64LE</span>(<span class="hljs-number">0</span>)); 2687<span class="hljs-comment">// Prints: 18446744069414584320n</span></code><code class="language-js cjs"><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>); 2688 2689<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x00</span>, <span class="hljs-number">0x00</span>, <span class="hljs-number">0x00</span>, <span class="hljs-number">0x00</span>, <span class="hljs-number">0xff</span>, <span class="hljs-number">0xff</span>, <span class="hljs-number">0xff</span>, <span class="hljs-number">0xff</span>]); 2690 2691<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readBigUInt64LE</span>(<span class="hljs-number">0</span>)); 2692<span class="hljs-comment">// Prints: 18446744069414584320n</span></code><button class="copy-button">copy</button></pre> 2693<h4><code>buf.readDoubleBE([offset])</code><span><a class="mark" href="#bufreaddoublebeoffset" id="bufreaddoublebeoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readdoublebe_offset"></a></h4> 2694<div class="api_metadata"> 2695<details class="changelog"><summary>History</summary> 2696<table> 2697<tbody><tr><th>Version</th><th>Changes</th></tr> 2698<tr><td>v10.0.0</td> 2699<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 2700<tr><td>v0.11.15</td> 2701<td><p><span>Added in: v0.11.15</span></p></td></tr> 2702</tbody></table> 2703</details> 2704</div> 2705<ul> 2706<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to read. Must 2707satisfy <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 2708<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 2709</ul> 2710<p>Reads a 64-bit, big-endian double from <code>buf</code> at the specified <code>offset</code>.</p> 2711 2712<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 2713 2714<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>]); 2715 2716<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readDoubleBE</span>(<span class="hljs-number">0</span>)); 2717<span class="hljs-comment">// Prints: 8.20788039913184e-304</span></code><code class="language-js cjs"><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>); 2718 2719<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>]); 2720 2721<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readDoubleBE</span>(<span class="hljs-number">0</span>)); 2722<span class="hljs-comment">// Prints: 8.20788039913184e-304</span></code><button class="copy-button">copy</button></pre> 2723<h4><code>buf.readDoubleLE([offset])</code><span><a class="mark" href="#bufreaddoubleleoffset" id="bufreaddoubleleoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readdoublele_offset"></a></h4> 2724<div class="api_metadata"> 2725<details class="changelog"><summary>History</summary> 2726<table> 2727<tbody><tr><th>Version</th><th>Changes</th></tr> 2728<tr><td>v10.0.0</td> 2729<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 2730<tr><td>v0.11.15</td> 2731<td><p><span>Added in: v0.11.15</span></p></td></tr> 2732</tbody></table> 2733</details> 2734</div> 2735<ul> 2736<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to read. Must 2737satisfy <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 2738<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 2739</ul> 2740<p>Reads a 64-bit, little-endian double from <code>buf</code> at the specified <code>offset</code>.</p> 2741 2742<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 2743 2744<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>]); 2745 2746<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readDoubleLE</span>(<span class="hljs-number">0</span>)); 2747<span class="hljs-comment">// Prints: 5.447603722011605e-270</span> 2748<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readDoubleLE</span>(<span class="hljs-number">1</span>)); 2749<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><code class="language-js cjs"><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>); 2750 2751<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>]); 2752 2753<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readDoubleLE</span>(<span class="hljs-number">0</span>)); 2754<span class="hljs-comment">// Prints: 5.447603722011605e-270</span> 2755<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readDoubleLE</span>(<span class="hljs-number">1</span>)); 2756<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> 2757<h4><code>buf.readFloatBE([offset])</code><span><a class="mark" href="#bufreadfloatbeoffset" id="bufreadfloatbeoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readfloatbe_offset"></a></h4> 2758<div class="api_metadata"> 2759<details class="changelog"><summary>History</summary> 2760<table> 2761<tbody><tr><th>Version</th><th>Changes</th></tr> 2762<tr><td>v10.0.0</td> 2763<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 2764<tr><td>v0.11.15</td> 2765<td><p><span>Added in: v0.11.15</span></p></td></tr> 2766</tbody></table> 2767</details> 2768</div> 2769<ul> 2770<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to read. Must 2771satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 2772<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 2773</ul> 2774<p>Reads a 32-bit, big-endian float from <code>buf</code> at the specified <code>offset</code>.</p> 2775 2776<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 2777 2778<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]); 2779 2780<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readFloatBE</span>(<span class="hljs-number">0</span>)); 2781<span class="hljs-comment">// Prints: 2.387939260590663e-38</span></code><code class="language-js cjs"><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>); 2782 2783<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]); 2784 2785<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readFloatBE</span>(<span class="hljs-number">0</span>)); 2786<span class="hljs-comment">// Prints: 2.387939260590663e-38</span></code><button class="copy-button">copy</button></pre> 2787<h4><code>buf.readFloatLE([offset])</code><span><a class="mark" href="#bufreadfloatleoffset" id="bufreadfloatleoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readfloatle_offset"></a></h4> 2788<div class="api_metadata"> 2789<details class="changelog"><summary>History</summary> 2790<table> 2791<tbody><tr><th>Version</th><th>Changes</th></tr> 2792<tr><td>v10.0.0</td> 2793<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 2794<tr><td>v0.11.15</td> 2795<td><p><span>Added in: v0.11.15</span></p></td></tr> 2796</tbody></table> 2797</details> 2798</div> 2799<ul> 2800<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to read. Must 2801satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 2802<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 2803</ul> 2804<p>Reads a 32-bit, little-endian float from <code>buf</code> at the specified <code>offset</code>.</p> 2805 2806<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 2807 2808<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]); 2809 2810<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readFloatLE</span>(<span class="hljs-number">0</span>)); 2811<span class="hljs-comment">// Prints: 1.539989614439558e-36</span> 2812<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readFloatLE</span>(<span class="hljs-number">1</span>)); 2813<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><code class="language-js cjs"><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>); 2814 2815<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]); 2816 2817<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readFloatLE</span>(<span class="hljs-number">0</span>)); 2818<span class="hljs-comment">// Prints: 1.539989614439558e-36</span> 2819<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readFloatLE</span>(<span class="hljs-number">1</span>)); 2820<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> 2821<h4><code>buf.readInt8([offset])</code><span><a class="mark" href="#bufreadint8offset" id="bufreadint8offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readint8_offset"></a></h4> 2822<div class="api_metadata"> 2823<details class="changelog"><summary>History</summary> 2824<table> 2825<tbody><tr><th>Version</th><th>Changes</th></tr> 2826<tr><td>v10.0.0</td> 2827<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 2828<tr><td>v0.5.0</td> 2829<td><p><span>Added in: v0.5.0</span></p></td></tr> 2830</tbody></table> 2831</details> 2832</div> 2833<ul> 2834<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to read. Must 2835satisfy <code>0 <= offset <= buf.length - 1</code>. <strong>Default:</strong> <code>0</code>.</li> 2836<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 2837</ul> 2838<p>Reads a signed 8-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> 2839<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p> 2840 2841<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 2842 2843<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([-<span class="hljs-number">1</span>, <span class="hljs-number">5</span>]); 2844 2845<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt8</span>(<span class="hljs-number">0</span>)); 2846<span class="hljs-comment">// Prints: -1</span> 2847<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt8</span>(<span class="hljs-number">1</span>)); 2848<span class="hljs-comment">// Prints: 5</span> 2849<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt8</span>(<span class="hljs-number">2</span>)); 2850<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><code class="language-js cjs"><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>); 2851 2852<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([-<span class="hljs-number">1</span>, <span class="hljs-number">5</span>]); 2853 2854<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt8</span>(<span class="hljs-number">0</span>)); 2855<span class="hljs-comment">// Prints: -1</span> 2856<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt8</span>(<span class="hljs-number">1</span>)); 2857<span class="hljs-comment">// Prints: 5</span> 2858<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt8</span>(<span class="hljs-number">2</span>)); 2859<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> 2860<h4><code>buf.readInt16BE([offset])</code><span><a class="mark" href="#bufreadint16beoffset" id="bufreadint16beoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readint16be_offset"></a></h4> 2861<div class="api_metadata"> 2862<details class="changelog"><summary>History</summary> 2863<table> 2864<tbody><tr><th>Version</th><th>Changes</th></tr> 2865<tr><td>v10.0.0</td> 2866<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 2867<tr><td>v0.5.5</td> 2868<td><p><span>Added in: v0.5.5</span></p></td></tr> 2869</tbody></table> 2870</details> 2871</div> 2872<ul> 2873<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to read. Must 2874satisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> 2875<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 2876</ul> 2877<p>Reads a signed, big-endian 16-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> 2878<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p> 2879 2880<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 2881 2882<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0</span>, <span class="hljs-number">5</span>]); 2883 2884<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt16BE</span>(<span class="hljs-number">0</span>)); 2885<span class="hljs-comment">// Prints: 5</span></code><code class="language-js cjs"><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>); 2886 2887<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0</span>, <span class="hljs-number">5</span>]); 2888 2889<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt16BE</span>(<span class="hljs-number">0</span>)); 2890<span class="hljs-comment">// Prints: 5</span></code><button class="copy-button">copy</button></pre> 2891<h4><code>buf.readInt16LE([offset])</code><span><a class="mark" href="#bufreadint16leoffset" id="bufreadint16leoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readint16le_offset"></a></h4> 2892<div class="api_metadata"> 2893<details class="changelog"><summary>History</summary> 2894<table> 2895<tbody><tr><th>Version</th><th>Changes</th></tr> 2896<tr><td>v10.0.0</td> 2897<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 2898<tr><td>v0.5.5</td> 2899<td><p><span>Added in: v0.5.5</span></p></td></tr> 2900</tbody></table> 2901</details> 2902</div> 2903<ul> 2904<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to read. Must 2905satisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> 2906<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 2907</ul> 2908<p>Reads a signed, little-endian 16-bit integer from <code>buf</code> at the specified 2909<code>offset</code>.</p> 2910<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p> 2911 2912<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 2913 2914<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0</span>, <span class="hljs-number">5</span>]); 2915 2916<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt16LE</span>(<span class="hljs-number">0</span>)); 2917<span class="hljs-comment">// Prints: 1280</span> 2918<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt16LE</span>(<span class="hljs-number">1</span>)); 2919<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><code class="language-js cjs"><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>); 2920 2921<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0</span>, <span class="hljs-number">5</span>]); 2922 2923<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt16LE</span>(<span class="hljs-number">0</span>)); 2924<span class="hljs-comment">// Prints: 1280</span> 2925<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt16LE</span>(<span class="hljs-number">1</span>)); 2926<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> 2927<h4><code>buf.readInt32BE([offset])</code><span><a class="mark" href="#bufreadint32beoffset" id="bufreadint32beoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readint32be_offset"></a></h4> 2928<div class="api_metadata"> 2929<details class="changelog"><summary>History</summary> 2930<table> 2931<tbody><tr><th>Version</th><th>Changes</th></tr> 2932<tr><td>v10.0.0</td> 2933<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 2934<tr><td>v0.5.5</td> 2935<td><p><span>Added in: v0.5.5</span></p></td></tr> 2936</tbody></table> 2937</details> 2938</div> 2939<ul> 2940<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to read. Must 2941satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 2942<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 2943</ul> 2944<p>Reads a signed, big-endian 32-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> 2945<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p> 2946 2947<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 2948 2949<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">5</span>]); 2950 2951<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt32BE</span>(<span class="hljs-number">0</span>)); 2952<span class="hljs-comment">// Prints: 5</span></code><code class="language-js cjs"><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>); 2953 2954<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">5</span>]); 2955 2956<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt32BE</span>(<span class="hljs-number">0</span>)); 2957<span class="hljs-comment">// Prints: 5</span></code><button class="copy-button">copy</button></pre> 2958<h4><code>buf.readInt32LE([offset])</code><span><a class="mark" href="#bufreadint32leoffset" id="bufreadint32leoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readint32le_offset"></a></h4> 2959<div class="api_metadata"> 2960<details class="changelog"><summary>History</summary> 2961<table> 2962<tbody><tr><th>Version</th><th>Changes</th></tr> 2963<tr><td>v10.0.0</td> 2964<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 2965<tr><td>v0.5.5</td> 2966<td><p><span>Added in: v0.5.5</span></p></td></tr> 2967</tbody></table> 2968</details> 2969</div> 2970<ul> 2971<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to read. Must 2972satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 2973<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 2974</ul> 2975<p>Reads a signed, little-endian 32-bit integer from <code>buf</code> at the specified 2976<code>offset</code>.</p> 2977<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p> 2978 2979<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 2980 2981<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">5</span>]); 2982 2983<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt32LE</span>(<span class="hljs-number">0</span>)); 2984<span class="hljs-comment">// Prints: 83886080</span> 2985<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt32LE</span>(<span class="hljs-number">1</span>)); 2986<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><code class="language-js cjs"><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>); 2987 2988<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">5</span>]); 2989 2990<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt32LE</span>(<span class="hljs-number">0</span>)); 2991<span class="hljs-comment">// Prints: 83886080</span> 2992<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt32LE</span>(<span class="hljs-number">1</span>)); 2993<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> 2994<h4><code>buf.readIntBE(offset, byteLength)</code><span><a class="mark" href="#bufreadintbeoffset-bytelength" id="bufreadintbeoffset-bytelength">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readintbe_offset_bytelength"></a></h4> 2995<div class="api_metadata"> 2996<details class="changelog"><summary>History</summary> 2997<table> 2998<tbody><tr><th>Version</th><th>Changes</th></tr> 2999<tr><td>v10.0.0</td> 3000<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset and <code>byteLength</code> to <code>uint32</code> anymore.</p></td></tr> 3001<tr><td>v0.11.15</td> 3002<td><p><span>Added in: v0.11.15</span></p></td></tr> 3003</tbody></table> 3004</details> 3005</div> 3006<ul> 3007<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to read. Must 3008satisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> 3009<li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to read. Must satisfy 3010<code>0 < byteLength <= 6</code>.</li> 3011<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3012</ul> 3013<p>Reads <code>byteLength</code> number of bytes from <code>buf</code> at the specified <code>offset</code> 3014and interprets the result as a big-endian, two's complement signed value 3015supporting up to 48 bits of accuracy.</p> 3016 3017<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 3018 3019<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>, <span class="hljs-number">0x78</span>, <span class="hljs-number">0x90</span>, <span class="hljs-number">0xab</span>]); 3020 3021<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readIntBE</span>(<span class="hljs-number">0</span>, <span class="hljs-number">6</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3022<span class="hljs-comment">// Prints: 1234567890ab</span> 3023<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readIntBE</span>(<span class="hljs-number">1</span>, <span class="hljs-number">6</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3024<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span> 3025<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readIntBE</span>(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3026<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><code class="language-js cjs"><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>); 3027 3028<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>, <span class="hljs-number">0x78</span>, <span class="hljs-number">0x90</span>, <span class="hljs-number">0xab</span>]); 3029 3030<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readIntBE</span>(<span class="hljs-number">0</span>, <span class="hljs-number">6</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3031<span class="hljs-comment">// Prints: 1234567890ab</span> 3032<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readIntBE</span>(<span class="hljs-number">1</span>, <span class="hljs-number">6</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3033<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span> 3034<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readIntBE</span>(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3035<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> 3036<h4><code>buf.readIntLE(offset, byteLength)</code><span><a class="mark" href="#bufreadintleoffset-bytelength" id="bufreadintleoffset-bytelength">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readintle_offset_bytelength"></a></h4> 3037<div class="api_metadata"> 3038<details class="changelog"><summary>History</summary> 3039<table> 3040<tbody><tr><th>Version</th><th>Changes</th></tr> 3041<tr><td>v10.0.0</td> 3042<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset and <code>byteLength</code> to <code>uint32</code> anymore.</p></td></tr> 3043<tr><td>v0.11.15</td> 3044<td><p><span>Added in: v0.11.15</span></p></td></tr> 3045</tbody></table> 3046</details> 3047</div> 3048<ul> 3049<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to read. Must 3050satisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> 3051<li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to read. Must satisfy 3052<code>0 < byteLength <= 6</code>.</li> 3053<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3054</ul> 3055<p>Reads <code>byteLength</code> number of bytes from <code>buf</code> at the specified <code>offset</code> 3056and interprets the result as a little-endian, two's complement signed value 3057supporting up to 48 bits of accuracy.</p> 3058 3059<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 3060 3061<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>, <span class="hljs-number">0x78</span>, <span class="hljs-number">0x90</span>, <span class="hljs-number">0xab</span>]); 3062 3063<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readIntLE</span>(<span class="hljs-number">0</span>, <span class="hljs-number">6</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3064<span class="hljs-comment">// Prints: -546f87a9cbee</span></code><code class="language-js cjs"><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>); 3065 3066<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>, <span class="hljs-number">0x78</span>, <span class="hljs-number">0x90</span>, <span class="hljs-number">0xab</span>]); 3067 3068<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readIntLE</span>(<span class="hljs-number">0</span>, <span class="hljs-number">6</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3069<span class="hljs-comment">// Prints: -546f87a9cbee</span></code><button class="copy-button">copy</button></pre> 3070<h4><code>buf.readUInt8([offset])</code><span><a class="mark" href="#bufreaduint8offset" id="bufreaduint8offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readuint8_offset"></a></h4> 3071<div class="api_metadata"> 3072<details class="changelog"><summary>History</summary> 3073<table> 3074<tbody><tr><th>Version</th><th>Changes</th></tr> 3075<tr><td>v14.9.0, v12.19.0</td> 3076<td><p>This function is also available as <code>buf.readUint8()</code>.</p></td></tr> 3077<tr><td>v10.0.0</td> 3078<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 3079<tr><td>v0.5.0</td> 3080<td><p><span>Added in: v0.5.0</span></p></td></tr> 3081</tbody></table> 3082</details> 3083</div> 3084<ul> 3085<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to read. Must 3086satisfy <code>0 <= offset <= buf.length - 1</code>. <strong>Default:</strong> <code>0</code>.</li> 3087<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3088</ul> 3089<p>Reads an unsigned 8-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> 3090<p>This function is also available under the <code>readUint8</code> alias.</p> 3091 3092<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 3093 3094<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, -<span class="hljs-number">2</span>]); 3095 3096<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt8</span>(<span class="hljs-number">0</span>)); 3097<span class="hljs-comment">// Prints: 1</span> 3098<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt8</span>(<span class="hljs-number">1</span>)); 3099<span class="hljs-comment">// Prints: 254</span> 3100<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt8</span>(<span class="hljs-number">2</span>)); 3101<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><code class="language-js cjs"><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>); 3102 3103<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, -<span class="hljs-number">2</span>]); 3104 3105<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt8</span>(<span class="hljs-number">0</span>)); 3106<span class="hljs-comment">// Prints: 1</span> 3107<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt8</span>(<span class="hljs-number">1</span>)); 3108<span class="hljs-comment">// Prints: 254</span> 3109<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt8</span>(<span class="hljs-number">2</span>)); 3110<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> 3111<h4><code>buf.readUInt16BE([offset])</code><span><a class="mark" href="#bufreaduint16beoffset" id="bufreaduint16beoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readuint16be_offset"></a></h4> 3112<div class="api_metadata"> 3113<details class="changelog"><summary>History</summary> 3114<table> 3115<tbody><tr><th>Version</th><th>Changes</th></tr> 3116<tr><td>v14.9.0, v12.19.0</td> 3117<td><p>This function is also available as <code>buf.readUint16BE()</code>.</p></td></tr> 3118<tr><td>v10.0.0</td> 3119<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 3120<tr><td>v0.5.5</td> 3121<td><p><span>Added in: v0.5.5</span></p></td></tr> 3122</tbody></table> 3123</details> 3124</div> 3125<ul> 3126<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to read. Must 3127satisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> 3128<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3129</ul> 3130<p>Reads an unsigned, big-endian 16-bit integer from <code>buf</code> at the specified 3131<code>offset</code>.</p> 3132<p>This function is also available under the <code>readUint16BE</code> alias.</p> 3133 3134<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 3135 3136<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>]); 3137 3138<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt16BE</span>(<span class="hljs-number">0</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3139<span class="hljs-comment">// Prints: 1234</span> 3140<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt16BE</span>(<span class="hljs-number">1</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3141<span class="hljs-comment">// Prints: 3456</span></code><code class="language-js cjs"><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>); 3142 3143<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>]); 3144 3145<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt16BE</span>(<span class="hljs-number">0</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3146<span class="hljs-comment">// Prints: 1234</span> 3147<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt16BE</span>(<span class="hljs-number">1</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3148<span class="hljs-comment">// Prints: 3456</span></code><button class="copy-button">copy</button></pre> 3149<h4><code>buf.readUInt16LE([offset])</code><span><a class="mark" href="#bufreaduint16leoffset" id="bufreaduint16leoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readuint16le_offset"></a></h4> 3150<div class="api_metadata"> 3151<details class="changelog"><summary>History</summary> 3152<table> 3153<tbody><tr><th>Version</th><th>Changes</th></tr> 3154<tr><td>v14.9.0, v12.19.0</td> 3155<td><p>This function is also available as <code>buf.readUint16LE()</code>.</p></td></tr> 3156<tr><td>v10.0.0</td> 3157<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 3158<tr><td>v0.5.5</td> 3159<td><p><span>Added in: v0.5.5</span></p></td></tr> 3160</tbody></table> 3161</details> 3162</div> 3163<ul> 3164<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to read. Must 3165satisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> 3166<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3167</ul> 3168<p>Reads an unsigned, little-endian 16-bit integer from <code>buf</code> at the specified 3169<code>offset</code>.</p> 3170<p>This function is also available under the <code>readUint16LE</code> alias.</p> 3171 3172<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 3173 3174<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>]); 3175 3176<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt16LE</span>(<span class="hljs-number">0</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3177<span class="hljs-comment">// Prints: 3412</span> 3178<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt16LE</span>(<span class="hljs-number">1</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3179<span class="hljs-comment">// Prints: 5634</span> 3180<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt16LE</span>(<span class="hljs-number">2</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3181<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><code class="language-js cjs"><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>); 3182 3183<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>]); 3184 3185<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt16LE</span>(<span class="hljs-number">0</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3186<span class="hljs-comment">// Prints: 3412</span> 3187<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt16LE</span>(<span class="hljs-number">1</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3188<span class="hljs-comment">// Prints: 5634</span> 3189<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt16LE</span>(<span class="hljs-number">2</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3190<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> 3191<h4><code>buf.readUInt32BE([offset])</code><span><a class="mark" href="#bufreaduint32beoffset" id="bufreaduint32beoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readuint32be_offset"></a></h4> 3192<div class="api_metadata"> 3193<details class="changelog"><summary>History</summary> 3194<table> 3195<tbody><tr><th>Version</th><th>Changes</th></tr> 3196<tr><td>v14.9.0, v12.19.0</td> 3197<td><p>This function is also available as <code>buf.readUint32BE()</code>.</p></td></tr> 3198<tr><td>v10.0.0</td> 3199<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 3200<tr><td>v0.5.5</td> 3201<td><p><span>Added in: v0.5.5</span></p></td></tr> 3202</tbody></table> 3203</details> 3204</div> 3205<ul> 3206<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to read. Must 3207satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 3208<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3209</ul> 3210<p>Reads an unsigned, big-endian 32-bit integer from <code>buf</code> at the specified 3211<code>offset</code>.</p> 3212<p>This function is also available under the <code>readUint32BE</code> alias.</p> 3213 3214<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 3215 3216<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>, <span class="hljs-number">0x78</span>]); 3217 3218<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt32BE</span>(<span class="hljs-number">0</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3219<span class="hljs-comment">// Prints: 12345678</span></code><code class="language-js cjs"><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>); 3220 3221<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>, <span class="hljs-number">0x78</span>]); 3222 3223<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt32BE</span>(<span class="hljs-number">0</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3224<span class="hljs-comment">// Prints: 12345678</span></code><button class="copy-button">copy</button></pre> 3225<h4><code>buf.readUInt32LE([offset])</code><span><a class="mark" href="#bufreaduint32leoffset" id="bufreaduint32leoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readuint32le_offset"></a></h4> 3226<div class="api_metadata"> 3227<details class="changelog"><summary>History</summary> 3228<table> 3229<tbody><tr><th>Version</th><th>Changes</th></tr> 3230<tr><td>v14.9.0, v12.19.0</td> 3231<td><p>This function is also available as <code>buf.readUint32LE()</code>.</p></td></tr> 3232<tr><td>v10.0.0</td> 3233<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 3234<tr><td>v0.5.5</td> 3235<td><p><span>Added in: v0.5.5</span></p></td></tr> 3236</tbody></table> 3237</details> 3238</div> 3239<ul> 3240<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to read. Must 3241satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 3242<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3243</ul> 3244<p>Reads an unsigned, little-endian 32-bit integer from <code>buf</code> at the specified 3245<code>offset</code>.</p> 3246<p>This function is also available under the <code>readUint32LE</code> alias.</p> 3247 3248<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 3249 3250<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>, <span class="hljs-number">0x78</span>]); 3251 3252<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt32LE</span>(<span class="hljs-number">0</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3253<span class="hljs-comment">// Prints: 78563412</span> 3254<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt32LE</span>(<span class="hljs-number">1</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3255<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><code class="language-js cjs"><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>); 3256 3257<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>, <span class="hljs-number">0x78</span>]); 3258 3259<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt32LE</span>(<span class="hljs-number">0</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3260<span class="hljs-comment">// Prints: 78563412</span> 3261<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt32LE</span>(<span class="hljs-number">1</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3262<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> 3263<h4><code>buf.readUIntBE(offset, byteLength)</code><span><a class="mark" href="#bufreaduintbeoffset-bytelength" id="bufreaduintbeoffset-bytelength">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readuintbe_offset_bytelength"></a></h4> 3264<div class="api_metadata"> 3265<details class="changelog"><summary>History</summary> 3266<table> 3267<tbody><tr><th>Version</th><th>Changes</th></tr> 3268<tr><td>v14.9.0, v12.19.0</td> 3269<td><p>This function is also available as <code>buf.readUintBE()</code>.</p></td></tr> 3270<tr><td>v10.0.0</td> 3271<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset and <code>byteLength</code> to <code>uint32</code> anymore.</p></td></tr> 3272<tr><td>v0.11.15</td> 3273<td><p><span>Added in: v0.11.15</span></p></td></tr> 3274</tbody></table> 3275</details> 3276</div> 3277<ul> 3278<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to read. Must 3279satisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> 3280<li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to read. Must satisfy 3281<code>0 < byteLength <= 6</code>.</li> 3282<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3283</ul> 3284<p>Reads <code>byteLength</code> number of bytes from <code>buf</code> at the specified <code>offset</code> 3285and interprets the result as an unsigned big-endian integer supporting 3286up to 48 bits of accuracy.</p> 3287<p>This function is also available under the <code>readUintBE</code> alias.</p> 3288 3289<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 3290 3291<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>, <span class="hljs-number">0x78</span>, <span class="hljs-number">0x90</span>, <span class="hljs-number">0xab</span>]); 3292 3293<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUIntBE</span>(<span class="hljs-number">0</span>, <span class="hljs-number">6</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3294<span class="hljs-comment">// Prints: 1234567890ab</span> 3295<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUIntBE</span>(<span class="hljs-number">1</span>, <span class="hljs-number">6</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3296<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><code class="language-js cjs"><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>); 3297 3298<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>, <span class="hljs-number">0x78</span>, <span class="hljs-number">0x90</span>, <span class="hljs-number">0xab</span>]); 3299 3300<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUIntBE</span>(<span class="hljs-number">0</span>, <span class="hljs-number">6</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3301<span class="hljs-comment">// Prints: 1234567890ab</span> 3302<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUIntBE</span>(<span class="hljs-number">1</span>, <span class="hljs-number">6</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3303<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> 3304<h4><code>buf.readUIntLE(offset, byteLength)</code><span><a class="mark" href="#bufreaduintleoffset-bytelength" id="bufreaduintleoffset-bytelength">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readuintle_offset_bytelength"></a></h4> 3305<div class="api_metadata"> 3306<details class="changelog"><summary>History</summary> 3307<table> 3308<tbody><tr><th>Version</th><th>Changes</th></tr> 3309<tr><td>v14.9.0, v12.19.0</td> 3310<td><p>This function is also available as <code>buf.readUintLE()</code>.</p></td></tr> 3311<tr><td>v10.0.0</td> 3312<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset and <code>byteLength</code> to <code>uint32</code> anymore.</p></td></tr> 3313<tr><td>v0.11.15</td> 3314<td><p><span>Added in: v0.11.15</span></p></td></tr> 3315</tbody></table> 3316</details> 3317</div> 3318<ul> 3319<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to read. Must 3320satisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> 3321<li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to read. Must satisfy 3322<code>0 < byteLength <= 6</code>.</li> 3323<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3324</ul> 3325<p>Reads <code>byteLength</code> number of bytes from <code>buf</code> at the specified <code>offset</code> 3326and interprets the result as an unsigned, little-endian integer supporting 3327up to 48 bits of accuracy.</p> 3328<p>This function is also available under the <code>readUintLE</code> alias.</p> 3329 3330<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 3331 3332<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>, <span class="hljs-number">0x78</span>, <span class="hljs-number">0x90</span>, <span class="hljs-number">0xab</span>]); 3333 3334<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUIntLE</span>(<span class="hljs-number">0</span>, <span class="hljs-number">6</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3335<span class="hljs-comment">// Prints: ab9078563412</span></code><code class="language-js cjs"><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>); 3336 3337<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>, <span class="hljs-number">0x78</span>, <span class="hljs-number">0x90</span>, <span class="hljs-number">0xab</span>]); 3338 3339<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUIntLE</span>(<span class="hljs-number">0</span>, <span class="hljs-number">6</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>)); 3340<span class="hljs-comment">// Prints: ab9078563412</span></code><button class="copy-button">copy</button></pre> 3341<h4><code>buf.subarray([start[, end]])</code><span><a class="mark" href="#bufsubarraystart-end" id="bufsubarraystart-end">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_subarray_start_end"></a></h4> 3342<div class="api_metadata"> 3343<span>Added in: v3.0.0</span> 3344</div> 3345<ul> 3346<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Where the new <code>Buffer</code> will start. <strong>Default:</strong> <code>0</code>.</li> 3347<li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Where the new <code>Buffer</code> will end (not inclusive). 3348<strong>Default:</strong> <a href="#buflength"><code>buf.length</code></a>.</li> 3349<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 3350</ul> 3351<p>Returns a new <code>Buffer</code> that references the same memory as the original, but 3352offset and cropped by the <code>start</code> and <code>end</code> indices.</p> 3353<p>Specifying <code>end</code> greater than <a href="#buflength"><code>buf.length</code></a> will return the same result as 3354that of <code>end</code> equal to <a href="#buflength"><code>buf.length</code></a>.</p> 3355<p>This method is inherited from <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/subarray"><code>TypedArray.prototype.subarray()</code></a>.</p> 3356<p>Modifying the new <code>Buffer</code> slice will modify the memory in the original <code>Buffer</code> 3357because the allocated memory of the two objects overlap.</p> 3358 3359<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 3360 3361<span class="hljs-comment">// Create a `Buffer` with the ASCII alphabet, take a slice, and modify one byte</span> 3362<span class="hljs-comment">// from the original `Buffer`.</span> 3363 3364<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">26</span>); 3365 3366<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">26</span>; i++) { 3367 <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> 3368 buf1[i] = i + <span class="hljs-number">97</span>; 3369} 3370 3371<span class="hljs-keyword">const</span> buf2 = buf1.<span class="hljs-title function_">subarray</span>(<span class="hljs-number">0</span>, <span class="hljs-number">3</span>); 3372 3373<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'ascii'</span>, <span class="hljs-number">0</span>, buf2.<span class="hljs-property">length</span>)); 3374<span class="hljs-comment">// Prints: abc</span> 3375 3376buf1[<span class="hljs-number">0</span>] = <span class="hljs-number">33</span>; 3377 3378<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'ascii'</span>, <span class="hljs-number">0</span>, buf2.<span class="hljs-property">length</span>)); 3379<span class="hljs-comment">// Prints: !bc</span></code><code class="language-js cjs"><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>); 3380 3381<span class="hljs-comment">// Create a `Buffer` with the ASCII alphabet, take a slice, and modify one byte</span> 3382<span class="hljs-comment">// from the original `Buffer`.</span> 3383 3384<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">26</span>); 3385 3386<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">26</span>; i++) { 3387 <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> 3388 buf1[i] = i + <span class="hljs-number">97</span>; 3389} 3390 3391<span class="hljs-keyword">const</span> buf2 = buf1.<span class="hljs-title function_">subarray</span>(<span class="hljs-number">0</span>, <span class="hljs-number">3</span>); 3392 3393<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'ascii'</span>, <span class="hljs-number">0</span>, buf2.<span class="hljs-property">length</span>)); 3394<span class="hljs-comment">// Prints: abc</span> 3395 3396buf1[<span class="hljs-number">0</span>] = <span class="hljs-number">33</span>; 3397 3398<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'ascii'</span>, <span class="hljs-number">0</span>, buf2.<span class="hljs-property">length</span>)); 3399<span class="hljs-comment">// Prints: !bc</span></code><button class="copy-button">copy</button></pre> 3400<p>Specifying negative indexes causes the slice to be generated relative to the 3401end of <code>buf</code> rather than the beginning.</p> 3402 3403<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 3404 3405<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>); 3406 3407<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">subarray</span>(-<span class="hljs-number">6</span>, -<span class="hljs-number">1</span>).<span class="hljs-title function_">toString</span>()); 3408<span class="hljs-comment">// Prints: buffe</span> 3409<span class="hljs-comment">// (Equivalent to buf.subarray(0, 5).)</span> 3410 3411<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">subarray</span>(-<span class="hljs-number">6</span>, -<span class="hljs-number">2</span>).<span class="hljs-title function_">toString</span>()); 3412<span class="hljs-comment">// Prints: buff</span> 3413<span class="hljs-comment">// (Equivalent to buf.subarray(0, 4).)</span> 3414 3415<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">subarray</span>(-<span class="hljs-number">5</span>, -<span class="hljs-number">2</span>).<span class="hljs-title function_">toString</span>()); 3416<span class="hljs-comment">// Prints: uff</span> 3417<span class="hljs-comment">// (Equivalent to buf.subarray(1, 4).)</span></code><code class="language-js cjs"><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>); 3418 3419<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>); 3420 3421<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">subarray</span>(-<span class="hljs-number">6</span>, -<span class="hljs-number">1</span>).<span class="hljs-title function_">toString</span>()); 3422<span class="hljs-comment">// Prints: buffe</span> 3423<span class="hljs-comment">// (Equivalent to buf.subarray(0, 5).)</span> 3424 3425<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">subarray</span>(-<span class="hljs-number">6</span>, -<span class="hljs-number">2</span>).<span class="hljs-title function_">toString</span>()); 3426<span class="hljs-comment">// Prints: buff</span> 3427<span class="hljs-comment">// (Equivalent to buf.subarray(0, 4).)</span> 3428 3429<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">subarray</span>(-<span class="hljs-number">5</span>, -<span class="hljs-number">2</span>).<span class="hljs-title function_">toString</span>()); 3430<span class="hljs-comment">// Prints: uff</span> 3431<span class="hljs-comment">// (Equivalent to buf.subarray(1, 4).)</span></code><button class="copy-button">copy</button></pre> 3432<h4><code>buf.slice([start[, end]])</code><span><a class="mark" href="#bufslicestart-end" id="bufslicestart-end">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_slice_start_end"></a></h4> 3433<div class="api_metadata"> 3434<details class="changelog"><summary>History</summary> 3435<table> 3436<tbody><tr><th>Version</th><th>Changes</th></tr> 3437<tr><td>v17.5.0</td> 3438<td><p>The buf.slice() method has been deprecated.</p></td></tr> 3439<tr><td>v7.0.0</td> 3440<td><p>All offsets are now coerced to integers before doing any calculations with them.</p></td></tr> 3441<tr><td>v7.1.0, v6.9.2</td> 3442<td><p>Coercing the offsets to integers now handles values outside the 32-bit integer range properly.</p></td></tr> 3443<tr><td>v0.3.0</td> 3444<td><p><span>Added in: v0.3.0</span></p></td></tr> 3445</tbody></table> 3446</details> 3447</div> 3448<ul> 3449<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Where the new <code>Buffer</code> will start. <strong>Default:</strong> <code>0</code>.</li> 3450<li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Where the new <code>Buffer</code> will end (not inclusive). 3451<strong>Default:</strong> <a href="#buflength"><code>buf.length</code></a>.</li> 3452<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 3453</ul> 3454<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#bufsubarraystart-end"><code>buf.subarray</code></a> instead.</div><p></p> 3455<p>Returns a new <code>Buffer</code> that references the same memory as the original, but 3456offset and cropped by the <code>start</code> and <code>end</code> indices.</p> 3457<p>This method is not compatible with the <code>Uint8Array.prototype.slice()</code>, 3458which is a superclass of <code>Buffer</code>. To copy the slice, use 3459<code>Uint8Array.prototype.slice()</code>.</p> 3460 3461<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 3462 3463<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>); 3464 3465<span class="hljs-keyword">const</span> copiedBuf = <span class="hljs-title class_">Uint8Array</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">slice</span>.<span class="hljs-title function_">call</span>(buf); 3466copiedBuf[<span class="hljs-number">0</span>]++; 3467<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(copiedBuf.<span class="hljs-title function_">toString</span>()); 3468<span class="hljs-comment">// Prints: cuffer</span> 3469 3470<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>()); 3471<span class="hljs-comment">// Prints: buffer</span> 3472 3473<span class="hljs-comment">// With buf.slice(), the original buffer is modified.</span> 3474<span class="hljs-keyword">const</span> notReallyCopiedBuf = buf.<span class="hljs-title function_">slice</span>(); 3475notReallyCopiedBuf[<span class="hljs-number">0</span>]++; 3476<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(notReallyCopiedBuf.<span class="hljs-title function_">toString</span>()); 3477<span class="hljs-comment">// Prints: cuffer</span> 3478<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>()); 3479<span class="hljs-comment">// Also prints: cuffer (!)</span></code><code class="language-js cjs"><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>); 3480 3481<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>); 3482 3483<span class="hljs-keyword">const</span> copiedBuf = <span class="hljs-title class_">Uint8Array</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">slice</span>.<span class="hljs-title function_">call</span>(buf); 3484copiedBuf[<span class="hljs-number">0</span>]++; 3485<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(copiedBuf.<span class="hljs-title function_">toString</span>()); 3486<span class="hljs-comment">// Prints: cuffer</span> 3487 3488<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>()); 3489<span class="hljs-comment">// Prints: buffer</span> 3490 3491<span class="hljs-comment">// With buf.slice(), the original buffer is modified.</span> 3492<span class="hljs-keyword">const</span> notReallyCopiedBuf = buf.<span class="hljs-title function_">slice</span>(); 3493notReallyCopiedBuf[<span class="hljs-number">0</span>]++; 3494<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(notReallyCopiedBuf.<span class="hljs-title function_">toString</span>()); 3495<span class="hljs-comment">// Prints: cuffer</span> 3496<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>()); 3497<span class="hljs-comment">// Also prints: cuffer (!)</span></code><button class="copy-button">copy</button></pre> 3498<h4><code>buf.swap16()</code><span><a class="mark" href="#bufswap16" id="bufswap16">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_swap16"></a></h4> 3499<div class="api_metadata"> 3500<span>Added in: v5.10.0</span> 3501</div> 3502<ul> 3503<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> A reference to <code>buf</code>.</li> 3504</ul> 3505<p>Interprets <code>buf</code> as an array of unsigned 16-bit integers and swaps the 3506byte order <em>in-place</em>. Throws <a href="errors.html#err_invalid_buffer_size"><code>ERR_INVALID_BUFFER_SIZE</code></a> if <a href="#buflength"><code>buf.length</code></a> 3507is not a multiple of 2.</p> 3508 3509<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 3510 3511<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>, <span class="hljs-number">0x4</span>, <span class="hljs-number">0x5</span>, <span class="hljs-number">0x6</span>, <span class="hljs-number">0x7</span>, <span class="hljs-number">0x8</span>]); 3512 3513<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 3514<span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05 06 07 08></span> 3515 3516buf1.<span class="hljs-title function_">swap16</span>(); 3517 3518<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 3519<span class="hljs-comment">// Prints: <Buffer 02 01 04 03 06 05 08 07></span> 3520 3521<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>]); 3522 3523buf2.<span class="hljs-title function_">swap16</span>(); 3524<span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code><code class="language-js cjs"><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>); 3525 3526<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>, <span class="hljs-number">0x4</span>, <span class="hljs-number">0x5</span>, <span class="hljs-number">0x6</span>, <span class="hljs-number">0x7</span>, <span class="hljs-number">0x8</span>]); 3527 3528<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 3529<span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05 06 07 08></span> 3530 3531buf1.<span class="hljs-title function_">swap16</span>(); 3532 3533<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 3534<span class="hljs-comment">// Prints: <Buffer 02 01 04 03 06 05 08 07></span> 3535 3536<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>]); 3537 3538buf2.<span class="hljs-title function_">swap16</span>(); 3539<span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code><button class="copy-button">copy</button></pre> 3540<p>One convenient use of <code>buf.swap16()</code> is to perform a fast in-place conversion 3541between UTF-16 little-endian and UTF-16 big-endian:</p> 3542 3543<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 3544 3545<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'This is little-endian UTF-16'</span>, <span class="hljs-string">'utf16le'</span>); 3546buf.<span class="hljs-title function_">swap16</span>(); <span class="hljs-comment">// Convert to big-endian UTF-16 text.</span></code><code class="language-js cjs"><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>); 3547 3548<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'This is little-endian UTF-16'</span>, <span class="hljs-string">'utf16le'</span>); 3549buf.<span class="hljs-title function_">swap16</span>(); <span class="hljs-comment">// Convert to big-endian UTF-16 text.</span></code><button class="copy-button">copy</button></pre> 3550<h4><code>buf.swap32()</code><span><a class="mark" href="#bufswap32" id="bufswap32">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_swap32"></a></h4> 3551<div class="api_metadata"> 3552<span>Added in: v5.10.0</span> 3553</div> 3554<ul> 3555<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> A reference to <code>buf</code>.</li> 3556</ul> 3557<p>Interprets <code>buf</code> as an array of unsigned 32-bit integers and swaps the 3558byte order <em>in-place</em>. Throws <a href="errors.html#err_invalid_buffer_size"><code>ERR_INVALID_BUFFER_SIZE</code></a> if <a href="#buflength"><code>buf.length</code></a> 3559is not a multiple of 4.</p> 3560 3561<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 3562 3563<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>, <span class="hljs-number">0x4</span>, <span class="hljs-number">0x5</span>, <span class="hljs-number">0x6</span>, <span class="hljs-number">0x7</span>, <span class="hljs-number">0x8</span>]); 3564 3565<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 3566<span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05 06 07 08></span> 3567 3568buf1.<span class="hljs-title function_">swap32</span>(); 3569 3570<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 3571<span class="hljs-comment">// Prints: <Buffer 04 03 02 01 08 07 06 05></span> 3572 3573<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>]); 3574 3575buf2.<span class="hljs-title function_">swap32</span>(); 3576<span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code><code class="language-js cjs"><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>); 3577 3578<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>, <span class="hljs-number">0x4</span>, <span class="hljs-number">0x5</span>, <span class="hljs-number">0x6</span>, <span class="hljs-number">0x7</span>, <span class="hljs-number">0x8</span>]); 3579 3580<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 3581<span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05 06 07 08></span> 3582 3583buf1.<span class="hljs-title function_">swap32</span>(); 3584 3585<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 3586<span class="hljs-comment">// Prints: <Buffer 04 03 02 01 08 07 06 05></span> 3587 3588<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>]); 3589 3590buf2.<span class="hljs-title function_">swap32</span>(); 3591<span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code><button class="copy-button">copy</button></pre> 3592<h4><code>buf.swap64()</code><span><a class="mark" href="#bufswap64" id="bufswap64">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_swap64"></a></h4> 3593<div class="api_metadata"> 3594<span>Added in: v6.3.0</span> 3595</div> 3596<ul> 3597<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> A reference to <code>buf</code>.</li> 3598</ul> 3599<p>Interprets <code>buf</code> as an array of 64-bit numbers and swaps byte order <em>in-place</em>. 3600Throws <a href="errors.html#err_invalid_buffer_size"><code>ERR_INVALID_BUFFER_SIZE</code></a> if <a href="#buflength"><code>buf.length</code></a> is not a multiple of 8.</p> 3601 3602<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 3603 3604<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>, <span class="hljs-number">0x4</span>, <span class="hljs-number">0x5</span>, <span class="hljs-number">0x6</span>, <span class="hljs-number">0x7</span>, <span class="hljs-number">0x8</span>]); 3605 3606<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 3607<span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05 06 07 08></span> 3608 3609buf1.<span class="hljs-title function_">swap64</span>(); 3610 3611<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 3612<span class="hljs-comment">// Prints: <Buffer 08 07 06 05 04 03 02 01></span> 3613 3614<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>]); 3615 3616buf2.<span class="hljs-title function_">swap64</span>(); 3617<span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code><code class="language-js cjs"><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>); 3618 3619<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>, <span class="hljs-number">0x4</span>, <span class="hljs-number">0x5</span>, <span class="hljs-number">0x6</span>, <span class="hljs-number">0x7</span>, <span class="hljs-number">0x8</span>]); 3620 3621<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 3622<span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05 06 07 08></span> 3623 3624buf1.<span class="hljs-title function_">swap64</span>(); 3625 3626<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 3627<span class="hljs-comment">// Prints: <Buffer 08 07 06 05 04 03 02 01></span> 3628 3629<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>]); 3630 3631buf2.<span class="hljs-title function_">swap64</span>(); 3632<span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code><button class="copy-button">copy</button></pre> 3633<h4><code>buf.toJSON()</code><span><a class="mark" href="#buftojson" id="buftojson">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_tojson"></a></h4> 3634<div class="api_metadata"> 3635<span>Added in: v0.9.2</span> 3636</div> 3637<ul> 3638<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 3639</ul> 3640<p>Returns a JSON representation of <code>buf</code>. <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify"><code>JSON.stringify()</code></a> implicitly calls 3641this function when stringifying a <code>Buffer</code> instance.</p> 3642<p><code>Buffer.from()</code> accepts objects in the format returned from this method. 3643In particular, <code>Buffer.from(buf.toJSON())</code> works like <code>Buffer.from(buf)</code>.</p> 3644 3645<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 3646 3647<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>, <span class="hljs-number">0x4</span>, <span class="hljs-number">0x5</span>]); 3648<span class="hljs-keyword">const</span> json = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(buf); 3649 3650<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(json); 3651<span class="hljs-comment">// Prints: {"type":"Buffer","data":[1,2,3,4,5]}</span> 3652 3653<span class="hljs-keyword">const</span> copy = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">parse</span>(json, <span class="hljs-function">(<span class="hljs-params">key, value</span>) =></span> { 3654 <span class="hljs-keyword">return</span> value && value.<span class="hljs-property">type</span> === <span class="hljs-string">'Buffer'</span> ? 3655 <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(value) : 3656 value; 3657}); 3658 3659<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(copy); 3660<span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05></span></code><code class="language-js cjs"><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>); 3661 3662<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>, <span class="hljs-number">0x4</span>, <span class="hljs-number">0x5</span>]); 3663<span class="hljs-keyword">const</span> json = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(buf); 3664 3665<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(json); 3666<span class="hljs-comment">// Prints: {"type":"Buffer","data":[1,2,3,4,5]}</span> 3667 3668<span class="hljs-keyword">const</span> copy = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">parse</span>(json, <span class="hljs-function">(<span class="hljs-params">key, value</span>) =></span> { 3669 <span class="hljs-keyword">return</span> value && value.<span class="hljs-property">type</span> === <span class="hljs-string">'Buffer'</span> ? 3670 <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(value) : 3671 value; 3672}); 3673 3674<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(copy); 3675<span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05></span></code><button class="copy-button">copy</button></pre> 3676<h4><code>buf.toString([encoding[, start[, end]]])</code><span><a class="mark" href="#buftostringencoding-start-end" id="buftostringencoding-start-end">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_tostring_encoding_start_end"></a></h4> 3677<div class="api_metadata"> 3678<span>Added in: v0.1.90</span> 3679</div> 3680<ul> 3681<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The character encoding to use. <strong>Default:</strong> <code>'utf8'</code>.</li> 3682<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The byte offset to start decoding at. <strong>Default:</strong> <code>0</code>.</li> 3683<li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The byte offset to stop decoding at (not inclusive). 3684<strong>Default:</strong> <a href="#buflength"><code>buf.length</code></a>.</li> 3685<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3686</ul> 3687<p>Decodes <code>buf</code> to a string according to the specified character encoding in 3688<code>encoding</code>. <code>start</code> and <code>end</code> may be passed to decode only a subset of <code>buf</code>.</p> 3689<p>If <code>encoding</code> is <code>'utf8'</code> and a byte sequence in the input is not valid UTF-8, 3690then each invalid byte is replaced with the replacement character <code>U+FFFD</code>.</p> 3691<p>The maximum length of a string instance (in UTF-16 code units) is available 3692as <a href="#bufferconstantsmax_string_length"><code>buffer.constants.MAX_STRING_LENGTH</code></a>.</p> 3693 3694<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 3695 3696<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">26</span>); 3697 3698<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">26</span>; i++) { 3699 <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> 3700 buf1[i] = i + <span class="hljs-number">97</span>; 3701} 3702 3703<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'utf8'</span>)); 3704<span class="hljs-comment">// Prints: abcdefghijklmnopqrstuvwxyz</span> 3705<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'utf8'</span>, <span class="hljs-number">0</span>, <span class="hljs-number">5</span>)); 3706<span class="hljs-comment">// Prints: abcde</span> 3707 3708<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'tést'</span>); 3709 3710<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>)); 3711<span class="hljs-comment">// Prints: 74c3a97374</span> 3712<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'utf8'</span>, <span class="hljs-number">0</span>, <span class="hljs-number">3</span>)); 3713<span class="hljs-comment">// Prints: té</span> 3714<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>(<span class="hljs-literal">undefined</span>, <span class="hljs-number">0</span>, <span class="hljs-number">3</span>)); 3715<span class="hljs-comment">// Prints: té</span></code><code class="language-js cjs"><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>); 3716 3717<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">26</span>); 3718 3719<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">26</span>; i++) { 3720 <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> 3721 buf1[i] = i + <span class="hljs-number">97</span>; 3722} 3723 3724<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'utf8'</span>)); 3725<span class="hljs-comment">// Prints: abcdefghijklmnopqrstuvwxyz</span> 3726<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'utf8'</span>, <span class="hljs-number">0</span>, <span class="hljs-number">5</span>)); 3727<span class="hljs-comment">// Prints: abcde</span> 3728 3729<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'tést'</span>); 3730 3731<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>)); 3732<span class="hljs-comment">// Prints: 74c3a97374</span> 3733<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'utf8'</span>, <span class="hljs-number">0</span>, <span class="hljs-number">3</span>)); 3734<span class="hljs-comment">// Prints: té</span> 3735<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>(<span class="hljs-literal">undefined</span>, <span class="hljs-number">0</span>, <span class="hljs-number">3</span>)); 3736<span class="hljs-comment">// Prints: té</span></code><button class="copy-button">copy</button></pre> 3737<h4><code>buf.values()</code><span><a class="mark" href="#bufvalues" id="bufvalues">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_values"></a></h4> 3738<div class="api_metadata"> 3739<span>Added in: v1.1.0</span> 3740</div> 3741<ul> 3742<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> 3743</ul> 3744<p>Creates and returns an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols">iterator</a> for <code>buf</code> values (bytes). This function is 3745called automatically when a <code>Buffer</code> is used in a <code>for..of</code> statement.</p> 3746 3747<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 3748 3749<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>); 3750 3751<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> value <span class="hljs-keyword">of</span> buf.<span class="hljs-title function_">values</span>()) { 3752 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value); 3753} 3754<span class="hljs-comment">// Prints:</span> 3755<span class="hljs-comment">// 98</span> 3756<span class="hljs-comment">// 117</span> 3757<span class="hljs-comment">// 102</span> 3758<span class="hljs-comment">// 102</span> 3759<span class="hljs-comment">// 101</span> 3760<span class="hljs-comment">// 114</span> 3761 3762<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> value <span class="hljs-keyword">of</span> buf) { 3763 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value); 3764} 3765<span class="hljs-comment">// Prints:</span> 3766<span class="hljs-comment">// 98</span> 3767<span class="hljs-comment">// 117</span> 3768<span class="hljs-comment">// 102</span> 3769<span class="hljs-comment">// 102</span> 3770<span class="hljs-comment">// 101</span> 3771<span class="hljs-comment">// 114</span></code><code class="language-js cjs"><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>); 3772 3773<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>); 3774 3775<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> value <span class="hljs-keyword">of</span> buf.<span class="hljs-title function_">values</span>()) { 3776 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value); 3777} 3778<span class="hljs-comment">// Prints:</span> 3779<span class="hljs-comment">// 98</span> 3780<span class="hljs-comment">// 117</span> 3781<span class="hljs-comment">// 102</span> 3782<span class="hljs-comment">// 102</span> 3783<span class="hljs-comment">// 101</span> 3784<span class="hljs-comment">// 114</span> 3785 3786<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> value <span class="hljs-keyword">of</span> buf) { 3787 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value); 3788} 3789<span class="hljs-comment">// Prints:</span> 3790<span class="hljs-comment">// 98</span> 3791<span class="hljs-comment">// 117</span> 3792<span class="hljs-comment">// 102</span> 3793<span class="hljs-comment">// 102</span> 3794<span class="hljs-comment">// 101</span> 3795<span class="hljs-comment">// 114</span></code><button class="copy-button">copy</button></pre> 3796<h4><code>buf.write(string[, offset[, length]][, encoding])</code><span><a class="mark" href="#bufwritestring-offset-length-encoding" id="bufwritestring-offset-length-encoding">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_write_string_offset_length_encoding"></a></h4> 3797<div class="api_metadata"> 3798<span>Added in: v0.1.90</span> 3799</div> 3800<ul> 3801<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> String to write to <code>buf</code>.</li> 3802<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to write <code>string</code>. 3803<strong>Default:</strong> <code>0</code>.</li> 3804<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Maximum number of bytes to write (written bytes will not 3805exceed <code>buf.length - offset</code>). <strong>Default:</strong> <code>buf.length - offset</code>.</li> 3806<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The character encoding of <code>string</code>. <strong>Default:</strong> <code>'utf8'</code>.</li> 3807<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes written.</li> 3808</ul> 3809<p>Writes <code>string</code> to <code>buf</code> at <code>offset</code> according to the character encoding in 3810<code>encoding</code>. The <code>length</code> parameter is the number of bytes to write. If <code>buf</code> did 3811not contain enough space to fit the entire string, only part of <code>string</code> will be 3812written. However, partially encoded characters will not be written.</p> 3813 3814<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 3815 3816<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">256</span>); 3817 3818<span class="hljs-keyword">const</span> len = buf.<span class="hljs-title function_">write</span>(<span class="hljs-string">'\u00bd + \u00bc = \u00be'</span>, <span class="hljs-number">0</span>); 3819 3820<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${len}</span> bytes: <span class="hljs-subst">${buf.toString(<span class="hljs-string">'utf8'</span>, <span class="hljs-number">0</span>, len)}</span>`</span>); 3821<span class="hljs-comment">// Prints: 12 bytes: ½ + ¼ = ¾</span> 3822 3823<span class="hljs-keyword">const</span> buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">10</span>); 3824 3825<span class="hljs-keyword">const</span> length = buffer.<span class="hljs-title function_">write</span>(<span class="hljs-string">'abcd'</span>, <span class="hljs-number">8</span>); 3826 3827<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${length}</span> bytes: <span class="hljs-subst">${buffer.toString(<span class="hljs-string">'utf8'</span>, <span class="hljs-number">8</span>, <span class="hljs-number">10</span>)}</span>`</span>); 3828<span class="hljs-comment">// Prints: 2 bytes : ab</span></code><code class="language-js cjs"><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>); 3829 3830<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">256</span>); 3831 3832<span class="hljs-keyword">const</span> len = buf.<span class="hljs-title function_">write</span>(<span class="hljs-string">'\u00bd + \u00bc = \u00be'</span>, <span class="hljs-number">0</span>); 3833 3834<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${len}</span> bytes: <span class="hljs-subst">${buf.toString(<span class="hljs-string">'utf8'</span>, <span class="hljs-number">0</span>, len)}</span>`</span>); 3835<span class="hljs-comment">// Prints: 12 bytes: ½ + ¼ = ¾</span> 3836 3837<span class="hljs-keyword">const</span> buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">10</span>); 3838 3839<span class="hljs-keyword">const</span> length = buffer.<span class="hljs-title function_">write</span>(<span class="hljs-string">'abcd'</span>, <span class="hljs-number">8</span>); 3840 3841<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${length}</span> bytes: <span class="hljs-subst">${buffer.toString(<span class="hljs-string">'utf8'</span>, <span class="hljs-number">8</span>, <span class="hljs-number">10</span>)}</span>`</span>); 3842<span class="hljs-comment">// Prints: 2 bytes : ab</span></code><button class="copy-button">copy</button></pre> 3843<h4><code>buf.writeBigInt64BE(value[, offset])</code><span><a class="mark" href="#bufwritebigint64bevalue-offset" id="bufwritebigint64bevalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writebigint64be_value_offset"></a></h4> 3844<div class="api_metadata"> 3845<span>Added in: v12.0.0, v10.20.0</span> 3846</div> 3847<ul> 3848<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a> Number to be written to <code>buf</code>.</li> 3849<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to write. Must 3850satisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 3851<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <code>offset</code> plus the number of bytes written.</li> 3852</ul> 3853<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian.</p> 3854<p><code>value</code> is interpreted and written as a two's complement signed integer.</p> 3855 3856<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 3857 3858<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">8</span>); 3859 3860buf.<span class="hljs-title function_">writeBigInt64BE</span>(<span class="hljs-number">0x0102030405060708n</span>, <span class="hljs-number">0</span>); 3861 3862<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 3863<span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05 06 07 08></span></code><code class="language-js cjs"><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>); 3864 3865<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">8</span>); 3866 3867buf.<span class="hljs-title function_">writeBigInt64BE</span>(<span class="hljs-number">0x0102030405060708n</span>, <span class="hljs-number">0</span>); 3868 3869<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 3870<span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05 06 07 08></span></code><button class="copy-button">copy</button></pre> 3871<h4><code>buf.writeBigInt64LE(value[, offset])</code><span><a class="mark" href="#bufwritebigint64levalue-offset" id="bufwritebigint64levalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writebigint64le_value_offset"></a></h4> 3872<div class="api_metadata"> 3873<span>Added in: v12.0.0, v10.20.0</span> 3874</div> 3875<ul> 3876<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a> Number to be written to <code>buf</code>.</li> 3877<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to write. Must 3878satisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 3879<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <code>offset</code> plus the number of bytes written.</li> 3880</ul> 3881<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian.</p> 3882<p><code>value</code> is interpreted and written as a two's complement signed integer.</p> 3883 3884<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 3885 3886<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">8</span>); 3887 3888buf.<span class="hljs-title function_">writeBigInt64LE</span>(<span class="hljs-number">0x0102030405060708n</span>, <span class="hljs-number">0</span>); 3889 3890<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 3891<span class="hljs-comment">// Prints: <Buffer 08 07 06 05 04 03 02 01></span></code><code class="language-js cjs"><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>); 3892 3893<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">8</span>); 3894 3895buf.<span class="hljs-title function_">writeBigInt64LE</span>(<span class="hljs-number">0x0102030405060708n</span>, <span class="hljs-number">0</span>); 3896 3897<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 3898<span class="hljs-comment">// Prints: <Buffer 08 07 06 05 04 03 02 01></span></code><button class="copy-button">copy</button></pre> 3899<h4><code>buf.writeBigUInt64BE(value[, offset])</code><span><a class="mark" href="#bufwritebiguint64bevalue-offset" id="bufwritebiguint64bevalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writebiguint64be_value_offset"></a></h4> 3900<div class="api_metadata"> 3901<details class="changelog"><summary>History</summary> 3902<table> 3903<tbody><tr><th>Version</th><th>Changes</th></tr> 3904<tr><td>v14.10.0, v12.19.0</td> 3905<td><p>This function is also available as <code>buf.writeBigUint64BE()</code>.</p></td></tr> 3906<tr><td>v12.0.0, v10.20.0</td> 3907<td><p><span>Added in: v12.0.0, v10.20.0</span></p></td></tr> 3908</tbody></table> 3909</details> 3910</div> 3911<ul> 3912<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a> Number to be written to <code>buf</code>.</li> 3913<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to write. Must 3914satisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 3915<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <code>offset</code> plus the number of bytes written.</li> 3916</ul> 3917<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian.</p> 3918<p>This function is also available under the <code>writeBigUint64BE</code> alias.</p> 3919 3920<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 3921 3922<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">8</span>); 3923 3924buf.<span class="hljs-title function_">writeBigUInt64BE</span>(<span class="hljs-number">0xdecafafecacefaden</span>, <span class="hljs-number">0</span>); 3925 3926<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 3927<span class="hljs-comment">// Prints: <Buffer de ca fa fe ca ce fa de></span></code><code class="language-js cjs"><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>); 3928 3929<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">8</span>); 3930 3931buf.<span class="hljs-title function_">writeBigUInt64BE</span>(<span class="hljs-number">0xdecafafecacefaden</span>, <span class="hljs-number">0</span>); 3932 3933<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 3934<span class="hljs-comment">// Prints: <Buffer de ca fa fe ca ce fa de></span></code><button class="copy-button">copy</button></pre> 3935<h4><code>buf.writeBigUInt64LE(value[, offset])</code><span><a class="mark" href="#bufwritebiguint64levalue-offset" id="bufwritebiguint64levalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writebiguint64le_value_offset"></a></h4> 3936<div class="api_metadata"> 3937<details class="changelog"><summary>History</summary> 3938<table> 3939<tbody><tr><th>Version</th><th>Changes</th></tr> 3940<tr><td>v14.10.0, v12.19.0</td> 3941<td><p>This function is also available as <code>buf.writeBigUint64LE()</code>.</p></td></tr> 3942<tr><td>v12.0.0, v10.20.0</td> 3943<td><p><span>Added in: v12.0.0, v10.20.0</span></p></td></tr> 3944</tbody></table> 3945</details> 3946</div> 3947<ul> 3948<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a> Number to be written to <code>buf</code>.</li> 3949<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to write. Must 3950satisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 3951<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <code>offset</code> plus the number of bytes written.</li> 3952</ul> 3953<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian</p> 3954 3955<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 3956 3957<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">8</span>); 3958 3959buf.<span class="hljs-title function_">writeBigUInt64LE</span>(<span class="hljs-number">0xdecafafecacefaden</span>, <span class="hljs-number">0</span>); 3960 3961<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 3962<span class="hljs-comment">// Prints: <Buffer de fa ce ca fe fa ca de></span></code><code class="language-js cjs"><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>); 3963 3964<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">8</span>); 3965 3966buf.<span class="hljs-title function_">writeBigUInt64LE</span>(<span class="hljs-number">0xdecafafecacefaden</span>, <span class="hljs-number">0</span>); 3967 3968<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 3969<span class="hljs-comment">// Prints: <Buffer de fa ce ca fe fa ca de></span></code><button class="copy-button">copy</button></pre> 3970<p>This function is also available under the <code>writeBigUint64LE</code> alias.</p> 3971<h4><code>buf.writeDoubleBE(value[, offset])</code><span><a class="mark" href="#bufwritedoublebevalue-offset" id="bufwritedoublebevalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writedoublebe_value_offset"></a></h4> 3972<div class="api_metadata"> 3973<details class="changelog"><summary>History</summary> 3974<table> 3975<tbody><tr><th>Version</th><th>Changes</th></tr> 3976<tr><td>v10.0.0</td> 3977<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 3978<tr><td>v0.11.15</td> 3979<td><p><span>Added in: v0.11.15</span></p></td></tr> 3980</tbody></table> 3981</details> 3982</div> 3983<ul> 3984<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Number to be written to <code>buf</code>.</li> 3985<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to write. Must 3986satisfy <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 3987<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <code>offset</code> plus the number of bytes written.</li> 3988</ul> 3989<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code> 3990must be a JavaScript number. Behavior is undefined when <code>value</code> is anything 3991other than a JavaScript number.</p> 3992 3993<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 3994 3995<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">8</span>); 3996 3997buf.<span class="hljs-title function_">writeDoubleBE</span>(<span class="hljs-number">123.456</span>, <span class="hljs-number">0</span>); 3998 3999<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4000<span class="hljs-comment">// Prints: <Buffer 40 5e dd 2f 1a 9f be 77></span></code><code class="language-js cjs"><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>); 4001 4002<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">8</span>); 4003 4004buf.<span class="hljs-title function_">writeDoubleBE</span>(<span class="hljs-number">123.456</span>, <span class="hljs-number">0</span>); 4005 4006<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4007<span class="hljs-comment">// Prints: <Buffer 40 5e dd 2f 1a 9f be 77></span></code><button class="copy-button">copy</button></pre> 4008<h4><code>buf.writeDoubleLE(value[, offset])</code><span><a class="mark" href="#bufwritedoublelevalue-offset" id="bufwritedoublelevalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writedoublele_value_offset"></a></h4> 4009<div class="api_metadata"> 4010<details class="changelog"><summary>History</summary> 4011<table> 4012<tbody><tr><th>Version</th><th>Changes</th></tr> 4013<tr><td>v10.0.0</td> 4014<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 4015<tr><td>v0.11.15</td> 4016<td><p><span>Added in: v0.11.15</span></p></td></tr> 4017</tbody></table> 4018</details> 4019</div> 4020<ul> 4021<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Number to be written to <code>buf</code>.</li> 4022<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to write. Must 4023satisfy <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 4024<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <code>offset</code> plus the number of bytes written.</li> 4025</ul> 4026<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code> 4027must be a JavaScript number. Behavior is undefined when <code>value</code> is anything 4028other than a JavaScript number.</p> 4029 4030<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 4031 4032<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">8</span>); 4033 4034buf.<span class="hljs-title function_">writeDoubleLE</span>(<span class="hljs-number">123.456</span>, <span class="hljs-number">0</span>); 4035 4036<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4037<span class="hljs-comment">// Prints: <Buffer 77 be 9f 1a 2f dd 5e 40></span></code><code class="language-js cjs"><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>); 4038 4039<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">8</span>); 4040 4041buf.<span class="hljs-title function_">writeDoubleLE</span>(<span class="hljs-number">123.456</span>, <span class="hljs-number">0</span>); 4042 4043<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4044<span class="hljs-comment">// Prints: <Buffer 77 be 9f 1a 2f dd 5e 40></span></code><button class="copy-button">copy</button></pre> 4045<h4><code>buf.writeFloatBE(value[, offset])</code><span><a class="mark" href="#bufwritefloatbevalue-offset" id="bufwritefloatbevalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writefloatbe_value_offset"></a></h4> 4046<div class="api_metadata"> 4047<details class="changelog"><summary>History</summary> 4048<table> 4049<tbody><tr><th>Version</th><th>Changes</th></tr> 4050<tr><td>v10.0.0</td> 4051<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 4052<tr><td>v0.11.15</td> 4053<td><p><span>Added in: v0.11.15</span></p></td></tr> 4054</tbody></table> 4055</details> 4056</div> 4057<ul> 4058<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Number to be written to <code>buf</code>.</li> 4059<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to write. Must 4060satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 4061<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <code>offset</code> plus the number of bytes written.</li> 4062</ul> 4063<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. Behavior is 4064undefined when <code>value</code> is anything other than a JavaScript number.</p> 4065 4066<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 4067 4068<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>); 4069 4070buf.<span class="hljs-title function_">writeFloatBE</span>(<span class="hljs-number">0xcafebabe</span>, <span class="hljs-number">0</span>); 4071 4072<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4073<span class="hljs-comment">// Prints: <Buffer 4f 4a fe bb></span></code><code class="language-js cjs"><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>); 4074 4075<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>); 4076 4077buf.<span class="hljs-title function_">writeFloatBE</span>(<span class="hljs-number">0xcafebabe</span>, <span class="hljs-number">0</span>); 4078 4079<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4080<span class="hljs-comment">// Prints: <Buffer 4f 4a fe bb></span></code><button class="copy-button">copy</button></pre> 4081<h4><code>buf.writeFloatLE(value[, offset])</code><span><a class="mark" href="#bufwritefloatlevalue-offset" id="bufwritefloatlevalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writefloatle_value_offset"></a></h4> 4082<div class="api_metadata"> 4083<details class="changelog"><summary>History</summary> 4084<table> 4085<tbody><tr><th>Version</th><th>Changes</th></tr> 4086<tr><td>v10.0.0</td> 4087<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 4088<tr><td>v0.11.15</td> 4089<td><p><span>Added in: v0.11.15</span></p></td></tr> 4090</tbody></table> 4091</details> 4092</div> 4093<ul> 4094<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Number to be written to <code>buf</code>.</li> 4095<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to write. Must 4096satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 4097<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <code>offset</code> plus the number of bytes written.</li> 4098</ul> 4099<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. Behavior is 4100undefined when <code>value</code> is anything other than a JavaScript number.</p> 4101 4102<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 4103 4104<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>); 4105 4106buf.<span class="hljs-title function_">writeFloatLE</span>(<span class="hljs-number">0xcafebabe</span>, <span class="hljs-number">0</span>); 4107 4108<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4109<span class="hljs-comment">// Prints: <Buffer bb fe 4a 4f></span></code><code class="language-js cjs"><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>); 4110 4111<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>); 4112 4113buf.<span class="hljs-title function_">writeFloatLE</span>(<span class="hljs-number">0xcafebabe</span>, <span class="hljs-number">0</span>); 4114 4115<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4116<span class="hljs-comment">// Prints: <Buffer bb fe 4a 4f></span></code><button class="copy-button">copy</button></pre> 4117<h4><code>buf.writeInt8(value[, offset])</code><span><a class="mark" href="#bufwriteint8value-offset" id="bufwriteint8value-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeint8_value_offset"></a></h4> 4118<div class="api_metadata"> 4119<details class="changelog"><summary>History</summary> 4120<table> 4121<tbody><tr><th>Version</th><th>Changes</th></tr> 4122<tr><td>v10.0.0</td> 4123<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 4124<tr><td>v0.5.0</td> 4125<td><p><span>Added in: v0.5.0</span></p></td></tr> 4126</tbody></table> 4127</details> 4128</div> 4129<ul> 4130<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number to be written to <code>buf</code>.</li> 4131<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to write. Must 4132satisfy <code>0 <= offset <= buf.length - 1</code>. <strong>Default:</strong> <code>0</code>.</li> 4133<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <code>offset</code> plus the number of bytes written.</li> 4134</ul> 4135<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code>. <code>value</code> must be a valid 4136signed 8-bit integer. Behavior is undefined when <code>value</code> is anything other than 4137a signed 8-bit integer.</p> 4138<p><code>value</code> is interpreted and written as a two's complement signed integer.</p> 4139 4140<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 4141 4142<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">2</span>); 4143 4144buf.<span class="hljs-title function_">writeInt8</span>(<span class="hljs-number">2</span>, <span class="hljs-number">0</span>); 4145buf.<span class="hljs-title function_">writeInt8</span>(-<span class="hljs-number">2</span>, <span class="hljs-number">1</span>); 4146 4147<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4148<span class="hljs-comment">// Prints: <Buffer 02 fe></span></code><code class="language-js cjs"><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>); 4149 4150<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">2</span>); 4151 4152buf.<span class="hljs-title function_">writeInt8</span>(<span class="hljs-number">2</span>, <span class="hljs-number">0</span>); 4153buf.<span class="hljs-title function_">writeInt8</span>(-<span class="hljs-number">2</span>, <span class="hljs-number">1</span>); 4154 4155<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4156<span class="hljs-comment">// Prints: <Buffer 02 fe></span></code><button class="copy-button">copy</button></pre> 4157<h4><code>buf.writeInt16BE(value[, offset])</code><span><a class="mark" href="#bufwriteint16bevalue-offset" id="bufwriteint16bevalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeint16be_value_offset"></a></h4> 4158<div class="api_metadata"> 4159<details class="changelog"><summary>History</summary> 4160<table> 4161<tbody><tr><th>Version</th><th>Changes</th></tr> 4162<tr><td>v10.0.0</td> 4163<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 4164<tr><td>v0.5.5</td> 4165<td><p><span>Added in: v0.5.5</span></p></td></tr> 4166</tbody></table> 4167</details> 4168</div> 4169<ul> 4170<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number to be written to <code>buf</code>.</li> 4171<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to write. Must 4172satisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> 4173<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <code>offset</code> plus the number of bytes written.</li> 4174</ul> 4175<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code> 4176must be a valid signed 16-bit integer. Behavior is undefined when <code>value</code> is 4177anything other than a signed 16-bit integer.</p> 4178<p>The <code>value</code> is interpreted and written as a two's complement signed integer.</p> 4179 4180<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 4181 4182<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">2</span>); 4183 4184buf.<span class="hljs-title function_">writeInt16BE</span>(<span class="hljs-number">0x0102</span>, <span class="hljs-number">0</span>); 4185 4186<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4187<span class="hljs-comment">// Prints: <Buffer 01 02></span></code><code class="language-js cjs"><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>); 4188 4189<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">2</span>); 4190 4191buf.<span class="hljs-title function_">writeInt16BE</span>(<span class="hljs-number">0x0102</span>, <span class="hljs-number">0</span>); 4192 4193<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4194<span class="hljs-comment">// Prints: <Buffer 01 02></span></code><button class="copy-button">copy</button></pre> 4195<h4><code>buf.writeInt16LE(value[, offset])</code><span><a class="mark" href="#bufwriteint16levalue-offset" id="bufwriteint16levalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeint16le_value_offset"></a></h4> 4196<div class="api_metadata"> 4197<details class="changelog"><summary>History</summary> 4198<table> 4199<tbody><tr><th>Version</th><th>Changes</th></tr> 4200<tr><td>v10.0.0</td> 4201<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 4202<tr><td>v0.5.5</td> 4203<td><p><span>Added in: v0.5.5</span></p></td></tr> 4204</tbody></table> 4205</details> 4206</div> 4207<ul> 4208<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number to be written to <code>buf</code>.</li> 4209<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to write. Must 4210satisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> 4211<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <code>offset</code> plus the number of bytes written.</li> 4212</ul> 4213<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code> 4214must be a valid signed 16-bit integer. Behavior is undefined when <code>value</code> is 4215anything other than a signed 16-bit integer.</p> 4216<p>The <code>value</code> is interpreted and written as a two's complement signed integer.</p> 4217 4218<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 4219 4220<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">2</span>); 4221 4222buf.<span class="hljs-title function_">writeInt16LE</span>(<span class="hljs-number">0x0304</span>, <span class="hljs-number">0</span>); 4223 4224<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4225<span class="hljs-comment">// Prints: <Buffer 04 03></span></code><code class="language-js cjs"><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>); 4226 4227<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">2</span>); 4228 4229buf.<span class="hljs-title function_">writeInt16LE</span>(<span class="hljs-number">0x0304</span>, <span class="hljs-number">0</span>); 4230 4231<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4232<span class="hljs-comment">// Prints: <Buffer 04 03></span></code><button class="copy-button">copy</button></pre> 4233<h4><code>buf.writeInt32BE(value[, offset])</code><span><a class="mark" href="#bufwriteint32bevalue-offset" id="bufwriteint32bevalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeint32be_value_offset"></a></h4> 4234<div class="api_metadata"> 4235<details class="changelog"><summary>History</summary> 4236<table> 4237<tbody><tr><th>Version</th><th>Changes</th></tr> 4238<tr><td>v10.0.0</td> 4239<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 4240<tr><td>v0.5.5</td> 4241<td><p><span>Added in: v0.5.5</span></p></td></tr> 4242</tbody></table> 4243</details> 4244</div> 4245<ul> 4246<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number to be written to <code>buf</code>.</li> 4247<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to write. Must 4248satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 4249<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <code>offset</code> plus the number of bytes written.</li> 4250</ul> 4251<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code> 4252must be a valid signed 32-bit integer. Behavior is undefined when <code>value</code> is 4253anything other than a signed 32-bit integer.</p> 4254<p>The <code>value</code> is interpreted and written as a two's complement signed integer.</p> 4255 4256<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 4257 4258<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>); 4259 4260buf.<span class="hljs-title function_">writeInt32BE</span>(<span class="hljs-number">0x01020304</span>, <span class="hljs-number">0</span>); 4261 4262<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4263<span class="hljs-comment">// Prints: <Buffer 01 02 03 04></span></code><code class="language-js cjs"><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>); 4264 4265<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>); 4266 4267buf.<span class="hljs-title function_">writeInt32BE</span>(<span class="hljs-number">0x01020304</span>, <span class="hljs-number">0</span>); 4268 4269<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4270<span class="hljs-comment">// Prints: <Buffer 01 02 03 04></span></code><button class="copy-button">copy</button></pre> 4271<h4><code>buf.writeInt32LE(value[, offset])</code><span><a class="mark" href="#bufwriteint32levalue-offset" id="bufwriteint32levalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeint32le_value_offset"></a></h4> 4272<div class="api_metadata"> 4273<details class="changelog"><summary>History</summary> 4274<table> 4275<tbody><tr><th>Version</th><th>Changes</th></tr> 4276<tr><td>v10.0.0</td> 4277<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 4278<tr><td>v0.5.5</td> 4279<td><p><span>Added in: v0.5.5</span></p></td></tr> 4280</tbody></table> 4281</details> 4282</div> 4283<ul> 4284<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number to be written to <code>buf</code>.</li> 4285<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to write. Must 4286satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 4287<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <code>offset</code> plus the number of bytes written.</li> 4288</ul> 4289<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code> 4290must be a valid signed 32-bit integer. Behavior is undefined when <code>value</code> is 4291anything other than a signed 32-bit integer.</p> 4292<p>The <code>value</code> is interpreted and written as a two's complement signed integer.</p> 4293 4294<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 4295 4296<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>); 4297 4298buf.<span class="hljs-title function_">writeInt32LE</span>(<span class="hljs-number">0x05060708</span>, <span class="hljs-number">0</span>); 4299 4300<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4301<span class="hljs-comment">// Prints: <Buffer 08 07 06 05></span></code><code class="language-js cjs"><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>); 4302 4303<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>); 4304 4305buf.<span class="hljs-title function_">writeInt32LE</span>(<span class="hljs-number">0x05060708</span>, <span class="hljs-number">0</span>); 4306 4307<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4308<span class="hljs-comment">// Prints: <Buffer 08 07 06 05></span></code><button class="copy-button">copy</button></pre> 4309<h4><code>buf.writeIntBE(value, offset, byteLength)</code><span><a class="mark" href="#bufwriteintbevalue-offset-bytelength" id="bufwriteintbevalue-offset-bytelength">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeintbe_value_offset_bytelength"></a></h4> 4310<div class="api_metadata"> 4311<details class="changelog"><summary>History</summary> 4312<table> 4313<tbody><tr><th>Version</th><th>Changes</th></tr> 4314<tr><td>v10.0.0</td> 4315<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset and <code>byteLength</code> to <code>uint32</code> anymore.</p></td></tr> 4316<tr><td>v0.11.15</td> 4317<td><p><span>Added in: v0.11.15</span></p></td></tr> 4318</tbody></table> 4319</details> 4320</div> 4321<ul> 4322<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number to be written to <code>buf</code>.</li> 4323<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to write. Must 4324satisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> 4325<li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to write. Must satisfy 4326<code>0 < byteLength <= 6</code>.</li> 4327<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <code>offset</code> plus the number of bytes written.</li> 4328</ul> 4329<p>Writes <code>byteLength</code> bytes of <code>value</code> to <code>buf</code> at the specified <code>offset</code> 4330as big-endian. Supports up to 48 bits of accuracy. Behavior is undefined when 4331<code>value</code> is anything other than a signed integer.</p> 4332 4333<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 4334 4335<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">6</span>); 4336 4337buf.<span class="hljs-title function_">writeIntBE</span>(<span class="hljs-number">0x1234567890ab</span>, <span class="hljs-number">0</span>, <span class="hljs-number">6</span>); 4338 4339<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4340<span class="hljs-comment">// Prints: <Buffer 12 34 56 78 90 ab></span></code><code class="language-js cjs"><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>); 4341 4342<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">6</span>); 4343 4344buf.<span class="hljs-title function_">writeIntBE</span>(<span class="hljs-number">0x1234567890ab</span>, <span class="hljs-number">0</span>, <span class="hljs-number">6</span>); 4345 4346<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4347<span class="hljs-comment">// Prints: <Buffer 12 34 56 78 90 ab></span></code><button class="copy-button">copy</button></pre> 4348<h4><code>buf.writeIntLE(value, offset, byteLength)</code><span><a class="mark" href="#bufwriteintlevalue-offset-bytelength" id="bufwriteintlevalue-offset-bytelength">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeintle_value_offset_bytelength"></a></h4> 4349<div class="api_metadata"> 4350<details class="changelog"><summary>History</summary> 4351<table> 4352<tbody><tr><th>Version</th><th>Changes</th></tr> 4353<tr><td>v10.0.0</td> 4354<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset and <code>byteLength</code> to <code>uint32</code> anymore.</p></td></tr> 4355<tr><td>v0.11.15</td> 4356<td><p><span>Added in: v0.11.15</span></p></td></tr> 4357</tbody></table> 4358</details> 4359</div> 4360<ul> 4361<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number to be written to <code>buf</code>.</li> 4362<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to write. Must 4363satisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> 4364<li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to write. Must satisfy 4365<code>0 < byteLength <= 6</code>.</li> 4366<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <code>offset</code> plus the number of bytes written.</li> 4367</ul> 4368<p>Writes <code>byteLength</code> bytes of <code>value</code> to <code>buf</code> at the specified <code>offset</code> 4369as little-endian. Supports up to 48 bits of accuracy. Behavior is undefined 4370when <code>value</code> is anything other than a signed integer.</p> 4371 4372<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 4373 4374<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">6</span>); 4375 4376buf.<span class="hljs-title function_">writeIntLE</span>(<span class="hljs-number">0x1234567890ab</span>, <span class="hljs-number">0</span>, <span class="hljs-number">6</span>); 4377 4378<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4379<span class="hljs-comment">// Prints: <Buffer ab 90 78 56 34 12></span></code><code class="language-js cjs"><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>); 4380 4381<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">6</span>); 4382 4383buf.<span class="hljs-title function_">writeIntLE</span>(<span class="hljs-number">0x1234567890ab</span>, <span class="hljs-number">0</span>, <span class="hljs-number">6</span>); 4384 4385<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4386<span class="hljs-comment">// Prints: <Buffer ab 90 78 56 34 12></span></code><button class="copy-button">copy</button></pre> 4387<h4><code>buf.writeUInt8(value[, offset])</code><span><a class="mark" href="#bufwriteuint8value-offset" id="bufwriteuint8value-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeuint8_value_offset"></a></h4> 4388<div class="api_metadata"> 4389<details class="changelog"><summary>History</summary> 4390<table> 4391<tbody><tr><th>Version</th><th>Changes</th></tr> 4392<tr><td>v14.9.0, v12.19.0</td> 4393<td><p>This function is also available as <code>buf.writeUint8()</code>.</p></td></tr> 4394<tr><td>v10.0.0</td> 4395<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 4396<tr><td>v0.5.0</td> 4397<td><p><span>Added in: v0.5.0</span></p></td></tr> 4398</tbody></table> 4399</details> 4400</div> 4401<ul> 4402<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number to be written to <code>buf</code>.</li> 4403<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to write. Must 4404satisfy <code>0 <= offset <= buf.length - 1</code>. <strong>Default:</strong> <code>0</code>.</li> 4405<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <code>offset</code> plus the number of bytes written.</li> 4406</ul> 4407<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code>. <code>value</code> must be a 4408valid unsigned 8-bit integer. Behavior is undefined when <code>value</code> is anything 4409other than an unsigned 8-bit integer.</p> 4410<p>This function is also available under the <code>writeUint8</code> alias.</p> 4411 4412<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 4413 4414<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>); 4415 4416buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x3</span>, <span class="hljs-number">0</span>); 4417buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x4</span>, <span class="hljs-number">1</span>); 4418buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x23</span>, <span class="hljs-number">2</span>); 4419buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x42</span>, <span class="hljs-number">3</span>); 4420 4421<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4422<span class="hljs-comment">// Prints: <Buffer 03 04 23 42></span></code><code class="language-js cjs"><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>); 4423 4424<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>); 4425 4426buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x3</span>, <span class="hljs-number">0</span>); 4427buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x4</span>, <span class="hljs-number">1</span>); 4428buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x23</span>, <span class="hljs-number">2</span>); 4429buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x42</span>, <span class="hljs-number">3</span>); 4430 4431<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4432<span class="hljs-comment">// Prints: <Buffer 03 04 23 42></span></code><button class="copy-button">copy</button></pre> 4433<h4><code>buf.writeUInt16BE(value[, offset])</code><span><a class="mark" href="#bufwriteuint16bevalue-offset" id="bufwriteuint16bevalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeuint16be_value_offset"></a></h4> 4434<div class="api_metadata"> 4435<details class="changelog"><summary>History</summary> 4436<table> 4437<tbody><tr><th>Version</th><th>Changes</th></tr> 4438<tr><td>v14.9.0, v12.19.0</td> 4439<td><p>This function is also available as <code>buf.writeUint16BE()</code>.</p></td></tr> 4440<tr><td>v10.0.0</td> 4441<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 4442<tr><td>v0.5.5</td> 4443<td><p><span>Added in: v0.5.5</span></p></td></tr> 4444</tbody></table> 4445</details> 4446</div> 4447<ul> 4448<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number to be written to <code>buf</code>.</li> 4449<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to write. Must 4450satisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> 4451<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <code>offset</code> plus the number of bytes written.</li> 4452</ul> 4453<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code> 4454must be a valid unsigned 16-bit integer. Behavior is undefined when <code>value</code> 4455is anything other than an unsigned 16-bit integer.</p> 4456<p>This function is also available under the <code>writeUint16BE</code> alias.</p> 4457 4458<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 4459 4460<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>); 4461 4462buf.<span class="hljs-title function_">writeUInt16BE</span>(<span class="hljs-number">0xdead</span>, <span class="hljs-number">0</span>); 4463buf.<span class="hljs-title function_">writeUInt16BE</span>(<span class="hljs-number">0xbeef</span>, <span class="hljs-number">2</span>); 4464 4465<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4466<span class="hljs-comment">// Prints: <Buffer de ad be ef></span></code><code class="language-js cjs"><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>); 4467 4468<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>); 4469 4470buf.<span class="hljs-title function_">writeUInt16BE</span>(<span class="hljs-number">0xdead</span>, <span class="hljs-number">0</span>); 4471buf.<span class="hljs-title function_">writeUInt16BE</span>(<span class="hljs-number">0xbeef</span>, <span class="hljs-number">2</span>); 4472 4473<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4474<span class="hljs-comment">// Prints: <Buffer de ad be ef></span></code><button class="copy-button">copy</button></pre> 4475<h4><code>buf.writeUInt16LE(value[, offset])</code><span><a class="mark" href="#bufwriteuint16levalue-offset" id="bufwriteuint16levalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeuint16le_value_offset"></a></h4> 4476<div class="api_metadata"> 4477<details class="changelog"><summary>History</summary> 4478<table> 4479<tbody><tr><th>Version</th><th>Changes</th></tr> 4480<tr><td>v14.9.0, v12.19.0</td> 4481<td><p>This function is also available as <code>buf.writeUint16LE()</code>.</p></td></tr> 4482<tr><td>v10.0.0</td> 4483<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 4484<tr><td>v0.5.5</td> 4485<td><p><span>Added in: v0.5.5</span></p></td></tr> 4486</tbody></table> 4487</details> 4488</div> 4489<ul> 4490<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number to be written to <code>buf</code>.</li> 4491<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to write. Must 4492satisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> 4493<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <code>offset</code> plus the number of bytes written.</li> 4494</ul> 4495<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code> 4496must be a valid unsigned 16-bit integer. Behavior is undefined when <code>value</code> is 4497anything other than an unsigned 16-bit integer.</p> 4498<p>This function is also available under the <code>writeUint16LE</code> alias.</p> 4499 4500<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 4501 4502<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>); 4503 4504buf.<span class="hljs-title function_">writeUInt16LE</span>(<span class="hljs-number">0xdead</span>, <span class="hljs-number">0</span>); 4505buf.<span class="hljs-title function_">writeUInt16LE</span>(<span class="hljs-number">0xbeef</span>, <span class="hljs-number">2</span>); 4506 4507<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4508<span class="hljs-comment">// Prints: <Buffer ad de ef be></span></code><code class="language-js cjs"><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>); 4509 4510<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>); 4511 4512buf.<span class="hljs-title function_">writeUInt16LE</span>(<span class="hljs-number">0xdead</span>, <span class="hljs-number">0</span>); 4513buf.<span class="hljs-title function_">writeUInt16LE</span>(<span class="hljs-number">0xbeef</span>, <span class="hljs-number">2</span>); 4514 4515<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4516<span class="hljs-comment">// Prints: <Buffer ad de ef be></span></code><button class="copy-button">copy</button></pre> 4517<h4><code>buf.writeUInt32BE(value[, offset])</code><span><a class="mark" href="#bufwriteuint32bevalue-offset" id="bufwriteuint32bevalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeuint32be_value_offset"></a></h4> 4518<div class="api_metadata"> 4519<details class="changelog"><summary>History</summary> 4520<table> 4521<tbody><tr><th>Version</th><th>Changes</th></tr> 4522<tr><td>v14.9.0, v12.19.0</td> 4523<td><p>This function is also available as <code>buf.writeUint32BE()</code>.</p></td></tr> 4524<tr><td>v10.0.0</td> 4525<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 4526<tr><td>v0.5.5</td> 4527<td><p><span>Added in: v0.5.5</span></p></td></tr> 4528</tbody></table> 4529</details> 4530</div> 4531<ul> 4532<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number to be written to <code>buf</code>.</li> 4533<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to write. Must 4534satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 4535<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <code>offset</code> plus the number of bytes written.</li> 4536</ul> 4537<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code> 4538must be a valid unsigned 32-bit integer. Behavior is undefined when <code>value</code> 4539is anything other than an unsigned 32-bit integer.</p> 4540<p>This function is also available under the <code>writeUint32BE</code> alias.</p> 4541 4542<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 4543 4544<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>); 4545 4546buf.<span class="hljs-title function_">writeUInt32BE</span>(<span class="hljs-number">0xfeedface</span>, <span class="hljs-number">0</span>); 4547 4548<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4549<span class="hljs-comment">// Prints: <Buffer fe ed fa ce></span></code><code class="language-js cjs"><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>); 4550 4551<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>); 4552 4553buf.<span class="hljs-title function_">writeUInt32BE</span>(<span class="hljs-number">0xfeedface</span>, <span class="hljs-number">0</span>); 4554 4555<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4556<span class="hljs-comment">// Prints: <Buffer fe ed fa ce></span></code><button class="copy-button">copy</button></pre> 4557<h4><code>buf.writeUInt32LE(value[, offset])</code><span><a class="mark" href="#bufwriteuint32levalue-offset" id="bufwriteuint32levalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeuint32le_value_offset"></a></h4> 4558<div class="api_metadata"> 4559<details class="changelog"><summary>History</summary> 4560<table> 4561<tbody><tr><th>Version</th><th>Changes</th></tr> 4562<tr><td>v14.9.0, v12.19.0</td> 4563<td><p>This function is also available as <code>buf.writeUint32LE()</code>.</p></td></tr> 4564<tr><td>v10.0.0</td> 4565<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 4566<tr><td>v0.5.5</td> 4567<td><p><span>Added in: v0.5.5</span></p></td></tr> 4568</tbody></table> 4569</details> 4570</div> 4571<ul> 4572<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number to be written to <code>buf</code>.</li> 4573<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to write. Must 4574satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 4575<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <code>offset</code> plus the number of bytes written.</li> 4576</ul> 4577<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code> 4578must be a valid unsigned 32-bit integer. Behavior is undefined when <code>value</code> is 4579anything other than an unsigned 32-bit integer.</p> 4580<p>This function is also available under the <code>writeUint32LE</code> alias.</p> 4581 4582<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 4583 4584<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>); 4585 4586buf.<span class="hljs-title function_">writeUInt32LE</span>(<span class="hljs-number">0xfeedface</span>, <span class="hljs-number">0</span>); 4587 4588<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4589<span class="hljs-comment">// Prints: <Buffer ce fa ed fe></span></code><code class="language-js cjs"><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>); 4590 4591<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>); 4592 4593buf.<span class="hljs-title function_">writeUInt32LE</span>(<span class="hljs-number">0xfeedface</span>, <span class="hljs-number">0</span>); 4594 4595<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4596<span class="hljs-comment">// Prints: <Buffer ce fa ed fe></span></code><button class="copy-button">copy</button></pre> 4597<h4><code>buf.writeUIntBE(value, offset, byteLength)</code><span><a class="mark" href="#bufwriteuintbevalue-offset-bytelength" id="bufwriteuintbevalue-offset-bytelength">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeuintbe_value_offset_bytelength"></a></h4> 4598<div class="api_metadata"> 4599<details class="changelog"><summary>History</summary> 4600<table> 4601<tbody><tr><th>Version</th><th>Changes</th></tr> 4602<tr><td>v14.9.0, v12.19.0</td> 4603<td><p>This function is also available as <code>buf.writeUintBE()</code>.</p></td></tr> 4604<tr><td>v10.0.0</td> 4605<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset and <code>byteLength</code> to <code>uint32</code> anymore.</p></td></tr> 4606<tr><td>v0.5.5</td> 4607<td><p><span>Added in: v0.5.5</span></p></td></tr> 4608</tbody></table> 4609</details> 4610</div> 4611<ul> 4612<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number to be written to <code>buf</code>.</li> 4613<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to write. Must 4614satisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> 4615<li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to write. Must satisfy 4616<code>0 < byteLength <= 6</code>.</li> 4617<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <code>offset</code> plus the number of bytes written.</li> 4618</ul> 4619<p>Writes <code>byteLength</code> bytes of <code>value</code> to <code>buf</code> at the specified <code>offset</code> 4620as big-endian. Supports up to 48 bits of accuracy. Behavior is undefined 4621when <code>value</code> is anything other than an unsigned integer.</p> 4622<p>This function is also available under the <code>writeUintBE</code> alias.</p> 4623 4624<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 4625 4626<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">6</span>); 4627 4628buf.<span class="hljs-title function_">writeUIntBE</span>(<span class="hljs-number">0x1234567890ab</span>, <span class="hljs-number">0</span>, <span class="hljs-number">6</span>); 4629 4630<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4631<span class="hljs-comment">// Prints: <Buffer 12 34 56 78 90 ab></span></code><code class="language-js cjs"><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>); 4632 4633<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">6</span>); 4634 4635buf.<span class="hljs-title function_">writeUIntBE</span>(<span class="hljs-number">0x1234567890ab</span>, <span class="hljs-number">0</span>, <span class="hljs-number">6</span>); 4636 4637<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4638<span class="hljs-comment">// Prints: <Buffer 12 34 56 78 90 ab></span></code><button class="copy-button">copy</button></pre> 4639<h4><code>buf.writeUIntLE(value, offset, byteLength)</code><span><a class="mark" href="#bufwriteuintlevalue-offset-bytelength" id="bufwriteuintlevalue-offset-bytelength">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeuintle_value_offset_bytelength"></a></h4> 4640<div class="api_metadata"> 4641<details class="changelog"><summary>History</summary> 4642<table> 4643<tbody><tr><th>Version</th><th>Changes</th></tr> 4644<tr><td>v14.9.0, v12.19.0</td> 4645<td><p>This function is also available as <code>buf.writeUintLE()</code>.</p></td></tr> 4646<tr><td>v10.0.0</td> 4647<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset and <code>byteLength</code> to <code>uint32</code> anymore.</p></td></tr> 4648<tr><td>v0.5.5</td> 4649<td><p><span>Added in: v0.5.5</span></p></td></tr> 4650</tbody></table> 4651</details> 4652</div> 4653<ul> 4654<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number to be written to <code>buf</code>.</li> 4655<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to skip before starting to write. Must 4656satisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> 4657<li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to write. Must satisfy 4658<code>0 < byteLength <= 6</code>.</li> 4659<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <code>offset</code> plus the number of bytes written.</li> 4660</ul> 4661<p>Writes <code>byteLength</code> bytes of <code>value</code> to <code>buf</code> at the specified <code>offset</code> 4662as little-endian. Supports up to 48 bits of accuracy. Behavior is undefined 4663when <code>value</code> is anything other than an unsigned integer.</p> 4664<p>This function is also available under the <code>writeUintLE</code> alias.</p> 4665 4666<pre class="with-42-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_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 4667 4668<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">6</span>); 4669 4670buf.<span class="hljs-title function_">writeUIntLE</span>(<span class="hljs-number">0x1234567890ab</span>, <span class="hljs-number">0</span>, <span class="hljs-number">6</span>); 4671 4672<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4673<span class="hljs-comment">// Prints: <Buffer ab 90 78 56 34 12></span></code><code class="language-js cjs"><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>); 4674 4675<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">6</span>); 4676 4677buf.<span class="hljs-title function_">writeUIntLE</span>(<span class="hljs-number">0x1234567890ab</span>, <span class="hljs-number">0</span>, <span class="hljs-number">6</span>); 4678 4679<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 4680<span class="hljs-comment">// Prints: <Buffer ab 90 78 56 34 12></span></code><button class="copy-button">copy</button></pre> 4681<h4><code>new Buffer(array)</code><span><a class="mark" href="#new-bufferarray" id="new-bufferarray">#</a></span><a aria-hidden="true" class="legacy" id="buffer_new_buffer_array"></a></h4> 4682<div class="api_metadata"> 4683<details class="changelog"><summary>History</summary> 4684<table> 4685<tbody><tr><th>Version</th><th>Changes</th></tr> 4686<tr><td>v10.0.0</td> 4687<td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr> 4688<tr><td>v7.2.1</td> 4689<td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr> 4690<tr><td>v7.0.0</td> 4691<td><p>Calling this constructor emits a deprecation warning now.</p></td></tr> 4692<tr><td>v6.0.0</td> 4693<td><p><span>Deprecated since: v6.0.0</span></p></td></tr> 4694</tbody></table> 4695</details> 4696</div> 4697<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#static-method-bufferfromarray"><code>Buffer.from(array)</code></a> instead.</div><p></p> 4698<ul> 4699<li><code>array</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer[]></a> An array of bytes to copy from.</li> 4700</ul> 4701<p>See <a href="#static-method-bufferfromarray"><code>Buffer.from(array)</code></a>.</p> 4702<h4><code>new Buffer(arrayBuffer[, byteOffset[, length]])</code><span><a class="mark" href="#new-bufferarraybuffer-byteoffset-length" id="new-bufferarraybuffer-byteoffset-length">#</a></span><a aria-hidden="true" class="legacy" id="buffer_new_buffer_arraybuffer_byteoffset_length"></a></h4> 4703<div class="api_metadata"> 4704<details class="changelog"><summary>History</summary> 4705<table> 4706<tbody><tr><th>Version</th><th>Changes</th></tr> 4707<tr><td>v10.0.0</td> 4708<td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr> 4709<tr><td>v7.2.1</td> 4710<td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr> 4711<tr><td>v7.0.0</td> 4712<td><p>Calling this constructor emits a deprecation warning now.</p></td></tr> 4713<tr><td>v6.0.0</td> 4714<td><p>The <code>byteOffset</code> and <code>length</code> parameters are supported now.</p></td></tr> 4715<tr><td>v6.0.0</td> 4716<td><p><span>Deprecated since: v6.0.0</span></p></td></tr> 4717<tr><td>v3.0.0</td> 4718<td><p><span>Added in: v3.0.0</span></p></td></tr> 4719</tbody></table> 4720</details> 4721</div> 4722<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use 4723<a href="#static-method-bufferfromarraybuffer-byteoffset-length"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a> 4724instead.</div><p></p> 4725<ul> 4726<li><code>arrayBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer" class="type"><SharedArrayBuffer></a> An <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a>, 4727<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"><code>SharedArrayBuffer</code></a> or the <code>.buffer</code> property of a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>.</li> 4728<li><code>byteOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Index of first byte to expose. <strong>Default:</strong> <code>0</code>.</li> 4729<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Number of bytes to expose. 4730<strong>Default:</strong> <code>arrayBuffer.byteLength - byteOffset</code>.</li> 4731</ul> 4732<p>See 4733<a href="#static-method-bufferfromarraybuffer-byteoffset-length"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a>.</p> 4734<h4><code>new Buffer(buffer)</code><span><a class="mark" href="#new-bufferbuffer" id="new-bufferbuffer">#</a></span><a aria-hidden="true" class="legacy" id="buffer_new_buffer_buffer"></a></h4> 4735<div class="api_metadata"> 4736<details class="changelog"><summary>History</summary> 4737<table> 4738<tbody><tr><th>Version</th><th>Changes</th></tr> 4739<tr><td>v10.0.0</td> 4740<td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr> 4741<tr><td>v7.2.1</td> 4742<td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr> 4743<tr><td>v7.0.0</td> 4744<td><p>Calling this constructor emits a deprecation warning now.</p></td></tr> 4745<tr><td>v6.0.0</td> 4746<td><p><span>Deprecated since: v6.0.0</span></p></td></tr> 4747</tbody></table> 4748</details> 4749</div> 4750<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#static-method-bufferfrombuffer"><code>Buffer.from(buffer)</code></a> instead.</div><p></p> 4751<ul> 4752<li><code>buffer</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/Uint8Array" class="type"><Uint8Array></a> An existing <code>Buffer</code> or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a> from 4753which to copy data.</li> 4754</ul> 4755<p>See <a href="#static-method-bufferfrombuffer"><code>Buffer.from(buffer)</code></a>.</p> 4756<h4><code>new Buffer(size)</code><span><a class="mark" href="#new-buffersize" id="new-buffersize">#</a></span><a aria-hidden="true" class="legacy" id="buffer_new_buffer_size"></a></h4> 4757<div class="api_metadata"> 4758<details class="changelog"><summary>History</summary> 4759<table> 4760<tbody><tr><th>Version</th><th>Changes</th></tr> 4761<tr><td>v10.0.0</td> 4762<td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr> 4763<tr><td>v8.0.0</td> 4764<td><p>The <code>new Buffer(size)</code> will return zero-filled memory by default.</p></td></tr> 4765<tr><td>v7.2.1</td> 4766<td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr> 4767<tr><td>v7.0.0</td> 4768<td><p>Calling this constructor emits a deprecation warning now.</p></td></tr> 4769<tr><td>v6.0.0</td> 4770<td><p><span>Deprecated since: v6.0.0</span></p></td></tr> 4771</tbody></table> 4772</details> 4773</div> 4774<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#static-method-bufferallocsize-fill-encoding"><code>Buffer.alloc()</code></a> instead (also see 4775<a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a>).</div><p></p> 4776<ul> 4777<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The desired length of the new <code>Buffer</code>.</li> 4778</ul> 4779<p>See <a href="#static-method-bufferallocsize-fill-encoding"><code>Buffer.alloc()</code></a> and <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a>. This variant of the 4780constructor is equivalent to <a href="#static-method-bufferallocsize-fill-encoding"><code>Buffer.alloc()</code></a>.</p> 4781<h4><code>new Buffer(string[, encoding])</code><span><a class="mark" href="#new-bufferstring-encoding" id="new-bufferstring-encoding">#</a></span><a aria-hidden="true" class="legacy" id="buffer_new_buffer_string_encoding"></a></h4> 4782<div class="api_metadata"> 4783<details class="changelog"><summary>History</summary> 4784<table> 4785<tbody><tr><th>Version</th><th>Changes</th></tr> 4786<tr><td>v10.0.0</td> 4787<td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr> 4788<tr><td>v7.2.1</td> 4789<td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr> 4790<tr><td>v7.0.0</td> 4791<td><p>Calling this constructor emits a deprecation warning now.</p></td></tr> 4792<tr><td>v6.0.0</td> 4793<td><p><span>Deprecated since: v6.0.0</span></p></td></tr> 4794</tbody></table> 4795</details> 4796</div> 4797<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: 4798Use <a href="#static-method-bufferfromstring-encoding"><code>Buffer.from(string[, encoding])</code></a> instead.</div><p></p> 4799<ul> 4800<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> String to encode.</li> 4801<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The encoding of <code>string</code>. <strong>Default:</strong> <code>'utf8'</code>.</li> 4802</ul> 4803<p>See <a href="#static-method-bufferfromstring-encoding"><code>Buffer.from(string[, encoding])</code></a>.</p> 4804</section><section><h3>Class: <code>File</code><span><a class="mark" href="#class-file" id="class-file">#</a></span><a aria-hidden="true" class="legacy" id="buffer_class_file"></a></h3> 4805<div class="api_metadata"> 4806<span>Added in: v18.13.0</span> 4807</div> 4808<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 4809<ul> 4810<li>Extends: <a href="buffer.html#class-blob" class="type"><Blob></a></li> 4811</ul> 4812<p>A <a href="https://developer.mozilla.org/en-US/docs/Web/API/File"><code>File</code></a> provides information about files.</p> 4813<h4><code>new buffer.File(sources, fileName[, options])</code><span><a class="mark" href="#new-bufferfilesources-filename-options" id="new-bufferfilesources-filename-options">#</a></span><a aria-hidden="true" class="legacy" id="buffer_new_buffer_file_sources_filename_options"></a></h4> 4814<div class="api_metadata"> 4815<span>Added in: v18.13.0</span> 4816</div> 4817<ul> 4818<li><code>sources</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer[]></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> | <a href="buffer.html#class-blob" class="type"><Blob[]></a> | <a href="buffer.html#class-file" class="type"><File[]></a> 4819An array of string, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></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>, <a href="buffer.html#class-file" class="type"><File></a>, or <a href="buffer.html#class-blob" class="type"><Blob></a> 4820objects, or any mix of such objects, that will be stored within the <code>File</code>.</li> 4821<li><code>fileName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The name of the file.</li> 4822<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 4823<ul> 4824<li><code>endings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> One of either <code>'transparent'</code> or <code>'native'</code>. When set 4825to <code>'native'</code>, line endings in string source parts will be converted to 4826the platform native line-ending as specified by <code>require('node:os').EOL</code>.</li> 4827<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The File content-type.</li> 4828<li><code>lastModified</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The last modified date of the file. 4829<strong>Default:</strong> <code>Date.now()</code>.</li> 4830</ul> 4831</li> 4832</ul> 4833<h4><code>file.name</code><span><a class="mark" href="#filename" id="filename">#</a></span><a aria-hidden="true" class="legacy" id="buffer_file_name"></a></h4> 4834<div class="api_metadata"> 4835<span>Added in: v18.13.0</span> 4836</div> 4837<ul> 4838<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 4839</ul> 4840<p>The name of the <code>File</code>.</p> 4841<h4><code>file.lastModified</code><span><a class="mark" href="#filelastmodified" id="filelastmodified">#</a></span><a aria-hidden="true" class="legacy" id="buffer_file_lastmodified"></a></h4> 4842<div class="api_metadata"> 4843<span>Added in: v18.13.0</span> 4844</div> 4845<ul> 4846<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 4847</ul> 4848<p>The last modified date of the <code>File</code>.</p> 4849</section><section><h3><code>node:buffer</code> module APIs<span><a class="mark" href="#nodebuffer-module-apis" id="nodebuffer-module-apis">#</a></span><a aria-hidden="true" class="legacy" id="buffer_node_buffer_module_apis"></a></h3> 4850<p>While, the <code>Buffer</code> object is available as a global, there are additional 4851<code>Buffer</code>-related APIs that are available only via the <code>node:buffer</code> module 4852accessed using <code>require('node:buffer')</code>.</p> 4853<h4><code>buffer.atob(data)</code><span><a class="mark" href="#bufferatobdata" id="bufferatobdata">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer_atob_data"></a></h4> 4854<div class="api_metadata"> 4855<span>Added in: v15.13.0, v14.17.0</span> 4856</div> 4857<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy. Use <code>Buffer.from(data, 'base64')</code> instead.</div><p></p> 4858<ul> 4859<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> The Base64-encoded input string.</li> 4860</ul> 4861<p>Decodes a string of Base64-encoded data into bytes, and encodes those bytes 4862into a string using Latin-1 (ISO-8859-1).</p> 4863<p>The <code>data</code> may be any JavaScript-value that can be coerced into a string.</p> 4864<p><strong>This function is only provided for compatibility with legacy web platform APIs 4865and should never be used in new code, because they use strings to represent 4866binary data and predate the introduction of typed arrays in JavaScript. 4867For code running using Node.js APIs, converting between base64-encoded strings 4868and binary data should be performed using <code>Buffer.from(str, 'base64')</code> and 4869<code>buf.toString('base64')</code>.</strong></p> 4870<h4><code>buffer.btoa(data)</code><span><a class="mark" href="#bufferbtoadata" id="bufferbtoadata">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer_btoa_data"></a></h4> 4871<div class="api_metadata"> 4872<span>Added in: v15.13.0, v14.17.0</span> 4873</div> 4874<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy. Use <code>buf.toString('base64')</code> instead.</div><p></p> 4875<ul> 4876<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> An ASCII (Latin1) string.</li> 4877</ul> 4878<p>Decodes a string into bytes using Latin-1 (ISO-8859), and encodes those bytes 4879into a string using Base64.</p> 4880<p>The <code>data</code> may be any JavaScript-value that can be coerced into a string.</p> 4881<p><strong>This function is only provided for compatibility with legacy web platform APIs 4882and should never be used in new code, because they use strings to represent 4883binary data and predate the introduction of typed arrays in JavaScript. 4884For code running using Node.js APIs, converting between base64-encoded strings 4885and binary data should be performed using <code>Buffer.from(str, 'base64')</code> and 4886<code>buf.toString('base64')</code>.</strong></p> 4887<h4><code>buffer.isAscii(input)</code><span><a class="mark" href="#bufferisasciiinput" id="bufferisasciiinput">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer_isascii_input"></a></h4> 4888<div class="api_metadata"> 4889<span>Added in: v18.15.0</span> 4890</div> 4891<ul> 4892<li>input <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> The input to validate.</li> 4893<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 4894</ul> 4895<p>This function returns <code>true</code> if <code>input</code> contains only valid ASCII-encoded data, 4896including the case in which <code>input</code> is empty.</p> 4897<p>Throws if the <code>input</code> is a detached array buffer.</p> 4898<h4><code>buffer.isUtf8(input)</code><span><a class="mark" href="#bufferisutf8input" id="bufferisutf8input">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer_isutf8_input"></a></h4> 4899<div class="api_metadata"> 4900<span>Added in: v18.14.0</span> 4901</div> 4902<ul> 4903<li>input <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> The input to validate.</li> 4904<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 4905</ul> 4906<p>This function returns <code>true</code> if <code>input</code> contains only valid UTF-8-encoded data, 4907including the case in which <code>input</code> is empty.</p> 4908<p>Throws if the <code>input</code> is a detached array buffer.</p> 4909<h4><code>buffer.INSPECT_MAX_BYTES</code><span><a class="mark" href="#bufferinspect_max_bytes" id="bufferinspect_max_bytes">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer_inspect_max_bytes"></a></h4> 4910<div class="api_metadata"> 4911<span>Added in: v0.5.4</span> 4912</div> 4913<ul> 4914<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>50</code></li> 4915</ul> 4916<p>Returns the maximum number of bytes that will be returned when 4917<code>buf.inspect()</code> is called. This can be overridden by user modules. See 4918<a href="util.html#utilinspectobject-options"><code>util.inspect()</code></a> for more details on <code>buf.inspect()</code> behavior.</p> 4919<h4><code>buffer.kMaxLength</code><span><a class="mark" href="#bufferkmaxlength" id="bufferkmaxlength">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer_kmaxlength"></a></h4> 4920<div class="api_metadata"> 4921<span>Added in: v3.0.0</span> 4922</div> 4923<ul> 4924<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The largest size allowed for a single <code>Buffer</code> instance.</li> 4925</ul> 4926<p>An alias for <a href="#bufferconstantsmax_length"><code>buffer.constants.MAX_LENGTH</code></a>.</p> 4927<h4><code>buffer.kStringMaxLength</code><span><a class="mark" href="#bufferkstringmaxlength" id="bufferkstringmaxlength">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer_kstringmaxlength"></a></h4> 4928<div class="api_metadata"> 4929<span>Added in: v3.0.0</span> 4930</div> 4931<ul> 4932<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The largest length allowed for a single <code>string</code> instance.</li> 4933</ul> 4934<p>An alias for <a href="#bufferconstantsmax_string_length"><code>buffer.constants.MAX_STRING_LENGTH</code></a>.</p> 4935<h4><code>buffer.resolveObjectURL(id)</code><span><a class="mark" href="#bufferresolveobjecturlid" id="bufferresolveobjecturlid">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer_resolveobjecturl_id"></a></h4> 4936<div class="api_metadata"> 4937<span>Added in: v16.7.0</span> 4938</div> 4939<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 4940<ul> 4941<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A <code>'blob:nodedata:...</code> URL string returned by a prior call to 4942<code>URL.createObjectURL()</code>.</li> 4943<li>Returns: <a href="buffer.html#class-blob" class="type"><Blob></a></li> 4944</ul> 4945<p>Resolves a <code>'blob:nodedata:...'</code> an associated <a href="buffer.html#class-blob" class="type"><Blob></a> object registered using 4946a prior call to <code>URL.createObjectURL()</code>.</p> 4947<h4><code>buffer.transcode(source, fromEnc, toEnc)</code><span><a class="mark" href="#buffertranscodesource-fromenc-toenc" id="buffertranscodesource-fromenc-toenc">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer_transcode_source_fromenc_toenc"></a></h4> 4948<div class="api_metadata"> 4949<details class="changelog"><summary>History</summary> 4950<table> 4951<tbody><tr><th>Version</th><th>Changes</th></tr> 4952<tr><td>v8.0.0</td> 4953<td><p>The <code>source</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 4954<tr><td>v7.1.0</td> 4955<td><p><span>Added in: v7.1.0</span></p></td></tr> 4956</tbody></table> 4957</details> 4958</div> 4959<ul> 4960<li><code>source</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/Uint8Array" class="type"><Uint8Array></a> A <code>Buffer</code> or <code>Uint8Array</code> instance.</li> 4961<li><code>fromEnc</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The current encoding.</li> 4962<li><code>toEnc</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> To target encoding.</li> 4963<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 4964</ul> 4965<p>Re-encodes the given <code>Buffer</code> or <code>Uint8Array</code> instance from one character 4966encoding to another. Returns a new <code>Buffer</code> instance.</p> 4967<p>Throws if the <code>fromEnc</code> or <code>toEnc</code> specify invalid character encodings or if 4968conversion from <code>fromEnc</code> to <code>toEnc</code> is not permitted.</p> 4969<p>Encodings supported by <code>buffer.transcode()</code> are: <code>'ascii'</code>, <code>'utf8'</code>, 4970<code>'utf16le'</code>, <code>'ucs2'</code>, <code>'latin1'</code>, and <code>'binary'</code>.</p> 4971<p>The transcoding process will use substitution characters if a given byte 4972sequence cannot be adequately represented in the target encoding. For instance:</p> 4973 4974<pre class="with-53-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_">Buffer</span>, transcode } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 4975 4976<span class="hljs-keyword">const</span> newBuf = <span class="hljs-title function_">transcode</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'€'</span>), <span class="hljs-string">'utf8'</span>, <span class="hljs-string">'ascii'</span>); 4977<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(newBuf.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'ascii'</span>)); 4978<span class="hljs-comment">// Prints: '?'</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span>, transcode } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>); 4979 4980<span class="hljs-keyword">const</span> newBuf = <span class="hljs-title function_">transcode</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'€'</span>), <span class="hljs-string">'utf8'</span>, <span class="hljs-string">'ascii'</span>); 4981<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(newBuf.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'ascii'</span>)); 4982<span class="hljs-comment">// Prints: '?'</span></code><button class="copy-button">copy</button></pre> 4983<p>Because the Euro (<code>€</code>) sign is not representable in US-ASCII, it is replaced 4984with <code>?</code> in the transcoded <code>Buffer</code>.</p> 4985<h4>Class: <code>SlowBuffer</code><span><a class="mark" href="#class-slowbuffer" id="class-slowbuffer">#</a></span><a aria-hidden="true" class="legacy" id="buffer_class_slowbuffer"></a></h4> 4986<div class="api_metadata"> 4987<span>Deprecated since: v6.0.0</span> 4988</div> 4989<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#static-method-bufferallocunsafeslowsize"><code>Buffer.allocUnsafeSlow()</code></a> instead.</div><p></p> 4990<p>See <a href="#static-method-bufferallocunsafeslowsize"><code>Buffer.allocUnsafeSlow()</code></a>. This was never a class in the sense that 4991the constructor always returned a <code>Buffer</code> instance, rather than a <code>SlowBuffer</code> 4992instance.</p> 4993<h5><code>new SlowBuffer(size)</code><span><a class="mark" href="#new-slowbuffersize" id="new-slowbuffersize">#</a></span><a aria-hidden="true" class="legacy" id="buffer_new_slowbuffer_size"></a></h5> 4994<div class="api_metadata"> 4995<span>Deprecated since: v6.0.0</span> 4996</div> 4997<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#static-method-bufferallocunsafeslowsize"><code>Buffer.allocUnsafeSlow()</code></a> instead.</div><p></p> 4998<ul> 4999<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The desired length of the new <code>SlowBuffer</code>.</li> 5000</ul> 5001<p>See <a href="#static-method-bufferallocunsafeslowsize"><code>Buffer.allocUnsafeSlow()</code></a>.</p> 5002<h4>Buffer constants<span><a class="mark" href="#buffer-constants" id="buffer-constants">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer_constants"></a></h4> 5003<div class="api_metadata"> 5004<span>Added in: v8.2.0</span> 5005</div> 5006<h5><code>buffer.constants.MAX_LENGTH</code><span><a class="mark" href="#bufferconstantsmax_length" id="bufferconstantsmax_length">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer_constants_max_length"></a></h5> 5007<div class="api_metadata"> 5008<details class="changelog"><summary>History</summary> 5009<table> 5010<tbody><tr><th>Version</th><th>Changes</th></tr> 5011<tr><td>v15.0.0</td> 5012<td><p>Value is changed to 2<sup>32</sup> on 64-bit architectures.</p></td></tr> 5013<tr><td>v14.0.0</td> 5014<td><p>Value is changed from 2<sup>31</sup> - 1 to 2<sup>32</sup> - 1 on 64-bit architectures.</p></td></tr> 5015<tr><td>v8.2.0</td> 5016<td><p><span>Added in: v8.2.0</span></p></td></tr> 5017</tbody></table> 5018</details> 5019</div> 5020<ul> 5021<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The largest size allowed for a single <code>Buffer</code> instance.</li> 5022</ul> 5023<p>On 32-bit architectures, this value currently is 2<sup>30</sup> - 1 (about 1 5024GiB).</p> 5025<p>On 64-bit architectures, this value currently is 2<sup>32</sup> (about 4 GiB).</p> 5026<p>It reflects <a href="https://v8.github.io/api/head/classv8_1_1TypedArray.html#a54a48f4373da0850663c4393d843b9b0"><code>v8::TypedArray::kMaxLength</code></a> under the hood.</p> 5027<p>This value is also available as <a href="#bufferkmaxlength"><code>buffer.kMaxLength</code></a>.</p> 5028<h5><code>buffer.constants.MAX_STRING_LENGTH</code><span><a class="mark" href="#bufferconstantsmax_string_length" id="bufferconstantsmax_string_length">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer_constants_max_string_length"></a></h5> 5029<div class="api_metadata"> 5030<span>Added in: v8.2.0</span> 5031</div> 5032<ul> 5033<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The largest length allowed for a single <code>string</code> instance.</li> 5034</ul> 5035<p>Represents the largest <code>length</code> that a <code>string</code> primitive can have, counted 5036in UTF-16 code units.</p> 5037<p>This value may depend on the JS engine that is being used.</p> 5038</section><section><h3><code>Buffer.from()</code>, <code>Buffer.alloc()</code>, and <code>Buffer.allocUnsafe()</code><span><a class="mark" href="#bufferfrom-bufferalloc-and-bufferallocunsafe" id="bufferfrom-bufferalloc-and-bufferallocunsafe">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer_from_buffer_alloc_and_buffer_allocunsafe"></a></h3> 5039<p>In versions of Node.js prior to 6.0.0, <code>Buffer</code> instances were created using the 5040<code>Buffer</code> constructor function, which allocates the returned <code>Buffer</code> 5041differently based on what arguments are provided:</p> 5042<ul> 5043<li>Passing a number as the first argument to <code>Buffer()</code> (e.g. <code>new Buffer(10)</code>) 5044allocates a new <code>Buffer</code> object of the specified size. Prior to Node.js 8.0.0, 5045the memory allocated for such <code>Buffer</code> instances is <em>not</em> initialized and 5046<em>can contain sensitive data</em>. Such <code>Buffer</code> instances <em>must</em> be subsequently 5047initialized by using either <a href="#buffillvalue-offset-end-encoding"><code>buf.fill(0)</code></a> or by writing to the 5048entire <code>Buffer</code> before reading data from the <code>Buffer</code>. 5049While this behavior is <em>intentional</em> to improve performance, 5050development experience has demonstrated that a more explicit distinction is 5051required between creating a fast-but-uninitialized <code>Buffer</code> versus creating a 5052slower-but-safer <code>Buffer</code>. Since Node.js 8.0.0, <code>Buffer(num)</code> and <code>new Buffer(num)</code> return a <code>Buffer</code> with initialized memory.</li> 5053<li>Passing a string, array, or <code>Buffer</code> as the first argument copies the 5054passed object's data into the <code>Buffer</code>.</li> 5055<li>Passing an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> or a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"><code>SharedArrayBuffer</code></a> returns a <code>Buffer</code> 5056that shares allocated memory with the given array buffer.</li> 5057</ul> 5058<p>Because the behavior of <code>new Buffer()</code> is different depending on the type of the 5059first argument, security and reliability issues can be inadvertently introduced 5060into applications when argument validation or <code>Buffer</code> initialization is not 5061performed.</p> 5062<p>For example, if an attacker can cause an application to receive a number where 5063a string is expected, the application may call <code>new Buffer(100)</code> 5064instead of <code>new Buffer("100")</code>, leading it to allocate a 100 byte buffer instead 5065of allocating a 3 byte buffer with content <code>"100"</code>. This is commonly possible 5066using JSON API calls. Since JSON distinguishes between numeric and string types, 5067it allows injection of numbers where a naively written application that does not 5068validate its input sufficiently might expect to always receive a string. 5069Before Node.js 8.0.0, the 100 byte buffer might contain 5070arbitrary pre-existing in-memory data, so may be used to expose in-memory 5071secrets to a remote attacker. Since Node.js 8.0.0, exposure of memory cannot 5072occur because the data is zero-filled. However, other attacks are still 5073possible, such as causing very large buffers to be allocated by the server, 5074leading to performance degradation or crashing on memory exhaustion.</p> 5075<p>To make the creation of <code>Buffer</code> instances more reliable and less error-prone, 5076the various forms of the <code>new Buffer()</code> constructor have been <strong>deprecated</strong> 5077and replaced by separate <code>Buffer.from()</code>, <a href="#static-method-bufferallocsize-fill-encoding"><code>Buffer.alloc()</code></a>, and 5078<a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> methods.</p> 5079<p><em>Developers should migrate all existing uses of the <code>new Buffer()</code> constructors 5080to one of these new APIs.</em></p> 5081<ul> 5082<li><a href="#static-method-bufferfromarray"><code>Buffer.from(array)</code></a> returns a new <code>Buffer</code> that <em>contains a copy</em> of the 5083provided octets.</li> 5084<li><a href="#static-method-bufferfromarraybuffer-byteoffset-length"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a> 5085returns a new <code>Buffer</code> that <em>shares the same allocated memory</em> as the given 5086<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a>.</li> 5087<li><a href="#static-method-bufferfrombuffer"><code>Buffer.from(buffer)</code></a> returns a new <code>Buffer</code> that <em>contains a copy</em> of the 5088contents of the given <code>Buffer</code>.</li> 5089<li><a href="#static-method-bufferfromstring-encoding"><code>Buffer.from(string[, encoding])</code></a> returns a new 5090<code>Buffer</code> that <em>contains a copy</em> of the provided string.</li> 5091<li><a href="#static-method-bufferallocsize-fill-encoding"><code>Buffer.alloc(size[, fill[, encoding]])</code></a> returns a new 5092initialized <code>Buffer</code> of the specified size. This method is slower than 5093<a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe(size)</code></a> but guarantees that newly 5094created <code>Buffer</code> instances never contain old data that is potentially 5095sensitive. A <code>TypeError</code> will be thrown if <code>size</code> is not a number.</li> 5096<li><a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe(size)</code></a> and 5097<a href="#static-method-bufferallocunsafeslowsize"><code>Buffer.allocUnsafeSlow(size)</code></a> each return a 5098new uninitialized <code>Buffer</code> of the specified <code>size</code>. Because the <code>Buffer</code> is 5099uninitialized, the allocated segment of memory might contain old data that is 5100potentially sensitive.</li> 5101</ul> 5102<p><code>Buffer</code> instances returned by <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> and 5103<a href="#static-method-bufferfromarray"><code>Buffer.from(array)</code></a> <em>may</em> be allocated off a shared internal memory pool 5104if <code>size</code> is less than or equal to half <a href="#class-property-bufferpoolsize"><code>Buffer.poolSize</code></a>. Instances 5105returned by <a href="#static-method-bufferallocunsafeslowsize"><code>Buffer.allocUnsafeSlow()</code></a> <em>never</em> use the shared internal 5106memory pool.</p> 5107<h4>The <code>--zero-fill-buffers</code> command-line option<span><a class="mark" href="#the---zero-fill-buffers-command-line-option" id="the---zero-fill-buffers-command-line-option">#</a></span><a aria-hidden="true" class="legacy" id="buffer_the_zero_fill_buffers_command_line_option"></a></h4> 5108<div class="api_metadata"> 5109<span>Added in: v5.10.0</span> 5110</div> 5111<p>Node.js can be started using the <code>--zero-fill-buffers</code> command-line option to 5112cause all newly-allocated <code>Buffer</code> instances to be zero-filled upon creation by 5113default. Without the option, buffers created with <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a>, 5114<a href="#static-method-bufferallocunsafeslowsize"><code>Buffer.allocUnsafeSlow()</code></a>, and <code>new SlowBuffer(size)</code> are not zero-filled. 5115Use of this flag can have a measurable negative impact on performance. Use the 5116<code>--zero-fill-buffers</code> option only when necessary to enforce that newly allocated 5117<code>Buffer</code> instances cannot contain old data that is potentially sensitive.</p> 5118<pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">node --zero-fill-buffers</span> 5119<span class="hljs-meta prompt_">> </span><span class="language-bash">Buffer.allocUnsafe(5);</span> 5120<Buffer 00 00 00 00 00></code> <button class="copy-button">copy</button></pre> 5121<h4>What makes <code>Buffer.allocUnsafe()</code> and <code>Buffer.allocUnsafeSlow()</code> "unsafe"?<span><a class="mark" href="#what-makes-bufferallocunsafe-and-bufferallocunsafeslow-unsafe" id="what-makes-bufferallocunsafe-and-bufferallocunsafeslow-unsafe">#</a></span><a aria-hidden="true" class="legacy" id="buffer_what_makes_buffer_allocunsafe_and_buffer_allocunsafeslow_unsafe"></a></h4> 5122<p>When calling <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> and <a href="#static-method-bufferallocunsafeslowsize"><code>Buffer.allocUnsafeSlow()</code></a>, the 5123segment of allocated memory is <em>uninitialized</em> (it is not zeroed-out). While 5124this design makes the allocation of memory quite fast, the allocated segment of 5125memory might contain old data that is potentially sensitive. Using a <code>Buffer</code> 5126created by <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> without <em>completely</em> overwriting the 5127memory can allow this old data to be leaked when the <code>Buffer</code> memory is read.</p> 5128<p>While there are clear performance advantages to using 5129<a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a>, extra care <em>must</em> be taken in order to avoid 5130introducing security vulnerabilities into an application.</p></section> 5131 <!-- API END --> 5132 </div> 5133 </div> 5134 </div> 5135</body> 5136</html> 5137