(base64: string)
| 59 | * Decode a base64 string into an `ArrayBuffer`. |
| 60 | */ |
| 61 | export function base64ToArrayBuffer(base64: string): ArrayBuffer { |
| 62 | // eslint-disable-next-line no-restricted-syntax -- feature-detecting Uint8Array.fromBase64 Stage-3 proposal not yet in lib.es types |
| 63 | const fast = (Uint8Array as unknown as Uint8ArrayWithBase64).fromBase64 |
| 64 | if (typeof fast === 'function') { |
| 65 | return fast(base64).buffer as ArrayBuffer |
| 66 | } |
| 67 | |
| 68 | if (typeof atob === 'function') { |
| 69 | const binary = atob(base64) |
| 70 | const bytes = new Uint8Array(binary.length) |
| 71 | for (let i = 0; i < binary.length; i++) { |
| 72 | bytes[i] = binary.charCodeAt(i) |
| 73 | } |
| 74 | return bytes.buffer |
| 75 | } |
| 76 | |
| 77 | if (typeof Buffer !== 'undefined' && typeof Buffer.from === 'function') { |
| 78 | const buf = Buffer.from(base64, 'base64') |
| 79 | return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength) |
| 80 | } |
| 81 | |
| 82 | throw new Error('No base64 decoder available in this environment.') |
| 83 | } |
no test coverage detected