(buffer: unknown)
| 405 | } |
| 406 | |
| 407 | function toBase64(buffer: unknown): string { |
| 408 | //TODO: replace with when is Baseline widely available |
| 409 | // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/toBase64 |
| 410 | const bytes = new Uint8Array(buffer as ArrayBufferLike); |
| 411 | |
| 412 | const CHUNK_SIZE = 0x8000; // 32,768 bytes (~32 KB) per chunk, to avoid stack overflow |
| 413 | |
| 414 | let binaryString = ''; |
| 415 | |
| 416 | for (let i = 0; i < bytes.length; i += CHUNK_SIZE) { |
| 417 | const chunk = bytes.subarray(i, i + CHUNK_SIZE); |
| 418 | binaryString += String.fromCharCode.apply(null, chunk as unknown as number[]); |
| 419 | } |
| 420 | return btoa(binaryString); |
| 421 | } |
| 422 | |
| 423 | function fromBase64(base64: string): ArrayBuffer { |
| 424 | const binary = atob(base64); |
no test coverage detected