Files
clean-architecture-frontend…/scripts/check-browser-file-storage-boundaries.ts
DongHyeonka 0d8e4f5adf perf(scripts): browser-file-storage 경계 검사를 ESLint 한 번으로 돌린다
문제: check:browser-file-storage-boundaries는 fixture마다
`pnpm exec eslint`를 새로 띄운다(파일 21개 + stdin 13번). 프로세스마다
flat config를 다시 읽어 desktop에서 53초가 걸리고, FE-GATE-010(66초)의
대부분이다. 같은 검사를 FE-GATE-020의 feature-scaffold 드릴이 복사본에서
한 번 더 돌아(62초) merge 1회에 약 2분을 쓴다.

고른 방법: ESLint Node API 인스턴스 하나로 같은 fixture를 같은 옵션으로
돈다. --no-ignore는 `ignore: false`, --max-warnings=0은 경고가 하나라도
있으면 실패, 파일이 없거나 설정을 못 읽으면 CLI처럼 2로 둔다. 실패
보고는 CLI 기본과 같은 stylish 형식이다. 같은 repo의 check-architecture.ts
·check-i18n.ts가 이미 API를 쓴다. 검사 대상·판정·통과 문구는 그대로다.

위험·한계: CLI가 아닌 API로 돌아 eslint CLI만의 동작(캐시, 색) 차이는
있다. 판정에는 쓰지 않는다.

검증(desktop, 38c0dc7d 위):
- 같은 트리: 전·후 모두 PASS, "34 direct or misplaced native access
  cases rejected". 53초 → 3.3초.
- 변이(forbidden/direct-indexeddb.ts를 무해한 코드로): 전·후 모두 rc=1,
  실패 목록 같음(2건). 변이(allowed fixture에 코드 추가) 두 개: 전·후
  모두 rc=0.
- tsc -p tsconfig.node.json, eslint 이 파일: rc=0
2026-11-11 23:42:14 +09:00

196 lines
6.4 KiB
TypeScript

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<LintOutcome> {
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<LintOutcome> {
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<LintOutcome> {
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`,
);