railyard/packages/cli/test/fixtures/fake-cloudflare.mjs
caelum0x 90f6ba3b78 feat(ship): custom domains for shipped apps on the customer's own Cloudflare account (QY1.5, I6)
- packages/cli/src/domains.mjs: a dependency-free client of Cloudflare's
  public Workers Custom Domains API (zones lookup, PUT/GET/DELETE
  /accounts/{id}/workers/domains, certificate pack state). Cloudflare
  issues the TLS certificate; a domain reads active once it is. The token
  goes in the Authorization header only and is scrubbed from every error.
  Byte copy at apps/console/lib/ship/cf-domains.mjs (drift test).
- railyard domains [add|remove HOSTNAME] [--app]: the console attaches the
  hostname to the app's production Worker with the org's Cloudflare grant
  (the QY1.1 OAuth step; the access token is opened and used inside the
  console only). Without a grant (409 no_cloudflare_grant) the CLI does the
  same with the local login (CLOUDFLARE_API_TOKEN or the wrangler login
  file after `wrangler whoami`) and the console records the outcome. Adding
  an attached hostname keeps it; one on another Worker is refused; remove
  is idempotent; a hostname outside the account's zones is refused plainly.
- Console: migration 0010 railyard.app_domains (one org per hostname,
  lower case, cascade, RLS, no anon); GET/POST/DELETE /api/v1/domains and
  POST /api/v1/domains/record (CLI bearer); states refreshed from
  Cloudflare when the org has a grant. Cloudflare OAuth default scopes add
  zone:read and ssl_certs:write (orgs linked before need to run
  railyard link once more for console-side domains).
- Scribase bridge: new signed route domains {scribase_org_id, app?} ->
  {available, domains:[{app, hostname, state, checked_at, created_at}],
  next} for the Deploy tab; contract fixture v2 adds it (additive).

Tests: CLI 23/23 (new domains.test.mjs 5: grant path with the real console
handlers and a fake Cloudflare, local-login path, wrangler config file,
refusals, idempotent remove, no token printed or sent to the console);
console 147/147 (new lib/ship/domains.test.ts 4: drift, worker naming
equal to the CLI's, attach/detach and cross-org refusal, record and
refresh, bridge listing; 0010 migration on a scratch PG); contract 6/6;
console tsc clean.

Owner-only: one real-domain proof (railyard domains add on a domain in the
owner's Cloudflare account) is not run here; record it at the deploy gate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 13:45:57 +03:00

47 lines
3.1 KiB
JavaScript

// A fake of the Cloudflare API calls custom domains make (QY1.5): the OAuth
// token endpoint, accounts, zones, Workers custom domains and certificate
// packs. It records every call with the token it carried.
import { createServer } from "node:http";
export async function startCloudflare({ zones = ["example.com"], account = "a".repeat(32), tokens = ["cf-access-1", "cf-local-token-123"] } = {}) {
const calls = [];
const domains = new Map(); // id -> {id, hostname, service, zone_id, zone_name}
let n = 0;
const zoneRows = zones.map((name, i) => ({ id: `zone${i}${"0".repeat(27)}`, name }));
const server = createServer(async (req, res) => {
let raw = "";
for await (const c of req) raw += c;
const url = new URL(req.url, "http://x");
const auth = req.headers.authorization ?? null;
calls.push({ method: req.method, path: url.pathname, query: url.search, auth, body: raw });
const send = (status, body) => res.writeHead(status, { "content-type": "application/json" }).end(JSON.stringify(body));
const ok = (result) => send(200, { success: true, errors: [], result });
if (url.pathname === "/oauth2/token") return send(200, { access_token: "cf-access-1", refresh_token: "cf-refresh-2", expires_in: 3600 });
const token = (auth ?? "").replace(/^Bearer /, "");
if (!tokens.includes(token)) return send(403, { success: false, errors: [{ code: 10000, message: "Authentication error" }] });
const p = url.pathname.replace(/^\/client\/v4/, "");
if (p === "/accounts") return ok([{ id: account, name: "Shop" }]);
if (p === "/zones") return ok(zoneRows.filter((z) => z.name === url.searchParams.get("name")));
const wd = new RegExp(`^/accounts/${account}/workers/domains(?:/([^/]+))?$`).exec(p);
if (wd && req.method === "GET") return ok([...domains.values()].filter((d) => !url.searchParams.get("service") || d.service === url.searchParams.get("service")));
if (wd && req.method === "PUT") {
const b = JSON.parse(raw);
const zone = zoneRows.find((z) => z.id === b.zone_id);
if (!zone || !b.hostname.endsWith(zone.name)) return send(400, { success: false, errors: [{ code: 100117, message: "hostname is not in the zone" }] });
const d = { id: `dom${++n}`, hostname: b.hostname, service: b.service, zone_id: zone.id, zone_name: zone.name, environment: b.environment };
domains.set(d.id, d);
return ok(d);
}
if (wd && req.method === "DELETE") {
if (!domains.delete(wd[1])) return send(404, { success: false, errors: [{ code: 100100, message: "not found" }] });
return ok(null);
}
const cp = /^\/zones\/([^/]+)\/ssl\/certificate_packs$/.exec(p);
if (cp) return ok([...domains.values()].filter((d) => d.zone_id === cp[1]).map((d, i) => ({ id: `cp${i}`, hosts: [d.hostname], status: d.hostname.startsWith("live.") ? "active" : "pending_validation" })));
send(404, { success: false, errors: [{ code: 7003, message: "no route" }] });
});
await new Promise((r) => server.listen(0, "127.0.0.1", r));
const base = `http://127.0.0.1:${server.address().port}`;
return { base, api: `${base}/client/v4`, calls, domains, account, close: () => new Promise((r) => server.close(r)) };
}