bare-buffer
Native buffers for JavaScript
v3.7.1bare-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-bufferUsage
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
| Parameter | Type | Default | Description |
|---|---|---|---|
arrayBuffer | ArrayBuffer | — | 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 exceedBuffer.constants.MAX_LENGTH.
at(index: number): number | undefined
Returns the item located at the specified index.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
index | number | — | 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): BufferParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
size | number | — | Number of bytes to allocate. |
fill | string | — | 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 ifsizeexceedsBuffer.constants.MAX_LENGTH.
Buffer.allocUnsafe(size: number): Buffer
Allocate a new Buffer of size bytes without zeroing its contents first.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
size | number | — | Number of bytes to allocate. |
Throws
RangeError— thrown ifsizeexceedsBuffer.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
| Parameter | Type | Default | Description |
|---|---|---|---|
size | number | — | Number of bytes to allocate. |
Throws
RangeError— thrown ifsizeexceedsBuffer.constants.MAX_LENGTH.
Buffer.atob(data: unknown): string
Decode a base64-encoded string into a Latin-1 binary string.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
data | unknown | — | The base64-encoded string to decode. |
Buffer.btoa(data: unknown): string
Encode a Latin-1 binary string into a base64 string.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
data | unknown | — | 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): numberReturn the number of bytes string would occupy once encoded as encoding.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
string | ArrayBufferView | 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 recognizedBufferEncoding.
Buffer.coerce(buffer: Buffer): Buffer
Return buffer unchanged if it is already a Buffer, otherwise wrap its underlying
ArrayBuffer in a new Buffer.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
buffer | Buffer | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
a | Buffer | — | The first buffer to compare. |
b | Buffer | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
buffers | Buffer[] | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
view | ArrayBufferLike | — | 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 ifoffset + lengthexceedsview.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): BufferParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
data | Iterable<number> | — | The array, array-like, string, buffer, or ArrayBuffer to create a new Buffer from. |
Throws
Error— thrown if the encoding is not a recognizedBufferEncoding.
Buffer.isAscii(buffer: Buffer): boolean
Check whether buffer contains only valid ASCII-encoded data.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
buffer | Buffer | — | The buffer to check. |
Buffer.isASCII(buffer: Buffer): boolean
Check whether buffer contains only valid ASCII-encoded data.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
buffer | Buffer | — | The buffer to check. |
Buffer.isBuffer(value: unknown): value is Buffer
Check whether value is a Buffer.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
value | unknown | — | The value to check. |
Buffer.isEncoding(encoding: string): encoding is BufferEncoding
Check whether encoding is a supported BufferEncoding.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
encoding | string | — | The encoding name to check. |
Buffer.isUtf8(buffer: Buffer): boolean
Check whether buffer contains only valid UTF-8-encoded data.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
buffer | Buffer | — | The buffer to check. |
Buffer.isUTF8(buffer: Buffer): boolean
Check whether buffer contains only valid UTF-8-encoded data.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
buffer | Buffer | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
buffer | Buffer | — | The buffer to re-encode. |
from | BufferEncoding | — | The encoding buffer is currently in. |
to | BufferEncoding | — | The encoding to convert to. |
Throws
Error— thrown if the encoding is not a recognizedBufferEncoding.
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): numberCompare this buffer's contents against target lexicographically, optionally comparing only
a sub-range of each, returning -1, 0, or 1 for sort ordering.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
target | Buffer | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
target | Buffer | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
target | number | — | If target is negative, it is treated as length+target where length is the length of the array. |
start | number | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
target | Buffer | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
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): thisParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
value | string | — | 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 recognizedBufferEncoding.
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
| Parameter | Type | Default | Description |
|---|---|---|---|
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 | undefinedReturns the value of the first element in the array where predicate is true, and undefined otherwise.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
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): numberReturns the index of the first element in the array where predicate is true, and -1 otherwise.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
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 | undefinedReturns the value of the last element in the array where predicate is true, and undefined otherwise.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
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): numberReturns the index of the last element in the array where predicate is true, and -1 otherwise.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
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
| Parameter | Type | Default | Description |
|---|---|---|---|
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): booleanParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
value | string | — | 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 recognizedBufferEncoding.
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): numberParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
value | string | — | 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 recognizedBufferEncoding.
join(separator?: string): string
Adds all the elements of an array separated by the specified separator string.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
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): numberParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
value | string | — | 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 recognizedBufferEncoding.
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
| Parameter | Type | Default | Description |
|---|---|---|---|
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
| Parameter | Type | Default | Description |
|---|---|---|---|
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
| Parameter | Type | Default | Description |
|---|---|---|---|
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
| Parameter | Type | Default | Description |
|---|---|---|---|
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
| Parameter | Type | Default | Description |
|---|---|---|---|
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
| Parameter | Type | Default | Description |
|---|---|---|---|
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
| Parameter | Type | Default | Description |
|---|---|---|---|
offset? | number | — | Byte offset to read from; defaults to 0. |
readDoubleBE(offset?: number): number
Read a 64-bit big-endian double at offset.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
offset? | number | — | Byte offset to read from; defaults to 0. |
readDoubleLE(offset?: number): number
Read a 64-bit little-endian double at offset.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
offset? | number | — | Byte offset to read from; defaults to 0. |
readFloatBE(offset?: number): number
Read a 32-bit big-endian float at offset.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
offset? | number | — | Byte offset to read from; defaults to 0. |
readFloatLE(offset?: number): number
Read a 32-bit little-endian float at offset.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
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
| Parameter | Type | Default | Description |
|---|---|---|---|
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
| Parameter | Type | Default | Description |
|---|---|---|---|
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
| Parameter | Type | Default | Description |
|---|---|---|---|
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
| Parameter | Type | Default | Description |
|---|---|---|---|
offset? | number | — | Byte offset to read from; defaults to 0. |
readInt8(offset?: number): number
Read a signed 8-bit integer at offset.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
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
| Parameter | Type | Default | Description |
|---|---|---|---|
offset | number | — | Byte offset to read from. |
byteLength | number | — | Number of bytes to read, from 1 to 6. |
Throws
RangeError— thrown ifbyteLengthis not between1and6.
readIntLE(offset: number, byteLength: number): number
Read a signed, little-endian, byteLength-byte integer at offset.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
offset | number | — | Byte offset to read from. |
byteLength | number | — | Number of bytes to read, from 1 to 6. |
Throws
RangeError— thrown ifbyteLengthis not between1and6.
readUint16BE(offset?: number): number
Read an unsigned 16-bit big-endian integer at offset.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
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
| Parameter | Type | Default | Description |
|---|---|---|---|
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
| Parameter | Type | Default | Description |
|---|---|---|---|
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
| Parameter | Type | Default | Description |
|---|---|---|---|
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
| Parameter | Type | Default | Description |
|---|---|---|---|
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
| Parameter | Type | Default | Description |
|---|---|---|---|
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
| Parameter | Type | Default | Description |
|---|---|---|---|
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
| Parameter | Type | Default | Description |
|---|---|---|---|
offset? | number | — | Byte offset to read from; defaults to 0. |
readUint8(offset?: number): number
Read an unsigned 8-bit integer at offset.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
offset? | number | — | Byte offset to read from; defaults to 0. |
readUInt8(offset?: number): number
Read an unsigned 8-bit integer at offset.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
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
| Parameter | Type | Default | Description |
|---|---|---|---|
offset | number | — | Byte offset to read from. |
byteLength | number | — | Number of bytes to read, from 1 to 6. |
Throws
RangeError— thrown ifbyteLengthis not between1and6.
readUIntBE(offset: number, byteLength: number): number
Read an unsigned, big-endian, byteLength-byte integer at offset.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
offset | number | — | Byte offset to read from. |
byteLength | number | — | Number of bytes to read, from 1 to 6. |
Throws
RangeError— thrown ifbyteLengthis not between1and6.
readUintLE(offset: number, byteLength: number): number
Read an unsigned, little-endian, byteLength-byte integer at offset.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
offset | number | — | Byte offset to read from. |
byteLength | number | — | Number of bytes to read, from 1 to 6. |
Throws
RangeError— thrown ifbyteLengthis not between1and6.
readUIntLE(offset: number, byteLength: number): number
Read an unsigned, little-endian, byteLength-byte integer at offset.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
offset | number | — | Byte offset to read from. |
byteLength | number | — | Number of bytes to read, from 1 to 6. |
Throws
RangeError— thrown ifbyteLengthis not between1and6.
reduce
reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: this) => number): numberCalls 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
| Parameter | Type | Default | Description |
|---|---|---|---|
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): numberCalls 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
| Parameter | Type | Default | Description |
|---|---|---|---|
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
| Parameter | Type | Default | Description |
|---|---|---|---|
array | ArrayLike<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
| Parameter | Type | Default | Description |
|---|---|---|---|
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
| Parameter | Type | Default | Description |
|---|---|---|---|
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
| Parameter | Type | Default | Description |
|---|---|---|---|
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
| Parameter | Type | Default | Description |
|---|---|---|---|
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): stringtoReversed(): 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
| Parameter | Type | Default | Description |
|---|---|---|---|
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
| Parameter | Type | Default | Description |
|---|---|---|---|
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 recognizedBufferEncoding.
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
| Parameter | Type | Default | Description |
|---|---|---|---|
index | number | — | The index of the value to overwrite. If the index is negative, then it replaces from the end of the array. |
value | number | — | 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): numberParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
string | string | — | The string to write. |
encoding? | BufferEncoding | — | Encoding used to encode string; defaults to 'utf8'. |
Throws
Error— thrown if the encoding is not a recognizedBufferEncoding.
writeBigInt64BE(value: bigint, offset?: number): number
Write a signed 64-bit big-endian integer at offset.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
value | bigint | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
value | bigint | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
value | bigint | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
value | bigint | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
value | bigint | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
value | bigint | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | — | The signed integer to write. |
offset | number | — | Byte offset to write to. |
byteLength | number | — | Number of bytes to write, from 1 to 6. |
Returns number — offset + byteLength, the offset immediately following the written value.
Throws
RangeError— thrown ifbyteLengthis not between1and6.
writeIntLE(value: number, offset: number, byteLength: number): number
Write a signed, little-endian, byteLength-byte integer at offset.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | — | The signed integer to write. |
offset | number | — | Byte offset to write to. |
byteLength | number | — | Number of bytes to write, from 1 to 6. |
Returns number — offset + byteLength, the offset immediately following the written value.
Throws
RangeError— thrown ifbyteLengthis not between1and6.
writeUint16BE(value: number, offset?: number): number
Write an unsigned 16-bit big-endian integer at offset.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | — | 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
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | — | The unsigned integer to write. |
offset | number | — | Byte offset to write to. |
byteLength | number | — | Number of bytes to write, from 1 to 6. |
Returns number — offset + byteLength, the offset immediately following the written value.
Throws
RangeError— thrown ifbyteLengthis not between1and6.
writeUIntBE(value: number, offset: number, byteLength: number): number
Write an unsigned, big-endian, byteLength-byte integer at offset.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | — | The unsigned integer to write. |
offset | number | — | Byte offset to write to. |
byteLength | number | — | Number of bytes to write, from 1 to 6. |
Returns number — offset + byteLength, the offset immediately following the written value.
Throws
RangeError— thrown ifbyteLengthis not between1and6.
writeUintLE(value: number, offset: number, byteLength: number): number
Write an unsigned, little-endian, byteLength-byte integer at offset.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | — | The unsigned integer to write. |
offset | number | — | Byte offset to write to. |
byteLength | number | — | Number of bytes to write, from 1 to 6. |
Returns number — offset + byteLength, the offset immediately following the written value.
Throws
RangeError— thrown ifbyteLengthis not between1and6.
writeUIntLE(value: number, offset: number, byteLength: number): number
Write an unsigned, little-endian, byteLength-byte integer at offset.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | — | The unsigned integer to write. |
offset | number | — | Byte offset to write to. |
byteLength | number | — | Number of bytes to write, from 1 to 6. |
Returns number — offset + byteLength, the offset immediately following the written value.
Throws
RangeError— thrown ifbyteLengthis not between1and6.
bare-buffer/global
Types
BufferConstructor
type BufferConstructor = typeof buffer.Bufferbare-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
- Bare modules — the full
bare-*catalog. - Bare runtime API — the runtime these modules extend.
Last updated on