import { env } from "cloudflare:workers";
import { all, first, run } from "../db";
import { randomId } from "../crypto";
import { HttpError, badRequest } from "../errors";
import { manifestKey, publishMeta, SITE_LIMITS, slotKey } from "./config";
import { isValidLabel, splitWorker } from "./naming";
import { isOrphanSite } from "./sync";
import type { SiteDeploymentRow, SiteEnvironment, SiteManifest, SiteRow, SlotPointer } from "./config";
import type { CollectedFiles } from "../pages/collect";
const HOUR = 60 * 60 * 1000;
const HTTP_TOO_MANY = 429;
export type SiteDeployRequest = {
site: SiteRow;
environment: SiteEnvironment;
label: string;
branch: string | null;
ref: string;
sha: string;
artifactId: string | null;
source: SiteDeploymentRow["source"];
creatorId: string | null;
};
export async function loadSiteById(id: string): Promise<SiteRow | null> {
return first<SiteRow>("SELECT * FROM sites WHERE id = ?", id);
}
export async function loadSiteForRepo(repoId: string): Promise<SiteRow | null> {
return first<SiteRow>("SELECT * FROM sites WHERE repo_id = ?", repoId);
}
export async function loadSiteDeployment(id: string): Promise<SiteDeploymentRow | null> {
return first<SiteDeploymentRow>("SELECT * FROM site_deployments WHERE id = ?", id);
}
/** Labels are first-come: a preview label may never take over another site's production or preview host. */
export async function assertLabelFree(siteId: string, label: string): Promise<void> {
if (!isValidLabel(label)) throw badRequest(`"${label}" is not a valid site host`);