{ "type": "module", "source": "doc/api/buffer.md", "modules": [ { "textRaw": "Buffer", "name": "buffer", "introduced_in": "v0.1.90", "stability": 2, "stabilityText": "Stable", "desc": "

Source Code: lib/buffer.js

\n

Buffer objects are used to represent a fixed-length sequence of bytes. Many\nNode.js APIs support Buffers.

\n

The Buffer class is a subclass of JavaScript's Uint8Array class and\nextends it with methods that cover additional use cases. Node.js APIs accept\nplain Uint8Arrays wherever Buffers are supported as well.

\n

While the Buffer class is available within the global scope, it is still\nrecommended to explicitly reference it via an import or require statement.

\n
import { Buffer } from 'buffer';\n\n// Creates a zero-filled Buffer of length 10.\nconst buf1 = Buffer.alloc(10);\n\n// Creates a Buffer of length 10,\n// filled with bytes which all have the value `1`.\nconst buf2 = Buffer.alloc(10, 1);\n\n// Creates an uninitialized buffer of length 10.\n// This is faster than calling Buffer.alloc() but the returned\n// Buffer instance might contain old data that needs to be\n// overwritten using fill(), write(), or other functions that fill the Buffer's\n// contents.\nconst buf3 = Buffer.allocUnsafe(10);\n\n// Creates a Buffer containing the bytes [1, 2, 3].\nconst buf4 = Buffer.from([1, 2, 3]);\n\n// Creates a Buffer containing the bytes [1, 1, 1, 1] – the entries\n// are all truncated using `(value & 255)` to fit into the range 0–255.\nconst buf5 = Buffer.from([257, 257.5, -255, '1']);\n\n// Creates a Buffer containing the UTF-8-encoded bytes for the string 'tést':\n// [0x74, 0xc3, 0xa9, 0x73, 0x74] (in hexadecimal notation)\n// [116, 195, 169, 115, 116] (in decimal notation)\nconst buf6 = Buffer.from('tést');\n\n// Creates a Buffer containing the Latin-1 bytes [0x74, 0xe9, 0x73, 0x74].\nconst buf7 = Buffer.from('tést', 'latin1');\n
\n
const { Buffer } = require('buffer');\n\n// Creates a zero-filled Buffer of length 10.\nconst buf1 = Buffer.alloc(10);\n\n// Creates a Buffer of length 10,\n// filled with bytes which all have the value `1`.\nconst buf2 = Buffer.alloc(10, 1);\n\n// Creates an uninitialized buffer of length 10.\n// This is faster than calling Buffer.alloc() but the returned\n// Buffer instance might contain old data that needs to be\n// overwritten using fill(), write(), or other functions that fill the Buffer's\n// contents.\nconst buf3 = Buffer.allocUnsafe(10);\n\n// Creates a Buffer containing the bytes [1, 2, 3].\nconst buf4 = Buffer.from([1, 2, 3]);\n\n// Creates a Buffer containing the bytes [1, 1, 1, 1] – the entries\n// are all truncated using `(value & 255)` to fit into the range 0–255.\nconst buf5 = Buffer.from([257, 257.5, -255, '1']);\n\n// Creates a Buffer containing the UTF-8-encoded bytes for the string 'tést':\n// [0x74, 0xc3, 0xa9, 0x73, 0x74] (in hexadecimal notation)\n// [116, 195, 169, 115, 116] (in decimal notation)\nconst buf6 = Buffer.from('tést');\n\n// Creates a Buffer containing the Latin-1 bytes [0x74, 0xe9, 0x73, 0x74].\nconst buf7 = Buffer.from('tést', 'latin1');\n
", "modules": [ { "textRaw": "Buffers and character encodings", "name": "buffers_and_character_encodings", "meta": { "changes": [ { "version": "v15.7.0", "pr-url": "https://github.com/nodejs/node/pull/36952", "description": "Introduced `base64url` encoding." }, { "version": "v6.4.0", "pr-url": "https://github.com/nodejs/node/pull/7111", "description": "Introduced `latin1` as an alias for `binary`." }, { "version": "v5.0.0", "pr-url": "https://github.com/nodejs/node/pull/2859", "description": "Removed the deprecated `raw` and `raws` encodings." } ] }, "desc": "

When converting between Buffers and strings, a character encoding may be\nspecified. If no character encoding is specified, UTF-8 will be used as the\ndefault.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from('hello world', 'utf8');\n\nconsole.log(buf.toString('hex'));\n// Prints: 68656c6c6f20776f726c64\nconsole.log(buf.toString('base64'));\n// Prints: aGVsbG8gd29ybGQ=\n\nconsole.log(Buffer.from('fhqwhgads', 'utf8'));\n// Prints: <Buffer 66 68 71 77 68 67 61 64 73>\nconsole.log(Buffer.from('fhqwhgads', 'utf16le'));\n// Prints: <Buffer 66 00 68 00 71 00 77 00 68 00 67 00 61 00 64 00 73 00>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from('hello world', 'utf8');\n\nconsole.log(buf.toString('hex'));\n// Prints: 68656c6c6f20776f726c64\nconsole.log(buf.toString('base64'));\n// Prints: aGVsbG8gd29ybGQ=\n\nconsole.log(Buffer.from('fhqwhgads', 'utf8'));\n// Prints: <Buffer 66 68 71 77 68 67 61 64 73>\nconsole.log(Buffer.from('fhqwhgads', 'utf16le'));\n// Prints: <Buffer 66 00 68 00 71 00 77 00 68 00 67 00 61 00 64 00 73 00>\n
\n

Node.js buffers accept all case variations of encoding strings that they\nreceive. For example, UTF-8 can be specified as 'utf8', 'UTF8' or 'uTf8'.

\n

The character encodings currently supported by Node.js are the following:

\n\n

Converting a Buffer into a string using one of the above is referred to as\ndecoding, and converting a string into a Buffer is referred to as encoding.

\n

Node.js also supports the following binary-to-text encodings. For\nbinary-to-text encodings, the naming convention is reversed: Converting a\nBuffer into a string is typically referred to as encoding, and converting a\nstring into a Buffer as decoding.

\n\n

The following legacy character encodings are also supported:

\n\n
import { Buffer } from 'buffer';\n\nBuffer.from('1ag', 'hex');\n// Prints <Buffer 1a>, data truncated when first non-hexadecimal value\n// ('g') encountered.\n\nBuffer.from('1a7g', 'hex');\n// Prints <Buffer 1a>, data truncated when data ends in single digit ('7').\n\nBuffer.from('1634', 'hex');\n// Prints <Buffer 16 34>, all data represented.\n
\n
const { Buffer } = require('buffer');\n\nBuffer.from('1ag', 'hex');\n// Prints <Buffer 1a>, data truncated when first non-hexadecimal value\n// ('g') encountered.\n\nBuffer.from('1a7g', 'hex');\n// Prints <Buffer 1a>, data truncated when data ends in single digit ('7').\n\nBuffer.from('1634', 'hex');\n// Prints <Buffer 16 34>, all data represented.\n
\n

Modern Web browsers follow the WHATWG Encoding Standard which aliases\nboth 'latin1' and 'ISO-8859-1' to 'win-1252'. This means that while doing\nsomething like http.get(), if the returned charset is one of those listed in\nthe WHATWG specification it is possible that the server actually returned\n'win-1252'-encoded data, and using 'latin1' encoding may incorrectly decode\nthe characters.

", "type": "module", "displayName": "Buffers and character encodings" }, { "textRaw": "Buffers and TypedArrays", "name": "buffers_and_typedarrays", "meta": { "changes": [ { "version": "v3.0.0", "pr-url": "https://github.com/nodejs/node/pull/2002", "description": "The `Buffer`s class now inherits from `Uint8Array`." } ] }, "desc": "

Buffer instances are also JavaScript Uint8Array and TypedArray\ninstances. All TypedArray methods are available on Buffers. There are,\nhowever, subtle incompatibilities between the Buffer API and the\nTypedArray API.

\n

In particular:

\n\n

There are two ways to create new TypedArray instances from a Buffer:

\n\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from([1, 2, 3, 4]);\nconst uint32array = new Uint32Array(buf);\n\nconsole.log(uint32array);\n\n// Prints: Uint32Array(4) [ 1, 2, 3, 4 ]\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from([1, 2, 3, 4]);\nconst uint32array = new Uint32Array(buf);\n\nconsole.log(uint32array);\n\n// Prints: Uint32Array(4) [ 1, 2, 3, 4 ]\n
\n\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from('hello', 'utf16le');\nconst uint16array = new Uint16Array(\n  buf.buffer,\n  buf.byteOffset,\n  buf.length / Uint16Array.BYTES_PER_ELEMENT);\n\nconsole.log(uint16array);\n\n// Prints: Uint16Array(5) [ 104, 101, 108, 108, 111 ]\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from('hello', 'utf16le');\nconst uint16array = new Uint16Array(\n  buf.buffer,\n  buf.byteOffset,\n  buf.length / Uint16Array.BYTES_PER_ELEMENT);\n\nconsole.log(uint16array);\n\n// Prints: Uint16Array(5) [ 104, 101, 108, 108, 111 ]\n
\n

It is possible to create a new Buffer that shares the same allocated\nmemory as a TypedArray instance by using the TypedArray object’s\n.buffer property in the same way. Buffer.from()\nbehaves like new Uint8Array() in this context.

\n
import { Buffer } from 'buffer';\n\nconst arr = new Uint16Array(2);\n\narr[0] = 5000;\narr[1] = 4000;\n\n// Copies the contents of `arr`.\nconst buf1 = Buffer.from(arr);\n\n// Shares memory with `arr`.\nconst buf2 = Buffer.from(arr.buffer);\n\nconsole.log(buf1);\n// Prints: <Buffer 88 a0>\nconsole.log(buf2);\n// Prints: <Buffer 88 13 a0 0f>\n\narr[1] = 6000;\n\nconsole.log(buf1);\n// Prints: <Buffer 88 a0>\nconsole.log(buf2);\n// Prints: <Buffer 88 13 70 17>\n
\n
const { Buffer } = require('buffer');\n\nconst arr = new Uint16Array(2);\n\narr[0] = 5000;\narr[1] = 4000;\n\n// Copies the contents of `arr`.\nconst buf1 = Buffer.from(arr);\n\n// Shares memory with `arr`.\nconst buf2 = Buffer.from(arr.buffer);\n\nconsole.log(buf1);\n// Prints: <Buffer 88 a0>\nconsole.log(buf2);\n// Prints: <Buffer 88 13 a0 0f>\n\narr[1] = 6000;\n\nconsole.log(buf1);\n// Prints: <Buffer 88 a0>\nconsole.log(buf2);\n// Prints: <Buffer 88 13 70 17>\n
\n

When creating a Buffer using a TypedArray's .buffer, it is\npossible to use only a portion of the underlying ArrayBuffer by passing in\nbyteOffset and length parameters.

\n
import { Buffer } from 'buffer';\n\nconst arr = new Uint16Array(20);\nconst buf = Buffer.from(arr.buffer, 0, 16);\n\nconsole.log(buf.length);\n// Prints: 16\n
\n
const { Buffer } = require('buffer');\n\nconst arr = new Uint16Array(20);\nconst buf = Buffer.from(arr.buffer, 0, 16);\n\nconsole.log(buf.length);\n// Prints: 16\n
\n

The Buffer.from() and TypedArray.from() have different signatures and\nimplementations. Specifically, the TypedArray variants accept a second\nargument that is a mapping function that is invoked on every element of the\ntyped array:

\n\n

The Buffer.from() method, however, does not support the use of a mapping\nfunction:

\n", "type": "module", "displayName": "Buffers and TypedArrays" }, { "textRaw": "Buffers and iteration", "name": "buffers_and_iteration", "desc": "

Buffer instances can be iterated over using for..of syntax:

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from([1, 2, 3]);\n\nfor (const b of buf) {\n  console.log(b);\n}\n// Prints:\n//   1\n//   2\n//   3\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from([1, 2, 3]);\n\nfor (const b of buf) {\n  console.log(b);\n}\n// Prints:\n//   1\n//   2\n//   3\n
\n

Additionally, the buf.values(), buf.keys(), and\nbuf.entries() methods can be used to create iterators.

", "type": "module", "displayName": "Buffers and iteration" }, { "textRaw": "`buffer` module APIs", "name": "`buffer`_module_apis", "desc": "

While, the Buffer object is available as a global, there are additional\nBuffer-related APIs that are available only via the buffer module\naccessed using require('buffer').

", "methods": [ { "textRaw": "`buffer.atob(data)`", "type": "method", "name": "atob", "meta": { "added": [ "v15.13.0" ], "changes": [] }, "stability": 3, "stabilityText": "Legacy. Use `Buffer.from(data, 'base64')` instead.", "signatures": [ { "params": [ { "textRaw": "`data` {any} The Base64-encoded input string.", "name": "data", "type": "any", "desc": "The Base64-encoded input string." } ] } ], "desc": "

Decodes a string of Base64-encoded data into bytes, and encodes those bytes\ninto a string using Latin-1 (ISO-8859-1).

\n

The data may be any JavaScript-value that can be coerced into a string.

\n

This function is only provided for compatibility with legacy web platform APIs\nand should never be used in new code, because they use strings to represent\nbinary data and predate the introduction of typed arrays in JavaScript.\nFor code running using Node.js APIs, converting between base64-encoded strings\nand binary data should be performed using Buffer.from(str, 'base64') and\nbuf.toString('base64').

" }, { "textRaw": "`buffer.btoa(data)`", "type": "method", "name": "btoa", "meta": { "added": [ "v15.13.0" ], "changes": [] }, "stability": 3, "stabilityText": "Legacy. Use `buf.toString('base64')` instead.", "signatures": [ { "params": [ { "textRaw": "`data` {any} An ASCII (Latin1) string.", "name": "data", "type": "any", "desc": "An ASCII (Latin1) string." } ] } ], "desc": "

Decodes a string into bytes using Latin-1 (ISO-8859), and encodes those bytes\ninto a string using Base64.

\n

The data may be any JavaScript-value that can be coerced into a string.

\n

This function is only provided for compatibility with legacy web platform APIs\nand should never be used in new code, because they use strings to represent\nbinary data and predate the introduction of typed arrays in JavaScript.\nFor code running using Node.js APIs, converting between base64-encoded strings\nand binary data should be performed using Buffer.from(str, 'base64') and\nbuf.toString('base64').

" }, { "textRaw": "`buffer.transcode(source, fromEnc, toEnc)`", "type": "method", "name": "transcode", "meta": { "added": [ "v7.1.0" ], "changes": [ { "version": "v8.0.0", "pr-url": "https://github.com/nodejs/node/pull/10236", "description": "The `source` parameter can now be a `Uint8Array`." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {Buffer}", "name": "return", "type": "Buffer" }, "params": [ { "textRaw": "`source` {Buffer|Uint8Array} A `Buffer` or `Uint8Array` instance.", "name": "source", "type": "Buffer|Uint8Array", "desc": "A `Buffer` or `Uint8Array` instance." }, { "textRaw": "`fromEnc` {string} The current encoding.", "name": "fromEnc", "type": "string", "desc": "The current encoding." }, { "textRaw": "`toEnc` {string} To target encoding.", "name": "toEnc", "type": "string", "desc": "To target encoding." } ] } ], "desc": "

Re-encodes the given Buffer or Uint8Array instance from one character\nencoding to another. Returns a new Buffer instance.

\n

Throws if the fromEnc or toEnc specify invalid character encodings or if\nconversion from fromEnc to toEnc is not permitted.

\n

Encodings supported by buffer.transcode() are: 'ascii', 'utf8',\n'utf16le', 'ucs2', 'latin1', and 'binary'.

\n

The transcoding process will use substitution characters if a given byte\nsequence cannot be adequately represented in the target encoding. For instance:

\n
import { Buffer, transcode } from 'buffer';\n\nconst newBuf = transcode(Buffer.from('€'), 'utf8', 'ascii');\nconsole.log(newBuf.toString('ascii'));\n// Prints: '?'\n
\n
const { Buffer, transcode } = require('buffer');\n\nconst newBuf = transcode(Buffer.from('€'), 'utf8', 'ascii');\nconsole.log(newBuf.toString('ascii'));\n// Prints: '?'\n
\n

Because the Euro () sign is not representable in US-ASCII, it is replaced\nwith ? in the transcoded Buffer.

" } ], "properties": [ { "textRaw": "`INSPECT_MAX_BYTES` {integer} **Default:** `50`", "type": "integer", "name": "INSPECT_MAX_BYTES", "meta": { "added": [ "v0.5.4" ], "changes": [] }, "default": "`50`", "desc": "

Returns the maximum number of bytes that will be returned when\nbuf.inspect() is called. This can be overridden by user modules. See\nutil.inspect() for more details on buf.inspect() behavior.

" }, { "textRaw": "`kMaxLength` {integer} The largest size allowed for a single `Buffer` instance.", "type": "integer", "name": "kMaxLength", "meta": { "added": [ "v3.0.0" ], "changes": [] }, "desc": "

An alias for buffer.constants.MAX_LENGTH.

", "shortDesc": "The largest size allowed for a single `Buffer` instance." }, { "textRaw": "`kStringMaxLength` {integer} The largest length allowed for a single `string` instance.", "type": "integer", "name": "kStringMaxLength", "meta": { "added": [ "v3.0.0" ], "changes": [] }, "desc": "

An alias for buffer.constants.MAX_STRING_LENGTH.

", "shortDesc": "The largest length allowed for a single `string` instance." } ], "classes": [ { "textRaw": "Class: `SlowBuffer`", "type": "class", "name": "SlowBuffer", "meta": { "deprecated": [ "v6.0.0" ], "changes": [] }, "stability": 0, "stabilityText": "Deprecated: Use [`Buffer.allocUnsafeSlow()`][] instead.", "desc": "

See Buffer.allocUnsafeSlow(). This was never a class in the sense that\nthe constructor always returned a Buffer instance, rather than a SlowBuffer\ninstance.

", "signatures": [ { "params": [ { "textRaw": "`size` {integer} The desired length of the new `SlowBuffer`.", "name": "size", "type": "integer", "desc": "The desired length of the new `SlowBuffer`." } ], "desc": "

See Buffer.allocUnsafeSlow().

" } ] } ], "modules": [ { "textRaw": "Buffer constants", "name": "buffer_constants", "meta": { "added": [ "v8.2.0" ], "changes": [] }, "properties": [ { "textRaw": "`MAX_LENGTH` {integer} The largest size allowed for a single `Buffer` instance.", "type": "integer", "name": "MAX_LENGTH", "meta": { "added": [ "v8.2.0" ], "changes": [ { "version": "v15.0.0", "pr-url": "https://github.com/nodejs/node/pull/35415", "description": "Value is changed to 232 on 64-bit architectures." }, { "version": "v14.0.0", "pr-url": "https://github.com/nodejs/node/pull/32116", "description": "Value is changed from 231 - 1 to 232 - 1 on 64-bit architectures." } ] }, "desc": "

On 32-bit architectures, this value currently is 230 - 1 (about 1\nGB).

\n

On 64-bit architectures, this value currently is 232 (about 4 GB).

\n

It reflects v8::TypedArray::kMaxLength under the hood.

\n

This value is also available as buffer.kMaxLength.

", "shortDesc": "The largest size allowed for a single `Buffer` instance." }, { "textRaw": "`MAX_STRING_LENGTH` {integer} The largest length allowed for a single `string` instance.", "type": "integer", "name": "MAX_STRING_LENGTH", "meta": { "added": [ "v8.2.0" ], "changes": [] }, "desc": "

Represents the largest length that a string primitive can have, counted\nin UTF-16 code units.

\n

This value may depend on the JS engine that is being used.

", "shortDesc": "The largest length allowed for a single `string` instance." } ], "type": "module", "displayName": "Buffer constants" } ], "type": "module", "displayName": "`buffer` module APIs" }, { "textRaw": "`Buffer.from()`, `Buffer.alloc()`, and `Buffer.allocUnsafe()`", "name": "`buffer.from()`,_`buffer.alloc()`,_and_`buffer.allocunsafe()`", "desc": "

In versions of Node.js prior to 6.0.0, Buffer instances were created using the\nBuffer constructor function, which allocates the returned Buffer\ndifferently based on what arguments are provided:

\n\n

Because the behavior of new Buffer() is different depending on the type of the\nfirst argument, security and reliability issues can be inadvertently introduced\ninto applications when argument validation or Buffer initialization is not\nperformed.

\n

For example, if an attacker can cause an application to receive a number where\na string is expected, the application may call new Buffer(100)\ninstead of new Buffer(\"100\"), leading it to allocate a 100 byte buffer instead\nof allocating a 3 byte buffer with content \"100\". This is commonly possible\nusing JSON API calls. Since JSON distinguishes between numeric and string types,\nit allows injection of numbers where a naively written application that does not\nvalidate its input sufficiently might expect to always receive a string.\nBefore Node.js 8.0.0, the 100 byte buffer might contain\narbitrary pre-existing in-memory data, so may be used to expose in-memory\nsecrets to a remote attacker. Since Node.js 8.0.0, exposure of memory cannot\noccur because the data is zero-filled. However, other attacks are still\npossible, such as causing very large buffers to be allocated by the server,\nleading to performance degradation or crashing on memory exhaustion.

\n

To make the creation of Buffer instances more reliable and less error-prone,\nthe various forms of the new Buffer() constructor have been deprecated\nand replaced by separate Buffer.from(), Buffer.alloc(), and\nBuffer.allocUnsafe() methods.

\n

Developers should migrate all existing uses of the new Buffer() constructors\nto one of these new APIs.

\n\n

Buffer instances returned by Buffer.allocUnsafe() and\nBuffer.from(array) may be allocated off a shared internal memory pool\nif size is less than or equal to half Buffer.poolSize. Instances\nreturned by Buffer.allocUnsafeSlow() never use the shared internal\nmemory pool.

", "modules": [ { "textRaw": "The `--zero-fill-buffers` command-line option", "name": "the_`--zero-fill-buffers`_command-line_option", "meta": { "added": [ "v5.10.0" ], "changes": [] }, "desc": "

Node.js can be started using the --zero-fill-buffers command-line option to\ncause all newly-allocated Buffer instances to be zero-filled upon creation by\ndefault. Without the option, buffers created with Buffer.allocUnsafe(),\nBuffer.allocUnsafeSlow(), and new SlowBuffer(size) are not zero-filled.\nUse of this flag can have a measurable negative impact on performance. Use the\n--zero-fill-buffers option only when necessary to enforce that newly allocated\nBuffer instances cannot contain old data that is potentially sensitive.

\n
$ node --zero-fill-buffers\n> Buffer.allocUnsafe(5);\n<Buffer 00 00 00 00 00>\n
", "type": "module", "displayName": "The `--zero-fill-buffers` command-line option" }, { "textRaw": "What makes `Buffer.allocUnsafe()` and `Buffer.allocUnsafeSlow()` \"unsafe\"?", "name": "what_makes_`buffer.allocunsafe()`_and_`buffer.allocunsafeslow()`_\"unsafe\"?", "desc": "

When calling Buffer.allocUnsafe() and Buffer.allocUnsafeSlow(), the\nsegment of allocated memory is uninitialized (it is not zeroed-out). While\nthis design makes the allocation of memory quite fast, the allocated segment of\nmemory might contain old data that is potentially sensitive. Using a Buffer\ncreated by Buffer.allocUnsafe() without completely overwriting the\nmemory can allow this old data to be leaked when the Buffer memory is read.

\n

While there are clear performance advantages to using\nBuffer.allocUnsafe(), extra care must be taken in order to avoid\nintroducing security vulnerabilities into an application.

", "type": "module", "displayName": "What makes `Buffer.allocUnsafe()` and `Buffer.allocUnsafeSlow()` \"unsafe\"?" } ], "type": "module", "displayName": "`Buffer.from()`, `Buffer.alloc()`, and `Buffer.allocUnsafe()`" } ], "classes": [ { "textRaw": "Class: `Blob`", "type": "class", "name": "Blob", "meta": { "added": [ "v15.7.0" ], "changes": [] }, "stability": 1, "stabilityText": "Experimental", "desc": "

A Blob encapsulates immutable, raw data that can be safely shared across\nmultiple worker threads.

", "methods": [ { "textRaw": "`blob.arrayBuffer()`", "type": "method", "name": "arrayBuffer", "meta": { "added": [ "v15.7.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise}", "name": "return", "type": "Promise" }, "params": [] } ], "desc": "

Returns a promise that fulfills with an <ArrayBuffer> containing a copy of\nthe Blob data.

" }, { "textRaw": "`blob.slice([start, [end, [type]]])`", "type": "method", "name": "slice", "meta": { "added": [ "v15.7.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`start` {number} The starting index.", "name": "start", "type": "number", "desc": "The starting index." }, { "textRaw": "`end` {number} The ending index.", "name": "end", "type": "number", "desc": "The ending index." }, { "textRaw": "`type` {string} The content-type for the new `Blob`", "name": "type", "type": "string", "desc": "The content-type for the new `Blob`" } ] } ], "desc": "

Creates and returns a new Blob containing a subset of this Blob objects\ndata. The original Blob is not altered.

" }, { "textRaw": "`blob.text()`", "type": "method", "name": "text", "meta": { "added": [ "v15.7.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise}", "name": "return", "type": "Promise" }, "params": [] } ], "desc": "

