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 {
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,

View File

@ -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<void>;
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<any> {
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 });
}