v1.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _rng = _interopRequireDefault(require("./rng.js"));
  7. var _stringify = require("./stringify.js");
  8. function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
  9. // **`v1()` - Generate time-based UUID**
  10. //
  11. // Inspired by https://github.com/LiosK/UUID.js
  12. // and http://docs.python.org/library/uuid.html
  13. let _nodeId;
  14. let _clockseq;
  15. // Previous uuid creation time
  16. let _lastMSecs = 0;
  17. let _lastNSecs = 0;
  18. // See https://github.com/uuidjs/uuid for API details
  19. function v1(options, buf, offset) {
  20. let i = buf && offset || 0;
  21. const b = buf || new Array(16);
  22. options = options || {};
  23. let node = options.node;
  24. let clockseq = options.clockseq;
  25. // v1 only: Use cached `node` and `clockseq` values
  26. if (!options._v6) {
  27. if (!node) {
  28. node = _nodeId;
  29. }
  30. if (clockseq == null) {
  31. clockseq = _clockseq;
  32. }
  33. }
  34. // Handle cases where we need entropy. We do this lazily to minimize issues
  35. // related to insufficient system entropy. See #189
  36. if (node == null || clockseq == null) {
  37. const seedBytes = options.random || (options.rng || _rng.default)();
  38. // Randomize node
  39. if (node == null) {
  40. node = [seedBytes[0], seedBytes[1], seedBytes[2], seedBytes[3], seedBytes[4], seedBytes[5]];
  41. // v1 only: cache node value for reuse
  42. if (!_nodeId && !options._v6) {
  43. // per RFC4122 4.5: Set MAC multicast bit (v1 only)
  44. node[0] |= 0x01; // Set multicast bit
  45. _nodeId = node;
  46. }
  47. }
  48. // Randomize clockseq
  49. if (clockseq == null) {
  50. // Per 4.2.2, randomize (14 bit) clockseq
  51. clockseq = (seedBytes[6] << 8 | seedBytes[7]) & 0x3fff;
  52. if (_clockseq === undefined && !options._v6) {
  53. _clockseq = clockseq;
  54. }
  55. }
  56. }
  57. // v1 & v6 timestamps are 100 nano-second units since the Gregorian epoch,
  58. // (1582-10-15 00:00). JSNumbers aren't precise enough for this, so time is
  59. // handled internally as 'msecs' (integer milliseconds) and 'nsecs'
  60. // (100-nanoseconds offset from msecs) since unix epoch, 1970-01-01 00:00.
  61. let msecs = options.msecs !== undefined ? options.msecs : Date.now();
  62. // Per 4.2.1.2, use count of uuid's generated during the current clock
  63. // cycle to simulate higher resolution clock
  64. let nsecs = options.nsecs !== undefined ? options.nsecs : _lastNSecs + 1;
  65. // Time since last uuid creation (in msecs)
  66. const dt = msecs - _lastMSecs + (nsecs - _lastNSecs) / 10000;
  67. // Per 4.2.1.2, Bump clockseq on clock regression
  68. if (dt < 0 && options.clockseq === undefined) {
  69. clockseq = clockseq + 1 & 0x3fff;
  70. }
  71. // Reset nsecs if clock regresses (new clockseq) or we've moved onto a new
  72. // time interval
  73. if ((dt < 0 || msecs > _lastMSecs) && options.nsecs === undefined) {
  74. nsecs = 0;
  75. }
  76. // Per 4.2.1.2 Throw error if too many uuids are requested
  77. if (nsecs >= 10000) {
  78. throw new Error("uuid.v1(): Can't create more than 10M uuids/sec");
  79. }
  80. _lastMSecs = msecs;
  81. _lastNSecs = nsecs;
  82. _clockseq = clockseq;
  83. // Per 4.1.4 - Convert from unix epoch to Gregorian epoch
  84. msecs += 12219292800000;
  85. // `time_low`
  86. const tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000;
  87. b[i++] = tl >>> 24 & 0xff;
  88. b[i++] = tl >>> 16 & 0xff;
  89. b[i++] = tl >>> 8 & 0xff;
  90. b[i++] = tl & 0xff;
  91. // `time_mid`
  92. const tmh = msecs / 0x100000000 * 10000 & 0xfffffff;
  93. b[i++] = tmh >>> 8 & 0xff;
  94. b[i++] = tmh & 0xff;
  95. // `time_high_and_version`
  96. b[i++] = tmh >>> 24 & 0xf | 0x10; // include version
  97. b[i++] = tmh >>> 16 & 0xff;
  98. // `clock_seq_hi_and_reserved` (Per 4.2.2 - include variant)
  99. b[i++] = clockseq >>> 8 | 0x80;
  100. // `clock_seq_low`
  101. b[i++] = clockseq & 0xff;
  102. // `node`
  103. for (let n = 0; n < 6; ++n) {
  104. b[i + n] = node[n];
  105. }
  106. return buf || (0, _stringify.unsafeStringify)(b);
  107. }
  108. var _default = exports.default = v1;