Returns a promise that resolves the contents of the Blob decoded as a UTF-8\nstring.

" } ], "properties": [ { "textRaw": "`blob.size`", "name": "size", "meta": { "added": [ "v15.7.0" ], "changes": [] }, "desc": "

The total size of the Blob in bytes.

" }, { "textRaw": "`type` Type: {string}", "type": "string", "name": "Type", "meta": { "added": [ "v15.7.0" ], "changes": [] }, "desc": "

The content-type of the Blob.

" } ], "modules": [ { "textRaw": "`Blob` objects and `MessageChannel`", "name": "`blob`_objects_and_`messagechannel`", "desc": "

Once a <Blob> object is created, it can be sent via MessagePort to multiple\ndestinations without transferring or immediately copying the data. The data\ncontained by the Blob is copied only when the arrayBuffer() or text()\nmethods are called.

\n
import { Blob, Buffer } from 'buffer';\nimport { setTimeout as delay } from 'timers/promises';\n\nconst blob = new Blob(['hello there']);\n\nconst mc1 = new MessageChannel();\nconst mc2 = new MessageChannel();\n\nmc1.port1.onmessage = async ({ data }) => {\n  console.log(await data.arrayBuffer());\n  mc1.port1.close();\n};\n\nmc2.port1.onmessage = async ({ data }) => {\n  await delay(1000);\n  console.log(await data.arrayBuffer());\n  mc2.port1.close();\n};\n\nmc1.port2.postMessage(blob);\nmc2.port2.postMessage(blob);\n\n// The Blob is still usable after posting.\ndata.text().then(console.log);\n
\n
const { Blob, Buffer } = require('buffer');\nconst { setTimeout: delay } = require('timers/promises');\n\nconst blob = new Blob(['hello there']);\n\nconst mc1 = new MessageChannel();\nconst mc2 = new MessageChannel();\n\nmc1.port1.onmessage = async ({ data }) => {\n  console.log(await data.arrayBuffer());\n  mc1.port1.close();\n};\n\nmc2.port1.onmessage = async ({ data }) => {\n  await delay(1000);\n  console.log(await data.arrayBuffer());\n  mc2.port1.close();\n};\n\nmc1.port2.postMessage(blob);\nmc2.port2.postMessage(blob);\n\n// The Blob is still usable after posting.\ndata.text().then(console.log);\n
", "type": "module", "displayName": "`Blob` objects and `MessageChannel`" } ], "signatures": [ { "params": [ { "textRaw": "`sources` {string[]|ArrayBuffer[]|TypedArray[]|DataView[]|Blob[]} An array of string, {ArrayBuffer}, {TypedArray}, {DataView}, or {Blob} objects, or any mix of such objects, that will be stored within the `Blob`.", "name": "sources", "type": "string[]|ArrayBuffer[]|TypedArray[]|DataView[]|Blob[]", "desc": "An array of string, {ArrayBuffer}, {TypedArray}, {DataView}, or {Blob} objects, or any mix of such objects, that will be stored within the `Blob`." }, { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`encoding` {string} The character encoding to use for string sources. **Default:** `'utf8'`.", "name": "encoding", "type": "string", "default": "`'utf8'`", "desc": "The character encoding to use for string sources." }, { "textRaw": "`type` {string} The Blob content-type. The intent is for `type` to convey the MIME media type of the data, however no validation of the type format is performed.", "name": "type", "type": "string", "desc": "The Blob content-type. The intent is for `type` to convey the MIME media type of the data, however no validation of the type format is performed." } ] } ], "desc": "

Creates a new Blob object containing a concatenation of the given sources.

\n

<ArrayBuffer>, <TypedArray>, <DataView>, and <Buffer> sources are copied into\nthe 'Blob' and can therefore be safely modified after the 'Blob' is created.

\n

String sources are also copied into the Blob.

" } ] }, { "textRaw": "Class: `Buffer`", "type": "class", "name": "Buffer", "desc": "

The Buffer class is a global type for dealing with binary data directly.\nIt can be constructed in a variety of ways.

", "classMethods": [ { "textRaw": "Static method: `Buffer.alloc(size[, fill[, encoding]])`", "type": "classMethod", "name": "alloc", "meta": { "added": [ "v5.10.0" ], "changes": [ { "version": "v15.0.0", "pr-url": "https://github.com/nodejs/node/pull/34682", "description": "Throw ERR_INVALID_ARG_VALUE instead of ERR_INVALID_OPT_VALUE for invalid input arguments." }, { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18129", "description": "Attempting to fill a non-zero length buffer with a zero length buffer triggers a thrown exception." }, { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/17427", "description": "Specifying an invalid string for `fill` triggers a thrown exception." }, { "version": "v8.9.3", "pr-url": "https://github.com/nodejs/node/pull/17428", "description": "Specifying an invalid string for `fill` now results in a zero-filled buffer." } ] }, "signatures": [ { "params": [ { "textRaw": "`size` {integer} The desired length of the new `Buffer`.", "name": "size", "type": "integer", "desc": "The desired length of the new `Buffer`." }, { "textRaw": "`fill` {string|Buffer|Uint8Array|integer} A value to pre-fill the new `Buffer` with. **Default:** `0`.", "name": "fill", "type": "string|Buffer|Uint8Array|integer", "default": "`0`", "desc": "A value to pre-fill the new `Buffer` with." }, { "textRaw": "`encoding` {string} If `fill` is a string, this is its encoding. **Default:** `'utf8'`.", "name": "encoding", "type": "string", "default": "`'utf8'`", "desc": "If `fill` is a string, this is its encoding." } ] } ], "desc": "

Allocates a new Buffer of size bytes. If fill is undefined, the\nBuffer will be zero-filled.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.alloc(5);\n\nconsole.log(buf);\n// Prints: <Buffer 00 00 00 00 00>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.alloc(5);\n\nconsole.log(buf);\n// Prints: <Buffer 00 00 00 00 00>\n
\n

If size is larger than\nbuffer.constants.MAX_LENGTH or smaller than 0, ERR_INVALID_ARG_VALUE\nis thrown.

\n

If fill is specified, the allocated Buffer will be initialized by calling\nbuf.fill(fill).

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.alloc(5, 'a');\n\nconsole.log(buf);\n// Prints: <Buffer 61 61 61 61 61>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.alloc(5, 'a');\n\nconsole.log(buf);\n// Prints: <Buffer 61 61 61 61 61>\n
\n

If both fill and encoding are specified, the allocated Buffer will be\ninitialized by calling buf.fill(fill, encoding).

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=', 'base64');\n\nconsole.log(buf);\n// Prints: <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=', 'base64');\n\nconsole.log(buf);\n// Prints: <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>\n
\n

Calling Buffer.alloc() can be measurably slower than the alternative\nBuffer.allocUnsafe() but ensures that the newly created Buffer instance\ncontents will never contain sensitive data from previous allocations, including\ndata that might not have been allocated for Buffers.

\n

A TypeError will be thrown if size is not a number.

" }, { "textRaw": "Static method: `Buffer.allocUnsafe(size)`", "type": "classMethod", "name": "allocUnsafe", "meta": { "added": [ "v5.10.0" ], "changes": [ { "version": "v15.0.0", "pr-url": "https://github.com/nodejs/node/pull/34682", "description": "Throw ERR_INVALID_ARG_VALUE instead of ERR_INVALID_OPT_VALUE for invalid input arguments." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7079", "description": "Passing a negative `size` will now throw an error." } ] }, "signatures": [ { "params": [ { "textRaw": "`size` {integer} The desired length of the new `Buffer`.", "name": "size", "type": "integer", "desc": "The desired length of the new `Buffer`." } ] } ], "desc": "

Allocates a new Buffer of size bytes. If size is larger than\nbuffer.constants.MAX_LENGTH or smaller than 0, ERR_INVALID_ARG_VALUE\nis thrown.

\n

The underlying memory for Buffer instances created in this way is not\ninitialized. The contents of the newly created Buffer are unknown and\nmay contain sensitive data. Use Buffer.alloc() instead to initialize\nBuffer instances with zeroes.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.allocUnsafe(10);\n\nconsole.log(buf);\n// Prints (contents may vary): <Buffer a0 8b 28 3f 01 00 00 00 50 32>\n\nbuf.fill(0);\n\nconsole.log(buf);\n// Prints: <Buffer 00 00 00 00 00 00 00 00 00 00>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.allocUnsafe(10);\n\nconsole.log(buf);\n// Prints (contents may vary): <Buffer a0 8b 28 3f 01 00 00 00 50 32>\n\nbuf.fill(0);\n\nconsole.log(buf);\n// Prints: <Buffer 00 00 00 00 00 00 00 00 00 00>\n
\n

A TypeError will be thrown if size is not a number.

\n

The Buffer module pre-allocates an internal Buffer instance of\nsize Buffer.poolSize that is used as a pool for the fast allocation of new\nBuffer instances created using Buffer.allocUnsafe(),\nBuffer.from(array), Buffer.concat(), and the deprecated\nnew Buffer(size) constructor only when size is less than or equal\nto Buffer.poolSize >> 1 (floor of Buffer.poolSize divided by two).

\n

Use of this pre-allocated internal memory pool is a key difference between\ncalling Buffer.alloc(size, fill) vs. Buffer.allocUnsafe(size).fill(fill).\nSpecifically, Buffer.alloc(size, fill) will never use the internal Buffer\npool, while Buffer.allocUnsafe(size).fill(fill) will use the internal\nBuffer pool if size is less than or equal to half Buffer.poolSize. The\ndifference is subtle but can be important when an application requires the\nadditional performance that Buffer.allocUnsafe() provides.

" }, { "textRaw": "Static method: `Buffer.allocUnsafeSlow(size)`", "type": "classMethod", "name": "allocUnsafeSlow", "meta": { "added": [ "v5.12.0" ], "changes": [ { "version": "v15.0.0", "pr-url": "https://github.com/nodejs/node/pull/34682", "description": "Throw ERR_INVALID_ARG_VALUE instead of ERR_INVALID_OPT_VALUE for invalid input arguments." } ] }, "signatures": [ { "params": [ { "textRaw": "`size` {integer} The desired length of the new `Buffer`.", "name": "size", "type": "integer", "desc": "The desired length of the new `Buffer`." } ] } ], "desc": "

Allocates a new Buffer of size bytes. If size is larger than\nbuffer.constants.MAX_LENGTH or smaller than 0, ERR_INVALID_ARG_VALUE\nis thrown. A zero-length Buffer is created if size is 0.

\n

The underlying memory for Buffer instances created in this way is not\ninitialized. The contents of the newly created Buffer are unknown and\nmay contain sensitive data. Use buf.fill(0) to initialize\nsuch Buffer instances with zeroes.

\n

When using Buffer.allocUnsafe() to allocate new Buffer instances,\nallocations under 4 KB are sliced from a single pre-allocated Buffer. This\nallows applications to avoid the garbage collection overhead of creating many\nindividually allocated Buffer instances. This approach improves both\nperformance and memory usage by eliminating the need to track and clean up as\nmany individual ArrayBuffer objects.

\n

However, in the case where a developer may need to retain a small chunk of\nmemory from a pool for an indeterminate amount of time, it may be appropriate\nto create an un-pooled Buffer instance using Buffer.allocUnsafeSlow() and\nthen copying out the relevant bits.

\n
import { Buffer } from 'buffer';\n\n// Need to keep around a few small chunks of memory.\nconst store = [];\n\nsocket.on('readable', () => {\n  let data;\n  while (null !== (data = readable.read())) {\n    // Allocate for retained data.\n    const sb = Buffer.allocUnsafeSlow(10);\n\n    // Copy the data into the new allocation.\n    data.copy(sb, 0, 0, 10);\n\n    store.push(sb);\n  }\n});\n
\n
const { Buffer } = require('buffer');\n\n// Need to keep around a few small chunks of memory.\nconst store = [];\n\nsocket.on('readable', () => {\n  let data;\n  while (null !== (data = readable.read())) {\n    // Allocate for retained data.\n    const sb = Buffer.allocUnsafeSlow(10);\n\n    // Copy the data into the new allocation.\n    data.copy(sb, 0, 0, 10);\n\n    store.push(sb);\n  }\n});\n
\n

A TypeError will be thrown if size is not a number.

" }, { "textRaw": "Static method: `Buffer.byteLength(string[, encoding])`", "type": "classMethod", "name": "byteLength", "meta": { "added": [ "v0.1.90" ], "changes": [ { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/8946", "description": "Passing invalid input will now throw an error." }, { "version": "v5.10.0", "pr-url": "https://github.com/nodejs/node/pull/5255", "description": "The `string` parameter can now be any `TypedArray`, `DataView` or `ArrayBuffer`." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} The number of bytes contained within `string`.", "name": "return", "type": "integer", "desc": "The number of bytes contained within `string`." }, "params": [ { "textRaw": "`string` {string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer} A value to calculate the length of.", "name": "string", "type": "string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer", "desc": "A value to calculate the length of." }, { "textRaw": "`encoding` {string} If `string` is a string, this is its encoding. **Default:** `'utf8'`.", "name": "encoding", "type": "string", "default": "`'utf8'`", "desc": "If `string` is a string, this is its encoding." } ] } ], "desc": "

Returns the byte length of a string when encoded using encoding.\nThis is not the same as String.prototype.length, which does not account\nfor the encoding that is used to convert the string into bytes.

\n

For 'base64', 'base64url', and 'hex', this function assumes valid input.\nFor strings that contain non-base64/hex-encoded data (e.g. whitespace), the\nreturn value might be greater than the length of a Buffer created from the\nstring.

\n
import { Buffer } from 'buffer';\n\nconst str = '\\u00bd + \\u00bc = \\u00be';\n\nconsole.log(`${str}: ${str.length} characters, ` +\n            `${Buffer.byteLength(str, 'utf8')} bytes`);\n// Prints: ½ + ¼ = ¾: 9 characters, 12 bytes\n
\n
const { Buffer } = require('buffer');\n\nconst str = '\\u00bd + \\u00bc = \\u00be';\n\nconsole.log(`${str}: ${str.length} characters, ` +\n            `${Buffer.byteLength(str, 'utf8')} bytes`);\n// Prints: ½ + ¼ = ¾: 9 characters, 12 bytes\n
\n

When string is a Buffer/DataView/TypedArray/ArrayBuffer/\nSharedArrayBuffer, the byte length as reported by .byteLength\nis returned.

