-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.ts
More file actions
64 lines (59 loc) · 1.89 KB
/
Copy pathutils.ts
File metadata and controls
64 lines (59 loc) · 1.89 KB
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
export function clampUint16(x: bigint): number {
if (x < 0n) return 0;
if (x > 65535n) return 65535;
return Number(x);
}
export function getRespBodyBytes(resp: any): Uint8Array {
const b = resp?.body;
if (!b) return new Uint8Array();
if (b instanceof Uint8Array) return b;
if (Array.isArray(b)) return new Uint8Array(b);
if (ArrayBuffer.isView(b) && b.buffer) return new Uint8Array(b.buffer, b.byteOffset, b.byteLength);
return new Uint8Array();
}
export function parseJsonBody(resp: any): any {
const bodyBytes = getRespBodyBytes(resp);
const text = new TextDecoder().decode(bodyBytes);
return JSON.parse(text);
}
export function extractJson(text: string): any | null {
const s = text.indexOf("{");
const e = text.lastIndexOf("}");
if (s === -1 || e === -1 || e <= s) return null;
try {
return JSON.parse(text.slice(s, e + 1));
} catch {
return null;
}
}
export function safeSnippet(s: string, max = 180): string {
const oneLine = s.replace(/\s+/g, " ").trim();
return oneLine.length <= max ? oneLine : oneLine.slice(0, max) + "…";
}
// (kept as-is; CRE HTTP wants base64-encoded body)
export function bytesToBase64(bytes: Uint8Array): string {
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
let out = "";
let i = 0;
for (; i + 2 < bytes.length; i += 3) {
const n = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2];
out +=
alphabet[(n >> 18) & 63] +
alphabet[(n >> 12) & 63] +
alphabet[(n >> 6) & 63] +
alphabet[n & 63];
}
const rem = bytes.length - i;
if (rem === 1) {
const n = bytes[i] << 16;
out += alphabet[(n >> 18) & 63] + alphabet[(n >> 12) & 63] + "==";
} else if (rem === 2) {
const n = (bytes[i] << 16) | (bytes[i + 1] << 8);
out +=
alphabet[(n >> 18) & 63] +
alphabet[(n >> 12) & 63] +
alphabet[(n >> 6) & 63] +
"=";
}
return out;
}