v1.js 3.2 KB

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