Bare Docs
ReferenceBareModules

bare-buffer

Native buffers for JavaScript

Documented against v3.7.1
stable

bare-buffer — Native buffers for JavaScript. It is a native addon and requires Bare >=1.20.0.

Mirrors the Node.js buffer module.

npm i bare-buffer

Usage

const Buffer = require('bare-buffer')

const message = Buffer.from('hello')
const empty = Buffer.alloc(16)
const buffer = Buffer.allocUnsafe(4).fill(123)
const combined = Buffer.concat([buffer, buffer])

API

Buffer

new Buffer(arrayBuffer: ArrayBuffer, offset?: number, length?: number)

Create a Buffer viewing arrayBuffer, optionally starting at offset for length bytes.

Parameters

ParameterTypeDefaultDescription
arrayBufferArrayBuffer—The ArrayBuffer to view.
offset?number—Byte offset into arrayBuffer to start the view at; defaults to 0.
length?number—Number of bytes to view from offset; defaults to the rest of arrayBuffer.

Throws

  • RangeError — thrown if the resulting length would exceed Buffer.constants.MAX_LENGTH.

at(index: number): number | undefined

Returns the item located at the specified index.

Parameters

ParameterTypeDefaultDescription
indexnumber—The zero-based index of the desired code unit. A negative index will count back from the last item.

buffer: TArrayBuffer

The ArrayBuffer instance referenced by the array.

Buffer.alloc(size: number, fill: string, encoding?: BufferEncoding): Buffer

Allocate a new, zero-filled Buffer of size bytes, optionally filled with fill.

Overloads:

Buffer.alloc(size: number, fill: string, encoding?: BufferEncoding): Buffer
Buffer.alloc(size: number, fill?: Buffer | number | boolean): Buffer

Parameters

ParameterTypeDefaultDescription
sizenumber—Number of bytes to allocate.
fillstring—Value to fill the buffer with — a string, Buffer, number byte, or boolean.
encoding?BufferEncoding—Encoding used to interpret fill when it's a string; defaults to 'utf8'.

Throws

  • RangeError — thrown if size exceeds Buffer.constants.MAX_LENGTH.

Buffer.allocUnsafe(size: number): Buffer

Allocate a new Buffer of size bytes without zeroing its contents first.

Parameters

ParameterTypeDefaultDescription
sizenumber—Number of bytes to allocate.

Throws

  • RangeError — thrown if size exceeds Buffer.constants.MAX_LENGTH.

Buffer.allocUnsafeSlow(size: number): Buffer

Allocate a new Buffer of size bytes without zeroing its contents first, bypassing the internal buffer pool.

Parameters

ParameterTypeDefaultDescription
sizenumber—Number of bytes to allocate.

Throws

  • RangeError — thrown if size exceeds Buffer.constants.MAX_LENGTH.

Buffer.atob(data: unknown): string

Decode a base64-encoded string into a Latin-1 binary string.

Parameters

ParameterTypeDefaultDescription
dataunknown—The base64-encoded string to decode.

Buffer.btoa(data: unknown): string

Encode a Latin-1 binary string into a base64 string.

Parameters

ParameterTypeDefaultDescription
dataunknown—The value to encode; non-strings are coerced with String().

Buffer.BufferEncoding

type BufferEncoding = | 'ascii'
  | 'base64'
  | 'binary'
  | 'hex'
  | 'latin1'
  | 'ucs-2'
  | 'ucs2'
  | 'utf-16le'
  | 'utf-8'
  | 'utf16le'
  | 'utf8'

The set of encodings supported by Buffer methods that convert between bytes and strings.

Buffer.byteLength

Buffer.byteLength(string: ArrayBufferView | ArrayBufferLike | string, encoding?: BufferEncoding): number

Return the number of bytes string would occupy once encoded as encoding.

Parameters

ParameterTypeDefaultDescription
stringArrayBufferView | ArrayBufferLike | string—The value whose encoded length to measure — a string, or an ArrayBufferView/ArrayBufferLike (whose own byteLength is returned unchanged).
encoding?BufferEncoding—Encoding used to measure string when it's a string; defaults to 'utf8'.

Throws

  • Error — thrown if the encoding is not a recognized BufferEncoding.

Buffer.coerce(buffer: Buffer): Buffer

Return buffer unchanged if it is already a Buffer, otherwise wrap its underlying ArrayBuffer in a new Buffer.

Parameters

ParameterTypeDefaultDescription
bufferBuffer—The value to coerce into a Buffer.

Buffer.compare(a: Buffer, b: Buffer): number

Compare two buffers' contents lexicographically, returning -1, 0, or 1 for sort ordering.

Parameters

ParameterTypeDefaultDescription
aBuffer—The first buffer to compare.
bBuffer—The second buffer to compare.

Buffer.concat(buffers: Buffer[], length?: number): Buffer

Concatenate buffers into a single new Buffer, optionally truncated or zero-padded to length.

Parameters

ParameterTypeDefaultDescription
buffersBuffer[]—The buffers to concatenate, in order.
length?number—Total byte length of the result; defaults to the sum of buffers' lengths.

Buffer.constants: { MAX_LENGTH: number; MAX_STRING_LENGTH: number }

Limits for buffers and strings: MAX_LENGTH (largest possible Buffer) and MAX_STRING_LENGTH (largest possible string).

Buffer.copyBytesFrom(view: ArrayBufferLike, offset?: number, length?: number): Buffer

Copy the bytes of view, starting at offset for length elements, into a new Buffer.

