-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
bytes.ts
70 lines (54 loc) · 2.08 KB
/
bytes.ts
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
/**
* This file provides common operations on Uint8Arrays.
* It should not use any Buffers to do so.
* Any function that uses Buffer should be in buffers.ts.
* This helps us avoid bringing in the heavy polyfill for Buffer
* when bundling for the browser.
*/
import { rfc4648 } from "../../deps.ts";
const decoder: TextDecoder = new TextDecoder();
const encoder: TextEncoder = new TextEncoder();
//--------------------------------------------------
export function bytesToString(bytes: Uint8Array): string {
return decoder.decode(bytes);
}
export function stringToBytes(str: string): Uint8Array {
return encoder.encode(str);
}
//--------------------------------------------------
export function stringLengthInBytes(str: string): number {
// TODO: is there a more efficient way to do this?
// If we had a Buffer we could just do Buffer.byteLength(str, 'utf-8');
return stringToBytes(str).length;
}
export function concatBytes(a: Uint8Array, b: Uint8Array): Uint8Array {
// Checks for truthy values or empty arrays on each argument
// to avoid the unnecessary construction of a new array and
// the type comparison
if (!b || b.length === 0) return a;
if (!a || a.length === 0) return b;
var c = new Uint8Array(a.length + b.length);
c.set(a);
c.set(b, a.length);
return c;
}
//--------------------------------------------------
export function b64StringToBytes(b64string: string): Uint8Array {
return rfc4648.base64.parse(b64string);
}
//--------------------------------------------------
export function isBytes(bytes: any): bytes is Uint8Array {
return bytes?.constructor?.name === "Uint8Array";
//return bytes.writeUInt8 === undefined && bytes instanceof Uint8Array;
}
export function isBuffer(buf: any): boolean {
// do this without any official reference to Buffer
// to avoid bringing in the Buffer polyfill
return buf?.constructor?.name === "Buffer";
//buf instanceof Buffer;
}
export function identifyBufOrBytes(bufOrBytes: any | Uint8Array): string {
if (isBytes(bufOrBytes)) return "bytes";
if (isBuffer(bufOrBytes)) return "buffer";
return "?";
}