The Buffer class is a cross platform alternative of Node buffer base on UInt8Array.

See

See the Buffer | Node.js Documentation for more information.

Hierarchy (view full)

Constructors

Properties

Static Methods

Methods

Method Aliases

Constructors

  • Creates a zero-length Buffer.

    Returns Buffer

  • Creates a new Buffer with length bytes. The contents are initialized to 0.

    Parameters

    • length: number

      The desired length of the new Buffer.

    Returns Buffer

    Example

    ;(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.

    Parameters

    • arrayLike: Iterable<number> | ArrayLike<number> | ArrayBufferView

      An array of bytes in the range 0255. Array entries outside that range will be truncated to fit into it.

    Returns Buffer

    Example

    ;(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.

    Parameters

    • arrayBuffer: ArrayBufferLike

      An ArrayBuffer or SharedArrayBuffer, for example the .buffer property of a TypedArray.

    • Optional byteOffset: number

      Index of first byte to expose. Default: 0.

    • Optional length: number

      Number of bytes to expose. Default: arrayBuffer.byteLength - byteOffset.

    Returns Buffer

    Example

    ;(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
    })()

Properties

BYTES_PER_ELEMENT: number

The size in bytes of each element in the array.

[toStringTag]: "Uint8Array"

The underlying ArrayBuffer object based on which this Buffer object is created.

byteLength: number

The length in bytes of the array.

byteOffset: number

The offset in bytes of the array.

length: number

The length of the array.

BYTES_PER_ELEMENT: number

The size in bytes of each element in the array.

Static Methods

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

    Parameters

    • size: number

      The desired length of the new Buffer.

    • Optional fill: number | Uint8Array | Buffer

      A value to pre-fill the new Buffer with. Default: 0.

    Returns Buffer

    Example

    ;(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.

    Parameters

    • size: number

      The desired length of the new Buffer.

    • fill: string

      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'.

    Returns Buffer

    Example

    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
    })()

    Example

    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
    })()
  • Allocates a new Buffer of size bytes. The contents are initialized to 0. This is equivalent to calling new Buffer(size).

    Parameters

    • size: number

      The desired length of the new Buffer.

    Returns Buffer

    Remarks

    This method is different from Node.js's Buffer.allocUnsafe() method.

    Example

    ;(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
    })()
  • Allocates a new Buffer of size bytes. The contents are initialized to 0. This is equivalent to calling new Buffer(size).

    Parameters

    • size: number

      The desired length of the new Buffer.

    Returns Buffer

    Remarks

    This method is different from Node.js's Buffer.allocUnsafeSlow() method.

    Example

    ;(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
    })()
  • Returns .byteLength if value is a Buffer/DataView/TypedArray/ArrayBuffer/SharedArrayBuffer.

    Parameters

    Returns number

    The number of bytes contained within value.

    Example

    ;(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.

    Parameters

    • string: 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'.

    Returns number

    The number of bytes contained within string.

    Example

    ;(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
    })()
  • Compares buf1 to buf2, typically for the purpose of sorting arrays of Buffer instances. This is equivalent to calling buf1.compare(buf2).

    Parameters

    Returns number

    Either -1, 0, or 1, depending on the result of the comparison. See buf.compare() for details.

    Example

    ;(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']
    })()
  • Returns 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 Buffers in list exceeds totalLength, the result is truncated to totalLength.

    Parameters

    • list: Buffer[]

      List of Buffer or Uint8Array instances to concatenate.

    • Optional totalLength: number

      Total length of the Buffer instances in list when concatenated.

    Returns Buffer

    Example

    ;(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
    })()
  • Copies the underlying memory of view into a new Buffer.

    Parameters

    • view: ArrayBufferView

      The TypedArray to copy.

    • offset: number = 0

      The starting offset within view. Default: 0.

    • Optional length: number

      The number of elements from view to copy. Default: view.length - offset.

    Returns Buffer

    Example

    ;(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
    })()
  • Parameters

    Returns boolean

    ture if buf1 and buf2 have the same byte length and contents, or false otherwise.

    Example

    ;(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
    })()
  • Allocates a new Buffer using an array of bytes in the range 0255. 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().

    Parameters

    Returns Buffer

    Example

    ;(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:

    Parameters

    • arrayBuffer: OrValueOf<ArrayBufferLike>

      An ArrayBuffer, SharedArrayBuffer, for example the .buffer property of a TypedArray.

    • Optional byteOffset: number

      Index of first byte to expose. Default: 0.

    • Optional length: number

      Number of bytes to expose. Default: arrayBuffer.byteLength - byteOffset.

    Returns Buffer

    Example

    ;(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
    })()

    Example

    ;(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
    })()

    Example

    ;(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.

    Parameters

    Returns Buffer

    Example

    ;(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().

    Parameters

    • json: {
          data: number[];
          type: "Buffer";
      }

      A JSON object returned from Buffer#toJSON().

      • data: number[]
      • type: "Buffer"

    Returns Buffer

    Example

    ;(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.

    Parameters

    • object: OrValueOf<string> | {
          [toPrimitive]: ((hint?) => string);
      }

      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'.

    Returns Buffer

    Example

    ;(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
    })()

    Example

    ;(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
    })()

    Example

    ;(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
    })()
  • Creates 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.

    Parameters

    • base64: string

      A string to encode.

    Returns Buffer

  • Creates 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.

    Parameters

    • base64: string

      A string to encode.

    Returns Buffer

  • Creates a new Buffer that encoded from the provided hex using hexadecimal encoding. Data truncation may occur when encode strings that do not exclusively consist of an even number of hexadecimal characters.

    Parameters

    • hex: string

      A string to encode.

    Returns Buffer

  • Creates 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.

    Parameters

    • latin1: string

      A string to encode.

    Returns Buffer

  • Creates a new Buffer that encoded from the provided string. The encoding parameter identifies the character encoding to be used when converting string into bytes.

    Parameters

    • string: string

      A string to encode.

    • encoding: "base64" | "binary" | "ucs-2" | "utf-16le" | "utf-8" | "ascii" | "base64url" | "hex" | "latin1" | "ucs2" | "utf16le" | "utf8"

      The encoding of string. Default: 'utf8'.

    Returns Buffer

  • Creates 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.

    Parameters

    • ucs2: string

      A string to encode.

    Returns Buffer

  • Creates 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.

    Parameters

    • utf8: string

      A string to encode.

    Returns Buffer

    See

    This method based on the TextEncoder API.

  • Creates a Buffer from view without copying the underlying memory.

    Parameters

    • view: ArrayBufferView

      The ArrayBufferView to create a Buffer from.

    • Optional offset: number

      The starting offset within view. Default: 0.

    • Optional length: number

      The number of elements from view to copy. Default: view.length - offset.

    Returns Buffer

  • Parameters

    • obj: any

    Returns obj is Buffer

    true if obj is a Buffer, false otherwise.

    Example

    ;(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
    })()
  • Parameters

    • encoding: any

      A character encoding name to check.

    Returns encoding is "base64" | "binary" | "ucs-2" | "utf-16le" | "utf-8" | "ascii" | "base64url" | "hex" | "latin1" | "ucs2" | "utf16le" | "utf8"

    true if encoding is the name of a supported character encoding, or false otherwise.

    Example

    ;(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
    })()
  • 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().

    Type Parameters

    • T extends any[]

    Parameters

    • buf: Buffer

      A Buffer to unpack from.

    • format: string

      A format string.

    Returns Generator<T, any, unknown>

    Remarks

    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.

    Example

    ;(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]]
    })()
  • Creates a new Buffer containing the vals packed according to the format string format. The arguments must match the vals required by the format exactly.

    Parameters

    • format: string

      A format string.

    • Rest ...vals: any[]

      Values to pack.

    Returns Buffer

    Remarks

    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.

    See

    Please refer to Python struct — Interpret bytes as packed binary data for more information.

    Example

    ;(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().

    Parameters

    • buf: Buffer

      A Buffer to pack into.

    • format: string

      A format string.

    • Rest ...vals: any[]

      Values to pack.

    Returns Buffer

    Remarks

    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.

    Example

    ;(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
    })()
  • Return the required size corresponding to the format string formatOrItems.

    Parameters

    • formatOrItems: string | [number, string][]

      A format string or parsed items return by Buffer.packParseFormat().items.

    Returns number

    Remarks

    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.

    Example

    ;(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
    })()
  • Parse a format string format. This is a internal method used by Buffer.packCalcSize(), Buffer.pack() and Buffer.unpack().

    Parameters

    • format: string

      A format string.

    Returns PackFormat

    Remarks

    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.

  • Decodes buf to a string according to the Base64 character encoding.

    Parameters

    • buf: Buffer

      The buffer to decode.

    Returns string

  • Decodes buf to a string according to the Base64 URL character encoding.

    Parameters

    • buf: Buffer

      The buffer to decode.

    Returns string

  • Decodes buf to a string according to the hexadecimal character encoding.

    Parameters

    • buf: Buffer

      The buffer to decode.

    Returns string

  • Decodes 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.

    Parameters

    • buf: Buffer

      The buffer to decode.

    Returns string

  • Decodes buf to a string according to the UCS-2 character encoding.

    Parameters

    • buf: Buffer

      The buffer to decode.

    Returns string

  • Decodes buf to a string according to the UTF-8 character encoding.

    Parameters

    • buf: Buffer

      The buffer to decode.

    Returns string

    See

    This method based on the TextDecoder API.

  • 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().

    Type Parameters

    • T extends any[]

    Parameters

    • buf: Buffer

      A Buffer to unpack from.

    • format: string

      A format string.

    Returns T

    Remarks

    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.

    Example

    ;(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]
    })()

