UNPKG

everything-dev

Version:

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

120 lines (118 loc) • 3.13 kB
const require_manifest_verify = require('./manifest-verify.cjs'); //#region src/services/deploy/rollback.ts function collectPinnedSlots(config) { const slots = []; const app = config.app; const plugins = config.plugins; const push = (slot, ref) => { if (ref && typeof ref === "object") slots.push({ slot, ref }); }; if (app) { for (const key of [ "host", "ui", "api", "auth" ]) push(`app.${key}`, app[key]); const auth = app.auth; if (auth?.ui) push("app.auth.ui", auth.ui); } if (plugins) for (const [key, plugin] of Object.entries(plugins)) { if (typeof plugin !== "object" || plugin === null) continue; push(key, plugin); const ui = plugin.ui; if (ui) push(`${key}.ui`, ui); } return slots; } function pinOf(ref) { const manifest = typeof ref.pin?.manifest === "string" ? ref.pin.manifest : void 0; const integrity = typeof ref.pin?.integrity === "string" ? ref.pin.integrity : void 0; return manifest && integrity ? { manifest, integrity } : null; } /** Every pinned slot with both a production base and a verifiable pin. */ function collectSlotPinRefs(config) { return collectPinnedSlots(config).map(({ slot, ref }) => { const pin = pinOf(ref); const base = typeof ref.production === "string" ? ref.production : void 0; return pin && base ? { slot, base, pin } : null; }).filter((entry) => entry !== null); } async function verifySlot(slot, ref, fetchImpl) { const base = typeof ref.production === "string" ? ref.production : void 0; const pin = pinOf(ref); if (!pin) return { slot, ok: true }; const { manifest, integrity } = pin; if (!base) return { slot, manifest, ok: false, reason: "no production URL to fetch the manifest from" }; const verified = await require_manifest_verify.fetchVerifiedManifest({ base, pin: { manifest, integrity }, fetchImpl }); if (!verified.ok) return { slot, manifest, ok: false, reason: verified.reason }; return { slot, manifest, ok: true }; } async function verifyRollbackSnapshot(config, opts) { const fetchImpl = opts?.fetchImpl ?? fetch; const pinned = collectPinnedSlots(config).filter((entry) => pinOf(entry.ref) !== null); if (pinned.length === 0) return { verifiable: false, ok: false, slots: [] }; const slots = await Promise.all(pinned.map((entry) => verifySlot(entry.slot, entry.ref, fetchImpl))); return { verifiable: true, ok: slots.every((check) => check.ok), slots }; } function summarizeSlotPins(config) { const pinned = collectPinnedSlots(config).map((entry) => { const pin = pinOf(entry.ref); return pin ? `${entry.slot}=${pin.manifest}` : null; }).filter(Boolean); return pinned.length > 0 ? pinned.join(" ") : "no version pins (pre-pin snapshot)"; } function buildRollbackPayload(targetConfig, rolledBackFrom) { return { ...targetConfig, rolledBackFrom }; } //#endregion exports.buildRollbackPayload = buildRollbackPayload; exports.collectSlotPinRefs = collectSlotPinRefs; exports.summarizeSlotPins = summarizeSlotPins; exports.verifyRollbackSnapshot = verifyRollbackSnapshot; //# sourceMappingURL=rollback.cjs.map