Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | 13x 13x 10x 2x 2x 8x 6x 6x 6x 2x 2x 4x 4x 4x 2x 2x 2x 2x 2x 2x 3x 3x 15x 15x 15x 12x 11x 11x 11x 4x 4x 12x 8x 8x 8x 4x 4x 4x 4x 4x 3x 3x 1x 12x 12x 1x 12x 3x 3x 16x 16x 13x 12x 12x 12x 4x 4x 9x 9x 9x 4x 4x 5x 5x 5x 3x 3x 2x 2x 3x 3x | /**
* Cloudflare Pages Project & Domain Utilities
*
* Manages Pages project creation, custom domain attachment,
* and external domain attachment via the Cloudflare REST API.
*
* Called by the publish endpoint BEFORE enqueueing a build job,
* so domain infrastructure is guaranteed to exist when the container deploys.
*/
import {
CF_API_BASE,
type CloudflareApiResponse,
ensureDnsCname,
} from "./dns";
/**
* Ensure a Cloudflare Pages project exists. Creates it if it doesn't.
*
* @returns true if the project exists (or was created), false on error
*/
export async function ensurePagesProject(
projectName: string,
apiToken: string,
accountId: string,
): Promise<boolean> {
try {
// Check if project already exists
const getResponse = await fetch(
`${CF_API_BASE}/accounts/${accountId}/pages/projects/${projectName}`,
{
headers: {
Authorization: `Bearer ${apiToken}`,
},
},
);
if (getResponse.ok) {
console.log(`[Pages] Project already exists: ${projectName}`);
return true;
}
// 404 means project doesn't exist — create it
if (getResponse.status === 404) {
console.log(`[Pages] Creating project: ${projectName}`);
const createResponse = await fetch(
`${CF_API_BASE}/accounts/${accountId}/pages/projects`,
{
method: "POST",
headers: {
Authorization: `Bearer ${apiToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
name: projectName,
production_branch: "main",
}),
},
);
if (createResponse.ok) {
console.log(`[Pages] Created project: ${projectName}`);
return true;
}
const errorData =
(await createResponse.json()) as CloudflareApiResponse;
// "already exists" race condition is fine
const errorStr = JSON.stringify(errorData.errors);
if (errorStr.includes("already exists")) {
console.log(`[Pages] Project already exists (race): ${projectName}`);
return true;
}
console.error(
`[Pages] Failed to create project ${projectName}: ${errorStr}`,
);
return false;
}
console.error(
`[Pages] Unexpected status checking project ${projectName}: ${getResponse.status}`,
);
return false;
} catch (error) {
console.error(
`[Pages] Error ensuring project ${projectName}: ${error instanceof Error ? error.message : error}`,
);
return false;
}
}
/**
* Ensure a custom domain is attached to a Cloudflare Pages project
* AND the corresponding DNS CNAME record exists.
*
* Two steps:
* 1. Add the domain to the Pages project (CF Pages custom domain)
* 2. Create a CNAME DNS record pointing to {project}.pages.dev
*
* @returns true if both domain attachment and DNS are set up, false on error
*/
export async function ensureCustomDomain(
projectName: string,
domain: string,
apiToken: string,
accountId: string,
): Promise<boolean> {
try {
// Step 1: Attach domain to Pages project
let domainAttached = false;
const listResponse = await fetch(
`${CF_API_BASE}/accounts/${accountId}/pages/projects/${projectName}/domains`,
{
headers: {
Authorization: `Bearer ${apiToken}`,
"Content-Type": "application/json",
},
},
);
if (listResponse.ok) {
const listData = (await listResponse.json()) as CloudflareApiResponse<
Array<{ name: string }>
>;
const existingDomains = listData.result || [];
if (existingDomains.some((d) => d.name === domain)) {
console.log(`[Pages] Domain already attached: ${domain}`);
domainAttached = true;
}
}
if (!domainAttached) {
console.log(`[Pages] Attaching domain ${domain} to ${projectName}`);
const addResponse = await fetch(
`${CF_API_BASE}/accounts/${accountId}/pages/projects/${projectName}/domains`,
{
method: "POST",
headers: {
Authorization: `Bearer ${apiToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ name: domain }),
},
);
if (addResponse.ok) {
console.log(`[Pages] Domain attached: ${domain}`);
domainAttached = true;
} else {
const errorData =
(await addResponse.json()) as CloudflareApiResponse;
const errorStr = JSON.stringify(errorData.errors);
if (
errorStr.includes("already exists") ||
errorStr.includes("already been added")
) {
console.log(`[Pages] Domain already attached (race): ${domain}`);
domainAttached = true;
} else {
console.error(
`[Pages] Failed to attach domain ${domain}: ${errorStr}`,
);
}
}
}
// Step 2: Ensure DNS CNAME record exists
const dnsCreated = await ensureDnsCname(domain, projectName, apiToken);
if (!dnsCreated) {
console.warn(
`[Pages] Domain attached but DNS CNAME creation failed for ${domain}. ` +
`You may need to manually create a CNAME: ${domain} → ${projectName}.pages.dev`,
);
}
return domainAttached;
} catch (error) {
console.error(
`[Pages] Error ensuring domain ${domain}: ${error instanceof Error ? error.message : error}`,
);
return false;
}
}
/**
* Attach a domain to a Cloudflare Pages project WITHOUT creating DNS records.
*
* Used for pro-owned external domains where the pro manages DNS.
* For platform subdomains (*.decorrocket.com), use `ensureCustomDomain` instead.
*/
export async function attachDomainToPages(
projectName: string,
domain: string,
apiToken: string,
accountId: string,
): Promise<boolean> {
try {
// Check if domain is already attached
const listResponse = await fetch(
`${CF_API_BASE}/accounts/${accountId}/pages/projects/${projectName}/domains`,
{
headers: {
Authorization: `Bearer ${apiToken}`,
"Content-Type": "application/json",
},
},
);
if (listResponse.ok) {
const listData = (await listResponse.json()) as CloudflareApiResponse<
Array<{ name: string }>
>;
const existingDomains = listData.result || [];
if (existingDomains.some((d) => d.name === domain)) {
console.log(`[Pages] External domain already attached: ${domain}`);
return true;
}
}
// Attach domain to Pages project
console.log(
`[Pages] Attaching external domain ${domain} to ${projectName}`,
);
const addResponse = await fetch(
`${CF_API_BASE}/accounts/${accountId}/pages/projects/${projectName}/domains`,
{
method: "POST",
headers: {
Authorization: `Bearer ${apiToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ name: domain }),
},
);
if (addResponse.ok) {
console.log(`[Pages] External domain attached: ${domain}`);
return true;
}
const errorData = (await addResponse.json()) as CloudflareApiResponse;
const errorStr = JSON.stringify(errorData.errors);
if (
errorStr.includes("already exists") ||
errorStr.includes("already been added")
) {
console.log(`[Pages] External domain already attached (race): ${domain}`);
return true;
}
console.error(
`[Pages] Failed to attach external domain ${domain}: ${errorStr}`,
);
return false;
} catch (error) {
console.error(
`[Pages] Error attaching external domain ${domain}: ${error instanceof Error ? error.message : error}`,
);
return false;
}
}
|