Skip to content

Commit 4f23404

Browse files
authored
Profile Saving Improvements (#894)
fix some observed errors that occur when saving profile: - use browser.cookies instead of page.cookies to get all cookies, not just from page - catch exception when clearing cache and ignore - logging: log when proxy init is happening on all paths, in case error in proxy connection
1 parent 002feb2 commit 4f23404

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

src/create-login-profile.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,11 @@ async function createProfile(
339339
cdp: CDPSession,
340340
targetFilename = "",
341341
) {
342-
await cdp.send("Network.clearBrowserCache");
342+
try {
343+
await cdp.send("Network.clearBrowserCache");
344+
} catch (e) {
345+
logger.warn("Error clearing cache", e, "browser");
346+
}
343347

344348
await browser.close();
345349

@@ -546,7 +550,8 @@ class InteractiveBrowser {
546550
return;
547551
}
548552

549-
const cookies = await this.browser.getCookies(this.page);
553+
const cookies = await this.browser.getCookies();
554+
550555
for (const cookieOrig of cookies) {
551556
// eslint-disable-next-line @typescript-eslint/no-explicit-any
552557
const cookie = cookieOrig as any;
@@ -566,7 +571,7 @@ class InteractiveBrowser {
566571
cookie.url = url;
567572
}
568573
}
569-
await this.browser.setCookies(this.page, cookies);
574+
await this.browser.setCookies(cookies);
570575
// eslint-disable-next-line @typescript-eslint/no-explicit-any
571576
} catch (e: any) {
572577
logger.error("Save Cookie Error: ", e);

src/util/browser.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import puppeteer, {
2222
Page,
2323
LaunchOptions,
2424
Viewport,
25+
CookieData,
2526
} from "puppeteer-core";
2627
import { CDPSession, Target, Browser as PptrBrowser } from "puppeteer-core";
2728
import { Recorder } from "./recorder.js";
@@ -616,14 +617,12 @@ export class Browser {
616617
await page.setViewport(params);
617618
}
618619

619-
async getCookies(page: Page) {
620-
return await page.cookies();
620+
async getCookies() {
621+
return (await this.browser?.cookies()) || [];
621622
}
622623

623-
// TODO: Fix this the next time the file is edited.
624-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
625-
async setCookies(page: Page, cookies: any) {
626-
return await page.setCookie(...cookies);
624+
async setCookies(cookies: CookieData[]) {
625+
return await this.browser?.setCookie(...cookies);
627626
}
628627
}
629628

src/util/proxy.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,6 @@ export async function initProxy(
153153
privateKeyFile = privateKeyFile || sshProxyPrivateKeyFile;
154154
publicHostsFile = publicHostsFile || sshProxyKnownHostsFile;
155155

156-
logger.debug("Initing proxy", {
157-
url: getSafeProxyString(proxyUrl),
158-
localPort,
159-
privateKeyFile,
160-
publicHostsFile,
161-
});
162-
163156
const entry = await initSingleProxy(
164157
proxyUrl,
165158
localPort++,
@@ -200,6 +193,13 @@ export async function initSingleProxy(
200193
sshProxyPrivateKeyFile?: string,
201194
sshProxyKnownHostsFile?: string,
202195
): Promise<{ proxyUrl: string; dispatcher: Dispatcher }> {
196+
logger.debug("Initing proxy", {
197+
url: getSafeProxyString(proxyUrl),
198+
localPort,
199+
sshProxyPrivateKeyFile,
200+
sshProxyKnownHostsFile,
201+
});
202+
203203
if (proxyUrl && proxyUrl.startsWith("ssh://")) {
204204
proxyUrl = await runSSHD(
205205
proxyUrl,

0 commit comments

Comments
 (0)