Skip to content

Commit f6fe863

Browse files
committed
...
2 parents b5b431d + b222adc commit f6fe863

File tree

3 files changed

+72
-47
lines changed

3 files changed

+72
-47
lines changed

dist/kuzzle.js

Lines changed: 70 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,74 @@
44
// Copyright (c) 2010-2012 Robert Kieffer
55
// MIT License - http://opensource.org/licenses/mit-license.php
66

7-
(function() {
8-
var _global = this;
7+
/*global window, require, define */
8+
(function(_window) {
9+
'use strict';
910

1011
// Unique ID creation requires a high quality random # generator. We feature
1112
// detect to determine the best RNG source, normalizing to a function that
1213
// 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+
}
1433

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+
}
2352
}
2453

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
2756
//
2857
// 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+
}
3465
}
3566

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();
5071
}
5172

5273
// Buffer class to use
53-
var BufferClass = typeof(_global.Buffer) == 'function' ? _global.Buffer : Array;
74+
var BufferClass = ('function' === typeof Buffer) ? Buffer : Array;
5475

5576
// Maps for number <-> hex string conversion
5677
var _byteToHex = [];
@@ -119,17 +140,17 @@
119140

120141
options = options || {};
121142

122-
var clockseq = options.clockseq != null ? options.clockseq : _clockseq;
143+
var clockseq = (options.clockseq != null) ? options.clockseq : _clockseq;
123144

124145
// UUID timestamps are 100 nano-second units since the Gregorian epoch,
125146
// (1582-10-15 00:00). JSNumbers aren't precise enough for this, so
126147
// time is handled internally as 'msecs' (integer milliseconds) and 'nsecs'
127148
// (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();
129150

130151
// Per 4.2.1.2, use count of uuid's generated during the current clock
131152
// 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;
133154

134155
// Time since last uuid creation (in msecs)
135156
var dt = (msecs - _lastMSecs) + (nsecs - _lastNSecs)/10000;
@@ -195,8 +216,8 @@
195216
// Deprecated - 'format' argument, as supported in v1.2
196217
var i = buf && offset || 0;
197218

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;
200221
options = null;
201222
}
202223
options = options || {};
@@ -224,28 +245,32 @@
224245
uuid.parse = parse;
225246
uuid.unparse = unparse;
226247
uuid.BufferClass = BufferClass;
248+
uuid._rng = _rng;
249+
uuid._mathRNG = _mathRNG;
250+
uuid._nodeRNG = _nodeRNG;
251+
uuid._whatwgRNG = _whatwgRNG;
227252

228-
if (typeof(module) != 'undefined' && module.exports) {
253+
if (('undefined' !== typeof module) && module.exports) {
229254
// Publish as node.js module
230255
module.exports = uuid;
231-
} else if (typeof define === 'function' && define.amd) {
256+
} else if (typeof define === 'function' && define.amd) {
232257
// Publish as AMD module
233258
define(function() {return uuid;});
234-
259+
235260

236261
} else {
237262
// Publish as global (in browsers)
238-
var _previousRoot = _global.uuid;
263+
_previousRoot = _window.uuid;
239264

240265
// **`noConflict()` - (browser only) to reset global 'uuid' var**
241266
uuid.noConflict = function() {
242-
_global.uuid = _previousRoot;
267+
_window.uuid = _previousRoot;
243268
return uuid;
244269
};
245270

246-
_global.uuid = uuid;
271+
_window.uuid = uuid;
247272
}
248-
}).call(this);
273+
})('undefined' !== typeof window ? window : null);
249274

250275
},{}],2:[function(require,module,exports){
251276
var

0 commit comments

Comments
 (0)