UNPKG

everything-dev

Version:

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

141 lines (139 loc) • 5.04 kB
const require_keys = require("../registry/keys.cjs"); const require_manifest_verify = require("./manifest-verify.cjs"); const require_manifest_schema = require("../platform/manifest-schema.cjs"); const require_pointer = require("../registry/pointer.cjs"); const require_rollback = require("./rollback.cjs"); let node_crypto = require("node:crypto"); let every_plugin_version_manifest = require("every-plugin/version-manifest"); //#region src/services/deploy/verify.ts const sha384Of = (bytes) => `sha384-${(0, node_crypto.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 = require_manifest_verify.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 = require_manifest_verify.computeManifestSri(text); if (integrity !== descriptor.digest.integrity) { result.manifestReason = `manifest SRI mismatch — pin ${descriptor.digest.integrity}, served ${integrity}`; return result; } let manifest; try { manifest = every_plugin_version_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 = require_rollback.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 = require_keys.parseRuntimeRef(extendsRef); const tagBytes = await require_pointer.readManifestTag(ref.accountId, ref.gatewayId, input.registry); if (!tagBytes) return { behind: false, pinned, message: "the parent's release pointer is absent" }; const published = require_manifest_schema.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 exports.verifyPublishedPins = verifyPublishedPins; exports.verifyReleaseFreshness = verifyReleaseFreshness; //# sourceMappingURL=verify.cjs.map