(buffer: ArrayBuffer)
| 184 | * call can hit `Maximum call stack size exceeded`. |
| 185 | */ |
| 186 | export function arrayBufferToBase64(buffer: ArrayBuffer): string { |
| 187 | if (typeof Buffer !== 'undefined') { |
| 188 | return Buffer.from(buffer).toString('base64') |
| 189 | } |
| 190 | const bytes = new Uint8Array(buffer) |
| 191 | const chunkSize = 0x8000 |
| 192 | let binary = '' |
| 193 | for (let i = 0; i < bytes.length; i += chunkSize) { |
| 194 | const end = Math.min(i + chunkSize, bytes.length) |
| 195 | binary += String.fromCharCode.apply( |
| 196 | null, |
| 197 | // eslint-disable-next-line no-restricted-syntax -- TS lib types String.fromCharCode.apply as Array<number> but runtime accepts any ArrayLike |
| 198 | bytes.subarray(i, end) as unknown as Array<number>, |
| 199 | ) |
| 200 | } |
| 201 | return btoa(binary) |
| 202 | } |
| 203 | |
| 204 | function base64ToArrayBuffer(base64: string): ArrayBuffer { |
| 205 | let binary: string |
no test coverage detected