Automations and webhooks for the AT Protocol airglow.run
airglow docs dev-server.md
4.8 kB · 108 lines
Markdown

Dev server and SSR architecture #

Airglow uses HonoX for file-based routing with islands architecture. Both dev and production run on Node, but differently: in dev the server-side code runs inside Vite's SSR module runner, while production runs the bundle Vite built.

How HonoX dev mode works #

Browser request
    ↓
Vite dev server (Node)
    ↓
HonoX vite plugin (SSR middleware)
    ↓
app/server.ts → routes → lib/*
    ↓
HTML response (SSR) + client JS (HMR)

vp dev (Vite+) starts a Vite dev server. HonoX registers a middleware via configureServer that intercepts all requests and runs them through the Hono app using Vite's SSR module runner. Route files in app/routes/ are discovered and registered automatically.

The key constraint: all server-side code executes through Vite's module runner, not Node's own loader. This means:

  • CJS packages must be loaded via createRequire() (see oauth.md)
  • .env is loaded by lib/config.ts (and vite.config.ts for PORT), since Node does not read it on its own

HonoX's noExternal: true #

HonoX's Vite plugin unconditionally sets:

{
  ssr: {
    noExternal: true;
  }
}

This tells Vite to ESM-transform all node_modules during SSR instead of letting Node load them natively. This is required for HonoX's file-based routing to work (it needs to process route imports through Vite's module graph). But it breaks CJS packages that use module.exports or require().

Workarounds:

  • createRequire() loads @atproto/oauth-client-node bypassing Vite's transform

PDS proxy #

The local PDS runs in Docker on localhost:3000 but is configured with hostname pds.dev. The browser needs to interact with the PDS (OAuth authorize page, asset loading), but pds.dev is unreachable.

A custom Vite plugin (pdsProxy in vite.config.ts) solves this:

Browser → http://127.0.0.1:5175/oauth/authorize
    ↓ (Vite middleware, before HonoX)
pdsProxy plugin
    ↓ (http.request to localhost:3000)
Local PDS
    ↓ (response with pds.dev URLs rewritten to 127.0.0.1:5175)
Browser

Plugin lifecycle #

  1. At plugin creation time, getPdsIssuer() synchronously fetches the PDS issuer URL via execSync + curl. This must be synchronous because Vite's defineConfig doesn't support async in all contexts.
  2. In configureServer, the middleware is registered before HonoX's middleware (plugin order matters — pdsProxy() is listed first in the plugins array).
  3. Requests matching /oauth/, /.well-known/, /xrpc/, /@atproto/ are proxied.

Why http.request() and not fetch() #

Node's fetch() follows the Fetch spec and silently strips sec-fetch-* headers. The PDS validates these headers:

  • sec-fetch-site: same-origin — required for the PDS to accept the request
  • sec-fetch-mode: navigate — required for page loads (the browser sends this naturally)

http.request() from node:http forwards headers as-is.

Response rewriting #

Text responses (HTML, JSON, JavaScript) have all occurrences of the PDS issuer URL (e.g. https://pds.dev) replaced with the app origin (http://127.0.0.1:5175). This ensures:

  • The PDS OAuth SPA's internal API calls go through the proxy
  • Redirect URLs in JSON responses point to the proxy
  • Location headers in 3xx responses are rewritten

Additionally:

  • Strict-Transport-Security headers are stripped (we're on HTTP)
  • upgrade-insecure-requests is removed from CSP
  • Secure flag is stripped from cookies

Production #

In production, none of these workarounds apply:

  • pnpm serve runs the built Hono app (dist/server.js) under Node (pnpm start builds first; the app cannot run from untransformed source, because HonoX's createApp needs Vite to resolve import.meta.glob)
  • CJS packages are loaded by Node's module system
  • No PDS proxy (the app and PDS are on reachable domains)
  • .env is loaded by the same process.loadEnvFile call in lib/config.ts when present; on Railway there is none and the env comes from the service

Key configuration #

Setting Dev Production
Runtime Node (via Vite) Node (dist/server.js)
Postgres driver postgres (postgres.js) postgres (postgres.js)
PDS access Vite proxy + URL rewriting Direct HTTPS
.env loading process.loadEnvFile in config Same, when a .env exists
OAuth client type Loopback (http://localhost?...) Confidential (HTTPS metadata URL)