Parameters

ParameterTypeDefaultDescription
viewArrayBufferLike—The typed array (or other ArrayBufferLike view) to copy bytes from.
offset?number—Index of the first element to copy; defaults to 0.
length?number—Number of elements to copy; defaults to the rest of view.

Throws

  • RangeError — thrown if offset + length exceeds view.length.

Buffer.from(data: Iterable<number>): Buffer

Create a new Buffer from an array, array-like, string, or ArrayBuffer.

Overloads:

Buffer.from(data: Iterable<number>): Buffer
Buffer.from(data: ArrayLike<number>): Buffer
Buffer.from(string: string, encoding?: BufferEncoding): Buffer
Buffer.from(arrayBuffer: ArrayBufferLike, offset?: number, length?: number): Buffer

Parameters

ParameterTypeDefaultDescription
dataIterable<number>—The array, array-like, string, buffer, or ArrayBuffer to create a new Buffer from.

Throws

  • Error — thrown if the encoding is not a recognized BufferEncoding.

Buffer.isAscii(buffer: Buffer): boolean

Check whether buffer contains only valid ASCII-encoded data.

Parameters

ParameterTypeDefaultDescription
bufferBuffer—The buffer to check.

Buffer.isASCII(buffer: Buffer): boolean

Check whether buffer contains only valid ASCII-encoded data.

Parameters

ParameterTypeDefaultDescription
bufferBuffer—The buffer to check.

Buffer.isBuffer(value: unknown): value is Buffer

Check whether value is a Buffer.

Parameters

ParameterTypeDefaultDescription
valueunknown—The value to check.

Buffer.isEncoding(encoding: string): encoding is BufferEncoding

Check whether encoding is a supported BufferEncoding.

Parameters

ParameterTypeDefaultDescription
encodingstring—The encoding name to check.

Buffer.isUtf8(buffer: Buffer): boolean

Check whether buffer contains only valid UTF-8-encoded data.

Parameters

ParameterTypeDefaultDescription
bufferBuffer—The buffer to check.

Buffer.isUTF8(buffer: Buffer): boolean

Check whether buffer contains only valid UTF-8-encoded data.

Parameters

ParameterTypeDefaultDescription
bufferBuffer—The buffer to check.

Buffer.poolSize: number

The size, in bytes, of the internal buffer pool used to satisfy small allocations.

Buffer.transcode(buffer: Buffer, from: BufferEncoding, to: BufferEncoding): Buffer

Re-encode buffer from one encoding to another, returning a new Buffer.

Parameters

ParameterTypeDefaultDescription
bufferBuffer—The buffer to re-encode.
fromBufferEncoding—The encoding buffer is currently in.
toBufferEncoding—The encoding to convert to.

Throws

  • Error — thrown if the encoding is not a recognized BufferEncoding.

byteOffset: number

The offset in bytes of the array.

BYTES_PER_ELEMENT: number

The size in bytes of each element in the array.

compare

compare(target: Buffer, targetStart?: number, targetEnd?: number, sourceStart?: number, sourceEnd?: number): number

Compare this buffer's contents against target lexicographically, optionally comparing only a sub-range of each, returning -1, 0, or 1 for sort ordering.

Parameters

ParameterTypeDefaultDescription
targetBuffer—The buffer to compare against.
targetStart?number—Offset within target to start comparing from; defaults to 0.
targetEnd?number—Offset within target (exclusive) to stop comparing at; defaults to target.byteLength.
sourceStart?number—Offset within this buffer to start comparing from; defaults to 0.
sourceEnd?number—Offset within this buffer (exclusive) to stop comparing at; defaults to this.byteLength.

copy(target: Buffer, targetStart?: number, sourceStart?: number, sourceEnd?: number): number

Copy bytes from this buffer into target, returning the number of bytes copied.

Parameters

ParameterTypeDefaultDescription
targetBuffer—The buffer to copy into.
targetStart?number—Offset within target to start writing at; defaults to 0.
sourceStart?number—Offset within this buffer to start copying from; defaults to 0.
sourceEnd?number—Offset within this buffer (exclusive) to stop copying at; defaults to this.byteLength.

copyWithin(target: number, start: number, end?: number): this

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

ParameterTypeDefaultDescription
targetnumber—If target is negative, it is treated as length+target where length is the length of the array.
startnumber—If start is negative, it is treated as length+start. If end is negative, it is treated as length+end.
end?number—If not specified, length of the this object is used as its default value.

entries(): ArrayIterator<[number, number]>

Returns an array of key, value pairs for every entry in the array

equals(target: Buffer): boolean

Check whether this buffer and target have identical contents.

Parameters

ParameterTypeDefaultDescription
targetBuffer—The buffer to compare this buffer's contents against.

every(predicate: (value: number, index: number, array: this) => unknown, thisArg?: any): boolean

Determines whether all the members of an array satisfy the specified test.

Parameters

ParameterTypeDefaultDescription
predicate(value: number, index: number, array: this) => unknown—A function that accepts up to three arguments. The every method calls the predicate function for each element in the array until the predicate returns a value which is coercible to the Boolean value false, or until the end of the array.
thisArg?any—An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

fill(value: string, encoding?: BufferEncoding): this

Fill this buffer with value, repeating as needed, and return it.

Overloads:

fill(value: string, encoding?: BufferEncoding): this
fill(value: string, offset?: number, encoding?: BufferEncoding): this
fill(value: string, offset?: number, end?: number, encoding?: BufferEncoding): this
fill(value: Buffer | number | boolean, offset?: number, end?: number): this

