// Deterministic checks on the built extension. Exit code 1 on any failure. // node scripts/validate-dist.mjs [dist-dir] import fs from "node:fs"; import path from "node:path"; import { LOCK, REPO, sha256, weightsShaKey } from "./lib.mjs"; import { VISION_LOCK } from "./setup-vision-model.mjs"; import { usableBackground, usableSiblings } from "../src/shared/vision.ts"; import { SIBLING_PHRASES } from "../src/shared/siblings.ts"; const DIST = path.resolve(process.argv[2] ?? path.join(REPO, "dist")); const errors = []; const notes = []; const err = (m) => errors.push(m); const rel = (p) => path.relative(DIST, p); function walk(dir) { return fs.readdirSync(dir, { withFileTypes: true }).flatMap((d) => (d.isDirectory() ? walk(path.join(dir, d.name)) : [path.join(dir, d.name)])); } const exists = (p) => fs.existsSync(path.join(DIST, p.split("?")[0])); if (!fs.existsSync(path.join(DIST, "manifest.json"))) { console.error(`✖ ${DIST}/manifest.json not found — run npm run build`); process.exit(1); } const files = walk(DIST); const manifest = JSON.parse(fs.readFileSync(path.join(DIST, "manifest.json"), "utf8")); const pkg = JSON.parse(fs.readFileSync(path.join(REPO, "package.json"), "utf8")); // ── Manifest ────────────────────────────────────────────────────────────── if (manifest.manifest_version !== 3) err("manifest_version must be 3"); if (manifest.name !== "ClearScroll") err(`manifest name is "${manifest.name}"`); if (manifest.version !== pkg.version) err(`manifest version ${manifest.version} != package.json ${pkg.version}`); if (!/^\d+(\.\d+){0,3}$/.test(manifest.version)) err("manifest version is not a valid Chrome version string"); if (!manifest.description || manifest.description.length > 132) err("description missing or longer than 132 chars"); if (manifest.web_accessible_resources) err("web_accessible_resources must not be declared (exposes the extension to fingerprinting)"); const allowedPerms = ["offscreen", "scripting", "storage"]; for (const p of manifest.permissions ?? []) if (!allowedPerms.includes(p)) err(`unexpected permission "${p}"`); const csp = manifest.content_security_policy?.extension_pages ?? ""; if (/'unsafe-eval'|'unsafe-inline'|https?:/.test(csp)) err(`extension CSP is too permissive: ${csp}`); if (!csp.includes("'wasm-unsafe-eval'")) err("extension CSP must allow 'wasm-unsafe-eval' for the engine"); // Cross-origin isolation of the extension pages: SharedArrayBuffer for the // image model's WebAssembly threads. if (manifest.cross_origin_embedder_policy?.value !== "require-corp" || manifest.cross_origin_opener_policy?.value !== "same-origin") err("manifest must declare COEP require-corp and COOP same-origin (image-model threads)"); const images = fs.existsSync(path.join(DIST, "offscreen/vision.worker.js")); const referenced = [ manifest.background?.service_worker, manifest.options_page, manifest.action?.default_popup, ...Object.values(manifest.icons ?? {}), ...Object.values(manifest.action?.default_icon ?? {}), // Registered dynamically by background.js / created as offscreen document: "content/content.js", "content/content.css", "offscreen/offscreen.html", "offscreen/engine.worker.js", "offscreen/pkg/clearscroll_engine_bg.wasm", "offscreen/model/weights.bin", "offscreen/model/vocab.txt", "offscreen/model/model.json", ...(images ? [ "offscreen/vision.worker.js", "offscreen/vision/ort-wasm-simd-threaded.wasm", "offscreen/vision/ort-wasm-simd-threaded.mjs", "offscreen/vision/vision_model_quantized.onnx", "offscreen/vision/text_model_quantized.onnx", "offscreen/vision/tokenizer.json", "offscreen/vision/background.json", "licenses/onnxruntime-LICENSE.txt", "licenses/onnxruntime-ThirdPartyNotices.txt", ] : []), "LICENSE", "THIRD_PARTY_NOTICES.md", "licenses/Apache-2.0.txt", "licenses/THIRD_PARTY_LICENSES.txt", ].filter(Boolean); for (const r of referenced) if (!exists(r)) err(`referenced file missing: ${r}`); // ── HTML references ─────────────────────────────────────────────────────── for (const f of files.filter((f) => f.endsWith(".html"))) { const html = fs.readFileSync(f, "utf8"); if (/]*\bsrc=)[^>]*>\s*\S/.test(html)) err(`${rel(f)}: inline