UNPKG

everything-dev

Version:

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

1 lines • 6.94 kB
{"version":3,"file":"boot.mjs","names":[],"sources":["../../../src/services/lockfile/boot.ts"],"sourcesContent":["/**\n * The boot-side lockfile application (ADR 0042 ticket 03): when a runtime\n * boots with a `bos.lock.json` present, the frozen composition is the\n * authority for remote coordinates — frozen descriptors stamp over the\n * resolved config (filling missing pins, overriding drifted ones), and every\n * disagreement is reported to the caller. The boot never fails BECAUSE of the\n * lockfile: reproducibility wins silently-when-agreeing, loudly-when-not\n * (the warnings ride the start summary), while `bos lock --check` in CI is\n * the gate that keeps the committed lockfile fresh.\n */\nimport type { BosConfig } from \"../../types\";\nimport type { CompositionLockfile } from \"./schema\";\n\nexport interface LockfileDrift {\n path: string;\n detail: string;\n}\n\nexport interface LockfileApplication {\n config: BosConfig;\n /** Dotted paths where the frozen descriptor was stamped — the resolved\n * remote had no pin of its own. */\n applied: string[];\n /** Dotted paths where the serving composition disagreed with the frozen\n * one — the frozen composition wins, the disagreement is reported. */\n drift: LockfileDrift[];\n}\n\nfunction segmentsOf(remotePath: string): string[] {\n return remotePath.split(\".\");\n}\n\nfunction entryAt(root: Record<string, unknown>, segments: string[]): unknown {\n let cursor: unknown = root;\n for (const segment of segments) {\n if (!cursor || typeof cursor !== \"object\") return undefined;\n cursor = (cursor as Record<string, unknown>)[segment];\n }\n return cursor;\n}\n\n/** Clone-and-set along the path: untouched branches keep their identity. */\nfunction setIn(\n root: Record<string, unknown>,\n segments: string[],\n value: unknown,\n): Record<string, unknown> {\n if (segments.length === 0) return root;\n const clone: Record<string, unknown> = { ...root };\n const [head, ...rest] = segments;\n const headKey = head!;\n if (rest.length === 0) {\n clone[headKey] = value;\n return clone;\n }\n const child = root[headKey];\n clone[headKey] =\n child && typeof child === \"object\"\n ? setIn(child as Record<string, unknown>, rest, value)\n : setIn({}, rest, value);\n return clone;\n}\n\nexport function applyCompositionLockfile(\n config: BosConfig,\n lockfile: CompositionLockfile,\n): LockfileApplication {\n const applied: string[] = [];\n const drift: LockfileDrift[] = [];\n let working: Record<string, unknown> = config as unknown as Record<string, unknown>;\n let mutated = false;\n\n const runtimeRef = `bos://${config.account}/${config.domain}`;\n if (lockfile.runtimeRef !== runtimeRef) {\n return {\n config,\n applied,\n drift: [\n {\n path: \"*\",\n detail: `the lockfile freezes ${lockfile.runtimeRef}, not this runtime (${runtimeRef}) — not applied`,\n },\n ],\n };\n }\n\n for (const [remotePath, entry] of Object.entries(lockfile.remotes)) {\n const segments = segmentsOf(remotePath);\n const current = entryAt(working, segments);\n if (entry.kind === \"pluginRef\") {\n if (typeof current === \"string\") {\n if (current !== entry.ref) {\n drift.push({\n path: remotePath,\n detail: `plugin ref ${current} differs from the frozen ${entry.ref}`,\n });\n working = setIn(working, segments, entry.ref);\n mutated = true;\n }\n continue;\n }\n drift.push({\n path: remotePath,\n detail: `the lockfile freezes a bare plugin ref but the resolved composition carries an entry`,\n });\n continue;\n }\n\n if (!current || typeof current !== \"object\") {\n drift.push({\n path: remotePath,\n detail: \"the resolved composition no longer carries this remote — run `bos lock`\",\n });\n continue;\n }\n const ref = current as {\n production?: unknown;\n digest?: { manifest?: unknown; integrity?: unknown };\n };\n const frozenDigest =\n entry.digest && typeof entry.digest.manifest === \"string\" ? entry.digest.manifest : undefined;\n const servingDigest =\n typeof ref.digest?.manifest === \"string\" ? ref.digest.manifest : undefined;\n if (!frozenDigest) continue;\n if (servingDigest && servingDigest === frozenDigest) continue;\n if (servingDigest) {\n drift.push({\n path: remotePath,\n detail: `serving ${servingDigest} differs from the frozen ${frozenDigest}`,\n });\n } else {\n applied.push(remotePath);\n }\n const base = entry.base ?? (typeof ref.production === \"string\" ? ref.production : undefined);\n const stamped: Record<string, unknown> = { ...ref };\n if (base) stamped.production = base;\n if (entry.digest) stamped.digest = entry.digest;\n working = setIn(working, segments, stamped);\n mutated = true;\n }\n\n return {\n config: mutated ? (working as unknown as BosConfig) : config,\n applied,\n drift,\n };\n}\n"],"mappings":";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