Server IP : 85.214.239.14 / Your IP : 3.137.198.239 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /usr/share/doc/nodejs/api/ |
Upload File : |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <meta name="nodejs.org:node-version" content="v20.18.0"> <title>Buffer | Node.js v20.18.0 Documentation</title> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> <link rel="stylesheet" href="assets/style.css"> <link rel="stylesheet" href="assets/hljs.css"> <link rel="canonical" href="https://nodejs.org/api/buffer.html"> <script async defer src="assets/api.js" type="text/javascript"></script> <script> const storedTheme = localStorage.getItem('theme'); // Follow operating system theme preference if (storedTheme === null && window.matchMedia) { const mq = window.matchMedia('(prefers-color-scheme: dark)'); if (mq.matches) { document.documentElement.classList.add('dark-mode'); } } else if (storedTheme === 'dark') { document.documentElement.classList.add('dark-mode'); } </script> <style>@media(max-width:558px){.with-42-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:718px){.with-62-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:646px){.with-53-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}</style> </head> <body class="alt apidoc" id="api-section-buffer"> <div id="content" class="clearfix"> <div role="navigation" id="column2" class="interior"> <div id="intro" class="interior"> <a href="/" title="Go back to the home page"> Node.js </a> </div> <ul> <li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> <li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> </ul> <hr class="line"> <ul> <li><a href="assert.html" class="nav-assert">Assertion testing</a></li> <li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> <li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> <li><a href="buffer.html" class="nav-buffer active">Buffer</a></li> <li><a href="addons.html" class="nav-addons">C++ addons</a></li> <li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> <li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> <li><a href="child_process.html" class="nav-child_process">Child processes</a></li> <li><a href="cluster.html" class="nav-cluster">Cluster</a></li> <li><a href="cli.html" class="nav-cli">Command-line options</a></li> <li><a href="console.html" class="nav-console">Console</a></li> <li><a href="corepack.html" class="nav-corepack">Corepack</a></li> <li><a href="crypto.html" class="nav-crypto">Crypto</a></li> <li><a href="debugger.html" class="nav-debugger">Debugger</a></li> <li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> <li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> <li><a href="dns.html" class="nav-dns">DNS</a></li> <li><a href="domain.html" class="nav-domain">Domain</a></li> <li><a href="errors.html" class="nav-errors">Errors</a></li> <li><a href="events.html" class="nav-events">Events</a></li> <li><a href="fs.html" class="nav-fs">File system</a></li> <li><a href="globals.html" class="nav-globals">Globals</a></li> <li><a href="http.html" class="nav-http">HTTP</a></li> <li><a href="http2.html" class="nav-http2">HTTP/2</a></li> <li><a href="https.html" class="nav-https">HTTPS</a></li> <li><a href="inspector.html" class="nav-inspector">Inspector</a></li> <li><a href="intl.html" class="nav-intl">Internationalization</a></li> <li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> <li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> <li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> <li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> <li><a href="net.html" class="nav-net">Net</a></li> <li><a href="os.html" class="nav-os">OS</a></li> <li><a href="path.html" class="nav-path">Path</a></li> <li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> <li><a href="permissions.html" class="nav-permissions">Permissions</a></li> <li><a href="process.html" class="nav-process">Process</a></li> <li><a href="punycode.html" class="nav-punycode">Punycode</a></li> <li><a href="querystring.html" class="nav-querystring">Query strings</a></li> <li><a href="readline.html" class="nav-readline">Readline</a></li> <li><a href="repl.html" class="nav-repl">REPL</a></li> <li><a href="report.html" class="nav-report">Report</a></li> <li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> <li><a href="stream.html" class="nav-stream">Stream</a></li> <li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> <li><a href="test.html" class="nav-test">Test runner</a></li> <li><a href="timers.html" class="nav-timers">Timers</a></li> <li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> <li><a href="tracing.html" class="nav-tracing">Trace events</a></li> <li><a href="tty.html" class="nav-tty">TTY</a></li> <li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> <li><a href="url.html" class="nav-url">URL</a></li> <li><a href="util.html" class="nav-util">Utilities</a></li> <li><a href="v8.html" class="nav-v8">V8</a></li> <li><a href="vm.html" class="nav-vm">VM</a></li> <li><a href="wasi.html" class="nav-wasi">WASI</a></li> <li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> <li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> <li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> <li><a href="zlib.html" class="nav-zlib">Zlib</a></li> </ul> <hr class="line"> <ul> <li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> </ul> </div> <div id="column1" data-id="buffer" class="interior"> <header class="header"> <div class="header-container"> <h1>Node.js v20.18.0 documentation</h1> <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> <path fill="none" d="M0 0h24v24H0z" /> <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"/> <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"/> </svg> <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> <path d="M0 0h24v24H0z" fill="none" /> <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"/> </svg> </button> </div> <div id="gtoc"> <ul> <li class="pinned-header">Node.js v20.18.0</li> <li class="picker-header"> <a href="#"> <span class="picker-arrow"></span> Table of contents </a> <div class="picker"><div class="toc"><ul> <li><span class="stability_2"><a href="#buffer">Buffer</a></span> <ul> <li><a href="#buffers-and-character-encodings">Buffers and character encodings</a></li> <li><a href="#buffers-and-typedarrays">Buffers and TypedArrays</a></li> <li><a href="#buffers-and-iteration">Buffers and iteration</a></li> <li><a href="#class-blob">Class: <code>Blob</code></a> <ul> <li><a href="#new-bufferblobsources-options"><code>new buffer.Blob([sources[, options]])</code></a></li> <li><a href="#blobarraybuffer"><code>blob.arrayBuffer()</code></a> <ul> <li><a href="#blobbytes"><code>blob.bytes()</code></a></li> </ul> </li> <li><a href="#blobsize"><code>blob.size</code></a></li> <li><a href="#blobslicestart-end-type"><code>blob.slice([start[, end[, type]]])</code></a></li> <li><a href="#blobstream"><code>blob.stream()</code></a></li> <li><a href="#blobtext"><code>blob.text()</code></a></li> <li><a href="#blobtype"><code>blob.type</code></a></li> <li><a href="#blob-objects-and-messagechannel"><code>Blob</code> objects and <code>MessageChannel</code></a></li> </ul> </li> <li><a href="#class-buffer">Class: <code>Buffer</code></a> <ul> <li><a href="#static-method-bufferallocsize-fill-encoding">Static method: <code>Buffer.alloc(size[, fill[, encoding]])</code></a></li> <li><a href="#static-method-bufferallocunsafesize">Static method: <code>Buffer.allocUnsafe(size)</code></a></li> <li><a href="#static-method-bufferallocunsafeslowsize">Static method: <code>Buffer.allocUnsafeSlow(size)</code></a></li> <li><a href="#static-method-bufferbytelengthstring-encoding">Static method: <code>Buffer.byteLength(string[, encoding])</code></a></li> <li><a href="#static-method-buffercomparebuf1-buf2">Static method: <code>Buffer.compare(buf1, buf2)</code></a></li> <li><a href="#static-method-bufferconcatlist-totallength">Static method: <code>Buffer.concat(list[, totalLength])</code></a></li> <li><a href="#static-method-buffercopybytesfromview-offset-length">Static method: <code>Buffer.copyBytesFrom(view[, offset[, length]])</code></a></li> <li><a href="#static-method-bufferfromarray">Static method: <code>Buffer.from(array)</code></a></li> <li><a href="#static-method-bufferfromarraybuffer-byteoffset-length">Static method: <code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a></li> <li><a href="#static-method-bufferfrombuffer">Static method: <code>Buffer.from(buffer)</code></a></li> <li><a href="#static-method-bufferfromobject-offsetorencoding-length">Static method: <code>Buffer.from(object[, offsetOrEncoding[, length]])</code></a></li> <li><a href="#static-method-bufferfromstring-encoding">Static method: <code>Buffer.from(string[, encoding])</code></a></li> <li><a href="#static-method-bufferisbufferobj">Static method: <code>Buffer.isBuffer(obj)</code></a></li> <li><a href="#static-method-bufferisencodingencoding">Static method: <code>Buffer.isEncoding(encoding)</code></a></li> <li><a href="#class-property-bufferpoolsize">Class property: <code>Buffer.poolSize</code></a></li> <li><a href="#bufindex"><code>buf[index]</code></a></li> <li><a href="#bufbuffer"><code>buf.buffer</code></a></li> <li><a href="#bufbyteoffset"><code>buf.byteOffset</code></a></li> <li><a href="#bufcomparetarget-targetstart-targetend-sourcestart-sourceend"><code>buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])</code></a></li> <li><a href="#bufcopytarget-targetstart-sourcestart-sourceend"><code>buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])</code></a></li> <li><a href="#bufentries"><code>buf.entries()</code></a></li> <li><a href="#bufequalsotherbuffer"><code>buf.equals(otherBuffer)</code></a></li> <li><a href="#buffillvalue-offset-end-encoding"><code>buf.fill(value[, offset[, end]][, encoding])</code></a></li> <li><a href="#bufincludesvalue-byteoffset-encoding"><code>buf.includes(value[, byteOffset][, encoding])</code></a></li> <li><a href="#bufindexofvalue-byteoffset-encoding"><code>buf.indexOf(value[, byteOffset][, encoding])</code></a></li> <li><a href="#bufkeys"><code>buf.keys()</code></a></li> <li><a href="#buflastindexofvalue-byteoffset-encoding"><code>buf.lastIndexOf(value[, byteOffset][, encoding])</code></a></li> <li><a href="#buflength"><code>buf.length</code></a></li> <li><span class="stability_0"><a href="#bufparent"><code>buf.parent</code></a></span></li> <li><a href="#bufreadbigint64beoffset"><code>buf.readBigInt64BE([offset])</code></a></li> <li><a href="#bufreadbigint64leoffset"><code>buf.readBigInt64LE([offset])</code></a></li> <li><a href="#bufreadbiguint64beoffset"><code>buf.readBigUInt64BE([offset])</code></a></li> <li><a href="#bufreadbiguint64leoffset"><code>buf.readBigUInt64LE([offset])</code></a></li> <li><a href="#bufreaddoublebeoffset"><code>buf.readDoubleBE([offset])</code></a></li> <li><a href="#bufreaddoubleleoffset"><code>buf.readDoubleLE([offset])</code></a></li> <li><a href="#bufreadfloatbeoffset"><code>buf.readFloatBE([offset])</code></a></li> <li><a href="#bufreadfloatleoffset"><code>buf.readFloatLE([offset])</code></a></li> <li><a href="#bufreadint8offset"><code>buf.readInt8([offset])</code></a></li> <li><a href="#bufreadint16beoffset"><code>buf.readInt16BE([offset])</code></a></li> <li><a href="#bufreadint16leoffset"><code>buf.readInt16LE([offset])</code></a></li> <li><a href="#bufreadint32beoffset"><code>buf.readInt32BE([offset])</code></a></li> <li><a href="#bufreadint32leoffset"><code>buf.readInt32LE([offset])</code></a></li> <li><a href="#bufreadintbeoffset-bytelength"><code>buf.readIntBE(offset, byteLength)</code></a></li> <li><a href="#bufreadintleoffset-bytelength"><code>buf.readIntLE(offset, byteLength)</code></a></li> <li><a href="#bufreaduint8offset"><code>buf.readUInt8([offset])</code></a></li> <li><a href="#bufreaduint16beoffset"><code>buf.readUInt16BE([offset])</code></a></li> <li><a href="#bufreaduint16leoffset"><code>buf.readUInt16LE([offset])</code></a></li> <li><a href="#bufreaduint32beoffset"><code>buf.readUInt32BE([offset])</code></a></li> <li><a href="#bufreaduint32leoffset"><code>buf.readUInt32LE([offset])</code></a></li> <li><a href="#bufreaduintbeoffset-bytelength"><code>buf.readUIntBE(offset, byteLength)</code></a></li> <li><a href="#bufreaduintleoffset-bytelength"><code>buf.readUIntLE(offset, byteLength)</code></a></li> <li><a href="#bufsubarraystart-end"><code>buf.subarray([start[, end]])</code></a></li> <li><span class="stability_0"><a href="#bufslicestart-end"><code>buf.slice([start[, end]])</code></a></span></li> <li><a href="#bufswap16"><code>buf.swap16()</code></a></li> <li><a href="#bufswap32"><code>buf.swap32()</code></a></li> <li><a href="#bufswap64"><code>buf.swap64()</code></a></li> <li><a href="#buftojson"><code>buf.toJSON()</code></a></li> <li><a href="#buftostringencoding-start-end"><code>buf.toString([encoding[, start[, end]]])</code></a></li> <li><a href="#bufvalues"><code>buf.values()</code></a></li> <li><a href="#bufwritestring-offset-length-encoding"><code>buf.write(string[, offset[, length]][, encoding])</code></a></li> <li><a href="#bufwritebigint64bevalue-offset"><code>buf.writeBigInt64BE(value[, offset])</code></a></li> <li><a href="#bufwritebigint64levalue-offset"><code>buf.writeBigInt64LE(value[, offset])</code></a></li> <li><a href="#bufwritebiguint64bevalue-offset"><code>buf.writeBigUInt64BE(value[, offset])</code></a></li> <li><a href="#bufwritebiguint64levalue-offset"><code>buf.writeBigUInt64LE(value[, offset])</code></a></li> <li><a href="#bufwritedoublebevalue-offset"><code>buf.writeDoubleBE(value[, offset])</code></a></li> <li><a href="#bufwritedoublelevalue-offset"><code>buf.writeDoubleLE(value[, offset])</code></a></li> <li><a href="#bufwritefloatbevalue-offset"><code>buf.writeFloatBE(value[, offset])</code></a></li> <li><a href="#bufwritefloatlevalue-offset"><code>buf.writeFloatLE(value[, offset])</code></a></li> <li><a href="#bufwriteint8value-offset"><code>buf.writeInt8(value[, offset])</code></a></li> <li><a href="#bufwriteint16bevalue-offset"><code>buf.writeInt16BE(value[, offset])</code></a></li> <li><a href="#bufwriteint16levalue-offset"><code>buf.writeInt16LE(value[, offset])</code></a></li> <li><a href="#bufwriteint32bevalue-offset"><code>buf.writeInt32BE(value[, offset])</code></a></li> <li><a href="#bufwriteint32levalue-offset"><code>buf.writeInt32LE(value[, offset])</code></a></li> <li><a href="#bufwriteintbevalue-offset-bytelength"><code>buf.writeIntBE(value, offset, byteLength)</code></a></li> <li><a href="#bufwriteintlevalue-offset-bytelength"><code>buf.writeIntLE(value, offset, byteLength)</code></a></li> <li><a href="#bufwriteuint8value-offset"><code>buf.writeUInt8(value[, offset])</code></a></li> <li><a href="#bufwriteuint16bevalue-offset"><code>buf.writeUInt16BE(value[, offset])</code></a></li> <li><a href="#bufwriteuint16levalue-offset"><code>buf.writeUInt16LE(value[, offset])</code></a></li> <li><a href="#bufwriteuint32bevalue-offset"><code>buf.writeUInt32BE(value[, offset])</code></a></li> <li><a href="#bufwriteuint32levalue-offset"><code>buf.writeUInt32LE(value[, offset])</code></a></li> <li><a href="#bufwriteuintbevalue-offset-bytelength"><code>buf.writeUIntBE(value, offset, byteLength)</code></a></li> <li><a href="#bufwriteuintlevalue-offset-bytelength"><code>buf.writeUIntLE(value, offset, byteLength)</code></a></li> <li><span class="stability_0"><a href="#new-bufferarray"><code>new Buffer(array)</code></a></span></li> <li><span class="stability_0"><a href="#new-bufferarraybuffer-byteoffset-length"><code>new Buffer(arrayBuffer[, byteOffset[, length]])</code></a></span></li> <li><span class="stability_0"><a href="#new-bufferbuffer"><code>new Buffer(buffer)</code></a></span></li> <li><span class="stability_0"><a href="#new-buffersize"><code>new Buffer(size)</code></a></span></li> <li><span class="stability_0"><a href="#new-bufferstring-encoding"><code>new Buffer(string[, encoding])</code></a></span></li> </ul> </li> <li><a href="#class-file">Class: <code>File</code></a> <ul> <li><a href="#new-bufferfilesources-filename-options"><code>new buffer.File(sources, fileName[, options])</code></a></li> <li><a href="#filename"><code>file.name</code></a></li> <li><a href="#filelastmodified"><code>file.lastModified</code></a></li> </ul> </li> <li><a href="#nodebuffer-module-apis"><code>node:buffer</code> module APIs</a> <ul> <li><span class="stability_3"><a href="#bufferatobdata"><code>buffer.atob(data)</code></a></span></li> <li><span class="stability_3"><a href="#bufferbtoadata"><code>buffer.btoa(data)</code></a></span></li> <li><a href="#bufferisasciiinput"><code>buffer.isAscii(input)</code></a></li> <li><a href="#bufferisutf8input"><code>buffer.isUtf8(input)</code></a></li> <li><a href="#bufferinspect_max_bytes"><code>buffer.INSPECT_MAX_BYTES</code></a></li> <li><a href="#bufferkmaxlength"><code>buffer.kMaxLength</code></a></li> <li><a href="#bufferkstringmaxlength"><code>buffer.kStringMaxLength</code></a></li> <li><span class="stability_1"><a href="#bufferresolveobjecturlid"><code>buffer.resolveObjectURL(id)</code></a></span></li> <li><a href="#buffertranscodesource-fromenc-toenc"><code>buffer.transcode(source, fromEnc, toEnc)</code></a></li> <li><span class="stability_0"><a href="#class-slowbuffer">Class: <code>SlowBuffer</code></a></span> <ul> <li><span class="stability_0"><a href="#new-slowbuffersize"><code>new SlowBuffer(size)</code></a></span></li> </ul> </li> <li><a href="#buffer-constants">Buffer constants</a> <ul> <li><a href="#bufferconstantsmax_length"><code>buffer.constants.MAX_LENGTH</code></a></li> <li><a href="#bufferconstantsmax_string_length"><code>buffer.constants.MAX_STRING_LENGTH</code></a></li> </ul> </li> </ul> </li> <li><a href="#bufferfrom-bufferalloc-and-bufferallocunsafe"><code>Buffer.from()</code>, <code>Buffer.alloc()</code>, and <code>Buffer.allocUnsafe()</code></a> <ul> <li><a href="#the---zero-fill-buffers-command-line-option">The <code>--zero-fill-buffers</code> command-line option</a></li> <li><a href="#what-makes-bufferallocunsafe-and-bufferallocunsafeslow-unsafe">What makes <code>Buffer.allocUnsafe()</code> and <code>Buffer.allocUnsafeSlow()</code> "unsafe"?</a></li> </ul> </li> </ul> </li> </ul></div></div> </li> <li class="picker-header"> <a href="#"> <span class="picker-arrow"></span> Index </a> <div class="picker"><ul> <li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> <li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> <li> <a href="index.html">Index</a> </li> </ul> <hr class="line"> <ul> <li><a href="assert.html" class="nav-assert">Assertion testing</a></li> <li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> <li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> <li><a href="buffer.html" class="nav-buffer active">Buffer</a></li> <li><a href="addons.html" class="nav-addons">C++ addons</a></li> <li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> <li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> <li><a href="child_process.html" class="nav-child_process">Child processes</a></li> <li><a href="cluster.html" class="nav-cluster">Cluster</a></li> <li><a href="cli.html" class="nav-cli">Command-line options</a></li> <li><a href="console.html" class="nav-console">Console</a></li> <li><a href="corepack.html" class="nav-corepack">Corepack</a></li> <li><a href="crypto.html" class="nav-crypto">Crypto</a></li> <li><a href="debugger.html" class="nav-debugger">Debugger</a></li> <li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> <li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> <li><a href="dns.html" class="nav-dns">DNS</a></li> <li><a href="domain.html" class="nav-domain">Domain</a></li> <li><a href="errors.html" class="nav-errors">Errors</a></li> <li><a href="events.html" class="nav-events">Events</a></li> <li><a href="fs.html" class="nav-fs">File system</a></li> <li><a href="globals.html" class="nav-globals">Globals</a></li> <li><a href="http.html" class="nav-http">HTTP</a></li> <li><a href="http2.html" class="nav-http2">HTTP/2</a></li> <li><a href="https.html" class="nav-https">HTTPS</a></li> <li><a href="inspector.html" class="nav-inspector">Inspector</a></li> <li><a href="intl.html" class="nav-intl">Internationalization</a></li> <li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> <li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> <li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> <li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> <li><a href="net.html" class="nav-net">Net</a></li> <li><a href="os.html" class="nav-os">OS</a></li> <li><a href="path.html" class="nav-path">Path</a></li> <li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> <li><a href="permissions.html" class="nav-permissions">Permissions</a></li> <li><a href="process.html" class="nav-process">Process</a></li> <li><a href="punycode.html" class="nav-punycode">Punycode</a></li> <li><a href="querystring.html" class="nav-querystring">Query strings</a></li> <li><a href="readline.html" class="nav-readline">Readline</a></li> <li><a href="repl.html" class="nav-repl">REPL</a></li> <li><a href="report.html" class="nav-report">Report</a></li> <li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> <li><a href="stream.html" class="nav-stream">Stream</a></li> <li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> <li><a href="test.html" class="nav-test">Test runner</a></li> <li><a href="timers.html" class="nav-timers">Timers</a></li> <li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> <li><a href="tracing.html" class="nav-tracing">Trace events</a></li> <li><a href="tty.html" class="nav-tty">TTY</a></li> <li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> <li><a href="url.html" class="nav-url">URL</a></li> <li><a href="util.html" class="nav-util">Utilities</a></li> <li><a href="v8.html" class="nav-v8">V8</a></li> <li><a href="vm.html" class="nav-vm">VM</a></li> <li><a href="wasi.html" class="nav-wasi">WASI</a></li> <li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> <li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> <li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> <li><a href="zlib.html" class="nav-zlib">Zlib</a></li> </ul> <hr class="line"> <ul> <li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> </ul></div> </li> <li class="picker-header"> <a href="#"> <span class="picker-arrow"></span> Other versions </a> <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v22.x/api/buffer.html">22.x</a></li> <li><a href="https://nodejs.org/docs/latest-v21.x/api/buffer.html">21.x</a></li> <li><a href="https://nodejs.org/docs/latest-v20.x/api/buffer.html">20.x <b>LTS</b></a></li> <li><a href="https://nodejs.org/docs/latest-v19.x/api/buffer.html">19.x</a></li> <li><a href="https://nodejs.org/docs/latest-v18.x/api/buffer.html">18.x <b>LTS</b></a></li> <li><a href="https://nodejs.org/docs/latest-v17.x/api/buffer.html">17.x</a></li> <li><a href="https://nodejs.org/docs/latest-v16.x/api/buffer.html">16.x</a></li> <li><a href="https://nodejs.org/docs/latest-v15.x/api/buffer.html">15.x</a></li> <li><a href="https://nodejs.org/docs/latest-v14.x/api/buffer.html">14.x</a></li> <li><a href="https://nodejs.org/docs/latest-v13.x/api/buffer.html">13.x</a></li> <li><a href="https://nodejs.org/docs/latest-v12.x/api/buffer.html">12.x</a></li> <li><a href="https://nodejs.org/docs/latest-v11.x/api/buffer.html">11.x</a></li> <li><a href="https://nodejs.org/docs/latest-v10.x/api/buffer.html">10.x</a></li> <li><a href="https://nodejs.org/docs/latest-v9.x/api/buffer.html">9.x</a></li> <li><a href="https://nodejs.org/docs/latest-v8.x/api/buffer.html">8.x</a></li> <li><a href="https://nodejs.org/docs/latest-v7.x/api/buffer.html">7.x</a></li> <li><a href="https://nodejs.org/docs/latest-v6.x/api/buffer.html">6.x</a></li> <li><a href="https://nodejs.org/docs/latest-v5.x/api/buffer.html">5.x</a></li> <li><a href="https://nodejs.org/docs/latest-v4.x/api/buffer.html">4.x</a></li> <li><a href="https://nodejs.org/docs/latest-v0.12.x/api/buffer.html">0.12.x</a></li> <li><a href="https://nodejs.org/docs/latest-v0.10.x/api/buffer.html">0.10.x</a></li></ol></div> </li> <li class="picker-header"> <a href="#"> <span class="picker-arrow"></span> Options </a> <div class="picker"> <ul> <li> <a href="all.html">View on single page</a> </li> <li> <a href="buffer.json">View as JSON</a> </li> <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/buffer.md">Edit on GitHub</a></li> </ul> </div> </li> </ul> </div> <hr> </header> <details role="navigation" id="toc" open><summary>Table of contents</summary><ul> <li><span class="stability_2"><a href="#buffer">Buffer</a></span> <ul> <li><a href="#buffers-and-character-encodings">Buffers and character encodings</a></li> <li><a href="#buffers-and-typedarrays">Buffers and TypedArrays</a></li> <li><a href="#buffers-and-iteration">Buffers and iteration</a></li> <li><a href="#class-blob">Class: <code>Blob</code></a> <ul> <li><a href="#new-bufferblobsources-options"><code>new buffer.Blob([sources[, options]])</code></a></li> <li><a href="#blobarraybuffer"><code>blob.arrayBuffer()</code></a> <ul> <li><a href="#blobbytes"><code>blob.bytes()</code></a></li> </ul> </li> <li><a href="#blobsize"><code>blob.size</code></a></li> <li><a href="#blobslicestart-end-type"><code>blob.slice([start[, end[, type]]])</code></a></li> <li><a href="#blobstream"><code>blob.stream()</code></a></li> <li><a href="#blobtext"><code>blob.text()</code></a></li> <li><a href="#blobtype"><code>blob.type</code></a></li> <li><a href="#blob-objects-and-messagechannel"><code>Blob</code> objects and <code>MessageChannel</code></a></li> </ul> </li> <li><a href="#class-buffer">Class: <code>Buffer</code></a> <ul> <li><a href="#static-method-bufferallocsize-fill-encoding">Static method: <code>Buffer.alloc(size[, fill[, encoding]])</code></a></li> <li><a href="#static-method-bufferallocunsafesize">Static method: <code>Buffer.allocUnsafe(size)</code></a></li> <li><a href="#static-method-bufferallocunsafeslowsize">Static method: <code>Buffer.allocUnsafeSlow(size)</code></a></li> <li><a href="#static-method-bufferbytelengthstring-encoding">Static method: <code>Buffer.byteLength(string[, encoding])</code></a></li> <li><a href="#static-method-buffercomparebuf1-buf2">Static method: <code>Buffer.compare(buf1, buf2)</code></a></li> <li><a href="#static-method-bufferconcatlist-totallength">Static method: <code>Buffer.concat(list[, totalLength])</code></a></li> <li><a href="#static-method-buffercopybytesfromview-offset-length">Static method: <code>Buffer.copyBytesFrom(view[, offset[, length]])</code></a></li> <li><a href="#static-method-bufferfromarray">Static method: <code>Buffer.from(array)</code></a></li> <li><a href="#static-method-bufferfromarraybuffer-byteoffset-length">Static method: <code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a></li> <li><a href="#static-method-bufferfrombuffer">Static method: <code>Buffer.from(buffer)</code></a></li> <li><a href="#static-method-bufferfromobject-offsetorencoding-length">Static method: <code>Buffer.from(object[, offsetOrEncoding[, length]])</code></a></li> <li><a href="#static-method-bufferfromstring-encoding">Static method: <code>Buffer.from(string[, encoding])</code></a></li> <li><a href="#static-method-bufferisbufferobj">Static method: <code>Buffer.isBuffer(obj)</code></a></li> <li><a href="#static-method-bufferisencodingencoding">Static method: <code>Buffer.isEncoding(encoding)</code></a></li> <li><a href="#class-property-bufferpoolsize">Class property: <code>Buffer.poolSize</code></a></li> <li><a href="#bufindex"><code>buf[index]</code></a></li> <li><a href="#bufbuffer"><code>buf.buffer</code></a></li> <li><a href="#bufbyteoffset"><code>buf.byteOffset</code></a></li> <li><a href="#bufcomparetarget-targetstart-targetend-sourcestart-sourceend"><code>buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])</code></a></li> <li><a href="#bufcopytarget-targetstart-sourcestart-sourceend"><code>buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])</code></a></li> <li><a href="#bufentries"><code>buf.entries()</code></a></li> <li><a href="#bufequalsotherbuffer"><code>buf.equals(otherBuffer)</code></a></li> <li><a href="#buffillvalue-offset-end-encoding"><code>buf.fill(value[, offset[, end]][, encoding])</code></a></li> <li><a href="#bufincludesvalue-byteoffset-encoding"><code>buf.includes(value[, byteOffset][, encoding])</code></a></li> <li><a href="#bufindexofvalue-byteoffset-encoding"><code>buf.indexOf(value[, byteOffset][, encoding])</code></a></li> <li><a href="#bufkeys"><code>buf.keys()</code></a></li> <li><a href="#buflastindexofvalue-byteoffset-encoding"><code>buf.lastIndexOf(value[, byteOffset][, encoding])</code></a></li> <li><a href="#buflength"><code>buf.length</code></a></li> <li><span class="stability_0"><a href="#bufparent"><code>buf.parent</code></a></span></li> <li><a href="#bufreadbigint64beoffset"><code>buf.readBigInt64BE([offset])</code></a></li> <li><a href="#bufreadbigint64leoffset"><code>buf.readBigInt64LE([offset])</code></a></li> <li><a href="#bufreadbiguint64beoffset"><code>buf.readBigUInt64BE([offset])</code></a></li> <li><a href="#bufreadbiguint64leoffset"><code>buf.readBigUInt64LE([offset])</code></a></li> <li><a href="#bufreaddoublebeoffset"><code>buf.readDoubleBE([offset])</code></a></li> <li><a href="#bufreaddoubleleoffset"><code>buf.readDoubleLE([offset])</code></a></li> <li><a href="#bufreadfloatbeoffset"><code>buf.readFloatBE([offset])</code></a></li> <li><a href="#bufreadfloatleoffset"><code>buf.readFloatLE([offset])</code></a></li> <li><a href="#bufreadint8offset"><code>buf.readInt8([offset])</code></a></li> <li><a href="#bufreadint16beoffset"><code>buf.readInt16BE([offset])</code></a></li> <li><a href="#bufreadint16leoffset"><code>buf.readInt16LE([offset])</code></a></li> <li><a href="#bufreadint32beoffset"><code>buf.readInt32BE([offset])</code></a></li> <li><a href="#bufreadint32leoffset"><code>buf.readInt32LE([offset])</code></a></li> <li><a href="#bufreadintbeoffset-bytelength"><code>buf.readIntBE(offset, byteLength)</code></a></li> <li><a href="#bufreadintleoffset-bytelength"><code>buf.readIntLE(offset, byteLength)</code></a></li> <li><a href="#bufreaduint8offset"><code>buf.readUInt8([offset])</code></a></li> <li><a href="#bufreaduint16beoffset"><code>buf.readUInt16BE([offset])</code></a></li> <li><a href="#bufreaduint16leoffset"><code>buf.readUInt16LE([offset])</code></a></li> <li><a href="#bufreaduint32beoffset"><code>buf.readUInt32BE([offset])</code></a></li> <li><a href="#bufreaduint32leoffset"><code>buf.readUInt32LE([offset])</code></a></li> <li><a href="#bufreaduintbeoffset-bytelength"><code>buf.readUIntBE(offset, byteLength)</code></a></li> <li><a href="#bufreaduintleoffset-bytelength"><code>buf.readUIntLE(offset, byteLength)</code></a></li> <li><a href="#bufsubarraystart-end"><code>buf.subarray([start[, end]])</code></a></li> <li><span class="stability_0"><a href="#bufslicestart-end"><code>buf.slice([start[, end]])</code></a></span></li> <li><a href="#bufswap16"><code>buf.swap16()</code></a></li> <li><a href="#bufswap32"><code>buf.swap32()</code></a></li> <li><a href="#bufswap64"><code>buf.swap64()</code></a></li> <li><a href="#buftojson"><code>buf.toJSON()</code></a></li> <li><a href="#buftostringencoding-start-end"><code>buf.toString([encoding[, start[, end]]])</code></a></li> <li><a href="#bufvalues"><code>buf.values()</code></a></li> <li><a href="#bufwritestring-offset-length-encoding"><code>buf.write(string[, offset[, length]][, encoding])</code></a></li> <li><a href="#bufwritebigint64bevalue-offset"><code>buf.writeBigInt64BE(value[, offset])</code></a></li> <li><a href="#bufwritebigint64levalue-offset"><code>buf.writeBigInt64LE(value[, offset])</code></a></li> <li><a href="#bufwritebiguint64bevalue-offset"><code>buf.writeBigUInt64BE(value[, offset])</code></a></li> <li><a href="#bufwritebiguint64levalue-offset"><code>buf.writeBigUInt64LE(value[, offset])</code></a></li> <li><a href="#bufwritedoublebevalue-offset"><code>buf.writeDoubleBE(value[, offset])</code></a></li> <li><a href="#bufwritedoublelevalue-offset"><code>buf.writeDoubleLE(value[, offset])</code></a></li> <li><a href="#bufwritefloatbevalue-offset"><code>buf.writeFloatBE(value[, offset])</code></a></li> <li><a href="#bufwritefloatlevalue-offset"><code>buf.writeFloatLE(value[, offset])</code></a></li> <li><a href="#bufwriteint8value-offset"><code>buf.writeInt8(value[, offset])</code></a></li> <li><a href="#bufwriteint16bevalue-offset"><code>buf.writeInt16BE(value[, offset])</code></a></li> <li><a href="#bufwriteint16levalue-offset"><code>buf.writeInt16LE(value[, offset])</code></a></li> <li><a href="#bufwriteint32bevalue-offset"><code>buf.writeInt32BE(value[, offset])</code></a></li> <li><a href="#bufwriteint32levalue-offset"><code>buf.writeInt32LE(value[, offset])</code></a></li> <li><a href="#bufwriteintbevalue-offset-bytelength"><code>buf.writeIntBE(value, offset, byteLength)</code></a></li> <li><a href="#bufwriteintlevalue-offset-bytelength"><code>buf.writeIntLE(value, offset, byteLength)</code></a></li> <li><a href="#bufwriteuint8value-offset"><code>buf.writeUInt8(value[, offset])</code></a></li> <li><a href="#bufwriteuint16bevalue-offset"><code>buf.writeUInt16BE(value[, offset])</code></a></li> <li><a href="#bufwriteuint16levalue-offset"><code>buf.writeUInt16LE(value[, offset])</code></a></li> <li><a href="#bufwriteuint32bevalue-offset"><code>buf.writeUInt32BE(value[, offset])</code></a></li> <li><a href="#bufwriteuint32levalue-offset"><code>buf.writeUInt32LE(value[, offset])</code></a></li> <li><a href="#bufwriteuintbevalue-offset-bytelength"><code>buf.writeUIntBE(value, offset, byteLength)</code></a></li> <li><a href="#bufwriteuintlevalue-offset-bytelength"><code>buf.writeUIntLE(value, offset, byteLength)</code></a></li> <li><span class="stability_0"><a href="#new-bufferarray"><code>new Buffer(array)</code></a></span></li> <li><span class="stability_0"><a href="#new-bufferarraybuffer-byteoffset-length"><code>new Buffer(arrayBuffer[, byteOffset[, length]])</code></a></span></li> <li><span class="stability_0"><a href="#new-bufferbuffer"><code>new Buffer(buffer)</code></a></span></li> <li><span class="stability_0"><a href="#new-buffersize"><code>new Buffer(size)</code></a></span></li> <li><span class="stability_0"><a href="#new-bufferstring-encoding"><code>new Buffer(string[, encoding])</code></a></span></li> </ul> </li> <li><a href="#class-file">Class: <code>File</code></a> <ul> <li><a href="#new-bufferfilesources-filename-options"><code>new buffer.File(sources, fileName[, options])</code></a></li> <li><a href="#filename"><code>file.name</code></a></li> <li><a href="#filelastmodified"><code>file.lastModified</code></a></li> </ul> </li> <li><a href="#nodebuffer-module-apis"><code>node:buffer</code> module APIs</a> <ul> <li><span class="stability_3"><a href="#bufferatobdata"><code>buffer.atob(data)</code></a></span></li> <li><span class="stability_3"><a href="#bufferbtoadata"><code>buffer.btoa(data)</code></a></span></li> <li><a href="#bufferisasciiinput"><code>buffer.isAscii(input)</code></a></li> <li><a href="#bufferisutf8input"><code>buffer.isUtf8(input)</code></a></li> <li><a href="#bufferinspect_max_bytes"><code>buffer.INSPECT_MAX_BYTES</code></a></li> <li><a href="#bufferkmaxlength"><code>buffer.kMaxLength</code></a></li> <li><a href="#bufferkstringmaxlength"><code>buffer.kStringMaxLength</code></a></li> <li><span class="stability_1"><a href="#bufferresolveobjecturlid"><code>buffer.resolveObjectURL(id)</code></a></span></li> <li><a href="#buffertranscodesource-fromenc-toenc"><code>buffer.transcode(source, fromEnc, toEnc)</code></a></li> <li><span class="stability_0"><a href="#class-slowbuffer">Class: <code>SlowBuffer</code></a></span> <ul> <li><span class="stability_0"><a href="#new-slowbuffersize"><code>new SlowBuffer(size)</code></a></span></li> </ul> </li> <li><a href="#buffer-constants">Buffer constants</a> <ul> <li><a href="#bufferconstantsmax_length"><code>buffer.constants.MAX_LENGTH</code></a></li> <li><a href="#bufferconstantsmax_string_length"><code>buffer.constants.MAX_STRING_LENGTH</code></a></li> </ul> </li> </ul> </li> <li><a href="#bufferfrom-bufferalloc-and-bufferallocunsafe"><code>Buffer.from()</code>, <code>Buffer.alloc()</code>, and <code>Buffer.allocUnsafe()</code></a> <ul> <li><a href="#the---zero-fill-buffers-command-line-option">The <code>--zero-fill-buffers</code> command-line option</a></li> <li><a href="#what-makes-bufferallocunsafe-and-bufferallocunsafeslow-unsafe">What makes <code>Buffer.allocUnsafe()</code> and <code>Buffer.allocUnsafeSlow()</code> "unsafe"?</a></li> </ul> </li> </ul> </li> </ul></details> <div role="main" id="apicontent"> <h2>Buffer<span><a class="mark" href="#buffer" id="buffer">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer"></a></h2> <p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> <p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v20.18.0/lib/buffer.js">lib/buffer.js</a></p> <p><code>Buffer</code> objects are used to represent a fixed-length sequence of bytes. Many Node.js APIs support <code>Buffer</code>s.</p> <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 extends it with methods that cover additional use cases. Node.js APIs accept plain <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> <p>While the <code>Buffer</code> class is available within the global scope, it is still recommended to explicitly reference it via an import or require statement.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <span class="hljs-comment">// Creates a zero-filled Buffer of length 10.</span> <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>); <span class="hljs-comment">// Creates a Buffer of length 10,</span> <span class="hljs-comment">// filled with bytes which all have the value `1`.</span> <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>); <span class="hljs-comment">// Creates an uninitialized buffer of length 10.</span> <span class="hljs-comment">// This is faster than calling Buffer.alloc() but the returned</span> <span class="hljs-comment">// Buffer instance might contain old data that needs to be</span> <span class="hljs-comment">// overwritten using fill(), write(), or other functions that fill the Buffer's</span> <span class="hljs-comment">// contents.</span> <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>); <span class="hljs-comment">// Creates a Buffer containing the bytes [1, 2, 3].</span> <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>]); <span class="hljs-comment">// Creates a Buffer containing the bytes [1, 1, 1, 1] – the entries</span> <span class="hljs-comment">// are all truncated using `(value & 255)` to fit into the range 0–255.</span> <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>]); <span class="hljs-comment">// Creates a Buffer containing the UTF-8-encoded bytes for the string 'tést':</span> <span class="hljs-comment">// [0x74, 0xc3, 0xa9, 0x73, 0x74] (in hexadecimal notation)</span> <span class="hljs-comment">// [116, 195, 169, 115, 116] (in decimal notation)</span> <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>); <span class="hljs-comment">// Creates a Buffer containing the Latin-1 bytes [0x74, 0xe9, 0x73, 0x74].</span> <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>); <span class="hljs-comment">// Creates a zero-filled Buffer of length 10.</span> <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>); <span class="hljs-comment">// Creates a Buffer of length 10,</span> <span class="hljs-comment">// filled with bytes which all have the value `1`.</span> <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>); <span class="hljs-comment">// Creates an uninitialized buffer of length 10.</span> <span class="hljs-comment">// This is faster than calling Buffer.alloc() but the returned</span> <span class="hljs-comment">// Buffer instance might contain old data that needs to be</span> <span class="hljs-comment">// overwritten using fill(), write(), or other functions that fill the Buffer's</span> <span class="hljs-comment">// contents.</span> <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>); <span class="hljs-comment">// Creates a Buffer containing the bytes [1, 2, 3].</span> <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>]); <span class="hljs-comment">// Creates a Buffer containing the bytes [1, 1, 1, 1] – the entries</span> <span class="hljs-comment">// are all truncated using `(value & 255)` to fit into the range 0–255.</span> <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>]); <span class="hljs-comment">// Creates a Buffer containing the UTF-8-encoded bytes for the string 'tést':</span> <span class="hljs-comment">// [0x74, 0xc3, 0xa9, 0x73, 0x74] (in hexadecimal notation)</span> <span class="hljs-comment">// [116, 195, 169, 115, 116] (in decimal notation)</span> <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>); <span class="hljs-comment">// Creates a Buffer containing the Latin-1 bytes [0x74, 0xe9, 0x73, 0x74].</span> <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> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v15.7.0, v14.18.0</td> <td><p>Introduced <code>base64url</code> encoding.</p></td></tr> <tr><td>v6.4.0</td> <td><p>Introduced <code>latin1</code> as an alias for <code>binary</code>.</p></td></tr> <tr><td>v5.0.0</td> <td><p>Removed the deprecated <code>raw</code> and <code>raws</code> encodings.</p></td></tr> </tbody></table> </details> </div> <p>When converting between <code>Buffer</code>s and strings, a character encoding may be specified. If no character encoding is specified, UTF-8 will be used as the default.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); <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>)); <span class="hljs-comment">// Prints: 68656c6c6f20776f726c64</span> <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>)); <span class="hljs-comment">// Prints: aGVsbG8gd29ybGQ=</span> <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>)); <span class="hljs-comment">// Prints: <Buffer 66 68 71 77 68 67 61 64 73></span> <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>)); <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>); <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>); <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>)); <span class="hljs-comment">// Prints: 68656c6c6f20776f726c64</span> <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>)); <span class="hljs-comment">// Prints: aGVsbG8gd29ybGQ=</span> <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>)); <span class="hljs-comment">// Prints: <Buffer 66 68 71 77 68 67 61 64 73></span> <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>)); <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> <p>Node.js buffers accept all case variations of encoding strings that they receive. For example, UTF-8 can be specified as <code>'utf8'</code>, <code>'UTF8'</code>, or <code>'uTf8'</code>.</p> <p>The character encodings currently supported by Node.js are the following:</p> <ul> <li> <p><code>'utf8'</code> (alias: <code>'utf-8'</code>): Multi-byte encoded Unicode characters. Many web pages and other document formats use <a href="https://en.wikipedia.org/wiki/UTF-8">UTF-8</a>. This is the default character encoding. When decoding a <code>Buffer</code> into a string that does not exclusively contain valid UTF-8 data, the Unicode replacement character <code>U+FFFD</code> � will be used to represent those errors.</p> </li> <li> <p><code>'utf16le'</code> (alias: <code>'utf-16le'</code>): Multi-byte encoded Unicode characters. Unlike <code>'utf8'</code>, each character in the string will be encoded using either 2 or 4 bytes. Node.js only supports the <a href="https://en.wikipedia.org/wiki/Endianness">little-endian</a> variant of <a href="https://en.wikipedia.org/wiki/UTF-16">UTF-16</a>.</p> </li> <li> <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 supports the Unicode characters from <code>U+0000</code> to <code>U+00FF</code>. Each character is encoded using a single byte. Characters that do not fit into that range are truncated and will be mapped to characters in that range.</p> </li> </ul> <p>Converting a <code>Buffer</code> into a string using one of the above is referred to as decoding, and converting a string into a <code>Buffer</code> is referred to as encoding.</p> <p>Node.js also supports the following binary-to-text encodings. For binary-to-text encodings, the naming convention is reversed: Converting a <code>Buffer</code> into a string is typically referred to as encoding, and converting a string into a <code>Buffer</code> as decoding.</p> <ul> <li> <p><code>'base64'</code>: <a href="https://en.wikipedia.org/wiki/Base64">Base64</a> encoding. When creating a <code>Buffer</code> from a string, this encoding will also correctly accept "URL and Filename Safe Alphabet" as specified in <a href="https://tools.ietf.org/html/rfc4648#section-5">RFC 4648, Section 5</a>. Whitespace characters such as spaces, tabs, and new lines contained within the base64-encoded string are ignored.</p> </li> <li> <p><code>'base64url'</code>: <a href="https://tools.ietf.org/html/rfc4648#section-5">base64url</a> encoding as specified in <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 encoding will also correctly accept regular base64-encoded strings. When encoding a <code>Buffer</code> to a string, this encoding will omit padding.</p> </li> <li> <p><code>'hex'</code>: Encode each byte as two hexadecimal characters. Data truncation may occur when decoding strings that do not exclusively consist of an even number of hexadecimal characters. See below for an example.</p> </li> </ul> <p>The following legacy character encodings are also supported:</p> <ul> <li> <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 <code>Buffer</code>, this is equivalent to using <code>'latin1'</code>. When decoding a <code>Buffer</code> into a string, using this encoding will additionally unset the highest bit of each byte before decoding as <code>'latin1'</code>. Generally, there should be no reason to use this encoding, as <code>'utf8'</code> (or, if the data is known to always be ASCII-only, <code>'latin1'</code>) will be a better choice when encoding or decoding ASCII-only text. It is only provided for legacy compatibility.</p> </li> <li> <p><code>'binary'</code>: Alias for <code>'latin1'</code>. The name of this encoding can be very misleading, as all of the encodings listed here convert between strings and binary data. For converting between strings and <code>Buffer</code>s, typically <code>'utf8'</code> is the right choice.</p> </li> <li> <p><code>'ucs2'</code>, <code>'ucs-2'</code>: Aliases of <code>'utf16le'</code>. UCS-2 used to refer to a variant of UTF-16 that did not support characters that had code points larger than U+FFFF. In Node.js, these code points are always supported.</p> </li> </ul> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); <span class="hljs-comment">// Prints <Buffer 1a>, data truncated when first non-hexadecimal value</span> <span class="hljs-comment">// ('g') encountered.</span> <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>); <span class="hljs-comment">// Prints <Buffer 1a>, data truncated when data ends in single digit ('7').</span> <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>); <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>); <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>); <span class="hljs-comment">// Prints <Buffer 1a>, data truncated when first non-hexadecimal value</span> <span class="hljs-comment">// ('g') encountered.</span> <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>); <span class="hljs-comment">// Prints <Buffer 1a>, data truncated when data ends in single digit ('7').</span> <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>); <span class="hljs-comment">// Prints <Buffer 16 34>, all data represented.</span></code><button class="copy-button">copy</button></pre> <p>Modern Web browsers follow the <a href="https://encoding.spec.whatwg.org/">WHATWG Encoding Standard</a> which aliases both <code>'latin1'</code> and <code>'ISO-8859-1'</code> to <code>'win-1252'</code>. This means that while doing something like <code>http.get()</code>, if the returned charset is one of those listed in the WHATWG specification it is possible that the server actually returned <code>'win-1252'</code>-encoded data, and using <code>'latin1'</code> encoding may incorrectly decode the characters.</p> </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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v3.0.0</td> <td><p>The <code>Buffer</code>s class now inherits from <code>Uint8Array</code>.</p></td></tr> </tbody></table> </details> </div> <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> instances. 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, however, subtle incompatibilities between the <code>Buffer</code> API and the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> API.</p> <p>In particular:</p> <ul> <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>, <a href="#bufslicestart-end"><code>Buffer.prototype.slice()</code></a> creates a view over the existing <code>Buffer</code> without copying. This behavior can be surprising, and only exists for legacy compatibility. <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 the behavior of <a href="#bufslicestart-end"><code>Buffer.prototype.slice()</code></a> on both <code>Buffer</code>s and other <code>TypedArray</code>s and should be preferred.</li> <li><a href="#buftostringencoding-start-end"><code>buf.toString()</code></a> is incompatible with its <code>TypedArray</code> equivalent.</li> <li>A number of methods, e.g. <a href="#bufindexofvalue-byteoffset-encoding"><code>buf.indexOf()</code></a>, support additional arguments.</li> </ul> <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> <ul> <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 contents, interpreted as an array of integers, and not as a byte sequence of the target type.</li> </ul> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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-keyword">const</span> uint32array = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint32Array</span>(buf); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(uint32array); <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>); <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-keyword">const</span> uint32array = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint32Array</span>(buf); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(uint32array); <span class="hljs-comment">// Prints: Uint32Array(4) [ 1, 2, 3, 4 ]</span></code><button class="copy-button">copy</button></pre> <ul> <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 <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> </ul> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); <span class="hljs-keyword">const</span> uint16array = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint16Array</span>( buf.<span class="hljs-property">buffer</span>, buf.<span class="hljs-property">byteOffset</span>, buf.<span class="hljs-property">length</span> / <span class="hljs-title class_">Uint16Array</span>.<span class="hljs-property">BYTES_PER_ELEMENT</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(uint16array); <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>); <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>); <span class="hljs-keyword">const</span> uint16array = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint16Array</span>( buf.<span class="hljs-property">buffer</span>, buf.<span class="hljs-property">byteOffset</span>, buf.<span class="hljs-property">length</span> / <span class="hljs-title class_">Uint16Array</span>.<span class="hljs-property">BYTES_PER_ELEMENT</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(uint16array); <span class="hljs-comment">// Prints: Uint16Array(5) [ 104, 101, 108, 108, 111 ]</span></code><button class="copy-button">copy</button></pre> <p>It is possible to create a new <code>Buffer</code> that shares the same allocated memory 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 <code>.buffer</code> property in the same way. <a href="#static-method-bufferfromarraybuffer-byteoffset-length"><code>Buffer.from()</code></a> behaves like <code>new Uint8Array()</code> in this context.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); arr[<span class="hljs-number">0</span>] = <span class="hljs-number">5000</span>; arr[<span class="hljs-number">1</span>] = <span class="hljs-number">4000</span>; <span class="hljs-comment">// Copies the contents of `arr`.</span> <span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arr); <span class="hljs-comment">// Shares memory with `arr`.</span> <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>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); <span class="hljs-comment">// Prints: <Buffer 88 a0></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2); <span class="hljs-comment">// Prints: <Buffer 88 13 a0 0f></span> arr[<span class="hljs-number">1</span>] = <span class="hljs-number">6000</span>; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); <span class="hljs-comment">// Prints: <Buffer 88 a0></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2); <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>); <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>); arr[<span class="hljs-number">0</span>] = <span class="hljs-number">5000</span>; arr[<span class="hljs-number">1</span>] = <span class="hljs-number">4000</span>; <span class="hljs-comment">// Copies the contents of `arr`.</span> <span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arr); <span class="hljs-comment">// Shares memory with `arr`.</span> <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>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); <span class="hljs-comment">// Prints: <Buffer 88 a0></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2); <span class="hljs-comment">// Prints: <Buffer 88 13 a0 0f></span> arr[<span class="hljs-number">1</span>] = <span class="hljs-number">6000</span>; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); <span class="hljs-comment">// Prints: <Buffer 88 a0></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2); <span class="hljs-comment">// Prints: <Buffer 88 13 70 17></span></code><button class="copy-button">copy</button></pre> <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 possible 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 <code>byteOffset</code> and <code>length</code> parameters.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); <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>); <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">// 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>); <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>); <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>); <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">// Prints: 16</span></code><button class="copy-button">copy</button></pre> <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 implementations. 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 argument that is a mapping function that is invoked on every element of the typed array:</p> <ul> <li><code>TypedArray.from(source[, mapFn[, thisArg]])</code></li> </ul> <p>The <code>Buffer.from()</code> method, however, does not support the use of a mapping function:</p> <ul> <li><a href="#static-method-bufferfromarray"><code>Buffer.from(array)</code></a></li> <li><a href="#static-method-bufferfrombuffer"><code>Buffer.from(buffer)</code></a></li> <li><a href="#static-method-bufferfromarraybuffer-byteoffset-length"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a></li> <li><a href="#static-method-bufferfromstring-encoding"><code>Buffer.from(string[, encoding])</code></a></li> </ul> </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> <p><code>Buffer</code> instances can be iterated over using <code>for..of</code> syntax:</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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-keyword">for</span> (<span class="hljs-keyword">const</span> b <span class="hljs-keyword">of</span> buf) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b); } <span class="hljs-comment">// Prints:</span> <span class="hljs-comment">// 1</span> <span class="hljs-comment">// 2</span> <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>); <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-keyword">for</span> (<span class="hljs-keyword">const</span> b <span class="hljs-keyword">of</span> buf) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b); } <span class="hljs-comment">// Prints:</span> <span class="hljs-comment">// 1</span> <span class="hljs-comment">// 2</span> <span class="hljs-comment">// 3</span></code><button class="copy-button">copy</button></pre> <p>Additionally, the <a href="#bufvalues"><code>buf.values()</code></a>, <a href="#bufkeys"><code>buf.keys()</code></a>, and <a href="#bufentries"><code>buf.entries()</code></a> methods can be used to create iterators.</p> </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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0, v16.17.0</td> <td><p>No longer experimental.</p></td></tr> <tr><td>v15.7.0, v14.18.0</td> <td><p><span>Added in: v15.7.0, v14.18.0</span></p></td></tr> </tbody></table> </details> </div> <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 multiple worker threads.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v16.7.0</td> <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> <tr><td>v15.7.0, v14.18.0</td> <td><p><span>Added in: v15.7.0, v14.18.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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 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>, or <a href="buffer.html#class-blob" class="type"><Blob></a> objects, or any mix of such objects, that will be stored within the <code>Blob</code>.</li> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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 to <code>'native'</code>, line endings in string source parts will be converted to the platform native line-ending as specified by <code>require('node:os').EOL</code>.</li> <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 the MIME media type of the data, however no validation of the type format is performed.</li> </ul> </li> </ul> <p>Creates a new <code>Blob</code> object containing a concatenation of the given sources.</p> <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 the 'Blob' and can therefore be safely modified after the 'Blob' is created.</p> <p>String sources are encoded as UTF-8 byte sequences and copied into the Blob. Unmatched surrogate pairs within each string part will be replaced by Unicode U+FFFD replacement characters.</p> <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> <div class="api_metadata"> <span>Added in: v15.7.0, v14.18.0</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <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 the <code>Blob</code> data.</p> <h5><code>blob.bytes()</code><span><a class="mark" href="#blobbytes" id="blobbytes">#</a></span><a aria-hidden="true" class="legacy" id="buffer_blob_bytes"></a></h5> <div class="api_metadata"> <span>Added in: v22.3.0, v20.16.0</span> </div> <p>The <code>blob.bytes()</code> method returns the byte of the <code>Blob</code> object as a <code>Promise<Uint8Array></code>.</p> <pre><code class="language-js"><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'</span>]); blob.<span class="hljs-title function_">bytes</span>().<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">bytes</span>) =></span> { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(bytes); <span class="hljs-comment">// Outputs: Uint8Array(5) [ 104, 101, 108, 108, 111 ]</span> });</code> <button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <span>Added in: v15.7.0, v14.18.0</span> </div> <p>The total size of the <code>Blob</code> in bytes.</p> <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> <div class="api_metadata"> <span>Added in: v15.7.0, v14.18.0</span> </div> <ul> <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> <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> <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> </ul> <p>Creates and returns a new <code>Blob</code> containing a subset of this <code>Blob</code> objects data. The original <code>Blob</code> is not altered.</p> <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> <div class="api_metadata"> <span>Added in: v16.7.0</span> </div> <ul> <li>Returns: <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a></li> </ul> <p>Returns a new <code>ReadableStream</code> that allows the content of the <code>Blob</code> to be read.</p> <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> <div class="api_metadata"> <span>Added in: v15.7.0, v14.18.0</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Returns a promise that fulfills with the contents of the <code>Blob</code> decoded as a UTF-8 string.</p> <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> <div class="api_metadata"> <span>Added in: v15.7.0, v14.18.0</span> </div> <ul> <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>The content-type of the <code>Blob</code>.</p> <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> <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 destinations without transferring or immediately copying the data. The data contained by the <code>Blob</code> is copied only when the <code>arrayBuffer()</code> or <code>text()</code> methods are called.</p> <pre class="with-62-chars"><input class="js-flavor-toggle" 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>; <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>; <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>]); <span class="hljs-keyword">const</span> mc1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); <span class="hljs-keyword">const</span> mc2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); mc1.<span class="hljs-property">port1</span>.<span class="hljs-property">onmessage</span> = <span class="hljs-keyword">async</span> ({ data }) => { <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>()); mc1.<span class="hljs-property">port1</span>.<span class="hljs-title function_">close</span>(); }; mc2.<span class="hljs-property">port1</span>.<span class="hljs-property">onmessage</span> = <span class="hljs-keyword">async</span> ({ data }) => { <span class="hljs-keyword">await</span> <span class="hljs-title function_">delay</span>(<span class="hljs-number">1000</span>); <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>()); mc2.<span class="hljs-property">port1</span>.<span class="hljs-title function_">close</span>(); }; mc1.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(blob); mc2.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(blob); <span class="hljs-comment">// The Blob is still usable after posting.</span> blob.<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>); <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>); <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>]); <span class="hljs-keyword">const</span> mc1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); <span class="hljs-keyword">const</span> mc2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); mc1.<span class="hljs-property">port1</span>.<span class="hljs-property">onmessage</span> = <span class="hljs-keyword">async</span> ({ data }) => { <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>()); mc1.<span class="hljs-property">port1</span>.<span class="hljs-title function_">close</span>(); }; mc2.<span class="hljs-property">port1</span>.<span class="hljs-property">onmessage</span> = <span class="hljs-keyword">async</span> ({ data }) => { <span class="hljs-keyword">await</span> <span class="hljs-title function_">delay</span>(<span class="hljs-number">1000</span>); <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>()); mc2.<span class="hljs-property">port1</span>.<span class="hljs-title function_">close</span>(); }; mc1.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(blob); mc2.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(blob); <span class="hljs-comment">// The Blob is still usable after posting.</span> blob.<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> </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> <p>The <code>Buffer</code> class is a global type for dealing with binary data directly. It can be constructed in a variety of ways.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.0.0</td> <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> <tr><td>v15.0.0</td> <td><p>Throw ERR_INVALID_ARG_VALUE instead of ERR_INVALID_OPT_VALUE for invalid input arguments.</p></td></tr> <tr><td>v10.0.0</td> <td><p>Attempting to fill a non-zero length buffer with a zero length buffer triggers a thrown exception.</p></td></tr> <tr><td>v10.0.0</td> <td><p>Specifying an invalid string for <code>fill</code> triggers a thrown exception.</p></td></tr> <tr><td>v8.9.3</td> <td><p>Specifying an invalid string for <code>fill</code> now results in a zero-filled buffer.</p></td></tr> <tr><td>v5.10.0</td> <td><p><span>Added in: v5.10.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> with. <strong>Default:</strong> <code>0</code>.</li> <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. <strong>Default:</strong> <code>'utf8'</code>.</li> </ul> <p>Allocates a new <code>Buffer</code> of <code>size</code> bytes. If <code>fill</code> is <code>undefined</code>, the <code>Buffer</code> will be zero-filled.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer 00 00 00 00 00></span></code><button class="copy-button">copy</button></pre> <p>If <code>size</code> is larger than <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> is thrown.</p> <p>If <code>fill</code> is specified, the allocated <code>Buffer</code> will be initialized by calling <a href="#buffillvalue-offset-end-encoding"><code>buf.fill(fill)</code></a>.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer 61 61 61 61 61></span></code><button class="copy-button">copy</button></pre> <p>If both <code>fill</code> and <code>encoding</code> are specified, the allocated <code>Buffer</code> will be initialized by calling <a href="#buffillvalue-offset-end-encoding"><code>buf.fill(fill, encoding)</code></a>.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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> <p>Calling <a href="#static-method-bufferallocsize-fill-encoding"><code>Buffer.alloc()</code></a> can be measurably slower than the alternative <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> but ensures that the newly created <code>Buffer</code> instance contents will never contain sensitive data from previous allocations, including data that might not have been allocated for <code>Buffer</code>s.</p> <p>A <code>TypeError</code> will be thrown if <code>size</code> is not a number.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.0.0</td> <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> <tr><td>v15.0.0</td> <td><p>Throw ERR_INVALID_ARG_VALUE instead of ERR_INVALID_OPT_VALUE for invalid input arguments.</p></td></tr> <tr><td>v7.0.0</td> <td><p>Passing a negative <code>size</code> will now throw an error.</p></td></tr> <tr><td>v5.10.0</td> <td><p><span>Added in: v5.10.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> </ul> <p>Allocates a new <code>Buffer</code> of <code>size</code> bytes. If <code>size</code> is larger than <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> is thrown.</p> <p>The underlying memory for <code>Buffer</code> instances created in this way is <em>not initialized</em>. The contents of the newly created <code>Buffer</code> are unknown and <em>may contain sensitive data</em>. Use <a href="#static-method-bufferallocsize-fill-encoding"><code>Buffer.alloc()</code></a> instead to initialize <code>Buffer</code> instances with zeroes.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints (contents may vary): <Buffer a0 8b 28 3f 01 00 00 00 50 32></span> buf.<span class="hljs-title function_">fill</span>(<span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints (contents may vary): <Buffer a0 8b 28 3f 01 00 00 00 50 32></span> buf.<span class="hljs-title function_">fill</span>(<span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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> <p>A <code>TypeError</code> will be thrown if <code>size</code> is not a number.</p> <p>The <code>Buffer</code> module pre-allocates an internal <code>Buffer</code> instance of size <a href="#class-property-bufferpoolsize"><code>Buffer.poolSize</code></a> that is used as a pool for the fast allocation of new <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>, <a href="#static-method-bufferfromstring-encoding"><code>Buffer.from(string)</code></a>, and <a href="#static-method-bufferconcatlist-totallength"><code>Buffer.concat()</code></a> only when <code>size</code> is less than <code>Buffer.poolSize >>> 1</code> (floor of <a href="#class-property-bufferpoolsize"><code>Buffer.poolSize</code></a> divided by two).</p> <p>Use of this pre-allocated internal memory pool is a key difference between calling <code>Buffer.alloc(size, fill)</code> vs. <code>Buffer.allocUnsafe(size).fill(fill)</code>. Specifically, <code>Buffer.alloc(size, fill)</code> will <em>never</em> use the internal <code>Buffer</code> pool, while <code>Buffer.allocUnsafe(size).fill(fill)</code> <em>will</em> use the internal <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 difference is subtle but can be important when an application requires the additional performance that <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> provides.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.0.0</td> <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> <tr><td>v15.0.0</td> <td><p>Throw ERR_INVALID_ARG_VALUE instead of ERR_INVALID_OPT_VALUE for invalid input arguments.</p></td></tr> <tr><td>v5.12.0</td> <td><p><span>Added in: v5.12.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> </ul> <p>Allocates a new <code>Buffer</code> of <code>size</code> bytes. If <code>size</code> is larger than <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> is thrown. A zero-length <code>Buffer</code> is created if <code>size</code> is 0.</p> <p>The underlying memory for <code>Buffer</code> instances created in this way is <em>not initialized</em>. The contents of the newly created <code>Buffer</code> are unknown and <em>may contain sensitive data</em>. Use <a href="#buffillvalue-offset-end-encoding"><code>buf.fill(0)</code></a> to initialize such <code>Buffer</code> instances with zeroes.</p> <p>When using <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> to allocate new <code>Buffer</code> instances, allocations less than <code>Buffer.poolSize >>> 1</code> (4KiB when default poolSize is used) are sliced from a single pre-allocated <code>Buffer</code>. This allows applications to avoid the garbage collection overhead of creating many individually allocated <code>Buffer</code> instances. This approach improves both performance and memory usage by eliminating the need to track and clean up as many individual <code>ArrayBuffer</code> objects.</p> <p>However, in the case where a developer may need to retain a small chunk of memory from a pool for an indeterminate amount of time, it may be appropriate to create an un-pooled <code>Buffer</code> instance using <code>Buffer.allocUnsafeSlow()</code> and then copying out the relevant bits.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <span class="hljs-comment">// Need to keep around a few small chunks of memory.</span> <span class="hljs-keyword">const</span> store = []; socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { <span class="hljs-keyword">let</span> data; <span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (data = readable.<span class="hljs-title function_">read</span>())) { <span class="hljs-comment">// Allocate for retained data.</span> <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>); <span class="hljs-comment">// Copy the data into the new allocation.</span> 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>); store.<span class="hljs-title function_">push</span>(sb); } });</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>); <span class="hljs-comment">// Need to keep around a few small chunks of memory.</span> <span class="hljs-keyword">const</span> store = []; socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { <span class="hljs-keyword">let</span> data; <span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (data = readable.<span class="hljs-title function_">read</span>())) { <span class="hljs-comment">// Allocate for retained data.</span> <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>); <span class="hljs-comment">// Copy the data into the new allocation.</span> 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>); store.<span class="hljs-title function_">push</span>(sb); } });</code><button class="copy-button">copy</button></pre> <p>A <code>TypeError</code> will be thrown if <code>size</code> is not a number.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.0.0</td> <td><p>Passing invalid input will now throw an error.</p></td></tr> <tr><td>v5.10.0</td> <td><p>The <code>string</code> parameter can now be any <code>TypedArray</code>, <code>DataView</code> or <code>ArrayBuffer</code>.</p></td></tr> <tr><td>v0.1.90</td> <td><p><span>Added in: v0.1.90</span></p></td></tr> </tbody></table> </details> </div> <ul> <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 value to calculate the length of.</li> <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. <strong>Default:</strong> <code>'utf8'</code>.</li> <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> </ul> <p>Returns the byte length of a string when encoded using <code>encoding</code>. This 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 for the encoding that is used to convert the string into bytes.</p> <p>For <code>'base64'</code>, <code>'base64url'</code>, and <code>'hex'</code>, this function assumes valid input. For strings that contain non-base64/hex-encoded data (e.g. whitespace), the return value might be greater than the length of a <code>Buffer</code> created from the string.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <span class="hljs-keyword">const</span> str = <span class="hljs-string">'\u00bd + \u00bc = \u00be'</span>; <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> + <span class="hljs-string">`<span class="hljs-subst">${Buffer.byteLength(str, <span class="hljs-string">'utf8'</span>)}</span> bytes`</span>); <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>); <span class="hljs-keyword">const</span> str = <span class="hljs-string">'\u00bd + \u00bc = \u00be'</span>; <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> + <span class="hljs-string">`<span class="hljs-subst">${Buffer.byteLength(str, <span class="hljs-string">'utf8'</span>)}</span> bytes`</span>); <span class="hljs-comment">// Prints: ½ + ¼ = ¾: 9 characters, 12 bytes</span></code><button class="copy-button">copy</button></pre> <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>/ <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> is returned.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v8.0.0</td> <td><p>The arguments can now be <code>Uint8Array</code>s.</p></td></tr> <tr><td>v0.11.13</td> <td><p><span>Added in: v0.11.13</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <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 comparison. See <a href="#bufcomparetarget-targetstart-targetend-sourcestart-sourceend"><code>buf.compare()</code></a> for details.</li> </ul> <p>Compares <code>buf1</code> to <code>buf2</code>, typically for the purpose of sorting arrays of <code>Buffer</code> instances. This is equivalent to calling <a href="#bufcomparetarget-targetstart-targetend-sourcestart-sourceend"><code>buf1.compare(buf2)</code></a>.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); <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>); <span class="hljs-keyword">const</span> arr = [buf1, buf2]; <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>)); <span class="hljs-comment">// Prints: [ <Buffer 30 31 32 33>, <Buffer 31 32 33 34> ]</span> <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>); <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>); <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>); <span class="hljs-keyword">const</span> arr = [buf1, buf2]; <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>)); <span class="hljs-comment">// Prints: [ <Buffer 30 31 32 33>, <Buffer 31 32 33 34> ]</span> <span class="hljs-comment">// (This result is equal to: [buf2, buf1].)</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v8.0.0</td> <td><p>The elements of <code>list</code> can now be <code>Uint8Array</code>s.</p></td></tr> <tr><td>v0.7.11</td> <td><p><span>Added in: v0.7.11</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> instances to concatenate.</li> <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> when concatenated.</li> <li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> </ul> <p>Returns a new <code>Buffer</code> which is the result of concatenating all the <code>Buffer</code> instances in the <code>list</code> together.</p> <p>If the list has no items, or if the <code>totalLength</code> is 0, then a new zero-length <code>Buffer</code> is returned.</p> <p>If <code>totalLength</code> is not provided, it is calculated from the <code>Buffer</code> instances in <code>list</code> by adding their lengths.</p> <p>If <code>totalLength</code> is provided, it is coerced to an unsigned integer. If the combined length of the <code>Buffer</code>s in <code>list</code> exceeds <code>totalLength</code>, the result is truncated to <code>totalLength</code>.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <span class="hljs-comment">// Create a single `Buffer` from a list of three `Buffer` instances.</span> <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>); <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>); <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>); <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>; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(totalLength); <span class="hljs-comment">// Prints: 42</span> <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); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(bufA); <span class="hljs-comment">// Prints: <Buffer 00 00 00 00 ...></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(bufA.<span class="hljs-property">length</span>); <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>); <span class="hljs-comment">// Create a single `Buffer` from a list of three `Buffer` instances.</span> <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>); <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>); <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>); <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>; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(totalLength); <span class="hljs-comment">// Prints: 42</span> <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); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(bufA); <span class="hljs-comment">// Prints: <Buffer 00 00 00 00 ...></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(bufA.<span class="hljs-property">length</span>); <span class="hljs-comment">// Prints: 42</span></code><button class="copy-button">copy</button></pre> <p><code>Buffer.concat()</code> may also use the internal <code>Buffer</code> pool like <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> does.</p> <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> <div class="api_metadata"> <span>Added in: v19.8.0</span> </div> <ul> <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> <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> <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. <strong>Default:</strong> <code>view.length - offset</code>.</li> </ul> <p>Copies the underlying memory of <code>view</code> into a new <code>Buffer</code>.</p> <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>]); <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>); u16[<span class="hljs-number">1</span>] = <span class="hljs-number">0</span>; <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> <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> <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> <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> <div class="api_metadata"> <span>Added in: v5.10.0</span> </div> <ul> <li><code>array</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer[]></a></li> </ul> <p>Allocates a new <code>Buffer</code> using an <code>array</code> of bytes in the range <code>0</code> – <code>255</code>. Array entries outside that range will be truncated to fit into it.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <span class="hljs-comment">// Creates a new Buffer containing the UTF-8 bytes of the string 'buffer'.</span> <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>); <span class="hljs-comment">// Creates a new Buffer containing the UTF-8 bytes of the string 'buffer'.</span> <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> <p>If <code>array</code> is an <code>Array</code>-like object (that is, one with a <code>length</code> property of type <code>number</code>), it is treated as if it is an array, unless it is a <code>Buffer</code> or a <code>Uint8Array</code>. This means all other <code>TypedArray</code> variants get treated as an <code>Array</code>. To create a <code>Buffer</code> from the bytes backing a <code>TypedArray</code>, use <a href="#static-method-buffercopybytesfromview-offset-length"><code>Buffer.copyBytesFrom()</code></a>.</p> <p>A <code>TypeError</code> will be thrown if <code>array</code> is not an <code>Array</code> or another type appropriate for <code>Buffer.from()</code> variants.</p> <p><code>Buffer.from(array)</code> and <a href="#static-method-bufferfromstring-encoding"><code>Buffer.from(string)</code></a> may also use the internal <code>Buffer</code> pool like <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> does.</p> <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> <div class="api_metadata"> <span>Added in: v5.10.0</span> </div> <ul> <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>, <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 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>.</li> <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> <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. <strong>Default:</strong> <code>arrayBuffer.byteLength - byteOffset</code>.</li> </ul> <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 memory. For example, when passed a reference to 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> instance, the newly created <code>Buffer</code> will share the same allocated 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> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); arr[<span class="hljs-number">0</span>] = <span class="hljs-number">5000</span>; arr[<span class="hljs-number">1</span>] = <span class="hljs-number">4000</span>; <span class="hljs-comment">// Shares memory with `arr`.</span> <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-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer 88 13 a0 0f></span> <span class="hljs-comment">// Changing the original Uint16Array changes the Buffer also.</span> arr[<span class="hljs-number">1</span>] = <span class="hljs-number">6000</span>; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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>); arr[<span class="hljs-number">0</span>] = <span class="hljs-number">5000</span>; arr[<span class="hljs-number">1</span>] = <span class="hljs-number">4000</span>; <span class="hljs-comment">// Shares memory with `arr`.</span> <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-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer 88 13 a0 0f></span> <span class="hljs-comment">// Changing the original Uint16Array changes the Buffer also.</span> arr[<span class="hljs-number">1</span>] = <span class="hljs-number">6000</span>; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer 88 13 70 17></span></code><button class="copy-button">copy</button></pre> <p>The optional <code>byteOffset</code> and <code>length</code> arguments specify a memory range within the <code>arrayBuffer</code> that will be shared by the <code>Buffer</code>.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); <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>); <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">// 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>); <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>); <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>); <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">// Prints: 2</span></code><button class="copy-button">copy</button></pre> <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 <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> variants.</p> <p>It is important to remember that a backing <code>ArrayBuffer</code> can cover a range of memory that extends beyond the bounds of a <code>TypedArray</code> view. A new <code>Buffer</code> created using the <code>buffer</code> property of a <code>TypedArray</code> may extend beyond the range of the <code>TypedArray</code>:</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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> <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> <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> <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>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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> <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> <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> <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>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer 63 64 65 66></span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <span>Added in: v5.10.0</span> </div> <ul> <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 which to copy data.</li> </ul> <p>Copies the passed <code>buffer</code> data onto a new <code>Buffer</code> instance.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); <span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(buf1); buf1[<span class="hljs-number">0</span>] = <span class="hljs-number">0x61</span>; <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-comment">// Prints: auffer</span> <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-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>); <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>); <span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(buf1); buf1[<span class="hljs-number">0</span>] = <span class="hljs-number">0x61</span>; <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-comment">// Prints: auffer</span> <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-comment">// Prints: buffer</span></code><button class="copy-button">copy</button></pre> <p>A <code>TypeError</code> will be thrown if <code>buffer</code> is not a <code>Buffer</code> or another type appropriate for <code>Buffer.from()</code> variants.</p> <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> <div class="api_metadata"> <span>Added in: v8.2.0</span> </div> <ul> <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> <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> <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> </ul> <p>For objects whose <code>valueOf()</code> function returns a value not strictly equal to <code>object</code>, returns <code>Buffer.from(object.valueOf(), offsetOrEncoding, length)</code>.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>)); <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>); <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>)); <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> <p>For objects that support <code>Symbol.toPrimitive</code>, returns <code>Buffer.from(object[Symbol.toPrimitive]('string'), offsetOrEncoding)</code>.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <span class="hljs-keyword">class</span> <span class="hljs-title class_">Foo</span> { [<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">toPrimitive</span>]() { <span class="hljs-keyword">return</span> <span class="hljs-string">'this is a test'</span>; } } <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>); <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>); <span class="hljs-keyword">class</span> <span class="hljs-title class_">Foo</span> { [<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">toPrimitive</span>]() { <span class="hljs-keyword">return</span> <span class="hljs-string">'this is a test'</span>; } } <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>); <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> <p>A <code>TypeError</code> will be thrown if <code>object</code> does not have the mentioned methods or is not of another type appropriate for <code>Buffer.from()</code> variants.</p> <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> <div class="api_metadata"> <span>Added in: v5.10.0</span> </div> <ul> <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> <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> </ul> <p>Creates a new <code>Buffer</code> containing <code>string</code>. The <code>encoding</code> parameter identifies the character encoding to be used when converting <code>string</code> into bytes.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); <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>); <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-comment">// Prints: this is a tést</span> <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-comment">// Prints: this is a tést</span> <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>)); <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>); <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>); <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>); <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-comment">// Prints: this is a tést</span> <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-comment">// Prints: this is a tést</span> <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>)); <span class="hljs-comment">// Prints: this is a tést</span></code><button class="copy-button">copy</button></pre> <p>A <code>TypeError</code> will be thrown if <code>string</code> is not a string or another type appropriate for <code>Buffer.from()</code> variants.</p> <p><a href="#static-method-bufferfromstring-encoding"><code>Buffer.from(string)</code></a> may also use the internal <code>Buffer</code> pool like <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> does.</p> <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> <div class="api_metadata"> <span>Added in: v0.1.101</span> </div> <ul> <li><code>obj</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if <code>obj</code> is a <code>Buffer</code>, <code>false</code> otherwise.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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> <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> <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> <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>([]); <span class="hljs-comment">// false</span> <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>); <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> <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> <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> <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>([]); <span class="hljs-comment">// false</span> <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> <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> <div class="api_metadata"> <span>Added in: v0.9.1</span> </div> <ul> <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> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if <code>encoding</code> is the name of a supported character encoding, or <code>false</code> otherwise.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>)); <span class="hljs-comment">// Prints: true</span> <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>)); <span class="hljs-comment">// Prints: true</span> <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>)); <span class="hljs-comment">// Prints: false</span> <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>)); <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>); <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>)); <span class="hljs-comment">// Prints: true</span> <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>)); <span class="hljs-comment">// Prints: true</span> <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>)); <span class="hljs-comment">// Prints: false</span> <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>)); <span class="hljs-comment">// Prints: false</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <span>Added in: v0.11.3</span> </div> <ul> <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> </ul> <p>This is the size (in bytes) of pre-allocated internal <code>Buffer</code> instances used for pooling. This value may be modified.</p> <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> <ul> <li><code>index</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>The index operator <code>[index]</code> can be used to get and set the octet at position <code>index</code> in <code>buf</code>. The values refer to individual bytes, so the legal value range is between <code>0x00</code> and <code>0xFF</code> (hex) or <code>0</code> and <code>255</code> (decimal).</p> <p>This operator is inherited from <code>Uint8Array</code>, so its behavior on out-of-bounds access is the same as <code>Uint8Array</code>. In other words, <code>buf[index]</code> returns <code>undefined</code> when <code>index</code> is negative or greater or equal to <code>buf.length</code>, and <code>buf[index] = value</code> does not modify the buffer if <code>index</code> is negative or <code>>= buf.length</code>.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <span class="hljs-comment">// Copy an ASCII string into a `Buffer` one byte at a time.</span> <span class="hljs-comment">// (This only works for ASCII-only strings. In general, one should use</span> <span class="hljs-comment">// `Buffer.from()` to perform this conversion.)</span> <span class="hljs-keyword">const</span> str = <span class="hljs-string">'Node.js'</span>; <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>); <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++) { buf[i] = str.<span class="hljs-title function_">charCodeAt</span>(i); } <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>)); <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>); <span class="hljs-comment">// Copy an ASCII string into a `Buffer` one byte at a time.</span> <span class="hljs-comment">// (This only works for ASCII-only strings. In general, one should use</span> <span class="hljs-comment">// `Buffer.from()` to perform this conversion.)</span> <span class="hljs-keyword">const</span> str = <span class="hljs-string">'Node.js'</span>; <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>); <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++) { buf[i] = str.<span class="hljs-title function_">charCodeAt</span>(i); } <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>)); <span class="hljs-comment">// Prints: Node.js</span></code><button class="copy-button">copy</button></pre> <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> <ul> <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> object is created.</li> </ul> <p>This <code>ArrayBuffer</code> is not guaranteed to correspond exactly to the original <code>Buffer</code>. See the notes on <code>buf.byteOffset</code> for details.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); <span class="hljs-keyword">const</span> buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arrayBuffer); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buffer.<span class="hljs-property">buffer</span> === arrayBuffer); <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>); <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>); <span class="hljs-keyword">const</span> buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arrayBuffer); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buffer.<span class="hljs-property">buffer</span> === arrayBuffer); <span class="hljs-comment">// Prints: true</span></code><button class="copy-button">copy</button></pre> <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> <ul> <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> </ul> <p>When setting <code>byteOffset</code> in <code>Buffer.from(ArrayBuffer, byteOffset, length)</code>, or sometimes when allocating a <code>Buffer</code> smaller than <code>Buffer.poolSize</code>, the buffer does not start from a zero offset on the underlying <code>ArrayBuffer</code>.</p> <p>This can cause problems when accessing the underlying <code>ArrayBuffer</code> directly using <code>buf.buffer</code>, as other parts of the <code>ArrayBuffer</code> may be unrelated to the <code>Buffer</code> object itself.</p> <p>A common issue when creating a <code>TypedArray</code> object that shares its memory with a <code>Buffer</code> is that in this case one needs to specify the <code>byteOffset</code> correctly:</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <span class="hljs-comment">// Create a buffer smaller than `Buffer.poolSize`.</span> <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>]); <span class="hljs-comment">// When casting the Node.js Buffer to an Int8Array, use the byteOffset</span> <span class="hljs-comment">// to refer only to the part of `nodeBuffer.buffer` that contains the memory</span> <span class="hljs-comment">// for `nodeBuffer`.</span> <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>); <span class="hljs-comment">// Create a buffer smaller than `Buffer.poolSize`.</span> <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>]); <span class="hljs-comment">// When casting the Node.js Buffer to an Int8Array, use the byteOffset</span> <span class="hljs-comment">// to refer only to the part of `nodeBuffer.buffer` that contains the memory</span> <span class="hljs-comment">// for `nodeBuffer`.</span> <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> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v8.0.0</td> <td><p>The <code>target</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> <tr><td>v5.11.0</td> <td><p>Additional parameters for specifying offsets are supported now.</p></td></tr> <tr><td>v0.11.13</td> <td><p><span>Added in: v0.11.13</span></p></td></tr> </tbody></table> </details> </div> <ul> <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 compare <code>buf</code>.</li> <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 comparison. <strong>Default:</strong> <code>0</code>.</li> <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 (not inclusive). <strong>Default:</strong> <code>target.length</code>.</li> <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. <strong>Default:</strong> <code>0</code>.</li> <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 (not inclusive). <strong>Default:</strong> <a href="#buflength"><code>buf.length</code></a>.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Compares <code>buf</code> with <code>target</code> and returns a number indicating whether <code>buf</code> comes before, after, or is the same as <code>target</code> in sort order. Comparison is based on the actual sequence of bytes in each <code>Buffer</code>.</p> <ul> <li><code>0</code> is returned if <code>target</code> is the same as <code>buf</code></li> <li><code>1</code> is returned if <code>target</code> should come <em>before</em> <code>buf</code> when sorted.</li> <li><code>-1</code> is returned if <code>target</code> should come <em>after</em> <code>buf</code> when sorted.</li> </ul> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); <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>); <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>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf1)); <span class="hljs-comment">// Prints: 0</span> <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-comment">// Prints: -1</span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf3)); <span class="hljs-comment">// Prints: -1</span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">compare</span>(buf1)); <span class="hljs-comment">// Prints: 1</span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">compare</span>(buf3)); <span class="hljs-comment">// Prints: 1</span> <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>)); <span class="hljs-comment">// Prints: [ <Buffer 41 42 43>, <Buffer 41 42 43 44>, <Buffer 42 43 44> ]</span> <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>); <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>); <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>); <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>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf1)); <span class="hljs-comment">// Prints: 0</span> <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-comment">// Prints: -1</span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf3)); <span class="hljs-comment">// Prints: -1</span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">compare</span>(buf1)); <span class="hljs-comment">// Prints: 1</span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">compare</span>(buf3)); <span class="hljs-comment">// Prints: 1</span> <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>)); <span class="hljs-comment">// Prints: [ <Buffer 41 42 43>, <Buffer 41 42 43 44>, <Buffer 42 43 44> ]</span> <span class="hljs-comment">// (This result is equal to: [buf1, buf3, buf2].)</span></code><button class="copy-button">copy</button></pre> <p>The optional <code>targetStart</code>, <code>targetEnd</code>, <code>sourceStart</code>, and <code>sourceEnd</code> arguments can be used to limit the comparison to specific ranges within <code>target</code> and <code>buf</code> respectively.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>]); <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>]); <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>)); <span class="hljs-comment">// Prints: 0</span> <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>)); <span class="hljs-comment">// Prints: -1</span> <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>)); <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>); <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>]); <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>]); <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>)); <span class="hljs-comment">// Prints: 0</span> <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>)); <span class="hljs-comment">// Prints: -1</span> <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>)); <span class="hljs-comment">// Prints: 1</span></code><button class="copy-button">copy</button></pre> <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>, <code>targetEnd > target.byteLength</code>, or <code>sourceEnd > source.byteLength</code>.</p> <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> <div class="api_metadata"> <span>Added in: v0.1.90</span> </div> <ul> <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> <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 writing. <strong>Default:</strong> <code>0</code>.</li> <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. <strong>Default:</strong> <code>0</code>.</li> <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 inclusive). <strong>Default:</strong> <a href="#buflength"><code>buf.length</code></a>.</li> <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> </ul> <p>Copies data from a region of <code>buf</code> to a region in <code>target</code>, even if the <code>target</code> memory region overlaps with <code>buf</code>.</p> <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 for all TypedArrays, including Node.js <code>Buffer</code>s, although it takes different function arguments.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <span class="hljs-comment">// Create two `Buffer` instances.</span> <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>); <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>); <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++) { <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> buf1[i] = i + <span class="hljs-number">97</span>; } <span class="hljs-comment">// Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`.</span> buf1.<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>); <span class="hljs-comment">// This is equivalent to:</span> <span class="hljs-comment">// buf2.set(buf1.subarray(16, 20), 8);</span> <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>)); <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>); <span class="hljs-comment">// Create two `Buffer` instances.</span> <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>); <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>); <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++) { <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> buf1[i] = i + <span class="hljs-number">97</span>; } <span class="hljs-comment">// Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`.</span> buf1.<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>); <span class="hljs-comment">// This is equivalent to:</span> <span class="hljs-comment">// buf2.set(buf1.subarray(16, 20), 8);</span> <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>)); <span class="hljs-comment">// Prints: !!!!!!!!qrst!!!!!!!!!!!!!</span></code><button class="copy-button">copy</button></pre> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <span class="hljs-comment">// Create a `Buffer` and copy data from one region to an overlapping region</span> <span class="hljs-comment">// within the same `Buffer`.</span> <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>); <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++) { <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> buf[i] = i + <span class="hljs-number">97</span>; } buf.<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>); <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-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>); <span class="hljs-comment">// Create a `Buffer` and copy data from one region to an overlapping region</span> <span class="hljs-comment">// within the same `Buffer`.</span> <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>); <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++) { <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> buf[i] = i + <span class="hljs-number">97</span>; } buf.<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>); <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-comment">// Prints: efghijghijklmnopqrstuvwxyz</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <span>Added in: v1.1.0</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> </ul> <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 of <code>buf</code>.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <span class="hljs-comment">// Log the entire contents of a `Buffer`.</span> <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>); <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>()) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(pair); } <span class="hljs-comment">// Prints:</span> <span class="hljs-comment">// [0, 98]</span> <span class="hljs-comment">// [1, 117]</span> <span class="hljs-comment">// [2, 102]</span> <span class="hljs-comment">// [3, 102]</span> <span class="hljs-comment">// [4, 101]</span> <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>); <span class="hljs-comment">// Log the entire contents of a `Buffer`.</span> <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>); <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>()) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(pair); } <span class="hljs-comment">// Prints:</span> <span class="hljs-comment">// [0, 98]</span> <span class="hljs-comment">// [1, 117]</span> <span class="hljs-comment">// [2, 102]</span> <span class="hljs-comment">// [3, 102]</span> <span class="hljs-comment">// [4, 101]</span> <span class="hljs-comment">// [5, 114]</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v8.0.0</td> <td><p>The arguments can now be <code>Uint8Array</code>s.</p></td></tr> <tr><td>v0.11.13</td> <td><p><span>Added in: v0.11.13</span></p></td></tr> </tbody></table> </details> </div> <ul> <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 compare <code>buf</code>.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if both <code>buf</code> and <code>otherBuffer</code> have exactly the same bytes, <code>false</code> otherwise. Equivalent to <a href="#bufcomparetarget-targetstart-targetend-sourcestart-sourceend"><code>buf.compare(otherBuffer) === 0</code></a>.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); <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>); <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>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">equals</span>(buf2)); <span class="hljs-comment">// Prints: true</span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">equals</span>(buf3)); <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>); <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>); <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>); <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>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">equals</span>(buf2)); <span class="hljs-comment">// Prints: true</span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">equals</span>(buf3)); <span class="hljs-comment">// Prints: false</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v11.0.0</td> <td><p>Throws <code>ERR_OUT_OF_RANGE</code> instead of <code>ERR_INDEX_OUT_OF_RANGE</code>.</p></td></tr> <tr><td>v10.0.0</td> <td><p>Negative <code>end</code> values throw an <code>ERR_INDEX_OUT_OF_RANGE</code> error.</p></td></tr> <tr><td>v10.0.0</td> <td><p>Attempting to fill a non-zero length buffer with a zero length buffer triggers a thrown exception.</p></td></tr> <tr><td>v10.0.0</td> <td><p>Specifying an invalid string for <code>value</code> triggers a thrown exception.</p></td></tr> <tr><td>v5.7.0</td> <td><p>The <code>encoding</code> parameter is supported now.</p></td></tr> <tr><td>v0.5.0</td> <td><p><span>Added in: v0.5.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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>. Empty value (string, Uint8Array, Buffer) is coerced to <code>0</code>.</li> <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>. <strong>Default:</strong> <code>0</code>.</li> <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> <a href="#buflength"><code>buf.length</code></a>.</li> <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. <strong>Default:</strong> <code>'utf8'</code>.</li> <li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> A reference to <code>buf</code>.</li> </ul> <p>Fills <code>buf</code> with the specified <code>value</code>. If the <code>offset</code> and <code>end</code> are not given, the entire <code>buf</code> will be filled:</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <span class="hljs-comment">// Fill a `Buffer` with the ASCII character 'h'.</span> <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>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">toString</span>()); <span class="hljs-comment">// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh</span> <span class="hljs-comment">// Fill a buffer with empty string</span> <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>); <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>)); <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>); <span class="hljs-comment">// Fill a `Buffer` with the ASCII character 'h'.</span> <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>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">toString</span>()); <span class="hljs-comment">// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh</span> <span class="hljs-comment">// Fill a buffer with empty string</span> <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>); <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>)); <span class="hljs-comment">// Prints: <Buffer 00 00 00 00 00></span></code><button class="copy-button">copy</button></pre> <p><code>value</code> is coerced to a <code>uint32</code> value if it is not a string, <code>Buffer</code>, or integer. If the resulting integer is greater than <code>255</code> (decimal), <code>buf</code> will be filled with <code>value & 255</code>.</p> <p>If the final write of a <code>fill()</code> operation falls on a multi-byte character, then only the bytes of that character that fit into <code>buf</code> are written:</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <span class="hljs-comment">// Fill a `Buffer` with character that takes up two bytes in UTF-8.</span> <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>)); <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>); <span class="hljs-comment">// Fill a `Buffer` with character that takes up two bytes in UTF-8.</span> <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>)); <span class="hljs-comment">// Prints: <Buffer c8 a2 c8 a2 c8></span></code><button class="copy-button">copy</button></pre> <p>If <code>value</code> contains invalid characters, it is truncated; if no valid fill data remains, an exception is thrown:</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); <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>)); <span class="hljs-comment">// Prints: <Buffer 61 61 61 61 61></span> <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>)); <span class="hljs-comment">// Prints: <Buffer aa aa aa aa aa></span> <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>)); <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>); <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>); <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>)); <span class="hljs-comment">// Prints: <Buffer 61 61 61 61 61></span> <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>)); <span class="hljs-comment">// Prints: <Buffer aa aa aa aa aa></span> <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>)); <span class="hljs-comment">// Throws an exception.</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <span>Added in: v5.3.0</span> </div> <ul> <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> <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 offset is calculated from the end of <code>buf</code>. <strong>Default:</strong> <code>0</code>.</li> <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. <strong>Default:</strong> <code>'utf8'</code>.</li> <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> </ul> <p>Equivalent to <a href="#bufindexofvalue-byteoffset-encoding"><code>buf.indexOf() !== -1</code></a>.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); <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-comment">// Prints: true</span> <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>)); <span class="hljs-comment">// Prints: true</span> <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>))); <span class="hljs-comment">// Prints: true</span> <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>)); <span class="hljs-comment">// Prints: true (97 is the decimal ASCII value for 'a')</span> <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-comment">// Prints: false</span> <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>))); <span class="hljs-comment">// Prints: true</span> <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>)); <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>); <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>); <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-comment">// Prints: true</span> <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>)); <span class="hljs-comment">// Prints: true</span> <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>))); <span class="hljs-comment">// Prints: true</span> <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>)); <span class="hljs-comment">// Prints: true (97 is the decimal ASCII value for 'a')</span> <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-comment">// Prints: false</span> <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>))); <span class="hljs-comment">// Prints: true</span> <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>)); <span class="hljs-comment">// Prints: false</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v8.0.0</td> <td><p>The <code>value</code> can now be a <code>Uint8Array</code>.</p></td></tr> <tr><td>v5.7.0, v4.4.0</td> <td><p>When <code>encoding</code> is being passed, the <code>byteOffset</code> parameter is no longer required.</p></td></tr> <tr><td>v1.5.0</td> <td><p><span>Added in: v1.5.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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 offset is calculated from the end of <code>buf</code>. <strong>Default:</strong> <code>0</code>.</li> <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 determine the binary representation of the string that will be searched for in <code>buf</code>. <strong>Default:</strong> <code>'utf8'</code>.</li> <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 <code>-1</code> if <code>buf</code> does not contain <code>value</code>.</li> </ul> <p>If <code>value</code> is:</p> <ul> <li>a string, <code>value</code> is interpreted according to the character encoding in <code>encoding</code>.</li> <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. To compare a partial <code>Buffer</code>, use <a href="#bufsubarraystart-end"><code>buf.subarray</code></a>.</li> <li>a number, <code>value</code> will be interpreted as an unsigned 8-bit integer value between <code>0</code> and <code>255</code>.</li> </ul> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); <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>)); <span class="hljs-comment">// Prints: 0</span> <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>)); <span class="hljs-comment">// Prints: 2</span> <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>))); <span class="hljs-comment">// Prints: 8</span> <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>)); <span class="hljs-comment">// Prints: 8 (97 is the decimal ASCII value for 'a')</span> <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-comment">// Prints: -1</span> <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>))); <span class="hljs-comment">// Prints: 8</span> <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>); <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>)); <span class="hljs-comment">// Prints: 4</span> <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>)); <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>); <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>); <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>)); <span class="hljs-comment">// Prints: 0</span> <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>)); <span class="hljs-comment">// Prints: 2</span> <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>))); <span class="hljs-comment">// Prints: 8</span> <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>)); <span class="hljs-comment">// Prints: 8 (97 is the decimal ASCII value for 'a')</span> <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-comment">// Prints: -1</span> <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>))); <span class="hljs-comment">// Prints: 8</span> <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>); <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>)); <span class="hljs-comment">// Prints: 4</span> <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>)); <span class="hljs-comment">// Prints: 6</span></code><button class="copy-button">copy</button></pre> <p>If <code>value</code> is not a string, number, or <code>Buffer</code>, this method will throw a <code>TypeError</code>. If <code>value</code> is a number, it will be coerced to a valid byte value, an integer between 0 and 255.</p> <p>If <code>byteOffset</code> is not a number, it will be coerced to a number. If the result of coercion is <code>NaN</code> or <code>0</code>, then the entire buffer will be searched. This behavior matches <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf"><code>String.prototype.indexOf()</code></a>.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); <span class="hljs-comment">// Passing a value that's a number, but not a valid byte.</span> <span class="hljs-comment">// Prints: 2, equivalent to searching for 99 or 'c'.</span> <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>)); <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>)); <span class="hljs-comment">// Passing a byteOffset that coerces to NaN or 0.</span> <span class="hljs-comment">// Prints: 1, searching the whole buffer.</span> <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>)); <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-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>)); <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>); <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>); <span class="hljs-comment">// Passing a value that's a number, but not a valid byte.</span> <span class="hljs-comment">// Prints: 2, equivalent to searching for 99 or 'c'.</span> <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>)); <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>)); <span class="hljs-comment">// Passing a byteOffset that coerces to NaN or 0.</span> <span class="hljs-comment">// Prints: 1, searching the whole buffer.</span> <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>)); <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-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>)); <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> <p>If <code>value</code> is an empty string or empty <code>Buffer</code> and <code>byteOffset</code> is less than <code>buf.length</code>, <code>byteOffset</code> will be returned. If <code>value</code> is empty and <code>byteOffset</code> is at least <code>buf.length</code>, <code>buf.length</code> will be returned.</p> <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> <div class="api_metadata"> <span>Added in: v1.1.0</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> </ul> <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 (indexes).</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); <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>()) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(key); } <span class="hljs-comment">// Prints:</span> <span class="hljs-comment">// 0</span> <span class="hljs-comment">// 1</span> <span class="hljs-comment">// 2</span> <span class="hljs-comment">// 3</span> <span class="hljs-comment">// 4</span> <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>); <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>); <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>()) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(key); } <span class="hljs-comment">// Prints:</span> <span class="hljs-comment">// 0</span> <span class="hljs-comment">// 1</span> <span class="hljs-comment">// 2</span> <span class="hljs-comment">// 3</span> <span class="hljs-comment">// 4</span> <span class="hljs-comment">// 5</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v8.0.0</td> <td><p>The <code>value</code> can now be a <code>Uint8Array</code>.</p></td></tr> <tr><td>v6.0.0</td> <td><p><span>Added in: v6.0.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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 offset is calculated from the end of <code>buf</code>. <strong>Default:</strong> <code>buf.length - 1</code>.</li> <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 determine the binary representation of the string that will be searched for in <code>buf</code>. <strong>Default:</strong> <code>'utf8'</code>.</li> <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 <code>-1</code> if <code>buf</code> does not contain <code>value</code>.</li> </ul> <p>Identical to <a href="#bufindexofvalue-byteoffset-encoding"><code>buf.indexOf()</code></a>, except the last occurrence of <code>value</code> is found rather than the first occurrence.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); <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>)); <span class="hljs-comment">// Prints: 0</span> <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-comment">// Prints: 17</span> <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>))); <span class="hljs-comment">// Prints: 17</span> <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>)); <span class="hljs-comment">// Prints: 15 (97 is the decimal ASCII value for 'a')</span> <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>))); <span class="hljs-comment">// Prints: -1</span> <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>)); <span class="hljs-comment">// Prints: 5</span> <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>)); <span class="hljs-comment">// Prints: -1</span> <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>); <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>)); <span class="hljs-comment">// Prints: 6</span> <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>)); <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>); <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>); <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>)); <span class="hljs-comment">// Prints: 0</span> <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-comment">// Prints: 17</span> <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>))); <span class="hljs-comment">// Prints: 17</span> <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>)); <span class="hljs-comment">// Prints: 15 (97 is the decimal ASCII value for 'a')</span> <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>))); <span class="hljs-comment">// Prints: -1</span> <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>)); <span class="hljs-comment">// Prints: 5</span> <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>)); <span class="hljs-comment">// Prints: -1</span> <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>); <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>)); <span class="hljs-comment">// Prints: 6</span> <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>)); <span class="hljs-comment">// Prints: 4</span></code><button class="copy-button">copy</button></pre> <p>If <code>value</code> is not a string, number, or <code>Buffer</code>, this method will throw a <code>TypeError</code>. If <code>value</code> is a number, it will be coerced to a valid byte value, an integer between 0 and 255.</p> <p>If <code>byteOffset</code> is not a number, it will be coerced to a number. Any arguments that coerce to <code>NaN</code>, like <code>{}</code> or <code>undefined</code>, will search the whole buffer. This 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> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); <span class="hljs-comment">// Passing a value that's a number, but not a valid byte.</span> <span class="hljs-comment">// Prints: 2, equivalent to searching for 99 or 'c'.</span> <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>)); <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>)); <span class="hljs-comment">// Passing a byteOffset that coerces to NaN.</span> <span class="hljs-comment">// Prints: 1, searching the whole buffer.</span> <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>)); <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-comment">// Passing a byteOffset that coerces to 0.</span> <span class="hljs-comment">// Prints: -1, equivalent to passing 0.</span> <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>)); <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>); <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>); <span class="hljs-comment">// Passing a value that's a number, but not a valid byte.</span> <span class="hljs-comment">// Prints: 2, equivalent to searching for 99 or 'c'.</span> <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>)); <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>)); <span class="hljs-comment">// Passing a byteOffset that coerces to NaN.</span> <span class="hljs-comment">// Prints: 1, searching the whole buffer.</span> <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>)); <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-comment">// Passing a byteOffset that coerces to 0.</span> <span class="hljs-comment">// Prints: -1, equivalent to passing 0.</span> <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>)); <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> <p>If <code>value</code> is an empty string or empty <code>Buffer</code>, <code>byteOffset</code> will be returned.</p> <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> <div class="api_metadata"> <span>Added in: v0.1.90</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Returns the number of bytes in <code>buf</code>.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <span class="hljs-comment">// Create a `Buffer` and write a shorter string to it using UTF-8.</span> <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>); <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">// Prints: 1234</span> buf.<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>); <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">// 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>); <span class="hljs-comment">// Create a `Buffer` and write a shorter string to it using UTF-8.</span> <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>); <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">// Prints: 1234</span> buf.<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>); <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">// Prints: 1234</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <span>Deprecated since: v8.0.0</span> </div> <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> <p>The <code>buf.parent</code> property is a deprecated alias for <code>buf.buffer</code>.</p> <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> <div class="api_metadata"> <span>Added in: v12.0.0, v10.20.0</span> </div> <ul> <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 satisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> </ul> <p>Reads a signed, big-endian 64-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> <p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p> <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> <div class="api_metadata"> <span>Added in: v12.0.0, v10.20.0</span> </div> <ul> <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 satisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> </ul> <p>Reads a signed, little-endian 64-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> <p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.10.0, v12.19.0</td> <td><p>This function is also available as <code>buf.readBigUint64BE()</code>.</p></td></tr> <tr><td>v12.0.0, v10.20.0</td> <td><p><span>Added in: v12.0.0, v10.20.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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 satisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> </ul> <p>Reads an unsigned, big-endian 64-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> <p>This function is also available under the <code>readBigUint64BE</code> alias.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>]); <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>)); <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>); <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>]); <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>)); <span class="hljs-comment">// Prints: 4294967295n</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.10.0, v12.19.0</td> <td><p>This function is also available as <code>buf.readBigUint64LE()</code>.</p></td></tr> <tr><td>v12.0.0, v10.20.0</td> <td><p><span>Added in: v12.0.0, v10.20.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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 satisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> </ul> <p>Reads an unsigned, little-endian 64-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> <p>This function is also available under the <code>readBigUint64LE</code> alias.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>]); <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>)); <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>); <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>]); <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>)); <span class="hljs-comment">// Prints: 18446744069414584320n</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.11.15</td> <td><p><span>Added in: v0.11.15</span></p></td></tr> </tbody></table> </details> </div> <ul> <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 satisfy <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> </ul> <p>Reads a 64-bit, big-endian double from <code>buf</code> at the specified <code>offset</code>.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>]); <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>)); <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>); <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>]); <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>)); <span class="hljs-comment">// Prints: 8.20788039913184e-304</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.11.15</td> <td><p><span>Added in: v0.11.15</span></p></td></tr> </tbody></table> </details> </div> <ul> <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 satisfy <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> </ul> <p>Reads a 64-bit, little-endian double from <code>buf</code> at the specified <code>offset</code>.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>]); <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>)); <span class="hljs-comment">// Prints: 5.447603722011605e-270</span> <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>)); <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>); <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>]); <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>)); <span class="hljs-comment">// Prints: 5.447603722011605e-270</span> <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>)); <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.11.15</td> <td><p><span>Added in: v0.11.15</span></p></td></tr> </tbody></table> </details> </div> <ul> <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 satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> </ul> <p>Reads a 32-bit, big-endian float from <code>buf</code> at the specified <code>offset</code>.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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-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>)); <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>); <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-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>)); <span class="hljs-comment">// Prints: 2.387939260590663e-38</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.11.15</td> <td><p><span>Added in: v0.11.15</span></p></td></tr> </tbody></table> </details> </div> <ul> <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 satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> </ul> <p>Reads a 32-bit, little-endian float from <code>buf</code> at the specified <code>offset</code>.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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-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>)); <span class="hljs-comment">// Prints: 1.539989614439558e-36</span> <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>)); <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>); <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-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>)); <span class="hljs-comment">// Prints: 1.539989614439558e-36</span> <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>)); <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.5.0</td> <td><p><span>Added in: v0.5.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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 satisfy <code>0 <= offset <= buf.length - 1</code>. <strong>Default:</strong> <code>0</code>.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Reads a signed 8-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> <p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>]); <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>)); <span class="hljs-comment">// Prints: -1</span> <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>)); <span class="hljs-comment">// Prints: 5</span> <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>)); <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>); <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>]); <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>)); <span class="hljs-comment">// Prints: -1</span> <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>)); <span class="hljs-comment">// Prints: 5</span> <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>)); <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.5.5</td> <td><p><span>Added in: v0.5.5</span></p></td></tr> </tbody></table> </details> </div> <ul> <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 satisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Reads a signed, big-endian 16-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> <p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>]); <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>)); <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>); <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>]); <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>)); <span class="hljs-comment">// Prints: 5</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.5.5</td> <td><p><span>Added in: v0.5.5</span></p></td></tr> </tbody></table> </details> </div> <ul> <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 satisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Reads a signed, little-endian 16-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> <p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>]); <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>)); <span class="hljs-comment">// Prints: 1280</span> <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>)); <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>); <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>]); <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>)); <span class="hljs-comment">// Prints: 1280</span> <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>)); <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.5.5</td> <td><p><span>Added in: v0.5.5</span></p></td></tr> </tbody></table> </details> </div> <ul> <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 satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Reads a signed, big-endian 32-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> <p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>]); <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>)); <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>); <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>]); <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>)); <span class="hljs-comment">// Prints: 5</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.5.5</td> <td><p><span>Added in: v0.5.5</span></p></td></tr> </tbody></table> </details> </div> <ul> <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 satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Reads a signed, little-endian 32-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> <p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>]); <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>)); <span class="hljs-comment">// Prints: 83886080</span> <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>)); <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>); <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>]); <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>)); <span class="hljs-comment">// Prints: 83886080</span> <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>)); <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <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> <tr><td>v0.11.15</td> <td><p><span>Added in: v0.11.15</span></p></td></tr> </tbody></table> </details> </div> <ul> <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 satisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> <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 <code>0 < byteLength <= 6</code>.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Reads <code>byteLength</code> number of bytes from <code>buf</code> at the specified <code>offset</code> and interprets the result as a big-endian, two's complement signed value supporting up to 48 bits of accuracy.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>]); <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>)); <span class="hljs-comment">// Prints: 1234567890ab</span> <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>)); <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span> <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>)); <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>); <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>]); <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>)); <span class="hljs-comment">// Prints: 1234567890ab</span> <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>)); <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span> <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>)); <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <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> <tr><td>v0.11.15</td> <td><p><span>Added in: v0.11.15</span></p></td></tr> </tbody></table> </details> </div> <ul> <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 satisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> <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 <code>0 < byteLength <= 6</code>.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Reads <code>byteLength</code> number of bytes from <code>buf</code> at the specified <code>offset</code> and interprets the result as a little-endian, two's complement signed value supporting up to 48 bits of accuracy.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>]); <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>)); <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>); <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>]); <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>)); <span class="hljs-comment">// Prints: -546f87a9cbee</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.9.0, v12.19.0</td> <td><p>This function is also available as <code>buf.readUint8()</code>.</p></td></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.5.0</td> <td><p><span>Added in: v0.5.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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 satisfy <code>0 <= offset <= buf.length - 1</code>. <strong>Default:</strong> <code>0</code>.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Reads an unsigned 8-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> <p>This function is also available under the <code>readUint8</code> alias.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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-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>)); <span class="hljs-comment">// Prints: 1</span> <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>)); <span class="hljs-comment">// Prints: 254</span> <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>)); <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>); <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-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>)); <span class="hljs-comment">// Prints: 1</span> <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>)); <span class="hljs-comment">// Prints: 254</span> <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>)); <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.9.0, v12.19.0</td> <td><p>This function is also available as <code>buf.readUint16BE()</code>.</p></td></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.5.5</td> <td><p><span>Added in: v0.5.5</span></p></td></tr> </tbody></table> </details> </div> <ul> <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 satisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Reads an unsigned, big-endian 16-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> <p>This function is also available under the <code>readUint16BE</code> alias.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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-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>)); <span class="hljs-comment">// Prints: 1234</span> <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>)); <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>); <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-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>)); <span class="hljs-comment">// Prints: 1234</span> <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>)); <span class="hljs-comment">// Prints: 3456</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.9.0, v12.19.0</td> <td><p>This function is also available as <code>buf.readUint16LE()</code>.</p></td></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.5.5</td> <td><p><span>Added in: v0.5.5</span></p></td></tr> </tbody></table> </details> </div> <ul> <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 satisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Reads an unsigned, little-endian 16-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> <p>This function is also available under the <code>readUint16LE</code> alias.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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-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>)); <span class="hljs-comment">// Prints: 3412</span> <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>)); <span class="hljs-comment">// Prints: 5634</span> <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>)); <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>); <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-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>)); <span class="hljs-comment">// Prints: 3412</span> <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>)); <span class="hljs-comment">// Prints: 5634</span> <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>)); <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.9.0, v12.19.0</td> <td><p>This function is also available as <code>buf.readUint32BE()</code>.</p></td></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.5.5</td> <td><p><span>Added in: v0.5.5</span></p></td></tr> </tbody></table> </details> </div> <ul> <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 satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Reads an unsigned, big-endian 32-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> <p>This function is also available under the <code>readUint32BE</code> alias.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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-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>)); <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>); <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-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>)); <span class="hljs-comment">// Prints: 12345678</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.9.0, v12.19.0</td> <td><p>This function is also available as <code>buf.readUint32LE()</code>.</p></td></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.5.5</td> <td><p><span>Added in: v0.5.5</span></p></td></tr> </tbody></table> </details> </div> <ul> <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 satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Reads an unsigned, little-endian 32-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> <p>This function is also available under the <code>readUint32LE</code> alias.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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-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>)); <span class="hljs-comment">// Prints: 78563412</span> <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>)); <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>); <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-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>)); <span class="hljs-comment">// Prints: 78563412</span> <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>)); <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.9.0, v12.19.0</td> <td><p>This function is also available as <code>buf.readUintBE()</code>.</p></td></tr> <tr><td>v10.0.0</td> <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> <tr><td>v0.11.15</td> <td><p><span>Added in: v0.11.15</span></p></td></tr> </tbody></table> </details> </div> <ul> <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 satisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> <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 <code>0 < byteLength <= 6</code>.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Reads <code>byteLength</code> number of bytes from <code>buf</code> at the specified <code>offset</code> and interprets the result as an unsigned big-endian integer supporting up to 48 bits of accuracy.</p> <p>This function is also available under the <code>readUintBE</code> alias.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>]); <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>)); <span class="hljs-comment">// Prints: 1234567890ab</span> <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>)); <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>); <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>]); <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>)); <span class="hljs-comment">// Prints: 1234567890ab</span> <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>)); <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.9.0, v12.19.0</td> <td><p>This function is also available as <code>buf.readUintLE()</code>.</p></td></tr> <tr><td>v10.0.0</td> <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> <tr><td>v0.11.15</td> <td><p><span>Added in: v0.11.15</span></p></td></tr> </tbody></table> </details> </div> <ul> <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 satisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> <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 <code>0 < byteLength <= 6</code>.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Reads <code>byteLength</code> number of bytes from <code>buf</code> at the specified <code>offset</code> and interprets the result as an unsigned, little-endian integer supporting up to 48 bits of accuracy.</p> <p>This function is also available under the <code>readUintLE</code> alias.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>]); <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>)); <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>); <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>]); <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>)); <span class="hljs-comment">// Prints: ab9078563412</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <span>Added in: v3.0.0</span> </div> <ul> <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> <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). <strong>Default:</strong> <a href="#buflength"><code>buf.length</code></a>.</li> <li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> </ul> <p>Returns a new <code>Buffer</code> that references the same memory as the original, but offset and cropped by the <code>start</code> and <code>end</code> indexes.</p> <p>Specifying <code>end</code> greater than <a href="#buflength"><code>buf.length</code></a> will return the same result as that of <code>end</code> equal to <a href="#buflength"><code>buf.length</code></a>.</p> <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> <p>Modifying the new <code>Buffer</code> slice will modify the memory in the original <code>Buffer</code> because the allocated memory of the two objects overlap.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <span class="hljs-comment">// Create a `Buffer` with the ASCII alphabet, take a slice, and modify one byte</span> <span class="hljs-comment">// from the original `Buffer`.</span> <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>); <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++) { <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> buf1[i] = i + <span class="hljs-number">97</span>; } <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>); <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>)); <span class="hljs-comment">// Prints: abc</span> buf1[<span class="hljs-number">0</span>] = <span class="hljs-number">33</span>; <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>)); <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>); <span class="hljs-comment">// Create a `Buffer` with the ASCII alphabet, take a slice, and modify one byte</span> <span class="hljs-comment">// from the original `Buffer`.</span> <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>); <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++) { <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> buf1[i] = i + <span class="hljs-number">97</span>; } <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>); <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>)); <span class="hljs-comment">// Prints: abc</span> buf1[<span class="hljs-number">0</span>] = <span class="hljs-number">33</span>; <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>)); <span class="hljs-comment">// Prints: !bc</span></code><button class="copy-button">copy</button></pre> <p>Specifying negative indexes causes the slice to be generated relative to the end of <code>buf</code> rather than the beginning.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); <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>()); <span class="hljs-comment">// Prints: buffe</span> <span class="hljs-comment">// (Equivalent to buf.subarray(0, 5).)</span> <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>()); <span class="hljs-comment">// Prints: buff</span> <span class="hljs-comment">// (Equivalent to buf.subarray(0, 4).)</span> <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>()); <span class="hljs-comment">// Prints: uff</span> <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>); <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>); <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>()); <span class="hljs-comment">// Prints: buffe</span> <span class="hljs-comment">// (Equivalent to buf.subarray(0, 5).)</span> <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>()); <span class="hljs-comment">// Prints: buff</span> <span class="hljs-comment">// (Equivalent to buf.subarray(0, 4).)</span> <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>()); <span class="hljs-comment">// Prints: uff</span> <span class="hljs-comment">// (Equivalent to buf.subarray(1, 4).)</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v17.5.0, v16.15.0</td> <td><p>The buf.slice() method has been deprecated.</p></td></tr> <tr><td>v7.0.0</td> <td><p>All offsets are now coerced to integers before doing any calculations with them.</p></td></tr> <tr><td>v7.1.0, v6.9.2</td> <td><p>Coercing the offsets to integers now handles values outside the 32-bit integer range properly.</p></td></tr> <tr><td>v0.3.0</td> <td><p><span>Added in: v0.3.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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). <strong>Default:</strong> <a href="#buflength"><code>buf.length</code></a>.</li> <li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> </ul> <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> <p>Returns a new <code>Buffer</code> that references the same memory as the original, but offset and cropped by the <code>start</code> and <code>end</code> indexes.</p> <p>This method is not compatible with the <code>Uint8Array.prototype.slice()</code>, which is a superclass of <code>Buffer</code>. To copy the slice, use <code>Uint8Array.prototype.slice()</code>.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); <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); copiedBuf[<span class="hljs-number">0</span>]++; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(copiedBuf.<span class="hljs-title function_">toString</span>()); <span class="hljs-comment">// Prints: cuffer</span> <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-comment">// Prints: buffer</span> <span class="hljs-comment">// With buf.slice(), the original buffer is modified.</span> <span class="hljs-keyword">const</span> notReallyCopiedBuf = buf.<span class="hljs-title function_">slice</span>(); notReallyCopiedBuf[<span class="hljs-number">0</span>]++; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(notReallyCopiedBuf.<span class="hljs-title function_">toString</span>()); <span class="hljs-comment">// Prints: cuffer</span> <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-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>); <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>); <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); copiedBuf[<span class="hljs-number">0</span>]++; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(copiedBuf.<span class="hljs-title function_">toString</span>()); <span class="hljs-comment">// Prints: cuffer</span> <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-comment">// Prints: buffer</span> <span class="hljs-comment">// With buf.slice(), the original buffer is modified.</span> <span class="hljs-keyword">const</span> notReallyCopiedBuf = buf.<span class="hljs-title function_">slice</span>(); notReallyCopiedBuf[<span class="hljs-number">0</span>]++; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(notReallyCopiedBuf.<span class="hljs-title function_">toString</span>()); <span class="hljs-comment">// Prints: cuffer</span> <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-comment">// Also prints: cuffer (!)</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <span>Added in: v5.10.0</span> </div> <ul> <li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> A reference to <code>buf</code>.</li> </ul> <p>Interprets <code>buf</code> as an array of unsigned 16-bit integers and swaps the byte 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> is not a multiple of 2.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>]); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); <span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05 06 07 08></span> buf1.<span class="hljs-title function_">swap16</span>(); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); <span class="hljs-comment">// Prints: <Buffer 02 01 04 03 06 05 08 07></span> <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>]); buf2.<span class="hljs-title function_">swap16</span>(); <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>); <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>]); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); <span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05 06 07 08></span> buf1.<span class="hljs-title function_">swap16</span>(); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); <span class="hljs-comment">// Prints: <Buffer 02 01 04 03 06 05 08 07></span> <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>]); buf2.<span class="hljs-title function_">swap16</span>(); <span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code><button class="copy-button">copy</button></pre> <p>One convenient use of <code>buf.swap16()</code> is to perform a fast in-place conversion between UTF-16 little-endian and UTF-16 big-endian:</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); buf.<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>); <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>); buf.<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> <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> <div class="api_metadata"> <span>Added in: v5.10.0</span> </div> <ul> <li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> A reference to <code>buf</code>.</li> </ul> <p>Interprets <code>buf</code> as an array of unsigned 32-bit integers and swaps the byte 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> is not a multiple of 4.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>]); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); <span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05 06 07 08></span> buf1.<span class="hljs-title function_">swap32</span>(); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); <span class="hljs-comment">// Prints: <Buffer 04 03 02 01 08 07 06 05></span> <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>]); buf2.<span class="hljs-title function_">swap32</span>(); <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>); <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>]); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); <span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05 06 07 08></span> buf1.<span class="hljs-title function_">swap32</span>(); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); <span class="hljs-comment">// Prints: <Buffer 04 03 02 01 08 07 06 05></span> <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>]); buf2.<span class="hljs-title function_">swap32</span>(); <span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <span>Added in: v6.3.0</span> </div> <ul> <li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> A reference to <code>buf</code>.</li> </ul> <p>Interprets <code>buf</code> as an array of 64-bit numbers and swaps byte 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> is not a multiple of 8.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>]); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); <span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05 06 07 08></span> buf1.<span class="hljs-title function_">swap64</span>(); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); <span class="hljs-comment">// Prints: <Buffer 08 07 06 05 04 03 02 01></span> <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>]); buf2.<span class="hljs-title function_">swap64</span>(); <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>); <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>]); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); <span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05 06 07 08></span> buf1.<span class="hljs-title function_">swap64</span>(); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); <span class="hljs-comment">// Prints: <Buffer 08 07 06 05 04 03 02 01></span> <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>]); buf2.<span class="hljs-title function_">swap64</span>(); <span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <span>Added in: v0.9.2</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> </ul> <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 this function when stringifying a <code>Buffer</code> instance.</p> <p><code>Buffer.from()</code> accepts objects in the format returned from this method. In particular, <code>Buffer.from(buf.toJSON())</code> works like <code>Buffer.from(buf)</code>.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>]); <span class="hljs-keyword">const</span> json = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(buf); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(json); <span class="hljs-comment">// Prints: {"type":"Buffer","data":[1,2,3,4,5]}</span> <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> { <span class="hljs-keyword">return</span> value && value.<span class="hljs-property">type</span> === <span class="hljs-string">'Buffer'</span> ? <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(value) : value; }); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(copy); <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>); <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>]); <span class="hljs-keyword">const</span> json = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(buf); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(json); <span class="hljs-comment">// Prints: {"type":"Buffer","data":[1,2,3,4,5]}</span> <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> { <span class="hljs-keyword">return</span> value && value.<span class="hljs-property">type</span> === <span class="hljs-string">'Buffer'</span> ? <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(value) : value; }); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(copy); <span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05></span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <span>Added in: v0.1.90</span> </div> <ul> <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> <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> <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). <strong>Default:</strong> <a href="#buflength"><code>buf.length</code></a>.</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>Decodes <code>buf</code> to a string according to the specified character encoding in <code>encoding</code>. <code>start</code> and <code>end</code> may be passed to decode only a subset of <code>buf</code>.</p> <p>If <code>encoding</code> is <code>'utf8'</code> and a byte sequence in the input is not valid UTF-8, then each invalid byte is replaced with the replacement character <code>U+FFFD</code>.</p> <p>The maximum length of a string instance (in UTF-16 code units) is available as <a href="#bufferconstantsmax_string_length"><code>buffer.constants.MAX_STRING_LENGTH</code></a>.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); <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++) { <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> buf1[i] = i + <span class="hljs-number">97</span>; } <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-comment">// Prints: abcdefghijklmnopqrstuvwxyz</span> <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>)); <span class="hljs-comment">// Prints: abcde</span> <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>); <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>)); <span class="hljs-comment">// Prints: 74c3a97374</span> <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>)); <span class="hljs-comment">// Prints: té</span> <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>)); <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>); <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>); <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++) { <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> buf1[i] = i + <span class="hljs-number">97</span>; } <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-comment">// Prints: abcdefghijklmnopqrstuvwxyz</span> <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>)); <span class="hljs-comment">// Prints: abcde</span> <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>); <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>)); <span class="hljs-comment">// Prints: 74c3a97374</span> <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>)); <span class="hljs-comment">// Prints: té</span> <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>)); <span class="hljs-comment">// Prints: té</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <span>Added in: v1.1.0</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> </ul> <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 called automatically when a <code>Buffer</code> is used in a <code>for..of</code> statement.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); <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>()) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value); } <span class="hljs-comment">// Prints:</span> <span class="hljs-comment">// 98</span> <span class="hljs-comment">// 117</span> <span class="hljs-comment">// 102</span> <span class="hljs-comment">// 102</span> <span class="hljs-comment">// 101</span> <span class="hljs-comment">// 114</span> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> value <span class="hljs-keyword">of</span> buf) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value); } <span class="hljs-comment">// Prints:</span> <span class="hljs-comment">// 98</span> <span class="hljs-comment">// 117</span> <span class="hljs-comment">// 102</span> <span class="hljs-comment">// 102</span> <span class="hljs-comment">// 101</span> <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>); <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>); <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>()) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value); } <span class="hljs-comment">// Prints:</span> <span class="hljs-comment">// 98</span> <span class="hljs-comment">// 117</span> <span class="hljs-comment">// 102</span> <span class="hljs-comment">// 102</span> <span class="hljs-comment">// 101</span> <span class="hljs-comment">// 114</span> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> value <span class="hljs-keyword">of</span> buf) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value); } <span class="hljs-comment">// Prints:</span> <span class="hljs-comment">// 98</span> <span class="hljs-comment">// 117</span> <span class="hljs-comment">// 102</span> <span class="hljs-comment">// 102</span> <span class="hljs-comment">// 101</span> <span class="hljs-comment">// 114</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <span>Added in: v0.1.90</span> </div> <ul> <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> <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>. <strong>Default:</strong> <code>0</code>.</li> <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 exceed <code>buf.length - offset</code>). <strong>Default:</strong> <code>buf.length - offset</code>.</li> <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> <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> </ul> <p>Writes <code>string</code> to <code>buf</code> at <code>offset</code> according to the character encoding in <code>encoding</code>. The <code>length</code> parameter is the number of bytes to write. If <code>buf</code> did not contain enough space to fit the entire string, only part of <code>string</code> will be written. However, partially encoded characters will not be written.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); <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>); <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>); <span class="hljs-comment">// Prints: 12 bytes: ½ + ¼ = ¾</span> <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>); <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>); <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>); <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>); <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>); <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>); <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>); <span class="hljs-comment">// Prints: 12 bytes: ½ + ¼ = ¾</span> <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>); <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>); <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>); <span class="hljs-comment">// Prints: 2 bytes : ab</span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <span>Added in: v12.0.0, v10.20.0</span> </div> <ul> <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> <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 satisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> <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> </ul> <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian.</p> <p><code>value</code> is interpreted and written as a two's complement signed integer.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); buf.<span class="hljs-title function_">writeBigInt64BE</span>(<span class="hljs-number">0x0102030405060708n</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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>); buf.<span class="hljs-title function_">writeBigInt64BE</span>(<span class="hljs-number">0x0102030405060708n</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05 06 07 08></span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <span>Added in: v12.0.0, v10.20.0</span> </div> <ul> <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> <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 satisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> <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> </ul> <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian.</p> <p><code>value</code> is interpreted and written as a two's complement signed integer.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); buf.<span class="hljs-title function_">writeBigInt64LE</span>(<span class="hljs-number">0x0102030405060708n</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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>); buf.<span class="hljs-title function_">writeBigInt64LE</span>(<span class="hljs-number">0x0102030405060708n</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer 08 07 06 05 04 03 02 01></span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.10.0, v12.19.0</td> <td><p>This function is also available as <code>buf.writeBigUint64BE()</code>.</p></td></tr> <tr><td>v12.0.0, v10.20.0</td> <td><p><span>Added in: v12.0.0, v10.20.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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 satisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> <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> </ul> <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian.</p> <p>This function is also available under the <code>writeBigUint64BE</code> alias.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); buf.<span class="hljs-title function_">writeBigUInt64BE</span>(<span class="hljs-number">0xdecafafecacefaden</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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>); buf.<span class="hljs-title function_">writeBigUInt64BE</span>(<span class="hljs-number">0xdecafafecacefaden</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer de ca fa fe ca ce fa de></span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.10.0, v12.19.0</td> <td><p>This function is also available as <code>buf.writeBigUint64LE()</code>.</p></td></tr> <tr><td>v12.0.0, v10.20.0</td> <td><p><span>Added in: v12.0.0, v10.20.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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 satisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> <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> </ul> <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); buf.<span class="hljs-title function_">writeBigUInt64LE</span>(<span class="hljs-number">0xdecafafecacefaden</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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>); buf.<span class="hljs-title function_">writeBigUInt64LE</span>(<span class="hljs-number">0xdecafafecacefaden</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer de fa ce ca fe fa ca de></span></code><button class="copy-button">copy</button></pre> <p>This function is also available under the <code>writeBigUint64LE</code> alias.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.11.15</td> <td><p><span>Added in: v0.11.15</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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 satisfy <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> <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> </ul> <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code> must be a JavaScript number. Behavior is undefined when <code>value</code> is anything other than a JavaScript number.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); buf.<span class="hljs-title function_">writeDoubleBE</span>(<span class="hljs-number">123.456</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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>); buf.<span class="hljs-title function_">writeDoubleBE</span>(<span class="hljs-number">123.456</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer 40 5e dd 2f 1a 9f be 77></span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.11.15</td> <td><p><span>Added in: v0.11.15</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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 satisfy <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> <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> </ul> <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code> must be a JavaScript number. Behavior is undefined when <code>value</code> is anything other than a JavaScript number.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); buf.<span class="hljs-title function_">writeDoubleLE</span>(<span class="hljs-number">123.456</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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>); buf.<span class="hljs-title function_">writeDoubleLE</span>(<span class="hljs-number">123.456</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer 77 be 9f 1a 2f dd 5e 40></span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.11.15</td> <td><p><span>Added in: v0.11.15</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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 satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> <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> </ul> <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. Behavior is undefined when <code>value</code> is anything other than a JavaScript number.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); buf.<span class="hljs-title function_">writeFloatBE</span>(<span class="hljs-number">0xcafebabe</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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>); buf.<span class="hljs-title function_">writeFloatBE</span>(<span class="hljs-number">0xcafebabe</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer 4f 4a fe bb></span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.11.15</td> <td><p><span>Added in: v0.11.15</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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 satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> <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> </ul> <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. Behavior is undefined when <code>value</code> is anything other than a JavaScript number.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); buf.<span class="hljs-title function_">writeFloatLE</span>(<span class="hljs-number">0xcafebabe</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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>); buf.<span class="hljs-title function_">writeFloatLE</span>(<span class="hljs-number">0xcafebabe</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer bb fe 4a 4f></span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.5.0</td> <td><p><span>Added in: v0.5.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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 satisfy <code>0 <= offset <= buf.length - 1</code>. <strong>Default:</strong> <code>0</code>.</li> <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> </ul> <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code>. <code>value</code> must be a valid signed 8-bit integer. Behavior is undefined when <code>value</code> is anything other than a signed 8-bit integer.</p> <p><code>value</code> is interpreted and written as a two's complement signed integer.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); buf.<span class="hljs-title function_">writeInt8</span>(<span class="hljs-number">2</span>, <span class="hljs-number">0</span>); buf.<span class="hljs-title function_">writeInt8</span>(-<span class="hljs-number">2</span>, <span class="hljs-number">1</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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>); buf.<span class="hljs-title function_">writeInt8</span>(<span class="hljs-number">2</span>, <span class="hljs-number">0</span>); buf.<span class="hljs-title function_">writeInt8</span>(-<span class="hljs-number">2</span>, <span class="hljs-number">1</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer 02 fe></span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.5.5</td> <td><p><span>Added in: v0.5.5</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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 satisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> <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> </ul> <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code> must be a valid signed 16-bit integer. Behavior is undefined when <code>value</code> is anything other than a signed 16-bit integer.</p> <p>The <code>value</code> is interpreted and written as a two's complement signed integer.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); buf.<span class="hljs-title function_">writeInt16BE</span>(<span class="hljs-number">0x0102</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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>); buf.<span class="hljs-title function_">writeInt16BE</span>(<span class="hljs-number">0x0102</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer 01 02></span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.5.5</td> <td><p><span>Added in: v0.5.5</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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 satisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> <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> </ul> <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code> must be a valid signed 16-bit integer. Behavior is undefined when <code>value</code> is anything other than a signed 16-bit integer.</p> <p>The <code>value</code> is interpreted and written as a two's complement signed integer.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); buf.<span class="hljs-title function_">writeInt16LE</span>(<span class="hljs-number">0x0304</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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>); buf.<span class="hljs-title function_">writeInt16LE</span>(<span class="hljs-number">0x0304</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer 04 03></span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.5.5</td> <td><p><span>Added in: v0.5.5</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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 satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> <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> </ul> <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code> must be a valid signed 32-bit integer. Behavior is undefined when <code>value</code> is anything other than a signed 32-bit integer.</p> <p>The <code>value</code> is interpreted and written as a two's complement signed integer.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); buf.<span class="hljs-title function_">writeInt32BE</span>(<span class="hljs-number">0x01020304</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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>); buf.<span class="hljs-title function_">writeInt32BE</span>(<span class="hljs-number">0x01020304</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer 01 02 03 04></span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.5.5</td> <td><p><span>Added in: v0.5.5</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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 satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> <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> </ul> <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code> must be a valid signed 32-bit integer. Behavior is undefined when <code>value</code> is anything other than a signed 32-bit integer.</p> <p>The <code>value</code> is interpreted and written as a two's complement signed integer.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); buf.<span class="hljs-title function_">writeInt32LE</span>(<span class="hljs-number">0x05060708</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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>); buf.<span class="hljs-title function_">writeInt32LE</span>(<span class="hljs-number">0x05060708</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer 08 07 06 05></span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <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> <tr><td>v0.11.15</td> <td><p><span>Added in: v0.11.15</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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 satisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> <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 <code>0 < byteLength <= 6</code>.</li> <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> </ul> <p>Writes <code>byteLength</code> bytes of <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. Supports up to 48 bits of accuracy. Behavior is undefined when <code>value</code> is anything other than a signed integer.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); buf.<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>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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>); buf.<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>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer 12 34 56 78 90 ab></span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <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> <tr><td>v0.11.15</td> <td><p><span>Added in: v0.11.15</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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 satisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> <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 <code>0 < byteLength <= 6</code>.</li> <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> </ul> <p>Writes <code>byteLength</code> bytes of <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. Supports up to 48 bits of accuracy. Behavior is undefined when <code>value</code> is anything other than a signed integer.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); buf.<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>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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>); buf.<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>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer ab 90 78 56 34 12></span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.9.0, v12.19.0</td> <td><p>This function is also available as <code>buf.writeUint8()</code>.</p></td></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.5.0</td> <td><p><span>Added in: v0.5.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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 satisfy <code>0 <= offset <= buf.length - 1</code>. <strong>Default:</strong> <code>0</code>.</li> <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> </ul> <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code>. <code>value</code> must be a valid unsigned 8-bit integer. Behavior is undefined when <code>value</code> is anything other than an unsigned 8-bit integer.</p> <p>This function is also available under the <code>writeUint8</code> alias.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x3</span>, <span class="hljs-number">0</span>); buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x4</span>, <span class="hljs-number">1</span>); buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x23</span>, <span class="hljs-number">2</span>); buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x42</span>, <span class="hljs-number">3</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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>); buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x3</span>, <span class="hljs-number">0</span>); buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x4</span>, <span class="hljs-number">1</span>); buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x23</span>, <span class="hljs-number">2</span>); buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x42</span>, <span class="hljs-number">3</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer 03 04 23 42></span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.9.0, v12.19.0</td> <td><p>This function is also available as <code>buf.writeUint16BE()</code>.</p></td></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.5.5</td> <td><p><span>Added in: v0.5.5</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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 satisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> <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> </ul> <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code> must be a valid unsigned 16-bit integer. Behavior is undefined when <code>value</code> is anything other than an unsigned 16-bit integer.</p> <p>This function is also available under the <code>writeUint16BE</code> alias.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); buf.<span class="hljs-title function_">writeUInt16BE</span>(<span class="hljs-number">0xdead</span>, <span class="hljs-number">0</span>); buf.<span class="hljs-title function_">writeUInt16BE</span>(<span class="hljs-number">0xbeef</span>, <span class="hljs-number">2</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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>); buf.<span class="hljs-title function_">writeUInt16BE</span>(<span class="hljs-number">0xdead</span>, <span class="hljs-number">0</span>); buf.<span class="hljs-title function_">writeUInt16BE</span>(<span class="hljs-number">0xbeef</span>, <span class="hljs-number">2</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer de ad be ef></span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.9.0, v12.19.0</td> <td><p>This function is also available as <code>buf.writeUint16LE()</code>.</p></td></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.5.5</td> <td><p><span>Added in: v0.5.5</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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 satisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> <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> </ul> <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code> must be a valid unsigned 16-bit integer. Behavior is undefined when <code>value</code> is anything other than an unsigned 16-bit integer.</p> <p>This function is also available under the <code>writeUint16LE</code> alias.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); buf.<span class="hljs-title function_">writeUInt16LE</span>(<span class="hljs-number">0xdead</span>, <span class="hljs-number">0</span>); buf.<span class="hljs-title function_">writeUInt16LE</span>(<span class="hljs-number">0xbeef</span>, <span class="hljs-number">2</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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>); buf.<span class="hljs-title function_">writeUInt16LE</span>(<span class="hljs-number">0xdead</span>, <span class="hljs-number">0</span>); buf.<span class="hljs-title function_">writeUInt16LE</span>(<span class="hljs-number">0xbeef</span>, <span class="hljs-number">2</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer ad de ef be></span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.9.0, v12.19.0</td> <td><p>This function is also available as <code>buf.writeUint32BE()</code>.</p></td></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.5.5</td> <td><p><span>Added in: v0.5.5</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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 satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> <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> </ul> <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code> must be a valid unsigned 32-bit integer. Behavior is undefined when <code>value</code> is anything other than an unsigned 32-bit integer.</p> <p>This function is also available under the <code>writeUint32BE</code> alias.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); buf.<span class="hljs-title function_">writeUInt32BE</span>(<span class="hljs-number">0xfeedface</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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>); buf.<span class="hljs-title function_">writeUInt32BE</span>(<span class="hljs-number">0xfeedface</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer fe ed fa ce></span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.9.0, v12.19.0</td> <td><p>This function is also available as <code>buf.writeUint32LE()</code>.</p></td></tr> <tr><td>v10.0.0</td> <td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> <tr><td>v0.5.5</td> <td><p><span>Added in: v0.5.5</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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 satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> <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> </ul> <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code> must be a valid unsigned 32-bit integer. Behavior is undefined when <code>value</code> is anything other than an unsigned 32-bit integer.</p> <p>This function is also available under the <code>writeUint32LE</code> alias.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); buf.<span class="hljs-title function_">writeUInt32LE</span>(<span class="hljs-number">0xfeedface</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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>); buf.<span class="hljs-title function_">writeUInt32LE</span>(<span class="hljs-number">0xfeedface</span>, <span class="hljs-number">0</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer ce fa ed fe></span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.9.0, v12.19.0</td> <td><p>This function is also available as <code>buf.writeUintBE()</code>.</p></td></tr> <tr><td>v10.0.0</td> <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> <tr><td>v0.5.5</td> <td><p><span>Added in: v0.5.5</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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 satisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> <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 <code>0 < byteLength <= 6</code>.</li> <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> </ul> <p>Writes <code>byteLength</code> bytes of <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. Supports up to 48 bits of accuracy. Behavior is undefined when <code>value</code> is anything other than an unsigned integer.</p> <p>This function is also available under the <code>writeUintBE</code> alias.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); buf.<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>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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>); buf.<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>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer 12 34 56 78 90 ab></span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.9.0, v12.19.0</td> <td><p>This function is also available as <code>buf.writeUintLE()</code>.</p></td></tr> <tr><td>v10.0.0</td> <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> <tr><td>v0.5.5</td> <td><p><span>Added in: v0.5.5</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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 satisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> <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 <code>0 < byteLength <= 6</code>.</li> <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> </ul> <p>Writes <code>byteLength</code> bytes of <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. Supports up to 48 bits of accuracy. Behavior is undefined when <code>value</code> is anything other than an unsigned integer.</p> <p>This function is also available under the <code>writeUintLE</code> alias.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <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>); buf.<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>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <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>); <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>); buf.<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>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); <span class="hljs-comment">// Prints: <Buffer ab 90 78 56 34 12></span></code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr> <tr><td>v7.2.1</td> <td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr> <tr><td>v7.0.0</td> <td><p>Calling this constructor emits a deprecation warning now.</p></td></tr> <tr><td>v6.0.0</td> <td><p><span>Deprecated since: v6.0.0</span></p></td></tr> </tbody></table> </details> </div> <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> <ul> <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> </ul> <p>See <a href="#static-method-bufferfromarray"><code>Buffer.from(array)</code></a>.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr> <tr><td>v7.2.1</td> <td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr> <tr><td>v7.0.0</td> <td><p>Calling this constructor emits a deprecation warning now.</p></td></tr> <tr><td>v6.0.0</td> <td><p>The <code>byteOffset</code> and <code>length</code> parameters are supported now.</p></td></tr> <tr><td>v6.0.0</td> <td><p><span>Deprecated since: v6.0.0</span></p></td></tr> <tr><td>v3.0.0</td> <td><p><span>Added in: v3.0.0</span></p></td></tr> </tbody></table> </details> </div> <p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#static-method-bufferfromarraybuffer-byteoffset-length"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a> instead.</div><p></p> <ul> <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>, <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> <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> <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. <strong>Default:</strong> <code>arrayBuffer.byteLength - byteOffset</code>.</li> </ul> <p>See <a href="#static-method-bufferfromarraybuffer-byteoffset-length"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a>.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr> <tr><td>v7.2.1</td> <td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr> <tr><td>v7.0.0</td> <td><p>Calling this constructor emits a deprecation warning now.</p></td></tr> <tr><td>v6.0.0</td> <td><p><span>Deprecated since: v6.0.0</span></p></td></tr> </tbody></table> </details> </div> <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> <ul> <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 which to copy data.</li> </ul> <p>See <a href="#static-method-bufferfrombuffer"><code>Buffer.from(buffer)</code></a>.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr> <tr><td>v8.0.0</td> <td><p>The <code>new Buffer(size)</code> will return zero-filled memory by default.</p></td></tr> <tr><td>v7.2.1</td> <td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr> <tr><td>v7.0.0</td> <td><p>Calling this constructor emits a deprecation warning now.</p></td></tr> <tr><td>v6.0.0</td> <td><p><span>Deprecated since: v6.0.0</span></p></td></tr> </tbody></table> </details> </div> <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 <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a>).</div><p></p> <ul> <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> </ul> <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 constructor is equivalent to <a href="#static-method-bufferallocsize-fill-encoding"><code>Buffer.alloc()</code></a>.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr> <tr><td>v7.2.1</td> <td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr> <tr><td>v7.0.0</td> <td><p>Calling this constructor emits a deprecation warning now.</p></td></tr> <tr><td>v6.0.0</td> <td><p><span>Deprecated since: v6.0.0</span></p></td></tr> </tbody></table> </details> </div> <p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#static-method-bufferfromstring-encoding"><code>Buffer.from(string[, encoding])</code></a> instead.</div><p></p> <ul> <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> <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> </ul> <p>See <a href="#static-method-bufferfromstring-encoding"><code>Buffer.from(string[, encoding])</code></a>.</p> </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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.0.0</td> <td><p>No longer experimental.</p></td></tr> <tr><td>v19.2.0, v18.13.0</td> <td><p><span>Added in: v19.2.0, v18.13.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <li>Extends: <a href="buffer.html#class-blob" class="type"><Blob></a></li> </ul> <p>A <a href="https://developer.mozilla.org/en-US/docs/Web/API/File"><code>File</code></a> provides information about files.</p> <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> <div class="api_metadata"> <span>Added in: v19.2.0, v18.13.0</span> </div> <ul> <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> An 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> objects, or any mix of such objects, that will be stored within the <code>File</code>.</li> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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 to <code>'native'</code>, line endings in string source parts will be converted to the platform native line-ending as specified by <code>require('node:os').EOL</code>.</li> <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> <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. <strong>Default:</strong> <code>Date.now()</code>.</li> </ul> </li> </ul> <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> <div class="api_metadata"> <span>Added in: v19.2.0, v18.13.0</span> </div> <ul> <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>The name of the <code>File</code>.</p> <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> <div class="api_metadata"> <span>Added in: v19.2.0, v18.13.0</span> </div> <ul> <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> </ul> <p>The last modified date of the <code>File</code>.</p> </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> <p>While, the <code>Buffer</code> object is available as a global, there are additional <code>Buffer</code>-related APIs that are available only via the <code>node:buffer</code> module accessed using <code>require('node:buffer')</code>.</p> <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> <div class="api_metadata"> <span>Added in: v15.13.0, v14.17.0</span> </div> <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> <ul> <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> </ul> <p>Decodes a string of Base64-encoded data into bytes, and encodes those bytes into a string using Latin-1 (ISO-8859-1).</p> <p>The <code>data</code> may be any JavaScript-value that can be coerced into a string.</p> <p><strong>This function is only provided for compatibility with legacy web platform APIs and should never be used in new code, because they use strings to represent binary data and predate the introduction of typed arrays in JavaScript. For code running using Node.js APIs, converting between base64-encoded strings and binary data should be performed using <code>Buffer.from(str, 'base64')</code> and <code>buf.toString('base64')</code>.</strong></p> <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> <div class="api_metadata"> <span>Added in: v15.13.0, v14.17.0</span> </div> <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> <ul> <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> </ul> <p>Decodes a string into bytes using Latin-1 (ISO-8859), and encodes those bytes into a string using Base64.</p> <p>The <code>data</code> may be any JavaScript-value that can be coerced into a string.</p> <p><strong>This function is only provided for compatibility with legacy web platform APIs and should never be used in new code, because they use strings to represent binary data and predate the introduction of typed arrays in JavaScript. For code running using Node.js APIs, converting between base64-encoded strings and binary data should be performed using <code>Buffer.from(str, 'base64')</code> and <code>buf.toString('base64')</code>.</strong></p> <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> <div class="api_metadata"> <span>Added in: v19.6.0, v18.15.0</span> </div> <ul> <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> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>This function returns <code>true</code> if <code>input</code> contains only valid ASCII-encoded data, including the case in which <code>input</code> is empty.</p> <p>Throws if the <code>input</code> is a detached array buffer.</p> <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> <div class="api_metadata"> <span>Added in: v19.4.0, v18.14.0</span> </div> <ul> <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> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>This function returns <code>true</code> if <code>input</code> contains only valid UTF-8-encoded data, including the case in which <code>input</code> is empty.</p> <p>Throws if the <code>input</code> is a detached array buffer.</p> <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> <div class="api_metadata"> <span>Added in: v0.5.4</span> </div> <ul> <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> </ul> <p>Returns the maximum number of bytes that will be returned when <code>buf.inspect()</code> is called. This can be overridden by user modules. See <a href="util.html#utilinspectobject-options"><code>util.inspect()</code></a> for more details on <code>buf.inspect()</code> behavior.</p> <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> <div class="api_metadata"> <span>Added in: v3.0.0</span> </div> <ul> <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> </ul> <p>An alias for <a href="#bufferconstantsmax_length"><code>buffer.constants.MAX_LENGTH</code></a>.</p> <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> <div class="api_metadata"> <span>Added in: v3.0.0</span> </div> <ul> <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> </ul> <p>An alias for <a href="#bufferconstantsmax_string_length"><code>buffer.constants.MAX_STRING_LENGTH</code></a>.</p> <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> <div class="api_metadata"> <span>Added in: v16.7.0</span> </div> <p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> <ul> <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 <code>URL.createObjectURL()</code>.</li> <li>Returns: <a href="buffer.html#class-blob" class="type"><Blob></a></li> </ul> <p>Resolves a <code>'blob:nodedata:...'</code> an associated <a href="buffer.html#class-blob" class="type"><Blob></a> object registered using a prior call to <code>URL.createObjectURL()</code>.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v8.0.0</td> <td><p>The <code>source</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> <tr><td>v7.1.0</td> <td><p><span>Added in: v7.1.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <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> <li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> </ul> <p>Re-encodes the given <code>Buffer</code> or <code>Uint8Array</code> instance from one character encoding to another. Returns a new <code>Buffer</code> instance.</p> <p>Throws if the <code>fromEnc</code> or <code>toEnc</code> specify invalid character encodings or if conversion from <code>fromEnc</code> to <code>toEnc</code> is not permitted.</p> <p>Encodings supported by <code>buffer.transcode()</code> are: <code>'ascii'</code>, <code>'utf8'</code>, <code>'utf16le'</code>, <code>'ucs2'</code>, <code>'latin1'</code>, and <code>'binary'</code>.</p> <p>The transcoding process will use substitution characters if a given byte sequence cannot be adequately represented in the target encoding. For instance:</p> <pre class="with-53-chars"><input class="js-flavor-toggle" 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>; <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>); <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>)); <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>); <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>); <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>)); <span class="hljs-comment">// Prints: '?'</span></code><button class="copy-button">copy</button></pre> <p>Because the Euro (<code>€</code>) sign is not representable in US-ASCII, it is replaced with <code>?</code> in the transcoded <code>Buffer</code>.</p> <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> <div class="api_metadata"> <span>Deprecated since: v6.0.0</span> </div> <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> <p>See <a href="#static-method-bufferallocunsafeslowsize"><code>Buffer.allocUnsafeSlow()</code></a>. This was never a class in the sense that the constructor always returned a <code>Buffer</code> instance, rather than a <code>SlowBuffer</code> instance.</p> <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> <div class="api_metadata"> <span>Deprecated since: v6.0.0</span> </div> <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> <ul> <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> </ul> <p>See <a href="#static-method-bufferallocunsafeslowsize"><code>Buffer.allocUnsafeSlow()</code></a>.</p> <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> <div class="api_metadata"> <span>Added in: v8.2.0</span> </div> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v15.0.0</td> <td><p>Value is changed to 2<sup>32</sup> on 64-bit architectures.</p></td></tr> <tr><td>v14.0.0</td> <td><p>Value is changed from 2<sup>31</sup> - 1 to 2<sup>32</sup> - 1 on 64-bit architectures.</p></td></tr> <tr><td>v8.2.0</td> <td><p><span>Added in: v8.2.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> </ul> <p>On 32-bit architectures, this value currently is 2<sup>30</sup> - 1 (about 1 GiB).</p> <p>On 64-bit architectures, this value currently is 2<sup>32</sup> (about 4 GiB).</p> <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> <p>This value is also available as <a href="#bufferkmaxlength"><code>buffer.kMaxLength</code></a>.</p> <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> <div class="api_metadata"> <span>Added in: v8.2.0</span> </div> <ul> <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> </ul> <p>Represents the largest <code>length</code> that a <code>string</code> primitive can have, counted in UTF-16 code units.</p> <p>This value may depend on the JS engine that is being used.</p> </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> <p>In versions of Node.js prior to 6.0.0, <code>Buffer</code> instances were created using the <code>Buffer</code> constructor function, which allocates the returned <code>Buffer</code> differently based on what arguments are provided:</p> <ul> <li>Passing a number as the first argument to <code>Buffer()</code> (e.g. <code>new Buffer(10)</code>) allocates a new <code>Buffer</code> object of the specified size. Prior to Node.js 8.0.0, the memory allocated for such <code>Buffer</code> instances is <em>not</em> initialized and <em>can contain sensitive data</em>. Such <code>Buffer</code> instances <em>must</em> be subsequently initialized by using either <a href="#buffillvalue-offset-end-encoding"><code>buf.fill(0)</code></a> or by writing to the entire <code>Buffer</code> before reading data from the <code>Buffer</code>. While this behavior is <em>intentional</em> to improve performance, development experience has demonstrated that a more explicit distinction is required between creating a fast-but-uninitialized <code>Buffer</code> versus creating a slower-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> <li>Passing a string, array, or <code>Buffer</code> as the first argument copies the passed object's data into the <code>Buffer</code>.</li> <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> that shares allocated memory with the given array buffer.</li> </ul> <p>Because the behavior of <code>new Buffer()</code> is different depending on the type of the first argument, security and reliability issues can be inadvertently introduced into applications when argument validation or <code>Buffer</code> initialization is not performed.</p> <p>For example, if an attacker can cause an application to receive a number where a string is expected, the application may call <code>new Buffer(100)</code> instead of <code>new Buffer("100")</code>, leading it to allocate a 100 byte buffer instead of allocating a 3 byte buffer with content <code>"100"</code>. This is commonly possible using JSON API calls. Since JSON distinguishes between numeric and string types, it allows injection of numbers where a naively written application that does not validate its input sufficiently might expect to always receive a string. Before Node.js 8.0.0, the 100 byte buffer might contain arbitrary pre-existing in-memory data, so may be used to expose in-memory secrets to a remote attacker. Since Node.js 8.0.0, exposure of memory cannot occur because the data is zero-filled. However, other attacks are still possible, such as causing very large buffers to be allocated by the server, leading to performance degradation or crashing on memory exhaustion.</p> <p>To make the creation of <code>Buffer</code> instances more reliable and less error-prone, the various forms of the <code>new Buffer()</code> constructor have been <strong>deprecated</strong> and replaced by separate <code>Buffer.from()</code>, <a href="#static-method-bufferallocsize-fill-encoding"><code>Buffer.alloc()</code></a>, and <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> methods.</p> <p><em>Developers should migrate all existing uses of the <code>new Buffer()</code> constructors to one of these new APIs.</em></p> <ul> <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 provided octets.</li> <li><a href="#static-method-bufferfromarraybuffer-byteoffset-length"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a> returns a new <code>Buffer</code> that <em>shares the same allocated memory</em> as the given <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a>.</li> <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 contents of the given <code>Buffer</code>.</li> <li><a href="#static-method-bufferfromstring-encoding"><code>Buffer.from(string[, encoding])</code></a> returns a new <code>Buffer</code> that <em>contains a copy</em> of the provided string.</li> <li><a href="#static-method-bufferallocsize-fill-encoding"><code>Buffer.alloc(size[, fill[, encoding]])</code></a> returns a new initialized <code>Buffer</code> of the specified size. This method is slower than <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe(size)</code></a> but guarantees that newly created <code>Buffer</code> instances never contain old data that is potentially sensitive. A <code>TypeError</code> will be thrown if <code>size</code> is not a number.</li> <li><a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe(size)</code></a> and <a href="#static-method-bufferallocunsafeslowsize"><code>Buffer.allocUnsafeSlow(size)</code></a> each return a new uninitialized <code>Buffer</code> of the specified <code>size</code>. Because the <code>Buffer</code> is uninitialized, the allocated segment of memory might contain old data that is potentially sensitive.</li> </ul> <p><code>Buffer</code> instances returned by <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a>, <a href="#static-method-bufferfromstring-encoding"><code>Buffer.from(string)</code></a>, <a href="#static-method-bufferconcatlist-totallength"><code>Buffer.concat()</code></a> and <a href="#static-method-bufferfromarray"><code>Buffer.from(array)</code></a> <em>may</em> be allocated off a shared internal memory pool if <code>size</code> is less than or equal to half <a href="#class-property-bufferpoolsize"><code>Buffer.poolSize</code></a>. Instances returned by <a href="#static-method-bufferallocunsafeslowsize"><code>Buffer.allocUnsafeSlow()</code></a> <em>never</em> use the shared internal memory pool.</p> <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> <div class="api_metadata"> <span>Added in: v5.10.0</span> </div> <p>Node.js can be started using the <code>--zero-fill-buffers</code> command-line option to cause all newly-allocated <code>Buffer</code> instances to be zero-filled upon creation by default. Without the option, buffers created with <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a>, <a href="#static-method-bufferallocunsafeslowsize"><code>Buffer.allocUnsafeSlow()</code></a>, and <code>new SlowBuffer(size)</code> are not zero-filled. Use of this flag can have a measurable negative impact on performance. Use the <code>--zero-fill-buffers</code> option only when necessary to enforce that newly allocated <code>Buffer</code> instances cannot contain old data that is potentially sensitive.</p> <pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">node --zero-fill-buffers</span> <span class="hljs-meta prompt_">> </span><span class="language-bash">Buffer.allocUnsafe(5);</span> <Buffer 00 00 00 00 00></code> <button class="copy-button">copy</button></pre> <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> <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 segment of allocated memory is <em>uninitialized</em> (it is not zeroed-out). While this design makes the allocation of memory quite fast, the allocated segment of memory might contain old data that is potentially sensitive. Using a <code>Buffer</code> created by <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> without <em>completely</em> overwriting the memory can allow this old data to be leaked when the <code>Buffer</code> memory is read.</p> <p>While there are clear performance advantages to using <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a>, extra care <em>must</em> be taken in order to avoid introducing security vulnerabilities into an application.</p></section> <!-- API END --> </div> </div> </div> </body> </html>