Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ repos
node_modules
dist
.claude
.wrangler
.DS_Store
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Last verified: 2026-02-16
- `src/core.ts` - Shared types, error classes, and transport-agnostic JSON-RPC 2.0 engine (wire format types, type guards, request/response builders, RpcProtocolError, RPC processor)
- `src/http-batch.ts` - HTTP batch transport: newHttpBatchRpcResponse (server) + newHttpBatchRpcSession (client with auto-batching)
- `src/session.ts` - Bidirectional RPC over message transports (defines session-specific types: RpcTransport, RpcSessionOptions, RpcSession)
- `src/websocket.ts` - WebSocket transport: newWorkersWebSocketRpcResponse (server, fire-and-forget) + newWorkersWebSocketRpcSession (server, bidirectional with typed remote proxy) + newWebSocketRpcSession (client with Disposable proxy)
- `src/websocket.ts` - WebSocket transport: newWorkersWebSocketRpcResponse (server, fire-and-forget) + newWorkersWebSocketRpcSession (server, returns {response, session} for bidirectional RPC) + newWebSocketRpcSession (client, returns RpcSession)
- `src/__tests__/` - Test files
- `src/__tests__/fixtures/worker.ts` - Test worker entry point for Workers runtime tests
- `src/__tests__/fixtures/wrangler.toml` - Minimal Workers config for test worker
Expand Down
49 changes: 49 additions & 0 deletions examples/worker-basic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# worker-basic

Interactive playground for `@jmorrell/jsonrpc` on Cloudflare Workers.

Defines a handful of toy RPC methods on the server and serves a static page
where you can invoke them individually or in bulk. A toggle switches between
**HTTP batch** and **WebSocket** transports — the server code is identical
because `newWorkersRpcResponse` routes both automatically.

## Running

```bash
# from repo root — the example links to the local library build
npm run build

# install and build the example
cd examples/worker-basic
npm install
cd web && npm install && npm run build && cd ..

# start the dev server
npm run dev
```

Open http://localhost:8787.

## Structure

```
src/worker.ts Cloudflare Worker — defines the service and routes /api
web/src/app.ts Client — uses newHttpBatchRpcSession or newWebSocketRpcSession
web/src/style.css Styles
web/index.html Vite entry point
web/vite.config.ts Aliases @jmorrell/jsonrpc to the local dist build
```

## What it demonstrates

- **Defining a service** — plain object with typed methods, passed to
`newWorkersRpcResponse`.
- **HTTP auto-batching** — calls made in the same tick are combined into a
single POST containing a JSON-RPC batch array.
- **WebSocket transport** — persistent connection, each call is an individual
JSON-RPC message (no batching needed).
- **Type-safe client** — `newHttpBatchRpcSession<Api>` and
`newWebSocketRpcSession<Api>` return a typed proxy where method calls map
directly to RPC requests.
- **Raw protocol visibility** — every call shows the JSON-RPC request and
response side by side.
Loading