The three routes
The state service has three routes, one table, and Fastify underneath,
plus the sign-in routes the vendored ward-auth library mounts under
/api/auth/. Anything else is a 404. No static files, no directory listing,
no fallback handler.
A service whose only job is to hold documents has no business serving anything else, and every route that does not exist is a route that cannot be wrong.
It is served behind handle_path /sports-app-api/*, which strips the prefix, so
the service keeps serving its own bare /api/* paths.
| Method | Path | What it does |
|---|---|---|
GET | /api/health | Liveness. No auth, no database read, no information. |
GET | /api/state | The caller's newest snapshot bytes, or 404. |
PUT | /api/state | Validate shallowly, store verbatim, prune to the cap. |
GET | /api/auth/session | Who the cookie belongs to (user: null for nobody), and whether Ward is on. The library's. |
POST | /api/auth/login, logout, password, ward/* | Sign in, sign out, change a password, the Ward round trip. The library's; there is no register. |
Who the caller is
/api/state needs a session. The service has its own accounts, and every route
but GET /api/health and the sign-in routes under /api/auth/ is guarded by the
sports_app_session cookie. A person gets one by signing in with a password, or
with Ward, an optional second way in that the service reaches through OpenID
Connect. Local sign-in never calls Ward, so the service works while Ward is
down. The signed-in account's username is the stream key.
| Situation | Answer |
|---|---|
| No session, or an expired one | 401 |
| A session that began with Ward, while Ward cannot answer | 401, reason: "ward_unavailable" |
| A session that began with Ward, which Ward has ended | 401, reason: "ward_session_ended" |
A Ward-originated session is checked against Ward at most every 30 seconds, so a revoke at Ward ends it within 30 seconds.
GET /api/auth/session says who the cookie belongs to. It is how the app learns
who it is after Ward sends the browser back: the login page asks it, writes the
username into the browser, and goes on to the app. The browser keeps that name,
so the app opens offline afterwards. A Ward account with no sports-app grant
never gets a session. Ward refuses it, and the callback lands on the login page
with ?ward_error=access_denied, which the page puts into words.
What Ward changed
The service used to hold one shared secret in a header — the same string for
every caller, with ?user= naming whose document was wanted.
That secret authenticated the installation, never a person. Anyone holding it could read or write anyone's training history by changing a query parameter, and the file header said so plainly rather than pretending otherwise.
A session authenticates a person, and its account's username is the
document key. (From 2026-09-06 to 2026-10-13 the key was the Ward subject; the
service's own accounts replaced that.) So ?user= stops being a target a caller
chooses and becomes a value the server already knows. The client no longer sends it, and the server
ignores one if it is there. That is the whole security change, and it is the
largest one in the repo's history.
The five things Fastify must not do
On 2026-07-29 Fastify replaced hand-rolled node:http routing without
changing one byte of the REST contract — same paths, methods, status codes,
headers and bodies. The Ward cutover changed the contract later, on purpose. The 73 tests in server/__tests__/ are what make that claim
checkable rather than hopeful: they were written against the wire, not the
implementation.
Five framework conveniences are switched off. Each is load-bearing and each is pinned by a test.
GET /api/statereturns the stored bytes verbatim. Fastify would parse and re-serialise JSON for you, which destroys the codec's layout while remaining "valid JSON". So the JSON body parser is replaced with one that hands the handler the raw string, the stored document goes out as aBuffer, and there is deliberately noresponseschema on that route's 200 — a serialiser attached to it would rewrite the very bytes it exists to preserve. The client's crash-safe save round-trips through a byte comparison, so this is not cosmetic.- The session is checked before any validation. It is a route-level
onRequesthook — the earliest point in Fastify's lifecycle, ahead of body parsing and ahead of schema validation. A caller without a session gets a401and never a400that would say anything about the document. The not-found handler checks too, so an unauthorised caller cannot tell a405on/api/statefrom a404. - A document whose
usernameis not the session's subject is a400, not a409, and stores nothing. It refuses a write into somebody else's stream as well as a confused client. - Usernames are rejected, never case-folded. Folding would make the stream
key disagree with the document's own
username, which is exactly what (3) refuses. The rule lives inshared/username.tsand says why. - Paths match exactly.
ignoreTrailingSlashstays off andexposeHeadRoutesis switched off — otherwise Fastify would helpfully addHEAD /api/state, which today is a405.
Fastify logs requests by default. logger: false turns the framework's
logging off completely, so there is no logger for a request body to reach even
in principle. The only log in the process is a callback called with fixed
strings the caller composes.
The test that pins it sends a distinctive password to the old login path and searches the database files and every captured log line for it.
Validation comes from one declaration
The wire shapes are TypeBox declarations in shared/api.ts, so the same
declaration that guards the service also types the client.
A Type.Object({...}) value is a JSON Schema object, so Fastify's AJV reads
the response schemas directly with no adapter, and Static<typeof X> gives the TypeScript type
from the same expression. Hand-written JSON Schema plus a separately maintained
interface would recreate exactly the duplication shared/ exists to delete.
The PUT body is checked with TypeCompiler rather than by Fastify,
because it must stay a raw string (see 1 above) and a body schema would require
Fastify to have parsed it first. One declaration, two entry points into it —
not two declarations.
The generated wire contract reference is TypeDoc over that package.