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