" }, { "textRaw": "Static method: `Buffer.compare(buf1, buf2)`", "type": "classMethod", "name": "compare", "meta": { "added": [ "v0.11.13" ], "changes": [ { "version": "v8.0.0", "pr-url": "https://github.com/nodejs/node/pull/10236", "description": "The arguments can now be `Uint8Array`s." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} Either `-1`, `0`, or `1`, depending on the result of the comparison. See [`buf.compare()`][] for details.", "name": "return", "type": "integer", "desc": "Either `-1`, `0`, or `1`, depending on the result of the comparison. See [`buf.compare()`][] for details." }, "params": [ { "textRaw": "`buf1` {Buffer|Uint8Array}", "name": "buf1", "type": "Buffer|Uint8Array" }, { "textRaw": "`buf2` {Buffer|Uint8Array}", "name": "buf2", "type": "Buffer|Uint8Array" } ] } ], "desc": "

Compares buf1 to buf2, typically for the purpose of sorting arrays of\nBuffer instances. This is equivalent to calling\nbuf1.compare(buf2).

\n
import { Buffer } from 'buffer';\n\nconst buf1 = Buffer.from('1234');\nconst buf2 = Buffer.from('0123');\nconst arr = [buf1, buf2];\n\nconsole.log(arr.sort(Buffer.compare));\n// Prints: [ <Buffer 30 31 32 33>, <Buffer 31 32 33 34> ]\n// (This result is equal to: [buf2, buf1].)\n
\n
const { Buffer } = require('buffer');\n\nconst buf1 = Buffer.from('1234');\nconst buf2 = Buffer.from('0123');\nconst arr = [buf1, buf2];\n\nconsole.log(arr.sort(Buffer.compare));\n// Prints: [ <Buffer 30 31 32 33>, <Buffer 31 32 33 34> ]\n// (This result is equal to: [buf2, buf1].)\n
" }, { "textRaw": "Static method: `Buffer.concat(list[, totalLength])`", "type": "classMethod", "name": "concat", "meta": { "added": [ "v0.7.11" ], "changes": [ { "version": "v8.0.0", "pr-url": "https://github.com/nodejs/node/pull/10236", "description": "The elements of `list` can now be `Uint8Array`s." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {Buffer}", "name": "return", "type": "Buffer" }, "params": [ { "textRaw": "`list` {Buffer[] | Uint8Array[]} List of `Buffer` or [`Uint8Array`][] instances to concatenate.", "name": "list", "type": "Buffer[] | Uint8Array[]", "desc": "List of `Buffer` or [`Uint8Array`][] instances to concatenate." }, { "textRaw": "`totalLength` {integer} Total length of the `Buffer` instances in `list` when concatenated.", "name": "totalLength", "type": "integer", "desc": "Total length of the `Buffer` instances in `list` when concatenated." } ] } ], "desc": "

Returns a new Buffer which is the result of concatenating all the Buffer\ninstances in the list together.

\n

If the list has no items, or if the totalLength is 0, then a new zero-length\nBuffer is returned.

\n

If totalLength is not provided, it is calculated from the Buffer instances\nin list by adding their lengths.

\n

If totalLength is provided, it is coerced to an unsigned integer. If the\ncombined length of the Buffers in list exceeds totalLength, the result is\ntruncated to totalLength.

\n
import { Buffer } from 'buffer';\n\n// Create a single `Buffer` from a list of three `Buffer` instances.\n\nconst buf1 = Buffer.alloc(10);\nconst buf2 = Buffer.alloc(14);\nconst buf3 = Buffer.alloc(18);\nconst totalLength = buf1.length + buf2.length + buf3.length;\n\nconsole.log(totalLength);\n// Prints: 42\n\nconst bufA = Buffer.concat([buf1, buf2, buf3], totalLength);\n\nconsole.log(bufA);\n// Prints: <Buffer 00 00 00 00 ...>\nconsole.log(bufA.length);\n// Prints: 42\n
\n
const { Buffer } = require('buffer');\n\n// Create a single `Buffer` from a list of three `Buffer` instances.\n\nconst buf1 = Buffer.alloc(10);\nconst buf2 = Buffer.alloc(14);\nconst buf3 = Buffer.alloc(18);\nconst totalLength = buf1.length + buf2.length + buf3.length;\n\nconsole.log(totalLength);\n// Prints: 42\n\nconst bufA = Buffer.concat([buf1, buf2, buf3], totalLength);\n\nconsole.log(bufA);\n// Prints: <Buffer 00 00 00 00 ...>\nconsole.log(bufA.length);\n// Prints: 42\n
\n

Buffer.concat() may also use the internal Buffer pool like\nBuffer.allocUnsafe() does.

" }, { "textRaw": "Static method: `Buffer.from(array)`", "type": "classMethod", "name": "from", "meta": { "added": [ "v5.10.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`array` {integer[]}", "name": "array", "type": "integer[]" } ] } ], "desc": "

Allocates a new Buffer using an array of bytes in the range 0255.\nArray entries outside that range will be truncated to fit into it.

\n
import { Buffer } from 'buffer';\n\n// Creates a new Buffer containing the UTF-8 bytes of the string 'buffer'.\nconst buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);\n
\n
const { Buffer } = require('buffer');\n\n// Creates a new Buffer containing the UTF-8 bytes of the string 'buffer'.\nconst buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);\n
\n

A TypeError will be thrown if array is not an Array or another type\nappropriate for Buffer.from() variants.

\n

Buffer.from(array) and Buffer.from(string) may also use the internal\nBuffer pool like Buffer.allocUnsafe() does.

" }, { "textRaw": "Static method: `Buffer.from(arrayBuffer[, byteOffset[, length]])`", "type": "classMethod", "name": "from", "meta": { "added": [ "v5.10.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`arrayBuffer` {ArrayBuffer|SharedArrayBuffer} An [`ArrayBuffer`][], [`SharedArrayBuffer`][], for example the `.buffer` property of a [`TypedArray`][].", "name": "arrayBuffer", "type": "ArrayBuffer|SharedArrayBuffer", "desc": "An [`ArrayBuffer`][], [`SharedArrayBuffer`][], for example the `.buffer` property of a [`TypedArray`][]." }, { "textRaw": "`byteOffset` {integer} Index of first byte to expose. **Default:** `0`.", "name": "byteOffset", "type": "integer", "default": "`0`", "desc": "Index of first byte to expose." }, { "textRaw": "`length` {integer} Number of bytes to expose. **Default:** `arrayBuffer.byteLength - byteOffset`.", "name": "length", "type": "integer", "default": "`arrayBuffer.byteLength - byteOffset`", "desc": "Number of bytes to expose." } ] } ], "desc": "

This creates a view of the ArrayBuffer without copying the underlying\nmemory. For example, when passed a reference to the .buffer property of a\nTypedArray instance, the newly created Buffer will share the same\nallocated memory as the TypedArray's underlying ArrayBuffer.

\n
import { Buffer } from 'buffer';\n\nconst arr = new Uint16Array(2);\n\narr[0] = 5000;\narr[1] = 4000;\n\n// Shares memory with `arr`.\nconst buf = Buffer.from(arr.buffer);\n\nconsole.log(buf);\n// Prints: <Buffer 88 13 a0 0f>\n\n// Changing the original Uint16Array changes the Buffer also.\narr[1] = 6000;\n\nconsole.log(buf);\n// Prints: <Buffer 88 13 70 17>\n
\n
const { Buffer } = require('buffer');\n\nconst arr = new Uint16Array(2);\n\narr[0] = 5000;\narr[1] = 4000;\n\n// Shares memory with `arr`.\nconst buf = Buffer.from(arr.buffer);\n\nconsole.log(buf);\n// Prints: <Buffer 88 13 a0 0f>\n\n// Changing the original Uint16Array changes the Buffer also.\narr[1] = 6000;\n\nconsole.log(buf);\n// Prints: <Buffer 88 13 70 17>\n
\n

The optional byteOffset and length arguments specify a memory range within\nthe arrayBuffer that will be shared by the Buffer.

\n
import { Buffer } from 'buffer';\n\nconst ab = new ArrayBuffer(10);\nconst buf = Buffer.from(ab, 0, 2);\n\nconsole.log(buf.length);\n// Prints: 2\n
\n
const { Buffer } = require('buffer');\n\nconst ab = new ArrayBuffer(10);\nconst buf = Buffer.from(ab, 0, 2);\n\nconsole.log(buf.length);\n// Prints: 2\n
\n

A TypeError will be thrown if arrayBuffer is not an ArrayBuffer or a\nSharedArrayBuffer or another type appropriate for Buffer.from()\nvariants.

\n

It is important to remember that a backing ArrayBuffer can cover a range\nof memory that extends beyond the bounds of a TypedArray view. A new\nBuffer created using the buffer property of a TypedArray may extend\nbeyond the range of the TypedArray:

\n
import { Buffer } from 'buffer';\n\nconst arrA = Uint8Array.from([0x63, 0x64, 0x65, 0x66]); // 4 elements\nconst arrB = new Uint8Array(arrA.buffer, 1, 2); // 2 elements\nconsole.log(arrA.buffer === arrB.buffer); // true\n\nconst buf = Buffer.from(arrB.buffer);\nconsole.log(buf);\n// Prints: <Buffer 63 64 65 66>\n
\n
const { Buffer } = require('buffer');\n\nconst arrA = Uint8Array.from([0x63, 0x64, 0x65, 0x66]); // 4 elements\nconst arrB = new Uint8Array(arrA.buffer, 1, 2); // 2 elements\nconsole.log(arrA.buffer === arrB.buffer); // true\n\nconst buf = Buffer.from(arrB.buffer);\nconsole.log(buf);\n// Prints: <Buffer 63 64 65 66>\n
" }, { "textRaw": "Static method: `Buffer.from(buffer)`", "type": "classMethod", "name": "from", "meta": { "added": [ "v5.10.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`buffer` {Buffer|Uint8Array} An existing `Buffer` or [`Uint8Array`][] from which to copy data.", "name": "buffer", "type": "Buffer|Uint8Array", "desc": "An existing `Buffer` or [`Uint8Array`][] from which to copy data." } ] } ], "desc": "

Copies the passed buffer data onto a new Buffer instance.

\n
import { Buffer } from 'buffer';\n\nconst buf1 = Buffer.from('buffer');\nconst buf2 = Buffer.from(buf1);\n\nbuf1[0] = 0x61;\n\nconsole.log(buf1.toString());\n// Prints: auffer\nconsole.log(buf2.toString());\n// Prints: buffer\n
\n
const { Buffer } = require('buffer');\n\nconst buf1 = Buffer.from('buffer');\nconst buf2 = Buffer.from(buf1);\n\nbuf1[0] = 0x61;\n\nconsole.log(buf1.toString());\n// Prints: auffer\nconsole.log(buf2.toString());\n// Prints: buffer\n
\n

A TypeError will be thrown if buffer is not a Buffer or another type\nappropriate for Buffer.from() variants.

" }, { "textRaw": "Static method: `Buffer.from(object[, offsetOrEncoding[, length]])`", "type": "classMethod", "name": "from", "meta": { "added": [ "v8.2.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`object` {Object} An object supporting `Symbol.toPrimitive` or `valueOf()`.", "name": "object", "type": "Object", "desc": "An object supporting `Symbol.toPrimitive` or `valueOf()`." }, { "textRaw": "`offsetOrEncoding` {integer|string} A byte-offset or encoding.", "name": "offsetOrEncoding", "type": "integer|string", "desc": "A byte-offset or encoding." }, { "textRaw": "`length` {integer} A length.", "name": "length", "type": "integer", "desc": "A length." } ] } ], "desc": "

For objects whose valueOf() function returns a value not strictly equal to\nobject, returns Buffer.from(object.valueOf(), offsetOrEncoding, length).

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from(new String('this is a test'));\n// Prints: <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from(new String('this is a test'));\n// Prints: <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74>\n
\n

For objects that support Symbol.toPrimitive, returns\nBuffer.from(object[Symbol.toPrimitive]('string'), offsetOrEncoding).

\n
import { Buffer } from 'buffer';\n\nclass Foo {\n  [Symbol.toPrimitive]() {\n    return 'this is a test';\n  }\n}\n\nconst buf = Buffer.from(new Foo(), 'utf8');\n// Prints: <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74>\n
\n
const { Buffer } = require('buffer');\n\nclass Foo {\n  [Symbol.toPrimitive]() {\n    return 'this is a test';\n  }\n}\n\nconst buf = Buffer.from(new Foo(), 'utf8');\n// Prints: <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74>\n
\n

A TypeError will be thrown if object does not have the mentioned methods or\nis not of another type appropriate for Buffer.from() variants.

" }, { "textRaw": "Static method: `Buffer.from(string[, encoding])`", "type": "classMethod", "name": "from", "meta": { "added": [ "v5.10.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`string` {string} A string to encode.", "name": "string", "type": "string", "desc": "A string to encode." }, { "textRaw": "`encoding` {string} The encoding of `string`. **Default:** `'utf8'`.", "name": "encoding", "type": "string", "default": "`'utf8'`", "desc": "The encoding of `string`." } ] } ], "desc": "

Creates a new Buffer containing string. The encoding parameter identifies\nthe character encoding to be used when converting string into bytes.

\n
import { Buffer } from 'buffer';\n\nconst buf1 = Buffer.from('this is a tést');\nconst buf2 = Buffer.from('7468697320697320612074c3a97374', 'hex');\n\nconsole.log(buf1.toString());\n// Prints: this is a tést\nconsole.log(buf2.toString());\n// Prints: this is a tést\nconsole.log(buf1.toString('latin1'));\n// Prints: this is a tést\n
\n
const { Buffer } = require('buffer');\n\nconst buf1 = Buffer.from('this is a tést');\nconst buf2 = Buffer.from('7468697320697320612074c3a97374', 'hex');\n\nconsole.log(buf1.toString());\n// Prints: this is a tést\nconsole.log(buf2.toString());\n// Prints: this is a tést\nconsole.log(buf1.toString('latin1'));\n// Prints: this is a tést\n
\n

A TypeError will be thrown if string is not a string or another type\nappropriate for Buffer.from() variants.

" }, { "textRaw": "Static method: `Buffer.isBuffer(obj)`", "type": "classMethod", "name": "isBuffer", "meta": { "added": [ "v0.1.101" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [ { "textRaw": "`obj` {Object}", "name": "obj", "type": "Object" } ] } ], "desc": "

Returns true if obj is a Buffer, false otherwise.

\n
import { Buffer } from 'buffer';\n\nBuffer.isBuffer(Buffer.alloc(10)); // true\nBuffer.isBuffer(Buffer.from('foo')); // true\nBuffer.isBuffer('a string'); // false\nBuffer.isBuffer([]); // false\nBuffer.isBuffer(new Uint8Array(1024)); // false\n
\n
const { Buffer } = require('buffer');\n\nBuffer.isBuffer(Buffer.alloc(10)); // true\nBuffer.isBuffer(Buffer.from('foo')); // true\nBuffer.isBuffer('a string'); // false\nBuffer.isBuffer([]); // false\nBuffer.isBuffer(new Uint8Array(1024)); // false\n
" }, { "textRaw": "Static method: `Buffer.isEncoding(encoding)`", "type": "classMethod", "name": "isEncoding", "meta": { "added": [ "v0.9.1" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [ { "textRaw": "`encoding` {string} A character encoding name to check.", "name": "encoding", "type": "string", "desc": "A character encoding name to check." } ] } ], "desc": "

Returns true if encoding is the name of a supported character encoding,\nor false otherwise.

\n
import { Buffer } from 'buffer';\n\nconsole.log(Buffer.isEncoding('utf8'));\n// Prints: true\n\nconsole.log(Buffer.isEncoding('hex'));\n// Prints: true\n\nconsole.log(Buffer.isEncoding('utf/8'));\n// Prints: false\n\nconsole.log(Buffer.isEncoding(''));\n// Prints: false\n
\n
const { Buffer } = require('buffer');\n\nconsole.log(Buffer.isEncoding('utf8'));\n// Prints: true\n\nconsole.log(Buffer.isEncoding('hex'));\n// Prints: true\n\nconsole.log(Buffer.isEncoding('utf/8'));\n// Prints: false\n\nconsole.log(Buffer.isEncoding(''));\n// Prints: false\n
" } ], "properties": [ { "textRaw": "`poolSize` {integer} **Default:** `8192`", "type": "integer", "name": "poolSize", "meta": { "added": [ "v0.11.3" ], "changes": [] }, "default": "`8192`", "desc": "

This is the size (in bytes) of pre-allocated internal Buffer instances used\nfor pooling. This value may be modified.

" }, { "textRaw": "`[index]` `index` {integer}", "type": "integer", "name": "index", "desc": "

The index operator [index] can be used to get and set the octet at position\nindex in buf. The values refer to individual bytes, so the legal value\nrange is between 0x00 and 0xFF (hex) or 0 and 255 (decimal).

\n

This operator is inherited from Uint8Array, so its behavior on out-of-bounds\naccess is the same as Uint8Array. In other words, buf[index] returns\nundefined when index is negative or greater or equal to buf.length, and\nbuf[index] = value does not modify the buffer if index is negative or\n>= buf.length.

\n
import { Buffer } from 'buffer';\n\n// Copy an ASCII string into a `Buffer` one byte at a time.\n// (This only works for ASCII-only strings. In general, one should use\n// `Buffer.from()` to perform this conversion.)\n\nconst str = 'Node.js';\nconst buf = Buffer.allocUnsafe(str.length);\n\nfor (let i = 0; i < str.length; i++) {\n  buf[i] = str.charCodeAt(i);\n}\n\nconsole.log(buf.toString('utf8'));\n// Prints: Node.js\n
\n
const { Buffer } = require('buffer');\n\n// Copy an ASCII string into a `Buffer` one byte at a time.\n// (This only works for ASCII-only strings. In general, one should use\n// `Buffer.from()` to perform this conversion.)\n\nconst str = 'Node.js';\nconst buf = Buffer.allocUnsafe(str.length);\n\nfor (let i = 0; i < str.length; i++) {\n  buf[i] = str.charCodeAt(i);\n}\n\nconsole.log(buf.toString('utf8'));\n// Prints: Node.js\n
" }, { "textRaw": "`buffer` {ArrayBuffer} The underlying `ArrayBuffer` object based on which this `Buffer` object is created.", "type": "ArrayBuffer", "name": "buffer", "desc": "

This ArrayBuffer is not guaranteed to correspond exactly to the original\nBuffer. See the notes on buf.byteOffset for details.

\n
import { Buffer } from 'buffer';\n\nconst arrayBuffer = new ArrayBuffer(16);\nconst buffer = Buffer.from(arrayBuffer);\n\nconsole.log(buffer.buffer === arrayBuffer);\n// Prints: true\n
\n
const { Buffer } = require('buffer');\n\nconst arrayBuffer = new ArrayBuffer(16);\nconst buffer = Buffer.from(arrayBuffer);\n\nconsole.log(buffer.buffer === arrayBuffer);\n// Prints: true\n
", "shortDesc": "The underlying `ArrayBuffer` object based on which this `Buffer` object is created." }, { "textRaw": "`byteOffset` {integer} The `byteOffset` of the `Buffer`s underlying `ArrayBuffer` object.", "type": "integer", "name": "byteOffset", "desc": "

When setting byteOffset in Buffer.from(ArrayBuffer, byteOffset, length),\nor sometimes when allocating a Buffer smaller than Buffer.poolSize, the\nbuffer does not start from a zero offset on the underlying ArrayBuffer.

\n

This can cause problems when accessing the underlying ArrayBuffer directly\nusing buf.buffer, as other parts of the ArrayBuffer may be unrelated\nto the Buffer object itself.

\n

A common issue when creating a TypedArray object that shares its memory with\na Buffer is that in this case one needs to specify the byteOffset correctly:

\n
import { Buffer } from 'buffer';\n\n// Create a buffer smaller than `Buffer.poolSize`.\nconst nodeBuffer = new Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);\n\n// When casting the Node.js Buffer to an Int8Array, use the byteOffset\n// to refer only to the part of `nodeBuffer.buffer` that contains the memory\n// for `nodeBuffer`.\nnew Int8Array(nodeBuffer.buffer, nodeBuffer.byteOffset, nodeBuffer.length);\n
\n
const { Buffer } = require('buffer');\n\n// Create a buffer smaller than `Buffer.poolSize`.\nconst nodeBuffer = new Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);\n\n// When casting the Node.js Buffer to an Int8Array, use the byteOffset\n// to refer only to the part of `nodeBuffer.buffer` that contains the memory\n// for `nodeBuffer`.\nnew Int8Array(nodeBuffer.buffer, nodeBuffer.byteOffset, nodeBuffer.length);\n
", "shortDesc": "The `byteOffset` of the `Buffer`s underlying `ArrayBuffer` object." }, { "textRaw": "`length` {integer}", "type": "integer", "name": "length", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "desc": "