Parameters

ParameterTypeDefaultDescription
valuestring—The value to fill with — a string (repeated across the range), or a Buffer/number/boolean byte.
encoding?BufferEncoding—Encoding used to interpret value when it's a string; defaults to 'utf8'.

Throws

  • Error — thrown if the encoding is not a recognized BufferEncoding.

filter

filter(predicate: (value: number, index: number, array: this) => any, thisArg?: any): Uint8Array<ArrayBuffer>

Returns the elements of an array that meet the condition specified in a callback function.

Parameters

ParameterTypeDefaultDescription
predicate(value: number, index: number, array: this) => any—A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.
thisArg?any—An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

find

find(predicate: (value: number, index: number, obj: this) => boolean, thisArg?: any): number | undefined

Returns the value of the first element in the array where predicate is true, and undefined otherwise.

Parameters

ParameterTypeDefaultDescription
predicate(value: number, index: number, obj: this) => boolean—find calls predicate once for each element of the array, in ascending order, until it finds one where predicate returns true. If such an element is found, find immediately returns that element value. Otherwise, find returns undefined.
thisArg?any—If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

findIndex

findIndex(predicate: (value: number, index: number, obj: this) => boolean, thisArg?: any): number

Returns the index of the first element in the array where predicate is true, and -1 otherwise.

Parameters

ParameterTypeDefaultDescription
predicate(value: number, index: number, obj: this) => boolean—find calls predicate once for each element of the array, in ascending order, until it finds one where predicate returns true. If such an element is found, findIndex immediately returns that element index. Otherwise, findIndex returns -1.
thisArg?any—If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

findLast

findLast<S extends number>(predicate: (
            value: number,
            index: number,
            array: this,
        ) => value is S, thisArg?: any): S | undefined

Returns the value of the last element in the array where predicate is true, and undefined otherwise.

Parameters

ParameterTypeDefaultDescription
predicate( value: number, index: number, array: this, ) => value is S—findLast calls predicate once for each element of the array, in descending order, until it finds one where predicate returns true. If such an element is found, findLast immediately returns that element value. Otherwise, findLast returns undefined.
thisArg?any—If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

findLastIndex

findLastIndex(predicate: (value: number, index: number, array: this) => unknown, thisArg?: any): number

Returns the index of the last element in the array where predicate is true, and -1 otherwise.

Parameters

ParameterTypeDefaultDescription
predicate(value: number, index: number, array: this) => unknown—findLastIndex calls predicate once for each element of the array, in descending order, until it finds one where predicate returns true. If such an element is found, findLastIndex immediately returns that element index. Otherwise, findLastIndex returns -1.
thisArg?any—If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

forEach(callbackfn: (value: number, index: number, array: this) => void, thisArg?: any): void

Performs the specified action for each element in an array.

Parameters

ParameterTypeDefaultDescription
callbackfn(value: number, index: number, array: this) => void—A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
thisArg?any—An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

includes(value: string, encoding?: BufferEncoding): boolean

Check whether this buffer contains value.

Overloads:

includes(value: string, encoding?: BufferEncoding): boolean
includes(value: string, offset?: number, encoding?: BufferEncoding): boolean
includes(value: Buffer | number | boolean, offset?: number): boolean

Parameters

ParameterTypeDefaultDescription
valuestring—The value to search for — a string, Buffer, number byte, or boolean.
encoding?BufferEncoding—Encoding used to interpret value when it's a string; defaults to 'utf8'.

Throws

  • Error — thrown if the encoding is not a recognized BufferEncoding.

indexOf(value: string, encoding?: BufferEncoding): number

Return the first index at which value occurs in this buffer, or -1 if not found.

Overloads:

indexOf(value: string, encoding?: BufferEncoding): number
indexOf(value: string, offset?: number, encoding?: BufferEncoding): number
indexOf(value: Buffer | number | boolean, offset?: number): number

Parameters

ParameterTypeDefaultDescription
valuestring—The value to search for — a string, Buffer, number byte, or boolean.
encoding?BufferEncoding—Encoding used to interpret value when it's a string; defaults to 'utf8'.

Throws

  • Error — thrown if the encoding is not a recognized BufferEncoding.

join(separator?: string): string

Adds all the elements of an array separated by the specified separator string.

Parameters

ParameterTypeDefaultDescription
separator?string—A string used to separate one element of an array from the next in the resulting String. If omitted, the array elements are separated with a comma.

keys(): ArrayIterator<number>

Returns an list of keys in the array

lastIndexOf(value: string, encoding?: BufferEncoding): number

Return the last index at which value occurs in this buffer, or -1 if not found.

Overloads:

lastIndexOf(value: string, encoding?: BufferEncoding): number
lastIndexOf(value: string, offset?: number, encoding?: BufferEncoding): number
lastIndexOf(value: Buffer | number | boolean, offset?: number): number

Parameters

ParameterTypeDefaultDescription
valuestring—The value to search for — a string, Buffer, number byte, or boolean.
encoding?BufferEncoding—Encoding used to interpret value when it's a string; defaults to 'utf8'.

Throws

  • Error — thrown if the encoding is not a recognized BufferEncoding.

length: number

The length of the array.

map

map(callbackfn: (value: number, index: number, array: this) => number, thisArg?: any): Uint8Array<ArrayBuffer>

Calls a defined callback function on each element of an array, and returns an array that contains the results.

Parameters

