Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close Browser #95

Merged
merged 10 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 25 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
"lodash.samplesize": "^4.2.0",
"npm": "^10.5.2",
"puppeteer": "^22.13.1",
"puppeteer-har": "^1.1.1",
"run": "^1.4.0",
"stacktrace-js": "^2.0.1",
"tldts": "^6.0.2",
"tmp": "^0.1.0",
"puppeteer-har": "^1.1.2",
"run": "^1.5.0",
"stacktrace-js": "^2.0.2",
"tldts": "^6.1.34",
"tmp": "^0.2.3",
"tough-cookie": "^4.1.4",
"winston": "^3.2.1"
"winston": "^3.13.1"
},
"devDependencies": {
"@types/chrome": "^0.0.268",
Expand Down
12 changes: 4 additions & 8 deletions src/collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { dedupLinks, getLinks, getSocialLinks } from './pptr-utils/get-links';
import { autoScroll, fillForms } from './pptr-utils/interaction-utils';
import { setupSessionRecordingInspector } from './session-recording';
import { setUpThirdPartyTrackersInspector } from './third-party-trackers';
import { clearDir } from './utils';
import { clearDir, closeBrowser } from './utils';

export type CollectorOptions = Partial<typeof DEFAULT_OPTIONS>;

Expand Down Expand Up @@ -221,8 +221,7 @@ export const collect = async (inUrl: string, args: CollectorOptions) => {

// Return if the page doesnt load
if (loadError) {
console.log("browser close 1");
await browser.close();
await closeBrowser(browser);
if (typeof userDataDir !== 'undefined') {
clearDir(userDataDir, false);
}
Expand Down Expand Up @@ -304,9 +303,7 @@ export const collect = async (inUrl: string, args: CollectorOptions) => {
// console.log('... done saving har');
}

console.log('closing browser');
await browser.close();
// console.log('... done closing browser');
await closeBrowser(browser);
if (typeof userDataDir !== 'undefined') {
clearDir(userDataDir, false);
}
Expand Down Expand Up @@ -403,8 +400,7 @@ export const collect = async (inUrl: string, args: CollectorOptions) => {
finally {
// close browser and clear tmp dir
if (browser && !didBrowserDisconnect) {
console.log("closing browser");
await browser.close();
await closeBrowser(browser);
}
// if (typeof userDataDir !== 'undefined') {
// clearDir(userDataDir, false);
Expand Down
16 changes: 16 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ const deleteFolderRecursive = path => {
}
};

// This is an annoying hack to get around an issue in Puppeteer
// where the browser.close method hangs indefinitely
// See https://github.com/Sparticuz/chromium/issues/85#issuecomment-1527692751
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the link you meant to put here? It talks about fixing the browser.close issue, but not the way you're doing it here (apparently by disabling the Chrome GPU function instead).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yes, it's at a different point in the thread. I'll update the link

export const closeBrowser = async (browser) => {
console.log("closing browser");
const pages = await browser.pages();
for (let i = 0; i < pages.length; i++) {
await pages[i].close();
}
const childProcess = browser.process()
if (childProcess) {
childProcess.kill(9)
}
await browser.close();
};

export const clearDir = (outDir, mkNewDir = true) => {
if (fs.existsSync(outDir)) {
deleteFolderRecursive(outDir);
Expand Down
Loading