Creates a zero-length Buffer
.
Creates a new Buffer
with length
bytes. The contents are initialized to 0
.
The desired length of the new Buffer
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = new Buffer(2)
console.log(buf.length) // Prints: 2
})()
Creates a new Buffer
copied from array
. TypedArray
will be treated as an Array
.
An array of bytes in the range 0
– 255
. Array entries outside that range will be truncated to fit into it.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf1 = new Buffer([21, 31])
console.log(buf1.toString('hex')) // Prints: 151f
const buf2 = new Buffer(new Uint16Array([0x1234, 0x5678]))
console.log(buf2.toString('hex')) // Prints: 3478
const buf3 = Buffer.from('0102', 'hex')
const buf4 = new Buffer(buf3)
buf3[0] = 0x03
console.log(buf3.toString('hex')) // Prints: 0302
console.log(buf4.toString('hex')) // Prints: 0102
})()
Creates a view of the ArrayBuffer
without copying the underlying memory. For example, when passed a reference to the .buffer
property of a TypedArray
instance, the newly created Buffer
will share the same allocated memory as the TypedArray
's underlying ArrayBuffer
.
An ArrayBuffer
or SharedArrayBuffer
, for example the .buffer
property of a TypedArray
.
Optional
byteOffset: numberIndex of first byte to expose. Default: 0
.
Optional
length: numberNumber of bytes to expose. Default: arrayBuffer.byteLength - byteOffset
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const uint16 = new Uint16Array([5000, 4000])
const buf = Buffer.from(uint16.buffer) // Shares memory with `uint16`.
console.log(buf.toString('hex')) // Prints: 8813a00f
uint16[1] = 6000
console.log(buf.toString('hex')) // Prints: 88137017
})()
Private
Readonly
#dvReadonly
BYTES_The size in bytes of each element in the array.
Readonly
[toReadonly
bufferThe underlying ArrayBuffer
object based on which this Buffer
object is created.
Readonly
byteThe length in bytes of the array.
Readonly
byteThe offset in bytes of the array.
Readonly
lengthThe length of the array.
Static
Readonly
BYTES_The size in bytes of each element in the array.
Static
allocAllocates a new Buffer
of size
bytes. If fill
is undefined
, the Buffer
will be zero-filled.
The desired length of the new Buffer
.
Optional
fill: number | Uint8Array | BufferA value to pre-fill the new Buffer
with. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.alloc(5)
console.log(buf.toString('hex')) // Prints: 0000000000
})()
Allocates a new Buffer
of size
bytes. If fill
is undefined
, the Buffer
will be zero-filled.
The desired length of the new Buffer
.
A value to pre-fill the new Buffer
with. Default: 0
.
Optional
encoding: "base64" | "binary" | "ucs-2" | "utf-16le" | "utf-8" | "ascii" | "base64url" | "hex" | "latin1" | "ucs2" | "utf16le" | "utf8"The encoding of fill
. Default: 'utf8'
.
If fill
is specified, the allocated Buffer
will be initialized by calling buf.fill(fill)
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.alloc(5, 'a')
console.log(buf.toString('hex')) // Prints: 6161616161
})()
If both fill
and encoding
are specified, the allocated Buffer
will be initialized by calling buf.fill(fill, encoding)
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=', 'base64')
console.log(buf.toString('hex')) // Prints: 68656c6c6f20776f726c64
})()
Static
allocAllocates a new Buffer
of size
bytes. The contents are initialized to 0
. This is equivalent to calling new Buffer(size)
.
The desired length of the new Buffer.
This method is different from Node.js's Buffer.allocUnsafe()
method.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.allocUnsafe(10)
console.log(buf.toString('hex')) // Prints: 00000000000000000000
})()
Static
allocAllocates a new Buffer
of size
bytes. The contents are initialized to 0
. This is equivalent to calling new Buffer(size)
.
The desired length of the new Buffer.
This method is different from Node.js's Buffer.allocUnsafeSlow()
method.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.allocUnsafeSlow(10)
console.log(buf.toString('hex')) // Prints: 00000000000000000000
})()
Static
byteReturns .byteLength
if value
is a Buffer
/DataView
/TypedArray
/ArrayBuffer
/SharedArrayBuffer
.
A value to calculate the length of bytes.
The number of bytes contained within value
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const u8arr = new Uint8Array(5)
console.log(Buffer.byteLength(u8arr)) // Prints: 5
})()
Returns the byte length of a string when encoded using encoding
. This is not the same as String.prototype.length
, which does not account for the encoding
that is used to convert the string into bytes.
For 'base64'
, 'base64url'
, and 'hex'
, this method assumes value
is valid. For strings that contain non-base64/hex-encoded data (e.g. whitespace), the return value might be greater than the length of a Buffer
created from the string.
A value to calculate the length of bytes.
Optional
encoding: "base64" | "binary" | "ucs-2" | "utf-16le" | "utf-8" | "ascii" | "base64url" | "hex" | "latin1" | "ucs2" | "utf16le" | "utf8"The encoding of string
. Default: 'utf8'
.
The number of bytes contained within string
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const str = '\u00bd + \u00bc = \u00be'
console.log(`${str}: ${str.length} characters, ${Buffer.byteLength(str, 'utf8')} bytes`)
// Prints: ½ + ¼ = ¾: 9 characters, 12 bytes
})()
Static
compareCompares buf1
to buf2
, typically for the purpose of sorting arrays of Buffer
instances. This is equivalent to calling buf1.compare(buf2)
.
Either -1
, 0
, or 1
, depending on the result of the comparison. See buf.compare()
for details.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf1 = Buffer.from('1234')
const buf2 = Buffer.from('0123')
console.log([buf1, buf2].sort(Buffer.compare).map(buf => buf.toString('hex')))
// Prints: ['30313233', '31323334']
// (This result is equal to: [buf2, buf1].)
console.log([buf2, buf1].sort(Buffer.compare).map(buf => buf.toString('hex')))
// Prints: ['30313233', '31323334']
})()
Static
concatReturns a new Buffer
which is the result of concatenating all the Buffer
instances in the list
together.
If the list
has no items, or if the totalLength
is 0, then a new zero-length Buffer
is returned.
If totalLength
is not provided, it is calculated from the Buffer
instances in list
by adding their lengths.
If totalLength
is provided, it is coerced to an unsigned integer. If the combined length of the Buffer
s in list
exceeds totalLength
, the result is truncated to totalLength
.
List of Buffer
or Uint8Array
instances to concatenate.
Optional
totalLength: numberTotal length of the Buffer
instances in list
when concatenated.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf1 = Buffer.alloc(4)
const buf2 = Buffer.alloc(5)
const buf3 = Buffer.alloc(6)
const totalLength = buf1.length + buf2.length + buf3.length
console.log(totalLength) // Prints: 15
const bufA = Buffer.concat([buf1, buf2, buf3], totalLength)
console.log(bufA.toString('hex'))
// Prints: 000000000000000000000000000000
console.log(bufA.length) // Prints: 15
})()
Static
copyCopies the underlying memory of view
into a new Buffer
.
The TypedArray
to copy.
The starting offset within view
. Default: 0
.
Optional
length: numberThe number of elements from view
to copy. Default: view.length - offset
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const u16 = new Uint16Array([0, 0xffff])
const buf = Buffer.copyBytesFrom(u16, 1, 1)
u16[1] = 0
console.log(buf.length) // Prints: 2
console.log(buf.toString('hex')) // Prints: ffff
})()
Static
equalsture
if buf1
and buf2
have the same byte length and contents, or false
otherwise.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
console.log(Buffer.equals(new Buffer([1, 2]), Buffer.from('0102', 'hex'))) // true
console.log(Buffer.equals(Buffer.from(new Uint8Array([1, 2])), Buffer.from('0102', 'hex'))) // true
console.log(Buffer.equals(new Uint8Array([1, 2]), Buffer.from('0102', 'hex'))) // false
console.log(Buffer.equals(1, new Uint8Array([1, 2]))) // false
console.log(Buffer.equals('', new Buffer(2))) // false
})()
Static
fromAllocates a new Buffer
using an array
of bytes in the range 0
– 255
. Array entries outside that range will be truncated to fit into it.
If array
is an Array
-like object (that is, one with a length
property of type number
), it is treated as if it is an array, unless it is a Buffer
or a Uint8Array
. This means all other TypedArray
variants get treated as an Array
. To create a Buffer
from the bytes backing a TypedArray
, use Buffer.copyBytesFrom()
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf1 = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72])
console.log(buf1.toString('hex')) // Prints: 627566666572
const u16 = new Uint16Array([0x0001, 0x0002])
const buf2 = Buffer.from(u16)
console.log(buf2.toString('hex')) // Prints: 0102
})()
This creates a view of the ArrayBuffer
without copying the underlying memory. For example, when passed a reference to the .buffer
property of a TypedArray
instance, the newly created Buffer
will share the same allocated memory as the TypedArray
's underlying ArrayBuffer
.
The optional byteOffset and length arguments specify a memory range within the arrayBuffer that will be shared by the Buffer.
It is important to remember that a backing ArrayBuffer
can cover a range of memory that extends beyond the bounds of a TypedArray
view. A new Buffer
created using the buffer
property of a TypedArray
may extend beyond the range of the TypedArray
:
An ArrayBuffer
, SharedArrayBuffer
, for example the .buffer
property of a TypedArray
.
Optional
byteOffset: numberIndex of first byte to expose. Default: 0
.
Optional
length: numberNumber of bytes to expose. Default: arrayBuffer.byteLength - byteOffset
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const arr = new Uint16Array(2)
arr[0] = 5000
arr[1] = 4000
const buf = Buffer.from(arr.buffer) // Shares memory with `arr`
console.log(buf.toString('hex')) // Prints: 8813a00f
// Changing the original Uint16Array changes the Buffer also.
arr[1] = 6000
console.log(buf.toString('hex')) // Prints: 88137017
})()
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const ab = new ArrayBuffer(10)
const buf = Buffer.from(ab, 0, 2)
console.log(buf.length) // Prints: 2
})()
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const arrA = Uint8Array.from([0x63, 0x64, 0x65, 0x66]) // 4 elements
const arrB = new Uint8Array(arrA.buffer, 1, 2) // 2 elements
console.log(arrA.buffer === arrB.buffer) // true
const buf = Buffer.from(arrB.buffer)
console.log(buf.toString('hex')) // Prints: 63646566
})()
Copies the passed buffer
data onto a new Buffer
instance.
An existing Buffer
or Uint8Array
from which to copy data.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf1 = Buffer.from('buffer')
const buf2 = Buffer.from(buf1)
buf1[0] = 0x61
console.log(buf1.toString()) // Prints: auffer
console.log(buf2.toString()) // Prints: buffer
})()
Restore a Buffer
in the format returned from Buffer#toJSON()
.
A JSON object returned from Buffer#toJSON()
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf1 = Buffer.from('buffer')
const buf2 = Buffer.from(buf1.toJSON())
console.log(buf1.equals(buf2)) // Prints: true
})()
Creates a new Buffer
containing string
. The encoding
parameter identifies the character encoding to be used when converting string
into bytes.
A string to encode.
Optional
encoding: "base64" | "binary" | "ucs-2" | "utf-16le" | "utf-8" | "ascii" | "base64url" | "hex" | "latin1" | "ucs2" | "utf16le" | "utf8"The encoding of string. Default: 'utf8'
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf1 = Buffer.from('this is a tést')
const buf2 = Buffer.from('7468697320697320612074c3a97374', 'hex')
console.log(buf1.toString()) // Prints: this is a tést
console.log(buf2.toString()) // Prints: this is a tést
console.log(buf1.toString('latin1')) // Prints: this is a tést
})()
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from(new String('this is a test'))
console.log(buf.toString('hex')) // Prints: 7468697320697320612074657374
})()
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
class Foo {
[Symbol.toPrimitive]() {
return 'this is a test';
}
}
const buf = Buffer.from(new Foo(), 'utf8')
console.log(buf.toString('hex')) // Prints: 7468697320697320612074657374
})()
Static
fromCreates a new Buffer
that encoded from the provided base64
using Base64 encoding. When creating a Buffer
from a string, this encoding will also correctly accept "URL and Filename Safe Alphabet" as specified in RFC 4648, Section 5. Whitespace characters such as spaces, tabs, and new lines contained within the base64-encoded string are ignored.
A string to encode.
Static
fromCreates a new Buffer
that encoded from the provided base64
using base64url encoding. base64url encoding as specified in RFC 4648, Section 5. When creating a Buffer
from a string, this encoding will also correctly accept regular base64-encoded strings.
A string to encode.
Static
fromStatic
fromCreates a new Buffer
that encoded from the provided latin1
using Latin-1 encoding. Latin-1 stands for ISO-8859-1. This character encoding only supports the Unicode characters from U+0000 to U+00FF. Each character is encoded using a single byte. Characters that do not fit into that range are truncated and will be mapped to characters in that range.
A string to encode.
Static
fromCreates a new Buffer
that encoded from the provided string
. The encoding
parameter identifies the character encoding to be used when converting string
into bytes.
A string to encode.
The encoding of string
. Default: 'utf8'
.
Static
fromCreates a new Buffer
that encoded from the provided ucs2
using UCS-2 encoding. UCS-2 used to refer to a variant of UTF-16 that did not support characters that had code points larger than U+FFFF. In Node.js, these code points are always supported.
A string to encode.
Static
fromCreates a new Buffer
that encoded from the provided utf8
using UTF-8 encoding. Multi-byte encoded Unicode characters. Many web pages and other document formats use UTF-8. This is the default character encoding.
A string to encode.
This method based on the TextEncoder API.
Static
fromCreates a Buffer
from view
without copying the underlying memory.
The ArrayBufferView
to create a Buffer
from.
Optional
offset: numberThe starting offset within view
. Default: 0
.
Optional
length: numberThe number of elements from view
to copy. Default: view.length - offset
.
Static
istrue
if obj
is a Buffer
, false
otherwise.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
console.log(Buffer.isBuffer(Buffer.alloc(10))) // true
console.log(Buffer.isBuffer(Buffer.from('foo'))) // true
console.log(Buffer.isBuffer('a string')) // false
console.log(Buffer.isBuffer([])) // false
console.log(Buffer.isBuffer(new Uint8Array(1024))) // false
})()
Static
isA character encoding name to check.
true
if encoding
is the name of a supported character encoding, or false
otherwise.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
console.log(Buffer.isEncoding('utf8')) // true
console.log(Buffer.isEncoding('hex')) // true
console.log(Buffer.isEncoding('utf/8')) // false
console.log(Buffer.isEncoding('')) // false
})()
Static
iterIteratively unpack from the buf
according to the format string format
. This method returns an iterator which will read equally sized chunks from the buf
until remaining contents smaller then the size required by the format
, as reflected by Buffer.packCalcSize()
.
A Buffer
to unpack from.
A format string.
Unlike Python struct, this method does not support native size and alignment (that wouldn't make much sense in a javascript). Instead, specify byte order and emit pad bytes explicitly.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf1 = Buffer.from('01fe01fe', 'hex')
console.log([...Buffer.iterUnpack(buf1, '!BB')]) // Prints: [[1, 254], [1, 254]]
})()
Static
packCreates a new Buffer
containing the vals
packed according to the format string format
. The arguments must match the vals
required by the format
exactly.
A format string.
Rest
...vals: any[]Values to pack.
Unlike Python struct, this method does not support native size and alignment (that wouldn't make much sense in a javascript). Instead, specify byte order and emit pad bytes explicitly.
Please refer to Python struct — Interpret bytes as packed binary data for more information.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
console.log(Buffer.pack('>h', 1023).toString('hex')) // Prints: 03ff
console.log(Buffer.pack('<h', 1023).toString('hex')) // Prints: ff03
console.log(Buffer.pack('>bhl', 1, 2, 3).toString('hex')) // Prints: 01000200000003
})()
Pack vals
into buf
according to the format string format
. The arguments must match the vals
required by the format
exactly. The buf
’s size in bytes must larger then the size required by the format
, as reflected by Buffer.packCalcSize()
.
A Buffer
to pack into.
A format string.
Rest
...vals: any[]Values to pack.
Unlike Python struct, this method does not support native size and alignment (that wouldn't make much sense in a javascript). Instead, specify byte order and emit pad bytes explicitly.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf1 = Buffer.alloc(3)
Buffer.pack(buf1, '>h', 0x0102)
console.log(buf1.toString('hex')) // Prints: 010200
const buf2 = Buffer.alloc(3)
Buffer.pack(buf2.subarray(1), '>h', 0x0102) // struct.pack_into
console.log(buf2.toString('hex')) // Prints: 000102
})()
Static
packReturn the required size corresponding to the format string formatOrItems
.
A format string or parsed items return by Buffer.packParseFormat().items
.
Unlike Python struct, this method does not support native size and alignment (that wouldn't make much sense in a javascript). Instead, specify byte order and emit pad bytes explicitly.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
console.log(Buffer.packCalcSize('>bhl')) // Prints: 7
console.log(Buffer.packCalcSize('>ci')) // Prints: 5
console.log(Buffer.packCalcSize('>ic')) // Prints: 5
console.log(Buffer.packCalcSize('>lhl')) // Prints: 10
console.log(Buffer.packCalcSize('>llh')) // Prints: 10
console.log(Buffer.packCalcSize('>llh0l')) // Prints: 10
console.log(Buffer.packCalcSize('<qh6xq')) // Prints: 24
console.log(Buffer.packCalcSize('<qqh6x')) // Prints: 24
})()
Static
packParse a format string format
. This is a internal method used by Buffer.packCalcSize()
, Buffer.pack()
and Buffer.unpack()
.
A format string.
Unlike Python struct, this method does not support native size and alignment (that wouldn't make much sense in a javascript). Instead, specify byte order and emit pad bytes explicitly.
Static
toDecodes buf
to a string according to the Base64 character encoding.
The buffer to decode.
Static
toDecodes buf
to a string according to the Base64 URL character encoding.
The buffer to decode.
Static
toDecodes buf
to a string according to the hexadecimal character encoding.
The buffer to decode.
Static
toDecodes buf
to a string according to the Latin1 character encoding. When decoding a Buffer
into a string, using this encoding will additionally unset the highest bit of each byte before decoding as 'latin1'
. Generally, there should be no reason to use this encoding, as 'utf8' (or, if the data is known to always be ASCII-only, 'latin1'
) will be a better choice when encoding or decoding ASCII-only text. It is only provided for legacy compatibility.
The buffer to decode.
Static
toDecodes buf
to a string according to the UCS-2 character encoding.
The buffer to decode.
Static
toDecodes buf
to a string according to the UTF-8 character encoding.
The buffer to decode.
This method based on the TextDecoder API.
Static
unpackUnpack from the buf
(presumably packed by pack(format, ...))
according to the format string format
. The result is a tuple even if it contains exactly one item. The buf
’s size in bytes must larger then the size required by the format
, as reflected by Buffer.packCalcSize()
.
A Buffer
to unpack from.
A format string.
Unlike Python struct, this method does not support native size and alignment (that wouldn't make much sense in a javascript). Instead, specify byte order and emit pad bytes explicitly.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf1 = Buffer.from('01fe01fe', 'hex')
console.log(Buffer.unpack(buf1, '!BBbb')) // Prints: [1, 254, 1, -2]
})()
Creates an array of chunk Buffer
s which length is bytesPerChunk
. If buf
's contents can't be split evenly, the final chunk will be the remaining contents.
The length of each chunk
The new array of chunks.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from('010203040506070809', 'hex')
console.log(buf.chunk(4).map(chunk => chunk.toString('hex')))
// Prints: ['01020304', '05060708', '09']
})()
Compares buf
with target
and returns a number indicating whether buf
comes before, after, or is the same as target
in sort order. Comparison is based on the actual sequence of bytes in each Buffer
.
The optional targetStart
, targetEnd
, sourceStart
, and sourceEnd
arguments can be used to limit the comparison to specific ranges within target
and buf
respectively.
A Buffer
or Uint8Array
with which to compare buf
.
The offset within target
at which to begin comparison. Default: 0
.
The offset within target
at which to end comparison (not inclusive). Default: target.length
.
The offset within buf
at which to begin comparison. Default: 0
.
The offset within buf at which to end comparison (not inclusive). Default: this.length
.
0
is returned if target
is the same as buf
1
is returned if target
should come before buf
when sorted.-1
is returned if target
should come after buf
when sorted.;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf1 = Buffer.from('ABC')
const buf2 = Buffer.from('BCD')
const buf3 = Buffer.from('ABCD')
console.log(buf1.compare(buf1)) // Prints: 0
console.log(buf1.compare(buf2)) // Prints: -1
console.log(buf1.compare(buf3)) // Prints: -1
console.log(buf2.compare(buf1)) // Prints: 1
console.log(buf2.compare(buf3)) // Prints: 1
console.log([buf1, buf2, buf3].sort(Buffer.compare).map(buf => buf.toString()))
// Prints: ['ABC', 'ABCD', 'BCD']
})()
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf1 = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9])
const buf2 = Buffer.from([5, 6, 7, 8, 9, 1, 2, 3, 4])
console.log(buf1.compare(buf2, 5, 9, 0, 4)) // Prints: 0
console.log(buf1.compare(buf2, 0, 6, 4)) // Prints: -1
console.log(buf1.compare(buf2, 5, 6, 5)) // Prints: 1
})()
Copies data from a region of buf
to a region in target
, even if the target
memory region overlaps with buf
.
TypedArray.prototype.set()
performs the same operation, and is available for all TypedArrays, including Node.js Buffer
s, although it takes different function arguments.
A Buffer
or Uint8Array
to copy into.
The offset within target
at which to begin writing. Default: 0
.
The offset within buf
from which to begin copying. Default: 0
.
The offset within buf
at which to stop copying (not inclusive). Default: this.length
.
The number of bytes copied.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf1 = Buffer.allocUnsafe(26)
const buf2 = Buffer.allocUnsafe(26).fill('!')
// 97 is the decimal ASCII value for 'a'.
for (let i = 0; i < 26; i++) buf1[i] = i + 97
// Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`.
buf1.copy(buf2, 8, 16, 20)
// This is equivalent to:
// buf2.set(buf1.subarray(16, 20), 8)
console.log(buf2.toString('ascii', 0, 25))
// Prints: !!!!!!!!qrst!!!!!!!!!!!!!
})()
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
// Create a `Buffer` and copy data from one region to an overlapping region within the same `Buffer`.
const buf = Buffer.allocUnsafe(26)
// 97 is the decimal ASCII value for 'a'.
for (let i = 0; i < 26; i++) buf[i] = i + 97
buf.copy(buf, 0, 4, 10)
console.log(buf.toString())
// Prints: efghijghijklmnopqrstuvwxyz
})()
Returns the this object after copying a section of the array identified by start and end to the same array starting at position target
If target is negative, it is treated as length+target where length is the length of the array.
If start is negative, it is treated as length+start. If end is negative, it is treated as length+end.
Optional
end: numberIf not specified, length of the this object is used as its default value.
Returns an array of key, value pairs for every entry in the array
A Buffer or Uint8Array with which to compare buf.
true
if both buf
and otherBuffer
have exactly the same bytes, false
otherwise. Equivalent to buf.compare(otherBuffer) === 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf1 = Buffer.from('ABC')
const buf2 = Buffer.from('414243', 'hex')
const buf3 = Buffer.from('ABCD')
console.log(buf1.equals(buf2)) // Prints: true
console.log(buf1.equals(buf3)) // Prints: false
})()
Determines whether all the members of an array satisfy the specified test.
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.
Optional
thisArg: anyAn object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.
The value with which to fill buf. Empty string is coerced to 0
.
Optional
encoding: "base64" | "binary" | "ucs-2" | "utf-16le" | "utf-8" | "ascii" | "base64url" | "hex" | "latin1" | "ucs2" | "utf16le" | "utf8"The encoding for value
. Default: 'utf8'
.
The value with which to fill buf. Empty string is coerced to 0
.
Number of bytes to skip before starting to fill buf
. Default: 0
.
Optional
encoding: "base64" | "binary" | "ucs-2" | "utf-16le" | "utf-8" | "ascii" | "base64url" | "hex" | "latin1" | "ucs2" | "utf16le" | "utf8"The encoding for value
. Default: 'utf8'
.
The value with which to fill buf. Empty string is coerced to 0
.
Number of bytes to skip before starting to fill buf
. Default: 0
.
Where to stop filling buf
(not inclusive). Default: buf.length
.
Optional
encoding: "base64" | "binary" | "ucs-2" | "utf-16le" | "utf-8" | "ascii" | "base64url" | "hex" | "latin1" | "ucs2" | "utf16le" | "utf8"The encoding for value
. Default: 'utf8'
.
The value with which to fill buf
. Empty value (Uint8Array, Buffer) is coerced to 0
. value
is coerced to a uint32
value if it is not a string, Buffer
, or integer. If the resulting integer is greater than 255
(decimal), buf
will be filled with value & 255
.
Optional
offset: numberNumber of bytes to skip before starting to fill buf
. Default: 0
.
Optional
end: numberWhere to stop filling buf
(not inclusive). Default: buf.length
.
Fills buf
with the specified value
. If the offset
and end
are not given, the entire buf
will be filled:
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
// Fill a `Buffer` with the ASCII character 'h'.
const b = Buffer.allocUnsafe(10).fill('h')
console.log(b.toString()) // Prints: hhhhhhhhhh
// Fill a buffer with empty string
const c = Buffer.allocUnsafe(5).fill('')
console.log(c.toString('hex')) // Prints: 0000000000
})()
If the final write of a fill()
operation falls on a multi-byte character, then only the bytes of that character that fit into buf
are written:
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
// Fill a `Buffer` with character that takes up two bytes in UTF-8.
const b = Buffer.allocUnsafe(5).fill('\u0222')
console.log(b.toString('hex')) // Prints: c8a2c8a2c8
})()
If value
contains invalid characters, it is truncated; if no valid fill data remains, an exception is thrown:
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.allocUnsafe(5)
console.log(buf.fill('a').toString('hex')) // Prints: 6161616161
console.log(buf.fill('aazz', 'hex').toString('hex')) // Prints: aaaaaaaaaa
console.log(buf.fill('zz', 'hex').toString('hex')) // Throws an exception.
})()
Returns the elements of an array that meet the condition specified in a callback function.
A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.
Optional
thisArg: anyAn object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.
Returns the value of the first element in the array where predicate is true, and undefined otherwise.
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.
Optional
thisArg: anyIf provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.
Returns the index of the first element in the array where predicate is true, and -1 otherwise.
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.
Optional
thisArg: anyIf provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.
Performs the specified action for each element in an array.
A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
Optional
thisArg: anyAn object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
Equivalent to buf.indexOf() !== -1
.
What to search for.
Optional
encoding: "base64" | "binary" | "ucs-2" | "utf-16le" | "utf-8" | "ascii" | "base64url" | "hex" | "latin1" | "ucs2" | "utf16le" | "utf8"The encoding of value
. Default: 'utf8'
.
true
if value
was found in buf
, false
otherwise.
Equivalent to buf.indexOf() !== -1
.
What to search for.
Where to begin searching in buf
. If negative, then offset is calculated from the end of buf
. Default: 0
.
Optional
encoding: "base64" | "binary" | "ucs-2" | "utf-16le" | "utf-8" | "ascii" | "base64url" | "hex" | "latin1" | "ucs2" | "utf16le" | "utf8"The encoding of value
. Default: 'utf8'
.
true
if value
was found in buf
, false
otherwise.
Equivalent to buf.indexOf() !== -1
.
What to search for.
Optional
byteOffset: numberWhere to begin searching in buf
. If negative, then offset is calculated from the end of buf
. Default: 0
.
true
if value
was found in buf
, false
otherwise.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from('this is a buffer')
console.log(buf.includes('this')) // Prints: true
console.log(buf.includes('is')) // Prints: true
console.log(buf.includes(Buffer.from('a buffer'))) // Prints: true
console.log(buf.includes(97)) // Prints: true (97 is the decimal ASCII value for 'a')
console.log(buf.includes(Buffer.from('a buffer example'))) // Prints: false
console.log(buf.includes(Buffer.from('a buffer example').slice(0, 8))) // Prints: true
console.log(buf.includes('this', 4)) // Prints: false
})()
What to search for.
value
is a string, value
is interpreted according to the character encoding in encoding
.value
is a Buffer
or Uint8Array
, value
will be used in its entirety. To compare a partial Buffer, use buf.subarray
.value
is a number, it will be coerced to a valid byte value, an integer between 0
and 255
.value
is an empty string or empty Buffer and byteOffset
is less than buf.length
, byteOffset
will be returned.value
is empty and byteOffset
is at least buf.length
, buf.length
will be returned.Optional
encoding: "base64" | "binary" | "ucs-2" | "utf-16le" | "utf-8" | "ascii" | "base64url" | "hex" | "latin1" | "ucs2" | "utf16le" | "utf8"The encoding of value
. Default: 'utf8'
.
value
in buf
-1
if buf
does not contain value.value
is not a string, number, or Buffer
, this method will throw a TypeError
.What to search for.
value
is a string, value
is interpreted according to the character encoding in encoding
.value
is a Buffer
or Uint8Array
, value
will be used in its entirety. To compare a partial Buffer, use buf.subarray
.value
is a number, it will be coerced to a valid byte value, an integer between 0
and 255
.value
is an empty string or empty Buffer and byteOffset
is less than buf.length
, byteOffset
will be returned.value
is empty and byteOffset
is at least buf.length
, buf.length
will be returned.Where to begin searching in buf
. Default: 0
.
buf
._.toSafeInteger
.Optional
encoding: "base64" | "binary" | "ucs-2" | "utf-16le" | "utf-8" | "ascii" | "base64url" | "hex" | "latin1" | "ucs2" | "utf16le" | "utf8"The encoding of value
. Default: 'utf8'
.
value
in buf
-1
if buf
does not contain value.value
is not a string, number, or Buffer
, this method will throw a TypeError
.What to search for.
value
is a string, value
is interpreted according to the character encoding in encoding
.value
is a Buffer
or Uint8Array
, value
will be used in its entirety. To compare a partial Buffer, use buf.subarray
.value
is a number, it will be coerced to a valid byte value, an integer between 0
and 255
.value
is an empty string or empty Buffer and byteOffset
is less than buf.length
, byteOffset
will be returned.value
is empty and byteOffset
is at least buf.length
, buf.length
will be returned.Optional
byteOffset: numberWhere to begin searching in buf
. Default: 0
.
buf
._.toSafeInteger
.value
in buf
-1
if buf
does not contain value.value
is not a string, number, or Buffer
, this method will throw a TypeError
.;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from('this is a buffer')
console.log(buf.indexOf('this')) // Prints: 0
console.log(buf.indexOf('is')) // Prints: 2
console.log(buf.indexOf(Buffer.from('a buffer'))) // Prints: 8
console.log(buf.indexOf(97)) // Prints: 8 (97 is the decimal ASCII value for 'a')
console.log(buf.indexOf(Buffer.from('a buffer example'))) // Prints: -1
console.log(buf.indexOf(Buffer.from('a buffer example').slice(0, 8))) // Prints: 8
const utf16Buffer = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'utf16le')
console.log(utf16Buffer.indexOf('\u03a3', 0, 'utf16le')) // Prints: 4
console.log(utf16Buffer.indexOf('\u03a3', -4, 'utf16le')) // Prints: 6
})()
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const b = Buffer.from('abcdef')
// Passing a value that's a number, but not a valid byte.
// Prints: 2, equivalent to searching for 99 or 'c'.
console.log(b.indexOf(99.9))
console.log(b.indexOf(256 + 99))
// Passing a byteOffset that coerces to 0.
// Prints: 1, searching the whole buffer.
console.log(b.indexOf('b', undefined))
console.log(b.indexOf('b', {}))
console.log(b.indexOf('b', null))
console.log(b.indexOf('b', []))
})()
Iteratively unpack from the buf
according to the format string format
. This method returns an iterator which will read equally sized chunks from the buf
until remaining contents smaller then the size required by the format
, as reflected by Buffer.packCalcSize()
.
A format string.
Unlike Python struct, this method does not support native size and alignment (that wouldn't make much sense in a javascript). Instead, specify byte order and emit pad bytes explicitly.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf1 = Buffer.from('01fe01fe', 'hex')
console.log([...buf1.iterUnpack('!BB')]) // Prints: [[1, 254], [1, 254]]
})()
Adds all the elements of an array separated by the specified separator string.
Optional
separator: stringA 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.
Returns an list of keys in the array
Identical to buf.indexOf()
, except the last occurrence of value
is found rather than the first occurrence.
What to search for.
value
is a string, value
is interpreted according to the character encoding in encoding
.value
is a Buffer
or Uint8Array
, value
will be used in its entirety. To compare a partial Buffer, use buf.subarray
.value
is a number, it will be coerced to a valid byte value, an integer between 0
and 255
.value
is an empty string or empty Buffer
, byteOffset
will be returned.Optional
encoding: "base64" | "binary" | "ucs-2" | "utf-16le" | "utf-8" | "ascii" | "base64url" | "hex" | "latin1" | "ucs2" | "utf16le" | "utf8"The encoding of value
. Default: 'utf8'
.
value
in buf
-1
if buf
does not contain value
.value
is not a string, number, or Buffer
, this method will throw a TypeError
.Identical to buf.indexOf()
, except the last occurrence of value
is found rather than the first occurrence.
What to search for.
value
is a string, value
is interpreted according to the character encoding in encoding
.value
is a Buffer
or Uint8Array
, value
will be used in its entirety. To compare a partial Buffer, use buf.subarray
.value
is a number, it will be coerced to a valid byte value, an integer between 0
and 255
.value
is an empty string or empty Buffer
, byteOffset
will be returned.Where to begin searching in buf
. Default: buf.length - 1
.
buf
._.toSafeInteger
.Optional
encoding: "base64" | "binary" | "ucs-2" | "utf-16le" | "utf-8" | "ascii" | "base64url" | "hex" | "latin1" | "ucs2" | "utf16le" | "utf8"The encoding of value
. Default: 'utf8'
.
value
in buf
-1
if buf
does not contain value
.value
is not a string, number, or Buffer
, this method will throw a TypeError
.Identical to buf.indexOf()
, except the last occurrence of value
is found rather than the first occurrence.
What to search for.
value
is a number, it will be coerced to a valid byte value, an integer between 0
and 255
.value
is an empty string or empty Buffer
, byteOffset
will be returned.Optional
byteOffset: numberWhere to begin searching in buf
. Default: buf.length - 1
.
buf
._.toSafeInteger
.value
in buf
-1
if buf
does not contain value
.value
is not a string, number, or Buffer
, this method will throw a TypeError
.;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from('this buffer is a buffer')
console.log(buf.lastIndexOf('this')) // Prints: 0
console.log(buf.lastIndexOf('buffer')) // Prints: 17
console.log(buf.lastIndexOf(Buffer.from('buffer'))) // Prints: 17
console.log(buf.lastIndexOf(97)) // Prints: 15 (97 is the decimal ASCII value for 'a')
console.log(buf.lastIndexOf(Buffer.from('yolo'))) // Prints: -1
console.log(buf.lastIndexOf('buffer', 5)) // Prints: 5
console.log(buf.lastIndexOf('buffer', 4)) // Prints: -1
const utf16Buffer = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'utf16le')
console.log(utf16Buffer.lastIndexOf('\u03a3', undefined, 'utf16le')) // Prints: 6
console.log(utf16Buffer.lastIndexOf('\u03a3', -5, 'utf16le')) // Prints: 4
})()
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const b = Buffer.from('abcdef')
// Passing a value that's a number, but not a valid byte.
// Prints: 2, equivalent to searching for 99 or 'c'.
console.log(b.lastIndexOf(99.9))
console.log(b.lastIndexOf(256 + 99))
// Passing a byteOffset that coerces to NaN.
// Prints: 1, searching the whole buffer.
console.log(b.lastIndexOf('b', undefined))
console.log(b.lastIndexOf('b', {}))
// Passing a byteOffset that coerces to 0.
// Prints: -1, equivalent to passing 0.
console.log(b.lastIndexOf('b', null))
console.log(b.lastIndexOf('b', []))
})()
Calls a defined callback function on each element of an array, and returns an array that contains the results.
A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
Optional
thisArg: anyAn object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
Pack vals
into buf
according to the format string format
. The arguments must match the vals
required by the format
exactly. The buf
’s size in bytes must larger then the size required by the format
, as reflected by Buffer.packCalcSize()
.
A format string.
Rest
...vals: any[]Values to pack.
Unlike Python struct, this method does not support native size and alignment (that wouldn't make much sense in a javascript). Instead, specify byte order and emit pad bytes explicitly.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf1 = Buffer.alloc(3)
buf1.pack('>h', 0x0102)
console.log(buf1.toString('hex')) // Prints: 010200
const buf2 = Buffer.alloc(3)
buf2.subarray(1).pack('>h', 0x0102) // struct.pack_into
console.log(buf2.toString('hex')) // Prints: 000102
})()
Reads a signed, big-endian 64-bit integer from buf
at the specified offset
. Integers read from a Buffer
are interpreted as two's complement signed values.
Number of bytes to skip before starting to read. Must satisfy: 0 <= offset <= buf.length - 8
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from('00000000ffffffff', 'hex')
console.log(buf.readBigInt64BE(0)) // Prints: 4294967295n
})()
Reads a signed, little-endian 64-bit integer from buf
at the specified offset
. Integers read from a Buffer
are interpreted as two's complement signed values.
Number of bytes to skip before starting to read. Must satisfy: 0 <= offset <= buf.length - 8
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from('00000000ffffffff', 'hex')
console.log(buf.readBigInt64LE(0)) // Prints: -4294967296n
})()
Reads an unsigned, big-endian 64-bit integer from buf at the specified offset.
Number of bytes to skip before starting to read. Must satisfy: 0 <= offset <= buf.length - 8
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from('00000000ffffffff', 'hex')
console.log(buf.readBigUInt64BE(0)) // Prints: 4294967295n
})()
Reads an unsigned, little-endian 64-bit integer from buf
at the specified offset
.
Number of bytes to skip before starting to read. Must satisfy: 0 <= offset <= buf.length - 8
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from('00000000ffffffff', 'hex')
console.log(buf.readBigUInt64LE(0)) // Prints: 18446744069414584320n
})()
Treat the buffer as a least significant bit array and read a bit at the specified bit offset.
bit offset to read from.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from([0b10010110])
const bits = new Array(8)
for (let i = 0; i < 8; i++) bits[i] = buf.readBitLSB(i)
console.log(bits) // Prints: [0, 1, 1, 0, 1, 0, 0, 1]
})()
Treat buf
as a most-significant bit array and read a bit at the specified bit offset.
bit offset to read from.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from([0b10010110])
const bits = new Array(8)
for (let i = 0; i < 8; i++) bits[i] = buf.readBitMSB(i)
console.log(bits) // Prints: [1, 0, 0, 1, 0, 1, 1, 0]
})()
Reads a 64-bit, big-endian double from buf
at the specified offset
.
Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 8
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from('0102030405060708', 'hex')
console.log(buf.readDoubleBE(0)) // Prints: 8.20788039913184e-304
})()
Reads a 64-bit, little-endian double from buf
at the specified offset
.
Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 8
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from('0102030405060708', 'hex')
console.log(buf.readDoubleLE(0)) // Prints: 5.447603722011605e-270
})()
Reads a 16-bit, big-endian float from buf
at the specified offset
.
Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 2
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
console.log(Buffer.from('0000', 'hex').readFloat16BE(0)) // Prints: 0
console.log(Buffer.from('8000', 'hex').readFloat16BE(0)) // Prints: -0
console.log(Buffer.from('3c00', 'hex').readFloat16BE(0)) // Prints: 1
console.log(Buffer.from('bc00', 'hex').readFloat16BE(0)) // Prints: -1
console.log(Buffer.from('7c00', 'hex').readFloat16BE(0)) // Prints: Infinity
console.log(Buffer.from('fc00', 'hex').readFloat16BE(0)) // Prints: -Infinity
console.log(Buffer.from('7e00', 'hex').readFloat16BE(0)) // Prints: NaN
console.log(Buffer.from('fe00', 'hex').readFloat16BE(0)) // Prints: NaN
})()
Reads a 16-bit, little-endian float from buf
at the specified offset
.
Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 2
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
console.log(Buffer.from('0000', 'hex').readFloat16LE(0)) // Prints: 0
console.log(Buffer.from('0080', 'hex').readFloat16LE(0)) // Prints: -0
console.log(Buffer.from('003c', 'hex').readFloat16LE(0)) // Prints: 1
console.log(Buffer.from('00bc', 'hex').readFloat16LE(0)) // Prints: -1
console.log(Buffer.from('007c', 'hex').readFloat16LE(0)) // Prints: Infinity
console.log(Buffer.from('00fc', 'hex').readFloat16LE(0)) // Prints: -Infinity
console.log(Buffer.from('007e', 'hex').readFloat16LE(0)) // Prints: NaN
console.log(Buffer.from('00fe', 'hex').readFloat16LE(0)) // Prints: NaN
})()
Reads a 32-bit, big-endian float from buf
at the specified offset
.
Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 4
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from('01020304', 'hex')
console.log(buf.readFloatBE(0)) // Prints: 2.387939260590663e-38
})()
Reads a 32-bit, little-endian float from buf
at the specified offset
.
Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 4
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from('01020304', 'hex')
console.log(buf.readFloatLE(0)) // Prints: 1.539989614439558e-36
})()
Reads a signed, big-endian 16-bit integer from buf
at the specified offset
. Integers read from a Buffer
are interpreted as two's complement signed values.
Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 2
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from([0, 5])
console.log(buf.readInt16BE(0)) // Prints: 5
})()
Reads a signed, little-endian 16-bit integer from buf
at the specified offset
. Integers read from a Buffer
are interpreted as two's complement signed values.
Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 2
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from([0, 5])
console.log(buf.readInt16LE(0)) // Prints: 1280
})()
Reads a signed, big-endian 32-bit integer from buf
at the specified offset
. Integers read from a Buffer
are interpreted as two's complement signed values.
Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 4
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from([0, 0, 0, 5])
console.log(buf.readInt32BE(0)) // Prints: 5
})()
Reads a signed, little-endian 32-bit integer from buf
at the specified offset
. Integers read from a Buffer
are interpreted as two's complement signed values.
Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 4
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from([0, 0, 0, 5])
console.log(buf.readInt32LE(0)) // Prints: 83886080
})()
Reads a signed 8-bit integer from buf
at the specified offset
. Integers read from a Buffer
are interpreted as two's complement signed values.
Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 1
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from([-1, 5])
console.log(buf.readInt8(0)) // Prints: -1
console.log(buf.readInt8(1)) // Prints: 5
})()
Reads byteLength
number of bytes from buf
at the specified offset
and interprets the result as a big-endian, two's complement signed value supporting up to 48 bits of accuracy.
Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - byteLength
.
Number of bytes to read. Must satisfy 1 <= byteLength <= 6
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab])
console.log(buf.readIntBE(0, 6).toString(16)) // Prints: 1234567890ab
})()
Reads byteLength
number of bytes from buf
at the specified offset
and interprets the result as a little-endian, two's complement signed value supporting up to 48 bits of accuracy.
Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - byteLength
.
Number of bytes to read. Must satisfy 1 <= byteLength <= 6
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab])
console.log(buf.readIntLE(0, 6).toString(16)) // Prints: -546f87a9cbee
})()
Reads an unsigned, big-endian 16-bit integer from buf
at the specified offset
.
Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 2
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from([0x12, 0x34, 0x56])
console.log(buf.readUInt16BE(0).toString(16)) // Prints: 1234
console.log(buf.readUInt16BE(1).toString(16)) // Prints: 3456
})()
Reads an unsigned, little-endian 16-bit integer from buf
at the specified offset
.
Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 2
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from([0x12, 0x34, 0x56])
console.log(buf.readUInt16LE(0).toString(16)) // Prints: 3412
console.log(buf.readUInt16LE(1).toString(16)) // Prints: 5634
})()
Reads an unsigned, big-endian 32-bit integer from buf
at the specified offset
.
Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 4
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from([0x12, 0x34, 0x56, 0x78])
console.log(buf.readUInt32BE(0).toString(16)) // Prints: 12345678
})()
Reads an unsigned, little-endian 32-bit integer from buf
at the specified offset
.
Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 4
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from([0x12, 0x34, 0x56, 0x78])
console.log(buf.readUInt32LE(0).toString(16)) // Prints: 78563412
})()
Reads an unsigned 8-bit integer from buf
at the specified offset
.
Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 1
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from([1, -2])
console.log(buf.readUInt8(0)) // Prints: 1
console.log(buf.readUInt8(1)) // Prints: 254
})()
Reads byteLength
number of bytes from buf
at the specified offset
and interprets the result as an unsigned big-endian integer supporting up to 48 bits of accuracy.
Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - byteLength
.
Number of bytes to read. Must satisfy 0 < byteLength <= 6
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab])
console.log(buf.readUIntBE(0, 6).toString(16)) // Prints: 1234567890ab
})()
Reads byteLength
number of bytes from buf
at the specified offset
and interprets the result as an unsigned big-endian integer supporting up to 48 bits of accuracy.
Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - byteLength
.
Number of bytes to read. Must satisfy 0 < byteLength <= 6
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab])
console.log(buf.readUIntLE(0, 6).toString(16)) // Prints: ab9078563412
})()
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.
A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
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.
A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
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.
A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.
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.
A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.
If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
Creates a new Buffer
which is reverse of the original buf
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf1 = Buffer.from('01020304', 'hex')
const buf2 = buf1.reverse()
console.log(buf1.toString('hex')) // Prints: 01020304
console.log(buf2.toString('hex')) // Prints: 04030201
})()
Sets a value or an array of values.
A typed or untyped array of values to set.
Optional
offset: numberThe index in the current array at which the values are to be written.
Returns a copy of a portion of a Buffer
into a new Buffer
object selected from start
to end
(end
not included) where start
and end
represent the index of items in that buf
. The original Buffer
will not be modified.
Where the new Buffer
will start. Default: 0
.
Where the new Buffer
will end (not inclusive). Default: buf.length
.
This method is different from Node.js's Buffer.slice()
method.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf1 = Buffer.from('1020304050', 'hex')
const buf2 = buf1.slice(1, 3)
buf1[1] = 0x60
console.log(buf1.toString('hex')) // Prints: 1060304050
console.log(buf2.toString('hex')) // Prints: 2030
})()
Determines whether the specified callback function returns true for any element of an array.
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.
Optional
thisArg: anyAn object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.
Sorts an array.
Optional
compareFn: ((a, b) => 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.
[11,2,22,1].sort((a, b) => a - b)
Returns a new Buffer
that references the same memory as the original, but offset and cropped by the start
and end
indexes.
Specifying end
greater than buf.length
will return the same result as that of end
equal to buf.length
.
Modifying the new Buffer
slice will modify the memory in the original Buffer
because the allocated memory of the two objects overlap.
Specifying negative indexes causes the slice to be generated relative to the end of buf
rather than the beginning.
Where the new Buffer
will start. Default: 0
.
Where the new Buffer
will end (not inclusive). Default: buf.length
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf1 = Buffer.allocUnsafe(26)
for (let i = 0; i < 26; i++) buf1[i] = i + 97
const buf2 = buf1.subarray(0, 3)
console.log(buf2.toString('ascii', 0, buf2.length)) // Prints: abc
buf1[0] = 33
console.log(buf2.toString('ascii', 0, buf2.length)) // Prints: !bc
})()
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf1 = Buffer.allocUnsafe(26)
for (let i = 0; i < 26; i++) buf1[i] = i + 97
const buf2 = buf1.subarray(0, 3)
console.log(buf2.toString('ascii', 0, buf2.length)) // Prints: abc
buf1[0] = 33
console.log(buf2.toString('ascii', 0, buf2.length)) // Prints: !bc
})()
Interprets buf
as an array of unsigned 16-bit integers and swaps the byte order in-place.
A reference to buf
.
If buf.length
is not a multiple of 2, this method will throw a RangeError
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf1 = Buffer.from('0102030405060708', 'hex')
console.log(buf1.toString('hex')) // Prints: 0102030405060708
buf1.swap16()
console.log(buf1.toString('hex')) // Prints: 0201040306050807
const utf16 = Buffer.from('This is little-endian UTF-16', 'utf16le')
utf16.swap16() // Convert to big-endian UTF-16 text.
})()
Interprets buf
as an array of unsigned 32-bit integers and swaps the byte order in-place.
A reference to buf
.
If buf.length
is not a multiple of 4, this method will throw a RangeError
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf1 = Buffer.from('0102030405060708', 'hex')
console.log(buf1.toString('hex')) // Prints: 0102030405060708
buf1.swap32()
console.log(buf1.toString('hex')) // Prints: 0403020108070605
})()
Interprets buf
as an array of unsigned 64-bit integers and swaps the byte order in-place.
A reference to buf
.
If buf.length
is not a multiple of 8, this method will throw a RangeError
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf1 = Buffer.from('0102030405060708', 'hex')
console.log(buf1.toString('hex')) // Prints: 0102030405060708
buf1.swap64()
console.log(buf1.toString('hex')) // Prints: 0807060504030201
})()
JSON.stringify()
implicitly calls this method when stringifying a Buffer
instance. Buffer.from()
accepts objects in the format returned from this method. In particular, Buffer.from(buf.toJSON())
works like Buffer.from(buf)
.
a JSON representation of buf
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from('0102030405', 'hex')
const json = JSON.stringify(buf)
console.log(json) // Prints: {"type":"Buffer","data":[1,2,3,4,5]}
const restored = JSON.parse(json, (key, value) => {
return value && value.type === 'Buffer' ? Buffer.from(value) : value
})
console.log(restored.toString('hex')) // Prints: 0102030405
})()
Decodes buf
to a string according to the specified character encoding in encoding
. start
and end
may be passed to decode only a subset of buf
.
The character encoding to use. Default: 'utf8'
.
The byte offset to start decoding at. Default: 0
.
The byte offset to stop decoding at (not inclusive). Default: buf.length
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf1 = Buffer.allocUnsafe(26)
for (let i = 0; i < 26; i++) buf1[i] = i + 97 // 97 is the decimal ASCII value for 'a'.
console.log(buf1.toString('utf8')) // Prints: abcdefghijklmnopqrstuvwxyz
console.log(buf1.toString('utf8', 0, 5)) // Prints: abcde
const buf2 = Buffer.from('tést')
console.log(buf2.toString('hex')) // Prints: 74c3a97374
console.log(buf2.toString('utf8', 0, 3)) // Prints: té
console.log(buf2.toString(undefined, 0, 3)) // Prints: té
})()
Unpack from the buf
(presumably packed by pack(format, ...))
according to the format string format
. The result is a tuple even if it contains exactly one item. The buf
’s size in bytes must larger then the size required by the format
, as reflected by Buffer.packCalcSize()
.
A format string.
Unlike Python struct, this method does not support native size and alignment (that wouldn't make much sense in a javascript). Instead, specify byte order and emit pad bytes explicitly.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf1 = Buffer.from('01fe01fe', 'hex')
console.log(buf1.unpack('!BBbb')) // Prints: [1, 254, 1, -2]
})()
Returns the primitive value of the specified object.
Returns an list of values in the array
Writes string
to buf
at offset
according to the character encoding in encoding
. The length
parameter is the number of bytes to write. If buf
did not contain enough space to fit the entire string, only part of string
will be written. However, partially encoded characters will not be written.
String to write to buf
.
Optional
encoding: "base64" | "binary" | "ucs-2" | "utf-16le" | "utf-8" | "ascii" | "base64url" | "hex" | "latin1" | "ucs2" | "utf16le" | "utf8"The character encoding of string
. Default: 'utf8'
.
Number of bytes written.
Writes string
to buf
at offset
according to the character encoding in encoding
. The length
parameter is the number of bytes to write. If buf
did not contain enough space to fit the entire string, only part of string
will be written. However, partially encoded characters will not be written.
String to write to buf
.
Number of bytes to skip before starting to write string
. Default: 0
.
Optional
encoding: "base64" | "binary" | "ucs-2" | "utf-16le" | "utf-8" | "ascii" | "base64url" | "hex" | "latin1" | "ucs2" | "utf16le" | "utf8"The character encoding of string
. Default: 'utf8'
.
Number of bytes written.
Writes string
to buf
at offset
according to the character encoding in encoding
. The length
parameter is the number of bytes to write. If buf
did not contain enough space to fit the entire string, only part of string
will be written. However, partially encoded characters will not be written.
String to write to buf
.
Number of bytes to skip before starting to write string
. Default: 0
.
Maximum number of bytes to write (written bytes will not exceed buf.length - offset
). Default: buf.length - offset
.
Optional
encoding: "base64" | "binary" | "ucs-2" | "utf-16le" | "utf-8" | "ascii" | "base64url" | "hex" | "latin1" | "ucs2" | "utf16le" | "utf8"The character encoding of string
. Default: 'utf8'
.
Number of bytes written.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf1 = Buffer.alloc(256)
const len1 = buf1.write('\u00bd + \u00bc = \u00be', 0)
console.log(`${len1} bytes: ${buf1.toString('utf8', 0, len1)}`)
// Prints: 12 bytes: ½ + ¼ = ¾
const buf2 = Buffer.alloc(10)
const len2 = buf2.write('abcd', 8)
console.log(`${len2} bytes: ${buf2.toString('utf8', 8, 10)}`)
// Prints: 2 bytes : ab
})()
Writes value
to buf
at the specified offset
as big-endian. value
is interpreted and written as a two's complement signed integer.
Number to be written to buf
.
Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 8
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = new Buffer(8)
buf.writeBigInt64BE(0x0102030405060708n, 0)
console.log(buf.toString('hex')) // Prints: 0102030405060708
})()
Writes value
to buf
at the specified offset
as little-endian. value
is interpreted and written as a two's complement signed integer.
Number to be written to buf
.
Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 8
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = new Buffer(8)
buf.writeBigInt64LE(0x0102030405060708n, 0)
console.log(buf.toString('hex')) // Prints: 0807060504030201
})()
Writes value
to buf
at the specified offset
as big-endian.
Number to be written to buf
.
Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 8
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = new Buffer(8)
buf.writeBigUInt64BE(0xdecafafecacefaden, 0)
console.log(buf.toString('hex')) // Prints: decafafecacefade
})()
Writes value
to buf
at the specified offset
as little-endian.
Number to be written to buf
.
Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 8
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = new Buffer(8)
buf.writeBigUInt64LE(0xdecafafecacefaden, 0)
console.log(buf.toString('hex')) // Prints: defacecafefacade
})()
Treats buf
as a least significant bit array and write a bit at the specified bit offset.
Bit value to write.
bit offset to read from.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = new Buffer(1)
const bits = [0, 1, 1, 0, 1, 0, 0, 1]
for (let i = 0; i < 8; i++) buf.writeBitLSB(bits[i], i)
console.log(buf[0].toString(2)) // Prints: 10010110
})()
Treats buf
as a most-significant bit array and write a bit at the specified bit offset.
Bit value to write.
bit offset to read from.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = new Buffer(1)
const bits = [1, 0, 0, 1, 0, 1, 1, 0]
for (let i = 0; i < 8; i++) buf.writeBitMSB(bits[i], i)
console.log(buf[0].toString(2)) // Prints: 10010110
})()
Writes value
to buf
at the specified offset
as big-endian. The value
must be a JavaScript number. Behavior is undefined when value
is anything other than a JavaScript number.
Number to be written to buf
.
Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 8
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = new Buffer(8)
buf.writeDoubleBE(123.456, 0)
console.log(buf.toString('hex')) // Prints: 405edd2f1a9fbe77
})()
Writes value
to buf
at the specified offset
as little-endian. The value
must be a JavaScript number. Behavior is undefined when value
is anything other than a JavaScript number.
Number to be written to buf
.
Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 8
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = new Buffer(8)
buf.writeDoubleLE(123.456, 0)
console.log(buf.toString('hex')) // Prints: 77be9f1a2fdd5e40
})()
Writes a 16-bit float value
to buf
at the specified offset
as big-endian. The value
must be a JavaScript number. Behavior is undefined when value
is anything other than a JavaScript number.
Number to be written to buf
.
Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 2
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = new Buffer(2)
buf.writeFloat16BE(123.456, 0)
console.log(buf.toString('hex')) // Prints: 57b7
})()
Writes a 16-bit float value
to buf
at the specified offset
as little-endian. The value
must be a JavaScript number. Behavior is undefined when value
is anything other than a JavaScript number.
Number to be written to buf
.
Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 2
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = new Buffer(2)
buf.writeFloat16LE(123.456, 0)
console.log(buf.toString('hex')) // Prints: b757
})()
Writes value
to buf
at the specified offset
as big-endian. The value
must be a JavaScript number. Behavior is undefined when value
is anything other than a JavaScript number.
Number to be written to buf
.
Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 4
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = new Buffer(4)
buf.writeFloatBE(123.456, 0)
console.log(buf.toString('hex')) // Prints: 42f6e979
})()
Writes value
to buf
at the specified offset
as little-endian. The value
must be a JavaScript number. Behavior is undefined when value
is anything other than a JavaScript number.
Number to be written to buf
.
Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 4
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = new Buffer(4)
buf.writeFloatLE(123.456, 0)
console.log(buf.toString('hex')) // Prints: 79e9f642
})()
Writes value
to buf
at the specified offset
as big-endian. The value must be a valid signed 16-bit integer. Behavior is undefined when value
is anything other than a signed 16-bit integer. The value
is interpreted and written as a two's complement signed integer.
Number to be written to buf
.
Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 2
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = new Buffer(2)
buf.writeInt16BE(0x0102, 0)
console.log(buf.toString('hex')) // Prints: 0102
})()
Writes value
to buf
at the specified offset
as little-endian. The value must be a valid signed 16-bit integer. Behavior is undefined when value
is anything other than a signed 16-bit integer. The value
is interpreted and written as a two's complement signed integer.
Number to be written to buf
.
Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 2
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = new Buffer(2)
buf.writeInt16LE(0x0102, 0)
console.log(buf.toString('hex')) // Prints: 0201
})()
Writes value
to buf
at the specified offset
as big-endian. The value must be a valid signed 32-bit integer. Behavior is undefined when value
is anything other than a signed 32-bit integer. The value
is interpreted and written as a two's complement signed integer.
Number to be written to buf
.
Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 4
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = new Buffer(4)
buf.writeInt32BE(0x01020304, 0)
console.log(buf.toString('hex')) // Prints: 01020304
})()
Writes value
to buf
at the specified offset
as little-endian. The value must be a valid signed 32-bit integer. Behavior is undefined when value
is anything other than a signed 32-bit integer. The value
is interpreted and written as a two's complement signed integer.
Number to be written to buf
.
Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 4
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = new Buffer(4)
buf.writeInt32LE(0x01020304, 0)
console.log(buf.toString('hex')) // Prints: 04030201
})()
Writes value
to buf
at the specified offset
. value
must be a valid signed 8-bit integer. Behavior is undefined when value
is anything other than a signed 8-bit integer. value
is interpreted and written as a two's complement signed integer.
Number to be written to buf
.
Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 1
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = new Buffer(2)
buf.writeInt8(2, 0)
buf.writeInt8(-2, 1)
console.log(buf.toString('hex')) // Prints: 02fe
})()
Writes byteLength
bytes of value
to buf
at the specified offset
as big-endian. Supports up to 48 bits of accuracy. Behavior is undefined when value
is anything other than a signed integer.
Number to be written to buf
.
Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - byteLength
.
Number of bytes to write. Must satisfy 1 <= byteLength <= 6
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = new Buffer(6)
buf.writeIntBE(0x1234567890ab, 0, 6)
console.log(buf.toString('hex')) // Prints: 1234567890ab
})()
Writes byteLength
bytes of value
to buf
at the specified offset
as little-endian. Supports up to 48 bits of accuracy. Behavior is undefined when value
is anything other than a signed integer.
Number to be written to buf
.
Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - byteLength
.
Number of bytes to write. Must satisfy 1 <= byteLength <= 6
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = new Buffer(6)
buf.writeIntLE(0x1234567890ab, 0, 6)
console.log(buf.toString('hex')) // Prints: ab9078563412
})()
Writes value
to buf
at the specified offset
as big-endian. The value
must be a valid unsigned 16-bit integer. Behavior is undefined when value
is anything other than an unsigned 16-bit integer.
Number to be written to buf
.
Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - 2
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = new Buffer(4)
buf.writeUInt16BE(0xdead, 0)
buf.writeUInt16BE(0xbeef, 2)
console.log(buf.toString('hex')) // Prints: deadbeef
})()
Writes value
to buf
at the specified offset
as little-endian. The value
must be a valid unsigned 16-bit integer. Behavior is undefined when value
is anything other than an unsigned 16-bit integer.
Number to be written to buf
.
Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - 2
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = new Buffer(4)
buf.writeUInt16LE(0xdead, 0)
buf.writeUInt16LE(0xbeef, 2)
console.log(buf.toString('hex')) // Prints: addeefbe
})()
Writes value
to buf
at the specified offset
as big-endian. The value
must be a valid unsigned 32-bit integer. Behavior is undefined when value
is anything other than an unsigned 32-bit integer.
Number to be written to buf
.
Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - 4
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = new Buffer(4)
buf.writeUInt32BE(0xfeedface, 0)
console.log(buf.toString('hex')) // Prints: feedface
})()
Writes value
to buf
at the specified offset
as little-endian. The value
must be a valid unsigned 32-bit integer. Behavior is undefined when value
is anything other than an unsigned 32-bit integer.
Number to be written to buf
.
Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - 4
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = new Buffer(4)
buf.writeUInt32LE(0xfeedface, 0)
console.log(buf.toString('hex')) // Prints: cefaedfe
})()
Writes value
to buf
at the specified offset
. value
must be a valid unsigned 8-bit integer. Behavior is undefined when value
is anything other than an unsigned 8-bit integer.
Number to be written to buf
.
Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - 1
. Default: 0
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = new Buffer(4)
buf.writeUInt8(0x3, 0)
buf.writeUInt8(0x4, 1)
buf.writeUInt8(0x23, 2)
buf.writeUInt8(0x42, 3)
console.log(buf.toString('hex')) // Prints: 03042342
})()
Writes byteLength
bytes of value
to buf
at the specified offset
as big-endian. Supports up to 48 bits of accuracy. Behavior is undefined when value
is anything other than an unsigned integer.
Number to be written to buf
.
Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - byteLength
.
Number of bytes to write. Must satisfy 1 <= byteLength <= 6
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = new Buffer(6)
buf.writeUIntBE(0x1234567890ab, 0, 6)
console.log(buf.toString('hex')) // Prints: 1234567890ab
})()
Writes byteLength
bytes of value
to buf
at the specified offset
as little-endian. Supports up to 48 bits of accuracy. Behavior is undefined when value
is anything other than an unsigned integer.
Number to be written to buf
.
Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - byteLength
.
Number of bytes to write. Must satisfy 1 <= byteLength <= 6
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = new Buffer(6)
buf.writeUIntLE(0x1234567890ab, 0, 6)
console.log(buf.toString('hex')) // Prints: ab9078563412
})()
Calculates the xor of all bytes in buf
. If buf
is empty, returns 0
. You can use buf.subarray()
to calculate the xor of a subset of buf
. Xor is usually used to calculate checksums in serial communication.
The xor of all bytes in buf
.
;(async function () {
const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/+esm')
const buf = Buffer.from('010203040506070809', 'hex')
console.log(buf.xor()) // Prints: 1
})()
Static
ofReturns a new array from a set of elements.
Rest
...items: number[]A set of elements to include in the new array object.
Alias of Buffer.readBigUInt64BE.
Alias of Buffer.readBigUInt64LE.
Alias of Buffer.readUInt16BE.
Alias of Buffer.readUInt16LE.
Alias of Buffer.readUInt32BE.
Alias of Buffer.readUInt32LE.
Alias of Buffer.readUInt8.
Alias of Buffer.readUIntBE.
Alias of Buffer.readUIntLE.
Alias of Buffer.writeBigUInt64BE.
Alias of Buffer.writeBigUInt64LE.
Alias of Buffer.writeUInt16BE.
Alias of Buffer.writeUInt16LE.
Alias of Buffer.writeUInt32BE.
Alias of Buffer.writeUInt32LE.
Alias of Buffer.writeUInt8.
Alias of Buffer.writeUIntBE.
Alias of Buffer.writeUIntLE.
The
Buffer
class is a cross platform alternative of Node buffer base onUInt8Array
.See
See the Buffer | Node.js Documentation for more information.