base64.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.encodeBase64 = exports.decodeBase64 = void 0;
  4. /**
  5. * [Base64 encoding](link-wiki-base64) using 6-bit words to encode
  6. * arbitrary bytes into a string using 65 printable symbols, the
  7. * upper-case and lower-case alphabet, the digits ``0`` through ``9``,
  8. * ``"+"`` and ``"/"`` with the ``"="`` used for padding.
  9. *
  10. * @_subsection: api/utils:Base64 Encoding [about-base64]
  11. */
  12. const data_js_1 = require("./data.js");
  13. /**
  14. * Decodes the base-64 encoded %%value%%.
  15. *
  16. * @example:
  17. * // The decoded value is always binary data...
  18. * result = decodeBase64("SGVsbG8gV29ybGQhIQ==")
  19. * //_result:
  20. *
  21. * // ...use toUtf8String to convert it to a string.
  22. * toUtf8String(result)
  23. * //_result:
  24. *
  25. * // Decoding binary data
  26. * decodeBase64("EjQ=")
  27. * //_result:
  28. */
  29. function decodeBase64(value) {
  30. return (0, data_js_1.getBytesCopy)(Buffer.from(value, "base64"));
  31. }
  32. exports.decodeBase64 = decodeBase64;
  33. ;
  34. /**
  35. * Encodes %%data%% as a base-64 encoded string.
  36. *
  37. * @example:
  38. * // Encoding binary data as a hexstring
  39. * encodeBase64("0x1234")
  40. * //_result:
  41. *
  42. * // Encoding binary data as a Uint8Array
  43. * encodeBase64(new Uint8Array([ 0x12, 0x34 ]))
  44. * //_result:
  45. *
  46. * // The input MUST be data...
  47. * encodeBase64("Hello World!!")
  48. * //_error:
  49. *
  50. * // ...use toUtf8Bytes for this.
  51. * encodeBase64(toUtf8Bytes("Hello World!!"))
  52. * //_result:
  53. */
  54. function encodeBase64(data) {
  55. return Buffer.from((0, data_js_1.getBytes)(data)).toString("base64");
  56. }
  57. exports.encodeBase64 = encodeBase64;
  58. //# sourceMappingURL=base64.js.map