everything-dev
Version:
A consolidated product package for building Module Federation apps with oRPC APIs.
208 lines (206 loc) • 9.16 kB
JavaScript
import { isPlainObject } from "./merge.mjs";
import { resolveRemoteVersion } from "../deploy/version-manifest.mjs";
import { ensureObjectContainers, walkRemotes } from "../remotes.mjs";
import { SHARED_DEP_SPECS } from "every-plugin/shared-deps-spec";
//#region src/services/resolution/release-composition.ts
/**
* The release plane (ADR 0034 amendment — release-scoped extends
* inheritance): a platform-pinned child composes the remotes the pinned
* release recorded instead of drifting with the parent's live config
* pointer. One implementation, two adapters: the deploy train stamps the
* child's publish payload before it publishes, and every consumer boot
* stamps the extends-merged shape before composition — so production, local
* dev, and type generation all resolve the same release bytes.
*
* Without the plane, a root publish (rc.N+1) moves the inherited auth pin
* while a platform-pinned child's own ui stays built against rc.N — the
* host's shared-singleton guard (#546) refuses the composition at boot.
*/
/** Keys that name where a remote's code comes from — owning the remote's bytes. */
const REMOTE_SOURCE_KEYS = [
"production",
"development",
"digest",
"extends",
"proxy"
];
/**
* A remote is authored when its entry names a code source (or opts out with
* `ui: false` / owns it via a bare plugin ref). An entry that only configures
* an inherited remote — `variables`, `secrets`, `routes`, … — does not own its
* bytes, so the release still pins them.
*/
function declaresRemoteSource(entry) {
if (entry === false || typeof entry === "string") return true;
return REMOTE_SOURCE_KEYS.some((key) => entry[key] !== void 0);
}
/** Authored remote paths: every remote whose code source the leaf itself declares. */
function authoredRemoteKeys(config) {
const paths = /* @__PURE__ */ new Set();
walkRemotes(config, ({ remote, entry, isPluginRef }) => {
if (declaresRemoteSource(entry)) paths.add(remote);
if (isPluginRef) paths.add(`${remote}.ui`);
});
return paths;
}
function stampInheritedRemotes(input) {
const remotes = input.remotes ?? {};
const authored = authoredRemoteKeys(input.authoredConfig);
const stamped = [];
const omitted = [];
for (const [remotePath, record] of Object.entries(remotes)) {
const isAppRemote = /^app\.[^.]+(\.ui)?$/.test(remotePath);
if (input.mode === "payload" && !isAppRemote) continue;
if (authored.has(remotePath)) continue;
if (input.mode === "consumer" && !remoteEntryAt(input.config, remotePath)) {
omitted.push(remotePath);
continue;
}
stampRemoteRecord(input.config, remotePath, record);
stamped.push(remotePath);
}
return {
stamped,
omitted
};
}
/** The entry object at a dotted remote path (2–3 segments), if it exists. */
function remoteEntryAt(config, remotePath) {
const segments = remotePath.split(".");
if (segments.length !== 2 && segments.length !== 3 || !segments[0] || !segments[1]) return;
let node = config;
for (const segment of segments.slice(0, -1)) {
if (!isPlainObject(node)) return void 0;
node = node[segment];
}
if (!isPlainObject(node)) return void 0;
const leaf = segments.at(-1);
if (leaf === void 0) return void 0;
const entry = node[leaf];
return isPlainObject(entry) ? entry : void 0;
}
/** Write `{ production, digest }` for one dotted remote path (2–3 segments),
* merging into an existing entry rather than replacing it. */
function stampRemoteRecord(config, remotePath, record) {
const segments = remotePath.split(".");
if (segments.length !== 2 && segments.length !== 3 || !segments[0] || !segments[1]) return;
const leaf = segments.at(-1);
const container = ensureObjectContainers(config, segments.slice(0, -1));
if (!leaf) return;
const existing = container[leaf];
const entry = isPlainObject(existing) ? { ...existing } : {};
entry.production = record.base;
entry.digest = {
manifest: record.digest.manifest,
integrity: record.digest.integrity
};
container[leaf] = entry;
}
/** The package name a subpath singleton resolves through: `everything-dev/ui/auth`
* → `everything-dev` (scoped: `@scope/pkg/sub` → `@scope/pkg`; a bare package
* name — including `@scope/pkg` — has no subpath and yields undefined). */
function subpathBaseName(name) {
if (!name.includes("/")) return void 0;
if (name.startsWith("@")) {
const segments = name.split("/");
return segments.length >= 3 ? `${segments[0]}/${segments[1]}` : void 0;
}
const [pkg] = name.split("/");
return pkg;
}
/**
* The publish key an owned remote realigns through: the workspace key the
* scoped deploy train and `bos plugin publish` understand — `app.<key>` and
* `app.<key>.ui` publish as the app key (`ui`, `api`, …), `plugins.<id>` and
* `plugins.<id>.ui` as the plugin id. Remote paths themselves are not keys.
*/
function workspaceKeyOfRemote(remote) {
const [domain, key] = remote.split(".");
return domain === "app" || domain === "plugins" ? key ?? remote : remote;
}
/** The repair instruction for an owned remote diverging on shared singletons:
* app remotes republish through the scoped deploy train (they are not plugins);
* plugin remotes through the single-plugin publish. */
function ownedRemoteRepair(remote) {
const key = workspaceKeyOfRemote(remote);
return `run ${remote.startsWith("app.") ? `bos deploy --packages ${key}` : `bos plugin publish ${key}`} (built against an older chain)`;
}
/**
* The dev-boot chain audit (ADR 0034 + #546's shared-singleton trust):
* every pinned remote's embedded singleton versions are compared
* against the local workspace catalog — the same `satisfiesConstraint`
* family the deploy-train compat-window gate speaks, so one mismatch
* reads the same way in the train and on screen. One line per divergent
* remote, with the repair named for its ownership: inherited →
* `bos upgrade` (the pin trails the chain serving the remote); owned →
* `bos plugin publish <key>` (built against an older chain). Empty out:
* the whole composition composes coherently.
*
* Two composition-shape signals ride beside the version check:
* - a config remote the pinned manifest does not record is genuinely
* provisioned upstream after the pin — it composes live, unpinned bytes,
* and `bos upgrade` claims it;
* - a manifest remote the merged config does not compose is NOT an upgrade
* candidate — a child declaring its own `plugins` domain wholesale-
* replaces the inherited one by design, so that is an uncomposed remote
* note, never a "run bos upgrade" instruction (an upgrade cannot change
* the merged shape).
*/
async function auditReleaseComposition(input) {
const lines = [];
const strictness = new Map(SHARED_DEP_SPECS.map((spec) => [spec.name, spec.critical]));
const manifestRemotePaths = new Set(input.manifestRemotePaths ?? []);
for (const { remote, base, digest } of input.remotes) {
const kind = input.authoredPaths.has(remote) ? "owned" : "inherited";
const repair = kind === "owned" ? ownedRemoteRepair(remote) : "run bos upgrade (the pin trails the chain serving this remote)";
const localOf = (sharedName) => {
const local = input.localVersionOf(sharedName);
if (local !== void 0) return local;
const base = subpathBaseName(sharedName);
return base ? input.localVersionOf(base) : void 0;
};
try {
const resolved = await resolveRemoteVersion({
base,
digest,
...input.fetchImpl ? { fetchImpl: input.fetchImpl } : {}
});
for (const [name, version] of Object.entries(resolved.sharedVersions ?? {})) {
if (!strictness.get(name)) continue;
const local = localOf(name);
if (local !== void 0 && local === version) continue;
lines.push({
remote,
kind,
message: `${remote} was built against ${name}@${version}, this runtime composes ${name}@${local ?? "unknown"} — ${repair}`
});
break;
}
} catch (error) {
const reason = error instanceof Error ? error.message : String(error);
lines.push({
remote,
kind,
message: `${remote}'s version manifest does not serve at its production URL (${reason}) — ${repair}`
});
}
}
const pinnedRemotes = new Set(input.remotes.map((ref) => ref.remote));
for (const remote of input.configRemotePaths ?? []) if (!manifestRemotePaths.has(remote) && !pinnedRemotes.has(remote) && !input.authoredPaths.has(remote)) {
if (remote === "plugins.auth" && manifestRemotePaths.has("app.auth")) continue;
lines.push({
remote,
kind: "provisioned",
message: `remote "${remote}" is served by the extends chain but missing from the ${input.pinnedReleaseVersion ?? "pinned"} release composition — it composes live, unpinned bytes; run bos upgrade to re-pin and claim it`
});
}
for (const remote of input.provisionedRemotes ?? []) lines.push({
remote,
kind: "uncomposed",
message: `remote "${remote}" is recorded in the ${input.pinnedReleaseVersion ?? "pinned"} release but not composed by this config — declare it in your config's app/plugins domain to compose it`
});
return lines;
}
//#endregion
export { auditReleaseComposition, authoredRemoteKeys, stampInheritedRemotes, workspaceKeyOfRemote };
//# sourceMappingURL=release-composition.mjs.map