/** * THE TEXT OF A POLICY PAGE, as the policy archive keeps it (docs/POLICY_ARCHIVE.md; the watcher * supabase/functions/policy-watch, the pages server/policies.mjs and src/pages/Policy*.tsx). * * A terms-of-service, privacy, usage-policy or licence page is read once a day; its text is kept * and hashed, and a new version is recorded only when the hash moves. So the text must not move by * itself: the scripts, styles, menus and every attribute are dropped, and of the words only what * is noise is taken out — a copyright year in the page's foot, a long printed token (a build id, * a nonce). Dates in the words are KEPT: a policy's "Last updated" or "Effective" date is part of * the document, and its change is a change of the document. * * The method follows Open Terms Archive's (opentermsarchive.org, engine EUPL-1.2): the page is * read, its main part selected, the rest removed, and what is left kept as a readable text in * which headings and list items keep their marks (`#`, `-`). Our copies are our own readings. * * Pure: no Node, no Deno, no network beyond Web Crypto, so the tests read a page as the edge * function does. Kept in _shared as TypeScript: the deploy uploads only that folder's .ts files * (scripts/ops/edge-imports.test.ts). */ /** Fewer characters than this is a wall, a shell drawn by script or an error page: not a reading. */ export const POLICY_MIN_CHARS = 400; /** A text longer than this is cut: no policy page we watch is near it (the longest ~200 KB). */ export const POLICY_MAX_CHARS = 800_000; /** What a bot wall, a challenge page or a script-only shell says in its first words. */ /** How a document is cut from its page (`policy_documents.extract`). */ export interface PolicyExtract { start?: string; end?: string; drop?: string[]; } export const WALL = /just a moment\.{0,3}|verify (?:that )?you are (?:a )?human|access denied|enable javascript (?:and cookies )?to continue|checking your browser|attention required|request blocked|are you a robot|unusual traffic/i; const NAMED: Record = { amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", nbsp: ' ', rsquo: '’', lsquo: '‘', rdquo: '”', ldquo: '“', mdash: '—', ndash: '–', hellip: '…', copy: '©', reg: '®', trade: '™', middot: '·', bull: '•', sect: '§', para: '¶', laquo: '«', raquo: '»', euro: '€', pound: '£', times: '×', shy: '', zwj: '', zwnj: '', }; /** Character references to characters. An unknown named one is left as written. */ export function decodeEntities(text: string): string { return String(text).replace( /&(#x[0-9a-f]+|#\d+|[a-z][a-z0-9]*);/gi, (whole: string, ref: string) => { if (ref[0] === '#') { const code = ref[1] === 'x' || ref[1] === 'X' ? parseInt(ref.slice(2), 16) : Number(ref.slice(1)); if (!Number.isFinite(code) || code <= 0 || code > 0x10ffff) return ' '; try { return String.fromCodePoint(code); } catch { return ' '; } } const named = NAMED[ref.toLowerCase()]; return named === undefined ? whole : named; } ); } /** Elements whose content is never the document. */ const DROP_ALWAYS = [ 'script', 'style', 'noscript', 'template', 'svg', 'iframe', 'head', 'object', 'canvas', ]; /** Chrome: menus, forms and buttons, inside or outside the main part. */ const DROP_CHROME = ['nav', 'form', 'button', 'select', 'dialog']; /** The page's own header and footer: dropped only when no main part was found (inside one they * can hold the document's title and its date). */ const DROP_PAGE = ['header', 'footer', 'aside']; const dropElements = (html: string, names: readonly string[]): string => { let out = html; for (const name of names) { // Twice: a non-greedy match leaves the outer part of one nested in its own kind. for (let i = 0; i < 2; i++) out = out.replace(new RegExp(`<${name}\\b[^>]*>[\\s\\S]*?<\\/${name}\\s*>`, 'gi'), ' '); // A void or unclosed one (