Quark #

Modules #

Quark v1.4.0 Documentation

Contents

quark/buffer #

Encoding #

All encoding algorithms of string

type Encoding =
  "binary"
  | "latin1"
  | "ascii"
  | "base64"
  | "hex"
  | "utf-8"
  | "utf8"
  | "utf-16"
  | "utf16"
  | "ucs4"

Bytes #

Bytes type

type Bytes = Uint8ClampedArray | Uint8Array | Buffer

Class: Buffer #

Buffer #

This class represents a binary buffer.

Extends: Uint8Array

buffer.copy(target,targetStart?,sourceStart?,sourceEnd?) #

Copy current buffer data to the target buffer.

copy(target: ArrayBufferView, targetStart?: Uint, sourceStart?: Uint, sourceEnd?: Uint): Uint

Parameters:

  • target — The target buffer to copy data into.
  • targetStart? — The starting index in the target buffer, Default is 0.
  • sourceStart? — The starting index in the source buffer, Default is 0.
  • sourceEnd? — The ending index in the source buffer, Default is the length of the source buffer.

Returns: The number of bytes copied.

  • @throws {TypeError} If the target is not a valid typed array.
  • @throws {RangeError} If the target start index is out of range.
  • @throws {RangeError} If the source start or end index is out of range.

For example:

const source = new Buffer([1, 2, 3, 4, 5]);
const target = new Buffer(3);
const bytesCopied = source.copy(target, 0, 1, 4);
console.log(bytesCopied); // Outputs: 3
console.log(target); // Outputs: Uint8Array(3) [2, 3, 4]

buffer.slice(start?,end?) #

slice the buffer data into a new buffer, shared memory with the original buffer.

slice(start?: Int, end?: Uint): Buffer

buffer.subarray(begin?,end?) #

subarray the buffer data into a new buffer, shared memory with the original buffer.

subarray(begin?: Int, end?: Uint): Buffer

buffer.toJSON() #

JSON representation of the buffer.

toJSON(): 

buffer.write(source,offset?) #

writing data to self buffer.

write(source: ArrayBufferView, offset?: Uint): Uint

buffer.compare(target,start?,end?,thisStart?,thisEnd?) #

Compare this buffer with another buffer or Uint8Array.

compare(target: Uint8Array, start?: Uint, end?: Uint, thisStart?: Uint, thisEnd?: Uint): Int

Parameters:

  • target — The target buffer or Uint8Array to compare with.
  • start? — The starting index in the target buffer, Default is 0.
  • end? — The ending index in the target buffer, Default is the length of the target buffer.
  • thisStart? — The starting index in this buffer, Default is 0.
  • thisEnd? — The ending index in this buffer, Default is the length of this buffer.

Returns: Returns 0 if the buffers are equal, a negative number if this buffer is less than the target, or a positive number if this buffer is greater than the target.

  • @throws {TypeError} If the target is not a valid typed array.
  • @throws {RangeError} If the start or end indices are out of range.

For example:

const buf1 = new Buffer([1, 2, 3]);
const buf2 = new Buffer([1, 2, 3, 4]);
console.log(buf1.compare(buf2)); // Outputs: -1 (buf1 is less than buf2)
console.log(buf2.compare(buf1)); // Outputs: 1 (buf2 is greater than buf1)
console.log(buf1.compare(buf1)); // Outputs: 0 (buf1 is equal to itself)

buffer.equals(b) #

Check if this buffer is equal to another buffer or Uint8Array.

equals(b: Uint8Array): boolean

Parameters:

  • b — The buffer or Uint8Array to compare with.

Returns: Returns true if the buffers are equal, false otherwise.

  • @throws {TypeError} If the argument is not a valid Uint8Array.

For example:

const buf1 = new Buffer([1, 2, 3]);
const buf2 = new Buffer([1, 2, 3]);
const buf3 = new Buffer([4, 5, 6]);
console.log(buf1.equals(buf2)); // Outputs: true (buf1 is equal to buf2)
console.log(buf1.equals(buf3)); // Outputs: false (buf1 is not equal to buf3)

buffer.toString(encoding?,start?,end?) #

Convert the buffer to a string using the specified encoding.

toString(encoding?: Encoding, start?: Uint, end?: Uint): string

Parameters:

  • encoding? — The encoding to use, Default is 'utf8'.
  • start? — The starting index, Default is 0.
  • end? — The ending index, Default is the length of the buffer.

Returns: The string representation of the buffer.

For example:

const buf = new Buffer([72, 101, 108, 108,111]);
console.log(buf.toString('utf8')); // Outputs: Hello

