-
-
Notifications
You must be signed in to change notification settings - Fork 905
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
uuid versions into separate modules, per #154
- Loading branch information
Showing
9 changed files
with
108 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
node_modules | ||
.DS_Store | ||
|
||
# VIM temp files | ||
*.sw* | ||
|
||
# Mac desktop services store | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
var v1 = require('./v1'); | ||
var v4 = require('./v4'); | ||
|
||
var uuid = v4; | ||
uuid.v1 = v1; | ||
uuid.v4 = v4; | ||
|
||
module.exports = uuid; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
// 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() { | ||
|
||
function rng() { | ||
return rb(16); | ||
}; | ||
|
||
module.exports = rng; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |