import { test } from "node:test"; import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import { fileURLToPath } from "node:url"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test components/LocalFilePicker.test.ts // // THE VISITOR'S OWN COPY NEVER LEAVES THE BROWSER (lane missing-source, slice // 2). The modules that touch the picked file, its object URL or its offset may // not mention any API that can carry bytes off the page or keep them: a fetch, // a beacon, a socket, a worker message, the Cache API, IndexedDB, a service // worker. The offset is the one thing stored, and only through the Storage the // provider hands lib/localFileClock.ts — so neither these modules nor the clock // names localStorage or sessionStorage either. The export e2e // (export/e2e/local-file.spec.ts) checks the running page; this checks the source. const FILES = [ "./LocalFilePicker.tsx", "./LocalFileBar.tsx", "../lib/localFileClock.ts", "../lib/sourceMissing.ts", // The in-browser clipper (lane browser-clip): its panel, its job, its pure // part and the engine that drives the library. "./LocalClipPanel.tsx", "../lib/localClipJob.ts", "../lib/localClip.ts", "../lib/localClipEngine.ts", ]; const FORBIDDEN: ReadonlyArray<[string, RegExp]> = [ ["fetch", /\bfetch\s*\(/], ["XMLHttpRequest", /XMLHttpRequest/], ["sendBeacon", /sendBeacon/], ["WebSocket", /WebSocket/], ["EventSource", /EventSource/], ["postMessage", /postMessage/], ["Cache API", /\bcaches\b/], ["IndexedDB", /indexedDB/i], ["service worker", /serviceWorker/], ["localStorage", /localStorage/], ["sessionStorage", /sessionStorage/], ["a dynamic import", /\bimport\s*\(/], ["FileReader", /FileReader/], ]; // Comments may name what the code must not do; only code is judged. function code(src: string): string { return src.replace(/\/\*[\s\S]*?\*\//g, "").replace(/(^|[^:])\/\/.*$/gm, "$1"); } for (const rel of FILES) { test(`${rel} names no API that could carry or keep the visitor's file`, () => { const src = code(readFileSync(fileURLToPath(new URL(rel, import.meta.url)), "utf8")); for (const [name, re] of FORBIDDEN) { assert.ok(!re.test(src), `${rel} mentions ${name}`); } }); } // The clipper's one door: lib/localClipLoader.ts may hold a dynamic import, // and only of the engine — so the library loads when a clip is asked for and // nothing else rides in with it. test("../lib/localClipLoader.ts holds exactly one dynamic import, of the engine, and nothing else forbidden", () => { const src = code(readFileSync(fileURLToPath(new URL("../lib/localClipLoader.ts", import.meta.url)), "utf8")); const imports = src.match(/\bimport\s*\(([^)]*)\)/g) ?? []; assert.deepEqual(imports, ['import("./localClipEngine")']); for (const [name, re] of FORBIDDEN) { if (name === "a dynamic import") continue; assert.ok(!re.test(src), `localClipLoader.ts mentions ${name}`); } assert.ok(!/^\s*import\s/m.test(src), "localClipLoader.ts has a static import"); }); // The engine takes from the library only what a stream copy of a local Blob // needs: no URL, HLS or stream sources (the library's own network doors). test("../lib/localClipEngine.ts imports no network source from the library", () => { const src = code(readFileSync(fileURLToPath(new URL("../lib/localClipEngine.ts", import.meta.url)), "utf8")); const m = src.match(/import\s*\{([^}]*)\}\s*from\s*"mediabunny"/); assert.ok(m, "the engine imports from mediabunny"); const names = m![1].split(",").map((s) => s.trim().replace(/^type\s+/, "")).filter(Boolean); for (const bad of ["UrlSource", "StreamSource", "ReadableStreamSource", "HLS", "HLS_FORMATS", "ALL_FORMATS", "FilePathSource", "FilePathTarget"]) { assert.ok(!names.includes(bad), `the engine imports ${bad}`); } assert.ok(names.includes("BlobSource")); });