-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathencoding.ts
More file actions
41 lines (37 loc) · 1.59 KB
/
Copy pathencoding.ts
File metadata and controls
41 lines (37 loc) · 1.59 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
// ============================================================================
// Base64 / Binary Encoding Utilities
// ============================================================================
export function concatBuffers(chunks: Uint8Array[]): Uint8Array {
if (chunks.length === 0) return new Uint8Array(0);
if (chunks.length === 1) return chunks[0];
const total = chunks.reduce((n, c) => n + c.length, 0);
const out = new Uint8Array(total);
let offset = 0;
for (const chunk of chunks) {
out.set(chunk, offset);
offset += chunk.length;
}
return out;
}
/** Convert a Uint8Array to an ArrayBuffer suitable for SQL BLOB binding. */
export function toBuffer(bytes: Uint8Array): ArrayBuffer {
if (bytes.byteOffset === 0 && bytes.byteLength === bytes.buffer.byteLength) {
return bytes.buffer as ArrayBuffer;
}
return bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength) as ArrayBuffer;
}
/** Decode a row's data column into a Uint8Array, handling both BLOB and legacy base64. */
export function rowDataToBytes(data: ArrayBuffer | string | null): Uint8Array {
if (data instanceof ArrayBuffer) return new Uint8Array(data);
if (typeof data === 'string' && data.length > 0) return base64ToUint8Array(data);
return new Uint8Array(0);
}
/** Decode a base64 string into a Uint8Array. */
export function base64ToUint8Array(base64: string): Uint8Array {
const binary = atob(base64);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) {
bytes[i] = binary.charCodeAt(i);
}
return bytes;
}