Buffer
Properties
BYTES_PER_ELEMENT
numberRequired[toStringTag]
"Uint8Array"RequiredbyteLength
numberRequiredbyteOffset
numberRequiredlength
numberRequiredMethods
[iterator]
**[iterator]**(): [IterableIterator](/references/services/interfaces/IterableIterator)<number>
Returns
IterableIterator<number>
at
**at**(index): undefined \| number
Returns the item located at the specified index.
Parameters
index
numberRequiredReturns
undefined
| number
undefined \| number
undefined | numbercompare
**compare**(target, targetStart?, targetEnd?, sourceStart?, sourceEnd?): 0 \| 1 \| -1
Compares buf
with target
and returns a number indicating whether buf
comes before, after, or is the same as target
in sort order.
Comparison is based on the actual sequence of bytes in each Buffer
.
0
is returned iftarget
is the same asbuf
1
is returned iftarget
should come _before_buf
when sorted.-1
is returned iftarget
should come _after_buf
when sorted.
import { Buffer } from 'node:buffer';
const buf1 = Buffer.from('ABC');
const buf2 = Buffer.from('BCD');
const buf3 = Buffer.from('ABCD');
console.log(buf1.compare(buf1));
// Prints: 0
console.log(buf1.compare(buf2));
// Prints: -1
console.log(buf1.compare(buf3));
// Prints: -1
console.log(buf2.compare(buf1));
// Prints: 1
console.log(buf2.compare(buf3));
// Prints: 1
console.log([buf1, buf2, buf3].sort(Buffer.compare));
// Prints: [ <Buffer 41 42 43>, <Buffer 41 42 43 44>, <Buffer 42 43 44> ]
// (This result is equal to: [buf1, buf3, buf2].)
The optional targetStart
, targetEnd
, sourceStart
, and sourceEnd
arguments can be used to limit the comparison to specific ranges within target
and buf
respectively.
import { Buffer } from 'node:buffer';
const buf1 = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9]);
const buf2 = Buffer.from([5, 6, 7, 8, 9, 1, 2, 3, 4]);
console.log(buf1.compare(buf2, 5, 9, 0, 4));
// Prints: 0
console.log(buf1.compare(buf2, 0, 6, 4));
// Prints: -1
console.log(buf1.compare(buf2, 5, 6, 5));
// Prints: 1
ERR_OUT_OF_RANGE
is thrown if targetStart < 0
, sourceStart < 0
,targetEnd > target.byteLength
, or sourceEnd > source.byteLength
.
Parameters
target
Uint8ArrayRequiredBuffer
or Uint8Array with which to compare buf
.targetStart
numbertarget
at which to begin comparison.targetEnd
numbertarget
at which to end comparison (not inclusive).sourceStart
numberbuf
at which to begin comparison.sourceEnd
numberbuf
at which to end comparison (not inclusive).Returns
0
| 1
| -1
0 \| 1 \| -1
0 | 1 | -1Since
v0.11.13
copy
**copy**(target, targetStart?, sourceStart?, sourceEnd?): number
Copies data from a region of buf
to a region in target
, even if the target
memory region overlaps with buf
.
TypedArray.prototype.set()
performs the same operation, and is available
for all TypedArrays, including Node.js Buffer
s, although it takes
different function arguments.
import { Buffer } from 'node:buffer';
// Create two `Buffer` instances.
const buf1 = Buffer.allocUnsafe(26);
const buf2 = Buffer.allocUnsafe(26).fill('!');
for (let i = 0; i < 26; i++) {
// 97 is the decimal ASCII value for 'a'.
buf1[i] = i + 97;
}
// Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`.
buf1.copy(buf2, 8, 16, 20);
// This is equivalent to:
// buf2.set(buf1.subarray(16, 20), 8);
console.log(buf2.toString('ascii', 0, 25));
// Prints: !!!!!!!!qrst!!!!!!!!!!!!!
import { Buffer } from 'node:buffer';
// Create a `Buffer` and copy data from one region to an overlapping region
// within the same `Buffer`.
const buf = Buffer.allocUnsafe(26);
for (let i = 0; i < 26; i++) {
// 97 is the decimal ASCII value for 'a'.
buf[i] = i + 97;
}
buf.copy(buf, 0, 4, 10);
console.log(buf.toString());
// Prints: efghijghijklmnopqrstuvwxyz
Parameters
target
Uint8ArrayRequiredBuffer
or Uint8Array to copy into.targetStart
numbertarget
at which to begin writing.sourceStart
numberbuf
from which to begin copying.sourceEnd
numberbuf
at which to stop copying (not inclusive).Returns
number
number
numberSince
v0.1.90
copyWithin
**copyWithin**(target, start, end?): [Buffer](/references/services/#buffer)
Returns the this object after copying a section of the array identified by start and end to the same array starting at position target
Parameters
target
numberRequiredstart
numberRequiredend
numberReturns
entries
**entries**(): [IterableIterator](/references/services/interfaces/IterableIterator)<[number, number]>
Creates and returns an iterator of [index, byte]
pairs from the contents
of buf
.
Returns
IterableIterator<[number, number]>
Since
v1.1.0
equals
**equals**(otherBuffer): boolean
Returns true
if both buf
and otherBuffer
have exactly the same bytes,false
otherwise. Equivalent to buf.compare(otherBuffer) === 0
.
Parameters
otherBuffer
Uint8ArrayRequiredBuffer
or Uint8Array with which to compare buf
.Returns
boolean
boolean
booleanSince
v0.11.13
every
**every**(predicate, thisArg?): boolean
Determines whether all the members of an array satisfy the specified test.
Parameters
predicate
(value: number, index: number, array: Uint8Array) => unknownRequiredthisArg
anyReturns
boolean
boolean
booleanfill
**fill**(value, offset?, end?, encoding?): [Buffer](/references/services/#buffer)
Fills buf
with the specified value
. If the offset
and end
are not given,
the entire buf
will be filled:
import { Buffer } from 'node:buffer';
// Fill a `Buffer` with the ASCII character 'h'.
const b = Buffer.allocUnsafe(50).fill('h');
console.log(b.toString());
// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
// Fill a buffer with empty string
const c = Buffer.allocUnsafe(5).fill('');
console.log(c.fill(''));
// Prints: <Buffer 00 00 00 00 00>
value
is coerced to a uint32
value if it is not a string, Buffer
, or
integer. If the resulting integer is greater than 255
(decimal), buf
will be
filled with value & 255
.
If the final write of a fill()
operation falls on a multi-byte character,
then only the bytes of that character that fit into buf
are written:
If value
contains invalid characters, it is truncated; if no valid
fill data remains, an exception is thrown:
Parameters
value
string | number | Uint8ArrayRequiredbuf
. Empty value (string, Uint8Array, Buffer) is coerced to 0
.offset
numberbuf
.end
numberbuf
(not inclusive).encoding
BufferEncodingvalue
if value
is a string.Returns
buf
.Since
v0.5.0
filter
**filter**(predicate, thisArg?): Uint8Array
Returns the elements of an array that meet the condition specified in a callback function.
Parameters
predicate
(value: number, index: number, array: Uint8Array) => anyRequiredthisArg
anyReturns
Uint8Array
Uint8Array
Uint8ArrayRequiredfind
**find**(predicate, thisArg?): undefined \| number
Returns the value of the first element in the array where predicate is true, and undefined otherwise.
Parameters
predicate
(value: number, index: number, obj: Uint8Array) => booleanRequiredthisArg
anyReturns
undefined
| number
undefined \| number
undefined | numberfindIndex
**findIndex**(predicate, thisArg?): number
Returns the index of the first element in the array where predicate is true, and -1 otherwise.
Parameters
predicate
(value: number, index: number, obj: Uint8Array) => booleanRequiredthisArg
anyReturns
number
number
numberfindLast
**findLast**<TypeParameter S>(predicate, thisArg?): undefined \| S
Returns the value of the last element in the array where predicate is true, and undefined otherwise.
Type Parameters
S
numberRequiredParameters
predicate
(value: number, index: number, array: Uint8Array) => value is SRequiredthisArg
anyReturns
undefined
| S
undefined \| S
undefined | S**findLast**(predicate, thisArg?): undefined \| number
Parameters
predicate
(value: number, index: number, array: Uint8Array) => unknownRequiredthisArg
anyReturns
undefined
| number
undefined \| number
undefined | numberfindLastIndex
**findLastIndex**(predicate, thisArg?): number
Returns the index of the last element in the array where predicate is true, and -1 otherwise.
Parameters
predicate
(value: number, index: number, array: Uint8Array) => unknownRequiredthisArg
anyReturns
number
number
numberforEach
**forEach**(callbackfn, thisArg?): void
Performs the specified action for each element in an array.
Parameters
callbackfn
(value: number, index: number, array: Uint8Array) => voidRequiredthisArg
anyReturns
void
void
voidincludes
**includes**(value, byteOffset?, encoding?): boolean
Equivalent to buf.indexOf() !== -1
.
import { Buffer } from 'node:buffer';
const buf = Buffer.from('this is a buffer');
console.log(buf.includes('this'));
// Prints: true
console.log(buf.includes('is'));
// Prints: true
console.log(buf.includes(Buffer.from('a buffer')));
// Prints: true
console.log(buf.includes(97));
// Prints: true (97 is the decimal ASCII value for 'a')
console.log(buf.includes(Buffer.from('a buffer example')));
// Prints: false
console.log(buf.includes(Buffer.from('a buffer example').slice(0, 8)));
// Prints: true
console.log(buf.includes('this', 4));
// Prints: false
Parameters
byteOffset
numberbuf
. If negative, then offset is calculated from the end of buf
.encoding
BufferEncodingvalue
is a string, this is its encoding.Returns
boolean
boolean
booleantrue
if value
was found in buf
, false
otherwise.Since
v5.3.0
indexOf
**indexOf**(value, byteOffset?, encoding?): number
If value
is:
- a string,
value
is interpreted according to the character encoding inencoding
. - a
Buffer
orUint8Array
,value
will be used in its entirety. To compare a partialBuffer
, usebuf.subarray
. - a number,
value
will be interpreted as an unsigned 8-bit integer value between0
and255
.
import { Buffer } from 'node:buffer';
const buf = Buffer.from('this is a buffer');
console.log(buf.indexOf('this'));
// Prints: 0
console.log(buf.indexOf('is'));
// Prints: 2
console.log(buf.indexOf(Buffer.from('a buffer')));
// Prints: 8
console.log(buf.indexOf(97));
// Prints: 8 (97 is the decimal ASCII value for 'a')
console.log(buf.indexOf(Buffer.from('a buffer example')));
// Prints: -1
console.log(buf.indexOf(Buffer.from('a buffer example').slice(0, 8)));
// Prints: 8
const utf16Buffer = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'utf16le');
console.log(utf16Buffer.indexOf('\u03a3', 0, 'utf16le'));
// Prints: 4
console.log(utf16Buffer.indexOf('\u03a3', -4, 'utf16le'));
// Prints: 6
If value
is not a string, number, or Buffer
, this method will throw aTypeError
. If value
is a number, it will be coerced to a valid byte value,
an integer between 0 and 255.
If byteOffset
is not a number, it will be coerced to a number. If the result
of coercion is NaN
or 0
, then the entire buffer will be searched. This
behavior matches String.prototype.indexOf()
.
import { Buffer } from 'node:buffer';
const b = Buffer.from('abcdef');
// Passing a value that's a number, but not a valid byte.
// Prints: 2, equivalent to searching for 99 or 'c'.
console.log(b.indexOf(99.9));
console.log(b.indexOf(256 + 99));
// Passing a byteOffset that coerces to NaN or 0.
// Prints: 1, searching the whole buffer.
console.log(b.indexOf('b', undefined));
console.log(b.indexOf('b', {}));
console.log(b.indexOf('b', null));
console.log(b.indexOf('b', []));
If value
is an empty string or empty Buffer
and byteOffset
is less
than buf.length
, byteOffset
will be returned. If value
is empty andbyteOffset
is at least buf.length
, buf.length
will be returned.
Parameters
value
string | number | Uint8ArrayRequiredbyteOffset
numberbuf
. If negative, then offset is calculated from the end of buf
.encoding
BufferEncodingvalue
is a string, this is the encoding used to determine the binary representation of the string that will be searched for in buf
.Returns
number
number
numbervalue
in buf
, or -1
if buf
does not contain value
.Since
v1.5.0
join
**join**(separator?): string
Adds all the elements of an array separated by the specified separator string.
Parameters
separator
stringReturns
string
string
stringkeys
**keys**(): [IterableIterator](/references/services/interfaces/IterableIterator)<number>
Creates and returns an iterator of buf
keys (indices).
Returns
IterableIterator<number>
Since
v1.1.0
lastIndexOf
**lastIndexOf**(value, byteOffset?, encoding?): number
Identical to buf.indexOf()
, except the last occurrence of value
is found
rather than the first occurrence.
import { Buffer } from 'node:buffer';
const buf = Buffer.from('this buffer is a buffer');
console.log(buf.lastIndexOf('this'));
// Prints: 0
console.log(buf.lastIndexOf('buffer'));
// Prints: 17
console.log(buf.lastIndexOf(Buffer.from('buffer')));
// Prints: 17
console.log(buf.lastIndexOf(97));
// Prints: 15 (97 is the decimal ASCII value for 'a')
console.log(buf.lastIndexOf(Buffer.from('yolo')));
// Prints: -1
console.log(buf.lastIndexOf('buffer', 5));
// Prints: 5
console.log(buf.lastIndexOf('buffer', 4));
// Prints: -1
const utf16Buffer = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'utf16le');
console.log(utf16Buffer.lastIndexOf('\u03a3', undefined, 'utf16le'));
// Prints: 6
console.log(utf16Buffer.lastIndexOf('\u03a3', -5, 'utf16le'));
// Prints: 4
If value
is not a string, number, or Buffer
, this method will throw aTypeError
. If value
is a number, it will be coerced to a valid byte value,
an integer between 0 and 255.
If byteOffset
is not a number, it will be coerced to a number. Any arguments
that coerce to NaN
, like {}
or undefined
, will search the whole buffer.
This behavior matches String.prototype.lastIndexOf()
.
import { Buffer } from 'node:buffer';
const b = Buffer.from('abcdef');
// Passing a value that's a number, but not a valid byte.
// Prints: 2, equivalent to searching for 99 or 'c'.
console.log(b.lastIndexOf(99.9));
console.log(b.lastIndexOf(256 + 99));
// Passing a byteOffset that coerces to NaN.
// Prints: 1, searching the whole buffer.
console.log(b.lastIndexOf('b', undefined));
console.log(b.lastIndexOf('b', {}));
// Passing a byteOffset that coerces to 0.
// Prints: -1, equivalent to passing 0.
console.log(b.lastIndexOf('b', null));
console.log(b.lastIndexOf('b', []));
If value
is an empty string or empty Buffer
, byteOffset
will be returned.
Parameters
value
string | number | Uint8ArrayRequiredbyteOffset
numberbuf
. If negative, then offset is calculated from the end of buf
.encoding
BufferEncodingvalue
is a string, this is the encoding used to determine the binary representation of the string that will be searched for in buf
.Returns
number
number
numbervalue
in buf
, or -1
if buf
does not contain value
.Since
v6.0.0
map
**map**(callbackfn, thisArg?): Uint8Array
Calls a defined callback function on each element of an array, and returns an array that contains the results.
Parameters
callbackfn
(value: number, index: number, array: Uint8Array) => numberRequiredthisArg
anyReturns
Uint8Array
Uint8Array
Uint8ArrayRequiredreadBigInt64BE
**readBigInt64BE**(offset?): bigint
Reads a signed, big-endian 64-bit integer from buf
at the specified offset
.
Integers read from a Buffer
are interpreted as two's complement signed
values.
Parameters
offset
number0 <= offset <= buf.length - 8
.Returns
bigint
bigint
bigintSince
v12.0.0, v10.20.0
readBigInt64LE
**readBigInt64LE**(offset?): bigint
Reads a signed, little-endian 64-bit integer from buf
at the specifiedoffset
.
Integers read from a Buffer
are interpreted as two's complement signed
values.
Parameters
offset
number0 <= offset <= buf.length - 8
.Returns
bigint
bigint
bigintSince
v12.0.0, v10.20.0
readBigUInt64BE
**readBigUInt64BE**(offset?): bigint
Reads an unsigned, big-endian 64-bit integer from buf
at the specifiedoffset
.
This function is also available under the readBigUint64BE
alias.
Parameters
offset
number0 <= offset <= buf.length - 8
.Returns
bigint
bigint
bigintSince
v12.0.0, v10.20.0
readBigUInt64LE
**readBigUInt64LE**(offset?): bigint
Reads an unsigned, little-endian 64-bit integer from buf
at the specifiedoffset
.
This function is also available under the readBigUint64LE
alias.
Parameters
offset
number0 <= offset <= buf.length - 8
.Returns
bigint
bigint
bigintSince
v12.0.0, v10.20.0
readBigUint64BE
**readBigUint64BE**(offset?): bigint
Parameters
offset
numberReturns
bigint
bigint
bigintAlias
Buffer.readBigUInt64BE
Since
v14.10.0, v12.19.0
readBigUint64LE
**readBigUint64LE**(offset?): bigint
Parameters
offset
numberReturns
bigint
bigint
bigintAlias
Buffer.readBigUInt64LE
Since
v14.10.0, v12.19.0
readDoubleBE
**readDoubleBE**(offset?): number
Reads a 64-bit, big-endian double from buf
at the specified offset
.
Parameters
offset
number0 <= offset <= buf.length - 8
.Returns
number
number
numberSince
v0.11.15
readDoubleLE
**readDoubleLE**(offset?): number
Reads a 64-bit, little-endian double from buf
at the specified offset
.
Parameters
offset
number0 <= offset <= buf.length - 8
.Returns
number
number
numberSince
v0.11.15
readFloatBE
**readFloatBE**(offset?): number
Reads a 32-bit, big-endian float from buf
at the specified offset
.
Parameters
offset
number0 <= offset <= buf.length - 4
.Returns
number
number
numberSince
v0.11.15
readFloatLE
**readFloatLE**(offset?): number
Reads a 32-bit, little-endian float from buf
at the specified offset
.
Parameters
offset
number0 <= offset <= buf.length - 4
.Returns
number
number
numberSince
v0.11.15
readInt16BE
**readInt16BE**(offset?): number
Reads a signed, big-endian 16-bit integer from buf
at the specified offset
.
Integers read from a Buffer
are interpreted as two's complement signed values.
Parameters
offset
number0 <= offset <= buf.length - 2
.