Comparing version 1.3.2 to 1.3.3
@@ -14,20 +14,2 @@ var crypto = require('crypto') | ||
/* eslint-disable-next-line */ | ||
var ATTEMPTS = crypto.randomBytes === crypto.pseudoRandomBytes ? 1 : 3 | ||
function safeRandom (size, attempt, callback) { | ||
random(size, function (err, bytes) { | ||
if (err) { | ||
attempt -= 1 | ||
if (attempt === 0) { | ||
callback(err) | ||
} else { | ||
setTimeout(safeRandom.bind(null, size, attempt, callback), 10) | ||
} | ||
} else { | ||
callback(null, bytes) | ||
} | ||
}) | ||
} | ||
/** | ||
@@ -53,3 +35,3 @@ * Generate secure URL-friendly unique ID. Non-blocking version. | ||
*/ | ||
module.exports = function (size, callback) { | ||
module.exports = function (size, callback, attempt) { | ||
size = size || 21 | ||
@@ -72,14 +54,19 @@ | ||
safeRandom(size, ATTEMPTS, function (error, bytes) { | ||
if (error) { | ||
return callback(error) | ||
random(size, function (err, bytes) { | ||
if (err) { | ||
if (typeof attempt === 'undefined') attempt = 3 | ||
attempt -= 1 | ||
if (attempt === 0) { | ||
callback(err) | ||
} else { | ||
setTimeout(module.exports.bind(null, size, callback, attempt), 10) | ||
} | ||
} else { | ||
var id = '' | ||
while (0 < size--) { | ||
id += url[bytes[size] & 63] | ||
} | ||
callback(null, id) | ||
} | ||
var id = '' | ||
while (0 < size--) { | ||
id += url[bytes[size] & 63] | ||
} | ||
callback(null, id) | ||
}) | ||
} |
# Change Log | ||
This project adheres to [Semantic Versioning](http://semver.org/). | ||
## 1.3.3 | ||
* Fix `nanoid/async` performance regression. | ||
* Fix old Node.js `not seeded` issue in synchronous version too. | ||
## 1.3.2 | ||
@@ -5,0 +9,0 @@ * Fix random generator `not seeded` issue of old Node.js. |
18
index.js
@@ -21,10 +21,24 @@ var random = require('./random') | ||
*/ | ||
module.exports = function (size) { | ||
module.exports = function (size, attempt) { | ||
size = size || 21 | ||
var bytes | ||
try { | ||
bytes = random(size) | ||
} catch (e) { | ||
if (typeof attempt === 'undefined') attempt = 3 | ||
attempt -= 1 | ||
if (attempt === 0) { | ||
throw e | ||
} else { | ||
return module.exports(size, attempt) | ||
} | ||
} | ||
var id = '' | ||
var bytes = random(size) | ||
while (0 < size--) { | ||
id += url[bytes[size] & 63] | ||
} | ||
return id | ||
} |
{ | ||
"name": "nanoid", | ||
"version": "1.3.2", | ||
"version": "1.3.3", | ||
"description": "A tiny (143 bytes), secure URL-friendly unique string ID generator", | ||
@@ -19,3 +19,8 @@ "keywords": [ | ||
"./async/random.js": "./async/random.browser.js" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
} | ||
} |
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
21725
379