@@ -6,8 +6,6 @@ import { mkdir, readFile, writeFile } from 'node:fs/promises'
66import { homedir } from 'node:os'
77import { join } from 'node:path'
88
9- import colors from 'yoctocolors-cjs'
10-
119import { logger } from '@socketsecurity/registry/lib/logger'
1210
1311interface CacheEntry < T = any > {
@@ -19,14 +17,17 @@ interface CacheEntry<T = any> {
1917}
2018
2119interface CacheOptions {
22- ttl ?: number // Time to live in milliseconds
23- offline ?: boolean // Force offline mode
24- refresh ?: boolean // Force refresh even if cached
25- namespace ?: string // Cache namespace for organization
20+ // Time to live in milliseconds
21+ ttl ?: number
22+ // Force offline mode
23+ offline ?: boolean
24+ // Force refresh even if cached
25+ refresh ?: boolean
26+ // Cache namespace for organization
27+ namespace ?: string
2628}
2729
2830const CACHE_DIR = join ( homedir ( ) , '.socket' , '_cacache' )
29- const CACHE_METADATA_FILE = 'cache-metadata.json'
3031
3132/**
3233 * Get cache key for a given operation
@@ -65,7 +66,8 @@ async function readFromCache<T>(
6566 // Check if expired
6667 const now = Date . now ( )
6768 if ( entry . ttl > 0 && now - entry . timestamp > entry . ttl ) {
68- return null // Expired
69+ // Expired
70+ return null
6971 }
7072
7173 return entry
@@ -88,8 +90,9 @@ async function writeToCache<T>(
8890 const entry : CacheEntry < T > = {
8991 data,
9092 timestamp : Date . now ( ) ,
91- ttl : options . ttl || 3600000 , // Default 1 hour
92- metadata : options . namespace ? { namespace : options . namespace } : undefined ,
93+ // Default 1 hour
94+ ttl : options . ttl || 3600000 ,
95+ ...( options . namespace ? { metadata : { namespace : options . namespace } } : { } ) ,
9396 }
9497
9598 await writeFile ( path , JSON . stringify ( entry , null , 2 ) )
@@ -209,12 +212,16 @@ export async function getCacheStats(): Promise<{
209212 oldest ?: Date
210213 newest ?: Date
211214} > {
212- const stats = {
215+ const stats : {
216+ size : number
217+ entries : number
218+ namespaces : string [ ]
219+ oldest ?: Date
220+ newest ?: Date
221+ } = {
213222 size : 0 ,
214223 entries : 0 ,
215- namespaces : [ ] as string [ ] ,
216- oldest : undefined as Date | undefined ,
217- newest : undefined as Date | undefined ,
224+ namespaces : [ ] ,
218225 }
219226
220227 if ( ! existsSync ( CACHE_DIR ) ) {
@@ -232,12 +239,14 @@ export async function getCacheStats(): Promise<{
232239
233240 for ( const ns of namespaces ) {
234241 const nsPath = join ( CACHE_DIR , ns )
242+ // eslint-disable-next-line no-await-in-loop
235243 const files = await readdir ( nsPath )
236244
237245 for ( const file of files ) {
238246 if ( file . endsWith ( '.json' ) ) {
239247 stats . entries ++
240248 const filePath = join ( nsPath , file )
249+ // eslint-disable-next-line no-await-in-loop
241250 const fileStat = await stat ( filePath )
242251 stats . size += fileStat . size
243252
0 commit comments