ParameterTypeDefaultDescription
callbackfn(value: number, index: number, array: this) => number—A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
thisArg?any—An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

readBigInt64BE(offset?: number): bigint

Read a signed 64-bit big-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
offset?number—Byte offset to read from; defaults to 0.

readBigInt64LE(offset?: number): bigint

Read a signed 64-bit little-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
offset?number—Byte offset to read from; defaults to 0.

readBigUint64BE(offset?: number): bigint

Read an unsigned 64-bit big-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
offset?number—Byte offset to read from; defaults to 0.

readBigUInt64BE(offset?: number): bigint

Read an unsigned 64-bit big-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
offset?number—Byte offset to read from; defaults to 0.

readBigUint64LE(offset?: number): bigint

Read an unsigned 64-bit little-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
offset?number—Byte offset to read from; defaults to 0.

readBigUInt64LE(offset?: number): bigint

Read an unsigned 64-bit little-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
offset?number—Byte offset to read from; defaults to 0.

readDoubleBE(offset?: number): number

Read a 64-bit big-endian double at offset.

Parameters

ParameterTypeDefaultDescription
offset?number—Byte offset to read from; defaults to 0.

readDoubleLE(offset?: number): number

Read a 64-bit little-endian double at offset.

Parameters

ParameterTypeDefaultDescription
offset?number—Byte offset to read from; defaults to 0.

readFloatBE(offset?: number): number

Read a 32-bit big-endian float at offset.

Parameters

ParameterTypeDefaultDescription
offset?number—Byte offset to read from; defaults to 0.

readFloatLE(offset?: number): number

Read a 32-bit little-endian float at offset.

Parameters

ParameterTypeDefaultDescription
offset?number—Byte offset to read from; defaults to 0.

readInt16BE(offset?: number): number

Read a signed 16-bit big-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
offset?number—Byte offset to read from; defaults to 0.

readInt16LE(offset?: number): number

Read a signed 16-bit little-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
offset?number—Byte offset to read from; defaults to 0.

readInt32BE(offset?: number): number

Read a signed 32-bit big-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
offset?number—Byte offset to read from; defaults to 0.

readInt32LE(offset?: number): number

Read a signed 32-bit little-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
offset?number—Byte offset to read from; defaults to 0.

readInt8(offset?: number): number

Read a signed 8-bit integer at offset.

Parameters

ParameterTypeDefaultDescription
offset?number—Byte offset to read from; defaults to 0.

readIntBE(offset: number, byteLength: number): number

Read a signed, big-endian, byteLength-byte integer at offset.

Parameters

ParameterTypeDefaultDescription
offsetnumber—Byte offset to read from.
byteLengthnumber—Number of bytes to read, from 1 to 6.

Throws

  • RangeError — thrown if byteLength is not between 1 and 6.

readIntLE(offset: number, byteLength: number): number

Read a signed, little-endian, byteLength-byte integer at offset.

Parameters

ParameterTypeDefaultDescription
offsetnumber—Byte offset to read from.
byteLengthnumber—Number of bytes to read, from 1 to 6.

Throws

  • RangeError — thrown if byteLength is not between 1 and 6.

readUint16BE(offset?: number): number

Read an unsigned 16-bit big-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
offset?number—Byte offset to read from; defaults to 0.

readUInt16BE(offset?: number): number

Read an unsigned 16-bit big-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
offset?number—Byte offset to read from; defaults to 0.

readUint16LE(offset?: number): number

Read an unsigned 16-bit little-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
offset?number—Byte offset to read from; defaults to 0.

readUInt16LE(offset?: number): number

Read an unsigned 16-bit little-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
offset?number—Byte offset to read from; defaults to 0.

readUint32BE(offset?: number): number

Read an unsigned 32-bit big-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
offset?number—Byte offset to read from; defaults to 0.

readUInt32BE(offset?: number): number

Read an unsigned 32-bit big-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
offset?number—Byte offset to read from; defaults to 0.

readUint32LE(offset?: number): number

Read an unsigned 32-bit little-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
offset?number—Byte offset to read from; defaults to 0.

readUInt32LE(offset?: number): number

Read an unsigned 32-bit little-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
offset?number—Byte offset to read from; defaults to 0.

readUint8(offset?: number): number

Read an unsigned 8-bit integer at offset.

Parameters

ParameterTypeDefaultDescription
offset?number—Byte offset to read from; defaults to 0.

readUInt8(offset?: number): number

Read an unsigned 8-bit integer at offset.

Parameters

ParameterTypeDefaultDescription
offset?number—Byte offset to read from; defaults to 0.

readUintBE(offset: number, byteLength: number): number

Read an unsigned, big-endian, byteLength-byte integer at offset.

Parameters

ParameterTypeDefaultDescription
offsetnumber—Byte offset to read from.
byteLengthnumber—Number of bytes to read, from 1 to 6.

Throws

  • RangeError — thrown if byteLength is not between 1 and 6.

readUIntBE(offset: number, byteLength: number): number

Read an unsigned, big-endian, byteLength-byte integer at offset.

Parameters

ParameterTypeDefaultDescription
offsetnumber—Byte offset to read from.
byteLengthnumber—Number of bytes to read, from 1 to 6.

Throws

  • RangeError — thrown if byteLength is not between 1 and 6.

readUintLE(offset: number, byteLength: number): number

Read an unsigned, little-endian, byteLength-byte integer at offset.

Parameters

ParameterTypeDefaultDescription
offsetnumber—Byte offset to read from.
byteLengthnumber—Number of bytes to read, from 1 to 6.

