encode.js 864 B

1234567891011121314151617181920212223242526272829
  1. // src/utils/encode.ts
  2. var decodeBase64Url = (str) => {
  3. return decodeBase64(str.replace(/_|-/g, (m) => ({ _: "/", "-": "+" })[m] ?? m));
  4. };
  5. var encodeBase64Url = (buf) => encodeBase64(buf).replace(/\/|\+/g, (m) => ({ "/": "_", "+": "-" })[m] ?? m);
  6. var encodeBase64 = (buf) => {
  7. let binary = "";
  8. const bytes = new Uint8Array(buf);
  9. for (let i = 0, len = bytes.length; i < len; i++) {
  10. binary += String.fromCharCode(bytes[i]);
  11. }
  12. return btoa(binary);
  13. };
  14. var decodeBase64 = (str) => {
  15. const binary = atob(str);
  16. const bytes = new Uint8Array(new ArrayBuffer(binary.length));
  17. const half = binary.length / 2;
  18. for (let i = 0, j = binary.length - 1; i <= half; i++, j--) {
  19. bytes[i] = binary.charCodeAt(i);
  20. bytes[j] = binary.charCodeAt(j);
  21. }
  22. return bytes;
  23. };
  24. export {
  25. decodeBase64,
  26. decodeBase64Url,
  27. encodeBase64,
  28. encodeBase64Url
  29. };