Skip to content

Shackworks Net internals

shackworks-net is an Astro site on Cloudflare Workers with D1, R2 (for themes) and Clerk (for hub accounts). Its bindings are DB and THEMES. It uses no sessions and no KV.

  • Net is never on an instance’s critical path. Instances sync about every 15 minutes with jitter, only when they have something to send. They back off and queue on 429 or try_later.
  • Accounts exist only to add and remove instances. You can sign up only from /link/<request id>, and you can also sign in at /account. The public site has no Log in or Sign up link and no /sign-in or /sign-up route, so never link to /account from a public page. Keep Clerk’s hosted Account Portal unlinked. A daily job deletes accounts that have no instance and no link request after 24 hours.
  • Net stores no IP addresses, in tables, in audit_log or in rate-limit keys.
  • Net holds summaries only, never QSOs.

The public pages are Home, Contests, Calendar (with calendar.ics), Themes and Leaderboard. They are prerendered or cached for 5 to 15 minutes. Dynamic routes set export const prerender = false. /admin is for reviewers only and returns 404 to everyone else.

The API lives under /api/v1: link, instance, telemetry, scores, submissions, library, templates, themes, events, health, account and admin.

Linking is the OAuth 2.0 authorization code flow with PKCE, narrowed to registering a public key.

  1. POST /api/v1/link/start takes the instance’s Ed25519 public key, its name, its version, the exact redirect_uri and an S256 code_challenge. It returns a request id that is valid for 15 minutes.
  2. The browser opens /link/<id>?state=…. The person signs in or signs up with Clerk (only here) and approves. Net issues a one-time code, valid for 2 minutes, and redirects to the registered URI. The page also shows the code so the person can paste it.
  3. POST /api/v1/link/token takes the code and code_verifier, signed. Net creates the instance row at this point and returns the instance_id.

There is no polling endpoint. Every failure that an instance can see is 400 {"error":"invalid_request"}. The logic lives in src/lib/link.ts, which is pure and tested.

After linking, every request carries three headers: x-instance-id, x-timestamp (unix seconds, within five minutes of Net’s clock) and x-signature. The signature is an Ed25519 signature over this string:

METHOD \n path \n timestamp \n sha256(body) base64url

The code is in src/lib/signing.ts and signed-route.ts. The private key never leaves the instance, which stores it in hub-key.json with mode 600. Removing an instance deletes everything Net holds for it, and removing the last instance deletes the account.

The sharing scopes are system_info, incidents and scores, and all are off by default. server/lib/hub/payloads.ts is the only place that shapes outgoing data. A contest shares scores only when it has config.share_scores and the instance has the scores scope.

A submission starts as pending, then becomes listed, rejected or withdrawn. A linked instance must sign every library request. Listed versions are immutable. The sender chooses a credit byline, and a reviewer can edit it. Reviewers can mark a template as verified, and a resubmission clears that mark. Net checks theme packages in the reviewer’s browser to save Worker CPU. Administrator changes go through POST /api/v1/admin/manage (src/lib/manage.ts) and are audited.

Each instance has these limits: 60 list requests per hour, 40 downloads per hour and 200 per day, 20 submissions per day at up to 4 MB each, and 5 packages waiting for review. docs/hub-limits.md explains the free-plan arithmetic, and test/limits.test.ts simulates 300 instances over a 12-hour contest.

  • Handlers take a Db, which is a slice of D1 defined in src/lib/db.ts, so tests need no Cloudflare. Tests use node:sqlite.
  • Tables are STRICT and timestamps are UTC ISO strings. Apply migrations before you deploy the code that needs them with npm run migrate:remote. Run npm run types after you change wrangler.jsonc.
  • Every state change writes audit_log without personal data. Rate-limit every unauthenticated endpoint with src/lib/ratelimit.ts.
  • src/lib/brand.ts must match the app’s shared/brand.ts. src/styles/foundation.css is a copy, which npm run sync-design refreshes.
  • Clerk forms use routing="hash" and live on their own page. Do not set the global PUBLIC_CLERK_SIGN_IN_URL or SIGN_UP_URL, which sent people to /account between steps.
  • To run locally with real bindings, put the keys in .dev.vars, which is never committed, then run npm run build && npx wrangler dev.

The instance code is in server/lib/hub/:

File Role
client.ts Linking, signing, check-in and unlinking.
keys.ts The instance’s key pair.
state.ts hub.json. It is a file because the panel process writes it.
sync.ts, outbox.ts Scheduled sync and the queue of items waiting for Net.
share.ts, collect.ts The sharing scopes and the data they collect.
cache.ts hub-cache.json. It caches templates and themes for 1 hour, the calendar for 30 minutes and review status for 10 minutes.
library.ts The library client.

Library fetches happen on demand, and the instance shows a stale copy if Net is down. server/hub-fake.ts stands in for Net in tests.