everything-dev
Version:
A consolidated product package for building Module Federation apps with oRPC APIs.
129 lines (127 loc) • 6.83 kB
JavaScript
const require_load = require("../resolution/load.cjs");
const require_bos_ref = require("../../utils/bos-ref.cjs");
const require_init_source = require("../resolution/init-source.cjs");
const require_resolution = require("../registry/resolution.cjs");
//#region src/services/scaffold/upgrade-source.ts
const neverCached = { state: "miss" };
function unresolvableError(outcome) {
const chain = outcome.context.chain.join(" → ");
if (outcome.reason === "unreachable") return /* @__PURE__ */ new Error(`The registry is unreachable — cannot choose a release to stamp (${outcome.detail ?? "read failed"})${chain ? ` — chain: ${chain}` : ""}. Retry when the registry is reachable.`);
if (outcome.reason === "integrityFailure") return /* @__PURE__ */ new Error(`Refusing to choose a release: ${outcome.detail ?? "integrity check failed"}${chain ? ` — chain: ${chain}` : ""}`);
if (outcome.reason === "absent" && outcome.context.tagServes === "thin") return /* @__PURE__ */ new Error(`The manifest tag names a release whose version record is not published (${outcome.detail ?? "record absent"})${chain ? ` — chain: ${chain}` : ""} — the release deploy is incomplete or still running; retry once the release is fully published`);
return /* @__PURE__ */ new Error(`No platform release manifest resolves in the extends chain (${chain || "empty"}) — publish the platform manifest (bos publish on the parent) or run \`bos platform manifest --check\` to diagnose` + (outcome.context.deadEnd ? `; the chain terminates at ${outcome.context.deadEnd.ref}${outcome.context.deadEnd.lastWrite ? ` (last write ${outcome.context.deadEnd.lastWrite})` : " which publishes no manifest legs"}` : ""));
}
/**
* The upgrade-of-record's release resolution: `Latest` over the extends
* chain, read fresh — the offline cache may never choose the release this
* upgrade stamps (a stale stand-in would pin children to bytes nobody
* serves). The chosen release's immutable version record is verified before
* returning, so the train fails loud BEFORE any mutation when a release
* deployed with a missing manifest leg (the #568 class) instead of stranding
* the tree mid-upgrade. Version records are immutable, so one verification
* covers the whole run.
*/
async function resolveLatestReleaseForUpgrade(input) {
const seams = {
...input.options?.seams ?? {},
cacheRead: () => neverCached
};
const common = {
localConfig: input.localConfig,
...input.localRepository !== void 0 ? { localRepository: input.localRepository } : {},
configDir: input.configDir,
seams,
...input.options?.fetchParent ? { fetchParent: input.options.fetchParent } : {}
};
const latest = await require_resolution.resolveRelease({
...common,
query: { kind: "Latest" }
});
if (latest.state === "Unresolved") throw unresolvableError(latest);
const release = latest.manifest.frameworkVersion;
const record = await require_resolution.resolveRelease({
...common,
query: {
kind: "Pinned",
release
}
});
if (record.state === "Unresolved") {
const chain = record.context.chain.join(" → ");
if (record.reason === "unreachable") throw new Error(`The registry is unreachable — cannot verify release ${release}'s version record (${record.detail ?? "read failed"})${chain ? ` — chain: ${chain}` : ""}. Retry when the registry is reachable.`);
throw new Error(`Release ${release}'s version record is not published in the extends chain${chain ? ` (${chain})` : ""} — the release deploy is incomplete or still running; retry once the release is fully published`);
}
return {
manifest: latest.manifest,
source: latest.source,
fromCache: false,
provenance: latest.provenance
};
}
async function readExtendedRootSource(projectDir, options = {}) {
const localConfig = await require_load.readAuthoredConfigInput(projectDir);
if (!localConfig) throw new Error("No authored config found in current directory");
if (!require_bos_ref.getExtendsRef(localConfig)?.startsWith("bos://")) throw new Error("No bos:// extends field found in the authored config — upgrade moves children that extend a deployed runtime");
const walk = await require_resolution.walkExtendsChain({
localConfig,
localRepository: typeof localConfig.repository === "string" ? localConfig.repository : void 0,
...options.fetchParent ? { fetchParent: options.fetchParent } : {}
});
const extendsChain = walk.chain.map((node) => `bos://${node.account}/${node.gateway}`);
if (walk.circularAt) throw new Error(`Circular extends detected while resolving upgrade source: ${walk.circularAt}`);
const platform = await resolveLatestReleaseForUpgrade({
localConfig,
localRepository: typeof localConfig.repository === "string" ? localConfig.repository : void 0,
configDir: projectDir,
options
});
return {
catalog: platform.manifest.catalog,
repository: walk.repository,
extendsChain,
platform,
...typeof localConfig.release === "string" ? { authoredRelease: localConfig.release } : {}
};
}
async function loadParentPluginOptions(projectDir) {
const localConfig = await require_load.readAuthoredConfigInput(projectDir);
if (!localConfig) return null;
const extendsRef = require_bos_ref.getExtendsRef(localConfig);
if (!extendsRef?.startsWith("bos://")) return null;
const parsed = require_bos_ref.parseBosRef(extendsRef);
if (!parsed) return null;
let parentConfig;
try {
parentConfig = await require_init_source.fetchParentConfig(parsed.account, parsed.gateway);
} catch {
return null;
}
const parentPlugins = parentConfig.plugins && typeof parentConfig.plugins === "object" ? parentConfig.plugins : {};
const localPlugins = localConfig.plugins && typeof localConfig.plugins === "object" ? localConfig.plugins : {};
return {
localConfig,
parentPlugins,
newPluginKeys: Object.keys(parentPlugins).filter((key) => !(key in localPlugins))
};
}
function extractSemver(value) {
if (!value) return null;
return value.match(/\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?/)?.[0] ?? null;
}
function buildChangelogUrl(oldVersion, newVersion, repository) {
const fromVersion = extractSemver(oldVersion);
const toVersion = extractSemver(newVersion);
if (!fromVersion || !toVersion || fromVersion === toVersion) return void 0;
const repoUrl = repository;
if (!repoUrl) return void 0;
const githubMatch = repoUrl.match(/^https?:\/\/github\.com\/([^/]+)\/([^/]+?)(?:\.git)?$/);
if (!githubMatch) return void 0;
const [, owner, repo] = githubMatch;
return `https://github.com/${owner}/${repo}/compare/v${fromVersion}...v${toVersion}`;
}
//#endregion
exports.buildChangelogUrl = buildChangelogUrl;
exports.loadParentPluginOptions = loadParentPluginOptions;
exports.readExtendedRootSource = readExtendedRootSource;
exports.resolveLatestReleaseForUpgrade = resolveLatestReleaseForUpgrade;
//# sourceMappingURL=upgrade-source.cjs.map