1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | (function(_window) {
|
8 | 'use strict';
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | var _rng, _mathRNG, _nodeRNG, _whatwgRNG, _previousRoot;
|
14 |
|
15 | function setupBrowser() {
|
16 |
|
17 | var _crypto = _window.crypto || _window.msCrypto;
|
18 |
|
19 | if (!_rng && _crypto && _crypto.getRandomValues) {
|
20 |
|
21 |
|
22 |
|
23 | try {
|
24 | var _rnds8 = new Uint8Array(16);
|
25 | _whatwgRNG = _rng = function whatwgRNG() {
|
26 | _crypto.getRandomValues(_rnds8);
|
27 | return _rnds8;
|
28 | };
|
29 | _rng();
|
30 | } catch(e) {}
|
31 | }
|
32 |
|
33 | if (!_rng) {
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | var _rnds = new Array(16);
|
39 | _mathRNG = _rng = function() {
|
40 | for (var i = 0, r; i < 16; i++) {
|
41 | if ((i & 0x03) === 0) { r = Math.random() * 0x100000000; }
|
42 | _rnds[i] = r >>> ((i & 0x03) << 3) & 0xff;
|
43 | }
|
44 |
|
45 | return _rnds;
|
46 | };
|
47 | if ('undefined' !== typeof console && console.warn) {
|
48 | console.warn("[SECURITY] node-uuid: crypto not usable, falling back to insecure Math.random()");
|
49 | }
|
50 | }
|
51 | }
|
52 |
|
53 | function setupNode() {
|
54 |
|
55 |
|
56 |
|
57 | if ('function' === typeof require) {
|
58 | try {
|
59 | var _rb = require('crypto').randomBytes;
|
60 | _nodeRNG = _rng = _rb && function() {return _rb(16);};
|
61 | _rng();
|
62 | } catch(e) {}
|
63 | }
|
64 | }
|
65 |
|
66 | if (_window) {
|
67 | setupBrowser();
|
68 | } else {
|
69 | setupNode();
|
70 | }
|
71 |
|
72 |
|
73 | var BufferClass = ('function' === typeof Buffer) ? Buffer : Array;
|
74 |
|
75 |
|
76 | var _byteToHex = [];
|
77 | var _hexToByte = {};
|
78 | for (var i = 0; i < 256; i++) {
|
79 | _byteToHex[i] = (i + 0x100).toString(16).substr(1);
|
80 | _hexToByte[_byteToHex[i]] = i;
|
81 | }
|
82 |
|
83 |
|
84 | function parse(s, buf, offset) {
|
85 | var i = (buf && offset) || 0, ii = 0;
|
86 |
|
87 | buf = buf || [];
|
88 | s.toLowerCase().replace(/[0-9a-f]{2}/g, function(oct) {
|
89 | if (ii < 16) {
|
90 | buf[i + ii++] = _hexToByte[oct];
|
91 | }
|
92 | });
|
93 |
|
94 |
|
95 | while (ii < 16) {
|
96 | buf[i + ii++] = 0;
|
97 | }
|
98 |
|
99 | return buf;
|
100 | }
|
101 |
|
102 |
|
103 | function unparse(buf, offset) {
|
104 | var i = offset || 0, bth = _byteToHex;
|
105 | return bth[buf[i++]] + bth[buf[i++]] +
|
106 | bth[buf[i++]] + bth[buf[i++]] + '-' +
|
107 | bth[buf[i++]] + bth[buf[i++]] + '-' +
|
108 | bth[buf[i++]] + bth[buf[i++]] + '-' +
|
109 | bth[buf[i++]] + bth[buf[i++]] + '-' +
|
110 | bth[buf[i++]] + bth[buf[i++]] +
|
111 | bth[buf[i++]] + bth[buf[i++]] +
|
112 | bth[buf[i++]] + bth[buf[i++]];
|
113 | }
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | var _seedBytes = _rng();
|
122 |
|
123 |
|
124 | var _nodeId = [
|
125 | _seedBytes[0] | 0x01,
|
126 | _seedBytes[1], _seedBytes[2], _seedBytes[3], _seedBytes[4], _seedBytes[5]
|
127 | ];
|
128 |
|
129 |
|
130 | var _clockseq = (_seedBytes[6] << 8 | _seedBytes[7]) & 0x3fff;
|
131 |
|
132 |
|
133 | var _lastMSecs = 0, _lastNSecs = 0;
|
134 |
|
135 |
|
136 | function v1(options, buf, offset) {
|
137 | var i = buf && offset || 0;
|
138 | var b = buf || [];
|
139 |
|
140 | options = options || {};
|
141 |
|
142 | var clockseq = (options.clockseq != null) ? options.clockseq : _clockseq;
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | var msecs = (options.msecs != null) ? options.msecs : new Date().getTime();
|
149 |
|
150 |
|
151 |
|
152 | var nsecs = (options.nsecs != null) ? options.nsecs : _lastNSecs + 1;
|
153 |
|
154 |
|
155 | var dt = (msecs - _lastMSecs) + (nsecs - _lastNSecs)/10000;
|
156 |
|
157 |
|
158 | if (dt < 0 && options.clockseq == null) {
|
159 | clockseq = clockseq + 1 & 0x3fff;
|
160 | }
|
161 |
|
162 |
|
163 |
|
164 | if ((dt < 0 || msecs > _lastMSecs) && options.nsecs == null) {
|
165 | nsecs = 0;
|
166 | }
|
167 |
|
168 |
|
169 | if (nsecs >= 10000) {
|
170 | throw new Error('uuid.v1(): Can\'t create more than 10M uuids/sec');
|
171 | }
|
172 |
|
173 | _lastMSecs = msecs;
|
174 | _lastNSecs = nsecs;
|
175 | _clockseq = clockseq;
|
176 |
|
177 |
|
178 | msecs += 12219292800000;
|
179 |
|
180 |
|
181 | var tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000;
|
182 | b[i++] = tl >>> 24 & 0xff;
|
183 | b[i++] = tl >>> 16 & 0xff;
|
184 | b[i++] = tl >>> 8 & 0xff;
|
185 | b[i++] = tl & 0xff;
|
186 |
|
187 |
|
188 | var tmh = (msecs / 0x100000000 * 10000) & 0xfffffff;
|
189 | b[i++] = tmh >>> 8 & 0xff;
|
190 | b[i++] = tmh & 0xff;
|
191 |
|
192 |
|
193 | b[i++] = tmh >>> 24 & 0xf | 0x10;
|
194 | b[i++] = tmh >>> 16 & 0xff;
|
195 |
|
196 |
|
197 | b[i++] = clockseq >>> 8 | 0x80;
|
198 |
|
199 |
|
200 | b[i++] = clockseq & 0xff;
|
201 |
|
202 |
|
203 | var node = options.node || _nodeId;
|
204 | for (var n = 0; n < 6; n++) {
|
205 | b[i + n] = node[n];
|
206 | }
|
207 |
|
208 | return buf ? buf : unparse(b);
|
209 | }
|
210 |
|
211 |
|
212 |
|
213 |
|
214 | function v4(options, buf, offset) {
|
215 |
|
216 | var i = buf && offset || 0;
|
217 |
|
218 | if (typeof(options) === 'string') {
|
219 | buf = (options === 'binary') ? new BufferClass(16) : null;
|
220 | options = null;
|
221 | }
|
222 | options = options || {};
|
223 |
|
224 | var rnds = options.random || (options.rng || _rng)();
|
225 |
|
226 |
|
227 | rnds[6] = (rnds[6] & 0x0f) | 0x40;
|
228 | rnds[8] = (rnds[8] & 0x3f) | 0x80;
|
229 |
|
230 |
|
231 | if (buf) {
|
232 | for (var ii = 0; ii < 16; ii++) {
|
233 | buf[i + ii] = rnds[ii];
|
234 | }
|
235 | }
|
236 |
|
237 | return buf || unparse(rnds);
|
238 | }
|
239 |
|
240 |
|
241 | var uuid = v4;
|
242 | uuid.v1 = v1;
|
243 | uuid.v4 = v4;
|
244 | uuid.parse = parse;
|
245 | uuid.unparse = unparse;
|
246 | uuid.BufferClass = BufferClass;
|
247 | uuid._rng = _rng;
|
248 | uuid._mathRNG = _mathRNG;
|
249 | uuid._nodeRNG = _nodeRNG;
|
250 | uuid._whatwgRNG = _whatwgRNG;
|
251 |
|
252 | if (('undefined' !== typeof module) && module.exports) {
|
253 |
|
254 | module.exports = uuid;
|
255 | } else if (typeof define === 'function' && define.amd) {
|
256 |
|
257 | define(function() {return uuid;});
|
258 |
|
259 |
|
260 | } else {
|
261 |
|
262 | _previousRoot = _window.uuid;
|
263 |
|
264 |
|
265 | uuid.noConflict = function() {
|
266 | _window.uuid = _previousRoot;
|
267 | return uuid;
|
268 | };
|
269 |
|
270 | _window.uuid = uuid;
|
271 | }
|
272 | })('undefined' !== typeof window ? window : null);
|