uuid 2.0.3 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,84 +0,0 @@
1
- try {
2
- var nodeuuid = require('../uuid');
3
- } catch (e) {
4
- console.error('node-uuid require failed - skipping tests');
5
- }
6
-
7
- try {
8
- var uuid = require('uuid');
9
- } catch (e) {
10
- console.error('uuid require failed - skipping tests');
11
- }
12
-
13
- try {
14
- var uuidjs = require('uuid-js');
15
- } catch (e) {
16
- console.error('uuid-js require failed - skipping tests');
17
- }
18
-
19
- var N = 5e5;
20
-
21
- function rate(msg, t) {
22
- console.log(msg + ': ' +
23
- (N / (Date.now() - t) * 1e3 | 0) +
24
- ' uuids/second');
25
- }
26
-
27
- console.log('# v4');
28
-
29
- // node-uuid - string form
30
- if (nodeuuid) {
31
- for (var i = 0, t = Date.now(); i < N; i++) nodeuuid.v4();
32
- rate('nodeuuid.v4() - using node.js crypto RNG', t);
33
-
34
- for (var i = 0, t = Date.now(); i < N; i++) nodeuuid.v4({rng: nodeuuid.mathRNG});
35
- rate('nodeuuid.v4() - using Math.random() RNG', t);
36
-
37
- for (var i = 0, t = Date.now(); i < N; i++) nodeuuid.v4('binary');
38
- rate('nodeuuid.v4(\'binary\')', t);
39
-
40
- var buffer = new nodeuuid.BufferClass(16);
41
- for (var i = 0, t = Date.now(); i < N; i++) nodeuuid.v4('binary', buffer);
42
- rate('nodeuuid.v4(\'binary\', buffer)', t);
43
- }
44
-
45
- // libuuid - string form
46
- if (uuid) {
47
- for (var i = 0, t = Date.now(); i < N; i++) uuid();
48
- rate('uuid()', t);
49
-
50
- for (var i = 0, t = Date.now(); i < N; i++) uuid('binary');
51
- rate('uuid(\'binary\')', t);
52
- }
53
-
54
- // uuid-js - string form
55
- if (uuidjs) {
56
- for (var i = 0, t = Date.now(); i < N; i++) uuidjs.create(4);
57
- rate('uuidjs.create(4)', t);
58
- }
59
-
60
- // 140byte.es
61
- for (var i = 0, t = Date.now(); i < N; i++) 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,function(s,r){r=Math.random()*16|0;return (s=='x'?r:r&0x3|0x8).toString(16)});
62
- rate('140byte.es_v4', t);
63
-
64
- console.log('');
65
- console.log('# v1');
66
-
67
- // node-uuid - v1 string form
68
- if (nodeuuid) {
69
- for (var i = 0, t = Date.now(); i < N; i++) nodeuuid.v1();
70
- rate('nodeuuid.v1()', t);
71
-
72
- for (var i = 0, t = Date.now(); i < N; i++) nodeuuid.v1('binary');
73
- rate('nodeuuid.v1(\'binary\')', t);
74
-
75
- var buffer = new nodeuuid.BufferClass(16);
76
- for (var i = 0, t = Date.now(); i < N; i++) nodeuuid.v1('binary', buffer);
77
- rate('nodeuuid.v1(\'binary\', buffer)', t);
78
- }
79
-
80
- // uuid-js - v1 string form
81
- if (uuidjs) {
82
- for (var i = 0, t = Date.now(); i < N; i++) uuidjs.create(1);
83
- rate('uuidjs.create(1)', t);
84
- }
@@ -1,9 +0,0 @@
1
- {
2
- "name": "benchmark-uuid",
3
- "private": true,
4
- "description": "Benchmarks for node-uuid",
5
- "dependencies": {
6
- "uuid": "1.4.1",
7
- "uuid-js": "0.7.4"
8
- }
9
- }
package/misc/compare.js DELETED
@@ -1,62 +0,0 @@
1
- var assert = require('assert'),
2
- nodeuuid = require('../'),
3
- uuidjs = require('uuid-js'),
4
- util = require('util'),
5
- exec = require('child_process').exec,
6
- os = require('os');
7
-
8
- // On Mac Os X / macports there's only the ossp-uuid package that provides uuid
9
- // On Linux there's uuid-runtime which provides uuidgen
10
- var uuidCmd = os.type() === 'Darwin' ? 'uuid -1' : 'uuidgen -t';
11
-
12
- function compare(ids) {
13
- console.log(ids);
14
- for (var i = 0; i < ids.length; i++) {
15
- var id = ids[i].split('-');
16
- id = [id[2], id[1], id[0]].join('');
17
- ids[i] = id;
18
- }
19
- var sorted = ([].concat(ids)).sort();
20
-
21
- if (sorted.toString() !== ids.toString()) {
22
- console.log('Warning: sorted !== ids');
23
- } else {
24
- console.log('everything in order!');
25
- }
26
- }
27
-
28
- // Test time order of v1 uuids
29
- var ids = [];
30
- while (ids.length < 10e3) ids.push(nodeuuid.v1());
31
-
32
- var max = 10;
33
- console.log('node-uuid:');
34
- ids = [];
35
- for (var i = 0; i < max; i++) ids.push(nodeuuid.v1());
36
- compare(ids);
37
-
38
- console.log('');
39
- console.log('uuidjs:');
40
- ids = [];
41
- for (var i = 0; i < max; i++) ids.push(uuidjs.create(1).toString());
42
- compare(ids);
43
-
44
- console.log('');
45
- console.log('libuuid:');
46
- ids = [];
47
- var count = 0;
48
- var last = function() {
49
- compare(ids);
50
- }
51
- var cb = function(err, stdout, stderr) {
52
- ids.push(stdout.substring(0, stdout.length-1));
53
- count++;
54
- if (count < max) {
55
- return next();
56
- }
57
- last();
58
- };
59
- var next = function() {
60
- exec(uuidCmd, cb);
61
- };
62
- next();
package/misc/perf.js DELETED
@@ -1,102 +0,0 @@
1
- var assert = require('assert');
2
-
3
- var uuid = require('../');
4
-
5
- var log = console.log;
6
-
7
- var generators = {
8
- v1: uuid.v1,
9
- v4: uuid.v4
10
- };
11
-
12
- var UUID_FORMAT = {
13
- v1: /[0-9a-f]{8}-[0-9a-f]{4}-1[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/i,
14
- v4: /[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/i
15
- };
16
-
17
- var N = 1e4;
18
-
19
- // Get %'age an actual value differs from the ideal value
20
- function divergence(actual, ideal) {
21
- return Math.round(100*100*(actual - ideal)/ideal)/100;
22
- }
23
-
24
- function rate(msg, t) {
25
- log(msg + ': ' + (N / (Date.now() - t) * 1e3 | 0) + ' uuids\/second');
26
- }
27
-
28
- for (var version in generators) {
29
- var counts = {}, max = 0;
30
- var generator = generators[version];
31
- var format = UUID_FORMAT[version];
32
-
33
- log('\nSanity check ' + N + ' ' + version + ' uuids');
34
- for (var i = 0, ok = 0; i < N; i++) {
35
- id = generator();
36
- if (!format.test(id)) {
37
- throw Error(id + ' is not a valid UUID string');
38
- }
39
-
40
- if (id != uuid.unparse(uuid.parse(id))) {
41
- assert(fail, id + ' is not a valid id');
42
- }
43
-
44
- // Count digits for our randomness check
45
- if (version == 'v4') {
46
- var digits = id.replace(/-/g, '').split('');
47
- for (var j = digits.length-1; j >= 0; j--) {
48
- var c = digits[j];
49
- max = Math.max(max, counts[c] = (counts[c] || 0) + 1);
50
- }
51
- }
52
- }
53
-
54
- // Check randomness for v4 UUIDs
55
- if (version == 'v4') {
56
- // Limit that we get worried about randomness. (Purely empirical choice, this!)
57
- var limit = 2*100*Math.sqrt(1/N);
58
-
59
- log('\nChecking v4 randomness. Distribution of Hex Digits (% deviation from ideal)');
60
-
61
- for (var i = 0; i < 16; i++) {
62
- var c = i.toString(16);
63
- var bar = '', n = counts[c], p = Math.round(n/max*100|0);
64
-
65
- // 1-3,5-8, and D-F: 1:16 odds over 30 digits
66
- var ideal = N*30/16;
67
- if (i == 4) {
68
- // 4: 1:1 odds on 1 digit, plus 1:16 odds on 30 digits
69
- ideal = N*(1 + 30/16);
70
- } else if (i >= 8 && i <= 11) {
71
- // 8-B: 1:4 odds on 1 digit, plus 1:16 odds on 30 digits
72
- ideal = N*(1/4 + 30/16);
73
- } else {
74
- // Otherwise: 1:16 odds on 30 digits
75
- ideal = N*30/16;
76
- }
77
- var d = divergence(n, ideal);
78
-
79
- // Draw bar using UTF squares (just for grins)
80
- var s = n/max*50 | 0;
81
- while (s--) bar += '=';
82
-
83
- assert(Math.abs(d) < limit, c + ' |' + bar + '| ' + counts[c] + ' (' + d + '% < ' + limit + '%)');
84
- }
85
- }
86
- }
87
-
88
- // Perf tests
89
- for (var version in generators) {
90
- log('\nPerformance testing ' + version + ' UUIDs');
91
- var generator = generators[version];
92
- var buf = new uuid.BufferClass(16);
93
-
94
- for (var i = 0, t = Date.now(); i < N; i++) generator();
95
- rate('uuid.' + version + '()', t);
96
-
97
- for (var i = 0, t = Date.now(); i < N; i++) generator('binary');
98
- rate('uuid.' + version + '(\'binary\')', t);
99
-
100
- for (var i = 0, t = Date.now(); i < N; i++) generator('binary', buf);
101
- rate('uuid.' + version + '(\'binary\', buffer)', t);
102
- }
package/rng-browser.js DELETED
@@ -1,32 +0,0 @@
1
-
2
- var rng;
3
-
4
- var crypto = global.crypto || global.msCrypto; // for IE 11
5
- if (crypto && crypto.getRandomValues) {
6
- // WHATWG crypto-based RNG - http://wiki.whatwg.org/wiki/Crypto
7
- // Moderately fast, high quality
8
- var _rnds8 = new Uint8Array(16);
9
- rng = function whatwgRNG() {
10
- crypto.getRandomValues(_rnds8);
11
- return _rnds8;
12
- };
13
- }
14
-
15
- if (!rng) {
16
- // Math.random()-based (RNG)
17
- //
18
- // If all else fails, use Math.random(). It's fast, but is of unspecified
19
- // quality.
20
- var _rnds = new Array(16);
21
- rng = function() {
22
- for (var i = 0, r; i < 16; i++) {
23
- if ((i & 0x03) === 0) r = Math.random() * 0x100000000;
24
- _rnds[i] = r >>> ((i & 0x03) << 3) & 0xff;
25
- }
26
-
27
- return _rnds;
28
- };
29
- }
30
-
31
- module.exports = rng;
32
-
package/rng.js DELETED
@@ -1,4 +0,0 @@
1
- var rb = require('crypto').randomBytes;
2
- module.exports = function() {
3
- return rb(16);
4
- };
package/test/mocha.opts DELETED
@@ -1 +0,0 @@
1
- --ui qunit
package/test/test.js DELETED
@@ -1,105 +0,0 @@
1
- var assert = require('assert');
2
-
3
- var uuid = require('../');
4
-
5
- // Verify ordering of v1 ids created with explicit times
6
- var TIME = 1321644961388; // 2011-11-18 11:36:01.388-08:00
7
-
8
- function compare(name, ids) {
9
- test(name, function() {
10
- // avoid .map for older browsers
11
- for (var i=0 ; i<ids.length ; ++i) {
12
- ids[i] = ids[i].split('-').reverse().join('-');
13
- }
14
- ids = ids.sort();
15
- var sorted = ([].concat(ids)).sort();
16
-
17
- assert(sorted.toString() == ids.toString(), name + ' have expected order');
18
- });
19
- }
20
-
21
- // Verify ordering of v1 ids created using default behavior
22
- compare('uuids with current time', [
23
- uuid.v1(),
24
- uuid.v1(),
25
- uuid.v1(),
26
- uuid.v1(),
27
- uuid.v1()
28
- ]);
29
-
30
- // Verify ordering of v1 ids created with explicit times
31
- compare('uuids with time option', [
32
- uuid.v1({msecs: TIME - 10*3600*1000}),
33
- uuid.v1({msecs: TIME - 1}),
34
- uuid.v1({msecs: TIME}),
35
- uuid.v1({msecs: TIME + 1}),
36
- uuid.v1({msecs: TIME + 28*24*3600*1000})
37
- ]);
38
-
39
- test('msec', function() {
40
- assert(
41
- uuid.v1({msecs: TIME}) != uuid.v1({msecs: TIME}),
42
- 'IDs created at same msec are different'
43
- );
44
- });
45
-
46
- test('exception thrown when > 10k ids created in 1ms', function() {
47
- // Verify throw if too many ids created
48
- var thrown = false;
49
- try {
50
- uuid.v1({msecs: TIME, nsecs: 10000});
51
- } catch (e) {
52
- thrown = true;
53
- }
54
- assert(thrown, 'Exception thrown when > 10K ids created in 1 ms');
55
- });
56
-
57
- test('clock regression by msec', function() {
58
- // Verify clock regression bumps clockseq
59
- var uidt = uuid.v1({msecs: TIME});
60
- var uidtb = uuid.v1({msecs: TIME - 1});
61
- assert(
62
- parseInt(uidtb.split('-')[3], 16) - parseInt(uidt.split('-')[3], 16) === 1,
63
- 'Clock regression by msec increments the clockseq'
64
- );
65
- });
66
-
67
- test('clock regression by nsec', function() {
68
- // Verify clock regression bumps clockseq
69
- var uidtn = uuid.v1({msecs: TIME, nsecs: 10});
70
- var uidtnb = uuid.v1({msecs: TIME, nsecs: 9});
71
- assert(
72
- parseInt(uidtnb.split('-')[3], 16) - parseInt(uidtn.split('-')[3], 16) === 1,
73
- 'Clock regression by nsec increments the clockseq'
74
- );
75
- });
76
-
77
- test('explicit options product expected id', function() {
78
- // Verify explicit options produce expected id
79
- var id = uuid.v1({
80
- msecs: 1321651533573,
81
- nsecs: 5432,
82
- clockseq: 0x385c,
83
- node: [ 0x61, 0xcd, 0x3c, 0xbb, 0x32, 0x10 ]
84
- });
85
- assert(id == 'd9428888-122b-11e1-b85c-61cd3cbb3210', 'Explicit options produce expected id');
86
- });
87
-
88
- test('ids spanning 1ms boundary are 100ns apart', function() {
89
- // Verify adjacent ids across a msec boundary are 1 time unit apart
90
- var u0 = uuid.v1({msecs: TIME, nsecs: 9999});
91
- var u1 = uuid.v1({msecs: TIME + 1, nsecs: 0});
92
-
93
- var before = u0.split('-')[0], after = u1.split('-')[0];
94
- var dt = parseInt(after, 16) - parseInt(before, 16);
95
- assert(dt === 1, 'Ids spanning 1ms boundary are 100ns apart');
96
- });
97
-
98
- test('parse/unparse', function() {
99
- var id = '00112233445566778899aabbccddeeff';
100
- assert(uuid.unparse(uuid.parse(id.substr(0,10))) ==
101
- '00112233-4400-0000-0000-000000000000', 'Short parse');
102
- assert(uuid.unparse(uuid.parse('(this is the uuid -> ' + id + id)) ==
103
- '00112233-4455-6677-8899-aabbccddeeff', 'Dirty parse');
104
- });
105
-
package/uuid.js DELETED
@@ -1,183 +0,0 @@
1
- // uuid.js
2
- //
3
- // Copyright (c) 2010-2012 Robert Kieffer
4
- // MIT License - http://opensource.org/licenses/mit-license.php
5
-
6
- // Unique ID creation requires a high quality random # generator. We feature
7
- // detect to determine the best RNG source, normalizing to a function that
8
- // returns 128-bits of randomness, since that's what's usually required
9
- var _rng = require('./rng');
10
-
11
- // Maps for number <-> hex string conversion
12
- var _byteToHex = [];
13
- var _hexToByte = {};
14
- for (var i = 0; i < 256; i++) {
15
- _byteToHex[i] = (i + 0x100).toString(16).substr(1);
16
- _hexToByte[_byteToHex[i]] = i;
17
- }
18
-
19
- // **`parse()` - Parse a UUID into it's component bytes**
20
- function parse(s, buf, offset) {
21
- var i = (buf && offset) || 0, ii = 0;
22
-
23
- buf = buf || [];
24
- s.toLowerCase().replace(/[0-9a-f]{2}/g, function(oct) {
25
- if (ii < 16) { // Don't overflow!
26
- buf[i + ii++] = _hexToByte[oct];
27
- }
28
- });
29
-
30
- // Zero out remaining bytes if string was short
31
- while (ii < 16) {
32
- buf[i + ii++] = 0;
33
- }
34
-
35
- return buf;
36
- }
37
-
38
- // **`unparse()` - Convert UUID byte array (ala parse()) into a string**
39
- function unparse(buf, offset) {
40
- var i = offset || 0, bth = _byteToHex;
41
- return bth[buf[i++]] + bth[buf[i++]] +
42
- bth[buf[i++]] + bth[buf[i++]] + '-' +
43
- bth[buf[i++]] + bth[buf[i++]] + '-' +
44
- bth[buf[i++]] + bth[buf[i++]] + '-' +
45
- bth[buf[i++]] + bth[buf[i++]] + '-' +
46
- bth[buf[i++]] + bth[buf[i++]] +
47
- bth[buf[i++]] + bth[buf[i++]] +
48
- bth[buf[i++]] + bth[buf[i++]];
49
- }
50
-
51
- // **`v1()` - Generate time-based UUID**
52
- //
53
- // Inspired by https://github.com/LiosK/UUID.js
54
- // and http://docs.python.org/library/uuid.html
55
-
56
- // random #'s we need to init node and clockseq
57
- var _seedBytes = _rng();
58
-
59
- // Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1)
60
- var _nodeId = [
61
- _seedBytes[0] | 0x01,
62
- _seedBytes[1], _seedBytes[2], _seedBytes[3], _seedBytes[4], _seedBytes[5]
63
- ];
64
-
65
- // Per 4.2.2, randomize (14 bit) clockseq
66
- var _clockseq = (_seedBytes[6] << 8 | _seedBytes[7]) & 0x3fff;
67
-
68
- // Previous uuid creation time
69
- var _lastMSecs = 0, _lastNSecs = 0;
70
-
71
- // See https://github.com/broofa/node-uuid for API details
72
- function v1(options, buf, offset) {
73
- var i = buf && offset || 0;
74
- var b = buf || [];
75
-
76
- options = options || {};
77
-
78
- var clockseq = options.clockseq !== undefined ? options.clockseq : _clockseq;
79
-
80
- // UUID timestamps are 100 nano-second units since the Gregorian epoch,
81
- // (1582-10-15 00:00). JSNumbers aren't precise enough for this, so
82
- // time is handled internally as 'msecs' (integer milliseconds) and 'nsecs'
83
- // (100-nanoseconds offset from msecs) since unix epoch, 1970-01-01 00:00.
84
- var msecs = options.msecs !== undefined ? options.msecs : new Date().getTime();
85
-
86
- // Per 4.2.1.2, use count of uuid's generated during the current clock
87
- // cycle to simulate higher resolution clock
88
- var nsecs = options.nsecs !== undefined ? options.nsecs : _lastNSecs + 1;
89
-
90
- // Time since last uuid creation (in msecs)
91
- var dt = (msecs - _lastMSecs) + (nsecs - _lastNSecs)/10000;
92
-
93
- // Per 4.2.1.2, Bump clockseq on clock regression
94
- if (dt < 0 && options.clockseq === undefined) {
95
- clockseq = clockseq + 1 & 0x3fff;
96
- }
97
-
98
- // Reset nsecs if clock regresses (new clockseq) or we've moved onto a new
99
- // time interval
100
- if ((dt < 0 || msecs > _lastMSecs) && options.nsecs === undefined) {
101
- nsecs = 0;
102
- }
103
-
104
- // Per 4.2.1.2 Throw error if too many uuids are requested
105
- if (nsecs >= 10000) {
106
- throw new Error('uuid.v1(): Can\'t create more than 10M uuids/sec');
107
- }
108
-
109
- _lastMSecs = msecs;
110
- _lastNSecs = nsecs;
111
- _clockseq = clockseq;
112
-
113
- // Per 4.1.4 - Convert from unix epoch to Gregorian epoch
114
- msecs += 12219292800000;
115
-
116
- // `time_low`
117
- var tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000;
118
- b[i++] = tl >>> 24 & 0xff;
119
- b[i++] = tl >>> 16 & 0xff;
120
- b[i++] = tl >>> 8 & 0xff;
121
- b[i++] = tl & 0xff;
122
-
123
- // `time_mid`
124
- var tmh = (msecs / 0x100000000 * 10000) & 0xfffffff;
125
- b[i++] = tmh >>> 8 & 0xff;
126
- b[i++] = tmh & 0xff;
127
-
128
- // `time_high_and_version`
129
- b[i++] = tmh >>> 24 & 0xf | 0x10; // include version
130
- b[i++] = tmh >>> 16 & 0xff;
131
-
132
- // `clock_seq_hi_and_reserved` (Per 4.2.2 - include variant)
133
- b[i++] = clockseq >>> 8 | 0x80;
134
-
135
- // `clock_seq_low`
136
- b[i++] = clockseq & 0xff;
137
-
138
- // `node`
139
- var node = options.node || _nodeId;
140
- for (var n = 0; n < 6; n++) {
141
- b[i + n] = node[n];
142
- }
143
-
144
- return buf ? buf : unparse(b);
145
- }
146
-
147
- // **`v4()` - Generate random UUID**
148
-
149
- // See https://github.com/broofa/node-uuid for API details
150
- function v4(options, buf, offset) {
151
- // Deprecated - 'format' argument, as supported in v1.2
152
- var i = buf && offset || 0;
153
-
154
- if (typeof(options) == 'string') {
155
- buf = options == 'binary' ? new Array(16) : null;
156
- options = null;
157
- }
158
- options = options || {};
159
-
160
- var rnds = options.random || (options.rng || _rng)();
161
-
162
- // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
163
- rnds[6] = (rnds[6] & 0x0f) | 0x40;
164
- rnds[8] = (rnds[8] & 0x3f) | 0x80;
165
-
166
- // Copy bytes to buffer, if provided
167
- if (buf) {
168
- for (var ii = 0; ii < 16; ii++) {
169
- buf[i + ii] = rnds[ii];
170
- }
171
- }
172
-
173
- return buf || unparse(rnds);
174
- }
175
-
176
- // Export public API
177
- var uuid = v4;
178
- uuid.v1 = v1;
179
- uuid.v4 = v4;
180
- uuid.parse = parse;
181
- uuid.unparse = unparse;
182
-
183
- module.exports = uuid;