From 90b34caa00b10a1b6a142d9f3d5da8fccfa9a300 Mon Sep 17 00:00:00 2001 From: Robert Kieffer Date: Thu, 24 Nov 2016 12:19:52 -0800 Subject: [PATCH 01/12] uuid versions into separate modules, per #154 --- .gitignore | 6 +++++ component.json | 5 ++-- index.js | 8 ++++++ lib/bytesToUuid.js | 23 +++++++++++++++++ lib/rng-browser.js | 25 ++++++++++-------- lib/rng.js | 10 +++++--- package.json | 5 ++-- uuid.js => v1.js | 64 ++++------------------------------------------ v4.js | 29 +++++++++++++++++++++ 9 files changed, 97 insertions(+), 78 deletions(-) create mode 100644 index.js create mode 100644 lib/bytesToUuid.js rename uuid.js => v1.js (66%) create mode 100644 v4.js diff --git a/.gitignore b/.gitignore index fd4f2b06..67b88724 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,8 @@ node_modules .DS_Store + +# VIM temp files +*.sw* + +# Mac desktop services store +.DS_Store diff --git a/component.json b/component.json index 56e7bb3e..fbbac4f0 100644 --- a/component.json +++ b/component.json @@ -1,6 +1,6 @@ { "name": "node-uuid", - "repo": "broofa/node-uuid", + "repo": "kelektiv/node-uuid", "description": "Rigorous implementation of RFC4122 (v1 and v4) UUIDs.", "version": "1.4.7", "author": "Robert Kieffer ", @@ -17,9 +17,8 @@ ], "dependencies": {}, "development": {}, - "main": "uuid.js", "scripts": [ - "uuid.js" + "index.js" ], "license": "MIT" } diff --git a/index.js b/index.js new file mode 100644 index 00000000..e96791ab --- /dev/null +++ b/index.js @@ -0,0 +1,8 @@ +var v1 = require('./v1'); +var v4 = require('./v4'); + +var uuid = v4; +uuid.v1 = v1; +uuid.v4 = v4; + +module.exports = uuid; diff --git a/lib/bytesToUuid.js b/lib/bytesToUuid.js new file mode 100644 index 00000000..9ee989cb --- /dev/null +++ b/lib/bytesToUuid.js @@ -0,0 +1,23 @@ +/** + * Convert array of 16 byte values to UUID string format of the form: + * XXXXXXXX-XXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX + */ +var byteToHex = []; +for (var i = 0; i < 256; ++i) { + byteToHex[i] = (i + 0x100).toString(16).substr(1); +} + +function bytesToUuid(buf, offset) { + var i = offset || 0; + var bth = byteToHex; + return bth[buf[i++]] + bth[buf[i++]] + + bth[buf[i++]] + bth[buf[i++]] + '-' + + bth[buf[i++]] + bth[buf[i++]] + '-' + + bth[buf[i++]] + bth[buf[i++]] + '-' + + bth[buf[i++]] + bth[buf[i++]] + '-' + + bth[buf[i++]] + bth[buf[i++]] + + bth[buf[i++]] + bth[buf[i++]] + + bth[buf[i++]] + bth[buf[i++]]; +} + +module.exports = bytesToUuid; diff --git a/lib/rng-browser.js b/lib/rng-browser.js index 82ffaf64..c84f3109 100644 --- a/lib/rng-browser.js +++ b/lib/rng-browser.js @@ -1,15 +1,19 @@ - +// Unique ID creation requires a high quality random # generator. In the +// browser this is a little complicated due to unknown quality of Math.random() +// and inconsistent support for the `crypto` API. We do the best we can via +// feature-detection var rng; var crypto = global.crypto || global.msCrypto; // for IE 11 if (crypto && crypto.getRandomValues) { - // WHATWG crypto-based RNG - http://wiki.whatwg.org/wiki/Crypto - // Moderately fast, high quality - var _rnds8 = new Uint8Array(16); + // WHATWG crypto RNG - http://wiki.whatwg.org/wiki/Crypto + var rnds8 = new Uint8Array(16); rng = function whatwgRNG() { - crypto.getRandomValues(_rnds8); - return _rnds8; + crypto.getRandomValues(rnds8); + return rnds8; }; + + rng.isCrypto = true; } if (!rng) { @@ -17,16 +21,17 @@ if (!rng) { // // If all else fails, use Math.random(). It's fast, but is of unspecified // quality. - var _rnds = new Array(16); + var rnds = new Array(16); rng = function() { for (var i = 0, r; i < 16; i++) { if ((i & 0x03) === 0) r = Math.random() * 0x100000000; - _rnds[i] = r >>> ((i & 0x03) << 3) & 0xff; + rnds[i] = r >>> ((i & 0x03) << 3) & 0xff; } - return _rnds; + return rnds; }; + + rng.isCrypto = false; } module.exports = rng; - diff --git a/lib/rng.js b/lib/rng.js index 3977f798..d82c9268 100644 --- a/lib/rng.js +++ b/lib/rng.js @@ -1,4 +1,8 @@ +// Unique ID creation requires a high quality random # generator. In node.js +// this is prett straight-forward - we use the crypto API. + var rb = require('crypto').randomBytes; -module.exports = function() { - return rb(16); -}; +function rng() {return rb(16);}; +rng.isCrypto = true; + +module.exports = rng; diff --git a/package.json b/package.json index 1a9e6b66..ab0466d7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "uuid", - "version": "3.0.0", + "version": "3.1.0", "description": "RFC4122 (v1 and v4) generator", "keywords": [ "uuid", @@ -8,7 +8,6 @@ "rfc4122" ], "license": "MIT", - "main": "./uuid.js", "bin": { "uuid": "./bin/uuid" }, @@ -25,4 +24,4 @@ "type": "git", "url": "https://github.com/kelektiv/node-uuid.git" } -} \ No newline at end of file +} diff --git a/uuid.js b/v1.js similarity index 66% rename from uuid.js rename to v1.js index a1f28f75..315bd4cd 100644 --- a/uuid.js +++ b/v1.js @@ -1,28 +1,8 @@ // Unique ID creation requires a high quality random # generator. We feature // detect to determine the best RNG source, normalizing to a function that // returns 128-bits of randomness, since that's what's usually required -var _rng = require('./lib/rng'); - -// Maps for number <-> hex string conversion -var _byteToHex = []; -var _hexToByte = {}; -for (var i = 0; i < 256; ++i) { - _byteToHex[i] = (i + 0x100).toString(16).substr(1); - _hexToByte[_byteToHex[i]] = i; -} - -function buff_to_string(buf, offset) { - var i = offset || 0; - var bth = _byteToHex; - return bth[buf[i++]] + bth[buf[i++]] + - bth[buf[i++]] + bth[buf[i++]] + '-' + - bth[buf[i++]] + bth[buf[i++]] + '-' + - bth[buf[i++]] + bth[buf[i++]] + '-' + - bth[buf[i++]] + bth[buf[i++]] + '-' + - bth[buf[i++]] + bth[buf[i++]] + - bth[buf[i++]] + bth[buf[i++]] + - bth[buf[i++]] + bth[buf[i++]]; -} +var rng = require('./lib/rng'); +var bytesToUuid = require('./lib/bytesToUuid'); // **`v1()` - Generate time-based UUID** // @@ -30,7 +10,7 @@ function buff_to_string(buf, offset) { // and http://docs.python.org/library/uuid.html // random #'s we need to init node and clockseq -var _seedBytes = _rng(); +var _seedBytes = rng(); // Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1) var _nodeId = [ @@ -117,41 +97,7 @@ function v1(options, buf, offset) { b[i + n] = node[n]; } - return buf ? buf : buff_to_string(b); + return buf ? buf : bytesToUuid(b); } -// **`v4()` - Generate random UUID** - -// See https://github.com/broofa/node-uuid for API details -function v4(options, buf, offset) { - // Deprecated - 'format' argument, as supported in v1.2 - var i = buf && offset || 0; - - if (typeof(options) == 'string') { - buf = options == 'binary' ? new Array(16) : null; - options = null; - } - options = options || {}; - - var rnds = options.random || (options.rng || _rng)(); - - // Per 4.4, set bits for version and `clock_seq_hi_and_reserved` - rnds[6] = (rnds[6] & 0x0f) | 0x40; - rnds[8] = (rnds[8] & 0x3f) | 0x80; - - // Copy bytes to buffer, if provided - if (buf) { - for (var ii = 0; ii < 16; ++ii) { - buf[i + ii] = rnds[ii]; - } - } - - return buf || buff_to_string(rnds); -} - -// Export public API -var uuid = v4; -uuid.v1 = v1; -uuid.v4 = v4; - -module.exports = uuid; +module.exports = v1; diff --git a/v4.js b/v4.js new file mode 100644 index 00000000..38b6f76a --- /dev/null +++ b/v4.js @@ -0,0 +1,29 @@ +var rng = require('./lib/rng'); +var bytesToUuid = require('./lib/bytesToUuid'); + +function v4(options, buf, offset) { + var i = buf && offset || 0; + + if (typeof(options) == 'string') { + buf = options == 'binary' ? new Array(16) : null; + options = null; + } + options = options || {}; + + var rnds = options.random || (options.rng || rng)(); + + // Per 4.4, set bits for version and `clock_seq_hi_and_reserved` + rnds[6] = (rnds[6] & 0x0f) | 0x40; + rnds[8] = (rnds[8] & 0x3f) | 0x80; + + // Copy bytes to buffer, if provided + if (buf) { + for (var ii = 0; ii < 16; ++ii) { + buf[i + ii] = rnds[ii]; + } + } + + return buf || bytesToUuid(rnds); +} + +module.exports = v4; From 9cd981d879aa68508065aaf8b84860ba1792c04e Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Fri, 25 Nov 2016 15:46:38 -0400 Subject: [PATCH 02/12] Readme: Fix typo (#160) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d1697256..aa956345 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ uuid.v1(); // -> '6c84fb90-12c4-11e1-840d-7b25c5ee775a' ## Quickstart - browser -**Not recommende for production or even semi-production use. Use a bundling tool instead (i.e. webpack, browserify, rollup, etc)** +**Not recommended for production or even semi-production use. Use a bundling tool instead (i.e. webpack, browserify, rollup, etc)** [wzrd.in](https://github.com/jfhbrook/wzrd.in) will serve up any npm module after performing basic bundling and minification. From 7e9445ed10069eef9e96de86fe16c7fd0799699d Mon Sep 17 00:00:00 2001 From: Nathan Hammond Date: Fri, 25 Nov 2016 11:47:17 -0800 Subject: [PATCH 03/12] ci: test against still-supported 0.12. (#158) --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index d1b90db7..8a2c585e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,5 @@ language: node_js node_js: + - "0.12" - "4" - "6" From 1f874bbb48698c20338500841f9261ebc323fa24 Mon Sep 17 00:00:00 2001 From: Roman Shtylman Date: Fri, 25 Nov 2016 11:54:31 -0800 Subject: [PATCH 04/12] delete component.json --- component.json | 25 ------------------------- 1 file changed, 25 deletions(-) delete mode 100644 component.json diff --git a/component.json b/component.json deleted file mode 100644 index 56e7bb3e..00000000 --- a/component.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "name": "node-uuid", - "repo": "broofa/node-uuid", - "description": "Rigorous implementation of RFC4122 (v1 and v4) UUIDs.", - "version": "1.4.7", - "author": "Robert Kieffer ", - "contributors": [ - { - "name": "Christoph Tavan ", - "github": "https://github.com/ctavan" - } - ], - "keywords": [ - "uuid", - "guid", - "rfc4122" - ], - "dependencies": {}, - "development": {}, - "main": "uuid.js", - "scripts": [ - "uuid.js" - ], - "license": "MIT" -} From 08a681a15a0bd9e243894e03cc3a2b7a74a5af91 Mon Sep 17 00:00:00 2001 From: Robert Kieffer Date: Fri, 25 Nov 2016 14:45:07 -0800 Subject: [PATCH 05/12] Updates per @defunctzombie's comments on #159 --- lib/rng-browser.js | 4 ---- lib/rng.js | 6 ++++-- package.json | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/rng-browser.js b/lib/rng-browser.js index c84f3109..88b7dfb6 100644 --- a/lib/rng-browser.js +++ b/lib/rng-browser.js @@ -12,8 +12,6 @@ if (crypto && crypto.getRandomValues) { crypto.getRandomValues(rnds8); return rnds8; }; - - rng.isCrypto = true; } if (!rng) { @@ -30,8 +28,6 @@ if (!rng) { return rnds; }; - - rng.isCrypto = false; } module.exports = rng; diff --git a/lib/rng.js b/lib/rng.js index d82c9268..5624d912 100644 --- a/lib/rng.js +++ b/lib/rng.js @@ -2,7 +2,9 @@ // this is prett straight-forward - we use the crypto API. var rb = require('crypto').randomBytes; -function rng() {return rb(16);}; -rng.isCrypto = true; + +function rng() { + return rb(16); +}; module.exports = rng; diff --git a/package.json b/package.json index ab0466d7..dee68a54 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "uuid", - "version": "3.1.0", + "version": "3.0.0", "description": "RFC4122 (v1 and v4) generator", "keywords": [ "uuid", From 6899c2ac83065775976f0f275ffa49ac6d5edbc8 Mon Sep 17 00:00:00 2001 From: Robert Kieffer Date: Fri, 25 Nov 2016 14:45:57 -0800 Subject: [PATCH 06/12] rm component.json --- component.json | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 component.json diff --git a/component.json b/component.json deleted file mode 100644 index fbbac4f0..00000000 --- a/component.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "name": "node-uuid", - "repo": "kelektiv/node-uuid", - "description": "Rigorous implementation of RFC4122 (v1 and v4) UUIDs.", - "version": "1.4.7", - "author": "Robert Kieffer ", - "contributors": [ - { - "name": "Christoph Tavan ", - "github": "https://github.com/ctavan" - } - ], - "keywords": [ - "uuid", - "guid", - "rfc4122" - ], - "dependencies": {}, - "development": {}, - "scripts": [ - "index.js" - ], - "license": "MIT" -} From 1b41e5a996c59f85d3eda679302f0c2fdc6a1b03 Mon Sep 17 00:00:00 2001 From: Robert Kieffer Date: Sat, 26 Nov 2016 06:38:35 -0800 Subject: [PATCH 07/12] Updated README --- README.md | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index d1697256..e31f0136 100644 --- a/README.md +++ b/README.md @@ -7,32 +7,33 @@ Features: * Generate RFC4122 version 1 or version 4 UUIDs * Runs in node.js and browsers * Cryptographically strong random number generation on supporting platforms -* Small footprint (Want something smaller? [Check this out](https://gist.github.com/982883) out!) +* Small footprint (Want something smaller? [Check this out](https://gist.github.com/982883)!) -## Quickstart - nodejs +## Quickstart - CommonJS (Recommended) ```shell npm install uuid ``` ```javascript -const uuid = require('uuid'); +// Generate a v1 UUID (time-based) +const uuidV1 = require('uuid/v1'); +uuidV1(); // -> '6c84fb90-12c4-11e1-840d-7b25c5ee775a' -// Generate a v4 (random) id -uuid() // -> '110ec58a-a0f2-4ac4-8393-c866d813b8d1' - -// Generate a v1 (time-based) id -uuid.v1(); // -> '6c84fb90-12c4-11e1-840d-7b25c5ee775a' +// Generate a v4 UUID (random) +const uuidV4 = require('uuid/v4'); +uuidV4(); // -> '110ec58a-a0f2-4ac4-8393-c866d813b8d1' ``` -## Quickstart - browser - -**Not recommende for production or even semi-production use. Use a bundling tool instead (i.e. webpack, browserify, rollup, etc)** +## Quickstart - CDN -[wzrd.in](https://github.com/jfhbrook/wzrd.in) will serve up any npm module after performing basic bundling and minification. +`browserify`-ed versions of this module can be included directly via [wzrd.in](https://github.com/jfhbrook/wzrd.in). ```html + +uuid.v1(); // -> v1 UUID +uuid.v4(); // -> v4 UUID ``` ## API From eaccea79340a61c3e5f329a9f65ed4dacd6bb232 Mon Sep 17 00:00:00 2001 From: Robert Kieffer Date: Sat, 26 Nov 2016 06:56:13 -0800 Subject: [PATCH 08/12] update README --- README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e31f0136..143f78a7 100644 --- a/README.md +++ b/README.md @@ -25,17 +25,22 @@ const uuidV4 = require('uuid/v4'); uuidV4(); // -> '110ec58a-a0f2-4ac4-8393-c866d813b8d1' ``` -## Quickstart - CDN +## Quickstart - Pre-packaged (Not recommended) -`browserify`-ed versions of this module can be included directly via [wzrd.in](https://github.com/jfhbrook/wzrd.in). +Browser-ready versions of this module are available via [wzrd.in](https://github.com/jfhbrook/wzrd.in). ```html + ``` +(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.) + + ## API ### uuid(...) From 7bfe355e4b8d30881851ae24d6fa730a4dd43cda Mon Sep 17 00:00:00 2001 From: Robert Kieffer Date: Thu, 24 Nov 2016 12:19:52 -0800 Subject: [PATCH 09/12] uuid versions into separate modules, per #154 --- .gitignore | 6 +++++ index.js | 8 ++++++ lib/bytesToUuid.js | 23 +++++++++++++++++ lib/rng-browser.js | 25 ++++++++++-------- lib/rng.js | 10 +++++--- package.json | 5 ++-- uuid.js => v1.js | 64 ++++------------------------------------------ v4.js | 29 +++++++++++++++++++++ 8 files changed, 95 insertions(+), 75 deletions(-) create mode 100644 index.js create mode 100644 lib/bytesToUuid.js rename uuid.js => v1.js (66%) create mode 100644 v4.js diff --git a/.gitignore b/.gitignore index fd4f2b06..67b88724 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,8 @@ node_modules .DS_Store + +# VIM temp files +*.sw* + +# Mac desktop services store +.DS_Store diff --git a/index.js b/index.js new file mode 100644 index 00000000..e96791ab --- /dev/null +++ b/index.js @@ -0,0 +1,8 @@ +var v1 = require('./v1'); +var v4 = require('./v4'); + +var uuid = v4; +uuid.v1 = v1; +uuid.v4 = v4; + +module.exports = uuid; diff --git a/lib/bytesToUuid.js b/lib/bytesToUuid.js new file mode 100644 index 00000000..9ee989cb --- /dev/null +++ b/lib/bytesToUuid.js @@ -0,0 +1,23 @@ +/** + * Convert array of 16 byte values to UUID string format of the form: + * XXXXXXXX-XXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX + */ +var byteToHex = []; +for (var i = 0; i < 256; ++i) { + byteToHex[i] = (i + 0x100).toString(16).substr(1); +} + +function bytesToUuid(buf, offset) { + var i = offset || 0; + var bth = byteToHex; + return bth[buf[i++]] + bth[buf[i++]] + + bth[buf[i++]] + bth[buf[i++]] + '-' + + bth[buf[i++]] + bth[buf[i++]] + '-' + + bth[buf[i++]] + bth[buf[i++]] + '-' + + bth[buf[i++]] + bth[buf[i++]] + '-' + + bth[buf[i++]] + bth[buf[i++]] + + bth[buf[i++]] + bth[buf[i++]] + + bth[buf[i++]] + bth[buf[i++]]; +} + +module.exports = bytesToUuid; diff --git a/lib/rng-browser.js b/lib/rng-browser.js index 82ffaf64..c84f3109 100644 --- a/lib/rng-browser.js +++ b/lib/rng-browser.js @@ -1,15 +1,19 @@ - +// Unique ID creation requires a high quality random # generator. In the +// browser this is a little complicated due to unknown quality of Math.random() +// and inconsistent support for the `crypto` API. We do the best we can via +// feature-detection var rng; var crypto = global.crypto || global.msCrypto; // for IE 11 if (crypto && crypto.getRandomValues) { - // WHATWG crypto-based RNG - http://wiki.whatwg.org/wiki/Crypto - // Moderately fast, high quality - var _rnds8 = new Uint8Array(16); + // WHATWG crypto RNG - http://wiki.whatwg.org/wiki/Crypto + var rnds8 = new Uint8Array(16); rng = function whatwgRNG() { - crypto.getRandomValues(_rnds8); - return _rnds8; + crypto.getRandomValues(rnds8); + return rnds8; }; + + rng.isCrypto = true; } if (!rng) { @@ -17,16 +21,17 @@ if (!rng) { // // If all else fails, use Math.random(). It's fast, but is of unspecified // quality. - var _rnds = new Array(16); + var rnds = new Array(16); rng = function() { for (var i = 0, r; i < 16; i++) { if ((i & 0x03) === 0) r = Math.random() * 0x100000000; - _rnds[i] = r >>> ((i & 0x03) << 3) & 0xff; + rnds[i] = r >>> ((i & 0x03) << 3) & 0xff; } - return _rnds; + return rnds; }; + + rng.isCrypto = false; } module.exports = rng; - diff --git a/lib/rng.js b/lib/rng.js index 3977f798..d82c9268 100644 --- a/lib/rng.js +++ b/lib/rng.js @@ -1,4 +1,8 @@ +// Unique ID creation requires a high quality random # generator. In node.js +// this is prett straight-forward - we use the crypto API. + var rb = require('crypto').randomBytes; -module.exports = function() { - return rb(16); -}; +function rng() {return rb(16);}; +rng.isCrypto = true; + +module.exports = rng; diff --git a/package.json b/package.json index 1a9e6b66..ab0466d7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "uuid", - "version": "3.0.0", + "version": "3.1.0", "description": "RFC4122 (v1 and v4) generator", "keywords": [ "uuid", @@ -8,7 +8,6 @@ "rfc4122" ], "license": "MIT", - "main": "./uuid.js", "bin": { "uuid": "./bin/uuid" }, @@ -25,4 +24,4 @@ "type": "git", "url": "https://github.com/kelektiv/node-uuid.git" } -} \ No newline at end of file +} diff --git a/uuid.js b/v1.js similarity index 66% rename from uuid.js rename to v1.js index a1f28f75..315bd4cd 100644 --- a/uuid.js +++ b/v1.js @@ -1,28 +1,8 @@ // Unique ID creation requires a high quality random # generator. We feature // detect to determine the best RNG source, normalizing to a function that // returns 128-bits of randomness, since that's what's usually required -var _rng = require('./lib/rng'); - -// Maps for number <-> hex string conversion -var _byteToHex = []; -var _hexToByte = {}; -for (var i = 0; i < 256; ++i) { - _byteToHex[i] = (i + 0x100).toString(16).substr(1); - _hexToByte[_byteToHex[i]] = i; -} - -function buff_to_string(buf, offset) { - var i = offset || 0; - var bth = _byteToHex; - return bth[buf[i++]] + bth[buf[i++]] + - bth[buf[i++]] + bth[buf[i++]] + '-' + - bth[buf[i++]] + bth[buf[i++]] + '-' + - bth[buf[i++]] + bth[buf[i++]] + '-' + - bth[buf[i++]] + bth[buf[i++]] + '-' + - bth[buf[i++]] + bth[buf[i++]] + - bth[buf[i++]] + bth[buf[i++]] + - bth[buf[i++]] + bth[buf[i++]]; -} +var rng = require('./lib/rng'); +var bytesToUuid = require('./lib/bytesToUuid'); // **`v1()` - Generate time-based UUID** // @@ -30,7 +10,7 @@ function buff_to_string(buf, offset) { // and http://docs.python.org/library/uuid.html // random #'s we need to init node and clockseq -var _seedBytes = _rng(); +var _seedBytes = rng(); // Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1) var _nodeId = [ @@ -117,41 +97,7 @@ function v1(options, buf, offset) { b[i + n] = node[n]; } - return buf ? buf : buff_to_string(b); + return buf ? buf : bytesToUuid(b); } -// **`v4()` - Generate random UUID** - -// See https://github.com/broofa/node-uuid for API details -function v4(options, buf, offset) { - // Deprecated - 'format' argument, as supported in v1.2 - var i = buf && offset || 0; - - if (typeof(options) == 'string') { - buf = options == 'binary' ? new Array(16) : null; - options = null; - } - options = options || {}; - - var rnds = options.random || (options.rng || _rng)(); - - // Per 4.4, set bits for version and `clock_seq_hi_and_reserved` - rnds[6] = (rnds[6] & 0x0f) | 0x40; - rnds[8] = (rnds[8] & 0x3f) | 0x80; - - // Copy bytes to buffer, if provided - if (buf) { - for (var ii = 0; ii < 16; ++ii) { - buf[i + ii] = rnds[ii]; - } - } - - return buf || buff_to_string(rnds); -} - -// Export public API -var uuid = v4; -uuid.v1 = v1; -uuid.v4 = v4; - -module.exports = uuid; +module.exports = v1; diff --git a/v4.js b/v4.js new file mode 100644 index 00000000..38b6f76a --- /dev/null +++ b/v4.js @@ -0,0 +1,29 @@ +var rng = require('./lib/rng'); +var bytesToUuid = require('./lib/bytesToUuid'); + +function v4(options, buf, offset) { + var i = buf && offset || 0; + + if (typeof(options) == 'string') { + buf = options == 'binary' ? new Array(16) : null; + options = null; + } + options = options || {}; + + var rnds = options.random || (options.rng || rng)(); + + // Per 4.4, set bits for version and `clock_seq_hi_and_reserved` + rnds[6] = (rnds[6] & 0x0f) | 0x40; + rnds[8] = (rnds[8] & 0x3f) | 0x80; + + // Copy bytes to buffer, if provided + if (buf) { + for (var ii = 0; ii < 16; ++ii) { + buf[i + ii] = rnds[ii]; + } + } + + return buf || bytesToUuid(rnds); +} + +module.exports = v4; From f40fd46d173b45dbc905d79b6b42dd1007f9c566 Mon Sep 17 00:00:00 2001 From: Robert Kieffer Date: Fri, 25 Nov 2016 14:45:07 -0800 Subject: [PATCH 10/12] Updates per @defunctzombie's comments on #159 --- lib/rng-browser.js | 4 ---- lib/rng.js | 6 ++++-- package.json | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/rng-browser.js b/lib/rng-browser.js index c84f3109..88b7dfb6 100644 --- a/lib/rng-browser.js +++ b/lib/rng-browser.js @@ -12,8 +12,6 @@ if (crypto && crypto.getRandomValues) { crypto.getRandomValues(rnds8); return rnds8; }; - - rng.isCrypto = true; } if (!rng) { @@ -30,8 +28,6 @@ if (!rng) { return rnds; }; - - rng.isCrypto = false; } module.exports = rng; diff --git a/lib/rng.js b/lib/rng.js index d82c9268..5624d912 100644 --- a/lib/rng.js +++ b/lib/rng.js @@ -2,7 +2,9 @@ // this is prett straight-forward - we use the crypto API. var rb = require('crypto').randomBytes; -function rng() {return rb(16);}; -rng.isCrypto = true; + +function rng() { + return rb(16); +}; module.exports = rng; diff --git a/package.json b/package.json index ab0466d7..dee68a54 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "uuid", - "version": "3.1.0", + "version": "3.0.0", "description": "RFC4122 (v1 and v4) generator", "keywords": [ "uuid", From 91bbec0e149fcc411686a24d7eb523c86d1b27e0 Mon Sep 17 00:00:00 2001 From: Robert Kieffer Date: Sat, 26 Nov 2016 06:38:35 -0800 Subject: [PATCH 11/12] Updated README --- README.md | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index aa956345..a9466316 100644 --- a/README.md +++ b/README.md @@ -7,32 +7,33 @@ Features: * Generate RFC4122 version 1 or version 4 UUIDs * Runs in node.js and browsers * Cryptographically strong random number generation on supporting platforms -* Small footprint (Want something smaller? [Check this out](https://gist.github.com/982883) out!) +* Small footprint (Want something smaller? [Check this out](https://gist.github.com/982883)!) -## Quickstart - nodejs +## Quickstart - CommonJS (Recommended) ```shell npm install uuid ``` ```javascript -const uuid = require('uuid'); +// Generate a v1 UUID (time-based) +const uuidV1 = require('uuid/v1'); +uuidV1(); // -> '6c84fb90-12c4-11e1-840d-7b25c5ee775a' -// Generate a v4 (random) id -uuid() // -> '110ec58a-a0f2-4ac4-8393-c866d813b8d1' - -// Generate a v1 (time-based) id -uuid.v1(); // -> '6c84fb90-12c4-11e1-840d-7b25c5ee775a' +// Generate a v4 UUID (random) +const uuidV4 = require('uuid/v4'); +uuidV4(); // -> '110ec58a-a0f2-4ac4-8393-c866d813b8d1' ``` -## Quickstart - browser - -**Not recommended for production or even semi-production use. Use a bundling tool instead (i.e. webpack, browserify, rollup, etc)** +## Quickstart - Pre-packaged for browsers (Not recommended) -[wzrd.in](https://github.com/jfhbrook/wzrd.in) will serve up any npm module after performing basic bundling and minification. +`browserify`-ed versions of this module can be included directly via [wzrd.in](https://github.com/jfhbrook/wzrd.in). ```html + +uuid.v1(); // -> v1 UUID +uuid.v4(); // -> v4 UUID ``` ## API From 56999af7d8b1b02d3ecad9309175697b9ce78034 Mon Sep 17 00:00:00 2001 From: Robert Kieffer Date: Sat, 26 Nov 2016 06:56:13 -0800 Subject: [PATCH 12/12] update README --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a9466316..82a79eb3 100644 --- a/README.md +++ b/README.md @@ -27,15 +27,20 @@ uuidV4(); // -> '110ec58a-a0f2-4ac4-8393-c866d813b8d1' ## Quickstart - Pre-packaged for browsers (Not recommended) -`browserify`-ed versions of this module can be included directly via [wzrd.in](https://github.com/jfhbrook/wzrd.in). +Browser-ready versions of this module are available via [wzrd.in](https://github.com/jfhbrook/wzrd.in). ```html + ``` +(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.) + + ## API ### uuid(...)