everything-dev
Version:
A consolidated product package for building Module Federation apps with oRPC APIs.
66 lines (64 loc) • 2.69 kB
JavaScript
const require_schema = require("../snapshot/schema.cjs");
//#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 require_schema.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
exports.BOOT_SNAPSHOT_ENV_KEY = BOOT_SNAPSHOT_ENV_KEY;
exports.bootSnapshotEnv = bootSnapshotEnv;
exports.createRegistryConfigSource = createRegistryConfigSource;
//# sourceMappingURL=snapshot-source.cjs.map