11cb0ef41Sopenharmony_ci# Buffer
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci<!--introduced_in=v0.1.90-->
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci> Stability: 2 - Stable
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci<!-- source_link=lib/buffer.js -->
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci`Buffer` objects are used to represent a fixed-length sequence of bytes. Many
101cb0ef41Sopenharmony_ciNode.js APIs support `Buffer`s.
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciThe `Buffer` class is a subclass of JavaScript's [`Uint8Array`][] class and
131cb0ef41Sopenharmony_ciextends it with methods that cover additional use cases. Node.js APIs accept
141cb0ef41Sopenharmony_ciplain [`Uint8Array`][]s wherever `Buffer`s are supported as well.
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciWhile the `Buffer` class is available within the global scope, it is still
171cb0ef41Sopenharmony_cirecommended to explicitly reference it via an import or require statement.
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci```mjs
201cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci// Creates a zero-filled Buffer of length 10.
231cb0ef41Sopenharmony_ciconst buf1 = Buffer.alloc(10);
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci// Creates a Buffer of length 10,
261cb0ef41Sopenharmony_ci// filled with bytes which all have the value `1`.
271cb0ef41Sopenharmony_ciconst buf2 = Buffer.alloc(10, 1);
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci// Creates an uninitialized buffer of length 10.
301cb0ef41Sopenharmony_ci// This is faster than calling Buffer.alloc() but the returned
311cb0ef41Sopenharmony_ci// Buffer instance might contain old data that needs to be
321cb0ef41Sopenharmony_ci// overwritten using fill(), write(), or other functions that fill the Buffer's
331cb0ef41Sopenharmony_ci// contents.
341cb0ef41Sopenharmony_ciconst buf3 = Buffer.allocUnsafe(10);
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci// Creates a Buffer containing the bytes [1, 2, 3].
371cb0ef41Sopenharmony_ciconst buf4 = Buffer.from([1, 2, 3]);
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci// Creates a Buffer containing the bytes [1, 1, 1, 1] – the entries
401cb0ef41Sopenharmony_ci// are all truncated using `(value & 255)` to fit into the range 0–255.
411cb0ef41Sopenharmony_ciconst buf5 = Buffer.from([257, 257.5, -255, '1']);
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci// Creates a Buffer containing the UTF-8-encoded bytes for the string 'tést':
441cb0ef41Sopenharmony_ci// [0x74, 0xc3, 0xa9, 0x73, 0x74] (in hexadecimal notation)
451cb0ef41Sopenharmony_ci// [116, 195, 169, 115, 116] (in decimal notation)
461cb0ef41Sopenharmony_ciconst buf6 = Buffer.from('tést');
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci// Creates a Buffer containing the Latin-1 bytes [0x74, 0xe9, 0x73, 0x74].
491cb0ef41Sopenharmony_ciconst buf7 = Buffer.from('tést', 'latin1');
501cb0ef41Sopenharmony_ci```
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci```cjs
531cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci// Creates a zero-filled Buffer of length 10.
561cb0ef41Sopenharmony_ciconst buf1 = Buffer.alloc(10);
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci// Creates a Buffer of length 10,
591cb0ef41Sopenharmony_ci// filled with bytes which all have the value `1`.
601cb0ef41Sopenharmony_ciconst buf2 = Buffer.alloc(10, 1);
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci// Creates an uninitialized buffer of length 10.
631cb0ef41Sopenharmony_ci// This is faster than calling Buffer.alloc() but the returned
641cb0ef41Sopenharmony_ci// Buffer instance might contain old data that needs to be
651cb0ef41Sopenharmony_ci// overwritten using fill(), write(), or other functions that fill the Buffer's
661cb0ef41Sopenharmony_ci// contents.
671cb0ef41Sopenharmony_ciconst buf3 = Buffer.allocUnsafe(10);
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci// Creates a Buffer containing the bytes [1, 2, 3].
701cb0ef41Sopenharmony_ciconst buf4 = Buffer.from([1, 2, 3]);
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci// Creates a Buffer containing the bytes [1, 1, 1, 1] – the entries
731cb0ef41Sopenharmony_ci// are all truncated using `(value & 255)` to fit into the range 0–255.
741cb0ef41Sopenharmony_ciconst buf5 = Buffer.from([257, 257.5, -255, '1']);
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci// Creates a Buffer containing the UTF-8-encoded bytes for the string 'tést':
771cb0ef41Sopenharmony_ci// [0x74, 0xc3, 0xa9, 0x73, 0x74] (in hexadecimal notation)
781cb0ef41Sopenharmony_ci// [116, 195, 169, 115, 116] (in decimal notation)
791cb0ef41Sopenharmony_ciconst buf6 = Buffer.from('tést');
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci// Creates a Buffer containing the Latin-1 bytes [0x74, 0xe9, 0x73, 0x74].
821cb0ef41Sopenharmony_ciconst buf7 = Buffer.from('tést', 'latin1');
831cb0ef41Sopenharmony_ci```
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci## Buffers and character encodings
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci<!-- YAML
881cb0ef41Sopenharmony_cichanges:
891cb0ef41Sopenharmony_ci  - version:
901cb0ef41Sopenharmony_ci      - v15.7.0
911cb0ef41Sopenharmony_ci      - v14.18.0
921cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/36952
931cb0ef41Sopenharmony_ci    description: Introduced `base64url` encoding.
941cb0ef41Sopenharmony_ci  - version: v6.4.0
951cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/7111
961cb0ef41Sopenharmony_ci    description: Introduced `latin1` as an alias for `binary`.
971cb0ef41Sopenharmony_ci  - version: v5.0.0
981cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/2859
991cb0ef41Sopenharmony_ci    description: Removed the deprecated `raw` and `raws` encodings.
1001cb0ef41Sopenharmony_ci-->
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ciWhen converting between `Buffer`s and strings, a character encoding may be
1031cb0ef41Sopenharmony_cispecified. If no character encoding is specified, UTF-8 will be used as the
1041cb0ef41Sopenharmony_cidefault.
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci```mjs
1071cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ciconst buf = Buffer.from('hello world', 'utf8');
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ciconsole.log(buf.toString('hex'));
1121cb0ef41Sopenharmony_ci// Prints: 68656c6c6f20776f726c64
1131cb0ef41Sopenharmony_ciconsole.log(buf.toString('base64'));
1141cb0ef41Sopenharmony_ci// Prints: aGVsbG8gd29ybGQ=
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ciconsole.log(Buffer.from('fhqwhgads', 'utf8'));
1171cb0ef41Sopenharmony_ci// Prints: <Buffer 66 68 71 77 68 67 61 64 73>
1181cb0ef41Sopenharmony_ciconsole.log(Buffer.from('fhqwhgads', 'utf16le'));
1191cb0ef41Sopenharmony_ci// Prints: <Buffer 66 00 68 00 71 00 77 00 68 00 67 00 61 00 64 00 73 00>
1201cb0ef41Sopenharmony_ci```
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci```cjs
1231cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ciconst buf = Buffer.from('hello world', 'utf8');
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ciconsole.log(buf.toString('hex'));
1281cb0ef41Sopenharmony_ci// Prints: 68656c6c6f20776f726c64
1291cb0ef41Sopenharmony_ciconsole.log(buf.toString('base64'));
1301cb0ef41Sopenharmony_ci// Prints: aGVsbG8gd29ybGQ=
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ciconsole.log(Buffer.from('fhqwhgads', 'utf8'));
1331cb0ef41Sopenharmony_ci// Prints: <Buffer 66 68 71 77 68 67 61 64 73>
1341cb0ef41Sopenharmony_ciconsole.log(Buffer.from('fhqwhgads', 'utf16le'));
1351cb0ef41Sopenharmony_ci// Prints: <Buffer 66 00 68 00 71 00 77 00 68 00 67 00 61 00 64 00 73 00>
1361cb0ef41Sopenharmony_ci```
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ciNode.js buffers accept all case variations of encoding strings that they
1391cb0ef41Sopenharmony_cireceive. For example, UTF-8 can be specified as `'utf8'`, `'UTF8'`, or `'uTf8'`.
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ciThe character encodings currently supported by Node.js are the following:
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_ci* `'utf8'` (alias: `'utf-8'`): Multi-byte encoded Unicode characters. Many web
1441cb0ef41Sopenharmony_ci  pages and other document formats use [UTF-8][]. This is the default character
1451cb0ef41Sopenharmony_ci  encoding. When decoding a `Buffer` into a string that does not exclusively
1461cb0ef41Sopenharmony_ci  contain valid UTF-8 data, the Unicode replacement character `U+FFFD` � will be
1471cb0ef41Sopenharmony_ci  used to represent those errors.
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ci* `'utf16le'` (alias: `'utf-16le'`): Multi-byte encoded Unicode characters.
1501cb0ef41Sopenharmony_ci  Unlike `'utf8'`, each character in the string will be encoded using either 2
1511cb0ef41Sopenharmony_ci  or 4 bytes. Node.js only supports the [little-endian][endianness] variant of
1521cb0ef41Sopenharmony_ci  [UTF-16][].
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_ci* `'latin1'`: Latin-1 stands for [ISO-8859-1][]. This character encoding only
1551cb0ef41Sopenharmony_ci  supports the Unicode characters from `U+0000` to `U+00FF`. Each character is
1561cb0ef41Sopenharmony_ci  encoded using a single byte. Characters that do not fit into that range are
1571cb0ef41Sopenharmony_ci  truncated and will be mapped to characters in that range.
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ciConverting a `Buffer` into a string using one of the above is referred to as
1601cb0ef41Sopenharmony_cidecoding, and converting a string into a `Buffer` is referred to as encoding.
1611cb0ef41Sopenharmony_ci
1621cb0ef41Sopenharmony_ciNode.js also supports the following binary-to-text encodings. For
1631cb0ef41Sopenharmony_cibinary-to-text encodings, the naming convention is reversed: Converting a
1641cb0ef41Sopenharmony_ci`Buffer` into a string is typically referred to as encoding, and converting a
1651cb0ef41Sopenharmony_cistring into a `Buffer` as decoding.
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_ci* `'base64'`: [Base64][] encoding. When creating a `Buffer` from a string,
1681cb0ef41Sopenharmony_ci  this encoding will also correctly accept "URL and Filename Safe Alphabet" as
1691cb0ef41Sopenharmony_ci  specified in [RFC 4648, Section 5][]. Whitespace characters such as spaces,
1701cb0ef41Sopenharmony_ci  tabs, and new lines contained within the base64-encoded string are ignored.
1711cb0ef41Sopenharmony_ci
1721cb0ef41Sopenharmony_ci* `'base64url'`: [base64url][] encoding as specified in
1731cb0ef41Sopenharmony_ci  [RFC 4648, Section 5][]. When creating a `Buffer` from a string, this
1741cb0ef41Sopenharmony_ci  encoding will also correctly accept regular base64-encoded strings. When
1751cb0ef41Sopenharmony_ci  encoding a `Buffer` to a string, this encoding will omit padding.
1761cb0ef41Sopenharmony_ci
1771cb0ef41Sopenharmony_ci* `'hex'`: Encode each byte as two hexadecimal characters. Data truncation
1781cb0ef41Sopenharmony_ci  may occur when decoding strings that do not exclusively consist of an even
1791cb0ef41Sopenharmony_ci  number of hexadecimal characters. See below for an example.
1801cb0ef41Sopenharmony_ci
1811cb0ef41Sopenharmony_ciThe following legacy character encodings are also supported:
1821cb0ef41Sopenharmony_ci
1831cb0ef41Sopenharmony_ci* `'ascii'`: For 7-bit [ASCII][] data only. When encoding a string into a
1841cb0ef41Sopenharmony_ci  `Buffer`, this is equivalent to using `'latin1'`. When decoding a `Buffer`
1851cb0ef41Sopenharmony_ci  into a string, using this encoding will additionally unset the highest bit of
1861cb0ef41Sopenharmony_ci  each byte before decoding as `'latin1'`.
1871cb0ef41Sopenharmony_ci  Generally, there should be no reason to use this encoding, as `'utf8'`
1881cb0ef41Sopenharmony_ci  (or, if the data is known to always be ASCII-only, `'latin1'`) will be a
1891cb0ef41Sopenharmony_ci  better choice when encoding or decoding ASCII-only text. It is only provided
1901cb0ef41Sopenharmony_ci  for legacy compatibility.
1911cb0ef41Sopenharmony_ci
1921cb0ef41Sopenharmony_ci* `'binary'`: Alias for `'latin1'`.
1931cb0ef41Sopenharmony_ci  The name of this encoding can be very misleading, as all of the
1941cb0ef41Sopenharmony_ci  encodings listed here convert between strings and binary data. For converting
1951cb0ef41Sopenharmony_ci  between strings and `Buffer`s, typically `'utf8'` is the right choice.
1961cb0ef41Sopenharmony_ci
1971cb0ef41Sopenharmony_ci* `'ucs2'`, `'ucs-2'`: Aliases of `'utf16le'`. UCS-2 used to refer to a variant
1981cb0ef41Sopenharmony_ci  of UTF-16 that did not support characters that had code points larger than
1991cb0ef41Sopenharmony_ci  U+FFFF. In Node.js, these code points are always supported.
2001cb0ef41Sopenharmony_ci
2011cb0ef41Sopenharmony_ci```mjs
2021cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
2031cb0ef41Sopenharmony_ci
2041cb0ef41Sopenharmony_ciBuffer.from('1ag123', 'hex');
2051cb0ef41Sopenharmony_ci// Prints <Buffer 1a>, data truncated when first non-hexadecimal value
2061cb0ef41Sopenharmony_ci// ('g') encountered.
2071cb0ef41Sopenharmony_ci
2081cb0ef41Sopenharmony_ciBuffer.from('1a7', 'hex');
2091cb0ef41Sopenharmony_ci// Prints <Buffer 1a>, data truncated when data ends in single digit ('7').
2101cb0ef41Sopenharmony_ci
2111cb0ef41Sopenharmony_ciBuffer.from('1634', 'hex');
2121cb0ef41Sopenharmony_ci// Prints <Buffer 16 34>, all data represented.
2131cb0ef41Sopenharmony_ci```
2141cb0ef41Sopenharmony_ci
2151cb0ef41Sopenharmony_ci```cjs
2161cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
2171cb0ef41Sopenharmony_ci
2181cb0ef41Sopenharmony_ciBuffer.from('1ag123', 'hex');
2191cb0ef41Sopenharmony_ci// Prints <Buffer 1a>, data truncated when first non-hexadecimal value
2201cb0ef41Sopenharmony_ci// ('g') encountered.
2211cb0ef41Sopenharmony_ci
2221cb0ef41Sopenharmony_ciBuffer.from('1a7', 'hex');
2231cb0ef41Sopenharmony_ci// Prints <Buffer 1a>, data truncated when data ends in single digit ('7').
2241cb0ef41Sopenharmony_ci
2251cb0ef41Sopenharmony_ciBuffer.from('1634', 'hex');
2261cb0ef41Sopenharmony_ci// Prints <Buffer 16 34>, all data represented.
2271cb0ef41Sopenharmony_ci```
2281cb0ef41Sopenharmony_ci
2291cb0ef41Sopenharmony_ciModern Web browsers follow the [WHATWG Encoding Standard][] which aliases
2301cb0ef41Sopenharmony_ciboth `'latin1'` and `'ISO-8859-1'` to `'win-1252'`. This means that while doing
2311cb0ef41Sopenharmony_cisomething like `http.get()`, if the returned charset is one of those listed in
2321cb0ef41Sopenharmony_cithe WHATWG specification it is possible that the server actually returned
2331cb0ef41Sopenharmony_ci`'win-1252'`-encoded data, and using `'latin1'` encoding may incorrectly decode
2341cb0ef41Sopenharmony_cithe characters.
2351cb0ef41Sopenharmony_ci
2361cb0ef41Sopenharmony_ci## Buffers and TypedArrays
2371cb0ef41Sopenharmony_ci
2381cb0ef41Sopenharmony_ci<!-- YAML
2391cb0ef41Sopenharmony_cichanges:
2401cb0ef41Sopenharmony_ci  - version: v3.0.0
2411cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/2002
2421cb0ef41Sopenharmony_ci    description: The `Buffer`s class now inherits from `Uint8Array`.
2431cb0ef41Sopenharmony_ci-->
2441cb0ef41Sopenharmony_ci
2451cb0ef41Sopenharmony_ci`Buffer` instances are also JavaScript [`Uint8Array`][] and [`TypedArray`][]
2461cb0ef41Sopenharmony_ciinstances. All [`TypedArray`][] methods are available on `Buffer`s. There are,
2471cb0ef41Sopenharmony_cihowever, subtle incompatibilities between the `Buffer` API and the
2481cb0ef41Sopenharmony_ci[`TypedArray`][] API.
2491cb0ef41Sopenharmony_ci
2501cb0ef41Sopenharmony_ciIn particular:
2511cb0ef41Sopenharmony_ci
2521cb0ef41Sopenharmony_ci* While [`TypedArray.prototype.slice()`][] creates a copy of part of the `TypedArray`,
2531cb0ef41Sopenharmony_ci  [`Buffer.prototype.slice()`][`buf.slice()`] creates a view over the existing `Buffer`
2541cb0ef41Sopenharmony_ci  without copying. This behavior can be surprising, and only exists for legacy
2551cb0ef41Sopenharmony_ci  compatibility. [`TypedArray.prototype.subarray()`][] can be used to achieve
2561cb0ef41Sopenharmony_ci  the behavior of [`Buffer.prototype.slice()`][`buf.slice()`] on both `Buffer`s
2571cb0ef41Sopenharmony_ci  and other `TypedArray`s and should be preferred.
2581cb0ef41Sopenharmony_ci* [`buf.toString()`][] is incompatible with its `TypedArray` equivalent.
2591cb0ef41Sopenharmony_ci* A number of methods, e.g. [`buf.indexOf()`][], support additional arguments.
2601cb0ef41Sopenharmony_ci
2611cb0ef41Sopenharmony_ciThere are two ways to create new [`TypedArray`][] instances from a `Buffer`:
2621cb0ef41Sopenharmony_ci
2631cb0ef41Sopenharmony_ci* Passing a `Buffer` to a [`TypedArray`][] constructor will copy the `Buffer`s
2641cb0ef41Sopenharmony_ci  contents, interpreted as an array of integers, and not as a byte sequence
2651cb0ef41Sopenharmony_ci  of the target type.
2661cb0ef41Sopenharmony_ci
2671cb0ef41Sopenharmony_ci```mjs
2681cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
2691cb0ef41Sopenharmony_ci
2701cb0ef41Sopenharmony_ciconst buf = Buffer.from([1, 2, 3, 4]);
2711cb0ef41Sopenharmony_ciconst uint32array = new Uint32Array(buf);
2721cb0ef41Sopenharmony_ci
2731cb0ef41Sopenharmony_ciconsole.log(uint32array);
2741cb0ef41Sopenharmony_ci
2751cb0ef41Sopenharmony_ci// Prints: Uint32Array(4) [ 1, 2, 3, 4 ]
2761cb0ef41Sopenharmony_ci```
2771cb0ef41Sopenharmony_ci
2781cb0ef41Sopenharmony_ci```cjs
2791cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
2801cb0ef41Sopenharmony_ci
2811cb0ef41Sopenharmony_ciconst buf = Buffer.from([1, 2, 3, 4]);
2821cb0ef41Sopenharmony_ciconst uint32array = new Uint32Array(buf);
2831cb0ef41Sopenharmony_ci
2841cb0ef41Sopenharmony_ciconsole.log(uint32array);
2851cb0ef41Sopenharmony_ci
2861cb0ef41Sopenharmony_ci// Prints: Uint32Array(4) [ 1, 2, 3, 4 ]
2871cb0ef41Sopenharmony_ci```
2881cb0ef41Sopenharmony_ci
2891cb0ef41Sopenharmony_ci* Passing the `Buffer`s underlying [`ArrayBuffer`][] will create a
2901cb0ef41Sopenharmony_ci  [`TypedArray`][] that shares its memory with the `Buffer`.
2911cb0ef41Sopenharmony_ci
2921cb0ef41Sopenharmony_ci```mjs
2931cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
2941cb0ef41Sopenharmony_ci
2951cb0ef41Sopenharmony_ciconst buf = Buffer.from('hello', 'utf16le');
2961cb0ef41Sopenharmony_ciconst uint16array = new Uint16Array(
2971cb0ef41Sopenharmony_ci  buf.buffer,
2981cb0ef41Sopenharmony_ci  buf.byteOffset,
2991cb0ef41Sopenharmony_ci  buf.length / Uint16Array.BYTES_PER_ELEMENT);
3001cb0ef41Sopenharmony_ci
3011cb0ef41Sopenharmony_ciconsole.log(uint16array);
3021cb0ef41Sopenharmony_ci
3031cb0ef41Sopenharmony_ci// Prints: Uint16Array(5) [ 104, 101, 108, 108, 111 ]
3041cb0ef41Sopenharmony_ci```
3051cb0ef41Sopenharmony_ci
3061cb0ef41Sopenharmony_ci```cjs
3071cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
3081cb0ef41Sopenharmony_ci
3091cb0ef41Sopenharmony_ciconst buf = Buffer.from('hello', 'utf16le');
3101cb0ef41Sopenharmony_ciconst uint16array = new Uint16Array(
3111cb0ef41Sopenharmony_ci  buf.buffer,
3121cb0ef41Sopenharmony_ci  buf.byteOffset,
3131cb0ef41Sopenharmony_ci  buf.length / Uint16Array.BYTES_PER_ELEMENT);
3141cb0ef41Sopenharmony_ci
3151cb0ef41Sopenharmony_ciconsole.log(uint16array);
3161cb0ef41Sopenharmony_ci
3171cb0ef41Sopenharmony_ci// Prints: Uint16Array(5) [ 104, 101, 108, 108, 111 ]
3181cb0ef41Sopenharmony_ci```
3191cb0ef41Sopenharmony_ci
3201cb0ef41Sopenharmony_ciIt is possible to create a new `Buffer` that shares the same allocated
3211cb0ef41Sopenharmony_cimemory as a [`TypedArray`][] instance by using the `TypedArray` object's
3221cb0ef41Sopenharmony_ci`.buffer` property in the same way. [`Buffer.from()`][`Buffer.from(arrayBuf)`]
3231cb0ef41Sopenharmony_cibehaves like `new Uint8Array()` in this context.
3241cb0ef41Sopenharmony_ci
3251cb0ef41Sopenharmony_ci```mjs
3261cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
3271cb0ef41Sopenharmony_ci
3281cb0ef41Sopenharmony_ciconst arr = new Uint16Array(2);
3291cb0ef41Sopenharmony_ci
3301cb0ef41Sopenharmony_ciarr[0] = 5000;
3311cb0ef41Sopenharmony_ciarr[1] = 4000;
3321cb0ef41Sopenharmony_ci
3331cb0ef41Sopenharmony_ci// Copies the contents of `arr`.
3341cb0ef41Sopenharmony_ciconst buf1 = Buffer.from(arr);
3351cb0ef41Sopenharmony_ci
3361cb0ef41Sopenharmony_ci// Shares memory with `arr`.
3371cb0ef41Sopenharmony_ciconst buf2 = Buffer.from(arr.buffer);
3381cb0ef41Sopenharmony_ci
3391cb0ef41Sopenharmony_ciconsole.log(buf1);
3401cb0ef41Sopenharmony_ci// Prints: <Buffer 88 a0>
3411cb0ef41Sopenharmony_ciconsole.log(buf2);
3421cb0ef41Sopenharmony_ci// Prints: <Buffer 88 13 a0 0f>
3431cb0ef41Sopenharmony_ci
3441cb0ef41Sopenharmony_ciarr[1] = 6000;
3451cb0ef41Sopenharmony_ci
3461cb0ef41Sopenharmony_ciconsole.log(buf1);
3471cb0ef41Sopenharmony_ci// Prints: <Buffer 88 a0>
3481cb0ef41Sopenharmony_ciconsole.log(buf2);
3491cb0ef41Sopenharmony_ci// Prints: <Buffer 88 13 70 17>
3501cb0ef41Sopenharmony_ci```
3511cb0ef41Sopenharmony_ci
3521cb0ef41Sopenharmony_ci```cjs
3531cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
3541cb0ef41Sopenharmony_ci
3551cb0ef41Sopenharmony_ciconst arr = new Uint16Array(2);
3561cb0ef41Sopenharmony_ci
3571cb0ef41Sopenharmony_ciarr[0] = 5000;
3581cb0ef41Sopenharmony_ciarr[1] = 4000;
3591cb0ef41Sopenharmony_ci
3601cb0ef41Sopenharmony_ci// Copies the contents of `arr`.
3611cb0ef41Sopenharmony_ciconst buf1 = Buffer.from(arr);
3621cb0ef41Sopenharmony_ci
3631cb0ef41Sopenharmony_ci// Shares memory with `arr`.
3641cb0ef41Sopenharmony_ciconst buf2 = Buffer.from(arr.buffer);
3651cb0ef41Sopenharmony_ci
3661cb0ef41Sopenharmony_ciconsole.log(buf1);
3671cb0ef41Sopenharmony_ci// Prints: <Buffer 88 a0>
3681cb0ef41Sopenharmony_ciconsole.log(buf2);
3691cb0ef41Sopenharmony_ci// Prints: <Buffer 88 13 a0 0f>
3701cb0ef41Sopenharmony_ci
3711cb0ef41Sopenharmony_ciarr[1] = 6000;
3721cb0ef41Sopenharmony_ci
3731cb0ef41Sopenharmony_ciconsole.log(buf1);
3741cb0ef41Sopenharmony_ci// Prints: <Buffer 88 a0>
3751cb0ef41Sopenharmony_ciconsole.log(buf2);
3761cb0ef41Sopenharmony_ci// Prints: <Buffer 88 13 70 17>
3771cb0ef41Sopenharmony_ci```
3781cb0ef41Sopenharmony_ci
3791cb0ef41Sopenharmony_ciWhen creating a `Buffer` using a [`TypedArray`][]'s `.buffer`, it is
3801cb0ef41Sopenharmony_cipossible to use only a portion of the underlying [`ArrayBuffer`][] by passing in
3811cb0ef41Sopenharmony_ci`byteOffset` and `length` parameters.
3821cb0ef41Sopenharmony_ci
3831cb0ef41Sopenharmony_ci```mjs
3841cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
3851cb0ef41Sopenharmony_ci
3861cb0ef41Sopenharmony_ciconst arr = new Uint16Array(20);
3871cb0ef41Sopenharmony_ciconst buf = Buffer.from(arr.buffer, 0, 16);
3881cb0ef41Sopenharmony_ci
3891cb0ef41Sopenharmony_ciconsole.log(buf.length);
3901cb0ef41Sopenharmony_ci// Prints: 16
3911cb0ef41Sopenharmony_ci```
3921cb0ef41Sopenharmony_ci
3931cb0ef41Sopenharmony_ci```cjs
3941cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
3951cb0ef41Sopenharmony_ci
3961cb0ef41Sopenharmony_ciconst arr = new Uint16Array(20);
3971cb0ef41Sopenharmony_ciconst buf = Buffer.from(arr.buffer, 0, 16);
3981cb0ef41Sopenharmony_ci
3991cb0ef41Sopenharmony_ciconsole.log(buf.length);
4001cb0ef41Sopenharmony_ci// Prints: 16
4011cb0ef41Sopenharmony_ci```
4021cb0ef41Sopenharmony_ci
4031cb0ef41Sopenharmony_ciThe `Buffer.from()` and [`TypedArray.from()`][] have different signatures and
4041cb0ef41Sopenharmony_ciimplementations. Specifically, the [`TypedArray`][] variants accept a second
4051cb0ef41Sopenharmony_ciargument that is a mapping function that is invoked on every element of the
4061cb0ef41Sopenharmony_cityped array:
4071cb0ef41Sopenharmony_ci
4081cb0ef41Sopenharmony_ci* `TypedArray.from(source[, mapFn[, thisArg]])`
4091cb0ef41Sopenharmony_ci
4101cb0ef41Sopenharmony_ciThe `Buffer.from()` method, however, does not support the use of a mapping
4111cb0ef41Sopenharmony_cifunction:
4121cb0ef41Sopenharmony_ci
4131cb0ef41Sopenharmony_ci* [`Buffer.from(array)`][]
4141cb0ef41Sopenharmony_ci* [`Buffer.from(buffer)`][]
4151cb0ef41Sopenharmony_ci* [`Buffer.from(arrayBuffer[, byteOffset[, length]])`][`Buffer.from(arrayBuf)`]
4161cb0ef41Sopenharmony_ci* [`Buffer.from(string[, encoding])`][`Buffer.from(string)`]
4171cb0ef41Sopenharmony_ci
4181cb0ef41Sopenharmony_ci## Buffers and iteration
4191cb0ef41Sopenharmony_ci
4201cb0ef41Sopenharmony_ci`Buffer` instances can be iterated over using `for..of` syntax:
4211cb0ef41Sopenharmony_ci
4221cb0ef41Sopenharmony_ci```mjs
4231cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
4241cb0ef41Sopenharmony_ci
4251cb0ef41Sopenharmony_ciconst buf = Buffer.from([1, 2, 3]);
4261cb0ef41Sopenharmony_ci
4271cb0ef41Sopenharmony_cifor (const b of buf) {
4281cb0ef41Sopenharmony_ci  console.log(b);
4291cb0ef41Sopenharmony_ci}
4301cb0ef41Sopenharmony_ci// Prints:
4311cb0ef41Sopenharmony_ci//   1
4321cb0ef41Sopenharmony_ci//   2
4331cb0ef41Sopenharmony_ci//   3
4341cb0ef41Sopenharmony_ci```
4351cb0ef41Sopenharmony_ci
4361cb0ef41Sopenharmony_ci```cjs
4371cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
4381cb0ef41Sopenharmony_ci
4391cb0ef41Sopenharmony_ciconst buf = Buffer.from([1, 2, 3]);
4401cb0ef41Sopenharmony_ci
4411cb0ef41Sopenharmony_cifor (const b of buf) {
4421cb0ef41Sopenharmony_ci  console.log(b);
4431cb0ef41Sopenharmony_ci}
4441cb0ef41Sopenharmony_ci// Prints:
4451cb0ef41Sopenharmony_ci//   1
4461cb0ef41Sopenharmony_ci//   2
4471cb0ef41Sopenharmony_ci//   3
4481cb0ef41Sopenharmony_ci```
4491cb0ef41Sopenharmony_ci
4501cb0ef41Sopenharmony_ciAdditionally, the [`buf.values()`][], [`buf.keys()`][], and
4511cb0ef41Sopenharmony_ci[`buf.entries()`][] methods can be used to create iterators.
4521cb0ef41Sopenharmony_ci
4531cb0ef41Sopenharmony_ci## Class: `Blob`
4541cb0ef41Sopenharmony_ci
4551cb0ef41Sopenharmony_ci<!-- YAML
4561cb0ef41Sopenharmony_ciadded:
4571cb0ef41Sopenharmony_ci  - v15.7.0
4581cb0ef41Sopenharmony_ci  - v14.18.0
4591cb0ef41Sopenharmony_cichanges:
4601cb0ef41Sopenharmony_ci  - version: v18.0.0
4611cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/41270
4621cb0ef41Sopenharmony_ci    description: No longer experimental.
4631cb0ef41Sopenharmony_ci-->
4641cb0ef41Sopenharmony_ci
4651cb0ef41Sopenharmony_ciA [`Blob`][] encapsulates immutable, raw data that can be safely shared across
4661cb0ef41Sopenharmony_cimultiple worker threads.
4671cb0ef41Sopenharmony_ci
4681cb0ef41Sopenharmony_ci### `new buffer.Blob([sources[, options]])`
4691cb0ef41Sopenharmony_ci
4701cb0ef41Sopenharmony_ci<!-- YAML
4711cb0ef41Sopenharmony_ciadded:
4721cb0ef41Sopenharmony_ci  - v15.7.0
4731cb0ef41Sopenharmony_ci  - v14.18.0
4741cb0ef41Sopenharmony_cichanges:
4751cb0ef41Sopenharmony_ci  - version: v16.7.0
4761cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/39708
4771cb0ef41Sopenharmony_ci    description: Added the standard `endings` option to replace line-endings,
4781cb0ef41Sopenharmony_ci                 and removed the non-standard `encoding` option.
4791cb0ef41Sopenharmony_ci-->
4801cb0ef41Sopenharmony_ci
4811cb0ef41Sopenharmony_ci* `sources` {string\[]|ArrayBuffer\[]|TypedArray\[]|DataView\[]|Blob\[]} An
4821cb0ef41Sopenharmony_ci  array of string, {ArrayBuffer}, {TypedArray}, {DataView}, or {Blob} objects,
4831cb0ef41Sopenharmony_ci  or any mix of such objects, that will be stored within the `Blob`.
4841cb0ef41Sopenharmony_ci* `options` {Object}
4851cb0ef41Sopenharmony_ci  * `endings` {string} One of either `'transparent'` or `'native'`. When set
4861cb0ef41Sopenharmony_ci    to `'native'`, line endings in string source parts will be converted to
4871cb0ef41Sopenharmony_ci    the platform native line-ending as specified by `require('node:os').EOL`.
4881cb0ef41Sopenharmony_ci  * `type` {string} The Blob content-type. The intent is for `type` to convey
4891cb0ef41Sopenharmony_ci    the MIME media type of the data, however no validation of the type format
4901cb0ef41Sopenharmony_ci    is performed.
4911cb0ef41Sopenharmony_ci
4921cb0ef41Sopenharmony_ciCreates a new `Blob` object containing a concatenation of the given sources.
4931cb0ef41Sopenharmony_ci
4941cb0ef41Sopenharmony_ci{ArrayBuffer}, {TypedArray}, {DataView}, and {Buffer} sources are copied into
4951cb0ef41Sopenharmony_cithe 'Blob' and can therefore be safely modified after the 'Blob' is created.
4961cb0ef41Sopenharmony_ci
4971cb0ef41Sopenharmony_ciString sources are encoded as UTF-8 byte sequences and copied into the Blob.
4981cb0ef41Sopenharmony_ciUnmatched surrogate pairs within each string part will be replaced by Unicode
4991cb0ef41Sopenharmony_ciU+FFFD replacement characters.
5001cb0ef41Sopenharmony_ci
5011cb0ef41Sopenharmony_ci### `blob.arrayBuffer()`
5021cb0ef41Sopenharmony_ci
5031cb0ef41Sopenharmony_ci<!-- YAML
5041cb0ef41Sopenharmony_ciadded:
5051cb0ef41Sopenharmony_ci  - v15.7.0
5061cb0ef41Sopenharmony_ci  - v14.18.0
5071cb0ef41Sopenharmony_ci-->
5081cb0ef41Sopenharmony_ci
5091cb0ef41Sopenharmony_ci* Returns: {Promise}
5101cb0ef41Sopenharmony_ci
5111cb0ef41Sopenharmony_ciReturns a promise that fulfills with an {ArrayBuffer} containing a copy of
5121cb0ef41Sopenharmony_cithe `Blob` data.
5131cb0ef41Sopenharmony_ci
5141cb0ef41Sopenharmony_ci### `blob.size`
5151cb0ef41Sopenharmony_ci
5161cb0ef41Sopenharmony_ci<!-- YAML
5171cb0ef41Sopenharmony_ciadded:
5181cb0ef41Sopenharmony_ci  - v15.7.0
5191cb0ef41Sopenharmony_ci  - v14.18.0
5201cb0ef41Sopenharmony_ci-->
5211cb0ef41Sopenharmony_ci
5221cb0ef41Sopenharmony_ciThe total size of the `Blob` in bytes.
5231cb0ef41Sopenharmony_ci
5241cb0ef41Sopenharmony_ci### `blob.slice([start[, end[, type]]])`
5251cb0ef41Sopenharmony_ci
5261cb0ef41Sopenharmony_ci<!-- YAML
5271cb0ef41Sopenharmony_ciadded:
5281cb0ef41Sopenharmony_ci  - v15.7.0
5291cb0ef41Sopenharmony_ci  - v14.18.0
5301cb0ef41Sopenharmony_ci-->
5311cb0ef41Sopenharmony_ci
5321cb0ef41Sopenharmony_ci* `start` {number} The starting index.
5331cb0ef41Sopenharmony_ci* `end` {number} The ending index.
5341cb0ef41Sopenharmony_ci* `type` {string} The content-type for the new `Blob`
5351cb0ef41Sopenharmony_ci
5361cb0ef41Sopenharmony_ciCreates and returns a new `Blob` containing a subset of this `Blob` objects
5371cb0ef41Sopenharmony_cidata. The original `Blob` is not altered.
5381cb0ef41Sopenharmony_ci
5391cb0ef41Sopenharmony_ci### `blob.stream()`
5401cb0ef41Sopenharmony_ci
5411cb0ef41Sopenharmony_ci<!-- YAML
5421cb0ef41Sopenharmony_ciadded: v16.7.0
5431cb0ef41Sopenharmony_ci-->
5441cb0ef41Sopenharmony_ci
5451cb0ef41Sopenharmony_ci* Returns: {ReadableStream}
5461cb0ef41Sopenharmony_ci
5471cb0ef41Sopenharmony_ciReturns a new `ReadableStream` that allows the content of the `Blob` to be read.
5481cb0ef41Sopenharmony_ci
5491cb0ef41Sopenharmony_ci### `blob.text()`
5501cb0ef41Sopenharmony_ci
5511cb0ef41Sopenharmony_ci<!-- YAML
5521cb0ef41Sopenharmony_ciadded:
5531cb0ef41Sopenharmony_ci  - v15.7.0
5541cb0ef41Sopenharmony_ci  - v14.18.0
5551cb0ef41Sopenharmony_ci-->
5561cb0ef41Sopenharmony_ci
5571cb0ef41Sopenharmony_ci* Returns: {Promise}
5581cb0ef41Sopenharmony_ci
5591cb0ef41Sopenharmony_ciReturns a promise that fulfills with the contents of the `Blob` decoded as a
5601cb0ef41Sopenharmony_ciUTF-8 string.
5611cb0ef41Sopenharmony_ci
5621cb0ef41Sopenharmony_ci### `blob.type`
5631cb0ef41Sopenharmony_ci
5641cb0ef41Sopenharmony_ci<!-- YAML
5651cb0ef41Sopenharmony_ciadded:
5661cb0ef41Sopenharmony_ci  - v15.7.0
5671cb0ef41Sopenharmony_ci  - v14.18.0
5681cb0ef41Sopenharmony_ci-->
5691cb0ef41Sopenharmony_ci
5701cb0ef41Sopenharmony_ci* Type: {string}
5711cb0ef41Sopenharmony_ci
5721cb0ef41Sopenharmony_ciThe content-type of the `Blob`.
5731cb0ef41Sopenharmony_ci
5741cb0ef41Sopenharmony_ci### `Blob` objects and `MessageChannel`
5751cb0ef41Sopenharmony_ci
5761cb0ef41Sopenharmony_ciOnce a {Blob} object is created, it can be sent via `MessagePort` to multiple
5771cb0ef41Sopenharmony_cidestinations without transferring or immediately copying the data. The data
5781cb0ef41Sopenharmony_cicontained by the `Blob` is copied only when the `arrayBuffer()` or `text()`
5791cb0ef41Sopenharmony_cimethods are called.
5801cb0ef41Sopenharmony_ci
5811cb0ef41Sopenharmony_ci```mjs
5821cb0ef41Sopenharmony_ciimport { Blob } from 'node:buffer';
5831cb0ef41Sopenharmony_ciimport { setTimeout as delay } from 'node:timers/promises';
5841cb0ef41Sopenharmony_ci
5851cb0ef41Sopenharmony_ciconst blob = new Blob(['hello there']);
5861cb0ef41Sopenharmony_ci
5871cb0ef41Sopenharmony_ciconst mc1 = new MessageChannel();
5881cb0ef41Sopenharmony_ciconst mc2 = new MessageChannel();
5891cb0ef41Sopenharmony_ci
5901cb0ef41Sopenharmony_cimc1.port1.onmessage = async ({ data }) => {
5911cb0ef41Sopenharmony_ci  console.log(await data.arrayBuffer());
5921cb0ef41Sopenharmony_ci  mc1.port1.close();
5931cb0ef41Sopenharmony_ci};
5941cb0ef41Sopenharmony_ci
5951cb0ef41Sopenharmony_cimc2.port1.onmessage = async ({ data }) => {
5961cb0ef41Sopenharmony_ci  await delay(1000);
5971cb0ef41Sopenharmony_ci  console.log(await data.arrayBuffer());
5981cb0ef41Sopenharmony_ci  mc2.port1.close();
5991cb0ef41Sopenharmony_ci};
6001cb0ef41Sopenharmony_ci
6011cb0ef41Sopenharmony_cimc1.port2.postMessage(blob);
6021cb0ef41Sopenharmony_cimc2.port2.postMessage(blob);
6031cb0ef41Sopenharmony_ci
6041cb0ef41Sopenharmony_ci// The Blob is still usable after posting.
6051cb0ef41Sopenharmony_ciblob.text().then(console.log);
6061cb0ef41Sopenharmony_ci```
6071cb0ef41Sopenharmony_ci
6081cb0ef41Sopenharmony_ci```cjs
6091cb0ef41Sopenharmony_ciconst { Blob } = require('node:buffer');
6101cb0ef41Sopenharmony_ciconst { setTimeout: delay } = require('node:timers/promises');
6111cb0ef41Sopenharmony_ci
6121cb0ef41Sopenharmony_ciconst blob = new Blob(['hello there']);
6131cb0ef41Sopenharmony_ci
6141cb0ef41Sopenharmony_ciconst mc1 = new MessageChannel();
6151cb0ef41Sopenharmony_ciconst mc2 = new MessageChannel();
6161cb0ef41Sopenharmony_ci
6171cb0ef41Sopenharmony_cimc1.port1.onmessage = async ({ data }) => {
6181cb0ef41Sopenharmony_ci  console.log(await data.arrayBuffer());
6191cb0ef41Sopenharmony_ci  mc1.port1.close();
6201cb0ef41Sopenharmony_ci};
6211cb0ef41Sopenharmony_ci
6221cb0ef41Sopenharmony_cimc2.port1.onmessage = async ({ data }) => {
6231cb0ef41Sopenharmony_ci  await delay(1000);
6241cb0ef41Sopenharmony_ci  console.log(await data.arrayBuffer());
6251cb0ef41Sopenharmony_ci  mc2.port1.close();
6261cb0ef41Sopenharmony_ci};
6271cb0ef41Sopenharmony_ci
6281cb0ef41Sopenharmony_cimc1.port2.postMessage(blob);
6291cb0ef41Sopenharmony_cimc2.port2.postMessage(blob);
6301cb0ef41Sopenharmony_ci
6311cb0ef41Sopenharmony_ci// The Blob is still usable after posting.
6321cb0ef41Sopenharmony_ciblob.text().then(console.log);
6331cb0ef41Sopenharmony_ci```
6341cb0ef41Sopenharmony_ci
6351cb0ef41Sopenharmony_ci## Class: `Buffer`
6361cb0ef41Sopenharmony_ci
6371cb0ef41Sopenharmony_ciThe `Buffer` class is a global type for dealing with binary data directly.
6381cb0ef41Sopenharmony_ciIt can be constructed in a variety of ways.
6391cb0ef41Sopenharmony_ci
6401cb0ef41Sopenharmony_ci### Static method: `Buffer.alloc(size[, fill[, encoding]])`
6411cb0ef41Sopenharmony_ci
6421cb0ef41Sopenharmony_ci<!-- YAML
6431cb0ef41Sopenharmony_ciadded: v5.10.0
6441cb0ef41Sopenharmony_cichanges:
6451cb0ef41Sopenharmony_ci  - version: v20.0.0
6461cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/45796
6471cb0ef41Sopenharmony_ci    description: Throw ERR_INVALID_ARG_TYPE or ERR_OUT_OF_RANGE instead of
6481cb0ef41Sopenharmony_ci                 ERR_INVALID_ARG_VALUE for invalid input arguments.
6491cb0ef41Sopenharmony_ci  - version: v15.0.0
6501cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34682
6511cb0ef41Sopenharmony_ci    description: Throw ERR_INVALID_ARG_VALUE instead of ERR_INVALID_OPT_VALUE
6521cb0ef41Sopenharmony_ci                 for invalid input arguments.
6531cb0ef41Sopenharmony_ci  - version: v10.0.0
6541cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18129
6551cb0ef41Sopenharmony_ci    description: Attempting to fill a non-zero length buffer with a zero length
6561cb0ef41Sopenharmony_ci                 buffer triggers a thrown exception.
6571cb0ef41Sopenharmony_ci  - version: v10.0.0
6581cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/17427
6591cb0ef41Sopenharmony_ci    description: Specifying an invalid string for `fill` triggers a thrown
6601cb0ef41Sopenharmony_ci                 exception.
6611cb0ef41Sopenharmony_ci  - version: v8.9.3
6621cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/17428
6631cb0ef41Sopenharmony_ci    description: Specifying an invalid string for `fill` now results in a
6641cb0ef41Sopenharmony_ci                 zero-filled buffer.
6651cb0ef41Sopenharmony_ci-->
6661cb0ef41Sopenharmony_ci
6671cb0ef41Sopenharmony_ci* `size` {integer} The desired length of the new `Buffer`.
6681cb0ef41Sopenharmony_ci* `fill` {string|Buffer|Uint8Array|integer} A value to pre-fill the new `Buffer`
6691cb0ef41Sopenharmony_ci  with. **Default:** `0`.
6701cb0ef41Sopenharmony_ci* `encoding` {string} If `fill` is a string, this is its encoding.
6711cb0ef41Sopenharmony_ci  **Default:** `'utf8'`.
6721cb0ef41Sopenharmony_ci
6731cb0ef41Sopenharmony_ciAllocates a new `Buffer` of `size` bytes. If `fill` is `undefined`, the
6741cb0ef41Sopenharmony_ci`Buffer` will be zero-filled.
6751cb0ef41Sopenharmony_ci
6761cb0ef41Sopenharmony_ci```mjs
6771cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
6781cb0ef41Sopenharmony_ci
6791cb0ef41Sopenharmony_ciconst buf = Buffer.alloc(5);
6801cb0ef41Sopenharmony_ci
6811cb0ef41Sopenharmony_ciconsole.log(buf);
6821cb0ef41Sopenharmony_ci// Prints: <Buffer 00 00 00 00 00>
6831cb0ef41Sopenharmony_ci```
6841cb0ef41Sopenharmony_ci
6851cb0ef41Sopenharmony_ci```cjs
6861cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
6871cb0ef41Sopenharmony_ci
6881cb0ef41Sopenharmony_ciconst buf = Buffer.alloc(5);
6891cb0ef41Sopenharmony_ci
6901cb0ef41Sopenharmony_ciconsole.log(buf);
6911cb0ef41Sopenharmony_ci// Prints: <Buffer 00 00 00 00 00>
6921cb0ef41Sopenharmony_ci```
6931cb0ef41Sopenharmony_ci
6941cb0ef41Sopenharmony_ciIf `size` is larger than
6951cb0ef41Sopenharmony_ci[`buffer.constants.MAX_LENGTH`][] or smaller than 0, [`ERR_OUT_OF_RANGE`][]
6961cb0ef41Sopenharmony_ciis thrown.
6971cb0ef41Sopenharmony_ci
6981cb0ef41Sopenharmony_ciIf `fill` is specified, the allocated `Buffer` will be initialized by calling
6991cb0ef41Sopenharmony_ci[`buf.fill(fill)`][`buf.fill()`].
7001cb0ef41Sopenharmony_ci
7011cb0ef41Sopenharmony_ci```mjs
7021cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
7031cb0ef41Sopenharmony_ci
7041cb0ef41Sopenharmony_ciconst buf = Buffer.alloc(5, 'a');
7051cb0ef41Sopenharmony_ci
7061cb0ef41Sopenharmony_ciconsole.log(buf);
7071cb0ef41Sopenharmony_ci// Prints: <Buffer 61 61 61 61 61>
7081cb0ef41Sopenharmony_ci```
7091cb0ef41Sopenharmony_ci
7101cb0ef41Sopenharmony_ci```cjs
7111cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
7121cb0ef41Sopenharmony_ci
7131cb0ef41Sopenharmony_ciconst buf = Buffer.alloc(5, 'a');
7141cb0ef41Sopenharmony_ci
7151cb0ef41Sopenharmony_ciconsole.log(buf);
7161cb0ef41Sopenharmony_ci// Prints: <Buffer 61 61 61 61 61>
7171cb0ef41Sopenharmony_ci```
7181cb0ef41Sopenharmony_ci
7191cb0ef41Sopenharmony_ciIf both `fill` and `encoding` are specified, the allocated `Buffer` will be
7201cb0ef41Sopenharmony_ciinitialized by calling [`buf.fill(fill, encoding)`][`buf.fill()`].
7211cb0ef41Sopenharmony_ci
7221cb0ef41Sopenharmony_ci```mjs
7231cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
7241cb0ef41Sopenharmony_ci
7251cb0ef41Sopenharmony_ciconst buf = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=', 'base64');
7261cb0ef41Sopenharmony_ci
7271cb0ef41Sopenharmony_ciconsole.log(buf);
7281cb0ef41Sopenharmony_ci// Prints: <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>
7291cb0ef41Sopenharmony_ci```
7301cb0ef41Sopenharmony_ci
7311cb0ef41Sopenharmony_ci```cjs
7321cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
7331cb0ef41Sopenharmony_ci
7341cb0ef41Sopenharmony_ciconst buf = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=', 'base64');
7351cb0ef41Sopenharmony_ci
7361cb0ef41Sopenharmony_ciconsole.log(buf);
7371cb0ef41Sopenharmony_ci// Prints: <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>
7381cb0ef41Sopenharmony_ci```
7391cb0ef41Sopenharmony_ci
7401cb0ef41Sopenharmony_ciCalling [`Buffer.alloc()`][] can be measurably slower than the alternative
7411cb0ef41Sopenharmony_ci[`Buffer.allocUnsafe()`][] but ensures that the newly created `Buffer` instance
7421cb0ef41Sopenharmony_cicontents will never contain sensitive data from previous allocations, including
7431cb0ef41Sopenharmony_cidata that might not have been allocated for `Buffer`s.
7441cb0ef41Sopenharmony_ci
7451cb0ef41Sopenharmony_ciA `TypeError` will be thrown if `size` is not a number.
7461cb0ef41Sopenharmony_ci
7471cb0ef41Sopenharmony_ci### Static method: `Buffer.allocUnsafe(size)`
7481cb0ef41Sopenharmony_ci
7491cb0ef41Sopenharmony_ci<!-- YAML
7501cb0ef41Sopenharmony_ciadded: v5.10.0
7511cb0ef41Sopenharmony_cichanges:
7521cb0ef41Sopenharmony_ci  - version: v20.0.0
7531cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/45796
7541cb0ef41Sopenharmony_ci    description: Throw ERR_INVALID_ARG_TYPE or ERR_OUT_OF_RANGE instead of
7551cb0ef41Sopenharmony_ci                 ERR_INVALID_ARG_VALUE for invalid input arguments.
7561cb0ef41Sopenharmony_ci  - version: v15.0.0
7571cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34682
7581cb0ef41Sopenharmony_ci    description: Throw ERR_INVALID_ARG_VALUE instead of ERR_INVALID_OPT_VALUE
7591cb0ef41Sopenharmony_ci                 for invalid input arguments.
7601cb0ef41Sopenharmony_ci  - version: v7.0.0
7611cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/7079
7621cb0ef41Sopenharmony_ci    description: Passing a negative `size` will now throw an error.
7631cb0ef41Sopenharmony_ci-->
7641cb0ef41Sopenharmony_ci
7651cb0ef41Sopenharmony_ci* `size` {integer} The desired length of the new `Buffer`.
7661cb0ef41Sopenharmony_ci
7671cb0ef41Sopenharmony_ciAllocates a new `Buffer` of `size` bytes. If `size` is larger than
7681cb0ef41Sopenharmony_ci[`buffer.constants.MAX_LENGTH`][] or smaller than 0, [`ERR_OUT_OF_RANGE`][]
7691cb0ef41Sopenharmony_ciis thrown.
7701cb0ef41Sopenharmony_ci
7711cb0ef41Sopenharmony_ciThe underlying memory for `Buffer` instances created in this way is _not
7721cb0ef41Sopenharmony_ciinitialized_. The contents of the newly created `Buffer` are unknown and
7731cb0ef41Sopenharmony_ci_may contain sensitive data_. Use [`Buffer.alloc()`][] instead to initialize
7741cb0ef41Sopenharmony_ci`Buffer` instances with zeroes.
7751cb0ef41Sopenharmony_ci
7761cb0ef41Sopenharmony_ci```mjs
7771cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
7781cb0ef41Sopenharmony_ci
7791cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(10);
7801cb0ef41Sopenharmony_ci
7811cb0ef41Sopenharmony_ciconsole.log(buf);
7821cb0ef41Sopenharmony_ci// Prints (contents may vary): <Buffer a0 8b 28 3f 01 00 00 00 50 32>
7831cb0ef41Sopenharmony_ci
7841cb0ef41Sopenharmony_cibuf.fill(0);
7851cb0ef41Sopenharmony_ci
7861cb0ef41Sopenharmony_ciconsole.log(buf);
7871cb0ef41Sopenharmony_ci// Prints: <Buffer 00 00 00 00 00 00 00 00 00 00>
7881cb0ef41Sopenharmony_ci```
7891cb0ef41Sopenharmony_ci
7901cb0ef41Sopenharmony_ci```cjs
7911cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
7921cb0ef41Sopenharmony_ci
7931cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(10);
7941cb0ef41Sopenharmony_ci
7951cb0ef41Sopenharmony_ciconsole.log(buf);
7961cb0ef41Sopenharmony_ci// Prints (contents may vary): <Buffer a0 8b 28 3f 01 00 00 00 50 32>
7971cb0ef41Sopenharmony_ci
7981cb0ef41Sopenharmony_cibuf.fill(0);
7991cb0ef41Sopenharmony_ci
8001cb0ef41Sopenharmony_ciconsole.log(buf);
8011cb0ef41Sopenharmony_ci// Prints: <Buffer 00 00 00 00 00 00 00 00 00 00>
8021cb0ef41Sopenharmony_ci```
8031cb0ef41Sopenharmony_ci
8041cb0ef41Sopenharmony_ciA `TypeError` will be thrown if `size` is not a number.
8051cb0ef41Sopenharmony_ci
8061cb0ef41Sopenharmony_ciThe `Buffer` module pre-allocates an internal `Buffer` instance of
8071cb0ef41Sopenharmony_cisize [`Buffer.poolSize`][] that is used as a pool for the fast allocation of new
8081cb0ef41Sopenharmony_ci`Buffer` instances created using [`Buffer.allocUnsafe()`][], [`Buffer.from(array)`][],
8091cb0ef41Sopenharmony_ciand [`Buffer.concat()`][] only when `size` is less than or equal to
8101cb0ef41Sopenharmony_ci`Buffer.poolSize >> 1` (floor of [`Buffer.poolSize`][] divided by two).
8111cb0ef41Sopenharmony_ci
8121cb0ef41Sopenharmony_ciUse of this pre-allocated internal memory pool is a key difference between
8131cb0ef41Sopenharmony_cicalling `Buffer.alloc(size, fill)` vs. `Buffer.allocUnsafe(size).fill(fill)`.
8141cb0ef41Sopenharmony_ciSpecifically, `Buffer.alloc(size, fill)` will _never_ use the internal `Buffer`
8151cb0ef41Sopenharmony_cipool, while `Buffer.allocUnsafe(size).fill(fill)` _will_ use the internal
8161cb0ef41Sopenharmony_ci`Buffer` pool if `size` is less than or equal to half [`Buffer.poolSize`][]. The
8171cb0ef41Sopenharmony_cidifference is subtle but can be important when an application requires the
8181cb0ef41Sopenharmony_ciadditional performance that [`Buffer.allocUnsafe()`][] provides.
8191cb0ef41Sopenharmony_ci
8201cb0ef41Sopenharmony_ci### Static method: `Buffer.allocUnsafeSlow(size)`
8211cb0ef41Sopenharmony_ci
8221cb0ef41Sopenharmony_ci<!-- YAML
8231cb0ef41Sopenharmony_ciadded: v5.12.0
8241cb0ef41Sopenharmony_cichanges:
8251cb0ef41Sopenharmony_ci  - version: v20.0.0
8261cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/45796
8271cb0ef41Sopenharmony_ci    description: Throw ERR_INVALID_ARG_TYPE or ERR_OUT_OF_RANGE instead of
8281cb0ef41Sopenharmony_ci                 ERR_INVALID_ARG_VALUE for invalid input arguments.
8291cb0ef41Sopenharmony_ci  - version: v15.0.0
8301cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34682
8311cb0ef41Sopenharmony_ci    description: Throw ERR_INVALID_ARG_VALUE instead of ERR_INVALID_OPT_VALUE
8321cb0ef41Sopenharmony_ci                 for invalid input arguments.
8331cb0ef41Sopenharmony_ci-->
8341cb0ef41Sopenharmony_ci
8351cb0ef41Sopenharmony_ci* `size` {integer} The desired length of the new `Buffer`.
8361cb0ef41Sopenharmony_ci
8371cb0ef41Sopenharmony_ciAllocates a new `Buffer` of `size` bytes. If `size` is larger than
8381cb0ef41Sopenharmony_ci[`buffer.constants.MAX_LENGTH`][] or smaller than 0, [`ERR_OUT_OF_RANGE`][]
8391cb0ef41Sopenharmony_ciis thrown. A zero-length `Buffer` is created if `size` is 0.
8401cb0ef41Sopenharmony_ci
8411cb0ef41Sopenharmony_ciThe underlying memory for `Buffer` instances created in this way is _not
8421cb0ef41Sopenharmony_ciinitialized_. The contents of the newly created `Buffer` are unknown and
8431cb0ef41Sopenharmony_ci_may contain sensitive data_. Use [`buf.fill(0)`][`buf.fill()`] to initialize
8441cb0ef41Sopenharmony_cisuch `Buffer` instances with zeroes.
8451cb0ef41Sopenharmony_ci
8461cb0ef41Sopenharmony_ciWhen using [`Buffer.allocUnsafe()`][] to allocate new `Buffer` instances,
8471cb0ef41Sopenharmony_ciallocations under 4 KiB are sliced from a single pre-allocated `Buffer`. This
8481cb0ef41Sopenharmony_ciallows applications to avoid the garbage collection overhead of creating many
8491cb0ef41Sopenharmony_ciindividually allocated `Buffer` instances. This approach improves both
8501cb0ef41Sopenharmony_ciperformance and memory usage by eliminating the need to track and clean up as
8511cb0ef41Sopenharmony_cimany individual `ArrayBuffer` objects.
8521cb0ef41Sopenharmony_ci
8531cb0ef41Sopenharmony_ciHowever, in the case where a developer may need to retain a small chunk of
8541cb0ef41Sopenharmony_cimemory from a pool for an indeterminate amount of time, it may be appropriate
8551cb0ef41Sopenharmony_cito create an un-pooled `Buffer` instance using `Buffer.allocUnsafeSlow()` and
8561cb0ef41Sopenharmony_cithen copying out the relevant bits.
8571cb0ef41Sopenharmony_ci
8581cb0ef41Sopenharmony_ci```mjs
8591cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
8601cb0ef41Sopenharmony_ci
8611cb0ef41Sopenharmony_ci// Need to keep around a few small chunks of memory.
8621cb0ef41Sopenharmony_ciconst store = [];
8631cb0ef41Sopenharmony_ci
8641cb0ef41Sopenharmony_cisocket.on('readable', () => {
8651cb0ef41Sopenharmony_ci  let data;
8661cb0ef41Sopenharmony_ci  while (null !== (data = readable.read())) {
8671cb0ef41Sopenharmony_ci    // Allocate for retained data.
8681cb0ef41Sopenharmony_ci    const sb = Buffer.allocUnsafeSlow(10);
8691cb0ef41Sopenharmony_ci
8701cb0ef41Sopenharmony_ci    // Copy the data into the new allocation.
8711cb0ef41Sopenharmony_ci    data.copy(sb, 0, 0, 10);
8721cb0ef41Sopenharmony_ci
8731cb0ef41Sopenharmony_ci    store.push(sb);
8741cb0ef41Sopenharmony_ci  }
8751cb0ef41Sopenharmony_ci});
8761cb0ef41Sopenharmony_ci```
8771cb0ef41Sopenharmony_ci
8781cb0ef41Sopenharmony_ci```cjs
8791cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
8801cb0ef41Sopenharmony_ci
8811cb0ef41Sopenharmony_ci// Need to keep around a few small chunks of memory.
8821cb0ef41Sopenharmony_ciconst store = [];
8831cb0ef41Sopenharmony_ci
8841cb0ef41Sopenharmony_cisocket.on('readable', () => {
8851cb0ef41Sopenharmony_ci  let data;
8861cb0ef41Sopenharmony_ci  while (null !== (data = readable.read())) {
8871cb0ef41Sopenharmony_ci    // Allocate for retained data.
8881cb0ef41Sopenharmony_ci    const sb = Buffer.allocUnsafeSlow(10);
8891cb0ef41Sopenharmony_ci
8901cb0ef41Sopenharmony_ci    // Copy the data into the new allocation.
8911cb0ef41Sopenharmony_ci    data.copy(sb, 0, 0, 10);
8921cb0ef41Sopenharmony_ci
8931cb0ef41Sopenharmony_ci    store.push(sb);
8941cb0ef41Sopenharmony_ci  }
8951cb0ef41Sopenharmony_ci});
8961cb0ef41Sopenharmony_ci```
8971cb0ef41Sopenharmony_ci
8981cb0ef41Sopenharmony_ciA `TypeError` will be thrown if `size` is not a number.
8991cb0ef41Sopenharmony_ci
9001cb0ef41Sopenharmony_ci### Static method: `Buffer.byteLength(string[, encoding])`
9011cb0ef41Sopenharmony_ci
9021cb0ef41Sopenharmony_ci<!-- YAML
9031cb0ef41Sopenharmony_ciadded: v0.1.90
9041cb0ef41Sopenharmony_cichanges:
9051cb0ef41Sopenharmony_ci  - version: v7.0.0
9061cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/8946
9071cb0ef41Sopenharmony_ci    description: Passing invalid input will now throw an error.
9081cb0ef41Sopenharmony_ci  - version: v5.10.0
9091cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/5255
9101cb0ef41Sopenharmony_ci    description: The `string` parameter can now be any `TypedArray`, `DataView`
9111cb0ef41Sopenharmony_ci                 or `ArrayBuffer`.
9121cb0ef41Sopenharmony_ci-->
9131cb0ef41Sopenharmony_ci
9141cb0ef41Sopenharmony_ci* `string` {string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer} A
9151cb0ef41Sopenharmony_ci  value to calculate the length of.
9161cb0ef41Sopenharmony_ci* `encoding` {string} If `string` is a string, this is its encoding.
9171cb0ef41Sopenharmony_ci  **Default:** `'utf8'`.
9181cb0ef41Sopenharmony_ci* Returns: {integer} The number of bytes contained within `string`.
9191cb0ef41Sopenharmony_ci
9201cb0ef41Sopenharmony_ciReturns the byte length of a string when encoded using `encoding`.
9211cb0ef41Sopenharmony_ciThis is not the same as [`String.prototype.length`][], which does not account
9221cb0ef41Sopenharmony_cifor the encoding that is used to convert the string into bytes.
9231cb0ef41Sopenharmony_ci
9241cb0ef41Sopenharmony_ciFor `'base64'`, `'base64url'`, and `'hex'`, this function assumes valid input.
9251cb0ef41Sopenharmony_ciFor strings that contain non-base64/hex-encoded data (e.g. whitespace), the
9261cb0ef41Sopenharmony_cireturn value might be greater than the length of a `Buffer` created from the
9271cb0ef41Sopenharmony_cistring.
9281cb0ef41Sopenharmony_ci
9291cb0ef41Sopenharmony_ci```mjs
9301cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
9311cb0ef41Sopenharmony_ci
9321cb0ef41Sopenharmony_ciconst str = '\u00bd + \u00bc = \u00be';
9331cb0ef41Sopenharmony_ci
9341cb0ef41Sopenharmony_ciconsole.log(`${str}: ${str.length} characters, ` +
9351cb0ef41Sopenharmony_ci            `${Buffer.byteLength(str, 'utf8')} bytes`);
9361cb0ef41Sopenharmony_ci// Prints: ½ + ¼ = ¾: 9 characters, 12 bytes
9371cb0ef41Sopenharmony_ci```
9381cb0ef41Sopenharmony_ci
9391cb0ef41Sopenharmony_ci```cjs
9401cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
9411cb0ef41Sopenharmony_ci
9421cb0ef41Sopenharmony_ciconst str = '\u00bd + \u00bc = \u00be';
9431cb0ef41Sopenharmony_ci
9441cb0ef41Sopenharmony_ciconsole.log(`${str}: ${str.length} characters, ` +
9451cb0ef41Sopenharmony_ci            `${Buffer.byteLength(str, 'utf8')} bytes`);
9461cb0ef41Sopenharmony_ci// Prints: ½ + ¼ = ¾: 9 characters, 12 bytes
9471cb0ef41Sopenharmony_ci```
9481cb0ef41Sopenharmony_ci
9491cb0ef41Sopenharmony_ciWhen `string` is a `Buffer`/[`DataView`][]/[`TypedArray`][]/[`ArrayBuffer`][]/
9501cb0ef41Sopenharmony_ci[`SharedArrayBuffer`][], the byte length as reported by `.byteLength`
9511cb0ef41Sopenharmony_ciis returned.
9521cb0ef41Sopenharmony_ci
9531cb0ef41Sopenharmony_ci### Static method: `Buffer.compare(buf1, buf2)`
9541cb0ef41Sopenharmony_ci
9551cb0ef41Sopenharmony_ci<!-- YAML
9561cb0ef41Sopenharmony_ciadded: v0.11.13
9571cb0ef41Sopenharmony_cichanges:
9581cb0ef41Sopenharmony_ci  - version: v8.0.0
9591cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/10236
9601cb0ef41Sopenharmony_ci    description: The arguments can now be `Uint8Array`s.
9611cb0ef41Sopenharmony_ci-->
9621cb0ef41Sopenharmony_ci
9631cb0ef41Sopenharmony_ci* `buf1` {Buffer|Uint8Array}
9641cb0ef41Sopenharmony_ci* `buf2` {Buffer|Uint8Array}
9651cb0ef41Sopenharmony_ci* Returns: {integer} Either `-1`, `0`, or `1`, depending on the result of the
9661cb0ef41Sopenharmony_ci  comparison. See [`buf.compare()`][] for details.
9671cb0ef41Sopenharmony_ci
9681cb0ef41Sopenharmony_ciCompares `buf1` to `buf2`, typically for the purpose of sorting arrays of
9691cb0ef41Sopenharmony_ci`Buffer` instances. This is equivalent to calling
9701cb0ef41Sopenharmony_ci[`buf1.compare(buf2)`][`buf.compare()`].
9711cb0ef41Sopenharmony_ci
9721cb0ef41Sopenharmony_ci```mjs
9731cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
9741cb0ef41Sopenharmony_ci
9751cb0ef41Sopenharmony_ciconst buf1 = Buffer.from('1234');
9761cb0ef41Sopenharmony_ciconst buf2 = Buffer.from('0123');
9771cb0ef41Sopenharmony_ciconst arr = [buf1, buf2];
9781cb0ef41Sopenharmony_ci
9791cb0ef41Sopenharmony_ciconsole.log(arr.sort(Buffer.compare));
9801cb0ef41Sopenharmony_ci// Prints: [ <Buffer 30 31 32 33>, <Buffer 31 32 33 34> ]
9811cb0ef41Sopenharmony_ci// (This result is equal to: [buf2, buf1].)
9821cb0ef41Sopenharmony_ci```
9831cb0ef41Sopenharmony_ci
9841cb0ef41Sopenharmony_ci```cjs
9851cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
9861cb0ef41Sopenharmony_ci
9871cb0ef41Sopenharmony_ciconst buf1 = Buffer.from('1234');
9881cb0ef41Sopenharmony_ciconst buf2 = Buffer.from('0123');
9891cb0ef41Sopenharmony_ciconst arr = [buf1, buf2];
9901cb0ef41Sopenharmony_ci
9911cb0ef41Sopenharmony_ciconsole.log(arr.sort(Buffer.compare));
9921cb0ef41Sopenharmony_ci// Prints: [ <Buffer 30 31 32 33>, <Buffer 31 32 33 34> ]
9931cb0ef41Sopenharmony_ci// (This result is equal to: [buf2, buf1].)
9941cb0ef41Sopenharmony_ci```
9951cb0ef41Sopenharmony_ci
9961cb0ef41Sopenharmony_ci### Static method: `Buffer.concat(list[, totalLength])`
9971cb0ef41Sopenharmony_ci
9981cb0ef41Sopenharmony_ci<!-- YAML
9991cb0ef41Sopenharmony_ciadded: v0.7.11
10001cb0ef41Sopenharmony_cichanges:
10011cb0ef41Sopenharmony_ci  - version: v8.0.0
10021cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/10236
10031cb0ef41Sopenharmony_ci    description: The elements of `list` can now be `Uint8Array`s.
10041cb0ef41Sopenharmony_ci-->
10051cb0ef41Sopenharmony_ci
10061cb0ef41Sopenharmony_ci* `list` {Buffer\[] | Uint8Array\[]} List of `Buffer` or [`Uint8Array`][]
10071cb0ef41Sopenharmony_ci  instances to concatenate.
10081cb0ef41Sopenharmony_ci* `totalLength` {integer} Total length of the `Buffer` instances in `list`
10091cb0ef41Sopenharmony_ci  when concatenated.
10101cb0ef41Sopenharmony_ci* Returns: {Buffer}
10111cb0ef41Sopenharmony_ci
10121cb0ef41Sopenharmony_ciReturns a new `Buffer` which is the result of concatenating all the `Buffer`
10131cb0ef41Sopenharmony_ciinstances in the `list` together.
10141cb0ef41Sopenharmony_ci
10151cb0ef41Sopenharmony_ciIf the list has no items, or if the `totalLength` is 0, then a new zero-length
10161cb0ef41Sopenharmony_ci`Buffer` is returned.
10171cb0ef41Sopenharmony_ci
10181cb0ef41Sopenharmony_ciIf `totalLength` is not provided, it is calculated from the `Buffer` instances
10191cb0ef41Sopenharmony_ciin `list` by adding their lengths.
10201cb0ef41Sopenharmony_ci
10211cb0ef41Sopenharmony_ciIf `totalLength` is provided, it is coerced to an unsigned integer. If the
10221cb0ef41Sopenharmony_cicombined length of the `Buffer`s in `list` exceeds `totalLength`, the result is
10231cb0ef41Sopenharmony_citruncated to `totalLength`.
10241cb0ef41Sopenharmony_ci
10251cb0ef41Sopenharmony_ci```mjs
10261cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
10271cb0ef41Sopenharmony_ci
10281cb0ef41Sopenharmony_ci// Create a single `Buffer` from a list of three `Buffer` instances.
10291cb0ef41Sopenharmony_ci
10301cb0ef41Sopenharmony_ciconst buf1 = Buffer.alloc(10);
10311cb0ef41Sopenharmony_ciconst buf2 = Buffer.alloc(14);
10321cb0ef41Sopenharmony_ciconst buf3 = Buffer.alloc(18);
10331cb0ef41Sopenharmony_ciconst totalLength = buf1.length + buf2.length + buf3.length;
10341cb0ef41Sopenharmony_ci
10351cb0ef41Sopenharmony_ciconsole.log(totalLength);
10361cb0ef41Sopenharmony_ci// Prints: 42
10371cb0ef41Sopenharmony_ci
10381cb0ef41Sopenharmony_ciconst bufA = Buffer.concat([buf1, buf2, buf3], totalLength);
10391cb0ef41Sopenharmony_ci
10401cb0ef41Sopenharmony_ciconsole.log(bufA);
10411cb0ef41Sopenharmony_ci// Prints: <Buffer 00 00 00 00 ...>
10421cb0ef41Sopenharmony_ciconsole.log(bufA.length);
10431cb0ef41Sopenharmony_ci// Prints: 42
10441cb0ef41Sopenharmony_ci```
10451cb0ef41Sopenharmony_ci
10461cb0ef41Sopenharmony_ci```cjs
10471cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
10481cb0ef41Sopenharmony_ci
10491cb0ef41Sopenharmony_ci// Create a single `Buffer` from a list of three `Buffer` instances.
10501cb0ef41Sopenharmony_ci
10511cb0ef41Sopenharmony_ciconst buf1 = Buffer.alloc(10);
10521cb0ef41Sopenharmony_ciconst buf2 = Buffer.alloc(14);
10531cb0ef41Sopenharmony_ciconst buf3 = Buffer.alloc(18);
10541cb0ef41Sopenharmony_ciconst totalLength = buf1.length + buf2.length + buf3.length;
10551cb0ef41Sopenharmony_ci
10561cb0ef41Sopenharmony_ciconsole.log(totalLength);
10571cb0ef41Sopenharmony_ci// Prints: 42
10581cb0ef41Sopenharmony_ci
10591cb0ef41Sopenharmony_ciconst bufA = Buffer.concat([buf1, buf2, buf3], totalLength);
10601cb0ef41Sopenharmony_ci
10611cb0ef41Sopenharmony_ciconsole.log(bufA);
10621cb0ef41Sopenharmony_ci// Prints: <Buffer 00 00 00 00 ...>
10631cb0ef41Sopenharmony_ciconsole.log(bufA.length);
10641cb0ef41Sopenharmony_ci// Prints: 42
10651cb0ef41Sopenharmony_ci```
10661cb0ef41Sopenharmony_ci
10671cb0ef41Sopenharmony_ci`Buffer.concat()` may also use the internal `Buffer` pool like
10681cb0ef41Sopenharmony_ci[`Buffer.allocUnsafe()`][] does.
10691cb0ef41Sopenharmony_ci
10701cb0ef41Sopenharmony_ci### Static method: `Buffer.copyBytesFrom(view[, offset[, length]])`
10711cb0ef41Sopenharmony_ci
10721cb0ef41Sopenharmony_ci<!-- YAML
10731cb0ef41Sopenharmony_ciadded: v18.16.0
10741cb0ef41Sopenharmony_ci-->
10751cb0ef41Sopenharmony_ci
10761cb0ef41Sopenharmony_ci* `view` {TypedArray} The {TypedArray} to copy.
10771cb0ef41Sopenharmony_ci* `offset` {integer} The starting offset within `view`. **Default:**: `0`.
10781cb0ef41Sopenharmony_ci* `length` {integer} The number of elements from `view` to copy.
10791cb0ef41Sopenharmony_ci  **Default:** `view.length - offset`.
10801cb0ef41Sopenharmony_ci
10811cb0ef41Sopenharmony_ciCopies the underlying memory of `view` into a new `Buffer`.
10821cb0ef41Sopenharmony_ci
10831cb0ef41Sopenharmony_ci```js
10841cb0ef41Sopenharmony_ciconst u16 = new Uint16Array([0, 0xffff]);
10851cb0ef41Sopenharmony_ciconst buf = Buffer.copyBytesFrom(u16, 1, 1);
10861cb0ef41Sopenharmony_ciu16[1] = 0;
10871cb0ef41Sopenharmony_ciconsole.log(buf.length); // 2
10881cb0ef41Sopenharmony_ciconsole.log(buf[0]); // 255
10891cb0ef41Sopenharmony_ciconsole.log(buf[1]); // 255
10901cb0ef41Sopenharmony_ci```
10911cb0ef41Sopenharmony_ci
10921cb0ef41Sopenharmony_ci### Static method: `Buffer.from(array)`
10931cb0ef41Sopenharmony_ci
10941cb0ef41Sopenharmony_ci<!-- YAML
10951cb0ef41Sopenharmony_ciadded: v5.10.0
10961cb0ef41Sopenharmony_ci-->
10971cb0ef41Sopenharmony_ci
10981cb0ef41Sopenharmony_ci* `array` {integer\[]}
10991cb0ef41Sopenharmony_ci
11001cb0ef41Sopenharmony_ciAllocates a new `Buffer` using an `array` of bytes in the range `0` – `255`.
11011cb0ef41Sopenharmony_ciArray entries outside that range will be truncated to fit into it.
11021cb0ef41Sopenharmony_ci
11031cb0ef41Sopenharmony_ci```mjs
11041cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
11051cb0ef41Sopenharmony_ci
11061cb0ef41Sopenharmony_ci// Creates a new Buffer containing the UTF-8 bytes of the string 'buffer'.
11071cb0ef41Sopenharmony_ciconst buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);
11081cb0ef41Sopenharmony_ci```
11091cb0ef41Sopenharmony_ci
11101cb0ef41Sopenharmony_ci```cjs
11111cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
11121cb0ef41Sopenharmony_ci
11131cb0ef41Sopenharmony_ci// Creates a new Buffer containing the UTF-8 bytes of the string 'buffer'.
11141cb0ef41Sopenharmony_ciconst buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);
11151cb0ef41Sopenharmony_ci```
11161cb0ef41Sopenharmony_ci
11171cb0ef41Sopenharmony_ciIf `array` is an `Array`-like object (that is, one with a `length` property of
11181cb0ef41Sopenharmony_citype `number`), it is treated as if it is an array, unless it is a `Buffer` or
11191cb0ef41Sopenharmony_cia `Uint8Array`. This means all other `TypedArray` variants get treated as an
11201cb0ef41Sopenharmony_ci`Array`. To create a `Buffer` from the bytes backing a `TypedArray`, use
11211cb0ef41Sopenharmony_ci[`Buffer.copyBytesFrom()`][].
11221cb0ef41Sopenharmony_ci
11231cb0ef41Sopenharmony_ciA `TypeError` will be thrown if `array` is not an `Array` or another type
11241cb0ef41Sopenharmony_ciappropriate for `Buffer.from()` variants.
11251cb0ef41Sopenharmony_ci
11261cb0ef41Sopenharmony_ci`Buffer.from(array)` and [`Buffer.from(string)`][] may also use the internal
11271cb0ef41Sopenharmony_ci`Buffer` pool like [`Buffer.allocUnsafe()`][] does.
11281cb0ef41Sopenharmony_ci
11291cb0ef41Sopenharmony_ci### Static method: `Buffer.from(arrayBuffer[, byteOffset[, length]])`
11301cb0ef41Sopenharmony_ci
11311cb0ef41Sopenharmony_ci<!-- YAML
11321cb0ef41Sopenharmony_ciadded: v5.10.0
11331cb0ef41Sopenharmony_ci-->
11341cb0ef41Sopenharmony_ci
11351cb0ef41Sopenharmony_ci* `arrayBuffer` {ArrayBuffer|SharedArrayBuffer} An [`ArrayBuffer`][],
11361cb0ef41Sopenharmony_ci  [`SharedArrayBuffer`][], for example the `.buffer` property of a
11371cb0ef41Sopenharmony_ci  [`TypedArray`][].
11381cb0ef41Sopenharmony_ci* `byteOffset` {integer} Index of first byte to expose. **Default:** `0`.
11391cb0ef41Sopenharmony_ci* `length` {integer} Number of bytes to expose.
11401cb0ef41Sopenharmony_ci  **Default:** `arrayBuffer.byteLength - byteOffset`.
11411cb0ef41Sopenharmony_ci
11421cb0ef41Sopenharmony_ciThis creates a view of the [`ArrayBuffer`][] without copying the underlying
11431cb0ef41Sopenharmony_cimemory. For example, when passed a reference to the `.buffer` property of a
11441cb0ef41Sopenharmony_ci[`TypedArray`][] instance, the newly created `Buffer` will share the same
11451cb0ef41Sopenharmony_ciallocated memory as the [`TypedArray`][]'s underlying `ArrayBuffer`.
11461cb0ef41Sopenharmony_ci
11471cb0ef41Sopenharmony_ci```mjs
11481cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
11491cb0ef41Sopenharmony_ci
11501cb0ef41Sopenharmony_ciconst arr = new Uint16Array(2);
11511cb0ef41Sopenharmony_ci
11521cb0ef41Sopenharmony_ciarr[0] = 5000;
11531cb0ef41Sopenharmony_ciarr[1] = 4000;
11541cb0ef41Sopenharmony_ci
11551cb0ef41Sopenharmony_ci// Shares memory with `arr`.
11561cb0ef41Sopenharmony_ciconst buf = Buffer.from(arr.buffer);
11571cb0ef41Sopenharmony_ci
11581cb0ef41Sopenharmony_ciconsole.log(buf);
11591cb0ef41Sopenharmony_ci// Prints: <Buffer 88 13 a0 0f>
11601cb0ef41Sopenharmony_ci
11611cb0ef41Sopenharmony_ci// Changing the original Uint16Array changes the Buffer also.
11621cb0ef41Sopenharmony_ciarr[1] = 6000;
11631cb0ef41Sopenharmony_ci
11641cb0ef41Sopenharmony_ciconsole.log(buf);
11651cb0ef41Sopenharmony_ci// Prints: <Buffer 88 13 70 17>
11661cb0ef41Sopenharmony_ci```
11671cb0ef41Sopenharmony_ci
11681cb0ef41Sopenharmony_ci```cjs
11691cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
11701cb0ef41Sopenharmony_ci
11711cb0ef41Sopenharmony_ciconst arr = new Uint16Array(2);
11721cb0ef41Sopenharmony_ci
11731cb0ef41Sopenharmony_ciarr[0] = 5000;
11741cb0ef41Sopenharmony_ciarr[1] = 4000;
11751cb0ef41Sopenharmony_ci
11761cb0ef41Sopenharmony_ci// Shares memory with `arr`.
11771cb0ef41Sopenharmony_ciconst buf = Buffer.from(arr.buffer);
11781cb0ef41Sopenharmony_ci
11791cb0ef41Sopenharmony_ciconsole.log(buf);
11801cb0ef41Sopenharmony_ci// Prints: <Buffer 88 13 a0 0f>
11811cb0ef41Sopenharmony_ci
11821cb0ef41Sopenharmony_ci// Changing the original Uint16Array changes the Buffer also.
11831cb0ef41Sopenharmony_ciarr[1] = 6000;
11841cb0ef41Sopenharmony_ci
11851cb0ef41Sopenharmony_ciconsole.log(buf);
11861cb0ef41Sopenharmony_ci// Prints: <Buffer 88 13 70 17>
11871cb0ef41Sopenharmony_ci```
11881cb0ef41Sopenharmony_ci
11891cb0ef41Sopenharmony_ciThe optional `byteOffset` and `length` arguments specify a memory range within
11901cb0ef41Sopenharmony_cithe `arrayBuffer` that will be shared by the `Buffer`.
11911cb0ef41Sopenharmony_ci
11921cb0ef41Sopenharmony_ci```mjs
11931cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
11941cb0ef41Sopenharmony_ci
11951cb0ef41Sopenharmony_ciconst ab = new ArrayBuffer(10);
11961cb0ef41Sopenharmony_ciconst buf = Buffer.from(ab, 0, 2);
11971cb0ef41Sopenharmony_ci
11981cb0ef41Sopenharmony_ciconsole.log(buf.length);
11991cb0ef41Sopenharmony_ci// Prints: 2
12001cb0ef41Sopenharmony_ci```
12011cb0ef41Sopenharmony_ci
12021cb0ef41Sopenharmony_ci```cjs
12031cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
12041cb0ef41Sopenharmony_ci
12051cb0ef41Sopenharmony_ciconst ab = new ArrayBuffer(10);
12061cb0ef41Sopenharmony_ciconst buf = Buffer.from(ab, 0, 2);
12071cb0ef41Sopenharmony_ci
12081cb0ef41Sopenharmony_ciconsole.log(buf.length);
12091cb0ef41Sopenharmony_ci// Prints: 2
12101cb0ef41Sopenharmony_ci```
12111cb0ef41Sopenharmony_ci
12121cb0ef41Sopenharmony_ciA `TypeError` will be thrown if `arrayBuffer` is not an [`ArrayBuffer`][] or a
12131cb0ef41Sopenharmony_ci[`SharedArrayBuffer`][] or another type appropriate for `Buffer.from()`
12141cb0ef41Sopenharmony_civariants.
12151cb0ef41Sopenharmony_ci
12161cb0ef41Sopenharmony_ciIt is important to remember that a backing `ArrayBuffer` can cover a range
12171cb0ef41Sopenharmony_ciof memory that extends beyond the bounds of a `TypedArray` view. A new
12181cb0ef41Sopenharmony_ci`Buffer` created using the `buffer` property of a `TypedArray` may extend
12191cb0ef41Sopenharmony_cibeyond the range of the `TypedArray`:
12201cb0ef41Sopenharmony_ci
12211cb0ef41Sopenharmony_ci```mjs
12221cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
12231cb0ef41Sopenharmony_ci
12241cb0ef41Sopenharmony_ciconst arrA = Uint8Array.from([0x63, 0x64, 0x65, 0x66]); // 4 elements
12251cb0ef41Sopenharmony_ciconst arrB = new Uint8Array(arrA.buffer, 1, 2); // 2 elements
12261cb0ef41Sopenharmony_ciconsole.log(arrA.buffer === arrB.buffer); // true
12271cb0ef41Sopenharmony_ci
12281cb0ef41Sopenharmony_ciconst buf = Buffer.from(arrB.buffer);
12291cb0ef41Sopenharmony_ciconsole.log(buf);
12301cb0ef41Sopenharmony_ci// Prints: <Buffer 63 64 65 66>
12311cb0ef41Sopenharmony_ci```
12321cb0ef41Sopenharmony_ci
12331cb0ef41Sopenharmony_ci```cjs
12341cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
12351cb0ef41Sopenharmony_ci
12361cb0ef41Sopenharmony_ciconst arrA = Uint8Array.from([0x63, 0x64, 0x65, 0x66]); // 4 elements
12371cb0ef41Sopenharmony_ciconst arrB = new Uint8Array(arrA.buffer, 1, 2); // 2 elements
12381cb0ef41Sopenharmony_ciconsole.log(arrA.buffer === arrB.buffer); // true
12391cb0ef41Sopenharmony_ci
12401cb0ef41Sopenharmony_ciconst buf = Buffer.from(arrB.buffer);
12411cb0ef41Sopenharmony_ciconsole.log(buf);
12421cb0ef41Sopenharmony_ci// Prints: <Buffer 63 64 65 66>
12431cb0ef41Sopenharmony_ci```
12441cb0ef41Sopenharmony_ci
12451cb0ef41Sopenharmony_ci### Static method: `Buffer.from(buffer)`
12461cb0ef41Sopenharmony_ci
12471cb0ef41Sopenharmony_ci<!-- YAML
12481cb0ef41Sopenharmony_ciadded: v5.10.0
12491cb0ef41Sopenharmony_ci-->
12501cb0ef41Sopenharmony_ci
12511cb0ef41Sopenharmony_ci* `buffer` {Buffer|Uint8Array} An existing `Buffer` or [`Uint8Array`][] from
12521cb0ef41Sopenharmony_ci  which to copy data.
12531cb0ef41Sopenharmony_ci
12541cb0ef41Sopenharmony_ciCopies the passed `buffer` data onto a new `Buffer` instance.
12551cb0ef41Sopenharmony_ci
12561cb0ef41Sopenharmony_ci```mjs
12571cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
12581cb0ef41Sopenharmony_ci
12591cb0ef41Sopenharmony_ciconst buf1 = Buffer.from('buffer');
12601cb0ef41Sopenharmony_ciconst buf2 = Buffer.from(buf1);
12611cb0ef41Sopenharmony_ci
12621cb0ef41Sopenharmony_cibuf1[0] = 0x61;
12631cb0ef41Sopenharmony_ci
12641cb0ef41Sopenharmony_ciconsole.log(buf1.toString());
12651cb0ef41Sopenharmony_ci// Prints: auffer
12661cb0ef41Sopenharmony_ciconsole.log(buf2.toString());
12671cb0ef41Sopenharmony_ci// Prints: buffer
12681cb0ef41Sopenharmony_ci```
12691cb0ef41Sopenharmony_ci
12701cb0ef41Sopenharmony_ci```cjs
12711cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
12721cb0ef41Sopenharmony_ci
12731cb0ef41Sopenharmony_ciconst buf1 = Buffer.from('buffer');
12741cb0ef41Sopenharmony_ciconst buf2 = Buffer.from(buf1);
12751cb0ef41Sopenharmony_ci
12761cb0ef41Sopenharmony_cibuf1[0] = 0x61;
12771cb0ef41Sopenharmony_ci
12781cb0ef41Sopenharmony_ciconsole.log(buf1.toString());
12791cb0ef41Sopenharmony_ci// Prints: auffer
12801cb0ef41Sopenharmony_ciconsole.log(buf2.toString());
12811cb0ef41Sopenharmony_ci// Prints: buffer
12821cb0ef41Sopenharmony_ci```
12831cb0ef41Sopenharmony_ci
12841cb0ef41Sopenharmony_ciA `TypeError` will be thrown if `buffer` is not a `Buffer` or another type
12851cb0ef41Sopenharmony_ciappropriate for `Buffer.from()` variants.
12861cb0ef41Sopenharmony_ci
12871cb0ef41Sopenharmony_ci### Static method: `Buffer.from(object[, offsetOrEncoding[, length]])`
12881cb0ef41Sopenharmony_ci
12891cb0ef41Sopenharmony_ci<!-- YAML
12901cb0ef41Sopenharmony_ciadded: v8.2.0
12911cb0ef41Sopenharmony_ci-->
12921cb0ef41Sopenharmony_ci
12931cb0ef41Sopenharmony_ci* `object` {Object} An object supporting `Symbol.toPrimitive` or `valueOf()`.
12941cb0ef41Sopenharmony_ci* `offsetOrEncoding` {integer|string} A byte-offset or encoding.
12951cb0ef41Sopenharmony_ci* `length` {integer} A length.
12961cb0ef41Sopenharmony_ci
12971cb0ef41Sopenharmony_ciFor objects whose `valueOf()` function returns a value not strictly equal to
12981cb0ef41Sopenharmony_ci`object`, returns `Buffer.from(object.valueOf(), offsetOrEncoding, length)`.
12991cb0ef41Sopenharmony_ci
13001cb0ef41Sopenharmony_ci```mjs
13011cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
13021cb0ef41Sopenharmony_ci
13031cb0ef41Sopenharmony_ciconst buf = Buffer.from(new String('this is a test'));
13041cb0ef41Sopenharmony_ci// Prints: <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74>
13051cb0ef41Sopenharmony_ci```
13061cb0ef41Sopenharmony_ci
13071cb0ef41Sopenharmony_ci```cjs
13081cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
13091cb0ef41Sopenharmony_ci
13101cb0ef41Sopenharmony_ciconst buf = Buffer.from(new String('this is a test'));
13111cb0ef41Sopenharmony_ci// Prints: <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74>
13121cb0ef41Sopenharmony_ci```
13131cb0ef41Sopenharmony_ci
13141cb0ef41Sopenharmony_ciFor objects that support `Symbol.toPrimitive`, returns
13151cb0ef41Sopenharmony_ci`Buffer.from(object[Symbol.toPrimitive]('string'), offsetOrEncoding)`.
13161cb0ef41Sopenharmony_ci
13171cb0ef41Sopenharmony_ci```mjs
13181cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
13191cb0ef41Sopenharmony_ci
13201cb0ef41Sopenharmony_ciclass Foo {
13211cb0ef41Sopenharmony_ci  [Symbol.toPrimitive]() {
13221cb0ef41Sopenharmony_ci    return 'this is a test';
13231cb0ef41Sopenharmony_ci  }
13241cb0ef41Sopenharmony_ci}
13251cb0ef41Sopenharmony_ci
13261cb0ef41Sopenharmony_ciconst buf = Buffer.from(new Foo(), 'utf8');
13271cb0ef41Sopenharmony_ci// Prints: <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74>
13281cb0ef41Sopenharmony_ci```
13291cb0ef41Sopenharmony_ci
13301cb0ef41Sopenharmony_ci```cjs
13311cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
13321cb0ef41Sopenharmony_ci
13331cb0ef41Sopenharmony_ciclass Foo {
13341cb0ef41Sopenharmony_ci  [Symbol.toPrimitive]() {
13351cb0ef41Sopenharmony_ci    return 'this is a test';
13361cb0ef41Sopenharmony_ci  }
13371cb0ef41Sopenharmony_ci}
13381cb0ef41Sopenharmony_ci
13391cb0ef41Sopenharmony_ciconst buf = Buffer.from(new Foo(), 'utf8');
13401cb0ef41Sopenharmony_ci// Prints: <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74>
13411cb0ef41Sopenharmony_ci```
13421cb0ef41Sopenharmony_ci
13431cb0ef41Sopenharmony_ciA `TypeError` will be thrown if `object` does not have the mentioned methods or
13441cb0ef41Sopenharmony_ciis not of another type appropriate for `Buffer.from()` variants.
13451cb0ef41Sopenharmony_ci
13461cb0ef41Sopenharmony_ci### Static method: `Buffer.from(string[, encoding])`
13471cb0ef41Sopenharmony_ci
13481cb0ef41Sopenharmony_ci<!-- YAML
13491cb0ef41Sopenharmony_ciadded: v5.10.0
13501cb0ef41Sopenharmony_ci-->
13511cb0ef41Sopenharmony_ci
13521cb0ef41Sopenharmony_ci* `string` {string} A string to encode.
13531cb0ef41Sopenharmony_ci* `encoding` {string} The encoding of `string`. **Default:** `'utf8'`.
13541cb0ef41Sopenharmony_ci
13551cb0ef41Sopenharmony_ciCreates a new `Buffer` containing `string`. The `encoding` parameter identifies
13561cb0ef41Sopenharmony_cithe character encoding to be used when converting `string` into bytes.
13571cb0ef41Sopenharmony_ci
13581cb0ef41Sopenharmony_ci```mjs
13591cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
13601cb0ef41Sopenharmony_ci
13611cb0ef41Sopenharmony_ciconst buf1 = Buffer.from('this is a tést');
13621cb0ef41Sopenharmony_ciconst buf2 = Buffer.from('7468697320697320612074c3a97374', 'hex');
13631cb0ef41Sopenharmony_ci
13641cb0ef41Sopenharmony_ciconsole.log(buf1.toString());
13651cb0ef41Sopenharmony_ci// Prints: this is a tést
13661cb0ef41Sopenharmony_ciconsole.log(buf2.toString());
13671cb0ef41Sopenharmony_ci// Prints: this is a tést
13681cb0ef41Sopenharmony_ciconsole.log(buf1.toString('latin1'));
13691cb0ef41Sopenharmony_ci// Prints: this is a tést
13701cb0ef41Sopenharmony_ci```
13711cb0ef41Sopenharmony_ci
13721cb0ef41Sopenharmony_ci```cjs
13731cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
13741cb0ef41Sopenharmony_ci
13751cb0ef41Sopenharmony_ciconst buf1 = Buffer.from('this is a tést');
13761cb0ef41Sopenharmony_ciconst buf2 = Buffer.from('7468697320697320612074c3a97374', 'hex');
13771cb0ef41Sopenharmony_ci
13781cb0ef41Sopenharmony_ciconsole.log(buf1.toString());
13791cb0ef41Sopenharmony_ci// Prints: this is a tést
13801cb0ef41Sopenharmony_ciconsole.log(buf2.toString());
13811cb0ef41Sopenharmony_ci// Prints: this is a tést
13821cb0ef41Sopenharmony_ciconsole.log(buf1.toString('latin1'));
13831cb0ef41Sopenharmony_ci// Prints: this is a tést
13841cb0ef41Sopenharmony_ci```
13851cb0ef41Sopenharmony_ci
13861cb0ef41Sopenharmony_ciA `TypeError` will be thrown if `string` is not a string or another type
13871cb0ef41Sopenharmony_ciappropriate for `Buffer.from()` variants.
13881cb0ef41Sopenharmony_ci
13891cb0ef41Sopenharmony_ci### Static method: `Buffer.isBuffer(obj)`
13901cb0ef41Sopenharmony_ci
13911cb0ef41Sopenharmony_ci<!-- YAML
13921cb0ef41Sopenharmony_ciadded: v0.1.101
13931cb0ef41Sopenharmony_ci-->
13941cb0ef41Sopenharmony_ci
13951cb0ef41Sopenharmony_ci* `obj` {Object}
13961cb0ef41Sopenharmony_ci* Returns: {boolean}
13971cb0ef41Sopenharmony_ci
13981cb0ef41Sopenharmony_ciReturns `true` if `obj` is a `Buffer`, `false` otherwise.
13991cb0ef41Sopenharmony_ci
14001cb0ef41Sopenharmony_ci```mjs
14011cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
14021cb0ef41Sopenharmony_ci
14031cb0ef41Sopenharmony_ciBuffer.isBuffer(Buffer.alloc(10)); // true
14041cb0ef41Sopenharmony_ciBuffer.isBuffer(Buffer.from('foo')); // true
14051cb0ef41Sopenharmony_ciBuffer.isBuffer('a string'); // false
14061cb0ef41Sopenharmony_ciBuffer.isBuffer([]); // false
14071cb0ef41Sopenharmony_ciBuffer.isBuffer(new Uint8Array(1024)); // false
14081cb0ef41Sopenharmony_ci```
14091cb0ef41Sopenharmony_ci
14101cb0ef41Sopenharmony_ci```cjs
14111cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
14121cb0ef41Sopenharmony_ci
14131cb0ef41Sopenharmony_ciBuffer.isBuffer(Buffer.alloc(10)); // true
14141cb0ef41Sopenharmony_ciBuffer.isBuffer(Buffer.from('foo')); // true
14151cb0ef41Sopenharmony_ciBuffer.isBuffer('a string'); // false
14161cb0ef41Sopenharmony_ciBuffer.isBuffer([]); // false
14171cb0ef41Sopenharmony_ciBuffer.isBuffer(new Uint8Array(1024)); // false
14181cb0ef41Sopenharmony_ci```
14191cb0ef41Sopenharmony_ci
14201cb0ef41Sopenharmony_ci### Static method: `Buffer.isEncoding(encoding)`
14211cb0ef41Sopenharmony_ci
14221cb0ef41Sopenharmony_ci<!-- YAML
14231cb0ef41Sopenharmony_ciadded: v0.9.1
14241cb0ef41Sopenharmony_ci-->
14251cb0ef41Sopenharmony_ci
14261cb0ef41Sopenharmony_ci* `encoding` {string} A character encoding name to check.
14271cb0ef41Sopenharmony_ci* Returns: {boolean}
14281cb0ef41Sopenharmony_ci
14291cb0ef41Sopenharmony_ciReturns `true` if `encoding` is the name of a supported character encoding,
14301cb0ef41Sopenharmony_cior `false` otherwise.
14311cb0ef41Sopenharmony_ci
14321cb0ef41Sopenharmony_ci```mjs
14331cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
14341cb0ef41Sopenharmony_ci
14351cb0ef41Sopenharmony_ciconsole.log(Buffer.isEncoding('utf8'));
14361cb0ef41Sopenharmony_ci// Prints: true
14371cb0ef41Sopenharmony_ci
14381cb0ef41Sopenharmony_ciconsole.log(Buffer.isEncoding('hex'));
14391cb0ef41Sopenharmony_ci// Prints: true
14401cb0ef41Sopenharmony_ci
14411cb0ef41Sopenharmony_ciconsole.log(Buffer.isEncoding('utf/8'));
14421cb0ef41Sopenharmony_ci// Prints: false
14431cb0ef41Sopenharmony_ci
14441cb0ef41Sopenharmony_ciconsole.log(Buffer.isEncoding(''));
14451cb0ef41Sopenharmony_ci// Prints: false
14461cb0ef41Sopenharmony_ci```
14471cb0ef41Sopenharmony_ci
14481cb0ef41Sopenharmony_ci```cjs
14491cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
14501cb0ef41Sopenharmony_ci
14511cb0ef41Sopenharmony_ciconsole.log(Buffer.isEncoding('utf8'));
14521cb0ef41Sopenharmony_ci// Prints: true
14531cb0ef41Sopenharmony_ci
14541cb0ef41Sopenharmony_ciconsole.log(Buffer.isEncoding('hex'));
14551cb0ef41Sopenharmony_ci// Prints: true
14561cb0ef41Sopenharmony_ci
14571cb0ef41Sopenharmony_ciconsole.log(Buffer.isEncoding('utf/8'));
14581cb0ef41Sopenharmony_ci// Prints: false
14591cb0ef41Sopenharmony_ci
14601cb0ef41Sopenharmony_ciconsole.log(Buffer.isEncoding(''));
14611cb0ef41Sopenharmony_ci// Prints: false
14621cb0ef41Sopenharmony_ci```
14631cb0ef41Sopenharmony_ci
14641cb0ef41Sopenharmony_ci### Class property: `Buffer.poolSize`
14651cb0ef41Sopenharmony_ci
14661cb0ef41Sopenharmony_ci<!-- YAML
14671cb0ef41Sopenharmony_ciadded: v0.11.3
14681cb0ef41Sopenharmony_ci-->
14691cb0ef41Sopenharmony_ci
14701cb0ef41Sopenharmony_ci* {integer} **Default:** `8192`
14711cb0ef41Sopenharmony_ci
14721cb0ef41Sopenharmony_ciThis is the size (in bytes) of pre-allocated internal `Buffer` instances used
14731cb0ef41Sopenharmony_cifor pooling. This value may be modified.
14741cb0ef41Sopenharmony_ci
14751cb0ef41Sopenharmony_ci### `buf[index]`
14761cb0ef41Sopenharmony_ci
14771cb0ef41Sopenharmony_ci* `index` {integer}
14781cb0ef41Sopenharmony_ci
14791cb0ef41Sopenharmony_ciThe index operator `[index]` can be used to get and set the octet at position
14801cb0ef41Sopenharmony_ci`index` in `buf`. The values refer to individual bytes, so the legal value
14811cb0ef41Sopenharmony_cirange is between `0x00` and `0xFF` (hex) or `0` and `255` (decimal).
14821cb0ef41Sopenharmony_ci
14831cb0ef41Sopenharmony_ciThis operator is inherited from `Uint8Array`, so its behavior on out-of-bounds
14841cb0ef41Sopenharmony_ciaccess is the same as `Uint8Array`. In other words, `buf[index]` returns
14851cb0ef41Sopenharmony_ci`undefined` when `index` is negative or greater or equal to `buf.length`, and
14861cb0ef41Sopenharmony_ci`buf[index] = value` does not modify the buffer if `index` is negative or
14871cb0ef41Sopenharmony_ci`>= buf.length`.
14881cb0ef41Sopenharmony_ci
14891cb0ef41Sopenharmony_ci```mjs
14901cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
14911cb0ef41Sopenharmony_ci
14921cb0ef41Sopenharmony_ci// Copy an ASCII string into a `Buffer` one byte at a time.
14931cb0ef41Sopenharmony_ci// (This only works for ASCII-only strings. In general, one should use
14941cb0ef41Sopenharmony_ci// `Buffer.from()` to perform this conversion.)
14951cb0ef41Sopenharmony_ci
14961cb0ef41Sopenharmony_ciconst str = 'Node.js';
14971cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(str.length);
14981cb0ef41Sopenharmony_ci
14991cb0ef41Sopenharmony_cifor (let i = 0; i < str.length; i++) {
15001cb0ef41Sopenharmony_ci  buf[i] = str.charCodeAt(i);
15011cb0ef41Sopenharmony_ci}
15021cb0ef41Sopenharmony_ci
15031cb0ef41Sopenharmony_ciconsole.log(buf.toString('utf8'));
15041cb0ef41Sopenharmony_ci// Prints: Node.js
15051cb0ef41Sopenharmony_ci```
15061cb0ef41Sopenharmony_ci
15071cb0ef41Sopenharmony_ci```cjs
15081cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
15091cb0ef41Sopenharmony_ci
15101cb0ef41Sopenharmony_ci// Copy an ASCII string into a `Buffer` one byte at a time.
15111cb0ef41Sopenharmony_ci// (This only works for ASCII-only strings. In general, one should use
15121cb0ef41Sopenharmony_ci// `Buffer.from()` to perform this conversion.)
15131cb0ef41Sopenharmony_ci
15141cb0ef41Sopenharmony_ciconst str = 'Node.js';
15151cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(str.length);
15161cb0ef41Sopenharmony_ci
15171cb0ef41Sopenharmony_cifor (let i = 0; i < str.length; i++) {
15181cb0ef41Sopenharmony_ci  buf[i] = str.charCodeAt(i);
15191cb0ef41Sopenharmony_ci}
15201cb0ef41Sopenharmony_ci
15211cb0ef41Sopenharmony_ciconsole.log(buf.toString('utf8'));
15221cb0ef41Sopenharmony_ci// Prints: Node.js
15231cb0ef41Sopenharmony_ci```
15241cb0ef41Sopenharmony_ci
15251cb0ef41Sopenharmony_ci### `buf.buffer`
15261cb0ef41Sopenharmony_ci
15271cb0ef41Sopenharmony_ci* {ArrayBuffer} The underlying `ArrayBuffer` object based on which this `Buffer`
15281cb0ef41Sopenharmony_ci  object is created.
15291cb0ef41Sopenharmony_ci
15301cb0ef41Sopenharmony_ciThis `ArrayBuffer` is not guaranteed to correspond exactly to the original
15311cb0ef41Sopenharmony_ci`Buffer`. See the notes on `buf.byteOffset` for details.
15321cb0ef41Sopenharmony_ci
15331cb0ef41Sopenharmony_ci```mjs
15341cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
15351cb0ef41Sopenharmony_ci
15361cb0ef41Sopenharmony_ciconst arrayBuffer = new ArrayBuffer(16);
15371cb0ef41Sopenharmony_ciconst buffer = Buffer.from(arrayBuffer);
15381cb0ef41Sopenharmony_ci
15391cb0ef41Sopenharmony_ciconsole.log(buffer.buffer === arrayBuffer);
15401cb0ef41Sopenharmony_ci// Prints: true
15411cb0ef41Sopenharmony_ci```
15421cb0ef41Sopenharmony_ci
15431cb0ef41Sopenharmony_ci```cjs
15441cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
15451cb0ef41Sopenharmony_ci
15461cb0ef41Sopenharmony_ciconst arrayBuffer = new ArrayBuffer(16);
15471cb0ef41Sopenharmony_ciconst buffer = Buffer.from(arrayBuffer);
15481cb0ef41Sopenharmony_ci
15491cb0ef41Sopenharmony_ciconsole.log(buffer.buffer === arrayBuffer);
15501cb0ef41Sopenharmony_ci// Prints: true
15511cb0ef41Sopenharmony_ci```
15521cb0ef41Sopenharmony_ci
15531cb0ef41Sopenharmony_ci### `buf.byteOffset`
15541cb0ef41Sopenharmony_ci
15551cb0ef41Sopenharmony_ci* {integer} The `byteOffset` of the `Buffer`s underlying `ArrayBuffer` object.
15561cb0ef41Sopenharmony_ci
15571cb0ef41Sopenharmony_ciWhen setting `byteOffset` in `Buffer.from(ArrayBuffer, byteOffset, length)`,
15581cb0ef41Sopenharmony_cior sometimes when allocating a `Buffer` smaller than `Buffer.poolSize`, the
15591cb0ef41Sopenharmony_cibuffer does not start from a zero offset on the underlying `ArrayBuffer`.
15601cb0ef41Sopenharmony_ci
15611cb0ef41Sopenharmony_ciThis can cause problems when accessing the underlying `ArrayBuffer` directly
15621cb0ef41Sopenharmony_ciusing `buf.buffer`, as other parts of the `ArrayBuffer` may be unrelated
15631cb0ef41Sopenharmony_cito the `Buffer` object itself.
15641cb0ef41Sopenharmony_ci
15651cb0ef41Sopenharmony_ciA common issue when creating a `TypedArray` object that shares its memory with
15661cb0ef41Sopenharmony_cia `Buffer` is that in this case one needs to specify the `byteOffset` correctly:
15671cb0ef41Sopenharmony_ci
15681cb0ef41Sopenharmony_ci```mjs
15691cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
15701cb0ef41Sopenharmony_ci
15711cb0ef41Sopenharmony_ci// Create a buffer smaller than `Buffer.poolSize`.
15721cb0ef41Sopenharmony_ciconst nodeBuffer = Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
15731cb0ef41Sopenharmony_ci
15741cb0ef41Sopenharmony_ci// When casting the Node.js Buffer to an Int8Array, use the byteOffset
15751cb0ef41Sopenharmony_ci// to refer only to the part of `nodeBuffer.buffer` that contains the memory
15761cb0ef41Sopenharmony_ci// for `nodeBuffer`.
15771cb0ef41Sopenharmony_cinew Int8Array(nodeBuffer.buffer, nodeBuffer.byteOffset, nodeBuffer.length);
15781cb0ef41Sopenharmony_ci```
15791cb0ef41Sopenharmony_ci
15801cb0ef41Sopenharmony_ci```cjs
15811cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
15821cb0ef41Sopenharmony_ci
15831cb0ef41Sopenharmony_ci// Create a buffer smaller than `Buffer.poolSize`.
15841cb0ef41Sopenharmony_ciconst nodeBuffer = Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
15851cb0ef41Sopenharmony_ci
15861cb0ef41Sopenharmony_ci// When casting the Node.js Buffer to an Int8Array, use the byteOffset
15871cb0ef41Sopenharmony_ci// to refer only to the part of `nodeBuffer.buffer` that contains the memory
15881cb0ef41Sopenharmony_ci// for `nodeBuffer`.
15891cb0ef41Sopenharmony_cinew Int8Array(nodeBuffer.buffer, nodeBuffer.byteOffset, nodeBuffer.length);
15901cb0ef41Sopenharmony_ci```
15911cb0ef41Sopenharmony_ci
15921cb0ef41Sopenharmony_ci### `buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])`
15931cb0ef41Sopenharmony_ci
15941cb0ef41Sopenharmony_ci<!-- YAML
15951cb0ef41Sopenharmony_ciadded: v0.11.13
15961cb0ef41Sopenharmony_cichanges:
15971cb0ef41Sopenharmony_ci  - version: v8.0.0
15981cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/10236
15991cb0ef41Sopenharmony_ci    description: The `target` parameter can now be a `Uint8Array`.
16001cb0ef41Sopenharmony_ci  - version: v5.11.0
16011cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/5880
16021cb0ef41Sopenharmony_ci    description: Additional parameters for specifying offsets are supported now.
16031cb0ef41Sopenharmony_ci-->
16041cb0ef41Sopenharmony_ci
16051cb0ef41Sopenharmony_ci* `target` {Buffer|Uint8Array} A `Buffer` or [`Uint8Array`][] with which to
16061cb0ef41Sopenharmony_ci  compare `buf`.
16071cb0ef41Sopenharmony_ci* `targetStart` {integer} The offset within `target` at which to begin
16081cb0ef41Sopenharmony_ci  comparison. **Default:** `0`.
16091cb0ef41Sopenharmony_ci* `targetEnd` {integer} The offset within `target` at which to end comparison
16101cb0ef41Sopenharmony_ci  (not inclusive). **Default:** `target.length`.
16111cb0ef41Sopenharmony_ci* `sourceStart` {integer} The offset within `buf` at which to begin comparison.
16121cb0ef41Sopenharmony_ci  **Default:** `0`.
16131cb0ef41Sopenharmony_ci* `sourceEnd` {integer} The offset within `buf` at which to end comparison
16141cb0ef41Sopenharmony_ci  (not inclusive). **Default:** [`buf.length`][].
16151cb0ef41Sopenharmony_ci* Returns: {integer}
16161cb0ef41Sopenharmony_ci
16171cb0ef41Sopenharmony_ciCompares `buf` with `target` and returns a number indicating whether `buf`
16181cb0ef41Sopenharmony_cicomes before, after, or is the same as `target` in sort order.
16191cb0ef41Sopenharmony_ciComparison is based on the actual sequence of bytes in each `Buffer`.
16201cb0ef41Sopenharmony_ci
16211cb0ef41Sopenharmony_ci* `0` is returned if `target` is the same as `buf`
16221cb0ef41Sopenharmony_ci* `1` is returned if `target` should come _before_ `buf` when sorted.
16231cb0ef41Sopenharmony_ci* `-1` is returned if `target` should come _after_ `buf` when sorted.
16241cb0ef41Sopenharmony_ci
16251cb0ef41Sopenharmony_ci```mjs
16261cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
16271cb0ef41Sopenharmony_ci
16281cb0ef41Sopenharmony_ciconst buf1 = Buffer.from('ABC');
16291cb0ef41Sopenharmony_ciconst buf2 = Buffer.from('BCD');
16301cb0ef41Sopenharmony_ciconst buf3 = Buffer.from('ABCD');
16311cb0ef41Sopenharmony_ci
16321cb0ef41Sopenharmony_ciconsole.log(buf1.compare(buf1));
16331cb0ef41Sopenharmony_ci// Prints: 0
16341cb0ef41Sopenharmony_ciconsole.log(buf1.compare(buf2));
16351cb0ef41Sopenharmony_ci// Prints: -1
16361cb0ef41Sopenharmony_ciconsole.log(buf1.compare(buf3));
16371cb0ef41Sopenharmony_ci// Prints: -1
16381cb0ef41Sopenharmony_ciconsole.log(buf2.compare(buf1));
16391cb0ef41Sopenharmony_ci// Prints: 1
16401cb0ef41Sopenharmony_ciconsole.log(buf2.compare(buf3));
16411cb0ef41Sopenharmony_ci// Prints: 1
16421cb0ef41Sopenharmony_ciconsole.log([buf1, buf2, buf3].sort(Buffer.compare));
16431cb0ef41Sopenharmony_ci// Prints: [ <Buffer 41 42 43>, <Buffer 41 42 43 44>, <Buffer 42 43 44> ]
16441cb0ef41Sopenharmony_ci// (This result is equal to: [buf1, buf3, buf2].)
16451cb0ef41Sopenharmony_ci```
16461cb0ef41Sopenharmony_ci
16471cb0ef41Sopenharmony_ci```cjs
16481cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
16491cb0ef41Sopenharmony_ci
16501cb0ef41Sopenharmony_ciconst buf1 = Buffer.from('ABC');
16511cb0ef41Sopenharmony_ciconst buf2 = Buffer.from('BCD');
16521cb0ef41Sopenharmony_ciconst buf3 = Buffer.from('ABCD');
16531cb0ef41Sopenharmony_ci
16541cb0ef41Sopenharmony_ciconsole.log(buf1.compare(buf1));
16551cb0ef41Sopenharmony_ci// Prints: 0
16561cb0ef41Sopenharmony_ciconsole.log(buf1.compare(buf2));
16571cb0ef41Sopenharmony_ci// Prints: -1
16581cb0ef41Sopenharmony_ciconsole.log(buf1.compare(buf3));
16591cb0ef41Sopenharmony_ci// Prints: -1
16601cb0ef41Sopenharmony_ciconsole.log(buf2.compare(buf1));
16611cb0ef41Sopenharmony_ci// Prints: 1
16621cb0ef41Sopenharmony_ciconsole.log(buf2.compare(buf3));
16631cb0ef41Sopenharmony_ci// Prints: 1
16641cb0ef41Sopenharmony_ciconsole.log([buf1, buf2, buf3].sort(Buffer.compare));
16651cb0ef41Sopenharmony_ci// Prints: [ <Buffer 41 42 43>, <Buffer 41 42 43 44>, <Buffer 42 43 44> ]
16661cb0ef41Sopenharmony_ci// (This result is equal to: [buf1, buf3, buf2].)
16671cb0ef41Sopenharmony_ci```
16681cb0ef41Sopenharmony_ci
16691cb0ef41Sopenharmony_ciThe optional `targetStart`, `targetEnd`, `sourceStart`, and `sourceEnd`
16701cb0ef41Sopenharmony_ciarguments can be used to limit the comparison to specific ranges within `target`
16711cb0ef41Sopenharmony_ciand `buf` respectively.
16721cb0ef41Sopenharmony_ci
16731cb0ef41Sopenharmony_ci```mjs
16741cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
16751cb0ef41Sopenharmony_ci
16761cb0ef41Sopenharmony_ciconst buf1 = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9]);
16771cb0ef41Sopenharmony_ciconst buf2 = Buffer.from([5, 6, 7, 8, 9, 1, 2, 3, 4]);
16781cb0ef41Sopenharmony_ci
16791cb0ef41Sopenharmony_ciconsole.log(buf1.compare(buf2, 5, 9, 0, 4));
16801cb0ef41Sopenharmony_ci// Prints: 0
16811cb0ef41Sopenharmony_ciconsole.log(buf1.compare(buf2, 0, 6, 4));
16821cb0ef41Sopenharmony_ci// Prints: -1
16831cb0ef41Sopenharmony_ciconsole.log(buf1.compare(buf2, 5, 6, 5));
16841cb0ef41Sopenharmony_ci// Prints: 1
16851cb0ef41Sopenharmony_ci```
16861cb0ef41Sopenharmony_ci
16871cb0ef41Sopenharmony_ci```cjs
16881cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
16891cb0ef41Sopenharmony_ci
16901cb0ef41Sopenharmony_ciconst buf1 = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9]);
16911cb0ef41Sopenharmony_ciconst buf2 = Buffer.from([5, 6, 7, 8, 9, 1, 2, 3, 4]);
16921cb0ef41Sopenharmony_ci
16931cb0ef41Sopenharmony_ciconsole.log(buf1.compare(buf2, 5, 9, 0, 4));
16941cb0ef41Sopenharmony_ci// Prints: 0
16951cb0ef41Sopenharmony_ciconsole.log(buf1.compare(buf2, 0, 6, 4));
16961cb0ef41Sopenharmony_ci// Prints: -1
16971cb0ef41Sopenharmony_ciconsole.log(buf1.compare(buf2, 5, 6, 5));
16981cb0ef41Sopenharmony_ci// Prints: 1
16991cb0ef41Sopenharmony_ci```
17001cb0ef41Sopenharmony_ci
17011cb0ef41Sopenharmony_ci[`ERR_OUT_OF_RANGE`][] is thrown if `targetStart < 0`, `sourceStart < 0`,
17021cb0ef41Sopenharmony_ci`targetEnd > target.byteLength`, or `sourceEnd > source.byteLength`.
17031cb0ef41Sopenharmony_ci
17041cb0ef41Sopenharmony_ci### `buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])`
17051cb0ef41Sopenharmony_ci
17061cb0ef41Sopenharmony_ci<!-- YAML
17071cb0ef41Sopenharmony_ciadded: v0.1.90
17081cb0ef41Sopenharmony_ci-->
17091cb0ef41Sopenharmony_ci
17101cb0ef41Sopenharmony_ci* `target` {Buffer|Uint8Array} A `Buffer` or [`Uint8Array`][] to copy into.
17111cb0ef41Sopenharmony_ci* `targetStart` {integer} The offset within `target` at which to begin
17121cb0ef41Sopenharmony_ci  writing. **Default:** `0`.
17131cb0ef41Sopenharmony_ci* `sourceStart` {integer} The offset within `buf` from which to begin copying.
17141cb0ef41Sopenharmony_ci  **Default:** `0`.
17151cb0ef41Sopenharmony_ci* `sourceEnd` {integer} The offset within `buf` at which to stop copying (not
17161cb0ef41Sopenharmony_ci  inclusive). **Default:** [`buf.length`][].
17171cb0ef41Sopenharmony_ci* Returns: {integer} The number of bytes copied.
17181cb0ef41Sopenharmony_ci
17191cb0ef41Sopenharmony_ciCopies data from a region of `buf` to a region in `target`, even if the `target`
17201cb0ef41Sopenharmony_cimemory region overlaps with `buf`.
17211cb0ef41Sopenharmony_ci
17221cb0ef41Sopenharmony_ci[`TypedArray.prototype.set()`][] performs the same operation, and is available
17231cb0ef41Sopenharmony_cifor all TypedArrays, including Node.js `Buffer`s, although it takes
17241cb0ef41Sopenharmony_cidifferent function arguments.
17251cb0ef41Sopenharmony_ci
17261cb0ef41Sopenharmony_ci```mjs
17271cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
17281cb0ef41Sopenharmony_ci
17291cb0ef41Sopenharmony_ci// Create two `Buffer` instances.
17301cb0ef41Sopenharmony_ciconst buf1 = Buffer.allocUnsafe(26);
17311cb0ef41Sopenharmony_ciconst buf2 = Buffer.allocUnsafe(26).fill('!');
17321cb0ef41Sopenharmony_ci
17331cb0ef41Sopenharmony_cifor (let i = 0; i < 26; i++) {
17341cb0ef41Sopenharmony_ci  // 97 is the decimal ASCII value for 'a'.
17351cb0ef41Sopenharmony_ci  buf1[i] = i + 97;
17361cb0ef41Sopenharmony_ci}
17371cb0ef41Sopenharmony_ci
17381cb0ef41Sopenharmony_ci// Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`.
17391cb0ef41Sopenharmony_cibuf1.copy(buf2, 8, 16, 20);
17401cb0ef41Sopenharmony_ci// This is equivalent to:
17411cb0ef41Sopenharmony_ci// buf2.set(buf1.subarray(16, 20), 8);
17421cb0ef41Sopenharmony_ci
17431cb0ef41Sopenharmony_ciconsole.log(buf2.toString('ascii', 0, 25));
17441cb0ef41Sopenharmony_ci// Prints: !!!!!!!!qrst!!!!!!!!!!!!!
17451cb0ef41Sopenharmony_ci```
17461cb0ef41Sopenharmony_ci
17471cb0ef41Sopenharmony_ci```cjs
17481cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
17491cb0ef41Sopenharmony_ci
17501cb0ef41Sopenharmony_ci// Create two `Buffer` instances.
17511cb0ef41Sopenharmony_ciconst buf1 = Buffer.allocUnsafe(26);
17521cb0ef41Sopenharmony_ciconst buf2 = Buffer.allocUnsafe(26).fill('!');
17531cb0ef41Sopenharmony_ci
17541cb0ef41Sopenharmony_cifor (let i = 0; i < 26; i++) {
17551cb0ef41Sopenharmony_ci  // 97 is the decimal ASCII value for 'a'.
17561cb0ef41Sopenharmony_ci  buf1[i] = i + 97;
17571cb0ef41Sopenharmony_ci}
17581cb0ef41Sopenharmony_ci
17591cb0ef41Sopenharmony_ci// Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`.
17601cb0ef41Sopenharmony_cibuf1.copy(buf2, 8, 16, 20);
17611cb0ef41Sopenharmony_ci// This is equivalent to:
17621cb0ef41Sopenharmony_ci// buf2.set(buf1.subarray(16, 20), 8);
17631cb0ef41Sopenharmony_ci
17641cb0ef41Sopenharmony_ciconsole.log(buf2.toString('ascii', 0, 25));
17651cb0ef41Sopenharmony_ci// Prints: !!!!!!!!qrst!!!!!!!!!!!!!
17661cb0ef41Sopenharmony_ci```
17671cb0ef41Sopenharmony_ci
17681cb0ef41Sopenharmony_ci```mjs
17691cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
17701cb0ef41Sopenharmony_ci
17711cb0ef41Sopenharmony_ci// Create a `Buffer` and copy data from one region to an overlapping region
17721cb0ef41Sopenharmony_ci// within the same `Buffer`.
17731cb0ef41Sopenharmony_ci
17741cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(26);
17751cb0ef41Sopenharmony_ci
17761cb0ef41Sopenharmony_cifor (let i = 0; i < 26; i++) {
17771cb0ef41Sopenharmony_ci  // 97 is the decimal ASCII value for 'a'.
17781cb0ef41Sopenharmony_ci  buf[i] = i + 97;
17791cb0ef41Sopenharmony_ci}
17801cb0ef41Sopenharmony_ci
17811cb0ef41Sopenharmony_cibuf.copy(buf, 0, 4, 10);
17821cb0ef41Sopenharmony_ci
17831cb0ef41Sopenharmony_ciconsole.log(buf.toString());
17841cb0ef41Sopenharmony_ci// Prints: efghijghijklmnopqrstuvwxyz
17851cb0ef41Sopenharmony_ci```
17861cb0ef41Sopenharmony_ci
17871cb0ef41Sopenharmony_ci```cjs
17881cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
17891cb0ef41Sopenharmony_ci
17901cb0ef41Sopenharmony_ci// Create a `Buffer` and copy data from one region to an overlapping region
17911cb0ef41Sopenharmony_ci// within the same `Buffer`.
17921cb0ef41Sopenharmony_ci
17931cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(26);
17941cb0ef41Sopenharmony_ci
17951cb0ef41Sopenharmony_cifor (let i = 0; i < 26; i++) {
17961cb0ef41Sopenharmony_ci  // 97 is the decimal ASCII value for 'a'.
17971cb0ef41Sopenharmony_ci  buf[i] = i + 97;
17981cb0ef41Sopenharmony_ci}
17991cb0ef41Sopenharmony_ci
18001cb0ef41Sopenharmony_cibuf.copy(buf, 0, 4, 10);
18011cb0ef41Sopenharmony_ci
18021cb0ef41Sopenharmony_ciconsole.log(buf.toString());
18031cb0ef41Sopenharmony_ci// Prints: efghijghijklmnopqrstuvwxyz
18041cb0ef41Sopenharmony_ci```
18051cb0ef41Sopenharmony_ci
18061cb0ef41Sopenharmony_ci### `buf.entries()`
18071cb0ef41Sopenharmony_ci
18081cb0ef41Sopenharmony_ci<!-- YAML
18091cb0ef41Sopenharmony_ciadded: v1.1.0
18101cb0ef41Sopenharmony_ci-->
18111cb0ef41Sopenharmony_ci
18121cb0ef41Sopenharmony_ci* Returns: {Iterator}
18131cb0ef41Sopenharmony_ci
18141cb0ef41Sopenharmony_ciCreates and returns an [iterator][] of `[index, byte]` pairs from the contents
18151cb0ef41Sopenharmony_ciof `buf`.
18161cb0ef41Sopenharmony_ci
18171cb0ef41Sopenharmony_ci```mjs
18181cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
18191cb0ef41Sopenharmony_ci
18201cb0ef41Sopenharmony_ci// Log the entire contents of a `Buffer`.
18211cb0ef41Sopenharmony_ci
18221cb0ef41Sopenharmony_ciconst buf = Buffer.from('buffer');
18231cb0ef41Sopenharmony_ci
18241cb0ef41Sopenharmony_cifor (const pair of buf.entries()) {
18251cb0ef41Sopenharmony_ci  console.log(pair);
18261cb0ef41Sopenharmony_ci}
18271cb0ef41Sopenharmony_ci// Prints:
18281cb0ef41Sopenharmony_ci//   [0, 98]
18291cb0ef41Sopenharmony_ci//   [1, 117]
18301cb0ef41Sopenharmony_ci//   [2, 102]
18311cb0ef41Sopenharmony_ci//   [3, 102]
18321cb0ef41Sopenharmony_ci//   [4, 101]
18331cb0ef41Sopenharmony_ci//   [5, 114]
18341cb0ef41Sopenharmony_ci```
18351cb0ef41Sopenharmony_ci
18361cb0ef41Sopenharmony_ci```cjs
18371cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
18381cb0ef41Sopenharmony_ci
18391cb0ef41Sopenharmony_ci// Log the entire contents of a `Buffer`.
18401cb0ef41Sopenharmony_ci
18411cb0ef41Sopenharmony_ciconst buf = Buffer.from('buffer');
18421cb0ef41Sopenharmony_ci
18431cb0ef41Sopenharmony_cifor (const pair of buf.entries()) {
18441cb0ef41Sopenharmony_ci  console.log(pair);
18451cb0ef41Sopenharmony_ci}
18461cb0ef41Sopenharmony_ci// Prints:
18471cb0ef41Sopenharmony_ci//   [0, 98]
18481cb0ef41Sopenharmony_ci//   [1, 117]
18491cb0ef41Sopenharmony_ci//   [2, 102]
18501cb0ef41Sopenharmony_ci//   [3, 102]
18511cb0ef41Sopenharmony_ci//   [4, 101]
18521cb0ef41Sopenharmony_ci//   [5, 114]
18531cb0ef41Sopenharmony_ci```
18541cb0ef41Sopenharmony_ci
18551cb0ef41Sopenharmony_ci### `buf.equals(otherBuffer)`
18561cb0ef41Sopenharmony_ci
18571cb0ef41Sopenharmony_ci<!-- YAML
18581cb0ef41Sopenharmony_ciadded: v0.11.13
18591cb0ef41Sopenharmony_cichanges:
18601cb0ef41Sopenharmony_ci  - version: v8.0.0
18611cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/10236
18621cb0ef41Sopenharmony_ci    description: The arguments can now be `Uint8Array`s.
18631cb0ef41Sopenharmony_ci-->
18641cb0ef41Sopenharmony_ci
18651cb0ef41Sopenharmony_ci* `otherBuffer` {Buffer|Uint8Array} A `Buffer` or [`Uint8Array`][] with which to
18661cb0ef41Sopenharmony_ci  compare `buf`.
18671cb0ef41Sopenharmony_ci* Returns: {boolean}
18681cb0ef41Sopenharmony_ci
18691cb0ef41Sopenharmony_ciReturns `true` if both `buf` and `otherBuffer` have exactly the same bytes,
18701cb0ef41Sopenharmony_ci`false` otherwise. Equivalent to
18711cb0ef41Sopenharmony_ci[`buf.compare(otherBuffer) === 0`][`buf.compare()`].
18721cb0ef41Sopenharmony_ci
18731cb0ef41Sopenharmony_ci```mjs
18741cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
18751cb0ef41Sopenharmony_ci
18761cb0ef41Sopenharmony_ciconst buf1 = Buffer.from('ABC');
18771cb0ef41Sopenharmony_ciconst buf2 = Buffer.from('414243', 'hex');
18781cb0ef41Sopenharmony_ciconst buf3 = Buffer.from('ABCD');
18791cb0ef41Sopenharmony_ci
18801cb0ef41Sopenharmony_ciconsole.log(buf1.equals(buf2));
18811cb0ef41Sopenharmony_ci// Prints: true
18821cb0ef41Sopenharmony_ciconsole.log(buf1.equals(buf3));
18831cb0ef41Sopenharmony_ci// Prints: false
18841cb0ef41Sopenharmony_ci```
18851cb0ef41Sopenharmony_ci
18861cb0ef41Sopenharmony_ci```cjs
18871cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
18881cb0ef41Sopenharmony_ci
18891cb0ef41Sopenharmony_ciconst buf1 = Buffer.from('ABC');
18901cb0ef41Sopenharmony_ciconst buf2 = Buffer.from('414243', 'hex');
18911cb0ef41Sopenharmony_ciconst buf3 = Buffer.from('ABCD');
18921cb0ef41Sopenharmony_ci
18931cb0ef41Sopenharmony_ciconsole.log(buf1.equals(buf2));
18941cb0ef41Sopenharmony_ci// Prints: true
18951cb0ef41Sopenharmony_ciconsole.log(buf1.equals(buf3));
18961cb0ef41Sopenharmony_ci// Prints: false
18971cb0ef41Sopenharmony_ci```
18981cb0ef41Sopenharmony_ci
18991cb0ef41Sopenharmony_ci### `buf.fill(value[, offset[, end]][, encoding])`
19001cb0ef41Sopenharmony_ci
19011cb0ef41Sopenharmony_ci<!-- YAML
19021cb0ef41Sopenharmony_ciadded: v0.5.0
19031cb0ef41Sopenharmony_cichanges:
19041cb0ef41Sopenharmony_ci  - version: v11.0.0
19051cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/22969
19061cb0ef41Sopenharmony_ci    description: Throws `ERR_OUT_OF_RANGE` instead of `ERR_INDEX_OUT_OF_RANGE`.
19071cb0ef41Sopenharmony_ci  - version: v10.0.0
19081cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18790
19091cb0ef41Sopenharmony_ci    description: Negative `end` values throw an `ERR_INDEX_OUT_OF_RANGE` error.
19101cb0ef41Sopenharmony_ci  - version: v10.0.0
19111cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18129
19121cb0ef41Sopenharmony_ci    description: Attempting to fill a non-zero length buffer with a zero length
19131cb0ef41Sopenharmony_ci                 buffer triggers a thrown exception.
19141cb0ef41Sopenharmony_ci  - version: v10.0.0
19151cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/17427
19161cb0ef41Sopenharmony_ci    description: Specifying an invalid string for `value` triggers a thrown
19171cb0ef41Sopenharmony_ci                 exception.
19181cb0ef41Sopenharmony_ci  - version: v5.7.0
19191cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/4935
19201cb0ef41Sopenharmony_ci    description: The `encoding` parameter is supported now.
19211cb0ef41Sopenharmony_ci-->
19221cb0ef41Sopenharmony_ci
19231cb0ef41Sopenharmony_ci* `value` {string|Buffer|Uint8Array|integer} The value with which to fill `buf`.
19241cb0ef41Sopenharmony_ci  Empty value (string, Uint8Array, Buffer) is coerced to `0`.
19251cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to fill `buf`.
19261cb0ef41Sopenharmony_ci  **Default:** `0`.
19271cb0ef41Sopenharmony_ci* `end` {integer} Where to stop filling `buf` (not inclusive). **Default:**
19281cb0ef41Sopenharmony_ci  [`buf.length`][].
19291cb0ef41Sopenharmony_ci* `encoding` {string} The encoding for `value` if `value` is a string.
19301cb0ef41Sopenharmony_ci  **Default:** `'utf8'`.
19311cb0ef41Sopenharmony_ci* Returns: {Buffer} A reference to `buf`.
19321cb0ef41Sopenharmony_ci
19331cb0ef41Sopenharmony_ciFills `buf` with the specified `value`. If the `offset` and `end` are not given,
19341cb0ef41Sopenharmony_cithe entire `buf` will be filled:
19351cb0ef41Sopenharmony_ci
19361cb0ef41Sopenharmony_ci```mjs
19371cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
19381cb0ef41Sopenharmony_ci
19391cb0ef41Sopenharmony_ci// Fill a `Buffer` with the ASCII character 'h'.
19401cb0ef41Sopenharmony_ci
19411cb0ef41Sopenharmony_ciconst b = Buffer.allocUnsafe(50).fill('h');
19421cb0ef41Sopenharmony_ci
19431cb0ef41Sopenharmony_ciconsole.log(b.toString());
19441cb0ef41Sopenharmony_ci// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
19451cb0ef41Sopenharmony_ci
19461cb0ef41Sopenharmony_ci// Fill a buffer with empty string
19471cb0ef41Sopenharmony_ciconst c = Buffer.allocUnsafe(5).fill('');
19481cb0ef41Sopenharmony_ci
19491cb0ef41Sopenharmony_ciconsole.log(c.fill(''));
19501cb0ef41Sopenharmony_ci// Prints: <Buffer 00 00 00 00 00>
19511cb0ef41Sopenharmony_ci```
19521cb0ef41Sopenharmony_ci
19531cb0ef41Sopenharmony_ci```cjs
19541cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
19551cb0ef41Sopenharmony_ci
19561cb0ef41Sopenharmony_ci// Fill a `Buffer` with the ASCII character 'h'.
19571cb0ef41Sopenharmony_ci
19581cb0ef41Sopenharmony_ciconst b = Buffer.allocUnsafe(50).fill('h');
19591cb0ef41Sopenharmony_ci
19601cb0ef41Sopenharmony_ciconsole.log(b.toString());
19611cb0ef41Sopenharmony_ci// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
19621cb0ef41Sopenharmony_ci
19631cb0ef41Sopenharmony_ci// Fill a buffer with empty string
19641cb0ef41Sopenharmony_ciconst c = Buffer.allocUnsafe(5).fill('');
19651cb0ef41Sopenharmony_ci
19661cb0ef41Sopenharmony_ciconsole.log(c.fill(''));
19671cb0ef41Sopenharmony_ci// Prints: <Buffer 00 00 00 00 00>
19681cb0ef41Sopenharmony_ci```
19691cb0ef41Sopenharmony_ci
19701cb0ef41Sopenharmony_ci`value` is coerced to a `uint32` value if it is not a string, `Buffer`, or
19711cb0ef41Sopenharmony_ciinteger. If the resulting integer is greater than `255` (decimal), `buf` will be
19721cb0ef41Sopenharmony_cifilled with `value & 255`.
19731cb0ef41Sopenharmony_ci
19741cb0ef41Sopenharmony_ciIf the final write of a `fill()` operation falls on a multi-byte character,
19751cb0ef41Sopenharmony_cithen only the bytes of that character that fit into `buf` are written:
19761cb0ef41Sopenharmony_ci
19771cb0ef41Sopenharmony_ci```mjs
19781cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
19791cb0ef41Sopenharmony_ci
19801cb0ef41Sopenharmony_ci// Fill a `Buffer` with character that takes up two bytes in UTF-8.
19811cb0ef41Sopenharmony_ci
19821cb0ef41Sopenharmony_ciconsole.log(Buffer.allocUnsafe(5).fill('\u0222'));
19831cb0ef41Sopenharmony_ci// Prints: <Buffer c8 a2 c8 a2 c8>
19841cb0ef41Sopenharmony_ci```
19851cb0ef41Sopenharmony_ci
19861cb0ef41Sopenharmony_ci```cjs
19871cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
19881cb0ef41Sopenharmony_ci
19891cb0ef41Sopenharmony_ci// Fill a `Buffer` with character that takes up two bytes in UTF-8.
19901cb0ef41Sopenharmony_ci
19911cb0ef41Sopenharmony_ciconsole.log(Buffer.allocUnsafe(5).fill('\u0222'));
19921cb0ef41Sopenharmony_ci// Prints: <Buffer c8 a2 c8 a2 c8>
19931cb0ef41Sopenharmony_ci```
19941cb0ef41Sopenharmony_ci
19951cb0ef41Sopenharmony_ciIf `value` contains invalid characters, it is truncated; if no valid
19961cb0ef41Sopenharmony_cifill data remains, an exception is thrown:
19971cb0ef41Sopenharmony_ci
19981cb0ef41Sopenharmony_ci```mjs
19991cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
20001cb0ef41Sopenharmony_ci
20011cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(5);
20021cb0ef41Sopenharmony_ci
20031cb0ef41Sopenharmony_ciconsole.log(buf.fill('a'));
20041cb0ef41Sopenharmony_ci// Prints: <Buffer 61 61 61 61 61>
20051cb0ef41Sopenharmony_ciconsole.log(buf.fill('aazz', 'hex'));
20061cb0ef41Sopenharmony_ci// Prints: <Buffer aa aa aa aa aa>
20071cb0ef41Sopenharmony_ciconsole.log(buf.fill('zz', 'hex'));
20081cb0ef41Sopenharmony_ci// Throws an exception.
20091cb0ef41Sopenharmony_ci```
20101cb0ef41Sopenharmony_ci
20111cb0ef41Sopenharmony_ci```cjs
20121cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
20131cb0ef41Sopenharmony_ci
20141cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(5);
20151cb0ef41Sopenharmony_ci
20161cb0ef41Sopenharmony_ciconsole.log(buf.fill('a'));
20171cb0ef41Sopenharmony_ci// Prints: <Buffer 61 61 61 61 61>
20181cb0ef41Sopenharmony_ciconsole.log(buf.fill('aazz', 'hex'));
20191cb0ef41Sopenharmony_ci// Prints: <Buffer aa aa aa aa aa>
20201cb0ef41Sopenharmony_ciconsole.log(buf.fill('zz', 'hex'));
20211cb0ef41Sopenharmony_ci// Throws an exception.
20221cb0ef41Sopenharmony_ci```
20231cb0ef41Sopenharmony_ci
20241cb0ef41Sopenharmony_ci### `buf.includes(value[, byteOffset][, encoding])`
20251cb0ef41Sopenharmony_ci
20261cb0ef41Sopenharmony_ci<!-- YAML
20271cb0ef41Sopenharmony_ciadded: v5.3.0
20281cb0ef41Sopenharmony_ci-->
20291cb0ef41Sopenharmony_ci
20301cb0ef41Sopenharmony_ci* `value` {string|Buffer|Uint8Array|integer} What to search for.
20311cb0ef41Sopenharmony_ci* `byteOffset` {integer} Where to begin searching in `buf`. If negative, then
20321cb0ef41Sopenharmony_ci  offset is calculated from the end of `buf`. **Default:** `0`.
20331cb0ef41Sopenharmony_ci* `encoding` {string} If `value` is a string, this is its encoding.
20341cb0ef41Sopenharmony_ci  **Default:** `'utf8'`.
20351cb0ef41Sopenharmony_ci* Returns: {boolean} `true` if `value` was found in `buf`, `false` otherwise.
20361cb0ef41Sopenharmony_ci
20371cb0ef41Sopenharmony_ciEquivalent to [`buf.indexOf() !== -1`][`buf.indexOf()`].
20381cb0ef41Sopenharmony_ci
20391cb0ef41Sopenharmony_ci```mjs
20401cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
20411cb0ef41Sopenharmony_ci
20421cb0ef41Sopenharmony_ciconst buf = Buffer.from('this is a buffer');
20431cb0ef41Sopenharmony_ci
20441cb0ef41Sopenharmony_ciconsole.log(buf.includes('this'));
20451cb0ef41Sopenharmony_ci// Prints: true
20461cb0ef41Sopenharmony_ciconsole.log(buf.includes('is'));
20471cb0ef41Sopenharmony_ci// Prints: true
20481cb0ef41Sopenharmony_ciconsole.log(buf.includes(Buffer.from('a buffer')));
20491cb0ef41Sopenharmony_ci// Prints: true
20501cb0ef41Sopenharmony_ciconsole.log(buf.includes(97));
20511cb0ef41Sopenharmony_ci// Prints: true (97 is the decimal ASCII value for 'a')
20521cb0ef41Sopenharmony_ciconsole.log(buf.includes(Buffer.from('a buffer example')));
20531cb0ef41Sopenharmony_ci// Prints: false
20541cb0ef41Sopenharmony_ciconsole.log(buf.includes(Buffer.from('a buffer example').slice(0, 8)));
20551cb0ef41Sopenharmony_ci// Prints: true
20561cb0ef41Sopenharmony_ciconsole.log(buf.includes('this', 4));
20571cb0ef41Sopenharmony_ci// Prints: false
20581cb0ef41Sopenharmony_ci```
20591cb0ef41Sopenharmony_ci
20601cb0ef41Sopenharmony_ci```cjs
20611cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
20621cb0ef41Sopenharmony_ci
20631cb0ef41Sopenharmony_ciconst buf = Buffer.from('this is a buffer');
20641cb0ef41Sopenharmony_ci
20651cb0ef41Sopenharmony_ciconsole.log(buf.includes('this'));
20661cb0ef41Sopenharmony_ci// Prints: true
20671cb0ef41Sopenharmony_ciconsole.log(buf.includes('is'));
20681cb0ef41Sopenharmony_ci// Prints: true
20691cb0ef41Sopenharmony_ciconsole.log(buf.includes(Buffer.from('a buffer')));
20701cb0ef41Sopenharmony_ci// Prints: true
20711cb0ef41Sopenharmony_ciconsole.log(buf.includes(97));
20721cb0ef41Sopenharmony_ci// Prints: true (97 is the decimal ASCII value for 'a')
20731cb0ef41Sopenharmony_ciconsole.log(buf.includes(Buffer.from('a buffer example')));
20741cb0ef41Sopenharmony_ci// Prints: false
20751cb0ef41Sopenharmony_ciconsole.log(buf.includes(Buffer.from('a buffer example').slice(0, 8)));
20761cb0ef41Sopenharmony_ci// Prints: true
20771cb0ef41Sopenharmony_ciconsole.log(buf.includes('this', 4));
20781cb0ef41Sopenharmony_ci// Prints: false
20791cb0ef41Sopenharmony_ci```
20801cb0ef41Sopenharmony_ci
20811cb0ef41Sopenharmony_ci### `buf.indexOf(value[, byteOffset][, encoding])`
20821cb0ef41Sopenharmony_ci
20831cb0ef41Sopenharmony_ci<!-- YAML
20841cb0ef41Sopenharmony_ciadded: v1.5.0
20851cb0ef41Sopenharmony_cichanges:
20861cb0ef41Sopenharmony_ci  - version: v8.0.0
20871cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/10236
20881cb0ef41Sopenharmony_ci    description: The `value` can now be a `Uint8Array`.
20891cb0ef41Sopenharmony_ci  - version:
20901cb0ef41Sopenharmony_ci    - v5.7.0
20911cb0ef41Sopenharmony_ci    - v4.4.0
20921cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/4803
20931cb0ef41Sopenharmony_ci    description: When `encoding` is being passed, the `byteOffset` parameter
20941cb0ef41Sopenharmony_ci                 is no longer required.
20951cb0ef41Sopenharmony_ci-->
20961cb0ef41Sopenharmony_ci
20971cb0ef41Sopenharmony_ci* `value` {string|Buffer|Uint8Array|integer} What to search for.
20981cb0ef41Sopenharmony_ci* `byteOffset` {integer} Where to begin searching in `buf`. If negative, then
20991cb0ef41Sopenharmony_ci  offset is calculated from the end of `buf`. **Default:** `0`.
21001cb0ef41Sopenharmony_ci* `encoding` {string} If `value` is a string, this is the encoding used to
21011cb0ef41Sopenharmony_ci  determine the binary representation of the string that will be searched for in
21021cb0ef41Sopenharmony_ci  `buf`. **Default:** `'utf8'`.
21031cb0ef41Sopenharmony_ci* Returns: {integer} The index of the first occurrence of `value` in `buf`, or
21041cb0ef41Sopenharmony_ci  `-1` if `buf` does not contain `value`.
21051cb0ef41Sopenharmony_ci
21061cb0ef41Sopenharmony_ciIf `value` is:
21071cb0ef41Sopenharmony_ci
21081cb0ef41Sopenharmony_ci* a string, `value` is interpreted according to the character encoding in
21091cb0ef41Sopenharmony_ci  `encoding`.
21101cb0ef41Sopenharmony_ci* a `Buffer` or [`Uint8Array`][], `value` will be used in its entirety.
21111cb0ef41Sopenharmony_ci  To compare a partial `Buffer`, use [`buf.subarray`][].
21121cb0ef41Sopenharmony_ci* a number, `value` will be interpreted as an unsigned 8-bit integer
21131cb0ef41Sopenharmony_ci  value between `0` and `255`.
21141cb0ef41Sopenharmony_ci
21151cb0ef41Sopenharmony_ci```mjs
21161cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
21171cb0ef41Sopenharmony_ci
21181cb0ef41Sopenharmony_ciconst buf = Buffer.from('this is a buffer');
21191cb0ef41Sopenharmony_ci
21201cb0ef41Sopenharmony_ciconsole.log(buf.indexOf('this'));
21211cb0ef41Sopenharmony_ci// Prints: 0
21221cb0ef41Sopenharmony_ciconsole.log(buf.indexOf('is'));
21231cb0ef41Sopenharmony_ci// Prints: 2
21241cb0ef41Sopenharmony_ciconsole.log(buf.indexOf(Buffer.from('a buffer')));
21251cb0ef41Sopenharmony_ci// Prints: 8
21261cb0ef41Sopenharmony_ciconsole.log(buf.indexOf(97));
21271cb0ef41Sopenharmony_ci// Prints: 8 (97 is the decimal ASCII value for 'a')
21281cb0ef41Sopenharmony_ciconsole.log(buf.indexOf(Buffer.from('a buffer example')));
21291cb0ef41Sopenharmony_ci// Prints: -1
21301cb0ef41Sopenharmony_ciconsole.log(buf.indexOf(Buffer.from('a buffer example').slice(0, 8)));
21311cb0ef41Sopenharmony_ci// Prints: 8
21321cb0ef41Sopenharmony_ci
21331cb0ef41Sopenharmony_ciconst utf16Buffer = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'utf16le');
21341cb0ef41Sopenharmony_ci
21351cb0ef41Sopenharmony_ciconsole.log(utf16Buffer.indexOf('\u03a3', 0, 'utf16le'));
21361cb0ef41Sopenharmony_ci// Prints: 4
21371cb0ef41Sopenharmony_ciconsole.log(utf16Buffer.indexOf('\u03a3', -4, 'utf16le'));
21381cb0ef41Sopenharmony_ci// Prints: 6
21391cb0ef41Sopenharmony_ci```
21401cb0ef41Sopenharmony_ci
21411cb0ef41Sopenharmony_ci```cjs
21421cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
21431cb0ef41Sopenharmony_ci
21441cb0ef41Sopenharmony_ciconst buf = Buffer.from('this is a buffer');
21451cb0ef41Sopenharmony_ci
21461cb0ef41Sopenharmony_ciconsole.log(buf.indexOf('this'));
21471cb0ef41Sopenharmony_ci// Prints: 0
21481cb0ef41Sopenharmony_ciconsole.log(buf.indexOf('is'));
21491cb0ef41Sopenharmony_ci// Prints: 2
21501cb0ef41Sopenharmony_ciconsole.log(buf.indexOf(Buffer.from('a buffer')));
21511cb0ef41Sopenharmony_ci// Prints: 8
21521cb0ef41Sopenharmony_ciconsole.log(buf.indexOf(97));
21531cb0ef41Sopenharmony_ci// Prints: 8 (97 is the decimal ASCII value for 'a')
21541cb0ef41Sopenharmony_ciconsole.log(buf.indexOf(Buffer.from('a buffer example')));
21551cb0ef41Sopenharmony_ci// Prints: -1
21561cb0ef41Sopenharmony_ciconsole.log(buf.indexOf(Buffer.from('a buffer example').slice(0, 8)));
21571cb0ef41Sopenharmony_ci// Prints: 8
21581cb0ef41Sopenharmony_ci
21591cb0ef41Sopenharmony_ciconst utf16Buffer = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'utf16le');
21601cb0ef41Sopenharmony_ci
21611cb0ef41Sopenharmony_ciconsole.log(utf16Buffer.indexOf('\u03a3', 0, 'utf16le'));
21621cb0ef41Sopenharmony_ci// Prints: 4
21631cb0ef41Sopenharmony_ciconsole.log(utf16Buffer.indexOf('\u03a3', -4, 'utf16le'));
21641cb0ef41Sopenharmony_ci// Prints: 6
21651cb0ef41Sopenharmony_ci```
21661cb0ef41Sopenharmony_ci
21671cb0ef41Sopenharmony_ciIf `value` is not a string, number, or `Buffer`, this method will throw a
21681cb0ef41Sopenharmony_ci`TypeError`. If `value` is a number, it will be coerced to a valid byte value,
21691cb0ef41Sopenharmony_cian integer between 0 and 255.
21701cb0ef41Sopenharmony_ci
21711cb0ef41Sopenharmony_ciIf `byteOffset` is not a number, it will be coerced to a number. If the result
21721cb0ef41Sopenharmony_ciof coercion is `NaN` or `0`, then the entire buffer will be searched. This
21731cb0ef41Sopenharmony_cibehavior matches [`String.prototype.indexOf()`][].
21741cb0ef41Sopenharmony_ci
21751cb0ef41Sopenharmony_ci```mjs
21761cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
21771cb0ef41Sopenharmony_ci
21781cb0ef41Sopenharmony_ciconst b = Buffer.from('abcdef');
21791cb0ef41Sopenharmony_ci
21801cb0ef41Sopenharmony_ci// Passing a value that's a number, but not a valid byte.
21811cb0ef41Sopenharmony_ci// Prints: 2, equivalent to searching for 99 or 'c'.
21821cb0ef41Sopenharmony_ciconsole.log(b.indexOf(99.9));
21831cb0ef41Sopenharmony_ciconsole.log(b.indexOf(256 + 99));
21841cb0ef41Sopenharmony_ci
21851cb0ef41Sopenharmony_ci// Passing a byteOffset that coerces to NaN or 0.
21861cb0ef41Sopenharmony_ci// Prints: 1, searching the whole buffer.
21871cb0ef41Sopenharmony_ciconsole.log(b.indexOf('b', undefined));
21881cb0ef41Sopenharmony_ciconsole.log(b.indexOf('b', {}));
21891cb0ef41Sopenharmony_ciconsole.log(b.indexOf('b', null));
21901cb0ef41Sopenharmony_ciconsole.log(b.indexOf('b', []));
21911cb0ef41Sopenharmony_ci```
21921cb0ef41Sopenharmony_ci
21931cb0ef41Sopenharmony_ci```cjs
21941cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
21951cb0ef41Sopenharmony_ci
21961cb0ef41Sopenharmony_ciconst b = Buffer.from('abcdef');
21971cb0ef41Sopenharmony_ci
21981cb0ef41Sopenharmony_ci// Passing a value that's a number, but not a valid byte.
21991cb0ef41Sopenharmony_ci// Prints: 2, equivalent to searching for 99 or 'c'.
22001cb0ef41Sopenharmony_ciconsole.log(b.indexOf(99.9));
22011cb0ef41Sopenharmony_ciconsole.log(b.indexOf(256 + 99));
22021cb0ef41Sopenharmony_ci
22031cb0ef41Sopenharmony_ci// Passing a byteOffset that coerces to NaN or 0.
22041cb0ef41Sopenharmony_ci// Prints: 1, searching the whole buffer.
22051cb0ef41Sopenharmony_ciconsole.log(b.indexOf('b', undefined));
22061cb0ef41Sopenharmony_ciconsole.log(b.indexOf('b', {}));
22071cb0ef41Sopenharmony_ciconsole.log(b.indexOf('b', null));
22081cb0ef41Sopenharmony_ciconsole.log(b.indexOf('b', []));
22091cb0ef41Sopenharmony_ci```
22101cb0ef41Sopenharmony_ci
22111cb0ef41Sopenharmony_ciIf `value` is an empty string or empty `Buffer` and `byteOffset` is less
22121cb0ef41Sopenharmony_cithan `buf.length`, `byteOffset` will be returned. If `value` is empty and
22131cb0ef41Sopenharmony_ci`byteOffset` is at least `buf.length`, `buf.length` will be returned.
22141cb0ef41Sopenharmony_ci
22151cb0ef41Sopenharmony_ci### `buf.keys()`
22161cb0ef41Sopenharmony_ci
22171cb0ef41Sopenharmony_ci<!-- YAML
22181cb0ef41Sopenharmony_ciadded: v1.1.0
22191cb0ef41Sopenharmony_ci-->
22201cb0ef41Sopenharmony_ci
22211cb0ef41Sopenharmony_ci* Returns: {Iterator}
22221cb0ef41Sopenharmony_ci
22231cb0ef41Sopenharmony_ciCreates and returns an [iterator][] of `buf` keys (indices).
22241cb0ef41Sopenharmony_ci
22251cb0ef41Sopenharmony_ci```mjs
22261cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
22271cb0ef41Sopenharmony_ci
22281cb0ef41Sopenharmony_ciconst buf = Buffer.from('buffer');
22291cb0ef41Sopenharmony_ci
22301cb0ef41Sopenharmony_cifor (const key of buf.keys()) {
22311cb0ef41Sopenharmony_ci  console.log(key);
22321cb0ef41Sopenharmony_ci}
22331cb0ef41Sopenharmony_ci// Prints:
22341cb0ef41Sopenharmony_ci//   0
22351cb0ef41Sopenharmony_ci//   1
22361cb0ef41Sopenharmony_ci//   2
22371cb0ef41Sopenharmony_ci//   3
22381cb0ef41Sopenharmony_ci//   4
22391cb0ef41Sopenharmony_ci//   5
22401cb0ef41Sopenharmony_ci```
22411cb0ef41Sopenharmony_ci
22421cb0ef41Sopenharmony_ci```cjs
22431cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
22441cb0ef41Sopenharmony_ci
22451cb0ef41Sopenharmony_ciconst buf = Buffer.from('buffer');
22461cb0ef41Sopenharmony_ci
22471cb0ef41Sopenharmony_cifor (const key of buf.keys()) {
22481cb0ef41Sopenharmony_ci  console.log(key);
22491cb0ef41Sopenharmony_ci}
22501cb0ef41Sopenharmony_ci// Prints:
22511cb0ef41Sopenharmony_ci//   0
22521cb0ef41Sopenharmony_ci//   1
22531cb0ef41Sopenharmony_ci//   2
22541cb0ef41Sopenharmony_ci//   3
22551cb0ef41Sopenharmony_ci//   4
22561cb0ef41Sopenharmony_ci//   5
22571cb0ef41Sopenharmony_ci```
22581cb0ef41Sopenharmony_ci
22591cb0ef41Sopenharmony_ci### `buf.lastIndexOf(value[, byteOffset][, encoding])`
22601cb0ef41Sopenharmony_ci
22611cb0ef41Sopenharmony_ci<!-- YAML
22621cb0ef41Sopenharmony_ciadded: v6.0.0
22631cb0ef41Sopenharmony_cichanges:
22641cb0ef41Sopenharmony_ci  - version: v8.0.0
22651cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/10236
22661cb0ef41Sopenharmony_ci    description: The `value` can now be a `Uint8Array`.
22671cb0ef41Sopenharmony_ci-->
22681cb0ef41Sopenharmony_ci
22691cb0ef41Sopenharmony_ci* `value` {string|Buffer|Uint8Array|integer} What to search for.
22701cb0ef41Sopenharmony_ci* `byteOffset` {integer} Where to begin searching in `buf`. If negative, then
22711cb0ef41Sopenharmony_ci  offset is calculated from the end of `buf`. **Default:**
22721cb0ef41Sopenharmony_ci  `buf.length - 1`.
22731cb0ef41Sopenharmony_ci* `encoding` {string} If `value` is a string, this is the encoding used to
22741cb0ef41Sopenharmony_ci  determine the binary representation of the string that will be searched for in
22751cb0ef41Sopenharmony_ci  `buf`. **Default:** `'utf8'`.
22761cb0ef41Sopenharmony_ci* Returns: {integer} The index of the last occurrence of `value` in `buf`, or
22771cb0ef41Sopenharmony_ci  `-1` if `buf` does not contain `value`.
22781cb0ef41Sopenharmony_ci
22791cb0ef41Sopenharmony_ciIdentical to [`buf.indexOf()`][], except the last occurrence of `value` is found
22801cb0ef41Sopenharmony_cirather than the first occurrence.
22811cb0ef41Sopenharmony_ci
22821cb0ef41Sopenharmony_ci```mjs
22831cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
22841cb0ef41Sopenharmony_ci
22851cb0ef41Sopenharmony_ciconst buf = Buffer.from('this buffer is a buffer');
22861cb0ef41Sopenharmony_ci
22871cb0ef41Sopenharmony_ciconsole.log(buf.lastIndexOf('this'));
22881cb0ef41Sopenharmony_ci// Prints: 0
22891cb0ef41Sopenharmony_ciconsole.log(buf.lastIndexOf('buffer'));
22901cb0ef41Sopenharmony_ci// Prints: 17
22911cb0ef41Sopenharmony_ciconsole.log(buf.lastIndexOf(Buffer.from('buffer')));
22921cb0ef41Sopenharmony_ci// Prints: 17
22931cb0ef41Sopenharmony_ciconsole.log(buf.lastIndexOf(97));
22941cb0ef41Sopenharmony_ci// Prints: 15 (97 is the decimal ASCII value for 'a')
22951cb0ef41Sopenharmony_ciconsole.log(buf.lastIndexOf(Buffer.from('yolo')));
22961cb0ef41Sopenharmony_ci// Prints: -1
22971cb0ef41Sopenharmony_ciconsole.log(buf.lastIndexOf('buffer', 5));
22981cb0ef41Sopenharmony_ci// Prints: 5
22991cb0ef41Sopenharmony_ciconsole.log(buf.lastIndexOf('buffer', 4));
23001cb0ef41Sopenharmony_ci// Prints: -1
23011cb0ef41Sopenharmony_ci
23021cb0ef41Sopenharmony_ciconst utf16Buffer = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'utf16le');
23031cb0ef41Sopenharmony_ci
23041cb0ef41Sopenharmony_ciconsole.log(utf16Buffer.lastIndexOf('\u03a3', undefined, 'utf16le'));
23051cb0ef41Sopenharmony_ci// Prints: 6
23061cb0ef41Sopenharmony_ciconsole.log(utf16Buffer.lastIndexOf('\u03a3', -5, 'utf16le'));
23071cb0ef41Sopenharmony_ci// Prints: 4
23081cb0ef41Sopenharmony_ci```
23091cb0ef41Sopenharmony_ci
23101cb0ef41Sopenharmony_ci```cjs
23111cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
23121cb0ef41Sopenharmony_ci
23131cb0ef41Sopenharmony_ciconst buf = Buffer.from('this buffer is a buffer');
23141cb0ef41Sopenharmony_ci
23151cb0ef41Sopenharmony_ciconsole.log(buf.lastIndexOf('this'));
23161cb0ef41Sopenharmony_ci// Prints: 0
23171cb0ef41Sopenharmony_ciconsole.log(buf.lastIndexOf('buffer'));
23181cb0ef41Sopenharmony_ci// Prints: 17
23191cb0ef41Sopenharmony_ciconsole.log(buf.lastIndexOf(Buffer.from('buffer')));
23201cb0ef41Sopenharmony_ci// Prints: 17
23211cb0ef41Sopenharmony_ciconsole.log(buf.lastIndexOf(97));
23221cb0ef41Sopenharmony_ci// Prints: 15 (97 is the decimal ASCII value for 'a')
23231cb0ef41Sopenharmony_ciconsole.log(buf.lastIndexOf(Buffer.from('yolo')));
23241cb0ef41Sopenharmony_ci// Prints: -1
23251cb0ef41Sopenharmony_ciconsole.log(buf.lastIndexOf('buffer', 5));
23261cb0ef41Sopenharmony_ci// Prints: 5
23271cb0ef41Sopenharmony_ciconsole.log(buf.lastIndexOf('buffer', 4));
23281cb0ef41Sopenharmony_ci// Prints: -1
23291cb0ef41Sopenharmony_ci
23301cb0ef41Sopenharmony_ciconst utf16Buffer = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'utf16le');
23311cb0ef41Sopenharmony_ci
23321cb0ef41Sopenharmony_ciconsole.log(utf16Buffer.lastIndexOf('\u03a3', undefined, 'utf16le'));
23331cb0ef41Sopenharmony_ci// Prints: 6
23341cb0ef41Sopenharmony_ciconsole.log(utf16Buffer.lastIndexOf('\u03a3', -5, 'utf16le'));
23351cb0ef41Sopenharmony_ci// Prints: 4
23361cb0ef41Sopenharmony_ci```
23371cb0ef41Sopenharmony_ci
23381cb0ef41Sopenharmony_ciIf `value` is not a string, number, or `Buffer`, this method will throw a
23391cb0ef41Sopenharmony_ci`TypeError`. If `value` is a number, it will be coerced to a valid byte value,
23401cb0ef41Sopenharmony_cian integer between 0 and 255.
23411cb0ef41Sopenharmony_ci
23421cb0ef41Sopenharmony_ciIf `byteOffset` is not a number, it will be coerced to a number. Any arguments
23431cb0ef41Sopenharmony_cithat coerce to `NaN`, like `{}` or `undefined`, will search the whole buffer.
23441cb0ef41Sopenharmony_ciThis behavior matches [`String.prototype.lastIndexOf()`][].
23451cb0ef41Sopenharmony_ci
23461cb0ef41Sopenharmony_ci```mjs
23471cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
23481cb0ef41Sopenharmony_ci
23491cb0ef41Sopenharmony_ciconst b = Buffer.from('abcdef');
23501cb0ef41Sopenharmony_ci
23511cb0ef41Sopenharmony_ci// Passing a value that's a number, but not a valid byte.
23521cb0ef41Sopenharmony_ci// Prints: 2, equivalent to searching for 99 or 'c'.
23531cb0ef41Sopenharmony_ciconsole.log(b.lastIndexOf(99.9));
23541cb0ef41Sopenharmony_ciconsole.log(b.lastIndexOf(256 + 99));
23551cb0ef41Sopenharmony_ci
23561cb0ef41Sopenharmony_ci// Passing a byteOffset that coerces to NaN.
23571cb0ef41Sopenharmony_ci// Prints: 1, searching the whole buffer.
23581cb0ef41Sopenharmony_ciconsole.log(b.lastIndexOf('b', undefined));
23591cb0ef41Sopenharmony_ciconsole.log(b.lastIndexOf('b', {}));
23601cb0ef41Sopenharmony_ci
23611cb0ef41Sopenharmony_ci// Passing a byteOffset that coerces to 0.
23621cb0ef41Sopenharmony_ci// Prints: -1, equivalent to passing 0.
23631cb0ef41Sopenharmony_ciconsole.log(b.lastIndexOf('b', null));
23641cb0ef41Sopenharmony_ciconsole.log(b.lastIndexOf('b', []));
23651cb0ef41Sopenharmony_ci```
23661cb0ef41Sopenharmony_ci
23671cb0ef41Sopenharmony_ci```cjs
23681cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
23691cb0ef41Sopenharmony_ci
23701cb0ef41Sopenharmony_ciconst b = Buffer.from('abcdef');
23711cb0ef41Sopenharmony_ci
23721cb0ef41Sopenharmony_ci// Passing a value that's a number, but not a valid byte.
23731cb0ef41Sopenharmony_ci// Prints: 2, equivalent to searching for 99 or 'c'.
23741cb0ef41Sopenharmony_ciconsole.log(b.lastIndexOf(99.9));
23751cb0ef41Sopenharmony_ciconsole.log(b.lastIndexOf(256 + 99));
23761cb0ef41Sopenharmony_ci
23771cb0ef41Sopenharmony_ci// Passing a byteOffset that coerces to NaN.
23781cb0ef41Sopenharmony_ci// Prints: 1, searching the whole buffer.
23791cb0ef41Sopenharmony_ciconsole.log(b.lastIndexOf('b', undefined));
23801cb0ef41Sopenharmony_ciconsole.log(b.lastIndexOf('b', {}));
23811cb0ef41Sopenharmony_ci
23821cb0ef41Sopenharmony_ci// Passing a byteOffset that coerces to 0.
23831cb0ef41Sopenharmony_ci// Prints: -1, equivalent to passing 0.
23841cb0ef41Sopenharmony_ciconsole.log(b.lastIndexOf('b', null));
23851cb0ef41Sopenharmony_ciconsole.log(b.lastIndexOf('b', []));
23861cb0ef41Sopenharmony_ci```
23871cb0ef41Sopenharmony_ci
23881cb0ef41Sopenharmony_ciIf `value` is an empty string or empty `Buffer`, `byteOffset` will be returned.
23891cb0ef41Sopenharmony_ci
23901cb0ef41Sopenharmony_ci### `buf.length`
23911cb0ef41Sopenharmony_ci
23921cb0ef41Sopenharmony_ci<!-- YAML
23931cb0ef41Sopenharmony_ciadded: v0.1.90
23941cb0ef41Sopenharmony_ci-->
23951cb0ef41Sopenharmony_ci
23961cb0ef41Sopenharmony_ci* {integer}
23971cb0ef41Sopenharmony_ci
23981cb0ef41Sopenharmony_ciReturns the number of bytes in `buf`.
23991cb0ef41Sopenharmony_ci
24001cb0ef41Sopenharmony_ci```mjs
24011cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
24021cb0ef41Sopenharmony_ci
24031cb0ef41Sopenharmony_ci// Create a `Buffer` and write a shorter string to it using UTF-8.
24041cb0ef41Sopenharmony_ci
24051cb0ef41Sopenharmony_ciconst buf = Buffer.alloc(1234);
24061cb0ef41Sopenharmony_ci
24071cb0ef41Sopenharmony_ciconsole.log(buf.length);
24081cb0ef41Sopenharmony_ci// Prints: 1234
24091cb0ef41Sopenharmony_ci
24101cb0ef41Sopenharmony_cibuf.write('some string', 0, 'utf8');
24111cb0ef41Sopenharmony_ci
24121cb0ef41Sopenharmony_ciconsole.log(buf.length);
24131cb0ef41Sopenharmony_ci// Prints: 1234
24141cb0ef41Sopenharmony_ci```
24151cb0ef41Sopenharmony_ci
24161cb0ef41Sopenharmony_ci```cjs
24171cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
24181cb0ef41Sopenharmony_ci
24191cb0ef41Sopenharmony_ci// Create a `Buffer` and write a shorter string to it using UTF-8.
24201cb0ef41Sopenharmony_ci
24211cb0ef41Sopenharmony_ciconst buf = Buffer.alloc(1234);
24221cb0ef41Sopenharmony_ci
24231cb0ef41Sopenharmony_ciconsole.log(buf.length);
24241cb0ef41Sopenharmony_ci// Prints: 1234
24251cb0ef41Sopenharmony_ci
24261cb0ef41Sopenharmony_cibuf.write('some string', 0, 'utf8');
24271cb0ef41Sopenharmony_ci
24281cb0ef41Sopenharmony_ciconsole.log(buf.length);
24291cb0ef41Sopenharmony_ci// Prints: 1234
24301cb0ef41Sopenharmony_ci```
24311cb0ef41Sopenharmony_ci
24321cb0ef41Sopenharmony_ci### `buf.parent`
24331cb0ef41Sopenharmony_ci
24341cb0ef41Sopenharmony_ci<!-- YAML
24351cb0ef41Sopenharmony_cideprecated: v8.0.0
24361cb0ef41Sopenharmony_ci-->
24371cb0ef41Sopenharmony_ci
24381cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use [`buf.buffer`][] instead.
24391cb0ef41Sopenharmony_ci
24401cb0ef41Sopenharmony_ciThe `buf.parent` property is a deprecated alias for `buf.buffer`.
24411cb0ef41Sopenharmony_ci
24421cb0ef41Sopenharmony_ci### `buf.readBigInt64BE([offset])`
24431cb0ef41Sopenharmony_ci
24441cb0ef41Sopenharmony_ci<!-- YAML
24451cb0ef41Sopenharmony_ciadded:
24461cb0ef41Sopenharmony_ci - v12.0.0
24471cb0ef41Sopenharmony_ci - v10.20.0
24481cb0ef41Sopenharmony_ci-->
24491cb0ef41Sopenharmony_ci
24501cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to read. Must
24511cb0ef41Sopenharmony_ci  satisfy: `0 <= offset <= buf.length - 8`. **Default:** `0`.
24521cb0ef41Sopenharmony_ci* Returns: {bigint}
24531cb0ef41Sopenharmony_ci
24541cb0ef41Sopenharmony_ciReads a signed, big-endian 64-bit integer from `buf` at the specified `offset`.
24551cb0ef41Sopenharmony_ci
24561cb0ef41Sopenharmony_ciIntegers read from a `Buffer` are interpreted as two's complement signed
24571cb0ef41Sopenharmony_civalues.
24581cb0ef41Sopenharmony_ci
24591cb0ef41Sopenharmony_ci### `buf.readBigInt64LE([offset])`
24601cb0ef41Sopenharmony_ci
24611cb0ef41Sopenharmony_ci<!-- YAML
24621cb0ef41Sopenharmony_ciadded:
24631cb0ef41Sopenharmony_ci - v12.0.0
24641cb0ef41Sopenharmony_ci - v10.20.0
24651cb0ef41Sopenharmony_ci-->
24661cb0ef41Sopenharmony_ci
24671cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to read. Must
24681cb0ef41Sopenharmony_ci  satisfy: `0 <= offset <= buf.length - 8`. **Default:** `0`.
24691cb0ef41Sopenharmony_ci* Returns: {bigint}
24701cb0ef41Sopenharmony_ci
24711cb0ef41Sopenharmony_ciReads a signed, little-endian 64-bit integer from `buf` at the specified
24721cb0ef41Sopenharmony_ci`offset`.
24731cb0ef41Sopenharmony_ci
24741cb0ef41Sopenharmony_ciIntegers read from a `Buffer` are interpreted as two's complement signed
24751cb0ef41Sopenharmony_civalues.
24761cb0ef41Sopenharmony_ci
24771cb0ef41Sopenharmony_ci### `buf.readBigUInt64BE([offset])`
24781cb0ef41Sopenharmony_ci
24791cb0ef41Sopenharmony_ci<!-- YAML
24801cb0ef41Sopenharmony_ciadded:
24811cb0ef41Sopenharmony_ci - v12.0.0
24821cb0ef41Sopenharmony_ci - v10.20.0
24831cb0ef41Sopenharmony_cichanges:
24841cb0ef41Sopenharmony_ci  - version:
24851cb0ef41Sopenharmony_ci    - v14.10.0
24861cb0ef41Sopenharmony_ci    - v12.19.0
24871cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34960
24881cb0ef41Sopenharmony_ci    description: This function is also available as `buf.readBigUint64BE()`.
24891cb0ef41Sopenharmony_ci-->
24901cb0ef41Sopenharmony_ci
24911cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to read. Must
24921cb0ef41Sopenharmony_ci  satisfy: `0 <= offset <= buf.length - 8`. **Default:** `0`.
24931cb0ef41Sopenharmony_ci* Returns: {bigint}
24941cb0ef41Sopenharmony_ci
24951cb0ef41Sopenharmony_ciReads an unsigned, big-endian 64-bit integer from `buf` at the specified
24961cb0ef41Sopenharmony_ci`offset`.
24971cb0ef41Sopenharmony_ci
24981cb0ef41Sopenharmony_ciThis function is also available under the `readBigUint64BE` alias.
24991cb0ef41Sopenharmony_ci
25001cb0ef41Sopenharmony_ci```mjs
25011cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
25021cb0ef41Sopenharmony_ci
25031cb0ef41Sopenharmony_ciconst buf = Buffer.from([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff]);
25041cb0ef41Sopenharmony_ci
25051cb0ef41Sopenharmony_ciconsole.log(buf.readBigUInt64BE(0));
25061cb0ef41Sopenharmony_ci// Prints: 4294967295n
25071cb0ef41Sopenharmony_ci```
25081cb0ef41Sopenharmony_ci
25091cb0ef41Sopenharmony_ci```cjs
25101cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
25111cb0ef41Sopenharmony_ci
25121cb0ef41Sopenharmony_ciconst buf = Buffer.from([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff]);
25131cb0ef41Sopenharmony_ci
25141cb0ef41Sopenharmony_ciconsole.log(buf.readBigUInt64BE(0));
25151cb0ef41Sopenharmony_ci// Prints: 4294967295n
25161cb0ef41Sopenharmony_ci```
25171cb0ef41Sopenharmony_ci
25181cb0ef41Sopenharmony_ci### `buf.readBigUInt64LE([offset])`
25191cb0ef41Sopenharmony_ci
25201cb0ef41Sopenharmony_ci<!-- YAML
25211cb0ef41Sopenharmony_ciadded:
25221cb0ef41Sopenharmony_ci - v12.0.0
25231cb0ef41Sopenharmony_ci - v10.20.0
25241cb0ef41Sopenharmony_cichanges:
25251cb0ef41Sopenharmony_ci  - version:
25261cb0ef41Sopenharmony_ci    - v14.10.0
25271cb0ef41Sopenharmony_ci    - v12.19.0
25281cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34960
25291cb0ef41Sopenharmony_ci    description: This function is also available as `buf.readBigUint64LE()`.
25301cb0ef41Sopenharmony_ci-->
25311cb0ef41Sopenharmony_ci
25321cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to read. Must
25331cb0ef41Sopenharmony_ci  satisfy: `0 <= offset <= buf.length - 8`. **Default:** `0`.
25341cb0ef41Sopenharmony_ci* Returns: {bigint}
25351cb0ef41Sopenharmony_ci
25361cb0ef41Sopenharmony_ciReads an unsigned, little-endian 64-bit integer from `buf` at the specified
25371cb0ef41Sopenharmony_ci`offset`.
25381cb0ef41Sopenharmony_ci
25391cb0ef41Sopenharmony_ciThis function is also available under the `readBigUint64LE` alias.
25401cb0ef41Sopenharmony_ci
25411cb0ef41Sopenharmony_ci```mjs
25421cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
25431cb0ef41Sopenharmony_ci
25441cb0ef41Sopenharmony_ciconst buf = Buffer.from([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff]);
25451cb0ef41Sopenharmony_ci
25461cb0ef41Sopenharmony_ciconsole.log(buf.readBigUInt64LE(0));
25471cb0ef41Sopenharmony_ci// Prints: 18446744069414584320n
25481cb0ef41Sopenharmony_ci```
25491cb0ef41Sopenharmony_ci
25501cb0ef41Sopenharmony_ci```cjs
25511cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
25521cb0ef41Sopenharmony_ci
25531cb0ef41Sopenharmony_ciconst buf = Buffer.from([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff]);
25541cb0ef41Sopenharmony_ci
25551cb0ef41Sopenharmony_ciconsole.log(buf.readBigUInt64LE(0));
25561cb0ef41Sopenharmony_ci// Prints: 18446744069414584320n
25571cb0ef41Sopenharmony_ci```
25581cb0ef41Sopenharmony_ci
25591cb0ef41Sopenharmony_ci### `buf.readDoubleBE([offset])`
25601cb0ef41Sopenharmony_ci
25611cb0ef41Sopenharmony_ci<!-- YAML
25621cb0ef41Sopenharmony_ciadded: v0.11.15
25631cb0ef41Sopenharmony_cichanges:
25641cb0ef41Sopenharmony_ci  - version: v10.0.0
25651cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
25661cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
25671cb0ef41Sopenharmony_ci                 to `uint32` anymore.
25681cb0ef41Sopenharmony_ci-->
25691cb0ef41Sopenharmony_ci
25701cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to read. Must
25711cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 8`. **Default:** `0`.
25721cb0ef41Sopenharmony_ci* Returns: {number}
25731cb0ef41Sopenharmony_ci
25741cb0ef41Sopenharmony_ciReads a 64-bit, big-endian double from `buf` at the specified `offset`.
25751cb0ef41Sopenharmony_ci
25761cb0ef41Sopenharmony_ci```mjs
25771cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
25781cb0ef41Sopenharmony_ci
25791cb0ef41Sopenharmony_ciconst buf = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
25801cb0ef41Sopenharmony_ci
25811cb0ef41Sopenharmony_ciconsole.log(buf.readDoubleBE(0));
25821cb0ef41Sopenharmony_ci// Prints: 8.20788039913184e-304
25831cb0ef41Sopenharmony_ci```
25841cb0ef41Sopenharmony_ci
25851cb0ef41Sopenharmony_ci```cjs
25861cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
25871cb0ef41Sopenharmony_ci
25881cb0ef41Sopenharmony_ciconst buf = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
25891cb0ef41Sopenharmony_ci
25901cb0ef41Sopenharmony_ciconsole.log(buf.readDoubleBE(0));
25911cb0ef41Sopenharmony_ci// Prints: 8.20788039913184e-304
25921cb0ef41Sopenharmony_ci```
25931cb0ef41Sopenharmony_ci
25941cb0ef41Sopenharmony_ci### `buf.readDoubleLE([offset])`
25951cb0ef41Sopenharmony_ci
25961cb0ef41Sopenharmony_ci<!-- YAML
25971cb0ef41Sopenharmony_ciadded: v0.11.15
25981cb0ef41Sopenharmony_cichanges:
25991cb0ef41Sopenharmony_ci  - version: v10.0.0
26001cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
26011cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
26021cb0ef41Sopenharmony_ci                 to `uint32` anymore.
26031cb0ef41Sopenharmony_ci-->
26041cb0ef41Sopenharmony_ci
26051cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to read. Must
26061cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 8`. **Default:** `0`.
26071cb0ef41Sopenharmony_ci* Returns: {number}
26081cb0ef41Sopenharmony_ci
26091cb0ef41Sopenharmony_ciReads a 64-bit, little-endian double from `buf` at the specified `offset`.
26101cb0ef41Sopenharmony_ci
26111cb0ef41Sopenharmony_ci```mjs
26121cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
26131cb0ef41Sopenharmony_ci
26141cb0ef41Sopenharmony_ciconst buf = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
26151cb0ef41Sopenharmony_ci
26161cb0ef41Sopenharmony_ciconsole.log(buf.readDoubleLE(0));
26171cb0ef41Sopenharmony_ci// Prints: 5.447603722011605e-270
26181cb0ef41Sopenharmony_ciconsole.log(buf.readDoubleLE(1));
26191cb0ef41Sopenharmony_ci// Throws ERR_OUT_OF_RANGE.
26201cb0ef41Sopenharmony_ci```
26211cb0ef41Sopenharmony_ci
26221cb0ef41Sopenharmony_ci```cjs
26231cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
26241cb0ef41Sopenharmony_ci
26251cb0ef41Sopenharmony_ciconst buf = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
26261cb0ef41Sopenharmony_ci
26271cb0ef41Sopenharmony_ciconsole.log(buf.readDoubleLE(0));
26281cb0ef41Sopenharmony_ci// Prints: 5.447603722011605e-270
26291cb0ef41Sopenharmony_ciconsole.log(buf.readDoubleLE(1));
26301cb0ef41Sopenharmony_ci// Throws ERR_OUT_OF_RANGE.
26311cb0ef41Sopenharmony_ci```
26321cb0ef41Sopenharmony_ci
26331cb0ef41Sopenharmony_ci### `buf.readFloatBE([offset])`
26341cb0ef41Sopenharmony_ci
26351cb0ef41Sopenharmony_ci<!-- YAML
26361cb0ef41Sopenharmony_ciadded: v0.11.15
26371cb0ef41Sopenharmony_cichanges:
26381cb0ef41Sopenharmony_ci  - version: v10.0.0
26391cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
26401cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
26411cb0ef41Sopenharmony_ci                 to `uint32` anymore.
26421cb0ef41Sopenharmony_ci-->
26431cb0ef41Sopenharmony_ci
26441cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to read. Must
26451cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.
26461cb0ef41Sopenharmony_ci* Returns: {number}
26471cb0ef41Sopenharmony_ci
26481cb0ef41Sopenharmony_ciReads a 32-bit, big-endian float from `buf` at the specified `offset`.
26491cb0ef41Sopenharmony_ci
26501cb0ef41Sopenharmony_ci```mjs
26511cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
26521cb0ef41Sopenharmony_ci
26531cb0ef41Sopenharmony_ciconst buf = Buffer.from([1, 2, 3, 4]);
26541cb0ef41Sopenharmony_ci
26551cb0ef41Sopenharmony_ciconsole.log(buf.readFloatBE(0));
26561cb0ef41Sopenharmony_ci// Prints: 2.387939260590663e-38
26571cb0ef41Sopenharmony_ci```
26581cb0ef41Sopenharmony_ci
26591cb0ef41Sopenharmony_ci```cjs
26601cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
26611cb0ef41Sopenharmony_ci
26621cb0ef41Sopenharmony_ciconst buf = Buffer.from([1, 2, 3, 4]);
26631cb0ef41Sopenharmony_ci
26641cb0ef41Sopenharmony_ciconsole.log(buf.readFloatBE(0));
26651cb0ef41Sopenharmony_ci// Prints: 2.387939260590663e-38
26661cb0ef41Sopenharmony_ci```
26671cb0ef41Sopenharmony_ci
26681cb0ef41Sopenharmony_ci### `buf.readFloatLE([offset])`
26691cb0ef41Sopenharmony_ci
26701cb0ef41Sopenharmony_ci<!-- YAML
26711cb0ef41Sopenharmony_ciadded: v0.11.15
26721cb0ef41Sopenharmony_cichanges:
26731cb0ef41Sopenharmony_ci  - version: v10.0.0
26741cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
26751cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
26761cb0ef41Sopenharmony_ci                 to `uint32` anymore.
26771cb0ef41Sopenharmony_ci-->
26781cb0ef41Sopenharmony_ci
26791cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to read. Must
26801cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.
26811cb0ef41Sopenharmony_ci* Returns: {number}
26821cb0ef41Sopenharmony_ci
26831cb0ef41Sopenharmony_ciReads a 32-bit, little-endian float from `buf` at the specified `offset`.
26841cb0ef41Sopenharmony_ci
26851cb0ef41Sopenharmony_ci```mjs
26861cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
26871cb0ef41Sopenharmony_ci
26881cb0ef41Sopenharmony_ciconst buf = Buffer.from([1, 2, 3, 4]);
26891cb0ef41Sopenharmony_ci
26901cb0ef41Sopenharmony_ciconsole.log(buf.readFloatLE(0));
26911cb0ef41Sopenharmony_ci// Prints: 1.539989614439558e-36
26921cb0ef41Sopenharmony_ciconsole.log(buf.readFloatLE(1));
26931cb0ef41Sopenharmony_ci// Throws ERR_OUT_OF_RANGE.
26941cb0ef41Sopenharmony_ci```
26951cb0ef41Sopenharmony_ci
26961cb0ef41Sopenharmony_ci```cjs
26971cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
26981cb0ef41Sopenharmony_ci
26991cb0ef41Sopenharmony_ciconst buf = Buffer.from([1, 2, 3, 4]);
27001cb0ef41Sopenharmony_ci
27011cb0ef41Sopenharmony_ciconsole.log(buf.readFloatLE(0));
27021cb0ef41Sopenharmony_ci// Prints: 1.539989614439558e-36
27031cb0ef41Sopenharmony_ciconsole.log(buf.readFloatLE(1));
27041cb0ef41Sopenharmony_ci// Throws ERR_OUT_OF_RANGE.
27051cb0ef41Sopenharmony_ci```
27061cb0ef41Sopenharmony_ci
27071cb0ef41Sopenharmony_ci### `buf.readInt8([offset])`
27081cb0ef41Sopenharmony_ci
27091cb0ef41Sopenharmony_ci<!-- YAML
27101cb0ef41Sopenharmony_ciadded: v0.5.0
27111cb0ef41Sopenharmony_cichanges:
27121cb0ef41Sopenharmony_ci  - version: v10.0.0
27131cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
27141cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
27151cb0ef41Sopenharmony_ci                 to `uint32` anymore.
27161cb0ef41Sopenharmony_ci-->
27171cb0ef41Sopenharmony_ci
27181cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to read. Must
27191cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 1`. **Default:** `0`.
27201cb0ef41Sopenharmony_ci* Returns: {integer}
27211cb0ef41Sopenharmony_ci
27221cb0ef41Sopenharmony_ciReads a signed 8-bit integer from `buf` at the specified `offset`.
27231cb0ef41Sopenharmony_ci
27241cb0ef41Sopenharmony_ciIntegers read from a `Buffer` are interpreted as two's complement signed values.
27251cb0ef41Sopenharmony_ci
27261cb0ef41Sopenharmony_ci```mjs
27271cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
27281cb0ef41Sopenharmony_ci
27291cb0ef41Sopenharmony_ciconst buf = Buffer.from([-1, 5]);
27301cb0ef41Sopenharmony_ci
27311cb0ef41Sopenharmony_ciconsole.log(buf.readInt8(0));
27321cb0ef41Sopenharmony_ci// Prints: -1
27331cb0ef41Sopenharmony_ciconsole.log(buf.readInt8(1));
27341cb0ef41Sopenharmony_ci// Prints: 5
27351cb0ef41Sopenharmony_ciconsole.log(buf.readInt8(2));
27361cb0ef41Sopenharmony_ci// Throws ERR_OUT_OF_RANGE.
27371cb0ef41Sopenharmony_ci```
27381cb0ef41Sopenharmony_ci
27391cb0ef41Sopenharmony_ci```cjs
27401cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
27411cb0ef41Sopenharmony_ci
27421cb0ef41Sopenharmony_ciconst buf = Buffer.from([-1, 5]);
27431cb0ef41Sopenharmony_ci
27441cb0ef41Sopenharmony_ciconsole.log(buf.readInt8(0));
27451cb0ef41Sopenharmony_ci// Prints: -1
27461cb0ef41Sopenharmony_ciconsole.log(buf.readInt8(1));
27471cb0ef41Sopenharmony_ci// Prints: 5
27481cb0ef41Sopenharmony_ciconsole.log(buf.readInt8(2));
27491cb0ef41Sopenharmony_ci// Throws ERR_OUT_OF_RANGE.
27501cb0ef41Sopenharmony_ci```
27511cb0ef41Sopenharmony_ci
27521cb0ef41Sopenharmony_ci### `buf.readInt16BE([offset])`
27531cb0ef41Sopenharmony_ci
27541cb0ef41Sopenharmony_ci<!-- YAML
27551cb0ef41Sopenharmony_ciadded: v0.5.5
27561cb0ef41Sopenharmony_cichanges:
27571cb0ef41Sopenharmony_ci  - version: v10.0.0
27581cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
27591cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
27601cb0ef41Sopenharmony_ci                 to `uint32` anymore.
27611cb0ef41Sopenharmony_ci-->
27621cb0ef41Sopenharmony_ci
27631cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to read. Must
27641cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 2`. **Default:** `0`.
27651cb0ef41Sopenharmony_ci* Returns: {integer}
27661cb0ef41Sopenharmony_ci
27671cb0ef41Sopenharmony_ciReads a signed, big-endian 16-bit integer from `buf` at the specified `offset`.
27681cb0ef41Sopenharmony_ci
27691cb0ef41Sopenharmony_ciIntegers read from a `Buffer` are interpreted as two's complement signed values.
27701cb0ef41Sopenharmony_ci
27711cb0ef41Sopenharmony_ci```mjs
27721cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
27731cb0ef41Sopenharmony_ci
27741cb0ef41Sopenharmony_ciconst buf = Buffer.from([0, 5]);
27751cb0ef41Sopenharmony_ci
27761cb0ef41Sopenharmony_ciconsole.log(buf.readInt16BE(0));
27771cb0ef41Sopenharmony_ci// Prints: 5
27781cb0ef41Sopenharmony_ci```
27791cb0ef41Sopenharmony_ci
27801cb0ef41Sopenharmony_ci```cjs
27811cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
27821cb0ef41Sopenharmony_ci
27831cb0ef41Sopenharmony_ciconst buf = Buffer.from([0, 5]);
27841cb0ef41Sopenharmony_ci
27851cb0ef41Sopenharmony_ciconsole.log(buf.readInt16BE(0));
27861cb0ef41Sopenharmony_ci// Prints: 5
27871cb0ef41Sopenharmony_ci```
27881cb0ef41Sopenharmony_ci
27891cb0ef41Sopenharmony_ci### `buf.readInt16LE([offset])`
27901cb0ef41Sopenharmony_ci
27911cb0ef41Sopenharmony_ci<!-- YAML
27921cb0ef41Sopenharmony_ciadded: v0.5.5
27931cb0ef41Sopenharmony_cichanges:
27941cb0ef41Sopenharmony_ci  - version: v10.0.0
27951cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
27961cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
27971cb0ef41Sopenharmony_ci                 to `uint32` anymore.
27981cb0ef41Sopenharmony_ci-->
27991cb0ef41Sopenharmony_ci
28001cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to read. Must
28011cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 2`. **Default:** `0`.
28021cb0ef41Sopenharmony_ci* Returns: {integer}
28031cb0ef41Sopenharmony_ci
28041cb0ef41Sopenharmony_ciReads a signed, little-endian 16-bit integer from `buf` at the specified
28051cb0ef41Sopenharmony_ci`offset`.
28061cb0ef41Sopenharmony_ci
28071cb0ef41Sopenharmony_ciIntegers read from a `Buffer` are interpreted as two's complement signed values.
28081cb0ef41Sopenharmony_ci
28091cb0ef41Sopenharmony_ci```mjs
28101cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
28111cb0ef41Sopenharmony_ci
28121cb0ef41Sopenharmony_ciconst buf = Buffer.from([0, 5]);
28131cb0ef41Sopenharmony_ci
28141cb0ef41Sopenharmony_ciconsole.log(buf.readInt16LE(0));
28151cb0ef41Sopenharmony_ci// Prints: 1280
28161cb0ef41Sopenharmony_ciconsole.log(buf.readInt16LE(1));
28171cb0ef41Sopenharmony_ci// Throws ERR_OUT_OF_RANGE.
28181cb0ef41Sopenharmony_ci```
28191cb0ef41Sopenharmony_ci
28201cb0ef41Sopenharmony_ci```cjs
28211cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
28221cb0ef41Sopenharmony_ci
28231cb0ef41Sopenharmony_ciconst buf = Buffer.from([0, 5]);
28241cb0ef41Sopenharmony_ci
28251cb0ef41Sopenharmony_ciconsole.log(buf.readInt16LE(0));
28261cb0ef41Sopenharmony_ci// Prints: 1280
28271cb0ef41Sopenharmony_ciconsole.log(buf.readInt16LE(1));
28281cb0ef41Sopenharmony_ci// Throws ERR_OUT_OF_RANGE.
28291cb0ef41Sopenharmony_ci```
28301cb0ef41Sopenharmony_ci
28311cb0ef41Sopenharmony_ci### `buf.readInt32BE([offset])`
28321cb0ef41Sopenharmony_ci
28331cb0ef41Sopenharmony_ci<!-- YAML
28341cb0ef41Sopenharmony_ciadded: v0.5.5
28351cb0ef41Sopenharmony_cichanges:
28361cb0ef41Sopenharmony_ci  - version: v10.0.0
28371cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
28381cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
28391cb0ef41Sopenharmony_ci                 to `uint32` anymore.
28401cb0ef41Sopenharmony_ci-->
28411cb0ef41Sopenharmony_ci
28421cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to read. Must
28431cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.
28441cb0ef41Sopenharmony_ci* Returns: {integer}
28451cb0ef41Sopenharmony_ci
28461cb0ef41Sopenharmony_ciReads a signed, big-endian 32-bit integer from `buf` at the specified `offset`.
28471cb0ef41Sopenharmony_ci
28481cb0ef41Sopenharmony_ciIntegers read from a `Buffer` are interpreted as two's complement signed values.
28491cb0ef41Sopenharmony_ci
28501cb0ef41Sopenharmony_ci```mjs
28511cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
28521cb0ef41Sopenharmony_ci
28531cb0ef41Sopenharmony_ciconst buf = Buffer.from([0, 0, 0, 5]);
28541cb0ef41Sopenharmony_ci
28551cb0ef41Sopenharmony_ciconsole.log(buf.readInt32BE(0));
28561cb0ef41Sopenharmony_ci// Prints: 5
28571cb0ef41Sopenharmony_ci```
28581cb0ef41Sopenharmony_ci
28591cb0ef41Sopenharmony_ci```cjs
28601cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
28611cb0ef41Sopenharmony_ci
28621cb0ef41Sopenharmony_ciconst buf = Buffer.from([0, 0, 0, 5]);
28631cb0ef41Sopenharmony_ci
28641cb0ef41Sopenharmony_ciconsole.log(buf.readInt32BE(0));
28651cb0ef41Sopenharmony_ci// Prints: 5
28661cb0ef41Sopenharmony_ci```
28671cb0ef41Sopenharmony_ci
28681cb0ef41Sopenharmony_ci### `buf.readInt32LE([offset])`
28691cb0ef41Sopenharmony_ci
28701cb0ef41Sopenharmony_ci<!-- YAML
28711cb0ef41Sopenharmony_ciadded: v0.5.5
28721cb0ef41Sopenharmony_cichanges:
28731cb0ef41Sopenharmony_ci  - version: v10.0.0
28741cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
28751cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
28761cb0ef41Sopenharmony_ci                 to `uint32` anymore.
28771cb0ef41Sopenharmony_ci-->
28781cb0ef41Sopenharmony_ci
28791cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to read. Must
28801cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.
28811cb0ef41Sopenharmony_ci* Returns: {integer}
28821cb0ef41Sopenharmony_ci
28831cb0ef41Sopenharmony_ciReads a signed, little-endian 32-bit integer from `buf` at the specified
28841cb0ef41Sopenharmony_ci`offset`.
28851cb0ef41Sopenharmony_ci
28861cb0ef41Sopenharmony_ciIntegers read from a `Buffer` are interpreted as two's complement signed values.
28871cb0ef41Sopenharmony_ci
28881cb0ef41Sopenharmony_ci```mjs
28891cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
28901cb0ef41Sopenharmony_ci
28911cb0ef41Sopenharmony_ciconst buf = Buffer.from([0, 0, 0, 5]);
28921cb0ef41Sopenharmony_ci
28931cb0ef41Sopenharmony_ciconsole.log(buf.readInt32LE(0));
28941cb0ef41Sopenharmony_ci// Prints: 83886080
28951cb0ef41Sopenharmony_ciconsole.log(buf.readInt32LE(1));
28961cb0ef41Sopenharmony_ci// Throws ERR_OUT_OF_RANGE.
28971cb0ef41Sopenharmony_ci```
28981cb0ef41Sopenharmony_ci
28991cb0ef41Sopenharmony_ci```cjs
29001cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
29011cb0ef41Sopenharmony_ci
29021cb0ef41Sopenharmony_ciconst buf = Buffer.from([0, 0, 0, 5]);
29031cb0ef41Sopenharmony_ci
29041cb0ef41Sopenharmony_ciconsole.log(buf.readInt32LE(0));
29051cb0ef41Sopenharmony_ci// Prints: 83886080
29061cb0ef41Sopenharmony_ciconsole.log(buf.readInt32LE(1));
29071cb0ef41Sopenharmony_ci// Throws ERR_OUT_OF_RANGE.
29081cb0ef41Sopenharmony_ci```
29091cb0ef41Sopenharmony_ci
29101cb0ef41Sopenharmony_ci### `buf.readIntBE(offset, byteLength)`
29111cb0ef41Sopenharmony_ci
29121cb0ef41Sopenharmony_ci<!-- YAML
29131cb0ef41Sopenharmony_ciadded: v0.11.15
29141cb0ef41Sopenharmony_cichanges:
29151cb0ef41Sopenharmony_ci  - version: v10.0.0
29161cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
29171cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
29181cb0ef41Sopenharmony_ci                 and `byteLength` to `uint32` anymore.
29191cb0ef41Sopenharmony_ci-->
29201cb0ef41Sopenharmony_ci
29211cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to read. Must
29221cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - byteLength`.
29231cb0ef41Sopenharmony_ci* `byteLength` {integer} Number of bytes to read. Must satisfy
29241cb0ef41Sopenharmony_ci  `0 < byteLength <= 6`.
29251cb0ef41Sopenharmony_ci* Returns: {integer}
29261cb0ef41Sopenharmony_ci
29271cb0ef41Sopenharmony_ciReads `byteLength` number of bytes from `buf` at the specified `offset`
29281cb0ef41Sopenharmony_ciand interprets the result as a big-endian, two's complement signed value
29291cb0ef41Sopenharmony_cisupporting up to 48 bits of accuracy.
29301cb0ef41Sopenharmony_ci
29311cb0ef41Sopenharmony_ci```mjs
29321cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
29331cb0ef41Sopenharmony_ci
29341cb0ef41Sopenharmony_ciconst buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);
29351cb0ef41Sopenharmony_ci
29361cb0ef41Sopenharmony_ciconsole.log(buf.readIntBE(0, 6).toString(16));
29371cb0ef41Sopenharmony_ci// Prints: 1234567890ab
29381cb0ef41Sopenharmony_ciconsole.log(buf.readIntBE(1, 6).toString(16));
29391cb0ef41Sopenharmony_ci// Throws ERR_OUT_OF_RANGE.
29401cb0ef41Sopenharmony_ciconsole.log(buf.readIntBE(1, 0).toString(16));
29411cb0ef41Sopenharmony_ci// Throws ERR_OUT_OF_RANGE.
29421cb0ef41Sopenharmony_ci```
29431cb0ef41Sopenharmony_ci
29441cb0ef41Sopenharmony_ci```cjs
29451cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
29461cb0ef41Sopenharmony_ci
29471cb0ef41Sopenharmony_ciconst buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);
29481cb0ef41Sopenharmony_ci
29491cb0ef41Sopenharmony_ciconsole.log(buf.readIntBE(0, 6).toString(16));
29501cb0ef41Sopenharmony_ci// Prints: 1234567890ab
29511cb0ef41Sopenharmony_ciconsole.log(buf.readIntBE(1, 6).toString(16));
29521cb0ef41Sopenharmony_ci// Throws ERR_OUT_OF_RANGE.
29531cb0ef41Sopenharmony_ciconsole.log(buf.readIntBE(1, 0).toString(16));
29541cb0ef41Sopenharmony_ci// Throws ERR_OUT_OF_RANGE.
29551cb0ef41Sopenharmony_ci```
29561cb0ef41Sopenharmony_ci
29571cb0ef41Sopenharmony_ci### `buf.readIntLE(offset, byteLength)`
29581cb0ef41Sopenharmony_ci
29591cb0ef41Sopenharmony_ci<!-- YAML
29601cb0ef41Sopenharmony_ciadded: v0.11.15
29611cb0ef41Sopenharmony_cichanges:
29621cb0ef41Sopenharmony_ci  - version: v10.0.0
29631cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
29641cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
29651cb0ef41Sopenharmony_ci                 and `byteLength` to `uint32` anymore.
29661cb0ef41Sopenharmony_ci-->
29671cb0ef41Sopenharmony_ci
29681cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to read. Must
29691cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - byteLength`.
29701cb0ef41Sopenharmony_ci* `byteLength` {integer} Number of bytes to read. Must satisfy
29711cb0ef41Sopenharmony_ci  `0 < byteLength <= 6`.
29721cb0ef41Sopenharmony_ci* Returns: {integer}
29731cb0ef41Sopenharmony_ci
29741cb0ef41Sopenharmony_ciReads `byteLength` number of bytes from `buf` at the specified `offset`
29751cb0ef41Sopenharmony_ciand interprets the result as a little-endian, two's complement signed value
29761cb0ef41Sopenharmony_cisupporting up to 48 bits of accuracy.
29771cb0ef41Sopenharmony_ci
29781cb0ef41Sopenharmony_ci```mjs
29791cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
29801cb0ef41Sopenharmony_ci
29811cb0ef41Sopenharmony_ciconst buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);
29821cb0ef41Sopenharmony_ci
29831cb0ef41Sopenharmony_ciconsole.log(buf.readIntLE(0, 6).toString(16));
29841cb0ef41Sopenharmony_ci// Prints: -546f87a9cbee
29851cb0ef41Sopenharmony_ci```
29861cb0ef41Sopenharmony_ci
29871cb0ef41Sopenharmony_ci```cjs
29881cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
29891cb0ef41Sopenharmony_ci
29901cb0ef41Sopenharmony_ciconst buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);
29911cb0ef41Sopenharmony_ci
29921cb0ef41Sopenharmony_ciconsole.log(buf.readIntLE(0, 6).toString(16));
29931cb0ef41Sopenharmony_ci// Prints: -546f87a9cbee
29941cb0ef41Sopenharmony_ci```
29951cb0ef41Sopenharmony_ci
29961cb0ef41Sopenharmony_ci### `buf.readUInt8([offset])`
29971cb0ef41Sopenharmony_ci
29981cb0ef41Sopenharmony_ci<!-- YAML
29991cb0ef41Sopenharmony_ciadded: v0.5.0
30001cb0ef41Sopenharmony_cichanges:
30011cb0ef41Sopenharmony_ci  - version:
30021cb0ef41Sopenharmony_ci    - v14.9.0
30031cb0ef41Sopenharmony_ci    - v12.19.0
30041cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34729
30051cb0ef41Sopenharmony_ci    description: This function is also available as `buf.readUint8()`.
30061cb0ef41Sopenharmony_ci  - version: v10.0.0
30071cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
30081cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
30091cb0ef41Sopenharmony_ci                 to `uint32` anymore.
30101cb0ef41Sopenharmony_ci-->
30111cb0ef41Sopenharmony_ci
30121cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to read. Must
30131cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 1`. **Default:** `0`.
30141cb0ef41Sopenharmony_ci* Returns: {integer}
30151cb0ef41Sopenharmony_ci
30161cb0ef41Sopenharmony_ciReads an unsigned 8-bit integer from `buf` at the specified `offset`.
30171cb0ef41Sopenharmony_ci
30181cb0ef41Sopenharmony_ciThis function is also available under the `readUint8` alias.
30191cb0ef41Sopenharmony_ci
30201cb0ef41Sopenharmony_ci```mjs
30211cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
30221cb0ef41Sopenharmony_ci
30231cb0ef41Sopenharmony_ciconst buf = Buffer.from([1, -2]);
30241cb0ef41Sopenharmony_ci
30251cb0ef41Sopenharmony_ciconsole.log(buf.readUInt8(0));
30261cb0ef41Sopenharmony_ci// Prints: 1
30271cb0ef41Sopenharmony_ciconsole.log(buf.readUInt8(1));
30281cb0ef41Sopenharmony_ci// Prints: 254
30291cb0ef41Sopenharmony_ciconsole.log(buf.readUInt8(2));
30301cb0ef41Sopenharmony_ci// Throws ERR_OUT_OF_RANGE.
30311cb0ef41Sopenharmony_ci```
30321cb0ef41Sopenharmony_ci
30331cb0ef41Sopenharmony_ci```cjs
30341cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
30351cb0ef41Sopenharmony_ci
30361cb0ef41Sopenharmony_ciconst buf = Buffer.from([1, -2]);
30371cb0ef41Sopenharmony_ci
30381cb0ef41Sopenharmony_ciconsole.log(buf.readUInt8(0));
30391cb0ef41Sopenharmony_ci// Prints: 1
30401cb0ef41Sopenharmony_ciconsole.log(buf.readUInt8(1));
30411cb0ef41Sopenharmony_ci// Prints: 254
30421cb0ef41Sopenharmony_ciconsole.log(buf.readUInt8(2));
30431cb0ef41Sopenharmony_ci// Throws ERR_OUT_OF_RANGE.
30441cb0ef41Sopenharmony_ci```
30451cb0ef41Sopenharmony_ci
30461cb0ef41Sopenharmony_ci### `buf.readUInt16BE([offset])`
30471cb0ef41Sopenharmony_ci
30481cb0ef41Sopenharmony_ci<!-- YAML
30491cb0ef41Sopenharmony_ciadded: v0.5.5
30501cb0ef41Sopenharmony_cichanges:
30511cb0ef41Sopenharmony_ci  - version:
30521cb0ef41Sopenharmony_ci    - v14.9.0
30531cb0ef41Sopenharmony_ci    - v12.19.0
30541cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34729
30551cb0ef41Sopenharmony_ci    description: This function is also available as `buf.readUint16BE()`.
30561cb0ef41Sopenharmony_ci  - version: v10.0.0
30571cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
30581cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
30591cb0ef41Sopenharmony_ci                 to `uint32` anymore.
30601cb0ef41Sopenharmony_ci-->
30611cb0ef41Sopenharmony_ci
30621cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to read. Must
30631cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 2`. **Default:** `0`.
30641cb0ef41Sopenharmony_ci* Returns: {integer}
30651cb0ef41Sopenharmony_ci
30661cb0ef41Sopenharmony_ciReads an unsigned, big-endian 16-bit integer from `buf` at the specified
30671cb0ef41Sopenharmony_ci`offset`.
30681cb0ef41Sopenharmony_ci
30691cb0ef41Sopenharmony_ciThis function is also available under the `readUint16BE` alias.
30701cb0ef41Sopenharmony_ci
30711cb0ef41Sopenharmony_ci```mjs
30721cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
30731cb0ef41Sopenharmony_ci
30741cb0ef41Sopenharmony_ciconst buf = Buffer.from([0x12, 0x34, 0x56]);
30751cb0ef41Sopenharmony_ci
30761cb0ef41Sopenharmony_ciconsole.log(buf.readUInt16BE(0).toString(16));
30771cb0ef41Sopenharmony_ci// Prints: 1234
30781cb0ef41Sopenharmony_ciconsole.log(buf.readUInt16BE(1).toString(16));
30791cb0ef41Sopenharmony_ci// Prints: 3456
30801cb0ef41Sopenharmony_ci```
30811cb0ef41Sopenharmony_ci
30821cb0ef41Sopenharmony_ci```cjs
30831cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
30841cb0ef41Sopenharmony_ci
30851cb0ef41Sopenharmony_ciconst buf = Buffer.from([0x12, 0x34, 0x56]);
30861cb0ef41Sopenharmony_ci
30871cb0ef41Sopenharmony_ciconsole.log(buf.readUInt16BE(0).toString(16));
30881cb0ef41Sopenharmony_ci// Prints: 1234
30891cb0ef41Sopenharmony_ciconsole.log(buf.readUInt16BE(1).toString(16));
30901cb0ef41Sopenharmony_ci// Prints: 3456
30911cb0ef41Sopenharmony_ci```
30921cb0ef41Sopenharmony_ci
30931cb0ef41Sopenharmony_ci### `buf.readUInt16LE([offset])`
30941cb0ef41Sopenharmony_ci
30951cb0ef41Sopenharmony_ci<!-- YAML
30961cb0ef41Sopenharmony_ciadded: v0.5.5
30971cb0ef41Sopenharmony_cichanges:
30981cb0ef41Sopenharmony_ci  - version:
30991cb0ef41Sopenharmony_ci    - v14.9.0
31001cb0ef41Sopenharmony_ci    - v12.19.0
31011cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34729
31021cb0ef41Sopenharmony_ci    description: This function is also available as `buf.readUint16LE()`.
31031cb0ef41Sopenharmony_ci  - version: v10.0.0
31041cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
31051cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
31061cb0ef41Sopenharmony_ci                 to `uint32` anymore.
31071cb0ef41Sopenharmony_ci-->
31081cb0ef41Sopenharmony_ci
31091cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to read. Must
31101cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 2`. **Default:** `0`.
31111cb0ef41Sopenharmony_ci* Returns: {integer}
31121cb0ef41Sopenharmony_ci
31131cb0ef41Sopenharmony_ciReads an unsigned, little-endian 16-bit integer from `buf` at the specified
31141cb0ef41Sopenharmony_ci`offset`.
31151cb0ef41Sopenharmony_ci
31161cb0ef41Sopenharmony_ciThis function is also available under the `readUint16LE` alias.
31171cb0ef41Sopenharmony_ci
31181cb0ef41Sopenharmony_ci```mjs
31191cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
31201cb0ef41Sopenharmony_ci
31211cb0ef41Sopenharmony_ciconst buf = Buffer.from([0x12, 0x34, 0x56]);
31221cb0ef41Sopenharmony_ci
31231cb0ef41Sopenharmony_ciconsole.log(buf.readUInt16LE(0).toString(16));
31241cb0ef41Sopenharmony_ci// Prints: 3412
31251cb0ef41Sopenharmony_ciconsole.log(buf.readUInt16LE(1).toString(16));
31261cb0ef41Sopenharmony_ci// Prints: 5634
31271cb0ef41Sopenharmony_ciconsole.log(buf.readUInt16LE(2).toString(16));
31281cb0ef41Sopenharmony_ci// Throws ERR_OUT_OF_RANGE.
31291cb0ef41Sopenharmony_ci```
31301cb0ef41Sopenharmony_ci
31311cb0ef41Sopenharmony_ci```cjs
31321cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
31331cb0ef41Sopenharmony_ci
31341cb0ef41Sopenharmony_ciconst buf = Buffer.from([0x12, 0x34, 0x56]);
31351cb0ef41Sopenharmony_ci
31361cb0ef41Sopenharmony_ciconsole.log(buf.readUInt16LE(0).toString(16));
31371cb0ef41Sopenharmony_ci// Prints: 3412
31381cb0ef41Sopenharmony_ciconsole.log(buf.readUInt16LE(1).toString(16));
31391cb0ef41Sopenharmony_ci// Prints: 5634
31401cb0ef41Sopenharmony_ciconsole.log(buf.readUInt16LE(2).toString(16));
31411cb0ef41Sopenharmony_ci// Throws ERR_OUT_OF_RANGE.
31421cb0ef41Sopenharmony_ci```
31431cb0ef41Sopenharmony_ci
31441cb0ef41Sopenharmony_ci### `buf.readUInt32BE([offset])`
31451cb0ef41Sopenharmony_ci
31461cb0ef41Sopenharmony_ci<!-- YAML
31471cb0ef41Sopenharmony_ciadded: v0.5.5
31481cb0ef41Sopenharmony_cichanges:
31491cb0ef41Sopenharmony_ci  - version:
31501cb0ef41Sopenharmony_ci    - v14.9.0
31511cb0ef41Sopenharmony_ci    - v12.19.0
31521cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34729
31531cb0ef41Sopenharmony_ci    description: This function is also available as `buf.readUint32BE()`.
31541cb0ef41Sopenharmony_ci  - version: v10.0.0
31551cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
31561cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
31571cb0ef41Sopenharmony_ci                 to `uint32` anymore.
31581cb0ef41Sopenharmony_ci-->
31591cb0ef41Sopenharmony_ci
31601cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to read. Must
31611cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.
31621cb0ef41Sopenharmony_ci* Returns: {integer}
31631cb0ef41Sopenharmony_ci
31641cb0ef41Sopenharmony_ciReads an unsigned, big-endian 32-bit integer from `buf` at the specified
31651cb0ef41Sopenharmony_ci`offset`.
31661cb0ef41Sopenharmony_ci
31671cb0ef41Sopenharmony_ciThis function is also available under the `readUint32BE` alias.
31681cb0ef41Sopenharmony_ci
31691cb0ef41Sopenharmony_ci```mjs
31701cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
31711cb0ef41Sopenharmony_ci
31721cb0ef41Sopenharmony_ciconst buf = Buffer.from([0x12, 0x34, 0x56, 0x78]);
31731cb0ef41Sopenharmony_ci
31741cb0ef41Sopenharmony_ciconsole.log(buf.readUInt32BE(0).toString(16));
31751cb0ef41Sopenharmony_ci// Prints: 12345678
31761cb0ef41Sopenharmony_ci```
31771cb0ef41Sopenharmony_ci
31781cb0ef41Sopenharmony_ci```cjs
31791cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
31801cb0ef41Sopenharmony_ci
31811cb0ef41Sopenharmony_ciconst buf = Buffer.from([0x12, 0x34, 0x56, 0x78]);
31821cb0ef41Sopenharmony_ci
31831cb0ef41Sopenharmony_ciconsole.log(buf.readUInt32BE(0).toString(16));
31841cb0ef41Sopenharmony_ci// Prints: 12345678
31851cb0ef41Sopenharmony_ci```
31861cb0ef41Sopenharmony_ci
31871cb0ef41Sopenharmony_ci### `buf.readUInt32LE([offset])`
31881cb0ef41Sopenharmony_ci
31891cb0ef41Sopenharmony_ci<!-- YAML
31901cb0ef41Sopenharmony_ciadded: v0.5.5
31911cb0ef41Sopenharmony_cichanges:
31921cb0ef41Sopenharmony_ci  - version:
31931cb0ef41Sopenharmony_ci    - v14.9.0
31941cb0ef41Sopenharmony_ci    - v12.19.0
31951cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34729
31961cb0ef41Sopenharmony_ci    description: This function is also available as `buf.readUint32LE()`.
31971cb0ef41Sopenharmony_ci  - version: v10.0.0
31981cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
31991cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
32001cb0ef41Sopenharmony_ci                 to `uint32` anymore.
32011cb0ef41Sopenharmony_ci-->
32021cb0ef41Sopenharmony_ci
32031cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to read. Must
32041cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.
32051cb0ef41Sopenharmony_ci* Returns: {integer}
32061cb0ef41Sopenharmony_ci
32071cb0ef41Sopenharmony_ciReads an unsigned, little-endian 32-bit integer from `buf` at the specified
32081cb0ef41Sopenharmony_ci`offset`.
32091cb0ef41Sopenharmony_ci
32101cb0ef41Sopenharmony_ciThis function is also available under the `readUint32LE` alias.
32111cb0ef41Sopenharmony_ci
32121cb0ef41Sopenharmony_ci```mjs
32131cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
32141cb0ef41Sopenharmony_ci
32151cb0ef41Sopenharmony_ciconst buf = Buffer.from([0x12, 0x34, 0x56, 0x78]);
32161cb0ef41Sopenharmony_ci
32171cb0ef41Sopenharmony_ciconsole.log(buf.readUInt32LE(0).toString(16));
32181cb0ef41Sopenharmony_ci// Prints: 78563412
32191cb0ef41Sopenharmony_ciconsole.log(buf.readUInt32LE(1).toString(16));
32201cb0ef41Sopenharmony_ci// Throws ERR_OUT_OF_RANGE.
32211cb0ef41Sopenharmony_ci```
32221cb0ef41Sopenharmony_ci
32231cb0ef41Sopenharmony_ci```cjs
32241cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
32251cb0ef41Sopenharmony_ci
32261cb0ef41Sopenharmony_ciconst buf = Buffer.from([0x12, 0x34, 0x56, 0x78]);
32271cb0ef41Sopenharmony_ci
32281cb0ef41Sopenharmony_ciconsole.log(buf.readUInt32LE(0).toString(16));
32291cb0ef41Sopenharmony_ci// Prints: 78563412
32301cb0ef41Sopenharmony_ciconsole.log(buf.readUInt32LE(1).toString(16));
32311cb0ef41Sopenharmony_ci// Throws ERR_OUT_OF_RANGE.
32321cb0ef41Sopenharmony_ci```
32331cb0ef41Sopenharmony_ci
32341cb0ef41Sopenharmony_ci### `buf.readUIntBE(offset, byteLength)`
32351cb0ef41Sopenharmony_ci
32361cb0ef41Sopenharmony_ci<!-- YAML
32371cb0ef41Sopenharmony_ciadded: v0.11.15
32381cb0ef41Sopenharmony_cichanges:
32391cb0ef41Sopenharmony_ci  - version:
32401cb0ef41Sopenharmony_ci    - v14.9.0
32411cb0ef41Sopenharmony_ci    - v12.19.0
32421cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34729
32431cb0ef41Sopenharmony_ci    description: This function is also available as `buf.readUintBE()`.
32441cb0ef41Sopenharmony_ci  - version: v10.0.0
32451cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
32461cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
32471cb0ef41Sopenharmony_ci                 and `byteLength` to `uint32` anymore.
32481cb0ef41Sopenharmony_ci-->
32491cb0ef41Sopenharmony_ci
32501cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to read. Must
32511cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - byteLength`.
32521cb0ef41Sopenharmony_ci* `byteLength` {integer} Number of bytes to read. Must satisfy
32531cb0ef41Sopenharmony_ci  `0 < byteLength <= 6`.
32541cb0ef41Sopenharmony_ci* Returns: {integer}
32551cb0ef41Sopenharmony_ci
32561cb0ef41Sopenharmony_ciReads `byteLength` number of bytes from `buf` at the specified `offset`
32571cb0ef41Sopenharmony_ciand interprets the result as an unsigned big-endian integer supporting
32581cb0ef41Sopenharmony_ciup to 48 bits of accuracy.
32591cb0ef41Sopenharmony_ci
32601cb0ef41Sopenharmony_ciThis function is also available under the `readUintBE` alias.
32611cb0ef41Sopenharmony_ci
32621cb0ef41Sopenharmony_ci```mjs
32631cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
32641cb0ef41Sopenharmony_ci
32651cb0ef41Sopenharmony_ciconst buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);
32661cb0ef41Sopenharmony_ci
32671cb0ef41Sopenharmony_ciconsole.log(buf.readUIntBE(0, 6).toString(16));
32681cb0ef41Sopenharmony_ci// Prints: 1234567890ab
32691cb0ef41Sopenharmony_ciconsole.log(buf.readUIntBE(1, 6).toString(16));
32701cb0ef41Sopenharmony_ci// Throws ERR_OUT_OF_RANGE.
32711cb0ef41Sopenharmony_ci```
32721cb0ef41Sopenharmony_ci
32731cb0ef41Sopenharmony_ci```cjs
32741cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
32751cb0ef41Sopenharmony_ci
32761cb0ef41Sopenharmony_ciconst buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);
32771cb0ef41Sopenharmony_ci
32781cb0ef41Sopenharmony_ciconsole.log(buf.readUIntBE(0, 6).toString(16));
32791cb0ef41Sopenharmony_ci// Prints: 1234567890ab
32801cb0ef41Sopenharmony_ciconsole.log(buf.readUIntBE(1, 6).toString(16));
32811cb0ef41Sopenharmony_ci// Throws ERR_OUT_OF_RANGE.
32821cb0ef41Sopenharmony_ci```
32831cb0ef41Sopenharmony_ci
32841cb0ef41Sopenharmony_ci### `buf.readUIntLE(offset, byteLength)`
32851cb0ef41Sopenharmony_ci
32861cb0ef41Sopenharmony_ci<!-- YAML
32871cb0ef41Sopenharmony_ciadded: v0.11.15
32881cb0ef41Sopenharmony_cichanges:
32891cb0ef41Sopenharmony_ci  - version:
32901cb0ef41Sopenharmony_ci    - v14.9.0
32911cb0ef41Sopenharmony_ci    - v12.19.0
32921cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34729
32931cb0ef41Sopenharmony_ci    description: This function is also available as `buf.readUintLE()`.
32941cb0ef41Sopenharmony_ci  - version: v10.0.0
32951cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
32961cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
32971cb0ef41Sopenharmony_ci                 and `byteLength` to `uint32` anymore.
32981cb0ef41Sopenharmony_ci-->
32991cb0ef41Sopenharmony_ci
33001cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to read. Must
33011cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - byteLength`.
33021cb0ef41Sopenharmony_ci* `byteLength` {integer} Number of bytes to read. Must satisfy
33031cb0ef41Sopenharmony_ci  `0 < byteLength <= 6`.
33041cb0ef41Sopenharmony_ci* Returns: {integer}
33051cb0ef41Sopenharmony_ci
33061cb0ef41Sopenharmony_ciReads `byteLength` number of bytes from `buf` at the specified `offset`
33071cb0ef41Sopenharmony_ciand interprets the result as an unsigned, little-endian integer supporting
33081cb0ef41Sopenharmony_ciup to 48 bits of accuracy.
33091cb0ef41Sopenharmony_ci
33101cb0ef41Sopenharmony_ciThis function is also available under the `readUintLE` alias.
33111cb0ef41Sopenharmony_ci
33121cb0ef41Sopenharmony_ci```mjs
33131cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
33141cb0ef41Sopenharmony_ci
33151cb0ef41Sopenharmony_ciconst buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);
33161cb0ef41Sopenharmony_ci
33171cb0ef41Sopenharmony_ciconsole.log(buf.readUIntLE(0, 6).toString(16));
33181cb0ef41Sopenharmony_ci// Prints: ab9078563412
33191cb0ef41Sopenharmony_ci```
33201cb0ef41Sopenharmony_ci
33211cb0ef41Sopenharmony_ci```cjs
33221cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
33231cb0ef41Sopenharmony_ci
33241cb0ef41Sopenharmony_ciconst buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);
33251cb0ef41Sopenharmony_ci
33261cb0ef41Sopenharmony_ciconsole.log(buf.readUIntLE(0, 6).toString(16));
33271cb0ef41Sopenharmony_ci// Prints: ab9078563412
33281cb0ef41Sopenharmony_ci```
33291cb0ef41Sopenharmony_ci
33301cb0ef41Sopenharmony_ci### `buf.subarray([start[, end]])`
33311cb0ef41Sopenharmony_ci
33321cb0ef41Sopenharmony_ci<!-- YAML
33331cb0ef41Sopenharmony_ciadded: v3.0.0
33341cb0ef41Sopenharmony_ci-->
33351cb0ef41Sopenharmony_ci
33361cb0ef41Sopenharmony_ci* `start` {integer} Where the new `Buffer` will start. **Default:** `0`.
33371cb0ef41Sopenharmony_ci* `end` {integer} Where the new `Buffer` will end (not inclusive).
33381cb0ef41Sopenharmony_ci  **Default:** [`buf.length`][].
33391cb0ef41Sopenharmony_ci* Returns: {Buffer}
33401cb0ef41Sopenharmony_ci
33411cb0ef41Sopenharmony_ciReturns a new `Buffer` that references the same memory as the original, but
33421cb0ef41Sopenharmony_cioffset and cropped by the `start` and `end` indices.
33431cb0ef41Sopenharmony_ci
33441cb0ef41Sopenharmony_ciSpecifying `end` greater than [`buf.length`][] will return the same result as
33451cb0ef41Sopenharmony_cithat of `end` equal to [`buf.length`][].
33461cb0ef41Sopenharmony_ci
33471cb0ef41Sopenharmony_ciThis method is inherited from [`TypedArray.prototype.subarray()`][].
33481cb0ef41Sopenharmony_ci
33491cb0ef41Sopenharmony_ciModifying the new `Buffer` slice will modify the memory in the original `Buffer`
33501cb0ef41Sopenharmony_cibecause the allocated memory of the two objects overlap.
33511cb0ef41Sopenharmony_ci
33521cb0ef41Sopenharmony_ci```mjs
33531cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
33541cb0ef41Sopenharmony_ci
33551cb0ef41Sopenharmony_ci// Create a `Buffer` with the ASCII alphabet, take a slice, and modify one byte
33561cb0ef41Sopenharmony_ci// from the original `Buffer`.
33571cb0ef41Sopenharmony_ci
33581cb0ef41Sopenharmony_ciconst buf1 = Buffer.allocUnsafe(26);
33591cb0ef41Sopenharmony_ci
33601cb0ef41Sopenharmony_cifor (let i = 0; i < 26; i++) {
33611cb0ef41Sopenharmony_ci  // 97 is the decimal ASCII value for 'a'.
33621cb0ef41Sopenharmony_ci  buf1[i] = i + 97;
33631cb0ef41Sopenharmony_ci}
33641cb0ef41Sopenharmony_ci
33651cb0ef41Sopenharmony_ciconst buf2 = buf1.subarray(0, 3);
33661cb0ef41Sopenharmony_ci
33671cb0ef41Sopenharmony_ciconsole.log(buf2.toString('ascii', 0, buf2.length));
33681cb0ef41Sopenharmony_ci// Prints: abc
33691cb0ef41Sopenharmony_ci
33701cb0ef41Sopenharmony_cibuf1[0] = 33;
33711cb0ef41Sopenharmony_ci
33721cb0ef41Sopenharmony_ciconsole.log(buf2.toString('ascii', 0, buf2.length));
33731cb0ef41Sopenharmony_ci// Prints: !bc
33741cb0ef41Sopenharmony_ci```
33751cb0ef41Sopenharmony_ci
33761cb0ef41Sopenharmony_ci```cjs
33771cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
33781cb0ef41Sopenharmony_ci
33791cb0ef41Sopenharmony_ci// Create a `Buffer` with the ASCII alphabet, take a slice, and modify one byte
33801cb0ef41Sopenharmony_ci// from the original `Buffer`.
33811cb0ef41Sopenharmony_ci
33821cb0ef41Sopenharmony_ciconst buf1 = Buffer.allocUnsafe(26);
33831cb0ef41Sopenharmony_ci
33841cb0ef41Sopenharmony_cifor (let i = 0; i < 26; i++) {
33851cb0ef41Sopenharmony_ci  // 97 is the decimal ASCII value for 'a'.
33861cb0ef41Sopenharmony_ci  buf1[i] = i + 97;
33871cb0ef41Sopenharmony_ci}
33881cb0ef41Sopenharmony_ci
33891cb0ef41Sopenharmony_ciconst buf2 = buf1.subarray(0, 3);
33901cb0ef41Sopenharmony_ci
33911cb0ef41Sopenharmony_ciconsole.log(buf2.toString('ascii', 0, buf2.length));
33921cb0ef41Sopenharmony_ci// Prints: abc
33931cb0ef41Sopenharmony_ci
33941cb0ef41Sopenharmony_cibuf1[0] = 33;
33951cb0ef41Sopenharmony_ci
33961cb0ef41Sopenharmony_ciconsole.log(buf2.toString('ascii', 0, buf2.length));
33971cb0ef41Sopenharmony_ci// Prints: !bc
33981cb0ef41Sopenharmony_ci```
33991cb0ef41Sopenharmony_ci
34001cb0ef41Sopenharmony_ciSpecifying negative indexes causes the slice to be generated relative to the
34011cb0ef41Sopenharmony_ciend of `buf` rather than the beginning.
34021cb0ef41Sopenharmony_ci
34031cb0ef41Sopenharmony_ci```mjs
34041cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
34051cb0ef41Sopenharmony_ci
34061cb0ef41Sopenharmony_ciconst buf = Buffer.from('buffer');
34071cb0ef41Sopenharmony_ci
34081cb0ef41Sopenharmony_ciconsole.log(buf.subarray(-6, -1).toString());
34091cb0ef41Sopenharmony_ci// Prints: buffe
34101cb0ef41Sopenharmony_ci// (Equivalent to buf.subarray(0, 5).)
34111cb0ef41Sopenharmony_ci
34121cb0ef41Sopenharmony_ciconsole.log(buf.subarray(-6, -2).toString());
34131cb0ef41Sopenharmony_ci// Prints: buff
34141cb0ef41Sopenharmony_ci// (Equivalent to buf.subarray(0, 4).)
34151cb0ef41Sopenharmony_ci
34161cb0ef41Sopenharmony_ciconsole.log(buf.subarray(-5, -2).toString());
34171cb0ef41Sopenharmony_ci// Prints: uff
34181cb0ef41Sopenharmony_ci// (Equivalent to buf.subarray(1, 4).)
34191cb0ef41Sopenharmony_ci```
34201cb0ef41Sopenharmony_ci
34211cb0ef41Sopenharmony_ci```cjs
34221cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
34231cb0ef41Sopenharmony_ci
34241cb0ef41Sopenharmony_ciconst buf = Buffer.from('buffer');
34251cb0ef41Sopenharmony_ci
34261cb0ef41Sopenharmony_ciconsole.log(buf.subarray(-6, -1).toString());
34271cb0ef41Sopenharmony_ci// Prints: buffe
34281cb0ef41Sopenharmony_ci// (Equivalent to buf.subarray(0, 5).)
34291cb0ef41Sopenharmony_ci
34301cb0ef41Sopenharmony_ciconsole.log(buf.subarray(-6, -2).toString());
34311cb0ef41Sopenharmony_ci// Prints: buff
34321cb0ef41Sopenharmony_ci// (Equivalent to buf.subarray(0, 4).)
34331cb0ef41Sopenharmony_ci
34341cb0ef41Sopenharmony_ciconsole.log(buf.subarray(-5, -2).toString());
34351cb0ef41Sopenharmony_ci// Prints: uff
34361cb0ef41Sopenharmony_ci// (Equivalent to buf.subarray(1, 4).)
34371cb0ef41Sopenharmony_ci```
34381cb0ef41Sopenharmony_ci
34391cb0ef41Sopenharmony_ci### `buf.slice([start[, end]])`
34401cb0ef41Sopenharmony_ci
34411cb0ef41Sopenharmony_ci<!-- YAML
34421cb0ef41Sopenharmony_ciadded: v0.3.0
34431cb0ef41Sopenharmony_cichanges:
34441cb0ef41Sopenharmony_ci  - version: v17.5.0
34451cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/41596
34461cb0ef41Sopenharmony_ci    description: The buf.slice() method has been deprecated.
34471cb0ef41Sopenharmony_ci  - version:
34481cb0ef41Sopenharmony_ci    - v7.1.0
34491cb0ef41Sopenharmony_ci    - v6.9.2
34501cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/9341
34511cb0ef41Sopenharmony_ci    description: Coercing the offsets to integers now handles values outside
34521cb0ef41Sopenharmony_ci                 the 32-bit integer range properly.
34531cb0ef41Sopenharmony_ci  - version: v7.0.0
34541cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/9101
34551cb0ef41Sopenharmony_ci    description: All offsets are now coerced to integers before doing any
34561cb0ef41Sopenharmony_ci                 calculations with them.
34571cb0ef41Sopenharmony_ci-->
34581cb0ef41Sopenharmony_ci
34591cb0ef41Sopenharmony_ci* `start` {integer} Where the new `Buffer` will start. **Default:** `0`.
34601cb0ef41Sopenharmony_ci* `end` {integer} Where the new `Buffer` will end (not inclusive).
34611cb0ef41Sopenharmony_ci  **Default:** [`buf.length`][].
34621cb0ef41Sopenharmony_ci* Returns: {Buffer}
34631cb0ef41Sopenharmony_ci
34641cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use [`buf.subarray`][] instead.
34651cb0ef41Sopenharmony_ci
34661cb0ef41Sopenharmony_ciReturns a new `Buffer` that references the same memory as the original, but
34671cb0ef41Sopenharmony_cioffset and cropped by the `start` and `end` indices.
34681cb0ef41Sopenharmony_ci
34691cb0ef41Sopenharmony_ciThis method is not compatible with the `Uint8Array.prototype.slice()`,
34701cb0ef41Sopenharmony_ciwhich is a superclass of `Buffer`. To copy the slice, use
34711cb0ef41Sopenharmony_ci`Uint8Array.prototype.slice()`.
34721cb0ef41Sopenharmony_ci
34731cb0ef41Sopenharmony_ci```mjs
34741cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
34751cb0ef41Sopenharmony_ci
34761cb0ef41Sopenharmony_ciconst buf = Buffer.from('buffer');
34771cb0ef41Sopenharmony_ci
34781cb0ef41Sopenharmony_ciconst copiedBuf = Uint8Array.prototype.slice.call(buf);
34791cb0ef41Sopenharmony_cicopiedBuf[0]++;
34801cb0ef41Sopenharmony_ciconsole.log(copiedBuf.toString());
34811cb0ef41Sopenharmony_ci// Prints: cuffer
34821cb0ef41Sopenharmony_ci
34831cb0ef41Sopenharmony_ciconsole.log(buf.toString());
34841cb0ef41Sopenharmony_ci// Prints: buffer
34851cb0ef41Sopenharmony_ci
34861cb0ef41Sopenharmony_ci// With buf.slice(), the original buffer is modified.
34871cb0ef41Sopenharmony_ciconst notReallyCopiedBuf = buf.slice();
34881cb0ef41Sopenharmony_cinotReallyCopiedBuf[0]++;
34891cb0ef41Sopenharmony_ciconsole.log(notReallyCopiedBuf.toString());
34901cb0ef41Sopenharmony_ci// Prints: cuffer
34911cb0ef41Sopenharmony_ciconsole.log(buf.toString());
34921cb0ef41Sopenharmony_ci// Also prints: cuffer (!)
34931cb0ef41Sopenharmony_ci```
34941cb0ef41Sopenharmony_ci
34951cb0ef41Sopenharmony_ci```cjs
34961cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
34971cb0ef41Sopenharmony_ci
34981cb0ef41Sopenharmony_ciconst buf = Buffer.from('buffer');
34991cb0ef41Sopenharmony_ci
35001cb0ef41Sopenharmony_ciconst copiedBuf = Uint8Array.prototype.slice.call(buf);
35011cb0ef41Sopenharmony_cicopiedBuf[0]++;
35021cb0ef41Sopenharmony_ciconsole.log(copiedBuf.toString());
35031cb0ef41Sopenharmony_ci// Prints: cuffer
35041cb0ef41Sopenharmony_ci
35051cb0ef41Sopenharmony_ciconsole.log(buf.toString());
35061cb0ef41Sopenharmony_ci// Prints: buffer
35071cb0ef41Sopenharmony_ci
35081cb0ef41Sopenharmony_ci// With buf.slice(), the original buffer is modified.
35091cb0ef41Sopenharmony_ciconst notReallyCopiedBuf = buf.slice();
35101cb0ef41Sopenharmony_cinotReallyCopiedBuf[0]++;
35111cb0ef41Sopenharmony_ciconsole.log(notReallyCopiedBuf.toString());
35121cb0ef41Sopenharmony_ci// Prints: cuffer
35131cb0ef41Sopenharmony_ciconsole.log(buf.toString());
35141cb0ef41Sopenharmony_ci// Also prints: cuffer (!)
35151cb0ef41Sopenharmony_ci```
35161cb0ef41Sopenharmony_ci
35171cb0ef41Sopenharmony_ci### `buf.swap16()`
35181cb0ef41Sopenharmony_ci
35191cb0ef41Sopenharmony_ci<!-- YAML
35201cb0ef41Sopenharmony_ciadded: v5.10.0
35211cb0ef41Sopenharmony_ci-->
35221cb0ef41Sopenharmony_ci
35231cb0ef41Sopenharmony_ci* Returns: {Buffer} A reference to `buf`.
35241cb0ef41Sopenharmony_ci
35251cb0ef41Sopenharmony_ciInterprets `buf` as an array of unsigned 16-bit integers and swaps the
35261cb0ef41Sopenharmony_cibyte order _in-place_. Throws [`ERR_INVALID_BUFFER_SIZE`][] if [`buf.length`][]
35271cb0ef41Sopenharmony_ciis not a multiple of 2.
35281cb0ef41Sopenharmony_ci
35291cb0ef41Sopenharmony_ci```mjs
35301cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
35311cb0ef41Sopenharmony_ci
35321cb0ef41Sopenharmony_ciconst buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);
35331cb0ef41Sopenharmony_ci
35341cb0ef41Sopenharmony_ciconsole.log(buf1);
35351cb0ef41Sopenharmony_ci// Prints: <Buffer 01 02 03 04 05 06 07 08>
35361cb0ef41Sopenharmony_ci
35371cb0ef41Sopenharmony_cibuf1.swap16();
35381cb0ef41Sopenharmony_ci
35391cb0ef41Sopenharmony_ciconsole.log(buf1);
35401cb0ef41Sopenharmony_ci// Prints: <Buffer 02 01 04 03 06 05 08 07>
35411cb0ef41Sopenharmony_ci
35421cb0ef41Sopenharmony_ciconst buf2 = Buffer.from([0x1, 0x2, 0x3]);
35431cb0ef41Sopenharmony_ci
35441cb0ef41Sopenharmony_cibuf2.swap16();
35451cb0ef41Sopenharmony_ci// Throws ERR_INVALID_BUFFER_SIZE.
35461cb0ef41Sopenharmony_ci```
35471cb0ef41Sopenharmony_ci
35481cb0ef41Sopenharmony_ci```cjs
35491cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
35501cb0ef41Sopenharmony_ci
35511cb0ef41Sopenharmony_ciconst buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);
35521cb0ef41Sopenharmony_ci
35531cb0ef41Sopenharmony_ciconsole.log(buf1);
35541cb0ef41Sopenharmony_ci// Prints: <Buffer 01 02 03 04 05 06 07 08>
35551cb0ef41Sopenharmony_ci
35561cb0ef41Sopenharmony_cibuf1.swap16();
35571cb0ef41Sopenharmony_ci
35581cb0ef41Sopenharmony_ciconsole.log(buf1);
35591cb0ef41Sopenharmony_ci// Prints: <Buffer 02 01 04 03 06 05 08 07>
35601cb0ef41Sopenharmony_ci
35611cb0ef41Sopenharmony_ciconst buf2 = Buffer.from([0x1, 0x2, 0x3]);
35621cb0ef41Sopenharmony_ci
35631cb0ef41Sopenharmony_cibuf2.swap16();
35641cb0ef41Sopenharmony_ci// Throws ERR_INVALID_BUFFER_SIZE.
35651cb0ef41Sopenharmony_ci```
35661cb0ef41Sopenharmony_ci
35671cb0ef41Sopenharmony_ciOne convenient use of `buf.swap16()` is to perform a fast in-place conversion
35681cb0ef41Sopenharmony_cibetween UTF-16 little-endian and UTF-16 big-endian:
35691cb0ef41Sopenharmony_ci
35701cb0ef41Sopenharmony_ci```mjs
35711cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
35721cb0ef41Sopenharmony_ci
35731cb0ef41Sopenharmony_ciconst buf = Buffer.from('This is little-endian UTF-16', 'utf16le');
35741cb0ef41Sopenharmony_cibuf.swap16(); // Convert to big-endian UTF-16 text.
35751cb0ef41Sopenharmony_ci```
35761cb0ef41Sopenharmony_ci
35771cb0ef41Sopenharmony_ci```cjs
35781cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
35791cb0ef41Sopenharmony_ci
35801cb0ef41Sopenharmony_ciconst buf = Buffer.from('This is little-endian UTF-16', 'utf16le');
35811cb0ef41Sopenharmony_cibuf.swap16(); // Convert to big-endian UTF-16 text.
35821cb0ef41Sopenharmony_ci```
35831cb0ef41Sopenharmony_ci
35841cb0ef41Sopenharmony_ci### `buf.swap32()`
35851cb0ef41Sopenharmony_ci
35861cb0ef41Sopenharmony_ci<!-- YAML
35871cb0ef41Sopenharmony_ciadded: v5.10.0
35881cb0ef41Sopenharmony_ci-->
35891cb0ef41Sopenharmony_ci
35901cb0ef41Sopenharmony_ci* Returns: {Buffer} A reference to `buf`.
35911cb0ef41Sopenharmony_ci
35921cb0ef41Sopenharmony_ciInterprets `buf` as an array of unsigned 32-bit integers and swaps the
35931cb0ef41Sopenharmony_cibyte order _in-place_. Throws [`ERR_INVALID_BUFFER_SIZE`][] if [`buf.length`][]
35941cb0ef41Sopenharmony_ciis not a multiple of 4.
35951cb0ef41Sopenharmony_ci
35961cb0ef41Sopenharmony_ci```mjs
35971cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
35981cb0ef41Sopenharmony_ci
35991cb0ef41Sopenharmony_ciconst buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);
36001cb0ef41Sopenharmony_ci
36011cb0ef41Sopenharmony_ciconsole.log(buf1);
36021cb0ef41Sopenharmony_ci// Prints: <Buffer 01 02 03 04 05 06 07 08>
36031cb0ef41Sopenharmony_ci
36041cb0ef41Sopenharmony_cibuf1.swap32();
36051cb0ef41Sopenharmony_ci
36061cb0ef41Sopenharmony_ciconsole.log(buf1);
36071cb0ef41Sopenharmony_ci// Prints: <Buffer 04 03 02 01 08 07 06 05>
36081cb0ef41Sopenharmony_ci
36091cb0ef41Sopenharmony_ciconst buf2 = Buffer.from([0x1, 0x2, 0x3]);
36101cb0ef41Sopenharmony_ci
36111cb0ef41Sopenharmony_cibuf2.swap32();
36121cb0ef41Sopenharmony_ci// Throws ERR_INVALID_BUFFER_SIZE.
36131cb0ef41Sopenharmony_ci```
36141cb0ef41Sopenharmony_ci
36151cb0ef41Sopenharmony_ci```cjs
36161cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
36171cb0ef41Sopenharmony_ci
36181cb0ef41Sopenharmony_ciconst buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);
36191cb0ef41Sopenharmony_ci
36201cb0ef41Sopenharmony_ciconsole.log(buf1);
36211cb0ef41Sopenharmony_ci// Prints: <Buffer 01 02 03 04 05 06 07 08>
36221cb0ef41Sopenharmony_ci
36231cb0ef41Sopenharmony_cibuf1.swap32();
36241cb0ef41Sopenharmony_ci
36251cb0ef41Sopenharmony_ciconsole.log(buf1);
36261cb0ef41Sopenharmony_ci// Prints: <Buffer 04 03 02 01 08 07 06 05>
36271cb0ef41Sopenharmony_ci
36281cb0ef41Sopenharmony_ciconst buf2 = Buffer.from([0x1, 0x2, 0x3]);
36291cb0ef41Sopenharmony_ci
36301cb0ef41Sopenharmony_cibuf2.swap32();
36311cb0ef41Sopenharmony_ci// Throws ERR_INVALID_BUFFER_SIZE.
36321cb0ef41Sopenharmony_ci```
36331cb0ef41Sopenharmony_ci
36341cb0ef41Sopenharmony_ci### `buf.swap64()`
36351cb0ef41Sopenharmony_ci
36361cb0ef41Sopenharmony_ci<!-- YAML
36371cb0ef41Sopenharmony_ciadded: v6.3.0
36381cb0ef41Sopenharmony_ci-->
36391cb0ef41Sopenharmony_ci
36401cb0ef41Sopenharmony_ci* Returns: {Buffer} A reference to `buf`.
36411cb0ef41Sopenharmony_ci
36421cb0ef41Sopenharmony_ciInterprets `buf` as an array of 64-bit numbers and swaps byte order _in-place_.
36431cb0ef41Sopenharmony_ciThrows [`ERR_INVALID_BUFFER_SIZE`][] if [`buf.length`][] is not a multiple of 8.
36441cb0ef41Sopenharmony_ci
36451cb0ef41Sopenharmony_ci```mjs
36461cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
36471cb0ef41Sopenharmony_ci
36481cb0ef41Sopenharmony_ciconst buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);
36491cb0ef41Sopenharmony_ci
36501cb0ef41Sopenharmony_ciconsole.log(buf1);
36511cb0ef41Sopenharmony_ci// Prints: <Buffer 01 02 03 04 05 06 07 08>
36521cb0ef41Sopenharmony_ci
36531cb0ef41Sopenharmony_cibuf1.swap64();
36541cb0ef41Sopenharmony_ci
36551cb0ef41Sopenharmony_ciconsole.log(buf1);
36561cb0ef41Sopenharmony_ci// Prints: <Buffer 08 07 06 05 04 03 02 01>
36571cb0ef41Sopenharmony_ci
36581cb0ef41Sopenharmony_ciconst buf2 = Buffer.from([0x1, 0x2, 0x3]);
36591cb0ef41Sopenharmony_ci
36601cb0ef41Sopenharmony_cibuf2.swap64();
36611cb0ef41Sopenharmony_ci// Throws ERR_INVALID_BUFFER_SIZE.
36621cb0ef41Sopenharmony_ci```
36631cb0ef41Sopenharmony_ci
36641cb0ef41Sopenharmony_ci```cjs
36651cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
36661cb0ef41Sopenharmony_ci
36671cb0ef41Sopenharmony_ciconst buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);
36681cb0ef41Sopenharmony_ci
36691cb0ef41Sopenharmony_ciconsole.log(buf1);
36701cb0ef41Sopenharmony_ci// Prints: <Buffer 01 02 03 04 05 06 07 08>
36711cb0ef41Sopenharmony_ci
36721cb0ef41Sopenharmony_cibuf1.swap64();
36731cb0ef41Sopenharmony_ci
36741cb0ef41Sopenharmony_ciconsole.log(buf1);
36751cb0ef41Sopenharmony_ci// Prints: <Buffer 08 07 06 05 04 03 02 01>
36761cb0ef41Sopenharmony_ci
36771cb0ef41Sopenharmony_ciconst buf2 = Buffer.from([0x1, 0x2, 0x3]);
36781cb0ef41Sopenharmony_ci
36791cb0ef41Sopenharmony_cibuf2.swap64();
36801cb0ef41Sopenharmony_ci// Throws ERR_INVALID_BUFFER_SIZE.
36811cb0ef41Sopenharmony_ci```
36821cb0ef41Sopenharmony_ci
36831cb0ef41Sopenharmony_ci### `buf.toJSON()`
36841cb0ef41Sopenharmony_ci
36851cb0ef41Sopenharmony_ci<!-- YAML
36861cb0ef41Sopenharmony_ciadded: v0.9.2
36871cb0ef41Sopenharmony_ci-->
36881cb0ef41Sopenharmony_ci
36891cb0ef41Sopenharmony_ci* Returns: {Object}
36901cb0ef41Sopenharmony_ci
36911cb0ef41Sopenharmony_ciReturns a JSON representation of `buf`. [`JSON.stringify()`][] implicitly calls
36921cb0ef41Sopenharmony_cithis function when stringifying a `Buffer` instance.
36931cb0ef41Sopenharmony_ci
36941cb0ef41Sopenharmony_ci`Buffer.from()` accepts objects in the format returned from this method.
36951cb0ef41Sopenharmony_ciIn particular, `Buffer.from(buf.toJSON())` works like `Buffer.from(buf)`.
36961cb0ef41Sopenharmony_ci
36971cb0ef41Sopenharmony_ci```mjs
36981cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
36991cb0ef41Sopenharmony_ci
37001cb0ef41Sopenharmony_ciconst buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5]);
37011cb0ef41Sopenharmony_ciconst json = JSON.stringify(buf);
37021cb0ef41Sopenharmony_ci
37031cb0ef41Sopenharmony_ciconsole.log(json);
37041cb0ef41Sopenharmony_ci// Prints: {"type":"Buffer","data":[1,2,3,4,5]}
37051cb0ef41Sopenharmony_ci
37061cb0ef41Sopenharmony_ciconst copy = JSON.parse(json, (key, value) => {
37071cb0ef41Sopenharmony_ci  return value && value.type === 'Buffer' ?
37081cb0ef41Sopenharmony_ci    Buffer.from(value) :
37091cb0ef41Sopenharmony_ci    value;
37101cb0ef41Sopenharmony_ci});
37111cb0ef41Sopenharmony_ci
37121cb0ef41Sopenharmony_ciconsole.log(copy);
37131cb0ef41Sopenharmony_ci// Prints: <Buffer 01 02 03 04 05>
37141cb0ef41Sopenharmony_ci```
37151cb0ef41Sopenharmony_ci
37161cb0ef41Sopenharmony_ci```cjs
37171cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
37181cb0ef41Sopenharmony_ci
37191cb0ef41Sopenharmony_ciconst buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5]);
37201cb0ef41Sopenharmony_ciconst json = JSON.stringify(buf);
37211cb0ef41Sopenharmony_ci
37221cb0ef41Sopenharmony_ciconsole.log(json);
37231cb0ef41Sopenharmony_ci// Prints: {"type":"Buffer","data":[1,2,3,4,5]}
37241cb0ef41Sopenharmony_ci
37251cb0ef41Sopenharmony_ciconst copy = JSON.parse(json, (key, value) => {
37261cb0ef41Sopenharmony_ci  return value && value.type === 'Buffer' ?
37271cb0ef41Sopenharmony_ci    Buffer.from(value) :
37281cb0ef41Sopenharmony_ci    value;
37291cb0ef41Sopenharmony_ci});
37301cb0ef41Sopenharmony_ci
37311cb0ef41Sopenharmony_ciconsole.log(copy);
37321cb0ef41Sopenharmony_ci// Prints: <Buffer 01 02 03 04 05>
37331cb0ef41Sopenharmony_ci```
37341cb0ef41Sopenharmony_ci
37351cb0ef41Sopenharmony_ci### `buf.toString([encoding[, start[, end]]])`
37361cb0ef41Sopenharmony_ci
37371cb0ef41Sopenharmony_ci<!-- YAML
37381cb0ef41Sopenharmony_ciadded: v0.1.90
37391cb0ef41Sopenharmony_ci-->
37401cb0ef41Sopenharmony_ci
37411cb0ef41Sopenharmony_ci* `encoding` {string} The character encoding to use. **Default:** `'utf8'`.
37421cb0ef41Sopenharmony_ci* `start` {integer} The byte offset to start decoding at. **Default:** `0`.
37431cb0ef41Sopenharmony_ci* `end` {integer} The byte offset to stop decoding at (not inclusive).
37441cb0ef41Sopenharmony_ci  **Default:** [`buf.length`][].
37451cb0ef41Sopenharmony_ci* Returns: {string}
37461cb0ef41Sopenharmony_ci
37471cb0ef41Sopenharmony_ciDecodes `buf` to a string according to the specified character encoding in
37481cb0ef41Sopenharmony_ci`encoding`. `start` and `end` may be passed to decode only a subset of `buf`.
37491cb0ef41Sopenharmony_ci
37501cb0ef41Sopenharmony_ciIf `encoding` is `'utf8'` and a byte sequence in the input is not valid UTF-8,
37511cb0ef41Sopenharmony_cithen each invalid byte is replaced with the replacement character `U+FFFD`.
37521cb0ef41Sopenharmony_ci
37531cb0ef41Sopenharmony_ciThe maximum length of a string instance (in UTF-16 code units) is available
37541cb0ef41Sopenharmony_cias [`buffer.constants.MAX_STRING_LENGTH`][].
37551cb0ef41Sopenharmony_ci
37561cb0ef41Sopenharmony_ci```mjs
37571cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
37581cb0ef41Sopenharmony_ci
37591cb0ef41Sopenharmony_ciconst buf1 = Buffer.allocUnsafe(26);
37601cb0ef41Sopenharmony_ci
37611cb0ef41Sopenharmony_cifor (let i = 0; i < 26; i++) {
37621cb0ef41Sopenharmony_ci  // 97 is the decimal ASCII value for 'a'.
37631cb0ef41Sopenharmony_ci  buf1[i] = i + 97;
37641cb0ef41Sopenharmony_ci}
37651cb0ef41Sopenharmony_ci
37661cb0ef41Sopenharmony_ciconsole.log(buf1.toString('utf8'));
37671cb0ef41Sopenharmony_ci// Prints: abcdefghijklmnopqrstuvwxyz
37681cb0ef41Sopenharmony_ciconsole.log(buf1.toString('utf8', 0, 5));
37691cb0ef41Sopenharmony_ci// Prints: abcde
37701cb0ef41Sopenharmony_ci
37711cb0ef41Sopenharmony_ciconst buf2 = Buffer.from('tést');
37721cb0ef41Sopenharmony_ci
37731cb0ef41Sopenharmony_ciconsole.log(buf2.toString('hex'));
37741cb0ef41Sopenharmony_ci// Prints: 74c3a97374
37751cb0ef41Sopenharmony_ciconsole.log(buf2.toString('utf8', 0, 3));
37761cb0ef41Sopenharmony_ci// Prints: té
37771cb0ef41Sopenharmony_ciconsole.log(buf2.toString(undefined, 0, 3));
37781cb0ef41Sopenharmony_ci// Prints: té
37791cb0ef41Sopenharmony_ci```
37801cb0ef41Sopenharmony_ci
37811cb0ef41Sopenharmony_ci```cjs
37821cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
37831cb0ef41Sopenharmony_ci
37841cb0ef41Sopenharmony_ciconst buf1 = Buffer.allocUnsafe(26);
37851cb0ef41Sopenharmony_ci
37861cb0ef41Sopenharmony_cifor (let i = 0; i < 26; i++) {
37871cb0ef41Sopenharmony_ci  // 97 is the decimal ASCII value for 'a'.
37881cb0ef41Sopenharmony_ci  buf1[i] = i + 97;
37891cb0ef41Sopenharmony_ci}
37901cb0ef41Sopenharmony_ci
37911cb0ef41Sopenharmony_ciconsole.log(buf1.toString('utf8'));
37921cb0ef41Sopenharmony_ci// Prints: abcdefghijklmnopqrstuvwxyz
37931cb0ef41Sopenharmony_ciconsole.log(buf1.toString('utf8', 0, 5));
37941cb0ef41Sopenharmony_ci// Prints: abcde
37951cb0ef41Sopenharmony_ci
37961cb0ef41Sopenharmony_ciconst buf2 = Buffer.from('tést');
37971cb0ef41Sopenharmony_ci
37981cb0ef41Sopenharmony_ciconsole.log(buf2.toString('hex'));
37991cb0ef41Sopenharmony_ci// Prints: 74c3a97374
38001cb0ef41Sopenharmony_ciconsole.log(buf2.toString('utf8', 0, 3));
38011cb0ef41Sopenharmony_ci// Prints: té
38021cb0ef41Sopenharmony_ciconsole.log(buf2.toString(undefined, 0, 3));
38031cb0ef41Sopenharmony_ci// Prints: té
38041cb0ef41Sopenharmony_ci```
38051cb0ef41Sopenharmony_ci
38061cb0ef41Sopenharmony_ci### `buf.values()`
38071cb0ef41Sopenharmony_ci
38081cb0ef41Sopenharmony_ci<!-- YAML
38091cb0ef41Sopenharmony_ciadded: v1.1.0
38101cb0ef41Sopenharmony_ci-->
38111cb0ef41Sopenharmony_ci
38121cb0ef41Sopenharmony_ci* Returns: {Iterator}
38131cb0ef41Sopenharmony_ci
38141cb0ef41Sopenharmony_ciCreates and returns an [iterator][] for `buf` values (bytes). This function is
38151cb0ef41Sopenharmony_cicalled automatically when a `Buffer` is used in a `for..of` statement.
38161cb0ef41Sopenharmony_ci
38171cb0ef41Sopenharmony_ci```mjs
38181cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
38191cb0ef41Sopenharmony_ci
38201cb0ef41Sopenharmony_ciconst buf = Buffer.from('buffer');
38211cb0ef41Sopenharmony_ci
38221cb0ef41Sopenharmony_cifor (const value of buf.values()) {
38231cb0ef41Sopenharmony_ci  console.log(value);
38241cb0ef41Sopenharmony_ci}
38251cb0ef41Sopenharmony_ci// Prints:
38261cb0ef41Sopenharmony_ci//   98
38271cb0ef41Sopenharmony_ci//   117
38281cb0ef41Sopenharmony_ci//   102
38291cb0ef41Sopenharmony_ci//   102
38301cb0ef41Sopenharmony_ci//   101
38311cb0ef41Sopenharmony_ci//   114
38321cb0ef41Sopenharmony_ci
38331cb0ef41Sopenharmony_cifor (const value of buf) {
38341cb0ef41Sopenharmony_ci  console.log(value);
38351cb0ef41Sopenharmony_ci}
38361cb0ef41Sopenharmony_ci// Prints:
38371cb0ef41Sopenharmony_ci//   98
38381cb0ef41Sopenharmony_ci//   117
38391cb0ef41Sopenharmony_ci//   102
38401cb0ef41Sopenharmony_ci//   102
38411cb0ef41Sopenharmony_ci//   101
38421cb0ef41Sopenharmony_ci//   114
38431cb0ef41Sopenharmony_ci```
38441cb0ef41Sopenharmony_ci
38451cb0ef41Sopenharmony_ci```cjs
38461cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
38471cb0ef41Sopenharmony_ci
38481cb0ef41Sopenharmony_ciconst buf = Buffer.from('buffer');
38491cb0ef41Sopenharmony_ci
38501cb0ef41Sopenharmony_cifor (const value of buf.values()) {
38511cb0ef41Sopenharmony_ci  console.log(value);
38521cb0ef41Sopenharmony_ci}
38531cb0ef41Sopenharmony_ci// Prints:
38541cb0ef41Sopenharmony_ci//   98
38551cb0ef41Sopenharmony_ci//   117
38561cb0ef41Sopenharmony_ci//   102
38571cb0ef41Sopenharmony_ci//   102
38581cb0ef41Sopenharmony_ci//   101
38591cb0ef41Sopenharmony_ci//   114
38601cb0ef41Sopenharmony_ci
38611cb0ef41Sopenharmony_cifor (const value of buf) {
38621cb0ef41Sopenharmony_ci  console.log(value);
38631cb0ef41Sopenharmony_ci}
38641cb0ef41Sopenharmony_ci// Prints:
38651cb0ef41Sopenharmony_ci//   98
38661cb0ef41Sopenharmony_ci//   117
38671cb0ef41Sopenharmony_ci//   102
38681cb0ef41Sopenharmony_ci//   102
38691cb0ef41Sopenharmony_ci//   101
38701cb0ef41Sopenharmony_ci//   114
38711cb0ef41Sopenharmony_ci```
38721cb0ef41Sopenharmony_ci
38731cb0ef41Sopenharmony_ci### `buf.write(string[, offset[, length]][, encoding])`
38741cb0ef41Sopenharmony_ci
38751cb0ef41Sopenharmony_ci<!-- YAML
38761cb0ef41Sopenharmony_ciadded: v0.1.90
38771cb0ef41Sopenharmony_ci-->
38781cb0ef41Sopenharmony_ci
38791cb0ef41Sopenharmony_ci* `string` {string} String to write to `buf`.
38801cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to write `string`.
38811cb0ef41Sopenharmony_ci  **Default:** `0`.
38821cb0ef41Sopenharmony_ci* `length` {integer} Maximum number of bytes to write (written bytes will not
38831cb0ef41Sopenharmony_ci  exceed `buf.length - offset`). **Default:** `buf.length - offset`.
38841cb0ef41Sopenharmony_ci* `encoding` {string} The character encoding of `string`. **Default:** `'utf8'`.
38851cb0ef41Sopenharmony_ci* Returns: {integer} Number of bytes written.
38861cb0ef41Sopenharmony_ci
38871cb0ef41Sopenharmony_ciWrites `string` to `buf` at `offset` according to the character encoding in
38881cb0ef41Sopenharmony_ci`encoding`. The `length` parameter is the number of bytes to write. If `buf` did
38891cb0ef41Sopenharmony_cinot contain enough space to fit the entire string, only part of `string` will be
38901cb0ef41Sopenharmony_ciwritten. However, partially encoded characters will not be written.
38911cb0ef41Sopenharmony_ci
38921cb0ef41Sopenharmony_ci```mjs
38931cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
38941cb0ef41Sopenharmony_ci
38951cb0ef41Sopenharmony_ciconst buf = Buffer.alloc(256);
38961cb0ef41Sopenharmony_ci
38971cb0ef41Sopenharmony_ciconst len = buf.write('\u00bd + \u00bc = \u00be', 0);
38981cb0ef41Sopenharmony_ci
38991cb0ef41Sopenharmony_ciconsole.log(`${len} bytes: ${buf.toString('utf8', 0, len)}`);
39001cb0ef41Sopenharmony_ci// Prints: 12 bytes: ½ + ¼ = ¾
39011cb0ef41Sopenharmony_ci
39021cb0ef41Sopenharmony_ciconst buffer = Buffer.alloc(10);
39031cb0ef41Sopenharmony_ci
39041cb0ef41Sopenharmony_ciconst length = buffer.write('abcd', 8);
39051cb0ef41Sopenharmony_ci
39061cb0ef41Sopenharmony_ciconsole.log(`${length} bytes: ${buffer.toString('utf8', 8, 10)}`);
39071cb0ef41Sopenharmony_ci// Prints: 2 bytes : ab
39081cb0ef41Sopenharmony_ci```
39091cb0ef41Sopenharmony_ci
39101cb0ef41Sopenharmony_ci```cjs
39111cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
39121cb0ef41Sopenharmony_ci
39131cb0ef41Sopenharmony_ciconst buf = Buffer.alloc(256);
39141cb0ef41Sopenharmony_ci
39151cb0ef41Sopenharmony_ciconst len = buf.write('\u00bd + \u00bc = \u00be', 0);
39161cb0ef41Sopenharmony_ci
39171cb0ef41Sopenharmony_ciconsole.log(`${len} bytes: ${buf.toString('utf8', 0, len)}`);
39181cb0ef41Sopenharmony_ci// Prints: 12 bytes: ½ + ¼ = ¾
39191cb0ef41Sopenharmony_ci
39201cb0ef41Sopenharmony_ciconst buffer = Buffer.alloc(10);
39211cb0ef41Sopenharmony_ci
39221cb0ef41Sopenharmony_ciconst length = buffer.write('abcd', 8);
39231cb0ef41Sopenharmony_ci
39241cb0ef41Sopenharmony_ciconsole.log(`${length} bytes: ${buffer.toString('utf8', 8, 10)}`);
39251cb0ef41Sopenharmony_ci// Prints: 2 bytes : ab
39261cb0ef41Sopenharmony_ci```
39271cb0ef41Sopenharmony_ci
39281cb0ef41Sopenharmony_ci### `buf.writeBigInt64BE(value[, offset])`
39291cb0ef41Sopenharmony_ci
39301cb0ef41Sopenharmony_ci<!-- YAML
39311cb0ef41Sopenharmony_ciadded:
39321cb0ef41Sopenharmony_ci - v12.0.0
39331cb0ef41Sopenharmony_ci - v10.20.0
39341cb0ef41Sopenharmony_ci-->
39351cb0ef41Sopenharmony_ci
39361cb0ef41Sopenharmony_ci* `value` {bigint} Number to be written to `buf`.
39371cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to write. Must
39381cb0ef41Sopenharmony_ci  satisfy: `0 <= offset <= buf.length - 8`. **Default:** `0`.
39391cb0ef41Sopenharmony_ci* Returns: {integer} `offset` plus the number of bytes written.
39401cb0ef41Sopenharmony_ci
39411cb0ef41Sopenharmony_ciWrites `value` to `buf` at the specified `offset` as big-endian.
39421cb0ef41Sopenharmony_ci
39431cb0ef41Sopenharmony_ci`value` is interpreted and written as a two's complement signed integer.
39441cb0ef41Sopenharmony_ci
39451cb0ef41Sopenharmony_ci```mjs
39461cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
39471cb0ef41Sopenharmony_ci
39481cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(8);
39491cb0ef41Sopenharmony_ci
39501cb0ef41Sopenharmony_cibuf.writeBigInt64BE(0x0102030405060708n, 0);
39511cb0ef41Sopenharmony_ci
39521cb0ef41Sopenharmony_ciconsole.log(buf);
39531cb0ef41Sopenharmony_ci// Prints: <Buffer 01 02 03 04 05 06 07 08>
39541cb0ef41Sopenharmony_ci```
39551cb0ef41Sopenharmony_ci
39561cb0ef41Sopenharmony_ci```cjs
39571cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
39581cb0ef41Sopenharmony_ci
39591cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(8);
39601cb0ef41Sopenharmony_ci
39611cb0ef41Sopenharmony_cibuf.writeBigInt64BE(0x0102030405060708n, 0);
39621cb0ef41Sopenharmony_ci
39631cb0ef41Sopenharmony_ciconsole.log(buf);
39641cb0ef41Sopenharmony_ci// Prints: <Buffer 01 02 03 04 05 06 07 08>
39651cb0ef41Sopenharmony_ci```
39661cb0ef41Sopenharmony_ci
39671cb0ef41Sopenharmony_ci### `buf.writeBigInt64LE(value[, offset])`
39681cb0ef41Sopenharmony_ci
39691cb0ef41Sopenharmony_ci<!-- YAML
39701cb0ef41Sopenharmony_ciadded:
39711cb0ef41Sopenharmony_ci - v12.0.0
39721cb0ef41Sopenharmony_ci - v10.20.0
39731cb0ef41Sopenharmony_ci-->
39741cb0ef41Sopenharmony_ci
39751cb0ef41Sopenharmony_ci* `value` {bigint} Number to be written to `buf`.
39761cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to write. Must
39771cb0ef41Sopenharmony_ci  satisfy: `0 <= offset <= buf.length - 8`. **Default:** `0`.
39781cb0ef41Sopenharmony_ci* Returns: {integer} `offset` plus the number of bytes written.
39791cb0ef41Sopenharmony_ci
39801cb0ef41Sopenharmony_ciWrites `value` to `buf` at the specified `offset` as little-endian.
39811cb0ef41Sopenharmony_ci
39821cb0ef41Sopenharmony_ci`value` is interpreted and written as a two's complement signed integer.
39831cb0ef41Sopenharmony_ci
39841cb0ef41Sopenharmony_ci```mjs
39851cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
39861cb0ef41Sopenharmony_ci
39871cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(8);
39881cb0ef41Sopenharmony_ci
39891cb0ef41Sopenharmony_cibuf.writeBigInt64LE(0x0102030405060708n, 0);
39901cb0ef41Sopenharmony_ci
39911cb0ef41Sopenharmony_ciconsole.log(buf);
39921cb0ef41Sopenharmony_ci// Prints: <Buffer 08 07 06 05 04 03 02 01>
39931cb0ef41Sopenharmony_ci```
39941cb0ef41Sopenharmony_ci
39951cb0ef41Sopenharmony_ci```cjs
39961cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
39971cb0ef41Sopenharmony_ci
39981cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(8);
39991cb0ef41Sopenharmony_ci
40001cb0ef41Sopenharmony_cibuf.writeBigInt64LE(0x0102030405060708n, 0);
40011cb0ef41Sopenharmony_ci
40021cb0ef41Sopenharmony_ciconsole.log(buf);
40031cb0ef41Sopenharmony_ci// Prints: <Buffer 08 07 06 05 04 03 02 01>
40041cb0ef41Sopenharmony_ci```
40051cb0ef41Sopenharmony_ci
40061cb0ef41Sopenharmony_ci### `buf.writeBigUInt64BE(value[, offset])`
40071cb0ef41Sopenharmony_ci
40081cb0ef41Sopenharmony_ci<!-- YAML
40091cb0ef41Sopenharmony_ciadded:
40101cb0ef41Sopenharmony_ci - v12.0.0
40111cb0ef41Sopenharmony_ci - v10.20.0
40121cb0ef41Sopenharmony_cichanges:
40131cb0ef41Sopenharmony_ci  - version:
40141cb0ef41Sopenharmony_ci    - v14.10.0
40151cb0ef41Sopenharmony_ci    - v12.19.0
40161cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34960
40171cb0ef41Sopenharmony_ci    description: This function is also available as `buf.writeBigUint64BE()`.
40181cb0ef41Sopenharmony_ci-->
40191cb0ef41Sopenharmony_ci
40201cb0ef41Sopenharmony_ci* `value` {bigint} Number to be written to `buf`.
40211cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to write. Must
40221cb0ef41Sopenharmony_ci  satisfy: `0 <= offset <= buf.length - 8`. **Default:** `0`.
40231cb0ef41Sopenharmony_ci* Returns: {integer} `offset` plus the number of bytes written.
40241cb0ef41Sopenharmony_ci
40251cb0ef41Sopenharmony_ciWrites `value` to `buf` at the specified `offset` as big-endian.
40261cb0ef41Sopenharmony_ci
40271cb0ef41Sopenharmony_ciThis function is also available under the `writeBigUint64BE` alias.
40281cb0ef41Sopenharmony_ci
40291cb0ef41Sopenharmony_ci```mjs
40301cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
40311cb0ef41Sopenharmony_ci
40321cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(8);
40331cb0ef41Sopenharmony_ci
40341cb0ef41Sopenharmony_cibuf.writeBigUInt64BE(0xdecafafecacefaden, 0);
40351cb0ef41Sopenharmony_ci
40361cb0ef41Sopenharmony_ciconsole.log(buf);
40371cb0ef41Sopenharmony_ci// Prints: <Buffer de ca fa fe ca ce fa de>
40381cb0ef41Sopenharmony_ci```
40391cb0ef41Sopenharmony_ci
40401cb0ef41Sopenharmony_ci```cjs
40411cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
40421cb0ef41Sopenharmony_ci
40431cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(8);
40441cb0ef41Sopenharmony_ci
40451cb0ef41Sopenharmony_cibuf.writeBigUInt64BE(0xdecafafecacefaden, 0);
40461cb0ef41Sopenharmony_ci
40471cb0ef41Sopenharmony_ciconsole.log(buf);
40481cb0ef41Sopenharmony_ci// Prints: <Buffer de ca fa fe ca ce fa de>
40491cb0ef41Sopenharmony_ci```
40501cb0ef41Sopenharmony_ci
40511cb0ef41Sopenharmony_ci### `buf.writeBigUInt64LE(value[, offset])`
40521cb0ef41Sopenharmony_ci
40531cb0ef41Sopenharmony_ci<!-- YAML
40541cb0ef41Sopenharmony_ciadded:
40551cb0ef41Sopenharmony_ci - v12.0.0
40561cb0ef41Sopenharmony_ci - v10.20.0
40571cb0ef41Sopenharmony_cichanges:
40581cb0ef41Sopenharmony_ci  - version:
40591cb0ef41Sopenharmony_ci    - v14.10.0
40601cb0ef41Sopenharmony_ci    - v12.19.0
40611cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34960
40621cb0ef41Sopenharmony_ci    description: This function is also available as `buf.writeBigUint64LE()`.
40631cb0ef41Sopenharmony_ci-->
40641cb0ef41Sopenharmony_ci
40651cb0ef41Sopenharmony_ci* `value` {bigint} Number to be written to `buf`.
40661cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to write. Must
40671cb0ef41Sopenharmony_ci  satisfy: `0 <= offset <= buf.length - 8`. **Default:** `0`.
40681cb0ef41Sopenharmony_ci* Returns: {integer} `offset` plus the number of bytes written.
40691cb0ef41Sopenharmony_ci
40701cb0ef41Sopenharmony_ciWrites `value` to `buf` at the specified `offset` as little-endian
40711cb0ef41Sopenharmony_ci
40721cb0ef41Sopenharmony_ci```mjs
40731cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
40741cb0ef41Sopenharmony_ci
40751cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(8);
40761cb0ef41Sopenharmony_ci
40771cb0ef41Sopenharmony_cibuf.writeBigUInt64LE(0xdecafafecacefaden, 0);
40781cb0ef41Sopenharmony_ci
40791cb0ef41Sopenharmony_ciconsole.log(buf);
40801cb0ef41Sopenharmony_ci// Prints: <Buffer de fa ce ca fe fa ca de>
40811cb0ef41Sopenharmony_ci```
40821cb0ef41Sopenharmony_ci
40831cb0ef41Sopenharmony_ci```cjs
40841cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
40851cb0ef41Sopenharmony_ci
40861cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(8);
40871cb0ef41Sopenharmony_ci
40881cb0ef41Sopenharmony_cibuf.writeBigUInt64LE(0xdecafafecacefaden, 0);
40891cb0ef41Sopenharmony_ci
40901cb0ef41Sopenharmony_ciconsole.log(buf);
40911cb0ef41Sopenharmony_ci// Prints: <Buffer de fa ce ca fe fa ca de>
40921cb0ef41Sopenharmony_ci```
40931cb0ef41Sopenharmony_ci
40941cb0ef41Sopenharmony_ciThis function is also available under the `writeBigUint64LE` alias.
40951cb0ef41Sopenharmony_ci
40961cb0ef41Sopenharmony_ci### `buf.writeDoubleBE(value[, offset])`
40971cb0ef41Sopenharmony_ci
40981cb0ef41Sopenharmony_ci<!-- YAML
40991cb0ef41Sopenharmony_ciadded: v0.11.15
41001cb0ef41Sopenharmony_cichanges:
41011cb0ef41Sopenharmony_ci  - version: v10.0.0
41021cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
41031cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
41041cb0ef41Sopenharmony_ci                 to `uint32` anymore.
41051cb0ef41Sopenharmony_ci-->
41061cb0ef41Sopenharmony_ci
41071cb0ef41Sopenharmony_ci* `value` {number} Number to be written to `buf`.
41081cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to write. Must
41091cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 8`. **Default:** `0`.
41101cb0ef41Sopenharmony_ci* Returns: {integer} `offset` plus the number of bytes written.
41111cb0ef41Sopenharmony_ci
41121cb0ef41Sopenharmony_ciWrites `value` to `buf` at the specified `offset` as big-endian. The `value`
41131cb0ef41Sopenharmony_cimust be a JavaScript number. Behavior is undefined when `value` is anything
41141cb0ef41Sopenharmony_ciother than a JavaScript number.
41151cb0ef41Sopenharmony_ci
41161cb0ef41Sopenharmony_ci```mjs
41171cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
41181cb0ef41Sopenharmony_ci
41191cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(8);
41201cb0ef41Sopenharmony_ci
41211cb0ef41Sopenharmony_cibuf.writeDoubleBE(123.456, 0);
41221cb0ef41Sopenharmony_ci
41231cb0ef41Sopenharmony_ciconsole.log(buf);
41241cb0ef41Sopenharmony_ci// Prints: <Buffer 40 5e dd 2f 1a 9f be 77>
41251cb0ef41Sopenharmony_ci```
41261cb0ef41Sopenharmony_ci
41271cb0ef41Sopenharmony_ci```cjs
41281cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
41291cb0ef41Sopenharmony_ci
41301cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(8);
41311cb0ef41Sopenharmony_ci
41321cb0ef41Sopenharmony_cibuf.writeDoubleBE(123.456, 0);
41331cb0ef41Sopenharmony_ci
41341cb0ef41Sopenharmony_ciconsole.log(buf);
41351cb0ef41Sopenharmony_ci// Prints: <Buffer 40 5e dd 2f 1a 9f be 77>
41361cb0ef41Sopenharmony_ci```
41371cb0ef41Sopenharmony_ci
41381cb0ef41Sopenharmony_ci### `buf.writeDoubleLE(value[, offset])`
41391cb0ef41Sopenharmony_ci
41401cb0ef41Sopenharmony_ci<!-- YAML
41411cb0ef41Sopenharmony_ciadded: v0.11.15
41421cb0ef41Sopenharmony_cichanges:
41431cb0ef41Sopenharmony_ci  - version: v10.0.0
41441cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
41451cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
41461cb0ef41Sopenharmony_ci                 to `uint32` anymore.
41471cb0ef41Sopenharmony_ci-->
41481cb0ef41Sopenharmony_ci
41491cb0ef41Sopenharmony_ci* `value` {number} Number to be written to `buf`.
41501cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to write. Must
41511cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 8`. **Default:** `0`.
41521cb0ef41Sopenharmony_ci* Returns: {integer} `offset` plus the number of bytes written.
41531cb0ef41Sopenharmony_ci
41541cb0ef41Sopenharmony_ciWrites `value` to `buf` at the specified `offset` as little-endian. The `value`
41551cb0ef41Sopenharmony_cimust be a JavaScript number. Behavior is undefined when `value` is anything
41561cb0ef41Sopenharmony_ciother than a JavaScript number.
41571cb0ef41Sopenharmony_ci
41581cb0ef41Sopenharmony_ci```mjs
41591cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
41601cb0ef41Sopenharmony_ci
41611cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(8);
41621cb0ef41Sopenharmony_ci
41631cb0ef41Sopenharmony_cibuf.writeDoubleLE(123.456, 0);
41641cb0ef41Sopenharmony_ci
41651cb0ef41Sopenharmony_ciconsole.log(buf);
41661cb0ef41Sopenharmony_ci// Prints: <Buffer 77 be 9f 1a 2f dd 5e 40>
41671cb0ef41Sopenharmony_ci```
41681cb0ef41Sopenharmony_ci
41691cb0ef41Sopenharmony_ci```cjs
41701cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
41711cb0ef41Sopenharmony_ci
41721cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(8);
41731cb0ef41Sopenharmony_ci
41741cb0ef41Sopenharmony_cibuf.writeDoubleLE(123.456, 0);
41751cb0ef41Sopenharmony_ci
41761cb0ef41Sopenharmony_ciconsole.log(buf);
41771cb0ef41Sopenharmony_ci// Prints: <Buffer 77 be 9f 1a 2f dd 5e 40>
41781cb0ef41Sopenharmony_ci```
41791cb0ef41Sopenharmony_ci
41801cb0ef41Sopenharmony_ci### `buf.writeFloatBE(value[, offset])`
41811cb0ef41Sopenharmony_ci
41821cb0ef41Sopenharmony_ci<!-- YAML
41831cb0ef41Sopenharmony_ciadded: v0.11.15
41841cb0ef41Sopenharmony_cichanges:
41851cb0ef41Sopenharmony_ci  - version: v10.0.0
41861cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
41871cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
41881cb0ef41Sopenharmony_ci                 to `uint32` anymore.
41891cb0ef41Sopenharmony_ci-->
41901cb0ef41Sopenharmony_ci
41911cb0ef41Sopenharmony_ci* `value` {number} Number to be written to `buf`.
41921cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to write. Must
41931cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.
41941cb0ef41Sopenharmony_ci* Returns: {integer} `offset` plus the number of bytes written.
41951cb0ef41Sopenharmony_ci
41961cb0ef41Sopenharmony_ciWrites `value` to `buf` at the specified `offset` as big-endian. Behavior is
41971cb0ef41Sopenharmony_ciundefined when `value` is anything other than a JavaScript number.
41981cb0ef41Sopenharmony_ci
41991cb0ef41Sopenharmony_ci```mjs
42001cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
42011cb0ef41Sopenharmony_ci
42021cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(4);
42031cb0ef41Sopenharmony_ci
42041cb0ef41Sopenharmony_cibuf.writeFloatBE(0xcafebabe, 0);
42051cb0ef41Sopenharmony_ci
42061cb0ef41Sopenharmony_ciconsole.log(buf);
42071cb0ef41Sopenharmony_ci// Prints: <Buffer 4f 4a fe bb>
42081cb0ef41Sopenharmony_ci```
42091cb0ef41Sopenharmony_ci
42101cb0ef41Sopenharmony_ci```cjs
42111cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
42121cb0ef41Sopenharmony_ci
42131cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(4);
42141cb0ef41Sopenharmony_ci
42151cb0ef41Sopenharmony_cibuf.writeFloatBE(0xcafebabe, 0);
42161cb0ef41Sopenharmony_ci
42171cb0ef41Sopenharmony_ciconsole.log(buf);
42181cb0ef41Sopenharmony_ci// Prints: <Buffer 4f 4a fe bb>
42191cb0ef41Sopenharmony_ci```
42201cb0ef41Sopenharmony_ci
42211cb0ef41Sopenharmony_ci### `buf.writeFloatLE(value[, offset])`
42221cb0ef41Sopenharmony_ci
42231cb0ef41Sopenharmony_ci<!-- YAML
42241cb0ef41Sopenharmony_ciadded: v0.11.15
42251cb0ef41Sopenharmony_cichanges:
42261cb0ef41Sopenharmony_ci  - version: v10.0.0
42271cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
42281cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
42291cb0ef41Sopenharmony_ci                 to `uint32` anymore.
42301cb0ef41Sopenharmony_ci-->
42311cb0ef41Sopenharmony_ci
42321cb0ef41Sopenharmony_ci* `value` {number} Number to be written to `buf`.
42331cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to write. Must
42341cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.
42351cb0ef41Sopenharmony_ci* Returns: {integer} `offset` plus the number of bytes written.
42361cb0ef41Sopenharmony_ci
42371cb0ef41Sopenharmony_ciWrites `value` to `buf` at the specified `offset` as little-endian. Behavior is
42381cb0ef41Sopenharmony_ciundefined when `value` is anything other than a JavaScript number.
42391cb0ef41Sopenharmony_ci
42401cb0ef41Sopenharmony_ci```mjs
42411cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
42421cb0ef41Sopenharmony_ci
42431cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(4);
42441cb0ef41Sopenharmony_ci
42451cb0ef41Sopenharmony_cibuf.writeFloatLE(0xcafebabe, 0);
42461cb0ef41Sopenharmony_ci
42471cb0ef41Sopenharmony_ciconsole.log(buf);
42481cb0ef41Sopenharmony_ci// Prints: <Buffer bb fe 4a 4f>
42491cb0ef41Sopenharmony_ci```
42501cb0ef41Sopenharmony_ci
42511cb0ef41Sopenharmony_ci```cjs
42521cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
42531cb0ef41Sopenharmony_ci
42541cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(4);
42551cb0ef41Sopenharmony_ci
42561cb0ef41Sopenharmony_cibuf.writeFloatLE(0xcafebabe, 0);
42571cb0ef41Sopenharmony_ci
42581cb0ef41Sopenharmony_ciconsole.log(buf);
42591cb0ef41Sopenharmony_ci// Prints: <Buffer bb fe 4a 4f>
42601cb0ef41Sopenharmony_ci```
42611cb0ef41Sopenharmony_ci
42621cb0ef41Sopenharmony_ci### `buf.writeInt8(value[, offset])`
42631cb0ef41Sopenharmony_ci
42641cb0ef41Sopenharmony_ci<!-- YAML
42651cb0ef41Sopenharmony_ciadded: v0.5.0
42661cb0ef41Sopenharmony_cichanges:
42671cb0ef41Sopenharmony_ci  - version: v10.0.0
42681cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
42691cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
42701cb0ef41Sopenharmony_ci                 to `uint32` anymore.
42711cb0ef41Sopenharmony_ci-->
42721cb0ef41Sopenharmony_ci
42731cb0ef41Sopenharmony_ci* `value` {integer} Number to be written to `buf`.
42741cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to write. Must
42751cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 1`. **Default:** `0`.
42761cb0ef41Sopenharmony_ci* Returns: {integer} `offset` plus the number of bytes written.
42771cb0ef41Sopenharmony_ci
42781cb0ef41Sopenharmony_ciWrites `value` to `buf` at the specified `offset`. `value` must be a valid
42791cb0ef41Sopenharmony_cisigned 8-bit integer. Behavior is undefined when `value` is anything other than
42801cb0ef41Sopenharmony_cia signed 8-bit integer.
42811cb0ef41Sopenharmony_ci
42821cb0ef41Sopenharmony_ci`value` is interpreted and written as a two's complement signed integer.
42831cb0ef41Sopenharmony_ci
42841cb0ef41Sopenharmony_ci```mjs
42851cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
42861cb0ef41Sopenharmony_ci
42871cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(2);
42881cb0ef41Sopenharmony_ci
42891cb0ef41Sopenharmony_cibuf.writeInt8(2, 0);
42901cb0ef41Sopenharmony_cibuf.writeInt8(-2, 1);
42911cb0ef41Sopenharmony_ci
42921cb0ef41Sopenharmony_ciconsole.log(buf);
42931cb0ef41Sopenharmony_ci// Prints: <Buffer 02 fe>
42941cb0ef41Sopenharmony_ci```
42951cb0ef41Sopenharmony_ci
42961cb0ef41Sopenharmony_ci```cjs
42971cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
42981cb0ef41Sopenharmony_ci
42991cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(2);
43001cb0ef41Sopenharmony_ci
43011cb0ef41Sopenharmony_cibuf.writeInt8(2, 0);
43021cb0ef41Sopenharmony_cibuf.writeInt8(-2, 1);
43031cb0ef41Sopenharmony_ci
43041cb0ef41Sopenharmony_ciconsole.log(buf);
43051cb0ef41Sopenharmony_ci// Prints: <Buffer 02 fe>
43061cb0ef41Sopenharmony_ci```
43071cb0ef41Sopenharmony_ci
43081cb0ef41Sopenharmony_ci### `buf.writeInt16BE(value[, offset])`
43091cb0ef41Sopenharmony_ci
43101cb0ef41Sopenharmony_ci<!-- YAML
43111cb0ef41Sopenharmony_ciadded: v0.5.5
43121cb0ef41Sopenharmony_cichanges:
43131cb0ef41Sopenharmony_ci  - version: v10.0.0
43141cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
43151cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
43161cb0ef41Sopenharmony_ci                 to `uint32` anymore.
43171cb0ef41Sopenharmony_ci-->
43181cb0ef41Sopenharmony_ci
43191cb0ef41Sopenharmony_ci* `value` {integer} Number to be written to `buf`.
43201cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to write. Must
43211cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 2`. **Default:** `0`.
43221cb0ef41Sopenharmony_ci* Returns: {integer} `offset` plus the number of bytes written.
43231cb0ef41Sopenharmony_ci
43241cb0ef41Sopenharmony_ciWrites `value` to `buf` at the specified `offset` as big-endian.  The `value`
43251cb0ef41Sopenharmony_cimust be a valid signed 16-bit integer. Behavior is undefined when `value` is
43261cb0ef41Sopenharmony_cianything other than a signed 16-bit integer.
43271cb0ef41Sopenharmony_ci
43281cb0ef41Sopenharmony_ciThe `value` is interpreted and written as a two's complement signed integer.
43291cb0ef41Sopenharmony_ci
43301cb0ef41Sopenharmony_ci```mjs
43311cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
43321cb0ef41Sopenharmony_ci
43331cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(2);
43341cb0ef41Sopenharmony_ci
43351cb0ef41Sopenharmony_cibuf.writeInt16BE(0x0102, 0);
43361cb0ef41Sopenharmony_ci
43371cb0ef41Sopenharmony_ciconsole.log(buf);
43381cb0ef41Sopenharmony_ci// Prints: <Buffer 01 02>
43391cb0ef41Sopenharmony_ci```
43401cb0ef41Sopenharmony_ci
43411cb0ef41Sopenharmony_ci```cjs
43421cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
43431cb0ef41Sopenharmony_ci
43441cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(2);
43451cb0ef41Sopenharmony_ci
43461cb0ef41Sopenharmony_cibuf.writeInt16BE(0x0102, 0);
43471cb0ef41Sopenharmony_ci
43481cb0ef41Sopenharmony_ciconsole.log(buf);
43491cb0ef41Sopenharmony_ci// Prints: <Buffer 01 02>
43501cb0ef41Sopenharmony_ci```
43511cb0ef41Sopenharmony_ci
43521cb0ef41Sopenharmony_ci### `buf.writeInt16LE(value[, offset])`
43531cb0ef41Sopenharmony_ci
43541cb0ef41Sopenharmony_ci<!-- YAML
43551cb0ef41Sopenharmony_ciadded: v0.5.5
43561cb0ef41Sopenharmony_cichanges:
43571cb0ef41Sopenharmony_ci  - version: v10.0.0
43581cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
43591cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
43601cb0ef41Sopenharmony_ci                 to `uint32` anymore.
43611cb0ef41Sopenharmony_ci-->
43621cb0ef41Sopenharmony_ci
43631cb0ef41Sopenharmony_ci* `value` {integer} Number to be written to `buf`.
43641cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to write. Must
43651cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 2`. **Default:** `0`.
43661cb0ef41Sopenharmony_ci* Returns: {integer} `offset` plus the number of bytes written.
43671cb0ef41Sopenharmony_ci
43681cb0ef41Sopenharmony_ciWrites `value` to `buf` at the specified `offset` as little-endian.  The `value`
43691cb0ef41Sopenharmony_cimust be a valid signed 16-bit integer. Behavior is undefined when `value` is
43701cb0ef41Sopenharmony_cianything other than a signed 16-bit integer.
43711cb0ef41Sopenharmony_ci
43721cb0ef41Sopenharmony_ciThe `value` is interpreted and written as a two's complement signed integer.
43731cb0ef41Sopenharmony_ci
43741cb0ef41Sopenharmony_ci```mjs
43751cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
43761cb0ef41Sopenharmony_ci
43771cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(2);
43781cb0ef41Sopenharmony_ci
43791cb0ef41Sopenharmony_cibuf.writeInt16LE(0x0304, 0);
43801cb0ef41Sopenharmony_ci
43811cb0ef41Sopenharmony_ciconsole.log(buf);
43821cb0ef41Sopenharmony_ci// Prints: <Buffer 04 03>
43831cb0ef41Sopenharmony_ci```
43841cb0ef41Sopenharmony_ci
43851cb0ef41Sopenharmony_ci```cjs
43861cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
43871cb0ef41Sopenharmony_ci
43881cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(2);
43891cb0ef41Sopenharmony_ci
43901cb0ef41Sopenharmony_cibuf.writeInt16LE(0x0304, 0);
43911cb0ef41Sopenharmony_ci
43921cb0ef41Sopenharmony_ciconsole.log(buf);
43931cb0ef41Sopenharmony_ci// Prints: <Buffer 04 03>
43941cb0ef41Sopenharmony_ci```
43951cb0ef41Sopenharmony_ci
43961cb0ef41Sopenharmony_ci### `buf.writeInt32BE(value[, offset])`
43971cb0ef41Sopenharmony_ci
43981cb0ef41Sopenharmony_ci<!-- YAML
43991cb0ef41Sopenharmony_ciadded: v0.5.5
44001cb0ef41Sopenharmony_cichanges:
44011cb0ef41Sopenharmony_ci  - version: v10.0.0
44021cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
44031cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
44041cb0ef41Sopenharmony_ci                 to `uint32` anymore.
44051cb0ef41Sopenharmony_ci-->
44061cb0ef41Sopenharmony_ci
44071cb0ef41Sopenharmony_ci* `value` {integer} Number to be written to `buf`.
44081cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to write. Must
44091cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.
44101cb0ef41Sopenharmony_ci* Returns: {integer} `offset` plus the number of bytes written.
44111cb0ef41Sopenharmony_ci
44121cb0ef41Sopenharmony_ciWrites `value` to `buf` at the specified `offset` as big-endian. The `value`
44131cb0ef41Sopenharmony_cimust be a valid signed 32-bit integer. Behavior is undefined when `value` is
44141cb0ef41Sopenharmony_cianything other than a signed 32-bit integer.
44151cb0ef41Sopenharmony_ci
44161cb0ef41Sopenharmony_ciThe `value` is interpreted and written as a two's complement signed integer.
44171cb0ef41Sopenharmony_ci
44181cb0ef41Sopenharmony_ci```mjs
44191cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
44201cb0ef41Sopenharmony_ci
44211cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(4);
44221cb0ef41Sopenharmony_ci
44231cb0ef41Sopenharmony_cibuf.writeInt32BE(0x01020304, 0);
44241cb0ef41Sopenharmony_ci
44251cb0ef41Sopenharmony_ciconsole.log(buf);
44261cb0ef41Sopenharmony_ci// Prints: <Buffer 01 02 03 04>
44271cb0ef41Sopenharmony_ci```
44281cb0ef41Sopenharmony_ci
44291cb0ef41Sopenharmony_ci```cjs
44301cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
44311cb0ef41Sopenharmony_ci
44321cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(4);
44331cb0ef41Sopenharmony_ci
44341cb0ef41Sopenharmony_cibuf.writeInt32BE(0x01020304, 0);
44351cb0ef41Sopenharmony_ci
44361cb0ef41Sopenharmony_ciconsole.log(buf);
44371cb0ef41Sopenharmony_ci// Prints: <Buffer 01 02 03 04>
44381cb0ef41Sopenharmony_ci```
44391cb0ef41Sopenharmony_ci
44401cb0ef41Sopenharmony_ci### `buf.writeInt32LE(value[, offset])`
44411cb0ef41Sopenharmony_ci
44421cb0ef41Sopenharmony_ci<!-- YAML
44431cb0ef41Sopenharmony_ciadded: v0.5.5
44441cb0ef41Sopenharmony_cichanges:
44451cb0ef41Sopenharmony_ci  - version: v10.0.0
44461cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
44471cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
44481cb0ef41Sopenharmony_ci                 to `uint32` anymore.
44491cb0ef41Sopenharmony_ci-->
44501cb0ef41Sopenharmony_ci
44511cb0ef41Sopenharmony_ci* `value` {integer} Number to be written to `buf`.
44521cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to write. Must
44531cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.
44541cb0ef41Sopenharmony_ci* Returns: {integer} `offset` plus the number of bytes written.
44551cb0ef41Sopenharmony_ci
44561cb0ef41Sopenharmony_ciWrites `value` to `buf` at the specified `offset` as little-endian. The `value`
44571cb0ef41Sopenharmony_cimust be a valid signed 32-bit integer. Behavior is undefined when `value` is
44581cb0ef41Sopenharmony_cianything other than a signed 32-bit integer.
44591cb0ef41Sopenharmony_ci
44601cb0ef41Sopenharmony_ciThe `value` is interpreted and written as a two's complement signed integer.
44611cb0ef41Sopenharmony_ci
44621cb0ef41Sopenharmony_ci```mjs
44631cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
44641cb0ef41Sopenharmony_ci
44651cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(4);
44661cb0ef41Sopenharmony_ci
44671cb0ef41Sopenharmony_cibuf.writeInt32LE(0x05060708, 0);
44681cb0ef41Sopenharmony_ci
44691cb0ef41Sopenharmony_ciconsole.log(buf);
44701cb0ef41Sopenharmony_ci// Prints: <Buffer 08 07 06 05>
44711cb0ef41Sopenharmony_ci```
44721cb0ef41Sopenharmony_ci
44731cb0ef41Sopenharmony_ci```cjs
44741cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
44751cb0ef41Sopenharmony_ci
44761cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(4);
44771cb0ef41Sopenharmony_ci
44781cb0ef41Sopenharmony_cibuf.writeInt32LE(0x05060708, 0);
44791cb0ef41Sopenharmony_ci
44801cb0ef41Sopenharmony_ciconsole.log(buf);
44811cb0ef41Sopenharmony_ci// Prints: <Buffer 08 07 06 05>
44821cb0ef41Sopenharmony_ci```
44831cb0ef41Sopenharmony_ci
44841cb0ef41Sopenharmony_ci### `buf.writeIntBE(value, offset, byteLength)`
44851cb0ef41Sopenharmony_ci
44861cb0ef41Sopenharmony_ci<!-- YAML
44871cb0ef41Sopenharmony_ciadded: v0.11.15
44881cb0ef41Sopenharmony_cichanges:
44891cb0ef41Sopenharmony_ci  - version: v10.0.0
44901cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
44911cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
44921cb0ef41Sopenharmony_ci                 and `byteLength` to `uint32` anymore.
44931cb0ef41Sopenharmony_ci-->
44941cb0ef41Sopenharmony_ci
44951cb0ef41Sopenharmony_ci* `value` {integer} Number to be written to `buf`.
44961cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to write. Must
44971cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - byteLength`.
44981cb0ef41Sopenharmony_ci* `byteLength` {integer} Number of bytes to write. Must satisfy
44991cb0ef41Sopenharmony_ci  `0 < byteLength <= 6`.
45001cb0ef41Sopenharmony_ci* Returns: {integer} `offset` plus the number of bytes written.
45011cb0ef41Sopenharmony_ci
45021cb0ef41Sopenharmony_ciWrites `byteLength` bytes of `value` to `buf` at the specified `offset`
45031cb0ef41Sopenharmony_cias big-endian. Supports up to 48 bits of accuracy. Behavior is undefined when
45041cb0ef41Sopenharmony_ci`value` is anything other than a signed integer.
45051cb0ef41Sopenharmony_ci
45061cb0ef41Sopenharmony_ci```mjs
45071cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
45081cb0ef41Sopenharmony_ci
45091cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(6);
45101cb0ef41Sopenharmony_ci
45111cb0ef41Sopenharmony_cibuf.writeIntBE(0x1234567890ab, 0, 6);
45121cb0ef41Sopenharmony_ci
45131cb0ef41Sopenharmony_ciconsole.log(buf);
45141cb0ef41Sopenharmony_ci// Prints: <Buffer 12 34 56 78 90 ab>
45151cb0ef41Sopenharmony_ci```
45161cb0ef41Sopenharmony_ci
45171cb0ef41Sopenharmony_ci```cjs
45181cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
45191cb0ef41Sopenharmony_ci
45201cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(6);
45211cb0ef41Sopenharmony_ci
45221cb0ef41Sopenharmony_cibuf.writeIntBE(0x1234567890ab, 0, 6);
45231cb0ef41Sopenharmony_ci
45241cb0ef41Sopenharmony_ciconsole.log(buf);
45251cb0ef41Sopenharmony_ci// Prints: <Buffer 12 34 56 78 90 ab>
45261cb0ef41Sopenharmony_ci```
45271cb0ef41Sopenharmony_ci
45281cb0ef41Sopenharmony_ci### `buf.writeIntLE(value, offset, byteLength)`
45291cb0ef41Sopenharmony_ci
45301cb0ef41Sopenharmony_ci<!-- YAML
45311cb0ef41Sopenharmony_ciadded: v0.11.15
45321cb0ef41Sopenharmony_cichanges:
45331cb0ef41Sopenharmony_ci  - version: v10.0.0
45341cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
45351cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
45361cb0ef41Sopenharmony_ci                 and `byteLength` to `uint32` anymore.
45371cb0ef41Sopenharmony_ci-->
45381cb0ef41Sopenharmony_ci
45391cb0ef41Sopenharmony_ci* `value` {integer} Number to be written to `buf`.
45401cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to write. Must
45411cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - byteLength`.
45421cb0ef41Sopenharmony_ci* `byteLength` {integer} Number of bytes to write. Must satisfy
45431cb0ef41Sopenharmony_ci  `0 < byteLength <= 6`.
45441cb0ef41Sopenharmony_ci* Returns: {integer} `offset` plus the number of bytes written.
45451cb0ef41Sopenharmony_ci
45461cb0ef41Sopenharmony_ciWrites `byteLength` bytes of `value` to `buf` at the specified `offset`
45471cb0ef41Sopenharmony_cias little-endian. Supports up to 48 bits of accuracy. Behavior is undefined
45481cb0ef41Sopenharmony_ciwhen `value` is anything other than a signed integer.
45491cb0ef41Sopenharmony_ci
45501cb0ef41Sopenharmony_ci```mjs
45511cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
45521cb0ef41Sopenharmony_ci
45531cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(6);
45541cb0ef41Sopenharmony_ci
45551cb0ef41Sopenharmony_cibuf.writeIntLE(0x1234567890ab, 0, 6);
45561cb0ef41Sopenharmony_ci
45571cb0ef41Sopenharmony_ciconsole.log(buf);
45581cb0ef41Sopenharmony_ci// Prints: <Buffer ab 90 78 56 34 12>
45591cb0ef41Sopenharmony_ci```
45601cb0ef41Sopenharmony_ci
45611cb0ef41Sopenharmony_ci```cjs
45621cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
45631cb0ef41Sopenharmony_ci
45641cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(6);
45651cb0ef41Sopenharmony_ci
45661cb0ef41Sopenharmony_cibuf.writeIntLE(0x1234567890ab, 0, 6);
45671cb0ef41Sopenharmony_ci
45681cb0ef41Sopenharmony_ciconsole.log(buf);
45691cb0ef41Sopenharmony_ci// Prints: <Buffer ab 90 78 56 34 12>
45701cb0ef41Sopenharmony_ci```
45711cb0ef41Sopenharmony_ci
45721cb0ef41Sopenharmony_ci### `buf.writeUInt8(value[, offset])`
45731cb0ef41Sopenharmony_ci
45741cb0ef41Sopenharmony_ci<!-- YAML
45751cb0ef41Sopenharmony_ciadded: v0.5.0
45761cb0ef41Sopenharmony_cichanges:
45771cb0ef41Sopenharmony_ci  - version:
45781cb0ef41Sopenharmony_ci    - v14.9.0
45791cb0ef41Sopenharmony_ci    - v12.19.0
45801cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34729
45811cb0ef41Sopenharmony_ci    description: This function is also available as `buf.writeUint8()`.
45821cb0ef41Sopenharmony_ci  - version: v10.0.0
45831cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
45841cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
45851cb0ef41Sopenharmony_ci                 to `uint32` anymore.
45861cb0ef41Sopenharmony_ci-->
45871cb0ef41Sopenharmony_ci
45881cb0ef41Sopenharmony_ci* `value` {integer} Number to be written to `buf`.
45891cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to write. Must
45901cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 1`. **Default:** `0`.
45911cb0ef41Sopenharmony_ci* Returns: {integer} `offset` plus the number of bytes written.
45921cb0ef41Sopenharmony_ci
45931cb0ef41Sopenharmony_ciWrites `value` to `buf` at the specified `offset`. `value` must be a
45941cb0ef41Sopenharmony_civalid unsigned 8-bit integer. Behavior is undefined when `value` is anything
45951cb0ef41Sopenharmony_ciother than an unsigned 8-bit integer.
45961cb0ef41Sopenharmony_ci
45971cb0ef41Sopenharmony_ciThis function is also available under the `writeUint8` alias.
45981cb0ef41Sopenharmony_ci
45991cb0ef41Sopenharmony_ci```mjs
46001cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
46011cb0ef41Sopenharmony_ci
46021cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(4);
46031cb0ef41Sopenharmony_ci
46041cb0ef41Sopenharmony_cibuf.writeUInt8(0x3, 0);
46051cb0ef41Sopenharmony_cibuf.writeUInt8(0x4, 1);
46061cb0ef41Sopenharmony_cibuf.writeUInt8(0x23, 2);
46071cb0ef41Sopenharmony_cibuf.writeUInt8(0x42, 3);
46081cb0ef41Sopenharmony_ci
46091cb0ef41Sopenharmony_ciconsole.log(buf);
46101cb0ef41Sopenharmony_ci// Prints: <Buffer 03 04 23 42>
46111cb0ef41Sopenharmony_ci```
46121cb0ef41Sopenharmony_ci
46131cb0ef41Sopenharmony_ci```cjs
46141cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
46151cb0ef41Sopenharmony_ci
46161cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(4);
46171cb0ef41Sopenharmony_ci
46181cb0ef41Sopenharmony_cibuf.writeUInt8(0x3, 0);
46191cb0ef41Sopenharmony_cibuf.writeUInt8(0x4, 1);
46201cb0ef41Sopenharmony_cibuf.writeUInt8(0x23, 2);
46211cb0ef41Sopenharmony_cibuf.writeUInt8(0x42, 3);
46221cb0ef41Sopenharmony_ci
46231cb0ef41Sopenharmony_ciconsole.log(buf);
46241cb0ef41Sopenharmony_ci// Prints: <Buffer 03 04 23 42>
46251cb0ef41Sopenharmony_ci```
46261cb0ef41Sopenharmony_ci
46271cb0ef41Sopenharmony_ci### `buf.writeUInt16BE(value[, offset])`
46281cb0ef41Sopenharmony_ci
46291cb0ef41Sopenharmony_ci<!-- YAML
46301cb0ef41Sopenharmony_ciadded: v0.5.5
46311cb0ef41Sopenharmony_cichanges:
46321cb0ef41Sopenharmony_ci  - version:
46331cb0ef41Sopenharmony_ci    - v14.9.0
46341cb0ef41Sopenharmony_ci    - v12.19.0
46351cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34729
46361cb0ef41Sopenharmony_ci    description: This function is also available as `buf.writeUint16BE()`.
46371cb0ef41Sopenharmony_ci  - version: v10.0.0
46381cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
46391cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
46401cb0ef41Sopenharmony_ci                 to `uint32` anymore.
46411cb0ef41Sopenharmony_ci-->
46421cb0ef41Sopenharmony_ci
46431cb0ef41Sopenharmony_ci* `value` {integer} Number to be written to `buf`.
46441cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to write. Must
46451cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 2`. **Default:** `0`.
46461cb0ef41Sopenharmony_ci* Returns: {integer} `offset` plus the number of bytes written.
46471cb0ef41Sopenharmony_ci
46481cb0ef41Sopenharmony_ciWrites `value` to `buf` at the specified `offset` as big-endian. The `value`
46491cb0ef41Sopenharmony_cimust be a valid unsigned 16-bit integer. Behavior is undefined when `value`
46501cb0ef41Sopenharmony_ciis anything other than an unsigned 16-bit integer.
46511cb0ef41Sopenharmony_ci
46521cb0ef41Sopenharmony_ciThis function is also available under the `writeUint16BE` alias.
46531cb0ef41Sopenharmony_ci
46541cb0ef41Sopenharmony_ci```mjs
46551cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
46561cb0ef41Sopenharmony_ci
46571cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(4);
46581cb0ef41Sopenharmony_ci
46591cb0ef41Sopenharmony_cibuf.writeUInt16BE(0xdead, 0);
46601cb0ef41Sopenharmony_cibuf.writeUInt16BE(0xbeef, 2);
46611cb0ef41Sopenharmony_ci
46621cb0ef41Sopenharmony_ciconsole.log(buf);
46631cb0ef41Sopenharmony_ci// Prints: <Buffer de ad be ef>
46641cb0ef41Sopenharmony_ci```
46651cb0ef41Sopenharmony_ci
46661cb0ef41Sopenharmony_ci```cjs
46671cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
46681cb0ef41Sopenharmony_ci
46691cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(4);
46701cb0ef41Sopenharmony_ci
46711cb0ef41Sopenharmony_cibuf.writeUInt16BE(0xdead, 0);
46721cb0ef41Sopenharmony_cibuf.writeUInt16BE(0xbeef, 2);
46731cb0ef41Sopenharmony_ci
46741cb0ef41Sopenharmony_ciconsole.log(buf);
46751cb0ef41Sopenharmony_ci// Prints: <Buffer de ad be ef>
46761cb0ef41Sopenharmony_ci```
46771cb0ef41Sopenharmony_ci
46781cb0ef41Sopenharmony_ci### `buf.writeUInt16LE(value[, offset])`
46791cb0ef41Sopenharmony_ci
46801cb0ef41Sopenharmony_ci<!-- YAML
46811cb0ef41Sopenharmony_ciadded: v0.5.5
46821cb0ef41Sopenharmony_cichanges:
46831cb0ef41Sopenharmony_ci  - version:
46841cb0ef41Sopenharmony_ci    - v14.9.0
46851cb0ef41Sopenharmony_ci    - v12.19.0
46861cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34729
46871cb0ef41Sopenharmony_ci    description: This function is also available as `buf.writeUint16LE()`.
46881cb0ef41Sopenharmony_ci  - version: v10.0.0
46891cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
46901cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
46911cb0ef41Sopenharmony_ci                 to `uint32` anymore.
46921cb0ef41Sopenharmony_ci-->
46931cb0ef41Sopenharmony_ci
46941cb0ef41Sopenharmony_ci* `value` {integer} Number to be written to `buf`.
46951cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to write. Must
46961cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 2`. **Default:** `0`.
46971cb0ef41Sopenharmony_ci* Returns: {integer} `offset` plus the number of bytes written.
46981cb0ef41Sopenharmony_ci
46991cb0ef41Sopenharmony_ciWrites `value` to `buf` at the specified `offset` as little-endian. The `value`
47001cb0ef41Sopenharmony_cimust be a valid unsigned 16-bit integer. Behavior is undefined when `value` is
47011cb0ef41Sopenharmony_cianything other than an unsigned 16-bit integer.
47021cb0ef41Sopenharmony_ci
47031cb0ef41Sopenharmony_ciThis function is also available under the `writeUint16LE` alias.
47041cb0ef41Sopenharmony_ci
47051cb0ef41Sopenharmony_ci```mjs
47061cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
47071cb0ef41Sopenharmony_ci
47081cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(4);
47091cb0ef41Sopenharmony_ci
47101cb0ef41Sopenharmony_cibuf.writeUInt16LE(0xdead, 0);
47111cb0ef41Sopenharmony_cibuf.writeUInt16LE(0xbeef, 2);
47121cb0ef41Sopenharmony_ci
47131cb0ef41Sopenharmony_ciconsole.log(buf);
47141cb0ef41Sopenharmony_ci// Prints: <Buffer ad de ef be>
47151cb0ef41Sopenharmony_ci```
47161cb0ef41Sopenharmony_ci
47171cb0ef41Sopenharmony_ci```cjs
47181cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
47191cb0ef41Sopenharmony_ci
47201cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(4);
47211cb0ef41Sopenharmony_ci
47221cb0ef41Sopenharmony_cibuf.writeUInt16LE(0xdead, 0);
47231cb0ef41Sopenharmony_cibuf.writeUInt16LE(0xbeef, 2);
47241cb0ef41Sopenharmony_ci
47251cb0ef41Sopenharmony_ciconsole.log(buf);
47261cb0ef41Sopenharmony_ci// Prints: <Buffer ad de ef be>
47271cb0ef41Sopenharmony_ci```
47281cb0ef41Sopenharmony_ci
47291cb0ef41Sopenharmony_ci### `buf.writeUInt32BE(value[, offset])`
47301cb0ef41Sopenharmony_ci
47311cb0ef41Sopenharmony_ci<!-- YAML
47321cb0ef41Sopenharmony_ciadded: v0.5.5
47331cb0ef41Sopenharmony_cichanges:
47341cb0ef41Sopenharmony_ci  - version:
47351cb0ef41Sopenharmony_ci    - v14.9.0
47361cb0ef41Sopenharmony_ci    - v12.19.0
47371cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34729
47381cb0ef41Sopenharmony_ci    description: This function is also available as `buf.writeUint32BE()`.
47391cb0ef41Sopenharmony_ci  - version: v10.0.0
47401cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
47411cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
47421cb0ef41Sopenharmony_ci                 to `uint32` anymore.
47431cb0ef41Sopenharmony_ci-->
47441cb0ef41Sopenharmony_ci
47451cb0ef41Sopenharmony_ci* `value` {integer} Number to be written to `buf`.
47461cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to write. Must
47471cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.
47481cb0ef41Sopenharmony_ci* Returns: {integer} `offset` plus the number of bytes written.
47491cb0ef41Sopenharmony_ci
47501cb0ef41Sopenharmony_ciWrites `value` to `buf` at the specified `offset` as big-endian. The `value`
47511cb0ef41Sopenharmony_cimust be a valid unsigned 32-bit integer. Behavior is undefined when `value`
47521cb0ef41Sopenharmony_ciis anything other than an unsigned 32-bit integer.
47531cb0ef41Sopenharmony_ci
47541cb0ef41Sopenharmony_ciThis function is also available under the `writeUint32BE` alias.
47551cb0ef41Sopenharmony_ci
47561cb0ef41Sopenharmony_ci```mjs
47571cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
47581cb0ef41Sopenharmony_ci
47591cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(4);
47601cb0ef41Sopenharmony_ci
47611cb0ef41Sopenharmony_cibuf.writeUInt32BE(0xfeedface, 0);
47621cb0ef41Sopenharmony_ci
47631cb0ef41Sopenharmony_ciconsole.log(buf);
47641cb0ef41Sopenharmony_ci// Prints: <Buffer fe ed fa ce>
47651cb0ef41Sopenharmony_ci```
47661cb0ef41Sopenharmony_ci
47671cb0ef41Sopenharmony_ci```cjs
47681cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
47691cb0ef41Sopenharmony_ci
47701cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(4);
47711cb0ef41Sopenharmony_ci
47721cb0ef41Sopenharmony_cibuf.writeUInt32BE(0xfeedface, 0);
47731cb0ef41Sopenharmony_ci
47741cb0ef41Sopenharmony_ciconsole.log(buf);
47751cb0ef41Sopenharmony_ci// Prints: <Buffer fe ed fa ce>
47761cb0ef41Sopenharmony_ci```
47771cb0ef41Sopenharmony_ci
47781cb0ef41Sopenharmony_ci### `buf.writeUInt32LE(value[, offset])`
47791cb0ef41Sopenharmony_ci
47801cb0ef41Sopenharmony_ci<!-- YAML
47811cb0ef41Sopenharmony_ciadded: v0.5.5
47821cb0ef41Sopenharmony_cichanges:
47831cb0ef41Sopenharmony_ci  - version:
47841cb0ef41Sopenharmony_ci    - v14.9.0
47851cb0ef41Sopenharmony_ci    - v12.19.0
47861cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34729
47871cb0ef41Sopenharmony_ci    description: This function is also available as `buf.writeUint32LE()`.
47881cb0ef41Sopenharmony_ci  - version: v10.0.0
47891cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
47901cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
47911cb0ef41Sopenharmony_ci                 to `uint32` anymore.
47921cb0ef41Sopenharmony_ci-->
47931cb0ef41Sopenharmony_ci
47941cb0ef41Sopenharmony_ci* `value` {integer} Number to be written to `buf`.
47951cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to write. Must
47961cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.
47971cb0ef41Sopenharmony_ci* Returns: {integer} `offset` plus the number of bytes written.
47981cb0ef41Sopenharmony_ci
47991cb0ef41Sopenharmony_ciWrites `value` to `buf` at the specified `offset` as little-endian. The `value`
48001cb0ef41Sopenharmony_cimust be a valid unsigned 32-bit integer. Behavior is undefined when `value` is
48011cb0ef41Sopenharmony_cianything other than an unsigned 32-bit integer.
48021cb0ef41Sopenharmony_ci
48031cb0ef41Sopenharmony_ciThis function is also available under the `writeUint32LE` alias.
48041cb0ef41Sopenharmony_ci
48051cb0ef41Sopenharmony_ci```mjs
48061cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
48071cb0ef41Sopenharmony_ci
48081cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(4);
48091cb0ef41Sopenharmony_ci
48101cb0ef41Sopenharmony_cibuf.writeUInt32LE(0xfeedface, 0);
48111cb0ef41Sopenharmony_ci
48121cb0ef41Sopenharmony_ciconsole.log(buf);
48131cb0ef41Sopenharmony_ci// Prints: <Buffer ce fa ed fe>
48141cb0ef41Sopenharmony_ci```
48151cb0ef41Sopenharmony_ci
48161cb0ef41Sopenharmony_ci```cjs
48171cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
48181cb0ef41Sopenharmony_ci
48191cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(4);
48201cb0ef41Sopenharmony_ci
48211cb0ef41Sopenharmony_cibuf.writeUInt32LE(0xfeedface, 0);
48221cb0ef41Sopenharmony_ci
48231cb0ef41Sopenharmony_ciconsole.log(buf);
48241cb0ef41Sopenharmony_ci// Prints: <Buffer ce fa ed fe>
48251cb0ef41Sopenharmony_ci```
48261cb0ef41Sopenharmony_ci
48271cb0ef41Sopenharmony_ci### `buf.writeUIntBE(value, offset, byteLength)`
48281cb0ef41Sopenharmony_ci
48291cb0ef41Sopenharmony_ci<!-- YAML
48301cb0ef41Sopenharmony_ciadded: v0.5.5
48311cb0ef41Sopenharmony_cichanges:
48321cb0ef41Sopenharmony_ci  - version:
48331cb0ef41Sopenharmony_ci    - v14.9.0
48341cb0ef41Sopenharmony_ci    - v12.19.0
48351cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34729
48361cb0ef41Sopenharmony_ci    description: This function is also available as `buf.writeUintBE()`.
48371cb0ef41Sopenharmony_ci  - version: v10.0.0
48381cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
48391cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
48401cb0ef41Sopenharmony_ci                 and `byteLength` to `uint32` anymore.
48411cb0ef41Sopenharmony_ci-->
48421cb0ef41Sopenharmony_ci
48431cb0ef41Sopenharmony_ci* `value` {integer} Number to be written to `buf`.
48441cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to write. Must
48451cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - byteLength`.
48461cb0ef41Sopenharmony_ci* `byteLength` {integer} Number of bytes to write. Must satisfy
48471cb0ef41Sopenharmony_ci  `0 < byteLength <= 6`.
48481cb0ef41Sopenharmony_ci* Returns: {integer} `offset` plus the number of bytes written.
48491cb0ef41Sopenharmony_ci
48501cb0ef41Sopenharmony_ciWrites `byteLength` bytes of `value` to `buf` at the specified `offset`
48511cb0ef41Sopenharmony_cias big-endian. Supports up to 48 bits of accuracy. Behavior is undefined
48521cb0ef41Sopenharmony_ciwhen `value` is anything other than an unsigned integer.
48531cb0ef41Sopenharmony_ci
48541cb0ef41Sopenharmony_ciThis function is also available under the `writeUintBE` alias.
48551cb0ef41Sopenharmony_ci
48561cb0ef41Sopenharmony_ci```mjs
48571cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
48581cb0ef41Sopenharmony_ci
48591cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(6);
48601cb0ef41Sopenharmony_ci
48611cb0ef41Sopenharmony_cibuf.writeUIntBE(0x1234567890ab, 0, 6);
48621cb0ef41Sopenharmony_ci
48631cb0ef41Sopenharmony_ciconsole.log(buf);
48641cb0ef41Sopenharmony_ci// Prints: <Buffer 12 34 56 78 90 ab>
48651cb0ef41Sopenharmony_ci```
48661cb0ef41Sopenharmony_ci
48671cb0ef41Sopenharmony_ci```cjs
48681cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
48691cb0ef41Sopenharmony_ci
48701cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(6);
48711cb0ef41Sopenharmony_ci
48721cb0ef41Sopenharmony_cibuf.writeUIntBE(0x1234567890ab, 0, 6);
48731cb0ef41Sopenharmony_ci
48741cb0ef41Sopenharmony_ciconsole.log(buf);
48751cb0ef41Sopenharmony_ci// Prints: <Buffer 12 34 56 78 90 ab>
48761cb0ef41Sopenharmony_ci```
48771cb0ef41Sopenharmony_ci
48781cb0ef41Sopenharmony_ci### `buf.writeUIntLE(value, offset, byteLength)`
48791cb0ef41Sopenharmony_ci
48801cb0ef41Sopenharmony_ci<!-- YAML
48811cb0ef41Sopenharmony_ciadded: v0.5.5
48821cb0ef41Sopenharmony_cichanges:
48831cb0ef41Sopenharmony_ci  - version:
48841cb0ef41Sopenharmony_ci    - v14.9.0
48851cb0ef41Sopenharmony_ci    - v12.19.0
48861cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34729
48871cb0ef41Sopenharmony_ci    description: This function is also available as `buf.writeUintLE()`.
48881cb0ef41Sopenharmony_ci  - version: v10.0.0
48891cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18395
48901cb0ef41Sopenharmony_ci    description: Removed `noAssert` and no implicit coercion of the offset
48911cb0ef41Sopenharmony_ci                 and `byteLength` to `uint32` anymore.
48921cb0ef41Sopenharmony_ci-->
48931cb0ef41Sopenharmony_ci
48941cb0ef41Sopenharmony_ci* `value` {integer} Number to be written to `buf`.
48951cb0ef41Sopenharmony_ci* `offset` {integer} Number of bytes to skip before starting to write. Must
48961cb0ef41Sopenharmony_ci  satisfy `0 <= offset <= buf.length - byteLength`.
48971cb0ef41Sopenharmony_ci* `byteLength` {integer} Number of bytes to write. Must satisfy
48981cb0ef41Sopenharmony_ci  `0 < byteLength <= 6`.
48991cb0ef41Sopenharmony_ci* Returns: {integer} `offset` plus the number of bytes written.
49001cb0ef41Sopenharmony_ci
49011cb0ef41Sopenharmony_ciWrites `byteLength` bytes of `value` to `buf` at the specified `offset`
49021cb0ef41Sopenharmony_cias little-endian. Supports up to 48 bits of accuracy. Behavior is undefined
49031cb0ef41Sopenharmony_ciwhen `value` is anything other than an unsigned integer.
49041cb0ef41Sopenharmony_ci
49051cb0ef41Sopenharmony_ciThis function is also available under the `writeUintLE` alias.
49061cb0ef41Sopenharmony_ci
49071cb0ef41Sopenharmony_ci```mjs
49081cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer';
49091cb0ef41Sopenharmony_ci
49101cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(6);
49111cb0ef41Sopenharmony_ci
49121cb0ef41Sopenharmony_cibuf.writeUIntLE(0x1234567890ab, 0, 6);
49131cb0ef41Sopenharmony_ci
49141cb0ef41Sopenharmony_ciconsole.log(buf);
49151cb0ef41Sopenharmony_ci// Prints: <Buffer ab 90 78 56 34 12>
49161cb0ef41Sopenharmony_ci```
49171cb0ef41Sopenharmony_ci
49181cb0ef41Sopenharmony_ci```cjs
49191cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer');
49201cb0ef41Sopenharmony_ci
49211cb0ef41Sopenharmony_ciconst buf = Buffer.allocUnsafe(6);
49221cb0ef41Sopenharmony_ci
49231cb0ef41Sopenharmony_cibuf.writeUIntLE(0x1234567890ab, 0, 6);
49241cb0ef41Sopenharmony_ci
49251cb0ef41Sopenharmony_ciconsole.log(buf);
49261cb0ef41Sopenharmony_ci// Prints: <Buffer ab 90 78 56 34 12>
49271cb0ef41Sopenharmony_ci```
49281cb0ef41Sopenharmony_ci
49291cb0ef41Sopenharmony_ci### `new Buffer(array)`
49301cb0ef41Sopenharmony_ci
49311cb0ef41Sopenharmony_ci<!-- YAML
49321cb0ef41Sopenharmony_cideprecated: v6.0.0
49331cb0ef41Sopenharmony_cichanges:
49341cb0ef41Sopenharmony_ci  - version: v10.0.0
49351cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/19524
49361cb0ef41Sopenharmony_ci    description: Calling this constructor emits a deprecation warning when
49371cb0ef41Sopenharmony_ci                 run from code outside the `node_modules` directory.
49381cb0ef41Sopenharmony_ci  - version: v7.2.1
49391cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/9529
49401cb0ef41Sopenharmony_ci    description: Calling this constructor no longer emits a deprecation warning.
49411cb0ef41Sopenharmony_ci  - version: v7.0.0
49421cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/8169
49431cb0ef41Sopenharmony_ci    description: Calling this constructor emits a deprecation warning now.
49441cb0ef41Sopenharmony_ci-->
49451cb0ef41Sopenharmony_ci
49461cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use [`Buffer.from(array)`][] instead.
49471cb0ef41Sopenharmony_ci
49481cb0ef41Sopenharmony_ci* `array` {integer\[]} An array of bytes to copy from.
49491cb0ef41Sopenharmony_ci
49501cb0ef41Sopenharmony_ciSee [`Buffer.from(array)`][].
49511cb0ef41Sopenharmony_ci
49521cb0ef41Sopenharmony_ci### `new Buffer(arrayBuffer[, byteOffset[, length]])`
49531cb0ef41Sopenharmony_ci
49541cb0ef41Sopenharmony_ci<!-- YAML
49551cb0ef41Sopenharmony_ciadded: v3.0.0
49561cb0ef41Sopenharmony_cideprecated: v6.0.0
49571cb0ef41Sopenharmony_cichanges:
49581cb0ef41Sopenharmony_ci  - version: v10.0.0
49591cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/19524
49601cb0ef41Sopenharmony_ci    description: Calling this constructor emits a deprecation warning when
49611cb0ef41Sopenharmony_ci                 run from code outside the `node_modules` directory.
49621cb0ef41Sopenharmony_ci  - version: v7.2.1
49631cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/9529
49641cb0ef41Sopenharmony_ci    description: Calling this constructor no longer emits a deprecation warning.
49651cb0ef41Sopenharmony_ci  - version: v7.0.0
49661cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/8169
49671cb0ef41Sopenharmony_ci    description: Calling this constructor emits a deprecation warning now.
49681cb0ef41Sopenharmony_ci  - version: v6.0.0
49691cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/4682
49701cb0ef41Sopenharmony_ci    description: The `byteOffset` and `length` parameters are supported now.
49711cb0ef41Sopenharmony_ci-->
49721cb0ef41Sopenharmony_ci
49731cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use
49741cb0ef41Sopenharmony_ci> [`Buffer.from(arrayBuffer[, byteOffset[, length]])`][`Buffer.from(arrayBuf)`]
49751cb0ef41Sopenharmony_ci> instead.
49761cb0ef41Sopenharmony_ci
49771cb0ef41Sopenharmony_ci* `arrayBuffer` {ArrayBuffer|SharedArrayBuffer} An [`ArrayBuffer`][],
49781cb0ef41Sopenharmony_ci  [`SharedArrayBuffer`][] or the `.buffer` property of a [`TypedArray`][].
49791cb0ef41Sopenharmony_ci* `byteOffset` {integer} Index of first byte to expose. **Default:** `0`.
49801cb0ef41Sopenharmony_ci* `length` {integer} Number of bytes to expose.
49811cb0ef41Sopenharmony_ci  **Default:** `arrayBuffer.byteLength - byteOffset`.
49821cb0ef41Sopenharmony_ci
49831cb0ef41Sopenharmony_ciSee
49841cb0ef41Sopenharmony_ci[`Buffer.from(arrayBuffer[, byteOffset[, length]])`][`Buffer.from(arrayBuf)`].
49851cb0ef41Sopenharmony_ci
49861cb0ef41Sopenharmony_ci### `new Buffer(buffer)`
49871cb0ef41Sopenharmony_ci
49881cb0ef41Sopenharmony_ci<!-- YAML
49891cb0ef41Sopenharmony_cideprecated: v6.0.0
49901cb0ef41Sopenharmony_cichanges:
49911cb0ef41Sopenharmony_ci  - version: v10.0.0
49921cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/19524
49931cb0ef41Sopenharmony_ci    description: Calling this constructor emits a deprecation warning when
49941cb0ef41Sopenharmony_ci                 run from code outside the `node_modules` directory.
49951cb0ef41Sopenharmony_ci  - version: v7.2.1
49961cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/9529
49971cb0ef41Sopenharmony_ci    description: Calling this constructor no longer emits a deprecation warning.
49981cb0ef41Sopenharmony_ci  - version: v7.0.0
49991cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/8169
50001cb0ef41Sopenharmony_ci    description: Calling this constructor emits a deprecation warning now.
50011cb0ef41Sopenharmony_ci-->
50021cb0ef41Sopenharmony_ci
50031cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use [`Buffer.from(buffer)`][] instead.
50041cb0ef41Sopenharmony_ci
50051cb0ef41Sopenharmony_ci* `buffer` {Buffer|Uint8Array} An existing `Buffer` or [`Uint8Array`][] from
50061cb0ef41Sopenharmony_ci  which to copy data.
50071cb0ef41Sopenharmony_ci
50081cb0ef41Sopenharmony_ciSee [`Buffer.from(buffer)`][].
50091cb0ef41Sopenharmony_ci
50101cb0ef41Sopenharmony_ci### `new Buffer(size)`
50111cb0ef41Sopenharmony_ci
50121cb0ef41Sopenharmony_ci<!-- YAML
50131cb0ef41Sopenharmony_cideprecated: v6.0.0
50141cb0ef41Sopenharmony_cichanges:
50151cb0ef41Sopenharmony_ci  - version: v10.0.0
50161cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/19524
50171cb0ef41Sopenharmony_ci    description: Calling this constructor emits a deprecation warning when
50181cb0ef41Sopenharmony_ci                 run from code outside the `node_modules` directory.
50191cb0ef41Sopenharmony_ci  - version: v8.0.0
50201cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/12141
50211cb0ef41Sopenharmony_ci    description: The `new Buffer(size)` will return zero-filled memory by
50221cb0ef41Sopenharmony_ci                 default.
50231cb0ef41Sopenharmony_ci  - version: v7.2.1
50241cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/9529
50251cb0ef41Sopenharmony_ci    description: Calling this constructor no longer emits a deprecation warning.
50261cb0ef41Sopenharmony_ci  - version: v7.0.0
50271cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/8169
50281cb0ef41Sopenharmony_ci    description: Calling this constructor emits a deprecation warning now.
50291cb0ef41Sopenharmony_ci-->
50301cb0ef41Sopenharmony_ci
50311cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use [`Buffer.alloc()`][] instead (also see
50321cb0ef41Sopenharmony_ci> [`Buffer.allocUnsafe()`][]).
50331cb0ef41Sopenharmony_ci
50341cb0ef41Sopenharmony_ci* `size` {integer} The desired length of the new `Buffer`.
50351cb0ef41Sopenharmony_ci
50361cb0ef41Sopenharmony_ciSee [`Buffer.alloc()`][] and [`Buffer.allocUnsafe()`][]. This variant of the
50371cb0ef41Sopenharmony_ciconstructor is equivalent to [`Buffer.alloc()`][].
50381cb0ef41Sopenharmony_ci
50391cb0ef41Sopenharmony_ci### `new Buffer(string[, encoding])`
50401cb0ef41Sopenharmony_ci
50411cb0ef41Sopenharmony_ci<!-- YAML
50421cb0ef41Sopenharmony_cideprecated: v6.0.0
50431cb0ef41Sopenharmony_cichanges:
50441cb0ef41Sopenharmony_ci  - version: v10.0.0
50451cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/19524
50461cb0ef41Sopenharmony_ci    description: Calling this constructor emits a deprecation warning when
50471cb0ef41Sopenharmony_ci                 run from code outside the `node_modules` directory.
50481cb0ef41Sopenharmony_ci  - version: v7.2.1
50491cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/9529
50501cb0ef41Sopenharmony_ci    description: Calling this constructor no longer emits a deprecation warning.
50511cb0ef41Sopenharmony_ci  - version: v7.0.0
50521cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/8169
50531cb0ef41Sopenharmony_ci    description: Calling this constructor emits a deprecation warning now.
50541cb0ef41Sopenharmony_ci-->
50551cb0ef41Sopenharmony_ci
50561cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated:
50571cb0ef41Sopenharmony_ci> Use [`Buffer.from(string[, encoding])`][`Buffer.from(string)`] instead.
50581cb0ef41Sopenharmony_ci
50591cb0ef41Sopenharmony_ci* `string` {string} String to encode.
50601cb0ef41Sopenharmony_ci* `encoding` {string} The encoding of `string`. **Default:** `'utf8'`.
50611cb0ef41Sopenharmony_ci
50621cb0ef41Sopenharmony_ciSee [`Buffer.from(string[, encoding])`][`Buffer.from(string)`].
50631cb0ef41Sopenharmony_ci
50641cb0ef41Sopenharmony_ci## Class: `File`
50651cb0ef41Sopenharmony_ci
50661cb0ef41Sopenharmony_ci<!-- YAML
50671cb0ef41Sopenharmony_ciadded: v18.13.0
50681cb0ef41Sopenharmony_ci-->
50691cb0ef41Sopenharmony_ci
50701cb0ef41Sopenharmony_ci> Stability: 1 - Experimental
50711cb0ef41Sopenharmony_ci
50721cb0ef41Sopenharmony_ci* Extends: {Blob}
50731cb0ef41Sopenharmony_ci
50741cb0ef41Sopenharmony_ciA [`File`][] provides information about files.
50751cb0ef41Sopenharmony_ci
50761cb0ef41Sopenharmony_ci### `new buffer.File(sources, fileName[, options])`
50771cb0ef41Sopenharmony_ci
50781cb0ef41Sopenharmony_ci<!-- YAML
50791cb0ef41Sopenharmony_ciadded: v18.13.0
50801cb0ef41Sopenharmony_ci-->
50811cb0ef41Sopenharmony_ci
50821cb0ef41Sopenharmony_ci* `sources` {string\[]|ArrayBuffer\[]|TypedArray\[]|DataView\[]|Blob\[]|File\[]}
50831cb0ef41Sopenharmony_ci  An array of string, {ArrayBuffer}, {TypedArray}, {DataView}, {File}, or {Blob}
50841cb0ef41Sopenharmony_ci  objects, or any mix of such objects, that will be stored within the `File`.
50851cb0ef41Sopenharmony_ci* `fileName` {string} The name of the file.
50861cb0ef41Sopenharmony_ci* `options` {Object}
50871cb0ef41Sopenharmony_ci  * `endings` {string} One of either `'transparent'` or `'native'`. When set
50881cb0ef41Sopenharmony_ci    to `'native'`, line endings in string source parts will be converted to
50891cb0ef41Sopenharmony_ci    the platform native line-ending as specified by `require('node:os').EOL`.
50901cb0ef41Sopenharmony_ci  * `type` {string} The File content-type.
50911cb0ef41Sopenharmony_ci  * `lastModified` {number} The last modified date of the file.
50921cb0ef41Sopenharmony_ci    **Default:** `Date.now()`.
50931cb0ef41Sopenharmony_ci
50941cb0ef41Sopenharmony_ci### `file.name`
50951cb0ef41Sopenharmony_ci
50961cb0ef41Sopenharmony_ci<!-- YAML
50971cb0ef41Sopenharmony_ciadded: v18.13.0
50981cb0ef41Sopenharmony_ci-->
50991cb0ef41Sopenharmony_ci
51001cb0ef41Sopenharmony_ci* Type: {string}
51011cb0ef41Sopenharmony_ci
51021cb0ef41Sopenharmony_ciThe name of the `File`.
51031cb0ef41Sopenharmony_ci
51041cb0ef41Sopenharmony_ci### `file.lastModified`
51051cb0ef41Sopenharmony_ci
51061cb0ef41Sopenharmony_ci<!-- YAML
51071cb0ef41Sopenharmony_ciadded: v18.13.0
51081cb0ef41Sopenharmony_ci-->
51091cb0ef41Sopenharmony_ci
51101cb0ef41Sopenharmony_ci* Type: {number}
51111cb0ef41Sopenharmony_ci
51121cb0ef41Sopenharmony_ciThe last modified date of the `File`.
51131cb0ef41Sopenharmony_ci
51141cb0ef41Sopenharmony_ci## `node:buffer` module APIs
51151cb0ef41Sopenharmony_ci
51161cb0ef41Sopenharmony_ciWhile, the `Buffer` object is available as a global, there are additional
51171cb0ef41Sopenharmony_ci`Buffer`-related APIs that are available only via the `node:buffer` module
51181cb0ef41Sopenharmony_ciaccessed using `require('node:buffer')`.
51191cb0ef41Sopenharmony_ci
51201cb0ef41Sopenharmony_ci### `buffer.atob(data)`
51211cb0ef41Sopenharmony_ci
51221cb0ef41Sopenharmony_ci<!-- YAML
51231cb0ef41Sopenharmony_ciadded:
51241cb0ef41Sopenharmony_ci  - v15.13.0
51251cb0ef41Sopenharmony_ci  - v14.17.0
51261cb0ef41Sopenharmony_ci-->
51271cb0ef41Sopenharmony_ci
51281cb0ef41Sopenharmony_ci> Stability: 3 - Legacy. Use `Buffer.from(data, 'base64')` instead.
51291cb0ef41Sopenharmony_ci
51301cb0ef41Sopenharmony_ci* `data` {any} The Base64-encoded input string.
51311cb0ef41Sopenharmony_ci
51321cb0ef41Sopenharmony_ciDecodes a string of Base64-encoded data into bytes, and encodes those bytes
51331cb0ef41Sopenharmony_ciinto a string using Latin-1 (ISO-8859-1).
51341cb0ef41Sopenharmony_ci
51351cb0ef41Sopenharmony_ciThe `data` may be any JavaScript-value that can be coerced into a string.
51361cb0ef41Sopenharmony_ci
51371cb0ef41Sopenharmony_ci**This function is only provided for compatibility with legacy web platform APIs
51381cb0ef41Sopenharmony_ciand should never be used in new code, because they use strings to represent
51391cb0ef41Sopenharmony_cibinary data and predate the introduction of typed arrays in JavaScript.
51401cb0ef41Sopenharmony_ciFor code running using Node.js APIs, converting between base64-encoded strings
51411cb0ef41Sopenharmony_ciand binary data should be performed using `Buffer.from(str, 'base64')` and
51421cb0ef41Sopenharmony_ci`buf.toString('base64')`.**
51431cb0ef41Sopenharmony_ci
51441cb0ef41Sopenharmony_ci### `buffer.btoa(data)`
51451cb0ef41Sopenharmony_ci
51461cb0ef41Sopenharmony_ci<!-- YAML
51471cb0ef41Sopenharmony_ciadded:
51481cb0ef41Sopenharmony_ci  - v15.13.0
51491cb0ef41Sopenharmony_ci  - v14.17.0
51501cb0ef41Sopenharmony_ci-->
51511cb0ef41Sopenharmony_ci
51521cb0ef41Sopenharmony_ci> Stability: 3 - Legacy. Use `buf.toString('base64')` instead.
51531cb0ef41Sopenharmony_ci
51541cb0ef41Sopenharmony_ci* `data` {any} An ASCII (Latin1) string.
51551cb0ef41Sopenharmony_ci
51561cb0ef41Sopenharmony_ciDecodes a string into bytes using Latin-1 (ISO-8859), and encodes those bytes
51571cb0ef41Sopenharmony_ciinto a string using Base64.
51581cb0ef41Sopenharmony_ci
51591cb0ef41Sopenharmony_ciThe `data` may be any JavaScript-value that can be coerced into a string.
51601cb0ef41Sopenharmony_ci
51611cb0ef41Sopenharmony_ci**This function is only provided for compatibility with legacy web platform APIs
51621cb0ef41Sopenharmony_ciand should never be used in new code, because they use strings to represent
51631cb0ef41Sopenharmony_cibinary data and predate the introduction of typed arrays in JavaScript.
51641cb0ef41Sopenharmony_ciFor code running using Node.js APIs, converting between base64-encoded strings
51651cb0ef41Sopenharmony_ciand binary data should be performed using `Buffer.from(str, 'base64')` and
51661cb0ef41Sopenharmony_ci`buf.toString('base64')`.**
51671cb0ef41Sopenharmony_ci
51681cb0ef41Sopenharmony_ci### `buffer.isAscii(input)`
51691cb0ef41Sopenharmony_ci
51701cb0ef41Sopenharmony_ci<!-- YAML
51711cb0ef41Sopenharmony_ciadded: v18.15.0
51721cb0ef41Sopenharmony_ci-->
51731cb0ef41Sopenharmony_ci
51741cb0ef41Sopenharmony_ci* input {Buffer | ArrayBuffer | TypedArray} The input to validate.
51751cb0ef41Sopenharmony_ci* Returns: {boolean}
51761cb0ef41Sopenharmony_ci
51771cb0ef41Sopenharmony_ciThis function returns `true` if `input` contains only valid ASCII-encoded data,
51781cb0ef41Sopenharmony_ciincluding the case in which `input` is empty.
51791cb0ef41Sopenharmony_ci
51801cb0ef41Sopenharmony_ciThrows if the `input` is a detached array buffer.
51811cb0ef41Sopenharmony_ci
51821cb0ef41Sopenharmony_ci### `buffer.isUtf8(input)`
51831cb0ef41Sopenharmony_ci
51841cb0ef41Sopenharmony_ci<!-- YAML
51851cb0ef41Sopenharmony_ciadded: v18.14.0
51861cb0ef41Sopenharmony_ci-->
51871cb0ef41Sopenharmony_ci
51881cb0ef41Sopenharmony_ci* input {Buffer | ArrayBuffer | TypedArray} The input to validate.
51891cb0ef41Sopenharmony_ci* Returns: {boolean}
51901cb0ef41Sopenharmony_ci
51911cb0ef41Sopenharmony_ciThis function returns `true` if `input` contains only valid UTF-8-encoded data,
51921cb0ef41Sopenharmony_ciincluding the case in which `input` is empty.
51931cb0ef41Sopenharmony_ci
51941cb0ef41Sopenharmony_ciThrows if the `input` is a detached array buffer.
51951cb0ef41Sopenharmony_ci
51961cb0ef41Sopenharmony_ci### `buffer.INSPECT_MAX_BYTES`
51971cb0ef41Sopenharmony_ci
51981cb0ef41Sopenharmony_ci<!-- YAML
51991cb0ef41Sopenharmony_ciadded: v0.5.4
52001cb0ef41Sopenharmony_ci-->
52011cb0ef41Sopenharmony_ci
52021cb0ef41Sopenharmony_ci* {integer} **Default:** `50`
52031cb0ef41Sopenharmony_ci
52041cb0ef41Sopenharmony_ciReturns the maximum number of bytes that will be returned when
52051cb0ef41Sopenharmony_ci`buf.inspect()` is called. This can be overridden by user modules. See
52061cb0ef41Sopenharmony_ci[`util.inspect()`][] for more details on `buf.inspect()` behavior.
52071cb0ef41Sopenharmony_ci
52081cb0ef41Sopenharmony_ci### `buffer.kMaxLength`
52091cb0ef41Sopenharmony_ci
52101cb0ef41Sopenharmony_ci<!-- YAML
52111cb0ef41Sopenharmony_ciadded: v3.0.0
52121cb0ef41Sopenharmony_ci-->
52131cb0ef41Sopenharmony_ci
52141cb0ef41Sopenharmony_ci* {integer} The largest size allowed for a single `Buffer` instance.
52151cb0ef41Sopenharmony_ci
52161cb0ef41Sopenharmony_ciAn alias for [`buffer.constants.MAX_LENGTH`][].
52171cb0ef41Sopenharmony_ci
52181cb0ef41Sopenharmony_ci### `buffer.kStringMaxLength`
52191cb0ef41Sopenharmony_ci
52201cb0ef41Sopenharmony_ci<!-- YAML
52211cb0ef41Sopenharmony_ciadded: v3.0.0
52221cb0ef41Sopenharmony_ci-->
52231cb0ef41Sopenharmony_ci
52241cb0ef41Sopenharmony_ci* {integer} The largest length allowed for a single `string` instance.
52251cb0ef41Sopenharmony_ci
52261cb0ef41Sopenharmony_ciAn alias for [`buffer.constants.MAX_STRING_LENGTH`][].
52271cb0ef41Sopenharmony_ci
52281cb0ef41Sopenharmony_ci### `buffer.resolveObjectURL(id)`
52291cb0ef41Sopenharmony_ci
52301cb0ef41Sopenharmony_ci<!-- YAML
52311cb0ef41Sopenharmony_ciadded: v16.7.0
52321cb0ef41Sopenharmony_ci-->
52331cb0ef41Sopenharmony_ci
52341cb0ef41Sopenharmony_ci> Stability: 1 - Experimental
52351cb0ef41Sopenharmony_ci
52361cb0ef41Sopenharmony_ci* `id` {string} A `'blob:nodedata:...` URL string returned by a prior call to
52371cb0ef41Sopenharmony_ci  `URL.createObjectURL()`.
52381cb0ef41Sopenharmony_ci* Returns: {Blob}
52391cb0ef41Sopenharmony_ci
52401cb0ef41Sopenharmony_ciResolves a `'blob:nodedata:...'` an associated {Blob} object registered using
52411cb0ef41Sopenharmony_cia prior call to `URL.createObjectURL()`.
52421cb0ef41Sopenharmony_ci
52431cb0ef41Sopenharmony_ci### `buffer.transcode(source, fromEnc, toEnc)`
52441cb0ef41Sopenharmony_ci
52451cb0ef41Sopenharmony_ci<!-- YAML
52461cb0ef41Sopenharmony_ciadded: v7.1.0
52471cb0ef41Sopenharmony_cichanges:
52481cb0ef41Sopenharmony_ci  - version: v8.0.0
52491cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/10236
52501cb0ef41Sopenharmony_ci    description: The `source` parameter can now be a `Uint8Array`.
52511cb0ef41Sopenharmony_ci-->
52521cb0ef41Sopenharmony_ci
52531cb0ef41Sopenharmony_ci* `source` {Buffer|Uint8Array} A `Buffer` or `Uint8Array` instance.
52541cb0ef41Sopenharmony_ci* `fromEnc` {string} The current encoding.
52551cb0ef41Sopenharmony_ci* `toEnc` {string} To target encoding.
52561cb0ef41Sopenharmony_ci* Returns: {Buffer}
52571cb0ef41Sopenharmony_ci
52581cb0ef41Sopenharmony_ciRe-encodes the given `Buffer` or `Uint8Array` instance from one character
52591cb0ef41Sopenharmony_ciencoding to another. Returns a new `Buffer` instance.
52601cb0ef41Sopenharmony_ci
52611cb0ef41Sopenharmony_ciThrows if the `fromEnc` or `toEnc` specify invalid character encodings or if
52621cb0ef41Sopenharmony_ciconversion from `fromEnc` to `toEnc` is not permitted.
52631cb0ef41Sopenharmony_ci
52641cb0ef41Sopenharmony_ciEncodings supported by `buffer.transcode()` are: `'ascii'`, `'utf8'`,
52651cb0ef41Sopenharmony_ci`'utf16le'`, `'ucs2'`, `'latin1'`, and `'binary'`.
52661cb0ef41Sopenharmony_ci
52671cb0ef41Sopenharmony_ciThe transcoding process will use substitution characters if a given byte
52681cb0ef41Sopenharmony_cisequence cannot be adequately represented in the target encoding. For instance:
52691cb0ef41Sopenharmony_ci
52701cb0ef41Sopenharmony_ci```mjs
52711cb0ef41Sopenharmony_ciimport { Buffer, transcode } from 'node:buffer';
52721cb0ef41Sopenharmony_ci
52731cb0ef41Sopenharmony_ciconst newBuf = transcode(Buffer.from('€'), 'utf8', 'ascii');
52741cb0ef41Sopenharmony_ciconsole.log(newBuf.toString('ascii'));
52751cb0ef41Sopenharmony_ci// Prints: '?'
52761cb0ef41Sopenharmony_ci```
52771cb0ef41Sopenharmony_ci
52781cb0ef41Sopenharmony_ci```cjs
52791cb0ef41Sopenharmony_ciconst { Buffer, transcode } = require('node:buffer');
52801cb0ef41Sopenharmony_ci
52811cb0ef41Sopenharmony_ciconst newBuf = transcode(Buffer.from('€'), 'utf8', 'ascii');
52821cb0ef41Sopenharmony_ciconsole.log(newBuf.toString('ascii'));
52831cb0ef41Sopenharmony_ci// Prints: '?'
52841cb0ef41Sopenharmony_ci```
52851cb0ef41Sopenharmony_ci
52861cb0ef41Sopenharmony_ciBecause the Euro (`€`) sign is not representable in US-ASCII, it is replaced
52871cb0ef41Sopenharmony_ciwith `?` in the transcoded `Buffer`.
52881cb0ef41Sopenharmony_ci
52891cb0ef41Sopenharmony_ci### Class: `SlowBuffer`
52901cb0ef41Sopenharmony_ci
52911cb0ef41Sopenharmony_ci<!-- YAML
52921cb0ef41Sopenharmony_cideprecated: v6.0.0
52931cb0ef41Sopenharmony_ci-->
52941cb0ef41Sopenharmony_ci
52951cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use [`Buffer.allocUnsafeSlow()`][] instead.
52961cb0ef41Sopenharmony_ci
52971cb0ef41Sopenharmony_ciSee [`Buffer.allocUnsafeSlow()`][]. This was never a class in the sense that
52981cb0ef41Sopenharmony_cithe constructor always returned a `Buffer` instance, rather than a `SlowBuffer`
52991cb0ef41Sopenharmony_ciinstance.
53001cb0ef41Sopenharmony_ci
53011cb0ef41Sopenharmony_ci#### `new SlowBuffer(size)`
53021cb0ef41Sopenharmony_ci
53031cb0ef41Sopenharmony_ci<!-- YAML
53041cb0ef41Sopenharmony_cideprecated: v6.0.0
53051cb0ef41Sopenharmony_ci-->
53061cb0ef41Sopenharmony_ci
53071cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use [`Buffer.allocUnsafeSlow()`][] instead.
53081cb0ef41Sopenharmony_ci
53091cb0ef41Sopenharmony_ci* `size` {integer} The desired length of the new `SlowBuffer`.
53101cb0ef41Sopenharmony_ci
53111cb0ef41Sopenharmony_ciSee [`Buffer.allocUnsafeSlow()`][].
53121cb0ef41Sopenharmony_ci
53131cb0ef41Sopenharmony_ci### Buffer constants
53141cb0ef41Sopenharmony_ci
53151cb0ef41Sopenharmony_ci<!-- YAML
53161cb0ef41Sopenharmony_ciadded: v8.2.0
53171cb0ef41Sopenharmony_ci-->
53181cb0ef41Sopenharmony_ci
53191cb0ef41Sopenharmony_ci#### `buffer.constants.MAX_LENGTH`
53201cb0ef41Sopenharmony_ci
53211cb0ef41Sopenharmony_ci<!-- YAML
53221cb0ef41Sopenharmony_ciadded: v8.2.0
53231cb0ef41Sopenharmony_cichanges:
53241cb0ef41Sopenharmony_ci  - version: v15.0.0
53251cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/35415
53261cb0ef41Sopenharmony_ci    description: Value is changed to 2<sup>32</sup> on 64-bit
53271cb0ef41Sopenharmony_ci      architectures.
53281cb0ef41Sopenharmony_ci  - version: v14.0.0
53291cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/32116
53301cb0ef41Sopenharmony_ci    description: Value is changed from 2<sup>31</sup> - 1 to
53311cb0ef41Sopenharmony_ci      2<sup>32</sup> - 1 on 64-bit architectures.
53321cb0ef41Sopenharmony_ci-->
53331cb0ef41Sopenharmony_ci
53341cb0ef41Sopenharmony_ci* {integer} The largest size allowed for a single `Buffer` instance.
53351cb0ef41Sopenharmony_ci
53361cb0ef41Sopenharmony_ciOn 32-bit architectures, this value currently is 2<sup>30</sup> - 1 (about 1
53371cb0ef41Sopenharmony_ciGiB).
53381cb0ef41Sopenharmony_ci
53391cb0ef41Sopenharmony_ciOn 64-bit architectures, this value currently is 2<sup>32</sup> (about 4 GiB).
53401cb0ef41Sopenharmony_ci
53411cb0ef41Sopenharmony_ciIt reflects [`v8::TypedArray::kMaxLength`][] under the hood.
53421cb0ef41Sopenharmony_ci
53431cb0ef41Sopenharmony_ciThis value is also available as [`buffer.kMaxLength`][].
53441cb0ef41Sopenharmony_ci
53451cb0ef41Sopenharmony_ci#### `buffer.constants.MAX_STRING_LENGTH`
53461cb0ef41Sopenharmony_ci
53471cb0ef41Sopenharmony_ci<!-- YAML
53481cb0ef41Sopenharmony_ciadded: v8.2.0
53491cb0ef41Sopenharmony_ci-->
53501cb0ef41Sopenharmony_ci
53511cb0ef41Sopenharmony_ci* {integer} The largest length allowed for a single `string` instance.
53521cb0ef41Sopenharmony_ci
53531cb0ef41Sopenharmony_ciRepresents the largest `length` that a `string` primitive can have, counted
53541cb0ef41Sopenharmony_ciin UTF-16 code units.
53551cb0ef41Sopenharmony_ci
53561cb0ef41Sopenharmony_ciThis value may depend on the JS engine that is being used.
53571cb0ef41Sopenharmony_ci
53581cb0ef41Sopenharmony_ci## `Buffer.from()`, `Buffer.alloc()`, and `Buffer.allocUnsafe()`
53591cb0ef41Sopenharmony_ci
53601cb0ef41Sopenharmony_ciIn versions of Node.js prior to 6.0.0, `Buffer` instances were created using the
53611cb0ef41Sopenharmony_ci`Buffer` constructor function, which allocates the returned `Buffer`
53621cb0ef41Sopenharmony_cidifferently based on what arguments are provided:
53631cb0ef41Sopenharmony_ci
53641cb0ef41Sopenharmony_ci* Passing a number as the first argument to `Buffer()` (e.g. `new Buffer(10)`)
53651cb0ef41Sopenharmony_ci  allocates a new `Buffer` object of the specified size. Prior to Node.js 8.0.0,
53661cb0ef41Sopenharmony_ci  the memory allocated for such `Buffer` instances is _not_ initialized and
53671cb0ef41Sopenharmony_ci  _can contain sensitive data_. Such `Buffer` instances _must_ be subsequently
53681cb0ef41Sopenharmony_ci  initialized by using either [`buf.fill(0)`][`buf.fill()`] or by writing to the
53691cb0ef41Sopenharmony_ci  entire `Buffer` before reading data from the `Buffer`.
53701cb0ef41Sopenharmony_ci  While this behavior is _intentional_ to improve performance,
53711cb0ef41Sopenharmony_ci  development experience has demonstrated that a more explicit distinction is
53721cb0ef41Sopenharmony_ci  required between creating a fast-but-uninitialized `Buffer` versus creating a
53731cb0ef41Sopenharmony_ci  slower-but-safer `Buffer`. Since Node.js 8.0.0, `Buffer(num)` and `new
53741cb0ef41Sopenharmony_ci  Buffer(num)` return a `Buffer` with initialized memory.
53751cb0ef41Sopenharmony_ci* Passing a string, array, or `Buffer` as the first argument copies the
53761cb0ef41Sopenharmony_ci  passed object's data into the `Buffer`.
53771cb0ef41Sopenharmony_ci* Passing an [`ArrayBuffer`][] or a [`SharedArrayBuffer`][] returns a `Buffer`
53781cb0ef41Sopenharmony_ci  that shares allocated memory with the given array buffer.
53791cb0ef41Sopenharmony_ci
53801cb0ef41Sopenharmony_ciBecause the behavior of `new Buffer()` is different depending on the type of the
53811cb0ef41Sopenharmony_cifirst argument, security and reliability issues can be inadvertently introduced
53821cb0ef41Sopenharmony_ciinto applications when argument validation or `Buffer` initialization is not
53831cb0ef41Sopenharmony_ciperformed.
53841cb0ef41Sopenharmony_ci
53851cb0ef41Sopenharmony_ciFor example, if an attacker can cause an application to receive a number where
53861cb0ef41Sopenharmony_cia string is expected, the application may call `new Buffer(100)`
53871cb0ef41Sopenharmony_ciinstead of `new Buffer("100")`, leading it to allocate a 100 byte buffer instead
53881cb0ef41Sopenharmony_ciof allocating a 3 byte buffer with content `"100"`. This is commonly possible
53891cb0ef41Sopenharmony_ciusing JSON API calls. Since JSON distinguishes between numeric and string types,
53901cb0ef41Sopenharmony_ciit allows injection of numbers where a naively written application that does not
53911cb0ef41Sopenharmony_civalidate its input sufficiently might expect to always receive a string.
53921cb0ef41Sopenharmony_ciBefore Node.js 8.0.0, the 100 byte buffer might contain
53931cb0ef41Sopenharmony_ciarbitrary pre-existing in-memory data, so may be used to expose in-memory
53941cb0ef41Sopenharmony_cisecrets to a remote attacker. Since Node.js 8.0.0, exposure of memory cannot
53951cb0ef41Sopenharmony_cioccur because the data is zero-filled. However, other attacks are still
53961cb0ef41Sopenharmony_cipossible, such as causing very large buffers to be allocated by the server,
53971cb0ef41Sopenharmony_cileading to performance degradation or crashing on memory exhaustion.
53981cb0ef41Sopenharmony_ci
53991cb0ef41Sopenharmony_ciTo make the creation of `Buffer` instances more reliable and less error-prone,
54001cb0ef41Sopenharmony_cithe various forms of the `new Buffer()` constructor have been **deprecated**
54011cb0ef41Sopenharmony_ciand replaced by separate `Buffer.from()`, [`Buffer.alloc()`][], and
54021cb0ef41Sopenharmony_ci[`Buffer.allocUnsafe()`][] methods.
54031cb0ef41Sopenharmony_ci
54041cb0ef41Sopenharmony_ci_Developers should migrate all existing uses of the `new Buffer()` constructors
54051cb0ef41Sopenharmony_cito one of these new APIs._
54061cb0ef41Sopenharmony_ci
54071cb0ef41Sopenharmony_ci* [`Buffer.from(array)`][] returns a new `Buffer` that _contains a copy_ of the
54081cb0ef41Sopenharmony_ci  provided octets.
54091cb0ef41Sopenharmony_ci* [`Buffer.from(arrayBuffer[, byteOffset[, length]])`][`Buffer.from(arrayBuf)`]
54101cb0ef41Sopenharmony_ci  returns a new `Buffer` that _shares the same allocated memory_ as the given
54111cb0ef41Sopenharmony_ci  [`ArrayBuffer`][].
54121cb0ef41Sopenharmony_ci* [`Buffer.from(buffer)`][] returns a new `Buffer` that _contains a copy_ of the
54131cb0ef41Sopenharmony_ci  contents of the given `Buffer`.
54141cb0ef41Sopenharmony_ci* [`Buffer.from(string[, encoding])`][`Buffer.from(string)`] returns a new
54151cb0ef41Sopenharmony_ci  `Buffer` that _contains a copy_ of the provided string.
54161cb0ef41Sopenharmony_ci* [`Buffer.alloc(size[, fill[, encoding]])`][`Buffer.alloc()`] returns a new
54171cb0ef41Sopenharmony_ci  initialized `Buffer` of the specified size. This method is slower than
54181cb0ef41Sopenharmony_ci  [`Buffer.allocUnsafe(size)`][`Buffer.allocUnsafe()`] but guarantees that newly
54191cb0ef41Sopenharmony_ci  created `Buffer` instances never contain old data that is potentially
54201cb0ef41Sopenharmony_ci  sensitive. A `TypeError` will be thrown if `size` is not a number.
54211cb0ef41Sopenharmony_ci* [`Buffer.allocUnsafe(size)`][`Buffer.allocUnsafe()`] and
54221cb0ef41Sopenharmony_ci  [`Buffer.allocUnsafeSlow(size)`][`Buffer.allocUnsafeSlow()`] each return a
54231cb0ef41Sopenharmony_ci  new uninitialized `Buffer` of the specified `size`. Because the `Buffer` is
54241cb0ef41Sopenharmony_ci  uninitialized, the allocated segment of memory might contain old data that is
54251cb0ef41Sopenharmony_ci  potentially sensitive.
54261cb0ef41Sopenharmony_ci
54271cb0ef41Sopenharmony_ci`Buffer` instances returned by [`Buffer.allocUnsafe()`][] and
54281cb0ef41Sopenharmony_ci[`Buffer.from(array)`][] _may_ be allocated off a shared internal memory pool
54291cb0ef41Sopenharmony_ciif `size` is less than or equal to half [`Buffer.poolSize`][]. Instances
54301cb0ef41Sopenharmony_cireturned by [`Buffer.allocUnsafeSlow()`][] _never_ use the shared internal
54311cb0ef41Sopenharmony_cimemory pool.
54321cb0ef41Sopenharmony_ci
54331cb0ef41Sopenharmony_ci### The `--zero-fill-buffers` command-line option
54341cb0ef41Sopenharmony_ci
54351cb0ef41Sopenharmony_ci<!-- YAML
54361cb0ef41Sopenharmony_ciadded: v5.10.0
54371cb0ef41Sopenharmony_ci-->
54381cb0ef41Sopenharmony_ci
54391cb0ef41Sopenharmony_ciNode.js can be started using the `--zero-fill-buffers` command-line option to
54401cb0ef41Sopenharmony_cicause all newly-allocated `Buffer` instances to be zero-filled upon creation by
54411cb0ef41Sopenharmony_cidefault. Without the option, buffers created with [`Buffer.allocUnsafe()`][],
54421cb0ef41Sopenharmony_ci[`Buffer.allocUnsafeSlow()`][], and `new SlowBuffer(size)` are not zero-filled.
54431cb0ef41Sopenharmony_ciUse of this flag can have a measurable negative impact on performance. Use the
54441cb0ef41Sopenharmony_ci`--zero-fill-buffers` option only when necessary to enforce that newly allocated
54451cb0ef41Sopenharmony_ci`Buffer` instances cannot contain old data that is potentially sensitive.
54461cb0ef41Sopenharmony_ci
54471cb0ef41Sopenharmony_ci```console
54481cb0ef41Sopenharmony_ci$ node --zero-fill-buffers
54491cb0ef41Sopenharmony_ci> Buffer.allocUnsafe(5);
54501cb0ef41Sopenharmony_ci<Buffer 00 00 00 00 00>
54511cb0ef41Sopenharmony_ci```
54521cb0ef41Sopenharmony_ci
54531cb0ef41Sopenharmony_ci### What makes `Buffer.allocUnsafe()` and `Buffer.allocUnsafeSlow()` "unsafe"?
54541cb0ef41Sopenharmony_ci
54551cb0ef41Sopenharmony_ciWhen calling [`Buffer.allocUnsafe()`][] and [`Buffer.allocUnsafeSlow()`][], the
54561cb0ef41Sopenharmony_cisegment of allocated memory is _uninitialized_ (it is not zeroed-out). While
54571cb0ef41Sopenharmony_cithis design makes the allocation of memory quite fast, the allocated segment of
54581cb0ef41Sopenharmony_cimemory might contain old data that is potentially sensitive. Using a `Buffer`
54591cb0ef41Sopenharmony_cicreated by [`Buffer.allocUnsafe()`][] without _completely_ overwriting the
54601cb0ef41Sopenharmony_cimemory can allow this old data to be leaked when the `Buffer` memory is read.
54611cb0ef41Sopenharmony_ci
54621cb0ef41Sopenharmony_ciWhile there are clear performance advantages to using
54631cb0ef41Sopenharmony_ci[`Buffer.allocUnsafe()`][], extra care _must_ be taken in order to avoid
54641cb0ef41Sopenharmony_ciintroducing security vulnerabilities into an application.
54651cb0ef41Sopenharmony_ci
54661cb0ef41Sopenharmony_ci[ASCII]: https://en.wikipedia.org/wiki/ASCII
54671cb0ef41Sopenharmony_ci[Base64]: https://en.wikipedia.org/wiki/Base64
54681cb0ef41Sopenharmony_ci[ISO-8859-1]: https://en.wikipedia.org/wiki/ISO-8859-1
54691cb0ef41Sopenharmony_ci[RFC 4648, Section 5]: https://tools.ietf.org/html/rfc4648#section-5
54701cb0ef41Sopenharmony_ci[UTF-16]: https://en.wikipedia.org/wiki/UTF-16
54711cb0ef41Sopenharmony_ci[UTF-8]: https://en.wikipedia.org/wiki/UTF-8
54721cb0ef41Sopenharmony_ci[WHATWG Encoding Standard]: https://encoding.spec.whatwg.org/
54731cb0ef41Sopenharmony_ci[`ArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
54741cb0ef41Sopenharmony_ci[`Blob`]: https://developer.mozilla.org/en-US/docs/Web/API/Blob
54751cb0ef41Sopenharmony_ci[`Buffer.alloc()`]: #static-method-bufferallocsize-fill-encoding
54761cb0ef41Sopenharmony_ci[`Buffer.allocUnsafe()`]: #static-method-bufferallocunsafesize
54771cb0ef41Sopenharmony_ci[`Buffer.allocUnsafeSlow()`]: #static-method-bufferallocunsafeslowsize
54781cb0ef41Sopenharmony_ci[`Buffer.concat()`]: #static-method-bufferconcatlist-totallength
54791cb0ef41Sopenharmony_ci[`Buffer.copyBytesFrom()`]: #static-method-buffercopybytesfromview-offset-length
54801cb0ef41Sopenharmony_ci[`Buffer.from(array)`]: #static-method-bufferfromarray
54811cb0ef41Sopenharmony_ci[`Buffer.from(arrayBuf)`]: #static-method-bufferfromarraybuffer-byteoffset-length
54821cb0ef41Sopenharmony_ci[`Buffer.from(buffer)`]: #static-method-bufferfrombuffer
54831cb0ef41Sopenharmony_ci[`Buffer.from(string)`]: #static-method-bufferfromstring-encoding
54841cb0ef41Sopenharmony_ci[`Buffer.poolSize`]: #class-property-bufferpoolsize
54851cb0ef41Sopenharmony_ci[`DataView`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView
54861cb0ef41Sopenharmony_ci[`ERR_INVALID_BUFFER_SIZE`]: errors.md#err_invalid_buffer_size
54871cb0ef41Sopenharmony_ci[`ERR_OUT_OF_RANGE`]: errors.md#err_out_of_range
54881cb0ef41Sopenharmony_ci[`File`]: https://developer.mozilla.org/en-US/docs/Web/API/File
54891cb0ef41Sopenharmony_ci[`JSON.stringify()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
54901cb0ef41Sopenharmony_ci[`SharedArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer
54911cb0ef41Sopenharmony_ci[`String.prototype.indexOf()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf
54921cb0ef41Sopenharmony_ci[`String.prototype.lastIndexOf()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf
54931cb0ef41Sopenharmony_ci[`String.prototype.length`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length
54941cb0ef41Sopenharmony_ci[`TypedArray.from()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from
54951cb0ef41Sopenharmony_ci[`TypedArray.prototype.set()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/set
54961cb0ef41Sopenharmony_ci[`TypedArray.prototype.slice()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/slice
54971cb0ef41Sopenharmony_ci[`TypedArray.prototype.subarray()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/subarray
54981cb0ef41Sopenharmony_ci[`TypedArray`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
54991cb0ef41Sopenharmony_ci[`Uint8Array`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array
55001cb0ef41Sopenharmony_ci[`buf.buffer`]: #bufbuffer
55011cb0ef41Sopenharmony_ci[`buf.compare()`]: #bufcomparetarget-targetstart-targetend-sourcestart-sourceend
55021cb0ef41Sopenharmony_ci[`buf.entries()`]: #bufentries
55031cb0ef41Sopenharmony_ci[`buf.fill()`]: #buffillvalue-offset-end-encoding
55041cb0ef41Sopenharmony_ci[`buf.indexOf()`]: #bufindexofvalue-byteoffset-encoding
55051cb0ef41Sopenharmony_ci[`buf.keys()`]: #bufkeys
55061cb0ef41Sopenharmony_ci[`buf.length`]: #buflength
55071cb0ef41Sopenharmony_ci[`buf.slice()`]: #bufslicestart-end
55081cb0ef41Sopenharmony_ci[`buf.subarray`]: #bufsubarraystart-end
55091cb0ef41Sopenharmony_ci[`buf.toString()`]: #buftostringencoding-start-end
55101cb0ef41Sopenharmony_ci[`buf.values()`]: #bufvalues
55111cb0ef41Sopenharmony_ci[`buffer.constants.MAX_LENGTH`]: #bufferconstantsmax_length
55121cb0ef41Sopenharmony_ci[`buffer.constants.MAX_STRING_LENGTH`]: #bufferconstantsmax_string_length
55131cb0ef41Sopenharmony_ci[`buffer.kMaxLength`]: #bufferkmaxlength
55141cb0ef41Sopenharmony_ci[`util.inspect()`]: util.md#utilinspectobject-options
55151cb0ef41Sopenharmony_ci[`v8::TypedArray::kMaxLength`]: https://v8.github.io/api/head/classv8_1_1TypedArray.html#a54a48f4373da0850663c4393d843b9b0
55161cb0ef41Sopenharmony_ci[base64url]: https://tools.ietf.org/html/rfc4648#section-5
55171cb0ef41Sopenharmony_ci[endianness]: https://en.wikipedia.org/wiki/Endianness
55181cb0ef41Sopenharmony_ci[iterator]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
5519