UNPKG

everything-dev

Version:

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

1 lines • 15.9 kB
{"version":3,"file":"manifest.mjs","names":[],"sources":["../../../src/services/platform/manifest.ts"],"sourcesContent":["/**\n * The platform release manifest (ADR 0034) — schema, composer, inventory,\n * registry key helpers, and the compat check. One composer feeds both publish\n * homes (FastKV thin tag + in-image staging); the image digest is\n * deliberately absent — it derives from the tags at deploy preflight. The\n * homes' one known divergence is the compat floor: publisher-side (previous\n * release) in FastKV, own version — failing closed — in the image, which\n * cannot read the registry inside the build.\n *\n * Resolution (the machine) and the cache live in `registry/resolution.ts` and\n * `registry/cache.ts`; the service below re-exposes the resolver through the\n * machine's compatibility adapter.\n */\nimport { createHash } from \"node:crypto\";\nimport { existsSync, readFileSync } from \"node:fs\";\nimport { join, relative } from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\nimport { Context, Layer } from \"effect\";\nimport {\n SHARED_DEP_SPECS,\n type SharedDependencyConfig,\n shareConfigFor,\n} from \"every-plugin/shared-deps-spec\";\nimport { glob } from \"glob\";\nimport { compareSemver, satisfiesConstraint } from \"../../utils/semver\";\nimport { resolvePlatformManifest } from \"../registry/resolution\";\nimport { AGENT_SYNC_GLOBS, FRAMEWORK_OWNED_SYNC_FILES } from \"../scaffold/ownership\";\n\nexport type {\n PlatformManifestCache,\n ResolvedPlatformManifest,\n} from \"../registry/resolution\";\nexport {\n type ResolvePlatformManifestOptions,\n resolvePlatformManifest,\n walkExtendsChain,\n} from \"../registry/resolution\";\nexport { type Manifest, ManifestSchema } from \"./manifest-schema\";\n\nimport { type Manifest, ManifestSchema } from \"./manifest-schema\";\n\n/** One recorded release remote (see {@link ManifestSchema.remotes}). */\nexport interface RemoteRecord {\n base: string;\n digest: { manifest: string; integrity: string };\n}\n\nexport interface TemplateInventoryEntry {\n path: string;\n sha256: string;\n}\n\n// The release-pointer key derivations moved to `registry/pointer.ts` (the\n// pre-cutover wire quarantine); the post-cutover keys live in the key model\n// (`registry/keys.ts`).\n\n/**\n * The template-source file set a release inventory hashes: the ownership\n * inventory's static paths, the agent-sync globs, and the per-plugin template\n * scaffolding (`plugins/_template/*` — the sources of the per-plugin sync\n * patterns). Files absent from the release source are skipped, mirroring\n * sync's transitional tolerance.\n */\nconst TEMPLATE_SCAFFOLD_SOURCES = [\n \"plugins/_template/rspack.config.js\",\n \"plugins/_template/tsconfig.json\",\n \"plugins/_template/tests/types.d.ts\",\n];\n\nfunction templateSourcePaths(sourceDir: string): string[] {\n const paths = new Set<string>([...FRAMEWORK_OWNED_SYNC_FILES, ...TEMPLATE_SCAFFOLD_SOURCES]);\n for (const match of glob.sync(AGENT_SYNC_GLOBS, {\n cwd: sourceDir,\n nodir: true,\n dot: true,\n ignore: [\"**/node_modules/**\", \"**/.git/**\", \"**/dist/**\", \"**/.bos/**\"],\n })) {\n paths.add(relative(sourceDir, join(sourceDir, match)));\n }\n return [...paths].sort();\n}\n\nexport function computeTemplateInventory(sourceDir: string): TemplateInventoryEntry[] {\n const inventory: TemplateInventoryEntry[] = [];\n for (const relativeFilePath of templateSourcePaths(sourceDir)) {\n const fullPath = join(sourceDir, relativeFilePath);\n if (!existsSync(fullPath)) continue;\n try {\n const content = readFileSync(fullPath);\n inventory.push({\n path: relativeFilePath,\n sha256: createHash(\"sha256\").update(content).digest(\"hex\"),\n });\n } catch {\n // unreadable at compose time → not part of the release inventory\n }\n }\n return inventory.sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0));\n}\n\nexport function composeSharedDepsFromCatalog(\n catalog: Record<string, string>,\n): Record<string, SharedDependencyConfig> {\n const deps: Record<string, SharedDependencyConfig> = {};\n for (const spec of SHARED_DEP_SPECS) {\n const version = catalog[spec.name];\n if (!version) continue;\n deps[spec.name] = { version, ...shareConfigFor(spec, version) };\n }\n return deps;\n}\n\nexport interface ComposePlatformReleaseManifestInputs {\n frameworkVersion: string;\n everyPluginVersion: string;\n gitSha?: string;\n imageRef: string;\n catalog: Record<string, string>;\n templateInventory: TemplateInventoryEntry[];\n compatMinChild: string;\n /** The release's own remote-record map — sorted here so identical payloads\n * compose identical manifest bytes across machines (the burned-version\n * guard compares serialized bytes). */\n remotes?: Record<string, RemoteRecord>;\n}\n\nexport function composePlatformReleaseManifest(\n inputs: ComposePlatformReleaseManifestInputs,\n): Manifest {\n // The manifest carries only the derivable tag — the exact version tag the\n // tag scheme pushes for semver releases (ADR 0021). Sha-tags are git facts\n // an image build cannot see; derivation (`ref@v<version>` → digest) keys off\n // the version tag so both publish homes stay byte-identical without any\n // build-context plumbing.\n const semver = parseSemver(inputs.frameworkVersion.trim());\n const tags = semver\n ? [\n `v${semver.major}.${semver.minor}.${semver.patch}${semver.prerelease ? `-${semver.prerelease}` : \"\"}`,\n ]\n : [];\n const remotes = inputs.remotes\n ? Object.fromEntries(Object.entries(inputs.remotes).sort(([a], [b]) => (a < b ? -1 : 1)))\n : undefined;\n return ManifestSchema.parse({\n schemaVersion: 1,\n kind: \"everything-dev/platform-manifest\",\n frameworkVersion: inputs.frameworkVersion,\n everyPluginVersion: inputs.everyPluginVersion,\n ...(inputs.gitSha ? { gitSha: inputs.gitSha } : {}),\n image: { ref: inputs.imageRef, tags },\n catalog: inputs.catalog,\n sharedDeps: composeSharedDepsFromCatalog(inputs.catalog),\n templateInventory: inputs.templateInventory,\n compat: { minChild: inputs.compatMinChild },\n ...(remotes ? { remotes } : {}),\n });\n}\n\n/** The one serializer for both publish homes — byte-identical by construction. */\nexport function serializePlatformReleaseManifest(manifest: Manifest): string {\n return JSON.stringify(manifest);\n}\n\n/**\n * The in-image copy (ADR 0034 pt 1): generated inside the docker build at\n * `packages/everything-dev/dist/platform-manifest.json`. Resolved relative to\n * this module — the compiled layout is `<pkg>/dist/services/platform/manifest.mjs`\n * (two directory levels up = `<pkg>/dist/`), the source layout is\n * `<pkg>/src/services/platform/manifest.ts` (four levels up = the pnpm parent\n * dir, then `<pkg>/dist/`). Both candidates are tried; absent or unparsable →\n * null.\n */\nexport function readInImagePlatformManifest(): Manifest | null {\n const moduleFile = fileURLToPath(import.meta.url);\n const candidates = [\n join(moduleFile, \"..\", \"..\", \"platform-manifest.json\"),\n join(moduleFile, \"..\", \"..\", \"..\", \"..\", \"dist\", \"platform-manifest.json\"),\n ];\n for (const path of candidates) {\n try {\n return ManifestSchema.parse(JSON.parse(readFileSync(path, \"utf-8\")));\n } catch {\n // try the next layout candidate\n }\n }\n return null;\n}\n\nfunction parseSemver(\n version: string,\n): { major: number; minor: number; patch: number; prerelease?: string } | null {\n const match = /^(\\d+)\\.(\\d+)\\.(\\d+)(?:-([0-9A-Za-z.-]+))?$/.exec(version.trim());\n if (!match) return null;\n return {\n major: Number(match[1]),\n minor: Number(match[2]),\n patch: Number(match[3]),\n ...(match[4] ? { prerelease: match[4] } : {}),\n };\n}\n\nexport interface PlatformCompatSubject {\n /** The child's recorded platform pin / installed framework version. */\n platformVersion?: string;\n catalog: Record<string, string>;\n}\n\nexport interface PlatformCompatViolation {\n kind: \"minChild\" | \"shared-dep\";\n name?: string;\n message: string;\n}\n\n/**\n * The additive compat gate (ADR 0034 pt 5): the child's framework version must\n * sit inside the release's compat window, and strict singletons must agree\n * exactly with the release's pins. A sibling of the federation check — never\n * a relaxation of it.\n */\nexport function checkPlatformCompat(\n manifest: Manifest,\n subject: PlatformCompatSubject,\n): PlatformCompatViolation[] {\n const violations: PlatformCompatViolation[] = [];\n\n if (subject.platformVersion) {\n if (compareSemver(subject.platformVersion, manifest.compat.minChild) < 0) {\n violations.push({\n kind: \"minChild\",\n message: `platform ${subject.platformVersion} is older than this release's compat floor ${manifest.compat.minChild}`,\n });\n }\n if (compareSemver(subject.platformVersion, manifest.frameworkVersion) > 0) {\n violations.push({\n kind: \"minChild\",\n message: `platform ${subject.platformVersion} is newer than this release ${manifest.frameworkVersion}`,\n });\n }\n }\n\n for (const [name, pin] of Object.entries(manifest.sharedDeps)) {\n const childVersion = subject.catalog[name];\n if (pin.strictVersion) {\n if (!childVersion) {\n violations.push({\n kind: \"shared-dep\",\n name,\n message: `strict singleton \"${name}\" is missing from the child catalog (release pins ${pin.version})`,\n });\n continue;\n }\n if (childVersion !== pin.version) {\n violations.push({\n kind: \"shared-dep\",\n name,\n message: `strict singleton \"${name}\" is ${childVersion}, the release pins ${pin.version}`,\n });\n }\n continue;\n }\n if (typeof pin.requiredVersion === \"string\" && childVersion) {\n if (!satisfiesConstraint(childVersion, pin.requiredVersion)) {\n violations.push({\n kind: \"shared-dep\",\n name,\n message: `\"${name}\" is ${childVersion}, which does not satisfy the release range ${pin.requiredVersion}`,\n });\n }\n }\n }\n\n return violations;\n}\n\n/**\n * The platform release manifest as a Context.Service: the same \"module is\n * the implementation, the tag is the seam\" shape as FastKVRegistryService —\n * CLI handlers `yield*` the tag; pure and standalone callers keep using the\n * plain functions. Tests provide `Layer.succeed` fakes instead of module\n * mocks. Resolution comes from `registry/resolution.ts` (the machine's\n * compatibility adapter).\n */\nexport class ManifestService extends Context.Service<\n ManifestService,\n {\n resolvePlatformManifest: typeof resolvePlatformManifest;\n composePlatformReleaseManifest: typeof composePlatformReleaseManifest;\n checkPlatformCompat: typeof checkPlatformCompat;\n readInImagePlatformManifest: typeof readInImagePlatformManifest;\n }\n>()(\"everything-dev/platform/manifest\") {\n static readonly Live = Layer.succeed(\n this,\n this.of({\n resolvePlatformManifest,\n composePlatformReleaseManifest,\n checkPlatformCompat,\n readInImagePlatformManifest,\n }),\n );\n}\n\n/**\n * The child-side upgrade-path line (`.scratch/catalog-drift/issues/05`):\n * when the resolved chain head is a NEWER release than this runtime's\n * authored release, the runtime is servable-but-behind — the dev boot\n * surfaces the upgrade instead of waiting for a load-time skew to explain\n * it. Empty (no notice) when either side is unknown, or the release is\n * current-or-ahead. Never reads npm: the FastKV pointer only advances when\n * the parent's release leg completed, so \"release trails pointer\" already\n * implies \"the chain is servable right now\" — no transient mid-train state\n * leaks in.\n */\nexport function chainHeadPinLagNotice(\n headFrameworkVersion: string | undefined,\n authoredRelease: string | undefined,\n): string | undefined {\n if (!headFrameworkVersion || !authoredRelease) return undefined;\n if (compareSemver(headFrameworkVersion, authoredRelease) <= 0) return undefined;\n return (\n `release upgrade available for this runtime: the chain head advanced to ${headFrameworkVersion}, ` +\n `this runtime targets ${authoredRelease} — run bos upgrade; until then the authored release ` +\n `keeps resolving the older chain`\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+DA,MAAM,4BAA4B;CAChC;CACA;CACA;AACF;AAEA,SAAS,oBAAoB,WAA6B;CACxD,MAAM,wBAAQ,IAAI,IAAY,CAAC,GAAG,4BAA4B,GAAG,yBAAyB,CAAC;CAC3F,KAAK,MAAM,SAAS,KAAK,KAAK,kBAAkB;EAC9C,KAAK;EACL,OAAO;EACP,KAAK;EACL,QAAQ;GAAC;GAAsB;GAAc;GAAc;EAAY;CACzE,CAAC,GACC,MAAM,IAAI,SAAS,WAAW,KAAK,WAAW,KAAK,CAAC,CAAC;CAEvD,OAAO,CAAC,GAAG,KAAK,CAAC,CAAC,KAAK;AACzB;AAEA,SAAgB,yBAAyB,WAA6C;CACpF,MAAM,YAAsC,CAAC;CAC7C,KAAK,MAAM,oBAAoB,oBAAoB,SAAS,GAAG;EAC7D,MAAM,WAAW,KAAK,WAAW,gBAAgB;EACjD,IAAI,CAAC,WAAW,QAAQ,GAAG;EAC3B,IAAI;GACF,MAAM,UAAU,aAAa,QAAQ;GACrC,UAAU,KAAK;IACb,MAAM;IACN,QAAQ,WAAW,QAAQ,CAAC,CAAC,OAAO,OAAO,CAAC,CAAC,OAAO,KAAK;GAC3D,CAAC;EACH,QAAQ,CAER;CACF;CACA,OAAO,UAAU,MAAM,GAAG,MAAO,EAAE,OAAO,EAAE,OAAO,KAAK,EAAE,OAAO,EAAE,OAAO,IAAI,CAAE;AAClF;AAEA,SAAgB,6BACd,SACwC;CACxC,MAAM,OAA+C,CAAC;CACtD,KAAK,MAAM,QAAQ,kBAAkB;EACnC,MAAM,UAAU,QAAQ,KAAK;EAC7B,IAAI,CAA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