source: trip-planner-front/node_modules/uuid/dist/esm-node/v1.js@ fa375fe

Last change on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.2 KB
Line 
1import rng from './rng.js';
2import 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
7let _nodeId;
8
9let _clockseq; // Previous uuid creation time
10
11
12let _lastMSecs = 0;
13let _lastNSecs = 0; // See https://github.com/uuidjs/uuid for API details
14
15function v1(options, buf, offset) {
16 let i = buf && offset || 0;
17 const b = buf || new Array(16);
18 options = options || {};
19 let node = options.node || _nodeId;
20 let clockseq = options.clockseq !== undefined ? options.clockseq : _clockseq; // node and clockseq need to be initialized to random values if they're not
21 // specified. We do this lazily to minimize issues related to insufficient
22 // system entropy. See #189
23
24 if (node == null || clockseq == null) {
25 const seedBytes = options.random || (options.rng || rng)();
26
27 if (node == null) {
28 // Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1)
29 node = _nodeId = [seedBytes[0] | 0x01, seedBytes[1], seedBytes[2], seedBytes[3], seedBytes[4], seedBytes[5]];
30 }
31
32 if (clockseq == null) {
33 // Per 4.2.2, randomize (14 bit) clockseq
34 clockseq = _clockseq = (seedBytes[6] << 8 | seedBytes[7]) & 0x3fff;
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
41
42 let msecs = options.msecs !== undefined ? options.msecs : Date.now(); // Per 4.2.1.2, use count of uuid's generated during the current clock
43 // cycle to simulate higher resolution clock
44
45 let nsecs = options.nsecs !== undefined ? options.nsecs : _lastNSecs + 1; // Time since last uuid creation (in msecs)
46
47 const dt = msecs - _lastMSecs + (nsecs - _lastNSecs) / 10000; // Per 4.2.1.2, Bump clockseq on clock regression
48
49 if (dt < 0 && options.clockseq === undefined) {
50 clockseq = clockseq + 1 & 0x3fff;
51 } // Reset nsecs if clock regresses (new clockseq) or we've moved onto a new
52 // time interval
53
54
55 if ((dt < 0 || msecs > _lastMSecs) && options.nsecs === undefined) {
56 nsecs = 0;
57 } // Per 4.2.1.2 Throw error if too many uuids are requested
58
59
60 if (nsecs >= 10000) {
61 throw new Error("uuid.v1(): Can't create more than 10M uuids/sec");
62 }
63
64 _lastMSecs = msecs;
65 _lastNSecs = nsecs;
66 _clockseq = clockseq; // Per 4.1.4 - Convert from unix epoch to Gregorian epoch
67
68 msecs += 12219292800000; // `time_low`
69
70 const tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000;
71 b[i++] = tl >>> 24 & 0xff;
72 b[i++] = tl >>> 16 & 0xff;
73 b[i++] = tl >>> 8 & 0xff;
74 b[i++] = tl & 0xff; // `time_mid`
75
76 const tmh = msecs / 0x100000000 * 10000 & 0xfffffff;
77 b[i++] = tmh >>> 8 & 0xff;
78 b[i++] = tmh & 0xff; // `time_high_and_version`
79
80 b[i++] = tmh >>> 24 & 0xf | 0x10; // include version
81
82 b[i++] = tmh >>> 16 & 0xff; // `clock_seq_hi_and_reserved` (Per 4.2.2 - include variant)
83
84 b[i++] = clockseq >>> 8 | 0x80; // `clock_seq_low`
85
86 b[i++] = clockseq & 0xff; // `node`
87
88 for (let n = 0; n < 6; ++n) {
89 b[i + n] = node[n];
90 }
91
92 return buf || stringify(b);
93}
94
95export default v1;
Note: See TracBrowser for help on using the repository browser.