Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up token generation #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions index.js
Expand Up @@ -13,9 +13,10 @@
*/

var rndm = require('rndm')
var uid = require('uid-safe')
var compare = require('tsscmp')
var crypto = require('crypto')
var nanoid = require('nanoid')
var nanoidAsync = require('nanoid/async')

/**
* Module variables.
Expand Down Expand Up @@ -92,7 +93,17 @@ Tokens.prototype.create = function create (secret) {
*/

Tokens.prototype.secret = function secret (callback) {
return uid(this.secretLength, callback)
// validate callback is a function, if provided
if (callback !== undefined && typeof callback !== 'function') {
throw new TypeError('argument callback must be a function')
}

// require the callback without promises
if (!callback && !global.Promise) {
throw new TypeError('argument callback is required')
}

return nanoidAsync(Math.floor(this.secretLength * 8 / 5), callback)
}

/**
Expand All @@ -101,7 +112,7 @@ Tokens.prototype.secret = function secret (callback) {
*/

Tokens.prototype.secretSync = function secretSync () {
return uid.sync(this.secretLength)
return nanoid(Math.floor(this.secretLength * 8 / 5))
}

/**
Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -9,9 +9,9 @@
"license": "MIT",
"repository": "pillarjs/csrf",
"dependencies": {
"nanoid": "2.0.0",
"rndm": "1.2.0",
"tsscmp": "1.0.6",
"uid-safe": "2.1.5"
"tsscmp": "1.0.6"
},
"devDependencies": {
"beautify-benchmark": "0.2.4",
Expand Down