base64.js 1.4 KB

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