// THE DISPATCHER (release 37 S1): a capture goes to the FIRST site extractor // (extractors/index.ts, SITE_EXTRACTORS) whose `matches` is true, else to the // generic extractor. A site extractor that throws, or hands back no record, // falls back to the generic one, and the record says so // (`page.extractorFallback`). Site extractors are offered HTML only; a // text/plain or JSON capture is the generic extractor's. // // Pure apart from TextDecoder (no node imports): what a capture's bytes say, // decoded by the charset the capture names. import { SITE_EXTRACTORS } from "./extractors/index"; import { genericExtractor } from "./extractors/generic"; import type { PageExtractInput, PageExtractor, PageRecordDraft } from "./types"; import { bareMimetype } from "./urls"; export type ExtractPageResult = { drafts: PageRecordDraft[]; // Which extractor made the drafts. extractor: { name: string; version: number }; // The site extractor that claimed the capture and failed, when one did. fallback?: { from: string; fromVersion: number; error: string }; }; // Which extractor a capture goes to (before it runs): the first that claims // it, else the generic one. What a re-extract compares against the stored // capture's extractor and version. export function chooseExtractor( input: PageExtractInput, extractors: readonly PageExtractor[] = SITE_EXTRACTORS, ): PageExtractor { if (bareMimetype(input.capture.mimetype ?? "text/html") !== "text/html") return genericExtractor; let url: URL; try { url = new URL(input.capture.originalUrl); } catch { return genericExtractor; } for (const x of extractors) { try { if (x.matches(url, input.html)) return x; } catch { // A matcher that throws claims nothing. } } return genericExtractor; } export function extractPage( input: PageExtractInput, extractors: readonly PageExtractor[] = SITE_EXTRACTORS, ): ExtractPageResult { const chosen = chooseExtractor(input, extractors); const generic = (): PageRecordDraft[] => genericExtractor.extract(input); const self = { name: chosen.name, version: chosen.version }; if (chosen === genericExtractor) return { drafts: generic(), extractor: self }; let error: string; try { const drafts = chosen.extract(input); if (drafts.length > 0) return { drafts, extractor: self }; error = "it made no record of the capture"; } catch (e) { error = (e as Error)?.message || String(e); } return { drafts: generic(), extractor: { name: genericExtractor.name, version: genericExtractor.version }, fallback: { from: chosen.name, fromVersion: chosen.version, error }, }; } // ─── Bytes → text ─── // The charset a Content-Type names, else the one the document declares in its // first 4 KB (``, ``, an // XML declaration), else UTF-8. A label TextDecoder does not know is UTF-8. export function decodeCaptureBody(body: Uint8Array, contentType?: string | null): string { const fromHeader = /charset\s*=\s*["']?([\w.:-]+)/i.exec(contentType ?? "")?.[1]; let label = fromHeader; if (!label) { const head = new TextDecoder("latin1").decode(body.subarray(0, 4096)); label = /]+charset\s*=\s*["']?([\w.:-]+)/i.exec(head)?.[1] ?? /<\?xml[^>]+encoding\s*=\s*["']([\w.:-]+)/i.exec(head)?.[1]; } let decoder: TextDecoder; try { decoder = new TextDecoder(label ?? "utf-8"); } catch { decoder = new TextDecoder("utf-8"); } return decoder.decode(body).replace(/^/, ""); }