cloud / internal-packages / installer / worker / pretend-deployer.jsmain at aacbc74
// The pretend deployer: the Deployer of deploy-run.js without a container. It acts a run out
// (pretend-run.js) in about 25 seconds, and answers start(), status() and kept() as the real one
// does. Like the real one it keeps no token: at the start it tells the pretend Cloudflare when the
// deployment will "exist", and gives the token up at once.
import { DurableObject } from "cloudflare:workers";
import { claimIn, revoke } from "./cloudflare-access.js";
import { firstOwnerTicket, withTicket } from "./first-owner.js";
import { INSTALL_OPS, Installation } from "./installation.js";
import { pretendRunner } from "./installation-pretend.js";
import { AHEAD, COMMIT, plan, stateAt, STEPS } from "./pretend-run.js";
const IDLE = {
status: "idle",
startedAt: null,
endedAt: null,
lines: [],
steps: STEPS,
result: null,
};
export class Deployer extends DurableObject {
/** As the real one: a first deploy and a recovery run make the owner's ticket, in memory alone.
* Here no stack is told its hash, so it opens nothing.
* @type {Awaited<ReturnType<typeof firstOwnerTicket>> | null} */
#first = null;
/** A chat installation (installation.js), its phases acted out (installation-pretend.js). */
/** @type {Installation | null} */
#installation = null;
/** @returns {Installation} */
installation() {
const cloudflare = (input, init) => this.env.CF_SERVICE.fetch(input, init);
this.#installation ||= new Installation({
storage: this.ctx.storage,
env: this.env,
fetch: cloudflare,
runner: pretendRunner({
fetch: cloudflare,
api: this.env.CF_API,
site: String(this.env.CF_SITE || "").replace(/\/$/, ""),
}),
});
return /** @type {Installation} */ (this.#installation);
}
install(op, args) {
if (!INSTALL_OPS.includes(op)) throw new Error(`no such operation: ${op}`);
return this.installation()[op](args);
}
async alarm() {
if (await this.ctx.storage.get("install")) await this.installation().alarm();
}
/** Start a run. A run already running is left alone. */
async start({ accountId, zone, name, token, ownerEmails, recoverOwner, erase, anyway }) {
const now = Date.now();
const run = /** @type {any} */ (await this.ctx.storage.get("state"));
if (run && now < run.startedAt + run.end)
return { began: false, state: stateAt(run, now - run.startedAt) };
const attempt = Number((await this.ctx.storage.get("attempts")) || 0) + 1;
// which iterate the stack runs, as its boundary Worker's tag would say it
const runs = /** @type {string | undefined} */ (await this.ctx.storage.get("runs"));
const next = {
...plan({ zone, name, erase, anyway, runs }, attempt),
startedAt: now,
zone,
name,
};
// An "ahead" domain's first deploy leaves a commit that this Cloud does not have, as a deploy
// from a computer after it would. Every other deploy leaves the Cloud's own.
const leaves = !runs && !erase && /ahead/.test(zone) ? AHEAD : COMMIT;
await this.ctx.storage.put({
state: next,
attempts: attempt,
...(next.deployed && { runs: leaves }),
});
const owner = ownerEmails ? ownerEmails[0] : recoverOwner;
this.#first = owner && next.status === "succeeded" ? await firstOwnerTicket(owner) : null;
const cloudflare = (input, init) => this.env.CF_SERVICE.fetch(input, init);
const site = String(this.env.CF_SITE || "").replace(/\/$/, "");
if (next.deployed)
await cloudflare(`${site}/_pretend/deployed`, {
method: "POST",
headers: { authorization: `Bearer ${token}`, "content-type": "application/json" },
body: JSON.stringify({
account: accountId,
name,
domain: zone,
at: now + next.end,
commit: leaves,
// an erase takes what else was on the domain with it
...(erase && { erased: true }),
}),
});
await revoke({
token,
clientId: this.env.CLIENT_ID,
clientSecret: this.env.CLIENT_SECRET,
revokeUrl: this.env.CF_REVOKE,
fetch: cloudflare,
});
return { began: true, state: stateAt(next, 0) };
}
/** `shown`: the answer goes to the person's page; the first one that says succeeded carries the
* ticket, as the real one's does. */
async status(shown = false) {
const run = /** @type {any} */ (await this.ctx.storage.get("state"));
const state = run ? stateAt(run, Date.now() - run.startedAt) : IDLE;
if (!shown || state.status !== "succeeded" || !this.#first) return state;
const first = this.#first;
this.#first = null;
return withTicket(state, first);
}
/** Only the object named `sign-ins` gets this call (cloudflare-access.js `claimToken`). */
claim(sessionId) {
return claimIn(this.ctx.storage, sessionId);
}
/** Every record this object keeps, as the real one lists them. */
async kept() {
const what = {
state: "The pretend run: its start, the domain, the deployment's name, and its lines.",
attempts: "How many runs this domain had (the pretend deployer fails some first tries).",
runs: "The commit of iterate that the pretend deployment runs.",
};
return {
records: [...(await this.ctx.storage.list()).keys()].map((key) => ({
where: "Durable Object storage",
key,
what: what[key] || "(not described)",
})),
};
}
}