Returns the number of bytes in buf.

\n
import { Buffer } from 'buffer';\n\n// Create a `Buffer` and write a shorter string to it using UTF-8.\n\nconst buf = Buffer.alloc(1234);\n\nconsole.log(buf.length);\n// Prints: 1234\n\nbuf.write('some string', 0, 'utf8');\n\nconsole.log(buf.length);\n// Prints: 1234\n
\n
const { Buffer } = require('buffer');\n\n// Create a `Buffer` and write a shorter string to it using UTF-8.\n\nconst buf = Buffer.alloc(1234);\n\nconsole.log(buf.length);\n// Prints: 1234\n\nbuf.write('some string', 0, 'utf8');\n\nconsole.log(buf.length);\n// Prints: 1234\n
" }, { "textRaw": "`buf.parent`", "name": "parent", "meta": { "deprecated": [ "v8.0.0" ], "changes": [] }, "stability": 0, "stabilityText": "Deprecated: Use [`buf.buffer`][] instead.", "desc": "

The buf.parent property is a deprecated alias for buf.buffer.

" } ], "methods": [ { "textRaw": "`buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])`", "type": "method", "name": "compare", "meta": { "added": [ "v0.11.13" ], "changes": [ { "version": "v8.0.0", "pr-url": "https://github.com/nodejs/node/pull/10236", "description": "The `target` parameter can now be a `Uint8Array`." }, { "version": "v5.11.0", "pr-url": "https://github.com/nodejs/node/pull/5880", "description": "Additional parameters for specifying offsets are supported now." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer}", "name": "return", "type": "integer" }, "params": [ { "textRaw": "`target` {Buffer|Uint8Array} A `Buffer` or [`Uint8Array`][] with which to compare `buf`.", "name": "target", "type": "Buffer|Uint8Array", "desc": "A `Buffer` or [`Uint8Array`][] with which to compare `buf`." }, { "textRaw": "`targetStart` {integer} The offset within `target` at which to begin comparison. **Default:** `0`.", "name": "targetStart", "type": "integer", "default": "`0`", "desc": "The offset within `target` at which to begin comparison." }, { "textRaw": "`targetEnd` {integer} The offset within `target` at which to end comparison (not inclusive). **Default:** `target.length`.", "name": "targetEnd", "type": "integer", "default": "`target.length`", "desc": "The offset within `target` at which to end comparison (not inclusive)." }, { "textRaw": "`sourceStart` {integer} The offset within `buf` at which to begin comparison. **Default:** `0`.", "name": "sourceStart", "type": "integer", "default": "`0`", "desc": "The offset within `buf` at which to begin comparison." }, { "textRaw": "`sourceEnd` {integer} The offset within `buf` at which to end comparison (not inclusive). **Default:** [`buf.length`][].", "name": "sourceEnd", "type": "integer", "default": "[`buf.length`][]", "desc": "The offset within `buf` at which to end comparison (not inclusive)." } ] } ], "desc": "

Compares buf with target and returns a number indicating whether buf\ncomes before, after, or is the same as target in sort order.\nComparison is based on the actual sequence of bytes in each Buffer.

\n\n
import { Buffer } from 'buffer';\n\nconst buf1 = Buffer.from('ABC');\nconst buf2 = Buffer.from('BCD');\nconst buf3 = Buffer.from('ABCD');\n\nconsole.log(buf1.compare(buf1));\n// Prints: 0\nconsole.log(buf1.compare(buf2));\n// Prints: -1\nconsole.log(buf1.compare(buf3));\n// Prints: -1\nconsole.log(buf2.compare(buf1));\n// Prints: 1\nconsole.log(buf2.compare(buf3));\n// Prints: 1\nconsole.log([buf1, buf2, buf3].sort(Buffer.compare));\n// Prints: [ <Buffer 41 42 43>, <Buffer 41 42 43 44>, <Buffer 42 43 44> ]\n// (This result is equal to: [buf1, buf3, buf2].)\n
\n
const { Buffer } = require('buffer');\n\nconst buf1 = Buffer.from('ABC');\nconst buf2 = Buffer.from('BCD');\nconst buf3 = Buffer.from('ABCD');\n\nconsole.log(buf1.compare(buf1));\n// Prints: 0\nconsole.log(buf1.compare(buf2));\n// Prints: -1\nconsole.log(buf1.compare(buf3));\n// Prints: -1\nconsole.log(buf2.compare(buf1));\n// Prints: 1\nconsole.log(buf2.compare(buf3));\n// Prints: 1\nconsole.log([buf1, buf2, buf3].sort(Buffer.compare));\n// Prints: [ <Buffer 41 42 43>, <Buffer 41 42 43 44>, <Buffer 42 43 44> ]\n// (This result is equal to: [buf1, buf3, buf2].)\n
\n

The optional targetStart, targetEnd, sourceStart, and sourceEnd\narguments can be used to limit the comparison to specific ranges within target\nand buf respectively.

\n
import { Buffer } from 'buffer';\n\nconst buf1 = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9]);\nconst buf2 = Buffer.from([5, 6, 7, 8, 9, 1, 2, 3, 4]);\n\nconsole.log(buf1.compare(buf2, 5, 9, 0, 4));\n// Prints: 0\nconsole.log(buf1.compare(buf2, 0, 6, 4));\n// Prints: -1\nconsole.log(buf1.compare(buf2, 5, 6, 5));\n// Prints: 1\n
\n
const { Buffer } = require('buffer');\n\nconst buf1 = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9]);\nconst buf2 = Buffer.from([5, 6, 7, 8, 9, 1, 2, 3, 4]);\n\nconsole.log(buf1.compare(buf2, 5, 9, 0, 4));\n// Prints: 0\nconsole.log(buf1.compare(buf2, 0, 6, 4));\n// Prints: -1\nconsole.log(buf1.compare(buf2, 5, 6, 5));\n// Prints: 1\n
\n

ERR_OUT_OF_RANGE is thrown if targetStart < 0, sourceStart < 0,\ntargetEnd > target.byteLength, or sourceEnd > source.byteLength.

" }, { "textRaw": "`buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])`", "type": "method", "name": "copy", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} The number of bytes copied.", "name": "return", "type": "integer", "desc": "The number of bytes copied." }, "params": [ { "textRaw": "`target` {Buffer|Uint8Array} A `Buffer` or [`Uint8Array`][] to copy into.", "name": "target", "type": "Buffer|Uint8Array", "desc": "A `Buffer` or [`Uint8Array`][] to copy into." }, { "textRaw": "`targetStart` {integer} The offset within `target` at which to begin writing. **Default:** `0`.", "name": "targetStart", "type": "integer", "default": "`0`", "desc": "The offset within `target` at which to begin writing." }, { "textRaw": "`sourceStart` {integer} The offset within `buf` from which to begin copying. **Default:** `0`.", "name": "sourceStart", "type": "integer", "default": "`0`", "desc": "The offset within `buf` from which to begin copying." }, { "textRaw": "`sourceEnd` {integer} The offset within `buf` at which to stop copying (not inclusive). **Default:** [`buf.length`][].", "name": "sourceEnd", "type": "integer", "default": "[`buf.length`][]", "desc": "The offset within `buf` at which to stop copying (not inclusive)." } ] } ], "desc": "

Copies data from a region of buf to a region in target, even if the target\nmemory region overlaps with buf.

\n

TypedArray.prototype.set() performs the same operation, and is available\nfor all TypedArrays, including Node.js Buffers, although it takes\ndifferent function arguments.

\n
import { Buffer } from 'buffer';\n\n// Create two `Buffer` instances.\nconst buf1 = Buffer.allocUnsafe(26);\nconst buf2 = Buffer.allocUnsafe(26).fill('!');\n\nfor (let i = 0; i < 26; i++) {\n  // 97 is the decimal ASCII value for 'a'.\n  buf1[i] = i + 97;\n}\n\n// Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`.\nbuf1.copy(buf2, 8, 16, 20);\n// This is equivalent to:\n// buf2.set(buf1.subarray(16, 20), 8);\n\nconsole.log(buf2.toString('ascii', 0, 25));\n// Prints: !!!!!!!!qrst!!!!!!!!!!!!!\n
\n
const { Buffer } = require('buffer');\n\n// Create two `Buffer` instances.\nconst buf1 = Buffer.allocUnsafe(26);\nconst buf2 = Buffer.allocUnsafe(26).fill('!');\n\nfor (let i = 0; i < 26; i++) {\n  // 97 is the decimal ASCII value for 'a'.\n  buf1[i] = i + 97;\n}\n\n// Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`.\nbuf1.copy(buf2, 8, 16, 20);\n// This is equivalent to:\n// buf2.set(buf1.subarray(16, 20), 8);\n\nconsole.log(buf2.toString('ascii', 0, 25));\n// Prints: !!!!!!!!qrst!!!!!!!!!!!!!\n
\n
import { Buffer } from 'buffer';\n\n// Create a `Buffer` and copy data from one region to an overlapping region\n// within the same `Buffer`.\n\nconst buf = Buffer.allocUnsafe(26);\n\nfor (let i = 0; i < 26; i++) {\n  // 97 is the decimal ASCII value for 'a'.\n  buf[i] = i + 97;\n}\n\nbuf.copy(buf, 0, 4, 10);\n\nconsole.log(buf.toString());\n// Prints: efghijghijklmnopqrstuvwxyz\n
\n
const { Buffer } = require('buffer');\n\n// Create a `Buffer` and copy data from one region to an overlapping region\n// within the same `Buffer`.\n\nconst buf = Buffer.allocUnsafe(26);\n\nfor (let i = 0; i < 26; i++) {\n  // 97 is the decimal ASCII value for 'a'.\n  buf[i] = i + 97;\n}\n\nbuf.copy(buf, 0, 4, 10);\n\nconsole.log(buf.toString());\n// Prints: efghijghijklmnopqrstuvwxyz\n
" }, { "textRaw": "`buf.entries()`", "type": "method", "name": "entries", "meta": { "added": [ "v1.1.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Iterator}", "name": "return", "type": "Iterator" }, "params": [] } ], "desc": "

Creates and returns an iterator of [index, byte] pairs from the contents\nof buf.

\n
import { Buffer } from 'buffer';\n\n// Log the entire contents of a `Buffer`.\n\nconst buf = Buffer.from('buffer');\n\nfor (const pair of buf.entries()) {\n  console.log(pair);\n}\n// Prints:\n//   [0, 98]\n//   [1, 117]\n//   [2, 102]\n//   [3, 102]\n//   [4, 101]\n//   [5, 114]\n
\n
const { Buffer } = require('buffer');\n\n// Log the entire contents of a `Buffer`.\n\nconst buf = Buffer.from('buffer');\n\nfor (const pair of buf.entries()) {\n  console.log(pair);\n}\n// Prints:\n//   [0, 98]\n//   [1, 117]\n//   [2, 102]\n//   [3, 102]\n//   [4, 101]\n//   [5, 114]\n
" }, { "textRaw": "`buf.equals(otherBuffer)`", "type": "method", "name": "equals", "meta": { "added": [ "v0.11.13" ], "changes": [ { "version": "v8.0.0", "pr-url": "https://github.com/nodejs/node/pull/10236", "description": "The arguments can now be `Uint8Array`s." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [ { "textRaw": "`otherBuffer` {Buffer|Uint8Array} A `Buffer` or [`Uint8Array`][] with which to compare `buf`.", "name": "otherBuffer", "type": "Buffer|Uint8Array", "desc": "A `Buffer` or [`Uint8Array`][] with which to compare `buf`." } ] } ], "desc": "

Returns true if both buf and otherBuffer have exactly the same bytes,\nfalse otherwise. Equivalent to\nbuf.compare(otherBuffer) === 0.

\n
import { Buffer } from 'buffer';\n\nconst buf1 = Buffer.from('ABC');\nconst buf2 = Buffer.from('414243', 'hex');\nconst buf3 = Buffer.from('ABCD');\n\nconsole.log(buf1.equals(buf2));\n// Prints: true\nconsole.log(buf1.equals(buf3));\n// Prints: false\n
\n
const { Buffer } = require('buffer');\n\nconst buf1 = Buffer.from('ABC');\nconst buf2 = Buffer.from('414243', 'hex');\nconst buf3 = Buffer.from('ABCD');\n\nconsole.log(buf1.equals(buf2));\n// Prints: true\nconsole.log(buf1.equals(buf3));\n// Prints: false\n
" }, { "textRaw": "`buf.fill(value[, offset[, end]][, encoding])`", "type": "method", "name": "fill", "meta": { "added": [ "v0.5.0" ], "changes": [ { "version": "v11.0.0", "pr-url": "https://github.com/nodejs/node/pull/22969", "description": "Throws `ERR_OUT_OF_RANGE` instead of `ERR_INDEX_OUT_OF_RANGE`." }, { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18790", "description": "Negative `end` values throw an `ERR_INDEX_OUT_OF_RANGE` error." }, { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18129", "description": "Attempting to fill a non-zero length buffer with a zero length buffer triggers a thrown exception." }, { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/17427", "description": "Specifying an invalid string for `value` triggers a thrown exception." }, { "version": "v5.7.0", "pr-url": "https://github.com/nodejs/node/pull/4935", "description": "The `encoding` parameter is supported now." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {Buffer} A reference to `buf`.", "name": "return", "type": "Buffer", "desc": "A reference to `buf`." }, "params": [ { "textRaw": "`value` {string|Buffer|Uint8Array|integer} The value with which to fill `buf`.", "name": "value", "type": "string|Buffer|Uint8Array|integer", "desc": "The value with which to fill `buf`." }, { "textRaw": "`offset` {integer} Number of bytes to skip before starting to fill `buf`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to fill `buf`." }, { "textRaw": "`end` {integer} Where to stop filling `buf` (not inclusive). **Default:** [`buf.length`][].", "name": "end", "type": "integer", "default": "[`buf.length`][]", "desc": "Where to stop filling `buf` (not inclusive)." }, { "textRaw": "`encoding` {string} The encoding for `value` if `value` is a string. **Default:** `'utf8'`.", "name": "encoding", "type": "string", "default": "`'utf8'`", "desc": "The encoding for `value` if `value` is a string." } ] } ], "desc": "

Fills buf with the specified value. If the offset and end are not given,\nthe entire buf will be filled:

\n
import { Buffer } from 'buffer';\n\n// Fill a `Buffer` with the ASCII character 'h'.\n\nconst b = Buffer.allocUnsafe(50).fill('h');\n\nconsole.log(b.toString());\n// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\n
\n
const { Buffer } = require('buffer');\n\n// Fill a `Buffer` with the ASCII character 'h'.\n\nconst b = Buffer.allocUnsafe(50).fill('h');\n\nconsole.log(b.toString());\n// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\n
\n

value is coerced to a uint32 value if it is not a string, Buffer, or\ninteger. If the resulting integer is greater than 255 (decimal), buf will be\nfilled with value & 255.

\n

If the final write of a fill() operation falls on a multi-byte character,\nthen only the bytes of that character that fit into buf are written:

\n
import { Buffer } from 'buffer';\n\n// Fill a `Buffer` with character that takes up two bytes in UTF-8.\n\nconsole.log(Buffer.allocUnsafe(5).fill('\\u0222'));\n// Prints: <Buffer c8 a2 c8 a2 c8>\n
\n
const { Buffer } = require('buffer');\n\n// Fill a `Buffer` with character that takes up two bytes in UTF-8.\n\nconsole.log(Buffer.allocUnsafe(5).fill('\\u0222'));\n// Prints: <Buffer c8 a2 c8 a2 c8>\n
\n

If value contains invalid characters, it is truncated; if no valid\nfill data remains, an exception is thrown:

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.allocUnsafe(5);\n\nconsole.log(buf.fill('a'));\n// Prints: <Buffer 61 61 61 61 61>\nconsole.log(buf.fill('aazz', 'hex'));\n// Prints: <Buffer aa aa aa aa aa>\nconsole.log(buf.fill('zz', 'hex'));\n// Throws an exception.\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.allocUnsafe(5);\n\nconsole.log(buf.fill('a'));\n// Prints: <Buffer 61 61 61 61 61>\nconsole.log(buf.fill('aazz', 'hex'));\n// Prints: <Buffer aa aa aa aa aa>\nconsole.log(buf.fill('zz', 'hex'));\n// Throws an exception.\n
" }, { "textRaw": "`buf.includes(value[, byteOffset][, encoding])`", "type": "method", "name": "includes", "meta": { "added": [ "v5.3.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean} `true` if `value` was found in `buf`, `false` otherwise.", "name": "return", "type": "boolean", "desc": "`true` if `value` was found in `buf`, `false` otherwise." }, "params": [ { "textRaw": "`value` {string|Buffer|Uint8Array|integer} What to search for.", "name": "value", "type": "string|Buffer|Uint8Array|integer", "desc": "What to search for." }, { "textRaw": "`byteOffset` {integer} Where to begin searching in `buf`. If negative, then offset is calculated from the end of `buf`. **Default:** `0`.", "name": "byteOffset", "type": "integer", "default": "`0`", "desc": "Where to begin searching in `buf`. If negative, then offset is calculated from the end of `buf`." }, { "textRaw": "`encoding` {string} If `value` is a string, this is its encoding. **Default:** `'utf8'`.", "name": "encoding", "type": "string", "default": "`'utf8'`", "desc": "If `value` is a string, this is its encoding." } ] } ], "desc": "

Equivalent to buf.indexOf() !== -1.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from('this is a buffer');\n\nconsole.log(buf.includes('this'));\n// Prints: true\nconsole.log(buf.includes('is'));\n// Prints: true\nconsole.log(buf.includes(Buffer.from('a buffer')));\n// Prints: true\nconsole.log(buf.includes(97));\n// Prints: true (97 is the decimal ASCII value for 'a')\nconsole.log(buf.includes(Buffer.from('a buffer example')));\n// Prints: false\nconsole.log(buf.includes(Buffer.from('a buffer example').slice(0, 8)));\n// Prints: true\nconsole.log(buf.includes('this', 4));\n// Prints: false\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from('this is a buffer');\n\nconsole.log(buf.includes('this'));\n// Prints: true\nconsole.log(buf.includes('is'));\n// Prints: true\nconsole.log(buf.includes(Buffer.from('a buffer')));\n// Prints: true\nconsole.log(buf.includes(97));\n// Prints: true (97 is the decimal ASCII value for 'a')\nconsole.log(buf.includes(Buffer.from('a buffer example')));\n// Prints: false\nconsole.log(buf.includes(Buffer.from('a buffer example').slice(0, 8)));\n// Prints: true\nconsole.log(buf.includes('this', 4));\n// Prints: false\n
" }, { "textRaw": "`buf.indexOf(value[, byteOffset][, encoding])`", "type": "method", "name": "indexOf", "meta": { "added": [ "v1.5.0" ], "changes": [ { "version": "v8.0.0", "pr-url": "https://github.com/nodejs/node/pull/10236", "description": "The `value` can now be a `Uint8Array`." }, { "version": [ "v5.7.0", "v4.4.0" ], "pr-url": "https://github.com/nodejs/node/pull/4803", "description": "When `encoding` is being passed, the `byteOffset` parameter is no longer required." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} The index of the first occurrence of `value` in `buf`, or `-1` if `buf` does not contain `value`.", "name": "return", "type": "integer", "desc": "The index of the first occurrence of `value` in `buf`, or `-1` if `buf` does not contain `value`." }, "params": [ { "textRaw": "`value` {string|Buffer|Uint8Array|integer} What to search for.", "name": "value", "type": "string|Buffer|Uint8Array|integer", "desc": "What to search for." }, { "textRaw": "`byteOffset` {integer} Where to begin searching in `buf`. If negative, then offset is calculated from the end of `buf`. **Default:** `0`.", "name": "byteOffset", "type": "integer", "default": "`0`", "desc": "Where to begin searching in `buf`. If negative, then offset is calculated from the end of `buf`." }, { "textRaw": "`encoding` {string} If `value` is a string, this is the encoding used to determine the binary representation of the string that will be searched for in `buf`. **Default:** `'utf8'`.", "name": "encoding", "type": "string", "default": "`'utf8'`", "desc": "If `value` is a string, this is the encoding used to determine the binary representation of the string that will be searched for in `buf`." } ] } ], "desc": "

