/** * src/platform/dependency-mode.ts * ================================ * THE production mock-fallback kill switch (Master-DoD §3: "Ingen * production-path faller tyst tillbaka till mock/in-memory", §10: "Production * kan inte falla tillbaka till mock"). * * Before this module, every platform client in src/clients/* documented the * same convention: "when no base URL is configured the client runs in * LOCAL/MOCK/NOOP mode". That convention is fine for unit tests and local * dev, and it is EXACTLY the silent-fallback failure mode the Master-DoD * forbids in production. This module centralizes the decision so every * factory makes it the same way: * * - production runtime + no URL → throw SiaxError DEPENDENCY_UNAVAILABLE * at CONSTRUCTION time (fail hard, fail early — not on first call). * - non-production + no URL → the factory may return its local/mock * implementation (hermetic tests, local dev). * - URL configured → real HTTP client, any runtime. * * "Production runtime" means NODE_ENV=production, the same switch the * existing auth layer uses (src/auth/context.ts refuses AUTH_ENFORCE=false * under NODE_ENV=production). There is deliberately NO override that lets * production run on mocks: if a deployment genuinely wants a dependency off, * that is a topology decision expressed by NOT calling the capability, never * by a mock silently answering it. */ import { SiaxError } from "./errors"; /** * True when this process is a production runtime — i.e. it can SERVE * requests. `next build` runs page-data collection under NODE_ENV=production * while serving nothing; failing the BUILD for unset dependency URLs would * block the compile step, not protect any request path, so the Next build * phase (NEXT_PHASE=phase-production-build) is explicitly not a production * runtime. The guard re-arms the moment the built app boots (`next start` * clears NEXT_PHASE). */ export function isProductionRuntime(env: NodeJS.ProcessEnv = process.env): boolean { if (env.NEXT_PHASE === "phase-production-build") return false; return env.NODE_ENV === "production"; } /** * Resolve a dependency base URL for a client factory. * * Returns the URL (trimmed, no trailing slash) when configured. Returns * `undefined` when unconfigured in a non-production runtime — the caller may * then use its documented local/mock implementation. THROWS * `SiaxError("DEPENDENCY_UNAVAILABLE")` when unconfigured in production. */ export function resolveDependencyBaseUrl( dependency: string, configuredUrl: string | undefined, env: NodeJS.ProcessEnv = process.env ): string | undefined { const url = configuredUrl?.trim(); if (url) return url.replace(/\/+$/, ""); if (isProductionRuntime(env)) { throw new SiaxError( "DEPENDENCY_UNAVAILABLE", dependency, `no base URL configured for required dependency "${dependency}" in a production runtime; ` + `refusing to fall back to a mock/in-memory implementation (Master-DoD §3/§10)` ); } return undefined; } /** * Same decision for stateful stores (Postgres-backed registries): production * with no DATABASE_URL must fail hard rather than serve an in-memory Map. */ export function resolveStoreMode( store: string, databaseUrl: string | undefined, env: NodeJS.ProcessEnv = process.env ): { mode: "postgres"; url: string } | { mode: "memory" } { const url = databaseUrl?.trim(); if (url) return { mode: "postgres", url }; if (isProductionRuntime(env)) { throw new SiaxError( "DEPENDENCY_UNAVAILABLE", store, `no database configured for persistent store "${store}" in a production runtime; ` + `refusing the in-memory fallback (Master-DoD §3/§10)` ); } return { mode: "memory" }; }