From c2b98dc70bac3b1dea41cd6e38f623b3ce2d5c2e Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Tue, 22 Jul 2025 13:49:39 -0700 Subject: [PATCH] chore(extension): handle root session id in the relay (#737) --- extension/src/relayConnection.ts | 14 +++++--------- src/extension/cdpRelay.ts | 17 +++++++++++++---- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/extension/src/relayConnection.ts b/extension/src/relayConnection.ts index 75b2881..0a9b964 100644 --- a/extension/src/relayConnection.ts +++ b/extension/src/relayConnection.ts @@ -38,7 +38,6 @@ type ProtocolResponse = { export class RelayConnection { private _debuggee: chrome.debugger.Debuggee = {}; - private _rootSessionId = ''; private _ws: WebSocket; private _eventListener: (source: chrome.debugger.DebuggerSession, method: string, params: any) => void; private _detachListener: (source: chrome.debugger.Debuggee, reason: string) => void; @@ -56,11 +55,9 @@ export class RelayConnection { setConnectedTabId(tabId: number | null): void { if (!tabId) { this._debuggee = { }; - this._rootSessionId = ''; return; } this._debuggee = { tabId }; - this._rootSessionId = `pw-tab-${tabId}`; } close(message?: string): void { @@ -77,7 +74,7 @@ export class RelayConnection { if (source.tabId !== this._debuggee.tabId) return; debugLog('Forwarding CDP event:', method, params); - const sessionId = source.sessionId || this._rootSessionId; + const sessionId = source.sessionId; this._sendMessage({ method: 'forwardCDPEvent', params: { @@ -137,7 +134,6 @@ export class RelayConnection { await chrome.debugger.attach(this._debuggee, '1.3'); const result: any = await chrome.debugger.sendCommand(this._debuggee, 'Target.getTargetInfo'); return { - sessionId: this._rootSessionId, targetInfo: result?.targetInfo, }; } @@ -148,10 +144,10 @@ export class RelayConnection { if (message.method === 'forwardCDPCommand') { const { sessionId, method, params } = message.params; debugLog('CDP command:', method, params); - const debuggerSession: chrome.debugger.DebuggerSession = { ...this._debuggee }; - // Pass session id, unless it's the root session. - if (sessionId && sessionId !== this._rootSessionId) - debuggerSession.sessionId = sessionId; + const debuggerSession: chrome.debugger.DebuggerSession = { + ...this._debuggee, + sessionId, + }; // Forward CDP command to chrome.debugger return await chrome.debugger.sendCommand( debuggerSession, diff --git a/src/extension/cdpRelay.ts b/src/extension/cdpRelay.ts index b39746f..1cc8ec3 100644 --- a/src/extension/cdpRelay.ts +++ b/src/extension/cdpRelay.ts @@ -65,6 +65,7 @@ export class CDPRelayServer { // Page sessionId that should be used by this connection. sessionId: string; } | undefined; + private _nextSessionId: number = 1; private _extensionConnectionPromise: Promise; private _extensionConnectionResolve: (() => void) | null = null; @@ -190,8 +191,9 @@ export class CDPRelayServer { private _handleExtensionMessage(method: string, params: any) { switch (method) { case 'forwardCDPEvent': + const sessionId = params.sessionId || this._connectedTabInfo?.sessionId; this._sendToPlaywright({ - sessionId: params.sessionId, + sessionId, method: params.method, params: params.params }); @@ -236,14 +238,18 @@ export class CDPRelayServer { if (sessionId) break; // Simulate auto-attach behavior with real target info - this._connectedTabInfo = await this._extensionConnection!.send('attachToTab'); + const { targetInfo } = await this._extensionConnection!.send('attachToTab'); + this._connectedTabInfo = { + targetInfo, + sessionId: `pw-tab-${this._nextSessionId++}`, + }; debugLogger('Simulating auto-attach'); this._sendToPlaywright({ method: 'Target.attachedToTarget', params: { - sessionId: this._connectedTabInfo!.sessionId, + sessionId: this._connectedTabInfo.sessionId, targetInfo: { - ...this._connectedTabInfo!.targetInfo, + ...this._connectedTabInfo.targetInfo, attached: true, }, waitingForDebugger: false @@ -261,6 +267,9 @@ export class CDPRelayServer { private async _forwardToExtension(method: string, params: any, sessionId: string | undefined): Promise { if (!this._extensionConnection) throw new Error('Extension not connected'); + // Top level sessionId is only passed between the relay and the client. + if (this._connectedTabInfo?.sessionId === sessionId) + sessionId = undefined; return await this._extensionConnection.send('forwardCDPCommand', { sessionId, method, params }); }