1<!DOCTYPE html> 2<html lang="en"> 3<head> 4 <meta charset="utf-8"> 5 <meta name="viewport" content="width=device-width"> 6 <meta name="nodejs.org:node-version" content="v14.20.1"> 7 <title>Buffer | Node.js v14.20.1 Documentation</title> 8 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 9 <link rel="stylesheet" href="assets/style.css"> 10 <link rel="stylesheet" href="assets/hljs.css"> 11 <link rel="canonical" href="https://nodejs.org/api/buffer.html"> 12</head> 13<body class="alt apidoc" id="api-section-buffer"> 14 <div id="content" class="clearfix"> 15 <div id="column2" class="interior"> 16 <div id="intro" class="interior"> 17 <a href="/" title="Go back to the home page"> 18 Node.js 19 </a> 20 </div> 21 <ul> 22<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 23<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 24</ul> 25<hr class="line"> 26<ul> 27<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 28<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 29<li><a href="buffer.html" class="nav-buffer active">Buffer</a></li> 30<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 31<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 32<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 33<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 34<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 35<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 36<li><a href="console.html" class="nav-console">Console</a></li> 37<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 38<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 39<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 40<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 41<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 42<li><a href="dns.html" class="nav-dns">DNS</a></li> 43<li><a href="domain.html" class="nav-domain">Domain</a></li> 44<li><a href="errors.html" class="nav-errors">Errors</a></li> 45<li><a href="events.html" class="nav-events">Events</a></li> 46<li><a href="fs.html" class="nav-fs">File system</a></li> 47<li><a href="globals.html" class="nav-globals">Globals</a></li> 48<li><a href="http.html" class="nav-http">HTTP</a></li> 49<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 50<li><a href="https.html" class="nav-https">HTTPS</a></li> 51<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 52<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 53<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 54<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 55<li><a href="module.html" class="nav-module">Modules: <code>module</code> API</a></li> 56<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 57<li><a href="net.html" class="nav-net">Net</a></li> 58<li><a href="os.html" class="nav-os">OS</a></li> 59<li><a href="path.html" class="nav-path">Path</a></li> 60<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 61<li><a href="policy.html" class="nav-policy">Policies</a></li> 62<li><a href="process.html" class="nav-process">Process</a></li> 63<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 64<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 65<li><a href="readline.html" class="nav-readline">Readline</a></li> 66<li><a href="repl.html" class="nav-repl">REPL</a></li> 67<li><a href="report.html" class="nav-report">Report</a></li> 68<li><a href="stream.html" class="nav-stream">Stream</a></li> 69<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 70<li><a href="timers.html" class="nav-timers">Timers</a></li> 71<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 72<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 73<li><a href="tty.html" class="nav-tty">TTY</a></li> 74<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 75<li><a href="url.html" class="nav-url">URL</a></li> 76<li><a href="util.html" class="nav-util">Utilities</a></li> 77<li><a href="v8.html" class="nav-v8">V8</a></li> 78<li><a href="vm.html" class="nav-vm">VM</a></li> 79<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 80<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 81<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 82</ul> 83<hr class="line"> 84<ul> 85<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 86</ul> 87 </div> 88 89 <div id="column1" data-id="buffer" class="interior"> 90 <header> 91 <div class="header-container"> 92 <h1>Node.js v14.20.1 documentation</h1> 93 <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 94 <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 95 <path fill="none" d="M0 0h24v24H0z" /> 96 <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"/> 97 <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"/> 98 </svg> 99 <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 100 <path d="M0 0h24v24H0z" fill="none" /> 101 <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"/> 102 </svg> 103 </button> 104 </div> 105 <div id="gtoc"> 106 <ul> 107 <li> 108 <a href="index.html">Index</a> 109 </li> 110 <li> 111 <a href="all.html">View on single page</a> 112 </li> 113 <li> 114 <a href="buffer.json">View as JSON</a> 115 </li> 116 117 <li class="version-picker"> 118 <a href="#">View another version <span>▼</span></a> 119 <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v18.x/api/buffer.html">18.x</a></li> 120<li><a href="https://nodejs.org/docs/latest-v17.x/api/buffer.html">17.x</a></li> 121<li><a href="https://nodejs.org/docs/latest-v16.x/api/buffer.html">16.x <b>LTS</b></a></li> 122<li><a href="https://nodejs.org/docs/latest-v15.x/api/buffer.html">15.x</a></li> 123<li><a href="https://nodejs.org/docs/latest-v14.x/api/buffer.html">14.x <b>LTS</b></a></li> 124<li><a href="https://nodejs.org/docs/latest-v13.x/api/buffer.html">13.x</a></li> 125<li><a href="https://nodejs.org/docs/latest-v12.x/api/buffer.html">12.x <b>LTS</b></a></li> 126<li><a href="https://nodejs.org/docs/latest-v11.x/api/buffer.html">11.x</a></li> 127<li><a href="https://nodejs.org/docs/latest-v10.x/api/buffer.html">10.x</a></li> 128<li><a href="https://nodejs.org/docs/latest-v9.x/api/buffer.html">9.x</a></li> 129<li><a href="https://nodejs.org/docs/latest-v8.x/api/buffer.html">8.x</a></li> 130<li><a href="https://nodejs.org/docs/latest-v7.x/api/buffer.html">7.x</a></li> 131<li><a href="https://nodejs.org/docs/latest-v6.x/api/buffer.html">6.x</a></li> 132<li><a href="https://nodejs.org/docs/latest-v5.x/api/buffer.html">5.x</a></li> 133<li><a href="https://nodejs.org/docs/latest-v4.x/api/buffer.html">4.x</a></li> 134<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/buffer.html">0.12.x</a></li> 135<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/buffer.html">0.10.x</a></li></ol> 136 </li> 137 138 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/buffer.md">Edit on GitHub</a></li> 139 </ul> 140 </div> 141 <hr> 142 </header> 143 144 <details id="toc" open><summary>Table of contents</summary><ul> 145<li><span class="stability_2"><a href="#buffer_buffer">Buffer</a></span> 146<ul> 147<li><a href="#buffer_buffers_and_character_encodings">Buffers and character encodings</a></li> 148<li><a href="#buffer_buffers_and_typedarrays">Buffers and TypedArrays</a></li> 149<li><a href="#buffer_buffers_and_iteration">Buffers and iteration</a></li> 150<li><span class="stability_1"><a href="#buffer_class_blob">Class: <code>Blob</code></a></span> 151<ul> 152<li><a href="#buffer_new_buffer_blob_sources_options"><code>new buffer.Blob([sources[, options]])</code></a></li> 153<li><a href="#buffer_blob_arraybuffer"><code>blob.arrayBuffer()</code></a></li> 154<li><a href="#buffer_blob_size"><code>blob.size</code></a></li> 155<li><a href="#buffer_blob_slice_start_end_type"><code>blob.slice([start, [end, [type]]])</code></a></li> 156<li><a href="#buffer_blob_text"><code>blob.text()</code></a></li> 157<li><a href="#buffer_blob_type"><code>blob.type</code></a></li> 158<li><a href="#buffer_blob_objects_and_messagechannel"><code>Blob</code> objects and <code>MessageChannel</code></a></li> 159</ul> 160</li> 161<li><a href="#buffer_class_buffer">Class: <code>Buffer</code></a> 162<ul> 163<li><a href="#buffer_static_method_buffer_alloc_size_fill_encoding">Static method: <code>Buffer.alloc(size[, fill[, encoding]])</code></a></li> 164<li><a href="#buffer_static_method_buffer_allocunsafe_size">Static method: <code>Buffer.allocUnsafe(size)</code></a></li> 165<li><a href="#buffer_static_method_buffer_allocunsafeslow_size">Static method: <code>Buffer.allocUnsafeSlow(size)</code></a></li> 166<li><a href="#buffer_static_method_buffer_bytelength_string_encoding">Static method: <code>Buffer.byteLength(string[, encoding])</code></a></li> 167<li><a href="#buffer_static_method_buffer_compare_buf1_buf2">Static method: <code>Buffer.compare(buf1, buf2)</code></a></li> 168<li><a href="#buffer_static_method_buffer_concat_list_totallength">Static method: <code>Buffer.concat(list[, totalLength])</code></a></li> 169<li><a href="#buffer_static_method_buffer_from_array">Static method: <code>Buffer.from(array)</code></a></li> 170<li><a href="#buffer_static_method_buffer_from_arraybuffer_byteoffset_length">Static method: <code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a></li> 171<li><a href="#buffer_static_method_buffer_from_buffer">Static method: <code>Buffer.from(buffer)</code></a></li> 172<li><a href="#buffer_static_method_buffer_from_object_offsetorencoding_length">Static method: <code>Buffer.from(object[, offsetOrEncoding[, length]])</code></a></li> 173<li><a href="#buffer_static_method_buffer_from_string_encoding">Static method: <code>Buffer.from(string[, encoding])</code></a></li> 174<li><a href="#buffer_static_method_buffer_isbuffer_obj">Static method: <code>Buffer.isBuffer(obj)</code></a></li> 175<li><a href="#buffer_static_method_buffer_isencoding_encoding">Static method: <code>Buffer.isEncoding(encoding)</code></a></li> 176<li><a href="#buffer_class_property_buffer_poolsize">Class property: <code>Buffer.poolSize</code></a></li> 177<li><a href="#buffer_buf_index"><code>buf[index]</code></a></li> 178<li><a href="#buffer_buf_buffer"><code>buf.buffer</code></a></li> 179<li><a href="#buffer_buf_byteoffset"><code>buf.byteOffset</code></a></li> 180<li><a href="#buffer_buf_compare_target_targetstart_targetend_sourcestart_sourceend"><code>buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])</code></a></li> 181<li><a href="#buffer_buf_copy_target_targetstart_sourcestart_sourceend"><code>buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])</code></a></li> 182<li><a href="#buffer_buf_entries"><code>buf.entries()</code></a></li> 183<li><a href="#buffer_buf_equals_otherbuffer"><code>buf.equals(otherBuffer)</code></a></li> 184<li><a href="#buffer_buf_fill_value_offset_end_encoding"><code>buf.fill(value[, offset[, end]][, encoding])</code></a></li> 185<li><a href="#buffer_buf_includes_value_byteoffset_encoding"><code>buf.includes(value[, byteOffset][, encoding])</code></a></li> 186<li><a href="#buffer_buf_indexof_value_byteoffset_encoding"><code>buf.indexOf(value[, byteOffset][, encoding])</code></a></li> 187<li><a href="#buffer_buf_keys"><code>buf.keys()</code></a></li> 188<li><a href="#buffer_buf_lastindexof_value_byteoffset_encoding"><code>buf.lastIndexOf(value[, byteOffset][, encoding])</code></a></li> 189<li><a href="#buffer_buf_length"><code>buf.length</code></a></li> 190<li><span class="stability_0"><a href="#buffer_buf_parent"><code>buf.parent</code></a></span></li> 191<li><a href="#buffer_buf_readbigint64be_offset"><code>buf.readBigInt64BE([offset])</code></a></li> 192<li><a href="#buffer_buf_readbigint64le_offset"><code>buf.readBigInt64LE([offset])</code></a></li> 193<li><a href="#buffer_buf_readbiguint64be_offset"><code>buf.readBigUInt64BE([offset])</code></a></li> 194<li><a href="#buffer_buf_readbiguint64le_offset"><code>buf.readBigUInt64LE([offset])</code></a></li> 195<li><a href="#buffer_buf_readdoublebe_offset"><code>buf.readDoubleBE([offset])</code></a></li> 196<li><a href="#buffer_buf_readdoublele_offset"><code>buf.readDoubleLE([offset])</code></a></li> 197<li><a href="#buffer_buf_readfloatbe_offset"><code>buf.readFloatBE([offset])</code></a></li> 198<li><a href="#buffer_buf_readfloatle_offset"><code>buf.readFloatLE([offset])</code></a></li> 199<li><a href="#buffer_buf_readint8_offset"><code>buf.readInt8([offset])</code></a></li> 200<li><a href="#buffer_buf_readint16be_offset"><code>buf.readInt16BE([offset])</code></a></li> 201<li><a href="#buffer_buf_readint16le_offset"><code>buf.readInt16LE([offset])</code></a></li> 202<li><a href="#buffer_buf_readint32be_offset"><code>buf.readInt32BE([offset])</code></a></li> 203<li><a href="#buffer_buf_readint32le_offset"><code>buf.readInt32LE([offset])</code></a></li> 204<li><a href="#buffer_buf_readintbe_offset_bytelength"><code>buf.readIntBE(offset, byteLength)</code></a></li> 205<li><a href="#buffer_buf_readintle_offset_bytelength"><code>buf.readIntLE(offset, byteLength)</code></a></li> 206<li><a href="#buffer_buf_readuint8_offset"><code>buf.readUInt8([offset])</code></a></li> 207<li><a href="#buffer_buf_readuint16be_offset"><code>buf.readUInt16BE([offset])</code></a></li> 208<li><a href="#buffer_buf_readuint16le_offset"><code>buf.readUInt16LE([offset])</code></a></li> 209<li><a href="#buffer_buf_readuint32be_offset"><code>buf.readUInt32BE([offset])</code></a></li> 210<li><a href="#buffer_buf_readuint32le_offset"><code>buf.readUInt32LE([offset])</code></a></li> 211<li><a href="#buffer_buf_readuintbe_offset_bytelength"><code>buf.readUIntBE(offset, byteLength)</code></a></li> 212<li><a href="#buffer_buf_readuintle_offset_bytelength"><code>buf.readUIntLE(offset, byteLength)</code></a></li> 213<li><a href="#buffer_buf_subarray_start_end"><code>buf.subarray([start[, end]])</code></a></li> 214<li><a href="#buffer_buf_slice_start_end"><code>buf.slice([start[, end]])</code></a></li> 215<li><a href="#buffer_buf_swap16"><code>buf.swap16()</code></a></li> 216<li><a href="#buffer_buf_swap32"><code>buf.swap32()</code></a></li> 217<li><a href="#buffer_buf_swap64"><code>buf.swap64()</code></a></li> 218<li><a href="#buffer_buf_tojson"><code>buf.toJSON()</code></a></li> 219<li><a href="#buffer_buf_tostring_encoding_start_end"><code>buf.toString([encoding[, start[, end]]])</code></a></li> 220<li><a href="#buffer_buf_values"><code>buf.values()</code></a></li> 221<li><a href="#buffer_buf_write_string_offset_length_encoding"><code>buf.write(string[, offset[, length]][, encoding])</code></a></li> 222<li><a href="#buffer_buf_writebigint64be_value_offset"><code>buf.writeBigInt64BE(value[, offset])</code></a></li> 223<li><a href="#buffer_buf_writebigint64le_value_offset"><code>buf.writeBigInt64LE(value[, offset])</code></a></li> 224<li><a href="#buffer_buf_writebiguint64be_value_offset"><code>buf.writeBigUInt64BE(value[, offset])</code></a></li> 225<li><a href="#buffer_buf_writebiguint64le_value_offset"><code>buf.writeBigUInt64LE(value[, offset])</code></a></li> 226<li><a href="#buffer_buf_writedoublebe_value_offset"><code>buf.writeDoubleBE(value[, offset])</code></a></li> 227<li><a href="#buffer_buf_writedoublele_value_offset"><code>buf.writeDoubleLE(value[, offset])</code></a></li> 228<li><a href="#buffer_buf_writefloatbe_value_offset"><code>buf.writeFloatBE(value[, offset])</code></a></li> 229<li><a href="#buffer_buf_writefloatle_value_offset"><code>buf.writeFloatLE(value[, offset])</code></a></li> 230<li><a href="#buffer_buf_writeint8_value_offset"><code>buf.writeInt8(value[, offset])</code></a></li> 231<li><a href="#buffer_buf_writeint16be_value_offset"><code>buf.writeInt16BE(value[, offset])</code></a></li> 232<li><a href="#buffer_buf_writeint16le_value_offset"><code>buf.writeInt16LE(value[, offset])</code></a></li> 233<li><a href="#buffer_buf_writeint32be_value_offset"><code>buf.writeInt32BE(value[, offset])</code></a></li> 234<li><a href="#buffer_buf_writeint32le_value_offset"><code>buf.writeInt32LE(value[, offset])</code></a></li> 235<li><a href="#buffer_buf_writeintbe_value_offset_bytelength"><code>buf.writeIntBE(value, offset, byteLength)</code></a></li> 236<li><a href="#buffer_buf_writeintle_value_offset_bytelength"><code>buf.writeIntLE(value, offset, byteLength)</code></a></li> 237<li><a href="#buffer_buf_writeuint8_value_offset"><code>buf.writeUInt8(value[, offset])</code></a></li> 238<li><a href="#buffer_buf_writeuint16be_value_offset"><code>buf.writeUInt16BE(value[, offset])</code></a></li> 239<li><a href="#buffer_buf_writeuint16le_value_offset"><code>buf.writeUInt16LE(value[, offset])</code></a></li> 240<li><a href="#buffer_buf_writeuint32be_value_offset"><code>buf.writeUInt32BE(value[, offset])</code></a></li> 241<li><a href="#buffer_buf_writeuint32le_value_offset"><code>buf.writeUInt32LE(value[, offset])</code></a></li> 242<li><a href="#buffer_buf_writeuintbe_value_offset_bytelength"><code>buf.writeUIntBE(value, offset, byteLength)</code></a></li> 243<li><a href="#buffer_buf_writeuintle_value_offset_bytelength"><code>buf.writeUIntLE(value, offset, byteLength)</code></a></li> 244<li><span class="stability_0"><a href="#buffer_new_buffer_array"><code>new Buffer(array)</code></a></span></li> 245<li><span class="stability_0"><a href="#buffer_new_buffer_arraybuffer_byteoffset_length"><code>new Buffer(arrayBuffer[, byteOffset[, length]])</code></a></span></li> 246<li><span class="stability_0"><a href="#buffer_new_buffer_buffer"><code>new Buffer(buffer)</code></a></span></li> 247<li><span class="stability_0"><a href="#buffer_new_buffer_size"><code>new Buffer(size)</code></a></span></li> 248<li><span class="stability_0"><a href="#buffer_new_buffer_string_encoding"><code>new Buffer(string[, encoding])</code></a></span></li> 249</ul> 250</li> 251<li><a href="#buffer_buffer_module_apis"><code>buffer</code> module APIs</a> 252<ul> 253<li><a href="#buffer_buffer_atob_data"><code>buffer.atob(data)</code></a></li> 254<li><a href="#buffer_buffer_btoa_data"><code>buffer.btoa(data)</code></a></li> 255<li><a href="#buffer_buffer_inspect_max_bytes"><code>buffer.INSPECT_MAX_BYTES</code></a></li> 256<li><a href="#buffer_buffer_kmaxlength"><code>buffer.kMaxLength</code></a></li> 257<li><a href="#buffer_buffer_kstringmaxlength"><code>buffer.kStringMaxLength</code></a></li> 258<li><a href="#buffer_buffer_transcode_source_fromenc_toenc"><code>buffer.transcode(source, fromEnc, toEnc)</code></a></li> 259<li><span class="stability_0"><a href="#buffer_class_slowbuffer">Class: <code>SlowBuffer</code></a></span> 260<ul> 261<li><span class="stability_0"><a href="#buffer_new_slowbuffer_size"><code>new SlowBuffer(size)</code></a></span></li> 262</ul> 263</li> 264<li><a href="#buffer_buffer_constants">Buffer constants</a> 265<ul> 266<li><a href="#buffer_buffer_constants_max_length"><code>buffer.constants.MAX_LENGTH</code></a></li> 267<li><a href="#buffer_buffer_constants_max_string_length"><code>buffer.constants.MAX_STRING_LENGTH</code></a></li> 268</ul> 269</li> 270</ul> 271</li> 272<li><a href="#buffer_buffer_from_buffer_alloc_and_buffer_allocunsafe"><code>Buffer.from()</code>, <code>Buffer.alloc()</code>, and <code>Buffer.allocUnsafe()</code></a> 273<ul> 274<li><a href="#buffer_the_zero_fill_buffers_command_line_option">The <code>--zero-fill-buffers</code> command-line option</a></li> 275<li><a href="#buffer_what_makes_buffer_allocunsafe_and_buffer_allocunsafeslow_unsafe">What makes <code>Buffer.allocUnsafe()</code> and <code>Buffer.allocUnsafeSlow()</code> "unsafe"?</a></li> 276</ul> 277</li> 278</ul> 279</li> 280</ul></details> 281 282 <div id="apicontent"> 283 <h2>Buffer<span><a class="mark" href="#buffer_buffer" id="buffer_buffer">#</a></span></h2> 284 285<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p> 286<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v14.20.1/lib/buffer.js">lib/buffer.js</a></p> 287<p><code>Buffer</code> objects are used to represent a fixed-length sequence of bytes. Many 288Node.js APIs support <code>Buffer</code>s.</p> 289<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 290extends it with methods that cover additional use cases. Node.js APIs accept 291plain <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> 292<p>The <code>Buffer</code> class is within the global scope, making it unlikely that one 293would need to ever use <code>require('buffer').Buffer</code>.</p> 294<pre><code class="language-js"><span class="hljs-comment">// Creates a zero-filled Buffer of length 10.</span> 295<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>); 296 297<span class="hljs-comment">// Creates a Buffer of length 10,</span> 298<span class="hljs-comment">// filled with bytes which all have the value `1`.</span> 299<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>); 300 301<span class="hljs-comment">// Creates an uninitialized buffer of length 10.</span> 302<span class="hljs-comment">// This is faster than calling Buffer.alloc() but the returned</span> 303<span class="hljs-comment">// Buffer instance might contain old data that needs to be</span> 304<span class="hljs-comment">// overwritten using fill(), write(), or other functions that fill the Buffer's</span> 305<span class="hljs-comment">// contents.</span> 306<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>); 307 308<span class="hljs-comment">// Creates a Buffer containing the bytes [1, 2, 3].</span> 309<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>]); 310 311<span class="hljs-comment">// Creates a Buffer containing the bytes [1, 1, 1, 1] – the entries</span> 312<span class="hljs-comment">// are all truncated using `(value & 255)` to fit into the range 0–255.</span> 313<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>]); 314 315<span class="hljs-comment">// Creates a Buffer containing the UTF-8-encoded bytes for the string 'tést':</span> 316<span class="hljs-comment">// [0x74, 0xc3, 0xa9, 0x73, 0x74] (in hexadecimal notation)</span> 317<span class="hljs-comment">// [116, 195, 169, 115, 116] (in decimal notation)</span> 318<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>); 319 320<span class="hljs-comment">// Creates a Buffer containing the Latin-1 bytes [0x74, 0xe9, 0x73, 0x74].</span> 321<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></pre> 322<section><h3>Buffers and character encodings<span><a class="mark" href="#buffer_buffers_and_character_encodings" id="buffer_buffers_and_character_encodings">#</a></span></h3> 323<div class="api_metadata"> 324<details class="changelog"><summary>History</summary> 325<table> 326<tbody><tr><th>Version</th><th>Changes</th></tr> 327<tr><td>v14.18.0</td> 328<td><p>Introduced <code>base64url</code> encoding.</p></td></tr> 329<tr><td>v6.4.0</td> 330<td><p>Introduced <code>latin1</code> as an alias for <code>binary</code>.</p></td></tr> 331<tr><td>v5.0.0</td> 332<td><p>Removed the deprecated <code>raw</code> and <code>raws</code> encodings.</p></td></tr> 333</tbody></table> 334</details> 335</div> 336<p>When converting between <code>Buffer</code>s and strings, a character encoding may be 337specified. If no character encoding is specified, UTF-8 will be used as the 338default.</p> 339<pre><code class="language-js"><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>); 340 341<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>)); 342<span class="hljs-comment">// Prints: 68656c6c6f20776f726c64</span> 343<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>)); 344<span class="hljs-comment">// Prints: aGVsbG8gd29ybGQ=</span> 345 346<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>)); 347<span class="hljs-comment">// Prints: <Buffer 66 68 71 77 68 67 61 64 73></span> 348<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>)); 349<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></pre> 350<p>The character encodings currently supported by Node.js are the following:</p> 351<ul> 352<li> 353<p><code>'utf8'</code>: Multi-byte encoded Unicode characters. Many web pages and other 354document formats use <a href="https://en.wikipedia.org/wiki/UTF-8">UTF-8</a>. This is the default character encoding. 355When decoding a <code>Buffer</code> into a string that does not exclusively contain 356valid UTF-8 data, the Unicode replacement character <code>U+FFFD</code> � will be used 357to represent those errors.</p> 358</li> 359<li> 360<p><code>'utf16le'</code>: Multi-byte encoded Unicode characters. Unlike <code>'utf8'</code>, each 361character in the string will be encoded using either 2 or 4 bytes. 362Node.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> 363</li> 364<li> 365<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 366supports the Unicode characters from <code>U+0000</code> to <code>U+00FF</code>. Each character is 367encoded using a single byte. Characters that do not fit into that range are 368truncated and will be mapped to characters in that range.</p> 369</li> 370</ul> 371<p>Converting a <code>Buffer</code> into a string using one of the above is referred to as 372decoding, and converting a string into a <code>Buffer</code> is referred to as encoding.</p> 373<p>Node.js also supports the following binary-to-text encodings. For 374binary-to-text encodings, the naming convention is reversed: Converting a 375<code>Buffer</code> into a string is typically referred to as encoding, and converting a 376string into a <code>Buffer</code> as decoding.</p> 377<ul> 378<li> 379<p><code>'base64'</code>: <a href="https://en.wikipedia.org/wiki/Base64">Base64</a> encoding. When creating a <code>Buffer</code> from a string, 380this encoding will also correctly accept "URL and Filename Safe Alphabet" as 381specified in <a href="https://tools.ietf.org/html/rfc4648#section-5">RFC 4648, Section 5</a>. Whitespace characters such as spaces, 382tabs, and new lines contained within the base64-encoded string are ignored.</p> 383</li> 384<li> 385<p><code>'base64url'</code>: <a href="https://tools.ietf.org/html/rfc4648#section-5">base64url</a> encoding as specified in 386<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 387encoding will also correctly accept regular base64-encoded strings. When 388encoding a <code>Buffer</code> to a string, this encoding will omit padding.</p> 389</li> 390<li> 391<p><code>'hex'</code>: Encode each byte as two hexadecimal characters. Data truncation 392may occur when decoding strings that do exclusively contain valid hexadecimal 393characters. See below for an example.</p> 394</li> 395</ul> 396<p>The following legacy character encodings are also supported:</p> 397<ul> 398<li> 399<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 400<code>Buffer</code>, this is equivalent to using <code>'latin1'</code>. When decoding a <code>Buffer</code> 401into a string, using this encoding will additionally unset the highest bit of 402each byte before decoding as <code>'latin1'</code>. 403Generally, there should be no reason to use this encoding, as <code>'utf8'</code> 404(or, if the data is known to always be ASCII-only, <code>'latin1'</code>) will be a 405better choice when encoding or decoding ASCII-only text. It is only provided 406for legacy compatibility.</p> 407</li> 408<li> 409<p><code>'binary'</code>: Alias for <code>'latin1'</code>. See <a href="https://developer.mozilla.org/en-US/docs/Web/API/DOMString/Binary">binary strings</a> for more background 410on this topic. The name of this encoding can be very misleading, as all of the 411encodings listed here convert between strings and binary data. For converting 412between strings and <code>Buffer</code>s, typically <code>'utf-8'</code> is the right choice.</p> 413</li> 414<li> 415<p><code>'ucs2'</code>: Alias of <code>'utf16le'</code>. UCS-2 used to refer to a variant of UTF-16 416that did not support characters that had code points larger than U+FFFF. 417In Node.js, these code points are always supported.</p> 418</li> 419</ul> 420<pre><code class="language-js"><span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'1ag'</span>, <span class="hljs-string">'hex'</span>); 421<span class="hljs-comment">// Prints <Buffer 1a>, data truncated when first non-hexadecimal value</span> 422<span class="hljs-comment">// ('g') encountered.</span> 423 424<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'1a7g'</span>, <span class="hljs-string">'hex'</span>); 425<span class="hljs-comment">// Prints <Buffer 1a>, data truncated when data ends in single digit ('7').</span> 426 427<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>); 428<span class="hljs-comment">// Prints <Buffer 16 34>, all data represented.</span></code></pre> 429<p>Modern Web browsers follow the <a href="https://encoding.spec.whatwg.org/">WHATWG Encoding Standard</a> which aliases 430both <code>'latin1'</code> and <code>'ISO-8859-1'</code> to <code>'win-1252'</code>. This means that while doing 431something like <code>http.get()</code>, if the returned charset is one of those listed in 432the WHATWG specification it is possible that the server actually returned 433<code>'win-1252'</code>-encoded data, and using <code>'latin1'</code> encoding may incorrectly decode 434the characters.</p> 435</section><section><h3>Buffers and TypedArrays<span><a class="mark" href="#buffer_buffers_and_typedarrays" id="buffer_buffers_and_typedarrays">#</a></span></h3> 436<div class="api_metadata"> 437<details class="changelog"><summary>History</summary> 438<table> 439<tbody><tr><th>Version</th><th>Changes</th></tr> 440<tr><td>v3.0.0</td> 441<td><p>The <code>Buffer</code>s class now inherits from <code>Uint8Array</code>.</p></td></tr> 442</tbody></table> 443</details> 444</div> 445<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> 446instances. 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, 447however, subtle incompatibilities between the <code>Buffer</code> API and the 448<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> API.</p> 449<p>In particular:</p> 450<ul> 451<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>, 452<a href="#buffer_buf_slice_start_end"><code>Buffer.prototype.slice()</code></a> creates a view over the existing <code>Buffer</code> 453without copying. This behavior can be surprising, and only exists for legacy 454compatibility. <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 455the behavior of <a href="#buffer_buf_slice_start_end"><code>Buffer.prototype.slice()</code></a> on both <code>Buffer</code>s 456and other <code>TypedArray</code>s.</li> 457<li><a href="#buffer_buf_tostring_encoding_start_end"><code>buf.toString()</code></a> is incompatible with its <code>TypedArray</code> equivalent.</li> 458<li>A number of methods, e.g. <a href="#buffer_buf_indexof_value_byteoffset_encoding"><code>buf.indexOf()</code></a>, support additional arguments.</li> 459</ul> 460<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> 461<ul> 462<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 463contents, interpreted as an array of integers, and not as a byte sequence 464of the target type.</li> 465</ul> 466<pre><code class="language-js"><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>]); 467<span class="hljs-keyword">const</span> uint32array = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint</span>32<span class="hljs-built_in">Array</span>(buf); 468 469<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(uint32array); 470 471<span class="hljs-comment">// Prints: Uint32Array(4) [ 1, 2, 3, 4 ]</span></code></pre> 472<ul> 473<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 474<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> 475</ul> 476<pre><code class="language-js"><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>); 477<span class="hljs-keyword">const</span> uint16array = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint</span>16<span class="hljs-built_in">Array</span>( 478 buf.<span class="hljs-property">buffer</span>, 479 buf.<span class="hljs-property">byteOffset</span>, 480 buf.<span class="hljs-property">length</span> / <span class="hljs-title class_">Uint</span>16<span class="hljs-built_in">Array</span>.<span class="hljs-property">BYTES_PER_ELEMENT</span>); 481 482<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(uint16array); 483 484<span class="hljs-comment">// Prints: Uint16Array(5) [ 104, 101, 108, 108, 111 ]</span></code></pre> 485<p>It is possible to create a new <code>Buffer</code> that shares the same allocated 486memory 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 487<code>.buffer</code> property in the same way. <a href="#buffer_static_method_buffer_from_arraybuffer_byteoffset_length"><code>Buffer.from()</code></a> 488behaves like <code>new Uint8Array()</code> in this context.</p> 489<pre><code class="language-js"><span class="hljs-keyword">const</span> arr = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint</span>16<span class="hljs-built_in">Array</span>(<span class="hljs-number">2</span>); 490 491arr[<span class="hljs-number">0</span>] = <span class="hljs-number">5000</span>; 492arr[<span class="hljs-number">1</span>] = <span class="hljs-number">4000</span>; 493 494<span class="hljs-comment">// Copies the contents of `arr`.</span> 495<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arr); 496 497<span class="hljs-comment">// Shares memory with `arr`.</span> 498<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>); 499 500<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 501<span class="hljs-comment">// Prints: <Buffer 88 a0></span> 502<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2); 503<span class="hljs-comment">// Prints: <Buffer 88 13 a0 0f></span> 504 505arr[<span class="hljs-number">1</span>] = <span class="hljs-number">6000</span>; 506 507<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 508<span class="hljs-comment">// Prints: <Buffer 88 a0></span> 509<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2); 510<span class="hljs-comment">// Prints: <Buffer 88 13 70 17></span></code></pre> 511<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 512possible 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 513<code>byteOffset</code> and <code>length</code> parameters.</p> 514<pre><code class="language-js"><span class="hljs-keyword">const</span> arr = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint</span>16<span class="hljs-built_in">Array</span>(<span class="hljs-number">20</span>); 515<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>); 516 517<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>); 518<span class="hljs-comment">// Prints: 16</span></code></pre> 519<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 520implementations. 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 521argument that is a mapping function that is invoked on every element of the 522typed array:</p> 523<ul> 524<li><code>TypedArray.from(source[, mapFn[, thisArg]])</code></li> 525</ul> 526<p>The <code>Buffer.from()</code> method, however, does not support the use of a mapping 527function:</p> 528<ul> 529<li><a href="#buffer_static_method_buffer_from_array"><code>Buffer.from(array)</code></a></li> 530<li><a href="#buffer_static_method_buffer_from_buffer"><code>Buffer.from(buffer)</code></a></li> 531<li><a href="#buffer_static_method_buffer_from_arraybuffer_byteoffset_length"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a></li> 532<li><a href="#buffer_static_method_buffer_from_string_encoding"><code>Buffer.from(string[, encoding])</code></a></li> 533</ul> 534</section><section><h3>Buffers and iteration<span><a class="mark" href="#buffer_buffers_and_iteration" id="buffer_buffers_and_iteration">#</a></span></h3> 535<p><code>Buffer</code> instances can be iterated over using <code>for..of</code> syntax:</p> 536<pre><code class="language-js"><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>]); 537 538<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> b <span class="hljs-keyword">of</span> buf) { 539 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b); 540} 541<span class="hljs-comment">// Prints:</span> 542<span class="hljs-comment">// 1</span> 543<span class="hljs-comment">// 2</span> 544<span class="hljs-comment">// 3</span></code></pre> 545<p>Additionally, the <a href="#buffer_buf_values"><code>buf.values()</code></a>, <a href="#buffer_buf_keys"><code>buf.keys()</code></a>, and 546<a href="#buffer_buf_entries"><code>buf.entries()</code></a> methods can be used to create iterators.</p> 547</section><section><h3>Class: <code>Blob</code><span><a class="mark" href="#buffer_class_blob" id="buffer_class_blob">#</a></span></h3> 548<div class="api_metadata"> 549<span>Added in: v14.18.0</span> 550</div> 551<p></p><div class="api_stability api_stability_1"><a href="documentation.html#documentation_stability_index">Stability: 1</a> - Experimental</div><p></p> 552<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 553multiple worker threads.</p> 554<h4><code>new buffer.Blob([sources[, options]])</code><span><a class="mark" href="#buffer_new_buffer_blob_sources_options" id="buffer_new_buffer_blob_sources_options">#</a></span></h4> 555<div class="api_metadata"> 556<span>Added in: v14.18.0</span> 557</div> 558<ul> 559<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#buffer_class_blob" class="type"><Blob[]></a> An array 560of 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#buffer_class_blob" class="type"><Blob></a> objects, or 561any mix of such objects, that will be stored within the <code>Blob</code>.</li> 562<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 563<ul> 564<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 for string sources. 565<strong>Default</strong>: <code>'utf8'</code>.</li> 566<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 567the MIME media type of the data, however no validation of the type format 568is performed.</li> 569</ul> 570</li> 571</ul> 572<p>Creates a new <code>Blob</code> object containing a concatenation of the given sources.</p> 573<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#buffer_class_buffer" class="type"><Buffer></a> sources are copied into 574the 'Blob' and can therefore be safely modified after the 'Blob' is created.</p> 575<p>String sources are also copied into the <code>Blob</code>.</p> 576<h4><code>blob.arrayBuffer()</code><span><a class="mark" href="#buffer_blob_arraybuffer" id="buffer_blob_arraybuffer">#</a></span></h4> 577<div class="api_metadata"> 578<span>Added in: v14.18.0</span> 579</div> 580<ul> 581<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 582</ul> 583<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 584the <code>Blob</code> data.</p> 585<h4><code>blob.size</code><span><a class="mark" href="#buffer_blob_size" id="buffer_blob_size">#</a></span></h4> 586<div class="api_metadata"> 587<span>Added in: v14.18.0</span> 588</div> 589<p>The total size of the <code>Blob</code> in bytes.</p> 590<h4><code>blob.slice([start, [end, [type]]])</code><span><a class="mark" href="#buffer_blob_slice_start_end_type" id="buffer_blob_slice_start_end_type">#</a></span></h4> 591<div class="api_metadata"> 592<span>Added in: v14.18.0</span> 593</div> 594<ul> 595<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> 596<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> 597<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> 598</ul> 599<p>Creates and returns a new <code>Blob</code> containing a subset of this <code>Blob</code> objects 600data. The original <code>Blob</code> is not alterered.</p> 601<h4><code>blob.text()</code><span><a class="mark" href="#buffer_blob_text" id="buffer_blob_text">#</a></span></h4> 602<div class="api_metadata"> 603<span>Added in: v14.18.0</span> 604</div> 605<ul> 606<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 607</ul> 608<p>Returns a promise that resolves the contents of the <code>Blob</code> decoded as a UTF-8 609string.</p> 610<h4><code>blob.type</code><span><a class="mark" href="#buffer_blob_type" id="buffer_blob_type">#</a></span></h4> 611<div class="api_metadata"> 612<span>Added in: v14.18.0</span> 613</div> 614<ul> 615<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 616</ul> 617<p>The content-type of the <code>Blob</code>.</p> 618<h4><code>Blob</code> objects and <code>MessageChannel</code><span><a class="mark" href="#buffer_blob_objects_and_messagechannel" id="buffer_blob_objects_and_messagechannel">#</a></span></h4> 619<p>Once a <a href="buffer.html#buffer_class_blob" class="type"><Blob></a> object is created, it can be sent via <code>MessagePort</code> to multiple 620destinations without transfering or immediately copying the data. The data 621contained by the <code>Blob</code> is copied only when the <code>arrayBuffer()</code> or <code>text()</code> 622methods are called.</p> 623<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Blob</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'buffer'</span>); 624<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>]); 625<span class="hljs-keyword">const</span> { <span class="hljs-attr">setTimeout</span>: delay } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'timers/promises'</span>); 626 627<span class="hljs-keyword">const</span> mc1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); 628<span class="hljs-keyword">const</span> mc2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); 629 630mc1.<span class="hljs-property">port1</span>.<span class="hljs-property">onmessage</span> = <span class="hljs-keyword">async</span> ({ data }) => { 631 <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>()); 632 mc1.<span class="hljs-property">port1</span>.<span class="hljs-title function_">close</span>(); 633}; 634 635mc2.<span class="hljs-property">port1</span>.<span class="hljs-property">onmessage</span> = <span class="hljs-keyword">async</span> ({ data }) => { 636 <span class="hljs-keyword">await</span> <span class="hljs-title function_">delay</span>(<span class="hljs-number">1000</span>); 637 <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>()); 638 mc2.<span class="hljs-property">port1</span>.<span class="hljs-title function_">close</span>(); 639}; 640 641mc1.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(blob); 642mc2.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(blob); 643 644<span class="hljs-comment">// The Blob is still usable after posting.</span> 645data.<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></pre> 646</section><section><h3>Class: <code>Buffer</code><span><a class="mark" href="#buffer_class_buffer" id="buffer_class_buffer">#</a></span></h3> 647<p>The <code>Buffer</code> class is a global type for dealing with binary data directly. 648It can be constructed in a variety of ways.</p> 649<h4>Static method: <code>Buffer.alloc(size[, fill[, encoding]])</code><span><a class="mark" href="#buffer_static_method_buffer_alloc_size_fill_encoding" id="buffer_static_method_buffer_alloc_size_fill_encoding">#</a></span></h4> 650<div class="api_metadata"> 651<details class="changelog"><summary>History</summary> 652<table> 653<tbody><tr><th>Version</th><th>Changes</th></tr> 654<tr><td>v10.0.0</td> 655<td><p>Attempting to fill a non-zero length buffer with a zero length buffer triggers a thrown exception.</p></td></tr> 656<tr><td>v10.0.0</td> 657<td><p>Specifying an invalid string for <code>fill</code> triggers a thrown exception.</p></td></tr> 658<tr><td>v8.9.3</td> 659<td><p>Specifying an invalid string for <code>fill</code> now results in a zero-filled buffer.</p></td></tr> 660<tr><td>v5.10.0</td> 661<td><p><span>Added in: v5.10.0</span></p></td></tr> 662</tbody></table> 663</details> 664</div> 665<ul> 666<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> 667<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#buffer_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> 668with. <strong>Default:</strong> <code>0</code>.</li> 669<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. 670<strong>Default:</strong> <code>'utf8'</code>.</li> 671</ul> 672<p>Allocates a new <code>Buffer</code> of <code>size</code> bytes. If <code>fill</code> is <code>undefined</code>, the 673<code>Buffer</code> will be zero-filled.</p> 674<pre><code class="language-js"><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>); 675 676<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 677<span class="hljs-comment">// Prints: <Buffer 00 00 00 00 00></span></code></pre> 678<p>If <code>size</code> is larger than 679<a href="#buffer_buffer_constants_max_length"><code>buffer.constants.MAX_LENGTH</code></a> or smaller than 0, <a href="errors.html#ERR_INVALID_OPT_VALUE"><code>ERR_INVALID_OPT_VALUE</code></a> 680is thrown.</p> 681<p>If <code>fill</code> is specified, the allocated <code>Buffer</code> will be initialized by calling 682<a href="#buffer_buf_fill_value_offset_end_encoding"><code>buf.fill(fill)</code></a>.</p> 683<pre><code class="language-js"><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>); 684 685<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 686<span class="hljs-comment">// Prints: <Buffer 61 61 61 61 61></span></code></pre> 687<p>If both <code>fill</code> and <code>encoding</code> are specified, the allocated <code>Buffer</code> will be 688initialized by calling <a href="#buffer_buf_fill_value_offset_end_encoding"><code>buf.fill(fill, encoding)</code></a>.</p> 689<pre><code class="language-js"><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>); 690 691<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 692<span class="hljs-comment">// Prints: <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64></span></code></pre> 693<p>Calling <a href="#buffer_static_method_buffer_alloc_size_fill_encoding"><code>Buffer.alloc()</code></a> can be measurably slower than the alternative 694<a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a> but ensures that the newly created <code>Buffer</code> instance 695contents will never contain sensitive data from previous allocations, including 696data that might not have been allocated for <code>Buffer</code>s.</p> 697<p>A <code>TypeError</code> will be thrown if <code>size</code> is not a number.</p> 698<h4>Static method: <code>Buffer.allocUnsafe(size)</code><span><a class="mark" href="#buffer_static_method_buffer_allocunsafe_size" id="buffer_static_method_buffer_allocunsafe_size">#</a></span></h4> 699<div class="api_metadata"> 700<details class="changelog"><summary>History</summary> 701<table> 702<tbody><tr><th>Version</th><th>Changes</th></tr> 703<tr><td>v7.0.0</td> 704<td><p>Passing a negative <code>size</code> will now throw an error.</p></td></tr> 705<tr><td>v5.10.0</td> 706<td><p><span>Added in: v5.10.0</span></p></td></tr> 707</tbody></table> 708</details> 709</div> 710<ul> 711<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> 712</ul> 713<p>Allocates a new <code>Buffer</code> of <code>size</code> bytes. If <code>size</code> is larger than 714<a href="#buffer_buffer_constants_max_length"><code>buffer.constants.MAX_LENGTH</code></a> or smaller than 0, <a href="errors.html#ERR_INVALID_OPT_VALUE"><code>ERR_INVALID_OPT_VALUE</code></a> 715is thrown.</p> 716<p>The underlying memory for <code>Buffer</code> instances created in this way is <em>not 717initialized</em>. The contents of the newly created <code>Buffer</code> are unknown and 718<em>may contain sensitive data</em>. Use <a href="#buffer_static_method_buffer_alloc_size_fill_encoding"><code>Buffer.alloc()</code></a> instead to initialize 719<code>Buffer</code> instances with zeroes.</p> 720<pre><code class="language-js"><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>); 721 722<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 723<span class="hljs-comment">// Prints (contents may vary): <Buffer a0 8b 28 3f 01 00 00 00 50 32></span> 724 725buf.<span class="hljs-title function_">fill</span>(<span class="hljs-number">0</span>); 726 727<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 728<span class="hljs-comment">// Prints: <Buffer 00 00 00 00 00 00 00 00 00 00></span></code></pre> 729<p>A <code>TypeError</code> will be thrown if <code>size</code> is not a number.</p> 730<p>The <code>Buffer</code> module pre-allocates an internal <code>Buffer</code> instance of 731size <a href="#buffer_class_property_buffer_poolsize"><code>Buffer.poolSize</code></a> that is used as a pool for the fast allocation of new 732<code>Buffer</code> instances created using <a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a>, 733<a href="#buffer_static_method_buffer_from_array"><code>Buffer.from(array)</code></a>, <a href="#buffer_static_method_buffer_concat_list_totallength"><code>Buffer.concat()</code></a>, and the deprecated 734<code>new Buffer(size)</code> constructor only when <code>size</code> is less than or equal 735to <code>Buffer.poolSize >> 1</code> (floor of <a href="#buffer_class_property_buffer_poolsize"><code>Buffer.poolSize</code></a> divided by two).</p> 736<p>Use of this pre-allocated internal memory pool is a key difference between 737calling <code>Buffer.alloc(size, fill)</code> vs. <code>Buffer.allocUnsafe(size).fill(fill)</code>. 738Specifically, <code>Buffer.alloc(size, fill)</code> will <em>never</em> use the internal <code>Buffer</code> 739pool, while <code>Buffer.allocUnsafe(size).fill(fill)</code> <em>will</em> use the internal 740<code>Buffer</code> pool if <code>size</code> is less than or equal to half <a href="#buffer_class_property_buffer_poolsize"><code>Buffer.poolSize</code></a>. The 741difference is subtle but can be important when an application requires the 742additional performance that <a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a> provides.</p> 743<h4>Static method: <code>Buffer.allocUnsafeSlow(size)</code><span><a class="mark" href="#buffer_static_method_buffer_allocunsafeslow_size" id="buffer_static_method_buffer_allocunsafeslow_size">#</a></span></h4> 744<div class="api_metadata"> 745<span>Added in: v5.12.0</span> 746</div> 747<ul> 748<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> 749</ul> 750<p>Allocates a new <code>Buffer</code> of <code>size</code> bytes. If <code>size</code> is larger than 751<a href="#buffer_buffer_constants_max_length"><code>buffer.constants.MAX_LENGTH</code></a> or smaller than 0, <a href="errors.html#ERR_INVALID_OPT_VALUE"><code>ERR_INVALID_OPT_VALUE</code></a> 752is thrown. A zero-length <code>Buffer</code> is created if <code>size</code> is 0.</p> 753<p>The underlying memory for <code>Buffer</code> instances created in this way is <em>not 754initialized</em>. The contents of the newly created <code>Buffer</code> are unknown and 755<em>may contain sensitive data</em>. Use <a href="#buffer_buf_fill_value_offset_end_encoding"><code>buf.fill(0)</code></a> to initialize 756such <code>Buffer</code> instances with zeroes.</p> 757<p>When using <a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a> to allocate new <code>Buffer</code> instances, 758allocations under 4KB are sliced from a single pre-allocated <code>Buffer</code>. This 759allows applications to avoid the garbage collection overhead of creating many 760individually allocated <code>Buffer</code> instances. This approach improves both 761performance and memory usage by eliminating the need to track and clean up as 762many individual <code>ArrayBuffer</code> objects.</p> 763<p>However, in the case where a developer may need to retain a small chunk of 764memory from a pool for an indeterminate amount of time, it may be appropriate 765to create an un-pooled <code>Buffer</code> instance using <code>Buffer.allocUnsafeSlow()</code> and 766then copying out the relevant bits.</p> 767<pre><code class="language-js"><span class="hljs-comment">// Need to keep around a few small chunks of memory.</span> 768<span class="hljs-keyword">const</span> store = []; 769 770socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 771 <span class="hljs-keyword">let</span> data; 772 <span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (data = readable.<span class="hljs-title function_">read</span>())) { 773 <span class="hljs-comment">// Allocate for retained data.</span> 774 <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>); 775 776 <span class="hljs-comment">// Copy the data into the new allocation.</span> 777 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>); 778 779 store.<span class="hljs-title function_">push</span>(sb); 780 } 781});</code></pre> 782<p>A <code>TypeError</code> will be thrown if <code>size</code> is not a number.</p> 783<h4>Static method: <code>Buffer.byteLength(string[, encoding])</code><span><a class="mark" href="#buffer_static_method_buffer_bytelength_string_encoding" id="buffer_static_method_buffer_bytelength_string_encoding">#</a></span></h4> 784<div class="api_metadata"> 785<details class="changelog"><summary>History</summary> 786<table> 787<tbody><tr><th>Version</th><th>Changes</th></tr> 788<tr><td>v7.0.0</td> 789<td><p>Passing invalid input will now throw an error.</p></td></tr> 790<tr><td>v5.10.0</td> 791<td><p>The <code>string</code> parameter can now be any <code>TypedArray</code>, <code>DataView</code> or <code>ArrayBuffer</code>.</p></td></tr> 792<tr><td>v0.1.90</td> 793<td><p><span>Added in: v0.1.90</span></p></td></tr> 794</tbody></table> 795</details> 796</div> 797<ul> 798<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#buffer_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 799value to calculate the length of.</li> 800<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. 801<strong>Default:</strong> <code>'utf8'</code>.</li> 802<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> 803</ul> 804<p>Returns the byte length of a string when encoded using <code>encoding</code>. 805This 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 806for the encoding that is used to convert the string into bytes.</p> 807<p>For <code>'base64'</code>, <code>'base64url'</code>, and <code>'hex'</code>, this function assumes valid input. 808For strings that contain non-base64/hex-encoded data (e.g. whitespace), the 809return value might be greater than the length of a <code>Buffer</code> created from the 810string.</p> 811<pre><code class="language-js"><span class="hljs-keyword">const</span> str = <span class="hljs-string">'\u00bd + \u00bc = \u00be'</span>; 812 813<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> + 814 <span class="hljs-string">`<span class="hljs-subst">${Buffer.byteLength(str, <span class="hljs-string">'utf8'</span>)}</span> bytes`</span>); 815<span class="hljs-comment">// Prints: ½ + ¼ = ¾: 9 characters, 12 bytes</span></code></pre> 816<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>/ 817<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> 818is returned.</p> 819<h4>Static method: <code>Buffer.compare(buf1, buf2)</code><span><a class="mark" href="#buffer_static_method_buffer_compare_buf1_buf2" id="buffer_static_method_buffer_compare_buf1_buf2">#</a></span></h4> 820<div class="api_metadata"> 821<details class="changelog"><summary>History</summary> 822<table> 823<tbody><tr><th>Version</th><th>Changes</th></tr> 824<tr><td>v8.0.0</td> 825<td><p>The arguments can now be <code>Uint8Array</code>s.</p></td></tr> 826<tr><td>v0.11.13</td> 827<td><p><span>Added in: v0.11.13</span></p></td></tr> 828</tbody></table> 829</details> 830</div> 831<ul> 832<li><code>buf1</code> <a href="buffer.html#buffer_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> 833<li><code>buf2</code> <a href="buffer.html#buffer_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> 834<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 835comparison. See <a href="#buffer_buf_compare_target_targetstart_targetend_sourcestart_sourceend"><code>buf.compare()</code></a> for details.</li> 836</ul> 837<p>Compares <code>buf1</code> to <code>buf2</code>, typically for the purpose of sorting arrays of 838<code>Buffer</code> instances. This is equivalent to calling 839<a href="#buffer_buf_compare_target_targetstart_targetend_sourcestart_sourceend"><code>buf1.compare(buf2)</code></a>.</p> 840<pre><code class="language-js"><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>); 841<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>); 842<span class="hljs-keyword">const</span> arr = [buf1, buf2]; 843 844<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>)); 845<span class="hljs-comment">// Prints: [ <Buffer 30 31 32 33>, <Buffer 31 32 33 34> ]</span> 846<span class="hljs-comment">// (This result is equal to: [buf2, buf1].)</span></code></pre> 847<h4>Static method: <code>Buffer.concat(list[, totalLength])</code><span><a class="mark" href="#buffer_static_method_buffer_concat_list_totallength" id="buffer_static_method_buffer_concat_list_totallength">#</a></span></h4> 848<div class="api_metadata"> 849<details class="changelog"><summary>History</summary> 850<table> 851<tbody><tr><th>Version</th><th>Changes</th></tr> 852<tr><td>v8.0.0</td> 853<td><p>The elements of <code>list</code> can now be <code>Uint8Array</code>s.</p></td></tr> 854<tr><td>v0.7.11</td> 855<td><p><span>Added in: v0.7.11</span></p></td></tr> 856</tbody></table> 857</details> 858</div> 859<ul> 860<li><code>list</code> <a href="buffer.html#buffer_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> 861instances to concatenate.</li> 862<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> 863when concatenated.</li> 864<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> 865</ul> 866<p>Returns a new <code>Buffer</code> which is the result of concatenating all the <code>Buffer</code> 867instances in the <code>list</code> together.</p> 868<p>If the list has no items, or if the <code>totalLength</code> is 0, then a new zero-length 869<code>Buffer</code> is returned.</p> 870<p>If <code>totalLength</code> is not provided, it is calculated from the <code>Buffer</code> instances 871in <code>list</code> by adding their lengths.</p> 872<p>If <code>totalLength</code> is provided, it is coerced to an unsigned integer. If the 873combined length of the <code>Buffer</code>s in <code>list</code> exceeds <code>totalLength</code>, the result is 874truncated to <code>totalLength</code>.</p> 875<pre><code class="language-js"><span class="hljs-comment">// Create a single `Buffer` from a list of three `Buffer` instances.</span> 876 877<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>); 878<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>); 879<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>); 880<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>; 881 882<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(totalLength); 883<span class="hljs-comment">// Prints: 42</span> 884 885<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); 886 887<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(bufA); 888<span class="hljs-comment">// Prints: <Buffer 00 00 00 00 ...></span> 889<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(bufA.<span class="hljs-property">length</span>); 890<span class="hljs-comment">// Prints: 42</span></code></pre> 891<p><code>Buffer.concat()</code> may also use the internal <code>Buffer</code> pool like 892<a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a> does.</p> 893<h4>Static method: <code>Buffer.from(array)</code><span><a class="mark" href="#buffer_static_method_buffer_from_array" id="buffer_static_method_buffer_from_array">#</a></span></h4> 894<div class="api_metadata"> 895<span>Added in: v5.10.0</span> 896</div> 897<ul> 898<li><code>array</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer[]></a></li> 899</ul> 900<p>Allocates a new <code>Buffer</code> using an <code>array</code> of bytes in the range <code>0</code> – <code>255</code>. 901Array entries outside that range will be truncated to fit into it.</p> 902<pre><code class="language-js"><span class="hljs-comment">// Creates a new Buffer containing the UTF-8 bytes of the string 'buffer'.</span> 903<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></pre> 904<p>A <code>TypeError</code> will be thrown if <code>array</code> is not an <code>Array</code> or another type 905appropriate for <code>Buffer.from()</code> variants.</p> 906<p><code>Buffer.from(array)</code> and <a href="#buffer_static_method_buffer_from_string_encoding"><code>Buffer.from(string)</code></a> may also use the internal 907<code>Buffer</code> pool like <a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a> does.</p> 908<h4>Static method: <code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code><span><a class="mark" href="#buffer_static_method_buffer_from_arraybuffer_byteoffset_length" id="buffer_static_method_buffer_from_arraybuffer_byteoffset_length">#</a></span></h4> 909<div class="api_metadata"> 910<span>Added in: v5.10.0</span> 911</div> 912<ul> 913<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>, 914<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 915<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>.</li> 916<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> 917<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. 918<strong>Default:</strong> <code>arrayBuffer.byteLength - byteOffset</code>.</li> 919</ul> 920<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 921memory. For example, when passed a reference to the <code>.buffer</code> property of a 922<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 923allocated 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> 924<pre><code class="language-js"><span class="hljs-keyword">const</span> arr = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint</span>16<span class="hljs-built_in">Array</span>(<span class="hljs-number">2</span>); 925 926arr[<span class="hljs-number">0</span>] = <span class="hljs-number">5000</span>; 927arr[<span class="hljs-number">1</span>] = <span class="hljs-number">4000</span>; 928 929<span class="hljs-comment">// Shares memory with `arr`.</span> 930<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>); 931 932<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 933<span class="hljs-comment">// Prints: <Buffer 88 13 a0 0f></span> 934 935<span class="hljs-comment">// Changing the original Uint16Array changes the Buffer also.</span> 936arr[<span class="hljs-number">1</span>] = <span class="hljs-number">6000</span>; 937 938<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 939<span class="hljs-comment">// Prints: <Buffer 88 13 70 17></span></code></pre> 940<p>The optional <code>byteOffset</code> and <code>length</code> arguments specify a memory range within 941the <code>arrayBuffer</code> that will be shared by the <code>Buffer</code>.</p> 942<pre><code class="language-js"><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>); 943<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>); 944 945<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>); 946<span class="hljs-comment">// Prints: 2</span></code></pre> 947<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 948<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> 949variants.</p> 950<p>It is important to remember that a backing <code>ArrayBuffer</code> can cover a range 951of memory that extends beyond the bounds of a <code>TypedArray</code> view. A new 952<code>Buffer</code> created using the <code>buffer</code> property of a <code>TypedArray</code> may extend 953beyond the range of the <code>TypedArray</code>:</p> 954<pre><code class="language-js"><span class="hljs-keyword">const</span> arrA = <span class="hljs-title class_">Uint</span>8<span class="hljs-built_in">Array</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> 955<span class="hljs-keyword">const</span> arrB = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint</span>8<span class="hljs-built_in">Array</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> 956<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> 957 958<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>); 959<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 960<span class="hljs-comment">// Prints: <Buffer 63 64 65 66></span></code></pre> 961<h4>Static method: <code>Buffer.from(buffer)</code><span><a class="mark" href="#buffer_static_method_buffer_from_buffer" id="buffer_static_method_buffer_from_buffer">#</a></span></h4> 962<div class="api_metadata"> 963<span>Added in: v5.10.0</span> 964</div> 965<ul> 966<li><code>buffer</code> <a href="buffer.html#buffer_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 967which to copy data.</li> 968</ul> 969<p>Copies the passed <code>buffer</code> data onto a new <code>Buffer</code> instance.</p> 970<pre><code class="language-js"><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>); 971<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(buf1); 972 973buf1[<span class="hljs-number">0</span>] = <span class="hljs-number">0x61</span>; 974 975<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>()); 976<span class="hljs-comment">// Prints: auffer</span> 977<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>()); 978<span class="hljs-comment">// Prints: buffer</span></code></pre> 979<p>A <code>TypeError</code> will be thrown if <code>buffer</code> is not a <code>Buffer</code> or another type 980appropriate for <code>Buffer.from()</code> variants.</p> 981<h4>Static method: <code>Buffer.from(object[, offsetOrEncoding[, length]])</code><span><a class="mark" href="#buffer_static_method_buffer_from_object_offsetorencoding_length" id="buffer_static_method_buffer_from_object_offsetorencoding_length">#</a></span></h4> 982<div class="api_metadata"> 983<span>Added in: v8.2.0</span> 984</div> 985<ul> 986<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> 987<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> 988<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> 989</ul> 990<p>For objects whose <code>valueOf()</code> function returns a value not strictly equal to 991<code>object</code>, returns <code>Buffer.from(object.valueOf(), offsetOrEncoding, length)</code>.</p> 992<pre><code class="language-js"><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>)); 993<span class="hljs-comment">// Prints: <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74></span></code></pre> 994<p>For objects that support <code>Symbol.toPrimitive</code>, returns 995<code>Buffer.from(object[Symbol.toPrimitive]('string'), offsetOrEncoding)</code>.</p> 996<pre><code class="language-js"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Foo</span> { 997 [<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">toPrimitive</span>]() { 998 <span class="hljs-keyword">return</span> <span class="hljs-string">'this is a test'</span>; 999 } 1000} 1001 1002<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>); 1003<span class="hljs-comment">// Prints: <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74></span></code></pre> 1004<p>A <code>TypeError</code> will be thrown if <code>object</code> does not have the mentioned methods or 1005is not of another type appropriate for <code>Buffer.from()</code> variants.</p> 1006<h4>Static method: <code>Buffer.from(string[, encoding])</code><span><a class="mark" href="#buffer_static_method_buffer_from_string_encoding" id="buffer_static_method_buffer_from_string_encoding">#</a></span></h4> 1007<div class="api_metadata"> 1008<span>Added in: v5.10.0</span> 1009</div> 1010<ul> 1011<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> 1012<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> 1013</ul> 1014<p>Creates a new <code>Buffer</code> containing <code>string</code>. The <code>encoding</code> parameter identifies 1015the character encoding to be used when converting <code>string</code> into bytes.</p> 1016<pre><code class="language-js"><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>); 1017<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>); 1018 1019<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>()); 1020<span class="hljs-comment">// Prints: this is a tést</span> 1021<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>()); 1022<span class="hljs-comment">// Prints: this is a tést</span> 1023<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>)); 1024<span class="hljs-comment">// Prints: this is a tést</span></code></pre> 1025<p>A <code>TypeError</code> will be thrown if <code>string</code> is not a string or another type 1026appropriate for <code>Buffer.from()</code> variants.</p> 1027<h4>Static method: <code>Buffer.isBuffer(obj)</code><span><a class="mark" href="#buffer_static_method_buffer_isbuffer_obj" id="buffer_static_method_buffer_isbuffer_obj">#</a></span></h4> 1028<div class="api_metadata"> 1029<span>Added in: v0.1.101</span> 1030</div> 1031<ul> 1032<li><code>obj</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 1033<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1034</ul> 1035<p>Returns <code>true</code> if <code>obj</code> is a <code>Buffer</code>, <code>false</code> otherwise.</p> 1036<pre><code class="language-js"><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> 1037<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> 1038<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> 1039<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>([]); <span class="hljs-comment">// false</span> 1040<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_">Uint</span>8<span class="hljs-built_in">Array</span>(<span class="hljs-number">1024</span>)); <span class="hljs-comment">// false</span></code></pre> 1041<h4>Static method: <code>Buffer.isEncoding(encoding)</code><span><a class="mark" href="#buffer_static_method_buffer_isencoding_encoding" id="buffer_static_method_buffer_isencoding_encoding">#</a></span></h4> 1042<div class="api_metadata"> 1043<span>Added in: v0.9.1</span> 1044</div> 1045<ul> 1046<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> 1047<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1048</ul> 1049<p>Returns <code>true</code> if <code>encoding</code> is the name of a supported character encoding, 1050or <code>false</code> otherwise.</p> 1051<pre><code class="language-js"><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>)); 1052<span class="hljs-comment">// Prints: true</span> 1053 1054<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>)); 1055<span class="hljs-comment">// Prints: true</span> 1056 1057<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>)); 1058<span class="hljs-comment">// Prints: false</span> 1059 1060<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>)); 1061<span class="hljs-comment">// Prints: false</span></code></pre> 1062<h4>Class property: <code>Buffer.poolSize</code><span><a class="mark" href="#buffer_class_property_buffer_poolsize" id="buffer_class_property_buffer_poolsize">#</a></span></h4> 1063<div class="api_metadata"> 1064<span>Added in: v0.11.3</span> 1065</div> 1066<ul> 1067<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> 1068</ul> 1069<p>This is the size (in bytes) of pre-allocated internal <code>Buffer</code> instances used 1070for pooling. This value may be modified.</p> 1071<h4><code>buf[index]</code><span><a class="mark" href="#buffer_buf_index" id="buffer_buf_index">#</a></span></h4> 1072<ul> 1073<li><code>index</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1074</ul> 1075<p>The index operator <code>[index]</code> can be used to get and set the octet at position 1076<code>index</code> in <code>buf</code>. The values refer to individual bytes, so the legal value 1077range is between <code>0x00</code> and <code>0xFF</code> (hex) or <code>0</code> and <code>255</code> (decimal).</p> 1078<p>This operator is inherited from <code>Uint8Array</code>, so its behavior on out-of-bounds 1079access is the same as <code>Uint8Array</code>. In other words, <code>buf[index]</code> returns 1080<code>undefined</code> when <code>index</code> is negative or greater or equal to <code>buf.length</code>, and 1081<code>buf[index] = value</code> does not modify the buffer if <code>index</code> is negative or 1082<code>>= buf.length</code>.</p> 1083<pre><code class="language-js"><span class="hljs-comment">// Copy an ASCII string into a `Buffer` one byte at a time.</span> 1084<span class="hljs-comment">// (This only works for ASCII-only strings. In general, one should use</span> 1085<span class="hljs-comment">// `Buffer.from()` to perform this conversion.)</span> 1086 1087<span class="hljs-keyword">const</span> str = <span class="hljs-string">'Node.js'</span>; 1088<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>); 1089 1090<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++) { 1091 buf[i] = str.<span class="hljs-title function_">charCodeAt</span>(i); 1092} 1093 1094<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>)); 1095<span class="hljs-comment">// Prints: Node.js</span></code></pre> 1096<h4><code>buf.buffer</code><span><a class="mark" href="#buffer_buf_buffer" id="buffer_buf_buffer">#</a></span></h4> 1097<ul> 1098<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> 1099object is created.</li> 1100</ul> 1101<p>This <code>ArrayBuffer</code> is not guaranteed to correspond exactly to the original 1102<code>Buffer</code>. See the notes on <code>buf.byteOffset</code> for details.</p> 1103<pre><code class="language-js"><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>); 1104<span class="hljs-keyword">const</span> buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arrayBuffer); 1105 1106<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buffer.<span class="hljs-property">buffer</span> === arrayBuffer); 1107<span class="hljs-comment">// Prints: true</span></code></pre> 1108<h4><code>buf.byteOffset</code><span><a class="mark" href="#buffer_buf_byteoffset" id="buffer_buf_byteoffset">#</a></span></h4> 1109<ul> 1110<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> 1111</ul> 1112<p>When setting <code>byteOffset</code> in <code>Buffer.from(ArrayBuffer, byteOffset, length)</code>, 1113or sometimes when allocating a <code>Buffer</code> smaller than <code>Buffer.poolSize</code>, the 1114buffer does not start from a zero offset on the underlying <code>ArrayBuffer</code>.</p> 1115<p>This can cause problems when accessing the underlying <code>ArrayBuffer</code> directly 1116using <code>buf.buffer</code>, as other parts of the <code>ArrayBuffer</code> may be unrelated 1117to the <code>Buffer</code> object itself.</p> 1118<p>A common issue when creating a <code>TypedArray</code> object that shares its memory with 1119a <code>Buffer</code> is that in this case one needs to specify the <code>byteOffset</code> correctly:</p> 1120<pre><code class="language-js"><span class="hljs-comment">// Create a buffer smaller than `Buffer.poolSize`.</span> 1121<span class="hljs-keyword">const</span> nodeBuffer = <span class="hljs-keyword">new</span> <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>]); 1122 1123<span class="hljs-comment">// When casting the Node.js Buffer to an Int8Array, use the byteOffset</span> 1124<span class="hljs-comment">// to refer only to the part of `nodeBuffer.buffer` that contains the memory</span> 1125<span class="hljs-comment">// for `nodeBuffer`.</span> 1126<span class="hljs-keyword">new</span> <span class="hljs-title class_">Int</span>8<span class="hljs-built_in">Array</span>(nodeBuffer.<span class="hljs-property">buffer</span>, nodeBuffer.<span class="hljs-property">byteOffset</span>, nodeBuffer.<span class="hljs-property">length</span>);</code></pre> 1127<h4><code>buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])</code><span><a class="mark" href="#buffer_buf_compare_target_targetstart_targetend_sourcestart_sourceend" id="buffer_buf_compare_target_targetstart_targetend_sourcestart_sourceend">#</a></span></h4> 1128<div class="api_metadata"> 1129<details class="changelog"><summary>History</summary> 1130<table> 1131<tbody><tr><th>Version</th><th>Changes</th></tr> 1132<tr><td>v8.0.0</td> 1133<td><p>The <code>target</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 1134<tr><td>v5.11.0</td> 1135<td><p>Additional parameters for specifying offsets are supported now.</p></td></tr> 1136<tr><td>v0.11.13</td> 1137<td><p><span>Added in: v0.11.13</span></p></td></tr> 1138</tbody></table> 1139</details> 1140</div> 1141<ul> 1142<li><code>target</code> <a href="buffer.html#buffer_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 1143compare <code>buf</code>.</li> 1144<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 1145comparison. <strong>Default:</strong> <code>0</code>.</li> 1146<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 1147(not inclusive). <strong>Default:</strong> <code>target.length</code>.</li> 1148<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. 1149<strong>Default:</strong> <code>0</code>.</li> 1150<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 1151(not inclusive). <strong>Default:</strong> <a href="#buffer_buf_length"><code>buf.length</code></a>.</li> 1152<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1153</ul> 1154<p>Compares <code>buf</code> with <code>target</code> and returns a number indicating whether <code>buf</code> 1155comes before, after, or is the same as <code>target</code> in sort order. 1156Comparison is based on the actual sequence of bytes in each <code>Buffer</code>.</p> 1157<ul> 1158<li><code>0</code> is returned if <code>target</code> is the same as <code>buf</code></li> 1159<li><code>1</code> is returned if <code>target</code> should come <em>before</em> <code>buf</code> when sorted.</li> 1160<li><code>-1</code> is returned if <code>target</code> should come <em>after</em> <code>buf</code> when sorted.</li> 1161</ul> 1162<pre><code class="language-js"><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>); 1163<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>); 1164<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>); 1165 1166<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf1)); 1167<span class="hljs-comment">// Prints: 0</span> 1168<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf2)); 1169<span class="hljs-comment">// Prints: -1</span> 1170<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf3)); 1171<span class="hljs-comment">// Prints: -1</span> 1172<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">compare</span>(buf1)); 1173<span class="hljs-comment">// Prints: 1</span> 1174<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">compare</span>(buf3)); 1175<span class="hljs-comment">// Prints: 1</span> 1176<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>)); 1177<span class="hljs-comment">// Prints: [ <Buffer 41 42 43>, <Buffer 41 42 43 44>, <Buffer 42 43 44> ]</span> 1178<span class="hljs-comment">// (This result is equal to: [buf1, buf3, buf2].)</span></code></pre> 1179<p>The optional <code>targetStart</code>, <code>targetEnd</code>, <code>sourceStart</code>, and <code>sourceEnd</code> 1180arguments can be used to limit the comparison to specific ranges within <code>target</code> 1181and <code>buf</code> respectively.</p> 1182<pre><code class="language-js"><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>]); 1183<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>]); 1184 1185<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>)); 1186<span class="hljs-comment">// Prints: 0</span> 1187<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>)); 1188<span class="hljs-comment">// Prints: -1</span> 1189<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>)); 1190<span class="hljs-comment">// Prints: 1</span></code></pre> 1191<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>, 1192<code>targetEnd > target.byteLength</code>, or <code>sourceEnd > source.byteLength</code>.</p> 1193<h4><code>buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])</code><span><a class="mark" href="#buffer_buf_copy_target_targetstart_sourcestart_sourceend" id="buffer_buf_copy_target_targetstart_sourcestart_sourceend">#</a></span></h4> 1194<div class="api_metadata"> 1195<span>Added in: v0.1.90</span> 1196</div> 1197<ul> 1198<li><code>target</code> <a href="buffer.html#buffer_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> 1199<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 1200writing. <strong>Default:</strong> <code>0</code>.</li> 1201<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. 1202<strong>Default:</strong> <code>0</code>.</li> 1203<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 1204inclusive). <strong>Default:</strong> <a href="#buffer_buf_length"><code>buf.length</code></a>.</li> 1205<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> 1206</ul> 1207<p>Copies data from a region of <code>buf</code> to a region in <code>target</code>, even if the <code>target</code> 1208memory region overlaps with <code>buf</code>.</p> 1209<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 1210for all TypedArrays, including Node.js <code>Buffer</code>s, although it takes 1211different function arguments.</p> 1212<pre><code class="language-js"><span class="hljs-comment">// Create two `Buffer` instances.</span> 1213<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>); 1214<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>); 1215 1216<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++) { 1217 <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> 1218 buf1[i] = i + <span class="hljs-number">97</span>; 1219} 1220 1221<span class="hljs-comment">// Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`.</span> 1222buf1.<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>); 1223<span class="hljs-comment">// This is equivalent to:</span> 1224<span class="hljs-comment">// buf2.set(buf1.subarray(16, 20), 8);</span> 1225 1226<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>)); 1227<span class="hljs-comment">// Prints: !!!!!!!!qrst!!!!!!!!!!!!!</span></code></pre> 1228<pre><code class="language-js"><span class="hljs-comment">// Create a `Buffer` and copy data from one region to an overlapping region</span> 1229<span class="hljs-comment">// within the same `Buffer`.</span> 1230 1231<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>); 1232 1233<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++) { 1234 <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> 1235 buf[i] = i + <span class="hljs-number">97</span>; 1236} 1237 1238buf.<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>); 1239 1240<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>()); 1241<span class="hljs-comment">// Prints: efghijghijklmnopqrstuvwxyz</span></code></pre> 1242<h4><code>buf.entries()</code><span><a class="mark" href="#buffer_buf_entries" id="buffer_buf_entries">#</a></span></h4> 1243<div class="api_metadata"> 1244<span>Added in: v1.1.0</span> 1245</div> 1246<ul> 1247<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> 1248</ul> 1249<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 1250of <code>buf</code>.</p> 1251<pre><code class="language-js"><span class="hljs-comment">// Log the entire contents of a `Buffer`.</span> 1252 1253<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>); 1254 1255<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>()) { 1256 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(pair); 1257} 1258<span class="hljs-comment">// Prints:</span> 1259<span class="hljs-comment">// [0, 98]</span> 1260<span class="hljs-comment">// [1, 117]</span> 1261<span class="hljs-comment">// [2, 102]</span> 1262<span class="hljs-comment">// [3, 102]</span> 1263<span class="hljs-comment">// [4, 101]</span> 1264<span class="hljs-comment">// [5, 114]</span></code></pre> 1265<h4><code>buf.equals(otherBuffer)</code><span><a class="mark" href="#buffer_buf_equals_otherbuffer" id="buffer_buf_equals_otherbuffer">#</a></span></h4> 1266<div class="api_metadata"> 1267<details class="changelog"><summary>History</summary> 1268<table> 1269<tbody><tr><th>Version</th><th>Changes</th></tr> 1270<tr><td>v8.0.0</td> 1271<td><p>The arguments can now be <code>Uint8Array</code>s.</p></td></tr> 1272<tr><td>v0.11.13</td> 1273<td><p><span>Added in: v0.11.13</span></p></td></tr> 1274</tbody></table> 1275</details> 1276</div> 1277<ul> 1278<li><code>otherBuffer</code> <a href="buffer.html#buffer_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 1279compare <code>buf</code>.</li> 1280<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1281</ul> 1282<p>Returns <code>true</code> if both <code>buf</code> and <code>otherBuffer</code> have exactly the same bytes, 1283<code>false</code> otherwise. Equivalent to 1284<a href="#buffer_buf_compare_target_targetstart_targetend_sourcestart_sourceend"><code>buf.compare(otherBuffer) === 0</code></a>.</p> 1285<pre><code class="language-js"><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>); 1286<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>); 1287<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>); 1288 1289<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">equals</span>(buf2)); 1290<span class="hljs-comment">// Prints: true</span> 1291<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">equals</span>(buf3)); 1292<span class="hljs-comment">// Prints: false</span></code></pre> 1293<h4><code>buf.fill(value[, offset[, end]][, encoding])</code><span><a class="mark" href="#buffer_buf_fill_value_offset_end_encoding" id="buffer_buf_fill_value_offset_end_encoding">#</a></span></h4> 1294<div class="api_metadata"> 1295<details class="changelog"><summary>History</summary> 1296<table> 1297<tbody><tr><th>Version</th><th>Changes</th></tr> 1298<tr><td>v11.0.0</td> 1299<td><p>Throws <code>ERR_OUT_OF_RANGE</code> instead of <code>ERR_INDEX_OUT_OF_RANGE</code>.</p></td></tr> 1300<tr><td>v10.0.0</td> 1301<td><p>Negative <code>end</code> values throw an <code>ERR_INDEX_OUT_OF_RANGE</code> error.</p></td></tr> 1302<tr><td>v10.0.0</td> 1303<td><p>Attempting to fill a non-zero length buffer with a zero length buffer triggers a thrown exception.</p></td></tr> 1304<tr><td>v10.0.0</td> 1305<td><p>Specifying an invalid string for <code>value</code> triggers a thrown exception.</p></td></tr> 1306<tr><td>v5.7.0</td> 1307<td><p>The <code>encoding</code> parameter is supported now.</p></td></tr> 1308<tr><td>v0.5.0</td> 1309<td><p><span>Added in: v0.5.0</span></p></td></tr> 1310</tbody></table> 1311</details> 1312</div> 1313<ul> 1314<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#buffer_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>.</li> 1315<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>. 1316<strong>Default:</strong> <code>0</code>.</li> 1317<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> 1318<a href="#buffer_buf_length"><code>buf.length</code></a>.</li> 1319<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. 1320<strong>Default:</strong> <code>'utf8'</code>.</li> 1321<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> A reference to <code>buf</code>.</li> 1322</ul> 1323<p>Fills <code>buf</code> with the specified <code>value</code>. If the <code>offset</code> and <code>end</code> are not given, 1324the entire <code>buf</code> will be filled:</p> 1325<pre><code class="language-js"><span class="hljs-comment">// Fill a `Buffer` with the ASCII character 'h'.</span> 1326 1327<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>); 1328 1329<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">toString</span>()); 1330<span class="hljs-comment">// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh</span></code></pre> 1331<p><code>value</code> is coerced to a <code>uint32</code> value if it is not a string, <code>Buffer</code>, or 1332integer. If the resulting integer is greater than <code>255</code> (decimal), <code>buf</code> will be 1333filled with <code>value & 255</code>.</p> 1334<p>If the final write of a <code>fill()</code> operation falls on a multi-byte character, 1335then only the bytes of that character that fit into <code>buf</code> are written:</p> 1336<pre><code class="language-js"><span class="hljs-comment">// Fill a `Buffer` with character that takes up two bytes in UTF-8.</span> 1337 1338<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>)); 1339<span class="hljs-comment">// Prints: <Buffer c8 a2 c8 a2 c8></span></code></pre> 1340<p>If <code>value</code> contains invalid characters, it is truncated; if no valid 1341fill data remains, an exception is thrown:</p> 1342<pre><code class="language-js"><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>); 1343 1344<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>)); 1345<span class="hljs-comment">// Prints: <Buffer 61 61 61 61 61></span> 1346<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>)); 1347<span class="hljs-comment">// Prints: <Buffer aa aa aa aa aa></span> 1348<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>)); 1349<span class="hljs-comment">// Throws an exception.</span></code></pre> 1350<h4><code>buf.includes(value[, byteOffset][, encoding])</code><span><a class="mark" href="#buffer_buf_includes_value_byteoffset_encoding" id="buffer_buf_includes_value_byteoffset_encoding">#</a></span></h4> 1351<div class="api_metadata"> 1352<span>Added in: v5.3.0</span> 1353</div> 1354<ul> 1355<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#buffer_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> 1356<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 1357offset is calculated from the end of <code>buf</code>. <strong>Default:</strong> <code>0</code>.</li> 1358<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. 1359<strong>Default:</strong> <code>'utf8'</code>.</li> 1360<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> 1361</ul> 1362<p>Equivalent to <a href="#buffer_buf_indexof_value_byteoffset_encoding"><code>buf.indexOf() !== -1</code></a>.</p> 1363<pre><code class="language-js"><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>); 1364 1365<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>)); 1366<span class="hljs-comment">// Prints: true</span> 1367<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>)); 1368<span class="hljs-comment">// Prints: true</span> 1369<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>))); 1370<span class="hljs-comment">// Prints: true</span> 1371<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>)); 1372<span class="hljs-comment">// Prints: true (97 is the decimal ASCII value for 'a')</span> 1373<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>))); 1374<span class="hljs-comment">// Prints: false</span> 1375<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>))); 1376<span class="hljs-comment">// Prints: true</span> 1377<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>)); 1378<span class="hljs-comment">// Prints: false</span></code></pre> 1379<h4><code>buf.indexOf(value[, byteOffset][, encoding])</code><span><a class="mark" href="#buffer_buf_indexof_value_byteoffset_encoding" id="buffer_buf_indexof_value_byteoffset_encoding">#</a></span></h4> 1380<div class="api_metadata"> 1381<details class="changelog"><summary>History</summary> 1382<table> 1383<tbody><tr><th>Version</th><th>Changes</th></tr> 1384<tr><td>v8.0.0</td> 1385<td><p>The <code>value</code> can now be a <code>Uint8Array</code>.</p></td></tr> 1386<tr><td>v5.7.0, v4.4.0</td> 1387<td><p>When <code>encoding</code> is being passed, the <code>byteOffset</code> parameter is no longer required.</p></td></tr> 1388<tr><td>v1.5.0</td> 1389<td><p><span>Added in: v1.5.0</span></p></td></tr> 1390</tbody></table> 1391</details> 1392</div> 1393<ul> 1394<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#buffer_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> 1395<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 1396offset is calculated from the end of <code>buf</code>. <strong>Default:</strong> <code>0</code>.</li> 1397<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 1398determine the binary representation of the string that will be searched for in 1399<code>buf</code>. <strong>Default:</strong> <code>'utf8'</code>.</li> 1400<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 1401<code>-1</code> if <code>buf</code> does not contain <code>value</code>.</li> 1402</ul> 1403<p>If <code>value</code> is:</p> 1404<ul> 1405<li>a string, <code>value</code> is interpreted according to the character encoding in 1406<code>encoding</code>.</li> 1407<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. 1408To compare a partial <code>Buffer</code>, use <a href="#buffer_buf_slice_start_end"><code>buf.slice()</code></a>.</li> 1409<li>a number, <code>value</code> will be interpreted as an unsigned 8-bit integer 1410value between <code>0</code> and <code>255</code>.</li> 1411</ul> 1412<pre><code class="language-js"><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>); 1413 1414<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>)); 1415<span class="hljs-comment">// Prints: 0</span> 1416<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>)); 1417<span class="hljs-comment">// Prints: 2</span> 1418<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>))); 1419<span class="hljs-comment">// Prints: 8</span> 1420<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>)); 1421<span class="hljs-comment">// Prints: 8 (97 is the decimal ASCII value for 'a')</span> 1422<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>))); 1423<span class="hljs-comment">// Prints: -1</span> 1424<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>))); 1425<span class="hljs-comment">// Prints: 8</span> 1426 1427<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>); 1428 1429<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>)); 1430<span class="hljs-comment">// Prints: 4</span> 1431<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>)); 1432<span class="hljs-comment">// Prints: 6</span></code></pre> 1433<p>If <code>value</code> is not a string, number, or <code>Buffer</code>, this method will throw a 1434<code>TypeError</code>. If <code>value</code> is a number, it will be coerced to a valid byte value, 1435an integer between 0 and 255.</p> 1436<p>If <code>byteOffset</code> is not a number, it will be coerced to a number. If the result 1437of coercion is <code>NaN</code> or <code>0</code>, then the entire buffer will be searched. This 1438behavior matches <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf"><code>String.prototype.indexOf()</code></a>.</p> 1439<pre><code class="language-js"><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>); 1440 1441<span class="hljs-comment">// Passing a value that's a number, but not a valid byte.</span> 1442<span class="hljs-comment">// Prints: 2, equivalent to searching for 99 or 'c'.</span> 1443<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>)); 1444<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>)); 1445 1446<span class="hljs-comment">// Passing a byteOffset that coerces to NaN or 0.</span> 1447<span class="hljs-comment">// Prints: 1, searching the whole buffer.</span> 1448<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>)); 1449<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>, {})); 1450<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>)); 1451<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></pre> 1452<p>If <code>value</code> is an empty string or empty <code>Buffer</code> and <code>byteOffset</code> is less 1453than <code>buf.length</code>, <code>byteOffset</code> will be returned. If <code>value</code> is empty and 1454<code>byteOffset</code> is at least <code>buf.length</code>, <code>buf.length</code> will be returned.</p> 1455<h4><code>buf.keys()</code><span><a class="mark" href="#buffer_buf_keys" id="buffer_buf_keys">#</a></span></h4> 1456<div class="api_metadata"> 1457<span>Added in: v1.1.0</span> 1458</div> 1459<ul> 1460<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> 1461</ul> 1462<p>Creates and returns an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols">iterator</a> of <code>buf</code> keys (indices).</p> 1463<pre><code class="language-js"><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>); 1464 1465<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>()) { 1466 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(key); 1467} 1468<span class="hljs-comment">// Prints:</span> 1469<span class="hljs-comment">// 0</span> 1470<span class="hljs-comment">// 1</span> 1471<span class="hljs-comment">// 2</span> 1472<span class="hljs-comment">// 3</span> 1473<span class="hljs-comment">// 4</span> 1474<span class="hljs-comment">// 5</span></code></pre> 1475<h4><code>buf.lastIndexOf(value[, byteOffset][, encoding])</code><span><a class="mark" href="#buffer_buf_lastindexof_value_byteoffset_encoding" id="buffer_buf_lastindexof_value_byteoffset_encoding">#</a></span></h4> 1476<div class="api_metadata"> 1477<details class="changelog"><summary>History</summary> 1478<table> 1479<tbody><tr><th>Version</th><th>Changes</th></tr> 1480<tr><td>v8.0.0</td> 1481<td><p>The <code>value</code> can now be a <code>Uint8Array</code>.</p></td></tr> 1482<tr><td>v6.0.0</td> 1483<td><p><span>Added in: v6.0.0</span></p></td></tr> 1484</tbody></table> 1485</details> 1486</div> 1487<ul> 1488<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#buffer_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> 1489<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 1490offset is calculated from the end of <code>buf</code>. <strong>Default:</strong> 1491<code>buf.length - 1</code>.</li> 1492<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 1493determine the binary representation of the string that will be searched for in 1494<code>buf</code>. <strong>Default:</strong> <code>'utf8'</code>.</li> 1495<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 1496<code>-1</code> if <code>buf</code> does not contain <code>value</code>.</li> 1497</ul> 1498<p>Identical to <a href="#buffer_buf_indexof_value_byteoffset_encoding"><code>buf.indexOf()</code></a>, except the last occurrence of <code>value</code> is found 1499rather than the first occurrence.</p> 1500<pre><code class="language-js"><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>); 1501 1502<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>)); 1503<span class="hljs-comment">// Prints: 0</span> 1504<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>)); 1505<span class="hljs-comment">// Prints: 17</span> 1506<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>))); 1507<span class="hljs-comment">// Prints: 17</span> 1508<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>)); 1509<span class="hljs-comment">// Prints: 15 (97 is the decimal ASCII value for 'a')</span> 1510<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>))); 1511<span class="hljs-comment">// Prints: -1</span> 1512<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>)); 1513<span class="hljs-comment">// Prints: 5</span> 1514<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>)); 1515<span class="hljs-comment">// Prints: -1</span> 1516 1517<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>); 1518 1519<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>)); 1520<span class="hljs-comment">// Prints: 6</span> 1521<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>)); 1522<span class="hljs-comment">// Prints: 4</span></code></pre> 1523<p>If <code>value</code> is not a string, number, or <code>Buffer</code>, this method will throw a 1524<code>TypeError</code>. If <code>value</code> is a number, it will be coerced to a valid byte value, 1525an integer between 0 and 255.</p> 1526<p>If <code>byteOffset</code> is not a number, it will be coerced to a number. Any arguments 1527that coerce to <code>NaN</code>, like <code>{}</code> or <code>undefined</code>, will search the whole buffer. 1528This 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> 1529<pre><code class="language-js"><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>); 1530 1531<span class="hljs-comment">// Passing a value that's a number, but not a valid byte.</span> 1532<span class="hljs-comment">// Prints: 2, equivalent to searching for 99 or 'c'.</span> 1533<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>)); 1534<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>)); 1535 1536<span class="hljs-comment">// Passing a byteOffset that coerces to NaN.</span> 1537<span class="hljs-comment">// Prints: 1, searching the whole buffer.</span> 1538<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>)); 1539<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>, {})); 1540 1541<span class="hljs-comment">// Passing a byteOffset that coerces to 0.</span> 1542<span class="hljs-comment">// Prints: -1, equivalent to passing 0.</span> 1543<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>)); 1544<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></pre> 1545<p>If <code>value</code> is an empty string or empty <code>Buffer</code>, <code>byteOffset</code> will be returned.</p> 1546<h4><code>buf.length</code><span><a class="mark" href="#buffer_buf_length" id="buffer_buf_length">#</a></span></h4> 1547<div class="api_metadata"> 1548<span>Added in: v0.1.90</span> 1549</div> 1550<ul> 1551<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1552</ul> 1553<p>Returns the number of bytes in <code>buf</code>.</p> 1554<pre><code class="language-js"><span class="hljs-comment">// Create a `Buffer` and write a shorter string to it using UTF-8.</span> 1555 1556<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>); 1557 1558<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>); 1559<span class="hljs-comment">// Prints: 1234</span> 1560 1561buf.<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>); 1562 1563<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>); 1564<span class="hljs-comment">// Prints: 1234</span></code></pre> 1565<h4><code>buf.parent</code><span><a class="mark" href="#buffer_buf_parent" id="buffer_buf_parent">#</a></span></h4> 1566<div class="api_metadata"> 1567<span>Deprecated since: v8.0.0</span> 1568</div> 1569<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#buffer_buf_buffer"><code>buf.buffer</code></a> instead.</div><p></p> 1570<p>The <code>buf.parent</code> property is a deprecated alias for <code>buf.buffer</code>.</p> 1571<h4><code>buf.readBigInt64BE([offset])</code><span><a class="mark" href="#buffer_buf_readbigint64be_offset" id="buffer_buf_readbigint64be_offset">#</a></span></h4> 1572<div class="api_metadata"> 1573<span>Added in: v12.0.0, v10.20.0</span> 1574</div> 1575<ul> 1576<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 1577satisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 1578<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 1579</ul> 1580<p>Reads a signed, big-endian 64-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> 1581<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed 1582values.</p> 1583<h4><code>buf.readBigInt64LE([offset])</code><span><a class="mark" href="#buffer_buf_readbigint64le_offset" id="buffer_buf_readbigint64le_offset">#</a></span></h4> 1584<div class="api_metadata"> 1585<span>Added in: v12.0.0, v10.20.0</span> 1586</div> 1587<ul> 1588<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 1589satisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 1590<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 1591</ul> 1592<p>Reads a signed, little-endian 64-bit integer from <code>buf</code> at the specified 1593<code>offset</code>.</p> 1594<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed 1595values.</p> 1596<h4><code>buf.readBigUInt64BE([offset])</code><span><a class="mark" href="#buffer_buf_readbiguint64be_offset" id="buffer_buf_readbiguint64be_offset">#</a></span></h4> 1597<div class="api_metadata"> 1598<details class="changelog"><summary>History</summary> 1599<table> 1600<tbody><tr><th>Version</th><th>Changes</th></tr> 1601<tr><td>v14.10.0</td> 1602<td><p>This function is also available as <code>buf.readBigUint64BE()</code>.</p></td></tr> 1603<tr><td>v12.0.0, v10.20.0</td> 1604<td><p><span>Added in: v12.0.0, v10.20.0</span></p></td></tr> 1605</tbody></table> 1606</details> 1607</div> 1608<ul> 1609<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 1610satisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 1611<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 1612</ul> 1613<p>Reads an unsigned, big-endian 64-bit integer from <code>buf</code> at the specified 1614<code>offset</code>.</p> 1615<p>This function is also available under the <code>readBigUint64BE</code> alias.</p> 1616<pre><code class="language-js"><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>]); 1617 1618<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>)); 1619<span class="hljs-comment">// Prints: 4294967295n</span></code></pre> 1620<h4><code>buf.readBigUInt64LE([offset])</code><span><a class="mark" href="#buffer_buf_readbiguint64le_offset" id="buffer_buf_readbiguint64le_offset">#</a></span></h4> 1621<div class="api_metadata"> 1622<details class="changelog"><summary>History</summary> 1623<table> 1624<tbody><tr><th>Version</th><th>Changes</th></tr> 1625<tr><td>v14.10.0, v12.19.0</td> 1626<td><p>This function is also available as <code>buf.readBigUint64LE()</code>.</p></td></tr> 1627<tr><td>v12.0.0, v10.20.0</td> 1628<td><p><span>Added in: v12.0.0, v10.20.0</span></p></td></tr> 1629</tbody></table> 1630</details> 1631</div> 1632<ul> 1633<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 1634satisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 1635<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 1636</ul> 1637<p>Reads an unsigned, little-endian 64-bit integer from <code>buf</code> at the specified 1638<code>offset</code>.</p> 1639<p>This function is also available under the <code>readBigUint64LE</code> alias.</p> 1640<pre><code class="language-js"><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>]); 1641 1642<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>)); 1643<span class="hljs-comment">// Prints: 18446744069414584320n</span></code></pre> 1644<h4><code>buf.readDoubleBE([offset])</code><span><a class="mark" href="#buffer_buf_readdoublebe_offset" id="buffer_buf_readdoublebe_offset">#</a></span></h4> 1645<div class="api_metadata"> 1646<details class="changelog"><summary>History</summary> 1647<table> 1648<tbody><tr><th>Version</th><th>Changes</th></tr> 1649<tr><td>v10.0.0</td> 1650<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 1651<tr><td>v0.11.15</td> 1652<td><p><span>Added in: v0.11.15</span></p></td></tr> 1653</tbody></table> 1654</details> 1655</div> 1656<ul> 1657<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 1658satisfy <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 1659<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1660</ul> 1661<p>Reads a 64-bit, big-endian double from <code>buf</code> at the specified <code>offset</code>.</p> 1662<pre><code class="language-js"><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>]); 1663 1664<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>)); 1665<span class="hljs-comment">// Prints: 8.20788039913184e-304</span></code></pre> 1666<h4><code>buf.readDoubleLE([offset])</code><span><a class="mark" href="#buffer_buf_readdoublele_offset" id="buffer_buf_readdoublele_offset">#</a></span></h4> 1667<div class="api_metadata"> 1668<details class="changelog"><summary>History</summary> 1669<table> 1670<tbody><tr><th>Version</th><th>Changes</th></tr> 1671<tr><td>v10.0.0</td> 1672<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 1673<tr><td>v0.11.15</td> 1674<td><p><span>Added in: v0.11.15</span></p></td></tr> 1675</tbody></table> 1676</details> 1677</div> 1678<ul> 1679<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 1680satisfy <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 1681<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1682</ul> 1683<p>Reads a 64-bit, little-endian double from <code>buf</code> at the specified <code>offset</code>.</p> 1684<pre><code class="language-js"><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>]); 1685 1686<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>)); 1687<span class="hljs-comment">// Prints: 5.447603722011605e-270</span> 1688<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>)); 1689<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre> 1690<h4><code>buf.readFloatBE([offset])</code><span><a class="mark" href="#buffer_buf_readfloatbe_offset" id="buffer_buf_readfloatbe_offset">#</a></span></h4> 1691<div class="api_metadata"> 1692<details class="changelog"><summary>History</summary> 1693<table> 1694<tbody><tr><th>Version</th><th>Changes</th></tr> 1695<tr><td>v10.0.0</td> 1696<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 1697<tr><td>v0.11.15</td> 1698<td><p><span>Added in: v0.11.15</span></p></td></tr> 1699</tbody></table> 1700</details> 1701</div> 1702<ul> 1703<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 1704satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 1705<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1706</ul> 1707<p>Reads a 32-bit, big-endian float from <code>buf</code> at the specified <code>offset</code>.</p> 1708<pre><code class="language-js"><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>]); 1709 1710<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>)); 1711<span class="hljs-comment">// Prints: 2.387939260590663e-38</span></code></pre> 1712<h4><code>buf.readFloatLE([offset])</code><span><a class="mark" href="#buffer_buf_readfloatle_offset" id="buffer_buf_readfloatle_offset">#</a></span></h4> 1713<div class="api_metadata"> 1714<details class="changelog"><summary>History</summary> 1715<table> 1716<tbody><tr><th>Version</th><th>Changes</th></tr> 1717<tr><td>v10.0.0</td> 1718<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 1719<tr><td>v0.11.15</td> 1720<td><p><span>Added in: v0.11.15</span></p></td></tr> 1721</tbody></table> 1722</details> 1723</div> 1724<ul> 1725<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 1726satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 1727<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1728</ul> 1729<p>Reads a 32-bit, little-endian float from <code>buf</code> at the specified <code>offset</code>.</p> 1730<pre><code class="language-js"><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>]); 1731 1732<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>)); 1733<span class="hljs-comment">// Prints: 1.539989614439558e-36</span> 1734<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>)); 1735<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre> 1736<h4><code>buf.readInt8([offset])</code><span><a class="mark" href="#buffer_buf_readint8_offset" id="buffer_buf_readint8_offset">#</a></span></h4> 1737<div class="api_metadata"> 1738<details class="changelog"><summary>History</summary> 1739<table> 1740<tbody><tr><th>Version</th><th>Changes</th></tr> 1741<tr><td>v10.0.0</td> 1742<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 1743<tr><td>v0.5.0</td> 1744<td><p><span>Added in: v0.5.0</span></p></td></tr> 1745</tbody></table> 1746</details> 1747</div> 1748<ul> 1749<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 1750satisfy <code>0 <= offset <= buf.length - 1</code>. <strong>Default:</strong> <code>0</code>.</li> 1751<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1752</ul> 1753<p>Reads a signed 8-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> 1754<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p> 1755<pre><code class="language-js"><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>]); 1756 1757<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>)); 1758<span class="hljs-comment">// Prints: -1</span> 1759<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>)); 1760<span class="hljs-comment">// Prints: 5</span> 1761<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>)); 1762<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre> 1763<h4><code>buf.readInt16BE([offset])</code><span><a class="mark" href="#buffer_buf_readint16be_offset" id="buffer_buf_readint16be_offset">#</a></span></h4> 1764<div class="api_metadata"> 1765<details class="changelog"><summary>History</summary> 1766<table> 1767<tbody><tr><th>Version</th><th>Changes</th></tr> 1768<tr><td>v10.0.0</td> 1769<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 1770<tr><td>v0.5.5</td> 1771<td><p><span>Added in: v0.5.5</span></p></td></tr> 1772</tbody></table> 1773</details> 1774</div> 1775<ul> 1776<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 1777satisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> 1778<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1779</ul> 1780<p>Reads a signed, big-endian 16-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> 1781<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p> 1782<pre><code class="language-js"><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>]); 1783 1784<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>)); 1785<span class="hljs-comment">// Prints: 5</span></code></pre> 1786<h4><code>buf.readInt16LE([offset])</code><span><a class="mark" href="#buffer_buf_readint16le_offset" id="buffer_buf_readint16le_offset">#</a></span></h4> 1787<div class="api_metadata"> 1788<details class="changelog"><summary>History</summary> 1789<table> 1790<tbody><tr><th>Version</th><th>Changes</th></tr> 1791<tr><td>v10.0.0</td> 1792<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 1793<tr><td>v0.5.5</td> 1794<td><p><span>Added in: v0.5.5</span></p></td></tr> 1795</tbody></table> 1796</details> 1797</div> 1798<ul> 1799<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 1800satisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> 1801<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1802</ul> 1803<p>Reads a signed, little-endian 16-bit integer from <code>buf</code> at the specified 1804<code>offset</code>.</p> 1805<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p> 1806<pre><code class="language-js"><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>]); 1807 1808<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>)); 1809<span class="hljs-comment">// Prints: 1280</span> 1810<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>)); 1811<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre> 1812<h4><code>buf.readInt32BE([offset])</code><span><a class="mark" href="#buffer_buf_readint32be_offset" id="buffer_buf_readint32be_offset">#</a></span></h4> 1813<div class="api_metadata"> 1814<details class="changelog"><summary>History</summary> 1815<table> 1816<tbody><tr><th>Version</th><th>Changes</th></tr> 1817<tr><td>v10.0.0</td> 1818<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 1819<tr><td>v0.5.5</td> 1820<td><p><span>Added in: v0.5.5</span></p></td></tr> 1821</tbody></table> 1822</details> 1823</div> 1824<ul> 1825<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 1826satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 1827<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1828</ul> 1829<p>Reads a signed, big-endian 32-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> 1830<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p> 1831<pre><code class="language-js"><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>]); 1832 1833<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>)); 1834<span class="hljs-comment">// Prints: 5</span></code></pre> 1835<h4><code>buf.readInt32LE([offset])</code><span><a class="mark" href="#buffer_buf_readint32le_offset" id="buffer_buf_readint32le_offset">#</a></span></h4> 1836<div class="api_metadata"> 1837<details class="changelog"><summary>History</summary> 1838<table> 1839<tbody><tr><th>Version</th><th>Changes</th></tr> 1840<tr><td>v10.0.0</td> 1841<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 1842<tr><td>v0.5.5</td> 1843<td><p><span>Added in: v0.5.5</span></p></td></tr> 1844</tbody></table> 1845</details> 1846</div> 1847<ul> 1848<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 1849satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 1850<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1851</ul> 1852<p>Reads a signed, little-endian 32-bit integer from <code>buf</code> at the specified 1853<code>offset</code>.</p> 1854<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p> 1855<pre><code class="language-js"><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>]); 1856 1857<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>)); 1858<span class="hljs-comment">// Prints: 83886080</span> 1859<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>)); 1860<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre> 1861<h4><code>buf.readIntBE(offset, byteLength)</code><span><a class="mark" href="#buffer_buf_readintbe_offset_bytelength" id="buffer_buf_readintbe_offset_bytelength">#</a></span></h4> 1862<div class="api_metadata"> 1863<details class="changelog"><summary>History</summary> 1864<table> 1865<tbody><tr><th>Version</th><th>Changes</th></tr> 1866<tr><td>v10.0.0</td> 1867<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> 1868<tr><td>v0.11.15</td> 1869<td><p><span>Added in: v0.11.15</span></p></td></tr> 1870</tbody></table> 1871</details> 1872</div> 1873<ul> 1874<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 1875satisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> 1876<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 1877<code>0 < byteLength <= 6</code>.</li> 1878<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1879</ul> 1880<p>Reads <code>byteLength</code> number of bytes from <code>buf</code> at the specified <code>offset</code> 1881and interprets the result as a big-endian, two's complement signed value 1882supporting up to 48 bits of accuracy.</p> 1883<pre><code class="language-js"><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>]); 1884 1885<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>)); 1886<span class="hljs-comment">// Prints: 1234567890ab</span> 1887<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>)); 1888<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span> 1889<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>)); 1890<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre> 1891<h4><code>buf.readIntLE(offset, byteLength)</code><span><a class="mark" href="#buffer_buf_readintle_offset_bytelength" id="buffer_buf_readintle_offset_bytelength">#</a></span></h4> 1892<div class="api_metadata"> 1893<details class="changelog"><summary>History</summary> 1894<table> 1895<tbody><tr><th>Version</th><th>Changes</th></tr> 1896<tr><td>v10.0.0</td> 1897<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> 1898<tr><td>v0.11.15</td> 1899<td><p><span>Added in: v0.11.15</span></p></td></tr> 1900</tbody></table> 1901</details> 1902</div> 1903<ul> 1904<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 1905satisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> 1906<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 1907<code>0 < byteLength <= 6</code>.</li> 1908<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1909</ul> 1910<p>Reads <code>byteLength</code> number of bytes from <code>buf</code> at the specified <code>offset</code> 1911and interprets the result as a little-endian, two's complement signed value 1912supporting up to 48 bits of accuracy.</p> 1913<pre><code class="language-js"><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>]); 1914 1915<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>)); 1916<span class="hljs-comment">// Prints: -546f87a9cbee</span></code></pre> 1917<h4><code>buf.readUInt8([offset])</code><span><a class="mark" href="#buffer_buf_readuint8_offset" id="buffer_buf_readuint8_offset">#</a></span></h4> 1918<div class="api_metadata"> 1919<details class="changelog"><summary>History</summary> 1920<table> 1921<tbody><tr><th>Version</th><th>Changes</th></tr> 1922<tr><td>v14.9.0</td> 1923<td><p>This function is also available as <code>buf.readUint8()</code>.</p></td></tr> 1924<tr><td>v10.0.0</td> 1925<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 1926<tr><td>v0.5.0</td> 1927<td><p><span>Added in: v0.5.0</span></p></td></tr> 1928</tbody></table> 1929</details> 1930</div> 1931<ul> 1932<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 1933satisfy <code>0 <= offset <= buf.length - 1</code>. <strong>Default:</strong> <code>0</code>.</li> 1934<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1935</ul> 1936<p>Reads an unsigned 8-bit integer from <code>buf</code> at the specified <code>offset</code>.</p> 1937<p>This function is also available under the <code>readUint8</code> alias.</p> 1938<pre><code class="language-js"><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>]); 1939 1940<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>)); 1941<span class="hljs-comment">// Prints: 1</span> 1942<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>)); 1943<span class="hljs-comment">// Prints: 254</span> 1944<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>)); 1945<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre> 1946<h4><code>buf.readUInt16BE([offset])</code><span><a class="mark" href="#buffer_buf_readuint16be_offset" id="buffer_buf_readuint16be_offset">#</a></span></h4> 1947<div class="api_metadata"> 1948<details class="changelog"><summary>History</summary> 1949<table> 1950<tbody><tr><th>Version</th><th>Changes</th></tr> 1951<tr><td>v14.9.0</td> 1952<td><p>This function is also available as <code>buf.readUint16BE()</code>.</p></td></tr> 1953<tr><td>v10.0.0</td> 1954<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 1955<tr><td>v0.5.5</td> 1956<td><p><span>Added in: v0.5.5</span></p></td></tr> 1957</tbody></table> 1958</details> 1959</div> 1960<ul> 1961<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 1962satisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> 1963<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1964</ul> 1965<p>Reads an unsigned, big-endian 16-bit integer from <code>buf</code> at the specified 1966<code>offset</code>.</p> 1967<p>This function is also available under the <code>readUint16BE</code> alias.</p> 1968<pre><code class="language-js"><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>]); 1969 1970<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>)); 1971<span class="hljs-comment">// Prints: 1234</span> 1972<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>)); 1973<span class="hljs-comment">// Prints: 3456</span></code></pre> 1974<h4><code>buf.readUInt16LE([offset])</code><span><a class="mark" href="#buffer_buf_readuint16le_offset" id="buffer_buf_readuint16le_offset">#</a></span></h4> 1975<div class="api_metadata"> 1976<details class="changelog"><summary>History</summary> 1977<table> 1978<tbody><tr><th>Version</th><th>Changes</th></tr> 1979<tr><td>v14.9.0</td> 1980<td><p>This function is also available as <code>buf.readUint16LE()</code>.</p></td></tr> 1981<tr><td>v10.0.0</td> 1982<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 1983<tr><td>v0.5.5</td> 1984<td><p><span>Added in: v0.5.5</span></p></td></tr> 1985</tbody></table> 1986</details> 1987</div> 1988<ul> 1989<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 1990satisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> 1991<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1992</ul> 1993<p>Reads an unsigned, little-endian 16-bit integer from <code>buf</code> at the specified 1994<code>offset</code>.</p> 1995<p>This function is also available under the <code>readUint16LE</code> alias.</p> 1996<pre><code class="language-js"><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>]); 1997 1998<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>)); 1999<span class="hljs-comment">// Prints: 3412</span> 2000<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>)); 2001<span class="hljs-comment">// Prints: 5634</span> 2002<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>)); 2003<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre> 2004<h4><code>buf.readUInt32BE([offset])</code><span><a class="mark" href="#buffer_buf_readuint32be_offset" id="buffer_buf_readuint32be_offset">#</a></span></h4> 2005<div class="api_metadata"> 2006<details class="changelog"><summary>History</summary> 2007<table> 2008<tbody><tr><th>Version</th><th>Changes</th></tr> 2009<tr><td>v14.9.0</td> 2010<td><p>This function is also available as <code>buf.readUint32BE()</code>.</p></td></tr> 2011<tr><td>v10.0.0</td> 2012<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 2013<tr><td>v0.5.5</td> 2014<td><p><span>Added in: v0.5.5</span></p></td></tr> 2015</tbody></table> 2016</details> 2017</div> 2018<ul> 2019<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 2020satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 2021<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 2022</ul> 2023<p>Reads an unsigned, big-endian 32-bit integer from <code>buf</code> at the specified 2024<code>offset</code>.</p> 2025<p>This function is also available under the <code>readUint32BE</code> alias.</p> 2026<pre><code class="language-js"><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>]); 2027 2028<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>)); 2029<span class="hljs-comment">// Prints: 12345678</span></code></pre> 2030<h4><code>buf.readUInt32LE([offset])</code><span><a class="mark" href="#buffer_buf_readuint32le_offset" id="buffer_buf_readuint32le_offset">#</a></span></h4> 2031<div class="api_metadata"> 2032<details class="changelog"><summary>History</summary> 2033<table> 2034<tbody><tr><th>Version</th><th>Changes</th></tr> 2035<tr><td>v14.9.0</td> 2036<td><p>This function is also available as <code>buf.readUint32LE()</code>.</p></td></tr> 2037<tr><td>v10.0.0</td> 2038<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 2039<tr><td>v0.5.5</td> 2040<td><p><span>Added in: v0.5.5</span></p></td></tr> 2041</tbody></table> 2042</details> 2043</div> 2044<ul> 2045<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 2046satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 2047<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 2048</ul> 2049<p>Reads an unsigned, little-endian 32-bit integer from <code>buf</code> at the specified 2050<code>offset</code>.</p> 2051<p>This function is also available under the <code>readUint32LE</code> alias.</p> 2052<pre><code class="language-js"><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>]); 2053 2054<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>)); 2055<span class="hljs-comment">// Prints: 78563412</span> 2056<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>)); 2057<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre> 2058<h4><code>buf.readUIntBE(offset, byteLength)</code><span><a class="mark" href="#buffer_buf_readuintbe_offset_bytelength" id="buffer_buf_readuintbe_offset_bytelength">#</a></span></h4> 2059<div class="api_metadata"> 2060<details class="changelog"><summary>History</summary> 2061<table> 2062<tbody><tr><th>Version</th><th>Changes</th></tr> 2063<tr><td>v14.9.0</td> 2064<td><p>This function is also available as <code>buf.readUintBE()</code>.</p></td></tr> 2065<tr><td>v10.0.0</td> 2066<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> 2067<tr><td>v0.11.15</td> 2068<td><p><span>Added in: v0.11.15</span></p></td></tr> 2069</tbody></table> 2070</details> 2071</div> 2072<ul> 2073<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 2074satisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> 2075<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 2076<code>0 < byteLength <= 6</code>.</li> 2077<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 2078</ul> 2079<p>Reads <code>byteLength</code> number of bytes from <code>buf</code> at the specified <code>offset</code> 2080and interprets the result as an unsigned big-endian integer supporting 2081up to 48 bits of accuracy.</p> 2082<p>This function is also available under the <code>readUintBE</code> alias.</p> 2083<pre><code class="language-js"><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>]); 2084 2085<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>)); 2086<span class="hljs-comment">// Prints: 1234567890ab</span> 2087<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>)); 2088<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre> 2089<h4><code>buf.readUIntLE(offset, byteLength)</code><span><a class="mark" href="#buffer_buf_readuintle_offset_bytelength" id="buffer_buf_readuintle_offset_bytelength">#</a></span></h4> 2090<div class="api_metadata"> 2091<details class="changelog"><summary>History</summary> 2092<table> 2093<tbody><tr><th>Version</th><th>Changes</th></tr> 2094<tr><td>v14.9.0</td> 2095<td><p>This function is also available as <code>buf.readUintLE()</code>.</p></td></tr> 2096<tr><td>v10.0.0</td> 2097<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> 2098<tr><td>v0.11.15</td> 2099<td><p><span>Added in: v0.11.15</span></p></td></tr> 2100</tbody></table> 2101</details> 2102</div> 2103<ul> 2104<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 2105satisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> 2106<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 2107<code>0 < byteLength <= 6</code>.</li> 2108<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 2109</ul> 2110<p>Reads <code>byteLength</code> number of bytes from <code>buf</code> at the specified <code>offset</code> 2111and interprets the result as an unsigned, little-endian integer supporting 2112up to 48 bits of accuracy.</p> 2113<p>This function is also available under the <code>readUintLE</code> alias.</p> 2114<pre><code class="language-js"><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>]); 2115 2116<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>)); 2117<span class="hljs-comment">// Prints: ab9078563412</span></code></pre> 2118<h4><code>buf.subarray([start[, end]])</code><span><a class="mark" href="#buffer_buf_subarray_start_end" id="buffer_buf_subarray_start_end">#</a></span></h4> 2119<div class="api_metadata"> 2120<span>Added in: v3.0.0</span> 2121</div> 2122<ul> 2123<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> 2124<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). 2125<strong>Default:</strong> <a href="#buffer_buf_length"><code>buf.length</code></a>.</li> 2126<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> 2127</ul> 2128<p>Returns a new <code>Buffer</code> that references the same memory as the original, but 2129offset and cropped by the <code>start</code> and <code>end</code> indices.</p> 2130<p>Specifying <code>end</code> greater than <a href="#buffer_buf_length"><code>buf.length</code></a> will return the same result as 2131that of <code>end</code> equal to <a href="#buffer_buf_length"><code>buf.length</code></a>.</p> 2132<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> 2133<p>Modifying the new <code>Buffer</code> slice will modify the memory in the original <code>Buffer</code> 2134because the allocated memory of the two objects overlap.</p> 2135<pre><code class="language-js"><span class="hljs-comment">// Create a `Buffer` with the ASCII alphabet, take a slice, and modify one byte</span> 2136<span class="hljs-comment">// from the original `Buffer`.</span> 2137 2138<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>); 2139 2140<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++) { 2141 <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> 2142 buf1[i] = i + <span class="hljs-number">97</span>; 2143} 2144 2145<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>); 2146 2147<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>)); 2148<span class="hljs-comment">// Prints: abc</span> 2149 2150buf1[<span class="hljs-number">0</span>] = <span class="hljs-number">33</span>; 2151 2152<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>)); 2153<span class="hljs-comment">// Prints: !bc</span></code></pre> 2154<p>Specifying negative indexes causes the slice to be generated relative to the 2155end of <code>buf</code> rather than the beginning.</p> 2156<pre><code class="language-js"><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>); 2157 2158<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>()); 2159<span class="hljs-comment">// Prints: buffe</span> 2160<span class="hljs-comment">// (Equivalent to buf.subarray(0, 5).)</span> 2161 2162<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>()); 2163<span class="hljs-comment">// Prints: buff</span> 2164<span class="hljs-comment">// (Equivalent to buf.subarray(0, 4).)</span> 2165 2166<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>()); 2167<span class="hljs-comment">// Prints: uff</span> 2168<span class="hljs-comment">// (Equivalent to buf.subarray(1, 4).)</span></code></pre> 2169<h4><code>buf.slice([start[, end]])</code><span><a class="mark" href="#buffer_buf_slice_start_end" id="buffer_buf_slice_start_end">#</a></span></h4> 2170<div class="api_metadata"> 2171<details class="changelog"><summary>History</summary> 2172<table> 2173<tbody><tr><th>Version</th><th>Changes</th></tr> 2174<tr><td>v7.0.0</td> 2175<td><p>All offsets are now coerced to integers before doing any calculations with them.</p></td></tr> 2176<tr><td>v7.1.0, v6.9.2</td> 2177<td><p>Coercing the offsets to integers now handles values outside the 32-bit integer range properly.</p></td></tr> 2178<tr><td>v0.3.0</td> 2179<td><p><span>Added in: v0.3.0</span></p></td></tr> 2180</tbody></table> 2181</details> 2182</div> 2183<ul> 2184<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> 2185<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). 2186<strong>Default:</strong> <a href="#buffer_buf_length"><code>buf.length</code></a>.</li> 2187<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> 2188</ul> 2189<p>Returns a new <code>Buffer</code> that references the same memory as the original, but 2190offset and cropped by the <code>start</code> and <code>end</code> indices.</p> 2191<p>This is the same behavior as <code>buf.subarray()</code>.</p> 2192<p>This method is not compatible with the <code>Uint8Array.prototype.slice()</code>, 2193which is a superclass of <code>Buffer</code>. To copy the slice, use 2194<code>Uint8Array.prototype.slice()</code>.</p> 2195<pre><code class="language-js"><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>); 2196 2197<span class="hljs-keyword">const</span> copiedBuf = <span class="hljs-title class_">Uint</span>8<span class="hljs-built_in">Array</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); 2198copiedBuf[<span class="hljs-number">0</span>]++; 2199<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(copiedBuf.<span class="hljs-title function_">toString</span>()); 2200<span class="hljs-comment">// Prints: cuffer</span> 2201 2202<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>()); 2203<span class="hljs-comment">// Prints: buffer</span></code></pre> 2204<h4><code>buf.swap16()</code><span><a class="mark" href="#buffer_buf_swap16" id="buffer_buf_swap16">#</a></span></h4> 2205<div class="api_metadata"> 2206<span>Added in: v5.10.0</span> 2207</div> 2208<ul> 2209<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> A reference to <code>buf</code>.</li> 2210</ul> 2211<p>Interprets <code>buf</code> as an array of unsigned 16-bit integers and swaps the 2212byte order <em>in-place</em>. Throws <a href="errors.html#ERR_INVALID_BUFFER_SIZE"><code>ERR_INVALID_BUFFER_SIZE</code></a> if <a href="#buffer_buf_length"><code>buf.length</code></a> 2213is not a multiple of 2.</p> 2214<pre><code class="language-js"><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>]); 2215 2216<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 2217<span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05 06 07 08></span> 2218 2219buf1.<span class="hljs-title function_">swap16</span>(); 2220 2221<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 2222<span class="hljs-comment">// Prints: <Buffer 02 01 04 03 06 05 08 07></span> 2223 2224<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>]); 2225 2226buf2.<span class="hljs-title function_">swap16</span>(); 2227<span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code></pre> 2228<p>One convenient use of <code>buf.swap16()</code> is to perform a fast in-place conversion 2229between UTF-16 little-endian and UTF-16 big-endian:</p> 2230<pre><code class="language-js"><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>); 2231buf.<span class="hljs-title function_">swap16</span>(); <span class="hljs-comment">// Convert to big-endian UTF-16 text.</span></code></pre> 2232<h4><code>buf.swap32()</code><span><a class="mark" href="#buffer_buf_swap32" id="buffer_buf_swap32">#</a></span></h4> 2233<div class="api_metadata"> 2234<span>Added in: v5.10.0</span> 2235</div> 2236<ul> 2237<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> A reference to <code>buf</code>.</li> 2238</ul> 2239<p>Interprets <code>buf</code> as an array of unsigned 32-bit integers and swaps the 2240byte order <em>in-place</em>. Throws <a href="errors.html#ERR_INVALID_BUFFER_SIZE"><code>ERR_INVALID_BUFFER_SIZE</code></a> if <a href="#buffer_buf_length"><code>buf.length</code></a> 2241is not a multiple of 4.</p> 2242<pre><code class="language-js"><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>]); 2243 2244<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 2245<span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05 06 07 08></span> 2246 2247buf1.<span class="hljs-title function_">swap32</span>(); 2248 2249<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 2250<span class="hljs-comment">// Prints: <Buffer 04 03 02 01 08 07 06 05></span> 2251 2252<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>]); 2253 2254buf2.<span class="hljs-title function_">swap32</span>(); 2255<span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code></pre> 2256<h4><code>buf.swap64()</code><span><a class="mark" href="#buffer_buf_swap64" id="buffer_buf_swap64">#</a></span></h4> 2257<div class="api_metadata"> 2258<span>Added in: v6.3.0</span> 2259</div> 2260<ul> 2261<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> A reference to <code>buf</code>.</li> 2262</ul> 2263<p>Interprets <code>buf</code> as an array of 64-bit numbers and swaps byte order <em>in-place</em>. 2264Throws <a href="errors.html#ERR_INVALID_BUFFER_SIZE"><code>ERR_INVALID_BUFFER_SIZE</code></a> if <a href="#buffer_buf_length"><code>buf.length</code></a> is not a multiple of 8.</p> 2265<pre><code class="language-js"><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>]); 2266 2267<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 2268<span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05 06 07 08></span> 2269 2270buf1.<span class="hljs-title function_">swap64</span>(); 2271 2272<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1); 2273<span class="hljs-comment">// Prints: <Buffer 08 07 06 05 04 03 02 01></span> 2274 2275<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>]); 2276 2277buf2.<span class="hljs-title function_">swap64</span>(); 2278<span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code></pre> 2279<h4><code>buf.toJSON()</code><span><a class="mark" href="#buffer_buf_tojson" id="buffer_buf_tojson">#</a></span></h4> 2280<div class="api_metadata"> 2281<span>Added in: v0.9.2</span> 2282</div> 2283<ul> 2284<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 2285</ul> 2286<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 2287this function when stringifying a <code>Buffer</code> instance.</p> 2288<p><code>Buffer.from()</code> accepts objects in the format returned from this method. 2289In particular, <code>Buffer.from(buf.toJSON())</code> works like <code>Buffer.from(buf)</code>.</p> 2290<pre><code class="language-js"><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>]); 2291<span class="hljs-keyword">const</span> json = <span class="hljs-variable constant_">JSON</span>.<span class="hljs-title function_">stringify</span>(buf); 2292 2293<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(json); 2294<span class="hljs-comment">// Prints: {"type":"Buffer","data":[1,2,3,4,5]}</span> 2295 2296<span class="hljs-keyword">const</span> copy = <span class="hljs-variable constant_">JSON</span>.<span class="hljs-title function_">parse</span>(json, <span class="hljs-function">(<span class="hljs-params">key, value</span>) =></span> { 2297 <span class="hljs-keyword">return</span> value && value.<span class="hljs-property">type</span> === <span class="hljs-string">'Buffer'</span> ? 2298 <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(value) : 2299 value; 2300}); 2301 2302<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(copy); 2303<span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05></span></code></pre> 2304<h4><code>buf.toString([encoding[, start[, end]]])</code><span><a class="mark" href="#buffer_buf_tostring_encoding_start_end" id="buffer_buf_tostring_encoding_start_end">#</a></span></h4> 2305<div class="api_metadata"> 2306<span>Added in: v0.1.90</span> 2307</div> 2308<ul> 2309<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> 2310<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> 2311<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). 2312<strong>Default:</strong> <a href="#buffer_buf_length"><code>buf.length</code></a>.</li> 2313<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2314</ul> 2315<p>Decodes <code>buf</code> to a string according to the specified character encoding in 2316<code>encoding</code>. <code>start</code> and <code>end</code> may be passed to decode only a subset of <code>buf</code>.</p> 2317<p>If <code>encoding</code> is <code>'utf8'</code> and a byte sequence in the input is not valid UTF-8, 2318then each invalid byte is replaced with the replacement character <code>U+FFFD</code>.</p> 2319<p>The maximum length of a string instance (in UTF-16 code units) is available 2320as <a href="#buffer_buffer_constants_max_string_length"><code>buffer.constants.MAX_STRING_LENGTH</code></a>.</p> 2321<pre><code class="language-js"><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>); 2322 2323<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++) { 2324 <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span> 2325 buf1[i] = i + <span class="hljs-number">97</span>; 2326} 2327 2328<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>)); 2329<span class="hljs-comment">// Prints: abcdefghijklmnopqrstuvwxyz</span> 2330<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>)); 2331<span class="hljs-comment">// Prints: abcde</span> 2332 2333<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>); 2334 2335<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>)); 2336<span class="hljs-comment">// Prints: 74c3a97374</span> 2337<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>)); 2338<span class="hljs-comment">// Prints: té</span> 2339<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>)); 2340<span class="hljs-comment">// Prints: té</span></code></pre> 2341<h4><code>buf.values()</code><span><a class="mark" href="#buffer_buf_values" id="buffer_buf_values">#</a></span></h4> 2342<div class="api_metadata"> 2343<span>Added in: v1.1.0</span> 2344</div> 2345<ul> 2346<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> 2347</ul> 2348<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 2349called automatically when a <code>Buffer</code> is used in a <code>for..of</code> statement.</p> 2350<pre><code class="language-js"><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>); 2351 2352<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>()) { 2353 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value); 2354} 2355<span class="hljs-comment">// Prints:</span> 2356<span class="hljs-comment">// 98</span> 2357<span class="hljs-comment">// 117</span> 2358<span class="hljs-comment">// 102</span> 2359<span class="hljs-comment">// 102</span> 2360<span class="hljs-comment">// 101</span> 2361<span class="hljs-comment">// 114</span> 2362 2363<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> value <span class="hljs-keyword">of</span> buf) { 2364 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value); 2365} 2366<span class="hljs-comment">// Prints:</span> 2367<span class="hljs-comment">// 98</span> 2368<span class="hljs-comment">// 117</span> 2369<span class="hljs-comment">// 102</span> 2370<span class="hljs-comment">// 102</span> 2371<span class="hljs-comment">// 101</span> 2372<span class="hljs-comment">// 114</span></code></pre> 2373<h4><code>buf.write(string[, offset[, length]][, encoding])</code><span><a class="mark" href="#buffer_buf_write_string_offset_length_encoding" id="buffer_buf_write_string_offset_length_encoding">#</a></span></h4> 2374<div class="api_metadata"> 2375<span>Added in: v0.1.90</span> 2376</div> 2377<ul> 2378<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> 2379<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>. 2380<strong>Default:</strong> <code>0</code>.</li> 2381<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 2382exceed <code>buf.length - offset</code>). <strong>Default:</strong> <code>buf.length - offset</code>.</li> 2383<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> 2384<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> 2385</ul> 2386<p>Writes <code>string</code> to <code>buf</code> at <code>offset</code> according to the character encoding in 2387<code>encoding</code>. The <code>length</code> parameter is the number of bytes to write. If <code>buf</code> did 2388not contain enough space to fit the entire string, only part of <code>string</code> will be 2389written. However, partially encoded characters will not be written.</p> 2390<pre><code class="language-js"><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>); 2391 2392<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>); 2393 2394<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>); 2395<span class="hljs-comment">// Prints: 12 bytes: ½ + ¼ = ¾</span> 2396 2397<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>); 2398 2399<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>); 2400 2401<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>); 2402<span class="hljs-comment">// Prints: 2 bytes : ab</span></code></pre> 2403<h4><code>buf.writeBigInt64BE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writebigint64be_value_offset" id="buffer_buf_writebigint64be_value_offset">#</a></span></h4> 2404<div class="api_metadata"> 2405<span>Added in: v12.0.0, v10.20.0</span> 2406</div> 2407<ul> 2408<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> 2409<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 2410satisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 2411<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> 2412</ul> 2413<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian.</p> 2414<p><code>value</code> is interpreted and written as a two's complement signed integer.</p> 2415<pre><code class="language-js"><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>); 2416 2417buf.<span class="hljs-title function_">writeBigInt64BE</span>(<span class="hljs-number">0x0102030405060708n</span>, <span class="hljs-number">0</span>); 2418 2419<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 2420<span class="hljs-comment">// Prints: <Buffer 01 02 03 04 05 06 07 08></span></code></pre> 2421<h4><code>buf.writeBigInt64LE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writebigint64le_value_offset" id="buffer_buf_writebigint64le_value_offset">#</a></span></h4> 2422<div class="api_metadata"> 2423<span>Added in: v12.0.0, v10.20.0</span> 2424</div> 2425<ul> 2426<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> 2427<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 2428satisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 2429<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> 2430</ul> 2431<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian.</p> 2432<p><code>value</code> is interpreted and written as a two's complement signed integer.</p> 2433<pre><code class="language-js"><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>); 2434 2435buf.<span class="hljs-title function_">writeBigInt64LE</span>(<span class="hljs-number">0x0102030405060708n</span>, <span class="hljs-number">0</span>); 2436 2437<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 2438<span class="hljs-comment">// Prints: <Buffer 08 07 06 05 04 03 02 01></span></code></pre> 2439<h4><code>buf.writeBigUInt64BE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writebiguint64be_value_offset" id="buffer_buf_writebiguint64be_value_offset">#</a></span></h4> 2440<div class="api_metadata"> 2441<details class="changelog"><summary>History</summary> 2442<table> 2443<tbody><tr><th>Version</th><th>Changes</th></tr> 2444<tr><td>v14.10.0</td> 2445<td><p>This function is also available as <code>buf.writeBigUint64BE()</code>.</p></td></tr> 2446<tr><td>v12.0.0, v10.20.0</td> 2447<td><p><span>Added in: v12.0.0, v10.20.0</span></p></td></tr> 2448</tbody></table> 2449</details> 2450</div> 2451<ul> 2452<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> 2453<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 2454satisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 2455<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> 2456</ul> 2457<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian.</p> 2458<p>This function is also available under the <code>writeBigUint64BE</code> alias.</p> 2459<pre><code class="language-js"><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>); 2460 2461buf.<span class="hljs-title function_">writeBigUInt64BE</span>(<span class="hljs-number">0xdecafafecacefaden</span>, <span class="hljs-number">0</span>); 2462 2463<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 2464<span class="hljs-comment">// Prints: <Buffer de ca fa fe ca ce fa de></span></code></pre> 2465<h4><code>buf.writeBigUInt64LE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writebiguint64le_value_offset" id="buffer_buf_writebiguint64le_value_offset">#</a></span></h4> 2466<div class="api_metadata"> 2467<details class="changelog"><summary>History</summary> 2468<table> 2469<tbody><tr><th>Version</th><th>Changes</th></tr> 2470<tr><td>v14.10.0</td> 2471<td><p>This function is also available as <code>buf.writeBigUint64LE()</code>.</p></td></tr> 2472<tr><td>v12.0.0, v10.20.0</td> 2473<td><p><span>Added in: v12.0.0, v10.20.0</span></p></td></tr> 2474</tbody></table> 2475</details> 2476</div> 2477<ul> 2478<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> 2479<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 2480satisfy: <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 2481<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> 2482</ul> 2483<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian</p> 2484<pre><code class="language-js"><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>); 2485 2486buf.<span class="hljs-title function_">writeBigUInt64LE</span>(<span class="hljs-number">0xdecafafecacefaden</span>, <span class="hljs-number">0</span>); 2487 2488<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 2489<span class="hljs-comment">// Prints: <Buffer de fa ce ca fe fa ca de></span></code></pre> 2490<p>This function is also available under the <code>writeBigUint64LE</code> alias.</p> 2491<h4><code>buf.writeDoubleBE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writedoublebe_value_offset" id="buffer_buf_writedoublebe_value_offset">#</a></span></h4> 2492<div class="api_metadata"> 2493<details class="changelog"><summary>History</summary> 2494<table> 2495<tbody><tr><th>Version</th><th>Changes</th></tr> 2496<tr><td>v10.0.0</td> 2497<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 2498<tr><td>v0.11.15</td> 2499<td><p><span>Added in: v0.11.15</span></p></td></tr> 2500</tbody></table> 2501</details> 2502</div> 2503<ul> 2504<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> 2505<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 2506satisfy <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 2507<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> 2508</ul> 2509<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code> 2510must be a JavaScript number. Behavior is undefined when <code>value</code> is anything 2511other than a JavaScript number.</p> 2512<pre><code class="language-js"><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>); 2513 2514buf.<span class="hljs-title function_">writeDoubleBE</span>(<span class="hljs-number">123.456</span>, <span class="hljs-number">0</span>); 2515 2516<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 2517<span class="hljs-comment">// Prints: <Buffer 40 5e dd 2f 1a 9f be 77></span></code></pre> 2518<h4><code>buf.writeDoubleLE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writedoublele_value_offset" id="buffer_buf_writedoublele_value_offset">#</a></span></h4> 2519<div class="api_metadata"> 2520<details class="changelog"><summary>History</summary> 2521<table> 2522<tbody><tr><th>Version</th><th>Changes</th></tr> 2523<tr><td>v10.0.0</td> 2524<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 2525<tr><td>v0.11.15</td> 2526<td><p><span>Added in: v0.11.15</span></p></td></tr> 2527</tbody></table> 2528</details> 2529</div> 2530<ul> 2531<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> 2532<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 2533satisfy <code>0 <= offset <= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li> 2534<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> 2535</ul> 2536<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code> 2537must be a JavaScript number. Behavior is undefined when <code>value</code> is anything 2538other than a JavaScript number.</p> 2539<pre><code class="language-js"><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>); 2540 2541buf.<span class="hljs-title function_">writeDoubleLE</span>(<span class="hljs-number">123.456</span>, <span class="hljs-number">0</span>); 2542 2543<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 2544<span class="hljs-comment">// Prints: <Buffer 77 be 9f 1a 2f dd 5e 40></span></code></pre> 2545<h4><code>buf.writeFloatBE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writefloatbe_value_offset" id="buffer_buf_writefloatbe_value_offset">#</a></span></h4> 2546<div class="api_metadata"> 2547<details class="changelog"><summary>History</summary> 2548<table> 2549<tbody><tr><th>Version</th><th>Changes</th></tr> 2550<tr><td>v10.0.0</td> 2551<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 2552<tr><td>v0.11.15</td> 2553<td><p><span>Added in: v0.11.15</span></p></td></tr> 2554</tbody></table> 2555</details> 2556</div> 2557<ul> 2558<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> 2559<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 2560satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 2561<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> 2562</ul> 2563<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. Behavior is 2564undefined when <code>value</code> is anything other than a JavaScript number.</p> 2565<pre><code class="language-js"><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>); 2566 2567buf.<span class="hljs-title function_">writeFloatBE</span>(<span class="hljs-number">0xcafebabe</span>, <span class="hljs-number">0</span>); 2568 2569<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 2570<span class="hljs-comment">// Prints: <Buffer 4f 4a fe bb></span></code></pre> 2571<h4><code>buf.writeFloatLE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writefloatle_value_offset" id="buffer_buf_writefloatle_value_offset">#</a></span></h4> 2572<div class="api_metadata"> 2573<details class="changelog"><summary>History</summary> 2574<table> 2575<tbody><tr><th>Version</th><th>Changes</th></tr> 2576<tr><td>v10.0.0</td> 2577<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 2578<tr><td>v0.11.15</td> 2579<td><p><span>Added in: v0.11.15</span></p></td></tr> 2580</tbody></table> 2581</details> 2582</div> 2583<ul> 2584<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> 2585<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 2586satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 2587<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> 2588</ul> 2589<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. Behavior is 2590undefined when <code>value</code> is anything other than a JavaScript number.</p> 2591<pre><code class="language-js"><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>); 2592 2593buf.<span class="hljs-title function_">writeFloatLE</span>(<span class="hljs-number">0xcafebabe</span>, <span class="hljs-number">0</span>); 2594 2595<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 2596<span class="hljs-comment">// Prints: <Buffer bb fe 4a 4f></span></code></pre> 2597<h4><code>buf.writeInt8(value[, offset])</code><span><a class="mark" href="#buffer_buf_writeint8_value_offset" id="buffer_buf_writeint8_value_offset">#</a></span></h4> 2598<div class="api_metadata"> 2599<details class="changelog"><summary>History</summary> 2600<table> 2601<tbody><tr><th>Version</th><th>Changes</th></tr> 2602<tr><td>v10.0.0</td> 2603<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 2604<tr><td>v0.5.0</td> 2605<td><p><span>Added in: v0.5.0</span></p></td></tr> 2606</tbody></table> 2607</details> 2608</div> 2609<ul> 2610<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> 2611<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 2612satisfy <code>0 <= offset <= buf.length - 1</code>. <strong>Default:</strong> <code>0</code>.</li> 2613<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> 2614</ul> 2615<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code>. <code>value</code> must be a valid 2616signed 8-bit integer. Behavior is undefined when <code>value</code> is anything other than 2617a signed 8-bit integer.</p> 2618<p><code>value</code> is interpreted and written as a two's complement signed integer.</p> 2619<pre><code class="language-js"><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>); 2620 2621buf.<span class="hljs-title function_">writeInt8</span>(<span class="hljs-number">2</span>, <span class="hljs-number">0</span>); 2622buf.<span class="hljs-title function_">writeInt8</span>(-<span class="hljs-number">2</span>, <span class="hljs-number">1</span>); 2623 2624<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 2625<span class="hljs-comment">// Prints: <Buffer 02 fe></span></code></pre> 2626<h4><code>buf.writeInt16BE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writeint16be_value_offset" id="buffer_buf_writeint16be_value_offset">#</a></span></h4> 2627<div class="api_metadata"> 2628<details class="changelog"><summary>History</summary> 2629<table> 2630<tbody><tr><th>Version</th><th>Changes</th></tr> 2631<tr><td>v10.0.0</td> 2632<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 2633<tr><td>v0.5.5</td> 2634<td><p><span>Added in: v0.5.5</span></p></td></tr> 2635</tbody></table> 2636</details> 2637</div> 2638<ul> 2639<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> 2640<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 2641satisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> 2642<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> 2643</ul> 2644<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code> 2645must be a valid signed 16-bit integer. Behavior is undefined when <code>value</code> is 2646anything other than a signed 16-bit integer.</p> 2647<p>The <code>value</code> is interpreted and written as a two's complement signed integer.</p> 2648<pre><code class="language-js"><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>); 2649 2650buf.<span class="hljs-title function_">writeInt16BE</span>(<span class="hljs-number">0x0102</span>, <span class="hljs-number">0</span>); 2651 2652<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 2653<span class="hljs-comment">// Prints: <Buffer 01 02></span></code></pre> 2654<h4><code>buf.writeInt16LE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writeint16le_value_offset" id="buffer_buf_writeint16le_value_offset">#</a></span></h4> 2655<div class="api_metadata"> 2656<details class="changelog"><summary>History</summary> 2657<table> 2658<tbody><tr><th>Version</th><th>Changes</th></tr> 2659<tr><td>v10.0.0</td> 2660<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 2661<tr><td>v0.5.5</td> 2662<td><p><span>Added in: v0.5.5</span></p></td></tr> 2663</tbody></table> 2664</details> 2665</div> 2666<ul> 2667<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> 2668<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 2669satisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> 2670<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> 2671</ul> 2672<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code> 2673must be a valid signed 16-bit integer. Behavior is undefined when <code>value</code> is 2674anything other than a signed 16-bit integer.</p> 2675<p>The <code>value</code> is interpreted and written as a two's complement signed integer.</p> 2676<pre><code class="language-js"><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>); 2677 2678buf.<span class="hljs-title function_">writeInt16LE</span>(<span class="hljs-number">0x0304</span>, <span class="hljs-number">0</span>); 2679 2680<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 2681<span class="hljs-comment">// Prints: <Buffer 04 03></span></code></pre> 2682<h4><code>buf.writeInt32BE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writeint32be_value_offset" id="buffer_buf_writeint32be_value_offset">#</a></span></h4> 2683<div class="api_metadata"> 2684<details class="changelog"><summary>History</summary> 2685<table> 2686<tbody><tr><th>Version</th><th>Changes</th></tr> 2687<tr><td>v10.0.0</td> 2688<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 2689<tr><td>v0.5.5</td> 2690<td><p><span>Added in: v0.5.5</span></p></td></tr> 2691</tbody></table> 2692</details> 2693</div> 2694<ul> 2695<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> 2696<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 2697satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 2698<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> 2699</ul> 2700<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code> 2701must be a valid signed 32-bit integer. Behavior is undefined when <code>value</code> is 2702anything other than a signed 32-bit integer.</p> 2703<p>The <code>value</code> is interpreted and written as a two's complement signed integer.</p> 2704<pre><code class="language-js"><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>); 2705 2706buf.<span class="hljs-title function_">writeInt32BE</span>(<span class="hljs-number">0x01020304</span>, <span class="hljs-number">0</span>); 2707 2708<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 2709<span class="hljs-comment">// Prints: <Buffer 01 02 03 04></span></code></pre> 2710<h4><code>buf.writeInt32LE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writeint32le_value_offset" id="buffer_buf_writeint32le_value_offset">#</a></span></h4> 2711<div class="api_metadata"> 2712<details class="changelog"><summary>History</summary> 2713<table> 2714<tbody><tr><th>Version</th><th>Changes</th></tr> 2715<tr><td>v10.0.0</td> 2716<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 2717<tr><td>v0.5.5</td> 2718<td><p><span>Added in: v0.5.5</span></p></td></tr> 2719</tbody></table> 2720</details> 2721</div> 2722<ul> 2723<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> 2724<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 2725satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 2726<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> 2727</ul> 2728<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code> 2729must be a valid signed 32-bit integer. Behavior is undefined when <code>value</code> is 2730anything other than a signed 32-bit integer.</p> 2731<p>The <code>value</code> is interpreted and written as a two's complement signed integer.</p> 2732<pre><code class="language-js"><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>); 2733 2734buf.<span class="hljs-title function_">writeInt32LE</span>(<span class="hljs-number">0x05060708</span>, <span class="hljs-number">0</span>); 2735 2736<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 2737<span class="hljs-comment">// Prints: <Buffer 08 07 06 05></span></code></pre> 2738<h4><code>buf.writeIntBE(value, offset, byteLength)</code><span><a class="mark" href="#buffer_buf_writeintbe_value_offset_bytelength" id="buffer_buf_writeintbe_value_offset_bytelength">#</a></span></h4> 2739<div class="api_metadata"> 2740<details class="changelog"><summary>History</summary> 2741<table> 2742<tbody><tr><th>Version</th><th>Changes</th></tr> 2743<tr><td>v10.0.0</td> 2744<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> 2745<tr><td>v0.11.15</td> 2746<td><p><span>Added in: v0.11.15</span></p></td></tr> 2747</tbody></table> 2748</details> 2749</div> 2750<ul> 2751<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> 2752<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 2753satisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> 2754<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 2755<code>0 < byteLength <= 6</code>.</li> 2756<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> 2757</ul> 2758<p>Writes <code>byteLength</code> bytes of <code>value</code> to <code>buf</code> at the specified <code>offset</code> 2759as big-endian. Supports up to 48 bits of accuracy. Behavior is undefined when 2760<code>value</code> is anything other than a signed integer.</p> 2761<pre><code class="language-js"><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>); 2762 2763buf.<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>); 2764 2765<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 2766<span class="hljs-comment">// Prints: <Buffer 12 34 56 78 90 ab></span></code></pre> 2767<h4><code>buf.writeIntLE(value, offset, byteLength)</code><span><a class="mark" href="#buffer_buf_writeintle_value_offset_bytelength" id="buffer_buf_writeintle_value_offset_bytelength">#</a></span></h4> 2768<div class="api_metadata"> 2769<details class="changelog"><summary>History</summary> 2770<table> 2771<tbody><tr><th>Version</th><th>Changes</th></tr> 2772<tr><td>v10.0.0</td> 2773<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> 2774<tr><td>v0.11.15</td> 2775<td><p><span>Added in: v0.11.15</span></p></td></tr> 2776</tbody></table> 2777</details> 2778</div> 2779<ul> 2780<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> 2781<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 2782satisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> 2783<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 2784<code>0 < byteLength <= 6</code>.</li> 2785<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> 2786</ul> 2787<p>Writes <code>byteLength</code> bytes of <code>value</code> to <code>buf</code> at the specified <code>offset</code> 2788as little-endian. Supports up to 48 bits of accuracy. Behavior is undefined 2789when <code>value</code> is anything other than a signed integer.</p> 2790<pre><code class="language-js"><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>); 2791 2792buf.<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>); 2793 2794<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 2795<span class="hljs-comment">// Prints: <Buffer ab 90 78 56 34 12></span></code></pre> 2796<h4><code>buf.writeUInt8(value[, offset])</code><span><a class="mark" href="#buffer_buf_writeuint8_value_offset" id="buffer_buf_writeuint8_value_offset">#</a></span></h4> 2797<div class="api_metadata"> 2798<details class="changelog"><summary>History</summary> 2799<table> 2800<tbody><tr><th>Version</th><th>Changes</th></tr> 2801<tr><td>v14.9.0</td> 2802<td><p>This function is also available as <code>buf.writeUint8()</code>.</p></td></tr> 2803<tr><td>v10.0.0</td> 2804<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 2805<tr><td>v0.5.0</td> 2806<td><p><span>Added in: v0.5.0</span></p></td></tr> 2807</tbody></table> 2808</details> 2809</div> 2810<ul> 2811<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> 2812<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 2813satisfy <code>0 <= offset <= buf.length - 1</code>. <strong>Default:</strong> <code>0</code>.</li> 2814<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> 2815</ul> 2816<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code>. <code>value</code> must be a 2817valid unsigned 8-bit integer. Behavior is undefined when <code>value</code> is anything 2818other than an unsigned 8-bit integer.</p> 2819<p>This function is also available under the <code>writeUint8</code> alias.</p> 2820<pre><code class="language-js"><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>); 2821 2822buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x3</span>, <span class="hljs-number">0</span>); 2823buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x4</span>, <span class="hljs-number">1</span>); 2824buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x23</span>, <span class="hljs-number">2</span>); 2825buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x42</span>, <span class="hljs-number">3</span>); 2826 2827<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 2828<span class="hljs-comment">// Prints: <Buffer 03 04 23 42></span></code></pre> 2829<h4><code>buf.writeUInt16BE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writeuint16be_value_offset" id="buffer_buf_writeuint16be_value_offset">#</a></span></h4> 2830<div class="api_metadata"> 2831<details class="changelog"><summary>History</summary> 2832<table> 2833<tbody><tr><th>Version</th><th>Changes</th></tr> 2834<tr><td>v14.9.0</td> 2835<td><p>This function is also available as <code>buf.writeUint16BE()</code>.</p></td></tr> 2836<tr><td>v10.0.0</td> 2837<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 2838<tr><td>v0.5.5</td> 2839<td><p><span>Added in: v0.5.5</span></p></td></tr> 2840</tbody></table> 2841</details> 2842</div> 2843<ul> 2844<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> 2845<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 2846satisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> 2847<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> 2848</ul> 2849<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code> 2850must be a valid unsigned 16-bit integer. Behavior is undefined when <code>value</code> 2851is anything other than an unsigned 16-bit integer.</p> 2852<p>This function is also available under the <code>writeUint16BE</code> alias.</p> 2853<pre><code class="language-js"><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>); 2854 2855buf.<span class="hljs-title function_">writeUInt16BE</span>(<span class="hljs-number">0xdead</span>, <span class="hljs-number">0</span>); 2856buf.<span class="hljs-title function_">writeUInt16BE</span>(<span class="hljs-number">0xbeef</span>, <span class="hljs-number">2</span>); 2857 2858<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 2859<span class="hljs-comment">// Prints: <Buffer de ad be ef></span></code></pre> 2860<h4><code>buf.writeUInt16LE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writeuint16le_value_offset" id="buffer_buf_writeuint16le_value_offset">#</a></span></h4> 2861<div class="api_metadata"> 2862<details class="changelog"><summary>History</summary> 2863<table> 2864<tbody><tr><th>Version</th><th>Changes</th></tr> 2865<tr><td>v14.9.0, v12.19.0</td> 2866<td><p>This function is also available as <code>buf.writeUint16LE()</code>.</p></td></tr> 2867<tr><td>v10.0.0</td> 2868<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 2869<tr><td>v0.5.5</td> 2870<td><p><span>Added in: v0.5.5</span></p></td></tr> 2871</tbody></table> 2872</details> 2873</div> 2874<ul> 2875<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> 2876<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 2877satisfy <code>0 <= offset <= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li> 2878<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> 2879</ul> 2880<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code> 2881must be a valid unsigned 16-bit integer. Behavior is undefined when <code>value</code> is 2882anything other than an unsigned 16-bit integer.</p> 2883<p>This function is also available under the <code>writeUint16LE</code> alias.</p> 2884<pre><code class="language-js"><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>); 2885 2886buf.<span class="hljs-title function_">writeUInt16LE</span>(<span class="hljs-number">0xdead</span>, <span class="hljs-number">0</span>); 2887buf.<span class="hljs-title function_">writeUInt16LE</span>(<span class="hljs-number">0xbeef</span>, <span class="hljs-number">2</span>); 2888 2889<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 2890<span class="hljs-comment">// Prints: <Buffer ad de ef be></span></code></pre> 2891<h4><code>buf.writeUInt32BE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writeuint32be_value_offset" id="buffer_buf_writeuint32be_value_offset">#</a></span></h4> 2892<div class="api_metadata"> 2893<details class="changelog"><summary>History</summary> 2894<table> 2895<tbody><tr><th>Version</th><th>Changes</th></tr> 2896<tr><td>v14.9.0</td> 2897<td><p>This function is also available as <code>buf.writeUint32BE()</code>.</p></td></tr> 2898<tr><td>v10.0.0</td> 2899<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 2900<tr><td>v0.5.5</td> 2901<td><p><span>Added in: v0.5.5</span></p></td></tr> 2902</tbody></table> 2903</details> 2904</div> 2905<ul> 2906<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> 2907<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 2908satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 2909<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> 2910</ul> 2911<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code> 2912must be a valid unsigned 32-bit integer. Behavior is undefined when <code>value</code> 2913is anything other than an unsigned 32-bit integer.</p> 2914<p>This function is also available under the <code>writeUint32BE</code> alias.</p> 2915<pre><code class="language-js"><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>); 2916 2917buf.<span class="hljs-title function_">writeUInt32BE</span>(<span class="hljs-number">0xfeedface</span>, <span class="hljs-number">0</span>); 2918 2919<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 2920<span class="hljs-comment">// Prints: <Buffer fe ed fa ce></span></code></pre> 2921<h4><code>buf.writeUInt32LE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writeuint32le_value_offset" id="buffer_buf_writeuint32le_value_offset">#</a></span></h4> 2922<div class="api_metadata"> 2923<details class="changelog"><summary>History</summary> 2924<table> 2925<tbody><tr><th>Version</th><th>Changes</th></tr> 2926<tr><td>v14.9.0</td> 2927<td><p>This function is also available as <code>buf.writeUint32LE()</code>.</p></td></tr> 2928<tr><td>v10.0.0</td> 2929<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr> 2930<tr><td>v0.5.5</td> 2931<td><p><span>Added in: v0.5.5</span></p></td></tr> 2932</tbody></table> 2933</details> 2934</div> 2935<ul> 2936<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> 2937<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 2938satisfy <code>0 <= offset <= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li> 2939<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> 2940</ul> 2941<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code> 2942must be a valid unsigned 32-bit integer. Behavior is undefined when <code>value</code> is 2943anything other than an unsigned 32-bit integer.</p> 2944<p>This function is also available under the <code>writeUint32LE</code> alias.</p> 2945<pre><code class="language-js"><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>); 2946 2947buf.<span class="hljs-title function_">writeUInt32LE</span>(<span class="hljs-number">0xfeedface</span>, <span class="hljs-number">0</span>); 2948 2949<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 2950<span class="hljs-comment">// Prints: <Buffer ce fa ed fe></span></code></pre> 2951<h4><code>buf.writeUIntBE(value, offset, byteLength)</code><span><a class="mark" href="#buffer_buf_writeuintbe_value_offset_bytelength" id="buffer_buf_writeuintbe_value_offset_bytelength">#</a></span></h4> 2952<div class="api_metadata"> 2953<details class="changelog"><summary>History</summary> 2954<table> 2955<tbody><tr><th>Version</th><th>Changes</th></tr> 2956<tr><td>v14.9.0</td> 2957<td><p>This function is also available as <code>buf.writeUintBE()</code>.</p></td></tr> 2958<tr><td>v10.0.0</td> 2959<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> 2960<tr><td>v0.5.5</td> 2961<td><p><span>Added in: v0.5.5</span></p></td></tr> 2962</tbody></table> 2963</details> 2964</div> 2965<ul> 2966<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> 2967<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 2968satisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> 2969<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 2970<code>0 < byteLength <= 6</code>.</li> 2971<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> 2972</ul> 2973<p>Writes <code>byteLength</code> bytes of <code>value</code> to <code>buf</code> at the specified <code>offset</code> 2974as big-endian. Supports up to 48 bits of accuracy. Behavior is undefined 2975when <code>value</code> is anything other than an unsigned integer.</p> 2976<p>This function is also available under the <code>writeUintBE</code> alias.</p> 2977<pre><code class="language-js"><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>); 2978 2979buf.<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>); 2980 2981<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 2982<span class="hljs-comment">// Prints: <Buffer 12 34 56 78 90 ab></span></code></pre> 2983<h4><code>buf.writeUIntLE(value, offset, byteLength)</code><span><a class="mark" href="#buffer_buf_writeuintle_value_offset_bytelength" id="buffer_buf_writeuintle_value_offset_bytelength">#</a></span></h4> 2984<div class="api_metadata"> 2985<details class="changelog"><summary>History</summary> 2986<table> 2987<tbody><tr><th>Version</th><th>Changes</th></tr> 2988<tr><td>v14.9.0</td> 2989<td><p>This function is also available as <code>buf.writeUintLE()</code>.</p></td></tr> 2990<tr><td>v10.0.0</td> 2991<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> 2992<tr><td>v0.5.5</td> 2993<td><p><span>Added in: v0.5.5</span></p></td></tr> 2994</tbody></table> 2995</details> 2996</div> 2997<ul> 2998<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> 2999<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 3000satisfy <code>0 <= offset <= buf.length - byteLength</code>.</li> 3001<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 3002<code>0 < byteLength <= 6</code>.</li> 3003<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> 3004</ul> 3005<p>Writes <code>byteLength</code> bytes of <code>value</code> to <code>buf</code> at the specified <code>offset</code> 3006as little-endian. Supports up to 48 bits of accuracy. Behavior is undefined 3007when <code>value</code> is anything other than an unsigned integer.</p> 3008<p>This function is also available under the <code>writeUintLE</code> alias.</p> 3009<pre><code class="language-js"><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>); 3010 3011buf.<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>); 3012 3013<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf); 3014<span class="hljs-comment">// Prints: <Buffer ab 90 78 56 34 12></span></code></pre> 3015<h4><code>new Buffer(array)</code><span><a class="mark" href="#buffer_new_buffer_array" id="buffer_new_buffer_array">#</a></span></h4> 3016<div class="api_metadata"> 3017<details class="changelog"><summary>History</summary> 3018<table> 3019<tbody><tr><th>Version</th><th>Changes</th></tr> 3020<tr><td>v10.0.0</td> 3021<td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr> 3022<tr><td>v7.2.1</td> 3023<td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr> 3024<tr><td>v7.0.0</td> 3025<td><p>Calling this constructor emits a deprecation warning now.</p></td></tr> 3026<tr><td>v6.0.0</td> 3027<td><p><span>Deprecated since: v6.0.0</span></p></td></tr> 3028</tbody></table> 3029</details> 3030</div> 3031<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#buffer_static_method_buffer_from_array"><code>Buffer.from(array)</code></a> instead.</div><p></p> 3032<ul> 3033<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> 3034</ul> 3035<p>See <a href="#buffer_static_method_buffer_from_array"><code>Buffer.from(array)</code></a>.</p> 3036<h4><code>new Buffer(arrayBuffer[, byteOffset[, length]])</code><span><a class="mark" href="#buffer_new_buffer_arraybuffer_byteoffset_length" id="buffer_new_buffer_arraybuffer_byteoffset_length">#</a></span></h4> 3037<div class="api_metadata"> 3038<details class="changelog"><summary>History</summary> 3039<table> 3040<tbody><tr><th>Version</th><th>Changes</th></tr> 3041<tr><td>v10.0.0</td> 3042<td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr> 3043<tr><td>v7.2.1</td> 3044<td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr> 3045<tr><td>v7.0.0</td> 3046<td><p>Calling this constructor emits a deprecation warning now.</p></td></tr> 3047<tr><td>v6.0.0</td> 3048<td><p>The <code>byteOffset</code> and <code>length</code> parameters are supported now.</p></td></tr> 3049<tr><td>v6.0.0</td> 3050<td><p><span>Deprecated since: v6.0.0</span></p></td></tr> 3051<tr><td>v3.0.0</td> 3052<td><p><span>Added in: v3.0.0</span></p></td></tr> 3053</tbody></table> 3054</details> 3055</div> 3056<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use 3057<a href="#buffer_static_method_buffer_from_arraybuffer_byteoffset_length"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a> 3058instead.</div><p></p> 3059<ul> 3060<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>, 3061<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> 3062<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> 3063<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. 3064<strong>Default:</strong> <code>arrayBuffer.byteLength - byteOffset</code>.</li> 3065</ul> 3066<p>See 3067<a href="#buffer_static_method_buffer_from_arraybuffer_byteoffset_length"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a>.</p> 3068<h4><code>new Buffer(buffer)</code><span><a class="mark" href="#buffer_new_buffer_buffer" id="buffer_new_buffer_buffer">#</a></span></h4> 3069<div class="api_metadata"> 3070<details class="changelog"><summary>History</summary> 3071<table> 3072<tbody><tr><th>Version</th><th>Changes</th></tr> 3073<tr><td>v10.0.0</td> 3074<td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr> 3075<tr><td>v7.2.1</td> 3076<td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr> 3077<tr><td>v7.0.0</td> 3078<td><p>Calling this constructor emits a deprecation warning now.</p></td></tr> 3079<tr><td>v6.0.0</td> 3080<td><p><span>Deprecated since: v6.0.0</span></p></td></tr> 3081</tbody></table> 3082</details> 3083</div> 3084<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#buffer_static_method_buffer_from_buffer"><code>Buffer.from(buffer)</code></a> instead.</div><p></p> 3085<ul> 3086<li><code>buffer</code> <a href="buffer.html#buffer_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 3087which to copy data.</li> 3088</ul> 3089<p>See <a href="#buffer_static_method_buffer_from_buffer"><code>Buffer.from(buffer)</code></a>.</p> 3090<h4><code>new Buffer(size)</code><span><a class="mark" href="#buffer_new_buffer_size" id="buffer_new_buffer_size">#</a></span></h4> 3091<div class="api_metadata"> 3092<details class="changelog"><summary>History</summary> 3093<table> 3094<tbody><tr><th>Version</th><th>Changes</th></tr> 3095<tr><td>v10.0.0</td> 3096<td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr> 3097<tr><td>v8.0.0</td> 3098<td><p>The <code>new Buffer(size)</code> will return zero-filled memory by default.</p></td></tr> 3099<tr><td>v7.2.1</td> 3100<td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr> 3101<tr><td>v7.0.0</td> 3102<td><p>Calling this constructor emits a deprecation warning now.</p></td></tr> 3103<tr><td>v6.0.0</td> 3104<td><p><span>Deprecated since: v6.0.0</span></p></td></tr> 3105</tbody></table> 3106</details> 3107</div> 3108<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#buffer_static_method_buffer_alloc_size_fill_encoding"><code>Buffer.alloc()</code></a> instead (also see 3109<a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a>).</div><p></p> 3110<ul> 3111<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> 3112</ul> 3113<p>See <a href="#buffer_static_method_buffer_alloc_size_fill_encoding"><code>Buffer.alloc()</code></a> and <a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a>. This variant of the 3114constructor is equivalent to <a href="#buffer_static_method_buffer_alloc_size_fill_encoding"><code>Buffer.alloc()</code></a>.</p> 3115<h4><code>new Buffer(string[, encoding])</code><span><a class="mark" href="#buffer_new_buffer_string_encoding" id="buffer_new_buffer_string_encoding">#</a></span></h4> 3116<div class="api_metadata"> 3117<details class="changelog"><summary>History</summary> 3118<table> 3119<tbody><tr><th>Version</th><th>Changes</th></tr> 3120<tr><td>v10.0.0</td> 3121<td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr> 3122<tr><td>v7.2.1</td> 3123<td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr> 3124<tr><td>v7.0.0</td> 3125<td><p>Calling this constructor emits a deprecation warning now.</p></td></tr> 3126<tr><td>v6.0.0</td> 3127<td><p><span>Deprecated since: v6.0.0</span></p></td></tr> 3128</tbody></table> 3129</details> 3130</div> 3131<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: 3132Use <a href="#buffer_static_method_buffer_from_string_encoding"><code>Buffer.from(string[, encoding])</code></a> instead.</div><p></p> 3133<ul> 3134<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> 3135<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> 3136</ul> 3137<p>See <a href="#buffer_static_method_buffer_from_string_encoding"><code>Buffer.from(string[, encoding])</code></a>.</p> 3138</section><section><h3><code>buffer</code> module APIs<span><a class="mark" href="#buffer_buffer_module_apis" id="buffer_buffer_module_apis">#</a></span></h3> 3139<p>While, the <code>Buffer</code> object is available as a global, there are additional 3140<code>Buffer</code>-related APIs that are available only via the <code>buffer</code> module 3141accessed using <code>require('buffer')</code>.</p> 3142<h4><code>buffer.atob(data)</code><span><a class="mark" href="#buffer_buffer_atob_data" id="buffer_buffer_atob_data">#</a></span></h4> 3143<div class="api_metadata"> 3144<span>Added in: v14.17.0</span> 3145</div> 3146<ul> 3147<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> 3148</ul> 3149<p>Decodes a string of Base64-encoded data into bytes, and encodes those bytes 3150into a string using Latin-1 (ISO-8859-1).</p> 3151<p>The <code>data</code> may be any JavaScript-value that can be coerced into a string.</p> 3152<p><strong>This function is only provided for compatibility with legacy web platform APIs 3153and should never be used in new code, because they use strings to represent 3154binary data and predate the introduction of typed arrays in JavaScript. 3155For code running using Node.js APIs, converting between base64-encoded strings 3156and binary data should be performed using <code>Buffer.from(str, 'base64')</code> and 3157<code>buf.toString('base64')</code>.</strong></p> 3158<h4><code>buffer.btoa(data)</code><span><a class="mark" href="#buffer_buffer_btoa_data" id="buffer_buffer_btoa_data">#</a></span></h4> 3159<div class="api_metadata"> 3160<span>Added in: v14.17.0</span> 3161</div> 3162<ul> 3163<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> 3164</ul> 3165<p>Decodes a string into bytes using Latin-1 (ISO-8859), and encodes those bytes 3166into a string using Base64.</p> 3167<p>The <code>data</code> may be any JavaScript-value that can be coerced into a string.</p> 3168<p><strong>This function is only provided for compatibility with legacy web platform APIs 3169and should never be used in new code, because they use strings to represent 3170binary data and predate the introduction of typed arrays in JavaScript. 3171For code running using Node.js APIs, converting between base64-encoded strings 3172and binary data should be performed using <code>Buffer.from(str, 'base64')</code> and 3173<code>buf.toString('base64')</code>.</strong></p> 3174<h4><code>buffer.INSPECT_MAX_BYTES</code><span><a class="mark" href="#buffer_buffer_inspect_max_bytes" id="buffer_buffer_inspect_max_bytes">#</a></span></h4> 3175<div class="api_metadata"> 3176<span>Added in: v0.5.4</span> 3177</div> 3178<ul> 3179<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> 3180</ul> 3181<p>Returns the maximum number of bytes that will be returned when 3182<code>buf.inspect()</code> is called. This can be overridden by user modules. See 3183<a href="util.html#util_util_inspect_object_options"><code>util.inspect()</code></a> for more details on <code>buf.inspect()</code> behavior.</p> 3184<h4><code>buffer.kMaxLength</code><span><a class="mark" href="#buffer_buffer_kmaxlength" id="buffer_buffer_kmaxlength">#</a></span></h4> 3185<div class="api_metadata"> 3186<span>Added in: v3.0.0</span> 3187</div> 3188<ul> 3189<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> 3190</ul> 3191<p>An alias for <a href="#buffer_buffer_constants_max_length"><code>buffer.constants.MAX_LENGTH</code></a>.</p> 3192<h4><code>buffer.kStringMaxLength</code><span><a class="mark" href="#buffer_buffer_kstringmaxlength" id="buffer_buffer_kstringmaxlength">#</a></span></h4> 3193<div class="api_metadata"> 3194<span>Added in: v3.0.0</span> 3195</div> 3196<ul> 3197<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> 3198</ul> 3199<p>An alias for <a href="#buffer_buffer_constants_max_string_length"><code>buffer.constants.MAX_STRING_LENGTH</code></a>.</p> 3200<h4><code>buffer.transcode(source, fromEnc, toEnc)</code><span><a class="mark" href="#buffer_buffer_transcode_source_fromenc_toenc" id="buffer_buffer_transcode_source_fromenc_toenc">#</a></span></h4> 3201<div class="api_metadata"> 3202<details class="changelog"><summary>History</summary> 3203<table> 3204<tbody><tr><th>Version</th><th>Changes</th></tr> 3205<tr><td>v8.0.0</td> 3206<td><p>The <code>source</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 3207<tr><td>v7.1.0</td> 3208<td><p><span>Added in: v7.1.0</span></p></td></tr> 3209</tbody></table> 3210</details> 3211</div> 3212<ul> 3213<li><code>source</code> <a href="buffer.html#buffer_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> 3214<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> 3215<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> 3216<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> 3217</ul> 3218<p>Re-encodes the given <code>Buffer</code> or <code>Uint8Array</code> instance from one character 3219encoding to another. Returns a new <code>Buffer</code> instance.</p> 3220<p>Throws if the <code>fromEnc</code> or <code>toEnc</code> specify invalid character encodings or if 3221conversion from <code>fromEnc</code> to <code>toEnc</code> is not permitted.</p> 3222<p>Encodings supported by <code>buffer.transcode()</code> are: <code>'ascii'</code>, <code>'utf8'</code>, 3223<code>'utf16le'</code>, <code>'ucs2'</code>, <code>'latin1'</code>, and <code>'binary'</code>.</p> 3224<p>The transcoding process will use substitution characters if a given byte 3225sequence cannot be adequately represented in the target encoding. For instance:</p> 3226<pre><code class="language-js"><span class="hljs-keyword">const</span> buffer = <span class="hljs-built_in">require</span>(<span class="hljs-string">'buffer'</span>); 3227 3228<span class="hljs-keyword">const</span> newBuf = buffer.<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>); 3229<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>)); 3230<span class="hljs-comment">// Prints: '?'</span></code></pre> 3231<p>Because the Euro (<code>€</code>) sign is not representable in US-ASCII, it is replaced 3232with <code>?</code> in the transcoded <code>Buffer</code>.</p> 3233<h4>Class: <code>SlowBuffer</code><span><a class="mark" href="#buffer_class_slowbuffer" id="buffer_class_slowbuffer">#</a></span></h4> 3234<div class="api_metadata"> 3235<span>Deprecated since: v6.0.0</span> 3236</div> 3237<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#buffer_static_method_buffer_allocunsafeslow_size"><code>Buffer.allocUnsafeSlow()</code></a> instead.</div><p></p> 3238<p>See <a href="#buffer_static_method_buffer_allocunsafeslow_size"><code>Buffer.allocUnsafeSlow()</code></a>. This was never a class in the sense that 3239the constructor always returned a <code>Buffer</code> instance, rather than a <code>SlowBuffer</code> 3240instance.</p> 3241<h5><code>new SlowBuffer(size)</code><span><a class="mark" href="#buffer_new_slowbuffer_size" id="buffer_new_slowbuffer_size">#</a></span></h5> 3242<div class="api_metadata"> 3243<span>Deprecated since: v6.0.0</span> 3244</div> 3245<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#buffer_static_method_buffer_allocunsafeslow_size"><code>Buffer.allocUnsafeSlow()</code></a> instead.</div><p></p> 3246<ul> 3247<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> 3248</ul> 3249<p>See <a href="#buffer_static_method_buffer_allocunsafeslow_size"><code>Buffer.allocUnsafeSlow()</code></a>.</p> 3250<h4>Buffer constants<span><a class="mark" href="#buffer_buffer_constants" id="buffer_buffer_constants">#</a></span></h4> 3251<div class="api_metadata"> 3252<span>Added in: v8.2.0</span> 3253</div> 3254<h5><code>buffer.constants.MAX_LENGTH</code><span><a class="mark" href="#buffer_buffer_constants_max_length" id="buffer_buffer_constants_max_length">#</a></span></h5> 3255<div class="api_metadata"> 3256<details class="changelog"><summary>History</summary> 3257<table> 3258<tbody><tr><th>Version</th><th>Changes</th></tr> 3259<tr><td>v14.0.0</td> 3260<td><p>Value is changed from 2<sup>31</sup> - 1 to 2<sup>32</sup> - 1 on 64-bit architectures.</p></td></tr> 3261<tr><td>v8.2.0</td> 3262<td><p><span>Added in: v8.2.0</span></p></td></tr> 3263</tbody></table> 3264</details> 3265</div> 3266<ul> 3267<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> 3268</ul> 3269<p>On 32-bit architectures, this value currently is 2<sup>30</sup> - 1 (~1GB).</p> 3270<p>On 64-bit architectures, this value currently is 2<sup>32</sup> - 1 (~4GB).</p> 3271<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> 3272<p>This value is also available as <a href="#buffer_buffer_kmaxlength"><code>buffer.kMaxLength</code></a>.</p> 3273<h5><code>buffer.constants.MAX_STRING_LENGTH</code><span><a class="mark" href="#buffer_buffer_constants_max_string_length" id="buffer_buffer_constants_max_string_length">#</a></span></h5> 3274<div class="api_metadata"> 3275<span>Added in: v8.2.0</span> 3276</div> 3277<ul> 3278<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> 3279</ul> 3280<p>Represents the largest <code>length</code> that a <code>string</code> primitive can have, counted 3281in UTF-16 code units.</p> 3282<p>This value may depend on the JS engine that is being used.</p> 3283</section><section><h3><code>Buffer.from()</code>, <code>Buffer.alloc()</code>, and <code>Buffer.allocUnsafe()</code><span><a class="mark" href="#buffer_buffer_from_buffer_alloc_and_buffer_allocunsafe" id="buffer_buffer_from_buffer_alloc_and_buffer_allocunsafe">#</a></span></h3> 3284<p>In versions of Node.js prior to 6.0.0, <code>Buffer</code> instances were created using the 3285<code>Buffer</code> constructor function, which allocates the returned <code>Buffer</code> 3286differently based on what arguments are provided:</p> 3287<ul> 3288<li>Passing a number as the first argument to <code>Buffer()</code> (e.g. <code>new Buffer(10)</code>) 3289allocates a new <code>Buffer</code> object of the specified size. Prior to Node.js 8.0.0, 3290the memory allocated for such <code>Buffer</code> instances is <em>not</em> initialized and 3291<em>can contain sensitive data</em>. Such <code>Buffer</code> instances <em>must</em> be subsequently 3292initialized by using either <a href="#buffer_buf_fill_value_offset_end_encoding"><code>buf.fill(0)</code></a> or by writing to the 3293entire <code>Buffer</code> before reading data from the <code>Buffer</code>. 3294While this behavior is <em>intentional</em> to improve performance, 3295development experience has demonstrated that a more explicit distinction is 3296required between creating a fast-but-uninitialized <code>Buffer</code> versus creating a 3297slower-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> 3298<li>Passing a string, array, or <code>Buffer</code> as the first argument copies the 3299passed object's data into the <code>Buffer</code>.</li> 3300<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> 3301that shares allocated memory with the given array buffer.</li> 3302</ul> 3303<p>Because the behavior of <code>new Buffer()</code> is different depending on the type of the 3304first argument, security and reliability issues can be inadvertently introduced 3305into applications when argument validation or <code>Buffer</code> initialization is not 3306performed.</p> 3307<p>For example, if an attacker can cause an application to receive a number where 3308a string is expected, the application may call <code>new Buffer(100)</code> 3309instead of <code>new Buffer("100")</code>, leading it to allocate a 100 byte buffer instead 3310of allocating a 3 byte buffer with content <code>"100"</code>. This is commonly possible 3311using JSON API calls. Since JSON distinguishes between numeric and string types, 3312it allows injection of numbers where a naively written application that does not 3313validate its input sufficiently might expect to always receive a string. 3314Before Node.js 8.0.0, the 100 byte buffer might contain 3315arbitrary pre-existing in-memory data, so may be used to expose in-memory 3316secrets to a remote attacker. Since Node.js 8.0.0, exposure of memory cannot 3317occur because the data is zero-filled. However, other attacks are still 3318possible, such as causing very large buffers to be allocated by the server, 3319leading to performance degradation or crashing on memory exhaustion.</p> 3320<p>To make the creation of <code>Buffer</code> instances more reliable and less error-prone, 3321the various forms of the <code>new Buffer()</code> constructor have been <strong>deprecated</strong> 3322and replaced by separate <code>Buffer.from()</code>, <a href="#buffer_static_method_buffer_alloc_size_fill_encoding"><code>Buffer.alloc()</code></a>, and 3323<a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a> methods.</p> 3324<p><em>Developers should migrate all existing uses of the <code>new Buffer()</code> constructors 3325to one of these new APIs.</em></p> 3326<ul> 3327<li><a href="#buffer_static_method_buffer_from_array"><code>Buffer.from(array)</code></a> returns a new <code>Buffer</code> that <em>contains a copy</em> of the 3328provided octets.</li> 3329<li><a href="#buffer_static_method_buffer_from_arraybuffer_byteoffset_length"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a> 3330returns a new <code>Buffer</code> that <em>shares the same allocated memory</em> as the given 3331<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a>.</li> 3332<li><a href="#buffer_static_method_buffer_from_buffer"><code>Buffer.from(buffer)</code></a> returns a new <code>Buffer</code> that <em>contains a copy</em> of the 3333contents of the given <code>Buffer</code>.</li> 3334<li><a href="#buffer_static_method_buffer_from_string_encoding"><code>Buffer.from(string[, encoding])</code></a> returns a new 3335<code>Buffer</code> that <em>contains a copy</em> of the provided string.</li> 3336<li><a href="#buffer_static_method_buffer_alloc_size_fill_encoding"><code>Buffer.alloc(size[, fill[, encoding]])</code></a> returns a new 3337initialized <code>Buffer</code> of the specified size. This method is slower than 3338<a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe(size)</code></a> but guarantees that newly 3339created <code>Buffer</code> instances never contain old data that is potentially 3340sensitive. A <code>TypeError</code> will be thrown if <code>size</code> is not a number.</li> 3341<li><a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe(size)</code></a> and 3342<a href="#buffer_static_method_buffer_allocunsafeslow_size"><code>Buffer.allocUnsafeSlow(size)</code></a> each return a 3343new uninitialized <code>Buffer</code> of the specified <code>size</code>. Because the <code>Buffer</code> is 3344uninitialized, the allocated segment of memory might contain old data that is 3345potentially sensitive.</li> 3346</ul> 3347<p><code>Buffer</code> instances returned by <a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a> and 3348<a href="#buffer_static_method_buffer_from_array"><code>Buffer.from(array)</code></a> <em>may</em> be allocated off a shared internal memory pool 3349if <code>size</code> is less than or equal to half <a href="#buffer_class_property_buffer_poolsize"><code>Buffer.poolSize</code></a>. Instances 3350returned by <a href="#buffer_static_method_buffer_allocunsafeslow_size"><code>Buffer.allocUnsafeSlow()</code></a> <em>never</em> use the shared internal 3351memory pool.</p> 3352<h4>The <code>--zero-fill-buffers</code> command-line option<span><a class="mark" href="#buffer_the_zero_fill_buffers_command_line_option" id="buffer_the_zero_fill_buffers_command_line_option">#</a></span></h4> 3353<div class="api_metadata"> 3354<span>Added in: v5.10.0</span> 3355</div> 3356<p>Node.js can be started using the <code>--zero-fill-buffers</code> command-line option to 3357cause all newly-allocated <code>Buffer</code> instances to be zero-filled upon creation by 3358default. Without the option, buffers created with <a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a>, 3359<a href="#buffer_static_method_buffer_allocunsafeslow_size"><code>Buffer.allocUnsafeSlow()</code></a>, and <code>new SlowBuffer(size)</code> are not zero-filled. 3360Use of this flag can have a measurable negative impact on performance. Use the 3361<code>--zero-fill-buffers</code> option only when necessary to enforce that newly allocated 3362<code>Buffer</code> instances cannot contain old data that is potentially sensitive.</p> 3363<pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">node --zero-fill-buffers</span> 3364<span class="hljs-meta">> </span><span class="language-bash">Buffer.allocUnsafe(5);</span> 3365<Buffer 00 00 00 00 00></code></pre> 3366<h4>What makes <code>Buffer.allocUnsafe()</code> and <code>Buffer.allocUnsafeSlow()</code> "unsafe"?<span><a class="mark" href="#buffer_what_makes_buffer_allocunsafe_and_buffer_allocunsafeslow_unsafe" id="buffer_what_makes_buffer_allocunsafe_and_buffer_allocunsafeslow_unsafe">#</a></span></h4> 3367<p>When calling <a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a> and <a href="#buffer_static_method_buffer_allocunsafeslow_size"><code>Buffer.allocUnsafeSlow()</code></a>, the 3368segment of allocated memory is <em>uninitialized</em> (it is not zeroed-out). While 3369this design makes the allocation of memory quite fast, the allocated segment of 3370memory might contain old data that is potentially sensitive. Using a <code>Buffer</code> 3371created by <a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a> without <em>completely</em> overwriting the 3372memory can allow this old data to be leaked when the <code>Buffer</code> memory is read.</p> 3373<p>While there are clear performance advantages to using 3374<a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a>, extra care <em>must</em> be taken in order to avoid 3375introducing security vulnerabilities into an application.</p></section> 3376 <!-- API END --> 3377 </div> 3378 </div> 3379 </div> 3380 <script> 3381 'use strict'; 3382 { 3383 const kCustomPreference = 'customDarkTheme'; 3384 const userSettings = sessionStorage.getItem(kCustomPreference); 3385 const themeToggleButton = document.getElementById('theme-toggle-btn'); 3386 if (userSettings === null && window.matchMedia) { 3387 const mq = window.matchMedia('(prefers-color-scheme: dark)'); 3388 if ('onchange' in mq) { 3389 function mqChangeListener(e) { 3390 document.body.classList.toggle('dark-mode', e.matches); 3391 } 3392 mq.addEventListener('change', mqChangeListener); 3393 if (themeToggleButton) { 3394 themeToggleButton.addEventListener('click', function() { 3395 mq.removeEventListener('change', mqChangeListener); 3396 }, { once: true }); 3397 } 3398 } 3399 if (mq.matches) { 3400 document.body.classList.add('dark-mode'); 3401 } 3402 } else if (userSettings === 'true') { 3403 document.body.classList.add('dark-mode'); 3404 } 3405 if (themeToggleButton) { 3406 themeToggleButton.hidden = false; 3407 themeToggleButton.addEventListener('click', function() { 3408 sessionStorage.setItem( 3409 kCustomPreference, 3410 document.body.classList.toggle('dark-mode') 3411 ); 3412 }); 3413 } 3414 } 3415 </script> 3416</body> 3417</html> 3418