If value is:

\n\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from('this is a buffer');\n\nconsole.log(buf.indexOf('this'));\n// Prints: 0\nconsole.log(buf.indexOf('is'));\n// Prints: 2\nconsole.log(buf.indexOf(Buffer.from('a buffer')));\n// Prints: 8\nconsole.log(buf.indexOf(97));\n// Prints: 8 (97 is the decimal ASCII value for 'a')\nconsole.log(buf.indexOf(Buffer.from('a buffer example')));\n// Prints: -1\nconsole.log(buf.indexOf(Buffer.from('a buffer example').slice(0, 8)));\n// Prints: 8\n\nconst utf16Buffer = Buffer.from('\\u039a\\u0391\\u03a3\\u03a3\\u0395', 'utf16le');\n\nconsole.log(utf16Buffer.indexOf('\\u03a3', 0, 'utf16le'));\n// Prints: 4\nconsole.log(utf16Buffer.indexOf('\\u03a3', -4, 'utf16le'));\n// Prints: 6\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from('this is a buffer');\n\nconsole.log(buf.indexOf('this'));\n// Prints: 0\nconsole.log(buf.indexOf('is'));\n// Prints: 2\nconsole.log(buf.indexOf(Buffer.from('a buffer')));\n// Prints: 8\nconsole.log(buf.indexOf(97));\n// Prints: 8 (97 is the decimal ASCII value for 'a')\nconsole.log(buf.indexOf(Buffer.from('a buffer example')));\n// Prints: -1\nconsole.log(buf.indexOf(Buffer.from('a buffer example').slice(0, 8)));\n// Prints: 8\n\nconst utf16Buffer = Buffer.from('\\u039a\\u0391\\u03a3\\u03a3\\u0395', 'utf16le');\n\nconsole.log(utf16Buffer.indexOf('\\u03a3', 0, 'utf16le'));\n// Prints: 4\nconsole.log(utf16Buffer.indexOf('\\u03a3', -4, 'utf16le'));\n// Prints: 6\n
\n

If value is not a string, number, or Buffer, this method will throw a\nTypeError. If value is a number, it will be coerced to a valid byte value,\nan integer between 0 and 255.

\n

If byteOffset is not a number, it will be coerced to a number. If the result\nof coercion is NaN or 0, then the entire buffer will be searched. This\nbehavior matches String.prototype.indexOf().

\n
import { Buffer } from 'buffer';\n\nconst b = Buffer.from('abcdef');\n\n// Passing a value that's a number, but not a valid byte.\n// Prints: 2, equivalent to searching for 99 or 'c'.\nconsole.log(b.indexOf(99.9));\nconsole.log(b.indexOf(256 + 99));\n\n// Passing a byteOffset that coerces to NaN or 0.\n// Prints: 1, searching the whole buffer.\nconsole.log(b.indexOf('b', undefined));\nconsole.log(b.indexOf('b', {}));\nconsole.log(b.indexOf('b', null));\nconsole.log(b.indexOf('b', []));\n
\n
const { Buffer } = require('buffer');\n\nconst b = Buffer.from('abcdef');\n\n// Passing a value that's a number, but not a valid byte.\n// Prints: 2, equivalent to searching for 99 or 'c'.\nconsole.log(b.indexOf(99.9));\nconsole.log(b.indexOf(256 + 99));\n\n// Passing a byteOffset that coerces to NaN or 0.\n// Prints: 1, searching the whole buffer.\nconsole.log(b.indexOf('b', undefined));\nconsole.log(b.indexOf('b', {}));\nconsole.log(b.indexOf('b', null));\nconsole.log(b.indexOf('b', []));\n
\n

If value is an empty string or empty Buffer and byteOffset is less\nthan buf.length, byteOffset will be returned. If value is empty and\nbyteOffset is at least buf.length, buf.length will be returned.

" }, { "textRaw": "`buf.keys()`", "type": "method", "name": "keys", "meta": { "added": [ "v1.1.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Iterator}", "name": "return", "type": "Iterator" }, "params": [] } ], "desc": "

Creates and returns an iterator of buf keys (indices).

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from('buffer');\n\nfor (const key of buf.keys()) {\n  console.log(key);\n}\n// Prints:\n//   0\n//   1\n//   2\n//   3\n//   4\n//   5\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from('buffer');\n\nfor (const key of buf.keys()) {\n  console.log(key);\n}\n// Prints:\n//   0\n//   1\n//   2\n//   3\n//   4\n//   5\n
" }, { "textRaw": "`buf.lastIndexOf(value[, byteOffset][, encoding])`", "type": "method", "name": "lastIndexOf", "meta": { "added": [ "v6.0.0" ], "changes": [ { "version": "v8.0.0", "pr-url": "https://github.com/nodejs/node/pull/10236", "description": "The `value` can now be a `Uint8Array`." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} The index of the last occurrence of `value` in `buf`, or `-1` if `buf` does not contain `value`.", "name": "return", "type": "integer", "desc": "The index of the last occurrence of `value` in `buf`, or `-1` if `buf` does not contain `value`." }, "params": [ { "textRaw": "`value` {string|Buffer|Uint8Array|integer} What to search for.", "name": "value", "type": "string|Buffer|Uint8Array|integer", "desc": "What to search for." }, { "textRaw": "`byteOffset` {integer} Where to begin searching in `buf`. If negative, then offset is calculated from the end of `buf`. **Default:** `buf.length - 1`.", "name": "byteOffset", "type": "integer", "default": "`buf.length - 1`", "desc": "Where to begin searching in `buf`. If negative, then offset is calculated from the end of `buf`." }, { "textRaw": "`encoding` {string} If `value` is a string, this is the encoding used to determine the binary representation of the string that will be searched for in `buf`. **Default:** `'utf8'`.", "name": "encoding", "type": "string", "default": "`'utf8'`", "desc": "If `value` is a string, this is the encoding used to determine the binary representation of the string that will be searched for in `buf`." } ] } ], "desc": "

Identical to buf.indexOf(), except the last occurrence of value is found\nrather than the first occurrence.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from('this buffer is a buffer');\n\nconsole.log(buf.lastIndexOf('this'));\n// Prints: 0\nconsole.log(buf.lastIndexOf('buffer'));\n// Prints: 17\nconsole.log(buf.lastIndexOf(Buffer.from('buffer')));\n// Prints: 17\nconsole.log(buf.lastIndexOf(97));\n// Prints: 15 (97 is the decimal ASCII value for 'a')\nconsole.log(buf.lastIndexOf(Buffer.from('yolo')));\n// Prints: -1\nconsole.log(buf.lastIndexOf('buffer', 5));\n// Prints: 5\nconsole.log(buf.lastIndexOf('buffer', 4));\n// Prints: -1\n\nconst utf16Buffer = Buffer.from('\\u039a\\u0391\\u03a3\\u03a3\\u0395', 'utf16le');\n\nconsole.log(utf16Buffer.lastIndexOf('\\u03a3', undefined, 'utf16le'));\n// Prints: 6\nconsole.log(utf16Buffer.lastIndexOf('\\u03a3', -5, 'utf16le'));\n// Prints: 4\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from('this buffer is a buffer');\n\nconsole.log(buf.lastIndexOf('this'));\n// Prints: 0\nconsole.log(buf.lastIndexOf('buffer'));\n// Prints: 17\nconsole.log(buf.lastIndexOf(Buffer.from('buffer')));\n// Prints: 17\nconsole.log(buf.lastIndexOf(97));\n// Prints: 15 (97 is the decimal ASCII value for 'a')\nconsole.log(buf.lastIndexOf(Buffer.from('yolo')));\n// Prints: -1\nconsole.log(buf.lastIndexOf('buffer', 5));\n// Prints: 5\nconsole.log(buf.lastIndexOf('buffer', 4));\n// Prints: -1\n\nconst utf16Buffer = Buffer.from('\\u039a\\u0391\\u03a3\\u03a3\\u0395', 'utf16le');\n\nconsole.log(utf16Buffer.lastIndexOf('\\u03a3', undefined, 'utf16le'));\n// Prints: 6\nconsole.log(utf16Buffer.lastIndexOf('\\u03a3', -5, 'utf16le'));\n// Prints: 4\n
\n

If value is not a string, number, or Buffer, this method will throw a\nTypeError. If value is a number, it will be coerced to a valid byte value,\nan integer between 0 and 255.

\n

If byteOffset is not a number, it will be coerced to a number. Any arguments\nthat coerce to NaN, like {} or undefined, will search the whole buffer.\nThis behavior matches String.prototype.lastIndexOf().

\n
import { Buffer } from 'buffer';\n\nconst b = Buffer.from('abcdef');\n\n// Passing a value that's a number, but not a valid byte.\n// Prints: 2, equivalent to searching for 99 or 'c'.\nconsole.log(b.lastIndexOf(99.9));\nconsole.log(b.lastIndexOf(256 + 99));\n\n// Passing a byteOffset that coerces to NaN.\n// Prints: 1, searching the whole buffer.\nconsole.log(b.lastIndexOf('b', undefined));\nconsole.log(b.lastIndexOf('b', {}));\n\n// Passing a byteOffset that coerces to 0.\n// Prints: -1, equivalent to passing 0.\nconsole.log(b.lastIndexOf('b', null));\nconsole.log(b.lastIndexOf('b', []));\n
\n
const { Buffer } = require('buffer');\n\nconst b = Buffer.from('abcdef');\n\n// Passing a value that's a number, but not a valid byte.\n// Prints: 2, equivalent to searching for 99 or 'c'.\nconsole.log(b.lastIndexOf(99.9));\nconsole.log(b.lastIndexOf(256 + 99));\n\n// Passing a byteOffset that coerces to NaN.\n// Prints: 1, searching the whole buffer.\nconsole.log(b.lastIndexOf('b', undefined));\nconsole.log(b.lastIndexOf('b', {}));\n\n// Passing a byteOffset that coerces to 0.\n// Prints: -1, equivalent to passing 0.\nconsole.log(b.lastIndexOf('b', null));\nconsole.log(b.lastIndexOf('b', []));\n
\n

If value is an empty string or empty Buffer, byteOffset will be returned.

" }, { "textRaw": "`buf.readBigInt64BE([offset])`", "type": "method", "name": "readBigInt64BE", "meta": { "added": [ "v12.0.0", "v10.20.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {bigint}", "name": "return", "type": "bigint" }, "params": [ { "textRaw": "`offset` {integer} Number of bytes to skip before starting to read. Must satisfy: `0 <= offset <= buf.length - 8`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to read. Must satisfy: `0 <= offset <= buf.length - 8`." } ] } ], "desc": "

Reads a signed, big-endian 64-bit integer from buf at the specified offset.

\n

Integers read from a Buffer are interpreted as two's complement signed\nvalues.

" }, { "textRaw": "`buf.readBigInt64LE([offset])`", "type": "method", "name": "readBigInt64LE", "meta": { "added": [ "v12.0.0", "v10.20.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {bigint}", "name": "return", "type": "bigint" }, "params": [ { "textRaw": "`offset` {integer} Number of bytes to skip before starting to read. Must satisfy: `0 <= offset <= buf.length - 8`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to read. Must satisfy: `0 <= offset <= buf.length - 8`." } ] } ], "desc": "

Reads a signed, little-endian 64-bit integer from buf at the specified\noffset.

\n

Integers read from a Buffer are interpreted as two's complement signed\nvalues.

" }, { "textRaw": "`buf.readBigUInt64BE([offset])`", "type": "method", "name": "readBigUInt64BE", "meta": { "added": [ "v12.0.0", "v10.20.0" ], "changes": [ { "version": [ "v14.10.0", "v12.19.0" ], "pr-url": "https://github.com/nodejs/node/pull/34960", "description": "This function is also available as `buf.readBigUint64BE()`." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {bigint}", "name": "return", "type": "bigint" }, "params": [ { "textRaw": "`offset` {integer} Number of bytes to skip before starting to read. Must satisfy: `0 <= offset <= buf.length - 8`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to read. Must satisfy: `0 <= offset <= buf.length - 8`." } ] } ], "desc": "

Reads an unsigned, big-endian 64-bit integer from buf at the specified\noffset.

\n

This function is also available under the readBigUint64BE alias.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff]);\n\nconsole.log(buf.readBigUInt64BE(0));\n// Prints: 4294967295n\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff]);\n\nconsole.log(buf.readBigUInt64BE(0));\n// Prints: 4294967295n\n
" }, { "textRaw": "`buf.readBigUInt64LE([offset])`", "type": "method", "name": "readBigUInt64LE", "meta": { "added": [ "v12.0.0", "v10.20.0" ], "changes": [ { "version": [ "v14.10.0", "v12.19.0" ], "pr-url": "https://github.com/nodejs/node/pull/34960", "description": "This function is also available as `buf.readBigUint64LE()`." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {bigint}", "name": "return", "type": "bigint" }, "params": [ { "textRaw": "`offset` {integer} Number of bytes to skip before starting to read. Must satisfy: `0 <= offset <= buf.length - 8`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to read. Must satisfy: `0 <= offset <= buf.length - 8`." } ] } ], "desc": "

Reads an unsigned, little-endian 64-bit integer from buf at the specified\noffset.

\n

This function is also available under the readBigUint64LE alias.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff]);\n\nconsole.log(buf.readBigUInt64LE(0));\n// Prints: 18446744069414584320n\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff]);\n\nconsole.log(buf.readBigUInt64LE(0));\n// Prints: 18446744069414584320n\n
" }, { "textRaw": "`buf.readDoubleBE([offset])`", "type": "method", "name": "readDoubleBE", "meta": { "added": [ "v0.11.15" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {number}", "name": "return", "type": "number" }, "params": [ { "textRaw": "`offset` {integer} Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 8`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 8`." } ] } ], "desc": "

Reads a 64-bit, big-endian double from buf at the specified offset.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);\n\nconsole.log(buf.readDoubleBE(0));\n// Prints: 8.20788039913184e-304\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);\n\nconsole.log(buf.readDoubleBE(0));\n// Prints: 8.20788039913184e-304\n
" }, { "textRaw": "`buf.readDoubleLE([offset])`", "type": "method", "name": "readDoubleLE", "meta": { "added": [ "v0.11.15" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {number}", "name": "return", "type": "number" }, "params": [ { "textRaw": "`offset` {integer} Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 8`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 8`." } ] } ], "desc": "

Reads a 64-bit, little-endian double from buf at the specified offset.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);\n\nconsole.log(buf.readDoubleLE(0));\n// Prints: 5.447603722011605e-270\nconsole.log(buf.readDoubleLE(1));\n// Throws ERR_OUT_OF_RANGE.\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);\n\nconsole.log(buf.readDoubleLE(0));\n// Prints: 5.447603722011605e-270\nconsole.log(buf.readDoubleLE(1));\n// Throws ERR_OUT_OF_RANGE.\n
" }, { "textRaw": "`buf.readFloatBE([offset])`", "type": "method", "name": "readFloatBE", "meta": { "added": [ "v0.11.15" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {number}", "name": "return", "type": "number" }, "params": [ { "textRaw": "`offset` {integer} Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 4`." } ] } ], "desc": "

Reads a 32-bit, big-endian float from buf at the specified offset.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from([1, 2, 3, 4]);\n\nconsole.log(buf.readFloatBE(0));\n// Prints: 2.387939260590663e-38\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from([1, 2, 3, 4]);\n\nconsole.log(buf.readFloatBE(0));\n// Prints: 2.387939260590663e-38\n
" }, { "textRaw": "`buf.readFloatLE([offset])`", "type": "method", "name": "readFloatLE", "meta": { "added": [ "v0.11.15" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {number}", "name": "return", "type": "number" }, "params": [ { "textRaw": "`offset` {integer} Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 4`." } ] } ], "desc": "

Reads a 32-bit, little-endian float from buf at the specified offset.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from([1, 2, 3, 4]);\n\nconsole.log(buf.readFloatLE(0));\n// Prints: 1.539989614439558e-36\nconsole.log(buf.readFloatLE(1));\n// Throws ERR_OUT_OF_RANGE.\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from([1, 2, 3, 4]);\n\nconsole.log(buf.readFloatLE(0));\n// Prints: 1.539989614439558e-36\nconsole.log(buf.readFloatLE(1));\n// Throws ERR_OUT_OF_RANGE.\n
" }, { "textRaw": "`buf.readInt8([offset])`", "type": "method", "name": "readInt8", "meta": { "added": [ "v0.5.0" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer}", "name": "return", "type": "integer" }, "params": [ { "textRaw": "`offset` {integer} Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 1`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 1`." } ] } ], "desc": "

Reads a signed 8-bit integer from buf at the specified offset.

\n

Integers read from a Buffer are interpreted as two's complement signed values.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from([-1, 5]);\n\nconsole.log(buf.readInt8(0));\n// Prints: -1\nconsole.log(buf.readInt8(1));\n// Prints: 5\nconsole.log(buf.readInt8(2));\n// Throws ERR_OUT_OF_RANGE.\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from([-1, 5]);\n\nconsole.log(buf.readInt8(0));\n// Prints: -1\nconsole.log(buf.readInt8(1));\n// Prints: 5\nconsole.log(buf.readInt8(2));\n// Throws ERR_OUT_OF_RANGE.\n
" }, { "textRaw": "`buf.readInt16BE([offset])`", "type": "method", "name": "readInt16BE", "meta": { "added": [ "v0.5.5" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer}", "name": "return", "type": "integer" }, "params": [ { "textRaw": "`offset` {integer} Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 2`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 2`." } ] } ], "desc": "

Reads a signed, big-endian 16-bit integer from buf at the specified offset.

\n

Integers read from a Buffer are interpreted as two's complement signed values.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from([0, 5]);\n\nconsole.log(buf.readInt16BE(0));\n// Prints: 5\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from([0, 5]);\n\nconsole.log(buf.readInt16BE(0));\n// Prints: 5\n
" }, { "textRaw": "`buf.readInt16LE([offset])`", "type": "method", "name": "readInt16LE", "meta": { "added": [ "v0.5.5" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer}", "name": "return", "type": "integer" }, "params": [ { "textRaw": "`offset` {integer} Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 2`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 2`." } ] } ], "desc": "

Reads a signed, little-endian 16-bit integer from buf at the specified\noffset.

\n

Integers read from a Buffer are interpreted as two's complement signed values.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from([0, 5]);\n\nconsole.log(buf.readInt16LE(0));\n// Prints: 1280\nconsole.log(buf.readInt16LE(1));\n// Throws ERR_OUT_OF_RANGE.\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from([0, 5]);\n\nconsole.log(buf.readInt16LE(0));\n// Prints: 1280\nconsole.log(buf.readInt16LE(1));\n// Throws ERR_OUT_OF_RANGE.\n
" }, { "textRaw": "`buf.readInt32BE([offset])`", "type": "method", "name": "readInt32BE", "meta": { "added": [ "v0.5.5" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer}", "name": "return", "type": "integer" }, "params": [ { "textRaw": "`offset` {integer} Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 4`." } ] } ], "desc": "

Reads a signed, big-endian 32-bit integer from buf at the specified offset.

\n

Integers read from a Buffer are interpreted as two's complement signed values.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from([0, 0, 0, 5]);\n\nconsole.log(buf.readInt32BE(0));\n// Prints: 5\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from([0, 0, 0, 5]);\n\nconsole.log(buf.readInt32BE(0));\n// Prints: 5\n
" }, { "textRaw": "`buf.readInt32LE([offset])`", "type": "method", "name": "readInt32LE", "meta": { "added": [ "v0.5.5" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer}", "name": "return", "type": "integer" }, "params": [ { "textRaw": "`offset` {integer} Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 4`." } ] } ], "desc": "

