import { env } from "cloudflare:workers"; export const PAGES_LIMITS = { maxFiles: 5000, maxFileBytes: 25 * 1024 * 1024, maxTotalBytes: 512 * 1024 * 1024, maxArtifactBytes: 1024 * 1024 * 1024, deploysPerRepoPerHour: 30, uploadBatch: 100, historyLimit: 50, } as const; export type PagesEnvironment = "production" | "preview"; export type PagesSource = "branch" | "workflow"; export type DeploymentStatus = "queued" | "building" | "active" | "failed" | "superseded"; export type PagesSiteRow = { repo_id: string; source: PagesSource; branch: string; folder: string; artifact_name: string; spa: number; previews: number; custom_domain: string | null; custom_hostname_id: string | null; domain_status: string | null; domain_claimed_at: number | null; active_deployment_id: string | null; created_at: number; updated_at: number; }; export type PagesDeploymentRow = { id: string; repo_id: string; environment: PagesEnvironment; pr_number: number | null; source: "branch" | "artifact"; ref: string; sha: string; artifact_id: string | null; status: DeploymentStatus; file_count: number; total_bytes: number; error: string | null; creator_id: string | null; created_at: number; completed_at: number | null; }; /** One manifest file entry: git blob sha (the R2 content address) and size. */ export type ManifestFile = [sha: string, size: number]; export type Manifest = { files: Record }; /** KV pointer from a served slot to the deployment it currently resolves to. */ export type SitePointer = { deploymentId: string; spa: boolean }; export function objectKey(sha: string): string { return `pages/objects/${sha}`; } export function manifestKey(deploymentId: string): string { return `pages:manifest:${deploymentId}`; } /** Slot key: production lives at `/`, previews at `/#pr-`. */ export function slotKey(owner: string, repo: string, prNumber: number | null): string { const base = `pages:site:${owner.toLowerCase()}/${repo.toLowerCase()}`; return prNumber === null ? base : `${base}#pr-${prNumber}`; } export function hostKey(hostname: string): string { return `pages:host:${hostname.toLowerCase()}`; } function forgeHost(): string { return new URL(env.PUBLIC_URL).hostname.toLowerCase(); } /** * The Pages apex domain, or null when Pages hosting is disabled. It must not contain the forge host, * otherwise user content could read or set forge cookies. */ export function pagesDomain(): string | null { const domain = (env.PAGES_DOMAIN ?? "").trim().toLowerCase(); if (!domain) return null; const forge = forgeHost(); if (forge === domain || forge.endsWith(`.${domain}`)) return null; // Sibling hosts under one registrable domain can toss cookies for each other, so Pages needs its own site. if (registrableDomain(domain) === registrableDomain(forge)) return null; return domain; } export function isForgeHost(hostname: string): boolean { return hostname.toLowerCase() === forgeHost(); } /** Second-level labels that form public suffixes under country TLDs (`co.uk`, `com.au`, ...). */ const COUNTRY_SECOND_LEVELS = new Set(["co", "com", "net", "org", "gov", "edu", "ac", "ne", "or", "go", "gv"]); /** Approximates the registrable domain (eTLD+1) without a public suffix list. */ function registrableDomain(hostname: string): string { const labels = hostname.toLowerCase().split("."); const [secondLevel, tld] = labels.slice(-2); const multiPartSuffix = labels.length > 2 && tld?.length === 2 && COUNTRY_SECOND_LEVELS.has(secondLevel ?? ""); return labels.slice(multiPartSuffix ? -3 : -2).join("."); } /** * Custom domains on the forge's own registrable domain could set cookies for the forge host, so the forge * host, its parent domains and any sibling under the same registrable domain are refused. */ export function isForgeRelatedHost(hostname: string): boolean { const host = hostname.toLowerCase(); const forge = forgeHost(); if (!forge) return false; const site = registrableDomain(forge); return host === forge || host === site || host.endsWith(`.${site}`) || forge.endsWith(`.${host}`); } export function siteUrl(owner: string, repo: string, prNumber: number | null): string | null { const domain = pagesDomain(); if (!domain) return null; const host = `${owner.toLowerCase()}.${domain}`; const userSite = repo.toLowerCase() === host; const base = userSite ? `https://${host}/` : `https://${host}/${repo}/`; return prNumber === null ? base : `${base}_pr/${prNumber}/`; } const HOSTNAME = /^(?=.{4,253}$)([a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}$/; export function isValidHostname(hostname: string): boolean { return HOSTNAME.test(hostname); }