@@ -2,10 +2,12 @@ import fs from "fs";
2
2
import os from "os" ;
3
3
import path from "path" ;
4
4
import { apiClient } from "./apiClient.js" ;
5
+ import config from "../config.js" ;
5
6
6
7
const CACHE_DIR = path . join ( os . homedir ( ) , ".browserstack" , "combined_cache" ) ;
7
8
const CACHE_FILE = path . join ( CACHE_DIR , "data.json" ) ;
8
9
const TTL_MS = 24 * 60 * 60 * 1000 ; // 1 day
10
+ const TTL_STARTED_MS = 3 * 60 * 60 * 1000 ; // 3 Hours
9
11
10
12
export enum BrowserStackProducts {
11
13
LIVE = "live" ,
@@ -64,3 +66,29 @@ export async function getDevicesAndBrowsers(
64
66
65
67
return cache [ type ] ;
66
68
}
69
+
70
+ // Rate limiter for started event (3H)
71
+ export function shouldSendStartedEvent ( ) : boolean {
72
+ try {
73
+ if ( config && config . REMOTE_MCP ) {
74
+ return false ;
75
+ }
76
+ if ( ! fs . existsSync ( CACHE_DIR ) ) {
77
+ fs . mkdirSync ( CACHE_DIR , { recursive : true } ) ;
78
+ }
79
+ let cache : Record < string , any > = { } ;
80
+ if ( fs . existsSync ( CACHE_FILE ) ) {
81
+ const raw = fs . readFileSync ( CACHE_FILE , "utf8" ) ;
82
+ cache = JSON . parse ( raw || "{}" ) ;
83
+ const last = parseInt ( cache . lastStartedEvent , 10 ) ;
84
+ if ( ! isNaN ( last ) && Date . now ( ) - last < TTL_STARTED_MS ) {
85
+ return false ;
86
+ }
87
+ }
88
+ cache . lastStartedEvent = Date . now ( ) ;
89
+ fs . writeFileSync ( CACHE_FILE , JSON . stringify ( cache , null , 2 ) , "utf8" ) ;
90
+ return true ;
91
+ } catch {
92
+ return true ;
93
+ }
94
+ }
0 commit comments