From bc2a2342d1fa60912a42a37cbb2d8e783f50651d Mon Sep 17 00:00:00 2001 From: Paul Irish Date: Tue, 2 Apr 2024 18:36:59 -0700 Subject: [PATCH 1/4] core(driver): add sendCommandAndIgnore alias for catch(_ => {}) behavior --- core/gather/driver/target-manager.js | 2 +- core/gather/driver/wait-for-condition.js | 5 ++--- core/gather/session.js | 12 ++++++++++++ types/gatherer.d.ts | 1 + 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/core/gather/driver/target-manager.js b/core/gather/driver/target-manager.js index 146810df67d9..6872df807e80 100644 --- a/core/gather/driver/target-manager.js +++ b/core/gather/driver/target-manager.js @@ -180,7 +180,7 @@ class TargetManager extends ProtocolEventEmitter { throw err; } finally { // Resume the target if it was paused, but if it's unnecessary, we don't care about the error. - await newSession.sendCommand('Runtime.runIfWaitingForDebugger').catch(() => {}); + await newSession.sendCommandAndIgnore('Runtime.runIfWaitingForDebugger'); } } diff --git a/core/gather/driver/wait-for-condition.js b/core/gather/driver/wait-for-condition.js index d3f65fc9f183..b110f686c12a 100644 --- a/core/gather/driver/wait-for-condition.js +++ b/core/gather/driver/wait-for-condition.js @@ -498,9 +498,8 @@ async function waitForFullyLoaded(session, networkMonitor, options) { if (await isPageHung(session)) { log.warn('waitFor', 'Page appears to be hung, killing JavaScript...'); // We don't await these, as we want to exit with PAGE_HUNG - void session.sendCommand('Emulation.setScriptExecutionDisabled', {value: true}) - .catch(_ => {}); - void session.sendCommand('Runtime.terminateExecution').catch(_ => {}); + void session.sendCommandAndIgnore('Emulation.setScriptExecutionDisabled', {value: true}); + void session.sendCommandAndIgnore('Runtime.terminateExecution'); throw new LighthouseError(LighthouseError.errors.PAGE_HUNG); } diff --git a/core/gather/session.js b/core/gather/session.js index 46581ae73a69..1126ed25e99f 100644 --- a/core/gather/session.js +++ b/core/gather/session.js @@ -120,6 +120,18 @@ class ProtocolSession extends CrdpEventEmitter { }); } + /** + * Send and if there's an error response, do not reject. + * @template {keyof LH.CrdpCommands} C + * @param {C} method + * @param {LH.CrdpCommands[C]['paramsType']} params + * @return {Promise} + */ + sendCommandAndIgnore(method, ...params) { + return this.sendCommand(method, ...params) + .catch(e => log.verbose('session', method, e.message)); + } + /** * Disposes of a session so that it can no longer talk to Chrome. * @return {Promise} diff --git a/types/gatherer.d.ts b/types/gatherer.d.ts index 8e6bc67db864..a972fd6dfe3e 100644 --- a/types/gatherer.d.ts +++ b/types/gatherer.d.ts @@ -33,6 +33,7 @@ declare module Gatherer { once(event: TEvent, callback: (...args: CrdpEvents[TEvent]) => void): void; off(event: TEvent, callback: (...args: CrdpEvents[TEvent]) => void): void; sendCommand(method: TMethod, ...params: CrdpCommands[TMethod]['paramsType']): Promise; + sendCommandAndIgnore(method: TMethod, ...params: CrdpCommands[TMethod]['paramsType']): Promise; dispose(): Promise; } From 1ec3b7ab79d417b06971e01257456b4e9eb089c3 Mon Sep 17 00:00:00 2001 From: Paul Irish Date: Tue, 2 Apr 2024 18:59:04 -0700 Subject: [PATCH 2/4] driver driver roger --- core/gather/driver.js | 1 + core/test/gather/mock-driver.js | 1 + 2 files changed, 2 insertions(+) diff --git a/core/gather/driver.js b/core/gather/driver.js index 0afef34f8f5f..73a9ba7cf78f 100644 --- a/core/gather/driver.js +++ b/core/gather/driver.js @@ -26,6 +26,7 @@ const throwingSession = { once: throwNotConnectedFn, off: throwNotConnectedFn, sendCommand: throwNotConnectedFn, + sendCommandAndIgnore: throwNotConnectedFn, dispose: throwNotConnectedFn, }; diff --git a/core/test/gather/mock-driver.js b/core/test/gather/mock-driver.js index 4e3618cb2aa6..b67e42c9b79b 100644 --- a/core/test/gather/mock-driver.js +++ b/core/test/gather/mock-driver.js @@ -27,6 +27,7 @@ function createMockSession() { return { setTargetInfo: fnAny(), sendCommand: createMockSendCommandFn(), + sendCommandAndIgnore: createMockSendCommandFn(), setNextProtocolTimeout: fnAny(), hasNextProtocolTimeout: fnAny(), getNextProtocolTimeout: fnAny(), From 5d4c678df5776cf8b048be2e9608cbbdfc04277b Mon Sep 17 00:00:00 2001 From: Paul Irish Date: Tue, 2 Apr 2024 19:38:28 -0700 Subject: [PATCH 3/4] mocksendcommand --- core/test/gather/mock-driver.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/test/gather/mock-driver.js b/core/test/gather/mock-driver.js index b67e42c9b79b..6ef25211ce99 100644 --- a/core/test/gather/mock-driver.js +++ b/core/test/gather/mock-driver.js @@ -24,10 +24,11 @@ import {NetworkMonitor} from '../../gather/driver/network-monitor.js'; /** @typedef {import('../../gather/driver/execution-context.js')} ExecutionContext */ function createMockSession() { + const mockSendCommand = createMockSendCommandFn(); return { setTargetInfo: fnAny(), - sendCommand: createMockSendCommandFn(), - sendCommandAndIgnore: createMockSendCommandFn(), + sendCommand: mockSendCommand, + sendCommandAndIgnore: mockSendCommand, setNextProtocolTimeout: fnAny(), hasNextProtocolTimeout: fnAny(), getNextProtocolTimeout: fnAny(), From aaef7562aa7ecaada0588bdab10584328a6a1f78 Mon Sep 17 00:00:00 2001 From: Paul Irish Date: Wed, 3 Apr 2024 10:40:23 -0700 Subject: [PATCH 4/4] @return {Promise} --- core/gather/session.js | 4 ++-- types/gatherer.d.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core/gather/session.js b/core/gather/session.js index 1126ed25e99f..b01e1d68bd0a 100644 --- a/core/gather/session.js +++ b/core/gather/session.js @@ -125,11 +125,11 @@ class ProtocolSession extends CrdpEventEmitter { * @template {keyof LH.CrdpCommands} C * @param {C} method * @param {LH.CrdpCommands[C]['paramsType']} params - * @return {Promise} + * @return {Promise} */ sendCommandAndIgnore(method, ...params) { return this.sendCommand(method, ...params) - .catch(e => log.verbose('session', method, e.message)); + .catch(e => log.verbose('session', method, e.message)).then(_ => void 0); } /** diff --git a/types/gatherer.d.ts b/types/gatherer.d.ts index a972fd6dfe3e..c5e6796a1aad 100644 --- a/types/gatherer.d.ts +++ b/types/gatherer.d.ts @@ -33,7 +33,7 @@ declare module Gatherer { once(event: TEvent, callback: (...args: CrdpEvents[TEvent]) => void): void; off(event: TEvent, callback: (...args: CrdpEvents[TEvent]) => void): void; sendCommand(method: TMethod, ...params: CrdpCommands[TMethod]['paramsType']): Promise; - sendCommandAndIgnore(method: TMethod, ...params: CrdpCommands[TMethod]['paramsType']): Promise; + sendCommandAndIgnore(method: TMethod, ...params: CrdpCommands[TMethod]['paramsType']): Promise; dispose(): Promise; }