UNPKG

everything-dev

Version:

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

235 lines (233 loc) • 10.5 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const require_runtime = require("../../_virtual/_rolldown/runtime.cjs"); const require_remotes = require("../remotes.cjs"); const require_services_registry_digest = require("../registry/digest.cjs"); let node_fs = require("node:fs"); let node_path = require("node:path"); node_path = require_runtime.__toESM(node_path, 1); let zod = require("zod"); let node_crypto = require("node:crypto"); let node_fs_promises = require("node:fs/promises"); //#region src/services/lockfile/schema.ts /** * The composition lockfile's wire schema (ADR 0042) — a leaf module so the * producers (`bos lock`, the deploy train), the boot consumer, and the * runtime-map generator can all import it without a cycle. The lockfile * freezes one runtime's exact composition: what the author wants * (`bos.app.ts`) resolved to named bytes. The pinned descriptor's `{base, * digest}` shape is deliberately the release manifest's `remotes:` entry shape * (ADR 0040) — one vocabulary for the same bytes; the scope differs (the * platform's per-release memory of inherited slots vs this app's frozen * whole). The `remotes` record is written in `walkRemotes` order — including * entries that freeze nothing — so the stored pointer digest recomputes exactly * (the `pointerDigest` field ≡ `pointerDigest(config)` over the same config), and * JSON round-trips preserve that order. */ const LOCKFILE_NAME = "bos.lock.json"; /** A frozen remote coordinate. `descriptor` mirrors the release manifest's * remote entry — `digest` (the immutable manifest pointer), a direct entry * `integrity` (digest-less fixed-name remotes), and the `base` they resolve * against; fields are individually optional because the freeze is faithful to * what the config carries, not to what a producer wishes it carried. * `pluginRef` freezes a bare plugin reference (`plugins.<id>: "<key>"`). */ const LockfileRemoteSchema = zod.z.discriminatedUnion("kind", [zod.z.object({ kind: zod.z.literal("descriptor"), base: zod.z.string().min(1).optional(), digest: zod.z.object({ manifest: zod.z.string().min(1), integrity: zod.z.string().min(1) }).optional(), integrity: zod.z.string().min(1).optional() }).strict(), zod.z.object({ kind: zod.z.literal("pluginRef"), ref: zod.z.string().min(1) }).strict()]); const CompositionLockfileSchema = zod.z.object({ schemaVersion: zod.z.literal(2), kind: zod.z.literal("everything-dev/composition-lockfile"), runtimeRef: zod.z.string().regex(/^bos:\/\/[^/]+\/[^/]+$/), /** The release the composition resolves to; null when the runtime is * unpinned — the lockfile still freezes what channel resolution * returned. */ release: zod.z.string().min(1).nullable(), /** The frozen composition, in `walkRemotes` order (dotted config path → * coordinate). Order is load-bearing: the pointer digest recomputes from it. */ remotes: zod.z.record(zod.z.string(), LockfileRemoteSchema), /** Shared-singleton versions the composition is verified against — * recorded when the sources provide them (remote plugin manifests, the * release manifest's `sharedDeps`); tightened as producers mature. */ sharedSingletons: zod.z.record(zod.z.string(), zod.z.string()).optional(), /** Per-remote contract hash (`plugin.manifest.json`'s * `contract.types.sha256`). */ contracts: zod.z.record(zod.z.string(), zod.z.string()).optional(), /** The template sync baseline (ADR 0047 — the absorbed * `.bos/sync-snapshot.json`): per-file template hashes recorded at * init/sync/import, read by `bos sync`'s three-way comparison. A local * annotation, never produced by `lockfileOf` — writers carry it forward * from the existing file, and it never enters the lockfile digest (the * digest answers "what exact composition was live"; the baseline is not * composition). */ template: zod.z.object({ syncedAt: zod.z.string().min(1), files: zod.z.record(zod.z.string(), zod.z.string()) }).strict().optional(), /** THE composition identity — equal to `pointerDigest(config)` by * construction (`lockfileOf`), recomputable from `remotes` alone * (`lockfilePointerDigest`). */ pointerDigest: zod.z.string().regex(/^[0-9a-f]{16}$/), resolvedAt: zod.z.string().min(1) }).strict(); function lockfilePath(configDir = process.cwd()) { return node_path.default.join(configDir, LOCKFILE_NAME); } function descriptorPart(remote, entry) { return `${remote}:${entry.digest ? `${entry.digest.manifest}:${entry.digest.integrity}` : ""}:${entry.integrity ?? ""}`; } /** The load-side identity check: recompute the pointer digest from the * stored `remotes` record (insertion order — JSON round-trips preserve it) * and compare. A mismatch means the file was hand-edited or written by a * different digest implementation — refuse, loud. */ function lockfilePointerDigest(lock) { const parts = Object.entries(lock.remotes).map(([remote, entry]) => entry.kind === "pluginRef" ? `${remote}:${entry.ref}` : descriptorPart(remote, entry)); return require_services_registry_digest.digestOfParts(parts); } function lockfileOf(input) { const remotes = {}; require_remotes.walkRemotes(input.config, ({ remote, entry, isPluginRef }) => { if (isPluginRef) { remotes[remote] = { kind: "pluginRef", ref: entry }; return; } if (!entry || typeof entry !== "object") return; const ref = entry; const base = typeof ref.production === "string" ? ref.production : void 0; const digest = ref.digest && typeof ref.digest.manifest === "string" && typeof ref.digest.integrity === "string" ? { manifest: ref.digest.manifest, integrity: ref.digest.integrity } : void 0; const integrity = typeof ref.integrity === "string" ? ref.integrity : void 0; remotes[remote] = { kind: "descriptor", ...base ? { base } : {}, ...digest ? { digest } : {}, ...integrity ? { integrity } : {} }; }); return CompositionLockfileSchema.parse({ schemaVersion: 2, kind: "everything-dev/composition-lockfile", runtimeRef: `bos://${input.account}/${input.gateway}`, release: input.release ?? null, remotes, ...input.sharedSingletons ? { sharedSingletons: input.sharedSingletons } : {}, ...input.contracts ? { contracts: input.contracts } : {}, pointerDigest: require_services_registry_digest.pointerDigest(input.config), resolvedAt: input.resolvedAt ?? (/* @__PURE__ */ new Date()).toISOString() }); } function serializeLockfile(lock) { return `${JSON.stringify(lock, null, 2)}\n`; } /** Writers carry the `template` annotation forward from the existing file: * `lockfileOf` never produces it, so a producer rewrite (`bos lock`, the * deploy train's persist leg) must not silently drop the sync baseline the * file already carries. A corrupt existing file degrades to a plain write. */ function carriedTemplate(lock, existing) { if (!existing) return lock; try { const parsed = parseLockfile(existing); if (!parsed.template) return lock; return { ...lock, template: parsed.template }; } catch { return lock; } } /** The lockfile's content digest — what a deployment record names when it * answers "what exact composition was live". Producers serialize `lockfileOf` * output, which never carries the `template` annotation, so the digest stays * a composition identity even when the on-disk file carries a baseline. */ function digestOfLockfile(serialized) { return (0, node_crypto.createHash)("sha256").update(serialized).digest("hex"); } function parseLockfile(bytes) { return CompositionLockfileSchema.parse(JSON.parse(bytes)); } /** The sync producer for the deploy train (its publish path is sync-fs): * atomic temp + rename, parent dirs created. Carries the `template` * annotation forward from the existing file. */ function writeLockfileSync(lock, configDir) { const target = lockfilePath(configDir); const existing = (0, node_fs.existsSync)(target) ? (0, node_fs.readFileSync)(target, "utf8") : null; (0, node_fs.mkdirSync)(node_path.default.dirname(target), { recursive: true }); const temp = `${target}.tmp`; (0, node_fs.writeFileSync)(temp, serializeLockfile(carriedTemplate(lock, existing))); (0, node_fs.renameSync)(temp, target); return target; } /** Absent or unparseable → null: the lockfile is an input, and a corrupt one * degrades the explainer's provenance rather than failing a build. */ async function readLockfileIfExists(configDir) { try { return parseLockfile(await (0, node_fs_promises.readFile)(lockfilePath(configDir), "utf8")); } catch { return null; } } /** The producer seam both writers share (`bos lock`, and the deploy train's * post-publish leg): freeze the resolved composition, atomically, with the * `--check` refusal the CI gate exits on. */ async function writeLockfile(lock, configDir, opts) { const target = lockfilePath(configDir); const existing = await (0, node_fs_promises.readFile)(target, "utf8").catch(() => null); const serialized = serializeLockfile(carriedTemplate(lock, existing)); if (opts?.check) { if (existing === null) return { status: "drift", path: target, pointerDigest: lock.pointerDigest, drift: ["bos.lock.json is missing — run `bos lock` and commit the result"] }; if (existing !== serialized) return { status: "drift", path: target, pointerDigest: lock.pointerDigest, drift: ["bos.lock.json differs from the resolved composition — run `bos lock` and commit the result"] }; return { status: "unchanged", path: target, pointerDigest: lock.pointerDigest, drift: [] }; } await (0, node_fs_promises.mkdir)(node_path.default.dirname(target), { recursive: true }); const temp = `${target}.tmp`; await (0, node_fs_promises.writeFile)(temp, serialized); await (0, node_fs_promises.rename)(temp, target); return { status: existing === serialized ? "unchanged" : "written", path: target, pointerDigest: lock.pointerDigest, drift: [] }; } //#endregion exports.CompositionLockfileSchema = CompositionLockfileSchema; exports.LOCKFILE_NAME = LOCKFILE_NAME; exports.LockfileRemoteSchema = LockfileRemoteSchema; exports.digestOfLockfile = digestOfLockfile; exports.lockfileOf = lockfileOf; exports.lockfilePath = lockfilePath; exports.lockfilePointerDigest = lockfilePointerDigest; exports.parseLockfile = parseLockfile; exports.readLockfileIfExists = readLockfileIfExists; exports.serializeLockfile = serializeLockfile; exports.writeLockfile = writeLockfile; exports.writeLockfileSync = writeLockfileSync; //# sourceMappingURL=schema.cjs.map