UNPKG

everything-dev

Version:

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

178 lines (176 loc) • 7.38 kB
import { walkRemotes } from "../remotes.mjs"; import { pointerFingerprint } from "../registry/fingerprint.mjs"; import { mkdirSync, renameSync, writeFileSync } from "node:fs"; import path from "node:path"; import { z } from "zod"; import { createHash } from "node:crypto"; import { mkdir, readFile, rename, writeFile } from "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 = z.discriminatedUnion("kind", [z.object({ kind: z.literal("descriptor"), base: z.string().min(1).optional(), digest: z.object({ manifest: z.string().min(1), integrity: z.string().min(1) }).optional(), integrity: z.string().min(1).optional() }).strict(), z.object({ kind: z.literal("pluginRef"), ref: z.string().min(1) }).strict()]); const CompositionLockfileSchema = z.object({ schemaVersion: z.literal(1), kind: z.literal("everything-dev/composition-lockfile"), runtimeRef: 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: 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: z.record(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: z.record(z.string(), z.string()).optional(), /** Per-remote contract hash (`plugin.manifest.json`'s * `contract.types.sha256`). */ contracts: z.record(z.string(), z.string()).optional(), /** THE composition identity — equal to `pointerFingerprint(config)` by * construction (`lockfileOf`), recomputable from `remotes` alone * (`lockfileFingerprint`). */ fingerprint: z.string().regex(/^[0-9a-f]{16}$/), resolvedAt: z.string().min(1) }).strict(); function lockfilePath(configDir = process.cwd()) { return path.join(configDir, LOCKFILE_NAME); } function lockfileOf(input) { const 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: 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 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); mkdirSync(path.dirname(target), { recursive: true }); const temp = `${target}.tmp`; writeFileSync(temp, serializeLockfile(lock)); 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 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 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 mkdir(path.dirname(target), { recursive: true }); const temp = `${target}.tmp`; await writeFile(temp, serialized); await rename(temp, target); return { status: existing === serialized ? "unchanged" : "written", path: target, fingerprint: lock.fingerprint, drift: [] }; } //#endregion export { CompositionLockfileSchema, LOCKFILE_NAME, LockfileRemoteSchema, digestOfLockfile, lockfileOf, lockfilePath, parseLockfile, readLockfileIfExists, serializeLockfile, writeLockfile, writeLockfileSync }; //# sourceMappingURL=schema.mjs.map