Files
0pt/src/platform/dependency-mode.ts
T
Simon AxelssonandClaude Fable 5 c47e3b468e
CI (SIAX Cloud) / integration (push) Failing after 22s
CI (SIAX Cloud) / security (push) Successful in 22s
Security Scan (SIAX Cloud) / security (push) Successful in 21s
CI (SIAX Cloud) / quality (push) Successful in 43s
CI (SIAX Cloud) / integration (pull_request) Failing after 3s
CI (SIAX Cloud) / security (pull_request) Successful in 17s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 16s
CI (SIAX Cloud) / quality (pull_request) Successful in 43s
test(integration)+ci+docs: live OpenSearch/ClickHouse suites, CI service containers, STATUS.md (§56)
- tests/integration/: 10 tests against REAL OpenSearch 2.11 (tenant
  isolation, purpose/rights filters, generation rebuild/rollback,
  projection-disaster rebuild §68) + REAL ClickHouse 24.8 (tenant-isolated
  metric DSL, TTL schema, injection-surface refusals). Honest skip without
  env - and CI fails hard if the suites skip instead of running.
- .gitea/workflows/ci.yml: new blocking 'integration' job with
  self-identifying service containers (st0re ci.yml's measured
  alias-collision discipline: run-unique cluster.name / credentials,
  getent-enumerated IP selection, no-match = hard fail).
- STATUS.md: full 8-level Master-DoD classification per capability incl.
  the honest gaps blocking PRODUCTION_READY.
- dependency-mode: NEXT_PHASE=phase-production-build is not a serving
  runtime (next build page-data collection must compile; guard re-arms at
  boot) - pinned by contract test.
- docs/02-architecture.md: superseded the accurate-but-now-fixed 'every
  client runs against its mock' paragraph; .env.example: client URLs now
  actually read from env + new P0 backend vars.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-31 16:56:33 +02:00

91 lines
3.7 KiB
TypeScript

/**
* 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" };
}