@@ -7,7 +7,8 @@ export class NodeCdpTransport {
77 this . _sessions = new Map ( ) ;
88 this . _children = new Map ( ) ;
99 this . _listeners = new Map ( ) ;
10- this . _unsubscribe = connection . subscribe ( ( event ) => this . _receiveEvent ( event ) ) ;
10+ this . _unsubscribe = null ;
11+ this . _pendingAttaches = 0 ;
1112 }
1213
1314 async listTargets ( ) {
@@ -30,20 +31,30 @@ export class NodeCdpTransport {
3031 }
3132
3233 async attach ( targetId ) {
33- const { sessionId } = await this . _connection . send ( "Target.attachToTarget" , { targetId, flatten : true } ) ;
34- const session = Object . freeze ( { id : sessionId , targetId : String ( targetId ) } ) ;
34+ this . _unsubscribe ||= this . _connection . subscribe ( ( event ) => this . _receiveEvent ( event ) ) ;
35+ this . _pendingAttaches += 1 ;
36+ let sessionId ;
3537 try {
38+ ( { sessionId } = await this . _attachTarget ( targetId ) ) ;
39+ const session = Object . freeze ( { id : sessionId , targetId : String ( targetId ) } ) ;
40+ this . _sessions . set ( sessionId , session ) ;
3641 // Same-origin navigation도 opaque locator document epoch을 바꿔야 한다. Page domain은
3742 // transport 운영 이벤트용으로 내부 활성화하며 raw command permission에는 추가하지 않는다.
3843 await this . _connection . send ( "Page.enable" , { } , session . id ) ;
39- this . _sessions . set ( sessionId , session ) ;
4044 if ( ! this . _guard ) await this . _autoAttach ( session . id ) ;
45+ if ( ! this . _sessions . has ( sessionId ) ) throw new Error ( "browser target detached during attach" ) ;
4146 return session ;
4247 } catch ( error ) {
43- await Promise . allSettled ( [
44- this . _connection . send ( "Target.detachFromTarget" , { sessionId } ) ,
45- ] ) ;
48+ if ( sessionId ) {
49+ await Promise . allSettled ( [
50+ this . _detachSession ( sessionId ) ,
51+ ] ) ;
52+ this . _removeSession ( sessionId ) ;
53+ }
4654 throw error ;
55+ } finally {
56+ this . _pendingAttaches -= 1 ;
57+ this . _releaseSubscription ( ) ;
4758 }
4859 }
4960
@@ -110,22 +121,40 @@ export class NodeCdpTransport {
110121
111122 async detach ( session ) {
112123 if ( ! this . _sessions . has ( session . id ) ) return ;
113- try { await this . _connection . send ( "Target.detachFromTarget" , { sessionId : session . id } ) ; }
114- finally {
115- this . _sessions . delete ( session . id ) ;
116- this . _removeChildren ( session . id ) ;
117- this . _listeners . delete ( session . id ) ;
118- }
124+ try { await this . _detachSession ( session . id ) ; }
125+ finally { this . _removeSession ( session . id ) ; }
119126 }
120127
121128 async close ( ) {
122129 this . _sessions . clear ( ) ;
123130 this . _children . clear ( ) ;
124131 this . _listeners . clear ( ) ;
125- this . _unsubscribe ( ) ;
132+ this . _unsubscribe ?. ( ) ;
133+ this . _unsubscribe = null ;
126134 this . _connection . close ( ) ;
127135 }
128136
137+ _releaseSubscription ( ) {
138+ if ( this . _sessions . size || this . _pendingAttaches ) return ;
139+ this . _unsubscribe ?. ( ) ;
140+ this . _unsubscribe = null ;
141+ }
142+
143+ _attachTarget ( targetId ) {
144+ return this . _connection . send ( "Target.attachToTarget" , { targetId, flatten : true } ) ;
145+ }
146+
147+ _detachSession ( sessionId ) {
148+ return this . _connection . send ( "Target.detachFromTarget" , { sessionId } ) ;
149+ }
150+
151+ _removeSession ( sessionId ) {
152+ this . _sessions . delete ( sessionId ) ;
153+ this . _removeChildren ( sessionId ) ;
154+ this . _listeners . delete ( sessionId ) ;
155+ this . _releaseSubscription ( ) ;
156+ }
157+
129158 _autoAttach ( sessionId ) {
130159 return this . _connection . send ( "Target.setAutoAttach" , {
131160 autoAttach : true , waitForDebuggerOnStart : false , flatten : true ,
@@ -179,8 +208,10 @@ export class NodeCdpTransport {
179208 if ( this . _sessions . has ( event . params ?. sessionId ) ) {
180209 this . _sessions . delete ( event . params . sessionId ) ;
181210 this . _removeChildren ( event . params . sessionId ) ;
182- this . _emit ( event . params . sessionId , { method : "Transport.detached" ,
183- params : { reason : event . params . reason || "target_closed" } } ) ;
211+ try {
212+ this . _emit ( event . params . sessionId , { method : "Transport.detached" ,
213+ params : { reason : event . params . reason || "target_closed" } } ) ;
214+ } finally { this . _removeSession ( event . params . sessionId ) ; }
184215 }
185216 return ;
186217 }
0 commit comments