Throws

  • RangeError — thrown if byteLength is not between 1 and 6.

readUIntLE(offset: number, byteLength: number): number

Read an unsigned, little-endian, byteLength-byte integer at offset.

Parameters

ParameterTypeDefaultDescription
offsetnumber—Byte offset to read from.
byteLengthnumber—Number of bytes to read, from 1 to 6.

Throws

  • RangeError — thrown if byteLength is not between 1 and 6.

reduce

reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: this) => number): number

Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

Parameters

ParameterTypeDefaultDescription
callbackfn(previousValue: number, currentValue: number, currentIndex: number, array: this) => number—A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.

reduceRight

reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: this) => number): number

Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

Parameters

ParameterTypeDefaultDescription
callbackfn(previousValue: number, currentValue: number, currentIndex: number, array: this) => number—A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.

reverse(): this

Reverses the elements in an Array.

set(array: ArrayLike<number>, offset?: number): void

Sets a value or an array of values.

Parameters

ParameterTypeDefaultDescription
arrayArrayLike<number>—A typed or untyped array of values to set.
offset?number—The index in the current array at which the values are to be written.

slice(start?: number, end?: number): Uint8Array<ArrayBuffer>

Returns a section of an array.

Parameters

ParameterTypeDefaultDescription
start?number—The beginning of the specified portion of the array.
end?number—The end of the specified portion of the array. This is exclusive of the element at the index 'end'.

some(predicate: (value: number, index: number, array: this) => unknown, thisArg?: any): boolean

Determines whether the specified callback function returns true for any element of an array.

Parameters

ParameterTypeDefaultDescription
predicate(value: number, index: number, array: this) => unknown—A function that accepts up to three arguments. The some method calls the predicate function for each element in the array until the predicate returns a value which is coercible to the Boolean value true, or until the end of the array.
thisArg?any—An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

sort(compareFn?: (a: number, b: number) => number): this

Sorts an array.

Parameters

ParameterTypeDefaultDescription
compareFn?(a: number, b: number) => number—Function used to determine the order of the elements. It is expected to return a negative value if first argument is less than second argument, zero if they're equal and a positive value otherwise. If omitted, the elements are sorted in ascending order. ts [11,2,22,1].sort((a, b) => a - b)

subarray(begin?: number, end?: number): Uint8Array<TArrayBuffer>

Gets a new Uint8Array view of the ArrayBuffer store for this array, referencing the elements at begin, inclusive, up to end, exclusive.

Parameters

ParameterTypeDefaultDescription
begin?number—The index of the beginning of the array.
end?number—The index of the end of the array.

swap16(): this

Swap the byte order of each 16-bit group in this buffer in place, and return it.

Throws

  • RangeError — thrown if the buffer's length is not a multiple of 2 bytes.

swap32(): this

Swap the byte order of each 32-bit group in this buffer in place, and return it.

Throws

  • RangeError — thrown if the buffer's length is not a multiple of 4 bytes.

swap64(): this

Swap the byte order of each 64-bit group in this buffer in place, and return it.

Throws

  • RangeError — thrown if the buffer's length is not a multiple of 8 bytes.

toJSON(): number[]

Return an array of this buffer's bytes, used when the buffer is passed to JSON.stringify().

toLocaleString(): string

Converts a number to a string by using the current locale.

Overloads:

toLocaleString(): string
toLocaleString(locales: string | string[], options?: Intl.NumberFormatOptions): string

toReversed(): Uint8Array<ArrayBuffer>

Copies the array and returns the copy with the elements in reverse order.

toSorted(compareFn?: (a: number, b: number) => number): Uint8Array<ArrayBuffer>

Copies and sorts the array.

Parameters

ParameterTypeDefaultDescription
compareFn?(a: number, b: number) => number—Function used to determine the order of the elements. It is expected to return a negative value if the first argument is less than the second argument, zero if they're equal, and a positive value otherwise. If omitted, the elements are sorted in ascending order. ts const myNums = Uint8Array.from([11, 2, 22, 1]); myNums.toSorted((a, b) => a - b) // Uint8Array(4) [1, 2, 11, 22]

toString(encoding?: BufferEncoding, start?: number, end?: number): string

Decode this buffer (or a slice of it) to a string using encoding.

Parameters

ParameterTypeDefaultDescription
encoding?BufferEncoding—Encoding used to decode the bytes; defaults to 'utf8'.
start?number—Byte offset to start decoding from; defaults to 0.
end?number—Byte offset (exclusive) to stop decoding at; defaults to this.byteLength.

Throws

  • Error — thrown if the encoding is not a recognized BufferEncoding.

valueOf(): this

Returns the primitive value of the specified object.

values(): ArrayIterator<number>

Returns an list of values in the array

with(index: number, value: number): Uint8Array<ArrayBuffer>

Copies the array and inserts the given number at the provided index.

Parameters

ParameterTypeDefaultDescription
indexnumber—The index of the value to overwrite. If the index is negative, then it replaces from the end of the array.
valuenumber—The value to insert into the copied array.

Returns Uint8Array<ArrayBuffer> — A copy of the original array with the inserted value.

write(string: string, encoding?: BufferEncoding): number

Write string into this buffer using encoding, returning the number of bytes written.

Overloads:

write(string: string, encoding?: BufferEncoding): number
write(string: string, offset?: number, encoding?: BufferEncoding): number
write(string: string, offset?: number, length?: number, encoding?: BufferEncoding): number

Parameters

