@@ -34,37 +34,41 @@ export async function getDevicesAndBrowsers(
3434 fs . mkdirSync ( CACHE_DIR , { recursive : true } ) ;
3535 }
3636
37- let cache : any = { } ;
37+ let cache : Record < string , any > = { } ;
3838
39+ // Load existing cache
3940 if ( fs . existsSync ( CACHE_FILE ) ) {
40- const stats = fs . statSync ( CACHE_FILE ) ;
41- if ( Date . now ( ) - stats . mtimeMs < TTL_MS ) {
42- try {
43- cache = JSON . parse ( fs . readFileSync ( CACHE_FILE , "utf8" ) ) ;
44- if ( cache [ type ] ) {
45- return cache [ type ] ;
46- }
47- } catch ( error ) {
48- console . error ( "Error parsing cache file:" , error ) ;
49- // Continue with fetching fresh data
50- }
41+ try {
42+ cache = JSON . parse ( fs . readFileSync ( CACHE_FILE , "utf8" ) ) ;
43+ } catch ( err ) {
44+ console . error ( "Error parsing cache file:" , err ) ;
45+ cache = { } ;
46+ }
47+
48+ // Check per-product TTL
49+ const cachedEntry = cache [ type ] ;
50+ if ( cachedEntry ?. timestamp && Date . now ( ) - cachedEntry . timestamp < TTL_MS ) {
51+ return cachedEntry . data ;
5152 }
5253 }
5354
55+ // Fetch fresh data from BrowserStack
5456 const liveRes = await apiClient . get ( { url : URLS [ type ] , raise_error : false } ) ;
55-
5657 if ( ! liveRes . ok ) {
5758 throw new Error (
58- `Failed to fetch configuration from BrowserStack : ${ type } = ${ liveRes . statusText } ` ,
59+ `Failed to fetch configuration from BrowserStack: ${ type } = ${ liveRes . statusText } ` ,
5960 ) ;
6061 }
6162
62- cache = {
63- [ type ] : liveRes . data ,
63+ // Save to cache with timestamp and data directly under product key
64+ cache [ type ] = {
65+ timestamp : Date . now ( ) ,
66+ data : liveRes . data ,
6467 } ;
65- fs . writeFileSync ( CACHE_FILE , JSON . stringify ( cache ) , "utf8" ) ;
6668
67- return cache [ type ] ;
69+ fs . writeFileSync ( CACHE_FILE , JSON . stringify ( cache , null , 2 ) , "utf8" ) ;
70+
71+ return liveRes . data ;
6872}
6973
7074// Rate limiter for started event (3H)
0 commit comments