33 */
44
55import { getErrorHandler , safeJsonParse } from "./errors.ts" ;
6+ import type { CacheVary } from "./types.ts" ;
7+
8+ // Strongly typed metadata entry for Vary data (LRU tracking via timestamp)
9+ interface VaryEntry {
10+ // Arbitrary vary data structure (headers/cookies/query lists etc.)
11+ // Use unknown to avoid any; callers narrow as needed
12+ [ key : string ] : unknown ;
13+ timestamp : number ; // LRU timestamp (ms)
14+ }
615
716const METADATA_LOCK_PREFIX = "https://cache-internal/lock-" ;
817const METADATA_LOCK_TIMEOUT = 5000 ; // 5 seconds
@@ -46,12 +55,7 @@ export async function atomicMetadataUpdate<T>(
4655 const updatedData = updateFn ( currentData ) ;
4756
4857 // Write back updated metadata
49- await cache . put (
50- metadataKey ,
51- new Response ( JSON . stringify ( updatedData ) , {
52- headers : { "Content-Type" : "application/json" } ,
53- } ) ,
54- ) ;
58+ await cache . put ( metadataKey , Response . json ( updatedData ) ) ;
5559
5660 return ; // Success
5761 } finally {
@@ -106,12 +110,7 @@ async function tryAcquireLock(cache: Cache, lockKey: string): Promise<boolean> {
106110 pid : Math . random ( ) . toString ( 36 ) . substring ( 2 ) , // Simple process identifier
107111 } ;
108112
109- await cache . put (
110- lockKey ,
111- new Response ( JSON . stringify ( lockData ) , {
112- headers : { "Content-Type" : "application/json" } ,
113- } ) ,
114- ) ;
113+ await cache . put ( lockKey , Response . json ( lockData ) ) ;
115114
116115 return true ;
117116 } catch ( error ) {
@@ -180,37 +179,37 @@ export async function updateVaryMetadata(
180179 cache : Cache ,
181180 metadataKey : string ,
182181 requestUrl : string ,
183- varyData : any ,
182+ varyData : CacheVary ,
184183 maxEntries = 1000 ,
185184) : Promise < void > {
186185 await atomicMetadataUpdate (
187186 cache ,
188187 metadataKey ,
189- ( metadata : Record < string , any > ) => {
188+ ( metadata : Record < string , VaryEntry > ) => {
190189 // Add timestamp for LRU cleanup
191- metadata [ requestUrl ] = {
190+ const entry : VaryEntry = {
192191 ...varyData ,
193192 timestamp : Date . now ( ) ,
194193 } ;
194+ metadata [ requestUrl ] = entry ;
195195
196196 // Implement LRU cleanup if we exceed maxEntries
197- const entries = Object . entries ( metadata ) ;
197+ const entries : Array < [ string , VaryEntry ] > = Object . entries (
198+ metadata ,
199+ ) as Array < [ string , VaryEntry ] > ;
198200 if ( entries . length > maxEntries ) {
199- // Sort by timestamp (oldest first) and remove oldest entries
200- entries . sort ( ( [ , a ] , [ , b ] ) => ( a . timestamp || 0 ) - ( b . timestamp || 0 ) ) ;
201+ // Sort by timestamp (oldest first) and keep newest maxEntries
202+ entries . sort ( ( [ , a ] , [ , b ] ) => a . timestamp - b . timestamp ) ;
201203 const toKeep = entries . slice ( - maxEntries ) ;
202-
203- // Rebuild metadata with only the entries to keep
204- const cleanedMetadata : Record < string , any > = { } ;
205- for ( const [ key , value ] of toKeep ) {
206- cleanedMetadata [ key ] = value ;
204+ const cleanedMetadata : Record < string , VaryEntry > = { } ;
205+ for ( const [ k , v ] of toKeep ) {
206+ cleanedMetadata [ k ] = v ;
207207 }
208208 return cleanedMetadata ;
209209 }
210-
211210 return metadata ;
212211 } ,
213- { } as Record < string , any > ,
212+ { } as Record < string , VaryEntry > ,
214213 ) ;
215214}
216215
@@ -225,19 +224,18 @@ export async function cleanupVaryMetadata(
225224 await atomicMetadataUpdate (
226225 cache ,
227226 metadataKey ,
228- ( metadata : Record < string , any > ) => {
227+ ( metadata : Record < string , VaryEntry > ) => {
229228 const now = Date . now ( ) ;
230- const cleanedMetadata : Record < string , any > = { } ;
231-
232- for ( const [ key , value ] of Object . entries ( metadata ) ) {
233- const timestamp = value . timestamp || 0 ;
234- if ( now - timestamp < maxAge ) {
235- cleanedMetadata [ key ] = value ;
229+ const cleanedMetadata : Record < string , VaryEntry > = { } ;
230+ for (
231+ const [ k , v ] of Object . entries ( metadata ) as Array < [ string , VaryEntry ] >
232+ ) {
233+ if ( now - v . timestamp < maxAge ) {
234+ cleanedMetadata [ k ] = v ;
236235 }
237236 }
238-
239237 return cleanedMetadata ;
240238 } ,
241- { } as const ,
239+ { } as Record < string , VaryEntry > ,
242240 ) ;
243241}
0 commit comments