harden cached file loading
Massimo Melina committed
Apr 9, 2026 at 13:01 UTC
9b7a03132051cda44188aaa52442a04c2e4f94c4
1 file changed
+5
-1
src/util-files.ts
+5
-1
@@ -167,8 +167,12 @@ export function exists(path: string) {
167
// parse a file, caching unless timestamp has changed
168
export const parseFileCache = new Map<string, { ts: Date, parsed: unknown }>()
169
export async function loadFileCached<T>(path: string, loader: (path: string) => T) {
170
- const { mtime: ts } = await statWithTimeout(path)
170
const cached = parseFileCache.get(path)
171
+ const ts = await statWithTimeout(path).then(x => x.mtime, e => {
172
+ if (e?.message !== 'timeout')
173
+ throw e
174
+ return cached?.ts || new Date(0) // on timeout (e.g. thread pool saturated), serve cache if any, or attempt the loader
175
+ })
176
if (cached && Number(ts) === Number(cached.ts))
177
return cached.parsed as T
178
const parsed = loader(path)