diff --git a/packages/puppeteer-core/src/common/Browser.ts b/packages/puppeteer-core/src/common/Browser.ts index 36ebb50e37d..8cb2de51a8d 100644 --- a/packages/puppeteer-core/src/common/Browser.ts +++ b/packages/puppeteer-core/src/common/Browser.ts @@ -33,6 +33,7 @@ import { import {BrowserContext} from '../api/BrowserContext.js'; import {Page} from '../api/Page.js'; import {assert} from '../util/assert.js'; +import {createDeferredPromise} from '../util/DeferredPromise.js'; import {ChromeTargetManager} from './ChromeTargetManager.js'; import {CDPSession, Connection, ConnectionEmittedEvents} from './Connection.js'; @@ -478,11 +479,8 @@ export class CDPBrowser extends BrowserBase { options: WaitForTargetOptions = {} ): Promise { const {timeout = 30000} = options; - let resolve: (value: Target | PromiseLike) => void; - let isResolved = false; - const targetPromise = new Promise(x => { - return (resolve = x); - }); + const targetPromise = createDeferredPromise>(); + this.on(BrowserEmittedEvents.TargetCreated, check); this.on(BrowserEmittedEvents.TargetChanged, check); try { @@ -497,9 +495,8 @@ export class CDPBrowser extends BrowserBase { } async function check(target: Target): Promise { - if ((await predicate(target)) && !isResolved) { - isResolved = true; - resolve(target); + if ((await predicate(target)) && !targetPromise.resolved()) { + targetPromise.resolve(target); } } } diff --git a/packages/puppeteer-core/src/common/ChromeTargetManager.ts b/packages/puppeteer-core/src/common/ChromeTargetManager.ts index 4ba515e48ff..58c8353aadb 100644 --- a/packages/puppeteer-core/src/common/ChromeTargetManager.ts +++ b/packages/puppeteer-core/src/common/ChromeTargetManager.ts @@ -18,6 +18,7 @@ import {Protocol} from 'devtools-protocol'; import {TargetFilterCallback} from '../api/Browser.js'; import {assert} from '../util/assert.js'; +import {createDeferredPromise} from '../util/DeferredPromise.js'; import {CDPSession, Connection} from './Connection.js'; import {EventEmitter} from './EventEmitter.js'; @@ -80,10 +81,7 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager { (event: Protocol.Target.DetachedFromTargetEvent) => void > = new WeakMap(); - #initializeCallback = () => {}; - #initializePromise: Promise = new Promise(resolve => { - this.#initializeCallback = resolve; - }); + #initializePromise = createDeferredPromise(); #targetsIdsForInit: Set = new Set(); constructor( @@ -381,7 +379,7 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager { #finishInitializationIfReady(targetId?: string): void { targetId !== undefined && this.#targetsIdsForInit.delete(targetId); if (this.#targetsIdsForInit.size === 0) { - this.#initializeCallback(); + this.#initializePromise.resolve(); } } diff --git a/packages/puppeteer-core/src/common/FirefoxTargetManager.ts b/packages/puppeteer-core/src/common/FirefoxTargetManager.ts index d5c8582072f..745c37d9502 100644 --- a/packages/puppeteer-core/src/common/FirefoxTargetManager.ts +++ b/packages/puppeteer-core/src/common/FirefoxTargetManager.ts @@ -18,6 +18,7 @@ import {Protocol} from 'devtools-protocol'; import {TargetFilterCallback} from '../api/Browser.js'; import {assert} from '../util/assert.js'; +import {createDeferredPromise} from '../util/DeferredPromise.js'; import {CDPSession, Connection} from './Connection.js'; import {EventEmitter} from './EventEmitter.js'; @@ -87,10 +88,7 @@ export class FirefoxTargetManager (event: Protocol.Target.AttachedToTargetEvent) => Promise > = new WeakMap(); - #initializeCallback = () => {}; - #initializePromise: Promise = new Promise(resolve => { - this.#initializeCallback = resolve; - }); + #initializePromise = createDeferredPromise(); #targetsIdsForInit: Set = new Set(); constructor( @@ -255,7 +253,7 @@ export class FirefoxTargetManager #finishInitializationIfReady(targetId: string): void { this.#targetsIdsForInit.delete(targetId); if (this.#targetsIdsForInit.size === 0) { - this.#initializeCallback(); + this.#initializePromise.resolve(); } } } diff --git a/packages/puppeteer-core/src/common/Frame.ts b/packages/puppeteer-core/src/common/Frame.ts index 962b85c622c..e9cf97f0bbf 100644 --- a/packages/puppeteer-core/src/common/Frame.ts +++ b/packages/puppeteer-core/src/common/Frame.ts @@ -798,17 +798,7 @@ export class Frame { } if (path) { - let fs; - try { - fs = (await import('fs')).promises; - } catch (error) { - if (error instanceof TypeError) { - throw new Error( - 'Can only pass a file path in a Node-like environment.' - ); - } - throw error; - } + const fs = await importFSPromises(); content = await fs.readFile(path, 'utf8'); content += `//# sourceURL=${path.replace(/\n/g, '')}`; }