Skip to content

Commit 0d4821e

Browse files
authored
perf: use optional chaining (#359)
1 parent c318c86 commit 0d4821e

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

jwt.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,14 @@ function validateOptions (options) {
6767
assert(!options.jwtSign || isString(options.jwtSign), 'Invalid options.jwtSign')
6868

6969
if (
70-
options.sign &&
71-
options.sign.algorithm &&
72-
options.sign.algorithm.includes('RS') &&
70+
options.sign?.algorithm?.includes('RS') &&
7371
(typeof options.secret === 'string' ||
7472
options.secret instanceof Buffer)
7573
) {
7674
throw new Error('RSA Signatures set as Algorithm in the options require a private and public key to be set as the secret')
7775
}
7876
if (
79-
options.sign &&
80-
options.sign.algorithm &&
81-
options.sign.algorithm.includes('ES') &&
77+
options.sign?.algorithm?.includes('ES') &&
8278
(typeof options.secret === 'string' ||
8379
options.secret instanceof Buffer)
8480
) {
@@ -509,7 +505,7 @@ function fastifyJwt (fastify, options, next) {
509505
} else {
510506
const maybePromise = trusted(request, result)
511507

512-
if (maybePromise && maybePromise.then) {
508+
if (maybePromise?.then) {
513509
maybePromise
514510
.then(trusted => trusted ? callback(null, result) : callback(new AuthorizationTokenUntrustedError()))
515511
} else if (maybePromise) {

test/options.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ test('Options validation', function (t) {
7575
secret: 'sec',
7676
jwtSign: {}
7777
}).ready((error) => {
78-
t.equal(error && error.message, 'Invalid options.jwtSign')
78+
t.equal(error?.message, 'Invalid options.jwtSign')
7979
})
8080
})
8181

@@ -111,7 +111,7 @@ test('Options validation', function (t) {
111111
sub: 'Some subject'
112112
}
113113
}).ready(function (error) {
114-
t.equal(error && error.message, 'RSA Signatures set as Algorithm in the options require a private and public key to be set as the secret')
114+
t.equal(error?.message, 'RSA Signatures set as Algorithm in the options require a private and public key to be set as the secret')
115115
})
116116
})
117117

@@ -127,7 +127,7 @@ test('Options validation', function (t) {
127127
sub: 'Some subject'
128128
}
129129
}).ready(function (error) {
130-
t.equal(error && error.message, 'ECDSA Signatures set as Algorithm in the options require a private and public key to be set as the secret')
130+
t.equal(error?.message, 'ECDSA Signatures set as Algorithm in the options require a private and public key to be set as the secret')
131131
})
132132
})
133133
})

0 commit comments

Comments
 (0)