Skip to content

Commit

Permalink
feat: additional tool in sign benchmark (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilteoood authored May 22, 2023
1 parent beafcbd commit 77d44e2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
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

0 comments on commit 77d44e2

Please sign in to comment.