| 21 | } |
| 22 | |
| 23 | export function encodeToBase64(value: string): string { |
| 24 | if (textEncoder && hasWindowFunction("btoa")) { |
| 25 | const bytes = textEncoder.encode(value); |
| 26 | let binary = ""; |
| 27 | for (let i = 0; i < bytes.length; i++) { |
| 28 | binary += String.fromCharCode(bytes[i]); |
| 29 | } |
| 30 | return window.btoa(binary); |
| 31 | } |
| 32 | |
| 33 | const buffer = getGlobalBuffer(); |
| 34 | if (buffer) { |
| 35 | return buffer.from(value, "utf8").toString("base64"); |
| 36 | } |
| 37 | |
| 38 | throw new Error("Base64 encoding is not supported in this environment."); |
| 39 | } |
| 40 | |
| 41 | export function decodeFromBase64(value: string): string { |
| 42 | if (textDecoder && hasWindowFunction("atob")) { |