@crawlee/browser-pool 3.18.2-beta.1 → 3.18.2-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -41,6 +41,7 @@ export declare abstract class BrowserController<Library extends CommonLibrary =
|
|
|
41
41
|
proxyUrl?: string;
|
|
42
42
|
isActive: boolean;
|
|
43
43
|
activePages: number;
|
|
44
|
+
private readonly closedPages;
|
|
44
45
|
totalPages: number;
|
|
45
46
|
lastPageOpenedAt: number;
|
|
46
47
|
private _activate;
|
|
@@ -77,6 +78,13 @@ export declare abstract class BrowserController<Library extends CommonLibrary =
|
|
|
77
78
|
* @ignore
|
|
78
79
|
*/
|
|
79
80
|
newPage(pageOptions?: NewPageOptions): Promise<NewPageResult>;
|
|
81
|
+
/**
|
|
82
|
+
* Accounts for a page no longer being open. Callable from either side and safe to call twice:
|
|
83
|
+
* the page's own `close` event, or the pool when it gives up on a close that never settles.
|
|
84
|
+
* Tracked in a `WeakSet` rather than via `isClosed()`, because that reports the same event.
|
|
85
|
+
* @ignore
|
|
86
|
+
*/
|
|
87
|
+
registerPageClosed(page: NewPageResult): void;
|
|
80
88
|
setCookies(page: NewPageResult, cookies: Cookie[]): Promise<void>;
|
|
81
89
|
getCookies(page: NewPageResult): Promise<Cookie[]>;
|
|
82
90
|
/**
|
|
@@ -82,6 +82,12 @@ class BrowserController extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
82
82
|
writable: true,
|
|
83
83
|
value: 0
|
|
84
84
|
});
|
|
85
|
+
Object.defineProperty(this, "closedPages", {
|
|
86
|
+
enumerable: true,
|
|
87
|
+
configurable: true,
|
|
88
|
+
writable: true,
|
|
89
|
+
value: new WeakSet()
|
|
90
|
+
});
|
|
85
91
|
Object.defineProperty(this, "totalPages", {
|
|
86
92
|
enumerable: true,
|
|
87
93
|
configurable: true,
|
|
@@ -195,6 +201,18 @@ class BrowserController extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
195
201
|
this.lastPageOpenedAt = Date.now();
|
|
196
202
|
return page;
|
|
197
203
|
}
|
|
204
|
+
/**
|
|
205
|
+
* Accounts for a page no longer being open. Callable from either side and safe to call twice:
|
|
206
|
+
* the page's own `close` event, or the pool when it gives up on a close that never settles.
|
|
207
|
+
* Tracked in a `WeakSet` rather than via `isClosed()`, because that reports the same event.
|
|
208
|
+
* @ignore
|
|
209
|
+
*/
|
|
210
|
+
registerPageClosed(page) {
|
|
211
|
+
if (this.closedPages.has(page))
|
|
212
|
+
return;
|
|
213
|
+
this.closedPages.add(page);
|
|
214
|
+
this.activePages--;
|
|
215
|
+
}
|
|
198
216
|
async setCookies(page, cookies) {
|
|
199
217
|
return this._setCookies(page, cookies);
|
|
200
218
|
}
|
package/browser-pool.js
CHANGED
|
@@ -14,6 +14,7 @@ const timeout_1 = require("@apify/timeout");
|
|
|
14
14
|
const hooks_1 = require("./fingerprinting/hooks");
|
|
15
15
|
const logger_1 = require("./logger");
|
|
16
16
|
const PAGE_CLOSE_KILL_TIMEOUT_MILLIS = 1000;
|
|
17
|
+
const PAGE_CLOSE_TIMEOUT_MILLIS = 5000;
|
|
17
18
|
const BROWSER_KILLER_INTERVAL_MILLIS = 10 * 1000;
|
|
18
19
|
/**
|
|
19
20
|
* The `BrowserPool` class is the most important class of the `browser-pool` module.
|
|
@@ -582,11 +583,48 @@ class BrowserPool extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
582
583
|
const browserController = this.pageToBrowserController.get(page);
|
|
583
584
|
const pageId = this.getPageId(page);
|
|
584
585
|
page.close = async (...args) => {
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
586
|
+
// A browser can acknowledge the close and then never destroy the target, in which case
|
|
587
|
+
// this never settles and the page's own `close` event never fires either (Chromium
|
|
588
|
+
// 536385539). Bound the whole sequence so nothing here can hang the caller, then
|
|
589
|
+
// reconcile the pool regardless of the outcome, so a page that refuses to close cannot
|
|
590
|
+
// leave the pool believing it is still open. `Promise.race` rather than
|
|
591
|
+
// `addTimeoutToPromise`: the latter inherits its AbortController from the calling frame,
|
|
592
|
+
// so a timeout here would cancel the task of whoever awaited `page.close()`.
|
|
593
|
+
let pageClosed = false;
|
|
594
|
+
const closing = (async () => {
|
|
595
|
+
await this._executeHooks(this.prePageCloseHooks, page, browserController);
|
|
596
|
+
await originalPageClose.apply(page, args).catch((err) => {
|
|
597
|
+
logger_1.log.debug(`Could not close page.\nCause:${err.message}`, { id: browserController.id });
|
|
598
|
+
});
|
|
599
|
+
// Whether the page itself is gone. Tracked separately from the sequence finishing,
|
|
600
|
+
// so that a slow hook does not get the browser retired.
|
|
601
|
+
pageClosed = true;
|
|
602
|
+
await this._executeHooks(this.postPageCloseHooks, pageId, browserController);
|
|
603
|
+
})();
|
|
604
|
+
let timeout;
|
|
605
|
+
const finished = await Promise.race([
|
|
606
|
+
closing.then(() => true, (err) => {
|
|
607
|
+
logger_1.log.warning(`Page close hook failed.\nCause:${err.message}`, {
|
|
608
|
+
id: browserController.id,
|
|
609
|
+
pageId,
|
|
610
|
+
});
|
|
611
|
+
return true;
|
|
612
|
+
}),
|
|
613
|
+
new Promise((resolve) => {
|
|
614
|
+
timeout = setTimeout(() => resolve(false), PAGE_CLOSE_TIMEOUT_MILLIS);
|
|
615
|
+
}),
|
|
616
|
+
]);
|
|
617
|
+
clearTimeout(timeout);
|
|
618
|
+
if (!finished) {
|
|
619
|
+
logger_1.log.warning(`Closing a page did not finish within ${PAGE_CLOSE_TIMEOUT_MILLIS / 1000} seconds, ` +
|
|
620
|
+
'releasing it from the pool anyway.', { id: browserController.id, pageId });
|
|
621
|
+
}
|
|
622
|
+
if (!pageClosed) {
|
|
623
|
+
// The page is still attached, so this browser cannot be trusted with more work.
|
|
624
|
+
// Retiring it lets the reclamation below close the process once its pages drain.
|
|
625
|
+
this.retireBrowserController(browserController);
|
|
626
|
+
}
|
|
627
|
+
browserController.registerPageClosed(page);
|
|
590
628
|
this.pages.delete(pageId);
|
|
591
629
|
this._closeRetiredBrowserWithNoPages(browserController);
|
|
592
630
|
this.emit("pageClosed" /* BROWSER_POOL_EVENTS.PAGE_CLOSED */, page);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crawlee/browser-pool",
|
|
3
|
-
"version": "3.18.2-beta.
|
|
3
|
+
"version": "3.18.2-beta.3",
|
|
4
4
|
"description": "Rotate multiple browsers using popular automation libraries such as Playwright or Puppeteer.",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=16.0.0"
|
|
@@ -38,8 +38,8 @@
|
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@apify/log": "^2.4.0",
|
|
40
40
|
"@apify/timeout": "^0.4.0",
|
|
41
|
-
"@crawlee/core": "3.18.2-beta.
|
|
42
|
-
"@crawlee/types": "3.18.2-beta.
|
|
41
|
+
"@crawlee/core": "3.18.2-beta.3",
|
|
42
|
+
"@crawlee/types": "3.18.2-beta.3",
|
|
43
43
|
"fingerprint-generator": "^2.1.68",
|
|
44
44
|
"fingerprint-injector": "^2.1.68",
|
|
45
45
|
"lodash.merge": "^4.6.2",
|
|
@@ -70,5 +70,5 @@
|
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
72
|
},
|
|
73
|
-
"gitHead": "
|
|
73
|
+
"gitHead": "feed328453d1967b36ccd45cc4531d44498915b8"
|
|
74
74
|
}
|
|
@@ -50,7 +50,7 @@ class PlaywrightController extends browser_controller_1.BrowserController {
|
|
|
50
50
|
try {
|
|
51
51
|
const page = await this.browser.newPage(contextOptions);
|
|
52
52
|
page.once('close', async () => {
|
|
53
|
-
this.
|
|
53
|
+
this.registerPageClosed(page);
|
|
54
54
|
await close();
|
|
55
55
|
});
|
|
56
56
|
if (this.launchContext.experimentalContainers) {
|
|
@@ -56,7 +56,7 @@ class PuppeteerController extends browser_controller_1.BrowserController {
|
|
|
56
56
|
}
|
|
57
57
|
*/
|
|
58
58
|
page.once('close', async () => {
|
|
59
|
-
this.
|
|
59
|
+
this.registerPageClosed(page);
|
|
60
60
|
try {
|
|
61
61
|
await context.close();
|
|
62
62
|
}
|
|
@@ -77,7 +77,7 @@ class PuppeteerController extends browser_controller_1.BrowserController {
|
|
|
77
77
|
const page = await this.browser.newPage();
|
|
78
78
|
(0, timeout_1.tryCancel)();
|
|
79
79
|
page.once('close', () => {
|
|
80
|
-
this.
|
|
80
|
+
this.registerPageClosed(page);
|
|
81
81
|
});
|
|
82
82
|
return page;
|
|
83
83
|
}
|