(buffer: unknown)
| 431 | } |
| 432 | |
| 433 | function toBase64(buffer: unknown): string { |
| 434 | //TODO: replace with when is Baseline widely available |
| 435 | // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/toBase64 |
| 436 | const bytes = new Uint8Array(buffer as ArrayBufferLike); |
| 437 | |
| 438 | const CHUNK_SIZE = 0x8000; // 32,768 bytes (~32 KB) per chunk, to avoid stack overflow |
| 439 | |
| 440 | let binaryString = ''; |
| 441 | |
| 442 | for (let i = 0; i < bytes.length; i += CHUNK_SIZE) { |
| 443 | const chunk = bytes.subarray(i, i + CHUNK_SIZE); |
| 444 | binaryString += String.fromCharCode.apply(null, chunk as unknown as number[]); |
| 445 | } |
| 446 | return btoa(binaryString); |
| 447 | } |
| 448 | |
| 449 | function fromBase64(base64: string): ArrayBuffer { |
| 450 | const binary = atob(base64); |
no test coverage detected