decode-shared.js 1.2 KB

12345678910111213141516171819202122232425262728
  1. /*
  2. * Shared base64 decode helper for generated decode data.
  3. * Assumes global atob is available.
  4. */
  5. export function decodeBase64(input) {
  6. const binary =
  7. // eslint-disable-next-line n/no-unsupported-features/node-builtins
  8. typeof atob === "function"
  9. ? // Browser (and Node >=16)
  10. // eslint-disable-next-line n/no-unsupported-features/node-builtins
  11. atob(input)
  12. : // Older Node versions (<16)
  13. // eslint-disable-next-line n/no-unsupported-features/node-builtins
  14. typeof Buffer.from === "function"
  15. ? // eslint-disable-next-line n/no-unsupported-features/node-builtins
  16. Buffer.from(input, "base64").toString("binary")
  17. : // eslint-disable-next-line unicorn/no-new-buffer, n/no-deprecated-api
  18. new Buffer(input, "base64").toString("binary");
  19. const evenLength = binary.length & ~1; // Round down to even length
  20. const out = new Uint16Array(evenLength / 2);
  21. for (let index = 0, outIndex = 0; index < evenLength; index += 2) {
  22. const lo = binary.charCodeAt(index);
  23. const hi = binary.charCodeAt(index + 1);
  24. out[outIndex++] = lo | (hi << 8);
  25. }
  26. return out;
  27. }
  28. //# sourceMappingURL=decode-shared.js.map