Skip to content

Commit 8afcb45

Browse files
committed
refactor(dlx): use shared generateCacheKey from @socketsecurity/lib
Replace local generateCacheKey implementation with the shared function from @socketsecurity/lib/dlx to eliminate code duplication and ensure consistent cache key generation across the Socket ecosystem. Changes: - Import generateCacheKey from @socketsecurity/lib/dlx - Update call site to use url:name spec format - Use getSocketDlxDir() for correct cache path (~/.socket/_dlx) - Update tests to expect correct cache path - Remove local generateCacheKey function (10 lines) The shared function uses SHA-512 truncated to 16 chars (aligning with npm/npx) and accepts a spec string in the format "url:binaryName" for binary downloads.
1 parent 272817f commit 8afcb45

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

packages/cli/src/utils/dlx/binary.mts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
* - listDlxCache: Get information about cached binaries
1111
*
1212
* Cache Management:
13-
* - Stores binaries in ~/.socket/cache/dlx-bin (POSIX)
14-
* - Stores binaries in %USERPROFILE%\.socket\cache\dlx-bin (Windows)
15-
* - Uses content-addressed storage (SHA-256 for keys, SHA-512 for content)
13+
* - Stores binaries in ~/.socket/_dlx (POSIX)
14+
* - Stores binaries in %USERPROFILE%\.socket\_dlx (Windows)
15+
* - Uses npm/npx approach: first 16 chars of SHA-512 (shorter Windows paths)
16+
* - Cache key input: URL + binary name for uniqueness
1617
* - Supports TTL-based cache expiration
1718
* - Verifies checksums for security
1819
*
@@ -27,8 +28,10 @@ import { existsSync, promises as fs } from 'node:fs'
2728
import os from 'node:os'
2829
import path from 'node:path'
2930

31+
import { generateCacheKey } from '@socketsecurity/lib/dlx'
3032
import { readJson } from '@socketsecurity/lib/fs'
3133
import { normalizePath } from '@socketsecurity/lib/path'
34+
import { getSocketDlxDir } from '@socketsecurity/lib/paths'
3235
import { spawn } from '@socketsecurity/lib/spawn'
3336

3437
import { DLX_BINARY_CACHE_TTL } from '../../constants/cache.mjs'
@@ -75,14 +78,6 @@ export interface DlxBinaryResult {
7578
spawnPromise: ReturnType<typeof spawn>
7679
}
7780

78-
/**
79-
* Generate a cache directory name from URL, similar to npm/cacache.
80-
* Uses SHA-256 hash for cache keys (like npm's index keys).
81-
* Content verification uses SHA-512 (like npm's content hashes).
82-
*/
83-
function generateCacheKey(url: string): string {
84-
return createHash('sha256').update(url).digest('hex')
85-
}
8681

8782
/**
8883
* Get metadata file path for a cached binary.
@@ -281,11 +276,13 @@ export async function dlxBinary(
281276

282277
// Generate cache paths similar to pnpm/npx structure.
283278
const cacheDir = getDlxCachePath()
284-
const cacheKey = generateCacheKey(url)
285-
const cacheEntryDir = path.join(cacheDir, cacheKey)
286279
const platformKey = `${platform}-${arch}`
287280
const binaryName =
288281
name || `binary-${platformKey}${platform === 'win32' ? '.exe' : ''}`
282+
// Use shared generateCacheKey from @socketsecurity/lib/dlx.
283+
// Spec format for binaries: `${url}:${binaryName}`.
284+
const cacheKey = generateCacheKey(`${url}:${binaryName}`)
285+
const cacheEntryDir = path.join(cacheDir, cacheKey)
289286
const binaryPath = path.join(cacheEntryDir, binaryName)
290287

291288
let downloaded = false
@@ -339,9 +336,10 @@ export async function dlxBinary(
339336
/**
340337
* Get the DLX binary cache directory path.
341338
* Returns normalized path for cross-platform compatibility.
339+
* Uses getSocketDlxDir from socket-lib for correct path: ~/.socket/_dlx
342340
*/
343341
export function getDlxCachePath(): string {
344-
return normalizePath(path.join(getSocketHomePath(), 'cache', 'dlx'))
342+
return getSocketDlxDir()
345343
}
346344

347345
/**

packages/cli/src/utils/dlx/binary.test.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('binary', () => {
5050
it('should return correct cache path', () => {
5151
const result = normalizePath(getDlxCachePath())
5252
const expected = normalizePath(
53-
path.join(os.homedir(), '.socket', 'cache', 'dlx'),
53+
path.join(os.homedir(), '.socket', '_dlx'),
5454
)
5555
expect(result).toBe(expected)
5656
})
@@ -122,7 +122,7 @@ describe('binary', () => {
122122
const socketHome = normalizePath(getSocketHomePath())
123123

124124
expect(cachePath.startsWith(socketHome)).toBe(true)
125-
expect(cachePath.endsWith(normalizePath(path.join('cache', 'dlx')))).toBe(
125+
expect(cachePath.endsWith(normalizePath('_dlx'))).toBe(
126126
true,
127127
)
128128

0 commit comments

Comments
 (0)