diff --git a/benchmarks/utils.mjs b/benchmarks/utils.mjs index 868a3ae..deec7ff 100644 --- a/benchmarks/utils.mjs +++ b/benchmarks/utils.mjs @@ -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' @@ -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 @@ -160,6 +167,7 @@ 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 }) @@ -167,16 +175,18 @@ export async function compareSigning(payload, algorithm, privateKey, publicKey) 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('-------') } @@ -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) } diff --git a/package.json b/package.json index 99cff57..aa5f412 100644 --- a/package.json +++ b/package.json @@ -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",