blob: e8714b2a54203312dde9a7dfc5301525f0dfeb6f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
export const requested = {};
export const recorded = {};
export function track(pathArg, {
readdir = false,
stat = false,
}) {
pathArg = path.resolve(pathArg);
requested[pathArg] = {readdir, stat};
}
export function tracking(pathArg, resolved = false) {
if (!resolved) pathArg = path.resolve(pathArg);
for (const key of Object.keys(requested)) {
if (pathArg.startsWith(key + '/')) {
return requested[key];
}
}
return null;
}
async function go(fn, key, pathArg) {
pathArg = path.resolve(pathArg);
const info = tracking(pathArg, true);
if (!info?.[key]) return fn(pathArg);
const record = recorded[pathArg]?.[key];
if (record) return record;
const result = await fn(pathArg);
recorded[pathArg] ??= {};
recorded[pathArg][key] = result;
return result;
}
export function stat(pathArg) {
return go(fs.stat, 'stat', pathArg);
}
export function readdir(pathArg) {
return go(fs.readdir, 'readdir', pathArg);
}
export function reset() {
for (const key in requested) delete requested[key];
for (const key in recorded) delete recorded[key];
}
|