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 |
|
15 |
|
16 |
|
17 |
|
18 | let _nodeId;
|
19 |
|
20 | let _clockseq;
|
21 |
|
22 |
|
23 | let _lastMSecs = 0;
|
24 | let _lastNSecs = 0;
|
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;
|
32 |
|
33 |
|
34 |
|
35 | if (node == null || clockseq == null) {
|
36 | const seedBytes = options.random || (options.rng || _rng.default)();
|
37 |
|
38 | if (node == null) {
|
39 |
|
40 | node = _nodeId = [seedBytes[0] | 0x01, seedBytes[1], seedBytes[2], seedBytes[3], seedBytes[4], seedBytes[5]];
|
41 | }
|
42 |
|
43 | if (clockseq == null) {
|
44 |
|
45 | clockseq = _clockseq = (seedBytes[6] << 8 | seedBytes[7]) & 0x3fff;
|
46 | }
|
47 | }
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | let msecs = options.msecs !== undefined ? options.msecs : Date.now();
|
54 |
|
55 |
|
56 | let nsecs = options.nsecs !== undefined ? options.nsecs : _lastNSecs + 1;
|
57 |
|
58 | const dt = msecs - _lastMSecs + (nsecs - _lastNSecs) / 10000;
|
59 |
|
60 | if (dt < 0 && options.clockseq === undefined) {
|
61 | clockseq = clockseq + 1 & 0x3fff;
|
62 | }
|
63 |
|
64 |
|
65 |
|
66 | if ((dt < 0 || msecs > _lastMSecs) && options.nsecs === undefined) {
|
67 | nsecs = 0;
|
68 | }
|
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;
|
78 |
|
79 | msecs += 12219292800000;
|
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;
|
86 |
|
87 | const tmh = msecs / 0x100000000 * 10000 & 0xfffffff;
|
88 | b[i++] = tmh >>> 8 & 0xff;
|
89 | b[i++] = tmh & 0xff;
|
90 |
|
91 | b[i++] = tmh >>> 24 & 0xf | 0x10;
|
92 |
|
93 | b[i++] = tmh >>> 16 & 0xff;
|
94 |
|
95 | b[i++] = clockseq >>> 8 | 0x80;
|
96 |
|
97 | b[i++] = clockseq & 0xff;
|
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; |
\ | No newline at end of file |