r/place on the atmosphere, targeting cloudflare's free tier
at-place CONTRIBUTING.md
6.4 kB · 121 lines
Markdown

Contributing #

atplace is an r/place clone on atproto. It runs on Cloudflare: one Worker, one Durable Object class, and a static frontend. It's meant to stay small, cheap to run (within Cloudflare's free tier), and easy to maintain. Keep that in mind before adding features.

Setup #

pnpm install
pnpm dev              # http://127.0.0.1:5173 (must be 127.0.0.1, not localhost)
pnpm check            # type-check worker, web and node code
pnpm run deploy       # see the README; plain `pnpm deploy` is a pnpm built-in

wrangler.jsonc is shared by everyone who deploys this project. Keep operator-specific values out of it: domains and canvases belong in each operator's deploy.env.

To play locally you need a canvas record in a real repo:

ATP_IDENTIFIER=you.bsky.social ATP_APP_PASSWORD=xxxx-xxxx-xxxx-xxxx pnpm create-canvas --rkey test
echo 'CANVAS_ALLOWLIST=<printed at-uri>' > .dev.vars

Run pnpm types after changing wrangler.jsonc, which regenerates src/worker/env.d.ts.

How it works #

  1. The browser signs in with atproto OAuth (src/web/auth.ts). Tokens stay in the browser, so the server has no sessions or secrets.
  2. To place a pixel, the browser writes a pixel record to the player's own repo, then POSTs its AT-URI to /api/canvas/<did>/<rkey>/place (src/web/place.ts).
  3. The canvas Durable Object fetches that record from the player's PDS. The record existing in the DID's repo is the authentication. The object then checks the cooldown, applies the pixel and queues it for broadcast.
  4. Viewers hold a hibernatable WebSocket. On connect they get the config (JSON) and a gzipped snapshot; after that, batched diffs about once a second (src/shared/protocol.ts).

There is no firehose consumer. Players' repos are the history log, so anyone can rebuild a canvas from the network. The Durable Object's state is the official canvas, because the cooldown is enforced on server receive time.

Layout #

Each module does one thing and talks to the others through plain data. Only canvas-object.ts and main.ts wire modules together.

Path Role
lexicons/ Lexicon JSON for canvas and pixel records
src/shared/lexicon.ts NSIDs, OAuth scope, record types and guards (mirror the JSON)
src/shared/aturi.ts AT-URI and TID parsing
src/shared/protocol.ts WebSocket message encoding, CanvasConfig, PlaceResult
src/shared/routes.ts API paths, used by both router and client
src/worker/index.ts Router and allowlist check
src/worker/canvas-object.ts The Durable Object: placement rules and wiring
src/worker/canvas-config.ts Record → config validation; which config edits are safe
src/worker/limits.ts Per-deployment limits from wrangler.jsonc vars
src/worker/pixel-grid.ts In-memory pixels and tile slicing (pure)
src/worker/canvas-store.ts All SQLite access
src/worker/broadcaster.ts Batching and fanning out diffs
src/shared/atproto.ts DID → PDS/handle resolution and getRecord (worker, web and scripts)
src/worker/client-metadata.ts OAuth client metadata for deployed instances
src/web/* Auth, placing, the live feed, canvas view, page wiring
scripts/create-canvas.ts Create or update a canvas record with an app password

Rules to preserve #

  • Free-tier budget. The binding limits are 100k Worker requests, 100k DO requests and 100k SQLite rows written per day, plus 13,000 GB-s of DO duration.
    • A placement costs one Worker request, one DO request and exactly two row writes (its tile and its player).
    • A viewer costs one request when they connect. Outgoing messages are free.
    • Don't add per-pixel rows, polling endpoints, or anything else that keeps the Durable Object awake when nothing is happening.
  • Hibernation. The Durable Object's constructor runs again after every hibernation. Everything in memory except the broadcast buffer must be rebuildable from SQLite, so placements are written synchronously when they happen.
  • Config safety. Width and height never change. The palette only grows at the end, because pixels store palette indices. unsafeChange enforces this. The deployment's limits cap what any canvas record can ask for.
  • Replay protection. Anyone can POST any record URI. Each player's accepted pixel rkeys (TIDs) must strictly increase, and the cooldown is per DID.
  • Lexicons. src/shared/lexicon.ts and lexicons/ must agree. The NSID root dev.marantz.atplace is effectively permanent, because every record ever created carries it. Changing it orphans existing canvases and pixels, and changes the OAuth scope.
  • SQLite schema. Tables are created with CREATE TABLE IF NOT EXISTS. Changing a table needs an explicit migration in CanvasStore, because deployed objects already hold data.

Known gaps #

  • There is no bot or abuse defense beyond cooldowns. Candidates: minimum account age, or Turnstile on placement.
  • Spam POSTs to /place cost requests even when rejected. A cheap pre-check in the Worker would help.
  • If a canvas is evicted, viewers reconnect with backoff, but there's no UI beyond "Reconnecting…".
  • There are no automated tests yet. PixelGrid, protocol.ts, routes.ts and canvas-config.ts are pure and easy to test first.

Future ideas #

  • Canvas gallery (firehose aggregator). A separate read-only app that shows every canvas on the network, not just those an instance allowlists:
    • Subscribe to Jetstream filtered to the canvas and pixel collections.
    • Backfill older history with com.atproto.sync.listReposByCollection and per-repo record reads, because Jetstream only keeps about a day.
    • Rebuild each canvas by grouping pixels on their canvas field. PixelGrid and protocol.ts can be reused.
    • Its canvases will be close to, not identical with, an instance's. Instances enforce cooldowns on receive time and reject some pixels, and our client deletes rejected records. Applying the same cooldown to firehose event time narrows the gap.
    • It needs a connection that is always open, so a single Durable Object would use most of the free tier's daily duration. A small VPS is an alternative.
    • Related lexicon change: an optional instance URL on canvas records, so a gallery can link to where each canvas is played.