base64.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /**
  2. * Base64 implementation with UTF-8 support
  3. * Replaces the previous UMD module to ensure correct bundling
  4. */
  5. export const Base64 = {
  6. /**
  7. * Decode Base64 string to UTF-8 string
  8. */
  9. decode(str: string): string {
  10. try {
  11. // Use TextDecoder for proper UTF-8 handling
  12. const binaryString = window.atob(str);
  13. const bytes = new Uint8Array(binaryString.length);
  14. for (let i = 0; i < binaryString.length; i++) {
  15. bytes[i] = binaryString.charCodeAt(i);
  16. }
  17. return new TextDecoder().decode(bytes);
  18. } catch (e) {
  19. console.error('Base64 decode error:', e);
  20. return '';
  21. }
  22. },
  23. /**
  24. * Encode UTF-8 string to Base64
  25. */
  26. encode(str: string): string {
  27. try {
  28. const bytes = new TextEncoder().encode(str);
  29. const binaryString = Array.from(bytes, (byte) => String.fromCharCode(byte)).join("");
  30. return window.btoa(binaryString);
  31. } catch (e) {
  32. console.error('Base64 encode error:', e);
  33. return '';
  34. }
  35. }
  36. };
  37. export default Base64;