Skip to content

Commit

Permalink
Closes npmGH-564 Use the MIT node-uuid lib instead of OSLv3 uuid.js.
Browse files Browse the repository at this point in the history
  • Loading branch information
reid authored and isaacs committed Feb 9, 2011
1 parent df8a7d9 commit e297e13
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 35 deletions.
4 changes: 4 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.


This software contains the node-uuid library.
Copyright 2010 Robert Kieffer. Dual licensed under the MIT and GPL licenses.
2 changes: 1 addition & 1 deletion lib/utils/registry/adduser.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function adduser (username, password, email, cb) {
if (password.indexOf(":") !== -1) return cb(new Error(
"Sorry, ':' chars are not allowed in passwords.\n"+
"See <https://issues.apache.org/jira/browse/COUCHDB-969> for why."))
var salt = uuid.generate()
var salt = uuid()
, userobj =
{ name : username
, salt : salt
Expand Down
118 changes: 84 additions & 34 deletions lib/utils/uuid.js
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;
}
})();

0 comments on commit e297e13

Please sign in to comment.