| 156 | } |
| 157 | |
| 158 | async _doAttach(): Promise<Cdp.Api | undefined> { |
| 159 | this._waitingForDebugger = false; |
| 160 | this._attached = true; |
| 161 | const result = await this._cdp.Target.attachToTarget({ targetId: this._targetId }); |
| 162 | if (!result) { |
| 163 | this.logger.info(LogTag.RuntimeLaunch, 'Failed to attach to target', { |
| 164 | targetId: this._targetId, |
| 165 | }); |
| 166 | return; // timed out or cancelled, may have been a short-lived process |
| 167 | } |
| 168 | |
| 169 | if (result && '__dynamicAttach' in result) { |
| 170 | await this._cdp.Debugger.enable({}); |
| 171 | await this._cdp.Runtime.enable({}); |
| 172 | } |
| 173 | |
| 174 | let defaultCountextId: number; |
| 175 | this._cdp.Runtime.on('executionContextCreated', event => { |
| 176 | if (event.context.auxData && event.context.auxData['isDefault']) |
| 177 | defaultCountextId = event.context.id; |
| 178 | }); |
| 179 | this._cdp.Runtime.on('executionContextDestroyed', event => { |
| 180 | if (event.executionContextId === defaultCountextId) this.connection.close(); |
| 181 | }); |
| 182 | return this._cdp; |
| 183 | } |
| 184 | |
| 185 | public async afterBind() { |
| 186 | this._cdp.resume(); |