* This is the main command handler for the driver. It wraps command * execution with timeout logic, checking that we have a valid session, * and ensuring that we execute commands one at a time. This method is called * by the WebDriver protocol's express router.
(cmd: string, ...args: any[])
| 101 | * by the WebDriver protocol's express router. |
| 102 | */ |
| 103 | async executeCommand<T = unknown>(cmd: string, ...args: any[]): Promise<T> { |
| 104 | // get start time for this command, and log in special cases |
| 105 | const startTime = Date.now(); |
| 106 | |
| 107 | if (cmd === 'createSession') { |
| 108 | this.logEvent(EVENT_SESSION_INIT); |
| 109 | } else if (cmd === DELETE_SESSION_COMMAND) { |
| 110 | this.logEvent(EVENT_SESSION_QUIT_START); |
| 111 | } |
| 112 | |
| 113 | // if we had a command timer running, clear it now that we're starting |
| 114 | // a new command and so don't want to time out |
| 115 | await this.clearNewCommandTimeout(); |
| 116 | |
| 117 | if (this.shutdownUnexpectedly) { |
| 118 | throw new errors.NoSuchDriverError('The driver was unexpectedly shut down!'); |
| 119 | } |
| 120 | |
| 121 | const invoker = this as unknown as CommandInvoker<C>; |
| 122 | const command = invoker[cmd]; |
| 123 | // If we don't have this command, it must not be implemented |
| 124 | if (!command) { |
| 125 | if (this.isCommandsQueueEnabled && !hasRunningCommands(this)) { |
| 126 | await this.startNewCommandTimeout(); |
| 127 | } |
| 128 | throw new errors.NotYetImplementedError(); |
| 129 | } |
| 130 | |
| 131 | const runCommandPromise = async () => { |
| 132 | let wasSessionShutdownUnexpectedly = false; |
| 133 | return await runWithCommandTimeout( |
| 134 | this, |
| 135 | async () => { |
| 136 | const shutdown = Promise.withResolvers<void>(); |
| 137 | const onUnexpectedShutdown = (e: Error) => { |
| 138 | wasSessionShutdownUnexpectedly = true; |
| 139 | shutdown.reject(e); |
| 140 | }; |
| 141 | try { |
| 142 | this.eventEmitter.once(ON_UNEXPECTED_SHUTDOWN_EVENT, onUnexpectedShutdown); |
| 143 | return await Promise.race([ |
| 144 | shutdown.promise, |
| 145 | // Observe both promises before the command can emit shutdown or throw synchronously. |
| 146 | Promise.resolve().then(() => command.call(this, ...args)), |
| 147 | ]); |
| 148 | } finally { |
| 149 | this.eventEmitter.removeListener(ON_UNEXPECTED_SHUTDOWN_EVENT, onUnexpectedShutdown); |
| 150 | shutdown.resolve(); |
| 151 | } |
| 152 | }, |
| 153 | {restart: () => cmd !== DELETE_SESSION_COMMAND && !wasSessionShutdownUnexpectedly}, |
| 154 | ); |
| 155 | }; |
| 156 | |
| 157 | // exempt commands bypass the queue, so they can run while other commands are queued/running, |
| 158 | // without needing to wait their turn behind them |
| 159 | const isQueueExempt = this.queueExemptCommands.has(cmd); |
| 160 |
nothing calls this directly
no test coverage detected