v1.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  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; // Previous uuid creation time
  15. let _lastMSecs = 0;
  16. let _lastNSecs = 0; // See https://github.com/uuidjs/uuid for API details
  17. function v1(options, buf, offset) {
  18. let i = buf && offset || 0;
  19. const b = buf || new Array(16);
  20. options = options || {};
  21. let node = options.node || _nodeId;
  22. let clockseq = options.clockseq !== undefined ? options.clockseq : _clockseq; // node and clockseq need to be initialized to random values if they're not
  23. // specified. We do this lazily to minimize issues related to insufficient
  24. // system entropy. See #189
  25. if (node == null || clockseq == null) {
  26. const seedBytes = options.random || (options.rng || _rng.default)();
  27. if (node == null) {
  28. // Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1)
  29. node = _nodeId = [seedBytes[0] | 0x01, seedBytes[1], seedBytes[2], seedBytes[3], seedBytes[4], seedBytes[5]];
  30. }
  31. if (clockseq == null) {
  32. // Per 4.2.2, randomize (14 bit) clockseq
  33. clockseq = _clockseq = (seedBytes[6] << 8 | seedBytes[7]) & 0x3fff;
  34. }
  35. } // UUID timestamps are 100 nano-second units since the Gregorian epoch,
  36. // (1582-10-15 00:00). JSNumbers aren't precise enough for this, so
  37. // time is handled internally as 'msecs' (integer milliseconds) and 'nsecs'
  38. // (100-nanoseconds offset from msecs) since unix epoch, 1970-01-01 00:00.
  39. let msecs = options.msecs !== undefined ? options.msecs : Date.now(); // Per 4.2.1.2, use count of uuid's generated during the current clock
  40. // cycle to simulate higher resolution clock
  41. let nsecs = options.nsecs !== undefined ? options.nsecs : _lastNSecs + 1; // Time since last uuid creation (in msecs)
  42. const dt = msecs - _lastMSecs + (nsecs - _lastNSecs) / 10000; // Per 4.2.1.2, Bump clockseq on clock regression
  43. if (dt < 0 && options.clockseq === undefined) {
  44. clockseq = clockseq + 1 & 0x3fff;
  45. } // Reset nsecs if clock regresses (new clockseq) or we've moved onto a new
  46. // time interval
  47. if ((dt < 0 || msecs > _lastMSecs) && options.nsecs === undefined) {
  48. nsecs = 0;
  49. } // Per 4.2.1.2 Throw error if too many uuids are requested
  50. if (nsecs >= 10000) {
  51. throw new Error("uuid.v1(): Can't create more than 10M uuids/sec");
  52. }
  53. _lastMSecs = msecs;
  54. _lastNSecs = nsecs;
  55. _clockseq = clockseq; // Per 4.1.4 - Convert from unix epoch to Gregorian epoch
  56. msecs += 12219292800000; // `time_low`
  57. const tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000;
  58. b[i++] = tl >>> 24 & 0xff;
  59. b[i++] = tl >>> 16 & 0xff;
  60. b[i++] = tl >>> 8 & 0xff;
  61. b[i++] = tl & 0xff; // `time_mid`
  62. const tmh = msecs / 0x100000000 * 10000 & 0xfffffff;
  63. b[i++] = tmh >>> 8 & 0xff;
  64. b[i++] = tmh & 0xff; // `time_high_and_version`
  65. b[i++] = tmh >>> 24 & 0xf | 0x10; // include version
  66. b[i++] = tmh >>> 16 & 0xff; // `clock_seq_hi_and_reserved` (Per 4.2.2 - include variant)
  67. b[i++] = clockseq >>> 8 | 0x80; // `clock_seq_low`
  68. b[i++] = clockseq & 0xff; // `node`
  69. for (let n = 0; n < 6; ++n) {
  70. b[i + n] = node[n];
  71. }
  72. return buf || (0, _stringify.unsafeStringify)(b);
  73. }
  74. var _default = v1;
  75. exports.default = _default;