chore(extension): handle root session id in the relay (#737)

This commit is contained in:
Yury Semikhatsky 2025-07-22 13:49:39 -07:00 committed by GitHub
parent 70862ce456
commit c2b98dc70b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 13 deletions

View File

@ -38,7 +38,6 @@ type ProtocolResponse = {
export class RelayConnection { export class RelayConnection {
private _debuggee: chrome.debugger.Debuggee = {}; private _debuggee: chrome.debugger.Debuggee = {};
private _rootSessionId = '';
private _ws: WebSocket; private _ws: WebSocket;
private _eventListener: (source: chrome.debugger.DebuggerSession, method: string, params: any) => void; private _eventListener: (source: chrome.debugger.DebuggerSession, method: string, params: any) => void;
private _detachListener: (source: chrome.debugger.Debuggee, reason: string) => void; private _detachListener: (source: chrome.debugger.Debuggee, reason: string) => void;
@ -56,11 +55,9 @@ export class RelayConnection {
setConnectedTabId(tabId: number | null): void { setConnectedTabId(tabId: number | null): void {
if (!tabId) { if (!tabId) {
this._debuggee = { }; this._debuggee = { };
this._rootSessionId = '';
return; return;
} }
this._debuggee = { tabId }; this._debuggee = { tabId };
this._rootSessionId = `pw-tab-${tabId}`;
} }
close(message?: string): void { close(message?: string): void {
@ -77,7 +74,7 @@ export class RelayConnection {
if (source.tabId !== this._debuggee.tabId) if (source.tabId !== this._debuggee.tabId)
return; return;
debugLog('Forwarding CDP event:', method, params); debugLog('Forwarding CDP event:', method, params);
const sessionId = source.sessionId || this._rootSessionId; const sessionId = source.sessionId;
this._sendMessage({ this._sendMessage({
method: 'forwardCDPEvent', method: 'forwardCDPEvent',
params: { params: {
@ -137,7 +134,6 @@ export class RelayConnection {
await chrome.debugger.attach(this._debuggee, '1.3'); await chrome.debugger.attach(this._debuggee, '1.3');
const result: any = await chrome.debugger.sendCommand(this._debuggee, 'Target.getTargetInfo'); const result: any = await chrome.debugger.sendCommand(this._debuggee, 'Target.getTargetInfo');
return { return {
sessionId: this._rootSessionId,
targetInfo: result?.targetInfo, targetInfo: result?.targetInfo,
}; };
} }
@ -148,10 +144,10 @@ export class RelayConnection {
if (message.method === 'forwardCDPCommand') { if (message.method === 'forwardCDPCommand') {
const { sessionId, method, params } = message.params; const { sessionId, method, params } = message.params;
debugLog('CDP command:', method, params); debugLog('CDP command:', method, params);
const debuggerSession: chrome.debugger.DebuggerSession = { ...this._debuggee }; const debuggerSession: chrome.debugger.DebuggerSession = {
// Pass session id, unless it's the root session. ...this._debuggee,
if (sessionId && sessionId !== this._rootSessionId) sessionId,
debuggerSession.sessionId = sessionId; };
// Forward CDP command to chrome.debugger // Forward CDP command to chrome.debugger
return await chrome.debugger.sendCommand( return await chrome.debugger.sendCommand(
debuggerSession, debuggerSession,

View File

@ -65,6 +65,7 @@ export class CDPRelayServer {
// Page sessionId that should be used by this connection. // Page sessionId that should be used by this connection.
sessionId: string; sessionId: string;
} | undefined; } | undefined;
private _nextSessionId: number = 1;
private _extensionConnectionPromise: Promise<void>; private _extensionConnectionPromise: Promise<void>;
private _extensionConnectionResolve: (() => void) | null = null; private _extensionConnectionResolve: (() => void) | null = null;
@ -190,8 +191,9 @@ export class CDPRelayServer {
private _handleExtensionMessage(method: string, params: any) { private _handleExtensionMessage(method: string, params: any) {
switch (method) { switch (method) {
case 'forwardCDPEvent': case 'forwardCDPEvent':
const sessionId = params.sessionId || this._connectedTabInfo?.sessionId;
this._sendToPlaywright({ this._sendToPlaywright({
sessionId: params.sessionId, sessionId,
method: params.method, method: params.method,
params: params.params params: params.params
}); });
@ -236,14 +238,18 @@ export class CDPRelayServer {
if (sessionId) if (sessionId)
break; break;
// Simulate auto-attach behavior with real target info // 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'); debugLogger('Simulating auto-attach');
this._sendToPlaywright({ this._sendToPlaywright({
method: 'Target.attachedToTarget', method: 'Target.attachedToTarget',
params: { params: {
sessionId: this._connectedTabInfo!.sessionId, sessionId: this._connectedTabInfo.sessionId,
targetInfo: { targetInfo: {
...this._connectedTabInfo!.targetInfo, ...this._connectedTabInfo.targetInfo,
attached: true, attached: true,
}, },
waitingForDebugger: false waitingForDebugger: false
@ -261,6 +267,9 @@ export class CDPRelayServer {
private async _forwardToExtension(method: string, params: any, sessionId: string | undefined): Promise<any> { private async _forwardToExtension(method: string, params: any, sessionId: string | undefined): Promise<any> {
if (!this._extensionConnection) if (!this._extensionConnection)
throw new Error('Extension not connected'); 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 }); return await this._extensionConnection.send('forwardCDPCommand', { sessionId, method, params });
} }