// The disk half of lib/waybackPages/scanFile.ts: // `channels//wayback-scan.json`, read and written (atomic), and the // channel's imported-record index read through the posts store's reader. // SERVER-ONLY (node:fs). import path from "node:path"; import { readFile } from "node:fs/promises"; import { writeJsonAtomic } from "../jsonFile-server"; import { readAllPosts } from "../posts-server"; import { isSafeChannelSegment } from "../citations/moments"; import { buildImportedIndex, type ImportedIndex, type ImportedPageFacts } from "./imported"; import { isWaybackScanFile, WAYBACK_SCAN_FILENAME, type WaybackScanFile } from "./scanFile"; export function waybackScanPath(channelsDir: string, slug: string): string { if (!isSafeChannelSegment(slug)) throw new Error(`not a channel slug: ${JSON.stringify(slug)}`); return path.join(channelsDir, slug, WAYBACK_SCAN_FILENAME); } // The channel's scan, or null (none yet, unreadable, or another format). export async function readWaybackScan(channelsDir: string, slug: string): Promise { try { const v = JSON.parse(await readFile(waybackScanPath(channelsDir, slug), "utf8")) as unknown; return isWaybackScanFile(v) ? v : null; } catch { return null; } } // Compact: a 50,000-row scan is ~8 MB of JSON as it is. export async function writeWaybackScan(channelsDir: string, file: WaybackScanFile): Promise { const p = waybackScanPath(channelsDir, file.slug); await writeJsonAtomic(p, file, { indent: 0 }); return p; } // The channel's webpage records, by page (imported.ts). Every record through // the posts store's own reader; a record without `page` is not a capture. export async function readImportedIndex(channelsDir: string, slug: string): Promise { const posts = await readAllPosts(path.join(channelsDir, slug)); return buildImportedIndex( posts.map((p) => ({ id: p.id, page: (p as { page?: ImportedPageFacts }).page ?? null })), ); }