|
4 | 4 | // Copyright (c) 2010-2012 Robert Kieffer
|
5 | 5 | // MIT License - http://opensource.org/licenses/mit-license.php
|
6 | 6 |
|
7 |
| -(function() { |
8 |
| - var _global = this; |
| 7 | +/*global window, require, define */ |
| 8 | +(function(_window) { |
| 9 | + 'use strict'; |
9 | 10 |
|
10 | 11 | // Unique ID creation requires a high quality random # generator. We feature
|
11 | 12 | // detect to determine the best RNG source, normalizing to a function that
|
12 | 13 | // returns 128-bits of randomness, since that's what's usually required
|
13 |
| - var _rng; |
| 14 | + var _rng, _mathRNG, _nodeRNG, _whatwgRNG, _previousRoot; |
| 15 | + |
| 16 | + function setupBrowser() { |
| 17 | + // Allow for MSIE11 msCrypto |
| 18 | + var _crypto = _window.crypto || _window.msCrypto; |
| 19 | + |
| 20 | + if (!_rng && _crypto && _crypto.getRandomValues) { |
| 21 | + // WHATWG crypto-based RNG - http://wiki.whatwg.org/wiki/Crypto |
| 22 | + // |
| 23 | + // Moderately fast, high quality |
| 24 | + try { |
| 25 | + var _rnds8 = new Uint8Array(16); |
| 26 | + _whatwgRNG = _rng = function whatwgRNG() { |
| 27 | + _crypto.getRandomValues(_rnds8); |
| 28 | + return _rnds8; |
| 29 | + }; |
| 30 | + _rng(); |
| 31 | + } catch(e) {} |
| 32 | + } |
14 | 33 |
|
15 |
| - // Node.js crypto-based RNG - http://nodejs.org/docs/v0.6.2/api/crypto.html |
16 |
| - // |
17 |
| - // Moderately fast, high quality |
18 |
| - if (typeof(_global.require) == 'function') { |
19 |
| - try { |
20 |
| - var _rb = _global.require('crypto').randomBytes; |
21 |
| - _rng = _rb && function() {return _rb(16);}; |
22 |
| - } catch(e) {} |
| 34 | + if (!_rng) { |
| 35 | + // Math.random()-based (RNG) |
| 36 | + // |
| 37 | + // If all else fails, use Math.random(). It's fast, but is of unspecified |
| 38 | + // quality. |
| 39 | + var _rnds = new Array(16); |
| 40 | + _mathRNG = _rng = function() { |
| 41 | + for (var i = 0, r; i < 16; i++) { |
| 42 | + if ((i & 0x03) === 0) { r = Math.random() * 0x100000000; } |
| 43 | + _rnds[i] = r >>> ((i & 0x03) << 3) & 0xff; |
| 44 | + } |
| 45 | + |
| 46 | + return _rnds; |
| 47 | + }; |
| 48 | + if ('undefined' !== typeof console && console.warn) { |
| 49 | + console.warn("[SECURITY] node-uuid: crypto not usable, falling back to insecure Math.random()"); |
| 50 | + } |
| 51 | + } |
23 | 52 | }
|
24 | 53 |
|
25 |
| - if (!_rng && _global.crypto && crypto.getRandomValues) { |
26 |
| - // WHATWG crypto-based RNG - http://wiki.whatwg.org/wiki/Crypto |
| 54 | + function setupNode() { |
| 55 | + // Node.js crypto-based RNG - http://nodejs.org/docs/v0.6.2/api/crypto.html |
27 | 56 | //
|
28 | 57 | // Moderately fast, high quality
|
29 |
| - var _rnds8 = new Uint8Array(16); |
30 |
| - _rng = function whatwgRNG() { |
31 |
| - crypto.getRandomValues(_rnds8); |
32 |
| - return _rnds8; |
33 |
| - }; |
| 58 | + if ('function' === typeof require) { |
| 59 | + try { |
| 60 | + var _rb = require('crypto').randomBytes; |
| 61 | + _nodeRNG = _rng = _rb && function() {return _rb(16);}; |
| 62 | + _rng(); |
| 63 | + } catch(e) {} |
| 64 | + } |
34 | 65 | }
|
35 | 66 |
|
36 |
| - if (!_rng) { |
37 |
| - // Math.random()-based (RNG) |
38 |
| - // |
39 |
| - // If all else fails, use Math.random(). It's fast, but is of unspecified |
40 |
| - // quality. |
41 |
| - var _rnds = new Array(16); |
42 |
| - _rng = function() { |
43 |
| - for (var i = 0, r; i < 16; i++) { |
44 |
| - if ((i & 0x03) === 0) r = Math.random() * 0x100000000; |
45 |
| - _rnds[i] = r >>> ((i & 0x03) << 3) & 0xff; |
46 |
| - } |
47 |
| - |
48 |
| - return _rnds; |
49 |
| - }; |
| 67 | + if (_window) { |
| 68 | + setupBrowser(); |
| 69 | + } else { |
| 70 | + setupNode(); |
50 | 71 | }
|
51 | 72 |
|
52 | 73 | // Buffer class to use
|
53 |
| - var BufferClass = typeof(_global.Buffer) == 'function' ? _global.Buffer : Array; |
| 74 | + var BufferClass = ('function' === typeof Buffer) ? Buffer : Array; |
54 | 75 |
|
55 | 76 | // Maps for number <-> hex string conversion
|
56 | 77 | var _byteToHex = [];
|
|
119 | 140 |
|
120 | 141 | options = options || {};
|
121 | 142 |
|
122 |
| - var clockseq = options.clockseq != null ? options.clockseq : _clockseq; |
| 143 | + var clockseq = (options.clockseq != null) ? options.clockseq : _clockseq; |
123 | 144 |
|
124 | 145 | // UUID timestamps are 100 nano-second units since the Gregorian epoch,
|
125 | 146 | // (1582-10-15 00:00). JSNumbers aren't precise enough for this, so
|
126 | 147 | // time is handled internally as 'msecs' (integer milliseconds) and 'nsecs'
|
127 | 148 | // (100-nanoseconds offset from msecs) since unix epoch, 1970-01-01 00:00.
|
128 |
| - var msecs = options.msecs != null ? options.msecs : new Date().getTime(); |
| 149 | + var msecs = (options.msecs != null) ? options.msecs : new Date().getTime(); |
129 | 150 |
|
130 | 151 | // Per 4.2.1.2, use count of uuid's generated during the current clock
|
131 | 152 | // cycle to simulate higher resolution clock
|
132 |
| - var nsecs = options.nsecs != null ? options.nsecs : _lastNSecs + 1; |
| 153 | + var nsecs = (options.nsecs != null) ? options.nsecs : _lastNSecs + 1; |
133 | 154 |
|
134 | 155 | // Time since last uuid creation (in msecs)
|
135 | 156 | var dt = (msecs - _lastMSecs) + (nsecs - _lastNSecs)/10000;
|
|
195 | 216 | // Deprecated - 'format' argument, as supported in v1.2
|
196 | 217 | var i = buf && offset || 0;
|
197 | 218 |
|
198 |
| - if (typeof(options) == 'string') { |
199 |
| - buf = options == 'binary' ? new BufferClass(16) : null; |
| 219 | + if (typeof(options) === 'string') { |
| 220 | + buf = (options === 'binary') ? new BufferClass(16) : null; |
200 | 221 | options = null;
|
201 | 222 | }
|
202 | 223 | options = options || {};
|
|
224 | 245 | uuid.parse = parse;
|
225 | 246 | uuid.unparse = unparse;
|
226 | 247 | uuid.BufferClass = BufferClass;
|
| 248 | + uuid._rng = _rng; |
| 249 | + uuid._mathRNG = _mathRNG; |
| 250 | + uuid._nodeRNG = _nodeRNG; |
| 251 | + uuid._whatwgRNG = _whatwgRNG; |
227 | 252 |
|
228 |
| - if (typeof(module) != 'undefined' && module.exports) { |
| 253 | + if (('undefined' !== typeof module) && module.exports) { |
229 | 254 | // Publish as node.js module
|
230 | 255 | module.exports = uuid;
|
231 |
| - } else if (typeof define === 'function' && define.amd) { |
| 256 | + } else if (typeof define === 'function' && define.amd) { |
232 | 257 | // Publish as AMD module
|
233 | 258 | define(function() {return uuid;});
|
234 |
| - |
| 259 | + |
235 | 260 |
|
236 | 261 | } else {
|
237 | 262 | // Publish as global (in browsers)
|
238 |
| - var _previousRoot = _global.uuid; |
| 263 | + _previousRoot = _window.uuid; |
239 | 264 |
|
240 | 265 | // **`noConflict()` - (browser only) to reset global 'uuid' var**
|
241 | 266 | uuid.noConflict = function() {
|
242 |
| - _global.uuid = _previousRoot; |
| 267 | + _window.uuid = _previousRoot; |
243 | 268 | return uuid;
|
244 | 269 | };
|
245 | 270 |
|
246 |
| - _global.uuid = uuid; |
| 271 | + _window.uuid = uuid; |
247 | 272 | }
|
248 |
| -}).call(this); |
| 273 | +})('undefined' !== typeof window ? window : null); |
249 | 274 |
|
250 | 275 | },{}],2:[function(require,module,exports){
|
251 | 276 | var
|
|
0 commit comments