Skip to content

perf: reuse global/cached signers & verifiers, fix callback signing with dynamic secret, and fast-path static request verification #438

Open

Description

@TalhaHunter101

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

Issue

Several hot-path regressions and overhead sources exist in reply.jwtSign() and request.jwtVerify():

  1. Promise-mode reply.jwtSign(payload) bypasses the precompiled global signer:
    When replySign(payload, options, next) converts omitted options for its Promise wrapper, it sets options = {} before calling reply[jwtSignName](payload, options, cb). Because {} is truthy, useLocalSigner stays true, causing every await reply.jwtSign(payload) call to go through steed.waterfall and recompile createSigner(...) (including createPrivateKey / detectPrivateKeyAlgorithm for RSA/ECDSA PEM keys) on every single request instead of reusing the startup signer.

  2. Callback-mode reply.jwtSign(payload, cb) throws TypeError: cb is not a function when secret is a dynamic function:
    When secret is a function (e.g. (_req, _payload, cb) => cb(null, key) or async () => key), reply.jwtSign(payload, cb) sets useLocalSigner = false and calls the global signer(payload), whose isAsync key fetcher invokes secretCallbackSign with (data, fastJwtCb) instead of (request, payload, cb).

  3. Dynamic signer/verifier caching gaps:

    • reply.jwtSign() has no dynamic signer cache (unlike validatorCache in getVerifier).
    • validatorCache only caches typeof options.key === 'string', skipping Buffer keys, and is unbounded (Map with no eviction limit).
  4. request.jwtVerify(cb) & static-key request.jwtVerify() overhead:

    • In requestVerify, const useGlobalOptions = !options is evaluated before if (typeof options === 'function'), so callback calls request.jwtVerify(cb) evaluate useGlobalOptions = false, create an unreachable wrapper Promise, and miss validatorCache / global verifier.
    • When configured with a static public key and default options without decode.checkTyp, requestVerify still pre-decodes the token with decode(token) (double-decoding the JWT and bypassing fast-jwt's verifier LRU cache), merges options redundantly in lookupToken, and dispatches through steed.waterfall.

Expected behavior

  • Reuse the startup signer directly (without steed.waterfall) in reply.jwtSign(payload) / reply.jwtSign(payload, cb) when secret is static and no per-call options are given.
  • Fix reply.jwtSign(payload, cb) for dynamic secret functions and share bounded dynamic caching (supporting non-empty string and Buffer keys, capped at 1000 entries) across signerCache and validatorCache.
  • Detect function-only options before checking useGlobalOptions / Promise mode in requestVerify, and add a synchronous fast path for global static-key verification (when !decodeOptions.checkTyp) using the global verifier directly while preserving error wrapping and trusted / formatUser semantics.

Activity

  1. Tony133 commented on Nov 15, 2026

    @Tony133
    Member

    Thanks for looking into this!

    A couple of suggestions:

    Smaller PRs are much easier to review, and the bug fix can land quickly without waiting on the perf discussion.

    Once all three PRs have been opened, we will close this issue and do the review directly on the PRs.

  2. TalhaHunter101 commented on Nov 16, 2026

    @TalhaHunter101
    ContributorAuthor

    Hi @Tony133,

    As requested, I have split the work into three dedicated, standalone PRs:

    1. Bug Fix: fix: reply.jwtSign(payload, cb) with a dynamic secret #440 (fix: reply.jwtSign(payload, cb) with a dynamic secret)
    2. Performance: perf: reuse the global signer for static keys #441 (perf: reuse the global signer for static keys)
    3. Performance: perf: bounded signer/verifier cache and static verify fast path #442 (perf: bounded signer/verifier cache and static verify fast path)

    Each PR is branched cleanly from main, contains dedicated unit tests, achieves 100% test coverage, and passes all TypeScript and ESLint checks. Ready for review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions