fix: types in Browser.ts to be compatible with strict mode Typescript (#7918)
Issues: #6769
This commit is contained in:
parent
08c0e8b75d
commit
a8ec0aadc9
@ -268,7 +268,7 @@ export class Browser extends EventEmitter {
|
|||||||
this._closeCallback = closeCallback || function (): void {};
|
this._closeCallback = closeCallback || function (): void {};
|
||||||
this._targetFilterCallback = targetFilterCallback || ((): boolean => true);
|
this._targetFilterCallback = targetFilterCallback || ((): boolean => true);
|
||||||
|
|
||||||
this._defaultContext = new BrowserContext(this._connection, this, null);
|
this._defaultContext = new BrowserContext(this._connection, this);
|
||||||
this._contexts = new Map();
|
this._contexts = new Map();
|
||||||
for (const contextId of contextIds)
|
for (const contextId of contextIds)
|
||||||
this._contexts.set(
|
this._contexts.set(
|
||||||
@ -296,7 +296,7 @@ export class Browser extends EventEmitter {
|
|||||||
* {@link Puppeteer.connect}.
|
* {@link Puppeteer.connect}.
|
||||||
*/
|
*/
|
||||||
process(): ChildProcess | null {
|
process(): ChildProcess | null {
|
||||||
return this._process;
|
return this._process ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -357,8 +357,11 @@ export class Browser extends EventEmitter {
|
|||||||
* Used by BrowserContext directly so cannot be marked private.
|
* Used by BrowserContext directly so cannot be marked private.
|
||||||
*/
|
*/
|
||||||
async _disposeContext(contextId?: string): Promise<void> {
|
async _disposeContext(contextId?: string): Promise<void> {
|
||||||
|
if (!contextId) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
await this._connection.send('Target.disposeBrowserContext', {
|
await this._connection.send('Target.disposeBrowserContext', {
|
||||||
browserContextId: contextId || undefined,
|
browserContextId: contextId,
|
||||||
});
|
});
|
||||||
this._contexts.delete(contextId);
|
this._contexts.delete(contextId);
|
||||||
}
|
}
|
||||||
@ -373,6 +376,10 @@ export class Browser extends EventEmitter {
|
|||||||
? this._contexts.get(browserContextId)
|
? this._contexts.get(browserContextId)
|
||||||
: this._defaultContext;
|
: this._defaultContext;
|
||||||
|
|
||||||
|
if (!context) {
|
||||||
|
throw new Error('Missing browser context');
|
||||||
|
}
|
||||||
|
|
||||||
const shouldAttachToTarget = this._targetFilterCallback(targetInfo);
|
const shouldAttachToTarget = this._targetFilterCallback(targetInfo);
|
||||||
if (!shouldAttachToTarget) {
|
if (!shouldAttachToTarget) {
|
||||||
this._ignoredTargets.add(targetInfo.targetId);
|
this._ignoredTargets.add(targetInfo.targetId);
|
||||||
@ -384,7 +391,7 @@ export class Browser extends EventEmitter {
|
|||||||
context,
|
context,
|
||||||
() => this._connection.createSession(targetInfo),
|
() => this._connection.createSession(targetInfo),
|
||||||
this._ignoreHTTPSErrors,
|
this._ignoreHTTPSErrors,
|
||||||
this._defaultViewport,
|
this._defaultViewport ?? null,
|
||||||
this._screenshotTaskQueue
|
this._screenshotTaskQueue
|
||||||
);
|
);
|
||||||
assert(
|
assert(
|
||||||
@ -402,6 +409,11 @@ export class Browser extends EventEmitter {
|
|||||||
private async _targetDestroyed(event: { targetId: string }): Promise<void> {
|
private async _targetDestroyed(event: { targetId: string }): Promise<void> {
|
||||||
if (this._ignoredTargets.has(event.targetId)) return;
|
if (this._ignoredTargets.has(event.targetId)) return;
|
||||||
const target = this._targets.get(event.targetId);
|
const target = this._targets.get(event.targetId);
|
||||||
|
if (!target) {
|
||||||
|
throw new Error(
|
||||||
|
`Missing target in _targetDestroyed (id = ${event.targetId})`
|
||||||
|
);
|
||||||
|
}
|
||||||
target._initializedCallback(false);
|
target._initializedCallback(false);
|
||||||
this._targets.delete(event.targetId);
|
this._targets.delete(event.targetId);
|
||||||
target._closedCallback();
|
target._closedCallback();
|
||||||
@ -418,7 +430,11 @@ export class Browser extends EventEmitter {
|
|||||||
): void {
|
): void {
|
||||||
if (this._ignoredTargets.has(event.targetInfo.targetId)) return;
|
if (this._ignoredTargets.has(event.targetInfo.targetId)) return;
|
||||||
const target = this._targets.get(event.targetInfo.targetId);
|
const target = this._targets.get(event.targetInfo.targetId);
|
||||||
assert(target, 'target should exist before targetInfoChanged');
|
if (!target) {
|
||||||
|
throw new Error(
|
||||||
|
`Missing target in targetInfoChanged (id = ${event.targetInfo.targetId})`
|
||||||
|
);
|
||||||
|
}
|
||||||
const previousURL = target.url();
|
const previousURL = target.url();
|
||||||
const wasInitialized = target._isInitialized;
|
const wasInitialized = target._isInitialized;
|
||||||
target._targetInfoChanged(event.targetInfo);
|
target._targetInfoChanged(event.targetInfo);
|
||||||
@ -469,11 +485,19 @@ export class Browser extends EventEmitter {
|
|||||||
browserContextId: contextId || undefined,
|
browserContextId: contextId || undefined,
|
||||||
});
|
});
|
||||||
const target = this._targets.get(targetId);
|
const target = this._targets.get(targetId);
|
||||||
assert(
|
if (!target) {
|
||||||
await target._initializedPromise,
|
throw new Error(`Missing target for page (id = ${targetId})`);
|
||||||
'Failed to create target for page'
|
}
|
||||||
);
|
const initialized = await target._initializedPromise;
|
||||||
|
if (!initialized) {
|
||||||
|
throw new Error(`Failed to create target for page (id = ${targetId})`);
|
||||||
|
}
|
||||||
const page = await target.page();
|
const page = await target.page();
|
||||||
|
if (!page) {
|
||||||
|
throw new Error(
|
||||||
|
`Failed to create a page for context (id = ${contextId})`
|
||||||
|
);
|
||||||
|
}
|
||||||
return page;
|
return page;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -491,7 +515,13 @@ export class Browser extends EventEmitter {
|
|||||||
* The target associated with the browser.
|
* The target associated with the browser.
|
||||||
*/
|
*/
|
||||||
target(): Target {
|
target(): Target {
|
||||||
return this.targets().find((target) => target.type() === 'browser');
|
const browserTarget = this.targets().find(
|
||||||
|
(target) => target.type() === 'browser'
|
||||||
|
);
|
||||||
|
if (!browserTarget) {
|
||||||
|
throw new Error('Browser target is not found');
|
||||||
|
}
|
||||||
|
return browserTarget;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -728,7 +758,7 @@ export class BrowserContext extends EventEmitter {
|
|||||||
.filter((target) => target.type() === 'page')
|
.filter((target) => target.type() === 'page')
|
||||||
.map((target) => target.page())
|
.map((target) => target.page())
|
||||||
);
|
);
|
||||||
return pages.filter((page) => !!page);
|
return pages.filter((page): page is Page => !!page);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user