4
4
// This is an AngularJS wrapper for the original node-uuid library
5
5
// written by Robert Kieffer – https://github.com/broofa/node-uuid
6
6
// MIT License - http://opensource.org/licenses/mit-license.php
7
+ // The wrapped node-uuid library is at version 1.4.7
7
8
8
- function AngularUUID ( )
9
- {
10
- angular . module ( "angular-uuid" , [ ] ) . factory ( "uuid" , [ "$window" , uuid ] ) ;
9
+ function AngularUUID ( ) {
10
+ 'use strict' ;
11
11
12
- function uuid ( $window )
13
- {
14
- var _global = $window ;
12
+ angular . module ( 'angular-uuid' , [ ] ) . factory ( 'uuid' , [ '$window' , nodeUUID ] ) ;
15
13
14
+ function nodeUUID ( $window ) {
16
15
// Unique ID creation requires a high quality random # generator. We feature
17
16
// detect to determine the best RNG source, normalizing to a function that
18
17
// returns 128-bits of randomness, since that's what's usually required
19
- var _rng ;
18
+ var _rng , _mathRNG , _whatwgRNG ;
20
19
21
- // Node.js crypto-based RNG - http://nodejs.org/docs/v0.6.2/api/crypto.html
22
- //
23
- // Moderately fast, high quality
24
- if ( typeof ( _global . require ) == 'function' ) {
25
- try {
26
- var _rb = _global . require ( 'crypto' ) . randomBytes ;
27
- _rng = _rb && function ( ) { return _rb ( 16 ) ; } ;
28
- } catch ( e ) { }
29
- }
20
+ // Allow for MSIE11 msCrypto
21
+ var _crypto = $window . crypto || $window . msCrypto ;
30
22
31
- if ( ! _rng && _global . crypto && crypto . getRandomValues ) {
23
+ if ( ! _rng && _crypto && _crypto . getRandomValues ) {
32
24
// WHATWG crypto-based RNG - http://wiki.whatwg.org/wiki/Crypto
33
25
//
34
26
// Moderately fast, high quality
35
- var _rnds8 = new Uint8Array ( 16 ) ;
36
- _rng = function whatwgRNG ( ) {
37
- crypto . getRandomValues ( _rnds8 ) ;
38
- return _rnds8 ;
39
- } ;
27
+ try {
28
+ var _rnds8 = new Uint8Array ( 16 ) ;
29
+ _whatwgRNG = _rng = function whatwgRNG ( ) {
30
+ _crypto . getRandomValues ( _rnds8 ) ;
31
+ return _rnds8 ;
32
+ } ;
33
+ _rng ( ) ;
34
+ } catch ( e ) { }
40
35
}
41
36
42
37
if ( ! _rng ) {
@@ -45,18 +40,21 @@ function AngularUUID ()
45
40
// If all else fails, use Math.random(). It's fast, but is of unspecified
46
41
// quality.
47
42
var _rnds = new Array ( 16 ) ;
48
- _rng = function ( ) {
43
+ _mathRNG = _rng = function ( ) {
49
44
for ( var i = 0 , r ; i < 16 ; i ++ ) {
50
- if ( ( i & 0x03 ) === 0 ) r = Math . random ( ) * 0x100000000 ;
45
+ if ( ( i & 0x03 ) === 0 ) { r = Math . random ( ) * 0x100000000 ; }
51
46
_rnds [ i ] = r >>> ( ( i & 0x03 ) << 3 ) & 0xff ;
52
47
}
53
48
54
49
return _rnds ;
55
50
} ;
51
+ if ( 'undefined' !== typeof console && console . warn ) {
52
+ console . warn ( '[SECURITY] node-uuid: crypto not usable, falling back to insecure Math.random()' ) ;
53
+ }
56
54
}
57
55
58
56
// Buffer class to use
59
- var BufferClass = typeof ( _global . Buffer ) == 'function' ? _global . Buffer : Array ;
57
+ var BufferClass = ( 'function' === typeof Buffer ) ? Buffer : Array ;
60
58
61
59
// Maps for number <-> hex string conversion
62
60
var _byteToHex = [ ] ;
@@ -74,8 +72,8 @@ function AngularUUID ()
74
72
s . toLowerCase ( ) . replace ( / [ 0 - 9 a - f ] { 2 } / g, function ( oct ) {
75
73
if ( ii < 16 ) { // Don't overflow!
76
74
buf [ i + ii ++ ] = _hexToByte [ oct ] ;
77
- }
78
- } ) ;
75
+ }
76
+ } ) ;
79
77
80
78
// Zero out remaining bytes if string was short
81
79
while ( ii < 16 ) {
@@ -89,13 +87,13 @@ function AngularUUID ()
89
87
function unparse ( buf , offset ) {
90
88
var i = offset || 0 , bth = _byteToHex ;
91
89
return bth [ buf [ i ++ ] ] + bth [ buf [ i ++ ] ] +
92
- bth [ buf [ i ++ ] ] + bth [ buf [ i ++ ] ] + '-' +
93
- bth [ buf [ i ++ ] ] + bth [ buf [ i ++ ] ] + '-' +
94
- bth [ buf [ i ++ ] ] + bth [ buf [ i ++ ] ] + '-' +
95
- bth [ buf [ i ++ ] ] + bth [ buf [ i ++ ] ] + '-' +
96
- bth [ buf [ i ++ ] ] + bth [ buf [ i ++ ] ] +
97
- bth [ buf [ i ++ ] ] + bth [ buf [ i ++ ] ] +
98
- bth [ buf [ i ++ ] ] + bth [ buf [ i ++ ] ] ;
90
+ bth [ buf [ i ++ ] ] + bth [ buf [ i ++ ] ] + '-' +
91
+ bth [ buf [ i ++ ] ] + bth [ buf [ i ++ ] ] + '-' +
92
+ bth [ buf [ i ++ ] ] + bth [ buf [ i ++ ] ] + '-' +
93
+ bth [ buf [ i ++ ] ] + bth [ buf [ i ++ ] ] + '-' +
94
+ bth [ buf [ i ++ ] ] + bth [ buf [ i ++ ] ] +
95
+ bth [ buf [ i ++ ] ] + bth [ buf [ i ++ ] ] +
96
+ bth [ buf [ i ++ ] ] + bth [ buf [ i ++ ] ] ;
99
97
}
100
98
101
99
// **`v1()` - Generate time-based UUID**
@@ -108,8 +106,8 @@ function AngularUUID ()
108
106
109
107
// Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1)
110
108
var _nodeId = [
111
- _seedBytes [ 0 ] | 0x01 ,
112
- _seedBytes [ 1 ] , _seedBytes [ 2 ] , _seedBytes [ 3 ] , _seedBytes [ 4 ] , _seedBytes [ 5 ]
109
+ _seedBytes [ 0 ] | 0x01 ,
110
+ _seedBytes [ 1 ] , _seedBytes [ 2 ] , _seedBytes [ 3 ] , _seedBytes [ 4 ] , _seedBytes [ 5 ]
113
111
] ;
114
112
115
113
// Per 4.2.2, randomize (14 bit) clockseq
@@ -125,17 +123,17 @@ function AngularUUID ()
125
123
126
124
options = options || { } ;
127
125
128
- var clockseq = options . clockseq != null ? options . clockseq : _clockseq ;
126
+ var clockseq = ( options . clockseq != null ) ? options . clockseq : _clockseq ;
129
127
130
128
// UUID timestamps are 100 nano-second units since the Gregorian epoch,
131
129
// (1582-10-15 00:00). JSNumbers aren't precise enough for this, so
132
130
// time is handled internally as 'msecs' (integer milliseconds) and 'nsecs'
133
131
// (100-nanoseconds offset from msecs) since unix epoch, 1970-01-01 00:00.
134
- var msecs = options . msecs != null ? options . msecs : new Date ( ) . getTime ( ) ;
132
+ var msecs = ( options . msecs != null ) ? options . msecs : new Date ( ) . getTime ( ) ;
135
133
136
134
// Per 4.2.1.2, use count of uuid's generated during the current clock
137
135
// cycle to simulate higher resolution clock
138
- var nsecs = options . nsecs != null ? options . nsecs : _lastNSecs + 1 ;
136
+ var nsecs = ( options . nsecs != null ) ? options . nsecs : _lastNSecs + 1 ;
139
137
140
138
// Time since last uuid creation (in msecs)
141
139
var dt = ( msecs - _lastMSecs ) + ( nsecs - _lastNSecs ) / 10000 ;
@@ -201,8 +199,8 @@ function AngularUUID ()
201
199
// Deprecated - 'format' argument, as supported in v1.2
202
200
var i = buf && offset || 0 ;
203
201
204
- if ( typeof ( options ) == 'string' ) {
205
- buf = options == 'binary' ? new BufferClass ( 16 ) : null ;
202
+ if ( typeof ( options ) === 'string' ) {
203
+ buf = ( options === 'binary' ) ? new BufferClass ( 16 ) : null ;
206
204
options = null ;
207
205
}
208
206
options = options || { } ;
@@ -224,31 +222,30 @@ function AngularUUID ()
224
222
}
225
223
226
224
// Export public API
227
- var publicAPI = v4 ;
228
- publicAPI . v1 = v1 ;
229
- publicAPI . v4 = v4 ;
230
- publicAPI . parse = parse ;
231
- publicAPI . unparse = unparse ;
232
- publicAPI . BufferClass = BufferClass ;
233
-
234
- return publicAPI ;
225
+ var uuid = v4 ;
226
+ uuid . v1 = v1 ;
227
+ uuid . v4 = v4 ;
228
+ uuid . parse = parse ;
229
+ uuid . unparse = unparse ;
230
+ uuid . BufferClass = BufferClass ;
231
+ uuid . _rng = _rng ;
232
+ uuid . _mathRNG = _mathRNG ;
233
+ uuid . _whatwgRNG = _whatwgRNG ;
234
+
235
+ return uuid ;
235
236
}
236
237
}
237
238
238
239
// check for Module/AMD support, otherwise call the uuid function to setup the angular module.
239
- if ( typeof module !== "undefined" && module . exports )
240
- {
240
+ if ( typeof module !== 'undefined' && module . exports ) {
241
241
module . exports = new AngularUUID ( ) ;
242
- }
243
- else if ( typeof define !== "undefined" && define . amd )
244
- {
242
+
243
+ } else if ( typeof define !== 'undefined' && define . amd ) {
245
244
// AMD. Register as an anonymous module.
246
- define ( function ( )
247
- {
245
+ define ( function ( ) {
248
246
return new AngularUUID ( ) ;
249
247
} ) ;
250
- }
251
- else
252
- {
248
+
249
+ } else {
253
250
AngularUUID ( ) ;
254
251
}
0 commit comments