chore(base-driver): Cleanup unnecessary command definitions (#20939)

This commit is contained in:
Mykola Mokhnach
2025-01-27 11:05:07 +01:00
committed by GitHub
parent a6b6077ecd
commit 841a835153
4 changed files with 42 additions and 77 deletions

View File

@@ -24,7 +24,7 @@ import {
import {util} from '@appium/support';
import {getDefaultsForExtension} from './schema';
import {DRIVER_TYPE, BIDI_BASE_PATH} from './constants';
import * as bidiHelpers from './bidi';
import * as bidiCommands from './bidi-commands';
import * as inspectorCommands from './inspector-commands';
const desiredCapabilityConstraints = /** @type {const} */ ({
@@ -429,7 +429,7 @@ class AppiumDriver extends DriverCore {
if (dCaps.webSocketUrl) {
const {address, port, basePath} = this.args;
const scheme = `ws${this.server.isSecure() ? 's' : ''}`;
const host = bidiHelpers.determineBiDiHost(address);
const host = bidiCommands.determineBiDiHost(address);
const bidiUrl = `${scheme}://${host}:${port}${basePath}${BIDI_BASE_PATH}/${innerSessionId}`;
this.log.info(
`Upstream driver responded with webSocketUrl ${dCaps.webSocketUrl}, will rewrite to ` +
@@ -568,32 +568,6 @@ class AppiumDriver extends DriverCore {
}
}
/**
* @param {string} sessionId
*/
cleanupBidiSockets(sessionId) {
// clean up any bidi sockets associated with session
if (this.bidiSockets[sessionId]) {
try {
this.log.debug(`Closing bidi socket(s) associated with session ${sessionId}`);
for (const ws of this.bidiSockets[sessionId]) {
// 1001 means server is going away
ws.close(1001, 'Appium session is closing');
}
} catch {}
delete this.bidiSockets[sessionId];
const proxyClient = this.bidiProxyClients[sessionId];
if (proxyClient) {
this.log.debug(`Also closing proxy connection to upstream bidi server`);
try {
// 1000 means normal closure, which seems correct when Appium is acting as the client
proxyClient.close(1000);
} catch {}
delete this.bidiProxyClients[sessionId];
}
}
}
async deleteAllSessions(opts = {}) {
const sessionsCount = _.size(this.sessions);
if (0 === sessionsCount) {
@@ -938,9 +912,11 @@ class AppiumDriver extends DriverCore {
return dstSession && dstSession.canProxy(sessionId);
}
onBidiConnection = bidiHelpers.onBidiConnection;
onBidiMessage = bidiHelpers.onBidiMessage;
onBidiServerError = bidiHelpers.onBidiServerError;
onBidiConnection = bidiCommands.onBidiConnection;
onBidiMessage = bidiCommands.onBidiMessage;
onBidiServerError = bidiCommands.onBidiServerError;
cleanupBidiSockets = bidiCommands.cleanupBidiSockets;
listCommands = inspectorCommands.listCommands;
listExtensions = inspectorCommands.listExtensions;
}

View File

@@ -176,6 +176,38 @@ export function onBidiServerError(this: AppiumDriver, err: Error): void {
this.log.warn(`Error from bidi websocket server: ${err}`);
}
/**
* Clean up any bidi sockets associated with session
*
* @param sessionId
*/
export function cleanupBidiSockets(this: AppiumDriver, sessionId: string): void {
if (!this.bidiSockets[sessionId]) {
return;
}
try {
this.log.debug(`Closing bidi socket(s) associated with session ${sessionId}`);
for (const ws of this.bidiSockets[sessionId]) {
// 1001 means server is going away
ws.close(1001, 'Appium session is closing');
}
} catch {}
delete this.bidiSockets[sessionId];
const proxyClient = this.bidiProxyClients[sessionId];
if (!proxyClient) {
return;
}
this.log.debug(`Also closing proxy connection to upstream bidi server`);
try {
// 1000 means normal closure, which seems correct when Appium is acting as the client
proxyClient.close(1000);
} catch {}
delete this.bidiProxyClients[sessionId];
}
// #region Private functions
/**
* Initialize a new bidi connection
* @param ws The websocket connection object
@@ -468,3 +500,5 @@ function initBidiEventListeners(
plugin.eventEmitter?.on(BIDI_EVENT_NAME, eventListenerFactory('plugin', plugin));
}
}
// #endregion

View File

@@ -1,39 +0,0 @@
import type {
Constraints,
IInspectorCommands,
ListCommandsResponse,
ListExtensionsResponse,
} from '@appium/types';
import {mixin} from './mixin';
declare module '../driver' {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface BaseDriver<C extends Constraints> extends IInspectorCommands {}
}
const InspectorCommands: IInspectorCommands = {
/**
* This command is supposed to be handled by the umbrella driver.
*
* @param sessionId
* @returns
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async listCommands(sessionId?: string | null): Promise<ListCommandsResponse> {
return {};
},
/**
* This command is supposed to be handled by the umbrella driver.
*
* @param sessionId
* @returns
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async listExtensions(sessionId?: string | null): Promise<ListExtensionsResponse> {
return {};
},
};
mixin(InspectorCommands);

View File

@@ -7,8 +7,6 @@ import type {
BiDiResultData,
ExecuteMethodMap,
MethodMap,
ListCommandsResponse,
ListExtensionsResponse,
} from './command';
import type {ServerArgs} from './config';
import type {HTTPHeaders, HTTPMethod} from './http';
@@ -24,6 +22,7 @@ export interface IDeviceSettings<T extends StringRecord> {
update(newSettings: T): Promise<void>;
getSettings(): T;
}
export interface ITimeoutCommands {
/**
* Set the various timeouts associated with a session
@@ -360,11 +359,6 @@ export interface IBidiCommands {
bidiStatus(): Promise<DriverStatus>;
}
export interface IInspectorCommands {
listCommands(): Promise<ListCommandsResponse>;
listExtensions(): Promise<ListExtensionsResponse>;
}
/**
* A record of {@linkcode LogDef} objects, keyed by the log type name.
* Used in {@linkcode ILogCommands.supportedLogTypes}