-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify.mjs
95 lines (86 loc) · 2.14 KB
/
verify.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { JwtClient, JwtCacheClient } from '../index.js';
import bench from 'benchmark';
import chalk from 'chalk';
import fastJwt from 'fast-jwt';
import * as jose from 'jose';
import jwt from 'jsonwebtoken';
const suite = new bench.Suite('Verify Token');
const secret = 'somelongsecretasdbnakwfbjawf';
const minSamples = 100;
const encodedKey = new TextEncoder().encode(secret);
const payload = { userId: 'abc123' };
const expires_in = 60000;
const client = new JwtClient(secret);
const cacheClient = new JwtCacheClient(secret, expires_in, 2);
const joseSign = async (payload) => {
const s = new jose.SignJWT(payload);
return s.setProtectedHeader({ alg: 'HS256' }).sign(encodedKey);
// const key = jose.JWK.asKey(secret);
// return await jose.JWT.sign(payload, key);
};
const joseSigned = await joseSign(payload);
const jwtSigned = jwt.sign(payload, secret);
const signer = fastJwt.createSigner({ key: secret });
const fastJwtVerify = fastJwt.createVerifier({ key: secret, cache: false });
const fastJwtCacheVerify = fastJwt.createVerifier({
key: secret,
cache: true,
cacheTTL: expires_in,
});
const fastJwtSigned = signer(payload);
const ctJwtSigned = client.sign(payload, expires_in);
suite
.add(
'jsonwebtoken',
() => {
jwt.verify(jwtSigned, secret);
},
{ minSamples },
)
.add(
'jose',
async (deferred) => {
await jose.jwtVerify(joseSigned, encodedKey);
deferred.resolve();
},
{ defer: true, minSamples },
)
.add(
'fast-jwt',
() => {
fastJwtVerify(fastJwtSigned);
},
{ minSamples },
)
.add(
'fast-jwt#withCache',
() => {
fastJwtCacheVerify(fastJwtSigned);
},
{ minSamples },
)
.add(
'@carbonteq/jwt',
() => {
client.verify(ctJwtSigned);
},
{ minSamples },
)
.add(
'@carbonteq/jwt#withCache',
() => {
cacheClient.verify(ctJwtSigned);
},
{ minSamples },
)
.on('cycle', function (e) {
console.log(String(e.target));
})
.on('complete', function () {
console.log(
`\nSUITE <${this.name}>: Fastest is ${chalk.green(
this.filter('fastest').map('name'),
)}`,
);
})
.run();