encode.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var encode_exports = {};
  19. __export(encode_exports, {
  20. decodeBase64: () => decodeBase64,
  21. decodeBase64Url: () => decodeBase64Url,
  22. encodeBase64: () => encodeBase64,
  23. encodeBase64Url: () => encodeBase64Url
  24. });
  25. module.exports = __toCommonJS(encode_exports);
  26. const decodeBase64Url = (str) => {
  27. return decodeBase64(str.replace(/_|-/g, (m) => ({ _: "/", "-": "+" })[m] ?? m));
  28. };
  29. const encodeBase64Url = (buf) => encodeBase64(buf).replace(/\/|\+/g, (m) => ({ "/": "_", "+": "-" })[m] ?? m);
  30. const encodeBase64 = (buf) => {
  31. let binary = "";
  32. const bytes = new Uint8Array(buf);
  33. for (let i = 0, len = bytes.length; i < len; i++) {
  34. binary += String.fromCharCode(bytes[i]);
  35. }
  36. return btoa(binary);
  37. };
  38. const decodeBase64 = (str) => {
  39. const binary = atob(str);
  40. const bytes = new Uint8Array(new ArrayBuffer(binary.length));
  41. const half = binary.length / 2;
  42. for (let i = 0, j = binary.length - 1; i <= half; i++, j--) {
  43. bytes[i] = binary.charCodeAt(i);
  44. bytes[j] = binary.charCodeAt(j);
  45. }
  46. return bytes;
  47. };
  48. // Annotate the CommonJS export names for ESM import in node:
  49. 0 && (module.exports = {
  50. decodeBase64,
  51. decodeBase64Url,
  52. encodeBase64,
  53. encodeBase64Url
  54. });