summaryrefslogtreecommitdiffhomepage
path: root/packages/server/src/helpers/uint8array.ts
blob: 4e2d4dc91a11d9ac9627bb0a4b16892a35b54e3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
 * Make sure two Uint8Arrays are deeply equivalent
 */
export function areEqual(array1: Uint8Array, array2: Uint8Array): boolean {
  if (array1.length != array2.length) {
    return false;
  }

  return array1.every((val, i) => val === array2[i]);
}

/**
 * Convert a Uint8Array to Hexadecimal.
 *
 * A replacement for `Buffer.toString('hex')`
 */
export function toHex(array: Uint8Array): string {
  const hexParts = Array.from(array, i => i.toString(16).padStart(2, "0"));

  // adce000235bcc60a648b0b25f1f05503
  return hexParts.join('');
}

/**
 * Convert a Uint8Array to Base64.
 *
 * A replacement for `Buffer.toString('base64')`
 */
export function toBase64(array: Uint8Array): string {
  let str = '';

  for (const charCode of array) {
    str += String.fromCharCode(charCode);
  }

  const base64String = btoa(str);

  return base64String;
}

/**
 * Combine multiple Uint8Arrays into a single Uint8Array
 */
export function concat(arrays: Uint8Array[]): Uint8Array {
  let pointer = 0;
  const totalLength = arrays.reduce((prev, curr) => prev + curr.length, 0);

  const toReturn = new Uint8Array(totalLength);

  arrays.forEach((arr) => {
    toReturn.set(arr, pointer);
    pointer += arr.length;
  });

  return toReturn;
}

/**
 * Convert an ASCII string to Uint8Array
 */
export function fromString(value: string): Uint8Array {
  return Uint8Array.from(value.split("").map(x => x.charCodeAt(0)));
}

export default {
  areEqual,
  toHex,
  toBase64,
  concat,
  fromString,
};