@@ -90,53 +90,42 @@ export function installModCDPServer(globalScope: ModCDPGlobalScope = globalThis
9090 } ;
9191 const attachedDebuggees = new Set < string > ( ) ;
9292 let runtime_types_promise : Promise < unknown > | null = null ;
93- const DOWNSTREAM_CLIENT_KEY = "downstream_client" ;
94- const registered_downstream_client_keys = new Set < string > ( ) ;
95- const downstream_client_leases = new Map <
96- string ,
97- {
98- cdpSessionId : string | null ;
99- last_seen_at : number ;
100- timer : ReturnType < typeof setTimeout > ;
101- }
102- > ( ) ;
103-
104- function downstreamClientKey ( _cdpSessionId : string | null ) {
105- return DOWNSTREAM_CLIENT_KEY ;
93+ let downstream_client_registered = false ;
94+ let downstream_client_lease : {
95+ cdpSessionId : string | null ;
96+ last_seen_at : number ;
97+ timer : ReturnType < typeof setTimeout > ;
98+ } | null = null ;
99+
100+ function registerDownstreamClient ( ) {
101+ downstream_client_registered = true ;
106102 }
107103
108- function registerDownstreamClient ( cdpSessionId : string | null ) {
109- const client_key = downstreamClientKey ( cdpSessionId ) ;
110- registered_downstream_client_keys . add ( client_key ) ;
111- return client_key ;
112- }
113-
114- function clearDownstreamClientLease ( client_key : string ) {
115- const lease = downstream_client_leases . get ( client_key ) ;
104+ function clearDownstreamClientLease ( ) {
105+ const lease = downstream_client_lease ;
116106 if ( ! lease ) return null ;
117107 clearTimeout ( lease . timer ) ;
118- downstream_client_leases . delete ( client_key ) ;
108+ downstream_client_lease = null ;
119109 return lease ;
120110 }
121111
122112 function touchDownstreamClientLease ( cdpSessionId : string | null ) {
123113 const timeout_ms = ModCDPServer . downstream_client_timeout_ms ;
124114 if ( ! ( timeout_ms > 0 ) ) return ;
125- const client_key = downstreamClientKey ( cdpSessionId ) ;
126- if ( ! registered_downstream_client_keys . has ( client_key ) ) return ;
115+ if ( ! downstream_client_registered ) return ;
127116 const last_seen_at = Date . now ( ) ;
128- clearDownstreamClientLease ( client_key ) ;
117+ clearDownstreamClientLease ( ) ;
129118 const timer = setTimeout ( ( ) => {
130- const expired = clearDownstreamClientLease ( client_key ) ;
119+ const expired = clearDownstreamClientLease ( ) ;
131120 if ( ! expired ) return ;
132121 if ( ModCDPServer . close_browser_on_downstream_disconnect !== true ) return ;
133122 void ModCDPServer . sendLoopback ( "Browser.close" , { } , null ) . catch ( ( ) => { } ) ;
134123 } , timeout_ms ) ;
135- downstream_client_leases . set ( client_key , {
124+ downstream_client_lease = {
136125 cdpSessionId,
137126 last_seen_at,
138127 timer,
139- } ) ;
128+ } ;
140129 }
141130
142131 function nativeCommandSchema ( method : string ) {
@@ -1272,7 +1261,7 @@ export function installModCDPServer(globalScope: ModCDPGlobalScope = globalThis
12721261 } ,
12731262
12741263 async handleCommand ( method : string , params : ProtocolParams = { } , cdpSessionId : string | null = null ) {
1275- if ( method === "Mod.configure" ) registerDownstreamClient ( cdpSessionId ) ;
1264+ if ( method === "Mod.configure" ) registerDownstreamClient ( ) ;
12761265 touchDownstreamClientLease ( cdpSessionId ) ;
12771266 const request = { method, params, cdpSessionId } ;
12781267 const middlewareParams = await this . runMiddleware ( "request" , method , params , { cdpSessionId, request } ) ;
0 commit comments