v1.js 3.6 KB

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