UNPKG

everything-dev

Version:

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

213 lines (211 loc) • 9.1 kB
import { compareSemver, satisfiesConstraint } from "../../utils/semver.mjs"; import { PlatformReleaseManifestSchema } from "./manifest-schema.mjs"; import { resolvePlatformManifest, walkExtendsChain } from "../registry/resolution.mjs"; import { AGENT_SYNC_GLOBS, FRAMEWORK_OWNED_SYNC_FILES } from "../scaffold/ownership.mjs"; import { existsSync, readFileSync } from "node:fs"; import { join, relative } from "node:path"; import { fileURLToPath } from "node:url"; import { Context, Layer } from "effect"; import { createHash } from "node:crypto"; import { SHARED_DEP_SPECS, shareConfigFor } from "every-plugin/shared-deps-spec"; import { glob } from "glob"; //#region src/services/platform/manifest.ts /** * The platform release manifest (ADR 0034) — schema, composer, inventory, * registry key helpers, and the compat check. One composer feeds both publish * homes (FastKV pointer slot + in-image staging); the image digest is * deliberately absent — it derives from the tags at deploy preflight. The * homes' one known divergence is the compat floor: publisher-side (previous * release) in FastKV, own version — failing closed — in the image, which * cannot read the registry inside the build. * * Resolution (the machine) and the cache live in `registry/resolution.ts` and * `registry/cache.ts`; the service below re-exposes the resolver through the * machine's compatibility adapter. */ /** * The template-source file set a release inventory hashes: the ownership * inventory's static paths, the agent-sync globs, and the per-plugin template * scaffolding (`plugins/_template/*` — the sources of the per-plugin sync * patterns). Files absent from the release source are skipped, mirroring * sync's transitional tolerance. */ const TEMPLATE_SCAFFOLD_SOURCES = [ "plugins/_template/rspack.config.js", "plugins/_template/tsconfig.json", "plugins/_template/tests/types.d.ts" ]; function templateSourcePaths(sourceDir) { const paths = /* @__PURE__ */ new Set([...FRAMEWORK_OWNED_SYNC_FILES, ...TEMPLATE_SCAFFOLD_SOURCES]); for (const match of glob.sync(AGENT_SYNC_GLOBS, { cwd: sourceDir, nodir: true, dot: true, ignore: [ "**/node_modules/**", "**/.git/**", "**/dist/**", "**/.bos/**" ] })) paths.add(relative(sourceDir, join(sourceDir, match))); return [...paths].sort(); } function computeTemplateInventory(sourceDir) { const inventory = []; for (const relativeFilePath of templateSourcePaths(sourceDir)) { const fullPath = join(sourceDir, relativeFilePath); if (!existsSync(fullPath)) continue; try { const content = readFileSync(fullPath); inventory.push({ path: relativeFilePath, sha256: createHash("sha256").update(content).digest("hex") }); } catch {} } return inventory.sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0); } function composeSharedDepsFromCatalog(catalog) { const deps = {}; for (const spec of SHARED_DEP_SPECS) { const version = catalog[spec.name]; if (!version) continue; deps[spec.name] = { version, ...shareConfigFor(spec, version) }; } return deps; } function composePlatformReleaseManifest(inputs) { const semver = parseSemver(inputs.frameworkVersion.trim()); const tags = semver ? [`v${semver.major}.${semver.minor}.${semver.patch}${semver.prerelease ? `-${semver.prerelease}` : ""}`] : []; const slots = inputs.slots ? Object.fromEntries(Object.entries(inputs.slots).sort(([a], [b]) => a < b ? -1 : 1)) : void 0; return PlatformReleaseManifestSchema.parse({ schemaVersion: 1, kind: "everything-dev/platform-manifest", frameworkVersion: inputs.frameworkVersion, everyPluginVersion: inputs.everyPluginVersion, ...inputs.gitSha ? { gitSha: inputs.gitSha } : {}, image: { ref: inputs.imageRef, tags }, catalog: inputs.catalog, sharedDeps: composeSharedDepsFromCatalog(inputs.catalog), templateInventory: inputs.templateInventory, compat: { minChild: inputs.compatMinChild }, ...slots ? { slots } : {} }); } /** The one serializer for both publish homes — byte-identical by construction. */ function serializePlatformReleaseManifest(manifest) { return JSON.stringify(manifest); } /** * The in-image copy (ADR 0034 pt 1): generated inside the docker build at * `packages/everything-dev/dist/platform-manifest.json`. Resolved relative to * this module — the compiled layout is `<pkg>/dist/services/platform/manifest.mjs` * (two directory levels up = `<pkg>/dist/`), the source layout is * `<pkg>/src/services/platform/manifest.ts` (four levels up = the pnpm parent * dir, then `<pkg>/dist/`). Both candidates are tried; absent or unparsable → * null. */ function readInImagePlatformManifest() { const moduleFile = fileURLToPath(import.meta.url); const candidates = [join(moduleFile, "..", "..", "platform-manifest.json"), join(moduleFile, "..", "..", "..", "..", "dist", "platform-manifest.json")]; for (const path of candidates) try { return PlatformReleaseManifestSchema.parse(JSON.parse(readFileSync(path, "utf-8"))); } catch {} return null; } function parseSemver(version) { const match = /^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?$/.exec(version.trim()); if (!match) return null; return { major: Number(match[1]), minor: Number(match[2]), patch: Number(match[3]), ...match[4] ? { prerelease: match[4] } : {} }; } /** * The additive compat gate (ADR 0034 pt 5): the child's framework version must * sit inside the release's compat window, and strict singletons must agree * exactly with the release's pins. A sibling of the federation check — never * a relaxation of it. */ function checkPlatformCompat(manifest, subject) { const violations = []; if (subject.platformVersion) { if (compareSemver(subject.platformVersion, manifest.compat.minChild) < 0) violations.push({ kind: "minChild", message: `platform ${subject.platformVersion} is older than this release's compat floor ${manifest.compat.minChild}` }); if (compareSemver(subject.platformVersion, manifest.frameworkVersion) > 0) violations.push({ kind: "minChild", message: `platform ${subject.platformVersion} is newer than this release ${manifest.frameworkVersion}` }); } for (const [name, pin] of Object.entries(manifest.sharedDeps)) { const childVersion = subject.catalog[name]; if (pin.strictVersion) { if (!childVersion) { violations.push({ kind: "shared-dep", name, message: `strict singleton "${name}" is missing from the child catalog (release pins ${pin.version})` }); continue; } if (childVersion !== pin.version) violations.push({ kind: "shared-dep", name, message: `strict singleton "${name}" is ${childVersion}, the release pins ${pin.version}` }); continue; } if (typeof pin.requiredVersion === "string" && childVersion) { if (!satisfiesConstraint(childVersion, pin.requiredVersion)) violations.push({ kind: "shared-dep", name, message: `"${name}" is ${childVersion}, which does not satisfy the release range ${pin.requiredVersion}` }); } } return violations; } /** * The platform release manifest as a Context.Service: the same "module is * the implementation, the tag is the seam" shape as FastKVRegistryService — * CLI handlers `yield*` the tag; pure and standalone callers keep using the * plain functions. Tests provide `Layer.succeed` fakes instead of module * mocks. Resolution comes from `registry/resolution.ts` (the machine's * compatibility adapter). */ var PlatformManifestService = class extends Context.Service()("everything-dev/platform/manifest") { static Live = Layer.succeed(this, this.of({ resolvePlatformManifest, composePlatformReleaseManifest, checkPlatformCompat, readInImagePlatformManifest })); }; /** * The child-side upgrade-path line (`.scratch/catalog-drift/issues/05`): * when the resolved chain head is a NEWER release than this runtime's * authored pin, the runtime is servable-but-behind — the dev boot surfaces * the upgrade instead of waiting for a load-time skew to explain it. Empty * (no notice) when either side is unknown, or the pin is current-or-ahead. * Never reads npm: the FastKV pointer only advances when the parent's * release leg completed, so "pin trails pointer" already implies "the * chain is servable right now" — no transient mid-train state leaks in. */ function chainHeadPinLagNotice(headFrameworkVersion, authoredPlatformPin) { if (!headFrameworkVersion || !authoredPlatformPin) return void 0; if (compareSemver(headFrameworkVersion, authoredPlatformPin) <= 0) return void 0; return `platform upgrade for this runtime: the chain head advanced to ${headFrameworkVersion}, this runtime pins ${authoredPlatformPin} — run bos upgrade; until then the pinned release keeps resolving the older chain`; } //#endregion export { PlatformManifestService, PlatformReleaseManifestSchema, chainHeadPinLagNotice, checkPlatformCompat, composePlatformReleaseManifest, composeSharedDepsFromCatalog, computeTemplateInventory, readInImagePlatformManifest, resolvePlatformManifest, serializePlatformReleaseManifest, walkExtendsChain }; //# sourceMappingURL=manifest.mjs.map