buffer.readInt8(offset?) #

Read int8 from the buffer at the specified offset.

readInt8(offset?: Uint): Int8

buffer.readUInt8(offset?) #

Read uint8 from the buffer at the specified offset.

readUInt8(offset?: Uint): Uint8

Zero #

Zero buffer

const Zero: Buffer

alloc(size,initFill?) #

Alloc new buffer

alloc(size: Uint, initFill?: Uint): Buffer

allocUnsafe(size) #

Alloc unsafe buffer

allocUnsafe(size: Uint): Buffer

isTypedArray(arr) #

Tests whether it is a typed array

isTypedArray(arr: any): boolean

isUint8Array(arr) #

Tests whether it is a Uint8Array

isUint8Array(arr: any): arr is Uint8Array

isBuffer(arr) #

Tests whether it is a buffer data type

isBuffer(arr: any): arr is Buffer

compare(a,b) #

Compare sizes of two buffers

compare(a: Uint8Array, b: Uint8Array): Int

encodeUTF8Length(str) #

Calculate the length of the string after encoding using the specified algorithm

encodeUTF8Length(str: string): Uint

toString(src,encoding?,start?,end?) #

Decode data into a string using the specified algorithm

toString(src: Uint8Array, encoding?: Encoding, start?: Uint, end?: Uint): string

fromString(str,targetEn?) #

Specify the algorithm to encode the string and return the encoded data

fromString(str: string, targetEn?: Encoding): Uint8Array

Parameters:

  • str — The string to encode.
  • targetEn? — Optional, the encoding algorithm to use, Default is 'utf8'.

Returns: A Uint8Array containing the encoded data.

For example:

const encoded = fromString("Hello, World!", "utf8");
console.log(encoded); // Outputs: Uint8Array(13) [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]

from(from,encoding?) #

From string create a buffer

from(from: string, encoding?: Encoding): Buffer

Parameters:

  • from — a string to create a buffer from
  • encoding? — Optional, this specifies the encoding to use.

from(from) #

From ArrayBufferView create a buffer

from(from: ArrayBufferView): Buffer

Parameters:

  • from — a TypedArray or DataView to create a buffer from

from(from,byteOffset?,length?) #

From ArrayBufferLike create a buffer

from(from: ArrayBufferLike, byteOffset?: Uint, length?: Uint): Buffer

Parameters:

  • from — an ArrayBuffer or SharedArrayBuffer to create a buffer from
  • byteOffset? — Optional, the offset in bytes to start from, Default is 0.
  • length? — Optional, the length in bytes of the buffer, Default is the remaining length of the ArrayBuffer.

For example:

const buffer = from(new ArrayBuffer(10), 0, 10);
console.log(buffer.byteLength); // Outputs: 10

from(from,mapFn?) #

From Iterable or ArrayLike create a buffer

from(from: Iterable<Int> | ArrayLike<Int>, mapFn?: MapFn): Buffer

Parameters:

  • from — an iterable or array-like object to create a buffer from
  • mapFn? — Optional, a mapping function to apply to each element.

For example:

const buffer = from([1, 2, 3, 4], (v, k) => v * 2);
console.log(buffer); // Outputs: Uint8Array(4) [2, 4, 6, 8]

byteLength(arg,encoding?) #

Calculate the byte length of a string when encoded with the specified encoding.

byteLength(arg: FromArg, encoding?: Encoding): Uint

Parameters:

  • arg — The string whose byte length is to be calculated.
  • encoding? — Optional, the encoding to use. If not provided, 'utf8' is assumed.

Returns: The byte length of the encoded string.

For example:

const length = byteLength("Hello, World!", "utf8");
console.log(length); // Outputs: 13

concat(list,length?) #

Concat multiple buffers into a new buffer

concat(list: ArrayLike<Int>[], length?: Uint): Buffer

Parameters:

  • list — An array of byte arrays to concatenate.
  • length? — Optional, the total length of the new buffer. If not provided, it will be calculated based on the lengths of the byte arrays in the list.

Returns: A new Buffer containing the concatenated data.

For example:

const buffer1 = new Buffer([1, 2, 3]);
const buffer2 = new Buffer([4, 5, 6]);
const concatenated = concat([buffer1, buffer2]);
console.log(concatenated); // Outputs: Uint8Array(6) [1, 2, 3, 4, 5, 6]
  • @throws {TypeError} If the provided list is not an array-like object or if any of the elements in the list are not of type Uint8Array or Buffer.
  • @throws {RangeError} If the specified length is negative.
  • @throws {Error} If the concatenation fails for any reason.

default #

default.from #

from: from