appium-remote-debugger
Version:
Appium proxy for Remote Debugger protocol
253 lines • 10.4 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RemoteDebugger = exports.REMOTE_DEBUGGER_PORT = void 0;
const node_events_1 = require("node:events");
const logger_1 = require("./logger");
const rpc_1 = require("./rpc");
const utils_1 = require("./utils");
const connectMixins = __importStar(require("./mixins/connect"));
const executeMixins = __importStar(require("./mixins/execute"));
const messageHandlerMixins = __importStar(require("./mixins/message-handlers"));
const navigationMixins = __importStar(require("./mixins/navigate"));
const cookieMixins = __importStar(require("./mixins/cookies"));
const screenshotMixins = __importStar(require("./mixins/screenshot"));
const eventMixins = __importStar(require("./mixins/events"));
const miscellaneousMixins = __importStar(require("./mixins/misc"));
const lodash_1 = __importDefault(require("lodash"));
exports.REMOTE_DEBUGGER_PORT = 27753;
const PAGE_READY_TIMEOUT_MS = 5000;
const { version: MODULE_VERSION } = (0, utils_1.getModuleProperties)();
class RemoteDebugger extends node_events_1.EventEmitter {
_skippedApps;
_clientEventListeners;
_appDict;
_appIdKey;
_pageIdKey;
_connectedDrivers;
_currentState;
_pageLoadDelay;
_rpcClient;
_pageLoading;
_navigatingToPage;
_allowNavigationWithoutReload;
_pageLoadMs;
_pageLoadStrategy;
_log;
_bundleId;
_additionalBundleIds;
_ignoredBundleIds;
_platformVersion;
_isSafari;
_includeSafari;
_garbageCollectOnExecute;
_host;
_port;
_socketPath;
_remoteDebugProxy;
_pageReadyTimeout;
_logAllCommunication;
_logAllCommunicationHexDump;
_socketChunkSize;
_webInspectorMaxFrameLength;
_fullPageInitialization;
// events
static EVENT_PAGE_CHANGE;
static EVENT_DISCONNECT;
static EVENT_FRAMES_DETACHED;
// methods
setConnectionKey = connectMixins.setConnectionKey;
disconnect = connectMixins.disconnect;
checkPageIsReady = navigationMixins.checkPageIsReady;
cancelPageLoad = navigationMixins.cancelPageLoad;
waitForDom = navigationMixins.waitForDom;
execute = executeMixins.execute;
executeAtom = executeMixins.executeAtom;
executeAtomAsync = executeMixins.executeAtomAsync;
isPageLoadingCompleted = navigationMixins.isPageLoadingCompleted;
selectApp = connectMixins.selectApp;
connect = connectMixins.connect;
selectPage = connectMixins.selectPage;
navToUrl = navigationMixins.navToUrl;
getCookies = cookieMixins.getCookies;
setCookie = cookieMixins.setCookie;
deleteCookie = cookieMixins.deleteCookie;
captureScreenshot = screenshotMixins.captureScreenshot;
addClientEventListener = eventMixins.addClientEventListener;
removeClientEventListener = eventMixins.removeClientEventListener;
startConsole = eventMixins.startConsole;
stopConsole = eventMixins.stopConsole;
startNetwork = eventMixins.startNetwork;
stopNetwork = eventMixins.stopNetwork;
launchSafari = miscellaneousMixins.launchSafari;
startTimeline = miscellaneousMixins.startTimeline;
stopTimeline = miscellaneousMixins.stopTimeline;
overrideUserAgent = miscellaneousMixins.overrideUserAgent;
garbageCollect = miscellaneousMixins.garbageCollect;
isJavascriptExecutionBlocked = miscellaneousMixins.isJavascriptExecutionBlocked;
// Callbacks
onPageChange = messageHandlerMixins.onPageChange;
onConnectedApplicationList = messageHandlerMixins.onConnectedApplicationList;
onAppConnect = messageHandlerMixins.onAppConnect;
onAppDisconnect = messageHandlerMixins.onAppDisconnect;
onAppUpdate = messageHandlerMixins.onAppUpdate;
onConnectedDriverList = messageHandlerMixins.onConnectedDriverList;
onCurrentState = messageHandlerMixins.onCurrentState;
frameDetached = navigationMixins.frameDetached;
constructor(opts = {}) {
super();
this._log = opts.log ?? logger_1.log;
this.log.info(`Remote Debugger version ${MODULE_VERSION}`);
const { bundleId, additionalBundleIds = [], ignoredBundleIds = [], platformVersion, isSafari = true, includeSafari = false, pageLoadMs, host, port = exports.REMOTE_DEBUGGER_PORT, socketPath, pageReadyTimeout = PAGE_READY_TIMEOUT_MS, remoteDebugProxy, garbageCollectOnExecute = false, logFullResponse = false, logAllCommunication = false, logAllCommunicationHexDump = false, webInspectorMaxFrameLength, socketChunkSize, fullPageInitialization, pageLoadStrategy, } = opts;
this._bundleId = bundleId;
this._additionalBundleIds = additionalBundleIds;
this._ignoredBundleIds = ignoredBundleIds;
this._platformVersion = platformVersion;
this._isSafari = isSafari;
this._includeSafari = includeSafari;
this._pageLoadMs = pageLoadMs;
this._allowNavigationWithoutReload = false;
this._garbageCollectOnExecute = garbageCollectOnExecute;
this._host = host;
this._port = port;
this._socketPath = socketPath;
this._remoteDebugProxy = remoteDebugProxy;
this._pageReadyTimeout = pageReadyTimeout;
this._logAllCommunication = lodash_1.default.isNil(logAllCommunication)
? !!logFullResponse
: !!logAllCommunication;
this._logAllCommunicationHexDump = logAllCommunicationHexDump;
this._socketChunkSize = socketChunkSize;
if (lodash_1.default.isInteger(webInspectorMaxFrameLength)) {
this._webInspectorMaxFrameLength = webInspectorMaxFrameLength;
}
this._fullPageInitialization = fullPageInitialization;
this._pageLoadStrategy = pageLoadStrategy;
this._skippedApps = [];
this.setup();
}
get log() {
return this._log;
}
requireRpcClient(checkConnected = false) {
if (!this._rpcClient) {
throw new Error(`rpcClient is undefined. Has 'initRpcClient' been called before?`);
}
if (checkConnected && !this._rpcClient.isConnected) {
throw new Error('Remote debugger is not connected');
}
return this._rpcClient;
}
setup() {
// app handling configuration
this._appDict = {};
this._appIdKey = undefined;
this._pageIdKey = undefined;
this._pageLoading = false;
this._navigatingToPage = false;
this._currentState = undefined;
this._connectedDrivers = undefined;
this._pageLoadDelay = undefined;
this._rpcClient = null;
this._clientEventListeners = {};
}
teardown() {
this.log.debug('Cleaning up listeners');
this._appDict = {};
this._appIdKey = undefined;
this._pageIdKey = undefined;
this._pageLoading = false;
this._rpcClient = null;
for (const evt of [
RemoteDebugger.EVENT_DISCONNECT,
RemoteDebugger.EVENT_PAGE_CHANGE,
RemoteDebugger.EVENT_FRAMES_DETACHED,
]) {
this.removeAllListeners(evt);
}
}
async initRpcClient() {
this._rpcClient = new rpc_1.RpcClientSimulator({
bundleId: this._bundleId,
platformVersion: this._platformVersion,
isSafari: this._isSafari,
host: this._host,
port: this._port,
socketPath: this._socketPath,
messageProxy: this._remoteDebugProxy,
logAllCommunication: this._logAllCommunication,
logAllCommunicationHexDump: this._logAllCommunicationHexDump,
fullPageInitialization: this._fullPageInitialization,
webInspectorMaxFrameLength: this._webInspectorMaxFrameLength,
pageLoadTimeoutMs: this._pageLoadMs,
});
}
get isConnected() {
return !!this._rpcClient?.isConnected;
}
// Only use this getter to read the appDict value.
// Any changes to it don't mutate the original property
// because the getter always returns the copy of it
get appDict() {
return lodash_1.default.cloneDeep(this._appDict);
}
set allowNavigationWithoutReload(allow) {
this._allowNavigationWithoutReload = allow;
}
get allowNavigationWithoutReload() {
return !!this._allowNavigationWithoutReload;
}
get currentState() {
return this._currentState;
}
get connectedDrivers() {
return this._connectedDrivers;
}
get pageLoadMs() {
return this._pageLoadMs ?? navigationMixins.DEFAULT_PAGE_READINESS_TIMEOUT_MS;
}
set pageLoadMs(value) {
this._pageLoadMs = value;
}
}
exports.RemoteDebugger = RemoteDebugger;
for (const [name, event] of lodash_1.default.toPairs(eventMixins.events)) {
RemoteDebugger[name] = event;
}
exports.default = RemoteDebugger;
//# sourceMappingURL=remote-debugger.js.map