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