@@ -40,11 +40,13 @@ export interface DlxBinaryResult {
4040}
4141
4242/**
43- * Generate a cache directory name from URL, similar to pnpm/npx .
43+ * Generate a cache directory name from URL and binary name .
4444 * 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.
4547 */
46- function generateCacheKey ( url : string ) : string {
47- return createHash ( 'sha256' ) . update ( url ) . digest ( 'hex' )
48+ function generateCacheKey ( url : string , name : string ) : string {
49+ return createHash ( 'sha256' ) . update ( ` ${ url } : ${ name } ` ) . digest ( 'hex' )
4850}
4951
5052/**
@@ -72,9 +74,12 @@ async function isCacheValid(
7274 return false
7375 }
7476 const now = Date . now ( )
75- const age =
76- now -
77- ( ( ( metadata as Record < string , unknown > ) [ 'timestamp' ] as number ) || 0 )
77+ const timestamp = ( metadata as Record < string , unknown > ) [ 'timestamp' ]
78+ // If timestamp is missing or invalid, cache is invalid
79+ if ( typeof timestamp !== 'number' || timestamp <= 0 ) {
80+ return false
81+ }
82+ const age = now - timestamp
7883
7984 return age < cacheTtl
8085 } catch {
@@ -166,7 +171,7 @@ async function writeMetadata(
166171 * Clean expired entries from the DLX cache.
167172 */
168173export async function cleanDlxCache (
169- maxAge : number = /*@__INLINE__ */ require ( '../ constants/time' ) . DLX_BINARY_CACHE_TTL ,
174+ maxAge : number = /*@__INLINE__ */ require ( '# constants/time' ) . DLX_BINARY_CACHE_TTL ,
170175) : Promise < number > {
171176 const cacheDir = getDlxCachePath ( )
172177
@@ -197,9 +202,12 @@ export async function cleanDlxCache(
197202 ) {
198203 continue
199204 }
205+ const timestamp = ( metadata as Record < string , unknown > ) [ 'timestamp' ]
206+ // If timestamp is missing or invalid, treat as expired (age = infinity)
200207 const age =
201- now -
202- ( ( ( metadata as Record < string , unknown > ) [ 'timestamp' ] as number ) || 0 )
208+ typeof timestamp === 'number' && timestamp > 0
209+ ? now - timestamp
210+ : Number . POSITIVE_INFINITY
203211
204212 if ( age > maxAge ) {
205213 // Remove entire cache entry directory.
@@ -234,7 +242,7 @@ export async function dlxBinary(
234242 spawnExtra ?: SpawnExtra | undefined ,
235243) : Promise < DlxBinaryResult > {
236244 const {
237- cacheTtl = /*@__INLINE__ */ require ( '../ constants/time' ) . DLX_BINARY_CACHE_TTL ,
245+ cacheTtl = /*@__INLINE__ */ require ( '# constants/time' ) . DLX_BINARY_CACHE_TTL ,
238246 checksum,
239247 force = false ,
240248 name,
@@ -244,9 +252,9 @@ export async function dlxBinary(
244252
245253 // Generate cache paths similar to pnpm/npx structure.
246254 const cacheDir = getDlxCachePath ( )
247- const cacheKey = generateCacheKey ( url )
248- const cacheEntryDir = path . join ( cacheDir , cacheKey )
249255 const binaryName = name || `binary-${ process . platform } -${ os . arch ( ) } `
256+ const cacheKey = generateCacheKey ( url , binaryName )
257+ const cacheEntryDir = path . join ( cacheDir , cacheKey )
250258 const binaryPath = normalizePath ( path . join ( cacheEntryDir , binaryName ) )
251259
252260 let downloaded = false
0 commit comments