Something went wrong. Try again.
Stable episode delivery and analytics on pod.place
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142import { normalizeIp } from "./origins";import type { PlatformCredential } from "./types";
function positiveInteger(value: string | undefined, fallback: number, name: string): number { const number = value === undefined ? fallback : Number(value); if (!Number.isSafeInteger(number) || number <= 0) throw new Error(`${name} must be a positive integer`); return number;}
export function readConfig(env: Record<string, string | undefined> = process.env) { const base = new URL(env.PUBLIC_BASE_URL ?? "https://pod.place"); if (base.protocol !== "https:" || base.username || base.password || base.port || base.pathname !== "/" || base.search || base.hash) { throw new Error("PUBLIC_BASE_URL must be the stable public HTTPS origin"); } if (env.PUBLISHER_CREDENTIALS && !env.PLATFORM_CREDENTIALS) { throw new Error("Replace PUBLISHER_CREDENTIALS with PLATFORM_CREDENTIALS containing {platform, token} entries"); } let credentials: PlatformCredential[]; try { credentials = JSON.parse(env.PLATFORM_CREDENTIALS ?? "[]"); } catch { throw new Error("PLATFORM_CREDENTIALS must be a JSON array"); } if (!Array.isArray(credentials)) throw new Error("PLATFORM_CREDENTIALS must be a JSON array"); const analyticsSecret = env.ANALYTICS_SECRET || undefined; if (analyticsSecret && analyticsSecret.length < 32) throw new Error("ANALYTICS_SECRET must contain at least 32 characters"); const trustedProxies = new Set((env.TRUSTED_PROXY_IPS ?? "").split(",").filter(Boolean).map((ip) => { const normalized = normalizeIp(ip.trim()); if (!normalized) throw new Error("TRUSTED_PROXY_IPS must contain literal IP addresses"); return normalized; })); const freshnessMs = positiveInteger(env.MAPPING_FRESHNESS_SECONDS, 300, "MAPPING_FRESHNESS_SECONDS") * 1000; const maxAgeMs = positiveInteger(env.MAPPING_MAX_AGE_SECONDS, 21_600, "MAPPING_MAX_AGE_SECONDS") * 1000; if (freshnessMs >= maxAgeMs) throw new Error("Mapping freshness must be shorter than the maximum verification age"); const port = positiveInteger(env.PORT, 3000, "PORT"); if (port > 65535) throw new Error("PORT must be at most 65535"); return { port, hostname: env.HOST ?? "0.0.0.0", baseUrl: base.origin, databasePath: env.DATABASE_PATH ?? "./data/resolver.sqlite", slingshotUrl: env.SLINGSHOT_URL ?? "https://slingshot.microcosm.blue", credentials, analyticsSecret, trustedProxies, freshnessMs, maxAgeMs, retentionDays: positiveInteger(env.ANALYTICS_RETENTION_DAYS, 7, "ANALYTICS_RETENTION_DAYS"), };}