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

Update to support Node 14+ #36

Merged
merged 1 commit into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
root: true
extends: standard
extends:
- standard
- plugin:markdown/recommended
113 changes: 0 additions & 113 deletions HISTORY.md

This file was deleted.

8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ information for TypeScript projects.
<!-- eslint-disable no-unused-vars -->

```js
var Tokens = require('@fastify/csrf')
const Tokens = require('@fastify/csrf')
```

### new Tokens([options])
Expand Down Expand Up @@ -75,8 +75,8 @@ expect the user's browser to provide back.
<!-- eslint-disable no-undef, no-unused-vars -->

```js
var secret = tokens.secretSync()
var token = tokens.create(secret)
const secret = tokens.secretSync()
const token = tokens.create(secret)
```

The `userInfo` parameter can be used to protect against cookie tossing
Expand Down Expand Up @@ -124,7 +124,7 @@ A synchronous version of `tokens.secret(callback)`. Please see
<!-- eslint-disable no-undef, no-unused-vars -->

```js
var secret = tokens.secretSync()
const secret = tokens.secretSync()
```

#### tokens.verify(secret, token[, userInfo])
Expand Down
8 changes: 4 additions & 4 deletions benchmark/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* Module dependencies.
*/

var benchmark = require('benchmark')
var benchmarks = require('beautify-benchmark')
var Tokens = require('..')
const benchmark = require('benchmark')
const benchmarks = require('beautify-benchmark')
const Tokens = require('..')

/**
* Globals for benchmark.js
Expand All @@ -14,7 +14,7 @@ var Tokens = require('..')
global.tokens = new Tokens()
global.secret = global.tokens.secretSync()

var suite = new benchmark.Suite()
const suite = new benchmark.Suite()

suite.add({
name: 'create',
Expand Down
18 changes: 9 additions & 9 deletions benchmark/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var fs = require('fs')
var path = require('path')
var spawn = require('child_process').spawn
const fs = require('fs')
const path = require('path')
const spawn = require('child_process').spawn

var exe = process.argv[0]
var cwd = process.cwd()
const exe = process.argv[0]
const cwd = process.cwd()

for (var dep in process.versions) {
for (const dep in process.versions) {
console.log(' %s@%s', dep, process.versions[dep])
}

Expand All @@ -14,17 +14,17 @@ console.log('')
runScripts(fs.readdirSync(__dirname))

function runScripts (fileNames) {
var fileName = fileNames.shift()
const fileName = fileNames.shift()

if (!fileName) return
if (!/\.js$/i.test(fileName)) return runScripts(fileNames)
if (fileName.toLowerCase() === 'index.js') return runScripts(fileNames)

var fullPath = path.join(__dirname, fileName)
const fullPath = path.join(__dirname, fileName)

console.log('> %s %s', exe, path.relative(cwd, fullPath))

var proc = spawn(exe, [fullPath], {
const proc = spawn(exe, [fullPath], {
stdio: 'inherit'
})

Expand Down
8 changes: 4 additions & 4 deletions benchmark/secret.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
* Module dependencies.
*/

var benchmark = require('benchmark')
var benchmarks = require('beautify-benchmark')
var Tokens = require('..')
const benchmark = require('benchmark')
const benchmarks = require('beautify-benchmark')
const Tokens = require('..')

/**
* Globals for benchmark.js
*/

global.tokens = new Tokens()

var suite = new benchmark.Suite()
const suite = new benchmark.Suite()

