forked from npm/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes npmGH-564 Use the MIT node-uuid lib instead of OSLv3 uuid.js.
- Loading branch information
Showing
3 changed files
with
89 additions
and
35 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
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,34 +1,84 @@ | ||
/* randomUUID.js - Version 1.0 | ||
* | ||
* Copyright 2008, Robert Kieffer | ||
* | ||
* This software is made available under the terms of the Open Software License | ||
* v3.0 (available here: http://www.opensource.org/licenses/osl-3.0.php ) | ||
* | ||
* The latest version of this file can be found at: | ||
* http://www.broofa.com/Tools/randomUUID.js | ||
* | ||
* For more information, or to comment on this, please go to: | ||
* http://www.broofa.com/blog/?p=151 | ||
*/ | ||
|
||
/** | ||
* Create and return a "version 4" RFC-4122 UUID string. | ||
*/ | ||
|
||
function randomUUID() { | ||
var s = [], itoh = '0123456789ABCDEF'; | ||
// Make array of random hex digits. The UUID only has 32 digits in it, but we | ||
// allocate an extra items to make room for the '-'s we'll be inserting. | ||
for (var i = 0; i <36; i++) s[i] = Math.floor(Math.random()*0x10); | ||
// Conform to RFC-4122, section 4.4 | ||
s[14] = 4; // Set 4 high bits of time_high field to version | ||
s[19] = (s[19] & 0x3) | 0x8; // Specify 2 high bits of clock sequence | ||
// Convert to hex chars | ||
for (var i = 0; i <36; i++) s[i] = itoh[s[i]]; | ||
// Insert '-'s | ||
s[8] = s[13] = s[18] = s[23] = '-'; | ||
return s.join(''); | ||
} | ||
|
||
exports.generate = function () {return randomUUID();} | ||
(function() { | ||
/* | ||
* Generate a RFC4122(v4) UUID | ||
* | ||
* Documentation at https://github.com/broofa/node-uuid | ||
* | ||
* Copyright 2010 Robert Kieffer | ||
* Dual licensed under the MIT and GPL licenses. | ||
*/ | ||
|
||
// Use node.js Buffer class if available, otherwise use the Array class | ||
var BufferClass = typeof(Buffer) == 'function' ? Buffer : Array; | ||
|
||
// Buffer used for generating string uuids | ||
var _buf = new BufferClass(16); | ||
|
||
// Cache number <-> hex string for octet values | ||
var toString = []; | ||
var toNumber = {}; | ||
for (var i = 0; i < 256; i++) { | ||
toString[i] = (i + 0x100).toString(16).substr(1).toUpperCase(); | ||
toNumber[toString[i]] = i; | ||
} | ||
|
||
function parse(s) { | ||
var buf = new BufferClass(16); | ||
var i = 0, ton = toNumber; | ||
s.toUpperCase().replace(/[0-9A-F][0-9A-F]/g, function(octet) { | ||
buf[i++] = toNumber[octet]; | ||
}); | ||
return buf; | ||
} | ||
|
||
function unparse(buf) { | ||
var tos = toString, b = buf; | ||
return tos[b[0]] + tos[b[1]] + tos[b[2]] + tos[b[3]] + '-' + | ||
tos[b[4]] + tos[b[5]] + '-' + | ||
tos[b[6]] + tos[b[7]] + '-' + | ||
tos[b[8]] + tos[b[9]] + '-' + | ||
tos[b[10]] + tos[b[11]] + tos[b[12]] + | ||
tos[b[13]] + tos[b[14]] + tos[b[15]]; | ||
} | ||
|
||
function uuid(fmt, buf, offset) { | ||
var b32 = 0x100000000, ff = 0xff; | ||
|
||
var b = fmt != 'binary' ? _buf : (buf ? buf : new BufferClass(16)); | ||
var i = buf && offset || 0; | ||
|
||
r = Math.random()*b32; | ||
b[i++] = r & ff; | ||
b[i++] = (r=r>>>8) & ff; | ||
b[i++] = (r=r>>>8) & ff; | ||
b[i++] = (r=r>>>8) & ff; | ||
r = Math.random()*b32; | ||
b[i++] = r & ff; | ||
b[i++] = (r=r>>>8) & ff; | ||
b[i++] = (r=r>>>8) & 0x0f | 0x40; // See RFC4122 sect. 4.1.3 | ||
b[i++] = (r=r>>>8) & ff; | ||
r = Math.random()*b32; | ||
b[i++] = r & 0x3f | 0x80; // See RFC4122 sect. 4.4 | ||
b[i++] = (r=r>>>8) & ff; | ||
b[i++] = (r=r>>>8) & ff; | ||
b[i++] = (r=r>>>8) & ff; | ||
r = Math.random()*b32; | ||
b[i++] = r & ff; | ||
b[i++] = (r=r>>>8) & ff; | ||
b[i++] = (r=r>>>8) & ff; | ||
b[i++] = (r=r>>>8) & ff; | ||
|
||
return fmt === undefined ? unparse(b) : b; | ||
}; | ||
|
||
uuid.parse = parse; | ||
uuid.unparse = unparse; | ||
uuid.BufferClass = BufferClass; | ||
|
||
if (typeof(module) != 'undefined') { | ||
module.exports = uuid; | ||
} else { | ||
// In browser? Set as top-level function | ||
this.uuid = uuid; | ||
} | ||
})(); |