Skip to content

Commit 5e092de

Browse files
committed
feat(dlx): align cache key generation with npm/npx pattern
- Changed from SHA-256 (64 chars) to SHA-512 truncated to 16 chars - Optimized for Windows MAX_PATH compatibility (260 character limit) - Accepts collision risk for shorter paths (~1 in 18 quintillion) - Added support for PURL-style package specifications - Documented Socket's shorthand format (without pkg: prefix) - References npm/cli v11.6.2 implementation
1 parent a266143 commit 5e092de

3 files changed

Lines changed: 36 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [2.8.0](https://github.com/SocketDev/socket-lib/releases/tag/v2.8.0) - 2025-10-29
9+
10+
### Changed
11+
12+
- **Enhanced DLX cache key generation with npm/npx compatibility**: Updated cache key strategy to align with npm/npx ecosystem patterns
13+
- Changed from SHA-256 (64 chars) to SHA-512 truncated to 16 chars (matching npm/npx)
14+
- Optimized for Windows MAX_PATH compatibility (260 character limit)
15+
- Accepts collision risk for shorter paths (~1 in 18 quintillion with 1000 entries)
16+
- Added support for PURL-style package specifications (e.g., `npm:prettier@3.0.0`, `pypi:requests@2.31.0`)
17+
- Documented Socket's shorthand format (without `pkg:` prefix) handled by `@socketregistry/packageurl-js`
18+
- References npm/cli v11.6.2 implementation for consistency
19+
820
## [2.7.0](https://github.com/SocketDev/socket-lib/releases/tag/v2.7.0) - 2025-10-28
921

1022
### Added

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@socketsecurity/lib",
3-
"version": "2.7.0",
3+
"version": "2.8.0",
44
"license": "MIT",
55
"description": "Core utilities and infrastructure for Socket.dev security tools",
66
"keywords": [

src/dlx-binary.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,31 @@ export interface DlxBinaryResult {
4040
}
4141

4242
/**
43-
* Generate a cache directory name from URL and binary name.
44-
* Uses SHA256 hash to create content-addressed storage.
45-
* Includes binary name to prevent collisions when multiple binaries
46-
* are downloaded from the same URL with different names.
43+
* Generate a cache directory name using npm/npx approach.
44+
* Uses first 16 characters of SHA-512 hash (like npm/npx).
45+
*
46+
* Rationale for SHA-512 truncated (vs full SHA-256):
47+
* - Matches npm/npx ecosystem behavior
48+
* - Shorter paths for Windows MAX_PATH compatibility (260 chars)
49+
* - 16 hex chars = 64 bits = acceptable collision risk for local cache
50+
* - Collision probability ~1 in 18 quintillion with 1000 entries
51+
*
52+
* Input strategy (aligned with npx):
53+
* - npx uses package spec strings (e.g., '@scope/pkg@1.0.0', 'prettier')
54+
* - For package installs: Use PURL-style spec with version
55+
* Examples: 'npm:prettier@3.0.0', 'pypi:requests@2.31.0', 'gem:rails@7.0.0'
56+
* Note: Socket uses shorthand format without 'pkg:' prefix
57+
* (handled by @socketregistry/packageurl-js)
58+
* - For binary downloads: Use URL + binary name for uniqueness
59+
*
60+
* Reference: npm/cli v11.6.2 libnpmexec/lib/index.js#L233-L244
61+
* https://github.com/npm/cli/blob/v11.6.2/workspaces/libnpmexec/lib/index.js#L233-L244
62+
* Implementation: packages.map().sort().join('\n') → SHA-512 → slice(0,16)
63+
* npx hashes the package spec (name@version), not just name
4764
*/
4865
function generateCacheKey(url: string, name: string): string {
49-
return createHash('sha256').update(`${url}:${name}`).digest('hex')
66+
const input = `${url}:${name}`
67+
return createHash('sha512').update(input).digest('hex').substring(0, 16)
5068
}
5169

5270
/**

0 commit comments

Comments
 (0)