suite.add({
name: 'secretSync',
Expand Down
8 changes: 4 additions & 4 deletions benchmark/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* Module dependencies.
*/

var benchmark = require('benchmark')
var benchmarks = require('beautify-benchmark')
var Tokens = require('..')
const benchmark = require('benchmark')
const benchmarks = require('beautify-benchmark')
const Tokens = require('..')

/**
* Globals for benchmark.js
Expand All @@ -14,7 +14,7 @@ var Tokens = require('..')
global.tokens = new Tokens()
global.secret = global.tokens.secretSync()

var suite = new benchmark.Suite()
const suite = new benchmark.Suite()

suite.add({
name: 'verify - valid',
Expand Down
42 changes: 21 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* csrf
* Copyright(c) 2014 Jonathan Ong
* Copyright(c) 2015 Douglas Christopher Wilson
* Copyright(c) 2021-2022 Fastify Collaborators
* MIT Licensed
*/

Expand All @@ -12,20 +13,20 @@
* @private
*/

var rndm = require('rndm')
var uid = require('uid-safe')
var compare = require('tsscmp')
var crypto = require('crypto')
const rndm = require('rndm')
const uid = require('uid-safe')
const compare = require('tsscmp')
const crypto = require('crypto')

/**
* Module variables.
* @private
*/

var EQUAL_GLOBAL_REGEXP = /=/g
var PLUS_GLOBAL_REGEXP = /\+/g
var SLASH_GLOBAL_REGEXP = /\//g
var MINUS_GLOBAL_REGEXP = /-/g
const EQUAL_GLOBAL_REGEXP = /=/g
const PLUS_GLOBAL_REGEXP = /\+/g
const SLASH_GLOBAL_REGEXP = /\//g
const MINUS_GLOBAL_REGEXP = /-/g

/**
* Module exports.
Expand All @@ -50,33 +51,33 @@ function Tokens (options) {
return new Tokens(options)
}

var opts = options || {}
const opts = options || {}

var saltLength = opts.saltLength !== undefined
const saltLength = opts.saltLength !== undefined
? opts.saltLength
: 8

if (typeof saltLength !== 'number' || !isFinite(saltLength) || saltLength < 1) {
throw new TypeError('option saltLength must be finite number > 1')
}

var secretLength = opts.secretLength !== undefined
const secretLength = opts.secretLength !== undefined
? opts.secretLength
: 18

if (typeof secretLength !== 'number' || !isFinite(secretLength) || secretLength < 1) {
throw new TypeError('option secretLength must be finite number > 1')
}

var validity = opts.validity !== undefined
const validity = opts.validity !== undefined
? opts.validity
: 0

if (typeof validity !== 'number' || !isFinite(validity) || validity < 0) {
throw new TypeError('option validity must be finite number > 0')
}

var userInfo = opts.userInfo !== undefined
const userInfo = opts.userInfo !== undefined
? opts.userInfo
: false

Expand All @@ -101,7 +102,7 @@ Tokens.prototype.create = function create (secret, userInfo) {
if (!secret || typeof secret !== 'string') {
throw new TypeError('argument secret is required')
}
var date = this.validity > 0 ? Date.now() : null
const date = this.validity > 0 ? Date.now() : null

if (this.userInfo) {
if (typeof userInfo !== 'string') {
Expand Down Expand Up @@ -138,7 +139,7 @@ Tokens.prototype.secretSync = function secretSync () {
*/

Tokens.prototype._tokenize = function tokenize (secret, salt, date, userInfo) {
var toHash = ''
let toHash = ''

if (date !== null) {
toHash += date.toString(36) + '-'
Expand Down Expand Up @@ -173,10 +174,9 @@ Tokens.prototype.verify = function verify (secret, token, userInfo) {
return false
}

var index = token.indexOf('-')
var toCompare = token
var date = null
var userInfo
let index = token.indexOf('-')
const toCompare = token
let date = null

if (index === -1) {
return false
Expand Down Expand Up @@ -214,8 +214,8 @@ Tokens.prototype.verify = function verify (secret, token, userInfo) {
}
}

var salt = token.substr(0, index)
var expected = this._tokenize(secret, salt, date, userInfo)
const salt = token.substr(0, index)
const expected = this._tokenize(secret, salt, date, userInfo)

return compare(toCompare, expected)
}
Expand Down
10 changes: 3 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@
"devDependencies": {
"beautify-benchmark": "^0.2.4",
"benchmark": "^2.1.4",
"bluebird": "^3.7.2",
"eslint": "^7.17.0",
"eslint-config-standard": "^16.0.2",
"eslint": "^8.0.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-markdown": "^2.0.0",
"eslint-plugin-node": "^11.0.0",
"eslint-plugin-promise": "^5.1.0",
"eslint-plugin-promise": "^6.0.0",
"mocha": "^10.0.0",
"nyc": "^15.0.0"
},
Expand All @@ -41,9 +40,6 @@
"index.d.ts",
"index.js"
],
"engines": {
"node": ">= 10.0"
},
"scripts": {
"bench": "node benchmark/index.js",
"lint": "eslint --plugin markdown --ext js,md .",
Expand Down
Loading