UNPKG

everything-dev

Version:

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

190 lines (188 loc) • 8.14 kB
const require_runtime = require("../../_virtual/_rolldown/runtime.cjs"); const require_remotes = require("../remotes.cjs"); const require_services_registry_fingerprint = require("../registry/fingerprint.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 fingerprint recomputes exactly * (`lockfileFingerprint` ≡ `pointerFingerprint` 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(1), 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 fingerprint 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 composition identity — equal to `pointerFingerprint(config)` by * construction (`lockfileOf`), recomputable from `remotes` alone * (`lockfileFingerprint`). */ fingerprint: 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 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: 1, 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 } : {}, fingerprint: require_services_registry_fingerprint.pointerFingerprint(input.config), resolvedAt: input.resolvedAt ?? (/* @__PURE__ */ new Date()).toISOString() }); } function serializeLockfile(lock) { return `${JSON.stringify(lock, null, 2)}\n`; } /** The lockfile's content digest — what a deployment record names when it * answers "what exact composition was live". */ 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. */ function writeLockfileSync(lock, configDir) { const target = lockfilePath(configDir); (0, node_fs.mkdirSync)(node_path.default.dirname(target), { recursive: true }); const temp = `${target}.tmp`; (0, node_fs.writeFileSync)(temp, serializeLockfile(lock)); (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 serialized = serializeLockfile(lock); const existing = await (0, node_fs_promises.readFile)(target, "utf8").catch(() => null); if (opts?.check) { if (existing === null) return { status: "drift", path: target, fingerprint: lock.fingerprint, drift: ["bos.lock.json is missing — run `bos lock` and commit the result"] }; if (existing !== serialized) return { status: "drift", path: target, fingerprint: lock.fingerprint, drift: ["bos.lock.json differs from the resolved composition — run `bos lock` and commit the result"] }; return { status: "unchanged", path: target, fingerprint: lock.fingerprint, 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, fingerprint: lock.fingerprint, drift: [] }; } //#endregion exports.CompositionLockfileSchema = CompositionLockfileSchema; exports.LOCKFILE_NAME = LOCKFILE_NAME; exports.LockfileRemoteSchema = LockfileRemoteSchema; exports.digestOfLockfile = digestOfLockfile; exports.lockfileOf = lockfileOf; exports.lockfilePath = lockfilePath; exports.parseLockfile = parseLockfile; exports.readLockfileIfExists = readLockfileIfExists; exports.serializeLockfile = serializeLockfile; exports.writeLockfile = writeLockfile; exports.writeLockfileSync = writeLockfileSync; //# sourceMappingURL=schema.cjs.map