import { readFile, readdir } from "node:fs/promises"; import path from "node:path"; import { ESLint } from "eslint"; /** A lint run's verdict as `eslint --no-ignore --max-warnings=0` would exit, and its report. */ type LintOutcome = Readonly<{ status: number; output: string }>; // One ESLint instance lints every fixture. The check used to start // `pnpm exec eslint` once per fixture (about 30 processes), and loading the // flat config each time was most of FE-GATE-010's minute. The options match // that command line: --no-ignore is `ignore: false`, --max-warnings=0 makes a // warning fail like an error. const eslint = new ESLint({ ignore: false }); const formatter = await eslint.loadFormatter("stylish"); async function lintOutcome(results: ESLint.LintResult[]): Promise { const failed = results.some((result) => result.errorCount > 0 || result.warningCount > 0); return { status: failed ? 1 : 0, output: await formatter.format(results) }; } async function runEslint(target: string): Promise { try { return await lintOutcome(await eslint.lintFiles([target])); } catch (error) { // The CLI exits 2 when nothing matches or the config fails to load. return { status: 2, output: `${error instanceof Error ? error.message : String(error)}\n` }; } } async function runEslintSource(source: string, virtualPath: string): Promise { return lintOutcome(await eslint.lintText(source, { filePath: virtualPath, warnIgnored: true })); } const allowed = await runEslint( "tests/fixtures/browser-file-storage-boundaries/allowed", ); const forbiddenRoot = "tests/fixtures/browser-file-storage-boundaries/forbidden"; const forbiddenFiles = ( await readdir(forbiddenRoot, { withFileTypes: true }) ) .filter((entry) => entry.isFile() && /\.tsx?$/u.test(entry.name)) .map((entry) => path.join(forbiddenRoot, entry.name)); const forbiddenResults: Array<{ file: string; result: LintOutcome }> = []; for (const file of forbiddenFiles) { forbiddenResults.push({ file, result: await runEslint(file) }); } const acceptedForbidden = forbiddenResults.filter( ({ result }) => result.status === 0, ); const indexedDbBypassSource = await readFile( path.join(forbiddenRoot, "direct-indexeddb.ts"), "utf8", ); const objectUrlBypassSource = await readFile( path.join(forbiddenRoot, "object-url.ts"), "utf8", ); const blobBypassSource = await readFile( path.join(forbiddenRoot, "direct-blob.ts"), "utf8", ); const crossContextAdapterSource = await readFile( "tests/fixtures/browser-file-storage-boundaries/allowed/cross-context-host.ts", "utf8", ); const browserStorageAdapterSource = await readFile( "tests/fixtures/browser-file-storage-boundaries/allowed/browser-storage-host.ts", "utf8", ); const protectedSourceFixtureNames = [ "aliased-globalthis.ts", "class-field-alias.ts", "constructor-root-escape.ts", "default-parameter-alias.ts", "direct-blob.ts", "dynamic-capability-key.ts", "global-object-container.ts", "identity-wrapped-global.ts", "instance-property-alias.ts", "property-descriptor-access.ts", ] as const; const protectedSourceFixtures = await Promise.all( protectedSourceFixtureNames.map(async (file) => ({ file, source: await readFile(path.join(forbiddenRoot, file), "utf8"), })), ); const approvedAdapterResults = [ { file: "src/adapters/storage/indexeddb/boundary-fixture.ts", result: await runEslintSource( indexedDbBypassSource, "src/adapters/storage/indexeddb/boundary-fixture.ts", ), }, { file: "src/adapters/cross-context-invalidation/boundary-fixture.ts", result: await runEslintSource( crossContextAdapterSource, "src/adapters/cross-context-invalidation/boundary-fixture.ts", ), }, { file: "src/adapters/storage/boundary-fixture.ts", result: await runEslintSource( browserStorageAdapterSource, "src/adapters/storage/boundary-fixture.ts", ), }, { file: "src/adapters/browser-transfer/boundary-fixture.ts", result: await runEslintSource( blobBypassSource, "src/adapters/browser-transfer/boundary-fixture.ts", ), }, ]; const rejectedApprovedAdapters = approvedAdapterResults.filter( ({ result }) => result.status !== 0, ); const misplacedAdapterResults = [ { file: "src/adapters/http/boundary-fixture.ts", result: await runEslintSource( indexedDbBypassSource, "src/adapters/http/boundary-fixture.ts", ), }, { file: "src/features/fixture-feature/adapters/boundary-fixture.ts", result: await runEslintSource( objectUrlBypassSource, "src/features/fixture-feature/adapters/boundary-fixture.ts", ), }, { file: "src/adapters/http/cross-context-boundary-fixture.ts", result: await runEslintSource( crossContextAdapterSource, "src/adapters/http/cross-context-boundary-fixture.ts", ), }, { file: "src/adapters/http/browser-storage-boundary-fixture.ts", result: await runEslintSource( browserStorageAdapterSource, "src/adapters/http/browser-storage-boundary-fixture.ts", ), }, ]; for (const { file, source } of protectedSourceFixtures) { const virtualPath = `src/presentation/${file}`; misplacedAdapterResults.push({ file: virtualPath, result: await runEslintSource(source, virtualPath), }); } const acceptedMisplacedAdapters = misplacedAdapterResults.filter( ({ result }) => result.status === 0, ); if ( allowed.status !== 0 || rejectedApprovedAdapters.length > 0 || forbiddenFiles.length === 0 || acceptedForbidden.length > 0 || acceptedMisplacedAdapters.length > 0 ) { process.stderr.write(allowed.output); for (const { file, result } of rejectedApprovedAdapters) { process.stderr.write( `${file}: owned browser capability adapter was rejected\n`, ); process.stderr.write(result.output); } for (const { file, result } of acceptedForbidden) { process.stderr.write( `${file}: forbidden browser API fixture was accepted\n`, ); process.stderr.write(result.output); } for (const { file, result } of acceptedMisplacedAdapters) { process.stderr.write( `${file}: native browser storage access outside its owned adapter was accepted\n`, ); process.stderr.write(result.output); } process.exit(1); } process.stdout.write( `Browser file/storage boundaries: PASS (owned adapter allowed, ${forbiddenFiles.length + misplacedAdapterResults.length} direct or misplaced native access cases rejected)\n`, );