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