import type { DomainCommandPayload, DomainResultPayload } from "./contracts.js"; import type { DomainProbes } from "./probes.js"; import { DomainProbeError } from "./probes.js"; import { GenerationConflictError, TraefikConfigWriter, UnsafeConfigPathError } from "./traefik.js"; export const DOMAIN_RETRY_DELAYS_MS = [15 * 60_000, 60 * 60_000, 6 * 60 * 60_000] as const; export const DOMAIN_MAX_ATTEMPTS = DOMAIN_RETRY_DELAYS_MS.length + 1; export class DomainControllerFailure extends Error { constructor( readonly reasonCode: string, readonly retryable: boolean, message: string ) { super(message); } } export type PortalDomainController = { handle(command: DomainCommandPayload, attempt: number): Promise; }; export function createPortalDomainController(options: { writer: TraefikConfigWriter; probes: DomainProbes; now?: () => Date }): PortalDomainController { const now = options.now ?? (() => new Date()); return { async handle(command, attempt) { try { if (command.action === "deprovision") { const mutation = await options.writer.applyDeprovision(command); return result(command, attempt, now(), mutation.disposition === "stale" ? "stale" : "deprovisioned"); } await options.probes.verifyDns(command.hostname); if (command.action === "provision") { const mutation = await options.writer.applyProvision(command); if (mutation.disposition === "stale") return result(command, attempt, now(), "stale"); const health = await options.probes.verifyHttps(command.hostname); const current = await options.writer.readAppliedState(command.claimId); if (!current || current.generation !== command.generation || current.action !== command.action) { return result(command, attempt, now(), "stale"); } return result(command, attempt, now(), "active", mutation.configHash, health.certificateExpiresAt); } const mutation = await options.writer.recordRevalidation(command); if (mutation.disposition === "stale") return result(command, attempt, now(), "stale"); const health = await options.probes.verifyHttps(command.hostname); const current = await options.writer.readAppliedState(command.claimId); if (!current || current.generation !== command.generation || (current.action !== "provision" && current.action !== "revalidate")) { return result(command, attempt, now(), "stale"); } return result(command, attempt, now(), "healthy", undefined, health.certificateExpiresAt); } catch (error) { throw classifyControllerError(error); } }, }; } export function retryDelayForAttempt(attemptsMade: number): number { const index = Math.max(0, Math.min(attemptsMade - 1, DOMAIN_RETRY_DELAYS_MS.length - 1)); return DOMAIN_RETRY_DELAYS_MS[index] ?? DOMAIN_RETRY_DELAYS_MS[DOMAIN_RETRY_DELAYS_MS.length - 1]; } export function classifyControllerError(error: unknown): DomainControllerFailure { if (error instanceof DomainControllerFailure) return error; if (error instanceof DomainProbeError) return new DomainControllerFailure(error.reasonCode, true, error.message); if (error instanceof UnsafeConfigPathError || error instanceof GenerationConflictError) { return new DomainControllerFailure(error.reasonCode, false, error.message); } if (isErrno(error, "EACCES") || isErrno(error, "EPERM") || isErrno(error, "EROFS")) { return new DomainControllerFailure("config_permission_denied", false, "The dedicated Traefik config directory is not writable"); } return new DomainControllerFailure("controller_internal_error", true, "The portal domain controller encountered a transient internal error"); } function result( command: DomainCommandPayload, attempt: number, completedAt: Date, status: DomainResultPayload["status"], configHash?: string, certificateExpiresAt?: string ): DomainResultPayload { return { ...command, status, attempt, completedAt: completedAt.toISOString(), ...(configHash ? { configHash } : {}), ...(certificateExpiresAt ? { certificateExpiresAt } : {}), }; } function isErrno(error: unknown, code: string): boolean { return typeof error === "object" && error !== null && "code" in error && (error as NodeJS.ErrnoException).code === code; }