everything-dev
Version:
A consolidated product package for building Module Federation apps with oRPC APIs.
78 lines (76 loc) • 3.19 kB
JavaScript
import { walkRemotes } from "../remotes.mjs";
import { createHash } from "node:crypto";
//#region src/services/registry/fingerprint.ts
/**
* The registry's fingerprint module — every identity hash lives here so the
* planes can't drift apart:
*
* - `pointerFingerprint` — THE composition identity: a hash over every
* remote's pin coordinates (digest manifest + integrity, or the direct
* entry integrity). It is the lockfile's fingerprint (ADR 0042), the
* watch's change-detection hash, and what deployment records name.
* - `deploymentFingerprint` — the serving-generation payload hash
* (atomic-deploys 10): entryUrl + entry integrity (+ SSR integrity) per
* remote, stamped into the client config as the soft-refresh signal and
* used by the host's adopt comparison for derived generations. Derived
* from the same remotes AFTER entry resolution, so it changes whenever
* the composition changes — plus when only resolved entry coordinates
* change. Semantic merge of the two hashes is a recorded future seam:
* both sides of every existing comparison must move together.
*/
/**
* The published pointer's version identity (atomic-deploys 08/12): a hash
* over every remote's `digest` — cheap to compute (no version-manifest
* fetch), and stable exactly when the deployed set is. Shared so the CLI and
* the host compute the same identity for the same pointer.
*/
function pointerFingerprint(config) {
const parts = [];
walkRemotes(config, ({ remote, entry, isPluginRef }) => {
if (isPluginRef) {
parts.push(`${remote}:${entry}`);
return;
}
const s = entry;
if (!s) return;
const digest = s.digest;
const digestPart = digest && typeof digest.manifest === "string" && typeof digest.integrity === "string" ? `${digest.manifest}:${digest.integrity}` : "";
const directPart = typeof s.integrity === "string" ? s.integrity : "";
parts.push(`${remote}:${digestPart}:${directPart}`);
});
return createHash("sha256").update(parts.join("|")).digest("hex").slice(0, 16);
}
/** Per-remote digest ids for display (remote → versioned manifest filename). */
function remoteDigests(config) {
const digests = {};
walkRemotes(config, ({ remote, entry, isPluginRef }) => {
if (isPluginRef) return;
const s = entry;
if (!s) return;
const manifest = s.digest?.manifest;
if (typeof manifest === "string") digests[remote] = manifest;
});
return digests;
}
/**
* The serving-generation payload hash — see the module docstring for its
* identity contract versus `pointerFingerprint`.
*/
function deploymentFingerprint(config) {
const parts = [];
const remote = (prefix, s) => {
if (!s) return;
parts.push(`${prefix}:${s.entryUrl ?? ""}:${s.integrity ?? ""}:${s.ssrIntegrity ?? ""}`);
};
remote("ui", config.ui);
remote("api", config.api);
remote("auth", config.auth);
for (const [key, plugin] of Object.entries(config.plugins ?? {})) {
remote(`plugin.${key}`, plugin);
remote(`plugin.${key}.ui`, plugin.ui);
}
return createHash("sha256").update(parts.join("|")).digest("hex").slice(0, 16);
}
//#endregion
export { deploymentFingerprint, pointerFingerprint, remoteDigests };
//# sourceMappingURL=fingerprint.mjs.map