iterate.com / cloud

git clone https://git.iterate.com/repos/cloud

cloud / internal-packages / installer / test / proof-worker / worker.jsmain at aacbc74

// proof-worker — PROVES THE DEPLOY RUN AND THE ACCESS ROUTES ON WORKERD, WITH NO PERSON. The
// installer's own pages need a person's Cloudflare consent, so a run through them cannot be
// repeated by a script. This Worker holds the same Deployer (../../worker/deploy-run.js) and the
// same access handler (../../worker/access.js), and adds three routes for a script that brings a
// Cloudflare token of its own (test/proof-worker/prove.sh). Deployed only to the dev account.
//
// The proof routes answer only the bearer OPERATOR_TOKEN. CF_REVOKE points at /proof/revoke, which
// refuses: the stand-in token is an API token, which Cloudflare's OAuth revoke must never be sent.
import { handle } from "../../worker/access.js";

export { Deployer } from "../../worker/deploy-run.js";

export default {
  async fetch(request, env) {
    const url = new URL(request.url);
    if (url.pathname === "/proof/revoke")
      return new Response("the proof revokes nothing\n", { status: 501 });
    const access = await handle(request, env);
    if (access) return access;
    if (
      !env.OPERATOR_TOKEN ||
      request.headers.get("authorization") !== `Bearer ${env.OPERATOR_TOKEN}`
    )
      return new Response("Not found\n", { status: 404 });
    const run = () => env.DEPLOYER.getByName(url.searchParams.get("run"));
    if (url.pathname === "/proof/start" && request.method === "POST")
      return Response.json((await run().start(await request.json())).state);
    // ?shown=1 is the person's page: the first such answer of a first deploy carries the first
    // owner's ticket, once (../../worker/first-owner.js)
    if (url.pathname === "/proof/status")
      return Response.json(await run().status(url.searchParams.get("shown") === "1"));
    if (url.pathname === "/proof/kept") return Response.json(await run().kept());
    return new Response("Not found\n", { status: 404 });
  },
};