ParameterTypeDefaultDescription
stringstring—The string to write.
encoding?BufferEncoding—Encoding used to encode string; defaults to 'utf8'.

Throws

  • Error — thrown if the encoding is not a recognized BufferEncoding.

writeBigInt64BE(value: bigint, offset?: number): number

Write a signed 64-bit big-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
valuebigint—The signed bigint to write.
offset?number—Byte offset to write to; defaults to 0.

Returns number — offset + 8, the offset immediately following the written value.

writeBigInt64LE(value: bigint, offset?: number): number

Write a signed 64-bit little-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
valuebigint—The signed bigint to write.
offset?number—Byte offset to write to; defaults to 0.

Returns number — offset + 8, the offset immediately following the written value.

writeBigUint64BE(value: bigint, offset?: number): number

Write an unsigned 64-bit big-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
valuebigint—The unsigned bigint to write.
offset?number—Byte offset to write to; defaults to 0.

Returns number — offset + 8, the offset immediately following the written value.

writeBigUInt64BE(value: bigint, offset?: number): number

Write an unsigned 64-bit big-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
valuebigint—The unsigned bigint to write.
offset?number—Byte offset to write to; defaults to 0.

Returns number — offset + 8, the offset immediately following the written value.

writeBigUint64LE(value: bigint, offset?: number): number

Write an unsigned 64-bit little-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
valuebigint—The unsigned bigint to write.
offset?number—Byte offset to write to; defaults to 0.

Returns number — offset + 8, the offset immediately following the written value.

writeBigUInt64LE(value: bigint, offset?: number): number

Write an unsigned 64-bit little-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
valuebigint—The unsigned bigint to write.
offset?number—Byte offset to write to; defaults to 0.

Returns number — offset + 8, the offset immediately following the written value.

writeDoubleBE(value: number, offset?: number): number

Write a 64-bit big-endian double at offset.

Parameters

ParameterTypeDefaultDescription
valuenumber—The double-precision number to write.
offset?number—Byte offset to write to; defaults to 0.

Returns number — offset + 8, the offset immediately following the written value.

writeDoubleLE(value: number, offset?: number): number

Write a 64-bit little-endian double at offset.

Parameters

ParameterTypeDefaultDescription
valuenumber—The double-precision number to write.
offset?number—Byte offset to write to; defaults to 0.

Returns number — offset + 8, the offset immediately following the written value.

writeFloatBE(value: number, offset?: number): number

Write a 32-bit big-endian float at offset.

Parameters

ParameterTypeDefaultDescription
valuenumber—The single-precision number to write.
offset?number—Byte offset to write to; defaults to 0.

Returns number — offset + 4, the offset immediately following the written value.

writeFloatLE(value: number, offset?: number): number

Write a 32-bit little-endian float at offset.

Parameters

ParameterTypeDefaultDescription
valuenumber—The single-precision number to write.
offset?number—Byte offset to write to; defaults to 0.

Returns number — offset + 4, the offset immediately following the written value.

writeInt16BE(value: number, offset?: number): number

Write a signed 16-bit big-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
valuenumber—The signed integer to write.
offset?number—Byte offset to write to; defaults to 0.

Returns number — offset + 2, the offset immediately following the written value.

writeInt16LE(value: number, offset?: number): number

Write a signed 16-bit little-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
valuenumber—The signed integer to write.
offset?number—Byte offset to write to; defaults to 0.

Returns number — offset + 2, the offset immediately following the written value.

writeInt32BE(value: number, offset?: number): number

Write a signed 32-bit big-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
valuenumber—The signed integer to write.
offset?number—Byte offset to write to; defaults to 0.

Returns number — offset + 4, the offset immediately following the written value.

writeInt32LE(value: number, offset?: number): number

Write a signed 32-bit little-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
valuenumber—The signed integer to write.
offset?number—Byte offset to write to; defaults to 0.

Returns number — offset + 4, the offset immediately following the written value.

writeInt8(value: number, offset?: number): number

Write a signed 8-bit integer at offset.

Parameters

ParameterTypeDefaultDescription
valuenumber—The signed integer to write.
offset?number—Byte offset to write to; defaults to 0.

Returns number — offset + 1, the offset immediately following the written value.

writeIntBE(value: number, offset: number, byteLength: number): number

Write a signed, big-endian, byteLength-byte integer at offset.

Parameters

ParameterTypeDefaultDescription
valuenumber—The signed integer to write.
offsetnumber—Byte offset to write to.
byteLengthnumber—Number of bytes to write, from 1 to 6.

Returns number — offset + byteLength, the offset immediately following the written value.

Throws

  • RangeError — thrown if byteLength is not between 1 and 6.

writeIntLE(value: number, offset: number, byteLength: number): number

Write a signed, little-endian, byteLength-byte integer at offset.

Parameters

ParameterTypeDefaultDescription
valuenumber—The signed integer to write.
offsetnumber—Byte offset to write to.
byteLengthnumber—Number of bytes to write, from 1 to 6.

Returns number — offset + byteLength, the offset immediately following the written value.

Throws

  • RangeError — thrown if byteLength is not between 1 and 6.

writeUint16BE(value: number, offset?: number): number

Write an unsigned 16-bit big-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
valuenumber—The unsigned integer to write.
offset?number—Byte offset to write to; defaults to 0.

Returns number — offset + 2, the offset immediately following the written value.

writeUInt16BE(value: number, offset?: number): number

Write an unsigned 16-bit big-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
valuenumber—The unsigned integer to write.
offset?number—Byte offset to write to; defaults to 0.

