v1.js 3.3 KB

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