Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
3.1 kB · 107 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108import { z } from "zod";import { domainRecord, type DomainRecord, type DomainStart,} from "./domain-metadata.ts";import { InstallationConflict } from "./installation-errors.ts";import { parse } from "./installation-metadata.ts";
export function beginDomain( current: DomainRecord | null, intent: DomainStart, operationId: string, deadline: number,): DomainRecord { if (current && ["connecting", "removing"].includes(current.status)) throw new InstallationConflict(); const next = { revision: (current?.revision ?? 0) + 1, deadline, operationId, errorCode: null, }; switch (intent.action) { case "attach": if (current && current.status !== "removed") throw new InstallationConflict(); return parse(domainRecord, { ...next, action: "attach", status: "connecting", origin: intent.origin, hostname: new URL(intent.origin).hostname, zoneId: intent.zoneId, domainId: null, writeIntent: false, }); case "retry": if (!current || current.status !== "failed") throw new InstallationConflict(); return parse(domainRecord, { ...current, ...next, status: current.action === "attach" ? "connecting" : "removing", }); case "remove": if ( !current || current.status === "removed" || (current.writeIntent && !current.domainId) ) throw new InstallationConflict(); return parse(domainRecord, { ...current, ...next, action: "remove", status: "removing", origin: null, }); }}
const domainChanges = z.strictObject({ status: domainRecord.shape.status, errorCode: domainRecord.shape.errorCode, domainId: domainRecord.shape.domainId, writeIntent: z.boolean(),});export type DomainChanges = z.infer<typeof domainChanges>;
export function changeDomain( current: DomainRecord, operationId: string, revision: number, input: DomainChanges,): DomainRecord { if ( current.operationId !== operationId || current.revision !== revision || !["connecting", "removing"].includes(current.status) ) throw new InstallationConflict(); const changes = parse(domainChanges, input); const definitiveRejection = current.domainId === null && changes.status === "failed" && [ "resource_conflict", "account_denied", "reauthorization_required", ].includes(changes.errorCode ?? ""); if ( (current.writeIntent && !changes.writeIntent && !definitiveRejection) || (current.domainId && current.domainId !== changes.domainId) || (current.action === "attach" && ["removing", "removed"].includes(changes.status)) || (current.action === "remove" && ["connecting", "active"].includes(changes.status)) || (changes.status === "active" && (!changes.domainId || !changes.writeIntent)) ) throw new InstallationConflict(); return parse(domainRecord, { ...current, ...changes, revision: current.revision + 1, });}