Skip to content

Commit 222e8d6

Browse files
committed
perf(fs): cache resolved allowed directories in safeDelete
Add getAllowedDirectories() helper that caches the path.resolve() calls for allowed directories. Even though getOsTmpDir(), getSocketCacacheDir(), and getSocketUserDir() are memoized, we were still calling path.resolve() on them for every safeDelete/safeDeleteSync invocation. Changes: - Add getAllowedDirectories() helper function with _cachedAllowedDirs cache - Replace inline allowedDirs array construction in both functions - Cache populated once on first call, reused for process lifetime Benefits: - Eliminates repeated path.resolve() calls - Reduces from O(n) to O(1) for directory resolution - Combines benefits of memoized getters + cached resolved paths - Applied to both safeDelete() and safeDeleteSync()
1 parent 5546c6d commit 222e8d6

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

src/fs.ts

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,31 @@ export function readJsonSync(
10561056
})
10571057
}
10581058

1059+
// Cache for resolved allowed directories
1060+
let _cachedAllowedDirs: string[] | undefined
1061+
1062+
/**
1063+
* Get resolved allowed directories for safe deletion with lazy caching.
1064+
* These directories are resolved once and cached for the process lifetime.
1065+
*/
1066+
function getAllowedDirectories(): string[] {
1067+
if (_cachedAllowedDirs === undefined) {
1068+
const path = getPath()
1069+
const {
1070+
getOsTmpDir,
1071+
getSocketCacacheDir,
1072+
getSocketUserDir,
1073+
} = /*@__PURE__*/ require('#lib/paths')
1074+
1075+
_cachedAllowedDirs = [
1076+
path.resolve(getOsTmpDir()),
1077+
path.resolve(getSocketCacacheDir()),
1078+
path.resolve(getSocketUserDir()),
1079+
]
1080+
}
1081+
return _cachedAllowedDirs
1082+
}
1083+
10591084
/**
10601085
* Safely delete a file or directory asynchronously with built-in protections.
10611086
* Uses `del` for safer deletion that prevents removing cwd and above by default.
@@ -1096,18 +1121,7 @@ export async function safeDelete(
10961121
let shouldForce = opts.force !== false
10971122
if (!shouldForce && patterns.length > 0) {
10981123
const path = getPath()
1099-
const {
1100-
getOsTmpDir,
1101-
getSocketCacacheDir,
1102-
getSocketUserDir,
1103-
} = /*@__PURE__*/ require('#lib/paths')
1104-
1105-
// Get allowed directories (all are memoized for performance)
1106-
const allowedDirs = [
1107-
path.resolve(getOsTmpDir()),
1108-
path.resolve(getSocketCacacheDir()),
1109-
path.resolve(getSocketUserDir()),
1110-
]
1124+
const allowedDirs = getAllowedDirectories()
11111125

11121126
// Check if all patterns are within allowed directories.
11131127
const allInAllowedDirs = patterns.every(pattern => {
@@ -1182,18 +1196,7 @@ export function safeDeleteSync(
11821196
let shouldForce = opts.force !== false
11831197
if (!shouldForce && patterns.length > 0) {
11841198
const path = getPath()
1185-
const {
1186-
getOsTmpDir,
1187-
getSocketCacacheDir,
1188-
getSocketUserDir,
1189-
} = /*@__PURE__*/ require('#lib/paths')
1190-
1191-
// Get allowed directories (all are memoized for performance)
1192-
const allowedDirs = [
1193-
path.resolve(getOsTmpDir()),
1194-
path.resolve(getSocketCacacheDir()),
1195-
path.resolve(getSocketUserDir()),
1196-
]
1199+
const allowedDirs = getAllowedDirectories()
11971200

11981201
// Check if all patterns are within allowed directories.
11991202
const allInAllowedDirs = patterns.every(pattern => {

0 commit comments

Comments
 (0)