Skip to content

Commit

Permalink
perf: use optional chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs committed Jan 7, 2025
1 parent c318c86 commit 75e6b48
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
10 changes: 3 additions & 7 deletions jwt.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,14 @@ function validateOptions (options) {
assert(!options.jwtSign || isString(options.jwtSign), 'Invalid options.jwtSign')

if (
options.sign &&
options.sign.algorithm &&
options.sign.algorithm.includes('RS') &&
options.sign?.algorithm?.includes('RS') &&
(typeof options.secret === 'string' ||
options.secret instanceof Buffer)
) {
throw new Error('RSA Signatures set as Algorithm in the options require a private and public key to be set as the secret')
}
if (
options.sign &&
options.sign.algorithm &&
options.sign.algorithm.includes('ES') &&
options.sign?.algorithm?.includes('ES') &&
(typeof options.secret === 'string' ||
options.secret instanceof Buffer)
) {
Expand Down Expand Up @@ -509,7 +505,7 @@ function fastifyJwt (fastify, options, next) {
} else {
const maybePromise = trusted(request, result)

if (maybePromise && maybePromise.then) {
if (maybePromise?.then) {
maybePromise
.then(trusted => trusted ? callback(null, result) : callback(new AuthorizationTokenUntrustedError()))
} else if (maybePromise) {
Expand Down
6 changes: 3 additions & 3 deletions test/options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ test('Options validation', function (t) {
secret: 'sec',
jwtSign: {}
}).ready((error) => {
t.equal(error && error.message, 'Invalid options.jwtSign')
t.equal(error?.message, 'Invalid options.jwtSign')
})
})

Expand Down Expand Up @@ -111,7 +111,7 @@ test('Options validation', function (t) {
sub: 'Some subject'
}
}).ready(function (error) {
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')
t.equal(error?.message, 'RSA Signatures set as Algorithm in the options require a private and public key to be set as the secret')
})
})

Expand All @@ -127,7 +127,7 @@ test('Options validation', function (t) {
sub: 'Some subject'
}
}).ready(function (error) {
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')
t.equal(error?.message, 'ECDSA Signatures set as Algorithm in the options require a private and public key to be set as the secret')
})
})
})
Expand Down

0 comments on commit 75e6b48

Please sign in to comment.