Skip to content

Upgrade @cap.js/server to 4.x, where createChallenge is async #1612

Closed

Description

@joshunrau

@cap.js/server is held at 2.0.0 in apps/gateway by the dependency update on the deps branch. The newest release eligible under the repo's 7-day minimumReleaseAge on 2026-10-04 was 4.0.5.

Why it was not upgraded

From 3.0.0, Cap#createChallenge() returns a Promise; 2.0.0 returned the challenge synchronously. The challenge route serialises the return value directly:

// apps/gateway/src/routers/cap.router.ts:28-35
router.post('/challenge', (_, res) => {
  res.status(200).json(
    cap.createChallenge({
      challengeDifficulty: 4,
      expiresMs: 60_000 * 5
    })
  );
});

On 3.x or 4.x this route would respond with {}, a serialised Promise, so the widget could never solve a challenge. No automated check below e2e would catch it:

  • tsc misses it, because res.json accepts any value.
  • cap.router.test.ts only covers $VerifyRequest.

Nothing else used here changed between 2.0.0 and 4.0.5:

  • The createChallenge options and redeemChallenge / validateToken behave as before.
  • The Solution type and the constructor's noFSState (apps/gateway/src/lib/cap.ts:3) are unchanged.
  • The wire format is still { challenge: { c, s, d }, token, expires }, which the installed @cap.js/widget 0.1.58 accepts.
  • 4.0.5 has no peer dependencies.

Upstream publishes no release notes for 3.x or 4.x. The comparison above comes from diffing the 2.0.0, 3.0.0 and 4.0.5 tarballs.

What is required

  • Make the /challenge handler async and await cap.createChallenge(…). The same file already wraps async handlers in ah (lines 39 and 53).
  • Bump @cap.js/server to ^4 in apps/gateway/package.json.
  • Add a unit test asserting that the /challenge response carries challenge and token.
  • Run testing/src/specs/remote-assignment.spec.ts and the rest of the e2e suite.

Activity

  1. added
    Priority: Low
    Difficulty: Low
    Chore
    Area: Gateway
    Good First Issue
    on Oct 5, 2026
  2. joshunrau commented on Oct 26, 2026

    @joshunrau
    CollaboratorAuthor

    Fixed by f49823d on #1660, which is still open (CONFLICTING, review required, last updated 2026-10-07). main (d86740e) still pins ^2.0.0 and has the synchronous handler at apps/gateway/src/routers/cap.router.ts:28-35.

    Corrections

    • Since this was filed, 278b827 added a /challenge test to cap.router.test.ts, and it asserts challenge and token. It would still pass after the bump without the await, because the mock is an untyped vi.fn() (L11-L17) set with mockReturnValueOnce (L68). What catches the bug in f49823d is mockResolvedValueOnce, plus typing the mocks as vi.fn<Cap['createChallenge']>() so a synchronous mock no longer type-checks. Keep both if this is redone outside build: upgrade dependencies held back by breaking changes #1660.
    • remote-assignment.spec.ts never opens the gateway. The specs that solve a real challenge are in testing/src/specs/gateway-assignment.spec.ts: the @smoke test (L67) and the malformed-submission test (L146). If /challenge answers {}, each fails only when its wait for Begin runs out at 120 s (L88, L158).

    Plan

    • If build: upgrade dependencies held back by breaking changes #1660 stays blocked, cherry-pick f49823d onto main by itself. apps/gateway/package.json, cap.router.ts and cap.router.test.ts are identical at its parent and at main. Its pnpm-lock.yaml hunks apply with only line offsets, and the @types/node@22.19.21 and undici-types@6.21.0 entries it drops are used only by @cap.js/server@2.0.0 on main too. It comes after build: upgrade dependencies held back by breaking changes #1660's Vitest 5 commit but does not depend on it: vi.fn<T>() and mockResolvedValueOnce are typed the same way in the Vitest 4.1.11 on main. 4.0.5 is still the newest release.
    • f49823d adds no e2e test. Add a fast one next to the /api/auth/verify request test at gateway-assignment.spec.ts:273: POST ${gatewayURL}/api/auth/challenge, then assert that challenge.c, challenge.s and challenge.d are numbers and token is a string. It needs no proof of work, and it fails in seconds with a readable diff instead of a timeout.

    Closing

    #1660's body has Closes #1624, #1626, …, #1612, … on one line. GitHub links only the first number, so #1624 is the PR's only closing reference, and merging it as it stands leaves this issue and 12 others open. Put the keyword before each number (Closes #1624, closes #1626, …). A cherry-pick PR needs its own Closes #1612.

    Triage note written with Claude Code. Code references point at main @ d86740e.

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

    Area: GatewayChoreDifficulty: LowGood First IssuePriority: Low

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions