11cb0ef41Sopenharmony_ci<!DOCTYPE html> 21cb0ef41Sopenharmony_ci<html lang="en"> 31cb0ef41Sopenharmony_ci<head> 41cb0ef41Sopenharmony_ci <meta charset="utf-8"> 51cb0ef41Sopenharmony_ci <meta name="viewport" content="width=device-width"> 61cb0ef41Sopenharmony_ci <meta name="nodejs.org:node-version" content="v18.20.1"> 71cb0ef41Sopenharmony_ci <title>Buffer | Node.js v18.20.1 Documentation</title> 81cb0ef41Sopenharmony_ci <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 91cb0ef41Sopenharmony_ci <link rel="stylesheet" href="assets/style.css"> 101cb0ef41Sopenharmony_ci <link rel="stylesheet" href="assets/hljs.css"> 111cb0ef41Sopenharmony_ci <link rel="canonical" href="https://nodejs.org/api/buffer.html"> 121cb0ef41Sopenharmony_ci <script async defer src="assets/api.js" type="text/javascript"></script> 131cb0ef41Sopenharmony_ci <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> 141cb0ef41Sopenharmony_ci</head> 151cb0ef41Sopenharmony_ci<body class="alt apidoc" id="api-section-buffer"> 161cb0ef41Sopenharmony_ci <div id="content" class="clearfix"> 171cb0ef41Sopenharmony_ci <div id="column2" class="interior"> 181cb0ef41Sopenharmony_ci <div id="intro" class="interior"> 191cb0ef41Sopenharmony_ci <a href="/" title="Go back to the home page"> 201cb0ef41Sopenharmony_ci Node.js 211cb0ef41Sopenharmony_ci </a> 221cb0ef41Sopenharmony_ci </div> 231cb0ef41Sopenharmony_ci <ul> 241cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 251cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 261cb0ef41Sopenharmony_ci</ul> 271cb0ef41Sopenharmony_ci<hr class="line"> 281cb0ef41Sopenharmony_ci<ul> 291cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 301cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 311cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 321cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer active">Buffer</a></li> 331cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 341cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 351cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 361cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 371cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 381cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 391cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li> 401cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 411cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 421cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 431cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 441cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 451cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li> 461cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li> 471cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li> 481cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li> 491cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li> 501cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li> 511cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li> 521cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 531cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li> 541cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 551cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 561cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 571cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 581cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 591cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 601cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li> 611cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li> 621cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li> 631cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 641cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 651cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li> 661cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 671cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 681cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li> 691cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li> 701cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li> 711cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 721cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li> 731cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 741cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li> 751cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li> 761cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 771cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 781cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li> 791cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 801cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li> 811cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li> 821cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li> 831cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li> 841cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 851cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 861cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 871cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 881cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 891cb0ef41Sopenharmony_ci</ul> 901cb0ef41Sopenharmony_ci<hr class="line"> 911cb0ef41Sopenharmony_ci<ul> 921cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 931cb0ef41Sopenharmony_ci</ul> 941cb0ef41Sopenharmony_ci </div> 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci <div id="column1" data-id="buffer" class="interior"> 971cb0ef41Sopenharmony_ci <header class="header"> 981cb0ef41Sopenharmony_ci <div class="header-container"> 991cb0ef41Sopenharmony_ci <h1>Node.js v18.20.1 documentation</h1> 1001cb0ef41Sopenharmony_ci <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 1011cb0ef41Sopenharmony_ci <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 1021cb0ef41Sopenharmony_ci <path fill="none" d="M0 0h24v24H0z" /> 1031cb0ef41Sopenharmony_ci <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"/> 1041cb0ef41Sopenharmony_ci <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"/> 1051cb0ef41Sopenharmony_ci </svg> 1061cb0ef41Sopenharmony_ci <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 1071cb0ef41Sopenharmony_ci <path d="M0 0h24v24H0z" fill="none" /> 1081cb0ef41Sopenharmony_ci <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"/> 1091cb0ef41Sopenharmony_ci </svg> 1101cb0ef41Sopenharmony_ci </button> 1111cb0ef41Sopenharmony_ci </div> 1121cb0ef41Sopenharmony_ci <div id="gtoc"> 1131cb0ef41Sopenharmony_ci <ul> 1141cb0ef41Sopenharmony_ci <li class="pinned-header">Node.js v18.20.1</li> 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci <li class="picker-header"> 1171cb0ef41Sopenharmony_ci <a href="#"> 1181cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 1191cb0ef41Sopenharmony_ci Table of contents 1201cb0ef41Sopenharmony_ci </a> 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_ci <div class="picker"><div class="toc"><ul> 1231cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#buffer">Buffer</a></span> 1241cb0ef41Sopenharmony_ci<ul> 1251cb0ef41Sopenharmony_ci<li><a href="#buffers-and-character-encodings">Buffers and character encodings</a></li> 1261cb0ef41Sopenharmony_ci<li><a href="#buffers-and-typedarrays">Buffers and TypedArrays</a></li> 1271cb0ef41Sopenharmony_ci<li><a href="#buffers-and-iteration">Buffers and iteration</a></li> 1281cb0ef41Sopenharmony_ci<li><a href="#class-blob">Class: <code>Blob</code></a> 1291cb0ef41Sopenharmony_ci<ul> 1301cb0ef41Sopenharmony_ci<li><a href="#new-bufferblobsources-options"><code>new buffer.Blob([sources[, options]])</code></a></li> 1311cb0ef41Sopenharmony_ci<li><a href="#blobarraybuffer"><code>blob.arrayBuffer()</code></a></li> 1321cb0ef41Sopenharmony_ci<li><a href="#blobsize"><code>blob.size</code></a></li> 1331cb0ef41Sopenharmony_ci<li><a href="#blobslicestart-end-type"><code>blob.slice([start[, end[, type]]])</code></a></li> 1341cb0ef41Sopenharmony_ci<li><a href="#blobstream"><code>blob.stream()</code></a></li> 1351cb0ef41Sopenharmony_ci<li><a href="#blobtext"><code>blob.text()</code></a></li> 1361cb0ef41Sopenharmony_ci<li><a href="#blobtype"><code>blob.type</code></a></li> 1371cb0ef41Sopenharmony_ci<li><a href="#blob-objects-and-messagechannel"><code>Blob</code> objects and <code>MessageChannel</code></a></li> 1381cb0ef41Sopenharmony_ci</ul> 1391cb0ef41Sopenharmony_ci</li> 1401cb0ef41Sopenharmony_ci<li><a href="#class-buffer">Class: <code>Buffer</code></a> 1411cb0ef41Sopenharmony_ci<ul> 1421cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferallocsize-fill-encoding">Static method: <code>Buffer.alloc(size[, fill[, encoding]])</code></a></li> 1431cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferallocunsafesize">Static method: <code>Buffer.allocUnsafe(size)</code></a></li> 1441cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferallocunsafeslowsize">Static method: <code>Buffer.allocUnsafeSlow(size)</code></a></li> 1451cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferbytelengthstring-encoding">Static method: <code>Buffer.byteLength(string[, encoding])</code></a></li> 1461cb0ef41Sopenharmony_ci<li><a href="#static-method-buffercomparebuf1-buf2">Static method: <code>Buffer.compare(buf1, buf2)</code></a></li> 1471cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferconcatlist-totallength">Static method: <code>Buffer.concat(list[, totalLength])</code></a></li> 1481cb0ef41Sopenharmony_ci<li><a href="#static-method-buffercopybytesfromview-offset-length">Static method: <code>Buffer.copyBytesFrom(view[, offset[, length]])</code></a></li> 1491cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferfromarray">Static method: <code>Buffer.from(array)</code></a></li> 1501cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferfromarraybuffer-byteoffset-length">Static method: <code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a></li> 1511cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferfrombuffer">Static method: <code>Buffer.from(buffer)</code></a></li> 1521cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferfromobject-offsetorencoding-length">Static method: <code>Buffer.from(object[, offsetOrEncoding[, length]])</code></a></li> 1531cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferfromstring-encoding">Static method: <code>Buffer.from(string[, encoding])</code></a></li> 1541cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferisbufferobj">Static method: <code>Buffer.isBuffer(obj)</code></a></li> 1551cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferisencodingencoding">Static method: <code>Buffer.isEncoding(encoding)</code></a></li> 1561cb0ef41Sopenharmony_ci<li><a href="#class-property-bufferpoolsize">Class property: <code>Buffer.poolSize</code></a></li> 1571cb0ef41Sopenharmony_ci<li><a href="#bufindex"><code>buf[index]</code></a></li> 1581cb0ef41Sopenharmony_ci<li><a href="#bufbuffer"><code>buf.buffer</code></a></li> 1591cb0ef41Sopenharmony_ci<li><a href="#bufbyteoffset"><code>buf.byteOffset</code></a></li> 1601cb0ef41Sopenharmony_ci<li><a href="#bufcomparetarget-targetstart-targetend-sourcestart-sourceend"><code>buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])</code></a></li> 1611cb0ef41Sopenharmony_ci<li><a href="#bufcopytarget-targetstart-sourcestart-sourceend"><code>buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])</code></a></li> 1621cb0ef41Sopenharmony_ci<li><a href="#bufentries"><code>buf.entries()</code></a></li> 1631cb0ef41Sopenharmony_ci<li><a href="#bufequalsotherbuffer"><code>buf.equals(otherBuffer)</code></a></li> 1641cb0ef41Sopenharmony_ci<li><a href="#buffillvalue-offset-end-encoding"><code>buf.fill(value[, offset[, end]][, encoding])</code></a></li> 1651cb0ef41Sopenharmony_ci<li><a href="#bufincludesvalue-byteoffset-encoding"><code>buf.includes(value[, byteOffset][, encoding])</code></a></li> 1661cb0ef41Sopenharmony_ci<li><a href="#bufindexofvalue-byteoffset-encoding"><code>buf.indexOf(value[, byteOffset][, encoding])</code></a></li> 1671cb0ef41Sopenharmony_ci<li><a href="#bufkeys"><code>buf.keys()</code></a></li> 1681cb0ef41Sopenharmony_ci<li><a href="#buflastindexofvalue-byteoffset-encoding"><code>buf.lastIndexOf(value[, byteOffset][, encoding])</code></a></li> 1691cb0ef41Sopenharmony_ci<li><a href="#buflength"><code>buf.length</code></a></li> 1701cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#bufparent"><code>buf.parent</code></a></span></li> 1711cb0ef41Sopenharmony_ci<li><a href="#bufreadbigint64beoffset"><code>buf.readBigInt64BE([offset])</code></a></li> 1721cb0ef41Sopenharmony_ci<li><a href="#bufreadbigint64leoffset"><code>buf.readBigInt64LE([offset])</code></a></li> 1731cb0ef41Sopenharmony_ci<li><a href="#bufreadbiguint64beoffset"><code>buf.readBigUInt64BE([offset])</code></a></li> 1741cb0ef41Sopenharmony_ci<li><a href="#bufreadbiguint64leoffset"><code>buf.readBigUInt64LE([offset])</code></a></li> 1751cb0ef41Sopenharmony_ci<li><a href="#bufreaddoublebeoffset"><code>buf.readDoubleBE([offset])</code></a></li> 1761cb0ef41Sopenharmony_ci<li><a href="#bufreaddoubleleoffset"><code>buf.readDoubleLE([offset])</code></a></li> 1771cb0ef41Sopenharmony_ci<li><a href="#bufreadfloatbeoffset"><code>buf.readFloatBE([offset])</code></a></li> 1781cb0ef41Sopenharmony_ci<li><a href="#bufreadfloatleoffset"><code>buf.readFloatLE([offset])</code></a></li> 1791cb0ef41Sopenharmony_ci<li><a href="#bufreadint8offset"><code>buf.readInt8([offset])</code></a></li> 1801cb0ef41Sopenharmony_ci<li><a href="#bufreadint16beoffset"><code>buf.readInt16BE([offset])</code></a></li> 1811cb0ef41Sopenharmony_ci<li><a href="#bufreadint16leoffset"><code>buf.readInt16LE([offset])</code></a></li> 1821cb0ef41Sopenharmony_ci<li><a href="#bufreadint32beoffset"><code>buf.readInt32BE([offset])</code></a></li> 1831cb0ef41Sopenharmony_ci<li><a href="#bufreadint32leoffset"><code>buf.readInt32LE([offset])</code></a></li> 1841cb0ef41Sopenharmony_ci<li><a href="#bufreadintbeoffset-bytelength"><code>buf.readIntBE(offset, byteLength)</code></a></li> 1851cb0ef41Sopenharmony_ci<li><a href="#bufreadintleoffset-bytelength"><code>buf.readIntLE(offset, byteLength)</code></a></li> 1861cb0ef41Sopenharmony_ci<li><a href="#bufreaduint8offset"><code>buf.readUInt8([offset])</code></a></li> 1871cb0ef41Sopenharmony_ci<li><a href="#bufreaduint16beoffset"><code>buf.readUInt16BE([offset])</code></a></li> 1881cb0ef41Sopenharmony_ci<li><a href="#bufreaduint16leoffset"><code>buf.readUInt16LE([offset])</code></a></li> 1891cb0ef41Sopenharmony_ci<li><a href="#bufreaduint32beoffset"><code>buf.readUInt32BE([offset])</code></a></li> 1901cb0ef41Sopenharmony_ci<li><a href="#bufreaduint32leoffset"><code>buf.readUInt32LE([offset])</code></a></li> 1911cb0ef41Sopenharmony_ci<li><a href="#bufreaduintbeoffset-bytelength"><code>buf.readUIntBE(offset, byteLength)</code></a></li> 1921cb0ef41Sopenharmony_ci<li><a href="#bufreaduintleoffset-bytelength"><code>buf.readUIntLE(offset, byteLength)</code></a></li> 1931cb0ef41Sopenharmony_ci<li><a href="#bufsubarraystart-end"><code>buf.subarray([start[, end]])</code></a></li> 1941cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#bufslicestart-end"><code>buf.slice([start[, end]])</code></a></span></li> 1951cb0ef41Sopenharmony_ci<li><a href="#bufswap16"><code>buf.swap16()</code></a></li> 1961cb0ef41Sopenharmony_ci<li><a href="#bufswap32"><code>buf.swap32()</code></a></li> 1971cb0ef41Sopenharmony_ci<li><a href="#bufswap64"><code>buf.swap64()</code></a></li> 1981cb0ef41Sopenharmony_ci<li><a href="#buftojson"><code>buf.toJSON()</code></a></li> 1991cb0ef41Sopenharmony_ci<li><a href="#buftostringencoding-start-end"><code>buf.toString([encoding[, start[, end]]])</code></a></li> 2001cb0ef41Sopenharmony_ci<li><a href="#bufvalues"><code>buf.values()</code></a></li> 2011cb0ef41Sopenharmony_ci<li><a href="#bufwritestring-offset-length-encoding"><code>buf.write(string[, offset[, length]][, encoding])</code></a></li> 2021cb0ef41Sopenharmony_ci<li><a href="#bufwritebigint64bevalue-offset"><code>buf.writeBigInt64BE(value[, offset])</code></a></li> 2031cb0ef41Sopenharmony_ci<li><a href="#bufwritebigint64levalue-offset"><code>buf.writeBigInt64LE(value[, offset])</code></a></li> 2041cb0ef41Sopenharmony_ci<li><a href="#bufwritebiguint64bevalue-offset"><code>buf.writeBigUInt64BE(value[, offset])</code></a></li> 2051cb0ef41Sopenharmony_ci<li><a href="#bufwritebiguint64levalue-offset"><code>buf.writeBigUInt64LE(value[, offset])</code></a></li> 2061cb0ef41Sopenharmony_ci<li><a href="#bufwritedoublebevalue-offset"><code>buf.writeDoubleBE(value[, offset])</code></a></li> 2071cb0ef41Sopenharmony_ci<li><a href="#bufwritedoublelevalue-offset"><code>buf.writeDoubleLE(value[, offset])</code></a></li> 2081cb0ef41Sopenharmony_ci<li><a href="#bufwritefloatbevalue-offset"><code>buf.writeFloatBE(value[, offset])</code></a></li> 2091cb0ef41Sopenharmony_ci<li><a href="#bufwritefloatlevalue-offset"><code>buf.writeFloatLE(value[, offset])</code></a></li> 2101cb0ef41Sopenharmony_ci<li><a href="#bufwriteint8value-offset"><code>buf.writeInt8(value[, offset])</code></a></li> 2111cb0ef41Sopenharmony_ci<li><a href="#bufwriteint16bevalue-offset"><code>buf.writeInt16BE(value[, offset])</code></a></li> 2121cb0ef41Sopenharmony_ci<li><a href="#bufwriteint16levalue-offset"><code>buf.writeInt16LE(value[, offset])</code></a></li> 2131cb0ef41Sopenharmony_ci<li><a href="#bufwriteint32bevalue-offset"><code>buf.writeInt32BE(value[, offset])</code></a></li> 2141cb0ef41Sopenharmony_ci<li><a href="#bufwriteint32levalue-offset"><code>buf.writeInt32LE(value[, offset])</code></a></li> 2151cb0ef41Sopenharmony_ci<li><a href="#bufwriteintbevalue-offset-bytelength"><code>buf.writeIntBE(value, offset, byteLength)</code></a></li> 2161cb0ef41Sopenharmony_ci<li><a href="#bufwriteintlevalue-offset-bytelength"><code>buf.writeIntLE(value, offset, byteLength)</code></a></li> 2171cb0ef41Sopenharmony_ci<li><a href="#bufwriteuint8value-offset"><code>buf.writeUInt8(value[, offset])</code></a></li> 2181cb0ef41Sopenharmony_ci<li><a href="#bufwriteuint16bevalue-offset"><code>buf.writeUInt16BE(value[, offset])</code></a></li> 2191cb0ef41Sopenharmony_ci<li><a href="#bufwriteuint16levalue-offset"><code>buf.writeUInt16LE(value[, offset])</code></a></li> 2201cb0ef41Sopenharmony_ci<li><a href="#bufwriteuint32bevalue-offset"><code>buf.writeUInt32BE(value[, offset])</code></a></li> 2211cb0ef41Sopenharmony_ci<li><a href="#bufwriteuint32levalue-offset"><code>buf.writeUInt32LE(value[, offset])</code></a></li> 2221cb0ef41Sopenharmony_ci<li><a href="#bufwriteuintbevalue-offset-bytelength"><code>buf.writeUIntBE(value, offset, byteLength)</code></a></li> 2231cb0ef41Sopenharmony_ci<li><a href="#bufwriteuintlevalue-offset-bytelength"><code>buf.writeUIntLE(value, offset, byteLength)</code></a></li> 2241cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#new-bufferarray"><code>new Buffer(array)</code></a></span></li> 2251cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#new-bufferarraybuffer-byteoffset-length"><code>new Buffer(arrayBuffer[, byteOffset[, length]])</code></a></span></li> 2261cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#new-bufferbuffer"><code>new Buffer(buffer)</code></a></span></li> 2271cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#new-buffersize"><code>new Buffer(size)</code></a></span></li> 2281cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#new-bufferstring-encoding"><code>new Buffer(string[, encoding])</code></a></span></li> 2291cb0ef41Sopenharmony_ci</ul> 2301cb0ef41Sopenharmony_ci</li> 2311cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#class-file">Class: <code>File</code></a></span> 2321cb0ef41Sopenharmony_ci<ul> 2331cb0ef41Sopenharmony_ci<li><a href="#new-bufferfilesources-filename-options"><code>new buffer.File(sources, fileName[, options])</code></a></li> 2341cb0ef41Sopenharmony_ci<li><a href="#filename"><code>file.name</code></a></li> 2351cb0ef41Sopenharmony_ci<li><a href="#filelastmodified"><code>file.lastModified</code></a></li> 2361cb0ef41Sopenharmony_ci</ul> 2371cb0ef41Sopenharmony_ci</li> 2381cb0ef41Sopenharmony_ci<li><a href="#nodebuffer-module-apis"><code>node:buffer</code> module APIs</a> 2391cb0ef41Sopenharmony_ci<ul> 2401cb0ef41Sopenharmony_ci<li><span class="stability_3"><a href="#bufferatobdata"><code>buffer.atob(data)</code></a></span></li> 2411cb0ef41Sopenharmony_ci<li><span class="stability_3"><a href="#bufferbtoadata"><code>buffer.btoa(data)</code></a></span></li> 2421cb0ef41Sopenharmony_ci<li><a href="#bufferisasciiinput"><code>buffer.isAscii(input)</code></a></li> 2431cb0ef41Sopenharmony_ci<li><a href="#bufferisutf8input"><code>buffer.isUtf8(input)</code></a></li> 2441cb0ef41Sopenharmony_ci<li><a href="#bufferinspect_max_bytes"><code>buffer.INSPECT_MAX_BYTES</code></a></li> 2451cb0ef41Sopenharmony_ci<li><a href="#bufferkmaxlength"><code>buffer.kMaxLength</code></a></li> 2461cb0ef41Sopenharmony_ci<li><a href="#bufferkstringmaxlength"><code>buffer.kStringMaxLength</code></a></li> 2471cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#bufferresolveobjecturlid"><code>buffer.resolveObjectURL(id)</code></a></span></li> 2481cb0ef41Sopenharmony_ci<li><a href="#buffertranscodesource-fromenc-toenc"><code>buffer.transcode(source, fromEnc, toEnc)</code></a></li> 2491cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#class-slowbuffer">Class: <code>SlowBuffer</code></a></span> 2501cb0ef41Sopenharmony_ci<ul> 2511cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#new-slowbuffersize"><code>new SlowBuffer(size)</code></a></span></li> 2521cb0ef41Sopenharmony_ci</ul> 2531cb0ef41Sopenharmony_ci</li> 2541cb0ef41Sopenharmony_ci<li><a href="#buffer-constants">Buffer constants</a> 2551cb0ef41Sopenharmony_ci<ul> 2561cb0ef41Sopenharmony_ci<li><a href="#bufferconstantsmax_length"><code>buffer.constants.MAX_LENGTH</code></a></li> 2571cb0ef41Sopenharmony_ci<li><a href="#bufferconstantsmax_string_length"><code>buffer.constants.MAX_STRING_LENGTH</code></a></li> 2581cb0ef41Sopenharmony_ci</ul> 2591cb0ef41Sopenharmony_ci</li> 2601cb0ef41Sopenharmony_ci</ul> 2611cb0ef41Sopenharmony_ci</li> 2621cb0ef41Sopenharmony_ci<li><a href="#bufferfrom-bufferalloc-and-bufferallocunsafe"><code>Buffer.from()</code>, <code>Buffer.alloc()</code>, and <code>Buffer.allocUnsafe()</code></a> 2631cb0ef41Sopenharmony_ci<ul> 2641cb0ef41Sopenharmony_ci<li><a href="#the---zero-fill-buffers-command-line-option">The <code>--zero-fill-buffers</code> command-line option</a></li> 2651cb0ef41Sopenharmony_ci<li><a href="#what-makes-bufferallocunsafe-and-bufferallocunsafeslow-unsafe">What makes <code>Buffer.allocUnsafe()</code> and <code>Buffer.allocUnsafeSlow()</code> "unsafe"?</a></li> 2661cb0ef41Sopenharmony_ci</ul> 2671cb0ef41Sopenharmony_ci</li> 2681cb0ef41Sopenharmony_ci</ul> 2691cb0ef41Sopenharmony_ci</li> 2701cb0ef41Sopenharmony_ci</ul></div></div> 2711cb0ef41Sopenharmony_ci </li> 2721cb0ef41Sopenharmony_ci 2731cb0ef41Sopenharmony_ci 2741cb0ef41Sopenharmony_ci <li class="picker-header"> 2751cb0ef41Sopenharmony_ci <a href="#"> 2761cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 2771cb0ef41Sopenharmony_ci Index 2781cb0ef41Sopenharmony_ci </a> 2791cb0ef41Sopenharmony_ci 2801cb0ef41Sopenharmony_ci <div class="picker"><ul> 2811cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 2821cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 2831cb0ef41Sopenharmony_ci 2841cb0ef41Sopenharmony_ci <li> 2851cb0ef41Sopenharmony_ci <a href="index.html">Index</a> 2861cb0ef41Sopenharmony_ci </li> 2871cb0ef41Sopenharmony_ci </ul> 2881cb0ef41Sopenharmony_ci 2891cb0ef41Sopenharmony_ci<hr class="line"> 2901cb0ef41Sopenharmony_ci<ul> 2911cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 2921cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 2931cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 2941cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer active">Buffer</a></li> 2951cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 2961cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 2971cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 2981cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 2991cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 3001cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 3011cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li> 3021cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 3031cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 3041cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 3051cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 3061cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 3071cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li> 3081cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li> 3091cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li> 3101cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li> 3111cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li> 3121cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li> 3131cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li> 3141cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 3151cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li> 3161cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 3171cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 3181cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 3191cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 3201cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 3211cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 3221cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li> 3231cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li> 3241cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li> 3251cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 3261cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 3271cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li> 3281cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 3291cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 3301cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li> 3311cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li> 3321cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li> 3331cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 3341cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li> 3351cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 3361cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li> 3371cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li> 3381cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 3391cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 3401cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li> 3411cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 3421cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li> 3431cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li> 3441cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li> 3451cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li> 3461cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 3471cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 3481cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 3491cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 3501cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 3511cb0ef41Sopenharmony_ci</ul> 3521cb0ef41Sopenharmony_ci<hr class="line"> 3531cb0ef41Sopenharmony_ci<ul> 3541cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 3551cb0ef41Sopenharmony_ci</ul></div> 3561cb0ef41Sopenharmony_ci </li> 3571cb0ef41Sopenharmony_ci 3581cb0ef41Sopenharmony_ci 3591cb0ef41Sopenharmony_ci <li class="picker-header"> 3601cb0ef41Sopenharmony_ci <a href="#"> 3611cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 3621cb0ef41Sopenharmony_ci Other versions 3631cb0ef41Sopenharmony_ci </a> 3641cb0ef41Sopenharmony_ci <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/buffer.html">21.x</a></li> 3651cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v20.x/api/buffer.html">20.x <b>LTS</b></a></li> 3661cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v19.x/api/buffer.html">19.x</a></li> 3671cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v18.x/api/buffer.html">18.x <b>LTS</b></a></li> 3681cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v17.x/api/buffer.html">17.x</a></li> 3691cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v16.x/api/buffer.html">16.x</a></li> 3701cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v15.x/api/buffer.html">15.x</a></li> 3711cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v14.x/api/buffer.html">14.x</a></li> 3721cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v13.x/api/buffer.html">13.x</a></li> 3731cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v12.x/api/buffer.html">12.x</a></li> 3741cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v11.x/api/buffer.html">11.x</a></li> 3751cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v10.x/api/buffer.html">10.x</a></li> 3761cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v9.x/api/buffer.html">9.x</a></li> 3771cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v8.x/api/buffer.html">8.x</a></li> 3781cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v7.x/api/buffer.html">7.x</a></li> 3791cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v6.x/api/buffer.html">6.x</a></li> 3801cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v5.x/api/buffer.html">5.x</a></li> 3811cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v4.x/api/buffer.html">4.x</a></li> 3821cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/buffer.html">0.12.x</a></li> 3831cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/buffer.html">0.10.x</a></li></ol></div> 3841cb0ef41Sopenharmony_ci </li> 3851cb0ef41Sopenharmony_ci 3861cb0ef41Sopenharmony_ci <li class="picker-header"> 3871cb0ef41Sopenharmony_ci <a href="#"> 3881cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 3891cb0ef41Sopenharmony_ci Options 3901cb0ef41Sopenharmony_ci </a> 3911cb0ef41Sopenharmony_ci 3921cb0ef41Sopenharmony_ci <div class="picker"> 3931cb0ef41Sopenharmony_ci <ul> 3941cb0ef41Sopenharmony_ci <li> 3951cb0ef41Sopenharmony_ci <a href="all.html">View on single page</a> 3961cb0ef41Sopenharmony_ci </li> 3971cb0ef41Sopenharmony_ci <li> 3981cb0ef41Sopenharmony_ci <a href="buffer.json">View as JSON</a> 3991cb0ef41Sopenharmony_ci </li> 4001cb0ef41Sopenharmony_ci <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/buffer.md">Edit on GitHub</a></li> 4011cb0ef41Sopenharmony_ci </ul> 4021cb0ef41Sopenharmony_ci </div> 4031cb0ef41Sopenharmony_ci </li> 4041cb0ef41Sopenharmony_ci </ul> 4051cb0ef41Sopenharmony_ci </div> 4061cb0ef41Sopenharmony_ci <hr> 4071cb0ef41Sopenharmony_ci </header> 4081cb0ef41Sopenharmony_ci 4091cb0ef41Sopenharmony_ci <details id="toc" open><summary>Table of contents</summary><ul> 4101cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#buffer">Buffer</a></span> 4111cb0ef41Sopenharmony_ci<ul> 4121cb0ef41Sopenharmony_ci<li><a href="#buffers-and-character-encodings">Buffers and character encodings</a></li> 4131cb0ef41Sopenharmony_ci<li><a href="#buffers-and-typedarrays">Buffers and TypedArrays</a></li> 4141cb0ef41Sopenharmony_ci<li><a href="#buffers-and-iteration">Buffers and iteration</a></li> 4151cb0ef41Sopenharmony_ci<li><a href="#class-blob">Class: <code>Blob</code></a> 4161cb0ef41Sopenharmony_ci<ul> 4171cb0ef41Sopenharmony_ci<li><a href="#new-bufferblobsources-options"><code>new buffer.Blob([sources[, options]])</code></a></li> 4181cb0ef41Sopenharmony_ci<li><a href="#blobarraybuffer"><code>blob.arrayBuffer()</code></a></li> 4191cb0ef41Sopenharmony_ci<li><a href="#blobsize"><code>blob.size</code></a></li> 4201cb0ef41Sopenharmony_ci<li><a href="#blobslicestart-end-type"><code>blob.slice([start[, end[, type]]])</code></a></li> 4211cb0ef41Sopenharmony_ci<li><a href="#blobstream"><code>blob.stream()</code></a></li> 4221cb0ef41Sopenharmony_ci<li><a href="#blobtext"><code>blob.text()</code></a></li> 4231cb0ef41Sopenharmony_ci<li><a href="#blobtype"><code>blob.type</code></a></li> 4241cb0ef41Sopenharmony_ci<li><a href="#blob-objects-and-messagechannel"><code>Blob</code> objects and <code>MessageChannel</code></a></li> 4251cb0ef41Sopenharmony_ci</ul> 4261cb0ef41Sopenharmony_ci</li> 4271cb0ef41Sopenharmony_ci<li><a href="#class-buffer">Class: <code>Buffer</code></a> 4281cb0ef41Sopenharmony_ci<ul> 4291cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferallocsize-fill-encoding">Static method: <code>Buffer.alloc(size[, fill[, encoding]])</code></a></li> 4301cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferallocunsafesize">Static method: <code>Buffer.allocUnsafe(size)</code></a></li> 4311cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferallocunsafeslowsize">Static method: <code>Buffer.allocUnsafeSlow(size)</code></a></li> 4321cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferbytelengthstring-encoding">Static method: <code>Buffer.byteLength(string[, encoding])</code></a></li> 4331cb0ef41Sopenharmony_ci<li><a href="#static-method-buffercomparebuf1-buf2">Static method: <code>Buffer.compare(buf1, buf2)</code></a></li> 4341cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferconcatlist-totallength">Static method: <code>Buffer.concat(list[, totalLength])</code></a></li> 4351cb0ef41Sopenharmony_ci<li><a href="#static-method-buffercopybytesfromview-offset-length">Static method: <code>Buffer.copyBytesFrom(view[, offset[, length]])</code></a></li> 4361cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferfromarray">Static method: <code>Buffer.from(array)</code></a></li> 4371cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferfromarraybuffer-byteoffset-length">Static method: <code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a></li> 4381cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferfrombuffer">Static method: <code>Buffer.from(buffer)</code></a></li> 4391cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferfromobject-offsetorencoding-length">Static method: <code>Buffer.from(object[, offsetOrEncoding[, length]])</code></a></li> 4401cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferfromstring-encoding">Static method: <code>Buffer.from(string[, encoding])</code></a></li> 4411cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferisbufferobj">Static method: <code>Buffer.isBuffer(obj)</code></a></li> 4421cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferisencodingencoding">Static method: <code>Buffer.isEncoding(encoding)</code></a></li> 4431cb0ef41Sopenharmony_ci<li><a href="#class-property-bufferpoolsize">Class property: <code>Buffer.poolSize</code></a></li> 4441cb0ef41Sopenharmony_ci<li><a href="#bufindex"><code>buf[index]</code></a></li> 4451cb0ef41Sopenharmony_ci<li><a href="#bufbuffer"><code>buf.buffer</code></a></li> 4461cb0ef41Sopenharmony_ci<li><a href="#bufbyteoffset"><code>buf.byteOffset</code></a></li> 4471cb0ef41Sopenharmony_ci<li><a href="#bufcomparetarget-targetstart-targetend-sourcestart-sourceend"><code>buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])</code></a></li> 4481cb0ef41Sopenharmony_ci<li><a href="#bufcopytarget-targetstart-sourcestart-sourceend"><code>buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])</code></a></li> 4491cb0ef41Sopenharmony_ci<li><a href="#bufentries"><code>buf.entries()</code></a></li> 4501cb0ef41Sopenharmony_ci<li><a href="#bufequalsotherbuffer"><code>buf.equals(otherBuffer)</code></a></li> 4511cb0ef41Sopenharmony_ci<li><a href="#buffillvalue-offset-end-encoding"><code>buf.fill(value[, offset[, end]][, encoding])</code></a></li> 4521cb0ef41Sopenharmony_ci<li><a href="#bufincludesvalue-byteoffset-encoding"><code>buf.includes(value[, byteOffset][, encoding])</code></a></li> 4531cb0ef41Sopenharmony_ci<li><a href="#bufindexofvalue-byteoffset-encoding"><code>buf.indexOf(value[, byteOffset][, encoding])</code></a></li> 4541cb0ef41Sopenharmony_ci<li><a href="#bufkeys"><code>buf.keys()</code></a></li> 4551cb0ef41Sopenharmony_ci<li><a href="#buflastindexofvalue-byteoffset-encoding"><code>buf.lastIndexOf(value[, byteOffset][, encoding])</code></a></li> 4561cb0ef41Sopenharmony_ci<li><a href="#buflength"><code>buf.length</code></a></li> 4571cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#bufparent"><code>buf.parent</code></a></span></li> 4581cb0ef41Sopenharmony_ci<li><a href="#bufreadbigint64beoffset"><code>buf.readBigInt64BE([offset])</code></a></li> 4591cb0ef41Sopenharmony_ci<li><a href="#bufreadbigint64leoffset"><code>buf.readBigInt64LE([offset])</code></a></li> 4601cb0ef41Sopenharmony_ci<li><a href="#bufreadbiguint64beoffset"><code>buf.readBigUInt64BE([offset])</code></a></li> 4611cb0ef41Sopenharmony_ci<li><a href="#bufreadbiguint64leoffset"><code>buf.readBigUInt64LE([offset])</code></a></li> 4621cb0ef41Sopenharmony_ci<li><a href="#bufreaddoublebeoffset"><code>buf.readDoubleBE([offset])</code></a></li> 4631cb0ef41Sopenharmony_ci<li><a href="#bufreaddoubleleoffset"><code>buf.readDoubleLE([offset])</code></a></li> 4641cb0ef41Sopenharmony_ci<li><a href="#bufreadfloatbeoffset"><code>buf.readFloatBE([offset])</code></a></li> 4651cb0ef41Sopenharmony_ci<li><a href="#bufreadfloatleoffset"><code>buf.readFloatLE([offset])</code></a></li> 4661cb0ef41Sopenharmony_ci<li><a href="#bufreadint8offset"><code>buf.readInt8([offset])</code></a></li> 4671cb0ef41Sopenharmony_ci<li><a href="#bufreadint16beoffset"><code>buf.readInt16BE([offset])</code></a></li> 4681cb0ef41Sopenharmony_ci<li><a href="#bufreadint16leoffset"><code>buf.readInt16LE([offset])</code></a></li> 4691cb0ef41Sopenharmony_ci<li><a href="#bufreadint32beoffset"><code>buf.readInt32BE([offset])</code></a></li> 4701cb0ef41Sopenharmony_ci<li><a href="#bufreadint32leoffset"><code>buf.readInt32LE([offset])</code></a></li> 4711cb0ef41Sopenharmony_ci<li><a href="#bufreadintbeoffset-bytelength"><code>buf.readIntBE(offset, byteLength)</code></a></li> 4721cb0ef41Sopenharmony_ci<li><a href="#bufreadintleoffset-bytelength"><code>buf.readIntLE(offset, byteLength)</code></a></li> 4731cb0ef41Sopenharmony_ci<li><a href="#bufreaduint8offset"><code>buf.readUInt8([offset])</code></a></li> 4741cb0ef41Sopenharmony_ci<li><a href="#bufreaduint16beoffset"><code>buf.readUInt16BE([offset])</code></a></li> 4751cb0ef41Sopenharmony_ci<li><a href="#bufreaduint16leoffset"><code>buf.readUInt16LE([offset])</code></a></li> 4761cb0ef41Sopenharmony_ci<li><a href="#bufreaduint32beoffset"><code>buf.readUInt32BE([offset])</code></a></li> 4771cb0ef41Sopenharmony_ci<li><a href="#bufreaduint32leoffset"><code>buf.readUInt32LE([offset])</code></a></li> 4781cb0ef41Sopenharmony_ci<li><a href="#bufreaduintbeoffset-bytelength"><code>buf.readUIntBE(offset, byteLength)</code></a></li> 4791cb0ef41Sopenharmony_ci<li><a href="#bufreaduintleoffset-bytelength"><code>buf.readUIntLE(offset, byteLength)</code></a></li> 4801cb0ef41Sopenharmony_ci<li><a href="#bufsubarraystart-end"><code>buf.subarray([start[, end]])</code></a></li> 4811cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#bufslicestart-end"><code>buf.slice([start[, end]])</code></a></span></li> 4821cb0ef41Sopenharmony_ci<li><a href="#bufswap16"><code>buf.swap16()</code></a></li> 4831cb0ef41Sopenharmony_ci<li><a href="#bufswap32"><code>buf.swap32()</code></a></li> 4841cb0ef41Sopenharmony_ci<li><a href="#bufswap64"><code>buf.swap64()</code></a></li> 4851cb0ef41Sopenharmony_ci<li><a href="#buftojson"><code>buf.toJSON()</code></a></li> 4861cb0ef41Sopenharmony_ci<li><a href="#buftostringencoding-start-end"><code>buf.toString([encoding[, start[, end]]])</code></a></li> 4871cb0ef41Sopenharmony_ci<li><a href="#bufvalues"><code>buf.values()</code></a></li> 4881cb0ef41Sopenharmony_ci<li><a href="#bufwritestring-offset-length-encoding"><code>buf.write(string[, offset[, length]][, encoding])</code></a></li> 4891cb0ef41Sopenharmony_ci<li><a href="#bufwritebigint64bevalue-offset"><code>buf.writeBigInt64BE(value[, offset])</code></a></li> 4901cb0ef41Sopenharmony_ci<li><a href="#bufwritebigint64levalue-offset"><code>buf.writeBigInt64LE(value[, offset])</code></a></li> 4911cb0ef41Sopenharmony_ci<li><a href="#bufwritebiguint64bevalue-offset"><code>buf.writeBigUInt64BE(value[, offset])</code></a></li> 4921cb0ef41Sopenharmony_ci<li><a href="#bufwritebiguint64levalue-offset"><code>buf.writeBigUInt64LE(value[, offset])</code></a></li> 4931cb0ef41Sopenharmony_ci<li><a href="#bufwritedoublebevalue-offset"><code>buf.writeDoubleBE(value[, offset])</code></a></li> 4941cb0ef41Sopenharmony_ci<li><a href="#bufwritedoublelevalue-offset"><code>buf.writeDoubleLE(value[, offset])</code></a></li> 4951cb0ef41Sopenharmony_ci<li><a href="#bufwritefloatbevalue-offset"><code>buf.writeFloatBE(value[, offset])</code></a></li> 4961cb0ef41Sopenharmony_ci<li><a href="#bufwritefloatlevalue-offset"><code>buf.writeFloatLE(value[, offset])</code></a></li> 4971cb0ef41Sopenharmony_ci<li><a href="#bufwriteint8value-offset"><code>buf.writeInt8(value[, offset])</code></a></li> 4981cb0ef41Sopenharmony_ci<li><a href="#bufwriteint16bevalue-offset"><code>buf.writeInt16BE(value[, offset])</code></a></li> 4991cb0ef41Sopenharmony_ci<li><a href="#bufwriteint16levalue-offset"><code>buf.writeInt16LE(value[, offset])</code></a></li> 5001cb0ef41Sopenharmony_ci<li><a href="#bufwriteint32bevalue-offset"><code>buf.writeInt32BE(value[, offset])</code></a></li> 5011cb0ef41Sopenharmony_ci<li><a href="#bufwriteint32levalue-offset"><code>buf.writeInt32LE(value[, offset])</code></a></li> 5021cb0ef41Sopenharmony_ci<li><a href="#bufwriteintbevalue-offset-bytelength"><code>buf.writeIntBE(value, offset, byteLength)</code></a></li> 5031cb0ef41Sopenharmony_ci<li><a href="#bufwriteintlevalue-offset-bytelength"><code>buf.writeIntLE(value, offset, byteLength)</code></a></li> 5041cb0ef41Sopenharmony_ci<li><a href="#bufwriteuint8value-offset"><code>buf.writeUInt8(value[, offset])</code></a></li> 5051cb0ef41Sopenharmony_ci<li><a href="#bufwriteuint16bevalue-offset"><code>buf.writeUInt16BE(value[, offset])</code></a></li> 5061cb0ef41Sopenharmony_ci<li><a href="#bufwriteuint16levalue-offset"><code>buf.writeUInt16LE(value[, offset])</code></a></li> 5071cb0ef41Sopenharmony_ci<li><a href="#bufwriteuint32bevalue-offset"><code>buf.writeUInt32BE(value[, offset])</code></a></li> 5081cb0ef41Sopenharmony_ci<li><a href="#bufwriteuint32levalue-offset"><code>buf.writeUInt32LE(value[, offset])</code></a></li> 5091cb0ef41Sopenharmony_ci<li><a href="#bufwriteuintbevalue-offset-bytelength"><code>buf.writeUIntBE(value, offset, byteLength)</code></a></li> 5101cb0ef41Sopenharmony_ci<li><a href="#bufwriteuintlevalue-offset-bytelength"><code>buf.writeUIntLE(value, offset, byteLength)</code></a></li> 5111cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#new-bufferarray"><code>new Buffer(array)</code></a></span></li> 5121cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#new-bufferarraybuffer-byteoffset-length"><code>new Buffer(arrayBuffer[, byteOffset[, length]])</code></a></span></li> 5131cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#new-bufferbuffer"><code>new Buffer(buffer)</code></a></span></li> 5141cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#new-buffersize"><code>new Buffer(size)</code></a></span></li> 5151cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#new-bufferstring-encoding"><code>new Buffer(string[, encoding])</code></a></span></li> 5161cb0ef41Sopenharmony_ci</ul> 5171cb0ef41Sopenharmony_ci</li> 5181cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#class-file">Class: <code>File</code></a></span> 5191cb0ef41Sopenharmony_ci<ul> 5201cb0ef41Sopenharmony_ci<li><a href="#new-bufferfilesources-filename-options"><code>new buffer.File(sources, fileName[, options])</code></a></li> 5211cb0ef41Sopenharmony_ci<li><a href="#filename"><code>file.name</code></a></li> 5221cb0ef41Sopenharmony_ci<li><a href="#filelastmodified"><code>file.lastModified</code></a></li> 5231cb0ef41Sopenharmony_ci</ul> 5241cb0ef41Sopenharmony_ci</li> 5251cb0ef41Sopenharmony_ci<li><a href="#nodebuffer-module-apis"><code>node:buffer</code> module APIs</a> 5261cb0ef41Sopenharmony_ci<ul> 5271cb0ef41Sopenharmony_ci<li><span class="stability_3"><a href="#bufferatobdata"><code>buffer.atob(data)</code></a></span></li> 5281cb0ef41Sopenharmony_ci<li><span class="stability_3"><a href="#bufferbtoadata"><code>buffer.btoa(data)</code></a></span></li> 5291cb0ef41Sopenharmony_ci<li><a href="#bufferisasciiinput"><code>buffer.isAscii(input)</code></a></li> 5301cb0ef41Sopenharmony_ci<li><a href="#bufferisutf8input"><code>buffer.isUtf8(input)</code></a></li> 5311cb0ef41Sopenharmony_ci<li><a href="#bufferinspect_max_bytes"><code>buffer.INSPECT_MAX_BYTES</code></a></li> 5321cb0ef41Sopenharmony_ci<li><a href="#bufferkmaxlength"><code>buffer.kMaxLength</code></a></li> 5331cb0ef41Sopenharmony_ci<li><a href="#bufferkstringmaxlength"><code>buffer.kStringMaxLength</code></a></li> 5341cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#bufferresolveobjecturlid"><code>buffer.resolveObjectURL(id)</code></a></span></li> 5351cb0ef41Sopenharmony_ci<li><a href="#buffertranscodesource-fromenc-toenc"><code>buffer.transcode(source, fromEnc, toEnc)</code></a></li> 5361cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#class-slowbuffer">Class: <code>SlowBuffer</code></a></span> 5371cb0ef41Sopenharmony_ci<ul> 5381cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#new-slowbuffersize"><code>new SlowBuffer(size)</code></a></span></li> 5391cb0ef41Sopenharmony_ci</ul> 5401cb0ef41Sopenharmony_ci</li> 5411cb0ef41Sopenharmony_ci<li><a href="#buffer-constants">Buffer constants</a> 5421cb0ef41Sopenharmony_ci<ul> 5431cb0ef41Sopenharmony_ci<li><a href="#bufferconstantsmax_length"><code>buffer.constants.MAX_LENGTH</code></a></li> 5441cb0ef41Sopenharmony_ci<li><a href="#bufferconstantsmax_string_length"><code>buffer.constants.MAX_STRING_LENGTH</code></a></li> 5451cb0ef41Sopenharmony_ci</ul> 5461cb0ef41Sopenharmony_ci</li> 5471cb0ef41Sopenharmony_ci</ul> 5481cb0ef41Sopenharmony_ci</li> 5491cb0ef41Sopenharmony_ci<li><a href="#bufferfrom-bufferalloc-and-bufferallocunsafe"><code>Buffer.from()</code>, <code>Buffer.alloc()</code>, and <code>Buffer.allocUnsafe()</code></a> 5501cb0ef41Sopenharmony_ci<ul> 5511cb0ef41Sopenharmony_ci<li><a href="#the---zero-fill-buffers-command-line-option">The <code>--zero-fill-buffers</code> command-line option</a></li> 5521cb0ef41Sopenharmony_ci<li><a href="#what-makes-bufferallocunsafe-and-bufferallocunsafeslow-unsafe">What makes <code>Buffer.allocUnsafe()</code> and <code>Buffer.allocUnsafeSlow()</code> "unsafe"?</a></li> 5531cb0ef41Sopenharmony_ci</ul> 5541cb0ef41Sopenharmony_ci</li> 5551cb0ef41Sopenharmony_ci</ul> 5561cb0ef41Sopenharmony_ci</li> 5571cb0ef41Sopenharmony_ci</ul></details> 5581cb0ef41Sopenharmony_ci 5591cb0ef41Sopenharmony_ci <div id="apicontent"> 5601cb0ef41Sopenharmony_ci <h2>Buffer<span><a class="mark" href="#buffer" id="buffer">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer"></a></h2> 5611cb0ef41Sopenharmony_ci 5621cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 5631cb0ef41Sopenharmony_ci<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/buffer.js">lib/buffer.js</a></p> 5641cb0ef41Sopenharmony_ci<p><code>Buffer</code> objects are used to represent a fixed-length sequence of bytes. Many 5651cb0ef41Sopenharmony_ciNode.js APIs support <code>Buffer</code>s.</p> 5661cb0ef41Sopenharmony_ci<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 5671cb0ef41Sopenharmony_ciextends it with methods that cover additional use cases. Node.js APIs accept 5681cb0ef41Sopenharmony_ciplain <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> 5691cb0ef41Sopenharmony_ci<p>While the <code>Buffer</code> class is available within the global scope, it is still 5701cb0ef41Sopenharmony_cirecommended to explicitly reference it via an import or require statement.</p> 5711cb0ef41Sopenharmony_ci 5721cb0ef41Sopenharmony_ci<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>; 5731cb0ef41Sopenharmony_ci 5741cb0ef41Sopenharmony_ci<span class="hljs-comment">// Creates a zero-filled Buffer of length 10.</span> 5751cb0ef41Sopenharmony_ci<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>); 5761cb0ef41Sopenharmony_ci 5771cb0ef41Sopenharmony_ci<span class="hljs-comment">// Creates a Buffer of length 10,</span> 5781cb0ef41Sopenharmony_ci<span class="hljs-comment">// filled with bytes which all have the value `1`.</span> 5791cb0ef41Sopenharmony_ci<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>); 5801cb0ef41Sopenharmony_ci 5811cb0ef41Sopenharmony_ci<span class="hljs-comment">// Creates an uninitialized buffer of length 10.</span> 5821cb0ef41Sopenharmony_ci<span class="hljs-comment">// This is faster than calling Buffer.alloc() but the returned</span> 5831cb0ef41Sopenharmony_ci<span class="hljs-comment">// Buffer instance might contain old data that needs to be</span> 5841cb0ef41Sopenharmony_ci<span class="hljs-comment">// overwritten using fill(), write(), or other functions that fill the Buffer's</span> 5851cb0ef41Sopenharmony_ci<span class="hljs-comment">// contents.</span> 5861cb0ef41Sopenharmony_ci<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>); 5871cb0ef41Sopenharmony_ci 5881cb0ef41Sopenharmony_ci<span class="hljs-comment">// Creates a Buffer containing the bytes [1, 2, 3].</span> 5891cb0ef41Sopenharmony_ci<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>]); 5901cb0ef41Sopenharmony_ci 5911cb0ef41Sopenharmony_ci<span class="hljs-comment">// Creates a Buffer containing the bytes [1, 1, 1, 1] – the entries</span> 5921cb0ef41Sopenharmony_ci<span class="hljs-comment">// are all truncated using `(value & 255)` to fit into the range 0–255.</span> 5931cb0ef41Sopenharmony_ci<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>]); 5941cb0ef41Sopenharmony_ci 5951cb0ef41Sopenharmony_ci<span class="hljs-comment">// Creates a Buffer containing the UTF-8-encoded bytes for the string 'tést':</span> 5961cb0ef41Sopenharmony_ci<span class="hljs-comment">// [0x74, 0xc3, 0xa9, 0x73, 0x74] (in hexadecimal notation)</span> 5971cb0ef41Sopenharmony_ci<span class="hljs-comment">// [116, 195, 169, 115, 116] (in decimal notation)</span> 5981cb0ef41Sopenharmony_ci<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>); 5991cb0ef41Sopenharmony_ci 6001cb0ef41Sopenharmony_ci<span class="hljs-comment">// Creates a Buffer containing the Latin-1 bytes [0x74, 0xe9, 0x73, 0x74].</span> 6011cb0ef41Sopenharmony_ci<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>); 6021cb0ef41Sopenharmony_ci 6031cb0ef41Sopenharmony_ci<span class="hljs-comment">// Creates a zero-filled Buffer of length 10.</span> 6041cb0ef41Sopenharmony_ci<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>); 6051cb0ef41Sopenharmony_ci 6061cb0ef41Sopenharmony_ci<span class="hljs-comment">// Creates a Buffer of length 10,</span> 6071cb0ef41Sopenharmony_ci<span class="hljs-comment">// filled with bytes which all have the value `1`.</span> 6081cb0ef41Sopenharmony_ci<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>); 6091cb0ef41Sopenharmony_ci 6101cb0ef41Sopenharmony_ci<span class="hljs-comment">// Creates an uninitialized buffer of length 10.</span> 6111cb0ef41Sopenharmony_ci<span class="hljs-comment">// This is faster than calling Buffer.alloc() but the returned</span> 6121cb0ef41Sopenharmony_ci<span class="hljs-comment">// Buffer instance might contain old data that needs to be</span> 6131cb0ef41Sopenharmony_ci<span class="hljs-comment">// overwritten using fill(), write(), or other functions that fill the Buffer's</span> 6141cb0ef41Sopenharmony_ci<span class="hljs-comment">// contents.</span> 6151cb0ef41Sopenharmony_ci<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>); 6161cb0ef41Sopenharmony_ci 6171cb0ef41Sopenharmony_ci<span class="hljs-comment">// Creates a Buffer containing the bytes [1, 2, 3].</span> 6181cb0ef41Sopenharmony_ci<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>]); 6191cb0ef41Sopenharmony_ci 6201cb0ef41Sopenharmony_ci<span class="hljs-comment">// Creates a Buffer containing the bytes [1, 1, 1, 1] – the entries</span> 6211cb0ef41Sopenharmony_ci<span class="hljs-comment">// are all truncated using `(value & 255)` to fit into the range 0–255.</span> 6221cb0ef41Sopenharmony_ci<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>]); 6231cb0ef41Sopenharmony_ci 6241cb0ef41Sopenharmony_ci<span class="hljs-comment">// Creates a Buffer containing the UTF-8-encoded bytes for the string 'tést':</span> 6251cb0ef41Sopenharmony_ci<span class="hljs-comment">// [0x74, 0xc3, 0xa9, 0x73, 0x74] (in hexadecimal notation)</span> 6261cb0ef41Sopenharmony_ci<span class="hljs-comment">// [116, 195, 169, 115, 116] (in decimal notation)</span> 6271cb0ef41Sopenharmony_ci<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>); 6281cb0ef41Sopenharmony_ci 6291cb0ef41Sopenharmony_ci<span class="hljs-comment">// Creates a Buffer containing the Latin-1 bytes [0x74, 0xe9, 0x73, 0x74].</span> 6301cb0ef41Sopenharmony_ci<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> 6311cb0ef41Sopenharmony_ci<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> 6321cb0ef41Sopenharmony_ci<div class="api_metadata"> 6331cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 6341cb0ef41Sopenharmony_ci<table> 6351cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 6361cb0ef41Sopenharmony_ci<tr><td>v15.7.0, v14.18.0</td> 6371cb0ef41Sopenharmony_ci<td><p>Introduced <code>base64url</code> encoding.</p></td></tr> 6381cb0ef41Sopenharmony_ci<tr><td>v6.4.0</td> 6391cb0ef41Sopenharmony_ci<td><p>Introduced <code>latin1</code> as an alias for <code>binary</code>.</p></td></tr> 6401cb0ef41Sopenharmony_ci<tr><td>v5.0.0</td> 6411cb0ef41Sopenharmony_ci<td><p>Removed the deprecated <code>raw</code> and <code>raws</code> encodings.</p></td></tr> 6421cb0ef41Sopenharmony_ci</tbody></table> 6431cb0ef41Sopenharmony_ci</details> 6441cb0ef41Sopenharmony_ci</div> 6451cb0ef41Sopenharmony_ci<p>When converting between <code>Buffer</code>s and strings, a character encoding may be 6461cb0ef41Sopenharmony_cispecified. If no character encoding is specified, UTF-8 will be used as the 6471cb0ef41Sopenharmony_cidefault.</p> 6481cb0ef41Sopenharmony_ci 6491cb0ef41Sopenharmony_ci<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>; 6501cb0ef41Sopenharmony_ci 6511cb0ef41Sopenharmony_ci<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>); 6521cb0ef41Sopenharmony_ci 6531cb0ef41Sopenharmony_ci<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>)); 6541cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 68656c6c6f20776f726c64</span> 6551cb0ef41Sopenharmony_ci<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>)); 6561cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: aGVsbG8gd29ybGQ=</span> 6571cb0ef41Sopenharmony_ci 6581cb0ef41Sopenharmony_ci<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>)); 6591cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 66 68 71 77 68 67 61 64 73></span> 6601cb0ef41Sopenharmony_ci<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>)); 6611cb0ef41Sopenharmony_ci<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>); 6621cb0ef41Sopenharmony_ci 6631cb0ef41Sopenharmony_ci<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>); 6641cb0ef41Sopenharmony_ci 6651cb0ef41Sopenharmony_ci<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>)); 6661cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 68656c6c6f20776f726c64</span> 6671cb0ef41Sopenharmony_ci<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>)); 6681cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: aGVsbG8gd29ybGQ=</span> 6691cb0ef41Sopenharmony_ci 6701cb0ef41Sopenharmony_ci<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>)); 6711cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 66 68 71 77 68 67 61 64 73></span> 6721cb0ef41Sopenharmony_ci<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>)); 6731cb0ef41Sopenharmony_ci<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> 6741cb0ef41Sopenharmony_ci<p>Node.js buffers accept all case variations of encoding strings that they 6751cb0ef41Sopenharmony_cireceive. For example, UTF-8 can be specified as <code>'utf8'</code>, <code>'UTF8'</code>, or <code>'uTf8'</code>.</p> 6761cb0ef41Sopenharmony_ci<p>The character encodings currently supported by Node.js are the following:</p> 6771cb0ef41Sopenharmony_ci<ul> 6781cb0ef41Sopenharmony_ci<li> 6791cb0ef41Sopenharmony_ci<p><code>'utf8'</code> (alias: <code>'utf-8'</code>): Multi-byte encoded Unicode characters. Many web 6801cb0ef41Sopenharmony_cipages and other document formats use <a href="https://en.wikipedia.org/wiki/UTF-8">UTF-8</a>. This is the default character 6811cb0ef41Sopenharmony_ciencoding. When decoding a <code>Buffer</code> into a string that does not exclusively 6821cb0ef41Sopenharmony_cicontain valid UTF-8 data, the Unicode replacement character <code>U+FFFD</code> � will be 6831cb0ef41Sopenharmony_ciused to represent those errors.</p> 6841cb0ef41Sopenharmony_ci</li> 6851cb0ef41Sopenharmony_ci<li> 6861cb0ef41Sopenharmony_ci<p><code>'utf16le'</code> (alias: <code>'utf-16le'</code>): Multi-byte encoded Unicode characters. 6871cb0ef41Sopenharmony_ciUnlike <code>'utf8'</code>, each character in the string will be encoded using either 2 6881cb0ef41Sopenharmony_cior 4 bytes. Node.js only supports the <a href="https://en.wikipedia.org/wiki/Endianness">little-endian</a> variant of 6891cb0ef41Sopenharmony_ci<a href="https://en.wikipedia.org/wiki/UTF-16">UTF-16</a>.</p> 6901cb0ef41Sopenharmony_ci</li> 6911cb0ef41Sopenharmony_ci<li> 6921cb0ef41Sopenharmony_ci<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 6931cb0ef41Sopenharmony_cisupports the Unicode characters from <code>U+0000</code> to <code>U+00FF</code>. Each character is 6941cb0ef41Sopenharmony_ciencoded using a single byte. Characters that do not fit into that range are 6951cb0ef41Sopenharmony_citruncated and will be mapped to characters in that range.</p> 6961cb0ef41Sopenharmony_ci</li> 6971cb0ef41Sopenharmony_ci</ul> 6981cb0ef41Sopenharmony_ci<p>Converting a <code>Buffer</code> into a string using one of the above is referred to as 6991cb0ef41Sopenharmony_cidecoding, and converting a string into a <code>Buffer</code> is referred to as encoding.</p> 7001cb0ef41Sopenharmony_ci<p>Node.js also supports the following binary-to-text encodings. For 7011cb0ef41Sopenharmony_cibinary-to-text encodings, the naming convention is reversed: Converting a 7021cb0ef41Sopenharmony_ci<code>Buffer</code> into a string is typically referred to as encoding, and converting a 7031cb0ef41Sopenharmony_cistring into a <code>Buffer</code> as decoding.</p> 7041cb0ef41Sopenharmony_ci<ul> 7051cb0ef41Sopenharmony_ci<li> 7061cb0ef41Sopenharmony_ci<p><code>'base64'</code>: <a href="https://en.wikipedia.org/wiki/Base64">Base64</a> encoding. When creating a <code>Buffer</code> from a string, 7071cb0ef41Sopenharmony_cithis encoding will also correctly accept "URL and Filename Safe Alphabet" as 7081cb0ef41Sopenharmony_cispecified in <a href="https://tools.ietf.org/html/rfc4648#section-5">RFC 4648, Section 5</a>. Whitespace characters such as spaces, 7091cb0ef41Sopenharmony_citabs, and new lines contained within the base64-encoded string are ignored.</p> 7101cb0ef41Sopenharmony_ci</li> 7111cb0ef41Sopenharmony_ci<li> 7121cb0ef41Sopenharmony_ci<p><code>'base64url'</code>: <a href="https://tools.ietf.org/html/rfc4648#section-5">base64url</a> encoding as specified in 7131cb0ef41Sopenharmony_ci<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 7141cb0ef41Sopenharmony_ciencoding will also correctly accept regular base64-encoded strings. When 7151cb0ef41Sopenharmony_ciencoding a <code>Buffer</code> to a string, this encoding will omit padding.</p> 7161cb0ef41Sopenharmony_ci</li> 7171cb0ef41Sopenharmony_ci<li> 7181cb0ef41Sopenharmony_ci<p><code>'hex'</code>: Encode each byte as two hexadecimal characters. Data truncation 7191cb0ef41Sopenharmony_cimay occur when decoding strings that do not exclusively consist of an even 7201cb0ef41Sopenharmony_cinumber of hexadecimal characters. See below for an example.</p> 7211cb0ef41Sopenharmony_ci</li> 7221cb0ef41Sopenharmony_ci</ul> 7231cb0ef41Sopenharmony_ci<p>The following legacy character encodings are also supported:</p> 7241cb0ef41Sopenharmony_ci<ul> 7251cb0ef41Sopenharmony_ci<li> 7261cb0ef41Sopenharmony_ci<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 7271cb0ef41Sopenharmony_ci<code>Buffer</code>, this is equivalent to using <code>'latin1'</code>. When decoding a <code>Buffer</code> 7281cb0ef41Sopenharmony_ciinto a string, using this encoding will additionally unset the highest bit of 7291cb0ef41Sopenharmony_cieach byte before decoding as <code>'latin1'</code>. 7301cb0ef41Sopenharmony_ciGenerally, there should be no reason to use this encoding, as <code>'utf8'</code> 7311cb0ef41Sopenharmony_ci(or, if the data is known to always be ASCII-only, <code>'latin1'</code>) will be a 7321cb0ef41Sopenharmony_cibetter choice when encoding or decoding ASCII-only text. It is only provided 7331cb0ef41Sopenharmony_cifor legacy compatibility.</p> 7341cb0ef41Sopenharmony_ci</li> 7351cb0ef41Sopenharmony_ci<li> 7361cb0ef41Sopenharmony_ci<p><code>'binary'</code>: Alias for <code>'latin1'</code>. 7371cb0ef41Sopenharmony_ciThe name of this encoding can be very misleading, as all of the 7381cb0ef41Sopenharmony_ciencodings listed here convert between strings and binary data. For converting 7391cb0ef41Sopenharmony_cibetween strings and <code>Buffer</code>s, typically <code>'utf8'</code> is the right choice.</p> 7401cb0ef41Sopenharmony_ci</li> 7411cb0ef41Sopenharmony_ci<li> 7421cb0ef41Sopenharmony_ci<p><code>'ucs2'</code>, <code>'ucs-2'</code>: Aliases of <code>'utf16le'</code>. UCS-2 used to refer to a variant 7431cb0ef41Sopenharmony_ciof UTF-16 that did not support characters that had code points larger than 7441cb0ef41Sopenharmony_ciU+FFFF. In Node.js, these code points are always supported.</p> 7451cb0ef41Sopenharmony_ci</li> 7461cb0ef41Sopenharmony_ci</ul> 7471cb0ef41Sopenharmony_ci 7481cb0ef41Sopenharmony_ci<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>; 7491cb0ef41Sopenharmony_ci 7501cb0ef41Sopenharmony_ci<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>); 7511cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints <Buffer 1a>, data truncated when first non-hexadecimal value</span> 7521cb0ef41Sopenharmony_ci<span class="hljs-comment">// ('g') encountered.</span> 7531cb0ef41Sopenharmony_ci 7541cb0ef41Sopenharmony_ci<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>); 7551cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints <Buffer 1a>, data truncated when data ends in single digit ('7').</span> 7561cb0ef41Sopenharmony_ci 7571cb0ef41Sopenharmony_ci<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>); 7581cb0ef41Sopenharmony_ci<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>); 7591cb0ef41Sopenharmony_ci 7601cb0ef41Sopenharmony_ci<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>); 7611cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints <Buffer 1a>, data truncated when first non-hexadecimal value</span> 7621cb0ef41Sopenharmony_ci<span class="hljs-comment">// ('g') encountered.</span> 7631cb0ef41Sopenharmony_ci 7641cb0ef41Sopenharmony_ci<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>); 7651cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints <Buffer 1a>, data truncated when data ends in single digit ('7').</span> 7661cb0ef41Sopenharmony_ci 7671cb0ef41Sopenharmony_ci<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>); 7681cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints <Buffer 16 34>, all data represented.</span></code><button class="copy-button">copy</button></pre> 7691cb0ef41Sopenharmony_ci<p>Modern Web browsers follow the <a href="https://encoding.spec.whatwg.org/">WHATWG Encoding Standard</a> which aliases 7701cb0ef41Sopenharmony_ciboth <code>'latin1'</code> and <code>'ISO-8859-1'</code> to <code>'win-1252'</code>. This means that while doing 7711cb0ef41Sopenharmony_cisomething like <code>http.get()</code>, if the returned charset is one of those listed in 7721cb0ef41Sopenharmony_cithe WHATWG specification it is possible that the server actually returned 7731cb0ef41Sopenharmony_ci<code>'win-1252'</code>-encoded data, and using <code>'latin1'</code> encoding may incorrectly decode 7741cb0ef41Sopenharmony_cithe characters.</p> 7751cb0ef41Sopenharmony_ci</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> 7761cb0ef41Sopenharmony_ci<div class="api_metadata"> 7771cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 7781cb0ef41Sopenharmony_ci<table> 7791cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 7801cb0ef41Sopenharmony_ci<tr><td>v3.0.0</td> 7811cb0ef41Sopenharmony_ci<td><p>The <code>Buffer</code>s class now inherits from <code>Uint8Array</code>.</p></td></tr> 7821cb0ef41Sopenharmony_ci</tbody></table> 7831cb0ef41Sopenharmony_ci</details> 7841cb0ef41Sopenharmony_ci</div> 7851cb0ef41Sopenharmony_ci<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> 7861cb0ef41Sopenharmony_ciinstances. 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, 7871cb0ef41Sopenharmony_cihowever, subtle incompatibilities between the <code>Buffer</code> API and the 7881cb0ef41Sopenharmony_ci<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> API.</p> 7891cb0ef41Sopenharmony_ci<p>In particular:</p> 7901cb0ef41Sopenharmony_ci<ul> 7911cb0ef41Sopenharmony_ci<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>, 7921cb0ef41Sopenharmony_ci<a href="#bufslicestart-end"><code>Buffer.prototype.slice()</code></a> creates a view over the existing <code>Buffer</code> 7931cb0ef41Sopenharmony_ciwithout copying. This behavior can be surprising, and only exists for legacy 7941cb0ef41Sopenharmony_cicompatibility. <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 7951cb0ef41Sopenharmony_cithe behavior of <a href="#bufslicestart-end"><code>Buffer.prototype.slice()</code></a> on both <code>Buffer</code>s 7961cb0ef41Sopenharmony_ciand other <code>TypedArray</code>s and should be preferred.</li> 7971cb0ef41Sopenharmony_ci<li><a href="#buftostringencoding-start-end"><code>buf.toString()</code></a> is incompatible with its <code>TypedArray</code> equivalent.</li> 7981cb0ef41Sopenharmony_ci<li>A number of methods, e.g. <a href="#bufindexofvalue-byteoffset-encoding"><code>buf.indexOf()</code></a>, support additional arguments.</li> 7991cb0ef41Sopenharmony_ci</ul> 8001cb0ef41Sopenharmony_ci<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> 8011cb0ef41Sopenharmony_ci<ul> 8021cb0ef41Sopenharmony_ci<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 8031cb0ef41Sopenharmony_cicontents, interpreted as an array of integers, and not as a byte sequence 8041cb0ef41Sopenharmony_ciof the target type.</li> 8051cb0ef41Sopenharmony_ci</ul> 8061cb0ef41Sopenharmony_ci 8071cb0ef41Sopenharmony_ci<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>; 8081cb0ef41Sopenharmony_ci 8091cb0ef41Sopenharmony_ci<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>]); 8101cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> uint32array = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint32Array</span>(buf); 8111cb0ef41Sopenharmony_ci 8121cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(uint32array); 8131cb0ef41Sopenharmony_ci 8141cb0ef41Sopenharmony_ci<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>); 8151cb0ef41Sopenharmony_ci 8161cb0ef41Sopenharmony_ci<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>]); 8171cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> uint32array = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint32Array</span>(buf); 8181cb0ef41Sopenharmony_ci 8191cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(uint32array); 8201cb0ef41Sopenharmony_ci 8211cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: Uint32Array(4) [ 1, 2, 3, 4 ]</span></code><button class="copy-button">copy</button></pre> 8221cb0ef41Sopenharmony_ci<ul> 8231cb0ef41Sopenharmony_ci<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 8241cb0ef41Sopenharmony_ci<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> 8251cb0ef41Sopenharmony_ci</ul> 8261cb0ef41Sopenharmony_ci 8271cb0ef41Sopenharmony_ci<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>; 8281cb0ef41Sopenharmony_ci 8291cb0ef41Sopenharmony_ci<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>); 8301cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> uint16array = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint16Array</span>( 8311cb0ef41Sopenharmony_ci buf.<span class="hljs-property">buffer</span>, 8321cb0ef41Sopenharmony_ci buf.<span class="hljs-property">byteOffset</span>, 8331cb0ef41Sopenharmony_ci buf.<span class="hljs-property">length</span> / <span class="hljs-title class_">Uint16Array</span>.<span class="hljs-property">BYTES_PER_ELEMENT</span>); 8341cb0ef41Sopenharmony_ci 8351cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(uint16array); 8361cb0ef41Sopenharmony_ci 8371cb0ef41Sopenharmony_ci<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>); 8381cb0ef41Sopenharmony_ci 8391cb0ef41Sopenharmony_ci<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>); 8401cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> uint16array = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint16Array</span>( 8411cb0ef41Sopenharmony_ci buf.<span class="hljs-property">buffer</span>, 8421cb0ef41Sopenharmony_ci buf.<span class="hljs-property">byteOffset</span>, 8431cb0ef41Sopenharmony_ci buf.<span class="hljs-property">length</span> / <span class="hljs-title class_">Uint16Array</span>.<span class="hljs-property">BYTES_PER_ELEMENT</span>); 8441cb0ef41Sopenharmony_ci 8451cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(uint16array); 8461cb0ef41Sopenharmony_ci 8471cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: Uint16Array(5) [ 104, 101, 108, 108, 111 ]</span></code><button class="copy-button">copy</button></pre> 8481cb0ef41Sopenharmony_ci<p>It is possible to create a new <code>Buffer</code> that shares the same allocated 8491cb0ef41Sopenharmony_cimemory 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 8501cb0ef41Sopenharmony_ci<code>.buffer</code> property in the same way. <a href="#static-method-bufferfromarraybuffer-byteoffset-length"><code>Buffer.from()</code></a> 8511cb0ef41Sopenharmony_cibehaves like <code>new Uint8Array()</code> in this context.</p> 8521cb0ef41Sopenharmony_ci 8531cb0ef41Sopenharmony_ci<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>; 8541cb0ef41Sopenharmony_ci 8551cb0ef41Sopenharmony_ci<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>); 8561cb0ef41Sopenharmony_ci 8571cb0ef41Sopenharmony_ciarr[<span class="hljs-number">0</span>] = <span class="hljs-number">5000</span>; 8581cb0ef41Sopenharmony_ciarr[<span class="hljs-number">1</span>] = <span class="hljs-number">4000</span>; 8591cb0ef41Sopenharmony_ci 8601cb0ef41Sopenharmony_ci<span class="hljs-comment">// Copies the contents of `arr`.</span> 8611cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arr); 8621cb0ef41Sopenharmony_ci 8631cb0ef41Sopenharmony_ci<span class="hljs-comment">// Shares memory with `arr`.</span> 8641cb0ef41Sopenharmony_ci<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>); 8651cb0ef41Sopenharmony_ci 8661cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 8671cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 88 a0></span> 8681cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2); 8691cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 88 13 a0 0f></span> 8701cb0ef41Sopenharmony_ci 8711cb0ef41Sopenharmony_ciarr[<span class="hljs-number">1</span>] = <span class="hljs-number">6000</span>; 8721cb0ef41Sopenharmony_ci 8731cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 8741cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 88 a0></span> 8751cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2); 8761cb0ef41Sopenharmony_ci<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>); 8771cb0ef41Sopenharmony_ci 8781cb0ef41Sopenharmony_ci<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>); 8791cb0ef41Sopenharmony_ci 8801cb0ef41Sopenharmony_ciarr[<span class="hljs-number">0</span>] = <span class="hljs-number">5000</span>; 8811cb0ef41Sopenharmony_ciarr[<span class="hljs-number">1</span>] = <span class="hljs-number">4000</span>; 8821cb0ef41Sopenharmony_ci 8831cb0ef41Sopenharmony_ci<span class="hljs-comment">// Copies the contents of `arr`.</span> 8841cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arr); 8851cb0ef41Sopenharmony_ci 8861cb0ef41Sopenharmony_ci<span class="hljs-comment">// Shares memory with `arr`.</span> 8871cb0ef41Sopenharmony_ci<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>); 8881cb0ef41Sopenharmony_ci 8891cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 8901cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 88 a0></span> 8911cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2); 8921cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 88 13 a0 0f></span> 8931cb0ef41Sopenharmony_ci 8941cb0ef41Sopenharmony_ciarr[<span class="hljs-number">1</span>] = <span class="hljs-number">6000</span>; 8951cb0ef41Sopenharmony_ci 8961cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 8971cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 88 a0></span> 8981cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2); 8991cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 88 13 70 17></span></code><button class="copy-button">copy</button></pre> 9001cb0ef41Sopenharmony_ci<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 9011cb0ef41Sopenharmony_cipossible 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 9021cb0ef41Sopenharmony_ci<code>byteOffset</code> and <code>length</code> parameters.</p> 9031cb0ef41Sopenharmony_ci 9041cb0ef41Sopenharmony_ci<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>; 9051cb0ef41Sopenharmony_ci 9061cb0ef41Sopenharmony_ci<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>); 9071cb0ef41Sopenharmony_ci<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>); 9081cb0ef41Sopenharmony_ci 9091cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>); 9101cb0ef41Sopenharmony_ci<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>); 9111cb0ef41Sopenharmony_ci 9121cb0ef41Sopenharmony_ci<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>); 9131cb0ef41Sopenharmony_ci<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>); 9141cb0ef41Sopenharmony_ci 9151cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>); 9161cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 16</span></code><button class="copy-button">copy</button></pre> 9171cb0ef41Sopenharmony_ci<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 9181cb0ef41Sopenharmony_ciimplementations. 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 9191cb0ef41Sopenharmony_ciargument that is a mapping function that is invoked on every element of the 9201cb0ef41Sopenharmony_cityped array:</p> 9211cb0ef41Sopenharmony_ci<ul> 9221cb0ef41Sopenharmony_ci<li><code>TypedArray.from(source[, mapFn[, thisArg]])</code></li> 9231cb0ef41Sopenharmony_ci</ul> 9241cb0ef41Sopenharmony_ci<p>The <code>Buffer.from()</code> method, however, does not support the use of a mapping 9251cb0ef41Sopenharmony_cifunction:</p> 9261cb0ef41Sopenharmony_ci<ul> 9271cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferfromarray"><code>Buffer.from(array)</code></a></li> 9281cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferfrombuffer"><code>Buffer.from(buffer)</code></a></li> 9291cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferfromarraybuffer-byteoffset-length"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a></li> 9301cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferfromstring-encoding"><code>Buffer.from(string[, encoding])</code></a></li> 9311cb0ef41Sopenharmony_ci</ul> 9321cb0ef41Sopenharmony_ci</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> 9331cb0ef41Sopenharmony_ci<p><code>Buffer</code> instances can be iterated over using <code>for..of</code> syntax:</p> 9341cb0ef41Sopenharmony_ci 9351cb0ef41Sopenharmony_ci<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>; 9361cb0ef41Sopenharmony_ci 9371cb0ef41Sopenharmony_ci<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>]); 9381cb0ef41Sopenharmony_ci 9391cb0ef41Sopenharmony_ci<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> b <span class="hljs-keyword">of</span> buf) { 9401cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b); 9411cb0ef41Sopenharmony_ci} 9421cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 9431cb0ef41Sopenharmony_ci<span class="hljs-comment">// 1</span> 9441cb0ef41Sopenharmony_ci<span class="hljs-comment">// 2</span> 9451cb0ef41Sopenharmony_ci<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>); 9461cb0ef41Sopenharmony_ci 9471cb0ef41Sopenharmony_ci<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>]); 9481cb0ef41Sopenharmony_ci 9491cb0ef41Sopenharmony_ci<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> b <span class="hljs-keyword">of</span> buf) { 9501cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b); 9511cb0ef41Sopenharmony_ci} 9521cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 9531cb0ef41Sopenharmony_ci<span class="hljs-comment">// 1</span> 9541cb0ef41Sopenharmony_ci<span class="hljs-comment">// 2</span> 9551cb0ef41Sopenharmony_ci<span class="hljs-comment">// 3</span></code><button class="copy-button">copy</button></pre> 9561cb0ef41Sopenharmony_ci<p>Additionally, the <a href="#bufvalues"><code>buf.values()</code></a>, <a href="#bufkeys"><code>buf.keys()</code></a>, and 9571cb0ef41Sopenharmony_ci<a href="#bufentries"><code>buf.entries()</code></a> methods can be used to create iterators.</p> 9581cb0ef41Sopenharmony_ci</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> 9591cb0ef41Sopenharmony_ci<div class="api_metadata"> 9601cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 9611cb0ef41Sopenharmony_ci<table> 9621cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 9631cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 9641cb0ef41Sopenharmony_ci<td><p>No longer experimental.</p></td></tr> 9651cb0ef41Sopenharmony_ci<tr><td>v15.7.0, v14.18.0</td> 9661cb0ef41Sopenharmony_ci<td><p><span>Added in: v15.7.0, v14.18.0</span></p></td></tr> 9671cb0ef41Sopenharmony_ci</tbody></table> 9681cb0ef41Sopenharmony_ci</details> 9691cb0ef41Sopenharmony_ci</div> 9701cb0ef41Sopenharmony_ci<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 9711cb0ef41Sopenharmony_cimultiple worker threads.</p> 9721cb0ef41Sopenharmony_ci<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> 9731cb0ef41Sopenharmony_ci<div class="api_metadata"> 9741cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 9751cb0ef41Sopenharmony_ci<table> 9761cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 9771cb0ef41Sopenharmony_ci<tr><td>v16.7.0</td> 9781cb0ef41Sopenharmony_ci<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> 9791cb0ef41Sopenharmony_ci<tr><td>v15.7.0, v14.18.0</td> 9801cb0ef41Sopenharmony_ci<td><p><span>Added in: v15.7.0, v14.18.0</span></p></td></tr> 9811cb0ef41Sopenharmony_ci</tbody></table> 9821cb0ef41Sopenharmony_ci</details> 9831cb0ef41Sopenharmony_ci</div> 9841cb0ef41Sopenharmony_ci<ul> 9851cb0ef41Sopenharmony_ci<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 9861cb0ef41Sopenharmony_ciarray 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, 9871cb0ef41Sopenharmony_cior any mix of such objects, that will be stored within the <code>Blob</code>.</li> 9881cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 9891cb0ef41Sopenharmony_ci<ul> 9901cb0ef41Sopenharmony_ci<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 9911cb0ef41Sopenharmony_cito <code>'native'</code>, line endings in string source parts will be converted to 9921cb0ef41Sopenharmony_cithe platform native line-ending as specified by <code>require('node:os').EOL</code>.</li> 9931cb0ef41Sopenharmony_ci<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 9941cb0ef41Sopenharmony_cithe MIME media type of the data, however no validation of the type format 9951cb0ef41Sopenharmony_ciis performed.</li> 9961cb0ef41Sopenharmony_ci</ul> 9971cb0ef41Sopenharmony_ci</li> 9981cb0ef41Sopenharmony_ci</ul> 9991cb0ef41Sopenharmony_ci<p>Creates a new <code>Blob</code> object containing a concatenation of the given sources.</p> 10001cb0ef41Sopenharmony_ci<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 10011cb0ef41Sopenharmony_cithe 'Blob' and can therefore be safely modified after the 'Blob' is created.</p> 10021cb0ef41Sopenharmony_ci<p>String sources are encoded as UTF-8 byte sequences and copied into the Blob. 10031cb0ef41Sopenharmony_ciUnmatched surrogate pairs within each string part will be replaced by Unicode 10041cb0ef41Sopenharmony_ciU+FFFD replacement characters.</p> 10051cb0ef41Sopenharmony_ci<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> 10061cb0ef41Sopenharmony_ci<div class="api_metadata"> 10071cb0ef41Sopenharmony_ci<span>Added in: v15.7.0, v14.18.0</span> 10081cb0ef41Sopenharmony_ci</div> 10091cb0ef41Sopenharmony_ci<ul> 10101cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 10111cb0ef41Sopenharmony_ci</ul> 10121cb0ef41Sopenharmony_ci<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 10131cb0ef41Sopenharmony_cithe <code>Blob</code> data.</p> 10141cb0ef41Sopenharmony_ci<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> 10151cb0ef41Sopenharmony_ci<div class="api_metadata"> 10161cb0ef41Sopenharmony_ci<span>Added in: v15.7.0, v14.18.0</span> 10171cb0ef41Sopenharmony_ci</div> 10181cb0ef41Sopenharmony_ci<p>The total size of the <code>Blob</code> in bytes.</p> 10191cb0ef41Sopenharmony_ci<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> 10201cb0ef41Sopenharmony_ci<div class="api_metadata"> 10211cb0ef41Sopenharmony_ci<span>Added in: v15.7.0, v14.18.0</span> 10221cb0ef41Sopenharmony_ci</div> 10231cb0ef41Sopenharmony_ci<ul> 10241cb0ef41Sopenharmony_ci<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> 10251cb0ef41Sopenharmony_ci<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> 10261cb0ef41Sopenharmony_ci<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> 10271cb0ef41Sopenharmony_ci</ul> 10281cb0ef41Sopenharmony_ci<p>Creates and returns a new <code>Blob</code> containing a subset of this <code>Blob</code> objects 10291cb0ef41Sopenharmony_cidata. The original <code>Blob</code> is not altered.</p> 10301cb0ef41Sopenharmony_ci<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> 10311cb0ef41Sopenharmony_ci<div class="api_metadata"> 10321cb0ef41Sopenharmony_ci<span>Added in: v16.7.0</span> 10331cb0ef41Sopenharmony_ci</div> 10341cb0ef41Sopenharmony_ci<ul> 10351cb0ef41Sopenharmony_ci<li>Returns: <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a></li> 10361cb0ef41Sopenharmony_ci</ul> 10371cb0ef41Sopenharmony_ci<p>Returns a new <code>ReadableStream</code> that allows the content of the <code>Blob</code> to be read.</p> 10381cb0ef41Sopenharmony_ci<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> 10391cb0ef41Sopenharmony_ci<div class="api_metadata"> 10401cb0ef41Sopenharmony_ci<span>Added in: v15.7.0, v14.18.0</span> 10411cb0ef41Sopenharmony_ci</div> 10421cb0ef41Sopenharmony_ci<ul> 10431cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 10441cb0ef41Sopenharmony_ci</ul> 10451cb0ef41Sopenharmony_ci<p>Returns a promise that fulfills with the contents of the <code>Blob</code> decoded as a 10461cb0ef41Sopenharmony_ciUTF-8 string.</p> 10471cb0ef41Sopenharmony_ci<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> 10481cb0ef41Sopenharmony_ci<div class="api_metadata"> 10491cb0ef41Sopenharmony_ci<span>Added in: v15.7.0, v14.18.0</span> 10501cb0ef41Sopenharmony_ci</div> 10511cb0ef41Sopenharmony_ci<ul> 10521cb0ef41Sopenharmony_ci<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 10531cb0ef41Sopenharmony_ci</ul> 10541cb0ef41Sopenharmony_ci<p>The content-type of the <code>Blob</code>.</p> 10551cb0ef41Sopenharmony_ci<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> 10561cb0ef41Sopenharmony_ci<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 10571cb0ef41Sopenharmony_cidestinations without transferring or immediately copying the data. The data 10581cb0ef41Sopenharmony_cicontained by the <code>Blob</code> is copied only when the <code>arrayBuffer()</code> or <code>text()</code> 10591cb0ef41Sopenharmony_cimethods are called.</p> 10601cb0ef41Sopenharmony_ci 10611cb0ef41Sopenharmony_ci<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>; 10621cb0ef41Sopenharmony_ci<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>; 10631cb0ef41Sopenharmony_ci 10641cb0ef41Sopenharmony_ci<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>]); 10651cb0ef41Sopenharmony_ci 10661cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> mc1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); 10671cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> mc2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); 10681cb0ef41Sopenharmony_ci 10691cb0ef41Sopenharmony_cimc1.<span class="hljs-property">port1</span>.<span class="hljs-property">onmessage</span> = <span class="hljs-keyword">async</span> ({ data }) => { 10701cb0ef41Sopenharmony_ci <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>()); 10711cb0ef41Sopenharmony_ci mc1.<span class="hljs-property">port1</span>.<span class="hljs-title function_">close</span>(); 10721cb0ef41Sopenharmony_ci}; 10731cb0ef41Sopenharmony_ci 10741cb0ef41Sopenharmony_cimc2.<span class="hljs-property">port1</span>.<span class="hljs-property">onmessage</span> = <span class="hljs-keyword">async</span> ({ data }) => { 10751cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> <span class="hljs-title function_">delay</span>(<span class="hljs-number">1000</span>); 10761cb0ef41Sopenharmony_ci <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>()); 10771cb0ef41Sopenharmony_ci mc2.<span class="hljs-property">port1</span>.<span class="hljs-title function_">close</span>(); 10781cb0ef41Sopenharmony_ci}; 10791cb0ef41Sopenharmony_ci 10801cb0ef41Sopenharmony_cimc1.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(blob); 10811cb0ef41Sopenharmony_cimc2.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(blob); 10821cb0ef41Sopenharmony_ci 10831cb0ef41Sopenharmony_ci<span class="hljs-comment">// The Blob is still usable after posting.</span> 10841cb0ef41Sopenharmony_ciblob.<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>); 10851cb0ef41Sopenharmony_ci<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>); 10861cb0ef41Sopenharmony_ci 10871cb0ef41Sopenharmony_ci<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>]); 10881cb0ef41Sopenharmony_ci 10891cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> mc1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); 10901cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> mc2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); 10911cb0ef41Sopenharmony_ci 10921cb0ef41Sopenharmony_cimc1.<span class="hljs-property">port1</span>.<span class="hljs-property">onmessage</span> = <span class="hljs-keyword">async</span> ({ data }) => { 10931cb0ef41Sopenharmony_ci <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>()); 10941cb0ef41Sopenharmony_ci mc1.<span class="hljs-property">port1</span>.<span class="hljs-title function_">close</span>(); 10951cb0ef41Sopenharmony_ci}; 10961cb0ef41Sopenharmony_ci 10971cb0ef41Sopenharmony_cimc2.<span class="hljs-property">port1</span>.<span class="hljs-property">onmessage</span> = <span class="hljs-keyword">async</span> ({ data }) => { 10981cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> <span class="hljs-title function_">delay</span>(<span class="hljs-number">1000</span>); 10991cb0ef41Sopenharmony_ci <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>()); 11001cb0ef41Sopenharmony_ci mc2.<span class="hljs-property">port1</span>.<span class="hljs-title function_">close</span>(); 11011cb0ef41Sopenharmony_ci}; 11021cb0ef41Sopenharmony_ci 11031cb0ef41Sopenharmony_cimc1.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(blob); 11041cb0ef41Sopenharmony_cimc2.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(blob); 11051cb0ef41Sopenharmony_ci 11061cb0ef41Sopenharmony_ci<span class="hljs-comment">// The Blob is still usable after posting.</span> 11071cb0ef41Sopenharmony_ciblob.<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> 11081cb0ef41Sopenharmony_ci</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> 11091cb0ef41Sopenharmony_ci<p>The <code>Buffer</code> class is a global type for dealing with binary data directly. 11101cb0ef41Sopenharmony_ciIt can be constructed in a variety of ways.</p> 11111cb0ef41Sopenharmony_ci<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> 11121cb0ef41Sopenharmony_ci<div class="api_metadata"> 11131cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 11141cb0ef41Sopenharmony_ci<table> 11151cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 11161cb0ef41Sopenharmony_ci<tr><td>v20.0.0</td> 11171cb0ef41Sopenharmony_ci<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> 11181cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 11191cb0ef41Sopenharmony_ci<td><p>Throw ERR_INVALID_ARG_VALUE instead of ERR_INVALID_OPT_VALUE for invalid input arguments.</p></td></tr> 11201cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 11211cb0ef41Sopenharmony_ci<td><p>Attempting to fill a non-zero length buffer with a zero length buffer triggers a thrown exception.</p></td></tr> 11221cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 11231cb0ef41Sopenharmony_ci<td><p>Specifying an invalid string for <code>fill</code> triggers a thrown exception.</p></td></tr> 11241cb0ef41Sopenharmony_ci<tr><td>v8.9.3</td> 11251cb0ef41Sopenharmony_ci<td><p>Specifying an invalid string for <code>fill</code> now results in a zero-filled buffer.</p></td></tr> 11261cb0ef41Sopenharmony_ci<tr><td>v5.10.0</td> 11271cb0ef41Sopenharmony_ci<td><p><span>Added in: v5.10.0</span></p></td></tr> 11281cb0ef41Sopenharmony_ci</tbody></table> 11291cb0ef41Sopenharmony_ci</details> 11301cb0ef41Sopenharmony_ci</div> 11311cb0ef41Sopenharmony_ci<ul> 11321cb0ef41Sopenharmony_ci<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> 11331cb0ef41Sopenharmony_ci<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> 11341cb0ef41Sopenharmony_ciwith. <strong>Default:</strong> <code>0</code>.</li> 11351cb0ef41Sopenharmony_ci<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. 11361cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'utf8'</code>.</li> 11371cb0ef41Sopenharmony_ci</ul> 11381cb0ef41Sopenharmony_ci<p>Allocates a new <code>Buffer</code> of <code>size</code> bytes. If <code>fill</code> is <code>undefined</code>, the 11391cb0ef41Sopenharmony_ci<code>Buffer</code> will be zero-filled.</p> 11401cb0ef41Sopenharmony_ci 11411cb0ef41Sopenharmony_ci<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>; 11421cb0ef41Sopenharmony_ci 11431cb0ef41Sopenharmony_ci<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>); 11441cb0ef41Sopenharmony_ci 11451cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 11461cb0ef41Sopenharmony_ci<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>); 11471cb0ef41Sopenharmony_ci 11481cb0ef41Sopenharmony_ci<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>); 11491cb0ef41Sopenharmony_ci 11501cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 11511cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 00 00 00 00 00></span></code><button class="copy-button">copy</button></pre> 11521cb0ef41Sopenharmony_ci<p>If <code>size</code> is larger than 11531cb0ef41Sopenharmony_ci<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> 11541cb0ef41Sopenharmony_ciis thrown.</p> 11551cb0ef41Sopenharmony_ci<p>If <code>fill</code> is specified, the allocated <code>Buffer</code> will be initialized by calling 11561cb0ef41Sopenharmony_ci<a href="#buffillvalue-offset-end-encoding"><code>buf.fill(fill)</code></a>.</p> 11571cb0ef41Sopenharmony_ci 11581cb0ef41Sopenharmony_ci<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>; 11591cb0ef41Sopenharmony_ci 11601cb0ef41Sopenharmony_ci<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>); 11611cb0ef41Sopenharmony_ci 11621cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 11631cb0ef41Sopenharmony_ci<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>); 11641cb0ef41Sopenharmony_ci 11651cb0ef41Sopenharmony_ci<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>); 11661cb0ef41Sopenharmony_ci 11671cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 11681cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 61 61 61 61 61></span></code><button class="copy-button">copy</button></pre> 11691cb0ef41Sopenharmony_ci<p>If both <code>fill</code> and <code>encoding</code> are specified, the allocated <code>Buffer</code> will be 11701cb0ef41Sopenharmony_ciinitialized by calling <a href="#buffillvalue-offset-end-encoding"><code>buf.fill(fill, encoding)</code></a>.</p> 11711cb0ef41Sopenharmony_ci 11721cb0ef41Sopenharmony_ci<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>; 11731cb0ef41Sopenharmony_ci 11741cb0ef41Sopenharmony_ci<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>); 11751cb0ef41Sopenharmony_ci 11761cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 11771cb0ef41Sopenharmony_ci<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>); 11781cb0ef41Sopenharmony_ci 11791cb0ef41Sopenharmony_ci<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>); 11801cb0ef41Sopenharmony_ci 11811cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 11821cb0ef41Sopenharmony_ci<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> 11831cb0ef41Sopenharmony_ci<p>Calling <a href="#static-method-bufferallocsize-fill-encoding"><code>Buffer.alloc()</code></a> can be measurably slower than the alternative 11841cb0ef41Sopenharmony_ci<a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> but ensures that the newly created <code>Buffer</code> instance 11851cb0ef41Sopenharmony_cicontents will never contain sensitive data from previous allocations, including 11861cb0ef41Sopenharmony_cidata that might not have been allocated for <code>Buffer</code>s.</p> 11871cb0ef41Sopenharmony_ci<p>A <code>TypeError</code> will be thrown if <code>size</code> is not a number.</p> 11881cb0ef41Sopenharmony_ci<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> 11891cb0ef41Sopenharmony_ci<div class="api_metadata"> 11901cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 11911cb0ef41Sopenharmony_ci<table> 11921cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 11931cb0ef41Sopenharmony_ci<tr><td>v20.0.0</td> 11941cb0ef41Sopenharmony_ci<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> 11951cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 11961cb0ef41Sopenharmony_ci<td><p>Throw ERR_INVALID_ARG_VALUE instead of ERR_INVALID_OPT_VALUE for invalid input arguments.</p></td></tr> 11971cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td> 11981cb0ef41Sopenharmony_ci<td><p>Passing a negative <code>size</code> will now throw an error.</p></td></tr> 11991cb0ef41Sopenharmony_ci<tr><td>v5.10.0</td> 12001cb0ef41Sopenharmony_ci<td><p><span>Added in: v5.10.0</span></p></td></tr> 12011cb0ef41Sopenharmony_ci</tbody></table> 12021cb0ef41Sopenharmony_ci</details> 12031cb0ef41Sopenharmony_ci</div> 12041cb0ef41Sopenharmony_ci<ul> 12051cb0ef41Sopenharmony_ci<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> 12061cb0ef41Sopenharmony_ci</ul> 12071cb0ef41Sopenharmony_ci<p>Allocates a new <code>Buffer</code> of <code>size</code> bytes. If <code>size</code> is larger than 12081cb0ef41Sopenharmony_ci<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> 12091cb0ef41Sopenharmony_ciis thrown.</p> 12101cb0ef41Sopenharmony_ci<p>The underlying memory for <code>Buffer</code> instances created in this way is <em>not 12111cb0ef41Sopenharmony_ciinitialized</em>. The contents of the newly created <code>Buffer</code> are unknown and 12121cb0ef41Sopenharmony_ci<em>may contain sensitive data</em>. Use <a href="#static-method-bufferallocsize-fill-encoding"><code>Buffer.alloc()</code></a> instead to initialize 12131cb0ef41Sopenharmony_ci<code>Buffer</code> instances with zeroes.</p> 12141cb0ef41Sopenharmony_ci 12151cb0ef41Sopenharmony_ci<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>; 12161cb0ef41Sopenharmony_ci 12171cb0ef41Sopenharmony_ci<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>); 12181cb0ef41Sopenharmony_ci 12191cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 12201cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints (contents may vary): <Buffer a0 8b 28 3f 01 00 00 00 50 32></span> 12211cb0ef41Sopenharmony_ci 12221cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">fill</span>(<span class="hljs-number">0</span>); 12231cb0ef41Sopenharmony_ci 12241cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 12251cb0ef41Sopenharmony_ci<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>); 12261cb0ef41Sopenharmony_ci 12271cb0ef41Sopenharmony_ci<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>); 12281cb0ef41Sopenharmony_ci 12291cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 12301cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints (contents may vary): <Buffer a0 8b 28 3f 01 00 00 00 50 32></span> 12311cb0ef41Sopenharmony_ci 12321cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">fill</span>(<span class="hljs-number">0</span>); 12331cb0ef41Sopenharmony_ci 12341cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 12351cb0ef41Sopenharmony_ci<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> 12361cb0ef41Sopenharmony_ci<p>A <code>TypeError</code> will be thrown if <code>size</code> is not a number.</p> 12371cb0ef41Sopenharmony_ci<p>The <code>Buffer</code> module pre-allocates an internal <code>Buffer</code> instance of 12381cb0ef41Sopenharmony_cisize <a href="#class-property-bufferpoolsize"><code>Buffer.poolSize</code></a> that is used as a pool for the fast allocation of new 12391cb0ef41Sopenharmony_ci<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>, 12401cb0ef41Sopenharmony_ciand <a href="#static-method-bufferconcatlist-totallength"><code>Buffer.concat()</code></a> only when <code>size</code> is less than or equal to 12411cb0ef41Sopenharmony_ci<code>Buffer.poolSize >> 1</code> (floor of <a href="#class-property-bufferpoolsize"><code>Buffer.poolSize</code></a> divided by two).</p> 12421cb0ef41Sopenharmony_ci<p>Use of this pre-allocated internal memory pool is a key difference between 12431cb0ef41Sopenharmony_cicalling <code>Buffer.alloc(size, fill)</code> vs. <code>Buffer.allocUnsafe(size).fill(fill)</code>. 12441cb0ef41Sopenharmony_ciSpecifically, <code>Buffer.alloc(size, fill)</code> will <em>never</em> use the internal <code>Buffer</code> 12451cb0ef41Sopenharmony_cipool, while <code>Buffer.allocUnsafe(size).fill(fill)</code> <em>will</em> use the internal 12461cb0ef41Sopenharmony_ci<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 12471cb0ef41Sopenharmony_cidifference is subtle but can be important when an application requires the 12481cb0ef41Sopenharmony_ciadditional performance that <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> provides.</p> 12491cb0ef41Sopenharmony_ci<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> 12501cb0ef41Sopenharmony_ci<div class="api_metadata"> 12511cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 12521cb0ef41Sopenharmony_ci<table> 12531cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 12541cb0ef41Sopenharmony_ci<tr><td>v20.0.0</td> 12551cb0ef41Sopenharmony_ci<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> 12561cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 12571cb0ef41Sopenharmony_ci<td><p>Throw ERR_INVALID_ARG_VALUE instead of ERR_INVALID_OPT_VALUE for invalid input arguments.</p></td></tr> 12581cb0ef41Sopenharmony_ci<tr><td>v5.12.0</td> 12591cb0ef41Sopenharmony_ci<td><p><span>Added in: v5.12.0</span></p></td></tr> 12601cb0ef41Sopenharmony_ci</tbody></table> 12611cb0ef41Sopenharmony_ci</details> 12621cb0ef41Sopenharmony_ci</div> 12631cb0ef41Sopenharmony_ci<ul> 12641cb0ef41Sopenharmony_ci<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> 12651cb0ef41Sopenharmony_ci</ul> 12661cb0ef41Sopenharmony_ci<p>Allocates a new <code>Buffer</code> of <code>size</code> bytes. If <code>size</code> is larger than 12671cb0ef41Sopenharmony_ci<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> 12681cb0ef41Sopenharmony_ciis thrown. A zero-length <code>Buffer</code> is created if <code>size</code> is 0.</p> 12691cb0ef41Sopenharmony_ci<p>The underlying memory for <code>Buffer</code> instances created in this way is <em>not 12701cb0ef41Sopenharmony_ciinitialized</em>. The contents of the newly created <code>Buffer</code> are unknown and 12711cb0ef41Sopenharmony_ci<em>may contain sensitive data</em>. Use <a href="#buffillvalue-offset-end-encoding"><code>buf.fill(0)</code></a> to initialize 12721cb0ef41Sopenharmony_cisuch <code>Buffer</code> instances with zeroes.</p> 12731cb0ef41Sopenharmony_ci<p>When using <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> to allocate new <code>Buffer</code> instances, 12741cb0ef41Sopenharmony_ciallocations under 4 KiB are sliced from a single pre-allocated <code>Buffer</code>. This 12751cb0ef41Sopenharmony_ciallows applications to avoid the garbage collection overhead of creating many 12761cb0ef41Sopenharmony_ciindividually allocated <code>Buffer</code> instances. This approach improves both 12771cb0ef41Sopenharmony_ciperformance and memory usage by eliminating the need to track and clean up as 12781cb0ef41Sopenharmony_cimany individual <code>ArrayBuffer</code> objects.</p> 12791cb0ef41Sopenharmony_ci<p>However, in the case where a developer may need to retain a small chunk of 12801cb0ef41Sopenharmony_cimemory from a pool for an indeterminate amount of time, it may be appropriate 12811cb0ef41Sopenharmony_cito create an un-pooled <code>Buffer</code> instance using <code>Buffer.allocUnsafeSlow()</code> and 12821cb0ef41Sopenharmony_cithen copying out the relevant bits.</p> 12831cb0ef41Sopenharmony_ci 12841cb0ef41Sopenharmony_ci<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>; 12851cb0ef41Sopenharmony_ci 12861cb0ef41Sopenharmony_ci<span class="hljs-comment">// Need to keep around a few small chunks of memory.</span> 12871cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> store = []; 12881cb0ef41Sopenharmony_ci 12891cb0ef41Sopenharmony_cisocket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 12901cb0ef41Sopenharmony_ci <span class="hljs-keyword">let</span> data; 12911cb0ef41Sopenharmony_ci <span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (data = readable.<span class="hljs-title function_">read</span>())) { 12921cb0ef41Sopenharmony_ci <span class="hljs-comment">// Allocate for retained data.</span> 12931cb0ef41Sopenharmony_ci <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>); 12941cb0ef41Sopenharmony_ci 12951cb0ef41Sopenharmony_ci <span class="hljs-comment">// Copy the data into the new allocation.</span> 12961cb0ef41Sopenharmony_ci 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>); 12971cb0ef41Sopenharmony_ci 12981cb0ef41Sopenharmony_ci store.<span class="hljs-title function_">push</span>(sb); 12991cb0ef41Sopenharmony_ci } 13001cb0ef41Sopenharmony_ci});</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>); 13011cb0ef41Sopenharmony_ci 13021cb0ef41Sopenharmony_ci<span class="hljs-comment">// Need to keep around a few small chunks of memory.</span> 13031cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> store = []; 13041cb0ef41Sopenharmony_ci 13051cb0ef41Sopenharmony_cisocket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 13061cb0ef41Sopenharmony_ci <span class="hljs-keyword">let</span> data; 13071cb0ef41Sopenharmony_ci <span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (data = readable.<span class="hljs-title function_">read</span>())) { 13081cb0ef41Sopenharmony_ci <span class="hljs-comment">// Allocate for retained data.</span> 13091cb0ef41Sopenharmony_ci <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>); 13101cb0ef41Sopenharmony_ci 13111cb0ef41Sopenharmony_ci <span class="hljs-comment">// Copy the data into the new allocation.</span> 13121cb0ef41Sopenharmony_ci 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>); 13131cb0ef41Sopenharmony_ci 13141cb0ef41Sopenharmony_ci store.<span class="hljs-title function_">push</span>(sb); 13151cb0ef41Sopenharmony_ci } 13161cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 13171cb0ef41Sopenharmony_ci<p>A <code>TypeError</code> will be thrown if <code>size</code> is not a number.</p> 13181cb0ef41Sopenharmony_ci<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> 13191cb0ef41Sopenharmony_ci<div class="api_metadata"> 13201cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 13211cb0ef41Sopenharmony_ci<table> 13221cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 13231cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td> 13241cb0ef41Sopenharmony_ci<td><p>Passing invalid input will now throw an error.</p></td></tr> 13251cb0ef41Sopenharmony_ci<tr><td>v5.10.0</td> 13261cb0ef41Sopenharmony_ci<td><p>The <code>string</code> parameter can now be any <code>TypedArray</code>, <code>DataView</code> or <code>ArrayBuffer</code>.</p></td></tr> 13271cb0ef41Sopenharmony_ci<tr><td>v0.1.90</td> 13281cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.90</span></p></td></tr> 13291cb0ef41Sopenharmony_ci</tbody></table> 13301cb0ef41Sopenharmony_ci</details> 13311cb0ef41Sopenharmony_ci</div> 13321cb0ef41Sopenharmony_ci<ul> 13331cb0ef41Sopenharmony_ci<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 13341cb0ef41Sopenharmony_civalue to calculate the length of.</li> 13351cb0ef41Sopenharmony_ci<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. 13361cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'utf8'</code>.</li> 13371cb0ef41Sopenharmony_ci<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> 13381cb0ef41Sopenharmony_ci</ul> 13391cb0ef41Sopenharmony_ci<p>Returns the byte length of a string when encoded using <code>encoding</code>. 13401cb0ef41Sopenharmony_ciThis 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 13411cb0ef41Sopenharmony_cifor the encoding that is used to convert the string into bytes.</p> 13421cb0ef41Sopenharmony_ci<p>For <code>'base64'</code>, <code>'base64url'</code>, and <code>'hex'</code>, this function assumes valid input. 13431cb0ef41Sopenharmony_ciFor strings that contain non-base64/hex-encoded data (e.g. whitespace), the 13441cb0ef41Sopenharmony_cireturn value might be greater than the length of a <code>Buffer</code> created from the 13451cb0ef41Sopenharmony_cistring.</p> 13461cb0ef41Sopenharmony_ci 13471cb0ef41Sopenharmony_ci<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>; 13481cb0ef41Sopenharmony_ci 13491cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> str = <span class="hljs-string">'\u00bd + \u00bc = \u00be'</span>; 13501cb0ef41Sopenharmony_ci 13511cb0ef41Sopenharmony_ci<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> + 13521cb0ef41Sopenharmony_ci <span class="hljs-string">`<span class="hljs-subst">${Buffer.byteLength(str, <span class="hljs-string">'utf8'</span>)}</span> bytes`</span>); 13531cb0ef41Sopenharmony_ci<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>); 13541cb0ef41Sopenharmony_ci 13551cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> str = <span class="hljs-string">'\u00bd + \u00bc = \u00be'</span>; 13561cb0ef41Sopenharmony_ci 13571cb0ef41Sopenharmony_ci<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> + 13581cb0ef41Sopenharmony_ci <span class="hljs-string">`<span class="hljs-subst">${Buffer.byteLength(str, <span class="hljs-string">'utf8'</span>)}</span> bytes`</span>); 13591cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: ½ + ¼ = ¾: 9 characters, 12 bytes</span></code><button class="copy-button">copy</button></pre> 13601cb0ef41Sopenharmony_ci<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>/ 13611cb0ef41Sopenharmony_ci<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> 13621cb0ef41Sopenharmony_ciis returned.</p> 13631cb0ef41Sopenharmony_ci<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> 13641cb0ef41Sopenharmony_ci<div class="api_metadata"> 13651cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 13661cb0ef41Sopenharmony_ci<table> 13671cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 13681cb0ef41Sopenharmony_ci<tr><td>v8.0.0</td> 13691cb0ef41Sopenharmony_ci<td><p>The arguments can now be <code>Uint8Array</code>s.</p></td></tr> 13701cb0ef41Sopenharmony_ci<tr><td>v0.11.13</td> 13711cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.13</span></p></td></tr> 13721cb0ef41Sopenharmony_ci</tbody></table> 13731cb0ef41Sopenharmony_ci</details> 13741cb0ef41Sopenharmony_ci</div> 13751cb0ef41Sopenharmony_ci<ul> 13761cb0ef41Sopenharmony_ci<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> 13771cb0ef41Sopenharmony_ci<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> 13781cb0ef41Sopenharmony_ci<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 13791cb0ef41Sopenharmony_cicomparison. See <a href="#bufcomparetarget-targetstart-targetend-sourcestart-sourceend"><code>buf.compare()</code></a> for details.</li> 13801cb0ef41Sopenharmony_ci</ul> 13811cb0ef41Sopenharmony_ci<p>Compares <code>buf1</code> to <code>buf2</code>, typically for the purpose of sorting arrays of 13821cb0ef41Sopenharmony_ci<code>Buffer</code> instances. This is equivalent to calling 13831cb0ef41Sopenharmony_ci<a href="#bufcomparetarget-targetstart-targetend-sourcestart-sourceend"><code>buf1.compare(buf2)</code></a>.</p> 13841cb0ef41Sopenharmony_ci 13851cb0ef41Sopenharmony_ci<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>; 13861cb0ef41Sopenharmony_ci 13871cb0ef41Sopenharmony_ci<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>); 13881cb0ef41Sopenharmony_ci<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>); 13891cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> arr = [buf1, buf2]; 13901cb0ef41Sopenharmony_ci 13911cb0ef41Sopenharmony_ci<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>)); 13921cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: [ <Buffer 30 31 32 33>, <Buffer 31 32 33 34> ]</span> 13931cb0ef41Sopenharmony_ci<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>); 13941cb0ef41Sopenharmony_ci 13951cb0ef41Sopenharmony_ci<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>); 13961cb0ef41Sopenharmony_ci<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>); 13971cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> arr = [buf1, buf2]; 13981cb0ef41Sopenharmony_ci 13991cb0ef41Sopenharmony_ci<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>)); 14001cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: [ <Buffer 30 31 32 33>, <Buffer 31 32 33 34> ]</span> 14011cb0ef41Sopenharmony_ci<span class="hljs-comment">// (This result is equal to: [buf2, buf1].)</span></code><button class="copy-button">copy</button></pre> 14021cb0ef41Sopenharmony_ci<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> 14031cb0ef41Sopenharmony_ci<div class="api_metadata"> 14041cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 14051cb0ef41Sopenharmony_ci<table> 14061cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 14071cb0ef41Sopenharmony_ci<tr><td>v8.0.0</td> 14081cb0ef41Sopenharmony_ci<td><p>The elements of <code>list</code> can now be <code>Uint8Array</code>s.</p></td></tr> 14091cb0ef41Sopenharmony_ci<tr><td>v0.7.11</td> 14101cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.7.11</span></p></td></tr> 14111cb0ef41Sopenharmony_ci</tbody></table> 14121cb0ef41Sopenharmony_ci</details> 14131cb0ef41Sopenharmony_ci</div> 14141cb0ef41Sopenharmony_ci<ul> 14151cb0ef41Sopenharmony_ci<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> 14161cb0ef41Sopenharmony_ciinstances to concatenate.</li> 14171cb0ef41Sopenharmony_ci<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> 14181cb0ef41Sopenharmony_ciwhen concatenated.</li> 14191cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 14201cb0ef41Sopenharmony_ci</ul> 14211cb0ef41Sopenharmony_ci<p>Returns a new <code>Buffer</code> which is the result of concatenating all the <code>Buffer</code> 14221cb0ef41Sopenharmony_ciinstances in the <code>list</code> together.</p> 14231cb0ef41Sopenharmony_ci<p>If the list has no items, or if the <code>totalLength</code> is 0, then a new zero-length 14241cb0ef41Sopenharmony_ci<code>Buffer</code> is returned.</p> 14251cb0ef41Sopenharmony_ci<p>If <code>totalLength</code> is not provided, it is calculated from the <code>Buffer</code> instances 14261cb0ef41Sopenharmony_ciin <code>list</code> by adding their lengths.</p> 14271cb0ef41Sopenharmony_ci<p>If <code>totalLength</code> is provided, it is coerced to an unsigned integer. If the 14281cb0ef41Sopenharmony_cicombined length of the <code>Buffer</code>s in <code>list</code> exceeds <code>totalLength</code>, the result is 14291cb0ef41Sopenharmony_citruncated to <code>totalLength</code>.</p> 14301cb0ef41Sopenharmony_ci 14311cb0ef41Sopenharmony_ci<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>; 14321cb0ef41Sopenharmony_ci 14331cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create a single `Buffer` from a list of three `Buffer` instances.</span> 14341cb0ef41Sopenharmony_ci 14351cb0ef41Sopenharmony_ci<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>); 14361cb0ef41Sopenharmony_ci<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>); 14371cb0ef41Sopenharmony_ci<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>); 14381cb0ef41Sopenharmony_ci<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>; 14391cb0ef41Sopenharmony_ci 14401cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(totalLength); 14411cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 42</span> 14421cb0ef41Sopenharmony_ci 14431cb0ef41Sopenharmony_ci<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); 14441cb0ef41Sopenharmony_ci 14451cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(bufA); 14461cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 00 00 00 00 ...></span> 14471cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(bufA.<span class="hljs-property">length</span>); 14481cb0ef41Sopenharmony_ci<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>); 14491cb0ef41Sopenharmony_ci 14501cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create a single `Buffer` from a list of three `Buffer` instances.</span> 14511cb0ef41Sopenharmony_ci 14521cb0ef41Sopenharmony_ci<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>); 14531cb0ef41Sopenharmony_ci<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>); 14541cb0ef41Sopenharmony_ci<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>); 14551cb0ef41Sopenharmony_ci<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>; 14561cb0ef41Sopenharmony_ci 14571cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(totalLength); 14581cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 42</span> 14591cb0ef41Sopenharmony_ci 14601cb0ef41Sopenharmony_ci<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); 14611cb0ef41Sopenharmony_ci 14621cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(bufA); 14631cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 00 00 00 00 ...></span> 14641cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(bufA.<span class="hljs-property">length</span>); 14651cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 42</span></code><button class="copy-button">copy</button></pre> 14661cb0ef41Sopenharmony_ci<p><code>Buffer.concat()</code> may also use the internal <code>Buffer</code> pool like 14671cb0ef41Sopenharmony_ci<a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> does.</p> 14681cb0ef41Sopenharmony_ci<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> 14691cb0ef41Sopenharmony_ci<div class="api_metadata"> 14701cb0ef41Sopenharmony_ci<span>Added in: v18.16.0</span> 14711cb0ef41Sopenharmony_ci</div> 14721cb0ef41Sopenharmony_ci<ul> 14731cb0ef41Sopenharmony_ci<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> 14741cb0ef41Sopenharmony_ci<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> 14751cb0ef41Sopenharmony_ci<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. 14761cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>view.length - offset</code>.</li> 14771cb0ef41Sopenharmony_ci</ul> 14781cb0ef41Sopenharmony_ci<p>Copies the underlying memory of <code>view</code> into a new <code>Buffer</code>.</p> 14791cb0ef41Sopenharmony_ci<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>]); 14801cb0ef41Sopenharmony_ci<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>); 14811cb0ef41Sopenharmony_ciu16[<span class="hljs-number">1</span>] = <span class="hljs-number">0</span>; 14821cb0ef41Sopenharmony_ci<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> 14831cb0ef41Sopenharmony_ci<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> 14841cb0ef41Sopenharmony_ci<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> 14851cb0ef41Sopenharmony_ci<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> 14861cb0ef41Sopenharmony_ci<div class="api_metadata"> 14871cb0ef41Sopenharmony_ci<span>Added in: v5.10.0</span> 14881cb0ef41Sopenharmony_ci</div> 14891cb0ef41Sopenharmony_ci<ul> 14901cb0ef41Sopenharmony_ci<li><code>array</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer[]></a></li> 14911cb0ef41Sopenharmony_ci</ul> 14921cb0ef41Sopenharmony_ci<p>Allocates a new <code>Buffer</code> using an <code>array</code> of bytes in the range <code>0</code> – <code>255</code>. 14931cb0ef41Sopenharmony_ciArray entries outside that range will be truncated to fit into it.</p> 14941cb0ef41Sopenharmony_ci 14951cb0ef41Sopenharmony_ci<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>; 14961cb0ef41Sopenharmony_ci 14971cb0ef41Sopenharmony_ci<span class="hljs-comment">// Creates a new Buffer containing the UTF-8 bytes of the string 'buffer'.</span> 14981cb0ef41Sopenharmony_ci<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>); 14991cb0ef41Sopenharmony_ci 15001cb0ef41Sopenharmony_ci<span class="hljs-comment">// Creates a new Buffer containing the UTF-8 bytes of the string 'buffer'.</span> 15011cb0ef41Sopenharmony_ci<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> 15021cb0ef41Sopenharmony_ci<p>If <code>array</code> is an <code>Array</code>-like object (that is, one with a <code>length</code> property of 15031cb0ef41Sopenharmony_citype <code>number</code>), it is treated as if it is an array, unless it is a <code>Buffer</code> or 15041cb0ef41Sopenharmony_cia <code>Uint8Array</code>. This means all other <code>TypedArray</code> variants get treated as an 15051cb0ef41Sopenharmony_ci<code>Array</code>. To create a <code>Buffer</code> from the bytes backing a <code>TypedArray</code>, use 15061cb0ef41Sopenharmony_ci<a href="#static-method-buffercopybytesfromview-offset-length"><code>Buffer.copyBytesFrom()</code></a>.</p> 15071cb0ef41Sopenharmony_ci<p>A <code>TypeError</code> will be thrown if <code>array</code> is not an <code>Array</code> or another type 15081cb0ef41Sopenharmony_ciappropriate for <code>Buffer.from()</code> variants.</p> 15091cb0ef41Sopenharmony_ci<p><code>Buffer.from(array)</code> and <a href="#static-method-bufferfromstring-encoding"><code>Buffer.from(string)</code></a> may also use the internal 15101cb0ef41Sopenharmony_ci<code>Buffer</code> pool like <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> does.</p> 15111cb0ef41Sopenharmony_ci<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> 15121cb0ef41Sopenharmony_ci<div class="api_metadata"> 15131cb0ef41Sopenharmony_ci<span>Added in: v5.10.0</span> 15141cb0ef41Sopenharmony_ci</div> 15151cb0ef41Sopenharmony_ci<ul> 15161cb0ef41Sopenharmony_ci<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>, 15171cb0ef41Sopenharmony_ci<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 15181cb0ef41Sopenharmony_ci<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>.</li> 15191cb0ef41Sopenharmony_ci<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> 15201cb0ef41Sopenharmony_ci<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. 15211cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>arrayBuffer.byteLength - byteOffset</code>.</li> 15221cb0ef41Sopenharmony_ci</ul> 15231cb0ef41Sopenharmony_ci<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 15241cb0ef41Sopenharmony_cimemory. For example, when passed a reference to the <code>.buffer</code> property of a 15251cb0ef41Sopenharmony_ci<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 15261cb0ef41Sopenharmony_ciallocated 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> 15271cb0ef41Sopenharmony_ci 15281cb0ef41Sopenharmony_ci<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>; 15291cb0ef41Sopenharmony_ci 15301cb0ef41Sopenharmony_ci<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>); 15311cb0ef41Sopenharmony_ci 15321cb0ef41Sopenharmony_ciarr[<span class="hljs-number">0</span>] = <span class="hljs-number">5000</span>; 15331cb0ef41Sopenharmony_ciarr[<span class="hljs-number">1</span>] = <span class="hljs-number">4000</span>; 15341cb0ef41Sopenharmony_ci 15351cb0ef41Sopenharmony_ci<span class="hljs-comment">// Shares memory with `arr`.</span> 15361cb0ef41Sopenharmony_ci<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>); 15371cb0ef41Sopenharmony_ci 15381cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 15391cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 88 13 a0 0f></span> 15401cb0ef41Sopenharmony_ci 15411cb0ef41Sopenharmony_ci<span class="hljs-comment">// Changing the original Uint16Array changes the Buffer also.</span> 15421cb0ef41Sopenharmony_ciarr[<span class="hljs-number">1</span>] = <span class="hljs-number">6000</span>; 15431cb0ef41Sopenharmony_ci 15441cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 15451cb0ef41Sopenharmony_ci<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>); 15461cb0ef41Sopenharmony_ci 15471cb0ef41Sopenharmony_ci<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>); 15481cb0ef41Sopenharmony_ci 15491cb0ef41Sopenharmony_ciarr[<span class="hljs-number">0</span>] = <span class="hljs-number">5000</span>; 15501cb0ef41Sopenharmony_ciarr[<span class="hljs-number">1</span>] = <span class="hljs-number">4000</span>; 15511cb0ef41Sopenharmony_ci 15521cb0ef41Sopenharmony_ci<span class="hljs-comment">// Shares memory with `arr`.</span> 15531cb0ef41Sopenharmony_ci<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>); 15541cb0ef41Sopenharmony_ci 15551cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 15561cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 88 13 a0 0f></span> 15571cb0ef41Sopenharmony_ci 15581cb0ef41Sopenharmony_ci<span class="hljs-comment">// Changing the original Uint16Array changes the Buffer also.</span> 15591cb0ef41Sopenharmony_ciarr[<span class="hljs-number">1</span>] = <span class="hljs-number">6000</span>; 15601cb0ef41Sopenharmony_ci 15611cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 15621cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 88 13 70 17></span></code><button class="copy-button">copy</button></pre> 15631cb0ef41Sopenharmony_ci<p>The optional <code>byteOffset</code> and <code>length</code> arguments specify a memory range within 15641cb0ef41Sopenharmony_cithe <code>arrayBuffer</code> that will be shared by the <code>Buffer</code>.</p> 15651cb0ef41Sopenharmony_ci 15661cb0ef41Sopenharmony_ci<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>; 15671cb0ef41Sopenharmony_ci 15681cb0ef41Sopenharmony_ci<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>); 15691cb0ef41Sopenharmony_ci<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>); 15701cb0ef41Sopenharmony_ci 15711cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>); 15721cb0ef41Sopenharmony_ci<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>); 15731cb0ef41Sopenharmony_ci 15741cb0ef41Sopenharmony_ci<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>); 15751cb0ef41Sopenharmony_ci<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>); 15761cb0ef41Sopenharmony_ci 15771cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>); 15781cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 2</span></code><button class="copy-button">copy</button></pre> 15791cb0ef41Sopenharmony_ci<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 15801cb0ef41Sopenharmony_ci<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> 15811cb0ef41Sopenharmony_civariants.</p> 15821cb0ef41Sopenharmony_ci<p>It is important to remember that a backing <code>ArrayBuffer</code> can cover a range 15831cb0ef41Sopenharmony_ciof memory that extends beyond the bounds of a <code>TypedArray</code> view. A new 15841cb0ef41Sopenharmony_ci<code>Buffer</code> created using the <code>buffer</code> property of a <code>TypedArray</code> may extend 15851cb0ef41Sopenharmony_cibeyond the range of the <code>TypedArray</code>:</p> 15861cb0ef41Sopenharmony_ci 15871cb0ef41Sopenharmony_ci<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>; 15881cb0ef41Sopenharmony_ci 15891cb0ef41Sopenharmony_ci<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> 15901cb0ef41Sopenharmony_ci<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> 15911cb0ef41Sopenharmony_ci<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> 15921cb0ef41Sopenharmony_ci 15931cb0ef41Sopenharmony_ci<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>); 15941cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 15951cb0ef41Sopenharmony_ci<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>); 15961cb0ef41Sopenharmony_ci 15971cb0ef41Sopenharmony_ci<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> 15981cb0ef41Sopenharmony_ci<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> 15991cb0ef41Sopenharmony_ci<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> 16001cb0ef41Sopenharmony_ci 16011cb0ef41Sopenharmony_ci<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>); 16021cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 16031cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 63 64 65 66></span></code><button class="copy-button">copy</button></pre> 16041cb0ef41Sopenharmony_ci<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> 16051cb0ef41Sopenharmony_ci<div class="api_metadata"> 16061cb0ef41Sopenharmony_ci<span>Added in: v5.10.0</span> 16071cb0ef41Sopenharmony_ci</div> 16081cb0ef41Sopenharmony_ci<ul> 16091cb0ef41Sopenharmony_ci<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 16101cb0ef41Sopenharmony_ciwhich to copy data.</li> 16111cb0ef41Sopenharmony_ci</ul> 16121cb0ef41Sopenharmony_ci<p>Copies the passed <code>buffer</code> data onto a new <code>Buffer</code> instance.</p> 16131cb0ef41Sopenharmony_ci 16141cb0ef41Sopenharmony_ci<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>; 16151cb0ef41Sopenharmony_ci 16161cb0ef41Sopenharmony_ci<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>); 16171cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(buf1); 16181cb0ef41Sopenharmony_ci 16191cb0ef41Sopenharmony_cibuf1[<span class="hljs-number">0</span>] = <span class="hljs-number">0x61</span>; 16201cb0ef41Sopenharmony_ci 16211cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>()); 16221cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: auffer</span> 16231cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>()); 16241cb0ef41Sopenharmony_ci<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>); 16251cb0ef41Sopenharmony_ci 16261cb0ef41Sopenharmony_ci<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>); 16271cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(buf1); 16281cb0ef41Sopenharmony_ci 16291cb0ef41Sopenharmony_cibuf1[<span class="hljs-number">0</span>] = <span class="hljs-number">0x61</span>; 16301cb0ef41Sopenharmony_ci 16311cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>()); 16321cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: auffer</span> 16331cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>()); 16341cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: buffer</span></code><button class="copy-button">copy</button></pre> 16351cb0ef41Sopenharmony_ci<p>A <code>TypeError</code> will be thrown if <code>buffer</code> is not a <code>Buffer</code> or another type 16361cb0ef41Sopenharmony_ciappropriate for <code>Buffer.from()</code> variants.</p> 16371cb0ef41Sopenharmony_ci<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> 16381cb0ef41Sopenharmony_ci<div class="api_metadata"> 16391cb0ef41Sopenharmony_ci<span>Added in: v8.2.0</span> 16401cb0ef41Sopenharmony_ci</div> 16411cb0ef41Sopenharmony_ci<ul> 16421cb0ef41Sopenharmony_ci<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> 16431cb0ef41Sopenharmony_ci<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> 16441cb0ef41Sopenharmony_ci<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> 16451cb0ef41Sopenharmony_ci</ul> 16461cb0ef41Sopenharmony_ci<p>For objects whose <code>valueOf()</code> function returns a value not strictly equal to 16471cb0ef41Sopenharmony_ci<code>object</code>, returns <code>Buffer.from(object.valueOf(), offsetOrEncoding, length)</code>.</p> 16481cb0ef41Sopenharmony_ci 16491cb0ef41Sopenharmony_ci<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>; 16501cb0ef41Sopenharmony_ci 16511cb0ef41Sopenharmony_ci<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>)); 16521cb0ef41Sopenharmony_ci<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>); 16531cb0ef41Sopenharmony_ci 16541cb0ef41Sopenharmony_ci<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>)); 16551cb0ef41Sopenharmony_ci<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> 16561cb0ef41Sopenharmony_ci<p>For objects that support <code>Symbol.toPrimitive</code>, returns 16571cb0ef41Sopenharmony_ci<code>Buffer.from(object[Symbol.toPrimitive]('string'), offsetOrEncoding)</code>.</p> 16581cb0ef41Sopenharmony_ci 16591cb0ef41Sopenharmony_ci<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>; 16601cb0ef41Sopenharmony_ci 16611cb0ef41Sopenharmony_ci<span class="hljs-keyword">class</span> <span class="hljs-title class_">Foo</span> { 16621cb0ef41Sopenharmony_ci [<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">toPrimitive</span>]() { 16631cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> <span class="hljs-string">'this is a test'</span>; 16641cb0ef41Sopenharmony_ci } 16651cb0ef41Sopenharmony_ci} 16661cb0ef41Sopenharmony_ci 16671cb0ef41Sopenharmony_ci<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>); 16681cb0ef41Sopenharmony_ci<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>); 16691cb0ef41Sopenharmony_ci 16701cb0ef41Sopenharmony_ci<span class="hljs-keyword">class</span> <span class="hljs-title class_">Foo</span> { 16711cb0ef41Sopenharmony_ci [<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">toPrimitive</span>]() { 16721cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> <span class="hljs-string">'this is a test'</span>; 16731cb0ef41Sopenharmony_ci } 16741cb0ef41Sopenharmony_ci} 16751cb0ef41Sopenharmony_ci 16761cb0ef41Sopenharmony_ci<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>); 16771cb0ef41Sopenharmony_ci<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> 16781cb0ef41Sopenharmony_ci<p>A <code>TypeError</code> will be thrown if <code>object</code> does not have the mentioned methods or 16791cb0ef41Sopenharmony_ciis not of another type appropriate for <code>Buffer.from()</code> variants.</p> 16801cb0ef41Sopenharmony_ci<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> 16811cb0ef41Sopenharmony_ci<div class="api_metadata"> 16821cb0ef41Sopenharmony_ci<span>Added in: v5.10.0</span> 16831cb0ef41Sopenharmony_ci</div> 16841cb0ef41Sopenharmony_ci<ul> 16851cb0ef41Sopenharmony_ci<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> 16861cb0ef41Sopenharmony_ci<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> 16871cb0ef41Sopenharmony_ci</ul> 16881cb0ef41Sopenharmony_ci<p>Creates a new <code>Buffer</code> containing <code>string</code>. The <code>encoding</code> parameter identifies 16891cb0ef41Sopenharmony_cithe character encoding to be used when converting <code>string</code> into bytes.</p> 16901cb0ef41Sopenharmony_ci 16911cb0ef41Sopenharmony_ci<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>; 16921cb0ef41Sopenharmony_ci 16931cb0ef41Sopenharmony_ci<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>); 16941cb0ef41Sopenharmony_ci<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>); 16951cb0ef41Sopenharmony_ci 16961cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>()); 16971cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: this is a tést</span> 16981cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>()); 16991cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: this is a tést</span> 17001cb0ef41Sopenharmony_ci<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>)); 17011cb0ef41Sopenharmony_ci<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>); 17021cb0ef41Sopenharmony_ci 17031cb0ef41Sopenharmony_ci<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>); 17041cb0ef41Sopenharmony_ci<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>); 17051cb0ef41Sopenharmony_ci 17061cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>()); 17071cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: this is a tést</span> 17081cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>()); 17091cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: this is a tést</span> 17101cb0ef41Sopenharmony_ci<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>)); 17111cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: this is a tést</span></code><button class="copy-button">copy</button></pre> 17121cb0ef41Sopenharmony_ci<p>A <code>TypeError</code> will be thrown if <code>string</code> is not a string or another type 17131cb0ef41Sopenharmony_ciappropriate for <code>Buffer.from()</code> variants.</p> 17141cb0ef41Sopenharmony_ci<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> 17151cb0ef41Sopenharmony_ci<div class="api_metadata"> 17161cb0ef41Sopenharmony_ci<span>Added in: v0.1.101</span> 17171cb0ef41Sopenharmony_ci</div> 17181cb0ef41Sopenharmony_ci<ul> 17191cb0ef41Sopenharmony_ci<li><code>obj</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 17201cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 17211cb0ef41Sopenharmony_ci</ul> 17221cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if <code>obj</code> is a <code>Buffer</code>, <code>false</code> otherwise.</p> 17231cb0ef41Sopenharmony_ci 17241cb0ef41Sopenharmony_ci<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>; 17251cb0ef41Sopenharmony_ci 17261cb0ef41Sopenharmony_ci<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> 17271cb0ef41Sopenharmony_ci<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> 17281cb0ef41Sopenharmony_ci<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> 17291cb0ef41Sopenharmony_ci<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>([]); <span class="hljs-comment">// false</span> 17301cb0ef41Sopenharmony_ci<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>); 17311cb0ef41Sopenharmony_ci 17321cb0ef41Sopenharmony_ci<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> 17331cb0ef41Sopenharmony_ci<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> 17341cb0ef41Sopenharmony_ci<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> 17351cb0ef41Sopenharmony_ci<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>([]); <span class="hljs-comment">// false</span> 17361cb0ef41Sopenharmony_ci<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> 17371cb0ef41Sopenharmony_ci<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> 17381cb0ef41Sopenharmony_ci<div class="api_metadata"> 17391cb0ef41Sopenharmony_ci<span>Added in: v0.9.1</span> 17401cb0ef41Sopenharmony_ci</div> 17411cb0ef41Sopenharmony_ci<ul> 17421cb0ef41Sopenharmony_ci<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> 17431cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 17441cb0ef41Sopenharmony_ci</ul> 17451cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if <code>encoding</code> is the name of a supported character encoding, 17461cb0ef41Sopenharmony_cior <code>false</code> otherwise.</p> 17471cb0ef41Sopenharmony_ci 17481cb0ef41Sopenharmony_ci<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>; 17491cb0ef41Sopenharmony_ci 17501cb0ef41Sopenharmony_ci<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>)); 17511cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: true</span> 17521cb0ef41Sopenharmony_ci 17531cb0ef41Sopenharmony_ci<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>)); 17541cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: true</span> 17551cb0ef41Sopenharmony_ci 17561cb0ef41Sopenharmony_ci<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>)); 17571cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: false</span> 17581cb0ef41Sopenharmony_ci 17591cb0ef41Sopenharmony_ci<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>)); 17601cb0ef41Sopenharmony_ci<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>); 17611cb0ef41Sopenharmony_ci 17621cb0ef41Sopenharmony_ci<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>)); 17631cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: true</span> 17641cb0ef41Sopenharmony_ci 17651cb0ef41Sopenharmony_ci<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>)); 17661cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: true</span> 17671cb0ef41Sopenharmony_ci 17681cb0ef41Sopenharmony_ci<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>)); 17691cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: false</span> 17701cb0ef41Sopenharmony_ci 17711cb0ef41Sopenharmony_ci<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>)); 17721cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: false</span></code><button class="copy-button">copy</button></pre> 17731cb0ef41Sopenharmony_ci<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> 17741cb0ef41Sopenharmony_ci<div class="api_metadata"> 17751cb0ef41Sopenharmony_ci<span>Added in: v0.11.3</span> 17761cb0ef41Sopenharmony_ci</div> 17771cb0ef41Sopenharmony_ci<ul> 17781cb0ef41Sopenharmony_ci<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> 17791cb0ef41Sopenharmony_ci</ul> 17801cb0ef41Sopenharmony_ci<p>This is the size (in bytes) of pre-allocated internal <code>Buffer</code> instances used 17811cb0ef41Sopenharmony_cifor pooling. This value may be modified.</p> 17821cb0ef41Sopenharmony_ci<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> 17831cb0ef41Sopenharmony_ci<ul> 17841cb0ef41Sopenharmony_ci<li><code>index</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 17851cb0ef41Sopenharmony_ci</ul> 17861cb0ef41Sopenharmony_ci<p>The index operator <code>[index]</code> can be used to get and set the octet at position 17871cb0ef41Sopenharmony_ci<code>index</code> in <code>buf</code>. The values refer to individual bytes, so the legal value 17881cb0ef41Sopenharmony_cirange is between <code>0x00</code> and <code>0xFF</code> (hex) or <code>0</code> and <code>255</code> (decimal).</p> 17891cb0ef41Sopenharmony_ci<p>This operator is inherited from <code>Uint8Array</code>, so its behavior on out-of-bounds 17901cb0ef41Sopenharmony_ciaccess is the same as <code>Uint8Array</code>. In other words, <code>buf[index]</code> returns 17911cb0ef41Sopenharmony_ci<code>undefined</code> when <code>index</code> is negative or greater or equal to <code>buf.length</code>, and 17921cb0ef41Sopenharmony_ci<code>buf[index] = value</code> does not modify the buffer if <code>index</code> is negative or 17931cb0ef41Sopenharmony_ci<code>>= buf.length</code>.</p> 17941cb0ef41Sopenharmony_ci 17951cb0ef41Sopenharmony_ci<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>; 17961cb0ef41Sopenharmony_ci 17971cb0ef41Sopenharmony_ci<span class="hljs-comment">// Copy an ASCII string into a `Buffer` one byte at a time.</span> 17981cb0ef41Sopenharmony_ci<span class="hljs-comment">// (This only works for ASCII-only strings. In general, one should use</span> 17991cb0ef41Sopenharmony_ci<span class="hljs-comment">// `Buffer.from()` to perform this conversion.)</span> 18001cb0ef41Sopenharmony_ci 18011cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> str = <span class="hljs-string">'Node.js'</span>; 18021cb0ef41Sopenharmony_ci<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>); 18031cb0ef41Sopenharmony_ci 18041cb0ef41Sopenharmony_ci<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++) { 18051cb0ef41Sopenharmony_ci buf[i] = str.<span class="hljs-title function_">charCodeAt</span>(i); 18061cb0ef41Sopenharmony_ci} 18071cb0ef41Sopenharmony_ci 18081cb0ef41Sopenharmony_ci<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>)); 18091cb0ef41Sopenharmony_ci<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>); 18101cb0ef41Sopenharmony_ci 18111cb0ef41Sopenharmony_ci<span class="hljs-comment">// Copy an ASCII string into a `Buffer` one byte at a time.</span> 18121cb0ef41Sopenharmony_ci<span class="hljs-comment">// (This only works for ASCII-only strings. In general, one should use</span> 18131cb0ef41Sopenharmony_ci<span class="hljs-comment">// `Buffer.from()` to perform this conversion.)</span> 18141cb0ef41Sopenharmony_ci 18151cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> str = <span class="hljs-string">'Node.js'</span>; 18161cb0ef41Sopenharmony_ci<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>); 18171cb0ef41Sopenharmony_ci 18181cb0ef41Sopenharmony_ci<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++) { 18191cb0ef41Sopenharmony_ci buf[i] = str.<span class="hljs-title function_">charCodeAt</span>(i); 18201cb0ef41Sopenharmony_ci} 18211cb0ef41Sopenharmony_ci 18221cb0ef41Sopenharmony_ci<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>)); 18231cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: Node.js</span></code><button class="copy-button">copy</button></pre> 18241cb0ef41Sopenharmony_ci<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> 18251cb0ef41Sopenharmony_ci<ul> 18261cb0ef41Sopenharmony_ci<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> 18271cb0ef41Sopenharmony_ciobject is created.</li> 18281cb0ef41Sopenharmony_ci</ul> 18291cb0ef41Sopenharmony_ci<p>This <code>ArrayBuffer</code> is not guaranteed to correspond exactly to the original 18301cb0ef41Sopenharmony_ci<code>Buffer</code>. See the notes on <code>buf.byteOffset</code> for details.</p> 18311cb0ef41Sopenharmony_ci 18321cb0ef41Sopenharmony_ci<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>; 18331cb0ef41Sopenharmony_ci 18341cb0ef41Sopenharmony_ci<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>); 18351cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arrayBuffer); 18361cb0ef41Sopenharmony_ci 18371cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buffer.<span class="hljs-property">buffer</span> === arrayBuffer); 18381cb0ef41Sopenharmony_ci<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>); 18391cb0ef41Sopenharmony_ci 18401cb0ef41Sopenharmony_ci<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>); 18411cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arrayBuffer); 18421cb0ef41Sopenharmony_ci 18431cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buffer.<span class="hljs-property">buffer</span> === arrayBuffer); 18441cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: true</span></code><button class="copy-button">copy</button></pre> 18451cb0ef41Sopenharmony_ci<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> 18461cb0ef41Sopenharmony_ci<ul> 18471cb0ef41Sopenharmony_ci<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> 18481cb0ef41Sopenharmony_ci</ul> 18491cb0ef41Sopenharmony_ci<p>When setting <code>byteOffset</code> in <code>Buffer.from(ArrayBuffer, byteOffset, length)</code>, 18501cb0ef41Sopenharmony_cior sometimes when allocating a <code>Buffer</code> smaller than <code>Buffer.poolSize</code>, the 18511cb0ef41Sopenharmony_cibuffer does not start from a zero offset on the underlying <code>ArrayBuffer</code>.</p> 18521cb0ef41Sopenharmony_ci<p>This can cause problems when accessing the underlying <code>ArrayBuffer</code> directly 18531cb0ef41Sopenharmony_ciusing <code>buf.buffer</code>, as other parts of the <code>ArrayBuffer</code> may be unrelated 18541cb0ef41Sopenharmony_cito the <code>Buffer</code> object itself.</p> 18551cb0ef41Sopenharmony_ci<p>A common issue when creating a <code>TypedArray</code> object that shares its memory with 18561cb0ef41Sopenharmony_cia <code>Buffer</code> is that in this case one needs to specify the <code>byteOffset</code> correctly:</p> 18571cb0ef41Sopenharmony_ci 18581cb0ef41Sopenharmony_ci<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>; 18591cb0ef41Sopenharmony_ci 18601cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create a buffer smaller than `Buffer.poolSize`.</span> 18611cb0ef41Sopenharmony_ci<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>]); 18621cb0ef41Sopenharmony_ci 18631cb0ef41Sopenharmony_ci<span class="hljs-comment">// When casting the Node.js Buffer to an Int8Array, use the byteOffset</span> 18641cb0ef41Sopenharmony_ci<span class="hljs-comment">// to refer only to the part of `nodeBuffer.buffer` that contains the memory</span> 18651cb0ef41Sopenharmony_ci<span class="hljs-comment">// for `nodeBuffer`.</span> 18661cb0ef41Sopenharmony_ci<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>); 18671cb0ef41Sopenharmony_ci 18681cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create a buffer smaller than `Buffer.poolSize`.</span> 18691cb0ef41Sopenharmony_ci<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>]); 18701cb0ef41Sopenharmony_ci 18711cb0ef41Sopenharmony_ci<span class="hljs-comment">// When casting the Node.js Buffer to an Int8Array, use the byteOffset</span> 18721cb0ef41Sopenharmony_ci<span class="hljs-comment">// to refer only to the part of `nodeBuffer.buffer` that contains the memory</span> 18731cb0ef41Sopenharmony_ci<span class="hljs-comment">// for `nodeBuffer`.</span> 18741cb0ef41Sopenharmony_ci<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> 18751cb0ef41Sopenharmony_ci<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> 18761cb0ef41Sopenharmony_ci<div class="api_metadata"> 18771cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 18781cb0ef41Sopenharmony_ci<table> 18791cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 18801cb0ef41Sopenharmony_ci<tr><td>v8.0.0</td> 18811cb0ef41Sopenharmony_ci<td><p>The <code>target</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 18821cb0ef41Sopenharmony_ci<tr><td>v5.11.0</td> 18831cb0ef41Sopenharmony_ci<td><p>Additional parameters for specifying offsets are supported now.</p></td></tr> 18841cb0ef41Sopenharmony_ci<tr><td>v0.11.13</td> 18851cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.13</span></p></td></tr> 18861cb0ef41Sopenharmony_ci</tbody></table> 18871cb0ef41Sopenharmony_ci</details> 18881cb0ef41Sopenharmony_ci</div> 18891cb0ef41Sopenharmony_ci<ul> 18901cb0ef41Sopenharmony_ci<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 18911cb0ef41Sopenharmony_cicompare <code>buf</code>.</li> 18921cb0ef41Sopenharmony_ci<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 18931cb0ef41Sopenharmony_cicomparison. <strong>Default:</strong> <code>0</code>.</li> 18941cb0ef41Sopenharmony_ci<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 18951cb0ef41Sopenharmony_ci(not inclusive). <strong>Default:</strong> <code>target.length</code>.</li> 18961cb0ef41Sopenharmony_ci<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. 18971cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>0</code>.</li> 18981cb0ef41Sopenharmony_ci<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 18991cb0ef41Sopenharmony_ci(not inclusive). <strong>Default:</strong> <a href="#buflength"><code>buf.length</code></a>.</li> 19001cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 19011cb0ef41Sopenharmony_ci</ul> 19021cb0ef41Sopenharmony_ci<p>Compares <code>buf</code> with <code>target</code> and returns a number indicating whether <code>buf</code> 19031cb0ef41Sopenharmony_cicomes before, after, or is the same as <code>target</code> in sort order. 19041cb0ef41Sopenharmony_ciComparison is based on the actual sequence of bytes in each <code>Buffer</code>.</p> 19051cb0ef41Sopenharmony_ci<ul> 19061cb0ef41Sopenharmony_ci<li><code>0</code> is returned if <code>target</code> is the same as <code>buf</code></li> 19071cb0ef41Sopenharmony_ci<li><code>1</code> is returned if <code>target</code> should come <em>before</em> <code>buf</code> when sorted.</li> 19081cb0ef41Sopenharmony_ci<li><code>-1</code> is returned if <code>target</code> should come <em>after</em> <code>buf</code> when sorted.</li> 19091cb0ef41Sopenharmony_ci</ul> 19101cb0ef41Sopenharmony_ci 19111cb0ef41Sopenharmony_ci<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>; 19121cb0ef41Sopenharmony_ci 19131cb0ef41Sopenharmony_ci<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>); 19141cb0ef41Sopenharmony_ci<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>); 19151cb0ef41Sopenharmony_ci<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>); 19161cb0ef41Sopenharmony_ci 19171cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf1)); 19181cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 0</span> 19191cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf2)); 19201cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: -1</span> 19211cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf3)); 19221cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: -1</span> 19231cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">compare</span>(buf1)); 19241cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1</span> 19251cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">compare</span>(buf3)); 19261cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1</span> 19271cb0ef41Sopenharmony_ci<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>)); 19281cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: [ <Buffer 41 42 43>, <Buffer 41 42 43 44>, <Buffer 42 43 44> ]</span> 19291cb0ef41Sopenharmony_ci<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>); 19301cb0ef41Sopenharmony_ci 19311cb0ef41Sopenharmony_ci<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>); 19321cb0ef41Sopenharmony_ci<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>); 19331cb0ef41Sopenharmony_ci<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>); 19341cb0ef41Sopenharmony_ci 19351cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf1)); 19361cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 0</span> 19371cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf2)); 19381cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: -1</span> 19391cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf3)); 19401cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: -1</span> 19411cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">compare</span>(buf1)); 19421cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1</span> 19431cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">compare</span>(buf3)); 19441cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1</span> 19451cb0ef41Sopenharmony_ci<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>)); 19461cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: [ <Buffer 41 42 43>, <Buffer 41 42 43 44>, <Buffer 42 43 44> ]</span> 19471cb0ef41Sopenharmony_ci<span class="hljs-comment">// (This result is equal to: [buf1, buf3, buf2].)</span></code><button class="copy-button">copy</button></pre> 19481cb0ef41Sopenharmony_ci<p>The optional <code>targetStart</code>, <code>targetEnd</code>, <code>sourceStart</code>, and <code>sourceEnd</code> 19491cb0ef41Sopenharmony_ciarguments can be used to limit the comparison to specific ranges within <code>target</code> 19501cb0ef41Sopenharmony_ciand <code>buf</code> respectively.</p> 19511cb0ef41Sopenharmony_ci 19521cb0ef41Sopenharmony_ci<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>; 19531cb0ef41Sopenharmony_ci 19541cb0ef41Sopenharmony_ci<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>]); 19551cb0ef41Sopenharmony_ci<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>]); 19561cb0ef41Sopenharmony_ci 19571cb0ef41Sopenharmony_ci<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>)); 19581cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 0</span> 19591cb0ef41Sopenharmony_ci<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>)); 19601cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: -1</span> 19611cb0ef41Sopenharmony_ci<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>)); 19621cb0ef41Sopenharmony_ci<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>); 19631cb0ef41Sopenharmony_ci 19641cb0ef41Sopenharmony_ci<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>]); 19651cb0ef41Sopenharmony_ci<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>]); 19661cb0ef41Sopenharmony_ci 19671cb0ef41Sopenharmony_ci<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>)); 19681cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 0</span> 19691cb0ef41Sopenharmony_ci<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>)); 19701cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: -1</span> 19711cb0ef41Sopenharmony_ci<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>)); 19721cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1</span></code><button class="copy-button">copy</button></pre> 19731cb0ef41Sopenharmony_ci<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>, 19741cb0ef41Sopenharmony_ci<code>targetEnd > target.byteLength</code>, or <code>sourceEnd > source.byteLength</code>.</p> 19751cb0ef41Sopenharmony_ci<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> 19761cb0ef41Sopenharmony_ci<div class="api_metadata"> 19771cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 19781cb0ef41Sopenharmony_ci</div> 19791cb0ef41Sopenharmony_ci<ul> 19801cb0ef41Sopenharmony_ci<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> 19811cb0ef41Sopenharmony_ci<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 19821cb0ef41Sopenharmony_ciwriting. <strong>Default:</strong> <code>0</code>.</li> 19831cb0ef41Sopenharmony_ci<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. 19841cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>0</code>.</li> 19851cb0ef41Sopenharmony_ci<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 19861cb0ef41Sopenharmony_ciinclusive). <strong>Default:</strong> <a href="#buflength"><code>buf.length</code></a>.</li> 19871cb0ef41Sopenharmony_ci<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> 19881cb0ef41Sopenharmony_ci</ul> 19891cb0ef41Sopenharmony_ci<p>Copies data from a region of <code>buf</code> to a region in <code>target</code>, even if the <code>target</code> 19901cb0ef41Sopenharmony_cimemory region overlaps with <code>buf</code>.</p> 19911cb0ef41Sopenharmony_ci<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 19921cb0ef41Sopenharmony_cifor all TypedArrays, including Node.js <code>Buffer</code>s, although it takes 19931cb0ef41Sopenharmony_cidifferent function arguments.</p> 19941cb0ef41Sopenharmony_ci 19951cb0ef41Sopenharmony_ci<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>; 19961cb0ef41Sopenharmony_ci 19971cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create two `Buffer` instances.</span> 19981cb0ef41Sopenharmony_ci<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>); 19991cb0ef41Sopenharmony_ci<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>); 20001cb0ef41Sopenharmony_ci 20011cb0ef41Sopenharmony_ci<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++) { 20021cb0ef41Sopenharmony_ci <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> 20031cb0ef41Sopenharmony_ci buf1[i] = i + <span class="hljs-number">97</span>; 20041cb0ef41Sopenharmony_ci} 20051cb0ef41Sopenharmony_ci 20061cb0ef41Sopenharmony_ci<span class="hljs-comment">// Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`.</span> 20071cb0ef41Sopenharmony_cibuf1.<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>); 20081cb0ef41Sopenharmony_ci<span class="hljs-comment">// This is equivalent to:</span> 20091cb0ef41Sopenharmony_ci<span class="hljs-comment">// buf2.set(buf1.subarray(16, 20), 8);</span> 20101cb0ef41Sopenharmony_ci 20111cb0ef41Sopenharmony_ci<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>)); 20121cb0ef41Sopenharmony_ci<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>); 20131cb0ef41Sopenharmony_ci 20141cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create two `Buffer` instances.</span> 20151cb0ef41Sopenharmony_ci<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>); 20161cb0ef41Sopenharmony_ci<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>); 20171cb0ef41Sopenharmony_ci 20181cb0ef41Sopenharmony_ci<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++) { 20191cb0ef41Sopenharmony_ci <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> 20201cb0ef41Sopenharmony_ci buf1[i] = i + <span class="hljs-number">97</span>; 20211cb0ef41Sopenharmony_ci} 20221cb0ef41Sopenharmony_ci 20231cb0ef41Sopenharmony_ci<span class="hljs-comment">// Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`.</span> 20241cb0ef41Sopenharmony_cibuf1.<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>); 20251cb0ef41Sopenharmony_ci<span class="hljs-comment">// This is equivalent to:</span> 20261cb0ef41Sopenharmony_ci<span class="hljs-comment">// buf2.set(buf1.subarray(16, 20), 8);</span> 20271cb0ef41Sopenharmony_ci 20281cb0ef41Sopenharmony_ci<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>)); 20291cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: !!!!!!!!qrst!!!!!!!!!!!!!</span></code><button class="copy-button">copy</button></pre> 20301cb0ef41Sopenharmony_ci 20311cb0ef41Sopenharmony_ci<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>; 20321cb0ef41Sopenharmony_ci 20331cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create a `Buffer` and copy data from one region to an overlapping region</span> 20341cb0ef41Sopenharmony_ci<span class="hljs-comment">// within the same `Buffer`.</span> 20351cb0ef41Sopenharmony_ci 20361cb0ef41Sopenharmony_ci<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>); 20371cb0ef41Sopenharmony_ci 20381cb0ef41Sopenharmony_ci<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++) { 20391cb0ef41Sopenharmony_ci <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> 20401cb0ef41Sopenharmony_ci buf[i] = i + <span class="hljs-number">97</span>; 20411cb0ef41Sopenharmony_ci} 20421cb0ef41Sopenharmony_ci 20431cb0ef41Sopenharmony_cibuf.<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>); 20441cb0ef41Sopenharmony_ci 20451cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>()); 20461cb0ef41Sopenharmony_ci<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>); 20471cb0ef41Sopenharmony_ci 20481cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create a `Buffer` and copy data from one region to an overlapping region</span> 20491cb0ef41Sopenharmony_ci<span class="hljs-comment">// within the same `Buffer`.</span> 20501cb0ef41Sopenharmony_ci 20511cb0ef41Sopenharmony_ci<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>); 20521cb0ef41Sopenharmony_ci 20531cb0ef41Sopenharmony_ci<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++) { 20541cb0ef41Sopenharmony_ci <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> 20551cb0ef41Sopenharmony_ci buf[i] = i + <span class="hljs-number">97</span>; 20561cb0ef41Sopenharmony_ci} 20571cb0ef41Sopenharmony_ci 20581cb0ef41Sopenharmony_cibuf.<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>); 20591cb0ef41Sopenharmony_ci 20601cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>()); 20611cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: efghijghijklmnopqrstuvwxyz</span></code><button class="copy-button">copy</button></pre> 20621cb0ef41Sopenharmony_ci<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> 20631cb0ef41Sopenharmony_ci<div class="api_metadata"> 20641cb0ef41Sopenharmony_ci<span>Added in: v1.1.0</span> 20651cb0ef41Sopenharmony_ci</div> 20661cb0ef41Sopenharmony_ci<ul> 20671cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> 20681cb0ef41Sopenharmony_ci</ul> 20691cb0ef41Sopenharmony_ci<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 20701cb0ef41Sopenharmony_ciof <code>buf</code>.</p> 20711cb0ef41Sopenharmony_ci 20721cb0ef41Sopenharmony_ci<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>; 20731cb0ef41Sopenharmony_ci 20741cb0ef41Sopenharmony_ci<span class="hljs-comment">// Log the entire contents of a `Buffer`.</span> 20751cb0ef41Sopenharmony_ci 20761cb0ef41Sopenharmony_ci<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>); 20771cb0ef41Sopenharmony_ci 20781cb0ef41Sopenharmony_ci<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>()) { 20791cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(pair); 20801cb0ef41Sopenharmony_ci} 20811cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 20821cb0ef41Sopenharmony_ci<span class="hljs-comment">// [0, 98]</span> 20831cb0ef41Sopenharmony_ci<span class="hljs-comment">// [1, 117]</span> 20841cb0ef41Sopenharmony_ci<span class="hljs-comment">// [2, 102]</span> 20851cb0ef41Sopenharmony_ci<span class="hljs-comment">// [3, 102]</span> 20861cb0ef41Sopenharmony_ci<span class="hljs-comment">// [4, 101]</span> 20871cb0ef41Sopenharmony_ci<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>); 20881cb0ef41Sopenharmony_ci 20891cb0ef41Sopenharmony_ci<span class="hljs-comment">// Log the entire contents of a `Buffer`.</span> 20901cb0ef41Sopenharmony_ci 20911cb0ef41Sopenharmony_ci<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>); 20921cb0ef41Sopenharmony_ci 20931cb0ef41Sopenharmony_ci<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>()) { 20941cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(pair); 20951cb0ef41Sopenharmony_ci} 20961cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 20971cb0ef41Sopenharmony_ci<span class="hljs-comment">// [0, 98]</span> 20981cb0ef41Sopenharmony_ci<span class="hljs-comment">// [1, 117]</span> 20991cb0ef41Sopenharmony_ci<span class="hljs-comment">// [2, 102]</span> 21001cb0ef41Sopenharmony_ci<span class="hljs-comment">// [3, 102]</span> 21011cb0ef41Sopenharmony_ci<span class="hljs-comment">// [4, 101]</span> 21021cb0ef41Sopenharmony_ci<span class="hljs-comment">// [5, 114]</span></code><button class="copy-button">copy</button></pre> 21031cb0ef41Sopenharmony_ci<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> 21041cb0ef41Sopenharmony_ci<div class="api_metadata"> 21051cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 21061cb0ef41Sopenharmony_ci<table> 21071cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 21081cb0ef41Sopenharmony_ci<tr><td>v8.0.0</td> 21091cb0ef41Sopenharmony_ci<td><p>The arguments can now be <code>Uint8Array</code>s.</p></td></tr> 21101cb0ef41Sopenharmony_ci<tr><td>v0.11.13</td> 21111cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.13</span></p></td></tr> 21121cb0ef41Sopenharmony_ci</tbody></table> 21131cb0ef41Sopenharmony_ci</details> 21141cb0ef41Sopenharmony_ci</div> 21151cb0ef41Sopenharmony_ci<ul> 21161cb0ef41Sopenharmony_ci<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 21171cb0ef41Sopenharmony_cicompare <code>buf</code>.</li> 21181cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 21191cb0ef41Sopenharmony_ci</ul> 21201cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if both <code>buf</code> and <code>otherBuffer</code> have exactly the same bytes, 21211cb0ef41Sopenharmony_ci<code>false</code> otherwise. Equivalent to 21221cb0ef41Sopenharmony_ci<a href="#bufcomparetarget-targetstart-targetend-sourcestart-sourceend"><code>buf.compare(otherBuffer) === 0</code></a>.</p> 21231cb0ef41Sopenharmony_ci 21241cb0ef41Sopenharmony_ci<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>; 21251cb0ef41Sopenharmony_ci 21261cb0ef41Sopenharmony_ci<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>); 21271cb0ef41Sopenharmony_ci<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>); 21281cb0ef41Sopenharmony_ci<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>); 21291cb0ef41Sopenharmony_ci 21301cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">equals</span>(buf2)); 21311cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: true</span> 21321cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">equals</span>(buf3)); 21331cb0ef41Sopenharmony_ci<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>); 21341cb0ef41Sopenharmony_ci 21351cb0ef41Sopenharmony_ci<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>); 21361cb0ef41Sopenharmony_ci<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>); 21371cb0ef41Sopenharmony_ci<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>); 21381cb0ef41Sopenharmony_ci 21391cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">equals</span>(buf2)); 21401cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: true</span> 21411cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">equals</span>(buf3)); 21421cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: false</span></code><button class="copy-button">copy</button></pre> 21431cb0ef41Sopenharmony_ci<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> 21441cb0ef41Sopenharmony_ci<div class="api_metadata"> 21451cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 21461cb0ef41Sopenharmony_ci<table> 21471cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 21481cb0ef41Sopenharmony_ci<tr><td>v11.0.0</td> 21491cb0ef41Sopenharmony_ci<td><p>Throws <code>ERR_OUT_OF_RANGE</code> instead of <code>ERR_INDEX_OUT_OF_RANGE</code>.</p></td></tr> 21501cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 21511cb0ef41Sopenharmony_ci<td><p>Negative <code>end</code> values throw an <code>ERR_INDEX_OUT_OF_RANGE</code> error.</p></td></tr> 21521cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 21531cb0ef41Sopenharmony_ci<td><p>Attempting to fill a non-zero length buffer with a zero length buffer triggers a thrown exception.</p></td></tr> 21541cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 21551cb0ef41Sopenharmony_ci<td><p>Specifying an invalid string for <code>value</code> triggers a thrown exception.</p></td></tr> 21561cb0ef41Sopenharmony_ci<tr><td>v5.7.0</td> 21571cb0ef41Sopenharmony_ci<td><p>The <code>encoding</code> parameter is supported now.</p></td></tr> 21581cb0ef41Sopenharmony_ci<tr><td>v0.5.0</td> 21591cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.0</span></p></td></tr> 21601cb0ef41Sopenharmony_ci</tbody></table> 21611cb0ef41Sopenharmony_ci</details> 21621cb0ef41Sopenharmony_ci</div> 21631cb0ef41Sopenharmony_ci<ul> 21641cb0ef41Sopenharmony_ci<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>. 21651cb0ef41Sopenharmony_ciEmpty value (string, Uint8Array, Buffer) is coerced to <code>0</code>.</li> 21661cb0ef41Sopenharmony_ci<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>. 21671cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>0</code>.</li> 21681cb0ef41Sopenharmony_ci<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> 21691cb0ef41Sopenharmony_ci<a href="#buflength"><code>buf.length</code></a>.</li> 21701cb0ef41Sopenharmony_ci<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. 21711cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'utf8'</code>.</li> 21721cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> A reference to <code>buf</code>.</li> 21731cb0ef41Sopenharmony_ci</ul> 21741cb0ef41Sopenharmony_ci<p>Fills <code>buf</code> with the specified <code>value</code>. If the <code>offset</code> and <code>end</code> are not given, 21751cb0ef41Sopenharmony_cithe entire <code>buf</code> will be filled:</p> 21761cb0ef41Sopenharmony_ci 21771cb0ef41Sopenharmony_ci<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>; 21781cb0ef41Sopenharmony_ci 21791cb0ef41Sopenharmony_ci<span class="hljs-comment">// Fill a `Buffer` with the ASCII character 'h'.</span> 21801cb0ef41Sopenharmony_ci 21811cb0ef41Sopenharmony_ci<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>); 21821cb0ef41Sopenharmony_ci 21831cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">toString</span>()); 21841cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh</span> 21851cb0ef41Sopenharmony_ci 21861cb0ef41Sopenharmony_ci<span class="hljs-comment">// Fill a buffer with empty string</span> 21871cb0ef41Sopenharmony_ci<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>); 21881cb0ef41Sopenharmony_ci 21891cb0ef41Sopenharmony_ci<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>)); 21901cb0ef41Sopenharmony_ci<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>); 21911cb0ef41Sopenharmony_ci 21921cb0ef41Sopenharmony_ci<span class="hljs-comment">// Fill a `Buffer` with the ASCII character 'h'.</span> 21931cb0ef41Sopenharmony_ci 21941cb0ef41Sopenharmony_ci<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>); 21951cb0ef41Sopenharmony_ci 21961cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">toString</span>()); 21971cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh</span> 21981cb0ef41Sopenharmony_ci 21991cb0ef41Sopenharmony_ci<span class="hljs-comment">// Fill a buffer with empty string</span> 22001cb0ef41Sopenharmony_ci<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>); 22011cb0ef41Sopenharmony_ci 22021cb0ef41Sopenharmony_ci<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>)); 22031cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 00 00 00 00 00></span></code><button class="copy-button">copy</button></pre> 22041cb0ef41Sopenharmony_ci<p><code>value</code> is coerced to a <code>uint32</code> value if it is not a string, <code>Buffer</code>, or 22051cb0ef41Sopenharmony_ciinteger. If the resulting integer is greater than <code>255</code> (decimal), <code>buf</code> will be 22061cb0ef41Sopenharmony_cifilled with <code>value & 255</code>.</p> 22071cb0ef41Sopenharmony_ci<p>If the final write of a <code>fill()</code> operation falls on a multi-byte character, 22081cb0ef41Sopenharmony_cithen only the bytes of that character that fit into <code>buf</code> are written:</p> 22091cb0ef41Sopenharmony_ci 22101cb0ef41Sopenharmony_ci<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>; 22111cb0ef41Sopenharmony_ci 22121cb0ef41Sopenharmony_ci<span class="hljs-comment">// Fill a `Buffer` with character that takes up two bytes in UTF-8.</span> 22131cb0ef41Sopenharmony_ci 22141cb0ef41Sopenharmony_ci<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>)); 22151cb0ef41Sopenharmony_ci<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>); 22161cb0ef41Sopenharmony_ci 22171cb0ef41Sopenharmony_ci<span class="hljs-comment">// Fill a `Buffer` with character that takes up two bytes in UTF-8.</span> 22181cb0ef41Sopenharmony_ci 22191cb0ef41Sopenharmony_ci<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>)); 22201cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer c8 a2 c8 a2 c8></span></code><button class="copy-button">copy</button></pre> 22211cb0ef41Sopenharmony_ci<p>If <code>value</code> contains invalid characters, it is truncated; if no valid 22221cb0ef41Sopenharmony_cifill data remains, an exception is thrown:</p> 22231cb0ef41Sopenharmony_ci 22241cb0ef41Sopenharmony_ci<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>; 22251cb0ef41Sopenharmony_ci 22261cb0ef41Sopenharmony_ci<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>); 22271cb0ef41Sopenharmony_ci 22281cb0ef41Sopenharmony_ci<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>)); 22291cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 61 61 61 61 61></span> 22301cb0ef41Sopenharmony_ci<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>)); 22311cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer aa aa aa aa aa></span> 22321cb0ef41Sopenharmony_ci<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>)); 22331cb0ef41Sopenharmony_ci<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>); 22341cb0ef41Sopenharmony_ci 22351cb0ef41Sopenharmony_ci<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>); 22361cb0ef41Sopenharmony_ci 22371cb0ef41Sopenharmony_ci<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>)); 22381cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 61 61 61 61 61></span> 22391cb0ef41Sopenharmony_ci<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>)); 22401cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer aa aa aa aa aa></span> 22411cb0ef41Sopenharmony_ci<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>)); 22421cb0ef41Sopenharmony_ci<span class="hljs-comment">// Throws an exception.</span></code><button class="copy-button">copy</button></pre> 22431cb0ef41Sopenharmony_ci<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> 22441cb0ef41Sopenharmony_ci<div class="api_metadata"> 22451cb0ef41Sopenharmony_ci<span>Added in: v5.3.0</span> 22461cb0ef41Sopenharmony_ci</div> 22471cb0ef41Sopenharmony_ci<ul> 22481cb0ef41Sopenharmony_ci<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> 22491cb0ef41Sopenharmony_ci<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 22501cb0ef41Sopenharmony_cioffset is calculated from the end of <code>buf</code>. <strong>Default:</strong> <code>0</code>.</li> 22511cb0ef41Sopenharmony_ci<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. 22521cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'utf8'</code>.</li> 22531cb0ef41Sopenharmony_ci<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> 22541cb0ef41Sopenharmony_ci</ul> 22551cb0ef41Sopenharmony_ci<p>Equivalent to <a href="#bufindexofvalue-byteoffset-encoding"><code>buf.indexOf() !== -1</code></a>.</p> 22561cb0ef41Sopenharmony_ci 22571cb0ef41Sopenharmony_ci<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>; 22581cb0ef41Sopenharmony_ci 22591cb0ef41Sopenharmony_ci<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>); 22601cb0ef41Sopenharmony_ci 22611cb0ef41Sopenharmony_ci<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>)); 22621cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: true</span> 22631cb0ef41Sopenharmony_ci<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>)); 22641cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: true</span> 22651cb0ef41Sopenharmony_ci<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>))); 22661cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: true</span> 22671cb0ef41Sopenharmony_ci<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>)); 22681cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: true (97 is the decimal ASCII value for 'a')</span> 22691cb0ef41Sopenharmony_ci<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>))); 22701cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: false</span> 22711cb0ef41Sopenharmony_ci<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>))); 22721cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: true</span> 22731cb0ef41Sopenharmony_ci<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>)); 22741cb0ef41Sopenharmony_ci<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>); 22751cb0ef41Sopenharmony_ci 22761cb0ef41Sopenharmony_ci<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>); 22771cb0ef41Sopenharmony_ci 22781cb0ef41Sopenharmony_ci<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>)); 22791cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: true</span> 22801cb0ef41Sopenharmony_ci<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>)); 22811cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: true</span> 22821cb0ef41Sopenharmony_ci<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>))); 22831cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: true</span> 22841cb0ef41Sopenharmony_ci<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>)); 22851cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: true (97 is the decimal ASCII value for 'a')</span> 22861cb0ef41Sopenharmony_ci<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>))); 22871cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: false</span> 22881cb0ef41Sopenharmony_ci<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>))); 22891cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: true</span> 22901cb0ef41Sopenharmony_ci<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>)); 22911cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: false</span></code><button class="copy-button">copy</button></pre> 22921cb0ef41Sopenharmony_ci<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> 22931cb0ef41Sopenharmony_ci<div class="api_metadata"> 22941cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 22951cb0ef41Sopenharmony_ci<table> 22961cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 22971cb0ef41Sopenharmony_ci<tr><td>v8.0.0</td> 22981cb0ef41Sopenharmony_ci<td><p>The <code>value</code> can now be a <code>Uint8Array</code>.</p></td></tr> 22991cb0ef41Sopenharmony_ci<tr><td>v5.7.0, v4.4.0</td> 23001cb0ef41Sopenharmony_ci<td><p>When <code>encoding</code> is being passed, the <code>byteOffset</code> parameter is no longer required.</p></td></tr> 23011cb0ef41Sopenharmony_ci<tr><td>v1.5.0</td> 23021cb0ef41Sopenharmony_ci<td><p><span>Added in: v1.5.0</span></p></td></tr> 23031cb0ef41Sopenharmony_ci</tbody></table> 23041cb0ef41Sopenharmony_ci</details> 23051cb0ef41Sopenharmony_ci</div> 23061cb0ef41Sopenharmony_ci<ul> 23071cb0ef41Sopenharmony_ci<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> 23081cb0ef41Sopenharmony_ci<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 23091cb0ef41Sopenharmony_cioffset is calculated from the end of <code>buf</code>. <strong>Default:</strong> <code>0</code>.</li> 23101cb0ef41Sopenharmony_ci<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 23111cb0ef41Sopenharmony_cidetermine the binary representation of the string that will be searched for in 23121cb0ef41Sopenharmony_ci<code>buf</code>. <strong>Default:</strong> <code>'utf8'</code>.</li> 23131cb0ef41Sopenharmony_ci<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 23141cb0ef41Sopenharmony_ci<code>-1</code> if <code>buf</code> does not contain <code>value</code>.</li> 23151cb0ef41Sopenharmony_ci</ul> 23161cb0ef41Sopenharmony_ci<p>If <code>value</code> is:</p> 23171cb0ef41Sopenharmony_ci<ul> 23181cb0ef41Sopenharmony_ci<li>a string, <code>value</code> is interpreted according to the character encoding in 23191cb0ef41Sopenharmony_ci<code>encoding</code>.</li> 23201cb0ef41Sopenharmony_ci<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. 23211cb0ef41Sopenharmony_ciTo compare a partial <code>Buffer</code>, use <a href="#bufsubarraystart-end"><code>buf.subarray</code></a>.</li> 23221cb0ef41Sopenharmony_ci<li>a number, <code>value</code> will be interpreted as an unsigned 8-bit integer 23231cb0ef41Sopenharmony_civalue between <code>0</code> and <code>255</code>.</li> 23241cb0ef41Sopenharmony_ci</ul> 23251cb0ef41Sopenharmony_ci 23261cb0ef41Sopenharmony_ci<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>; 23271cb0ef41Sopenharmony_ci 23281cb0ef41Sopenharmony_ci<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>); 23291cb0ef41Sopenharmony_ci 23301cb0ef41Sopenharmony_ci<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>)); 23311cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 0</span> 23321cb0ef41Sopenharmony_ci<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>)); 23331cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 2</span> 23341cb0ef41Sopenharmony_ci<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>))); 23351cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 8</span> 23361cb0ef41Sopenharmony_ci<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>)); 23371cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 8 (97 is the decimal ASCII value for 'a')</span> 23381cb0ef41Sopenharmony_ci<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>))); 23391cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: -1</span> 23401cb0ef41Sopenharmony_ci<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>))); 23411cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 8</span> 23421cb0ef41Sopenharmony_ci 23431cb0ef41Sopenharmony_ci<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>); 23441cb0ef41Sopenharmony_ci 23451cb0ef41Sopenharmony_ci<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>)); 23461cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 4</span> 23471cb0ef41Sopenharmony_ci<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>)); 23481cb0ef41Sopenharmony_ci<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>); 23491cb0ef41Sopenharmony_ci 23501cb0ef41Sopenharmony_ci<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>); 23511cb0ef41Sopenharmony_ci 23521cb0ef41Sopenharmony_ci<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>)); 23531cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 0</span> 23541cb0ef41Sopenharmony_ci<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>)); 23551cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 2</span> 23561cb0ef41Sopenharmony_ci<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>))); 23571cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 8</span> 23581cb0ef41Sopenharmony_ci<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>)); 23591cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 8 (97 is the decimal ASCII value for 'a')</span> 23601cb0ef41Sopenharmony_ci<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>))); 23611cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: -1</span> 23621cb0ef41Sopenharmony_ci<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>))); 23631cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 8</span> 23641cb0ef41Sopenharmony_ci 23651cb0ef41Sopenharmony_ci<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>); 23661cb0ef41Sopenharmony_ci 23671cb0ef41Sopenharmony_ci<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>)); 23681cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 4</span> 23691cb0ef41Sopenharmony_ci<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>)); 23701cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 6</span></code><button class="copy-button">copy</button></pre> 23711cb0ef41Sopenharmony_ci<p>If <code>value</code> is not a string, number, or <code>Buffer</code>, this method will throw a 23721cb0ef41Sopenharmony_ci<code>TypeError</code>. If <code>value</code> is a number, it will be coerced to a valid byte value, 23731cb0ef41Sopenharmony_cian integer between 0 and 255.</p> 23741cb0ef41Sopenharmony_ci<p>If <code>byteOffset</code> is not a number, it will be coerced to a number. If the result 23751cb0ef41Sopenharmony_ciof coercion is <code>NaN</code> or <code>0</code>, then the entire buffer will be searched. This 23761cb0ef41Sopenharmony_cibehavior matches <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf"><code>String.prototype.indexOf()</code></a>.</p> 23771cb0ef41Sopenharmony_ci 23781cb0ef41Sopenharmony_ci<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>; 23791cb0ef41Sopenharmony_ci 23801cb0ef41Sopenharmony_ci<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>); 23811cb0ef41Sopenharmony_ci 23821cb0ef41Sopenharmony_ci<span class="hljs-comment">// Passing a value that's a number, but not a valid byte.</span> 23831cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 2, equivalent to searching for 99 or 'c'.</span> 23841cb0ef41Sopenharmony_ci<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>)); 23851cb0ef41Sopenharmony_ci<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>)); 23861cb0ef41Sopenharmony_ci 23871cb0ef41Sopenharmony_ci<span class="hljs-comment">// Passing a byteOffset that coerces to NaN or 0.</span> 23881cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1, searching the whole buffer.</span> 23891cb0ef41Sopenharmony_ci<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>)); 23901cb0ef41Sopenharmony_ci<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>, {})); 23911cb0ef41Sopenharmony_ci<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>)); 23921cb0ef41Sopenharmony_ci<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>); 23931cb0ef41Sopenharmony_ci 23941cb0ef41Sopenharmony_ci<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>); 23951cb0ef41Sopenharmony_ci 23961cb0ef41Sopenharmony_ci<span class="hljs-comment">// Passing a value that's a number, but not a valid byte.</span> 23971cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 2, equivalent to searching for 99 or 'c'.</span> 23981cb0ef41Sopenharmony_ci<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>)); 23991cb0ef41Sopenharmony_ci<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>)); 24001cb0ef41Sopenharmony_ci 24011cb0ef41Sopenharmony_ci<span class="hljs-comment">// Passing a byteOffset that coerces to NaN or 0.</span> 24021cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1, searching the whole buffer.</span> 24031cb0ef41Sopenharmony_ci<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>)); 24041cb0ef41Sopenharmony_ci<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>, {})); 24051cb0ef41Sopenharmony_ci<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>)); 24061cb0ef41Sopenharmony_ci<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> 24071cb0ef41Sopenharmony_ci<p>If <code>value</code> is an empty string or empty <code>Buffer</code> and <code>byteOffset</code> is less 24081cb0ef41Sopenharmony_cithan <code>buf.length</code>, <code>byteOffset</code> will be returned. If <code>value</code> is empty and 24091cb0ef41Sopenharmony_ci<code>byteOffset</code> is at least <code>buf.length</code>, <code>buf.length</code> will be returned.</p> 24101cb0ef41Sopenharmony_ci<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> 24111cb0ef41Sopenharmony_ci<div class="api_metadata"> 24121cb0ef41Sopenharmony_ci<span>Added in: v1.1.0</span> 24131cb0ef41Sopenharmony_ci</div> 24141cb0ef41Sopenharmony_ci<ul> 24151cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> 24161cb0ef41Sopenharmony_ci</ul> 24171cb0ef41Sopenharmony_ci<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> 24181cb0ef41Sopenharmony_ci 24191cb0ef41Sopenharmony_ci<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>; 24201cb0ef41Sopenharmony_ci 24211cb0ef41Sopenharmony_ci<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>); 24221cb0ef41Sopenharmony_ci 24231cb0ef41Sopenharmony_ci<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>()) { 24241cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(key); 24251cb0ef41Sopenharmony_ci} 24261cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 24271cb0ef41Sopenharmony_ci<span class="hljs-comment">// 0</span> 24281cb0ef41Sopenharmony_ci<span class="hljs-comment">// 1</span> 24291cb0ef41Sopenharmony_ci<span class="hljs-comment">// 2</span> 24301cb0ef41Sopenharmony_ci<span class="hljs-comment">// 3</span> 24311cb0ef41Sopenharmony_ci<span class="hljs-comment">// 4</span> 24321cb0ef41Sopenharmony_ci<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>); 24331cb0ef41Sopenharmony_ci 24341cb0ef41Sopenharmony_ci<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>); 24351cb0ef41Sopenharmony_ci 24361cb0ef41Sopenharmony_ci<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>()) { 24371cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(key); 24381cb0ef41Sopenharmony_ci} 24391cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 24401cb0ef41Sopenharmony_ci<span class="hljs-comment">// 0</span> 24411cb0ef41Sopenharmony_ci<span class="hljs-comment">// 1</span> 24421cb0ef41Sopenharmony_ci<span class="hljs-comment">// 2</span> 24431cb0ef41Sopenharmony_ci<span class="hljs-comment">// 3</span> 24441cb0ef41Sopenharmony_ci<span class="hljs-comment">// 4</span> 24451cb0ef41Sopenharmony_ci<span class="hljs-comment">// 5</span></code><button class="copy-button">copy</button></pre> 24461cb0ef41Sopenharmony_ci<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> 24471cb0ef41Sopenharmony_ci<div class="api_metadata"> 24481cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 24491cb0ef41Sopenharmony_ci<table> 24501cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 24511cb0ef41Sopenharmony_ci<tr><td>v8.0.0</td> 24521cb0ef41Sopenharmony_ci<td><p>The <code>value</code> can now be a <code>Uint8Array</code>.</p></td></tr> 24531cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td> 24541cb0ef41Sopenharmony_ci<td><p><span>Added in: v6.0.0</span></p></td></tr> 24551cb0ef41Sopenharmony_ci</tbody></table> 24561cb0ef41Sopenharmony_ci</details> 24571cb0ef41Sopenharmony_ci</div> 24581cb0ef41Sopenharmony_ci<ul> 24591cb0ef41Sopenharmony_ci<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> 24601cb0ef41Sopenharmony_ci<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 24611cb0ef41Sopenharmony_cioffset is calculated from the end of <code>buf</code>. <strong>Default:</strong> 24621cb0ef41Sopenharmony_ci<code>buf.length - 1</code>.</li> 24631cb0ef41Sopenharmony_ci<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 24641cb0ef41Sopenharmony_cidetermine the binary representation of the string that will be searched for in 24651cb0ef41Sopenharmony_ci<code>buf</code>. <strong>Default:</strong> <code>'utf8'</code>.</li> 24661cb0ef41Sopenharmony_ci<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 24671cb0ef41Sopenharmony_ci<code>-1</code> if <code>buf</code> does not contain <code>value</code>.</li> 24681cb0ef41Sopenharmony_ci</ul> 24691cb0ef41Sopenharmony_ci<p>Identical to <a href="#bufindexofvalue-byteoffset-encoding"><code>buf.indexOf()</code></a>, except the last occurrence of <code>value</code> is found 24701cb0ef41Sopenharmony_cirather than the first occurrence.</p> 24711cb0ef41Sopenharmony_ci 24721cb0ef41Sopenharmony_ci<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>; 24731cb0ef41Sopenharmony_ci 24741cb0ef41Sopenharmony_ci<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>); 24751cb0ef41Sopenharmony_ci 24761cb0ef41Sopenharmony_ci<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>)); 24771cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 0</span> 24781cb0ef41Sopenharmony_ci<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>)); 24791cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 17</span> 24801cb0ef41Sopenharmony_ci<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>))); 24811cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 17</span> 24821cb0ef41Sopenharmony_ci<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>)); 24831cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 15 (97 is the decimal ASCII value for 'a')</span> 24841cb0ef41Sopenharmony_ci<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>))); 24851cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: -1</span> 24861cb0ef41Sopenharmony_ci<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>)); 24871cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 5</span> 24881cb0ef41Sopenharmony_ci<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>)); 24891cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: -1</span> 24901cb0ef41Sopenharmony_ci 24911cb0ef41Sopenharmony_ci<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>); 24921cb0ef41Sopenharmony_ci 24931cb0ef41Sopenharmony_ci<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>)); 24941cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 6</span> 24951cb0ef41Sopenharmony_ci<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>)); 24961cb0ef41Sopenharmony_ci<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>); 24971cb0ef41Sopenharmony_ci 24981cb0ef41Sopenharmony_ci<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>); 24991cb0ef41Sopenharmony_ci 25001cb0ef41Sopenharmony_ci<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>)); 25011cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 0</span> 25021cb0ef41Sopenharmony_ci<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>)); 25031cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 17</span> 25041cb0ef41Sopenharmony_ci<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>))); 25051cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 17</span> 25061cb0ef41Sopenharmony_ci<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>)); 25071cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 15 (97 is the decimal ASCII value for 'a')</span> 25081cb0ef41Sopenharmony_ci<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>))); 25091cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: -1</span> 25101cb0ef41Sopenharmony_ci<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>)); 25111cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 5</span> 25121cb0ef41Sopenharmony_ci<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>)); 25131cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: -1</span> 25141cb0ef41Sopenharmony_ci 25151cb0ef41Sopenharmony_ci<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>); 25161cb0ef41Sopenharmony_ci 25171cb0ef41Sopenharmony_ci<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>)); 25181cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 6</span> 25191cb0ef41Sopenharmony_ci<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>)); 25201cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 4</span></code><button class="copy-button">copy</button></pre> 25211cb0ef41Sopenharmony_ci<p>If <code>value</code> is not a string, number, or <code>Buffer</code>, this method will throw a 25221cb0ef41Sopenharmony_ci<code>TypeError</code>. If <code>value</code> is a number, it will be coerced to a valid byte value, 25231cb0ef41Sopenharmony_cian integer between 0 and 255.</p> 25241cb0ef41Sopenharmony_ci<p>If <code>byteOffset</code> is not a number, it will be coerced to a number. Any arguments 25251cb0ef41Sopenharmony_cithat coerce to <code>NaN</code>, like <code>{}</code> or <code>undefined</code>, will search the whole buffer. 25261cb0ef41Sopenharmony_ciThis 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> 25271cb0ef41Sopenharmony_ci 25281cb0ef41Sopenharmony_ci<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>; 25291cb0ef41Sopenharmony_ci 25301cb0ef41Sopenharmony_ci<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>); 25311cb0ef41Sopenharmony_ci 25321cb0ef41Sopenharmony_ci<span class="hljs-comment">// Passing a value that's a number, but not a valid byte.</span> 25331cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 2, equivalent to searching for 99 or 'c'.</span> 25341cb0ef41Sopenharmony_ci<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>)); 25351cb0ef41Sopenharmony_ci<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>)); 25361cb0ef41Sopenharmony_ci 25371cb0ef41Sopenharmony_ci<span class="hljs-comment">// Passing a byteOffset that coerces to NaN.</span> 25381cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1, searching the whole buffer.</span> 25391cb0ef41Sopenharmony_ci<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>)); 25401cb0ef41Sopenharmony_ci<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>, {})); 25411cb0ef41Sopenharmony_ci 25421cb0ef41Sopenharmony_ci<span class="hljs-comment">// Passing a byteOffset that coerces to 0.</span> 25431cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: -1, equivalent to passing 0.</span> 25441cb0ef41Sopenharmony_ci<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>)); 25451cb0ef41Sopenharmony_ci<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>); 25461cb0ef41Sopenharmony_ci 25471cb0ef41Sopenharmony_ci<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>); 25481cb0ef41Sopenharmony_ci 25491cb0ef41Sopenharmony_ci<span class="hljs-comment">// Passing a value that's a number, but not a valid byte.</span> 25501cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 2, equivalent to searching for 99 or 'c'.</span> 25511cb0ef41Sopenharmony_ci<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>)); 25521cb0ef41Sopenharmony_ci<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>)); 25531cb0ef41Sopenharmony_ci 25541cb0ef41Sopenharmony_ci<span class="hljs-comment">// Passing a byteOffset that coerces to NaN.</span> 25551cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1, searching the whole buffer.</span> 25561cb0ef41Sopenharmony_ci<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>)); 25571cb0ef41Sopenharmony_ci<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>, {})); 25581cb0ef41Sopenharmony_ci 25591cb0ef41Sopenharmony_ci<span class="hljs-comment">// Passing a byteOffset that coerces to 0.</span> 25601cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: -1, equivalent to passing 0.</span> 25611cb0ef41Sopenharmony_ci<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>)); 25621cb0ef41Sopenharmony_ci<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> 25631cb0ef41Sopenharmony_ci<p>If <code>value</code> is an empty string or empty <code>Buffer</code>, <code>byteOffset</code> will be returned.</p> 25641cb0ef41Sopenharmony_ci<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> 25651cb0ef41Sopenharmony_ci<div class="api_metadata"> 25661cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 25671cb0ef41Sopenharmony_ci</div> 25681cb0ef41Sopenharmony_ci<ul> 25691cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 25701cb0ef41Sopenharmony_ci</ul> 25711cb0ef41Sopenharmony_ci<p>Returns the number of bytes in <code>buf</code>.</p> 25721cb0ef41Sopenharmony_ci 25731cb0ef41Sopenharmony_ci<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>; 25741cb0ef41Sopenharmony_ci 25751cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create a `Buffer` and write a shorter string to it using UTF-8.</span> 25761cb0ef41Sopenharmony_ci 25771cb0ef41Sopenharmony_ci<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>); 25781cb0ef41Sopenharmony_ci 25791cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>); 25801cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1234</span> 25811cb0ef41Sopenharmony_ci 25821cb0ef41Sopenharmony_cibuf.<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>); 25831cb0ef41Sopenharmony_ci 25841cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>); 25851cb0ef41Sopenharmony_ci<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>); 25861cb0ef41Sopenharmony_ci 25871cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create a `Buffer` and write a shorter string to it using UTF-8.</span> 25881cb0ef41Sopenharmony_ci 25891cb0ef41Sopenharmony_ci<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>); 25901cb0ef41Sopenharmony_ci 25911cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>); 25921cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1234</span> 25931cb0ef41Sopenharmony_ci 25941cb0ef41Sopenharmony_cibuf.<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>); 25951cb0ef41Sopenharmony_ci 25961cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>); 25971cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1234</span></code><button class="copy-button">copy</button></pre> 25981cb0ef41Sopenharmony_ci<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> 25991cb0ef41Sopenharmony_ci<div class="api_metadata"> 26001cb0ef41Sopenharmony_ci<span>Deprecated since: v8.0.0</span> 26011cb0ef41Sopenharmony_ci</div> 26021cb0ef41Sopenharmony_ci<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> 26031cb0ef41Sopenharmony_ci<p>The <code>buf.parent</code> property is a deprecated alias for <code>buf.buffer</code>.</p> 26041cb0ef41Sopenharmony_ci<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> 26051cb0ef41Sopenharmony_ci<div class="api_metadata"> 26061cb0ef41Sopenharmony_ci<span>Added in: v12.0.0, v10.20.0</span> 26071cb0ef41Sopenharmony_ci</div> 26081cb0ef41Sopenharmony_ci<ul> 26091cb0ef41Sopenharmony_ci<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 26101cb0ef41Sopenharmony_cisatisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 26111cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 26121cb0ef41Sopenharmony_ci</ul> 26131cb0ef41Sopenharmony_ci<p>Reads a signed, big-endian 64-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> 26141cb0ef41Sopenharmony_ci<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed 26151cb0ef41Sopenharmony_civalues.</p> 26161cb0ef41Sopenharmony_ci<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> 26171cb0ef41Sopenharmony_ci<div class="api_metadata"> 26181cb0ef41Sopenharmony_ci<span>Added in: v12.0.0, v10.20.0</span> 26191cb0ef41Sopenharmony_ci</div> 26201cb0ef41Sopenharmony_ci<ul> 26211cb0ef41Sopenharmony_ci<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 26221cb0ef41Sopenharmony_cisatisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 26231cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 26241cb0ef41Sopenharmony_ci</ul> 26251cb0ef41Sopenharmony_ci<p>Reads a signed, little-endian 64-bit integer from <code>buf</code> at the specified 26261cb0ef41Sopenharmony_ci<code>offset</code>.</p> 26271cb0ef41Sopenharmony_ci<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed 26281cb0ef41Sopenharmony_civalues.</p> 26291cb0ef41Sopenharmony_ci<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> 26301cb0ef41Sopenharmony_ci<div class="api_metadata"> 26311cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 26321cb0ef41Sopenharmony_ci<table> 26331cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 26341cb0ef41Sopenharmony_ci<tr><td>v14.10.0, v12.19.0</td> 26351cb0ef41Sopenharmony_ci<td><p>This function is also available as <code>buf.readBigUint64BE()</code>.</p></td></tr> 26361cb0ef41Sopenharmony_ci<tr><td>v12.0.0, v10.20.0</td> 26371cb0ef41Sopenharmony_ci<td><p><span>Added in: v12.0.0, v10.20.0</span></p></td></tr> 26381cb0ef41Sopenharmony_ci</tbody></table> 26391cb0ef41Sopenharmony_ci</details> 26401cb0ef41Sopenharmony_ci</div> 26411cb0ef41Sopenharmony_ci<ul> 26421cb0ef41Sopenharmony_ci<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 26431cb0ef41Sopenharmony_cisatisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 26441cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 26451cb0ef41Sopenharmony_ci</ul> 26461cb0ef41Sopenharmony_ci<p>Reads an unsigned, big-endian 64-bit integer from <code>buf</code> at the specified 26471cb0ef41Sopenharmony_ci<code>offset</code>.</p> 26481cb0ef41Sopenharmony_ci<p>This function is also available under the <code>readBigUint64BE</code> alias.</p> 26491cb0ef41Sopenharmony_ci 26501cb0ef41Sopenharmony_ci<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>; 26511cb0ef41Sopenharmony_ci 26521cb0ef41Sopenharmony_ci<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>]); 26531cb0ef41Sopenharmony_ci 26541cb0ef41Sopenharmony_ci<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>)); 26551cb0ef41Sopenharmony_ci<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>); 26561cb0ef41Sopenharmony_ci 26571cb0ef41Sopenharmony_ci<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>]); 26581cb0ef41Sopenharmony_ci 26591cb0ef41Sopenharmony_ci<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>)); 26601cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 4294967295n</span></code><button class="copy-button">copy</button></pre> 26611cb0ef41Sopenharmony_ci<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> 26621cb0ef41Sopenharmony_ci<div class="api_metadata"> 26631cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 26641cb0ef41Sopenharmony_ci<table> 26651cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 26661cb0ef41Sopenharmony_ci<tr><td>v14.10.0, v12.19.0</td> 26671cb0ef41Sopenharmony_ci<td><p>This function is also available as <code>buf.readBigUint64LE()</code>.</p></td></tr> 26681cb0ef41Sopenharmony_ci<tr><td>v12.0.0, v10.20.0</td> 26691cb0ef41Sopenharmony_ci<td><p><span>Added in: v12.0.0, v10.20.0</span></p></td></tr> 26701cb0ef41Sopenharmony_ci</tbody></table> 26711cb0ef41Sopenharmony_ci</details> 26721cb0ef41Sopenharmony_ci</div> 26731cb0ef41Sopenharmony_ci<ul> 26741cb0ef41Sopenharmony_ci<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 26751cb0ef41Sopenharmony_cisatisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 26761cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 26771cb0ef41Sopenharmony_ci</ul> 26781cb0ef41Sopenharmony_ci<p>Reads an unsigned, little-endian 64-bit integer from <code>buf</code> at the specified 26791cb0ef41Sopenharmony_ci<code>offset</code>.</p> 26801cb0ef41Sopenharmony_ci<p>This function is also available under the <code>readBigUint64LE</code> alias.</p> 26811cb0ef41Sopenharmony_ci 26821cb0ef41Sopenharmony_ci<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>; 26831cb0ef41Sopenharmony_ci 26841cb0ef41Sopenharmony_ci<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>]); 26851cb0ef41Sopenharmony_ci 26861cb0ef41Sopenharmony_ci<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>)); 26871cb0ef41Sopenharmony_ci<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>); 26881cb0ef41Sopenharmony_ci 26891cb0ef41Sopenharmony_ci<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>]); 26901cb0ef41Sopenharmony_ci 26911cb0ef41Sopenharmony_ci<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>)); 26921cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 18446744069414584320n</span></code><button class="copy-button">copy</button></pre> 26931cb0ef41Sopenharmony_ci<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> 26941cb0ef41Sopenharmony_ci<div class="api_metadata"> 26951cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 26961cb0ef41Sopenharmony_ci<table> 26971cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 26981cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 26991cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 27001cb0ef41Sopenharmony_ci<tr><td>v0.11.15</td> 27011cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.15</span></p></td></tr> 27021cb0ef41Sopenharmony_ci</tbody></table> 27031cb0ef41Sopenharmony_ci</details> 27041cb0ef41Sopenharmony_ci</div> 27051cb0ef41Sopenharmony_ci<ul> 27061cb0ef41Sopenharmony_ci<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 27071cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 27081cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 27091cb0ef41Sopenharmony_ci</ul> 27101cb0ef41Sopenharmony_ci<p>Reads a 64-bit, big-endian double from <code>buf</code> at the specified <code>offset</code>.</p> 27111cb0ef41Sopenharmony_ci 27121cb0ef41Sopenharmony_ci<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>; 27131cb0ef41Sopenharmony_ci 27141cb0ef41Sopenharmony_ci<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>]); 27151cb0ef41Sopenharmony_ci 27161cb0ef41Sopenharmony_ci<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>)); 27171cb0ef41Sopenharmony_ci<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>); 27181cb0ef41Sopenharmony_ci 27191cb0ef41Sopenharmony_ci<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>]); 27201cb0ef41Sopenharmony_ci 27211cb0ef41Sopenharmony_ci<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>)); 27221cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 8.20788039913184e-304</span></code><button class="copy-button">copy</button></pre> 27231cb0ef41Sopenharmony_ci<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> 27241cb0ef41Sopenharmony_ci<div class="api_metadata"> 27251cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 27261cb0ef41Sopenharmony_ci<table> 27271cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 27281cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 27291cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 27301cb0ef41Sopenharmony_ci<tr><td>v0.11.15</td> 27311cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.15</span></p></td></tr> 27321cb0ef41Sopenharmony_ci</tbody></table> 27331cb0ef41Sopenharmony_ci</details> 27341cb0ef41Sopenharmony_ci</div> 27351cb0ef41Sopenharmony_ci<ul> 27361cb0ef41Sopenharmony_ci<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 27371cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 27381cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 27391cb0ef41Sopenharmony_ci</ul> 27401cb0ef41Sopenharmony_ci<p>Reads a 64-bit, little-endian double from <code>buf</code> at the specified <code>offset</code>.</p> 27411cb0ef41Sopenharmony_ci 27421cb0ef41Sopenharmony_ci<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>; 27431cb0ef41Sopenharmony_ci 27441cb0ef41Sopenharmony_ci<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>]); 27451cb0ef41Sopenharmony_ci 27461cb0ef41Sopenharmony_ci<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>)); 27471cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 5.447603722011605e-270</span> 27481cb0ef41Sopenharmony_ci<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>)); 27491cb0ef41Sopenharmony_ci<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>); 27501cb0ef41Sopenharmony_ci 27511cb0ef41Sopenharmony_ci<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>]); 27521cb0ef41Sopenharmony_ci 27531cb0ef41Sopenharmony_ci<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>)); 27541cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 5.447603722011605e-270</span> 27551cb0ef41Sopenharmony_ci<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>)); 27561cb0ef41Sopenharmony_ci<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> 27571cb0ef41Sopenharmony_ci<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> 27581cb0ef41Sopenharmony_ci<div class="api_metadata"> 27591cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 27601cb0ef41Sopenharmony_ci<table> 27611cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 27621cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 27631cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 27641cb0ef41Sopenharmony_ci<tr><td>v0.11.15</td> 27651cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.15</span></p></td></tr> 27661cb0ef41Sopenharmony_ci</tbody></table> 27671cb0ef41Sopenharmony_ci</details> 27681cb0ef41Sopenharmony_ci</div> 27691cb0ef41Sopenharmony_ci<ul> 27701cb0ef41Sopenharmony_ci<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 27711cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 27721cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 27731cb0ef41Sopenharmony_ci</ul> 27741cb0ef41Sopenharmony_ci<p>Reads a 32-bit, big-endian float from <code>buf</code> at the specified <code>offset</code>.</p> 27751cb0ef41Sopenharmony_ci 27761cb0ef41Sopenharmony_ci<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>; 27771cb0ef41Sopenharmony_ci 27781cb0ef41Sopenharmony_ci<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>]); 27791cb0ef41Sopenharmony_ci 27801cb0ef41Sopenharmony_ci<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>)); 27811cb0ef41Sopenharmony_ci<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>); 27821cb0ef41Sopenharmony_ci 27831cb0ef41Sopenharmony_ci<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>]); 27841cb0ef41Sopenharmony_ci 27851cb0ef41Sopenharmony_ci<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>)); 27861cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 2.387939260590663e-38</span></code><button class="copy-button">copy</button></pre> 27871cb0ef41Sopenharmony_ci<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> 27881cb0ef41Sopenharmony_ci<div class="api_metadata"> 27891cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 27901cb0ef41Sopenharmony_ci<table> 27911cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 27921cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 27931cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 27941cb0ef41Sopenharmony_ci<tr><td>v0.11.15</td> 27951cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.15</span></p></td></tr> 27961cb0ef41Sopenharmony_ci</tbody></table> 27971cb0ef41Sopenharmony_ci</details> 27981cb0ef41Sopenharmony_ci</div> 27991cb0ef41Sopenharmony_ci<ul> 28001cb0ef41Sopenharmony_ci<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 28011cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 28021cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 28031cb0ef41Sopenharmony_ci</ul> 28041cb0ef41Sopenharmony_ci<p>Reads a 32-bit, little-endian float from <code>buf</code> at the specified <code>offset</code>.</p> 28051cb0ef41Sopenharmony_ci 28061cb0ef41Sopenharmony_ci<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>; 28071cb0ef41Sopenharmony_ci 28081cb0ef41Sopenharmony_ci<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>]); 28091cb0ef41Sopenharmony_ci 28101cb0ef41Sopenharmony_ci<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>)); 28111cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1.539989614439558e-36</span> 28121cb0ef41Sopenharmony_ci<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>)); 28131cb0ef41Sopenharmony_ci<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>); 28141cb0ef41Sopenharmony_ci 28151cb0ef41Sopenharmony_ci<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>]); 28161cb0ef41Sopenharmony_ci 28171cb0ef41Sopenharmony_ci<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>)); 28181cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1.539989614439558e-36</span> 28191cb0ef41Sopenharmony_ci<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>)); 28201cb0ef41Sopenharmony_ci<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> 28211cb0ef41Sopenharmony_ci<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> 28221cb0ef41Sopenharmony_ci<div class="api_metadata"> 28231cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 28241cb0ef41Sopenharmony_ci<table> 28251cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 28261cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 28271cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 28281cb0ef41Sopenharmony_ci<tr><td>v0.5.0</td> 28291cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.0</span></p></td></tr> 28301cb0ef41Sopenharmony_ci</tbody></table> 28311cb0ef41Sopenharmony_ci</details> 28321cb0ef41Sopenharmony_ci</div> 28331cb0ef41Sopenharmony_ci<ul> 28341cb0ef41Sopenharmony_ci<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 28351cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 1</code>. <strong>Default:</strong> <code>0</code>.</li> 28361cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 28371cb0ef41Sopenharmony_ci</ul> 28381cb0ef41Sopenharmony_ci<p>Reads a signed 8-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> 28391cb0ef41Sopenharmony_ci<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p> 28401cb0ef41Sopenharmony_ci 28411cb0ef41Sopenharmony_ci<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>; 28421cb0ef41Sopenharmony_ci 28431cb0ef41Sopenharmony_ci<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>]); 28441cb0ef41Sopenharmony_ci 28451cb0ef41Sopenharmony_ci<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>)); 28461cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: -1</span> 28471cb0ef41Sopenharmony_ci<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>)); 28481cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 5</span> 28491cb0ef41Sopenharmony_ci<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>)); 28501cb0ef41Sopenharmony_ci<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>); 28511cb0ef41Sopenharmony_ci 28521cb0ef41Sopenharmony_ci<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>]); 28531cb0ef41Sopenharmony_ci 28541cb0ef41Sopenharmony_ci<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>)); 28551cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: -1</span> 28561cb0ef41Sopenharmony_ci<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>)); 28571cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 5</span> 28581cb0ef41Sopenharmony_ci<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>)); 28591cb0ef41Sopenharmony_ci<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> 28601cb0ef41Sopenharmony_ci<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> 28611cb0ef41Sopenharmony_ci<div class="api_metadata"> 28621cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 28631cb0ef41Sopenharmony_ci<table> 28641cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 28651cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 28661cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 28671cb0ef41Sopenharmony_ci<tr><td>v0.5.5</td> 28681cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.5</span></p></td></tr> 28691cb0ef41Sopenharmony_ci</tbody></table> 28701cb0ef41Sopenharmony_ci</details> 28711cb0ef41Sopenharmony_ci</div> 28721cb0ef41Sopenharmony_ci<ul> 28731cb0ef41Sopenharmony_ci<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 28741cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> 28751cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 28761cb0ef41Sopenharmony_ci</ul> 28771cb0ef41Sopenharmony_ci<p>Reads a signed, big-endian 16-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> 28781cb0ef41Sopenharmony_ci<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p> 28791cb0ef41Sopenharmony_ci 28801cb0ef41Sopenharmony_ci<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>; 28811cb0ef41Sopenharmony_ci 28821cb0ef41Sopenharmony_ci<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>]); 28831cb0ef41Sopenharmony_ci 28841cb0ef41Sopenharmony_ci<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>)); 28851cb0ef41Sopenharmony_ci<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>); 28861cb0ef41Sopenharmony_ci 28871cb0ef41Sopenharmony_ci<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>]); 28881cb0ef41Sopenharmony_ci 28891cb0ef41Sopenharmony_ci<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>)); 28901cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 5</span></code><button class="copy-button">copy</button></pre> 28911cb0ef41Sopenharmony_ci<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> 28921cb0ef41Sopenharmony_ci<div class="api_metadata"> 28931cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 28941cb0ef41Sopenharmony_ci<table> 28951cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 28961cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 28971cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 28981cb0ef41Sopenharmony_ci<tr><td>v0.5.5</td> 28991cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.5</span></p></td></tr> 29001cb0ef41Sopenharmony_ci</tbody></table> 29011cb0ef41Sopenharmony_ci</details> 29021cb0ef41Sopenharmony_ci</div> 29031cb0ef41Sopenharmony_ci<ul> 29041cb0ef41Sopenharmony_ci<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 29051cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> 29061cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 29071cb0ef41Sopenharmony_ci</ul> 29081cb0ef41Sopenharmony_ci<p>Reads a signed, little-endian 16-bit integer from <code>buf</code> at the specified 29091cb0ef41Sopenharmony_ci<code>offset</code>.</p> 29101cb0ef41Sopenharmony_ci<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p> 29111cb0ef41Sopenharmony_ci 29121cb0ef41Sopenharmony_ci<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>; 29131cb0ef41Sopenharmony_ci 29141cb0ef41Sopenharmony_ci<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>]); 29151cb0ef41Sopenharmony_ci 29161cb0ef41Sopenharmony_ci<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>)); 29171cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1280</span> 29181cb0ef41Sopenharmony_ci<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>)); 29191cb0ef41Sopenharmony_ci<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>); 29201cb0ef41Sopenharmony_ci 29211cb0ef41Sopenharmony_ci<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>]); 29221cb0ef41Sopenharmony_ci 29231cb0ef41Sopenharmony_ci<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>)); 29241cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1280</span> 29251cb0ef41Sopenharmony_ci<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>)); 29261cb0ef41Sopenharmony_ci<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> 29271cb0ef41Sopenharmony_ci<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> 29281cb0ef41Sopenharmony_ci<div class="api_metadata"> 29291cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 29301cb0ef41Sopenharmony_ci<table> 29311cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 29321cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 29331cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 29341cb0ef41Sopenharmony_ci<tr><td>v0.5.5</td> 29351cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.5</span></p></td></tr> 29361cb0ef41Sopenharmony_ci</tbody></table> 29371cb0ef41Sopenharmony_ci</details> 29381cb0ef41Sopenharmony_ci</div> 29391cb0ef41Sopenharmony_ci<ul> 29401cb0ef41Sopenharmony_ci<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 29411cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 29421cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 29431cb0ef41Sopenharmony_ci</ul> 29441cb0ef41Sopenharmony_ci<p>Reads a signed, big-endian 32-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> 29451cb0ef41Sopenharmony_ci<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p> 29461cb0ef41Sopenharmony_ci 29471cb0ef41Sopenharmony_ci<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>; 29481cb0ef41Sopenharmony_ci 29491cb0ef41Sopenharmony_ci<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>]); 29501cb0ef41Sopenharmony_ci 29511cb0ef41Sopenharmony_ci<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>)); 29521cb0ef41Sopenharmony_ci<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>); 29531cb0ef41Sopenharmony_ci 29541cb0ef41Sopenharmony_ci<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>]); 29551cb0ef41Sopenharmony_ci 29561cb0ef41Sopenharmony_ci<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>)); 29571cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 5</span></code><button class="copy-button">copy</button></pre> 29581cb0ef41Sopenharmony_ci<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> 29591cb0ef41Sopenharmony_ci<div class="api_metadata"> 29601cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 29611cb0ef41Sopenharmony_ci<table> 29621cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 29631cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 29641cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 29651cb0ef41Sopenharmony_ci<tr><td>v0.5.5</td> 29661cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.5</span></p></td></tr> 29671cb0ef41Sopenharmony_ci</tbody></table> 29681cb0ef41Sopenharmony_ci</details> 29691cb0ef41Sopenharmony_ci</div> 29701cb0ef41Sopenharmony_ci<ul> 29711cb0ef41Sopenharmony_ci<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 29721cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 29731cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 29741cb0ef41Sopenharmony_ci</ul> 29751cb0ef41Sopenharmony_ci<p>Reads a signed, little-endian 32-bit integer from <code>buf</code> at the specified 29761cb0ef41Sopenharmony_ci<code>offset</code>.</p> 29771cb0ef41Sopenharmony_ci<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p> 29781cb0ef41Sopenharmony_ci 29791cb0ef41Sopenharmony_ci<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>; 29801cb0ef41Sopenharmony_ci 29811cb0ef41Sopenharmony_ci<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>]); 29821cb0ef41Sopenharmony_ci 29831cb0ef41Sopenharmony_ci<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>)); 29841cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 83886080</span> 29851cb0ef41Sopenharmony_ci<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>)); 29861cb0ef41Sopenharmony_ci<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>); 29871cb0ef41Sopenharmony_ci 29881cb0ef41Sopenharmony_ci<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>]); 29891cb0ef41Sopenharmony_ci 29901cb0ef41Sopenharmony_ci<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>)); 29911cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 83886080</span> 29921cb0ef41Sopenharmony_ci<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>)); 29931cb0ef41Sopenharmony_ci<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> 29941cb0ef41Sopenharmony_ci<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> 29951cb0ef41Sopenharmony_ci<div class="api_metadata"> 29961cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 29971cb0ef41Sopenharmony_ci<table> 29981cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 29991cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 30001cb0ef41Sopenharmony_ci<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> 30011cb0ef41Sopenharmony_ci<tr><td>v0.11.15</td> 30021cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.15</span></p></td></tr> 30031cb0ef41Sopenharmony_ci</tbody></table> 30041cb0ef41Sopenharmony_ci</details> 30051cb0ef41Sopenharmony_ci</div> 30061cb0ef41Sopenharmony_ci<ul> 30071cb0ef41Sopenharmony_ci<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 30081cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> 30091cb0ef41Sopenharmony_ci<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 30101cb0ef41Sopenharmony_ci<code>0 < byteLength <= 6</code>.</li> 30111cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 30121cb0ef41Sopenharmony_ci</ul> 30131cb0ef41Sopenharmony_ci<p>Reads <code>byteLength</code> number of bytes from <code>buf</code> at the specified <code>offset</code> 30141cb0ef41Sopenharmony_ciand interprets the result as a big-endian, two's complement signed value 30151cb0ef41Sopenharmony_cisupporting up to 48 bits of accuracy.</p> 30161cb0ef41Sopenharmony_ci 30171cb0ef41Sopenharmony_ci<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>; 30181cb0ef41Sopenharmony_ci 30191cb0ef41Sopenharmony_ci<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>]); 30201cb0ef41Sopenharmony_ci 30211cb0ef41Sopenharmony_ci<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>)); 30221cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1234567890ab</span> 30231cb0ef41Sopenharmony_ci<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>)); 30241cb0ef41Sopenharmony_ci<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span> 30251cb0ef41Sopenharmony_ci<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>)); 30261cb0ef41Sopenharmony_ci<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>); 30271cb0ef41Sopenharmony_ci 30281cb0ef41Sopenharmony_ci<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>]); 30291cb0ef41Sopenharmony_ci 30301cb0ef41Sopenharmony_ci<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>)); 30311cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1234567890ab</span> 30321cb0ef41Sopenharmony_ci<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>)); 30331cb0ef41Sopenharmony_ci<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span> 30341cb0ef41Sopenharmony_ci<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>)); 30351cb0ef41Sopenharmony_ci<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> 30361cb0ef41Sopenharmony_ci<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> 30371cb0ef41Sopenharmony_ci<div class="api_metadata"> 30381cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 30391cb0ef41Sopenharmony_ci<table> 30401cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 30411cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 30421cb0ef41Sopenharmony_ci<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> 30431cb0ef41Sopenharmony_ci<tr><td>v0.11.15</td> 30441cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.15</span></p></td></tr> 30451cb0ef41Sopenharmony_ci</tbody></table> 30461cb0ef41Sopenharmony_ci</details> 30471cb0ef41Sopenharmony_ci</div> 30481cb0ef41Sopenharmony_ci<ul> 30491cb0ef41Sopenharmony_ci<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 30501cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> 30511cb0ef41Sopenharmony_ci<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 30521cb0ef41Sopenharmony_ci<code>0 < byteLength <= 6</code>.</li> 30531cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 30541cb0ef41Sopenharmony_ci</ul> 30551cb0ef41Sopenharmony_ci<p>Reads <code>byteLength</code> number of bytes from <code>buf</code> at the specified <code>offset</code> 30561cb0ef41Sopenharmony_ciand interprets the result as a little-endian, two's complement signed value 30571cb0ef41Sopenharmony_cisupporting up to 48 bits of accuracy.</p> 30581cb0ef41Sopenharmony_ci 30591cb0ef41Sopenharmony_ci<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>; 30601cb0ef41Sopenharmony_ci 30611cb0ef41Sopenharmony_ci<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>]); 30621cb0ef41Sopenharmony_ci 30631cb0ef41Sopenharmony_ci<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>)); 30641cb0ef41Sopenharmony_ci<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>); 30651cb0ef41Sopenharmony_ci 30661cb0ef41Sopenharmony_ci<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>]); 30671cb0ef41Sopenharmony_ci 30681cb0ef41Sopenharmony_ci<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>)); 30691cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: -546f87a9cbee</span></code><button class="copy-button">copy</button></pre> 30701cb0ef41Sopenharmony_ci<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> 30711cb0ef41Sopenharmony_ci<div class="api_metadata"> 30721cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 30731cb0ef41Sopenharmony_ci<table> 30741cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 30751cb0ef41Sopenharmony_ci<tr><td>v14.9.0, v12.19.0</td> 30761cb0ef41Sopenharmony_ci<td><p>This function is also available as <code>buf.readUint8()</code>.</p></td></tr> 30771cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 30781cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 30791cb0ef41Sopenharmony_ci<tr><td>v0.5.0</td> 30801cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.0</span></p></td></tr> 30811cb0ef41Sopenharmony_ci</tbody></table> 30821cb0ef41Sopenharmony_ci</details> 30831cb0ef41Sopenharmony_ci</div> 30841cb0ef41Sopenharmony_ci<ul> 30851cb0ef41Sopenharmony_ci<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 30861cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 1</code>. <strong>Default:</strong> <code>0</code>.</li> 30871cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 30881cb0ef41Sopenharmony_ci</ul> 30891cb0ef41Sopenharmony_ci<p>Reads an unsigned 8-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> 30901cb0ef41Sopenharmony_ci<p>This function is also available under the <code>readUint8</code> alias.</p> 30911cb0ef41Sopenharmony_ci 30921cb0ef41Sopenharmony_ci<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>; 30931cb0ef41Sopenharmony_ci 30941cb0ef41Sopenharmony_ci<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>]); 30951cb0ef41Sopenharmony_ci 30961cb0ef41Sopenharmony_ci<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>)); 30971cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1</span> 30981cb0ef41Sopenharmony_ci<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>)); 30991cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 254</span> 31001cb0ef41Sopenharmony_ci<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>)); 31011cb0ef41Sopenharmony_ci<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>); 31021cb0ef41Sopenharmony_ci 31031cb0ef41Sopenharmony_ci<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>]); 31041cb0ef41Sopenharmony_ci 31051cb0ef41Sopenharmony_ci<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>)); 31061cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1</span> 31071cb0ef41Sopenharmony_ci<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>)); 31081cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 254</span> 31091cb0ef41Sopenharmony_ci<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>)); 31101cb0ef41Sopenharmony_ci<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> 31111cb0ef41Sopenharmony_ci<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> 31121cb0ef41Sopenharmony_ci<div class="api_metadata"> 31131cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 31141cb0ef41Sopenharmony_ci<table> 31151cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 31161cb0ef41Sopenharmony_ci<tr><td>v14.9.0, v12.19.0</td> 31171cb0ef41Sopenharmony_ci<td><p>This function is also available as <code>buf.readUint16BE()</code>.</p></td></tr> 31181cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 31191cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 31201cb0ef41Sopenharmony_ci<tr><td>v0.5.5</td> 31211cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.5</span></p></td></tr> 31221cb0ef41Sopenharmony_ci</tbody></table> 31231cb0ef41Sopenharmony_ci</details> 31241cb0ef41Sopenharmony_ci</div> 31251cb0ef41Sopenharmony_ci<ul> 31261cb0ef41Sopenharmony_ci<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 31271cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> 31281cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 31291cb0ef41Sopenharmony_ci</ul> 31301cb0ef41Sopenharmony_ci<p>Reads an unsigned, big-endian 16-bit integer from <code>buf</code> at the specified 31311cb0ef41Sopenharmony_ci<code>offset</code>.</p> 31321cb0ef41Sopenharmony_ci<p>This function is also available under the <code>readUint16BE</code> alias.</p> 31331cb0ef41Sopenharmony_ci 31341cb0ef41Sopenharmony_ci<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>; 31351cb0ef41Sopenharmony_ci 31361cb0ef41Sopenharmony_ci<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>]); 31371cb0ef41Sopenharmony_ci 31381cb0ef41Sopenharmony_ci<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>)); 31391cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1234</span> 31401cb0ef41Sopenharmony_ci<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>)); 31411cb0ef41Sopenharmony_ci<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>); 31421cb0ef41Sopenharmony_ci 31431cb0ef41Sopenharmony_ci<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>]); 31441cb0ef41Sopenharmony_ci 31451cb0ef41Sopenharmony_ci<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>)); 31461cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1234</span> 31471cb0ef41Sopenharmony_ci<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>)); 31481cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 3456</span></code><button class="copy-button">copy</button></pre> 31491cb0ef41Sopenharmony_ci<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> 31501cb0ef41Sopenharmony_ci<div class="api_metadata"> 31511cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 31521cb0ef41Sopenharmony_ci<table> 31531cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 31541cb0ef41Sopenharmony_ci<tr><td>v14.9.0, v12.19.0</td> 31551cb0ef41Sopenharmony_ci<td><p>This function is also available as <code>buf.readUint16LE()</code>.</p></td></tr> 31561cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 31571cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 31581cb0ef41Sopenharmony_ci<tr><td>v0.5.5</td> 31591cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.5</span></p></td></tr> 31601cb0ef41Sopenharmony_ci</tbody></table> 31611cb0ef41Sopenharmony_ci</details> 31621cb0ef41Sopenharmony_ci</div> 31631cb0ef41Sopenharmony_ci<ul> 31641cb0ef41Sopenharmony_ci<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 31651cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> 31661cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 31671cb0ef41Sopenharmony_ci</ul> 31681cb0ef41Sopenharmony_ci<p>Reads an unsigned, little-endian 16-bit integer from <code>buf</code> at the specified 31691cb0ef41Sopenharmony_ci<code>offset</code>.</p> 31701cb0ef41Sopenharmony_ci<p>This function is also available under the <code>readUint16LE</code> alias.</p> 31711cb0ef41Sopenharmony_ci 31721cb0ef41Sopenharmony_ci<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>; 31731cb0ef41Sopenharmony_ci 31741cb0ef41Sopenharmony_ci<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>]); 31751cb0ef41Sopenharmony_ci 31761cb0ef41Sopenharmony_ci<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>)); 31771cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 3412</span> 31781cb0ef41Sopenharmony_ci<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>)); 31791cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 5634</span> 31801cb0ef41Sopenharmony_ci<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>)); 31811cb0ef41Sopenharmony_ci<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>); 31821cb0ef41Sopenharmony_ci 31831cb0ef41Sopenharmony_ci<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>]); 31841cb0ef41Sopenharmony_ci 31851cb0ef41Sopenharmony_ci<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>)); 31861cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 3412</span> 31871cb0ef41Sopenharmony_ci<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>)); 31881cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 5634</span> 31891cb0ef41Sopenharmony_ci<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>)); 31901cb0ef41Sopenharmony_ci<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> 31911cb0ef41Sopenharmony_ci<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> 31921cb0ef41Sopenharmony_ci<div class="api_metadata"> 31931cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 31941cb0ef41Sopenharmony_ci<table> 31951cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 31961cb0ef41Sopenharmony_ci<tr><td>v14.9.0, v12.19.0</td> 31971cb0ef41Sopenharmony_ci<td><p>This function is also available as <code>buf.readUint32BE()</code>.</p></td></tr> 31981cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 31991cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 32001cb0ef41Sopenharmony_ci<tr><td>v0.5.5</td> 32011cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.5</span></p></td></tr> 32021cb0ef41Sopenharmony_ci</tbody></table> 32031cb0ef41Sopenharmony_ci</details> 32041cb0ef41Sopenharmony_ci</div> 32051cb0ef41Sopenharmony_ci<ul> 32061cb0ef41Sopenharmony_ci<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 32071cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 32081cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 32091cb0ef41Sopenharmony_ci</ul> 32101cb0ef41Sopenharmony_ci<p>Reads an unsigned, big-endian 32-bit integer from <code>buf</code> at the specified 32111cb0ef41Sopenharmony_ci<code>offset</code>.</p> 32121cb0ef41Sopenharmony_ci<p>This function is also available under the <code>readUint32BE</code> alias.</p> 32131cb0ef41Sopenharmony_ci 32141cb0ef41Sopenharmony_ci<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>; 32151cb0ef41Sopenharmony_ci 32161cb0ef41Sopenharmony_ci<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>]); 32171cb0ef41Sopenharmony_ci 32181cb0ef41Sopenharmony_ci<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>)); 32191cb0ef41Sopenharmony_ci<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>); 32201cb0ef41Sopenharmony_ci 32211cb0ef41Sopenharmony_ci<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>]); 32221cb0ef41Sopenharmony_ci 32231cb0ef41Sopenharmony_ci<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>)); 32241cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 12345678</span></code><button class="copy-button">copy</button></pre> 32251cb0ef41Sopenharmony_ci<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> 32261cb0ef41Sopenharmony_ci<div class="api_metadata"> 32271cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 32281cb0ef41Sopenharmony_ci<table> 32291cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 32301cb0ef41Sopenharmony_ci<tr><td>v14.9.0, v12.19.0</td> 32311cb0ef41Sopenharmony_ci<td><p>This function is also available as <code>buf.readUint32LE()</code>.</p></td></tr> 32321cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 32331cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 32341cb0ef41Sopenharmony_ci<tr><td>v0.5.5</td> 32351cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.5</span></p></td></tr> 32361cb0ef41Sopenharmony_ci</tbody></table> 32371cb0ef41Sopenharmony_ci</details> 32381cb0ef41Sopenharmony_ci</div> 32391cb0ef41Sopenharmony_ci<ul> 32401cb0ef41Sopenharmony_ci<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 32411cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 32421cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 32431cb0ef41Sopenharmony_ci</ul> 32441cb0ef41Sopenharmony_ci<p>Reads an unsigned, little-endian 32-bit integer from <code>buf</code> at the specified 32451cb0ef41Sopenharmony_ci<code>offset</code>.</p> 32461cb0ef41Sopenharmony_ci<p>This function is also available under the <code>readUint32LE</code> alias.</p> 32471cb0ef41Sopenharmony_ci 32481cb0ef41Sopenharmony_ci<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>; 32491cb0ef41Sopenharmony_ci 32501cb0ef41Sopenharmony_ci<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>]); 32511cb0ef41Sopenharmony_ci 32521cb0ef41Sopenharmony_ci<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>)); 32531cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 78563412</span> 32541cb0ef41Sopenharmony_ci<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>)); 32551cb0ef41Sopenharmony_ci<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>); 32561cb0ef41Sopenharmony_ci 32571cb0ef41Sopenharmony_ci<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>]); 32581cb0ef41Sopenharmony_ci 32591cb0ef41Sopenharmony_ci<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>)); 32601cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 78563412</span> 32611cb0ef41Sopenharmony_ci<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>)); 32621cb0ef41Sopenharmony_ci<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> 32631cb0ef41Sopenharmony_ci<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> 32641cb0ef41Sopenharmony_ci<div class="api_metadata"> 32651cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 32661cb0ef41Sopenharmony_ci<table> 32671cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 32681cb0ef41Sopenharmony_ci<tr><td>v14.9.0, v12.19.0</td> 32691cb0ef41Sopenharmony_ci<td><p>This function is also available as <code>buf.readUintBE()</code>.</p></td></tr> 32701cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 32711cb0ef41Sopenharmony_ci<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> 32721cb0ef41Sopenharmony_ci<tr><td>v0.11.15</td> 32731cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.15</span></p></td></tr> 32741cb0ef41Sopenharmony_ci</tbody></table> 32751cb0ef41Sopenharmony_ci</details> 32761cb0ef41Sopenharmony_ci</div> 32771cb0ef41Sopenharmony_ci<ul> 32781cb0ef41Sopenharmony_ci<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 32791cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> 32801cb0ef41Sopenharmony_ci<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 32811cb0ef41Sopenharmony_ci<code>0 < byteLength <= 6</code>.</li> 32821cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 32831cb0ef41Sopenharmony_ci</ul> 32841cb0ef41Sopenharmony_ci<p>Reads <code>byteLength</code> number of bytes from <code>buf</code> at the specified <code>offset</code> 32851cb0ef41Sopenharmony_ciand interprets the result as an unsigned big-endian integer supporting 32861cb0ef41Sopenharmony_ciup to 48 bits of accuracy.</p> 32871cb0ef41Sopenharmony_ci<p>This function is also available under the <code>readUintBE</code> alias.</p> 32881cb0ef41Sopenharmony_ci 32891cb0ef41Sopenharmony_ci<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>; 32901cb0ef41Sopenharmony_ci 32911cb0ef41Sopenharmony_ci<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>]); 32921cb0ef41Sopenharmony_ci 32931cb0ef41Sopenharmony_ci<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>)); 32941cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1234567890ab</span> 32951cb0ef41Sopenharmony_ci<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>)); 32961cb0ef41Sopenharmony_ci<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>); 32971cb0ef41Sopenharmony_ci 32981cb0ef41Sopenharmony_ci<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>]); 32991cb0ef41Sopenharmony_ci 33001cb0ef41Sopenharmony_ci<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>)); 33011cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1234567890ab</span> 33021cb0ef41Sopenharmony_ci<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>)); 33031cb0ef41Sopenharmony_ci<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> 33041cb0ef41Sopenharmony_ci<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> 33051cb0ef41Sopenharmony_ci<div class="api_metadata"> 33061cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 33071cb0ef41Sopenharmony_ci<table> 33081cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 33091cb0ef41Sopenharmony_ci<tr><td>v14.9.0, v12.19.0</td> 33101cb0ef41Sopenharmony_ci<td><p>This function is also available as <code>buf.readUintLE()</code>.</p></td></tr> 33111cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 33121cb0ef41Sopenharmony_ci<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> 33131cb0ef41Sopenharmony_ci<tr><td>v0.11.15</td> 33141cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.15</span></p></td></tr> 33151cb0ef41Sopenharmony_ci</tbody></table> 33161cb0ef41Sopenharmony_ci</details> 33171cb0ef41Sopenharmony_ci</div> 33181cb0ef41Sopenharmony_ci<ul> 33191cb0ef41Sopenharmony_ci<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 33201cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> 33211cb0ef41Sopenharmony_ci<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 33221cb0ef41Sopenharmony_ci<code>0 < byteLength <= 6</code>.</li> 33231cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 33241cb0ef41Sopenharmony_ci</ul> 33251cb0ef41Sopenharmony_ci<p>Reads <code>byteLength</code> number of bytes from <code>buf</code> at the specified <code>offset</code> 33261cb0ef41Sopenharmony_ciand interprets the result as an unsigned, little-endian integer supporting 33271cb0ef41Sopenharmony_ciup to 48 bits of accuracy.</p> 33281cb0ef41Sopenharmony_ci<p>This function is also available under the <code>readUintLE</code> alias.</p> 33291cb0ef41Sopenharmony_ci 33301cb0ef41Sopenharmony_ci<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>; 33311cb0ef41Sopenharmony_ci 33321cb0ef41Sopenharmony_ci<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>]); 33331cb0ef41Sopenharmony_ci 33341cb0ef41Sopenharmony_ci<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>)); 33351cb0ef41Sopenharmony_ci<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>); 33361cb0ef41Sopenharmony_ci 33371cb0ef41Sopenharmony_ci<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>]); 33381cb0ef41Sopenharmony_ci 33391cb0ef41Sopenharmony_ci<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>)); 33401cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: ab9078563412</span></code><button class="copy-button">copy</button></pre> 33411cb0ef41Sopenharmony_ci<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> 33421cb0ef41Sopenharmony_ci<div class="api_metadata"> 33431cb0ef41Sopenharmony_ci<span>Added in: v3.0.0</span> 33441cb0ef41Sopenharmony_ci</div> 33451cb0ef41Sopenharmony_ci<ul> 33461cb0ef41Sopenharmony_ci<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> 33471cb0ef41Sopenharmony_ci<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). 33481cb0ef41Sopenharmony_ci<strong>Default:</strong> <a href="#buflength"><code>buf.length</code></a>.</li> 33491cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 33501cb0ef41Sopenharmony_ci</ul> 33511cb0ef41Sopenharmony_ci<p>Returns a new <code>Buffer</code> that references the same memory as the original, but 33521cb0ef41Sopenharmony_cioffset and cropped by the <code>start</code> and <code>end</code> indices.</p> 33531cb0ef41Sopenharmony_ci<p>Specifying <code>end</code> greater than <a href="#buflength"><code>buf.length</code></a> will return the same result as 33541cb0ef41Sopenharmony_cithat of <code>end</code> equal to <a href="#buflength"><code>buf.length</code></a>.</p> 33551cb0ef41Sopenharmony_ci<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> 33561cb0ef41Sopenharmony_ci<p>Modifying the new <code>Buffer</code> slice will modify the memory in the original <code>Buffer</code> 33571cb0ef41Sopenharmony_cibecause the allocated memory of the two objects overlap.</p> 33581cb0ef41Sopenharmony_ci 33591cb0ef41Sopenharmony_ci<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>; 33601cb0ef41Sopenharmony_ci 33611cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create a `Buffer` with the ASCII alphabet, take a slice, and modify one byte</span> 33621cb0ef41Sopenharmony_ci<span class="hljs-comment">// from the original `Buffer`.</span> 33631cb0ef41Sopenharmony_ci 33641cb0ef41Sopenharmony_ci<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>); 33651cb0ef41Sopenharmony_ci 33661cb0ef41Sopenharmony_ci<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++) { 33671cb0ef41Sopenharmony_ci <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> 33681cb0ef41Sopenharmony_ci buf1[i] = i + <span class="hljs-number">97</span>; 33691cb0ef41Sopenharmony_ci} 33701cb0ef41Sopenharmony_ci 33711cb0ef41Sopenharmony_ci<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>); 33721cb0ef41Sopenharmony_ci 33731cb0ef41Sopenharmony_ci<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>)); 33741cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: abc</span> 33751cb0ef41Sopenharmony_ci 33761cb0ef41Sopenharmony_cibuf1[<span class="hljs-number">0</span>] = <span class="hljs-number">33</span>; 33771cb0ef41Sopenharmony_ci 33781cb0ef41Sopenharmony_ci<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>)); 33791cb0ef41Sopenharmony_ci<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>); 33801cb0ef41Sopenharmony_ci 33811cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create a `Buffer` with the ASCII alphabet, take a slice, and modify one byte</span> 33821cb0ef41Sopenharmony_ci<span class="hljs-comment">// from the original `Buffer`.</span> 33831cb0ef41Sopenharmony_ci 33841cb0ef41Sopenharmony_ci<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>); 33851cb0ef41Sopenharmony_ci 33861cb0ef41Sopenharmony_ci<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++) { 33871cb0ef41Sopenharmony_ci <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> 33881cb0ef41Sopenharmony_ci buf1[i] = i + <span class="hljs-number">97</span>; 33891cb0ef41Sopenharmony_ci} 33901cb0ef41Sopenharmony_ci 33911cb0ef41Sopenharmony_ci<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>); 33921cb0ef41Sopenharmony_ci 33931cb0ef41Sopenharmony_ci<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>)); 33941cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: abc</span> 33951cb0ef41Sopenharmony_ci 33961cb0ef41Sopenharmony_cibuf1[<span class="hljs-number">0</span>] = <span class="hljs-number">33</span>; 33971cb0ef41Sopenharmony_ci 33981cb0ef41Sopenharmony_ci<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>)); 33991cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: !bc</span></code><button class="copy-button">copy</button></pre> 34001cb0ef41Sopenharmony_ci<p>Specifying negative indexes causes the slice to be generated relative to the 34011cb0ef41Sopenharmony_ciend of <code>buf</code> rather than the beginning.</p> 34021cb0ef41Sopenharmony_ci 34031cb0ef41Sopenharmony_ci<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>; 34041cb0ef41Sopenharmony_ci 34051cb0ef41Sopenharmony_ci<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>); 34061cb0ef41Sopenharmony_ci 34071cb0ef41Sopenharmony_ci<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>()); 34081cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: buffe</span> 34091cb0ef41Sopenharmony_ci<span class="hljs-comment">// (Equivalent to buf.subarray(0, 5).)</span> 34101cb0ef41Sopenharmony_ci 34111cb0ef41Sopenharmony_ci<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>()); 34121cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: buff</span> 34131cb0ef41Sopenharmony_ci<span class="hljs-comment">// (Equivalent to buf.subarray(0, 4).)</span> 34141cb0ef41Sopenharmony_ci 34151cb0ef41Sopenharmony_ci<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>()); 34161cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: uff</span> 34171cb0ef41Sopenharmony_ci<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>); 34181cb0ef41Sopenharmony_ci 34191cb0ef41Sopenharmony_ci<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>); 34201cb0ef41Sopenharmony_ci 34211cb0ef41Sopenharmony_ci<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>()); 34221cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: buffe</span> 34231cb0ef41Sopenharmony_ci<span class="hljs-comment">// (Equivalent to buf.subarray(0, 5).)</span> 34241cb0ef41Sopenharmony_ci 34251cb0ef41Sopenharmony_ci<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>()); 34261cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: buff</span> 34271cb0ef41Sopenharmony_ci<span class="hljs-comment">// (Equivalent to buf.subarray(0, 4).)</span> 34281cb0ef41Sopenharmony_ci 34291cb0ef41Sopenharmony_ci<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>()); 34301cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: uff</span> 34311cb0ef41Sopenharmony_ci<span class="hljs-comment">// (Equivalent to buf.subarray(1, 4).)</span></code><button class="copy-button">copy</button></pre> 34321cb0ef41Sopenharmony_ci<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> 34331cb0ef41Sopenharmony_ci<div class="api_metadata"> 34341cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 34351cb0ef41Sopenharmony_ci<table> 34361cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 34371cb0ef41Sopenharmony_ci<tr><td>v17.5.0</td> 34381cb0ef41Sopenharmony_ci<td><p>The buf.slice() method has been deprecated.</p></td></tr> 34391cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td> 34401cb0ef41Sopenharmony_ci<td><p>All offsets are now coerced to integers before doing any calculations with them.</p></td></tr> 34411cb0ef41Sopenharmony_ci<tr><td>v7.1.0, v6.9.2</td> 34421cb0ef41Sopenharmony_ci<td><p>Coercing the offsets to integers now handles values outside the 32-bit integer range properly.</p></td></tr> 34431cb0ef41Sopenharmony_ci<tr><td>v0.3.0</td> 34441cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.3.0</span></p></td></tr> 34451cb0ef41Sopenharmony_ci</tbody></table> 34461cb0ef41Sopenharmony_ci</details> 34471cb0ef41Sopenharmony_ci</div> 34481cb0ef41Sopenharmony_ci<ul> 34491cb0ef41Sopenharmony_ci<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> 34501cb0ef41Sopenharmony_ci<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). 34511cb0ef41Sopenharmony_ci<strong>Default:</strong> <a href="#buflength"><code>buf.length</code></a>.</li> 34521cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 34531cb0ef41Sopenharmony_ci</ul> 34541cb0ef41Sopenharmony_ci<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> 34551cb0ef41Sopenharmony_ci<p>Returns a new <code>Buffer</code> that references the same memory as the original, but 34561cb0ef41Sopenharmony_cioffset and cropped by the <code>start</code> and <code>end</code> indices.</p> 34571cb0ef41Sopenharmony_ci<p>This method is not compatible with the <code>Uint8Array.prototype.slice()</code>, 34581cb0ef41Sopenharmony_ciwhich is a superclass of <code>Buffer</code>. To copy the slice, use 34591cb0ef41Sopenharmony_ci<code>Uint8Array.prototype.slice()</code>.</p> 34601cb0ef41Sopenharmony_ci 34611cb0ef41Sopenharmony_ci<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>; 34621cb0ef41Sopenharmony_ci 34631cb0ef41Sopenharmony_ci<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>); 34641cb0ef41Sopenharmony_ci 34651cb0ef41Sopenharmony_ci<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); 34661cb0ef41Sopenharmony_cicopiedBuf[<span class="hljs-number">0</span>]++; 34671cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(copiedBuf.<span class="hljs-title function_">toString</span>()); 34681cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: cuffer</span> 34691cb0ef41Sopenharmony_ci 34701cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>()); 34711cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: buffer</span> 34721cb0ef41Sopenharmony_ci 34731cb0ef41Sopenharmony_ci<span class="hljs-comment">// With buf.slice(), the original buffer is modified.</span> 34741cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> notReallyCopiedBuf = buf.<span class="hljs-title function_">slice</span>(); 34751cb0ef41Sopenharmony_cinotReallyCopiedBuf[<span class="hljs-number">0</span>]++; 34761cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(notReallyCopiedBuf.<span class="hljs-title function_">toString</span>()); 34771cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: cuffer</span> 34781cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>()); 34791cb0ef41Sopenharmony_ci<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>); 34801cb0ef41Sopenharmony_ci 34811cb0ef41Sopenharmony_ci<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>); 34821cb0ef41Sopenharmony_ci 34831cb0ef41Sopenharmony_ci<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); 34841cb0ef41Sopenharmony_cicopiedBuf[<span class="hljs-number">0</span>]++; 34851cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(copiedBuf.<span class="hljs-title function_">toString</span>()); 34861cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: cuffer</span> 34871cb0ef41Sopenharmony_ci 34881cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>()); 34891cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: buffer</span> 34901cb0ef41Sopenharmony_ci 34911cb0ef41Sopenharmony_ci<span class="hljs-comment">// With buf.slice(), the original buffer is modified.</span> 34921cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> notReallyCopiedBuf = buf.<span class="hljs-title function_">slice</span>(); 34931cb0ef41Sopenharmony_cinotReallyCopiedBuf[<span class="hljs-number">0</span>]++; 34941cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(notReallyCopiedBuf.<span class="hljs-title function_">toString</span>()); 34951cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: cuffer</span> 34961cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>()); 34971cb0ef41Sopenharmony_ci<span class="hljs-comment">// Also prints: cuffer (!)</span></code><button class="copy-button">copy</button></pre> 34981cb0ef41Sopenharmony_ci<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> 34991cb0ef41Sopenharmony_ci<div class="api_metadata"> 35001cb0ef41Sopenharmony_ci<span>Added in: v5.10.0</span> 35011cb0ef41Sopenharmony_ci</div> 35021cb0ef41Sopenharmony_ci<ul> 35031cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> A reference to <code>buf</code>.</li> 35041cb0ef41Sopenharmony_ci</ul> 35051cb0ef41Sopenharmony_ci<p>Interprets <code>buf</code> as an array of unsigned 16-bit integers and swaps the 35061cb0ef41Sopenharmony_cibyte 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> 35071cb0ef41Sopenharmony_ciis not a multiple of 2.</p> 35081cb0ef41Sopenharmony_ci 35091cb0ef41Sopenharmony_ci<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>; 35101cb0ef41Sopenharmony_ci 35111cb0ef41Sopenharmony_ci<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>]); 35121cb0ef41Sopenharmony_ci 35131cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 35141cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05 06 07 08></span> 35151cb0ef41Sopenharmony_ci 35161cb0ef41Sopenharmony_cibuf1.<span class="hljs-title function_">swap16</span>(); 35171cb0ef41Sopenharmony_ci 35181cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 35191cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 02 01 04 03 06 05 08 07></span> 35201cb0ef41Sopenharmony_ci 35211cb0ef41Sopenharmony_ci<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>]); 35221cb0ef41Sopenharmony_ci 35231cb0ef41Sopenharmony_cibuf2.<span class="hljs-title function_">swap16</span>(); 35241cb0ef41Sopenharmony_ci<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>); 35251cb0ef41Sopenharmony_ci 35261cb0ef41Sopenharmony_ci<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>]); 35271cb0ef41Sopenharmony_ci 35281cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 35291cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05 06 07 08></span> 35301cb0ef41Sopenharmony_ci 35311cb0ef41Sopenharmony_cibuf1.<span class="hljs-title function_">swap16</span>(); 35321cb0ef41Sopenharmony_ci 35331cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 35341cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 02 01 04 03 06 05 08 07></span> 35351cb0ef41Sopenharmony_ci 35361cb0ef41Sopenharmony_ci<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>]); 35371cb0ef41Sopenharmony_ci 35381cb0ef41Sopenharmony_cibuf2.<span class="hljs-title function_">swap16</span>(); 35391cb0ef41Sopenharmony_ci<span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code><button class="copy-button">copy</button></pre> 35401cb0ef41Sopenharmony_ci<p>One convenient use of <code>buf.swap16()</code> is to perform a fast in-place conversion 35411cb0ef41Sopenharmony_cibetween UTF-16 little-endian and UTF-16 big-endian:</p> 35421cb0ef41Sopenharmony_ci 35431cb0ef41Sopenharmony_ci<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>; 35441cb0ef41Sopenharmony_ci 35451cb0ef41Sopenharmony_ci<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>); 35461cb0ef41Sopenharmony_cibuf.<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>); 35471cb0ef41Sopenharmony_ci 35481cb0ef41Sopenharmony_ci<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>); 35491cb0ef41Sopenharmony_cibuf.<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> 35501cb0ef41Sopenharmony_ci<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> 35511cb0ef41Sopenharmony_ci<div class="api_metadata"> 35521cb0ef41Sopenharmony_ci<span>Added in: v5.10.0</span> 35531cb0ef41Sopenharmony_ci</div> 35541cb0ef41Sopenharmony_ci<ul> 35551cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> A reference to <code>buf</code>.</li> 35561cb0ef41Sopenharmony_ci</ul> 35571cb0ef41Sopenharmony_ci<p>Interprets <code>buf</code> as an array of unsigned 32-bit integers and swaps the 35581cb0ef41Sopenharmony_cibyte 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> 35591cb0ef41Sopenharmony_ciis not a multiple of 4.</p> 35601cb0ef41Sopenharmony_ci 35611cb0ef41Sopenharmony_ci<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>; 35621cb0ef41Sopenharmony_ci 35631cb0ef41Sopenharmony_ci<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>]); 35641cb0ef41Sopenharmony_ci 35651cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 35661cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05 06 07 08></span> 35671cb0ef41Sopenharmony_ci 35681cb0ef41Sopenharmony_cibuf1.<span class="hljs-title function_">swap32</span>(); 35691cb0ef41Sopenharmony_ci 35701cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 35711cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 04 03 02 01 08 07 06 05></span> 35721cb0ef41Sopenharmony_ci 35731cb0ef41Sopenharmony_ci<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>]); 35741cb0ef41Sopenharmony_ci 35751cb0ef41Sopenharmony_cibuf2.<span class="hljs-title function_">swap32</span>(); 35761cb0ef41Sopenharmony_ci<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>); 35771cb0ef41Sopenharmony_ci 35781cb0ef41Sopenharmony_ci<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>]); 35791cb0ef41Sopenharmony_ci 35801cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 35811cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05 06 07 08></span> 35821cb0ef41Sopenharmony_ci 35831cb0ef41Sopenharmony_cibuf1.<span class="hljs-title function_">swap32</span>(); 35841cb0ef41Sopenharmony_ci 35851cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 35861cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 04 03 02 01 08 07 06 05></span> 35871cb0ef41Sopenharmony_ci 35881cb0ef41Sopenharmony_ci<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>]); 35891cb0ef41Sopenharmony_ci 35901cb0ef41Sopenharmony_cibuf2.<span class="hljs-title function_">swap32</span>(); 35911cb0ef41Sopenharmony_ci<span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code><button class="copy-button">copy</button></pre> 35921cb0ef41Sopenharmony_ci<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> 35931cb0ef41Sopenharmony_ci<div class="api_metadata"> 35941cb0ef41Sopenharmony_ci<span>Added in: v6.3.0</span> 35951cb0ef41Sopenharmony_ci</div> 35961cb0ef41Sopenharmony_ci<ul> 35971cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> A reference to <code>buf</code>.</li> 35981cb0ef41Sopenharmony_ci</ul> 35991cb0ef41Sopenharmony_ci<p>Interprets <code>buf</code> as an array of 64-bit numbers and swaps byte order <em>in-place</em>. 36001cb0ef41Sopenharmony_ciThrows <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> 36011cb0ef41Sopenharmony_ci 36021cb0ef41Sopenharmony_ci<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>; 36031cb0ef41Sopenharmony_ci 36041cb0ef41Sopenharmony_ci<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>]); 36051cb0ef41Sopenharmony_ci 36061cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 36071cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05 06 07 08></span> 36081cb0ef41Sopenharmony_ci 36091cb0ef41Sopenharmony_cibuf1.<span class="hljs-title function_">swap64</span>(); 36101cb0ef41Sopenharmony_ci 36111cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 36121cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 08 07 06 05 04 03 02 01></span> 36131cb0ef41Sopenharmony_ci 36141cb0ef41Sopenharmony_ci<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>]); 36151cb0ef41Sopenharmony_ci 36161cb0ef41Sopenharmony_cibuf2.<span class="hljs-title function_">swap64</span>(); 36171cb0ef41Sopenharmony_ci<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>); 36181cb0ef41Sopenharmony_ci 36191cb0ef41Sopenharmony_ci<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>]); 36201cb0ef41Sopenharmony_ci 36211cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 36221cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05 06 07 08></span> 36231cb0ef41Sopenharmony_ci 36241cb0ef41Sopenharmony_cibuf1.<span class="hljs-title function_">swap64</span>(); 36251cb0ef41Sopenharmony_ci 36261cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 36271cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 08 07 06 05 04 03 02 01></span> 36281cb0ef41Sopenharmony_ci 36291cb0ef41Sopenharmony_ci<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>]); 36301cb0ef41Sopenharmony_ci 36311cb0ef41Sopenharmony_cibuf2.<span class="hljs-title function_">swap64</span>(); 36321cb0ef41Sopenharmony_ci<span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code><button class="copy-button">copy</button></pre> 36331cb0ef41Sopenharmony_ci<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> 36341cb0ef41Sopenharmony_ci<div class="api_metadata"> 36351cb0ef41Sopenharmony_ci<span>Added in: v0.9.2</span> 36361cb0ef41Sopenharmony_ci</div> 36371cb0ef41Sopenharmony_ci<ul> 36381cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 36391cb0ef41Sopenharmony_ci</ul> 36401cb0ef41Sopenharmony_ci<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 36411cb0ef41Sopenharmony_cithis function when stringifying a <code>Buffer</code> instance.</p> 36421cb0ef41Sopenharmony_ci<p><code>Buffer.from()</code> accepts objects in the format returned from this method. 36431cb0ef41Sopenharmony_ciIn particular, <code>Buffer.from(buf.toJSON())</code> works like <code>Buffer.from(buf)</code>.</p> 36441cb0ef41Sopenharmony_ci 36451cb0ef41Sopenharmony_ci<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>; 36461cb0ef41Sopenharmony_ci 36471cb0ef41Sopenharmony_ci<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>]); 36481cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> json = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(buf); 36491cb0ef41Sopenharmony_ci 36501cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(json); 36511cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: {"type":"Buffer","data":[1,2,3,4,5]}</span> 36521cb0ef41Sopenharmony_ci 36531cb0ef41Sopenharmony_ci<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> { 36541cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> value && value.<span class="hljs-property">type</span> === <span class="hljs-string">'Buffer'</span> ? 36551cb0ef41Sopenharmony_ci <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(value) : 36561cb0ef41Sopenharmony_ci value; 36571cb0ef41Sopenharmony_ci}); 36581cb0ef41Sopenharmony_ci 36591cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(copy); 36601cb0ef41Sopenharmony_ci<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>); 36611cb0ef41Sopenharmony_ci 36621cb0ef41Sopenharmony_ci<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>]); 36631cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> json = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(buf); 36641cb0ef41Sopenharmony_ci 36651cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(json); 36661cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: {"type":"Buffer","data":[1,2,3,4,5]}</span> 36671cb0ef41Sopenharmony_ci 36681cb0ef41Sopenharmony_ci<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> { 36691cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> value && value.<span class="hljs-property">type</span> === <span class="hljs-string">'Buffer'</span> ? 36701cb0ef41Sopenharmony_ci <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(value) : 36711cb0ef41Sopenharmony_ci value; 36721cb0ef41Sopenharmony_ci}); 36731cb0ef41Sopenharmony_ci 36741cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(copy); 36751cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05></span></code><button class="copy-button">copy</button></pre> 36761cb0ef41Sopenharmony_ci<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> 36771cb0ef41Sopenharmony_ci<div class="api_metadata"> 36781cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 36791cb0ef41Sopenharmony_ci</div> 36801cb0ef41Sopenharmony_ci<ul> 36811cb0ef41Sopenharmony_ci<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> 36821cb0ef41Sopenharmony_ci<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> 36831cb0ef41Sopenharmony_ci<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). 36841cb0ef41Sopenharmony_ci<strong>Default:</strong> <a href="#buflength"><code>buf.length</code></a>.</li> 36851cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 36861cb0ef41Sopenharmony_ci</ul> 36871cb0ef41Sopenharmony_ci<p>Decodes <code>buf</code> to a string according to the specified character encoding in 36881cb0ef41Sopenharmony_ci<code>encoding</code>. <code>start</code> and <code>end</code> may be passed to decode only a subset of <code>buf</code>.</p> 36891cb0ef41Sopenharmony_ci<p>If <code>encoding</code> is <code>'utf8'</code> and a byte sequence in the input is not valid UTF-8, 36901cb0ef41Sopenharmony_cithen each invalid byte is replaced with the replacement character <code>U+FFFD</code>.</p> 36911cb0ef41Sopenharmony_ci<p>The maximum length of a string instance (in UTF-16 code units) is available 36921cb0ef41Sopenharmony_cias <a href="#bufferconstantsmax_string_length"><code>buffer.constants.MAX_STRING_LENGTH</code></a>.</p> 36931cb0ef41Sopenharmony_ci 36941cb0ef41Sopenharmony_ci<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>; 36951cb0ef41Sopenharmony_ci 36961cb0ef41Sopenharmony_ci<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>); 36971cb0ef41Sopenharmony_ci 36981cb0ef41Sopenharmony_ci<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++) { 36991cb0ef41Sopenharmony_ci <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> 37001cb0ef41Sopenharmony_ci buf1[i] = i + <span class="hljs-number">97</span>; 37011cb0ef41Sopenharmony_ci} 37021cb0ef41Sopenharmony_ci 37031cb0ef41Sopenharmony_ci<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>)); 37041cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: abcdefghijklmnopqrstuvwxyz</span> 37051cb0ef41Sopenharmony_ci<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>)); 37061cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: abcde</span> 37071cb0ef41Sopenharmony_ci 37081cb0ef41Sopenharmony_ci<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>); 37091cb0ef41Sopenharmony_ci 37101cb0ef41Sopenharmony_ci<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>)); 37111cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 74c3a97374</span> 37121cb0ef41Sopenharmony_ci<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>)); 37131cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: té</span> 37141cb0ef41Sopenharmony_ci<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>)); 37151cb0ef41Sopenharmony_ci<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>); 37161cb0ef41Sopenharmony_ci 37171cb0ef41Sopenharmony_ci<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>); 37181cb0ef41Sopenharmony_ci 37191cb0ef41Sopenharmony_ci<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++) { 37201cb0ef41Sopenharmony_ci <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> 37211cb0ef41Sopenharmony_ci buf1[i] = i + <span class="hljs-number">97</span>; 37221cb0ef41Sopenharmony_ci} 37231cb0ef41Sopenharmony_ci 37241cb0ef41Sopenharmony_ci<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>)); 37251cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: abcdefghijklmnopqrstuvwxyz</span> 37261cb0ef41Sopenharmony_ci<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>)); 37271cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: abcde</span> 37281cb0ef41Sopenharmony_ci 37291cb0ef41Sopenharmony_ci<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>); 37301cb0ef41Sopenharmony_ci 37311cb0ef41Sopenharmony_ci<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>)); 37321cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 74c3a97374</span> 37331cb0ef41Sopenharmony_ci<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>)); 37341cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: té</span> 37351cb0ef41Sopenharmony_ci<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>)); 37361cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: té</span></code><button class="copy-button">copy</button></pre> 37371cb0ef41Sopenharmony_ci<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> 37381cb0ef41Sopenharmony_ci<div class="api_metadata"> 37391cb0ef41Sopenharmony_ci<span>Added in: v1.1.0</span> 37401cb0ef41Sopenharmony_ci</div> 37411cb0ef41Sopenharmony_ci<ul> 37421cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> 37431cb0ef41Sopenharmony_ci</ul> 37441cb0ef41Sopenharmony_ci<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 37451cb0ef41Sopenharmony_cicalled automatically when a <code>Buffer</code> is used in a <code>for..of</code> statement.</p> 37461cb0ef41Sopenharmony_ci 37471cb0ef41Sopenharmony_ci<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>; 37481cb0ef41Sopenharmony_ci 37491cb0ef41Sopenharmony_ci<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>); 37501cb0ef41Sopenharmony_ci 37511cb0ef41Sopenharmony_ci<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>()) { 37521cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value); 37531cb0ef41Sopenharmony_ci} 37541cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 37551cb0ef41Sopenharmony_ci<span class="hljs-comment">// 98</span> 37561cb0ef41Sopenharmony_ci<span class="hljs-comment">// 117</span> 37571cb0ef41Sopenharmony_ci<span class="hljs-comment">// 102</span> 37581cb0ef41Sopenharmony_ci<span class="hljs-comment">// 102</span> 37591cb0ef41Sopenharmony_ci<span class="hljs-comment">// 101</span> 37601cb0ef41Sopenharmony_ci<span class="hljs-comment">// 114</span> 37611cb0ef41Sopenharmony_ci 37621cb0ef41Sopenharmony_ci<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> value <span class="hljs-keyword">of</span> buf) { 37631cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value); 37641cb0ef41Sopenharmony_ci} 37651cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 37661cb0ef41Sopenharmony_ci<span class="hljs-comment">// 98</span> 37671cb0ef41Sopenharmony_ci<span class="hljs-comment">// 117</span> 37681cb0ef41Sopenharmony_ci<span class="hljs-comment">// 102</span> 37691cb0ef41Sopenharmony_ci<span class="hljs-comment">// 102</span> 37701cb0ef41Sopenharmony_ci<span class="hljs-comment">// 101</span> 37711cb0ef41Sopenharmony_ci<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>); 37721cb0ef41Sopenharmony_ci 37731cb0ef41Sopenharmony_ci<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>); 37741cb0ef41Sopenharmony_ci 37751cb0ef41Sopenharmony_ci<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>()) { 37761cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value); 37771cb0ef41Sopenharmony_ci} 37781cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 37791cb0ef41Sopenharmony_ci<span class="hljs-comment">// 98</span> 37801cb0ef41Sopenharmony_ci<span class="hljs-comment">// 117</span> 37811cb0ef41Sopenharmony_ci<span class="hljs-comment">// 102</span> 37821cb0ef41Sopenharmony_ci<span class="hljs-comment">// 102</span> 37831cb0ef41Sopenharmony_ci<span class="hljs-comment">// 101</span> 37841cb0ef41Sopenharmony_ci<span class="hljs-comment">// 114</span> 37851cb0ef41Sopenharmony_ci 37861cb0ef41Sopenharmony_ci<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> value <span class="hljs-keyword">of</span> buf) { 37871cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value); 37881cb0ef41Sopenharmony_ci} 37891cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 37901cb0ef41Sopenharmony_ci<span class="hljs-comment">// 98</span> 37911cb0ef41Sopenharmony_ci<span class="hljs-comment">// 117</span> 37921cb0ef41Sopenharmony_ci<span class="hljs-comment">// 102</span> 37931cb0ef41Sopenharmony_ci<span class="hljs-comment">// 102</span> 37941cb0ef41Sopenharmony_ci<span class="hljs-comment">// 101</span> 37951cb0ef41Sopenharmony_ci<span class="hljs-comment">// 114</span></code><button class="copy-button">copy</button></pre> 37961cb0ef41Sopenharmony_ci<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> 37971cb0ef41Sopenharmony_ci<div class="api_metadata"> 37981cb0ef41Sopenharmony_ci<span>Added in: v0.1.90</span> 37991cb0ef41Sopenharmony_ci</div> 38001cb0ef41Sopenharmony_ci<ul> 38011cb0ef41Sopenharmony_ci<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> 38021cb0ef41Sopenharmony_ci<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>. 38031cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>0</code>.</li> 38041cb0ef41Sopenharmony_ci<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 38051cb0ef41Sopenharmony_ciexceed <code>buf.length - offset</code>). <strong>Default:</strong> <code>buf.length - offset</code>.</li> 38061cb0ef41Sopenharmony_ci<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> 38071cb0ef41Sopenharmony_ci<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> 38081cb0ef41Sopenharmony_ci</ul> 38091cb0ef41Sopenharmony_ci<p>Writes <code>string</code> to <code>buf</code> at <code>offset</code> according to the character encoding in 38101cb0ef41Sopenharmony_ci<code>encoding</code>. The <code>length</code> parameter is the number of bytes to write. If <code>buf</code> did 38111cb0ef41Sopenharmony_cinot contain enough space to fit the entire string, only part of <code>string</code> will be 38121cb0ef41Sopenharmony_ciwritten. However, partially encoded characters will not be written.</p> 38131cb0ef41Sopenharmony_ci 38141cb0ef41Sopenharmony_ci<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>; 38151cb0ef41Sopenharmony_ci 38161cb0ef41Sopenharmony_ci<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>); 38171cb0ef41Sopenharmony_ci 38181cb0ef41Sopenharmony_ci<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>); 38191cb0ef41Sopenharmony_ci 38201cb0ef41Sopenharmony_ci<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>); 38211cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 12 bytes: ½ + ¼ = ¾</span> 38221cb0ef41Sopenharmony_ci 38231cb0ef41Sopenharmony_ci<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>); 38241cb0ef41Sopenharmony_ci 38251cb0ef41Sopenharmony_ci<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>); 38261cb0ef41Sopenharmony_ci 38271cb0ef41Sopenharmony_ci<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>); 38281cb0ef41Sopenharmony_ci<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>); 38291cb0ef41Sopenharmony_ci 38301cb0ef41Sopenharmony_ci<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>); 38311cb0ef41Sopenharmony_ci 38321cb0ef41Sopenharmony_ci<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>); 38331cb0ef41Sopenharmony_ci 38341cb0ef41Sopenharmony_ci<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>); 38351cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 12 bytes: ½ + ¼ = ¾</span> 38361cb0ef41Sopenharmony_ci 38371cb0ef41Sopenharmony_ci<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>); 38381cb0ef41Sopenharmony_ci 38391cb0ef41Sopenharmony_ci<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>); 38401cb0ef41Sopenharmony_ci 38411cb0ef41Sopenharmony_ci<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>); 38421cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 2 bytes : ab</span></code><button class="copy-button">copy</button></pre> 38431cb0ef41Sopenharmony_ci<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> 38441cb0ef41Sopenharmony_ci<div class="api_metadata"> 38451cb0ef41Sopenharmony_ci<span>Added in: v12.0.0, v10.20.0</span> 38461cb0ef41Sopenharmony_ci</div> 38471cb0ef41Sopenharmony_ci<ul> 38481cb0ef41Sopenharmony_ci<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> 38491cb0ef41Sopenharmony_ci<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 38501cb0ef41Sopenharmony_cisatisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 38511cb0ef41Sopenharmony_ci<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> 38521cb0ef41Sopenharmony_ci</ul> 38531cb0ef41Sopenharmony_ci<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian.</p> 38541cb0ef41Sopenharmony_ci<p><code>value</code> is interpreted and written as a two's complement signed integer.</p> 38551cb0ef41Sopenharmony_ci 38561cb0ef41Sopenharmony_ci<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>; 38571cb0ef41Sopenharmony_ci 38581cb0ef41Sopenharmony_ci<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>); 38591cb0ef41Sopenharmony_ci 38601cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeBigInt64BE</span>(<span class="hljs-number">0x0102030405060708n</span>, <span class="hljs-number">0</span>); 38611cb0ef41Sopenharmony_ci 38621cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 38631cb0ef41Sopenharmony_ci<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>); 38641cb0ef41Sopenharmony_ci 38651cb0ef41Sopenharmony_ci<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>); 38661cb0ef41Sopenharmony_ci 38671cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeBigInt64BE</span>(<span class="hljs-number">0x0102030405060708n</span>, <span class="hljs-number">0</span>); 38681cb0ef41Sopenharmony_ci 38691cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 38701cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05 06 07 08></span></code><button class="copy-button">copy</button></pre> 38711cb0ef41Sopenharmony_ci<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> 38721cb0ef41Sopenharmony_ci<div class="api_metadata"> 38731cb0ef41Sopenharmony_ci<span>Added in: v12.0.0, v10.20.0</span> 38741cb0ef41Sopenharmony_ci</div> 38751cb0ef41Sopenharmony_ci<ul> 38761cb0ef41Sopenharmony_ci<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> 38771cb0ef41Sopenharmony_ci<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 38781cb0ef41Sopenharmony_cisatisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 38791cb0ef41Sopenharmony_ci<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> 38801cb0ef41Sopenharmony_ci</ul> 38811cb0ef41Sopenharmony_ci<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian.</p> 38821cb0ef41Sopenharmony_ci<p><code>value</code> is interpreted and written as a two's complement signed integer.</p> 38831cb0ef41Sopenharmony_ci 38841cb0ef41Sopenharmony_ci<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>; 38851cb0ef41Sopenharmony_ci 38861cb0ef41Sopenharmony_ci<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>); 38871cb0ef41Sopenharmony_ci 38881cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeBigInt64LE</span>(<span class="hljs-number">0x0102030405060708n</span>, <span class="hljs-number">0</span>); 38891cb0ef41Sopenharmony_ci 38901cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 38911cb0ef41Sopenharmony_ci<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>); 38921cb0ef41Sopenharmony_ci 38931cb0ef41Sopenharmony_ci<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>); 38941cb0ef41Sopenharmony_ci 38951cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeBigInt64LE</span>(<span class="hljs-number">0x0102030405060708n</span>, <span class="hljs-number">0</span>); 38961cb0ef41Sopenharmony_ci 38971cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 38981cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 08 07 06 05 04 03 02 01></span></code><button class="copy-button">copy</button></pre> 38991cb0ef41Sopenharmony_ci<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> 39001cb0ef41Sopenharmony_ci<div class="api_metadata"> 39011cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 39021cb0ef41Sopenharmony_ci<table> 39031cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 39041cb0ef41Sopenharmony_ci<tr><td>v14.10.0, v12.19.0</td> 39051cb0ef41Sopenharmony_ci<td><p>This function is also available as <code>buf.writeBigUint64BE()</code>.</p></td></tr> 39061cb0ef41Sopenharmony_ci<tr><td>v12.0.0, v10.20.0</td> 39071cb0ef41Sopenharmony_ci<td><p><span>Added in: v12.0.0, v10.20.0</span></p></td></tr> 39081cb0ef41Sopenharmony_ci</tbody></table> 39091cb0ef41Sopenharmony_ci</details> 39101cb0ef41Sopenharmony_ci</div> 39111cb0ef41Sopenharmony_ci<ul> 39121cb0ef41Sopenharmony_ci<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> 39131cb0ef41Sopenharmony_ci<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 39141cb0ef41Sopenharmony_cisatisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 39151cb0ef41Sopenharmony_ci<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> 39161cb0ef41Sopenharmony_ci</ul> 39171cb0ef41Sopenharmony_ci<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian.</p> 39181cb0ef41Sopenharmony_ci<p>This function is also available under the <code>writeBigUint64BE</code> alias.</p> 39191cb0ef41Sopenharmony_ci 39201cb0ef41Sopenharmony_ci<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>; 39211cb0ef41Sopenharmony_ci 39221cb0ef41Sopenharmony_ci<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>); 39231cb0ef41Sopenharmony_ci 39241cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeBigUInt64BE</span>(<span class="hljs-number">0xdecafafecacefaden</span>, <span class="hljs-number">0</span>); 39251cb0ef41Sopenharmony_ci 39261cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 39271cb0ef41Sopenharmony_ci<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>); 39281cb0ef41Sopenharmony_ci 39291cb0ef41Sopenharmony_ci<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>); 39301cb0ef41Sopenharmony_ci 39311cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeBigUInt64BE</span>(<span class="hljs-number">0xdecafafecacefaden</span>, <span class="hljs-number">0</span>); 39321cb0ef41Sopenharmony_ci 39331cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 39341cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer de ca fa fe ca ce fa de></span></code><button class="copy-button">copy</button></pre> 39351cb0ef41Sopenharmony_ci<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> 39361cb0ef41Sopenharmony_ci<div class="api_metadata"> 39371cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 39381cb0ef41Sopenharmony_ci<table> 39391cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 39401cb0ef41Sopenharmony_ci<tr><td>v14.10.0, v12.19.0</td> 39411cb0ef41Sopenharmony_ci<td><p>This function is also available as <code>buf.writeBigUint64LE()</code>.</p></td></tr> 39421cb0ef41Sopenharmony_ci<tr><td>v12.0.0, v10.20.0</td> 39431cb0ef41Sopenharmony_ci<td><p><span>Added in: v12.0.0, v10.20.0</span></p></td></tr> 39441cb0ef41Sopenharmony_ci</tbody></table> 39451cb0ef41Sopenharmony_ci</details> 39461cb0ef41Sopenharmony_ci</div> 39471cb0ef41Sopenharmony_ci<ul> 39481cb0ef41Sopenharmony_ci<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> 39491cb0ef41Sopenharmony_ci<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 39501cb0ef41Sopenharmony_cisatisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 39511cb0ef41Sopenharmony_ci<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> 39521cb0ef41Sopenharmony_ci</ul> 39531cb0ef41Sopenharmony_ci<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian</p> 39541cb0ef41Sopenharmony_ci 39551cb0ef41Sopenharmony_ci<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>; 39561cb0ef41Sopenharmony_ci 39571cb0ef41Sopenharmony_ci<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>); 39581cb0ef41Sopenharmony_ci 39591cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeBigUInt64LE</span>(<span class="hljs-number">0xdecafafecacefaden</span>, <span class="hljs-number">0</span>); 39601cb0ef41Sopenharmony_ci 39611cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 39621cb0ef41Sopenharmony_ci<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>); 39631cb0ef41Sopenharmony_ci 39641cb0ef41Sopenharmony_ci<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>); 39651cb0ef41Sopenharmony_ci 39661cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeBigUInt64LE</span>(<span class="hljs-number">0xdecafafecacefaden</span>, <span class="hljs-number">0</span>); 39671cb0ef41Sopenharmony_ci 39681cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 39691cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer de fa ce ca fe fa ca de></span></code><button class="copy-button">copy</button></pre> 39701cb0ef41Sopenharmony_ci<p>This function is also available under the <code>writeBigUint64LE</code> alias.</p> 39711cb0ef41Sopenharmony_ci<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> 39721cb0ef41Sopenharmony_ci<div class="api_metadata"> 39731cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 39741cb0ef41Sopenharmony_ci<table> 39751cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 39761cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 39771cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 39781cb0ef41Sopenharmony_ci<tr><td>v0.11.15</td> 39791cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.15</span></p></td></tr> 39801cb0ef41Sopenharmony_ci</tbody></table> 39811cb0ef41Sopenharmony_ci</details> 39821cb0ef41Sopenharmony_ci</div> 39831cb0ef41Sopenharmony_ci<ul> 39841cb0ef41Sopenharmony_ci<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> 39851cb0ef41Sopenharmony_ci<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 39861cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 39871cb0ef41Sopenharmony_ci<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> 39881cb0ef41Sopenharmony_ci</ul> 39891cb0ef41Sopenharmony_ci<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code> 39901cb0ef41Sopenharmony_cimust be a JavaScript number. Behavior is undefined when <code>value</code> is anything 39911cb0ef41Sopenharmony_ciother than a JavaScript number.</p> 39921cb0ef41Sopenharmony_ci 39931cb0ef41Sopenharmony_ci<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>; 39941cb0ef41Sopenharmony_ci 39951cb0ef41Sopenharmony_ci<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>); 39961cb0ef41Sopenharmony_ci 39971cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeDoubleBE</span>(<span class="hljs-number">123.456</span>, <span class="hljs-number">0</span>); 39981cb0ef41Sopenharmony_ci 39991cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 40001cb0ef41Sopenharmony_ci<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>); 40011cb0ef41Sopenharmony_ci 40021cb0ef41Sopenharmony_ci<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>); 40031cb0ef41Sopenharmony_ci 40041cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeDoubleBE</span>(<span class="hljs-number">123.456</span>, <span class="hljs-number">0</span>); 40051cb0ef41Sopenharmony_ci 40061cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 40071cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 40 5e dd 2f 1a 9f be 77></span></code><button class="copy-button">copy</button></pre> 40081cb0ef41Sopenharmony_ci<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> 40091cb0ef41Sopenharmony_ci<div class="api_metadata"> 40101cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 40111cb0ef41Sopenharmony_ci<table> 40121cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 40131cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 40141cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 40151cb0ef41Sopenharmony_ci<tr><td>v0.11.15</td> 40161cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.15</span></p></td></tr> 40171cb0ef41Sopenharmony_ci</tbody></table> 40181cb0ef41Sopenharmony_ci</details> 40191cb0ef41Sopenharmony_ci</div> 40201cb0ef41Sopenharmony_ci<ul> 40211cb0ef41Sopenharmony_ci<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> 40221cb0ef41Sopenharmony_ci<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 40231cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 40241cb0ef41Sopenharmony_ci<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> 40251cb0ef41Sopenharmony_ci</ul> 40261cb0ef41Sopenharmony_ci<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code> 40271cb0ef41Sopenharmony_cimust be a JavaScript number. Behavior is undefined when <code>value</code> is anything 40281cb0ef41Sopenharmony_ciother than a JavaScript number.</p> 40291cb0ef41Sopenharmony_ci 40301cb0ef41Sopenharmony_ci<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>; 40311cb0ef41Sopenharmony_ci 40321cb0ef41Sopenharmony_ci<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>); 40331cb0ef41Sopenharmony_ci 40341cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeDoubleLE</span>(<span class="hljs-number">123.456</span>, <span class="hljs-number">0</span>); 40351cb0ef41Sopenharmony_ci 40361cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 40371cb0ef41Sopenharmony_ci<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>); 40381cb0ef41Sopenharmony_ci 40391cb0ef41Sopenharmony_ci<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>); 40401cb0ef41Sopenharmony_ci 40411cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeDoubleLE</span>(<span class="hljs-number">123.456</span>, <span class="hljs-number">0</span>); 40421cb0ef41Sopenharmony_ci 40431cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 40441cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 77 be 9f 1a 2f dd 5e 40></span></code><button class="copy-button">copy</button></pre> 40451cb0ef41Sopenharmony_ci<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> 40461cb0ef41Sopenharmony_ci<div class="api_metadata"> 40471cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 40481cb0ef41Sopenharmony_ci<table> 40491cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 40501cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 40511cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 40521cb0ef41Sopenharmony_ci<tr><td>v0.11.15</td> 40531cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.15</span></p></td></tr> 40541cb0ef41Sopenharmony_ci</tbody></table> 40551cb0ef41Sopenharmony_ci</details> 40561cb0ef41Sopenharmony_ci</div> 40571cb0ef41Sopenharmony_ci<ul> 40581cb0ef41Sopenharmony_ci<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> 40591cb0ef41Sopenharmony_ci<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 40601cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 40611cb0ef41Sopenharmony_ci<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> 40621cb0ef41Sopenharmony_ci</ul> 40631cb0ef41Sopenharmony_ci<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. Behavior is 40641cb0ef41Sopenharmony_ciundefined when <code>value</code> is anything other than a JavaScript number.</p> 40651cb0ef41Sopenharmony_ci 40661cb0ef41Sopenharmony_ci<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>; 40671cb0ef41Sopenharmony_ci 40681cb0ef41Sopenharmony_ci<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>); 40691cb0ef41Sopenharmony_ci 40701cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeFloatBE</span>(<span class="hljs-number">0xcafebabe</span>, <span class="hljs-number">0</span>); 40711cb0ef41Sopenharmony_ci 40721cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 40731cb0ef41Sopenharmony_ci<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>); 40741cb0ef41Sopenharmony_ci 40751cb0ef41Sopenharmony_ci<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>); 40761cb0ef41Sopenharmony_ci 40771cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeFloatBE</span>(<span class="hljs-number">0xcafebabe</span>, <span class="hljs-number">0</span>); 40781cb0ef41Sopenharmony_ci 40791cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 40801cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 4f 4a fe bb></span></code><button class="copy-button">copy</button></pre> 40811cb0ef41Sopenharmony_ci<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> 40821cb0ef41Sopenharmony_ci<div class="api_metadata"> 40831cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 40841cb0ef41Sopenharmony_ci<table> 40851cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 40861cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 40871cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 40881cb0ef41Sopenharmony_ci<tr><td>v0.11.15</td> 40891cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.15</span></p></td></tr> 40901cb0ef41Sopenharmony_ci</tbody></table> 40911cb0ef41Sopenharmony_ci</details> 40921cb0ef41Sopenharmony_ci</div> 40931cb0ef41Sopenharmony_ci<ul> 40941cb0ef41Sopenharmony_ci<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> 40951cb0ef41Sopenharmony_ci<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 40961cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 40971cb0ef41Sopenharmony_ci<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> 40981cb0ef41Sopenharmony_ci</ul> 40991cb0ef41Sopenharmony_ci<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. Behavior is 41001cb0ef41Sopenharmony_ciundefined when <code>value</code> is anything other than a JavaScript number.</p> 41011cb0ef41Sopenharmony_ci 41021cb0ef41Sopenharmony_ci<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>; 41031cb0ef41Sopenharmony_ci 41041cb0ef41Sopenharmony_ci<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>); 41051cb0ef41Sopenharmony_ci 41061cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeFloatLE</span>(<span class="hljs-number">0xcafebabe</span>, <span class="hljs-number">0</span>); 41071cb0ef41Sopenharmony_ci 41081cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 41091cb0ef41Sopenharmony_ci<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>); 41101cb0ef41Sopenharmony_ci 41111cb0ef41Sopenharmony_ci<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>); 41121cb0ef41Sopenharmony_ci 41131cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeFloatLE</span>(<span class="hljs-number">0xcafebabe</span>, <span class="hljs-number">0</span>); 41141cb0ef41Sopenharmony_ci 41151cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 41161cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer bb fe 4a 4f></span></code><button class="copy-button">copy</button></pre> 41171cb0ef41Sopenharmony_ci<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> 41181cb0ef41Sopenharmony_ci<div class="api_metadata"> 41191cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 41201cb0ef41Sopenharmony_ci<table> 41211cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 41221cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 41231cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 41241cb0ef41Sopenharmony_ci<tr><td>v0.5.0</td> 41251cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.0</span></p></td></tr> 41261cb0ef41Sopenharmony_ci</tbody></table> 41271cb0ef41Sopenharmony_ci</details> 41281cb0ef41Sopenharmony_ci</div> 41291cb0ef41Sopenharmony_ci<ul> 41301cb0ef41Sopenharmony_ci<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> 41311cb0ef41Sopenharmony_ci<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 41321cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 1</code>. <strong>Default:</strong> <code>0</code>.</li> 41331cb0ef41Sopenharmony_ci<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> 41341cb0ef41Sopenharmony_ci</ul> 41351cb0ef41Sopenharmony_ci<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code>. <code>value</code> must be a valid 41361cb0ef41Sopenharmony_cisigned 8-bit integer. Behavior is undefined when <code>value</code> is anything other than 41371cb0ef41Sopenharmony_cia signed 8-bit integer.</p> 41381cb0ef41Sopenharmony_ci<p><code>value</code> is interpreted and written as a two's complement signed integer.</p> 41391cb0ef41Sopenharmony_ci 41401cb0ef41Sopenharmony_ci<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>; 41411cb0ef41Sopenharmony_ci 41421cb0ef41Sopenharmony_ci<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>); 41431cb0ef41Sopenharmony_ci 41441cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeInt8</span>(<span class="hljs-number">2</span>, <span class="hljs-number">0</span>); 41451cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeInt8</span>(-<span class="hljs-number">2</span>, <span class="hljs-number">1</span>); 41461cb0ef41Sopenharmony_ci 41471cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 41481cb0ef41Sopenharmony_ci<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>); 41491cb0ef41Sopenharmony_ci 41501cb0ef41Sopenharmony_ci<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>); 41511cb0ef41Sopenharmony_ci 41521cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeInt8</span>(<span class="hljs-number">2</span>, <span class="hljs-number">0</span>); 41531cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeInt8</span>(-<span class="hljs-number">2</span>, <span class="hljs-number">1</span>); 41541cb0ef41Sopenharmony_ci 41551cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 41561cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 02 fe></span></code><button class="copy-button">copy</button></pre> 41571cb0ef41Sopenharmony_ci<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> 41581cb0ef41Sopenharmony_ci<div class="api_metadata"> 41591cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 41601cb0ef41Sopenharmony_ci<table> 41611cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 41621cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 41631cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 41641cb0ef41Sopenharmony_ci<tr><td>v0.5.5</td> 41651cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.5</span></p></td></tr> 41661cb0ef41Sopenharmony_ci</tbody></table> 41671cb0ef41Sopenharmony_ci</details> 41681cb0ef41Sopenharmony_ci</div> 41691cb0ef41Sopenharmony_ci<ul> 41701cb0ef41Sopenharmony_ci<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> 41711cb0ef41Sopenharmony_ci<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 41721cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> 41731cb0ef41Sopenharmony_ci<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> 41741cb0ef41Sopenharmony_ci</ul> 41751cb0ef41Sopenharmony_ci<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code> 41761cb0ef41Sopenharmony_cimust be a valid signed 16-bit integer. Behavior is undefined when <code>value</code> is 41771cb0ef41Sopenharmony_cianything other than a signed 16-bit integer.</p> 41781cb0ef41Sopenharmony_ci<p>The <code>value</code> is interpreted and written as a two's complement signed integer.</p> 41791cb0ef41Sopenharmony_ci 41801cb0ef41Sopenharmony_ci<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>; 41811cb0ef41Sopenharmony_ci 41821cb0ef41Sopenharmony_ci<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>); 41831cb0ef41Sopenharmony_ci 41841cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeInt16BE</span>(<span class="hljs-number">0x0102</span>, <span class="hljs-number">0</span>); 41851cb0ef41Sopenharmony_ci 41861cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 41871cb0ef41Sopenharmony_ci<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>); 41881cb0ef41Sopenharmony_ci 41891cb0ef41Sopenharmony_ci<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>); 41901cb0ef41Sopenharmony_ci 41911cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeInt16BE</span>(<span class="hljs-number">0x0102</span>, <span class="hljs-number">0</span>); 41921cb0ef41Sopenharmony_ci 41931cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 41941cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 01 02></span></code><button class="copy-button">copy</button></pre> 41951cb0ef41Sopenharmony_ci<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> 41961cb0ef41Sopenharmony_ci<div class="api_metadata"> 41971cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 41981cb0ef41Sopenharmony_ci<table> 41991cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 42001cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 42011cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 42021cb0ef41Sopenharmony_ci<tr><td>v0.5.5</td> 42031cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.5</span></p></td></tr> 42041cb0ef41Sopenharmony_ci</tbody></table> 42051cb0ef41Sopenharmony_ci</details> 42061cb0ef41Sopenharmony_ci</div> 42071cb0ef41Sopenharmony_ci<ul> 42081cb0ef41Sopenharmony_ci<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> 42091cb0ef41Sopenharmony_ci<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 42101cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> 42111cb0ef41Sopenharmony_ci<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> 42121cb0ef41Sopenharmony_ci</ul> 42131cb0ef41Sopenharmony_ci<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code> 42141cb0ef41Sopenharmony_cimust be a valid signed 16-bit integer. Behavior is undefined when <code>value</code> is 42151cb0ef41Sopenharmony_cianything other than a signed 16-bit integer.</p> 42161cb0ef41Sopenharmony_ci<p>The <code>value</code> is interpreted and written as a two's complement signed integer.</p> 42171cb0ef41Sopenharmony_ci 42181cb0ef41Sopenharmony_ci<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>; 42191cb0ef41Sopenharmony_ci 42201cb0ef41Sopenharmony_ci<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>); 42211cb0ef41Sopenharmony_ci 42221cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeInt16LE</span>(<span class="hljs-number">0x0304</span>, <span class="hljs-number">0</span>); 42231cb0ef41Sopenharmony_ci 42241cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 42251cb0ef41Sopenharmony_ci<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>); 42261cb0ef41Sopenharmony_ci 42271cb0ef41Sopenharmony_ci<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>); 42281cb0ef41Sopenharmony_ci 42291cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeInt16LE</span>(<span class="hljs-number">0x0304</span>, <span class="hljs-number">0</span>); 42301cb0ef41Sopenharmony_ci 42311cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 42321cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 04 03></span></code><button class="copy-button">copy</button></pre> 42331cb0ef41Sopenharmony_ci<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> 42341cb0ef41Sopenharmony_ci<div class="api_metadata"> 42351cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 42361cb0ef41Sopenharmony_ci<table> 42371cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 42381cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 42391cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 42401cb0ef41Sopenharmony_ci<tr><td>v0.5.5</td> 42411cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.5</span></p></td></tr> 42421cb0ef41Sopenharmony_ci</tbody></table> 42431cb0ef41Sopenharmony_ci</details> 42441cb0ef41Sopenharmony_ci</div> 42451cb0ef41Sopenharmony_ci<ul> 42461cb0ef41Sopenharmony_ci<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> 42471cb0ef41Sopenharmony_ci<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 42481cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 42491cb0ef41Sopenharmony_ci<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> 42501cb0ef41Sopenharmony_ci</ul> 42511cb0ef41Sopenharmony_ci<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code> 42521cb0ef41Sopenharmony_cimust be a valid signed 32-bit integer. Behavior is undefined when <code>value</code> is 42531cb0ef41Sopenharmony_cianything other than a signed 32-bit integer.</p> 42541cb0ef41Sopenharmony_ci<p>The <code>value</code> is interpreted and written as a two's complement signed integer.</p> 42551cb0ef41Sopenharmony_ci 42561cb0ef41Sopenharmony_ci<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>; 42571cb0ef41Sopenharmony_ci 42581cb0ef41Sopenharmony_ci<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>); 42591cb0ef41Sopenharmony_ci 42601cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeInt32BE</span>(<span class="hljs-number">0x01020304</span>, <span class="hljs-number">0</span>); 42611cb0ef41Sopenharmony_ci 42621cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 42631cb0ef41Sopenharmony_ci<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>); 42641cb0ef41Sopenharmony_ci 42651cb0ef41Sopenharmony_ci<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>); 42661cb0ef41Sopenharmony_ci 42671cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeInt32BE</span>(<span class="hljs-number">0x01020304</span>, <span class="hljs-number">0</span>); 42681cb0ef41Sopenharmony_ci 42691cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 42701cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 01 02 03 04></span></code><button class="copy-button">copy</button></pre> 42711cb0ef41Sopenharmony_ci<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> 42721cb0ef41Sopenharmony_ci<div class="api_metadata"> 42731cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 42741cb0ef41Sopenharmony_ci<table> 42751cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 42761cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 42771cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 42781cb0ef41Sopenharmony_ci<tr><td>v0.5.5</td> 42791cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.5</span></p></td></tr> 42801cb0ef41Sopenharmony_ci</tbody></table> 42811cb0ef41Sopenharmony_ci</details> 42821cb0ef41Sopenharmony_ci</div> 42831cb0ef41Sopenharmony_ci<ul> 42841cb0ef41Sopenharmony_ci<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> 42851cb0ef41Sopenharmony_ci<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 42861cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 42871cb0ef41Sopenharmony_ci<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> 42881cb0ef41Sopenharmony_ci</ul> 42891cb0ef41Sopenharmony_ci<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code> 42901cb0ef41Sopenharmony_cimust be a valid signed 32-bit integer. Behavior is undefined when <code>value</code> is 42911cb0ef41Sopenharmony_cianything other than a signed 32-bit integer.</p> 42921cb0ef41Sopenharmony_ci<p>The <code>value</code> is interpreted and written as a two's complement signed integer.</p> 42931cb0ef41Sopenharmony_ci 42941cb0ef41Sopenharmony_ci<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>; 42951cb0ef41Sopenharmony_ci 42961cb0ef41Sopenharmony_ci<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>); 42971cb0ef41Sopenharmony_ci 42981cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeInt32LE</span>(<span class="hljs-number">0x05060708</span>, <span class="hljs-number">0</span>); 42991cb0ef41Sopenharmony_ci 43001cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 43011cb0ef41Sopenharmony_ci<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>); 43021cb0ef41Sopenharmony_ci 43031cb0ef41Sopenharmony_ci<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>); 43041cb0ef41Sopenharmony_ci 43051cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeInt32LE</span>(<span class="hljs-number">0x05060708</span>, <span class="hljs-number">0</span>); 43061cb0ef41Sopenharmony_ci 43071cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 43081cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 08 07 06 05></span></code><button class="copy-button">copy</button></pre> 43091cb0ef41Sopenharmony_ci<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> 43101cb0ef41Sopenharmony_ci<div class="api_metadata"> 43111cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 43121cb0ef41Sopenharmony_ci<table> 43131cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 43141cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 43151cb0ef41Sopenharmony_ci<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> 43161cb0ef41Sopenharmony_ci<tr><td>v0.11.15</td> 43171cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.15</span></p></td></tr> 43181cb0ef41Sopenharmony_ci</tbody></table> 43191cb0ef41Sopenharmony_ci</details> 43201cb0ef41Sopenharmony_ci</div> 43211cb0ef41Sopenharmony_ci<ul> 43221cb0ef41Sopenharmony_ci<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> 43231cb0ef41Sopenharmony_ci<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 43241cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> 43251cb0ef41Sopenharmony_ci<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 43261cb0ef41Sopenharmony_ci<code>0 < byteLength <= 6</code>.</li> 43271cb0ef41Sopenharmony_ci<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> 43281cb0ef41Sopenharmony_ci</ul> 43291cb0ef41Sopenharmony_ci<p>Writes <code>byteLength</code> bytes of <code>value</code> to <code>buf</code> at the specified <code>offset</code> 43301cb0ef41Sopenharmony_cias big-endian. Supports up to 48 bits of accuracy. Behavior is undefined when 43311cb0ef41Sopenharmony_ci<code>value</code> is anything other than a signed integer.</p> 43321cb0ef41Sopenharmony_ci 43331cb0ef41Sopenharmony_ci<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>; 43341cb0ef41Sopenharmony_ci 43351cb0ef41Sopenharmony_ci<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>); 43361cb0ef41Sopenharmony_ci 43371cb0ef41Sopenharmony_cibuf.<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>); 43381cb0ef41Sopenharmony_ci 43391cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 43401cb0ef41Sopenharmony_ci<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>); 43411cb0ef41Sopenharmony_ci 43421cb0ef41Sopenharmony_ci<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>); 43431cb0ef41Sopenharmony_ci 43441cb0ef41Sopenharmony_cibuf.<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>); 43451cb0ef41Sopenharmony_ci 43461cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 43471cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 12 34 56 78 90 ab></span></code><button class="copy-button">copy</button></pre> 43481cb0ef41Sopenharmony_ci<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> 43491cb0ef41Sopenharmony_ci<div class="api_metadata"> 43501cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 43511cb0ef41Sopenharmony_ci<table> 43521cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 43531cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 43541cb0ef41Sopenharmony_ci<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> 43551cb0ef41Sopenharmony_ci<tr><td>v0.11.15</td> 43561cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.15</span></p></td></tr> 43571cb0ef41Sopenharmony_ci</tbody></table> 43581cb0ef41Sopenharmony_ci</details> 43591cb0ef41Sopenharmony_ci</div> 43601cb0ef41Sopenharmony_ci<ul> 43611cb0ef41Sopenharmony_ci<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> 43621cb0ef41Sopenharmony_ci<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 43631cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> 43641cb0ef41Sopenharmony_ci<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 43651cb0ef41Sopenharmony_ci<code>0 < byteLength <= 6</code>.</li> 43661cb0ef41Sopenharmony_ci<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> 43671cb0ef41Sopenharmony_ci</ul> 43681cb0ef41Sopenharmony_ci<p>Writes <code>byteLength</code> bytes of <code>value</code> to <code>buf</code> at the specified <code>offset</code> 43691cb0ef41Sopenharmony_cias little-endian. Supports up to 48 bits of accuracy. Behavior is undefined 43701cb0ef41Sopenharmony_ciwhen <code>value</code> is anything other than a signed integer.</p> 43711cb0ef41Sopenharmony_ci 43721cb0ef41Sopenharmony_ci<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>; 43731cb0ef41Sopenharmony_ci 43741cb0ef41Sopenharmony_ci<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>); 43751cb0ef41Sopenharmony_ci 43761cb0ef41Sopenharmony_cibuf.<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>); 43771cb0ef41Sopenharmony_ci 43781cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 43791cb0ef41Sopenharmony_ci<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>); 43801cb0ef41Sopenharmony_ci 43811cb0ef41Sopenharmony_ci<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>); 43821cb0ef41Sopenharmony_ci 43831cb0ef41Sopenharmony_cibuf.<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>); 43841cb0ef41Sopenharmony_ci 43851cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 43861cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer ab 90 78 56 34 12></span></code><button class="copy-button">copy</button></pre> 43871cb0ef41Sopenharmony_ci<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> 43881cb0ef41Sopenharmony_ci<div class="api_metadata"> 43891cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 43901cb0ef41Sopenharmony_ci<table> 43911cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 43921cb0ef41Sopenharmony_ci<tr><td>v14.9.0, v12.19.0</td> 43931cb0ef41Sopenharmony_ci<td><p>This function is also available as <code>buf.writeUint8()</code>.</p></td></tr> 43941cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 43951cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 43961cb0ef41Sopenharmony_ci<tr><td>v0.5.0</td> 43971cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.0</span></p></td></tr> 43981cb0ef41Sopenharmony_ci</tbody></table> 43991cb0ef41Sopenharmony_ci</details> 44001cb0ef41Sopenharmony_ci</div> 44011cb0ef41Sopenharmony_ci<ul> 44021cb0ef41Sopenharmony_ci<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> 44031cb0ef41Sopenharmony_ci<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 44041cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 1</code>. <strong>Default:</strong> <code>0</code>.</li> 44051cb0ef41Sopenharmony_ci<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> 44061cb0ef41Sopenharmony_ci</ul> 44071cb0ef41Sopenharmony_ci<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code>. <code>value</code> must be a 44081cb0ef41Sopenharmony_civalid unsigned 8-bit integer. Behavior is undefined when <code>value</code> is anything 44091cb0ef41Sopenharmony_ciother than an unsigned 8-bit integer.</p> 44101cb0ef41Sopenharmony_ci<p>This function is also available under the <code>writeUint8</code> alias.</p> 44111cb0ef41Sopenharmony_ci 44121cb0ef41Sopenharmony_ci<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>; 44131cb0ef41Sopenharmony_ci 44141cb0ef41Sopenharmony_ci<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>); 44151cb0ef41Sopenharmony_ci 44161cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x3</span>, <span class="hljs-number">0</span>); 44171cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x4</span>, <span class="hljs-number">1</span>); 44181cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x23</span>, <span class="hljs-number">2</span>); 44191cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x42</span>, <span class="hljs-number">3</span>); 44201cb0ef41Sopenharmony_ci 44211cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 44221cb0ef41Sopenharmony_ci<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>); 44231cb0ef41Sopenharmony_ci 44241cb0ef41Sopenharmony_ci<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>); 44251cb0ef41Sopenharmony_ci 44261cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x3</span>, <span class="hljs-number">0</span>); 44271cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x4</span>, <span class="hljs-number">1</span>); 44281cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x23</span>, <span class="hljs-number">2</span>); 44291cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x42</span>, <span class="hljs-number">3</span>); 44301cb0ef41Sopenharmony_ci 44311cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 44321cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 03 04 23 42></span></code><button class="copy-button">copy</button></pre> 44331cb0ef41Sopenharmony_ci<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> 44341cb0ef41Sopenharmony_ci<div class="api_metadata"> 44351cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 44361cb0ef41Sopenharmony_ci<table> 44371cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 44381cb0ef41Sopenharmony_ci<tr><td>v14.9.0, v12.19.0</td> 44391cb0ef41Sopenharmony_ci<td><p>This function is also available as <code>buf.writeUint16BE()</code>.</p></td></tr> 44401cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 44411cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 44421cb0ef41Sopenharmony_ci<tr><td>v0.5.5</td> 44431cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.5</span></p></td></tr> 44441cb0ef41Sopenharmony_ci</tbody></table> 44451cb0ef41Sopenharmony_ci</details> 44461cb0ef41Sopenharmony_ci</div> 44471cb0ef41Sopenharmony_ci<ul> 44481cb0ef41Sopenharmony_ci<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> 44491cb0ef41Sopenharmony_ci<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 44501cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> 44511cb0ef41Sopenharmony_ci<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> 44521cb0ef41Sopenharmony_ci</ul> 44531cb0ef41Sopenharmony_ci<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code> 44541cb0ef41Sopenharmony_cimust be a valid unsigned 16-bit integer. Behavior is undefined when <code>value</code> 44551cb0ef41Sopenharmony_ciis anything other than an unsigned 16-bit integer.</p> 44561cb0ef41Sopenharmony_ci<p>This function is also available under the <code>writeUint16BE</code> alias.</p> 44571cb0ef41Sopenharmony_ci 44581cb0ef41Sopenharmony_ci<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>; 44591cb0ef41Sopenharmony_ci 44601cb0ef41Sopenharmony_ci<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>); 44611cb0ef41Sopenharmony_ci 44621cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeUInt16BE</span>(<span class="hljs-number">0xdead</span>, <span class="hljs-number">0</span>); 44631cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeUInt16BE</span>(<span class="hljs-number">0xbeef</span>, <span class="hljs-number">2</span>); 44641cb0ef41Sopenharmony_ci 44651cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 44661cb0ef41Sopenharmony_ci<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>); 44671cb0ef41Sopenharmony_ci 44681cb0ef41Sopenharmony_ci<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>); 44691cb0ef41Sopenharmony_ci 44701cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeUInt16BE</span>(<span class="hljs-number">0xdead</span>, <span class="hljs-number">0</span>); 44711cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeUInt16BE</span>(<span class="hljs-number">0xbeef</span>, <span class="hljs-number">2</span>); 44721cb0ef41Sopenharmony_ci 44731cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 44741cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer de ad be ef></span></code><button class="copy-button">copy</button></pre> 44751cb0ef41Sopenharmony_ci<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> 44761cb0ef41Sopenharmony_ci<div class="api_metadata"> 44771cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 44781cb0ef41Sopenharmony_ci<table> 44791cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 44801cb0ef41Sopenharmony_ci<tr><td>v14.9.0, v12.19.0</td> 44811cb0ef41Sopenharmony_ci<td><p>This function is also available as <code>buf.writeUint16LE()</code>.</p></td></tr> 44821cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 44831cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 44841cb0ef41Sopenharmony_ci<tr><td>v0.5.5</td> 44851cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.5</span></p></td></tr> 44861cb0ef41Sopenharmony_ci</tbody></table> 44871cb0ef41Sopenharmony_ci</details> 44881cb0ef41Sopenharmony_ci</div> 44891cb0ef41Sopenharmony_ci<ul> 44901cb0ef41Sopenharmony_ci<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> 44911cb0ef41Sopenharmony_ci<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 44921cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> 44931cb0ef41Sopenharmony_ci<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> 44941cb0ef41Sopenharmony_ci</ul> 44951cb0ef41Sopenharmony_ci<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code> 44961cb0ef41Sopenharmony_cimust be a valid unsigned 16-bit integer. Behavior is undefined when <code>value</code> is 44971cb0ef41Sopenharmony_cianything other than an unsigned 16-bit integer.</p> 44981cb0ef41Sopenharmony_ci<p>This function is also available under the <code>writeUint16LE</code> alias.</p> 44991cb0ef41Sopenharmony_ci 45001cb0ef41Sopenharmony_ci<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>; 45011cb0ef41Sopenharmony_ci 45021cb0ef41Sopenharmony_ci<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>); 45031cb0ef41Sopenharmony_ci 45041cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeUInt16LE</span>(<span class="hljs-number">0xdead</span>, <span class="hljs-number">0</span>); 45051cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeUInt16LE</span>(<span class="hljs-number">0xbeef</span>, <span class="hljs-number">2</span>); 45061cb0ef41Sopenharmony_ci 45071cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 45081cb0ef41Sopenharmony_ci<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>); 45091cb0ef41Sopenharmony_ci 45101cb0ef41Sopenharmony_ci<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>); 45111cb0ef41Sopenharmony_ci 45121cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeUInt16LE</span>(<span class="hljs-number">0xdead</span>, <span class="hljs-number">0</span>); 45131cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeUInt16LE</span>(<span class="hljs-number">0xbeef</span>, <span class="hljs-number">2</span>); 45141cb0ef41Sopenharmony_ci 45151cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 45161cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer ad de ef be></span></code><button class="copy-button">copy</button></pre> 45171cb0ef41Sopenharmony_ci<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> 45181cb0ef41Sopenharmony_ci<div class="api_metadata"> 45191cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 45201cb0ef41Sopenharmony_ci<table> 45211cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 45221cb0ef41Sopenharmony_ci<tr><td>v14.9.0, v12.19.0</td> 45231cb0ef41Sopenharmony_ci<td><p>This function is also available as <code>buf.writeUint32BE()</code>.</p></td></tr> 45241cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 45251cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 45261cb0ef41Sopenharmony_ci<tr><td>v0.5.5</td> 45271cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.5</span></p></td></tr> 45281cb0ef41Sopenharmony_ci</tbody></table> 45291cb0ef41Sopenharmony_ci</details> 45301cb0ef41Sopenharmony_ci</div> 45311cb0ef41Sopenharmony_ci<ul> 45321cb0ef41Sopenharmony_ci<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> 45331cb0ef41Sopenharmony_ci<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 45341cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 45351cb0ef41Sopenharmony_ci<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> 45361cb0ef41Sopenharmony_ci</ul> 45371cb0ef41Sopenharmony_ci<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code> 45381cb0ef41Sopenharmony_cimust be a valid unsigned 32-bit integer. Behavior is undefined when <code>value</code> 45391cb0ef41Sopenharmony_ciis anything other than an unsigned 32-bit integer.</p> 45401cb0ef41Sopenharmony_ci<p>This function is also available under the <code>writeUint32BE</code> alias.</p> 45411cb0ef41Sopenharmony_ci 45421cb0ef41Sopenharmony_ci<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>; 45431cb0ef41Sopenharmony_ci 45441cb0ef41Sopenharmony_ci<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>); 45451cb0ef41Sopenharmony_ci 45461cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeUInt32BE</span>(<span class="hljs-number">0xfeedface</span>, <span class="hljs-number">0</span>); 45471cb0ef41Sopenharmony_ci 45481cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 45491cb0ef41Sopenharmony_ci<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>); 45501cb0ef41Sopenharmony_ci 45511cb0ef41Sopenharmony_ci<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>); 45521cb0ef41Sopenharmony_ci 45531cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeUInt32BE</span>(<span class="hljs-number">0xfeedface</span>, <span class="hljs-number">0</span>); 45541cb0ef41Sopenharmony_ci 45551cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 45561cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer fe ed fa ce></span></code><button class="copy-button">copy</button></pre> 45571cb0ef41Sopenharmony_ci<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> 45581cb0ef41Sopenharmony_ci<div class="api_metadata"> 45591cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 45601cb0ef41Sopenharmony_ci<table> 45611cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 45621cb0ef41Sopenharmony_ci<tr><td>v14.9.0, v12.19.0</td> 45631cb0ef41Sopenharmony_ci<td><p>This function is also available as <code>buf.writeUint32LE()</code>.</p></td></tr> 45641cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 45651cb0ef41Sopenharmony_ci<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 45661cb0ef41Sopenharmony_ci<tr><td>v0.5.5</td> 45671cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.5</span></p></td></tr> 45681cb0ef41Sopenharmony_ci</tbody></table> 45691cb0ef41Sopenharmony_ci</details> 45701cb0ef41Sopenharmony_ci</div> 45711cb0ef41Sopenharmony_ci<ul> 45721cb0ef41Sopenharmony_ci<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> 45731cb0ef41Sopenharmony_ci<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 45741cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 45751cb0ef41Sopenharmony_ci<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> 45761cb0ef41Sopenharmony_ci</ul> 45771cb0ef41Sopenharmony_ci<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code> 45781cb0ef41Sopenharmony_cimust be a valid unsigned 32-bit integer. Behavior is undefined when <code>value</code> is 45791cb0ef41Sopenharmony_cianything other than an unsigned 32-bit integer.</p> 45801cb0ef41Sopenharmony_ci<p>This function is also available under the <code>writeUint32LE</code> alias.</p> 45811cb0ef41Sopenharmony_ci 45821cb0ef41Sopenharmony_ci<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>; 45831cb0ef41Sopenharmony_ci 45841cb0ef41Sopenharmony_ci<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>); 45851cb0ef41Sopenharmony_ci 45861cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeUInt32LE</span>(<span class="hljs-number">0xfeedface</span>, <span class="hljs-number">0</span>); 45871cb0ef41Sopenharmony_ci 45881cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 45891cb0ef41Sopenharmony_ci<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>); 45901cb0ef41Sopenharmony_ci 45911cb0ef41Sopenharmony_ci<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>); 45921cb0ef41Sopenharmony_ci 45931cb0ef41Sopenharmony_cibuf.<span class="hljs-title function_">writeUInt32LE</span>(<span class="hljs-number">0xfeedface</span>, <span class="hljs-number">0</span>); 45941cb0ef41Sopenharmony_ci 45951cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 45961cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer ce fa ed fe></span></code><button class="copy-button">copy</button></pre> 45971cb0ef41Sopenharmony_ci<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> 45981cb0ef41Sopenharmony_ci<div class="api_metadata"> 45991cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 46001cb0ef41Sopenharmony_ci<table> 46011cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 46021cb0ef41Sopenharmony_ci<tr><td>v14.9.0, v12.19.0</td> 46031cb0ef41Sopenharmony_ci<td><p>This function is also available as <code>buf.writeUintBE()</code>.</p></td></tr> 46041cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 46051cb0ef41Sopenharmony_ci<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> 46061cb0ef41Sopenharmony_ci<tr><td>v0.5.5</td> 46071cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.5</span></p></td></tr> 46081cb0ef41Sopenharmony_ci</tbody></table> 46091cb0ef41Sopenharmony_ci</details> 46101cb0ef41Sopenharmony_ci</div> 46111cb0ef41Sopenharmony_ci<ul> 46121cb0ef41Sopenharmony_ci<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> 46131cb0ef41Sopenharmony_ci<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 46141cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> 46151cb0ef41Sopenharmony_ci<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 46161cb0ef41Sopenharmony_ci<code>0 < byteLength <= 6</code>.</li> 46171cb0ef41Sopenharmony_ci<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> 46181cb0ef41Sopenharmony_ci</ul> 46191cb0ef41Sopenharmony_ci<p>Writes <code>byteLength</code> bytes of <code>value</code> to <code>buf</code> at the specified <code>offset</code> 46201cb0ef41Sopenharmony_cias big-endian. Supports up to 48 bits of accuracy. Behavior is undefined 46211cb0ef41Sopenharmony_ciwhen <code>value</code> is anything other than an unsigned integer.</p> 46221cb0ef41Sopenharmony_ci<p>This function is also available under the <code>writeUintBE</code> alias.</p> 46231cb0ef41Sopenharmony_ci 46241cb0ef41Sopenharmony_ci<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>; 46251cb0ef41Sopenharmony_ci 46261cb0ef41Sopenharmony_ci<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>); 46271cb0ef41Sopenharmony_ci 46281cb0ef41Sopenharmony_cibuf.<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>); 46291cb0ef41Sopenharmony_ci 46301cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 46311cb0ef41Sopenharmony_ci<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>); 46321cb0ef41Sopenharmony_ci 46331cb0ef41Sopenharmony_ci<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>); 46341cb0ef41Sopenharmony_ci 46351cb0ef41Sopenharmony_cibuf.<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>); 46361cb0ef41Sopenharmony_ci 46371cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 46381cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer 12 34 56 78 90 ab></span></code><button class="copy-button">copy</button></pre> 46391cb0ef41Sopenharmony_ci<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> 46401cb0ef41Sopenharmony_ci<div class="api_metadata"> 46411cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 46421cb0ef41Sopenharmony_ci<table> 46431cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 46441cb0ef41Sopenharmony_ci<tr><td>v14.9.0, v12.19.0</td> 46451cb0ef41Sopenharmony_ci<td><p>This function is also available as <code>buf.writeUintLE()</code>.</p></td></tr> 46461cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 46471cb0ef41Sopenharmony_ci<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> 46481cb0ef41Sopenharmony_ci<tr><td>v0.5.5</td> 46491cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.5</span></p></td></tr> 46501cb0ef41Sopenharmony_ci</tbody></table> 46511cb0ef41Sopenharmony_ci</details> 46521cb0ef41Sopenharmony_ci</div> 46531cb0ef41Sopenharmony_ci<ul> 46541cb0ef41Sopenharmony_ci<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> 46551cb0ef41Sopenharmony_ci<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 46561cb0ef41Sopenharmony_cisatisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> 46571cb0ef41Sopenharmony_ci<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 46581cb0ef41Sopenharmony_ci<code>0 < byteLength <= 6</code>.</li> 46591cb0ef41Sopenharmony_ci<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> 46601cb0ef41Sopenharmony_ci</ul> 46611cb0ef41Sopenharmony_ci<p>Writes <code>byteLength</code> bytes of <code>value</code> to <code>buf</code> at the specified <code>offset</code> 46621cb0ef41Sopenharmony_cias little-endian. Supports up to 48 bits of accuracy. Behavior is undefined 46631cb0ef41Sopenharmony_ciwhen <code>value</code> is anything other than an unsigned integer.</p> 46641cb0ef41Sopenharmony_ci<p>This function is also available under the <code>writeUintLE</code> alias.</p> 46651cb0ef41Sopenharmony_ci 46661cb0ef41Sopenharmony_ci<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>; 46671cb0ef41Sopenharmony_ci 46681cb0ef41Sopenharmony_ci<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>); 46691cb0ef41Sopenharmony_ci 46701cb0ef41Sopenharmony_cibuf.<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>); 46711cb0ef41Sopenharmony_ci 46721cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 46731cb0ef41Sopenharmony_ci<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>); 46741cb0ef41Sopenharmony_ci 46751cb0ef41Sopenharmony_ci<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>); 46761cb0ef41Sopenharmony_ci 46771cb0ef41Sopenharmony_cibuf.<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>); 46781cb0ef41Sopenharmony_ci 46791cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 46801cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: <Buffer ab 90 78 56 34 12></span></code><button class="copy-button">copy</button></pre> 46811cb0ef41Sopenharmony_ci<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> 46821cb0ef41Sopenharmony_ci<div class="api_metadata"> 46831cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 46841cb0ef41Sopenharmony_ci<table> 46851cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 46861cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 46871cb0ef41Sopenharmony_ci<td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr> 46881cb0ef41Sopenharmony_ci<tr><td>v7.2.1</td> 46891cb0ef41Sopenharmony_ci<td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr> 46901cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td> 46911cb0ef41Sopenharmony_ci<td><p>Calling this constructor emits a deprecation warning now.</p></td></tr> 46921cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td> 46931cb0ef41Sopenharmony_ci<td><p><span>Deprecated since: v6.0.0</span></p></td></tr> 46941cb0ef41Sopenharmony_ci</tbody></table> 46951cb0ef41Sopenharmony_ci</details> 46961cb0ef41Sopenharmony_ci</div> 46971cb0ef41Sopenharmony_ci<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> 46981cb0ef41Sopenharmony_ci<ul> 46991cb0ef41Sopenharmony_ci<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> 47001cb0ef41Sopenharmony_ci</ul> 47011cb0ef41Sopenharmony_ci<p>See <a href="#static-method-bufferfromarray"><code>Buffer.from(array)</code></a>.</p> 47021cb0ef41Sopenharmony_ci<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> 47031cb0ef41Sopenharmony_ci<div class="api_metadata"> 47041cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 47051cb0ef41Sopenharmony_ci<table> 47061cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 47071cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 47081cb0ef41Sopenharmony_ci<td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr> 47091cb0ef41Sopenharmony_ci<tr><td>v7.2.1</td> 47101cb0ef41Sopenharmony_ci<td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr> 47111cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td> 47121cb0ef41Sopenharmony_ci<td><p>Calling this constructor emits a deprecation warning now.</p></td></tr> 47131cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td> 47141cb0ef41Sopenharmony_ci<td><p>The <code>byteOffset</code> and <code>length</code> parameters are supported now.</p></td></tr> 47151cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td> 47161cb0ef41Sopenharmony_ci<td><p><span>Deprecated since: v6.0.0</span></p></td></tr> 47171cb0ef41Sopenharmony_ci<tr><td>v3.0.0</td> 47181cb0ef41Sopenharmony_ci<td><p><span>Added in: v3.0.0</span></p></td></tr> 47191cb0ef41Sopenharmony_ci</tbody></table> 47201cb0ef41Sopenharmony_ci</details> 47211cb0ef41Sopenharmony_ci</div> 47221cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use 47231cb0ef41Sopenharmony_ci<a href="#static-method-bufferfromarraybuffer-byteoffset-length"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a> 47241cb0ef41Sopenharmony_ciinstead.</div><p></p> 47251cb0ef41Sopenharmony_ci<ul> 47261cb0ef41Sopenharmony_ci<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>, 47271cb0ef41Sopenharmony_ci<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> 47281cb0ef41Sopenharmony_ci<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> 47291cb0ef41Sopenharmony_ci<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. 47301cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>arrayBuffer.byteLength - byteOffset</code>.</li> 47311cb0ef41Sopenharmony_ci</ul> 47321cb0ef41Sopenharmony_ci<p>See 47331cb0ef41Sopenharmony_ci<a href="#static-method-bufferfromarraybuffer-byteoffset-length"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a>.</p> 47341cb0ef41Sopenharmony_ci<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> 47351cb0ef41Sopenharmony_ci<div class="api_metadata"> 47361cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 47371cb0ef41Sopenharmony_ci<table> 47381cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 47391cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 47401cb0ef41Sopenharmony_ci<td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr> 47411cb0ef41Sopenharmony_ci<tr><td>v7.2.1</td> 47421cb0ef41Sopenharmony_ci<td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr> 47431cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td> 47441cb0ef41Sopenharmony_ci<td><p>Calling this constructor emits a deprecation warning now.</p></td></tr> 47451cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td> 47461cb0ef41Sopenharmony_ci<td><p><span>Deprecated since: v6.0.0</span></p></td></tr> 47471cb0ef41Sopenharmony_ci</tbody></table> 47481cb0ef41Sopenharmony_ci</details> 47491cb0ef41Sopenharmony_ci</div> 47501cb0ef41Sopenharmony_ci<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> 47511cb0ef41Sopenharmony_ci<ul> 47521cb0ef41Sopenharmony_ci<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 47531cb0ef41Sopenharmony_ciwhich to copy data.</li> 47541cb0ef41Sopenharmony_ci</ul> 47551cb0ef41Sopenharmony_ci<p>See <a href="#static-method-bufferfrombuffer"><code>Buffer.from(buffer)</code></a>.</p> 47561cb0ef41Sopenharmony_ci<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> 47571cb0ef41Sopenharmony_ci<div class="api_metadata"> 47581cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 47591cb0ef41Sopenharmony_ci<table> 47601cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 47611cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 47621cb0ef41Sopenharmony_ci<td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr> 47631cb0ef41Sopenharmony_ci<tr><td>v8.0.0</td> 47641cb0ef41Sopenharmony_ci<td><p>The <code>new Buffer(size)</code> will return zero-filled memory by default.</p></td></tr> 47651cb0ef41Sopenharmony_ci<tr><td>v7.2.1</td> 47661cb0ef41Sopenharmony_ci<td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr> 47671cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td> 47681cb0ef41Sopenharmony_ci<td><p>Calling this constructor emits a deprecation warning now.</p></td></tr> 47691cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td> 47701cb0ef41Sopenharmony_ci<td><p><span>Deprecated since: v6.0.0</span></p></td></tr> 47711cb0ef41Sopenharmony_ci</tbody></table> 47721cb0ef41Sopenharmony_ci</details> 47731cb0ef41Sopenharmony_ci</div> 47741cb0ef41Sopenharmony_ci<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 47751cb0ef41Sopenharmony_ci<a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a>).</div><p></p> 47761cb0ef41Sopenharmony_ci<ul> 47771cb0ef41Sopenharmony_ci<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> 47781cb0ef41Sopenharmony_ci</ul> 47791cb0ef41Sopenharmony_ci<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 47801cb0ef41Sopenharmony_ciconstructor is equivalent to <a href="#static-method-bufferallocsize-fill-encoding"><code>Buffer.alloc()</code></a>.</p> 47811cb0ef41Sopenharmony_ci<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> 47821cb0ef41Sopenharmony_ci<div class="api_metadata"> 47831cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 47841cb0ef41Sopenharmony_ci<table> 47851cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 47861cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 47871cb0ef41Sopenharmony_ci<td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr> 47881cb0ef41Sopenharmony_ci<tr><td>v7.2.1</td> 47891cb0ef41Sopenharmony_ci<td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr> 47901cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td> 47911cb0ef41Sopenharmony_ci<td><p>Calling this constructor emits a deprecation warning now.</p></td></tr> 47921cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td> 47931cb0ef41Sopenharmony_ci<td><p><span>Deprecated since: v6.0.0</span></p></td></tr> 47941cb0ef41Sopenharmony_ci</tbody></table> 47951cb0ef41Sopenharmony_ci</details> 47961cb0ef41Sopenharmony_ci</div> 47971cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: 47981cb0ef41Sopenharmony_ciUse <a href="#static-method-bufferfromstring-encoding"><code>Buffer.from(string[, encoding])</code></a> instead.</div><p></p> 47991cb0ef41Sopenharmony_ci<ul> 48001cb0ef41Sopenharmony_ci<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> 48011cb0ef41Sopenharmony_ci<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> 48021cb0ef41Sopenharmony_ci</ul> 48031cb0ef41Sopenharmony_ci<p>See <a href="#static-method-bufferfromstring-encoding"><code>Buffer.from(string[, encoding])</code></a>.</p> 48041cb0ef41Sopenharmony_ci</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> 48051cb0ef41Sopenharmony_ci<div class="api_metadata"> 48061cb0ef41Sopenharmony_ci<span>Added in: v18.13.0</span> 48071cb0ef41Sopenharmony_ci</div> 48081cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 48091cb0ef41Sopenharmony_ci<ul> 48101cb0ef41Sopenharmony_ci<li>Extends: <a href="buffer.html#class-blob" class="type"><Blob></a></li> 48111cb0ef41Sopenharmony_ci</ul> 48121cb0ef41Sopenharmony_ci<p>A <a href="https://developer.mozilla.org/en-US/docs/Web/API/File"><code>File</code></a> provides information about files.</p> 48131cb0ef41Sopenharmony_ci<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> 48141cb0ef41Sopenharmony_ci<div class="api_metadata"> 48151cb0ef41Sopenharmony_ci<span>Added in: v18.13.0</span> 48161cb0ef41Sopenharmony_ci</div> 48171cb0ef41Sopenharmony_ci<ul> 48181cb0ef41Sopenharmony_ci<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> 48191cb0ef41Sopenharmony_ciAn 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> 48201cb0ef41Sopenharmony_ciobjects, or any mix of such objects, that will be stored within the <code>File</code>.</li> 48211cb0ef41Sopenharmony_ci<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> 48221cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 48231cb0ef41Sopenharmony_ci<ul> 48241cb0ef41Sopenharmony_ci<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 48251cb0ef41Sopenharmony_cito <code>'native'</code>, line endings in string source parts will be converted to 48261cb0ef41Sopenharmony_cithe platform native line-ending as specified by <code>require('node:os').EOL</code>.</li> 48271cb0ef41Sopenharmony_ci<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> 48281cb0ef41Sopenharmony_ci<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. 48291cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>Date.now()</code>.</li> 48301cb0ef41Sopenharmony_ci</ul> 48311cb0ef41Sopenharmony_ci</li> 48321cb0ef41Sopenharmony_ci</ul> 48331cb0ef41Sopenharmony_ci<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> 48341cb0ef41Sopenharmony_ci<div class="api_metadata"> 48351cb0ef41Sopenharmony_ci<span>Added in: v18.13.0</span> 48361cb0ef41Sopenharmony_ci</div> 48371cb0ef41Sopenharmony_ci<ul> 48381cb0ef41Sopenharmony_ci<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 48391cb0ef41Sopenharmony_ci</ul> 48401cb0ef41Sopenharmony_ci<p>The name of the <code>File</code>.</p> 48411cb0ef41Sopenharmony_ci<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> 48421cb0ef41Sopenharmony_ci<div class="api_metadata"> 48431cb0ef41Sopenharmony_ci<span>Added in: v18.13.0</span> 48441cb0ef41Sopenharmony_ci</div> 48451cb0ef41Sopenharmony_ci<ul> 48461cb0ef41Sopenharmony_ci<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 48471cb0ef41Sopenharmony_ci</ul> 48481cb0ef41Sopenharmony_ci<p>The last modified date of the <code>File</code>.</p> 48491cb0ef41Sopenharmony_ci</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> 48501cb0ef41Sopenharmony_ci<p>While, the <code>Buffer</code> object is available as a global, there are additional 48511cb0ef41Sopenharmony_ci<code>Buffer</code>-related APIs that are available only via the <code>node:buffer</code> module 48521cb0ef41Sopenharmony_ciaccessed using <code>require('node:buffer')</code>.</p> 48531cb0ef41Sopenharmony_ci<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> 48541cb0ef41Sopenharmony_ci<div class="api_metadata"> 48551cb0ef41Sopenharmony_ci<span>Added in: v15.13.0, v14.17.0</span> 48561cb0ef41Sopenharmony_ci</div> 48571cb0ef41Sopenharmony_ci<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> 48581cb0ef41Sopenharmony_ci<ul> 48591cb0ef41Sopenharmony_ci<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> 48601cb0ef41Sopenharmony_ci</ul> 48611cb0ef41Sopenharmony_ci<p>Decodes a string of Base64-encoded data into bytes, and encodes those bytes 48621cb0ef41Sopenharmony_ciinto a string using Latin-1 (ISO-8859-1).</p> 48631cb0ef41Sopenharmony_ci<p>The <code>data</code> may be any JavaScript-value that can be coerced into a string.</p> 48641cb0ef41Sopenharmony_ci<p><strong>This function is only provided for compatibility with legacy web platform APIs 48651cb0ef41Sopenharmony_ciand should never be used in new code, because they use strings to represent 48661cb0ef41Sopenharmony_cibinary data and predate the introduction of typed arrays in JavaScript. 48671cb0ef41Sopenharmony_ciFor code running using Node.js APIs, converting between base64-encoded strings 48681cb0ef41Sopenharmony_ciand binary data should be performed using <code>Buffer.from(str, 'base64')</code> and 48691cb0ef41Sopenharmony_ci<code>buf.toString('base64')</code>.</strong></p> 48701cb0ef41Sopenharmony_ci<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> 48711cb0ef41Sopenharmony_ci<div class="api_metadata"> 48721cb0ef41Sopenharmony_ci<span>Added in: v15.13.0, v14.17.0</span> 48731cb0ef41Sopenharmony_ci</div> 48741cb0ef41Sopenharmony_ci<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> 48751cb0ef41Sopenharmony_ci<ul> 48761cb0ef41Sopenharmony_ci<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> 48771cb0ef41Sopenharmony_ci</ul> 48781cb0ef41Sopenharmony_ci<p>Decodes a string into bytes using Latin-1 (ISO-8859), and encodes those bytes 48791cb0ef41Sopenharmony_ciinto a string using Base64.</p> 48801cb0ef41Sopenharmony_ci<p>The <code>data</code> may be any JavaScript-value that can be coerced into a string.</p> 48811cb0ef41Sopenharmony_ci<p><strong>This function is only provided for compatibility with legacy web platform APIs 48821cb0ef41Sopenharmony_ciand should never be used in new code, because they use strings to represent 48831cb0ef41Sopenharmony_cibinary data and predate the introduction of typed arrays in JavaScript. 48841cb0ef41Sopenharmony_ciFor code running using Node.js APIs, converting between base64-encoded strings 48851cb0ef41Sopenharmony_ciand binary data should be performed using <code>Buffer.from(str, 'base64')</code> and 48861cb0ef41Sopenharmony_ci<code>buf.toString('base64')</code>.</strong></p> 48871cb0ef41Sopenharmony_ci<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> 48881cb0ef41Sopenharmony_ci<div class="api_metadata"> 48891cb0ef41Sopenharmony_ci<span>Added in: v18.15.0</span> 48901cb0ef41Sopenharmony_ci</div> 48911cb0ef41Sopenharmony_ci<ul> 48921cb0ef41Sopenharmony_ci<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> 48931cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 48941cb0ef41Sopenharmony_ci</ul> 48951cb0ef41Sopenharmony_ci<p>This function returns <code>true</code> if <code>input</code> contains only valid ASCII-encoded data, 48961cb0ef41Sopenharmony_ciincluding the case in which <code>input</code> is empty.</p> 48971cb0ef41Sopenharmony_ci<p>Throws if the <code>input</code> is a detached array buffer.</p> 48981cb0ef41Sopenharmony_ci<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> 48991cb0ef41Sopenharmony_ci<div class="api_metadata"> 49001cb0ef41Sopenharmony_ci<span>Added in: v18.14.0</span> 49011cb0ef41Sopenharmony_ci</div> 49021cb0ef41Sopenharmony_ci<ul> 49031cb0ef41Sopenharmony_ci<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> 49041cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 49051cb0ef41Sopenharmony_ci</ul> 49061cb0ef41Sopenharmony_ci<p>This function returns <code>true</code> if <code>input</code> contains only valid UTF-8-encoded data, 49071cb0ef41Sopenharmony_ciincluding the case in which <code>input</code> is empty.</p> 49081cb0ef41Sopenharmony_ci<p>Throws if the <code>input</code> is a detached array buffer.</p> 49091cb0ef41Sopenharmony_ci<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> 49101cb0ef41Sopenharmony_ci<div class="api_metadata"> 49111cb0ef41Sopenharmony_ci<span>Added in: v0.5.4</span> 49121cb0ef41Sopenharmony_ci</div> 49131cb0ef41Sopenharmony_ci<ul> 49141cb0ef41Sopenharmony_ci<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> 49151cb0ef41Sopenharmony_ci</ul> 49161cb0ef41Sopenharmony_ci<p>Returns the maximum number of bytes that will be returned when 49171cb0ef41Sopenharmony_ci<code>buf.inspect()</code> is called. This can be overridden by user modules. See 49181cb0ef41Sopenharmony_ci<a href="util.html#utilinspectobject-options"><code>util.inspect()</code></a> for more details on <code>buf.inspect()</code> behavior.</p> 49191cb0ef41Sopenharmony_ci<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> 49201cb0ef41Sopenharmony_ci<div class="api_metadata"> 49211cb0ef41Sopenharmony_ci<span>Added in: v3.0.0</span> 49221cb0ef41Sopenharmony_ci</div> 49231cb0ef41Sopenharmony_ci<ul> 49241cb0ef41Sopenharmony_ci<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> 49251cb0ef41Sopenharmony_ci</ul> 49261cb0ef41Sopenharmony_ci<p>An alias for <a href="#bufferconstantsmax_length"><code>buffer.constants.MAX_LENGTH</code></a>.</p> 49271cb0ef41Sopenharmony_ci<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> 49281cb0ef41Sopenharmony_ci<div class="api_metadata"> 49291cb0ef41Sopenharmony_ci<span>Added in: v3.0.0</span> 49301cb0ef41Sopenharmony_ci</div> 49311cb0ef41Sopenharmony_ci<ul> 49321cb0ef41Sopenharmony_ci<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> 49331cb0ef41Sopenharmony_ci</ul> 49341cb0ef41Sopenharmony_ci<p>An alias for <a href="#bufferconstantsmax_string_length"><code>buffer.constants.MAX_STRING_LENGTH</code></a>.</p> 49351cb0ef41Sopenharmony_ci<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> 49361cb0ef41Sopenharmony_ci<div class="api_metadata"> 49371cb0ef41Sopenharmony_ci<span>Added in: v16.7.0</span> 49381cb0ef41Sopenharmony_ci</div> 49391cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 49401cb0ef41Sopenharmony_ci<ul> 49411cb0ef41Sopenharmony_ci<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 49421cb0ef41Sopenharmony_ci<code>URL.createObjectURL()</code>.</li> 49431cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-blob" class="type"><Blob></a></li> 49441cb0ef41Sopenharmony_ci</ul> 49451cb0ef41Sopenharmony_ci<p>Resolves a <code>'blob:nodedata:...'</code> an associated <a href="buffer.html#class-blob" class="type"><Blob></a> object registered using 49461cb0ef41Sopenharmony_cia prior call to <code>URL.createObjectURL()</code>.</p> 49471cb0ef41Sopenharmony_ci<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> 49481cb0ef41Sopenharmony_ci<div class="api_metadata"> 49491cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 49501cb0ef41Sopenharmony_ci<table> 49511cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 49521cb0ef41Sopenharmony_ci<tr><td>v8.0.0</td> 49531cb0ef41Sopenharmony_ci<td><p>The <code>source</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 49541cb0ef41Sopenharmony_ci<tr><td>v7.1.0</td> 49551cb0ef41Sopenharmony_ci<td><p><span>Added in: v7.1.0</span></p></td></tr> 49561cb0ef41Sopenharmony_ci</tbody></table> 49571cb0ef41Sopenharmony_ci</details> 49581cb0ef41Sopenharmony_ci</div> 49591cb0ef41Sopenharmony_ci<ul> 49601cb0ef41Sopenharmony_ci<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> 49611cb0ef41Sopenharmony_ci<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> 49621cb0ef41Sopenharmony_ci<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> 49631cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 49641cb0ef41Sopenharmony_ci</ul> 49651cb0ef41Sopenharmony_ci<p>Re-encodes the given <code>Buffer</code> or <code>Uint8Array</code> instance from one character 49661cb0ef41Sopenharmony_ciencoding to another. Returns a new <code>Buffer</code> instance.</p> 49671cb0ef41Sopenharmony_ci<p>Throws if the <code>fromEnc</code> or <code>toEnc</code> specify invalid character encodings or if 49681cb0ef41Sopenharmony_ciconversion from <code>fromEnc</code> to <code>toEnc</code> is not permitted.</p> 49691cb0ef41Sopenharmony_ci<p>Encodings supported by <code>buffer.transcode()</code> are: <code>'ascii'</code>, <code>'utf8'</code>, 49701cb0ef41Sopenharmony_ci<code>'utf16le'</code>, <code>'ucs2'</code>, <code>'latin1'</code>, and <code>'binary'</code>.</p> 49711cb0ef41Sopenharmony_ci<p>The transcoding process will use substitution characters if a given byte 49721cb0ef41Sopenharmony_cisequence cannot be adequately represented in the target encoding. For instance:</p> 49731cb0ef41Sopenharmony_ci 49741cb0ef41Sopenharmony_ci<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>; 49751cb0ef41Sopenharmony_ci 49761cb0ef41Sopenharmony_ci<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>); 49771cb0ef41Sopenharmony_ci<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>)); 49781cb0ef41Sopenharmony_ci<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>); 49791cb0ef41Sopenharmony_ci 49801cb0ef41Sopenharmony_ci<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>); 49811cb0ef41Sopenharmony_ci<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>)); 49821cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: '?'</span></code><button class="copy-button">copy</button></pre> 49831cb0ef41Sopenharmony_ci<p>Because the Euro (<code>€</code>) sign is not representable in US-ASCII, it is replaced 49841cb0ef41Sopenharmony_ciwith <code>?</code> in the transcoded <code>Buffer</code>.</p> 49851cb0ef41Sopenharmony_ci<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> 49861cb0ef41Sopenharmony_ci<div class="api_metadata"> 49871cb0ef41Sopenharmony_ci<span>Deprecated since: v6.0.0</span> 49881cb0ef41Sopenharmony_ci</div> 49891cb0ef41Sopenharmony_ci<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> 49901cb0ef41Sopenharmony_ci<p>See <a href="#static-method-bufferallocunsafeslowsize"><code>Buffer.allocUnsafeSlow()</code></a>. This was never a class in the sense that 49911cb0ef41Sopenharmony_cithe constructor always returned a <code>Buffer</code> instance, rather than a <code>SlowBuffer</code> 49921cb0ef41Sopenharmony_ciinstance.</p> 49931cb0ef41Sopenharmony_ci<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> 49941cb0ef41Sopenharmony_ci<div class="api_metadata"> 49951cb0ef41Sopenharmony_ci<span>Deprecated since: v6.0.0</span> 49961cb0ef41Sopenharmony_ci</div> 49971cb0ef41Sopenharmony_ci<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> 49981cb0ef41Sopenharmony_ci<ul> 49991cb0ef41Sopenharmony_ci<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> 50001cb0ef41Sopenharmony_ci</ul> 50011cb0ef41Sopenharmony_ci<p>See <a href="#static-method-bufferallocunsafeslowsize"><code>Buffer.allocUnsafeSlow()</code></a>.</p> 50021cb0ef41Sopenharmony_ci<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> 50031cb0ef41Sopenharmony_ci<div class="api_metadata"> 50041cb0ef41Sopenharmony_ci<span>Added in: v8.2.0</span> 50051cb0ef41Sopenharmony_ci</div> 50061cb0ef41Sopenharmony_ci<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> 50071cb0ef41Sopenharmony_ci<div class="api_metadata"> 50081cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 50091cb0ef41Sopenharmony_ci<table> 50101cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 50111cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 50121cb0ef41Sopenharmony_ci<td><p>Value is changed to 2<sup>32</sup> on 64-bit architectures.</p></td></tr> 50131cb0ef41Sopenharmony_ci<tr><td>v14.0.0</td> 50141cb0ef41Sopenharmony_ci<td><p>Value is changed from 2<sup>31</sup> - 1 to 2<sup>32</sup> - 1 on 64-bit architectures.</p></td></tr> 50151cb0ef41Sopenharmony_ci<tr><td>v8.2.0</td> 50161cb0ef41Sopenharmony_ci<td><p><span>Added in: v8.2.0</span></p></td></tr> 50171cb0ef41Sopenharmony_ci</tbody></table> 50181cb0ef41Sopenharmony_ci</details> 50191cb0ef41Sopenharmony_ci</div> 50201cb0ef41Sopenharmony_ci<ul> 50211cb0ef41Sopenharmony_ci<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> 50221cb0ef41Sopenharmony_ci</ul> 50231cb0ef41Sopenharmony_ci<p>On 32-bit architectures, this value currently is 2<sup>30</sup> - 1 (about 1 50241cb0ef41Sopenharmony_ciGiB).</p> 50251cb0ef41Sopenharmony_ci<p>On 64-bit architectures, this value currently is 2<sup>32</sup> (about 4 GiB).</p> 50261cb0ef41Sopenharmony_ci<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> 50271cb0ef41Sopenharmony_ci<p>This value is also available as <a href="#bufferkmaxlength"><code>buffer.kMaxLength</code></a>.</p> 50281cb0ef41Sopenharmony_ci<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> 50291cb0ef41Sopenharmony_ci<div class="api_metadata"> 50301cb0ef41Sopenharmony_ci<span>Added in: v8.2.0</span> 50311cb0ef41Sopenharmony_ci</div> 50321cb0ef41Sopenharmony_ci<ul> 50331cb0ef41Sopenharmony_ci<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> 50341cb0ef41Sopenharmony_ci</ul> 50351cb0ef41Sopenharmony_ci<p>Represents the largest <code>length</code> that a <code>string</code> primitive can have, counted 50361cb0ef41Sopenharmony_ciin UTF-16 code units.</p> 50371cb0ef41Sopenharmony_ci<p>This value may depend on the JS engine that is being used.</p> 50381cb0ef41Sopenharmony_ci</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> 50391cb0ef41Sopenharmony_ci<p>In versions of Node.js prior to 6.0.0, <code>Buffer</code> instances were created using the 50401cb0ef41Sopenharmony_ci<code>Buffer</code> constructor function, which allocates the returned <code>Buffer</code> 50411cb0ef41Sopenharmony_cidifferently based on what arguments are provided:</p> 50421cb0ef41Sopenharmony_ci<ul> 50431cb0ef41Sopenharmony_ci<li>Passing a number as the first argument to <code>Buffer()</code> (e.g. <code>new Buffer(10)</code>) 50441cb0ef41Sopenharmony_ciallocates a new <code>Buffer</code> object of the specified size. Prior to Node.js 8.0.0, 50451cb0ef41Sopenharmony_cithe memory allocated for such <code>Buffer</code> instances is <em>not</em> initialized and 50461cb0ef41Sopenharmony_ci<em>can contain sensitive data</em>. Such <code>Buffer</code> instances <em>must</em> be subsequently 50471cb0ef41Sopenharmony_ciinitialized by using either <a href="#buffillvalue-offset-end-encoding"><code>buf.fill(0)</code></a> or by writing to the 50481cb0ef41Sopenharmony_cientire <code>Buffer</code> before reading data from the <code>Buffer</code>. 50491cb0ef41Sopenharmony_ciWhile this behavior is <em>intentional</em> to improve performance, 50501cb0ef41Sopenharmony_cidevelopment experience has demonstrated that a more explicit distinction is 50511cb0ef41Sopenharmony_cirequired between creating a fast-but-uninitialized <code>Buffer</code> versus creating a 50521cb0ef41Sopenharmony_cislower-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> 50531cb0ef41Sopenharmony_ci<li>Passing a string, array, or <code>Buffer</code> as the first argument copies the 50541cb0ef41Sopenharmony_cipassed object's data into the <code>Buffer</code>.</li> 50551cb0ef41Sopenharmony_ci<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> 50561cb0ef41Sopenharmony_cithat shares allocated memory with the given array buffer.</li> 50571cb0ef41Sopenharmony_ci</ul> 50581cb0ef41Sopenharmony_ci<p>Because the behavior of <code>new Buffer()</code> is different depending on the type of the 50591cb0ef41Sopenharmony_cifirst argument, security and reliability issues can be inadvertently introduced 50601cb0ef41Sopenharmony_ciinto applications when argument validation or <code>Buffer</code> initialization is not 50611cb0ef41Sopenharmony_ciperformed.</p> 50621cb0ef41Sopenharmony_ci<p>For example, if an attacker can cause an application to receive a number where 50631cb0ef41Sopenharmony_cia string is expected, the application may call <code>new Buffer(100)</code> 50641cb0ef41Sopenharmony_ciinstead of <code>new Buffer("100")</code>, leading it to allocate a 100 byte buffer instead 50651cb0ef41Sopenharmony_ciof allocating a 3 byte buffer with content <code>"100"</code>. This is commonly possible 50661cb0ef41Sopenharmony_ciusing JSON API calls. Since JSON distinguishes between numeric and string types, 50671cb0ef41Sopenharmony_ciit allows injection of numbers where a naively written application that does not 50681cb0ef41Sopenharmony_civalidate its input sufficiently might expect to always receive a string. 50691cb0ef41Sopenharmony_ciBefore Node.js 8.0.0, the 100 byte buffer might contain 50701cb0ef41Sopenharmony_ciarbitrary pre-existing in-memory data, so may be used to expose in-memory 50711cb0ef41Sopenharmony_cisecrets to a remote attacker. Since Node.js 8.0.0, exposure of memory cannot 50721cb0ef41Sopenharmony_cioccur because the data is zero-filled. However, other attacks are still 50731cb0ef41Sopenharmony_cipossible, such as causing very large buffers to be allocated by the server, 50741cb0ef41Sopenharmony_cileading to performance degradation or crashing on memory exhaustion.</p> 50751cb0ef41Sopenharmony_ci<p>To make the creation of <code>Buffer</code> instances more reliable and less error-prone, 50761cb0ef41Sopenharmony_cithe various forms of the <code>new Buffer()</code> constructor have been <strong>deprecated</strong> 50771cb0ef41Sopenharmony_ciand replaced by separate <code>Buffer.from()</code>, <a href="#static-method-bufferallocsize-fill-encoding"><code>Buffer.alloc()</code></a>, and 50781cb0ef41Sopenharmony_ci<a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> methods.</p> 50791cb0ef41Sopenharmony_ci<p><em>Developers should migrate all existing uses of the <code>new Buffer()</code> constructors 50801cb0ef41Sopenharmony_cito one of these new APIs.</em></p> 50811cb0ef41Sopenharmony_ci<ul> 50821cb0ef41Sopenharmony_ci<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 50831cb0ef41Sopenharmony_ciprovided octets.</li> 50841cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferfromarraybuffer-byteoffset-length"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a> 50851cb0ef41Sopenharmony_cireturns a new <code>Buffer</code> that <em>shares the same allocated memory</em> as the given 50861cb0ef41Sopenharmony_ci<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a>.</li> 50871cb0ef41Sopenharmony_ci<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 50881cb0ef41Sopenharmony_cicontents of the given <code>Buffer</code>.</li> 50891cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferfromstring-encoding"><code>Buffer.from(string[, encoding])</code></a> returns a new 50901cb0ef41Sopenharmony_ci<code>Buffer</code> that <em>contains a copy</em> of the provided string.</li> 50911cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferallocsize-fill-encoding"><code>Buffer.alloc(size[, fill[, encoding]])</code></a> returns a new 50921cb0ef41Sopenharmony_ciinitialized <code>Buffer</code> of the specified size. This method is slower than 50931cb0ef41Sopenharmony_ci<a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe(size)</code></a> but guarantees that newly 50941cb0ef41Sopenharmony_cicreated <code>Buffer</code> instances never contain old data that is potentially 50951cb0ef41Sopenharmony_cisensitive. A <code>TypeError</code> will be thrown if <code>size</code> is not a number.</li> 50961cb0ef41Sopenharmony_ci<li><a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe(size)</code></a> and 50971cb0ef41Sopenharmony_ci<a href="#static-method-bufferallocunsafeslowsize"><code>Buffer.allocUnsafeSlow(size)</code></a> each return a 50981cb0ef41Sopenharmony_cinew uninitialized <code>Buffer</code> of the specified <code>size</code>. Because the <code>Buffer</code> is 50991cb0ef41Sopenharmony_ciuninitialized, the allocated segment of memory might contain old data that is 51001cb0ef41Sopenharmony_cipotentially sensitive.</li> 51011cb0ef41Sopenharmony_ci</ul> 51021cb0ef41Sopenharmony_ci<p><code>Buffer</code> instances returned by <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> and 51031cb0ef41Sopenharmony_ci<a href="#static-method-bufferfromarray"><code>Buffer.from(array)</code></a> <em>may</em> be allocated off a shared internal memory pool 51041cb0ef41Sopenharmony_ciif <code>size</code> is less than or equal to half <a href="#class-property-bufferpoolsize"><code>Buffer.poolSize</code></a>. Instances 51051cb0ef41Sopenharmony_cireturned by <a href="#static-method-bufferallocunsafeslowsize"><code>Buffer.allocUnsafeSlow()</code></a> <em>never</em> use the shared internal 51061cb0ef41Sopenharmony_cimemory pool.</p> 51071cb0ef41Sopenharmony_ci<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> 51081cb0ef41Sopenharmony_ci<div class="api_metadata"> 51091cb0ef41Sopenharmony_ci<span>Added in: v5.10.0</span> 51101cb0ef41Sopenharmony_ci</div> 51111cb0ef41Sopenharmony_ci<p>Node.js can be started using the <code>--zero-fill-buffers</code> command-line option to 51121cb0ef41Sopenharmony_cicause all newly-allocated <code>Buffer</code> instances to be zero-filled upon creation by 51131cb0ef41Sopenharmony_cidefault. Without the option, buffers created with <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a>, 51141cb0ef41Sopenharmony_ci<a href="#static-method-bufferallocunsafeslowsize"><code>Buffer.allocUnsafeSlow()</code></a>, and <code>new SlowBuffer(size)</code> are not zero-filled. 51151cb0ef41Sopenharmony_ciUse of this flag can have a measurable negative impact on performance. Use the 51161cb0ef41Sopenharmony_ci<code>--zero-fill-buffers</code> option only when necessary to enforce that newly allocated 51171cb0ef41Sopenharmony_ci<code>Buffer</code> instances cannot contain old data that is potentially sensitive.</p> 51181cb0ef41Sopenharmony_ci<pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">node --zero-fill-buffers</span> 51191cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">> </span><span class="language-bash">Buffer.allocUnsafe(5);</span> 51201cb0ef41Sopenharmony_ci<Buffer 00 00 00 00 00></code> <button class="copy-button">copy</button></pre> 51211cb0ef41Sopenharmony_ci<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> 51221cb0ef41Sopenharmony_ci<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 51231cb0ef41Sopenharmony_cisegment of allocated memory is <em>uninitialized</em> (it is not zeroed-out). While 51241cb0ef41Sopenharmony_cithis design makes the allocation of memory quite fast, the allocated segment of 51251cb0ef41Sopenharmony_cimemory might contain old data that is potentially sensitive. Using a <code>Buffer</code> 51261cb0ef41Sopenharmony_cicreated by <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> without <em>completely</em> overwriting the 51271cb0ef41Sopenharmony_cimemory can allow this old data to be leaked when the <code>Buffer</code> memory is read.</p> 51281cb0ef41Sopenharmony_ci<p>While there are clear performance advantages to using 51291cb0ef41Sopenharmony_ci<a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a>, extra care <em>must</em> be taken in order to avoid 51301cb0ef41Sopenharmony_ciintroducing security vulnerabilities into an application.</p></section> 51311cb0ef41Sopenharmony_ci <!-- API END --> 51321cb0ef41Sopenharmony_ci </div> 51331cb0ef41Sopenharmony_ci </div> 51341cb0ef41Sopenharmony_ci </div> 51351cb0ef41Sopenharmony_ci</body> 51361cb0ef41Sopenharmony_ci</html> 5137