diff --git a/src/common/Errors.ts b/src/common/Errors.ts index 5b9a09a0..2676caa8 100644 --- a/src/common/Errors.ts +++ b/src/common/Errors.ts @@ -43,7 +43,7 @@ export class TimeoutError extends CustomError {} */ export class ProtocolError extends CustomError { public code?: number; - public originalMessage: string; + public originalMessage = ''; } /** * @public diff --git a/src/common/EventEmitter.ts b/src/common/EventEmitter.ts index 91cf7e09..95eb0c10 100644 --- a/src/common/EventEmitter.ts +++ b/src/common/EventEmitter.ts @@ -144,6 +144,6 @@ export class EventEmitter implements CommonEventEmitter { } private eventListenersCount(event: EventType): number { - return this.eventsMap.has(event) ? this.eventsMap.get(event).length : 0; + return this.eventsMap.get(event)?.length || 0; } } diff --git a/src/common/helper.ts b/src/common/helper.ts index ba20970d..aa0dea68 100644 --- a/src/common/helper.ts +++ b/src/common/helper.ts @@ -134,7 +134,9 @@ async function waitForEvent( timeout: number, abortPromise: Promise ): Promise { - let eventTimeout, resolveCallback, rejectCallback; + let eventTimeout: NodeJS.Timeout; + let resolveCallback: (value: T | PromiseLike) => void; + let rejectCallback: (value: Error) => void; const promise = new Promise((resolve, reject) => { resolveCallback = resolve; rejectCallback = reject; @@ -192,7 +194,7 @@ function pageBindingInitString(type: string, name: string): string { const binding = win[bindingName]; win[bindingName] = (...args: unknown[]): Promise => { - const me = window[bindingName]; + const me = (window as any)[bindingName]; let callbacks = me.callbacks; if (!callbacks) { callbacks = new Map(); @@ -216,8 +218,8 @@ function pageBindingDeliverResultString( result: unknown ): string { function deliverResult(name: string, seq: number, result: unknown): void { - window[name].callbacks.get(seq).resolve(result); - window[name].callbacks.delete(seq); + (window as any)[name].callbacks.get(seq).resolve(result); + (window as any)[name].callbacks.delete(seq); } return evaluationString(deliverResult, name, seq, result); } @@ -236,8 +238,8 @@ function pageBindingDeliverErrorString( ): void { const error = new Error(message); error.stack = stack; - window[name].callbacks.get(seq).reject(error); - window[name].callbacks.delete(seq); + (window as any)[name].callbacks.get(seq).reject(error); + (window as any)[name].callbacks.delete(seq); } return evaluationString(deliverError, name, seq, message, stack); } @@ -248,8 +250,8 @@ function pageBindingDeliverErrorValueString( value: unknown ): string { function deliverErrorValue(name: string, seq: number, value: unknown): void { - window[name].callbacks.get(seq).reject(value); - window[name].callbacks.delete(seq); + (window as any)[name].callbacks.get(seq).reject(value); + (window as any)[name].callbacks.delete(seq); } return evaluationString(deliverErrorValue, name, seq, value); } @@ -266,7 +268,9 @@ function makePredicateString( if (!node) return waitForHidden; if (!waitForVisible && !waitForHidden) return node; const element = - node.nodeType === Node.TEXT_NODE ? node.parentElement : (node as Element); + node.nodeType === Node.TEXT_NODE + ? (node.parentElement as Element) + : (node as Element); const style = window.getComputedStyle(element); const isVisible = @@ -296,7 +300,7 @@ async function waitWithTimeout( taskName: string, timeout: number ): Promise { - let reject; + let reject: (reason?: Error) => void; const timeoutError = new TimeoutError( `waiting for ${taskName} failed: timeout ${timeout}ms exceeded` ); @@ -313,14 +317,14 @@ async function waitWithTimeout( async function getReadableAsBuffer( readable: Readable, path?: string -): Promise { +): Promise { if (!isNode && path) { throw new Error('Cannot write to a path outside of Node.js environment.'); } const fs = isNode ? await importFSModule() : null; - let fileHandle: import('fs').promises.FileHandle; + let fileHandle: import('fs').promises.FileHandle | undefined; if (path && fs) { fileHandle = await fs.promises.open(path, 'w'); @@ -328,12 +332,12 @@ async function getReadableAsBuffer( const buffers = []; for await (const chunk of readable) { buffers.push(chunk); - if (fileHandle) { + if (fileHandle && fs) { await fs.promises.writeFile(fileHandle, chunk); } } - if (path) await fileHandle.close(); + if (path && fileHandle) await fileHandle.close(); let resultBuffer = null; try { resultBuffer = Buffer.concat(buffers);