Skip to content

Commit 18ea380

Browse files
committed
fix(paths): add missing getPathValue caching to getSocketDlxDir
- Refactored getSocketDlxDir() to use getPathValue() for performance - Adds test override support via setPath('socket-dlx-dir', ...) - Fixes pattern inconsistency with getSocketUserDir() and getSocketCacacheDir() - Updated test to call resetPaths() after env var override - Added resetPaths() calls to mockHomeDir() helper for cache invalidation - Updated documentation to reflect caching and test override support
1 parent 6354015 commit 18ea380

File tree

5 files changed

+32
-17
lines changed

5 files changed

+32
-17
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@
714714
"@socketregistry/is-unicode-supported": "1.0.5",
715715
"@socketregistry/packageurl-js": "1.3.5",
716716
"@socketregistry/yocto-spinner": "1.0.25",
717-
"@socketsecurity/lib-stable": "npm:@socketsecurity/lib@5.1.0",
717+
"@socketsecurity/lib-stable": "npm:@socketsecurity/lib@5.1.1",
718718
"@types/node": "24.9.2",
719719
"@typescript/native-preview": "7.0.0-dev.20250920.1",
720720
"@vitest/coverage-v8": "4.0.3",

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/paths/socket.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,21 +114,28 @@ export function getSocketCacacheDir(): string {
114114

115115
/**
116116
* Get the Socket DLX directory (~/.socket/_dlx).
117-
* Can be overridden with environment variables.
117+
* Can be overridden with SOCKET_DLX_DIR environment variable or via setPath() for testing.
118+
* Result is cached via getPathValue for performance.
118119
*
119120
* Priority order:
120-
* 1. SOCKET_DLX_DIR - Full override of DLX cache directory
121-
* 2. SOCKET_HOME/_dlx - Base directory override (inherits from getSocketUserDir)
122-
* 3. Default: $HOME/.socket/_dlx
123-
* 4. Fallback: /tmp/.socket/_dlx (Unix) or %TEMP%\.socket\_dlx (Windows)
121+
* 1. Test override via setPath('socket-dlx-dir', ...)
122+
* 2. SOCKET_DLX_DIR - Full override of DLX cache directory
123+
* 3. SOCKET_HOME/_dlx - Base directory override (inherits from getSocketUserDir)
124+
* 4. Default: $HOME/.socket/_dlx
125+
* 5. Fallback: /tmp/.socket/_dlx (Unix) or %TEMP%\.socket\_dlx (Windows)
124126
*/
125127
export function getSocketDlxDir(): string {
126-
if (getSocketDlxDirEnv()) {
127-
return normalizePath(getSocketDlxDirEnv() as string)
128-
}
129-
return normalizePath(
130-
path.join(getSocketUserDir(), `${SOCKET_APP_PREFIX}${SOCKET_DLX_APP_NAME}`),
131-
)
128+
return getPathValue('socket-dlx-dir', () => {
129+
if (getSocketDlxDirEnv()) {
130+
return normalizePath(getSocketDlxDirEnv() as string)
131+
}
132+
return normalizePath(
133+
path.join(
134+
getSocketUserDir(),
135+
`${SOCKET_APP_PREFIX}${SOCKET_DLX_APP_NAME}`,
136+
),
137+
)
138+
})
132139
}
133140

134141
/**

test/unit/paths/socket.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ describe('paths/socket', () => {
206206

207207
it('should be overridable via env var', () => {
208208
setEnv('SOCKET_DLX_DIR', '/custom/dlx')
209+
resetPaths()
209210
const result = getSocketDlxDir()
210211
expect(result).toBe('/custom/dlx')
211212
})

test/unit/utils/temp-file-helper.mts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import os from 'node:os'
88
import path from 'node:path'
99

1010
import { clearEnv, setEnv } from '@socketsecurity/lib/env/rewire'
11+
import { resetPaths } from '@socketsecurity/lib/paths/rewire'
1112

1213
/**
1314
* Mock the home directory for cross-platform testing.
@@ -40,6 +41,9 @@ export function mockHomeDir(homeDir: string): () => void {
4041
process.env['USERPROFILE'] = homeDir
4142
}
4243

44+
// Reset path cache after env changes.
45+
resetPaths()
46+
4347
// Return restore function.
4448
return () => {
4549
clearEnv('HOME')
@@ -61,6 +65,9 @@ export function mockHomeDir(homeDir: string): () => void {
6165
} else {
6266
process.env['USERPROFILE'] = originalEnv.USERPROFILE
6367
}
68+
69+
// Reset path cache after restoring env.
70+
resetPaths()
6471
}
6572
}
6673

0 commit comments

Comments
 (0)