Comparing version 0.0.2 to 0.0.3
26
index.js
@@ -8,9 +8,9 @@ /** | ||
/** | ||
* The size ratio between a base64 string and the equivalent byte buffer | ||
* 62 characters in the ascii range that can be used in URLs without special | ||
* encoding. | ||
*/ | ||
var UIDCHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
var ratio = Math.log(64) / Math.log(256); | ||
/** | ||
* Make a Base64 string ready for use in URLs | ||
* Make a Buffer into a string ready for use in URLs | ||
* | ||
@@ -21,5 +21,11 @@ * @param {String} | ||
*/ | ||
function tostr(bytes) { | ||
var chars, r, i; | ||
function urlReady(str) { | ||
return str.replace(/\+/g, '_').replace(/\//g, '-'); | ||
r = []; | ||
for (i = 0; i < bytes.length; i++) { | ||
r.push(UIDCHARS[bytes[i] % UIDCHARS.length]); | ||
} | ||
return r.join(''); | ||
} | ||
@@ -36,9 +42,9 @@ | ||
function uid(length, cb) { | ||
var numbytes = Math.ceil(length * ratio); | ||
if (typeof cb === 'undefined') { | ||
return urlReady(crypto.randomBytes(numbytes).toString('base64').slice(0, length)); | ||
return tostr(crypto.pseudoRandomBytes(length)); | ||
} else { | ||
crypto.randomBytes(numbytes, function(err, bytes) { | ||
crypto.pseudoRandomBytes(length, function(err, bytes) { | ||
if (err) return cb(err); | ||
cb(null, urlReady(bytes.toString('base64').slice(0, length))); | ||
cb(null, tostr(bytes)); | ||
}) | ||
@@ -45,0 +51,0 @@ } |
@@ -5,5 +5,5 @@ { | ||
"tags": ["uid"], | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"dependencies": { | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
2217
3
0
45