{"version":3,"file":"verify.mjs","names":[],"sources":["../../../src/services/deploy/verify.ts"],"sourcesContent":["import { createHash } from \"node:crypto\";\nimport { WorkspaceVersionManifestSchema } from \"every-plugin/version-manifest\";\nimport type { BosConfigInput } from \"../../types\";\nimport { ManifestTagSchema } from \"../platform/manifest-schema\";\nimport { parseRuntimeRef } from \"../registry/keys\";\nimport { readManifestTag } from \"../registry/pointer\";\nimport { computeManifestSri, manifestDocumentUrl } from \"./manifest-verify\";\nimport { collectRemoteDescriptors, type RemoteDescriptor } from \"./rollback\";\n\n/**\n * The pin→bytes proof (#826 drift alarm, the 061 `bos verify` core): for\n * every pinned remote, SRI-verify the pinned version manifest document, then\n * hash every file the manifest's `files` map names against the bytes the CDN\n * serves — the manual curl+sha384 sweep, automated. A published pin that no\n * longer resolves byte-for-byte is the mixed-build rot that killed pinned\n * children; this names the file instead of a webpack TypeError.\n */\n\nexport interface VerifyFileMismatch {\n  file: string;\n  expected: string;\n  actual?: string;\n  error?: string;\n}\n\nexport interface VerifyRemoteResult {\n  remote: string;\n  base: string;\n  manifest: string;\n  manifestOk: boolean;\n  manifestReason?: string;\n  /** the immutable version directory the pin resolves into (absent on legacy pins) */\n  dir?: string;\n  legacy: boolean;\n  filesChecked: number;\n  mismatches: VerifyFileMismatch[];\n}\n\nexport interface VerifyReport {\n  remotes: VerifyRemoteResult[];\n}\n\nconst sha384Of = (bytes: Uint8Array): string =>\n  `sha384-${createHash(\"sha384\").update(bytes).digest(\"base64\")}`;\n\nconst fileBaseOf = (base: string, dir: string | undefined): string =>\n  dir ? `${base.replace(/\\/$/, \"\")}/${dir}` : base.replace(/\\/$/, \"\");\n\nconst VERIFY_CONCURRENCY = 8;\n\n/** Verify one pinned remote: manifest document SRI, then every file it names. */\nasync function verifyRemotePin(\n  descriptor: RemoteDescriptor,\n  fetchImpl: typeof fetch,\n): Promise<VerifyRemoteResult> {\n  const manifestUrl = manifestDocumentUrl(descriptor.base, descriptor.digest.manifest);\n  const result: VerifyRemoteResult = {\n    remote: descriptor.remote,\n    base: descriptor.base,\n    manifest: descriptor.digest.manifest,\n    manifestOk: false,\n    legacy: true,\n    filesChecked: 0,\n    mismatches: [],\n  };\n\n  let text: string;\n  try {\n    const response = await fetchImpl(manifestUrl);\n    if (!response.ok) {\n      result.manifestReason = `manifest GET ${response.status}`;\n      return result;\n    }\n    text = await response.text();\n  } catch (error) {\n    result.manifestReason = `manifest fetch failed: ${error instanceof Error ? error.message : String(error)}`;\n    return result;\n  }\n\n  const integrity = computeManifestSri(text);\n  if (integrity !== descriptor.digest.integrity) {\n    result.manifestReason = `manifest SRI mismatch — pin ${descriptor.digest.integrity}, served ${integrity}`;\n    return result;\n  }\n\n  let manifest: ReturnType<typeof WorkspaceVersionManifestSchema.parse>;\n  try {\n    manifest = WorkspaceVersionManifestSchema.parse(JSON.parse(text));\n  } catch (error) {\n    result.manifestReason = `manifest schema: ${error instanceof Error ? error.message : String(error)}`;\n    return result;\n  }\n\n  result.manifestOk = true;\n  if (manifest.dir) {\n    result.dir = manifest.dir;\n    result.legacy = false;\n  }\n\n  const fileBase = fileBaseOf(descriptor.base, manifest.dir);\n  const entries = Object.entries(manifest.files ?? {});\n  result.filesChecked = entries.length;\n\n  const check = async ([path, expected]: [string, string]): Promise<VerifyFileMismatch | null> => {\n    try {\n      const response = await fetchImpl(`${fileBase}/${path}`);\n      if (!response.ok) {\n        return { file: path, expected, error: `GET ${response.status}` };\n      }\n      const actual = sha384Of(new Uint8Array(await response.arrayBuffer()));\n      return actual === expected ? null : { file: path, expected, actual };\n    } catch (error) {\n      return {\n        file: path,\n        expected,\n        error: error instanceof Error ? error.message : String(error),\n      };\n    }\n  };\n\n  const mismatches: VerifyFileMismatch[] = [];\n  let cursor = 0;\n  const workers = Array.from({ length: Math.min(VERIFY_CONCURRENCY, entries.length) }, async () => {\n    while (cursor < entries.length) {\n      const entry = entries[cursor++];\n      if (!entry) continue;\n      const mismatch = await check(entry);\n      if (mismatch) mismatches.push(mismatch);\n    }\n  });\n  await Promise.all(workers);\n  result.mismatches = mismatches.sort((a, b) => a.file.localeCompare(b.file));\n  return result;\n}\n\nexport async function verifyPublishedPins(input: {\n  config: BosConfigInput;\n  fetchImpl?: typeof fetch;\n}): Promise<VerifyReport> {\n  const fetchImpl = input.fetchImpl ?? fetch;\n  const descriptors = collectRemoteDescriptors(input.config);\n  const remotes: VerifyRemoteResult[] = [];\n  for (const descriptor of descriptors) {\n    remotes.push(await verifyRemotePin(descriptor, fetchImpl));\n  }\n  return { remotes };\n}\n\nexport interface VerifyFreshness {\n  behind: boolean;\n  pinned?: string;\n  published?: string;\n  message: string;\n}\n\n/**\n * The freshness half of the drift alarm: the authored `release` vs the\n * parent's live thin-tag release. Behind is advisory only — drift never\n * blocks a deploy, `bos upgrade && bos deploy` realigns.\n */\nexport async function verifyReleaseFreshness(input: {\n  config: BosConfigInput;\n  registry?: string;\n}): Promise<VerifyFreshness | undefined> {\n  const pinned = typeof input.config.release === \"string\" ? input.config.release : undefined;\n  const extendsRef = typeof input.config.extends === \"string\" ? input.config.extends : undefined;\n  if (!pinned || !extendsRef) return undefined;\n  try {\n    const ref = parseRuntimeRef(extendsRef);\n    const tagBytes = await readManifestTag(ref.accountId, ref.gatewayId, input.registry);\n    if (!tagBytes) {\n      return { behind: false, pinned, message: \"the parent's release pointer is absent\" };\n    }\n    const published = ManifestTagSchema.parse(tagBytes).version;\n    return published === pinned\n      ? { behind: false, pinned, published, message: `release ${pinned} is current` }\n      : {\n          behind: true,\n          pinned,\n          published,\n          message: `parent release is ${published}, you target ${pinned} — run \\`bos upgrade && bos deploy\\` to realign`,\n        };\n  } catch (error) {\n    return {\n      behind: false,\n      pinned,\n      message: `freshness check failed (non-blocking): ${error instanceof Error ? error.message : String(error)}`,\n    };\n  }\n}\n"],"mappings":";;;;;;;;;AA0CA,MAAM,YAAY,UAChB,UAAU,WAAW,QAAQ,CAAC,CAAC,OAAO,KAAK,CAAC,CAAC,OAAO,QAAQ;AAE9D,MAAM,cAAc,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