annotate src/implab/Uuid.js @ 0:fc2517695ee1

Initial commit, draft import of existing work
author cin
date Thu, 01 Jun 2017 13:20:03 +0300
parents
children 93fb6c09f2e1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
1 // uuid.js
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
2 //
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
3 // Copyright (c) 2010-2012 Robert Kieffer
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
4 // MIT License - http://opensource.org/licenses/mit-license.php
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
5 define(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
6 [],
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
7 function () {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
8 'use strict';
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
9
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
10 var _window = 'undefined' !== typeof window ? window : null;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
11
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
12 // Unique ID creation requires a high quality random # generator. We
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
13 // feature
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
14 // detect to determine the best RNG source, normalizing to a function
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
15 // that
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
16 // returns 128-bits of randomness, since that's what's usually required
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
17 var _rng, _mathRNG, _nodeRNG, _whatwgRNG, _previousRoot;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
18
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
19 function setupBrowser() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
20 // Allow for MSIE11 msCrypto
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
21 var _crypto = _window.crypto || _window.msCrypto;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
22
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
23 if (!_rng && _crypto && _crypto.getRandomValues) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
24 // WHATWG crypto-based RNG - http://wiki.whatwg.org/wiki/Crypto
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
25 //
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
26 // Moderately fast, high quality
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
27 try {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
28 var _rnds8 = new Uint8Array(16);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
29 _whatwgRNG = _rng = function whatwgRNG() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
30 _crypto.getRandomValues(_rnds8);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
31 return _rnds8;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
32 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
33 _rng();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
34 } catch (e) {}
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
35 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
36
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
37 if (!_rng) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
38 // Math.random()-based (RNG)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
39 //
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
40 // If all else fails, use Math.random(). It's fast, but is of
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
41 // unspecified
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
42 // quality.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
43 var _rnds = new Array(16);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
44 _mathRNG = _rng = function () {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
45 for (var i = 0, r; i < 16; i++) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
46 if ((i & 0x03) === 0) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
47 r = Math.random() * 0x100000000;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
48 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
49 _rnds[i] = r >>> ((i & 0x03) << 3) & 0xff;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
50 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
51
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
52 return _rnds;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
53 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
54 if ('undefined' !== typeof console && console.warn) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
55 console
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
56 .warn("[SECURITY] node-uuid: crypto not usable, falling back to insecure Math.random()");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
57 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
58 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
59 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
60
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
61 function setupNode() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
62 // Node.js crypto-based RNG -
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
63 // http://nodejs.org/docs/v0.6.2/api/crypto.html
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
64 //
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
65 // Moderately fast, high quality
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
66 if ('function' === typeof require) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
67 try {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
68 var _rb = require('crypto').randomBytes;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
69 _nodeRNG = _rng = _rb && function () {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
70 return _rb(16);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
71 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
72 _rng();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
73 } catch (e) {}
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
74 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
75 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
76
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
77 if (_window) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
78 setupBrowser();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
79 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
80 setupNode();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
81 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
82
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
83 // Buffer class to use
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
84 var BufferClass = ('function' === typeof Buffer) ? Buffer : Array;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
85
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
86 // Maps for number <-> hex string conversion
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
87 var _byteToHex = [];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
88 var _hexToByte = {};
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
89 for (var i = 0; i < 256; i++) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
90 _byteToHex[i] = (i + 0x100).toString(16).substr(1);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
91 _hexToByte[_byteToHex[i]] = i;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
92 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
93
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
94 // **`parse()` - Parse a UUID into it's component bytes**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
95 function parse(s, buf, offset) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
96 var i = (buf && offset) || 0,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
97 ii = 0;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
98
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
99 buf = buf || [];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
100 s.toLowerCase().replace(/[0-9a-f]{2}/g, function (oct) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
101 if (ii < 16) { // Don't overflow!
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
102 buf[i + ii++] = _hexToByte[oct];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
103 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
104 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
105
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
106 // Zero out remaining bytes if string was short
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
107 while (ii < 16) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
108 buf[i + ii++] = 0;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
109 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
110
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
111 return buf;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
112 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
113
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
114 // **`unparse()` - Convert UUID byte array (ala parse()) into a string**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
115 function unparse(buf, offset) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
116 var i = offset || 0,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
117 bth = _byteToHex;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
118 return bth[buf[i++]] + bth[buf[i++]] + bth[buf[i++]] +
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
119 bth[buf[i++]] + '-' + bth[buf[i++]] + bth[buf[i++]] + '-' +
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
120 bth[buf[i++]] + bth[buf[i++]] + '-' + bth[buf[i++]] +
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
121 bth[buf[i++]] + '-' + bth[buf[i++]] + bth[buf[i++]] +
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
122 bth[buf[i++]] + bth[buf[i++]] + bth[buf[i++]] + bth[buf[i++]];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
123 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
124
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
125 // **`v1()` - Generate time-based UUID**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
126 //
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
127 // Inspired by https://github.com/LiosK/UUID.js
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
128 // and http://docs.python.org/library/uuid.html
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
129
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
130 // random #'s we need to init node and clockseq
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
131 var _seedBytes = _rng();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
132
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
133 // Per 4.5, create and 48-bit node id, (47 random bits + multicast bit =
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
134 // 1)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
135 var _nodeId = [
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
136 _seedBytes[0] | 0x01,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
137 _seedBytes[1],
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
138 _seedBytes[2],
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
139 _seedBytes[3],
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
140 _seedBytes[4],
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
141 _seedBytes[5]
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
142 ];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
143
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
144 // Per 4.2.2, randomize (14 bit) clockseq
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
145 var _clockseq = (_seedBytes[6] << 8 | _seedBytes[7]) & 0x3fff;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
146
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
147 // Previous uuid creation time
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
148 var _lastMSecs = 0,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
149 _lastNSecs = 0;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
150
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
151 // See https://github.com/broofa/node-uuid for API details
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
152 function v1(options, buf, offset) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
153 var i = buf && offset || 0;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
154 var b = buf || [];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
155
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
156 options = options || {};
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
157
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
158 var clockseq = (options.clockseq != null) ? options.clockseq : _clockseq;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
159
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
160 // UUID timestamps are 100 nano-second units since the Gregorian
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
161 // epoch,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
162 // (1582-10-15 00:00). JSNumbers aren't precise enough for this, so
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
163 // time is handled internally as 'msecs' (integer milliseconds) and
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
164 // 'nsecs'
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
165 // (100-nanoseconds offset from msecs) since unix epoch, 1970-01-01
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
166 // 00:00.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
167 var msecs = (options.msecs != null) ? options.msecs : new Date()
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
168 .getTime();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
169
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
170 // Per 4.2.1.2, use count of uuid's generated during the current
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
171 // clock
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
172 // cycle to simulate higher resolution clock
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
173 var nsecs = (options.nsecs != null) ? options.nsecs : _lastNSecs + 1;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
174
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
175 // Time since last uuid creation (in msecs)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
176 var dt = (msecs - _lastMSecs) + (nsecs - _lastNSecs) / 10000;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
177
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
178 // Per 4.2.1.2, Bump clockseq on clock regression
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
179 if (dt < 0 && options.clockseq == null) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
180 clockseq = clockseq + 1 & 0x3fff;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
181 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
182
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
183 // Reset nsecs if clock regresses (new clockseq) or we've moved onto
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
184 // a new
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
185 // time interval
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
186 if ((dt < 0 || msecs > _lastMSecs) && options.nsecs == null) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
187 nsecs = 0;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
188 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
189
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
190 // Per 4.2.1.2 Throw error if too many uuids are requested
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
191 if (nsecs >= 10000) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
192 throw new Error(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
193 'uuid.v1(): Can\'t create more than 10M uuids/sec');
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
194 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
195
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
196 _lastMSecs = msecs;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
197 _lastNSecs = nsecs;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
198 _clockseq = clockseq;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
199
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
200 // Per 4.1.4 - Convert from unix epoch to Gregorian epoch
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
201 msecs += 12219292800000;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
202
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
203 // `time_low`
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
204 var tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
205 b[i++] = tl >>> 24 & 0xff;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
206 b[i++] = tl >>> 16 & 0xff;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
207 b[i++] = tl >>> 8 & 0xff;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
208 b[i++] = tl & 0xff;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
209
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
210 // `time_mid`
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
211 var tmh = (msecs / 0x100000000 * 10000) & 0xfffffff;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
212 b[i++] = tmh >>> 8 & 0xff;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
213 b[i++] = tmh & 0xff;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
214
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
215 // `time_high_and_version`
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
216 b[i++] = tmh >>> 24 & 0xf | 0x10; // include version
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
217 b[i++] = tmh >>> 16 & 0xff;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
218
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
219 // `clock_seq_hi_and_reserved` (Per 4.2.2 - include variant)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
220 b[i++] = clockseq >>> 8 | 0x80;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
221
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
222 // `clock_seq_low`
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
223 b[i++] = clockseq & 0xff;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
224
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
225 // `node`
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
226 var node = options.node || _nodeId;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
227 for (var n = 0; n < 6; n++) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
228 b[i + n] = node[n];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
229 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
230
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
231 return buf ? buf : unparse(b);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
232 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
233
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
234 // **`v4()` - Generate random UUID**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
235
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
236 // See https://github.com/broofa/node-uuid for API details
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
237 function v4(options, buf, offset) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
238 // Deprecated - 'format' argument, as supported in v1.2
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
239 var i = buf && offset || 0;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
240
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
241 if (typeof (options) === 'string') {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
242 buf = (options === 'binary') ? new BufferClass(16) : null;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
243 options = null;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
244 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
245 options = options || {};
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
246
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
247 var rnds = options.random || (options.rng || _rng)();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
248
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
249 // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
250 rnds[6] = (rnds[6] & 0x0f) | 0x40;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
251 rnds[8] = (rnds[8] & 0x3f) | 0x80;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
252
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
253 // Copy bytes to buffer, if provided
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
254 if (buf) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
255 for (var ii = 0; ii < 16; ii++) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
256 buf[i + ii] = rnds[ii];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
257 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
258 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
259
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
260 return buf || unparse(rnds);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
261 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
262
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
263 // Export public API
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
264 var uuid = function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
265 return new String(v4());
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
266 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
267 uuid.v1 = v1;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
268 uuid.v4 = v4;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
269 uuid.create = v4;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
270 uuid.empty = "00000000-0000-0000-0000-000000000000";
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
271 uuid.parse = parse;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
272 uuid.unparse = unparse;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
273 uuid.BufferClass = BufferClass;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
274 uuid._rng = _rng;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
275 uuid._mathRNG = _mathRNG;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
276 uuid._nodeRNG = _nodeRNG;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
277 uuid._whatwgRNG = _whatwgRNG;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
278
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
279 return uuid;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
280 });