From 6c9fd8e3c27a7b4b2371dc6aa3b4ec6a3e5a60b1 Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Thu, 7 Dec 2023 14:59:22 -0800 Subject: [PATCH 1/2] clients(lr): modify CdpBrowser usage to work with new tab target connection logic --- clients/lightrider/lightrider-entry.js | 33 ++++++++++++++++++++------ 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/clients/lightrider/lightrider-entry.js b/clients/lightrider/lightrider-entry.js index d3d106421103..de274bd84aa7 100644 --- a/clients/lightrider/lightrider-entry.js +++ b/clients/lightrider/lightrider-entry.js @@ -54,15 +54,34 @@ async function getPageFromConnection(connection) { undefined /* defaultViewport */, undefined /* process */, undefined /* closeCallback */, - // @ts-expect-error internal property - targetInfo => targetInfo._targetId === mainTargetInfo.targetId + // eslint-disable-next-line no-unused-vars + targetInfo => true ); - const pages = await browser.pages(); - const page = pages.find(p => p.mainFrame()._id === frameTree.frame.id); - if (!page) throw new Error('Could not find relevant puppeteer page'); - - // @ts-expect-error Page has a slightly different type when importing the browser module directly. + // We should be able to find the relevant page instantly, but just in case + // the relevant tab target comes a bit delayed, check every time a new + // target is seen. + /** @type {(value: any) => void} */ + let resolve; + /** @type {(value: Error) => void} */ + let reject; + const promise = new Promise((resolve_, reject_) => { + resolve = resolve_; + reject = reject_; + }); + browser.waitForTarget(async () => { + const pages = await browser.pages(); + const page = pages.find(p => p.mainFrame()._id === frameTree.frame.id); + if (page) { + resolve(page); + return true; + } + return false; + }); + const timeoutHandle = setTimeout(() => + reject(new Error('Could not find relevant puppeteer page')), 5000); + const page = await promise; + clearTimeout(timeoutHandle); return page; } From fea4a3d6a073ddf87ad1b707ef7ca34b1b15e1d5 Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Thu, 7 Dec 2023 16:08:22 -0800 Subject: [PATCH 2/2] update --- clients/lightrider/lightrider-entry.js | 35 ++++++++------------------ 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/clients/lightrider/lightrider-entry.js b/clients/lightrider/lightrider-entry.js index de274bd84aa7..4a47c7b5efd2 100644 --- a/clients/lightrider/lightrider-entry.js +++ b/clients/lightrider/lightrider-entry.js @@ -50,38 +50,23 @@ async function getPageFromConnection(connection) { 'chrome', pptrConnection, [] /* contextIds */, - false /* ignoreHTTPSErrors */, - undefined /* defaultViewport */, - undefined /* process */, - undefined /* closeCallback */, - // eslint-disable-next-line no-unused-vars - targetInfo => true + false /* ignoreHTTPSErrors */ ); // We should be able to find the relevant page instantly, but just in case // the relevant tab target comes a bit delayed, check every time a new // target is seen. - /** @type {(value: any) => void} */ - let resolve; - /** @type {(value: Error) => void} */ - let reject; - const promise = new Promise((resolve_, reject_) => { - resolve = resolve_; - reject = reject_; - }); - browser.waitForTarget(async () => { - const pages = await browser.pages(); - const page = pages.find(p => p.mainFrame()._id === frameTree.frame.id); - if (page) { - resolve(page); - return true; - } + const targetPromise = browser.waitForTarget(async (target) => { + const page = await target.page(); + if (page && page.mainFrame()._id === frameTree.frame.id) return true; return false; }); - const timeoutHandle = setTimeout(() => - reject(new Error('Could not find relevant puppeteer page')), 5000); - const page = await promise; - clearTimeout(timeoutHandle); + const page = await Promise.race([ + targetPromise.then(target => target.page()), + new Promise((_, reject) => { + setTimeout(() => reject(new Error('Could not find relevant puppeteer page')), 5000); + }), + ]); return page; }