(array, { urlSafe = false } = {})
| 152 | const MAX_BLOCK_SIZE = 65_535; |
| 153 | |
| 154 | export function uint8ArrayToBase64(array, { urlSafe = false } = {}) { |
| 155 | assertUint8Array(array); |
| 156 | |
| 157 | let base64; |
| 158 | |
| 159 | if (array.length < MAX_BLOCK_SIZE) { |
| 160 | // Required as `btoa` and `atob` don't properly support Unicode: https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem |
| 161 | base64 = globalThis.btoa(String.fromCodePoint.apply(this, array)); |
| 162 | } else { |
| 163 | base64 = ''; |
| 164 | for (const value of array) { |
| 165 | base64 += String.fromCodePoint(value); |
| 166 | } |
| 167 | |
| 168 | base64 = globalThis.btoa(base64); |
| 169 | } |
| 170 | |
| 171 | return urlSafe ? base64ToBase64Url(base64) : base64; |
| 172 | } |
| 173 | |
| 174 | export function base64ToUint8Array(base64String) { |
| 175 | assertString(base64String); |
no test coverage detected