btoa.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. "use strict";
  2. /**
  3. * btoa() as defined by the HTML and Infra specs, which mostly just references
  4. * RFC 4648.
  5. */
  6. function btoa(s) {
  7. let i;
  8. // String conversion as required by Web IDL.
  9. s = `${s}`;
  10. // "The btoa() method must throw an "InvalidCharacterError" DOMException if
  11. // data contains any character whose code point is greater than U+00FF."
  12. for (i = 0; i < s.length; i++) {
  13. if (s.charCodeAt(i) > 255) {
  14. return null;
  15. }
  16. }
  17. let out = "";
  18. for (i = 0; i < s.length; i += 3) {
  19. const groupsOfSix = [undefined, undefined, undefined, undefined];
  20. groupsOfSix[0] = s.charCodeAt(i) >> 2;
  21. groupsOfSix[1] = (s.charCodeAt(i) & 0x03) << 4;
  22. if (s.length > i + 1) {
  23. groupsOfSix[1] |= s.charCodeAt(i + 1) >> 4;
  24. groupsOfSix[2] = (s.charCodeAt(i + 1) & 0x0f) << 2;
  25. }
  26. if (s.length > i + 2) {
  27. groupsOfSix[2] |= s.charCodeAt(i + 2) >> 6;
  28. groupsOfSix[3] = s.charCodeAt(i + 2) & 0x3f;
  29. }
  30. for (let j = 0; j < groupsOfSix.length; j++) {
  31. if (typeof groupsOfSix[j] === "undefined") {
  32. out += "=";
  33. } else {
  34. out += btoaLookup(groupsOfSix[j]);
  35. }
  36. }
  37. }
  38. return out;
  39. }
  40. /**
  41. * Lookup table for btoa(), which converts a six-bit number into the
  42. * corresponding ASCII character.
  43. */
  44. const keystr =
  45. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  46. function btoaLookup(index) {
  47. if (index >= 0 && index < 64) {
  48. return keystr[index];
  49. }
  50. // Throw INVALID_CHARACTER_ERR exception here -- won't be hit in the tests.
  51. return undefined;
  52. }
  53. module.exports = btoa;