UNPKG

everything-dev

Version:

A consolidated product package for building Module Federation apps with oRPC APIs.

64 lines (62 loc) • 2.6 kB
import { verifiedBootSnapshot } from "../snapshot/schema.mjs"; //#region src/services/start/snapshot-source.ts const describe = (error) => error instanceof Error ? error.message : String(error); /** The degraded boot's marker (ADR 0041): the host process was started from * the approved boot snapshot, so /health carries composition provenance until * the watch adopts a published generation. */ const BOOT_SNAPSHOT_ENV_KEY = "BOS_BOOT_SNAPSHOT"; function bootSnapshotEnv(source) { return source === "snapshot" ? { [BOOT_SNAPSHOT_ENV_KEY]: "1" } : {}; } function createRegistryConfigSource(deps) { return { resolve: async (input) => { let fetched; try { fetched = await deps.fetchPublishedConfig(input.account, input.gateway, input.registry); } catch (fetchError) { if (!input.allowSnapshot) throw fetchError; const snapshot = await usableSnapshot(input, fetchError); if (!snapshot) throw fetchError; const tag = snapshot.manifestTag ? `, ${snapshot.manifestTag.version}` : ""; return { config: snapshot.config, source: "snapshot", warnings: [`registry unreachable — serving the approved boot snapshot (written ${snapshot.writtenAt}${tag}); the pointer watch adopts the published config when the registry returns`] }; } if (!fetched) return null; const warnings = []; try { await deps.persistSnapshot(input.account, input.gateway, fetched); } catch (error) { warnings.push(`approved-snapshot persist failed (non-fatal): ${describe(error)}`); } return { config: fetched, source: "registry", warnings }; } }; /** Absent snapshot → null (the original transport error is rethrown by the * caller). Corrupt or unverifiable → a combined error naming both causes. */ async function usableSnapshot(input, fetchError) { let loaded; try { loaded = await deps.loadSnapshot(input.account, input.gateway, input.dir); } catch (snapshotError) { throw new Error(bothBroken(fetchError, snapshotError)); } if (!loaded) return null; try { return verifiedBootSnapshot(loaded); } catch (snapshotError) { throw new Error(bothBroken(fetchError, snapshotError)); } } function bothBroken(fetchError, snapshotError) { return `the registry is unreachable (${describe(fetchError)}), and the approved boot snapshot is unusable (${describe(snapshotError)}) — refusing to serve an unverified composition (ADR 0041; BOS_SNAPSHOT_BOOT=never disables the snapshot rung)`; } } //#endregion export { BOOT_SNAPSHOT_ENV_KEY, bootSnapshotEnv, createRegistryConfigSource }; //# sourceMappingURL=snapshot-source.mjs.map