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