(uint8arr: Uint8Array<ArrayBufferLike>)
| 32 | } |
| 33 | |
| 34 | export function uint8ToBase64(uint8arr: Uint8Array<ArrayBufferLike>): string { |
| 35 | if (typeof (uint8arr as any).toBase64 === "function") { |
| 36 | // JS 2025 |
| 37 | return (uint8arr as any).toBase64() as string; |
| 38 | } else if (typeof Buffer !== "undefined" && typeof Buffer.from === "function") { |
| 39 | // Node.js |
| 40 | return Buffer.from(uint8arr).toString("base64") as string; |
| 41 | } else { |
| 42 | // Fallback |
| 43 | let binary = ""; |
| 44 | let i = 0; |
| 45 | while (uint8arr.length - i > 65535) { |
| 46 | binary += String.fromCharCode(...uint8arr.slice(i, i + 65535)); |
| 47 | i += 65535; |
| 48 | } |
| 49 | binary += String.fromCharCode(...(i ? uint8arr.slice(i) : uint8arr)); |
| 50 | return btoa(binary) as string; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // Split Uint8Array (or ArrayBuffer) into 2MB chunks as Uint8Array views |
| 55 | export function chunkUint8(src: Uint8Array | ArrayBuffer, chunkSize = 2 * 1024 * 1024): Uint8Array<ArrayBufferLike>[] { |
no test coverage detected