v1.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.updateV1State = void 0;
  4. const rng_js_1 = require("./rng.js");
  5. const stringify_js_1 = require("./stringify.js");
  6. const _state = {};
  7. function v1(options, buf, offset) {
  8. let bytes;
  9. const isV6 = options?._v6 ?? false;
  10. if (options) {
  11. const optionsKeys = Object.keys(options);
  12. if (optionsKeys.length === 1 && optionsKeys[0] === '_v6') {
  13. options = undefined;
  14. }
  15. }
  16. if (options) {
  17. bytes = v1Bytes(options.random ?? options.rng?.() ?? (0, rng_js_1.default)(), options.msecs, options.nsecs, options.clockseq, options.node, buf, offset);
  18. }
  19. else {
  20. const now = Date.now();
  21. const rnds = (0, rng_js_1.default)();
  22. updateV1State(_state, now, rnds);
  23. bytes = v1Bytes(rnds, _state.msecs, _state.nsecs, isV6 ? undefined : _state.clockseq, isV6 ? undefined : _state.node, buf, offset);
  24. }
  25. return buf ?? (0, stringify_js_1.unsafeStringify)(bytes);
  26. }
  27. function updateV1State(state, now, rnds) {
  28. state.msecs ??= -Infinity;
  29. state.nsecs ??= 0;
  30. if (now === state.msecs) {
  31. state.nsecs++;
  32. if (state.nsecs >= 10000) {
  33. state.node = undefined;
  34. state.nsecs = 0;
  35. }
  36. }
  37. else if (now > state.msecs) {
  38. state.nsecs = 0;
  39. }
  40. else if (now < state.msecs) {
  41. state.node = undefined;
  42. }
  43. if (!state.node) {
  44. state.node = rnds.slice(10, 16);
  45. state.node[0] |= 0x01;
  46. state.clockseq = ((rnds[8] << 8) | rnds[9]) & 0x3fff;
  47. }
  48. state.msecs = now;
  49. return state;
  50. }
  51. exports.updateV1State = updateV1State;
  52. function v1Bytes(rnds, msecs, nsecs, clockseq, node, buf, offset = 0) {
  53. if (rnds.length < 16) {
  54. throw new Error('Random bytes length must be >= 16');
  55. }
  56. if (!buf) {
  57. buf = new Uint8Array(16);
  58. offset = 0;
  59. }
  60. else {
  61. if (offset < 0 || offset + 16 > buf.length) {
  62. throw new RangeError(`UUID byte range ${offset}:${offset + 15} is out of buffer bounds`);
  63. }
  64. }
  65. msecs ??= Date.now();
  66. nsecs ??= 0;
  67. clockseq ??= ((rnds[8] << 8) | rnds[9]) & 0x3fff;
  68. node ??= rnds.slice(10, 16);
  69. msecs += 12219292800000;
  70. const tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000;
  71. buf[offset++] = (tl >>> 24) & 0xff;
  72. buf[offset++] = (tl >>> 16) & 0xff;
  73. buf[offset++] = (tl >>> 8) & 0xff;
  74. buf[offset++] = tl & 0xff;
  75. const tmh = ((msecs / 0x100000000) * 10000) & 0xfffffff;
  76. buf[offset++] = (tmh >>> 8) & 0xff;
  77. buf[offset++] = tmh & 0xff;
  78. buf[offset++] = ((tmh >>> 24) & 0xf) | 0x10;
  79. buf[offset++] = (tmh >>> 16) & 0xff;
  80. buf[offset++] = (clockseq >>> 8) | 0x80;
  81. buf[offset++] = clockseq & 0xff;
  82. for (let n = 0; n < 6; ++n) {
  83. buf[offset++] = node[n];
  84. }
  85. return buf;
  86. }
  87. exports.default = v1;