// Runtime validation for `Post.page` (a page record's capture facts) read back // off disk or off the wire. The parsePost stance: unknown keys dropped, and a // `page` missing its provenance or its extractor is dropped WHOLE — never the // post, never repaired. Client-safe: types only from ./types (whose urlHash is // node:crypto), so this file pulls nothing node into a browser bundle. import type { PageKind, PagePostInfo } from "./types"; const TS_RE = /^\d{4,14}$/; // A kind is a short lowercase word. Not checked against PAGE_KINDS on // purpose: a record made by a newer extractor stays readable by an older // reader (it shows as a page, its kind as written). const KIND_RE = /^[a-z][a-z0-9-]{0,39}$/; const str = (v: unknown): string | undefined => (typeof v === "string" && v ? v : undefined); export function parsePagePostInfo(raw: unknown): PagePostInfo | null { if (!raw || typeof raw !== "object" || Array.isArray(raw)) return null; const r = raw as Record; const originalUrl = str(r.originalUrl); const captureTs = str(r.captureTs); const waybackUrl = str(r.waybackUrl); const rawUrl = str(r.rawUrl); const kind = str(r.kind); const extractor = str(r.extractor); const extractorVersion = r.extractorVersion; if (!originalUrl || !captureTs || !TS_RE.test(captureTs) || !waybackUrl || !rawUrl) return null; if (!kind || !KIND_RE.test(kind) || !extractor) return null; if (typeof extractorVersion !== "number" || !Number.isInteger(extractorVersion) || extractorVersion < 0) return null; const out: PagePostInfo = { originalUrl, captureTs, waybackUrl, rawUrl, kind: kind as PageKind, extractor, extractorVersion, }; const title = str(r.title); if (title) out.title = title; const mimetype = str(r.mimetype); if (mimetype) out.mimetype = mimetype; const digest = str(r.digest); if (digest) out.digest = digest; if (typeof r.status === "number" && Number.isInteger(r.status) && r.status >= 100 && r.status < 600) out.status = r.status; const publishedAt = str(r.publishedAt); if (publishedAt && Number.isFinite(Date.parse(publishedAt))) out.publishedAt = publishedAt; const firstSeenTs = str(r.firstSeenTs); if (firstSeenTs && TS_RE.test(firstSeenTs)) out.firstSeenTs = firstSeenTs; const versionOf = str(r.versionOf); if (versionOf) out.versionOf = versionOf; if (r.noContent === true) out.noContent = true; const fb = r.extractorFallback; if (fb && typeof fb === "object" && !Array.isArray(fb)) { const f = fb as Record; const from = str(f.from); if (from && typeof f.fromVersion === "number" && Number.isInteger(f.fromVersion)) { out.extractorFallback = { from, fromVersion: f.fromVersion, error: typeof f.error === "string" ? f.error : "" }; } } // Release 37 lane F: the archive source (a word; absent = the Wayback // Machine) and its copy's URL. const source = str(r.source); if (source && KIND_RE.test(source.replace(/\./g, "-")) && source !== "wayback") out.source = source; const archiveUrl = str(r.archiveUrl); if (archiveUrl && /^https?:\/\//i.test(archiveUrl)) out.archiveUrl = archiveUrl; if (Array.isArray(r.tags)) { const tags = r.tags.filter((t): t is string => typeof t === "string" && t.trim() !== ""); if (tags.length > 0) out.tags = tags; } return out; }