@@ -34,37 +34,41 @@ export async function getDevicesAndBrowsers(
34
34
fs . mkdirSync ( CACHE_DIR , { recursive : true } ) ;
35
35
}
36
36
37
- let cache : any = { } ;
37
+ let cache : Record < string , any > = { } ;
38
38
39
+ // Load existing cache
39
40
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 ;
51
52
}
52
53
}
53
54
55
+ // Fetch fresh data from BrowserStack
54
56
const liveRes = await apiClient . get ( { url : URLS [ type ] , raise_error : false } ) ;
55
-
56
57
if ( ! liveRes . ok ) {
57
58
throw new Error (
58
- `Failed to fetch configuration from BrowserStack : ${ type } = ${ liveRes . statusText } ` ,
59
+ `Failed to fetch configuration from BrowserStack: ${ type } = ${ liveRes . statusText } ` ,
59
60
) ;
60
61
}
61
62
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 ,
64
67
} ;
65
- fs . writeFileSync ( CACHE_FILE , JSON . stringify ( cache ) , "utf8" ) ;
66
68
67
- return cache [ type ] ;
69
+ fs . writeFileSync ( CACHE_FILE , JSON . stringify ( cache , null , 2 ) , "utf8" ) ;
70
+
71
+ return liveRes . data ;
68
72
}
69
73
70
74
// Rate limiter for started event (3H)
0 commit comments