|
| 1 | +/** |
| 2 | + * @fileoverview On-disk cache metadata helpers for dlx binaries. |
| 3 | + * |
| 4 | + * - `getDlxCachePath` — root of the binary cache (alias of getSocketDlxDir) |
| 5 | + * - `getBinaryCacheMetadataPath` — path to a cache entry's .dlx-metadata.json |
| 6 | + * - `isBinaryCacheValid` — TTL-based liveness check |
| 7 | + * - `writeBinaryCacheMetadata` — atomic write of cache metadata |
| 8 | + * - `cleanDlxCache` — TTL-based eviction sweep |
| 9 | + * - `listDlxCache` — enumerate cached binaries with their metadata |
| 10 | + * |
| 11 | + * Split out of `dlx/binary.ts` for size hygiene. |
| 12 | + */ |
| 13 | + |
| 14 | +/* oxlint-disable socket/prefer-exists-sync -- DLX binary metadata uses stat for size/mtime; not existence-only checks. */ |
| 15 | + |
| 16 | +import process from 'node:process' |
| 17 | +import { existsSync } from 'node:fs' |
| 18 | + |
| 19 | +import { DLX_BINARY_CACHE_TTL } from '../constants/time' |
| 20 | +import { readJson } from '../fs/read-json' |
| 21 | +import { safeDelete } from '../fs/safe' |
| 22 | +import { isObjectObject } from '../objects/predicates' |
| 23 | +import { getSocketDlxDir } from '../paths/socket' |
| 24 | + |
| 25 | +import { ArrayIsArray, ArrayPrototypeFind } from '../primordials/array' |
| 26 | + |
| 27 | +import { DateNow } from '../primordials/date' |
| 28 | + |
| 29 | +import { StringPrototypeStartsWith } from '../primordials/string' |
| 30 | + |
| 31 | +import { getFs, getPath } from './_internal' |
| 32 | + |
| 33 | +import type { DlxMetadata } from './binary-types' |
| 34 | + |
| 35 | +/** |
| 36 | + * Clean expired entries from the DLX cache. |
| 37 | + * |
| 38 | + * @example |
| 39 | + * ```typescript |
| 40 | + * // Remove cache entries older than the default TTL |
| 41 | + * const removed = await cleanDlxCache() |
| 42 | + * |
| 43 | + * // Remove entries older than 1 hour |
| 44 | + * const removed2 = await cleanDlxCache(60 * 60 * 1000) |
| 45 | + * ``` |
| 46 | + */ |
| 47 | +export async function cleanDlxCache( |
| 48 | + maxAge: number = DLX_BINARY_CACHE_TTL, |
| 49 | +): Promise<number> { |
| 50 | + const cacheDir = getDlxCachePath() |
| 51 | + const fs = getFs() |
| 52 | + |
| 53 | + if (!fs.existsSync(cacheDir)) { |
| 54 | + return 0 |
| 55 | + } |
| 56 | + |
| 57 | + let cleaned = 0 |
| 58 | + const now = DateNow() |
| 59 | + const path = getPath() |
| 60 | + const entries = await fs.promises.readdir(cacheDir) |
| 61 | + |
| 62 | + for (const entry of entries) { |
| 63 | + const entryPath = path.join(cacheDir, entry) |
| 64 | + const metaPath = getBinaryCacheMetadataPath(entryPath) |
| 65 | + |
| 66 | + try { |
| 67 | + // eslint-disable-next-line no-await-in-loop |
| 68 | + if (!(await existsSync(entryPath))) { |
| 69 | + continue |
| 70 | + } |
| 71 | + |
| 72 | + // eslint-disable-next-line no-await-in-loop |
| 73 | + const metadata = await readJson(metaPath, { throws: false }) |
| 74 | + if (!metadata || typeof metadata !== 'object' || ArrayIsArray(metadata)) { |
| 75 | + continue |
| 76 | + } |
| 77 | + const timestamp = (metadata as Record<string, unknown>)['timestamp'] |
| 78 | + // If timestamp is missing or invalid, treat as expired (age = infinity) |
| 79 | + const age = |
| 80 | + typeof timestamp === 'number' && timestamp > 0 |
| 81 | + ? now - timestamp |
| 82 | + : Number.POSITIVE_INFINITY |
| 83 | + |
| 84 | + // Treat future timestamps (clock skew) as expired |
| 85 | + if (age < 0 || age > maxAge) { |
| 86 | + // Remove entire cache entry directory. |
| 87 | + // eslint-disable-next-line no-await-in-loop |
| 88 | + await safeDelete(entryPath, { force: true, recursive: true }) |
| 89 | + cleaned += 1 |
| 90 | + } |
| 91 | + } catch { |
| 92 | + // If we can't read metadata, check if directory is empty or corrupted. |
| 93 | + try { |
| 94 | + // eslint-disable-next-line no-await-in-loop |
| 95 | + const contents = await fs.promises.readdir(entryPath) |
| 96 | + if (!contents.length) { |
| 97 | + // Remove empty directory. |
| 98 | + // eslint-disable-next-line no-await-in-loop |
| 99 | + await safeDelete(entryPath) |
| 100 | + cleaned += 1 |
| 101 | + } |
| 102 | + } catch {} |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return cleaned |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * Get metadata file path for a cached binary. |
| 111 | + * |
| 112 | + * @example |
| 113 | + * ```typescript |
| 114 | + * const metaPath = getBinaryCacheMetadataPath('/tmp/dlx-cache/a1b2c3d4') |
| 115 | + * // '/tmp/dlx-cache/a1b2c3d4/.dlx-metadata.json' |
| 116 | + * ``` |
| 117 | + */ |
| 118 | +export function getBinaryCacheMetadataPath(cacheEntryPath: string): string { |
| 119 | + return getPath().join(cacheEntryPath, '.dlx-metadata.json') |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * Get the DLX binary cache directory path. |
| 124 | + * Alias of `getSocketDlxDir` — DLX binary cache uses the same directory |
| 125 | + * as dlx-package for unified DLX storage. |
| 126 | + * |
| 127 | + * @example |
| 128 | + * ```typescript |
| 129 | + * const cachePath = getDlxCachePath() |
| 130 | + * ``` |
| 131 | + */ |
| 132 | +export const getDlxCachePath = getSocketDlxDir |
| 133 | + |
| 134 | +/** |
| 135 | + * Check if a cached binary is still valid. |
| 136 | + * |
| 137 | + * @example |
| 138 | + * ```typescript |
| 139 | + * const ttl = 7 * 24 * 60 * 60 * 1000 |
| 140 | + * const valid = await isBinaryCacheValid('/tmp/dlx-cache/a1b2c3d4', ttl) |
| 141 | + * if (!valid) { |
| 142 | + * // Re-download the binary |
| 143 | + * } |
| 144 | + * ``` |
| 145 | + */ |
| 146 | +export async function isBinaryCacheValid( |
| 147 | + cacheEntryPath: string, |
| 148 | + cacheTtl: number, |
| 149 | +): Promise<boolean> { |
| 150 | + const fs = getFs() |
| 151 | + try { |
| 152 | + const metaPath = getBinaryCacheMetadataPath(cacheEntryPath) |
| 153 | + if (!fs.existsSync(metaPath)) { |
| 154 | + return false |
| 155 | + } |
| 156 | + |
| 157 | + const metadata = await readJson(metaPath, { throws: false }) |
| 158 | + if (!isObjectObject(metadata)) { |
| 159 | + return false |
| 160 | + } |
| 161 | + const now = DateNow() |
| 162 | + const timestamp = (metadata as Record<string, unknown>)['timestamp'] |
| 163 | + // If timestamp is missing or invalid, cache is invalid |
| 164 | + if (typeof timestamp !== 'number' || timestamp <= 0) { |
| 165 | + return false |
| 166 | + } |
| 167 | + const age = now - timestamp |
| 168 | + // Reject future timestamps (clock skew or corruption) |
| 169 | + if (age < 0) { |
| 170 | + return false |
| 171 | + } |
| 172 | + return age < cacheTtl |
| 173 | + } catch { |
| 174 | + return false |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +/** |
| 179 | + * Get information about cached binaries. |
| 180 | + * |
| 181 | + * @example |
| 182 | + * ```typescript |
| 183 | + * const entries = await listDlxCache() |
| 184 | + * for (const entry of entries) { |
| 185 | + * console.log(`${entry.name}: ${entry.size} bytes`) |
| 186 | + * } |
| 187 | + * ``` |
| 188 | + */ |
| 189 | +export async function listDlxCache(): Promise< |
| 190 | + Array<{ |
| 191 | + age: number |
| 192 | + integrity: string |
| 193 | + name: string |
| 194 | + size: number |
| 195 | + url: string |
| 196 | + }> |
| 197 | +> { |
| 198 | + const cacheDir = getDlxCachePath() |
| 199 | + const fs = getFs() |
| 200 | + |
| 201 | + if (!fs.existsSync(cacheDir)) { |
| 202 | + return [] |
| 203 | + } |
| 204 | + |
| 205 | + const results = [] |
| 206 | + const now = DateNow() |
| 207 | + const path = getPath() |
| 208 | + const entries = await fs.promises.readdir(cacheDir) |
| 209 | + |
| 210 | + for (const entry of entries) { |
| 211 | + const entryPath = path.join(cacheDir, entry) |
| 212 | + try { |
| 213 | + // eslint-disable-next-line no-await-in-loop |
| 214 | + if (!(await existsSync(entryPath))) { |
| 215 | + continue |
| 216 | + } |
| 217 | + |
| 218 | + const metaPath = getBinaryCacheMetadataPath(entryPath) |
| 219 | + // eslint-disable-next-line no-await-in-loop |
| 220 | + const metadata = await readJson(metaPath, { throws: false }) |
| 221 | + if (!metadata || typeof metadata !== 'object' || ArrayIsArray(metadata)) { |
| 222 | + continue |
| 223 | + } |
| 224 | + |
| 225 | + const metaObj = metadata as Record<string, unknown> |
| 226 | + |
| 227 | + // Get URL from unified schema (source.url) or legacy schema (url). |
| 228 | + // Allow empty URL for backward compatibility with partial metadata. |
| 229 | + const source = metaObj['source'] as Record<string, unknown> | undefined |
| 230 | + const url = |
| 231 | + (source?.['url'] as string) || (metaObj['url'] as string) || '' |
| 232 | + |
| 233 | + // Find the binary file in the directory. |
| 234 | + // eslint-disable-next-line no-await-in-loop |
| 235 | + const files = await fs.promises.readdir(entryPath) |
| 236 | + const binaryFile = ArrayPrototypeFind( |
| 237 | + files, |
| 238 | + f => !StringPrototypeStartsWith(f, '.'), |
| 239 | + ) |
| 240 | + |
| 241 | + if (binaryFile) { |
| 242 | + const binaryPath = path.join(entryPath, binaryFile) |
| 243 | + // eslint-disable-next-line no-await-in-loop |
| 244 | + const binaryStats = await fs.promises.stat(binaryPath) |
| 245 | + |
| 246 | + results.push({ |
| 247 | + age: now - ((metaObj['timestamp'] as number) || 0), |
| 248 | + integrity: (metaObj['integrity'] as string) || '', |
| 249 | + name: binaryFile, |
| 250 | + size: binaryStats.size, |
| 251 | + url, |
| 252 | + }) |
| 253 | + } |
| 254 | + } catch {} |
| 255 | + } |
| 256 | + |
| 257 | + return results |
| 258 | +} |
| 259 | + |
| 260 | +/** |
| 261 | + * Write metadata for a cached binary. |
| 262 | + * Uses unified schema shared with C++ decompressor and CLI dlxBinary. |
| 263 | + * |
| 264 | + * @example |
| 265 | + * ```typescript |
| 266 | + * await writeBinaryCacheMetadata( |
| 267 | + * '/tmp/dlx-cache/a1b2c3d4', |
| 268 | + * 'a1b2c3d4', |
| 269 | + * 'https://example.com/tool', |
| 270 | + * 'sha512-abc123...', |
| 271 | + * 15000000 |
| 272 | + * ) |
| 273 | + * ``` |
| 274 | + */ |
| 275 | +export async function writeBinaryCacheMetadata( |
| 276 | + cacheEntryPath: string, |
| 277 | + cacheKey: string, |
| 278 | + url: string, |
| 279 | + integrity: string, |
| 280 | + size: number, |
| 281 | +): Promise<void> { |
| 282 | + const metaPath = getBinaryCacheMetadataPath(cacheEntryPath) |
| 283 | + const metadata: DlxMetadata = { |
| 284 | + version: '1.0.0', |
| 285 | + cache_key: cacheKey, |
| 286 | + timestamp: DateNow(), |
| 287 | + integrity, |
| 288 | + size, |
| 289 | + source: { |
| 290 | + type: 'download', |
| 291 | + url, |
| 292 | + }, |
| 293 | + } |
| 294 | + const fs = getFs() |
| 295 | + // Use atomic write-then-rename pattern to prevent corruption on crash |
| 296 | + const tmpPath = `${metaPath}.tmp.${process.pid}` |
| 297 | + await fs.promises.writeFile(tmpPath, JSON.stringify(metadata, null, 2)) |
| 298 | + await fs.promises.rename(tmpPath, metaPath) |
| 299 | +} |
0 commit comments