Skip to content

Commit

Permalink
Defer random initialization of node and clockseq. Fixes #189 (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
broofa committed Sep 9, 2017
1 parent dc02a76 commit c1f720d
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions v1.js
Expand Up @@ -6,17 +6,7 @@ var bytesToUuid = require('./lib/bytesToUuid');
// Inspired by https://github.com/LiosK/UUID.js
// and http://docs.python.org/library/uuid.html

// random #'s we need to init node and clockseq
var _seedBytes = rng();

// Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1)
var _nodeId = [
_seedBytes[0] | 0x01,
_seedBytes[1], _seedBytes[2], _seedBytes[3], _seedBytes[4], _seedBytes[5]
];

// Per 4.2.2, randomize (14 bit) clockseq
var _clockseq = (_seedBytes[6] << 8 | _seedBytes[7]) & 0x3fff;
var _nodeId, _clockseq;

// Previous uuid creation time
var _lastMSecs = 0, _lastNSecs = 0;
Expand All @@ -27,9 +17,27 @@ function v1(options, buf, offset) {
var b = buf || [];

options = options || {};

var node = options.node || _nodeId;
var clockseq = options.clockseq !== undefined ? options.clockseq : _clockseq;

// node and clockseq need to be initialized to random values if they're not
// specified. We do this lazily to minimize issues related to insufficient
// system entropy. See #189
if (node == null || clockseq == null) {
var seedBytes = rng();
if (node == null) {
// Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1)
node = _nodeId = [
seedBytes[0] | 0x01,
seedBytes[1], seedBytes[2], seedBytes[3], seedBytes[4], seedBytes[5]
];
}
if (clockseq == null) {
// Per 4.2.2, randomize (14 bit) clockseq
clockseq = _clockseq = (seedBytes[6] << 8 | seedBytes[7]) & 0x3fff;
}
}

// UUID timestamps are 100 nano-second units since the Gregorian epoch,
// (1582-10-15 00:00). JSNumbers aren't precise enough for this, so
// time is handled internally as 'msecs' (integer milliseconds) and 'nsecs'
Expand Down Expand Up @@ -89,7 +97,6 @@ function v1(options, buf, offset) {
b[i++] = clockseq & 0xff;

// `node`
var node = options.node || _nodeId;
for (var n = 0; n < 6; ++n) {
b[i + n] = node[n];
}
Expand Down

0 comments on commit c1f720d

Please sign in to comment.