Reads a signed, little-endian 32-bit integer from buf at the specified\noffset.

\n

Integers read from a Buffer are interpreted as two's complement signed values.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from([0, 0, 0, 5]);\n\nconsole.log(buf.readInt32LE(0));\n// Prints: 83886080\nconsole.log(buf.readInt32LE(1));\n// Throws ERR_OUT_OF_RANGE.\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from([0, 0, 0, 5]);\n\nconsole.log(buf.readInt32LE(0));\n// Prints: 83886080\nconsole.log(buf.readInt32LE(1));\n// Throws ERR_OUT_OF_RANGE.\n
" }, { "textRaw": "`buf.readIntBE(offset, byteLength)`", "type": "method", "name": "readIntBE", "meta": { "added": [ "v0.11.15" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset and `byteLength` to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer}", "name": "return", "type": "integer" }, "params": [ { "textRaw": "`offset` {integer} Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - byteLength`.", "name": "offset", "type": "integer", "desc": "Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - byteLength`." }, { "textRaw": "`byteLength` {integer} Number of bytes to read. Must satisfy `0 < byteLength <= 6`.", "name": "byteLength", "type": "integer", "desc": "Number of bytes to read. Must satisfy `0 < byteLength <= 6`." } ] } ], "desc": "

Reads byteLength number of bytes from buf at the specified offset\nand interprets the result as a big-endian, two's complement signed value\nsupporting up to 48 bits of accuracy.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);\n\nconsole.log(buf.readIntBE(0, 6).toString(16));\n// Prints: 1234567890ab\nconsole.log(buf.readIntBE(1, 6).toString(16));\n// Throws ERR_OUT_OF_RANGE.\nconsole.log(buf.readIntBE(1, 0).toString(16));\n// Throws ERR_OUT_OF_RANGE.\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);\n\nconsole.log(buf.readIntBE(0, 6).toString(16));\n// Prints: 1234567890ab\nconsole.log(buf.readIntBE(1, 6).toString(16));\n// Throws ERR_OUT_OF_RANGE.\nconsole.log(buf.readIntBE(1, 0).toString(16));\n// Throws ERR_OUT_OF_RANGE.\n
" }, { "textRaw": "`buf.readIntLE(offset, byteLength)`", "type": "method", "name": "readIntLE", "meta": { "added": [ "v0.11.15" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset and `byteLength` to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer}", "name": "return", "type": "integer" }, "params": [ { "textRaw": "`offset` {integer} Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - byteLength`.", "name": "offset", "type": "integer", "desc": "Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - byteLength`." }, { "textRaw": "`byteLength` {integer} Number of bytes to read. Must satisfy `0 < byteLength <= 6`.", "name": "byteLength", "type": "integer", "desc": "Number of bytes to read. Must satisfy `0 < byteLength <= 6`." } ] } ], "desc": "

Reads byteLength number of bytes from buf at the specified offset\nand interprets the result as a little-endian, two's complement signed value\nsupporting up to 48 bits of accuracy.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);\n\nconsole.log(buf.readIntLE(0, 6).toString(16));\n// Prints: -546f87a9cbee\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);\n\nconsole.log(buf.readIntLE(0, 6).toString(16));\n// Prints: -546f87a9cbee\n
" }, { "textRaw": "`buf.readUInt8([offset])`", "type": "method", "name": "readUInt8", "meta": { "added": [ "v0.5.0" ], "changes": [ { "version": [ "v14.9.0", "v12.19.0" ], "pr-url": "https://github.com/nodejs/node/pull/34729", "description": "This function is also available as `buf.readUint8()`." }, { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer}", "name": "return", "type": "integer" }, "params": [ { "textRaw": "`offset` {integer} Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 1`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 1`." } ] } ], "desc": "

Reads an unsigned 8-bit integer from buf at the specified offset.

\n

This function is also available under the readUint8 alias.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from([1, -2]);\n\nconsole.log(buf.readUInt8(0));\n// Prints: 1\nconsole.log(buf.readUInt8(1));\n// Prints: 254\nconsole.log(buf.readUInt8(2));\n// Throws ERR_OUT_OF_RANGE.\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from([1, -2]);\n\nconsole.log(buf.readUInt8(0));\n// Prints: 1\nconsole.log(buf.readUInt8(1));\n// Prints: 254\nconsole.log(buf.readUInt8(2));\n// Throws ERR_OUT_OF_RANGE.\n
" }, { "textRaw": "`buf.readUInt16BE([offset])`", "type": "method", "name": "readUInt16BE", "meta": { "added": [ "v0.5.5" ], "changes": [ { "version": [ "v14.9.0", "v12.19.0" ], "pr-url": "https://github.com/nodejs/node/pull/34729", "description": "This function is also available as `buf.readUint16BE()`." }, { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer}", "name": "return", "type": "integer" }, "params": [ { "textRaw": "`offset` {integer} Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 2`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 2`." } ] } ], "desc": "

Reads an unsigned, big-endian 16-bit integer from buf at the specified\noffset.

\n

This function is also available under the readUint16BE alias.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from([0x12, 0x34, 0x56]);\n\nconsole.log(buf.readUInt16BE(0).toString(16));\n// Prints: 1234\nconsole.log(buf.readUInt16BE(1).toString(16));\n// Prints: 3456\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from([0x12, 0x34, 0x56]);\n\nconsole.log(buf.readUInt16BE(0).toString(16));\n// Prints: 1234\nconsole.log(buf.readUInt16BE(1).toString(16));\n// Prints: 3456\n
" }, { "textRaw": "`buf.readUInt16LE([offset])`", "type": "method", "name": "readUInt16LE", "meta": { "added": [ "v0.5.5" ], "changes": [ { "version": [ "v14.9.0", "v12.19.0" ], "pr-url": "https://github.com/nodejs/node/pull/34729", "description": "This function is also available as `buf.readUint16LE()`." }, { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer}", "name": "return", "type": "integer" }, "params": [ { "textRaw": "`offset` {integer} Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 2`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 2`." } ] } ], "desc": "

Reads an unsigned, little-endian 16-bit integer from buf at the specified\noffset.

\n

This function is also available under the readUint16LE alias.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from([0x12, 0x34, 0x56]);\n\nconsole.log(buf.readUInt16LE(0).toString(16));\n// Prints: 3412\nconsole.log(buf.readUInt16LE(1).toString(16));\n// Prints: 5634\nconsole.log(buf.readUInt16LE(2).toString(16));\n// Throws ERR_OUT_OF_RANGE.\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from([0x12, 0x34, 0x56]);\n\nconsole.log(buf.readUInt16LE(0).toString(16));\n// Prints: 3412\nconsole.log(buf.readUInt16LE(1).toString(16));\n// Prints: 5634\nconsole.log(buf.readUInt16LE(2).toString(16));\n// Throws ERR_OUT_OF_RANGE.\n
" }, { "textRaw": "`buf.readUInt32BE([offset])`", "type": "method", "name": "readUInt32BE", "meta": { "added": [ "v0.5.5" ], "changes": [ { "version": [ "v14.9.0", "v12.19.0" ], "pr-url": "https://github.com/nodejs/node/pull/34729", "description": "This function is also available as `buf.readUint32BE()`." }, { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer}", "name": "return", "type": "integer" }, "params": [ { "textRaw": "`offset` {integer} Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 4`." } ] } ], "desc": "

Reads an unsigned, big-endian 32-bit integer from buf at the specified\noffset.

\n

This function is also available under the readUint32BE alias.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from([0x12, 0x34, 0x56, 0x78]);\n\nconsole.log(buf.readUInt32BE(0).toString(16));\n// Prints: 12345678\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from([0x12, 0x34, 0x56, 0x78]);\n\nconsole.log(buf.readUInt32BE(0).toString(16));\n// Prints: 12345678\n
" }, { "textRaw": "`buf.readUInt32LE([offset])`", "type": "method", "name": "readUInt32LE", "meta": { "added": [ "v0.5.5" ], "changes": [ { "version": [ "v14.9.0", "v12.19.0" ], "pr-url": "https://github.com/nodejs/node/pull/34729", "description": "This function is also available as `buf.readUint32LE()`." }, { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer}", "name": "return", "type": "integer" }, "params": [ { "textRaw": "`offset` {integer} Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 4`." } ] } ], "desc": "

Reads an unsigned, little-endian 32-bit integer from buf at the specified\noffset.

\n

This function is also available under the readUint32LE alias.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from([0x12, 0x34, 0x56, 0x78]);\n\nconsole.log(buf.readUInt32LE(0).toString(16));\n// Prints: 78563412\nconsole.log(buf.readUInt32LE(1).toString(16));\n// Throws ERR_OUT_OF_RANGE.\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from([0x12, 0x34, 0x56, 0x78]);\n\nconsole.log(buf.readUInt32LE(0).toString(16));\n// Prints: 78563412\nconsole.log(buf.readUInt32LE(1).toString(16));\n// Throws ERR_OUT_OF_RANGE.\n
" }, { "textRaw": "`buf.readUIntBE(offset, byteLength)`", "type": "method", "name": "readUIntBE", "meta": { "added": [ "v0.11.15" ], "changes": [ { "version": [ "v14.9.0", "v12.19.0" ], "pr-url": "https://github.com/nodejs/node/pull/34729", "description": "This function is also available as `buf.readUintBE()`." }, { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset and `byteLength` to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer}", "name": "return", "type": "integer" }, "params": [ { "textRaw": "`offset` {integer} Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - byteLength`.", "name": "offset", "type": "integer", "desc": "Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - byteLength`." }, { "textRaw": "`byteLength` {integer} Number of bytes to read. Must satisfy `0 < byteLength <= 6`.", "name": "byteLength", "type": "integer", "desc": "Number of bytes to read. Must satisfy `0 < byteLength <= 6`." } ] } ], "desc": "

Reads byteLength number of bytes from buf at the specified offset\nand interprets the result as an unsigned big-endian integer supporting\nup to 48 bits of accuracy.

\n

This function is also available under the readUintBE alias.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);\n\nconsole.log(buf.readUIntBE(0, 6).toString(16));\n// Prints: 1234567890ab\nconsole.log(buf.readUIntBE(1, 6).toString(16));\n// Throws ERR_OUT_OF_RANGE.\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);\n\nconsole.log(buf.readUIntBE(0, 6).toString(16));\n// Prints: 1234567890ab\nconsole.log(buf.readUIntBE(1, 6).toString(16));\n// Throws ERR_OUT_OF_RANGE.\n
" }, { "textRaw": "`buf.readUIntLE(offset, byteLength)`", "type": "method", "name": "readUIntLE", "meta": { "added": [ "v0.11.15" ], "changes": [ { "version": [ "v14.9.0", "v12.19.0" ], "pr-url": "https://github.com/nodejs/node/pull/34729", "description": "This function is also available as `buf.readUintLE()`." }, { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset and `byteLength` to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer}", "name": "return", "type": "integer" }, "params": [ { "textRaw": "`offset` {integer} Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - byteLength`.", "name": "offset", "type": "integer", "desc": "Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - byteLength`." }, { "textRaw": "`byteLength` {integer} Number of bytes to read. Must satisfy `0 < byteLength <= 6`.", "name": "byteLength", "type": "integer", "desc": "Number of bytes to read. Must satisfy `0 < byteLength <= 6`." } ] } ], "desc": "

Reads byteLength number of bytes from buf at the specified offset\nand interprets the result as an unsigned, little-endian integer supporting\nup to 48 bits of accuracy.

\n

This function is also available under the readUintLE alias.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);\n\nconsole.log(buf.readUIntLE(0, 6).toString(16));\n// Prints: ab9078563412\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]);\n\nconsole.log(buf.readUIntLE(0, 6).toString(16));\n// Prints: ab9078563412\n
" }, { "textRaw": "`buf.subarray([start[, end]])`", "type": "method", "name": "subarray", "meta": { "added": [ "v3.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Buffer}", "name": "return", "type": "Buffer" }, "params": [ { "textRaw": "`start` {integer} Where the new `Buffer` will start. **Default:** `0`.", "name": "start", "type": "integer", "default": "`0`", "desc": "Where the new `Buffer` will start." }, { "textRaw": "`end` {integer} Where the new `Buffer` will end (not inclusive). **Default:** [`buf.length`][].", "name": "end", "type": "integer", "default": "[`buf.length`][]", "desc": "Where the new `Buffer` will end (not inclusive)." } ] } ], "desc": "

Returns a new Buffer that references the same memory as the original, but\noffset and cropped by the start and end indices.

\n

Specifying end greater than buf.length will return the same result as\nthat of end equal to buf.length.

\n

This method is inherited from TypedArray.prototype.subarray().

\n

Modifying the new Buffer slice will modify the memory in the original Buffer\nbecause the allocated memory of the two objects overlap.

\n
import { Buffer } from 'buffer';\n\n// Create a `Buffer` with the ASCII alphabet, take a slice, and modify one byte\n// from the original `Buffer`.\n\nconst buf1 = Buffer.allocUnsafe(26);\n\nfor (let i = 0; i < 26; i++) {\n  // 97 is the decimal ASCII value for 'a'.\n  buf1[i] = i + 97;\n}\n\nconst buf2 = buf1.subarray(0, 3);\n\nconsole.log(buf2.toString('ascii', 0, buf2.length));\n// Prints: abc\n\nbuf1[0] = 33;\n\nconsole.log(buf2.toString('ascii', 0, buf2.length));\n// Prints: !bc\n
\n
const { Buffer } = require('buffer');\n\n// Create a `Buffer` with the ASCII alphabet, take a slice, and modify one byte\n// from the original `Buffer`.\n\nconst buf1 = Buffer.allocUnsafe(26);\n\nfor (let i = 0; i < 26; i++) {\n  // 97 is the decimal ASCII value for 'a'.\n  buf1[i] = i + 97;\n}\n\nconst buf2 = buf1.subarray(0, 3);\n\nconsole.log(buf2.toString('ascii', 0, buf2.length));\n// Prints: abc\n\nbuf1[0] = 33;\n\nconsole.log(buf2.toString('ascii', 0, buf2.length));\n// Prints: !bc\n
\n

Specifying negative indexes causes the slice to be generated relative to the\nend of buf rather than the beginning.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from('buffer');\n\nconsole.log(buf.subarray(-6, -1).toString());\n// Prints: buffe\n// (Equivalent to buf.subarray(0, 5).)\n\nconsole.log(buf.subarray(-6, -2).toString());\n// Prints: buff\n// (Equivalent to buf.subarray(0, 4).)\n\nconsole.log(buf.subarray(-5, -2).toString());\n// Prints: uff\n// (Equivalent to buf.subarray(1, 4).)\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from('buffer');\n\nconsole.log(buf.subarray(-6, -1).toString());\n// Prints: buffe\n// (Equivalent to buf.subarray(0, 5).)\n\nconsole.log(buf.subarray(-6, -2).toString());\n// Prints: buff\n// (Equivalent to buf.subarray(0, 4).)\n\nconsole.log(buf.subarray(-5, -2).toString());\n// Prints: uff\n// (Equivalent to buf.subarray(1, 4).)\n
" }, { "textRaw": "`buf.slice([start[, end]])`", "type": "method", "name": "slice", "meta": { "added": [ "v0.3.0" ], "changes": [ { "version": [ "v7.1.0", "v6.9.2" ], "pr-url": "https://github.com/nodejs/node/pull/9341", "description": "Coercing the offsets to integers now handles values outside the 32-bit integer range properly." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/9101", "description": "All offsets are now coerced to integers before doing any calculations with them." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {Buffer}", "name": "return", "type": "Buffer" }, "params": [ { "textRaw": "`start` {integer} Where the new `Buffer` will start. **Default:** `0`.", "name": "start", "type": "integer", "default": "`0`", "desc": "Where the new `Buffer` will start." }, { "textRaw": "`end` {integer} Where the new `Buffer` will end (not inclusive). **Default:** [`buf.length`][].", "name": "end", "type": "integer", "default": "[`buf.length`][]", "desc": "Where the new `Buffer` will end (not inclusive)." } ] } ], "desc": "

Returns a new Buffer that references the same memory as the original, but\noffset and cropped by the start and end indices.

\n

This is the same behavior as buf.subarray().

\n

This method is not compatible with the Uint8Array.prototype.slice(),\nwhich is a superclass of Buffer. To copy the slice, use\nUint8Array.prototype.slice().

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from('buffer');\n\nconst copiedBuf = Uint8Array.prototype.slice.call(buf);\ncopiedBuf[0]++;\nconsole.log(copiedBuf.toString());\n// Prints: cuffer\n\nconsole.log(buf.toString());\n// Prints: buffer\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from('buffer');\n\nconst copiedBuf = Uint8Array.prototype.slice.call(buf);\ncopiedBuf[0]++;\nconsole.log(copiedBuf.toString());\n// Prints: cuffer\n\nconsole.log(buf.toString());\n// Prints: buffer\n
" }, { "textRaw": "`buf.swap16()`", "type": "method", "name": "swap16", "meta": { "added": [ "v5.10.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Buffer} A reference to `buf`.", "name": "return", "type": "Buffer", "desc": "A reference to `buf`." }, "params": [] } ], "desc": "

Interprets buf as an array of unsigned 16-bit integers and swaps the\nbyte order in-place. Throws ERR_INVALID_BUFFER_SIZE if buf.length\nis not a multiple of 2.

\n
import { Buffer } from 'buffer';\n\nconst buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);\n\nconsole.log(buf1);\n// Prints: <Buffer 01 02 03 04 05 06 07 08>\n\nbuf1.swap16();\n\nconsole.log(buf1);\n// Prints: <Buffer 02 01 04 03 06 05 08 07>\n\nconst buf2 = Buffer.from([0x1, 0x2, 0x3]);\n\nbuf2.swap16();\n// Throws ERR_INVALID_BUFFER_SIZE.\n
\n
const { Buffer } = require('buffer');\n\nconst buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);\n\nconsole.log(buf1);\n// Prints: <Buffer 01 02 03 04 05 06 07 08>\n\nbuf1.swap16();\n\nconsole.log(buf1);\n// Prints: <Buffer 02 01 04 03 06 05 08 07>\n\nconst buf2 = Buffer.from([0x1, 0x2, 0x3]);\n\nbuf2.swap16();\n// Throws ERR_INVALID_BUFFER_SIZE.\n
\n

One convenient use of buf.swap16() is to perform a fast in-place conversion\nbetween UTF-16 little-endian and UTF-16 big-endian:

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from('This is little-endian UTF-16', 'utf16le');\nbuf.swap16(); // Convert to big-endian UTF-16 text.\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from('This is little-endian UTF-16', 'utf16le');\nbuf.swap16(); // Convert to big-endian UTF-16 text.\n
" }, { "textRaw": "`buf.swap32()`", "type": "method", "name": "swap32", "meta": { "added": [ "v5.10.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Buffer} A reference to `buf`.", "name": "return", "type": "Buffer", "desc": "A reference to `buf`." }, "params": [] } ], "desc": "

Interprets buf as an array of unsigned 32-bit integers and swaps the\nbyte order in-place. Throws ERR_INVALID_BUFFER_SIZE if buf.length\nis not a multiple of 4.

\n
import { Buffer } from 'buffer';\n\nconst buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);\n\nconsole.log(buf1);\n// Prints: <Buffer 01 02 03 04 05 06 07 08>\n\nbuf1.swap32();\n\nconsole.log(buf1);\n// Prints: <Buffer 04 03 02 01 08 07 06 05>\n\nconst buf2 = Buffer.from([0x1, 0x2, 0x3]);\n\nbuf2.swap32();\n// Throws ERR_INVALID_BUFFER_SIZE.\n
\n
const { Buffer } = require('buffer');\n\nconst buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);\n\nconsole.log(buf1);\n// Prints: <Buffer 01 02 03 04 05 06 07 08>\n\nbuf1.swap32();\n\nconsole.log(buf1);\n// Prints: <Buffer 04 03 02 01 08 07 06 05>\n\nconst buf2 = Buffer.from([0x1, 0x2, 0x3]);\n\nbuf2.swap32();\n// Throws ERR_INVALID_BUFFER_SIZE.\n
" }, { "textRaw": "`buf.swap64()`", "type": "method", "name": "swap64", "meta": { "added": [ "v6.3.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Buffer} A reference to `buf`.", "name": "return", "type": "Buffer", "desc": "A reference to `buf`." }, "params": [] } ], "desc": "