Returns number — offset + 2, the offset immediately following the written value.

writeUint16LE(value: number, offset?: number): number

Write an unsigned 16-bit little-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
valuenumber—The unsigned integer to write.
offset?number—Byte offset to write to; defaults to 0.

Returns number — offset + 2, the offset immediately following the written value.

writeUInt16LE(value: number, offset?: number): number

Write an unsigned 16-bit little-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
valuenumber—The unsigned integer to write.
offset?number—Byte offset to write to; defaults to 0.

Returns number — offset + 2, the offset immediately following the written value.

writeUint32BE(value: number, offset?: number): number

Write an unsigned 32-bit big-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
valuenumber—The unsigned integer to write.
offset?number—Byte offset to write to; defaults to 0.

Returns number — offset + 4, the offset immediately following the written value.

writeUInt32BE(value: number, offset?: number): number

Write an unsigned 32-bit big-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
valuenumber—The unsigned integer to write.
offset?number—Byte offset to write to; defaults to 0.

Returns number — offset + 4, the offset immediately following the written value.

writeUint32LE(value: number, offset?: number): number

Write an unsigned 32-bit little-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
valuenumber—The unsigned integer to write.
offset?number—Byte offset to write to; defaults to 0.

Returns number — offset + 4, the offset immediately following the written value.

writeUInt32LE(value: number, offset?: number): number

Write an unsigned 32-bit little-endian integer at offset.

Parameters

ParameterTypeDefaultDescription
valuenumber—The unsigned integer to write.
offset?number—Byte offset to write to; defaults to 0.

Returns number — offset + 4, the offset immediately following the written value.

writeUint8(value: number, offset?: number): number

Write an unsigned 8-bit integer at offset.

Parameters

ParameterTypeDefaultDescription
valuenumber—The unsigned integer to write.
offset?number—Byte offset to write to; defaults to 0.

Returns number — offset + 1, the offset immediately following the written value.

writeUInt8(value: number, offset?: number): number

Write an unsigned 8-bit integer at offset.

Parameters

ParameterTypeDefaultDescription
valuenumber—The unsigned integer to write.
offset?number—Byte offset to write to; defaults to 0.

Returns number — offset + 1, the offset immediately following the written value.

writeUintBE(value: number, offset: number, byteLength: number): number

Write an unsigned, big-endian, byteLength-byte integer at offset.

Parameters

ParameterTypeDefaultDescription
valuenumber—The unsigned integer to write.
offsetnumber—Byte offset to write to.
byteLengthnumber—Number of bytes to write, from 1 to 6.

Returns number — offset + byteLength, the offset immediately following the written value.

Throws

  • RangeError — thrown if byteLength is not between 1 and 6.

writeUIntBE(value: number, offset: number, byteLength: number): number

Write an unsigned, big-endian, byteLength-byte integer at offset.

Parameters

ParameterTypeDefaultDescription
valuenumber—The unsigned integer to write.
offsetnumber—Byte offset to write to.
byteLengthnumber—Number of bytes to write, from 1 to 6.

Returns number — offset + byteLength, the offset immediately following the written value.

Throws

  • RangeError — thrown if byteLength is not between 1 and 6.

writeUintLE(value: number, offset: number, byteLength: number): number

Write an unsigned, little-endian, byteLength-byte integer at offset.

Parameters

ParameterTypeDefaultDescription
valuenumber—The unsigned integer to write.
offsetnumber—Byte offset to write to.
byteLengthnumber—Number of bytes to write, from 1 to 6.

Returns number — offset + byteLength, the offset immediately following the written value.

Throws

  • RangeError — thrown if byteLength is not between 1 and 6.

writeUIntLE(value: number, offset: number, byteLength: number): number

Write an unsigned, little-endian, byteLength-byte integer at offset.

Parameters

ParameterTypeDefaultDescription
valuenumber—The unsigned integer to write.
offsetnumber—Byte offset to write to.
byteLengthnumber—Number of bytes to write, from 1 to 6.

Returns number — offset + byteLength, the offset immediately following the written value.

Throws

  • RangeError — thrown if byteLength is not between 1 and 6.

bare-buffer/global

Types

BufferConstructor

type BufferConstructor = typeof buffer.Buffer

bare-buffer/constants

Constants and variables

constants: { MAX_LENGTH: number; MAX_STRING_LENGTH: number }

Limits for buffers and strings: MAX_LENGTH (largest possible Buffer) and MAX_STRING_LENGTH (largest possible string).

See also

Last updated on

Was this helpful?

On this page

