uuid.js 894 B

123456789101112131415161718192021222324252627282930
  1. /**
  2. * Explain UUID and link to RFC here.
  3. *
  4. * @_subsection: api/utils:UUID [about-uuid]
  5. */
  6. import { getBytes, hexlify } from "./data.js";
  7. /**
  8. * Returns the version 4 [[link-uuid]] for the %%randomBytes%%.
  9. *
  10. * @see: https://www.ietf.org/rfc/rfc4122.txt (Section 4.4)
  11. */
  12. export function uuidV4(randomBytes) {
  13. const bytes = getBytes(randomBytes, "randomBytes");
  14. // Section: 4.1.3:
  15. // - time_hi_and_version[12:16] = 0b0100
  16. bytes[6] = (bytes[6] & 0x0f) | 0x40;
  17. // Section 4.4
  18. // - clock_seq_hi_and_reserved[6] = 0b0
  19. // - clock_seq_hi_and_reserved[7] = 0b1
  20. bytes[8] = (bytes[8] & 0x3f) | 0x80;
  21. const value = hexlify(bytes);
  22. return [
  23. value.substring(2, 10),
  24. value.substring(10, 14),
  25. value.substring(14, 18),
  26. value.substring(18, 22),
  27. value.substring(22, 34),
  28. ].join("-");
  29. }
  30. //# sourceMappingURL=uuid.js.map