From 6edd99676827de2c83f7a858e4f903b1c34e7d35 Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Thu, 26 Jan 2023 09:50:06 +0100 Subject: [PATCH] fix(revert): use LazyArg for puppeteer utilities (#9590) Reverts puppeteer/puppeteer#9575 --- packages/puppeteer-core/src/common/Frame.ts | 15 ++++---------- .../src/common/IsolatedWorld.ts | 20 +++++++++++-------- packages/puppeteer-core/src/common/LazyArg.ts | 8 +------- .../puppeteer-core/src/common/QueryHandler.ts | 14 ++----------- .../puppeteer-core/src/common/WaitTask.ts | 13 +++--------- packages/puppeteer-core/src/common/types.ts | 13 ++++++++++++ test/src/injected.spec.ts | 5 +---- 7 files changed, 36 insertions(+), 52 deletions(-) diff --git a/packages/puppeteer-core/src/common/Frame.ts b/packages/puppeteer-core/src/common/Frame.ts index a4e56d16fbe..cbd1097afd6 100644 --- a/packages/puppeteer-core/src/common/Frame.ts +++ b/packages/puppeteer-core/src/common/Frame.ts @@ -34,7 +34,6 @@ import {Page} from '../api/Page.js'; import {getQueryHandlerAndSelector} from './QueryHandler.js'; import {EvaluateFunc, HandleFor, NodeFor} from './types.js'; import {importFS} from './util.js'; -import {LazyArg} from './LazyArg.js'; /** * @public @@ -805,9 +804,8 @@ export class Frame { type = type ?? 'text/javascript'; - const puppeteerWorld = this.worlds[PUPPETEER_WORLD]; return this.worlds[MAIN_WORLD].transferHandle( - await puppeteerWorld.evaluateHandle( + await this.worlds[PUPPETEER_WORLD].evaluateHandle( async ({createDeferredPromise}, {url, id, type, content}) => { const promise = createDeferredPromise(); const script = document.createElement('script'); @@ -841,9 +839,7 @@ export class Frame { await promise; return script; }, - LazyArg.create(() => { - return puppeteerWorld.puppeteerUtil; - }), + await this.worlds[PUPPETEER_WORLD].puppeteerUtil, {...options, type, content} ) ); @@ -891,9 +887,8 @@ export class Frame { options.content = content; } - const puppeteerWorld = this.worlds[PUPPETEER_WORLD]; return this.worlds[MAIN_WORLD].transferHandle( - await puppeteerWorld.evaluateHandle( + await this.worlds[PUPPETEER_WORLD].evaluateHandle( async ({createDeferredPromise}, {url, content}) => { const promise = createDeferredPromise(); let element: HTMLStyleElement | HTMLLinkElement; @@ -928,9 +923,7 @@ export class Frame { await promise; return element; }, - LazyArg.create(() => { - return puppeteerWorld.puppeteerUtil; - }), + await this.worlds[PUPPETEER_WORLD].puppeteerUtil, options ) ); diff --git a/packages/puppeteer-core/src/common/IsolatedWorld.ts b/packages/puppeteer-core/src/common/IsolatedWorld.ts index 51af6705291..13a22555cb8 100644 --- a/packages/puppeteer-core/src/common/IsolatedWorld.ts +++ b/packages/puppeteer-core/src/common/IsolatedWorld.ts @@ -28,7 +28,7 @@ import {JSHandle} from './JSHandle.js'; import {LazyArg} from './LazyArg.js'; import {LifecycleWatcher, PuppeteerLifeCycleEvent} from './LifecycleWatcher.js'; import {TimeoutSettings} from './TimeoutSettings.js'; -import {EvaluateFunc, HandleFor, NodeFor} from './types.js'; +import {EvaluateFunc, HandleFor, InnerLazyParams, NodeFor} from './types.js'; import {createJSHandle, debugError, pageBindingInitString} from './util.js'; import {TaskManager, WaitTask} from './WaitTask.js'; import {MAIN_WORLD, PUPPETEER_WORLD} from './IsolatedWorlds.js'; @@ -138,11 +138,8 @@ export class IsolatedWorld { } clearContext(): void { - // Only create a new promise if the old one was resolved. - if (this.#puppeteerUtil.resolved()) { - this.#puppeteerUtil = createDeferredPromise(); - } this.#document = undefined; + this.#puppeteerUtil = createDeferredPromise(); this.#context = createDeferredPromise(); } @@ -520,8 +517,13 @@ export class IsolatedWorld { root, timeout, }, - LazyArg.create(() => { - return this.puppeteerUtil; + new LazyArg(async () => { + try { + // In case CDP fails. + return await this.puppeteerUtil; + } catch { + return undefined; + } }), queryOne.toString(), selector, @@ -545,7 +547,9 @@ export class IsolatedWorld { waitForFunction< Params extends unknown[], - Func extends EvaluateFunc = EvaluateFunc + Func extends EvaluateFunc> = EvaluateFunc< + InnerLazyParams + > >( pageFunction: Func | string, options: { diff --git a/packages/puppeteer-core/src/common/LazyArg.ts b/packages/puppeteer-core/src/common/LazyArg.ts index 8599e82cffc..c182663a295 100644 --- a/packages/puppeteer-core/src/common/LazyArg.ts +++ b/packages/puppeteer-core/src/common/LazyArg.ts @@ -18,14 +18,8 @@ * @internal */ export class LazyArg { - static create = (callback: () => Promise): T => { - // We do type coercion here because we don't want to introduce LazyArgs to - // the type system. - return new LazyArg(callback) as unknown as T; - }; - #get: () => Promise; - private constructor(get: () => Promise) { + constructor(get: () => Promise) { this.#get = get; } diff --git a/packages/puppeteer-core/src/common/QueryHandler.ts b/packages/puppeteer-core/src/common/QueryHandler.ts index b8cba2d701b..da9a4530ae4 100644 --- a/packages/puppeteer-core/src/common/QueryHandler.ts +++ b/packages/puppeteer-core/src/common/QueryHandler.ts @@ -15,13 +15,11 @@ */ import PuppeteerUtil from '../injected/injected.js'; -import {assert} from '../util/assert.js'; import {ariaHandler} from './AriaQueryHandler.js'; import {ElementHandle} from './ElementHandle.js'; import {Frame} from './Frame.js'; import {WaitForSelectorOptions} from './IsolatedWorld.js'; import {MAIN_WORLD, PUPPETEER_WORLD} from './IsolatedWorlds.js'; -import {LazyArg} from './LazyArg.js'; /** * @public @@ -104,11 +102,7 @@ function createPuppeteerQueryHandler( const jsHandle = await element.evaluateHandle( queryOne, selector, - LazyArg.create(() => { - const world = element.executionContext()._world; - assert(world); - return world.puppeteerUtil; - }) + await element.executionContext()._world!.puppeteerUtil ); const elementHandle = jsHandle.asElement(); if (elementHandle) { @@ -154,11 +148,7 @@ function createPuppeteerQueryHandler( const jsHandle = await element.evaluateHandle( queryAll, selector, - LazyArg.create(() => { - const world = element.executionContext()._world; - assert(world); - return world.puppeteerUtil; - }) + await element.executionContext()._world!.puppeteerUtil ); const properties = await jsHandle.getProperties(); await jsHandle.dispose(); diff --git a/packages/puppeteer-core/src/common/WaitTask.ts b/packages/puppeteer-core/src/common/WaitTask.ts index 3fff4d96957..955cc2d8651 100644 --- a/packages/puppeteer-core/src/common/WaitTask.ts +++ b/packages/puppeteer-core/src/common/WaitTask.ts @@ -20,7 +20,6 @@ import {ElementHandle} from './ElementHandle.js'; import {TimeoutError} from './Errors.js'; import {IsolatedWorld} from './IsolatedWorld.js'; import {JSHandle} from './JSHandle.js'; -import {LazyArg} from './LazyArg.js'; import {HandleFor} from './types.js'; /** @@ -115,9 +114,7 @@ export class WaitTask { return fun(...args) as Promise; }); }, - LazyArg.create(() => { - return this.#world.puppeteerUtil; - }), + await this.#world.puppeteerUtil, this.#fn, ...this.#args ); @@ -130,9 +127,7 @@ export class WaitTask { return fun(...args) as Promise; }, root || document); }, - LazyArg.create(() => { - return this.#world.puppeteerUtil; - }), + await this.#world.puppeteerUtil, this.#root, this.#fn, ...this.#args @@ -146,9 +141,7 @@ export class WaitTask { return fun(...args) as Promise; }, ms); }, - LazyArg.create(() => { - return this.#world.puppeteerUtil; - }), + await this.#world.puppeteerUtil, this.#polling, this.#fn, ...this.#args diff --git a/packages/puppeteer-core/src/common/types.ts b/packages/puppeteer-core/src/common/types.ts index e51abde4d4a..ed30e735238 100644 --- a/packages/puppeteer-core/src/common/types.ts +++ b/packages/puppeteer-core/src/common/types.ts @@ -16,6 +16,7 @@ import {JSHandle} from './JSHandle.js'; import {ElementHandle} from './ElementHandle.js'; +import {LazyArg} from './LazyArg.js'; /** * @public @@ -37,6 +38,18 @@ export type HandleOr = HandleFor | JSHandle | T; */ export type FlattenHandle = T extends HandleOr ? U : never; +/** + * @internal + */ +export type FlattenLazyArg = T extends LazyArg ? U : T; + +/** + * @internal + */ +export type InnerLazyParams = { + [K in keyof T]: FlattenLazyArg; +}; + /** * @public */ diff --git a/test/src/injected.spec.ts b/test/src/injected.spec.ts index 3b51fa9f657..cde47fcc406 100644 --- a/test/src/injected.spec.ts +++ b/test/src/injected.spec.ts @@ -16,7 +16,6 @@ import expect from 'expect'; import {PUPPETEER_WORLD} from 'puppeteer-core/internal/common/IsolatedWorlds.js'; -import {LazyArg} from 'puppeteer-core/internal/common/LazyArg.js'; import { getTestState, setupTestBrowserHooks, @@ -46,9 +45,7 @@ describe('PuppeteerUtil tests', function () { ({createFunction}, fnString) => { return createFunction(fnString)(4); }, - LazyArg.create(() => { - return world.puppeteerUtil; - }), + await world.puppeteerUtil, (() => { return 4; }).toString()