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