Usage
API
Buffer
new Buffer(arrayBuffer: ArrayBuffer, offset?: number, length?: number)
at(index: number): number | undefined
buffer: TArrayBuffer
Buffer.alloc(size: number, fill: string, encoding?: BufferEncoding): Buffer
Buffer.allocUnsafe(size: number): Buffer
Buffer.allocUnsafeSlow(size: number): Buffer
Buffer.atob(data: unknown): string
Buffer.btoa(data: unknown): string
Buffer.BufferEncoding
Buffer.byteLength
Buffer.coerce(buffer: Buffer): Buffer
Buffer.compare(a: Buffer, b: Buffer): number
Buffer.concat(buffers: Buffer[], length?: number): Buffer
Buffer.constants: { MAX_LENGTH: number; MAX_STRING_LENGTH: number }
Buffer.copyBytesFrom(view: ArrayBufferLike, offset?: number, length?: number): Buffer
Buffer.from(data: Iterable<number>): Buffer
Buffer.isAscii(buffer: Buffer): boolean
Buffer.isASCII(buffer: Buffer): boolean
Buffer.isBuffer(value: unknown): value is Buffer
Buffer.isEncoding(encoding: string): encoding is BufferEncoding
Buffer.isUtf8(buffer: Buffer): boolean
Buffer.isUTF8(buffer: Buffer): boolean
Buffer.poolSize: number
Buffer.transcode(buffer: Buffer, from: BufferEncoding, to: BufferEncoding): Buffer
byteOffset: number
BYTES_PER_ELEMENT: number
compare
copy(target: Buffer, targetStart?: number, sourceStart?: number, sourceEnd?: number): number
copyWithin(target: number, start: number, end?: number): this
entries(): ArrayIterator<[number, number]>
equals(target: Buffer): boolean
every(predicate: (value: number, index: number, array: this) => unknown, thisArg?: any): boolean
fill(value: string, encoding?: BufferEncoding): this
filter
find
findIndex
findLast
findLastIndex
forEach(callbackfn: (value: number, index: number, array: this) => void, thisArg?: any): void
includes(value: string, encoding?: BufferEncoding): boolean
indexOf(value: string, encoding?: BufferEncoding): number
join(separator?: string): string
keys(): ArrayIterator<number>
lastIndexOf(value: string, encoding?: BufferEncoding): number
length: number
map
readBigInt64BE(offset?: number): bigint
readBigInt64LE(offset?: number): bigint
readBigUint64BE(offset?: number): bigint
readBigUInt64BE(offset?: number): bigint
readBigUint64LE(offset?: number): bigint
readBigUInt64LE(offset?: number): bigint
readDoubleBE(offset?: number): number
readDoubleLE(offset?: number): number
readFloatBE(offset?: number): number
readFloatLE(offset?: number): number
readInt16BE(offset?: number): number
readInt16LE(offset?: number): number
readInt32BE(offset?: number): number
readInt32LE(offset?: number): number
readInt8(offset?: number): number
readIntBE(offset: number, byteLength: number): number
readIntLE(offset: number, byteLength: number): number
readUint16BE(offset?: number): number
readUInt16BE(offset?: number): number
readUint16LE(offset?: number): number
readUInt16LE(offset?: number): number
readUint32BE(offset?: number): number
readUInt32BE(offset?: number): number
readUint32LE(offset?: number): number
readUInt32LE(offset?: number): number
readUint8(offset?: number): number
readUInt8(offset?: number): number
readUintBE(offset: number, byteLength: number): number
readUIntBE(offset: number, byteLength: number): number
readUintLE(offset: number, byteLength: number): number
readUIntLE(offset: number, byteLength: number): number
reduce
reduceRight
reverse(): this
set(array: ArrayLike<number>, offset?: number): void
slice(start?: number, end?: number): Uint8Array<ArrayBuffer>
some(predicate: (value: number, index: number, array: this) => unknown, thisArg?: any): boolean
sort(compareFn?: (a: number, b: number) => number): this
subarray(begin?: number, end?: number): Uint8Array<TArrayBuffer>
swap16(): this
swap32(): this
swap64(): this
toJSON(): number[]
toLocaleString(): string
toReversed(): Uint8Array<ArrayBuffer>
toSorted(compareFn?: (a: number, b: number) => number): Uint8Array<ArrayBuffer>
toString(encoding?: BufferEncoding, start?: number, end?: number): string
valueOf(): this
values(): ArrayIterator<number>
with(index: number, value: number): Uint8Array<ArrayBuffer>
write(string: string, encoding?: BufferEncoding): number
writeBigInt64BE(value: bigint, offset?: number): number
writeBigInt64LE(value: bigint, offset?: number): number
writeBigUint64BE(value: bigint, offset?: number): number
writeBigUInt64BE(value: bigint, offset?: number): number
writeBigUint64LE(value: bigint, offset?: number): number
writeBigUInt64LE(value: bigint, offset?: number): number
writeDoubleBE(value: number, offset?: number): number
writeDoubleLE(value: number, offset?: number): number
writeFloatBE(value: number, offset?: number): number
writeFloatLE(value: number, offset?: number): number
writeInt16BE(value: number, offset?: number): number
writeInt16LE(value: number, offset?: number): number
writeInt32BE(value: number, offset?: number): number
writeInt32LE(value: number, offset?: number): number
writeInt8(value: number, offset?: number): number
writeIntBE(value: number, offset: number, byteLength: number): number
writeIntLE(value: number, offset: number, byteLength: number): number
writeUint16BE(value: number, offset?: number): number
writeUInt16BE(value: number, offset?: number): number
writeUint16LE(value: number, offset?: number): number
writeUInt16LE(value: number, offset?: number): number
writeUint32BE(value: number, offset?: number): number
writeUInt32BE(value: number, offset?: number): number
writeUint32LE(value: number, offset?: number): number
writeUInt32LE(value: number, offset?: number): number
writeUint8(value: number, offset?: number): number
writeUInt8(value: number, offset?: number): number
writeUintBE(value: number, offset: number, byteLength: number): number
writeUIntBE(value: number, offset: number, byteLength: number): number
writeUintLE(value: number, offset: number, byteLength: number): number
writeUIntLE(value: number, offset: number, byteLength: number): number
bare-buffer/global
Types
BufferConstructor
bare-buffer/constants
Constants and variables
constants: { MAX_LENGTH: number; MAX_STRING_LENGTH: number }
See also