base64.js 673 B

12345678910111213141516171819202122
  1. export function encodeBase64(input) {
  2. if (Uint8Array.prototype.toBase64) {
  3. return input.toBase64();
  4. }
  5. const CHUNK_SIZE = 0x8000;
  6. const arr = [];
  7. for (let i = 0; i < input.length; i += CHUNK_SIZE) {
  8. arr.push(String.fromCharCode.apply(null, input.subarray(i, i + CHUNK_SIZE)));
  9. }
  10. return btoa(arr.join(''));
  11. }
  12. export function decodeBase64(encoded) {
  13. if (Uint8Array.fromBase64) {
  14. return Uint8Array.fromBase64(encoded);
  15. }
  16. const binary = atob(encoded);
  17. const bytes = new Uint8Array(binary.length);
  18. for (let i = 0; i < binary.length; i++) {
  19. bytes[i] = binary.charCodeAt(i);
  20. }
  21. return bytes;
  22. }