Skip to content

Commit 5a7fc19

Browse files
DavidBabinecclaude
andcommitted
docs: update server.md for binary helpers and current route table
Document the new toArrayBuffer / binaryResponse helpers introduced in server/binary.ts. Add a "Binary helpers" subsection under HTTP helpers and list the file in the Related → source-of-truth section. Also bring the route table up to date: replace the removed tryServeAgent / tryServeAgentToolResult entries with tryServeAi (/admin/api/ai/*), and add the missing tryServeHoleRuntimeAsset, tryServeHole, tryServePublicFormRuntimeAsset, and tryServePublicForm handlers that landed in earlier commits. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 487a177 commit 5a7fc19

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

docs/server.md

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,17 @@ It walks an ordered `routes` array of `RouteHandler` functions. Each handler ret
6161
```ts
6262
const routes: readonly RouteHandler[] = [
6363
tryServeHealth, // /health
64-
tryServeAgent, // /admin/api/agent
65-
tryServeAgentToolResult, // /admin/api/agent/tool-result
66-
tryServeCmsApi, // /admin/api/cms/* → handlers/cms/index.ts
67-
tryServeLoopRuntimeAsset, // loop runtime asset (CMS-owned)
64+
tryServeAi, // /admin/api/ai/* → server/ai/handlers/
65+
tryServeCmsApi, // /admin/api/cms/* → handlers/cms/index.ts
66+
tryServeLoopRuntimeAsset, // /_instatic/loop-runtime.js (fixed CMS asset)
6867
tryServeLoop, // /_instatic/loop/* → handlers/cms/loop.ts
68+
tryServeHoleRuntimeAsset, // /_instatic/hole-runtime.js (fixed CMS asset)
69+
tryServeHole, // /_instatic/hole/* → handlers/cms/hole.ts
70+
tryServePublicFormRuntimeAsset, // /_instatic/form-runtime.js (fixed CMS asset)
71+
tryServePublicForm, // /_instatic/form/* → forms/handler.ts
6972
tryServeRuntimeAsset, // /_instatic/assets/* → published runtime assets
7073
tryServeRuntimePackageNamespace, // /_instatic/runtime/cache/<hash>/<...> → bun install workspace
71-
tryServeSiteCssNamespace, // /_instatic/css/* → hashed CSS bundles
74+
tryServeSiteCssNamespace, // /_instatic/css/* → hashed CSS bundles
7275
tryServeMediaRedirect, // /_instatic/media/<adapterId>/<path> → 302 to signed read URL
7376
tryServeStaticAsset, // /assets/* → dist/ (admin app)
7477
tryServeUpload, // /uploads/* → uploadsDir (with nosniff hardening)
@@ -83,7 +86,7 @@ const routes: readonly RouteHandler[] = [
8386

8487
Order matters. Two examples:
8588

86-
- `tryServeCmsApi` is matched **after** `tryServeAgent` and `tryServeAgentToolResult` so the agent endpoints (under `/admin/api/agent*`not `/admin/api/cms/*`) aren't swallowed by the CMS dispatcher.
89+
- `tryServeAi` is matched **before** `tryServeCmsApi` so the AI endpoints (`/admin/api/ai/*`) aren't swallowed by the broader CMS dispatcher (`/admin/api/cms/*`).
8790
- `tryServeUpload` is matched **before** `tryServeAdminApp` because `/uploads/...` is a sub-tree the SPA fallback would otherwise consume.
8891

8992
Adding a new endpoint is a one-line edit to `routes` plus a focused `tryServeX` function.
@@ -201,6 +204,17 @@ Conventions:
201204

202205
`readValidatedBody` is the canonical body parser: it parses JSON and validates the shape against a TypeBox schema in one step, so handlers receive a fully typed value or return `badRequest` immediately.
203206

207+
### Binary helpers (`server/binary.ts`)
208+
209+
`server/binary.ts` provides two helpers for safely handing `Uint8Array` bytes to `Response` bodies and worker `postMessage` transfers:
210+
211+
| Helper | Purpose |
212+
|------------------------------------|----------------------------------------------------------------------|
213+
| `toArrayBuffer(bytes: Uint8Array)` | Copies the view's logical range into a fresh, exactly-sized `ArrayBuffer`. Required because a `Uint8Array` is only a view — its `.buffer` may be larger (pooled or sliced backing store) and resolves to `ArrayBuffer \| SharedArrayBuffer` which transfer/body slots reject. |
214+
| `binaryResponse(bytes, init?)` | Convenience wrapper: calls `toArrayBuffer` then wraps the result in a `new Response(...)`. Use for every "serve raw bytes" response in route handlers. |
215+
216+
Use `binaryResponse` whenever a route handler returns binary content (runtime assets, CSS bundles, images). Use `toArrayBuffer` when bytes must cross a worker `postMessage` boundary as a transferable.
217+
204218
**Error envelope.** Every CMS handler error returns `{ error: string }` and is validated client-side by `ErrorEnvelopeSchema` in `src/core/http/apiClient.ts` (re-exported from `responseSchemas.ts`). The canonical client `apiRequest` (and `readEnvelope`) extract the message via `responseErrorMessage(res, fallback)` and throw an `ApiError` carrying the HTTP status.
205219

206220
---
@@ -534,7 +548,8 @@ See [docs/reference/typebox-patterns.md](reference/typebox-patterns.md) for boun
534548
- Source-of-truth files:
535549
- `server/index.ts` — entrypoint and boot
536550
- `server/router.ts` — request dispatch
537-
- `server/http.ts` — HTTP helpers
551+
- `server/http.ts` — JSON / error HTTP helpers
552+
- `server/binary.ts` — binary response helpers (`toArrayBuffer`, `binaryResponse`)
538553
- `server/handlers/cms/index.ts` — CMS dispatcher
539554
- `server/auth/authz.ts``requireCapability` and friends
540555
- `server/db/client.ts``DbClient` interface

0 commit comments

Comments
 (0)