Add capnweb-typecheck RPC validators#169
Draft
teamchong wants to merge 5 commits into
Draft
Conversation
|
Contributor
|
All contributors have signed the CLA ✍️ ✅ |
commit: |
c0dc243 to
a762c16
Compare
400182b to
7d866ac
Compare
bda749b to
4ceeec5
Compare
Add the capnweb-typecheck package, marker transform, plugin adapters, CLI, runtime validators, tests, and Worker React debug example.
4ceeec5 to
7f94aff
Compare
0a6e258 to
2e725b9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #174
Summary
Adds
capnweb-validate, an opt-in TypeScript-to-runtime-validator transform for Cap'n Web and Workers RPC services.Server validation is enabled with
@validateRpc()on the service class. Users keep importing Cap'n Web APIs fromcapnweb, so the service remains usable with native Workers RPC and with Cap'n Web.Client validation is enabled by the transform recognizing Cap'n Web session constructors by TypeScript symbol resolution. No client import change is required.
Validated paths:
If
@validateRpc()is left untransformed, it throws a configuration error instead of silently running without validation.How it works
@validateRpc()decorators imported fromcapnweb-validate.@validateRpc<T>()type argument is given, the transform emits a warning and falls back to class public methods.capnweb-validate/internal/core.capnweband injects client-side validation wrappers.Supported pass-by-value types include primitives, arrays, tuples, plain objects, unions,
Record<string, T>, selected platform built-ins, andPromise<T>return unwrapping.Pass-by-reference values include plain functions,
RpcStub<T>,RpcPromise<T>,RpcTargetsubclasses, and Workers RPC stubs.Unsupported wire types such as
Map,Set,WeakMap,WeakSet,RegExp,ArrayBuffer, and non-Uint8Arraytyped arrays fail at build time.Included
packages/capnweb-validateworkspace package:@validateRpc()class decorator@skipRpcValidation()method opt-outRpcValidationErrorcapnweb-validate/capnwebcapnwebis an optional peer dependency. The root entry andcapnweb-validate/internal/corehave nocapnwebimports; Cap'n Web-specific helpers live undercapnweb-validate/capnwebandcapnweb-validate/internal/capnweb. Workers RPC-only users can installcapnweb-validatealone.WorkerEntrypointsupport. Runtime-invoked methods onWorkerEntrypointsubclasses (fetch,tail,trace,scheduled,queue,test,email,connect,tailStream) bypass the validator so the Workers runtime can still invoke them; user-defined RPC methods are validated.RpcValidationError extends TypeErrorwitherr.rpcValidation = { path, expected, actual, value }.pathis JSON-pointer-style, for example["Api", "authenticate", 0, "profile", "email"].capnweb.examples/worker-reactvalidation wiring.Example
Server:
Client:
Wrangler flow
Wrangler does not expose a bundler plugin hook, so use the CLI for Worker source:
Point Wrangler's
mainat the generated entry under.wrangler/validate.The React client uses the Vite plugin, not the CLI.
Testing
Package:
npm run -w capnweb-validate build npm run -w capnweb-validate testRepo:
Example: