UNPKG

everything-dev

Version:

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

140 lines (138 loc) • 4.84 kB
import { parseRuntimeRef } from "../registry/keys.mjs"; import { computeManifestSri, manifestDocumentUrl } from "./manifest-verify.mjs"; import { ManifestTagSchema } from "../platform/manifest-schema.mjs"; import { readManifestTag } from "../registry/pointer.mjs"; import { collectRemoteDescriptors } from "./rollback.mjs"; import { createHash } from "node:crypto"; import { WorkspaceVersionManifestSchema } from "every-plugin/version-manifest"; //#region src/services/deploy/verify.ts const sha384Of = (bytes) => `sha384-${createHash("sha384").update(bytes).digest("base64")}`; const fileBaseOf = (base, dir) => dir ? `${base.replace(/\/$/, "")}/${dir}` : base.replace(/\/$/, ""); const VERIFY_CONCURRENCY = 8; /** Verify one pinned remote: manifest document SRI, then every file it names. */ async function verifyRemotePin(descriptor, fetchImpl) { const manifestUrl = manifestDocumentUrl(descriptor.base, descriptor.digest.manifest); const result = { remote: descriptor.remote, base: descriptor.base, manifest: descriptor.digest.manifest, manifestOk: false, legacy: true, filesChecked: 0, mismatches: [] }; let text; try { const response = await fetchImpl(manifestUrl); if (!response.ok) { result.manifestReason = `manifest GET ${response.status}`; return result; } text = await response.text(); } catch (error) { result.manifestReason = `manifest fetch failed: ${error instanceof Error ? error.message : String(error)}`; return result; } const integrity = computeManifestSri(text); if (integrity !== descriptor.digest.integrity) { result.manifestReason = `manifest SRI mismatch — pin ${descriptor.digest.integrity}, served ${integrity}`; return result; } let manifest; try { manifest = WorkspaceVersionManifestSchema.parse(JSON.parse(text)); } catch (error) { result.manifestReason = `manifest schema: ${error instanceof Error ? error.message : String(error)}`; return result; } result.manifestOk = true; if (manifest.dir) { result.dir = manifest.dir; result.legacy = false; } const fileBase = fileBaseOf(descriptor.base, manifest.dir); const entries = Object.entries(manifest.files ?? {}); result.filesChecked = entries.length; const check = async ([path, expected]) => { try { const response = await fetchImpl(`${fileBase}/${path}`); if (!response.ok) return { file: path, expected, error: `GET ${response.status}` }; const actual = sha384Of(new Uint8Array(await response.arrayBuffer())); return actual === expected ? null : { file: path, expected, actual }; } catch (error) { return { file: path, expected, error: error instanceof Error ? error.message : String(error) }; } }; const mismatches = []; let cursor = 0; const workers = Array.from({ length: Math.min(VERIFY_CONCURRENCY, entries.length) }, async () => { while (cursor < entries.length) { const entry = entries[cursor++]; if (!entry) continue; const mismatch = await check(entry); if (mismatch) mismatches.push(mismatch); } }); await Promise.all(workers); result.mismatches = mismatches.sort((a, b) => a.file.localeCompare(b.file)); return result; } async function verifyPublishedPins(input) { const fetchImpl = input.fetchImpl ?? fetch; const descriptors = collectRemoteDescriptors(input.config); const remotes = []; for (const descriptor of descriptors) remotes.push(await verifyRemotePin(descriptor, fetchImpl)); return { remotes }; } /** * The freshness half of the drift alarm: the authored `release` vs the * parent's live thin-tag release. Behind is advisory only — drift never * blocks a deploy, `bos upgrade && bos deploy` realigns. */ async function verifyReleaseFreshness(input) { const pinned = typeof input.config.release === "string" ? input.config.release : void 0; const extendsRef = typeof input.config.extends === "string" ? input.config.extends : void 0; if (!pinned || !extendsRef) return void 0; try { const ref = parseRuntimeRef(extendsRef); const tagBytes = await readManifestTag(ref.accountId, ref.gatewayId, input.registry); if (!tagBytes) return { behind: false, pinned, message: "the parent's release pointer is absent" }; const published = ManifestTagSchema.parse(tagBytes).version; return published === pinned ? { behind: false, pinned, published, message: `release ${pinned} is current` } : { behind: true, pinned, published, message: `parent release is ${published}, you target ${pinned} — run \`bos upgrade && bos deploy\` to realign` }; } catch (error) { return { behind: false, pinned, message: `freshness check failed (non-blocking): ${error instanceof Error ? error.message : String(error)}` }; } } //#endregion export { verifyPublishedPins, verifyReleaseFreshness }; //# sourceMappingURL=verify.mjs.map