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

feat: additional tool in sign benchmark #338

Merged
merged 1 commit into from
May 22, 2023
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
39 changes: 29 additions & 10 deletions benchmarks/utils.mjs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
'use strict'

import nodeRsJwt, { Algorithm } from '@node-rs/jsonwebtoken'
import cronometro from 'cronometro'
import { readFileSync } from 'fs'
import { mkdir, writeFile } from 'fs/promises'
import { resolve, dirname } from 'path'
import { dirname, resolve } from 'path'
import { fileURLToPath } from 'url'

import jwt from 'jsonwebtoken'

import {
JWT as JWTJose,
JWK as JWKJose
JWK as JWKJose,
JWT as JWTJose
} from 'jose'

import {
createSigner,
createDecoder,
createSigner,
createVerifier
} from '../src/index.js'

Expand All @@ -27,6 +28,12 @@ const {
verify: jsonwebtokenVerify
} = jwt

const {
sign: nodeRsSign,
signSync: nodeRsSignSync,
verifySync: nodeRsVerifySync
} = nodeRsJwt

const { sign: joseSign, verify: joseVerify, decode: joseDecode } = JWTJose
const { asKey } = JWKJose

Expand Down Expand Up @@ -160,23 +167,26 @@ export async function compareSigning(payload, algorithm, privateKey, publicKey)
if ((process.env.NODE_DEBUG || '').includes('fast-jwt')) {
const fastjwtGenerated = fastjwtSign(payload)
const joseGenerated = joseSign(payload, josePrivateKey, joseOptions)
const nodeRsGenerated = nodeRsSignSync({ data: payload, exp: Date.now() }, privateKey, { algorithm: Algorithm[algorithm.toUpperCase()] })
const jsonwebtokenGenerated = isEdDSA
? null
: jsonwebtokenSign(payload, privateKey, { algorithm, noTimestamp: true })

log('-------')
log(`Generated ${algorithm} tokens:`)
if (!isEdDSA) {
log(` jsonwebtoken: ${JSON.stringify(jsonwebtokenGenerated)}`)
log(` jsonwebtoken: ${JSON.stringify(jsonwebtokenGenerated)}`)
}
log(` jose: ${JSON.stringify(joseGenerated)}`)
log(` fastjwt: ${JSON.stringify(fastjwtSign(payload))}`)
log(` jose: ${JSON.stringify(joseGenerated)}`)
log(` fastjwt: ${JSON.stringify(fastjwtGenerated)}`)
log(`@node-rs/jsonwebtoken: ${JSON.stringify(nodeRsGenerated)}`)
log('Generated tokens verification:')
if (!isEdDSA) {
log(` jsonwebtoken: ${JSON.stringify(jsonwebtokenVerify(jsonwebtokenGenerated, publicKey))}`)
log(` jsonwebtoken: ${JSON.stringify(jsonwebtokenVerify(jsonwebtokenGenerated, publicKey))}`)
}
log(` jose: ${JSON.stringify(joseVerify(joseGenerated, asKey(publicKey)))}`)
log(` fastjwt: ${JSON.stringify(fastjwtVerify(fastjwtGenerated))}`)
log(` jose: ${JSON.stringify(joseVerify(joseGenerated, asKey(publicKey)))}`)
log(` fastjwt: ${JSON.stringify(fastjwtVerify(fastjwtGenerated))}`)
log(`@node-rs/jsonwebtoken: ${JSON.stringify(nodeRsVerifySync(nodeRsGenerated, publicKey, { algorithms: [Algorithm[algorithm.toUpperCase()]] }))}`)
log('-------')
}

Expand Down Expand Up @@ -206,6 +216,15 @@ export async function compareSigning(payload, algorithm, privateKey, publicKey)
}
})

Object.assign(tests, {
[`${algorithm} - @node-rs/jsonwebtoken (sync)`]: function() {
nodeRsSignSync({ data: payload }, privateKey, { algorithm: Algorithm[algorithm.toUpperCase()] })
},
[`${algorithm} - @node-rs/jsonwebtoken (async)`]: function(done) {
nodeRsSign({ data: payload }, privateKey, { algorithm: Algorithm[algorithm.toUpperCase()] }).then(() => done())
}
})

return cronometro(tests, cronometroOptions)
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"mnemonist": "^0.39.5"
},
"devDependencies": {
"@node-rs/jsonwebtoken": "^0.2.0",
"@sinonjs/fake-timers": "^10.0.2",
"@types/node": "^20.0.0",
"@typescript-eslint/eslint-plugin": "^5.49.0",
Expand Down