Interprets buf as an array of 64-bit numbers and swaps byte order in-place.\nThrows ERR_INVALID_BUFFER_SIZE if buf.length is not a multiple of 8.

\n
import { Buffer } from 'buffer';\n\nconst buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);\n\nconsole.log(buf1);\n// Prints: <Buffer 01 02 03 04 05 06 07 08>\n\nbuf1.swap64();\n\nconsole.log(buf1);\n// Prints: <Buffer 08 07 06 05 04 03 02 01>\n\nconst buf2 = Buffer.from([0x1, 0x2, 0x3]);\n\nbuf2.swap64();\n// Throws ERR_INVALID_BUFFER_SIZE.\n
\n
const { Buffer } = require('buffer');\n\nconst buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);\n\nconsole.log(buf1);\n// Prints: <Buffer 01 02 03 04 05 06 07 08>\n\nbuf1.swap64();\n\nconsole.log(buf1);\n// Prints: <Buffer 08 07 06 05 04 03 02 01>\n\nconst buf2 = Buffer.from([0x1, 0x2, 0x3]);\n\nbuf2.swap64();\n// Throws ERR_INVALID_BUFFER_SIZE.\n
" }, { "textRaw": "`buf.toJSON()`", "type": "method", "name": "toJSON", "meta": { "added": [ "v0.9.2" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Object}", "name": "return", "type": "Object" }, "params": [] } ], "desc": "

Returns a JSON representation of buf. JSON.stringify() implicitly calls\nthis function when stringifying a Buffer instance.

\n

Buffer.from() accepts objects in the format returned from this method.\nIn particular, Buffer.from(buf.toJSON()) works like Buffer.from(buf).

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5]);\nconst json = JSON.stringify(buf);\n\nconsole.log(json);\n// Prints: {\"type\":\"Buffer\",\"data\":[1,2,3,4,5]}\n\nconst copy = JSON.parse(json, (key, value) => {\n  return value && value.type === 'Buffer' ?\n    Buffer.from(value) :\n    value;\n});\n\nconsole.log(copy);\n// Prints: <Buffer 01 02 03 04 05>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5]);\nconst json = JSON.stringify(buf);\n\nconsole.log(json);\n// Prints: {\"type\":\"Buffer\",\"data\":[1,2,3,4,5]}\n\nconst copy = JSON.parse(json, (key, value) => {\n  return value && value.type === 'Buffer' ?\n    Buffer.from(value) :\n    value;\n});\n\nconsole.log(copy);\n// Prints: <Buffer 01 02 03 04 05>\n
" }, { "textRaw": "`buf.toString([encoding[, start[, end]]])`", "type": "method", "name": "toString", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {string}", "name": "return", "type": "string" }, "params": [ { "textRaw": "`encoding` {string} The character encoding to use. **Default:** `'utf8'`.", "name": "encoding", "type": "string", "default": "`'utf8'`", "desc": "The character encoding to use." }, { "textRaw": "`start` {integer} The byte offset to start decoding at. **Default:** `0`.", "name": "start", "type": "integer", "default": "`0`", "desc": "The byte offset to start decoding at." }, { "textRaw": "`end` {integer} The byte offset to stop decoding at (not inclusive). **Default:** [`buf.length`][].", "name": "end", "type": "integer", "default": "[`buf.length`][]", "desc": "The byte offset to stop decoding at (not inclusive)." } ] } ], "desc": "

Decodes buf to a string according to the specified character encoding in\nencoding. start and end may be passed to decode only a subset of buf.

\n

If encoding is 'utf8' and a byte sequence in the input is not valid UTF-8,\nthen each invalid byte is replaced with the replacement character U+FFFD.

\n

The maximum length of a string instance (in UTF-16 code units) is available\nas buffer.constants.MAX_STRING_LENGTH.

\n
import { Buffer } from 'buffer';\n\nconst buf1 = Buffer.allocUnsafe(26);\n\nfor (let i = 0; i < 26; i++) {\n  // 97 is the decimal ASCII value for 'a'.\n  buf1[i] = i + 97;\n}\n\nconsole.log(buf1.toString('utf8'));\n// Prints: abcdefghijklmnopqrstuvwxyz\nconsole.log(buf1.toString('utf8', 0, 5));\n// Prints: abcde\n\nconst buf2 = Buffer.from('tést');\n\nconsole.log(buf2.toString('hex'));\n// Prints: 74c3a97374\nconsole.log(buf2.toString('utf8', 0, 3));\n// Prints: té\nconsole.log(buf2.toString(undefined, 0, 3));\n// Prints: té\n
\n
const { Buffer } = require('buffer');\n\nconst buf1 = Buffer.allocUnsafe(26);\n\nfor (let i = 0; i < 26; i++) {\n  // 97 is the decimal ASCII value for 'a'.\n  buf1[i] = i + 97;\n}\n\nconsole.log(buf1.toString('utf8'));\n// Prints: abcdefghijklmnopqrstuvwxyz\nconsole.log(buf1.toString('utf8', 0, 5));\n// Prints: abcde\n\nconst buf2 = Buffer.from('tést');\n\nconsole.log(buf2.toString('hex'));\n// Prints: 74c3a97374\nconsole.log(buf2.toString('utf8', 0, 3));\n// Prints: té\nconsole.log(buf2.toString(undefined, 0, 3));\n// Prints: té\n
" }, { "textRaw": "`buf.values()`", "type": "method", "name": "values", "meta": { "added": [ "v1.1.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Iterator}", "name": "return", "type": "Iterator" }, "params": [] } ], "desc": "

Creates and returns an iterator for buf values (bytes). This function is\ncalled automatically when a Buffer is used in a for..of statement.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.from('buffer');\n\nfor (const value of buf.values()) {\n  console.log(value);\n}\n// Prints:\n//   98\n//   117\n//   102\n//   102\n//   101\n//   114\n\nfor (const value of buf) {\n  console.log(value);\n}\n// Prints:\n//   98\n//   117\n//   102\n//   102\n//   101\n//   114\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.from('buffer');\n\nfor (const value of buf.values()) {\n  console.log(value);\n}\n// Prints:\n//   98\n//   117\n//   102\n//   102\n//   101\n//   114\n\nfor (const value of buf) {\n  console.log(value);\n}\n// Prints:\n//   98\n//   117\n//   102\n//   102\n//   101\n//   114\n
" }, { "textRaw": "`buf.write(string[, offset[, length]][, encoding])`", "type": "method", "name": "write", "meta": { "added": [ "v0.1.90" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} Number of bytes written.", "name": "return", "type": "integer", "desc": "Number of bytes written." }, "params": [ { "textRaw": "`string` {string} String to write to `buf`.", "name": "string", "type": "string", "desc": "String to write to `buf`." }, { "textRaw": "`offset` {integer} Number of bytes to skip before starting to write `string`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to write `string`." }, { "textRaw": "`length` {integer} Maximum number of bytes to write (written bytes will not exceed `buf.length - offset`). **Default:** `buf.length - offset`.", "name": "length", "type": "integer", "default": "`buf.length - offset`", "desc": "Maximum number of bytes to write (written bytes will not exceed `buf.length - offset`)." }, { "textRaw": "`encoding` {string} The character encoding of `string`. **Default:** `'utf8'`.", "name": "encoding", "type": "string", "default": "`'utf8'`", "desc": "The character encoding of `string`." } ] } ], "desc": "

Writes string to buf at offset according to the character encoding in\nencoding. The length parameter is the number of bytes to write. If buf did\nnot contain enough space to fit the entire string, only part of string will be\nwritten. However, partially encoded characters will not be written.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.alloc(256);\n\nconst len = buf.write('\\u00bd + \\u00bc = \\u00be', 0);\n\nconsole.log(`${len} bytes: ${buf.toString('utf8', 0, len)}`);\n// Prints: 12 bytes: ½ + ¼ = ¾\n\nconst buffer = Buffer.alloc(10);\n\nconst length = buffer.write('abcd', 8);\n\nconsole.log(`${length} bytes: ${buffer.toString('utf8', 8, 10)}`);\n// Prints: 2 bytes : ab\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.alloc(256);\n\nconst len = buf.write('\\u00bd + \\u00bc = \\u00be', 0);\n\nconsole.log(`${len} bytes: ${buf.toString('utf8', 0, len)}`);\n// Prints: 12 bytes: ½ + ¼ = ¾\n\nconst buffer = Buffer.alloc(10);\n\nconst length = buffer.write('abcd', 8);\n\nconsole.log(`${length} bytes: ${buffer.toString('utf8', 8, 10)}`);\n// Prints: 2 bytes : ab\n
" }, { "textRaw": "`buf.writeBigInt64BE(value[, offset])`", "type": "method", "name": "writeBigInt64BE", "meta": { "added": [ "v12.0.0", "v10.20.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} `offset` plus the number of bytes written.", "name": "return", "type": "integer", "desc": "`offset` plus the number of bytes written." }, "params": [ { "textRaw": "`value` {bigint} Number to be written to `buf`.", "name": "value", "type": "bigint", "desc": "Number to be written to `buf`." }, { "textRaw": "`offset` {integer} Number of bytes to skip before starting to write. Must satisfy: `0 <= offset <= buf.length - 8`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to write. Must satisfy: `0 <= offset <= buf.length - 8`." } ] } ], "desc": "

Writes value to buf at the specified offset as big-endian.

\n

value is interpreted and written as a two's complement signed integer.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.allocUnsafe(8);\n\nbuf.writeBigInt64BE(0x0102030405060708n, 0);\n\nconsole.log(buf);\n// Prints: <Buffer 01 02 03 04 05 06 07 08>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.allocUnsafe(8);\n\nbuf.writeBigInt64BE(0x0102030405060708n, 0);\n\nconsole.log(buf);\n// Prints: <Buffer 01 02 03 04 05 06 07 08>\n
" }, { "textRaw": "`buf.writeBigInt64LE(value[, offset])`", "type": "method", "name": "writeBigInt64LE", "meta": { "added": [ "v12.0.0", "v10.20.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} `offset` plus the number of bytes written.", "name": "return", "type": "integer", "desc": "`offset` plus the number of bytes written." }, "params": [ { "textRaw": "`value` {bigint} Number to be written to `buf`.", "name": "value", "type": "bigint", "desc": "Number to be written to `buf`." }, { "textRaw": "`offset` {integer} Number of bytes to skip before starting to write. Must satisfy: `0 <= offset <= buf.length - 8`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to write. Must satisfy: `0 <= offset <= buf.length - 8`." } ] } ], "desc": "

Writes value to buf at the specified offset as little-endian.

\n

value is interpreted and written as a two's complement signed integer.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.allocUnsafe(8);\n\nbuf.writeBigInt64LE(0x0102030405060708n, 0);\n\nconsole.log(buf);\n// Prints: <Buffer 08 07 06 05 04 03 02 01>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.allocUnsafe(8);\n\nbuf.writeBigInt64LE(0x0102030405060708n, 0);\n\nconsole.log(buf);\n// Prints: <Buffer 08 07 06 05 04 03 02 01>\n
" }, { "textRaw": "`buf.writeBigUInt64BE(value[, offset])`", "type": "method", "name": "writeBigUInt64BE", "meta": { "added": [ "v12.0.0", "v10.20.0" ], "changes": [ { "version": [ "v14.10.0", "v12.19.0" ], "pr-url": "https://github.com/nodejs/node/pull/34960", "description": "This function is also available as `buf.writeBigUint64BE()`." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} `offset` plus the number of bytes written.", "name": "return", "type": "integer", "desc": "`offset` plus the number of bytes written." }, "params": [ { "textRaw": "`value` {bigint} Number to be written to `buf`.", "name": "value", "type": "bigint", "desc": "Number to be written to `buf`." }, { "textRaw": "`offset` {integer} Number of bytes to skip before starting to write. Must satisfy: `0 <= offset <= buf.length - 8`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to write. Must satisfy: `0 <= offset <= buf.length - 8`." } ] } ], "desc": "

Writes value to buf at the specified offset as big-endian.

\n

This function is also available under the writeBigUint64BE alias.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.allocUnsafe(8);\n\nbuf.writeBigUInt64BE(0xdecafafecacefaden, 0);\n\nconsole.log(buf);\n// Prints: <Buffer de ca fa fe ca ce fa de>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.allocUnsafe(8);\n\nbuf.writeBigUInt64BE(0xdecafafecacefaden, 0);\n\nconsole.log(buf);\n// Prints: <Buffer de ca fa fe ca ce fa de>\n
" }, { "textRaw": "`buf.writeBigUInt64LE(value[, offset])`", "type": "method", "name": "writeBigUInt64LE", "meta": { "added": [ "v12.0.0", "v10.20.0" ], "changes": [ { "version": [ "v14.10.0", "v12.19.0" ], "pr-url": "https://github.com/nodejs/node/pull/34960", "description": "This function is also available as `buf.writeBigUint64LE()`." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} `offset` plus the number of bytes written.", "name": "return", "type": "integer", "desc": "`offset` plus the number of bytes written." }, "params": [ { "textRaw": "`value` {bigint} Number to be written to `buf`.", "name": "value", "type": "bigint", "desc": "Number to be written to `buf`." }, { "textRaw": "`offset` {integer} Number of bytes to skip before starting to write. Must satisfy: `0 <= offset <= buf.length - 8`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to write. Must satisfy: `0 <= offset <= buf.length - 8`." } ] } ], "desc": "

Writes value to buf at the specified offset as little-endian

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.allocUnsafe(8);\n\nbuf.writeBigUInt64LE(0xdecafafecacefaden, 0);\n\nconsole.log(buf);\n// Prints: <Buffer de fa ce ca fe fa ca de>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.allocUnsafe(8);\n\nbuf.writeBigUInt64LE(0xdecafafecacefaden, 0);\n\nconsole.log(buf);\n// Prints: <Buffer de fa ce ca fe fa ca de>\n
\n

This function is also available under the writeBigUint64LE alias.

" }, { "textRaw": "`buf.writeDoubleBE(value[, offset])`", "type": "method", "name": "writeDoubleBE", "meta": { "added": [ "v0.11.15" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} `offset` plus the number of bytes written.", "name": "return", "type": "integer", "desc": "`offset` plus the number of bytes written." }, "params": [ { "textRaw": "`value` {number} Number to be written to `buf`.", "name": "value", "type": "number", "desc": "Number to be written to `buf`." }, { "textRaw": "`offset` {integer} Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 8`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 8`." } ] } ], "desc": "

Writes value to buf at the specified offset as big-endian. The value\nmust be a JavaScript number. Behavior is undefined when value is anything\nother than a JavaScript number.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.allocUnsafe(8);\n\nbuf.writeDoubleBE(123.456, 0);\n\nconsole.log(buf);\n// Prints: <Buffer 40 5e dd 2f 1a 9f be 77>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.allocUnsafe(8);\n\nbuf.writeDoubleBE(123.456, 0);\n\nconsole.log(buf);\n// Prints: <Buffer 40 5e dd 2f 1a 9f be 77>\n
" }, { "textRaw": "`buf.writeDoubleLE(value[, offset])`", "type": "method", "name": "writeDoubleLE", "meta": { "added": [ "v0.11.15" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} `offset` plus the number of bytes written.", "name": "return", "type": "integer", "desc": "`offset` plus the number of bytes written." }, "params": [ { "textRaw": "`value` {number} Number to be written to `buf`.", "name": "value", "type": "number", "desc": "Number to be written to `buf`." }, { "textRaw": "`offset` {integer} Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 8`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 8`." } ] } ], "desc": "

Writes value to buf at the specified offset as little-endian. The value\nmust be a JavaScript number. Behavior is undefined when value is anything\nother than a JavaScript number.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.allocUnsafe(8);\n\nbuf.writeDoubleLE(123.456, 0);\n\nconsole.log(buf);\n// Prints: <Buffer 77 be 9f 1a 2f dd 5e 40>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.allocUnsafe(8);\n\nbuf.writeDoubleLE(123.456, 0);\n\nconsole.log(buf);\n// Prints: <Buffer 77 be 9f 1a 2f dd 5e 40>\n
" }, { "textRaw": "`buf.writeFloatBE(value[, offset])`", "type": "method", "name": "writeFloatBE", "meta": { "added": [ "v0.11.15" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} `offset` plus the number of bytes written.", "name": "return", "type": "integer", "desc": "`offset` plus the number of bytes written." }, "params": [ { "textRaw": "`value` {number} Number to be written to `buf`.", "name": "value", "type": "number", "desc": "Number to be written to `buf`." }, { "textRaw": "`offset` {integer} Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 4`." } ] } ], "desc": "

Writes value to buf at the specified offset as big-endian. Behavior is\nundefined when value is anything other than a JavaScript number.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.allocUnsafe(4);\n\nbuf.writeFloatBE(0xcafebabe, 0);\n\nconsole.log(buf);\n// Prints: <Buffer 4f 4a fe bb>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.allocUnsafe(4);\n\nbuf.writeFloatBE(0xcafebabe, 0);\n\nconsole.log(buf);\n// Prints: <Buffer 4f 4a fe bb>\n
" }, { "textRaw": "`buf.writeFloatLE(value[, offset])`", "type": "method", "name": "writeFloatLE", "meta": { "added": [ "v0.11.15" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} `offset` plus the number of bytes written.", "name": "return", "type": "integer", "desc": "`offset` plus the number of bytes written." }, "params": [ { "textRaw": "`value` {number} Number to be written to `buf`.", "name": "value", "type": "number", "desc": "Number to be written to `buf`." }, { "textRaw": "`offset` {integer} Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 4`." } ] } ], "desc": "

Writes value to buf at the specified offset as little-endian. Behavior is\nundefined when value is anything other than a JavaScript number.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.allocUnsafe(4);\n\nbuf.writeFloatLE(0xcafebabe, 0);\n\nconsole.log(buf);\n// Prints: <Buffer bb fe 4a 4f>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.allocUnsafe(4);\n\nbuf.writeFloatLE(0xcafebabe, 0);\n\nconsole.log(buf);\n// Prints: <Buffer bb fe 4a 4f>\n
" }, { "textRaw": "`buf.writeInt8(value[, offset])`", "type": "method", "name": "writeInt8", "meta": { "added": [ "v0.5.0" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} `offset` plus the number of bytes written.", "name": "return", "type": "integer", "desc": "`offset` plus the number of bytes written." }, "params": [ { "textRaw": "`value` {integer} Number to be written to `buf`.", "name": "value", "type": "integer", "desc": "Number to be written to `buf`." }, { "textRaw": "`offset` {integer} Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 1`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 1`." } ] } ], "desc": "

Writes value to buf at the specified offset. value must be a valid\nsigned 8-bit integer. Behavior is undefined when value is anything other than\na signed 8-bit integer.

\n

value is interpreted and written as a two's complement signed integer.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.allocUnsafe(2);\n\nbuf.writeInt8(2, 0);\nbuf.writeInt8(-2, 1);\n\nconsole.log(buf);\n// Prints: <Buffer 02 fe>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.allocUnsafe(2);\n\nbuf.writeInt8(2, 0);\nbuf.writeInt8(-2, 1);\n\nconsole.log(buf);\n// Prints: <Buffer 02 fe>\n
" }, { "textRaw": "`buf.writeInt16BE(value[, offset])`", "type": "method", "name": "writeInt16BE", "meta": { "added": [ "v0.5.5" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} `offset` plus the number of bytes written.", "name": "return", "type": "integer", "desc": "`offset` plus the number of bytes written." }, "params": [ { "textRaw": "`value` {integer} Number to be written to `buf`.", "name": "value", "type": "integer", "desc": "Number to be written to `buf`." }, { "textRaw": "`offset` {integer} Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 2`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 2`." } ] } ], "desc": "