Methods

  • A helper function which Buffer.isBuffer() will invoke and determine whether this is a Buffer or not.

    Returns boolean

    true if this is a Buffer, false otherwise.

  • Custom inspect functions which util.inspect() will invoke and use the result of when inspecting the object.

    Returns string

    a string representation of buf.

  • Returns the item located at the specified index.

    Parameters

    • index: number

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

    Returns undefined | number

  • Creates an array of chunk Buffers which length is bytesPerChunk. If buf's contents can't be split evenly, the final chunk will be the remaining contents.

    Parameters

    • bytesPerChunk: number

      The length of each chunk

    Returns Buffer[]

    The new array of chunks.

    Example

    ;(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.

    Parameters

    • target: any

      A Buffer or Uint8Array with which to compare buf.

    • targetStart: number = 0

      The offset within target at which to begin comparison. Default: 0.

    • targetEnd: number = target.length

      The offset within target at which to end comparison (not inclusive). Default: target.length.

    • sourceStart: number = 0

      The offset within buf at which to begin comparison. Default: 0.

    • sourceEnd: number = ...

      The offset within buf at which to end comparison (not inclusive). Default: this.length.

    Returns number

    • 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.

    Example

    ;(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']
    })()

    Example

    ;(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 Buffers, although it takes different function arguments.

    Parameters

    • target: ArrayBufferView

      A Buffer or Uint8Array to copy into.

    • targetStart: number = 0

      The offset within target at which to begin writing. Default: 0.

    • sourceStart: number = 0

      The offset within buf from which to begin copying. Default: 0.

    • sourceEnd: number = ...

      The offset within buf at which to stop copying (not inclusive). Default: this.length.

    Returns number

    The number of bytes copied.

    Example

    ;(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!!!!!!!!!!!!!
    })()

    Example

    ;(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

    Parameters

    • 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.

    • Optional end: number

      If not specified, length of the this object is used as its default value.

    Returns this

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

    Returns IterableIterator<[number, number]>

  • Parameters

    • otherBuffer: Uint8Array | Buffer

      A Buffer or Uint8Array with which to compare buf.

    Returns boolean

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

    Example

    ;(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.

    Parameters

    • predicate: ((value, index, array) => 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.

        • (value, index, array): unknown
        • Parameters

          Returns unknown

    • Optional 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.

    Returns boolean

  • Parameters

    • value: string

      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'.

    Returns this

  • Parameters

    • value: string

      The value with which to fill buf. Empty string is coerced to 0.

    • offset: number

      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'.

    Returns this

  • Parameters

    • value: string

      The value with which to fill buf. Empty string is coerced to 0.

    • offset: number

      Number of bytes to skip before starting to fill buf. Default: 0.

    • end: number

      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'.

    Returns this

  • Parameters

    • value: number | Uint8Array | Buffer

      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: number

      Number of bytes to skip before starting to fill buf. Default: 0.

    • Optional end: number

      Where to stop filling buf (not inclusive). Default: buf.length.

    Returns this

    Example

    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
    })()

    Example

    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
    })()

    Example

    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.

    Parameters

    • predicate: ((value, index, array) => any)

      A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.

        • (value, index, array): any
        • Parameters

          Returns any

    • Optional 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.

    Returns Uint8Array

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

    Parameters

    • predicate: ((value, index, obj) => 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.

        • (value, index, obj): boolean
        • Parameters

          Returns boolean

    • Optional 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.

    Returns undefined | number

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

    Parameters

    • predicate: ((value, index, obj) => 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.

        • (value, index, obj): boolean
        • Parameters

          Returns boolean

    • Optional 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.

    Returns number

  • Performs the specified action for each element in an array.

    Parameters

    • callbackfn: ((value, index, array) => void)

      A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.

        • (value, index, array): void
        • Parameters

          Returns void

    • Optional 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.

    Returns void

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

    Parameters

    • value: string

      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'.

    Returns boolean

    true if value was found in buf, false otherwise.

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

    Parameters

    • value: string

      What to search for.

    • byteOffset: number

      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'.

    Returns boolean

    true if value was found in buf, false otherwise.

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

    Parameters

    • value: number | Uint8Array | Buffer

      What to search for.

    • Optional byteOffset: number

      Where to begin searching in buf. If negative, then offset is calculated from the end of buf. Default: 0.

    Returns boolean

    true if value was found in buf, false otherwise.

    Example

    ;(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
    })()
  • Parameters

    • value: string

      What to search for.

      • If value is a string, value is interpreted according to the character encoding in encoding.
      • If value is a Buffer or Uint8Array, value will be used in its entirety. To compare a partial Buffer, use buf.subarray.
      • If value is a number, it will be coerced to a valid byte value, an integer between 0 and 255.
      • If value is an empty string or empty Buffer and byteOffset is less than buf.length, byteOffset will be returned.
      • If 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'.

    Returns number

    • The index of the first occurrence of value in buf
    • -1 if buf does not contain value.

    Throws

    • If value is not a string, number, or Buffer, this method will throw a TypeError.
  • Parameters

    • value: string

      What to search for.

      • If value is a string, value is interpreted according to the character encoding in encoding.
      • If value is a Buffer or Uint8Array, value will be used in its entirety. To compare a partial Buffer, use buf.subarray.
      • If value is a number, it will be coerced to a valid byte value, an integer between 0 and 255.
      • If value is an empty string or empty Buffer and byteOffset is less than buf.length, byteOffset will be returned.
      • If value is empty and byteOffset is at least buf.length, buf.length will be returned.
    • byteOffset: number

      Where to begin searching in buf. Default: 0.

      • If negative, then offset is calculated from the end of buf.
      • If not a integer, it will be coerced to integer by _.toSafeInteger.
    • Optional encoding: "base64" | "binary" | "ucs-2" | "utf-16le" | "utf-8" | "ascii" | "base64url" | "hex" | "latin1" | "ucs2" | "utf16le" | "utf8"

      The encoding of value. Default: 'utf8'.

    Returns number

    • The index of the first occurrence of value in buf
    • -1 if buf does not contain value.

    Throws

    • If value is not a string, number, or Buffer, this method will throw a TypeError.
  • Parameters

    • value: number | Uint8Array | Buffer

      What to search for.

      • If value is a string, value is interpreted according to the character encoding in encoding.
      • If value is a Buffer or Uint8Array, value will be used in its entirety. To compare a partial Buffer, use buf.subarray.
      • If value is a number, it will be coerced to a valid byte value, an integer between 0 and 255.
      • If value is an empty string or empty Buffer and byteOffset is less than buf.length, byteOffset will be returned.
      • If value is empty and byteOffset is at least buf.length, buf.length will be returned.
    • Optional byteOffset: number

      Where to begin searching in buf. Default: 0.

      • If negative, then offset is calculated from the end of buf.
      • If not a integer, it will be coerced to integer by _.toSafeInteger.

    Returns number

    • The index of the first occurrence of value in buf
    • -1 if buf does not contain value.

    Throws

    • If value is not a string, number, or Buffer, this method will throw a TypeError.

    Example

    ;(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
    })()

    Example

    ;(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().

    Type Parameters

    • T extends any[]

    Parameters

    • format: string

      A format string.

    Returns Generator<T, any, unknown>

    Remarks

    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.

    Example

    ;(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.

    Parameters

    • Optional 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.

    Returns string

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

    Parameters

    • value: string

      What to search for.

      • If value is a string, value is interpreted according to the character encoding in encoding.
      • If value is a Buffer or Uint8Array, value will be used in its entirety. To compare a partial Buffer, use buf.subarray.
      • If value is a number, it will be coerced to a valid byte value, an integer between 0 and 255.
      • If 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'.

    Returns number

    • The index of the last occurrence of value in buf
    • -1 if buf does not contain value.

    Throws

    • If 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.

    Parameters

    • value: string

      What to search for.

      • If value is a string, value is interpreted according to the character encoding in encoding.
      • If value is a Buffer or Uint8Array, value will be used in its entirety. To compare a partial Buffer, use buf.subarray.
      • If value is a number, it will be coerced to a valid byte value, an integer between 0 and 255.
      • If value is an empty string or empty Buffer, byteOffset will be returned.
    • byteOffset: number

      Where to begin searching in buf. Default: buf.length - 1.

      • If negative, then offset is calculated from the end of buf.
      • If not a integer, it will be coerced to integer by _.toSafeInteger.
    • Optional encoding: "base64" | "binary" | "ucs-2" | "utf-16le" | "utf-8" | "ascii" | "base64url" | "hex" | "latin1" | "ucs2" | "utf16le" | "utf8"

      The encoding of value. Default: 'utf8'.

    Returns number

    • The index of the last occurrence of value in buf
    • -1 if buf does not contain value.

    Throws

    • If 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.

    Parameters

    • value: number | Uint8Array | Buffer

      What to search for.

      • If value is a number, it will be coerced to a valid byte value, an integer between 0 and 255.
      • If value is an empty string or empty Buffer, byteOffset will be returned.
    • Optional byteOffset: number

      Where to begin searching in buf. Default: buf.length - 1.

      • If negative, then offset is calculated from the end of buf.
      • If not a integer, it will be coerced to integer by _.toSafeInteger.

    Returns number

    • The index of the last occurrence of value in buf
    • -1 if buf does not contain value.

    Throws

    • If value is not a string, number, or Buffer, this method will throw a TypeError.

    Example

    ;(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
    })()

    Example

    ;(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.

    Parameters

    • callbackfn: ((value, index, array) => number)

      A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.

        • (value, index, array): number
        • Parameters

          Returns number

    • Optional 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.

    Returns Uint8Array

  • 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().

    Parameters

    • format: string

      A format string.

    • Rest ...vals: any[]

      Values to pack.

    Returns this

    Remarks

    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.

    Example

    ;(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.

    Parameters

    • offset: number = 0

      Number of bytes to skip before starting to read. Must satisfy: 0 <= offset <= buf.length - 8. Default: 0.

    Returns bigint

    Example

    ;(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.

    Parameters

    • offset: number = 0

      Number of bytes to skip before starting to read. Must satisfy: 0 <= offset <= buf.length - 8. Default: 0.

    Returns bigint

    Example

    ;(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.

    Parameters

    • offset: number = 0

      Number of bytes to skip before starting to read. Must satisfy: 0 <= offset <= buf.length - 8. Default: 0.

    Returns bigint

    Example

    ;(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.

    Parameters

    • offset: number = 0

      Number of bytes to skip before starting to read. Must satisfy: 0 <= offset <= buf.length - 8. Default: 0.

    Returns bigint

    Example

    ;(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.

    Parameters

    • bitOffset: number

      bit offset to read from.

    Returns number

    Example

    ;(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.

    Parameters

    • bitOffset: number

      bit offset to read from.

    Returns number

    Example

    ;(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.

    Parameters

    • offset: number = 0

      Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 8. Default: 0.

    Returns number

    Example

    ;(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.

    Parameters

    • offset: number = 0

      Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 8. Default: 0.

    Returns number

    Example

    ;(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.

    Parameters

    • offset: number = 0

      Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 2. Default: 0.

    Returns number

    Example

    ;(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.

    Parameters

    • offset: number = 0

      Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 2. Default: 0.

    Returns number

    Example

    ;(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.

    Parameters

    • offset: number = 0

      Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 4. Default: 0.

    Returns number

    Example

    ;(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.

    Parameters

    • offset: number = 0

      Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 4. Default: 0.

    Returns number

    Example

    ;(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.

    Parameters

    • offset: number = 0

      Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 2. Default: 0.

    Returns number

    Example

    ;(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.

    Parameters

    • offset: number = 0

      Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 2. Default: 0.

    Returns number

    Example

    ;(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.

    Parameters

    • offset: number = 0

      Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 4. Default: 0.

    Returns number

    Example

    ;(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.

    Parameters

    • offset: number = 0

      Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 4. Default: 0.

    Returns number

    Example

    ;(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.

    Parameters

    • offset: number = 0

      Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 1. Default: 0.

    Returns number

    Example

    ;(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.

    Parameters

    • offset: number = 0

      Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - byteLength.

    • byteLength: number = 6

      Number of bytes to read. Must satisfy 1 <= byteLength <= 6.

    Returns number

    Example

    ;(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.

    Parameters

    • offset: number = 0

      Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - byteLength.

    • byteLength: number = 6

      Number of bytes to read. Must satisfy 1 <= byteLength <= 6.

    Returns number

    Example

    ;(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.

    Parameters

    • offset: number = 0

      Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 2. Default: 0.

    Returns number

    Example

    ;(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.

    Parameters

    • offset: number = 0

      Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 2. Default: 0.

    Returns number

    Example

    ;(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.

    Parameters

    • offset: number = 0

      Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 4. Default: 0.

    Returns number

    Example

    ;(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.

    Parameters

    • offset: number = 0

      Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 4. Default: 0.

    Returns number

    Example

    ;(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.

    Parameters

    • offset: number = 0

      Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 1. Default: 0.

    Returns number

    Example

    ;(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.

    Parameters

    • offset: number = 0

      Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - byteLength.

    • byteLength: number = 6

      Number of bytes to read. Must satisfy 0 < byteLength <= 6.

    Returns number

    Example

    ;(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.

    Parameters

    • offset: number = 0

      Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - byteLength.

    • byteLength: number = 6

      Number of bytes to read. Must satisfy 0 < byteLength <= 6.

    Returns number

    Example

    ;(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.

    Parameters

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

      A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.

        • (previousValue, currentValue, currentIndex, array): number
        • Parameters

          • previousValue: number
          • currentValue: number
          • currentIndex: number
          • array: Uint8Array

          Returns number

    Returns number

  • Parameters

    • callbackfn: ((previousValue, currentValue, currentIndex, array) => number)
        • (previousValue, currentValue, currentIndex, array): number
        • Parameters

          • previousValue: number
          • currentValue: number
          • currentIndex: number
          • array: Uint8Array

          Returns number

    • initialValue: number

    Returns number

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

    Type Parameters

    • U

    Parameters

    • callbackfn: ((previousValue, currentValue, currentIndex, array) => U)

      A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.

        • (previousValue, currentValue, currentIndex, array): U
        • Parameters

          • previousValue: U
          • currentValue: number
          • currentIndex: number
          • array: Uint8Array

          Returns U

    • initialValue: U

      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.

    Returns U

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

    Parameters

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

      A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.

        • (previousValue, currentValue, currentIndex, array): number
        • Parameters

          • previousValue: number
          • currentValue: number
          • currentIndex: number
          • array: Uint8Array

          Returns number

    Returns number

  • Parameters

    • callbackfn: ((previousValue, currentValue, currentIndex, array) => number)
        • (previousValue, currentValue, currentIndex, array): number
        • Parameters

          • previousValue: number
          • currentValue: number
          • currentIndex: number
          • array: Uint8Array

          Returns number

    • initialValue: number

    Returns number

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

    Type Parameters

    • U

    Parameters

    • callbackfn: ((previousValue, currentValue, currentIndex, array) => U)

      A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.

        • (previousValue, currentValue, currentIndex, array): U
        • Parameters

          • previousValue: U
          • currentValue: number
          • currentIndex: number
          • array: Uint8Array

          Returns U

    • initialValue: U

      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.

    Returns U

  • Creates a new Buffer which is reverse of the original buf.

    Returns Buffer

    Example

    ;(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.

    Parameters

    • array: ArrayLike<number>

      A typed or untyped array of values to set.

    • Optional offset: number

      The index in the current array at which the values are to be written.

    Returns void

  • 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.

    Parameters

    • start: number = 0

      Where the new Buffer will start. Default: 0.

    • end: number = ...

      Where the new Buffer will end (not inclusive). Default: buf.length.

    Returns Buffer

    Remarks

    This method is different from Node.js's Buffer.slice() method.

    Example

    ;(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.

    Parameters

    • predicate: ((value, index, array) => 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.

        • (value, index, array): unknown
        • Parameters

          Returns unknown

    • Optional 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.

    Returns boolean

  • Sorts an array.

    Parameters

    • 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)
      
        • (a, b): number
        • Parameters

          • a: number
          • b: number

          Returns number

    Returns this

  • 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.

    Parameters

    • start: number = 0

      Where the new Buffer will start. Default: 0.

    • end: number = ...

      Where the new Buffer will end (not inclusive). Default: buf.length.

    Returns Buffer

    Example

    ;(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
    })()

    Example

    ;(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.

    Returns this

    A reference to buf.

    Throws

    If buf.length is not a multiple of 2, this method will throw a RangeError.

    Example

    ;(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.

    Returns this

    A reference to buf.

    Throws

    If buf.length is not a multiple of 4, this method will throw a RangeError.

    Example

    ;(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.

    Returns this

    A reference to buf.

    Throws

    If buf.length is not a multiple of 8, this method will throw a RangeError.

    Example

    ;(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).

    Returns {
        data: number[];
        type: "Buffer";
    }

    a JSON representation of buf.

    • data: number[]
    • type: "Buffer"

    Example

    ;(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
    })()
  • Converts a number to a string by using the current locale.

    Returns string

  • 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.

    Parameters

    • encoding: "base64" | "binary" | "ucs-2" | "utf-16le" | "utf-8" | "ascii" | "base64url" | "hex" | "latin1" | "ucs2" | "utf16le" | "utf8" = 'utf8'

      The character encoding to use. Default: 'utf8'.

    • start: number = 0

      The byte offset to start decoding at. Default: 0.

    • end: number = ...

      The byte offset to stop decoding at (not inclusive). Default: buf.length.

    Returns string

    Example

    ;(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().

    Type Parameters

    • T extends any[]

    Parameters

    • format: string

      A format string.

    Returns T

    Remarks

    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.

    Example

    ;(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]
    })()
  • 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.

    Parameters

    • string: string

      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'.

    Returns number

    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.

    Parameters

    • string: string

      String to write to buf.

    • offset: number

      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'.

    Returns number

    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.

    Parameters

    • string: string

      String to write to buf.

    • offset: number

      Number of bytes to skip before starting to write string. Default: 0.

    • length: number

      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'.

    Returns number

    Number of bytes written.

    Example

    ;(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.

    Parameters

    • val: bigint

      Number to be written to buf.

    • offset: number = 0

      Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 8. Default: 0.

    Returns this

    Example

    ;(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.

    Parameters

    • val: bigint

      Number to be written to buf.

    • offset: number = 0

      Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 8. Default: 0.

    Returns this

    Example

    ;(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.

    Parameters

    • val: bigint

      Number to be written to buf.

    • offset: number = 0

      Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 8. Default: 0.

    Returns this

    Example

    ;(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.

    Parameters

    • val: bigint

      Number to be written to buf.

    • offset: number = 0

      Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 8. Default: 0.

    Returns this

    Example

    ;(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.

    Parameters

    • val: number | boolean

      Bit value to write.

    • bitOffset: number

      bit offset to read from.

    Returns this

    Example

    ;(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.

    Parameters

    • val: number | boolean

      Bit value to write.

    • bitOffset: number

      bit offset to read from.

    Returns this

    Example

    ;(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.

    Parameters

    • val: number

      Number to be written to buf.

    • offset: number = 0

      Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 8. Default: 0.

    Returns this

    Example

    ;(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.

    Parameters

    • val: number

      Number to be written to buf.

    • offset: number = 0

      Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 8. Default: 0.

    Returns this

    Example

    ;(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.

    Parameters

    • val: number

      Number to be written to buf.

    • offset: number = 0

      Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 2. Default: 0.

    Returns this

    Example

    ;(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.

    Parameters

    • val: number

      Number to be written to buf.

    • offset: number = 0

      Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 2. Default: 0.

    Returns this

    Example

    ;(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.

    Parameters

    • val: number

      Number to be written to buf.

    • offset: number = 0

      Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 4. Default: 0.

    Returns this

    Example

    ;(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.

    Parameters

    • val: number

      Number to be written to buf.

    • offset: number = 0

      Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 4. Default: 0.

    Returns this

    Example

    ;(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.

    Parameters

    • val: number

      Number to be written to buf.

    • offset: number = 0

      Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 2. Default: 0.

    Returns this

    Example

    ;(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.

    Parameters

    • val: number

      Number to be written to buf.

    • offset: number = 0

      Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 2. Default: 0.

    Returns this

    Example

    ;(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.

    Parameters

    • val: number

      Number to be written to buf.

    • offset: number = 0

      Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 4. Default: 0.

    Returns this

    Example

    ;(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.

    Parameters

    • val: number

      Number to be written to buf.

    • offset: number = 0

      Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 4. Default: 0.

    Returns this

    Example

    ;(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.

    Parameters

    • val: number

      Number to be written to buf.

    • offset: number = 0

      Number of bytes to skip before starting to write. Must satisfy: 0 <= offset <= buf.length - 1. Default: 0.

    Returns this

    Example

    ;(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.

    Parameters

    • val: number

      Number to be written to buf.

    • offset: number = 0

      Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - byteLength.

    • byteLength: number = 6

      Number of bytes to write. Must satisfy 1 <= byteLength <= 6.

    Returns this

    Example

    ;(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.

    Parameters

    • val: number

      Number to be written to buf.

    • offset: number = 0

      Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - byteLength.

    • byteLength: number = 6

      Number of bytes to write. Must satisfy 1 <= byteLength <= 6.

    Returns this

    Example

    ;(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.

    Parameters

    • val: number

      Number to be written to buf.

    • offset: number = 0

      Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - 2. Default: 0.

    Returns this

    Example

    ;(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.

    Parameters

    • val: number

      Number to be written to buf.

    • offset: number = 0

      Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - 2. Default: 0.

    Returns this

    Example

    ;(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.

    Parameters

    • val: number

      Number to be written to buf.

    • offset: number = 0

      Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - 4. Default: 0.

    Returns this

    Example

    ;(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.

    Parameters

    • val: number

      Number to be written to buf.

    • offset: number = 0

      Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - 4. Default: 0.

    Returns this

    Example

    ;(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.

    Parameters

    • val: number

      Number to be written to buf.

    • offset: number = 0

      Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - 1. Default: 0.

    Returns this

    Example

    ;(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.

    Parameters

    • val: number

      Number to be written to buf.

    • offset: number = 0

      Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - byteLength.

    • byteLength: number = 6

      Number of bytes to write. Must satisfy 1 <= byteLength <= 6.

    Returns this

    Example

    ;(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.

    Parameters

    • val: number

      Number to be written to buf.

    • offset: number = 0

      Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - byteLength.

    • byteLength: number = 6

      Number of bytes to write. Must satisfy 1 <= byteLength <= 6.

    Returns this

    Example

    ;(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.

    Returns number

    The xor of all bytes in buf.

    Example

    ;(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
    })()
  • Returns a new array from a set of elements.

    Parameters

    • Rest ...items: number[]

      A set of elements to include in the new array object.

    Returns Uint8Array

Method Aliases