mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
fix: migrate more files to strict-mode TypeScript (#7950)
This commit is contained in:
parent
f708ceb9f9
commit
aaac8d9c44
@ -43,7 +43,7 @@ export class TimeoutError extends CustomError {}
|
|||||||
*/
|
*/
|
||||||
export class ProtocolError extends CustomError {
|
export class ProtocolError extends CustomError {
|
||||||
public code?: number;
|
public code?: number;
|
||||||
public originalMessage: string;
|
public originalMessage = '';
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* @public
|
* @public
|
||||||
|
@ -144,6 +144,6 @@ export class EventEmitter implements CommonEventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private eventListenersCount(event: EventType): number {
|
private eventListenersCount(event: EventType): number {
|
||||||
return this.eventsMap.has(event) ? this.eventsMap.get(event).length : 0;
|
return this.eventsMap.get(event)?.length || 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -134,7 +134,9 @@ async function waitForEvent<T extends any>(
|
|||||||
timeout: number,
|
timeout: number,
|
||||||
abortPromise: Promise<Error>
|
abortPromise: Promise<Error>
|
||||||
): Promise<T> {
|
): Promise<T> {
|
||||||
let eventTimeout, resolveCallback, rejectCallback;
|
let eventTimeout: NodeJS.Timeout;
|
||||||
|
let resolveCallback: (value: T | PromiseLike<T>) => void;
|
||||||
|
let rejectCallback: (value: Error) => void;
|
||||||
const promise = new Promise<T>((resolve, reject) => {
|
const promise = new Promise<T>((resolve, reject) => {
|
||||||
resolveCallback = resolve;
|
resolveCallback = resolve;
|
||||||
rejectCallback = reject;
|
rejectCallback = reject;
|
||||||
@ -192,7 +194,7 @@ function pageBindingInitString(type: string, name: string): string {
|
|||||||
const binding = win[bindingName];
|
const binding = win[bindingName];
|
||||||
|
|
||||||
win[bindingName] = (...args: unknown[]): Promise<unknown> => {
|
win[bindingName] = (...args: unknown[]): Promise<unknown> => {
|
||||||
const me = window[bindingName];
|
const me = (window as any)[bindingName];
|
||||||
let callbacks = me.callbacks;
|
let callbacks = me.callbacks;
|
||||||
if (!callbacks) {
|
if (!callbacks) {
|
||||||
callbacks = new Map();
|
callbacks = new Map();
|
||||||
@ -216,8 +218,8 @@ function pageBindingDeliverResultString(
|
|||||||
result: unknown
|
result: unknown
|
||||||
): string {
|
): string {
|
||||||
function deliverResult(name: string, seq: number, result: unknown): void {
|
function deliverResult(name: string, seq: number, result: unknown): void {
|
||||||
window[name].callbacks.get(seq).resolve(result);
|
(window as any)[name].callbacks.get(seq).resolve(result);
|
||||||
window[name].callbacks.delete(seq);
|
(window as any)[name].callbacks.delete(seq);
|
||||||
}
|
}
|
||||||
return evaluationString(deliverResult, name, seq, result);
|
return evaluationString(deliverResult, name, seq, result);
|
||||||
}
|
}
|
||||||
@ -236,8 +238,8 @@ function pageBindingDeliverErrorString(
|
|||||||
): void {
|
): void {
|
||||||
const error = new Error(message);
|
const error = new Error(message);
|
||||||
error.stack = stack;
|
error.stack = stack;
|
||||||
window[name].callbacks.get(seq).reject(error);
|
(window as any)[name].callbacks.get(seq).reject(error);
|
||||||
window[name].callbacks.delete(seq);
|
(window as any)[name].callbacks.delete(seq);
|
||||||
}
|
}
|
||||||
return evaluationString(deliverError, name, seq, message, stack);
|
return evaluationString(deliverError, name, seq, message, stack);
|
||||||
}
|
}
|
||||||
@ -248,8 +250,8 @@ function pageBindingDeliverErrorValueString(
|
|||||||
value: unknown
|
value: unknown
|
||||||
): string {
|
): string {
|
||||||
function deliverErrorValue(name: string, seq: number, value: unknown): void {
|
function deliverErrorValue(name: string, seq: number, value: unknown): void {
|
||||||
window[name].callbacks.get(seq).reject(value);
|
(window as any)[name].callbacks.get(seq).reject(value);
|
||||||
window[name].callbacks.delete(seq);
|
(window as any)[name].callbacks.delete(seq);
|
||||||
}
|
}
|
||||||
return evaluationString(deliverErrorValue, name, seq, value);
|
return evaluationString(deliverErrorValue, name, seq, value);
|
||||||
}
|
}
|
||||||
@ -266,7 +268,9 @@ function makePredicateString(
|
|||||||
if (!node) return waitForHidden;
|
if (!node) return waitForHidden;
|
||||||
if (!waitForVisible && !waitForHidden) return node;
|
if (!waitForVisible && !waitForHidden) return node;
|
||||||
const element =
|
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 style = window.getComputedStyle(element);
|
||||||
const isVisible =
|
const isVisible =
|
||||||
@ -296,7 +300,7 @@ async function waitWithTimeout<T extends any>(
|
|||||||
taskName: string,
|
taskName: string,
|
||||||
timeout: number
|
timeout: number
|
||||||
): Promise<T> {
|
): Promise<T> {
|
||||||
let reject;
|
let reject: (reason?: Error) => void;
|
||||||
const timeoutError = new TimeoutError(
|
const timeoutError = new TimeoutError(
|
||||||
`waiting for ${taskName} failed: timeout ${timeout}ms exceeded`
|
`waiting for ${taskName} failed: timeout ${timeout}ms exceeded`
|
||||||
);
|
);
|
||||||
@ -313,14 +317,14 @@ async function waitWithTimeout<T extends any>(
|
|||||||
async function getReadableAsBuffer(
|
async function getReadableAsBuffer(
|
||||||
readable: Readable,
|
readable: Readable,
|
||||||
path?: string
|
path?: string
|
||||||
): Promise<Buffer> {
|
): Promise<Buffer | null> {
|
||||||
if (!isNode && path) {
|
if (!isNode && path) {
|
||||||
throw new Error('Cannot write to a path outside of Node.js environment.');
|
throw new Error('Cannot write to a path outside of Node.js environment.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const fs = isNode ? await importFSModule() : null;
|
const fs = isNode ? await importFSModule() : null;
|
||||||
|
|
||||||
let fileHandle: import('fs').promises.FileHandle;
|
let fileHandle: import('fs').promises.FileHandle | undefined;
|
||||||
|
|
||||||
if (path && fs) {
|
if (path && fs) {
|
||||||
fileHandle = await fs.promises.open(path, 'w');
|
fileHandle = await fs.promises.open(path, 'w');
|
||||||
@ -328,12 +332,12 @@ async function getReadableAsBuffer(
|
|||||||
const buffers = [];
|
const buffers = [];
|
||||||
for await (const chunk of readable) {
|
for await (const chunk of readable) {
|
||||||
buffers.push(chunk);
|
buffers.push(chunk);
|
||||||
if (fileHandle) {
|
if (fileHandle && fs) {
|
||||||
await fs.promises.writeFile(fileHandle, chunk);
|
await fs.promises.writeFile(fileHandle, chunk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path) await fileHandle.close();
|
if (path && fileHandle) await fileHandle.close();
|
||||||
let resultBuffer = null;
|
let resultBuffer = null;
|
||||||
try {
|
try {
|
||||||
resultBuffer = Buffer.concat(buffers);
|
resultBuffer = Buffer.concat(buffers);
|
||||||
|
Loading…
Reference in New Issue
Block a user