Skip to content

Add capnweb-typecheck for TypeScript RPC validation codegen#169

Draft
teamchong wants to merge 9 commits into
cloudflare:mainfrom
teamchong:typescript-rpc-validators
Draft

Add capnweb-typecheck for TypeScript RPC validation codegen#169
teamchong wants to merge 9 commits into
cloudflare:mainfrom
teamchong:typescript-rpc-validators

Conversation

@teamchong
Copy link
Copy Markdown

@teamchong teamchong commented May 11, 2026

Summary

Adds build-time TypeScript RPC validation codegen to the single capnweb package. The capnweb bin reads RpcTarget classes from TypeScript and generates runtime validators that fire when RPC methods are called.
capnweb stays dependency-free at install time. Tooling is exposed as the capnweb typecheck gen CLI command, capnweb/vite, and the generated-code-only capnweb/internal/typecheck subpath.

What this PR adds

Typecheck tooling in capnweb:

  • capnweb CLI (bin) for Wrangler-style builds: capnweb typecheck gen src/worker.ts --out .capnweb.
  • capnweb/vite plugin for Vite builds.
  • No typia, no ts-morph, no .npmrc, and no separate packages/capnweb-typecheck package.
  • Codegen uses the local TypeScript compiler API plus local validator metadata/runtime.
  • For each typed RpcTarget method, the generator emits runtime validators for every parameter and return value, plus glue that registers them on the class at startup. Generated artifacts are written to .capnweb/ (configurable).

Runtime integration:

  • Zero runtime dependencies.
  • New internal subpath capnweb/internal/typecheck consumed only by generated code, not by user code.
  • Server-side validation: keyed by RpcTarget constructor; runs in deliverCall() before invoking the method and before returning a result.
  • Client-side validation: bound to the original RpcStub, preserving RpcPromise pipelining, disposal, and StubBase semantics. Typed new RpcSession<T>(...) is supported via a session-shape wrapper that binds validators on session.getRemoteMain() without changing the caller-visible value.

VS Code Debug

  1. In a JavaScript Debug Terminal:
./examples/worker-react/dev-debug.sh

# ./examples/worker-react/dev-debug.sh --typecheck # for checking how the typecheck work
  1. In VS Code Command Palette, run Debug: Open Link and open:
    http://127.0.0.1:5173

Set breakpoints in examples/worker-react/web/src/main/App.tsx, then click Run demo.

Testing

  • npm run build
  • npm run test:types
  • npm run test:bun
  • npx vitest run --no-coverage tests/typecheck.test.ts tests/vite-plugin.test.ts
  • npx vitest run --no-coverage --project node
  • ./examples/worker-react/dev-debug.sh smoke reached http://127.0.0.1:5173
  • ./examples/worker-react/dev-debug.sh --typecheck smoke reached http://127.0.0.1:5173

Changeset
.changeset/typescript-rpc-validators.md — minor bump for capnweb.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 11, 2026

🦋 Changeset detected

Latest commit: 23c1f7e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
capnweb Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions
Copy link
Copy Markdown
Contributor


Thank you for your submission, we really appreciate it. Like many open-source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution. You can sign the CLA by just posting a Pull Request Comment same as the below format.


I have read the CLA Document and I hereby sign the CLA


You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 11, 2026

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/capnweb@169

commit: 23c1f7e

@teamchong teamchong force-pushed the typescript-rpc-validators branch from c0dc243 to a762c16 Compare May 11, 2026 18:56
@teamchong teamchong changed the title Add TypeScript RPC validation codegen Add capnweb-typecheck for TypeScript RPC validation codegen May 11, 2026
@teamchong teamchong force-pushed the typescript-rpc-validators branch 4 times, most recently from e1b7d41 to 400182b Compare May 12, 2026 02:17
@teamchong teamchong force-pushed the typescript-rpc-validators branch from 400182b to 7d866ac Compare May 12, 2026 02:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant