import fs from "node:fs"; import path from "node:path"; // Facts about the published source snapshot, written by `archilyzer source // publish` (common/publish/source.ts) beside the tarball it describes. Since // release 12 the tarball is `git archive` of the scrubbed MIRROR's main, so // `commit` is a mirror id — the /source/ page names the private main it // reflects. export type Snapshot = { generatedAt: string; commit: string; subject: string; bytes: number; sha256: string; }; // The tarball's public path. Stable by design: a dated filename would invalidate // every link that has ever been shared the moment a new snapshot ships. The date // lives in the sidecar instead. export const SNAPSHOT_HREF = "/downloads/archilyzer-source.tar.gz"; // Read the sidecar, or null when this build has no snapshot. // // The null case is not hypothetical: the tarball and its sidecar are gitignored // build artefacts, so a fresh unpack of the source has neither until // `archilyzer source publish` runs (`archilyzer build homepage` runs it). The download page MUST render no link at all in // that case — a link to a file that isn't there is worse than an explanation of // why there isn't one. export function loadSnapshot(): Snapshot | null { try { const file = path.join( /* turbopackIgnore: true */ process.cwd(), "public", "downloads", "snapshot.json", ); const parsed = JSON.parse(fs.readFileSync(/* turbopackIgnore: true */ file, "utf8")) as Snapshot; if (!parsed || typeof parsed.bytes !== "number" || !parsed.sha256) { return null; } // Believe the sidecar only if the file it describes is actually present — // they are written together but deployed as separate assets. const tarball = path.join( /* turbopackIgnore: true */ process.cwd(), "public", "downloads", path.basename(SNAPSHOT_HREF), ); if (!fs.statSync(/* turbopackIgnore: true */ tarball).isFile()) return null; return parsed; } catch { return null; } } export function formatBytes(bytes: number): string { const mb = bytes / (1024 * 1024); if (mb >= 1) return `${mb.toFixed(2)} MB`; return `${Math.round(bytes / 1024)} KB`; }