Writes value to buf at the specified offset as big-endian. The value\nmust be a valid signed 16-bit integer. Behavior is undefined when value is\nanything other than a signed 16-bit integer.

\n

The value is interpreted and written as a two's complement signed integer.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.allocUnsafe(2);\n\nbuf.writeInt16BE(0x0102, 0);\n\nconsole.log(buf);\n// Prints: <Buffer 01 02>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.allocUnsafe(2);\n\nbuf.writeInt16BE(0x0102, 0);\n\nconsole.log(buf);\n// Prints: <Buffer 01 02>\n
" }, { "textRaw": "`buf.writeInt16LE(value[, offset])`", "type": "method", "name": "writeInt16LE", "meta": { "added": [ "v0.5.5" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} `offset` plus the number of bytes written.", "name": "return", "type": "integer", "desc": "`offset` plus the number of bytes written." }, "params": [ { "textRaw": "`value` {integer} Number to be written to `buf`.", "name": "value", "type": "integer", "desc": "Number to be written to `buf`." }, { "textRaw": "`offset` {integer} Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 2`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 2`." } ] } ], "desc": "

Writes value to buf at the specified offset as little-endian. The value\nmust be a valid signed 16-bit integer. Behavior is undefined when value is\nanything other than a signed 16-bit integer.

\n

The value is interpreted and written as a two's complement signed integer.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.allocUnsafe(2);\n\nbuf.writeInt16LE(0x0304, 0);\n\nconsole.log(buf);\n// Prints: <Buffer 04 03>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.allocUnsafe(2);\n\nbuf.writeInt16LE(0x0304, 0);\n\nconsole.log(buf);\n// Prints: <Buffer 04 03>\n
" }, { "textRaw": "`buf.writeInt32BE(value[, offset])`", "type": "method", "name": "writeInt32BE", "meta": { "added": [ "v0.5.5" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} `offset` plus the number of bytes written.", "name": "return", "type": "integer", "desc": "`offset` plus the number of bytes written." }, "params": [ { "textRaw": "`value` {integer} Number to be written to `buf`.", "name": "value", "type": "integer", "desc": "Number to be written to `buf`." }, { "textRaw": "`offset` {integer} Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 4`." } ] } ], "desc": "

Writes value to buf at the specified offset as big-endian. The value\nmust be a valid signed 32-bit integer. Behavior is undefined when value is\nanything other than a signed 32-bit integer.

\n

The value is interpreted and written as a two's complement signed integer.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.allocUnsafe(4);\n\nbuf.writeInt32BE(0x01020304, 0);\n\nconsole.log(buf);\n// Prints: <Buffer 01 02 03 04>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.allocUnsafe(4);\n\nbuf.writeInt32BE(0x01020304, 0);\n\nconsole.log(buf);\n// Prints: <Buffer 01 02 03 04>\n
" }, { "textRaw": "`buf.writeInt32LE(value[, offset])`", "type": "method", "name": "writeInt32LE", "meta": { "added": [ "v0.5.5" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} `offset` plus the number of bytes written.", "name": "return", "type": "integer", "desc": "`offset` plus the number of bytes written." }, "params": [ { "textRaw": "`value` {integer} Number to be written to `buf`.", "name": "value", "type": "integer", "desc": "Number to be written to `buf`." }, { "textRaw": "`offset` {integer} Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 4`." } ] } ], "desc": "

Writes value to buf at the specified offset as little-endian. The value\nmust be a valid signed 32-bit integer. Behavior is undefined when value is\nanything other than a signed 32-bit integer.

\n

The value is interpreted and written as a two's complement signed integer.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.allocUnsafe(4);\n\nbuf.writeInt32LE(0x05060708, 0);\n\nconsole.log(buf);\n// Prints: <Buffer 08 07 06 05>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.allocUnsafe(4);\n\nbuf.writeInt32LE(0x05060708, 0);\n\nconsole.log(buf);\n// Prints: <Buffer 08 07 06 05>\n
" }, { "textRaw": "`buf.writeIntBE(value, offset, byteLength)`", "type": "method", "name": "writeIntBE", "meta": { "added": [ "v0.11.15" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset and `byteLength` to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} `offset` plus the number of bytes written.", "name": "return", "type": "integer", "desc": "`offset` plus the number of bytes written." }, "params": [ { "textRaw": "`value` {integer} Number to be written to `buf`.", "name": "value", "type": "integer", "desc": "Number to be written to `buf`." }, { "textRaw": "`offset` {integer} Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - byteLength`.", "name": "offset", "type": "integer", "desc": "Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - byteLength`." }, { "textRaw": "`byteLength` {integer} Number of bytes to write. Must satisfy `0 < byteLength <= 6`.", "name": "byteLength", "type": "integer", "desc": "Number of bytes to write. Must satisfy `0 < byteLength <= 6`." } ] } ], "desc": "

Writes byteLength bytes of value to buf at the specified offset\nas big-endian. Supports up to 48 bits of accuracy. Behavior is undefined when\nvalue is anything other than a signed integer.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.allocUnsafe(6);\n\nbuf.writeIntBE(0x1234567890ab, 0, 6);\n\nconsole.log(buf);\n// Prints: <Buffer 12 34 56 78 90 ab>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.allocUnsafe(6);\n\nbuf.writeIntBE(0x1234567890ab, 0, 6);\n\nconsole.log(buf);\n// Prints: <Buffer 12 34 56 78 90 ab>\n
" }, { "textRaw": "`buf.writeIntLE(value, offset, byteLength)`", "type": "method", "name": "writeIntLE", "meta": { "added": [ "v0.11.15" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset and `byteLength` to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} `offset` plus the number of bytes written.", "name": "return", "type": "integer", "desc": "`offset` plus the number of bytes written." }, "params": [ { "textRaw": "`value` {integer} Number to be written to `buf`.", "name": "value", "type": "integer", "desc": "Number to be written to `buf`." }, { "textRaw": "`offset` {integer} Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - byteLength`.", "name": "offset", "type": "integer", "desc": "Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - byteLength`." }, { "textRaw": "`byteLength` {integer} Number of bytes to write. Must satisfy `0 < byteLength <= 6`.", "name": "byteLength", "type": "integer", "desc": "Number of bytes to write. Must satisfy `0 < byteLength <= 6`." } ] } ], "desc": "

Writes byteLength bytes of value to buf at the specified offset\nas little-endian. Supports up to 48 bits of accuracy. Behavior is undefined\nwhen value is anything other than a signed integer.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.allocUnsafe(6);\n\nbuf.writeIntLE(0x1234567890ab, 0, 6);\n\nconsole.log(buf);\n// Prints: <Buffer ab 90 78 56 34 12>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.allocUnsafe(6);\n\nbuf.writeIntLE(0x1234567890ab, 0, 6);\n\nconsole.log(buf);\n// Prints: <Buffer ab 90 78 56 34 12>\n
" }, { "textRaw": "`buf.writeUInt8(value[, offset])`", "type": "method", "name": "writeUInt8", "meta": { "added": [ "v0.5.0" ], "changes": [ { "version": [ "v14.9.0", "v12.19.0" ], "pr-url": "https://github.com/nodejs/node/pull/34729", "description": "This function is also available as `buf.writeUint8()`." }, { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} `offset` plus the number of bytes written.", "name": "return", "type": "integer", "desc": "`offset` plus the number of bytes written." }, "params": [ { "textRaw": "`value` {integer} Number to be written to `buf`.", "name": "value", "type": "integer", "desc": "Number to be written to `buf`." }, { "textRaw": "`offset` {integer} Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 1`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 1`." } ] } ], "desc": "

Writes value to buf at the specified offset. value must be a\nvalid unsigned 8-bit integer. Behavior is undefined when value is anything\nother than an unsigned 8-bit integer.

\n

This function is also available under the writeUint8 alias.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.allocUnsafe(4);\n\nbuf.writeUInt8(0x3, 0);\nbuf.writeUInt8(0x4, 1);\nbuf.writeUInt8(0x23, 2);\nbuf.writeUInt8(0x42, 3);\n\nconsole.log(buf);\n// Prints: <Buffer 03 04 23 42>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.allocUnsafe(4);\n\nbuf.writeUInt8(0x3, 0);\nbuf.writeUInt8(0x4, 1);\nbuf.writeUInt8(0x23, 2);\nbuf.writeUInt8(0x42, 3);\n\nconsole.log(buf);\n// Prints: <Buffer 03 04 23 42>\n
" }, { "textRaw": "`buf.writeUInt16BE(value[, offset])`", "type": "method", "name": "writeUInt16BE", "meta": { "added": [ "v0.5.5" ], "changes": [ { "version": [ "v14.9.0", "v12.19.0" ], "pr-url": "https://github.com/nodejs/node/pull/34729", "description": "This function is also available as `buf.writeUint16BE()`." }, { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} `offset` plus the number of bytes written.", "name": "return", "type": "integer", "desc": "`offset` plus the number of bytes written." }, "params": [ { "textRaw": "`value` {integer} Number to be written to `buf`.", "name": "value", "type": "integer", "desc": "Number to be written to `buf`." }, { "textRaw": "`offset` {integer} Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 2`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 2`." } ] } ], "desc": "

Writes value to buf at the specified offset as big-endian. The value\nmust be a valid unsigned 16-bit integer. Behavior is undefined when value\nis anything other than an unsigned 16-bit integer.

\n

This function is also available under the writeUint16BE alias.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.allocUnsafe(4);\n\nbuf.writeUInt16BE(0xdead, 0);\nbuf.writeUInt16BE(0xbeef, 2);\n\nconsole.log(buf);\n// Prints: <Buffer de ad be ef>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.allocUnsafe(4);\n\nbuf.writeUInt16BE(0xdead, 0);\nbuf.writeUInt16BE(0xbeef, 2);\n\nconsole.log(buf);\n// Prints: <Buffer de ad be ef>\n
" }, { "textRaw": "`buf.writeUInt16LE(value[, offset])`", "type": "method", "name": "writeUInt16LE", "meta": { "added": [ "v0.5.5" ], "changes": [ { "version": [ "v14.9.0", "v12.19.0" ], "pr-url": "https://github.com/nodejs/node/pull/34729", "description": "This function is also available as `buf.writeUint16LE()`." }, { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} `offset` plus the number of bytes written.", "name": "return", "type": "integer", "desc": "`offset` plus the number of bytes written." }, "params": [ { "textRaw": "`value` {integer} Number to be written to `buf`.", "name": "value", "type": "integer", "desc": "Number to be written to `buf`." }, { "textRaw": "`offset` {integer} Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 2`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 2`." } ] } ], "desc": "

Writes value to buf at the specified offset as little-endian. The value\nmust be a valid unsigned 16-bit integer. Behavior is undefined when value is\nanything other than an unsigned 16-bit integer.

\n

This function is also available under the writeUint16LE alias.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.allocUnsafe(4);\n\nbuf.writeUInt16LE(0xdead, 0);\nbuf.writeUInt16LE(0xbeef, 2);\n\nconsole.log(buf);\n// Prints: <Buffer ad de ef be>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.allocUnsafe(4);\n\nbuf.writeUInt16LE(0xdead, 0);\nbuf.writeUInt16LE(0xbeef, 2);\n\nconsole.log(buf);\n// Prints: <Buffer ad de ef be>\n
" }, { "textRaw": "`buf.writeUInt32BE(value[, offset])`", "type": "method", "name": "writeUInt32BE", "meta": { "added": [ "v0.5.5" ], "changes": [ { "version": [ "v14.9.0", "v12.19.0" ], "pr-url": "https://github.com/nodejs/node/pull/34729", "description": "This function is also available as `buf.writeUint32BE()`." }, { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} `offset` plus the number of bytes written.", "name": "return", "type": "integer", "desc": "`offset` plus the number of bytes written." }, "params": [ { "textRaw": "`value` {integer} Number to be written to `buf`.", "name": "value", "type": "integer", "desc": "Number to be written to `buf`." }, { "textRaw": "`offset` {integer} Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 4`." } ] } ], "desc": "

Writes value to buf at the specified offset as big-endian. The value\nmust be a valid unsigned 32-bit integer. Behavior is undefined when value\nis anything other than an unsigned 32-bit integer.

\n

This function is also available under the writeUint32BE alias.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.allocUnsafe(4);\n\nbuf.writeUInt32BE(0xfeedface, 0);\n\nconsole.log(buf);\n// Prints: <Buffer fe ed fa ce>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.allocUnsafe(4);\n\nbuf.writeUInt32BE(0xfeedface, 0);\n\nconsole.log(buf);\n// Prints: <Buffer fe ed fa ce>\n
" }, { "textRaw": "`buf.writeUInt32LE(value[, offset])`", "type": "method", "name": "writeUInt32LE", "meta": { "added": [ "v0.5.5" ], "changes": [ { "version": [ "v14.9.0", "v12.19.0" ], "pr-url": "https://github.com/nodejs/node/pull/34729", "description": "This function is also available as `buf.writeUint32LE()`." }, { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} `offset` plus the number of bytes written.", "name": "return", "type": "integer", "desc": "`offset` plus the number of bytes written." }, "params": [ { "textRaw": "`value` {integer} Number to be written to `buf`.", "name": "value", "type": "integer", "desc": "Number to be written to `buf`." }, { "textRaw": "`offset` {integer} Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 4`. **Default:** `0`.", "name": "offset", "type": "integer", "default": "`0`", "desc": "Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 4`." } ] } ], "desc": "

Writes value to buf at the specified offset as little-endian. The value\nmust be a valid unsigned 32-bit integer. Behavior is undefined when value is\nanything other than an unsigned 32-bit integer.

\n

This function is also available under the writeUint32LE alias.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.allocUnsafe(4);\n\nbuf.writeUInt32LE(0xfeedface, 0);\n\nconsole.log(buf);\n// Prints: <Buffer ce fa ed fe>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.allocUnsafe(4);\n\nbuf.writeUInt32LE(0xfeedface, 0);\n\nconsole.log(buf);\n// Prints: <Buffer ce fa ed fe>\n
" }, { "textRaw": "`buf.writeUIntBE(value, offset, byteLength)`", "type": "method", "name": "writeUIntBE", "meta": { "added": [ "v0.5.5" ], "changes": [ { "version": [ "v14.9.0", "v12.19.0" ], "pr-url": "https://github.com/nodejs/node/pull/34729", "description": "This function is also available as `buf.writeUintBE()`." }, { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset and `byteLength` to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} `offset` plus the number of bytes written.", "name": "return", "type": "integer", "desc": "`offset` plus the number of bytes written." }, "params": [ { "textRaw": "`value` {integer} Number to be written to `buf`.", "name": "value", "type": "integer", "desc": "Number to be written to `buf`." }, { "textRaw": "`offset` {integer} Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - byteLength`.", "name": "offset", "type": "integer", "desc": "Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - byteLength`." }, { "textRaw": "`byteLength` {integer} Number of bytes to write. Must satisfy `0 < byteLength <= 6`.", "name": "byteLength", "type": "integer", "desc": "Number of bytes to write. Must satisfy `0 < byteLength <= 6`." } ] } ], "desc": "

Writes byteLength bytes of value to buf at the specified offset\nas big-endian. Supports up to 48 bits of accuracy. Behavior is undefined\nwhen value is anything other than an unsigned integer.

\n

This function is also available under the writeUintBE alias.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.allocUnsafe(6);\n\nbuf.writeUIntBE(0x1234567890ab, 0, 6);\n\nconsole.log(buf);\n// Prints: <Buffer 12 34 56 78 90 ab>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.allocUnsafe(6);\n\nbuf.writeUIntBE(0x1234567890ab, 0, 6);\n\nconsole.log(buf);\n// Prints: <Buffer 12 34 56 78 90 ab>\n
" }, { "textRaw": "`buf.writeUIntLE(value, offset, byteLength)`", "type": "method", "name": "writeUIntLE", "meta": { "added": [ "v0.5.5" ], "changes": [ { "version": [ "v14.9.0", "v12.19.0" ], "pr-url": "https://github.com/nodejs/node/pull/34729", "description": "This function is also available as `buf.writeUintLE()`." }, { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/18395", "description": "Removed `noAssert` and no implicit coercion of the offset and `byteLength` to `uint32` anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {integer} `offset` plus the number of bytes written.", "name": "return", "type": "integer", "desc": "`offset` plus the number of bytes written." }, "params": [ { "textRaw": "`value` {integer} Number to be written to `buf`.", "name": "value", "type": "integer", "desc": "Number to be written to `buf`." }, { "textRaw": "`offset` {integer} Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - byteLength`.", "name": "offset", "type": "integer", "desc": "Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - byteLength`." }, { "textRaw": "`byteLength` {integer} Number of bytes to write. Must satisfy `0 < byteLength <= 6`.", "name": "byteLength", "type": "integer", "desc": "Number of bytes to write. Must satisfy `0 < byteLength <= 6`." } ] } ], "desc": "

Writes byteLength bytes of value to buf at the specified offset\nas little-endian. Supports up to 48 bits of accuracy. Behavior is undefined\nwhen value is anything other than an unsigned integer.

\n

This function is also available under the writeUintLE alias.

\n
import { Buffer } from 'buffer';\n\nconst buf = Buffer.allocUnsafe(6);\n\nbuf.writeUIntLE(0x1234567890ab, 0, 6);\n\nconsole.log(buf);\n// Prints: <Buffer ab 90 78 56 34 12>\n
\n
const { Buffer } = require('buffer');\n\nconst buf = Buffer.allocUnsafe(6);\n\nbuf.writeUIntLE(0x1234567890ab, 0, 6);\n\nconsole.log(buf);\n// Prints: <Buffer ab 90 78 56 34 12>\n
" } ], "signatures": [ { "params": [ { "textRaw": "`array` {integer[]} An array of bytes to copy from.", "name": "array", "type": "integer[]", "desc": "An array of bytes to copy from." } ], "desc": "

See Buffer.from(array).

" }, { "params": [ { "textRaw": "`arrayBuffer` {ArrayBuffer|SharedArrayBuffer} An [`ArrayBuffer`][], [`SharedArrayBuffer`][] or the `.buffer` property of a [`TypedArray`][].", "name": "arrayBuffer", "type": "ArrayBuffer|SharedArrayBuffer", "desc": "An [`ArrayBuffer`][], [`SharedArrayBuffer`][] or the `.buffer` property of a [`TypedArray`][]." }, { "textRaw": "`byteOffset` {integer} Index of first byte to expose. **Default:** `0`.", "name": "byteOffset", "type": "integer", "default": "`0`", "desc": "Index of first byte to expose." }, { "textRaw": "`length` {integer} Number of bytes to expose. **Default:** `arrayBuffer.byteLength - byteOffset`.", "name": "length", "type": "integer", "default": "`arrayBuffer.byteLength - byteOffset`", "desc": "Number of bytes to expose." } ], "desc": "

See\nBuffer.from(arrayBuffer[, byteOffset[, length]]).

" }, { "params": [ { "textRaw": "`buffer` {Buffer|Uint8Array} An existing `Buffer` or [`Uint8Array`][] from which to copy data.", "name": "buffer", "type": "Buffer|Uint8Array", "desc": "An existing `Buffer` or [`Uint8Array`][] from which to copy data." } ], "desc": "

See Buffer.from(buffer).

" }, { "params": [ { "textRaw": "`size` {integer} The desired length of the new `Buffer`.", "name": "size", "type": "integer", "desc": "The desired length of the new `Buffer`." } ], "desc": "

See Buffer.alloc() and Buffer.allocUnsafe(). This variant of the\nconstructor is equivalent to Buffer.alloc().

" }, { "params": [ { "textRaw": "`string` {string} String to encode.", "name": "string", "type": "string", "desc": "String to encode." }, { "textRaw": "`encoding` {string} The encoding of `string`. **Default:** `'utf8'`.", "name": "encoding", "type": "string", "default": "`'utf8'`", "desc": "The encoding of `string`." } ], "desc": "

See Buffer.from(string[, encoding]).

" } ] } ], "type": "module", "displayName": "Buffer" } ] }