From 541abf36ed5cc5cab5ec92a0bf0fe921f82ed19a Mon Sep 17 00:00:00 2001 From: Aolinge <153434584+aolingge@users.noreply.github.com> Date: Thu, 7 May 2026 08:02:23 +0800 Subject: [PATCH] fix: avoid scanner file read race --- src/scanner.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/scanner.ts b/src/scanner.ts index 45c8911..e35cd5a 100644 --- a/src/scanner.ts +++ b/src/scanner.ts @@ -1,4 +1,4 @@ -import { readdir, readFile, stat } from "node:fs/promises"; +import { open, readdir } from "node:fs/promises"; import { join, relative } from "node:path"; import { scanFileWithRules } from "./rules.js"; import type { Finding, ScanFile, ScanResult, ScanTargetInput } from "./types.js"; @@ -84,13 +84,16 @@ async function walk(root: string, current: string, files: ScanFile[], excludePat continue; } - const fileStat = await stat(absolutePath); - if (fileStat.size > MAX_FILE_BYTES) { - continue; + const file = await open(absolutePath, "r"); + try { + const fileStat = await file.stat(); + if (fileStat.size <= MAX_FILE_BYTES) { + const content = await file.readFile("utf8"); + files.push({ path: relativePath, content }); + } + } finally { + await file.close(); } - - const content = await readFile(absolutePath, "utf8"); - files.push({ path: relativePath, content }); } }