Drip-Creator / lib /cloudflareConfig.ts
Alexander Canji
fix(space): Docker SDK auf Port 7860, Secrets entfernt, PWA + API-Härtung
19e93fc
Raw History Blame Contribute Delete
1.72 kB
/* ============================================================
* Cloudflare-Konfigurations-Validierung
* Verhindert den häufigsten Fehler: Token in der Account-ID.
* ============================================================ */
/** Echte Cloudflare Account-IDs sind exakt 32 Hex-Zeichen. */
export const CF_ACCOUNT_ID_RE = /^[a-f0-9]{32}$/i;
/** Neue scannbare Cloudflare-Credential-Formate (Token, KEINE Account-ID). */
const CF_TOKEN_PREFIXES = /^(cfat_|cfut_|cfk_)/i;
/**
* true, wenn in CF_ACCOUNT_ID versehentlich ein API-Token steht
* (z. B. "cfat_…") statt der 32-stelligen hexadezimalen Account-ID.
*/
export function cfAccountLooksLikeToken(accountId: string): boolean {
return CF_TOKEN_PREFIXES.test(accountId.trim());
}
/** Konfigurationsstatus für /api/health (keine Werte, nur Diagnose). */
export function cloudflareStatus(): { configured: boolean; issue: string | null } {
const id = process.env.CF_ACCOUNT_ID?.trim() || "";
const token = process.env.CF_API_TOKEN?.trim() || "";
if (!id && !token) return { configured: false, issue: null }; // einfach nicht gesetzt
if (!id) return { configured: false, issue: "CF_ACCOUNT_ID fehlt (32-stellige hex Account-ID aus dem Dashboard)." };
if (!token) return { configured: false, issue: "CF_API_TOKEN fehlt (API-Token mit Workers-AI-Berechtigung)." };
if (cfAccountLooksLikeToken(id))
return { configured: false, issue: "CF_ACCOUNT_ID enthält einen Token (cfat_/cfut_) — dort gehört die 32-stellige hex Account-ID hin." };
if (!CF_ACCOUNT_ID_RE.test(id))
return { configured: false, issue: "CF_ACCOUNT_ID hat ein unerwartetes Format (erwartet: 32 Hex-Zeichen)." };
return { configured: true, issue: null };
}