uuid 3.0.1 → 3.1.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.
package/.eslintrc.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "root": true,
3
+ "env": {
4
+ "browser": true,
5
+ "commonjs": true,
6
+ "node": true,
7
+ "mocha": true
8
+ },
9
+ "extends": ["eslint:recommended"],
10
+ "installedESLint": true,
11
+ "rules": {
12
+ "array-bracket-spacing": ["warn", "never"],
13
+ "arrow-body-style": ["warn", "as-needed"],
14
+ "arrow-parens": ["warn", "as-needed"],
15
+ "arrow-spacing": "warn",
16
+ "brace-style": "warn",
17
+ "camelcase": "warn",
18
+ "comma-spacing": ["warn", {"after": true}],
19
+ "dot-notation": "warn",
20
+ "indent": ["warn", 2, {
21
+ "SwitchCase": 1,
22
+ "FunctionDeclaration": {"parameters": 1},
23
+ "MemberExpression": 1,
24
+ "CallExpression": {"arguments": 1}
25
+ }],
26
+ "key-spacing": ["warn", {"beforeColon": false, "afterColon": true, "mode": "minimum"}],
27
+ "keyword-spacing": "warn",
28
+ "no-console": "off",
29
+ "no-empty": "off",
30
+ "no-multi-spaces": "warn",
31
+ "no-redeclare": "off",
32
+ "no-restricted-globals": ["warn", "Promise"],
33
+ "no-trailing-spaces": "warn",
34
+ "no-undef": "error",
35
+ "no-unused-vars": ["warn", {"args": "none"}],
36
+ "padded-blocks": ["warn", "never"],
37
+ "object-curly-spacing": ["warn", "never"],
38
+ "quotes": ["warn", "single"],
39
+ "react/prop-types": "off",
40
+ "react/jsx-no-bind": "off",
41
+ "semi": ["warn", "always"],
42
+ "space-before-blocks": ["warn", "always"],
43
+ "space-before-function-paren": ["warn", "never"],
44
+ "space-in-parens": ["warn", "never"]
45
+ }
46
+ }
package/README.md CHANGED
@@ -4,10 +4,10 @@ Simple, fast generation of [RFC4122](http://www.ietf.org/rfc/rfc4122.txt) UUIDS.
4
4
 
5
5
  Features:
6
6
 
7
- * Generate RFC4122 version 1 or version 4 UUIDs
8
- * Runs in node.js and browsers
9
- * Cryptographically strong random number generation on supporting platforms
10
- * Small footprint (Want something smaller? [Check this out](https://gist.github.com/982883)!)
7
+ * Support for version 1, 4 and 5 UUIDs
8
+ * Cross-platform
9
+ * Uses cryptographically-strong random number APIs (when available)
10
+ * Zero-dependency, small footprint (... but not [this small](https://gist.github.com/982883))
11
11
 
12
12
  ## Quickstart - CommonJS (Recommended)
13
13
 
@@ -15,39 +15,81 @@ Features:
15
15
  npm install uuid
16
16
  ```
17
17
 
18
+ Then generate your uuid version of choice ...
19
+
20
+ Version 1 (timestamp):
21
+
22
+ ```javascript
23
+ const uuidv1 = require('uuid/v1');
24
+ uuidv1(); // -> '6c84fb90-12c4-11e1-840d-7b25c5ee775a'
25
+ ```
26
+
27
+ Version 4 (random):
28
+
29
+ ```javascript
30
+ const uuidv4 = require('uuid/v4');
31
+ uuidv4(); // -> '110ec58a-a0f2-4ac4-8393-c866d813b8d1'
32
+ ```
33
+
34
+ Version 5 (namespace):
35
+
18
36
  ```javascript
19
- // Generate a v1 UUID (time-based)
20
- const uuidV1 = require('uuid/v1');
21
- uuidV1(); // -> '6c84fb90-12c4-11e1-840d-7b25c5ee775a'
37
+ const uuidv5 = require('uuid/v5');
38
+
39
+ // ... using predefined DNS namespace (for domain names)
40
+ uuidv5('hello.example.com', uuidv5.DNS)); // -> 'fdda765f-fc57-5604-a269-52a7df8164ec'
22
41
 
23
- // Generate a v4 UUID (random)
24
- const uuidV4 = require('uuid/v4');
25
- uuidV4(); // -> '110ec58a-a0f2-4ac4-8393-c866d813b8d1'
42
+ // ... using predefined URL namespace (for, well, URLs)
43
+ uuidv5('http://example.com/hello', uuidv5.URL); // -> '3bbcee75-cecc-5b56-8031-b6641c1ed1f1'
44
+
45
+ // ... using a custom namespace
46
+ const MY_NAMESPACE = '<UUID string you previously generated elsewhere>';
47
+ uuidv5('Hello, World!', MY_NAMESPACE); // -> '90123e1c-7512-523e-bb28-76fab9f2f73d'
26
48
  ```
27
49
 
28
- ## Quickstart - Pre-packaged for browsers (Not recommended)
50
+ ## Quickstart - Browser-ready Versions
29
51
 
30
52
  Browser-ready versions of this module are available via [wzrd.in](https://github.com/jfhbrook/wzrd.in).
31
53
 
54
+ For version 1 uuids:
55
+
32
56
  ```html
33
- <script src="http://wzrd.in/standalone/uuid@latest"></script>
57
+ <script src="http://wzrd.in/standalone/uuid%2Fv1@latest"></script>
58
+ <script>
59
+ uuidv1(); // -> v1 UUID
60
+ </script>
61
+ ```
34
62
 
63
+ For version 4 uuids:
64
+
65
+ ```html
66
+ <script src="http://wzrd.in/standalone/uuid%2Fv4@latest"></script>
35
67
  <script>
36
- uuid.v1(); // -> v1 UUID
37
- uuid.v4(); // -> v4 UUID
68
+ uuidv4(); // -> v4 UUID
38
69
  </script>
39
70
  ```
40
71
 
41
- (Note: Do not do this in production. Just don't. wzrd.in is a great service, but if you're deploying a "real" service you should be using a packaging tool like browserify or webpack. If you do go this route you would be well advised to link to a specific version instead of `uuid@latest` to avoid having your code break when we roll out breaking changes.)
72
+ For version 5 uuids:
42
73
 
74
+ ```html
75
+ <script src="http://wzrd.in/standalone/uuid%2Fv5@latest"></script>
76
+ <script>
77
+ uuidv5('http://example.com/hello', uuidv5.URL); // -> v5 UUID
78
+ </script>
79
+ ```
43
80
 
44
81
  ## API
45
82
 
46
- ### uuid(...)
83
+ ### Version 1
47
84
 
48
- Generate a V4 uuid. See uuid.v4 documentation below.
85
+ ```javascript
86
+ const uuidv1 = require('uuid/v1');
49
87
 
50
- ### uuid.v1([`options` [, `buffer` [, `offset`]]])
88
+ // Allowed arguments
89
+ uuidv1();
90
+ uuidv1(options);
91
+ uuidv1(options, buffer, offset);
92
+ ```
51
93
 
52
94
  Generate and return a RFC4122 v1 (timestamp-based) UUID.
53
95
 
@@ -63,14 +105,12 @@ Generate and return a RFC4122 v1 (timestamp-based) UUID.
63
105
 
64
106
  Returns `buffer`, if specified, otherwise the string form of the UUID
65
107
 
66
- Notes:
67
-
68
- 1. The randomly generated node id is only guaranteed to stay constant for the lifetime of the current JS runtime. (Future versions of this module may use persistent storage mechanisms to extend this guarantee.)
108
+ Note: The <node> id is generated guaranteed to stay constant for the lifetime of the current JS runtime. (Future versions of this module may use persistent storage mechanisms to extend this guarantee.)
69
109
 
70
110
  Example: Generate string UUID with fully-specified options
71
111
 
72
112
  ```javascript
73
- uuid.v1({
113
+ uuidv1({
74
114
  node: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab],
75
115
  clockseq: 0x1234,
76
116
  msecs: new Date('2011-11-01').getTime(),
@@ -83,19 +123,26 @@ Example: In-place generation of two binary IDs
83
123
  ```javascript
84
124
  // Generate two ids in an array
85
125
  const arr = new Array(32); // -> []
86
- uuid.v1(null, arr, 0); // -> [02 a2 ce 90 14 32 11 e1 85 58 0b 48 8e 4f c1 15]
87
- uuid.v1(null, arr, 16); // -> [02 a2 ce 90 14 32 11 e1 85 58 0b 48 8e 4f c1 15 02 a3 1c b0 14 32 11 e1 85 58 0b 48 8e 4f c1 15]
126
+ uuidv1(null, arr, 0); // -> [02 a2 ce 90 14 32 11 e1 85 58 0b 48 8e 4f c1 15]
127
+ uuidv1(null, arr, 16); // -> [02 a2 ce 90 14 32 11 e1 85 58 0b 48 8e 4f c1 15 02 a3 1c b0 14 32 11 e1 85 58 0b 48 8e 4f c1 15]
88
128
  ```
89
129
 
90
- ### uuid.v4([`options` [, `buffer` [, `offset`]]])
130
+ ### Version 4
131
+
132
+ ```javascript
133
+ const uuidv4 = require('uuid/v4')
134
+
135
+ // Allowed arguments
136
+ uuidv4();
137
+ uuidv4(options);
138
+ uuidv4(options, buffer, offset);
139
+ ```
91
140
 
92
141
  Generate and return a RFC4122 v4 UUID.
93
142
 
94
143
  * `options` - (Object) Optional uuid state to apply. Properties may include:
95
-
96
144
  * `random` - (Number[16]) Array of 16 numbers (0-255) to use in place of randomly generated values
97
- * `rng` - (Function) Random # generator to use. Set to one of the built-in generators - `uuid.mathRNG` (all platforms), `uuid.nodeRNG` (node.js only), `uuid.whatwgRNG` (WebKit only) - or a custom function that returns an array[16] of byte values.
98
-
145
+ * `rng` - (Function) Random # generator function that returns an Array[16] of byte values (0-255)
99
146
  * `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
100
147
  * `offset` - (Number) Starting index in `buffer` at which to begin writing.
101
148
 
@@ -121,12 +168,60 @@ uuid.v4(null, buffer, 0);
121
168
  uuid.v4(null, buffer, 16);
122
169
  ```
123
170
 
124
- ## Testing
171
+ ### Version 5
125
172
 
173
+ ```javascript
174
+ const uuidv5 = require('uuid/v4');
175
+
176
+ // Allowed arguments
177
+ uuidv5(name, namespace);
178
+ uuidv5(name, namespace, buffer);
179
+ uuidv5(name, namespace, buffer, offset);
126
180
  ```
181
+
182
+ Generate and return a RFC4122 v4 UUID.
183
+
184
+ * `name` - (String | Array[]) "name" to create UUID with
185
+ * `namespace` - (String | Array[]) "namespace" UUID either as a String or Array[16] of byte values
186
+ * `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
187
+ * `offset` - (Number) Starting index in `buffer` at which to begin writing. Default = 0
188
+
189
+ Returns `buffer`, if specified, otherwise the string form of the UUID
190
+
191
+ Example:
192
+
193
+ ```javascript
194
+ // Generate a unique namespace (typically you would do this once, outside of
195
+ // your project, then bake this value into your code)
196
+ const uuidv4 = require('uuid/v4');
197
+ const MY_NAMESPACE = uuidv4(); //
198
+
199
+ // Generate a couple namespace uuids
200
+ const uuidv5 = require('uuid/v5');
201
+ uuidv5('hello', MY_NAMESPACE);
202
+ uuidv5('world', MY_NAMESPACE);
203
+ ```
204
+
205
+ ## Testing
206
+
207
+ ```shell
127
208
  npm test
128
209
  ```
129
210
 
211
+ ## Deprecated / Browser-ready API
212
+
213
+ The API below is available for legacy purposes and is not expected to be available post-3.X
214
+
215
+ ```javascript
216
+ const uuid = require('uuid');
217
+
218
+ uuid.v1(...); // alias of uuid/v1
219
+ uuid.v4(...); // alias of uuid/v4
220
+ uuid(...); // alias of uuid/v4
221
+
222
+ // uuid.v5() is not supported in this API
223
+ ```
224
+
130
225
  ## Legacy node-uuid package
131
226
 
132
227
  The code for the legacy node-uuid package is available in the `node-uuid` branch.
package/bin/uuid CHANGED
@@ -1,26 +1,50 @@
1
1
  #!/usr/bin/env node
2
+ var assert = require('assert');
2
3
 
3
- var path = require('path');
4
- var uuid = require(path.join(__dirname, '..'));
4
+ function usage() {
5
+ console.log('Usage:');
6
+ console.log(' uuid');
7
+ console.log(' uuid v1');
8
+ console.log(' uuid v4');
9
+ console.log(' uuid v5 <name> <namespace uuid>');
10
+ console.log(' uuid --help');
11
+ console.log('\nNote: <namespace uuid> may be "URL" or "DNS" to use the corresponding UUIDs defined by RFC4122');
12
+ }
5
13
 
6
- var arg = process.argv[2];
14
+ var args = process.argv.slice(2);
7
15
 
8
- if ('--help' === arg) {
9
- console.log('\n USAGE: uuid [version] [options]\n\n');
10
- console.log(' options:\n');
11
- console.log(' --help Display this message and exit\n');
16
+ if (args.indexOf('--help') >= 0) {
17
+ usage();
12
18
  process.exit(0);
13
19
  }
20
+ var version = args.shift() || 'v4';
14
21
 
15
- if (null == arg) {
16
- console.log(uuid());
17
- process.exit(0);
18
- }
22
+ switch (version) {
23
+ case 'v1':
24
+ var uuidV1 = require('../v1');
25
+ console.log(uuidV1());
26
+ break;
19
27
 
20
- if ('v1' !== arg && 'v4' !== arg) {
21
- console.error('Version must be RFC4122 version 1 or version 4, denoted as "v1" or "v4"');
22
- process.exit(1);
23
- }
28
+ case 'v4':
29
+ var uuidV4 = require('../v4');
30
+ console.log(uuidV4());
31
+ break;
32
+
33
+ case 'v5':
34
+ var uuidV5 = require('../v5');
24
35
 
25
- console.log(uuid[arg]());
26
- process.exit(0);
36
+ var name = args.shift();
37
+ var namespace = args.shift();
38
+ assert(name != null, 'v5 name not specified');
39
+ assert(namespace != null, 'v5 namespace not specified');
40
+
41
+ if (namespace == 'URL') namespace = uuidV5.URL;
42
+ if (namespace == 'DNS') namespace = uuidV5.DNS;
43
+
44
+ console.log(uuidV5(name, namespace));
45
+ break;
46
+
47
+ default:
48
+ usage();
49
+ process.exit(1);
50
+ }
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Convert array of 16 byte values to UUID string format of the form:
3
- * XXXXXXXX-XXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
3
+ * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
4
4
  */
5
5
  var byteToHex = [];
6
6
  for (var i = 0; i < 256; ++i) {
@@ -10,7 +10,7 @@ for (var i = 0; i < 256; ++i) {
10
10
  function bytesToUuid(buf, offset) {
11
11
  var i = offset || 0;
12
12
  var bth = byteToHex;
13
- return bth[buf[i++]] + bth[buf[i++]] +
13
+ return bth[buf[i++]] + bth[buf[i++]] +
14
14
  bth[buf[i++]] + bth[buf[i++]] + '-' +
15
15
  bth[buf[i++]] + bth[buf[i++]] + '-' +
16
16
  bth[buf[i++]] + bth[buf[i++]] + '-' +
@@ -7,7 +7,7 @@ var rng;
7
7
  var crypto = global.crypto || global.msCrypto; // for IE 11
8
8
  if (crypto && crypto.getRandomValues) {
9
9
  // WHATWG crypto RNG - http://wiki.whatwg.org/wiki/Crypto
10
- var rnds8 = new Uint8Array(16);
10
+ var rnds8 = new Uint8Array(16); // eslint-disable-line no-undef
11
11
  rng = function whatwgRNG() {
12
12
  crypto.getRandomValues(rnds8);
13
13
  return rnds8;
@@ -19,7 +19,7 @@ if (!rng) {
19
19
  //
20
20
  // If all else fails, use Math.random(). It's fast, but is of unspecified
21
21
  // quality.
22
- var rnds = new Array(16);
22
+ var rnds = new Array(16);
23
23
  rng = function() {
24
24
  for (var i = 0, r; i < 16; i++) {
25
25
  if ((i & 0x03) === 0) r = Math.random() * 0x100000000;
package/lib/rng.js CHANGED
@@ -1,10 +1,10 @@
1
1
  // Unique ID creation requires a high quality random # generator. In node.js
2
- // this is prett straight-forward - we use the crypto API.
2
+ // this is pretty straight-forward - we use the crypto API.
3
3
 
4
4
  var rb = require('crypto').randomBytes;
5
5
 
6
6
  function rng() {
7
7
  return rb(16);
8
- };
8
+ }
9
9
 
10
10
  module.exports = rng;
@@ -0,0 +1,85 @@
1
+ // Adapted from Chris Veness' SHA1 code at
2
+ // http://www.movable-type.co.uk/scripts/sha1.html
3
+ 'use strict';
4
+
5
+ function f(s, x, y, z) {
6
+ switch (s) {
7
+ case 0: return (x & y) ^ (~x & z);
8
+ case 1: return x ^ y ^ z;
9
+ case 2: return (x & y) ^ (x & z) ^ (y & z);
10
+ case 3: return x ^ y ^ z;
11
+ }
12
+ }
13
+
14
+ function ROTL(x, n) {
15
+ return (x << n) | (x>>> (32 - n));
16
+ }
17
+
18
+ function sha1(bytes) {
19
+ var K = [0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6];
20
+ var H = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0];
21
+
22
+ if (typeof(bytes) == 'string') {
23
+ var msg = unescape(encodeURIComponent(bytes)); // UTF8 escape
24
+ bytes = new Array(msg.length);
25
+ for (var i = 0; i < msg.length; i++) bytes[i] = msg.charCodeAt(i);
26
+ }
27
+
28
+ bytes.push(0x80);
29
+
30
+ var l = bytes.length/4 + 2;
31
+ var N = Math.ceil(l/16);
32
+ var M = new Array(N);
33
+
34
+ for (var i=0; i<N; i++) {
35
+ M[i] = new Array(16);
36
+ for (var j=0; j<16; j++) {
37
+ M[i][j] =
38
+ bytes[i * 64 + j * 4] << 24 |
39
+ bytes[i * 64 + j * 4 + 1] << 16 |
40
+ bytes[i * 64 + j * 4 + 2] << 8 |
41
+ bytes[i * 64 + j * 4 + 3];
42
+ }
43
+ }
44
+
45
+ M[N - 1][14] = ((bytes.length - 1) * 8) /
46
+ Math.pow(2, 32); M[N - 1][14] = Math.floor(M[N - 1][14]);
47
+ M[N - 1][15] = ((bytes.length - 1) * 8) & 0xffffffff;
48
+
49
+ for (var i=0; i<N; i++) {
50
+ var W = new Array(80);
51
+
52
+ for (var t=0; t<16; t++) W[t] = M[i][t];
53
+ for (var t=16; t<80; t++) {
54
+ W[t] = ROTL(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1);
55
+ }
56
+
57
+ var a = H[0], b = H[1], c = H[2], d = H[3], e = H[4];
58
+
59
+ for (var t=0; t<80; t++) {
60
+ var s = Math.floor(t/20);
61
+ var T = ROTL(a, 5) + f(s, b, c, d) + e + K[s] + W[t] >>> 0;
62
+ e = d;
63
+ d = c;
64
+ c = ROTL(b, 30) >>> 0;
65
+ b = a;
66
+ a = T;
67
+ }
68
+
69
+ H[0] = (H[0] + a) >>> 0;
70
+ H[1] = (H[1] + b) >>> 0;
71
+ H[2] = (H[2] + c) >>> 0;
72
+ H[3] = (H[3] + d) >>> 0;
73
+ H[4] = (H[4] + e) >>> 0;
74
+ }
75
+
76
+ return [
77
+ H[0] >> 24 & 0xff, H[0] >> 16 & 0xff, H[0] >> 8 & 0xff, H[0] & 0xff,
78
+ H[1] >> 24 & 0xff, H[1] >> 16 & 0xff, H[1] >> 8 & 0xff, H[1] & 0xff,
79
+ H[2] >> 24 & 0xff, H[2] >> 16 & 0xff, H[2] >> 8 & 0xff, H[2] & 0xff,
80
+ H[3] >> 24 & 0xff, H[3] >> 16 & 0xff, H[3] >> 8 & 0xff, H[3] & 0xff,
81
+ H[4] >> 24 & 0xff, H[4] >> 16 & 0xff, H[4] >> 8 & 0xff, H[4] & 0xff
82
+ ];
83
+ }
84
+
85
+ module.exports = sha1;
package/lib/sha1.js ADDED
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ var crypto = require('crypto');
4
+
5
+ function sha1(bytes) {
6
+ // support modern Buffer API
7
+ if (typeof Buffer.from === 'function') {
8
+ if (Array.isArray(bytes)) bytes = Buffer.from(bytes);
9
+ else if (typeof bytes === 'string') bytes = Buffer.from(bytes, 'utf8');
10
+ }
11
+
12
+ // support pre-v4 Buffer API
13
+ else {
14
+ if (Array.isArray(bytes)) bytes = new Buffer(bytes);
15
+ else if (typeof bytes === 'string') bytes = new Buffer(bytes, 'utf8');
16
+ }
17
+
18
+ return crypto.createHash('sha1').update(bytes).digest();
19
+ }
20
+
21
+ module.exports = sha1;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "uuid",
3
- "version": "3.0.1",
4
- "description": "RFC4122 (v1 and v4) generator",
3
+ "version": "3.1.0",
4
+ "description": "RFC4122 (v1, v4, and v5) UUIDs",
5
5
  "keywords": [
6
6
  "uuid",
7
7
  "guid",
@@ -18,10 +18,11 @@
18
18
  "test": "mocha test/test.js"
19
19
  },
20
20
  "browser": {
21
- "./lib/rng.js": "./lib/rng-browser.js"
21
+ "./lib/rng.js": "./lib/rng-browser.js",
22
+ "./lib/sha1.js": "./lib/sha1-browser.js"
22
23
  },
23
24
  "repository": {
24
25
  "type": "git",
25
26
  "url": "https://github.com/kelektiv/node-uuid.git"
26
27
  }
27
- }
28
+ }
package/v1.js CHANGED
@@ -1,6 +1,3 @@
1
- // Unique ID creation requires a high quality random # generator. We feature
2
- // detect to determine the best RNG source, normalizing to a function that
3
- // returns 128-bits of randomness, since that's what's usually required
4
1
  var rng = require('./lib/rng');
5
2
  var bytesToUuid = require('./lib/bytesToUuid');
6
3
 
package/v5.js ADDED
@@ -0,0 +1,42 @@
1
+ var sha1 = require('./lib/sha1-browser');
2
+ var bytesToUuid = require('./lib/bytesToUuid');
3
+
4
+ function uuidToBytes(uuid) {
5
+ // Note: We assume we're being passed a valid uuid string
6
+ var bytes = [];
7
+ uuid.replace(/[a-fA-F0-9]{2}/g, function(hex) {
8
+ bytes.push(parseInt(hex, 16));
9
+ });
10
+
11
+ return bytes;
12
+ }
13
+
14
+ function stringToBytes(str) {
15
+ str = unescape(encodeURIComponent(str)); // UTF8 escape
16
+ var bytes = new Array(str.length);
17
+ for (var i = 0; i < str.length; i++) {
18
+ bytes[i] = str.charCodeAt(i);
19
+ }
20
+ return bytes;
21
+ }
22
+
23
+ function v5(name, namespace, buf, offset) {
24
+ if (typeof(name) == 'string') name = stringToBytes(name);
25
+ if (typeof(namespace) == 'string') namespace = uuidToBytes(namespace);
26
+
27
+ if (!Array.isArray(name)) throw TypeError('name must be an array of bytes');
28
+ if (!Array.isArray(namespace) || namespace.length != 16) throw TypeError('namespace must be uuid string or an Array of 16 byte values');
29
+
30
+ // Per 4.3
31
+ var bytes = sha1(namespace.concat(name));
32
+ bytes[6] = (bytes[6] & 0x0f) | 0x50;
33
+ bytes[8] = (bytes[8] & 0x3f) | 0x80;
34
+
35
+ return buf || bytesToUuid(bytes);
36
+ }
37
+
38
+ // Pre-defined namespaces, per Appendix C
39
+ v5.DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
40
+ v5.URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
41
+
42
+ module.exports = v5;
package/.npmignore DELETED
@@ -1,8 +0,0 @@
1
- node_modules
2
- .DS_Store
3
-
4
- # VIM temp files
5
- *.sw*
6
-
7
- # Mac desktop services store
8
- .DS_Store
package/.travis.yml DELETED
@@ -1,5 +0,0 @@
1
- language: node_js
2
- node_js:
3
- - "0.12"
4
- - "4"
5
- - "6"
package/test/mocha.opts DELETED
@@ -1,3 +0,0 @@
1
- --ui qunit
2
- --reporter spec
3
- --check-leaks
package/test/test.js DELETED
@@ -1,96 +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
- });