@crawlee/browser-pool 4.0.0-beta.8 → 4.0.0-beta.80
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.
- package/README.md +17 -13
- package/abstract-classes/browser-controller.d.ts +67 -8
- package/abstract-classes/browser-controller.js +8 -11
- package/abstract-classes/browser-plugin.d.ts +38 -3
- package/abstract-classes/browser-plugin.js +65 -7
- package/anonymize-proxy.d.ts +5 -2
- package/anonymize-proxy.js +9 -5
- package/browser-pool.d.ts +88 -14
- package/browser-pool.js +151 -28
- package/container-proxy-server.d.ts +0 -1
- package/container-proxy-server.js +0 -1
- package/events.d.ts +0 -1
- package/events.js +0 -1
- package/fingerprinting/hooks.d.ts +0 -1
- package/fingerprinting/hooks.js +24 -9
- package/fingerprinting/types.d.ts +0 -1
- package/fingerprinting/types.js +0 -1
- package/fingerprinting/utils.d.ts +0 -1
- package/fingerprinting/utils.js +0 -1
- package/index.d.ts +14 -7
- package/index.js +5 -3
- package/launch-context.d.ts +19 -3
- package/launch-context.js +12 -4
- package/package.json +7 -8
- package/playwright/playwright-browser.d.ts +0 -1
- package/playwright/playwright-browser.js +0 -1
- package/playwright/playwright-controller.d.ts +0 -1
- package/playwright/playwright-controller.js +20 -10
- package/playwright/playwright-plugin.d.ts +6 -1
- package/playwright/playwright-plugin.js +27 -4
- package/proxy-server.d.ts +0 -1
- package/proxy-server.js +0 -1
- package/puppeteer/puppeteer-controller.d.ts +0 -1
- package/puppeteer/puppeteer-controller.js +16 -9
- package/puppeteer/puppeteer-plugin.d.ts +3 -1
- package/puppeteer/puppeteer-plugin.js +81 -44
- package/remote-browser-pool.d.ts +172 -0
- package/remote-browser-pool.js +191 -0
- package/remote-browser-provider.d.ts +83 -0
- package/remote-browser-provider.js +67 -0
- package/utils.d.ts +7 -1
- package/utils.js +19 -1
- package/abstract-classes/browser-controller.d.ts.map +0 -1
- package/abstract-classes/browser-controller.js.map +0 -1
- package/abstract-classes/browser-plugin.d.ts.map +0 -1
- package/abstract-classes/browser-plugin.js.map +0 -1
- package/anonymize-proxy.d.ts.map +0 -1
- package/anonymize-proxy.js.map +0 -1
- package/browser-pool.d.ts.map +0 -1
- package/browser-pool.js.map +0 -1
- package/container-proxy-server.d.ts.map +0 -1
- package/container-proxy-server.js.map +0 -1
- package/events.d.ts.map +0 -1
- package/events.js.map +0 -1
- package/fingerprinting/hooks.d.ts.map +0 -1
- package/fingerprinting/hooks.js.map +0 -1
- package/fingerprinting/types.d.ts.map +0 -1
- package/fingerprinting/types.js.map +0 -1
- package/fingerprinting/utils.d.ts.map +0 -1
- package/fingerprinting/utils.js.map +0 -1
- package/index.d.ts.map +0 -1
- package/index.js.map +0 -1
- package/launch-context.d.ts.map +0 -1
- package/launch-context.js.map +0 -1
- package/logger.d.ts +0 -3
- package/logger.d.ts.map +0 -1
- package/logger.js +0 -5
- package/logger.js.map +0 -1
- package/playwright/playwright-browser.d.ts.map +0 -1
- package/playwright/playwright-browser.js.map +0 -1
- package/playwright/playwright-controller.d.ts.map +0 -1
- package/playwright/playwright-controller.js.map +0 -1
- package/playwright/playwright-plugin.d.ts.map +0 -1
- package/playwright/playwright-plugin.js.map +0 -1
- package/proxy-server.d.ts.map +0 -1
- package/proxy-server.js.map +0 -1
- package/puppeteer/puppeteer-controller.d.ts.map +0 -1
- package/puppeteer/puppeteer-controller.js.map +0 -1
- package/puppeteer/puppeteer-plugin.d.ts.map +0 -1
- package/puppeteer/puppeteer-plugin.js.map +0 -1
- package/tsconfig.build.tsbuildinfo +0 -1
- package/utils.d.ts.map +0 -1
- package/utils.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crawlee/browser-pool",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.80",
|
|
4
4
|
"description": "Rotate multiple browsers using popular automation libraries such as Playwright or Puppeteer.",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=22.0.0"
|
|
@@ -24,18 +24,17 @@
|
|
|
24
24
|
"url": "https://github.com/apify/crawlee/issues"
|
|
25
25
|
},
|
|
26
26
|
"scripts": {
|
|
27
|
-
"build": "
|
|
27
|
+
"build": "pnpm clean && pnpm compile && pnpm copy",
|
|
28
28
|
"clean": "rimraf ./dist",
|
|
29
29
|
"compile": "tsc -p tsconfig.build.json",
|
|
30
30
|
"copy": "tsx ../../scripts/copy.ts"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@apify/log": "^2.5.18",
|
|
34
33
|
"@apify/timeout": "^0.3.2",
|
|
35
|
-
"@crawlee/core": "4.0.0-beta.
|
|
36
|
-
"@crawlee/types": "4.0.0-beta.
|
|
37
|
-
"fingerprint-generator": "^2.1.
|
|
38
|
-
"fingerprint-injector": "^2.1.
|
|
34
|
+
"@crawlee/core": "4.0.0-beta.80",
|
|
35
|
+
"@crawlee/types": "4.0.0-beta.80",
|
|
36
|
+
"fingerprint-generator": "^2.1.68",
|
|
37
|
+
"fingerprint-injector": "^2.1.68",
|
|
39
38
|
"lodash.merge": "^4.6.2",
|
|
40
39
|
"nanoid": "^5.1.5",
|
|
41
40
|
"ow": "^2.0.0",
|
|
@@ -64,5 +63,5 @@
|
|
|
64
63
|
}
|
|
65
64
|
}
|
|
66
65
|
},
|
|
67
|
-
"gitHead": "
|
|
66
|
+
"gitHead": "96c57b4a0c999e4b2bd198792490af28db7aa42d"
|
|
68
67
|
}
|
|
@@ -11,7 +11,7 @@ export class PlaywrightController extends BrowserController {
|
|
|
11
11
|
const password = decodeURIComponent(url.password);
|
|
12
12
|
return {
|
|
13
13
|
proxy: {
|
|
14
|
-
server: url.
|
|
14
|
+
server: `${url.protocol}//${url.host}`,
|
|
15
15
|
username,
|
|
16
16
|
password,
|
|
17
17
|
bypass: pageOptions?.proxy?.bypass,
|
|
@@ -23,15 +23,26 @@ export class PlaywrightController extends BrowserController {
|
|
|
23
23
|
throw new Error('A new page can be created with provided context only when using incognito pages.');
|
|
24
24
|
}
|
|
25
25
|
let close = async () => { };
|
|
26
|
-
if (this.launchContext.useIncognitoPages
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
26
|
+
if (this.launchContext.useIncognitoPages) {
|
|
27
|
+
// Each page requires to have all the context options applied
|
|
28
|
+
contextOptions = {
|
|
29
|
+
...this.launchContext.launchOptions,
|
|
30
|
+
...contextOptions,
|
|
31
|
+
};
|
|
32
|
+
// Remote browsers handle their own proxy — don't inject local proxy settings into context
|
|
33
|
+
if (this.launchContext.isRemote) {
|
|
34
|
+
delete contextOptions?.proxy;
|
|
35
|
+
}
|
|
36
|
+
if (contextOptions?.proxy) {
|
|
37
|
+
const [anonymizedProxyUrl, closeProxy] = await anonymizeProxySugar(contextOptions.proxy.server, contextOptions.proxy.username, contextOptions.proxy.password, { ignoreProxyCertificate: this.launchContext.ignoreProxyCertificate });
|
|
38
|
+
if (anonymizedProxyUrl) {
|
|
39
|
+
contextOptions.proxy = {
|
|
40
|
+
server: anonymizedProxyUrl,
|
|
41
|
+
bypass: contextOptions.proxy.bypass,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
close = closeProxy;
|
|
33
45
|
}
|
|
34
|
-
close = closeProxy;
|
|
35
46
|
}
|
|
36
47
|
try {
|
|
37
48
|
const page = await this.browser.newPage(contextOptions);
|
|
@@ -63,4 +74,3 @@ export class PlaywrightController extends BrowserController {
|
|
|
63
74
|
return context.addCookies(cookies);
|
|
64
75
|
}
|
|
65
76
|
}
|
|
66
|
-
//# sourceMappingURL=playwright-controller.js.map
|
|
@@ -3,15 +3,20 @@ import type { Browser as PlaywrightBrowser, BrowserType } from 'playwright';
|
|
|
3
3
|
import { BrowserPlugin } from '../abstract-classes/browser-plugin.js';
|
|
4
4
|
import type { createProxyServerForContainers } from '../container-proxy-server.js';
|
|
5
5
|
import type { LaunchContext } from '../launch-context.js';
|
|
6
|
+
import type { RemoteConnection, RemoteConnectionParameters } from '../remote-browser-pool.js';
|
|
6
7
|
import type { SafeParameters } from '../utils.js';
|
|
7
8
|
import { PlaywrightController } from './playwright-controller.js';
|
|
8
9
|
export declare class PlaywrightPlugin extends BrowserPlugin<BrowserType, SafeParameters<BrowserType['launch']>[0], PlaywrightBrowser> {
|
|
9
10
|
private _browserVersion?;
|
|
10
11
|
_containerProxyServer?: Awaited<ReturnType<typeof createProxyServerForContainers>>;
|
|
12
|
+
/**
|
|
13
|
+
* Playwright remote connections only support incognito pages — `connect()` / `connectOverCDP()` don't
|
|
14
|
+
* accept persistent contexts. Force it on (and inform the user) when wired for a remote connection.
|
|
15
|
+
*/
|
|
16
|
+
useRemoteConnection(connection: RemoteConnection, parameters?: RemoteConnectionParameters): void;
|
|
11
17
|
protected _launch(launchContext: LaunchContext<BrowserType>): Promise<PlaywrightBrowser>;
|
|
12
18
|
private _throwOnFailedLaunch;
|
|
13
19
|
createController(): PlaywrightController;
|
|
14
20
|
protected _addProxyToLaunchOptions(launchContext: LaunchContext<BrowserType>): Promise<void>;
|
|
15
21
|
protected _isChromiumBasedBrowser(): boolean;
|
|
16
22
|
}
|
|
17
|
-
//# sourceMappingURL=playwright-plugin.d.ts.map
|
|
@@ -1,14 +1,36 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import { BrowserPlugin } from '../abstract-classes/browser-plugin.js';
|
|
3
3
|
import { anonymizeProxySugar } from '../anonymize-proxy.js';
|
|
4
|
-
import { log } from '../logger.js';
|
|
5
4
|
import { getLocalProxyAddress } from '../proxy-server.js';
|
|
6
5
|
import { PlaywrightBrowser as PlaywrightBrowserWithPersistentContext } from './playwright-browser.js';
|
|
7
6
|
import { PlaywrightController } from './playwright-controller.js';
|
|
8
7
|
export class PlaywrightPlugin extends BrowserPlugin {
|
|
9
8
|
_browserVersion;
|
|
10
9
|
_containerProxyServer;
|
|
10
|
+
/**
|
|
11
|
+
* Playwright remote connections only support incognito pages — `connect()` / `connectOverCDP()` don't
|
|
12
|
+
* accept persistent contexts. Force it on (and inform the user) when wired for a remote connection.
|
|
13
|
+
*/
|
|
14
|
+
useRemoteConnection(connection, parameters = {}) {
|
|
15
|
+
super.useRemoteConnection(connection, parameters);
|
|
16
|
+
if (!this.useIncognitoPages) {
|
|
17
|
+
this.log.info('Remote Playwright connection — useIncognitoPages forced to true. ' +
|
|
18
|
+
'Pages will not share cookies/storage between each other; use the SessionPool for shared state.');
|
|
19
|
+
}
|
|
20
|
+
this.useIncognitoPages = true;
|
|
21
|
+
}
|
|
11
22
|
async _launch(launchContext) {
|
|
23
|
+
if (this.remoteConnection) {
|
|
24
|
+
return this._connectToRemoteBrowser(launchContext, async (url) => {
|
|
25
|
+
const connectOptions = (this.remoteConnectionParameters?.connectOptions ?? {});
|
|
26
|
+
if (this.remoteConnectionParameters?.protocol === 'playwright') {
|
|
27
|
+
this.log.info('Connecting to remote browser via connect (Playwright WebSocket).');
|
|
28
|
+
return this.library.connect(url, connectOptions);
|
|
29
|
+
}
|
|
30
|
+
this.log.info('Connecting to remote browser via connectOverCDP.');
|
|
31
|
+
return this.library.connectOverCDP(url, connectOptions);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
12
34
|
const { launchOptions, useIncognitoPages, userDataDir, proxyUrl } = launchContext;
|
|
13
35
|
let browser;
|
|
14
36
|
// Required for the `proxy` context option to work.
|
|
@@ -20,7 +42,9 @@ export class PlaywrightPlugin extends BrowserPlugin {
|
|
|
20
42
|
if (this.library.name() === 'webkit') {
|
|
21
43
|
launchOptions.args = launchOptions.args?.filter((arg) => arg !== '--no-sandbox');
|
|
22
44
|
}
|
|
23
|
-
const [anonymizedProxyUrl, close] = await anonymizeProxySugar(proxyUrl
|
|
45
|
+
const [anonymizedProxyUrl, close] = await anonymizeProxySugar(proxyUrl, undefined, undefined, {
|
|
46
|
+
ignoreProxyCertificate: launchContext.ignoreProxyCertificate,
|
|
47
|
+
});
|
|
24
48
|
if (anonymizedProxyUrl) {
|
|
25
49
|
launchOptions.proxy = {
|
|
26
50
|
server: anonymizedProxyUrl,
|
|
@@ -62,7 +86,7 @@ export class PlaywrightPlugin extends BrowserPlugin {
|
|
|
62
86
|
const inactiveBrowser = await this.library.launch(launchOptions);
|
|
63
87
|
this._browserVersion = inactiveBrowser.version();
|
|
64
88
|
inactiveBrowser.close().catch((error) => {
|
|
65
|
-
log.exception(error, 'Failed to close browser.');
|
|
89
|
+
this.log.exception(error, 'Failed to close browser.');
|
|
66
90
|
});
|
|
67
91
|
}
|
|
68
92
|
browser = new PlaywrightBrowserWithPersistentContext({
|
|
@@ -100,4 +124,3 @@ export class PlaywrightPlugin extends BrowserPlugin {
|
|
|
100
124
|
return name === 'chromium';
|
|
101
125
|
}
|
|
102
126
|
}
|
|
103
|
-
//# sourceMappingURL=playwright-plugin.js.map
|
package/proxy-server.d.ts
CHANGED
package/proxy-server.js
CHANGED
|
@@ -16,4 +16,3 @@ export declare class PuppeteerController extends BrowserController<typeof Puppet
|
|
|
16
16
|
protected _getCookies(page: PuppeteerTypes.Page): Promise<Cookie[]>;
|
|
17
17
|
protected _setCookies(page: PuppeteerTypes.Page, cookies: Cookie[]): Promise<void>;
|
|
18
18
|
}
|
|
19
|
-
//# sourceMappingURL=puppeteer-controller.d.ts.map
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { tryCancel } from '@apify/timeout';
|
|
2
2
|
import { BrowserController } from '../abstract-classes/browser-controller.js';
|
|
3
3
|
import { anonymizeProxySugar } from '../anonymize-proxy.js';
|
|
4
|
-
import { log } from '../logger.js';
|
|
5
4
|
const PROCESS_KILL_TIMEOUT_MILLIS = 5000;
|
|
6
5
|
export class PuppeteerController extends BrowserController {
|
|
7
6
|
normalizeProxyOptions(proxyUrl, pageOptions) {
|
|
@@ -12,7 +11,7 @@ export class PuppeteerController extends BrowserController {
|
|
|
12
11
|
const username = decodeURIComponent(url.username);
|
|
13
12
|
const password = decodeURIComponent(url.password);
|
|
14
13
|
return {
|
|
15
|
-
proxyServer: url.
|
|
14
|
+
proxyServer: `${url.protocol}//${url.host}`,
|
|
16
15
|
proxyUsername: username,
|
|
17
16
|
proxyPassword: password,
|
|
18
17
|
proxyBypassList: pageOptions?.proxyBypassList,
|
|
@@ -25,7 +24,7 @@ export class PuppeteerController extends BrowserController {
|
|
|
25
24
|
}
|
|
26
25
|
let close = async () => { };
|
|
27
26
|
if (contextOptions.proxyServer) {
|
|
28
|
-
const [anonymizedProxyUrl, closeProxy] = await anonymizeProxySugar(contextOptions.proxyServer, contextOptions.proxyUsername, contextOptions.proxyPassword);
|
|
27
|
+
const [anonymizedProxyUrl, closeProxy] = await anonymizeProxySugar(contextOptions.proxyServer, contextOptions.proxyUsername, contextOptions.proxyPassword, { ignoreProxyCertificate: this.launchContext.ignoreProxyCertificate });
|
|
29
28
|
if (anonymizedProxyUrl) {
|
|
30
29
|
contextOptions.proxyServer = anonymizedProxyUrl;
|
|
31
30
|
delete contextOptions.proxyUsername;
|
|
@@ -58,7 +57,7 @@ export class PuppeteerController extends BrowserController {
|
|
|
58
57
|
await context.close();
|
|
59
58
|
}
|
|
60
59
|
catch (error) {
|
|
61
|
-
log.exception(error, 'Failed to close context.');
|
|
60
|
+
this.log.exception(error, 'Failed to close context.');
|
|
62
61
|
}
|
|
63
62
|
finally {
|
|
64
63
|
await close();
|
|
@@ -84,7 +83,7 @@ export class PuppeteerController extends BrowserController {
|
|
|
84
83
|
async _kill() {
|
|
85
84
|
const browserProcess = this.browser.process();
|
|
86
85
|
if (!browserProcess) {
|
|
87
|
-
log.debug('Browser was connected using the `puppeteer.connect` method no browser to kill.');
|
|
86
|
+
this.log.debug('Browser was connected using the `puppeteer.connect` method no browser to kill.');
|
|
88
87
|
return;
|
|
89
88
|
}
|
|
90
89
|
const timeout = setTimeout(() => {
|
|
@@ -98,14 +97,22 @@ export class PuppeteerController extends BrowserController {
|
|
|
98
97
|
clearTimeout(timeout);
|
|
99
98
|
}
|
|
100
99
|
catch (error) {
|
|
101
|
-
log.debug('Browser was already killed.', { error });
|
|
100
|
+
this.log.debug('Browser was already killed.', { error });
|
|
102
101
|
}
|
|
103
102
|
}
|
|
104
103
|
async _getCookies(page) {
|
|
105
|
-
|
|
104
|
+
const cookies = await page.browserContext().cookies();
|
|
105
|
+
// Puppeteer 25+ can report `sameSite: 'Default'`, which means the attribute is unspecified.
|
|
106
|
+
return cookies.map(({ sameSite, ...rest }) => ({
|
|
107
|
+
...rest,
|
|
108
|
+
sameSite: sameSite === 'Default' ? undefined : sameSite,
|
|
109
|
+
}));
|
|
106
110
|
}
|
|
107
111
|
async _setCookies(page, cookies) {
|
|
108
|
-
|
|
112
|
+
// BrowserContext.setCookie requires `url` or `domain`; the page-level API used to back-fill
|
|
113
|
+
// the page's current URL for us. Replicate that so callers who pass neither don't get rejected.
|
|
114
|
+
const pageUrl = page.url();
|
|
115
|
+
const normalized = cookies.map((cookie) => cookie.url || cookie.domain ? cookie : { ...cookie, url: pageUrl });
|
|
116
|
+
return page.browserContext().setCookie(...normalized);
|
|
109
117
|
}
|
|
110
118
|
}
|
|
111
|
-
//# sourceMappingURL=puppeteer-controller.js.map
|
|
@@ -4,12 +4,14 @@ import type Puppeteer from 'puppeteer';
|
|
|
4
4
|
import type * as PuppeteerTypes from 'puppeteer';
|
|
5
5
|
import { BrowserPlugin } from '../abstract-classes/browser-plugin.js';
|
|
6
6
|
import type { LaunchContext } from '../launch-context.js';
|
|
7
|
+
import type { RemoteConnection, RemoteConnectionParameters } from '../remote-browser-pool.js';
|
|
7
8
|
import type { PuppeteerNewPageOptions } from './puppeteer-controller.js';
|
|
8
9
|
import { PuppeteerController } from './puppeteer-controller.js';
|
|
9
10
|
export declare class PuppeteerPlugin extends BrowserPlugin<typeof Puppeteer, PuppeteerTypes.LaunchOptions, PuppeteerTypes.Browser, PuppeteerNewPageOptions> {
|
|
11
|
+
/** Pages share cookies/storage on the remote browser (Puppeteer defaults to non-incognito). */
|
|
12
|
+
useRemoteConnection(connection: RemoteConnection, parameters?: RemoteConnectionParameters): void;
|
|
10
13
|
protected _launch(launchContext: LaunchContext<typeof Puppeteer, PuppeteerTypes.LaunchOptions, PuppeteerTypes.Browser, PuppeteerNewPageOptions>): Promise<PuppeteerTypes.Browser>;
|
|
11
14
|
createController(): PuppeteerController;
|
|
12
15
|
protected _addProxyToLaunchOptions(_launchContext: LaunchContext<typeof Puppeteer, PuppeteerTypes.LaunchOptions, PuppeteerTypes.Browser, PuppeteerNewPageOptions>): Promise<void>;
|
|
13
16
|
protected _isChromiumBasedBrowser(_launchContext: LaunchContext<typeof Puppeteer, PuppeteerTypes.LaunchOptions, PuppeteerTypes.Browser, PuppeteerNewPageOptions>): boolean;
|
|
14
17
|
}
|
|
15
|
-
//# sourceMappingURL=puppeteer-plugin.d.ts.map
|
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
import { readFile } from 'node:fs/promises';
|
|
2
2
|
import { BrowserPlugin } from '../abstract-classes/browser-plugin.js';
|
|
3
3
|
import { anonymizeProxySugar } from '../anonymize-proxy.js';
|
|
4
|
-
import { log } from '../logger.js';
|
|
5
4
|
import { noop } from '../utils.js';
|
|
6
5
|
import { PuppeteerController } from './puppeteer-controller.js';
|
|
7
6
|
const PROXY_SERVER_ARG = '--proxy-server=';
|
|
8
7
|
export class PuppeteerPlugin extends BrowserPlugin {
|
|
8
|
+
/** Pages share cookies/storage on the remote browser (Puppeteer defaults to non-incognito). */
|
|
9
|
+
useRemoteConnection(connection, parameters = {}) {
|
|
10
|
+
super.useRemoteConnection(connection, parameters);
|
|
11
|
+
if (!this.useIncognitoPages) {
|
|
12
|
+
this.log.info('Remote Puppeteer connection — pages will share cookies and storage on the remote ' +
|
|
13
|
+
'browser instance (useIncognitoPages defaults to false).');
|
|
14
|
+
}
|
|
15
|
+
}
|
|
9
16
|
async _launch(launchContext) {
|
|
10
17
|
let oldPuppeteerVersion = false;
|
|
11
18
|
try {
|
|
@@ -17,58 +24,80 @@ export class PuppeteerPlugin extends BrowserPlugin {
|
|
|
17
24
|
catch {
|
|
18
25
|
// ignore
|
|
19
26
|
}
|
|
20
|
-
const {
|
|
21
|
-
launchOptions.userDataDir = launchOptions.userDataDir ?? userDataDir;
|
|
22
|
-
if (launchOptions.headless === false) {
|
|
23
|
-
if (Array.isArray(launchOptions.args)) {
|
|
24
|
-
launchOptions.args.push('--disable-site-isolation-trials');
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
launchOptions.args = ['--disable-site-isolation-trials'];
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
if (launchOptions.headless === true && oldPuppeteerVersion) {
|
|
31
|
-
launchOptions.headless = 'new';
|
|
32
|
-
}
|
|
27
|
+
const { useIncognitoPages, proxyUrl, ignoreProxyCertificate } = launchContext;
|
|
33
28
|
let browser;
|
|
34
|
-
{
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
29
|
+
if (this.remoteConnection) {
|
|
30
|
+
browser = await this._connectToRemoteBrowser(launchContext, async (url) => {
|
|
31
|
+
const connectOptions = this.remoteConnectionParameters?.connectOptions ?? {};
|
|
32
|
+
this.log.info('Connecting to remote browser via connect (CDP).');
|
|
33
|
+
return this.library.connect({ ...connectOptions, browserWSEndpoint: url });
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
const { launchOptions, userDataDir, experimentalContainers } = launchContext;
|
|
38
|
+
if (experimentalContainers) {
|
|
39
|
+
throw new Error('Experimental containers are only available with Playwright');
|
|
40
|
+
}
|
|
41
|
+
launchOptions.userDataDir = launchOptions.userDataDir ?? userDataDir;
|
|
42
|
+
if (launchOptions.headless === false) {
|
|
38
43
|
if (Array.isArray(launchOptions.args)) {
|
|
39
|
-
launchOptions.args.push(
|
|
44
|
+
launchOptions.args.push('--disable-site-isolation-trials');
|
|
40
45
|
}
|
|
41
46
|
else {
|
|
42
|
-
launchOptions.args = [
|
|
47
|
+
launchOptions.args = ['--disable-site-isolation-trials'];
|
|
43
48
|
}
|
|
44
49
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
if (anonymizedProxyUrl) {
|
|
48
|
-
browser.on('disconnected', async () => {
|
|
49
|
-
await close();
|
|
50
|
-
});
|
|
51
|
-
}
|
|
50
|
+
if (launchOptions.headless === true && oldPuppeteerVersion) {
|
|
51
|
+
launchOptions.headless = 'new';
|
|
52
52
|
}
|
|
53
|
-
|
|
54
|
-
await
|
|
55
|
-
|
|
53
|
+
{
|
|
54
|
+
const [anonymizedProxyUrl, close] = await anonymizeProxySugar(proxyUrl, undefined, undefined, {
|
|
55
|
+
ignoreProxyCertificate: launchContext.ignoreProxyCertificate,
|
|
56
|
+
});
|
|
57
|
+
if (proxyUrl) {
|
|
58
|
+
const proxyArg = `${PROXY_SERVER_ARG}${anonymizedProxyUrl ?? proxyUrl}`;
|
|
59
|
+
if (Array.isArray(launchOptions.args)) {
|
|
60
|
+
launchOptions.args.push(proxyArg);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
launchOptions.args = [proxyArg];
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
browser = await this.library.launch(launchOptions);
|
|
68
|
+
if (anonymizedProxyUrl) {
|
|
69
|
+
browser.on('disconnected', async () => {
|
|
70
|
+
await close();
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
await close();
|
|
76
|
+
this._throwAugmentedLaunchError(error, launchContext.launchOptions?.executablePath, '`apify/actor-node-puppeteer-chrome`', "Try installing a browser, if it's missing, by running `npx @puppeteer/browsers install chromium --path [path]` and pointing `executablePath` to the downloaded executable (https://pptr.dev/browsers-api)");
|
|
77
|
+
}
|
|
56
78
|
}
|
|
57
79
|
}
|
|
58
|
-
|
|
80
|
+
const targetCreatedHandler = async (target) => {
|
|
59
81
|
try {
|
|
60
82
|
const page = await target.page();
|
|
61
83
|
if (page) {
|
|
62
84
|
page.on('error', (error) => {
|
|
63
|
-
log.exception(error, 'Page crashed.');
|
|
85
|
+
this.log.exception(error, 'Page crashed.');
|
|
64
86
|
page.close().catch(noop);
|
|
65
87
|
});
|
|
66
88
|
}
|
|
67
89
|
}
|
|
68
90
|
catch (error) {
|
|
69
|
-
log.exception(error, 'Failed to retrieve page from target.');
|
|
91
|
+
this.log.exception(error, 'Failed to retrieve page from target.');
|
|
70
92
|
}
|
|
71
|
-
}
|
|
93
|
+
};
|
|
94
|
+
browser.on('targetcreated', targetCreatedHandler);
|
|
95
|
+
// Clean up the listener when a remote browser disconnects to prevent leaks
|
|
96
|
+
if (this.remoteConnection) {
|
|
97
|
+
browser.once('disconnected', () => {
|
|
98
|
+
browser.off('targetcreated', targetCreatedHandler);
|
|
99
|
+
});
|
|
100
|
+
}
|
|
72
101
|
const boundMethods = [
|
|
73
102
|
'newPage',
|
|
74
103
|
'close',
|
|
@@ -78,6 +107,7 @@ export class PuppeteerPlugin extends BrowserPlugin {
|
|
|
78
107
|
'version',
|
|
79
108
|
'on',
|
|
80
109
|
'process',
|
|
110
|
+
'pages',
|
|
81
111
|
].reduce((map, method) => {
|
|
82
112
|
map[method] = browser[method]?.bind(browser);
|
|
83
113
|
return map;
|
|
@@ -89,22 +119,30 @@ export class PuppeteerPlugin extends BrowserPlugin {
|
|
|
89
119
|
return async (...args) => {
|
|
90
120
|
let page;
|
|
91
121
|
if (useIncognitoPages) {
|
|
92
|
-
|
|
122
|
+
// Skip proxy setup for remote connections — proxy is managed by the remote service.
|
|
123
|
+
const effectiveProxyUrl = this.remoteConnection ? undefined : proxyUrl;
|
|
124
|
+
const [anonymizedProxyUrl, close] = effectiveProxyUrl
|
|
125
|
+
? await anonymizeProxySugar(effectiveProxyUrl, undefined, undefined, {
|
|
126
|
+
ignoreProxyCertificate,
|
|
127
|
+
})
|
|
128
|
+
: [undefined, noop];
|
|
129
|
+
const proxyServer = anonymizedProxyUrl ?? effectiveProxyUrl;
|
|
130
|
+
const contextOptions = proxyServer ? { proxyServer } : {};
|
|
131
|
+
const context = (await browser[method](contextOptions));
|
|
93
132
|
try {
|
|
94
|
-
const context = (await browser[method]({
|
|
95
|
-
proxyServer: anonymizedProxyUrl ?? proxyUrl,
|
|
96
|
-
}));
|
|
97
133
|
page = await context.newPage(...args);
|
|
98
|
-
if (anonymizedProxyUrl) {
|
|
99
|
-
page.on('close', async () => {
|
|
100
|
-
await close();
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
134
|
}
|
|
104
135
|
catch (error) {
|
|
136
|
+
await context.close().catch(noop);
|
|
105
137
|
await close();
|
|
106
138
|
throw error;
|
|
107
139
|
}
|
|
140
|
+
page.once('close', async () => {
|
|
141
|
+
if (anonymizedProxyUrl) {
|
|
142
|
+
await close();
|
|
143
|
+
}
|
|
144
|
+
await context.close().catch(noop);
|
|
145
|
+
});
|
|
108
146
|
}
|
|
109
147
|
else {
|
|
110
148
|
page = await boundMethods.newPage(...args);
|
|
@@ -170,4 +208,3 @@ export class PuppeteerPlugin extends BrowserPlugin {
|
|
|
170
208
|
return true;
|
|
171
209
|
}
|
|
172
210
|
}
|
|
173
|
-
//# sourceMappingURL=puppeteer-plugin.js.map
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import type { IBrowserPool, NewPageOptions, PageState } from '@crawlee/types';
|
|
2
|
+
import type { BrowserPlugin } from './abstract-classes/browser-plugin.js';
|
|
3
|
+
import { BrowserPool } from './browser-pool.js';
|
|
4
|
+
import type { BrowserPoolHooks, BrowserPoolOptions } from './browser-pool.js';
|
|
5
|
+
import { RemoteBrowserProvider } from './remote-browser-provider.js';
|
|
6
|
+
/**
|
|
7
|
+
* The result of resolving a remote browser endpoint: the URL to connect to plus an optional opaque
|
|
8
|
+
* `context` object that is handed back to `release`.
|
|
9
|
+
*/
|
|
10
|
+
export interface ResolvedRemoteEndpoint {
|
|
11
|
+
/** The browser endpoint URL to connect to. */
|
|
12
|
+
url: string;
|
|
13
|
+
/** Opaque metadata passed back to `release()` — e.g. session IDs, API tokens. */
|
|
14
|
+
context?: Record<string, unknown>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* A remote browser endpoint: either a static URL string, or a function called once per browser launch
|
|
18
|
+
* that returns a URL (optionally with a `context` for `release`).
|
|
19
|
+
*
|
|
20
|
+
* The function receives the `proxyUrl` resolved by Crawlee's proxy configuration for the launch, so it
|
|
21
|
+
* can forward it to the remote service's proxy API.
|
|
22
|
+
*/
|
|
23
|
+
export type RemoteBrowserEndpoint = string | ((options?: {
|
|
24
|
+
proxyUrl?: string;
|
|
25
|
+
}) => string | ResolvedRemoteEndpoint | Promise<string | ResolvedRemoteEndpoint>);
|
|
26
|
+
/**
|
|
27
|
+
* The bridge a {@link RemoteBrowserPool} injects into a {@link BrowserPlugin} so the plugin can
|
|
28
|
+
* connect to a remote browser without owning any remote-session policy.
|
|
29
|
+
*
|
|
30
|
+
* The plugin only knows how to make the library-specific `connect()` call; everything else — resolving
|
|
31
|
+
* the endpoint, calling the user's `release()`, and guaranteeing release fires at most once — lives in
|
|
32
|
+
* the pool. The plugin calls {@link RemoteConnection.resolve|resolve} before connecting, stores the
|
|
33
|
+
* returned `token` on its launch context, and the controller later calls
|
|
34
|
+
* {@link RemoteConnection.release|release} with that token when the browser closes.
|
|
35
|
+
*
|
|
36
|
+
* @internal
|
|
37
|
+
*/
|
|
38
|
+
export interface RemoteConnection {
|
|
39
|
+
/** Resolves the endpoint for a single browser launch. The `token` identifies the session for release. */
|
|
40
|
+
resolve(options?: {
|
|
41
|
+
proxyUrl?: string;
|
|
42
|
+
}): Promise<{
|
|
43
|
+
url: string;
|
|
44
|
+
token: number;
|
|
45
|
+
}>;
|
|
46
|
+
/** Releases the remote session for `token`. Idempotent — safe to call from both `close()` and `kill()`. */
|
|
47
|
+
release(token: number): Promise<void>;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Per-plugin remote connection parameters, passed to {@link BrowserPlugin.useRemoteConnection}.
|
|
51
|
+
* The endpoint is supplied per-launch via {@link RemoteConnection}; these are the static connect()
|
|
52
|
+
* parameters (protocol, headers, timeouts, …).
|
|
53
|
+
*/
|
|
54
|
+
export interface RemoteConnectionParameters {
|
|
55
|
+
/**
|
|
56
|
+
* Playwright only: which protocol to connect with. `'cdp'` uses `connectOverCDP()` (the default),
|
|
57
|
+
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
58
|
+
* `'playwright'` uses `connect()` (Playwright's own WebSocket protocol). Ignored by Puppeteer.
|
|
59
|
+
*/
|
|
60
|
+
protocol?: 'cdp' | 'playwright';
|
|
61
|
+
/** Extra options forwarded to the library `connect()` / `connectOverCDP()` call (endpoint excluded). */
|
|
62
|
+
connectOptions?: Record<string, unknown>;
|
|
63
|
+
}
|
|
64
|
+
export interface RemoteBrowserPoolOptions {
|
|
65
|
+
/**
|
|
66
|
+
* The browser plugin(s) used to connect to the remote service — e.g. `new PlaywrightPlugin(playwright.chromium)`
|
|
67
|
+
* or `new PuppeteerPlugin(puppeteer)`. The pool configures them for remote connection; do not set a local
|
|
68
|
+
* `launchOptions` on them.
|
|
69
|
+
*/
|
|
70
|
+
browserPlugins: BrowserPlugin[];
|
|
71
|
+
/**
|
|
72
|
+
* The remote browser endpoint: a static URL, a function returning one per launch, or a
|
|
73
|
+
* {@link RemoteBrowserProvider} instance encapsulating a session create/release lifecycle.
|
|
74
|
+
*/
|
|
75
|
+
endpoint: RemoteBrowserEndpoint | RemoteBrowserProvider<any>;
|
|
76
|
+
/**
|
|
77
|
+
* Cleanup callback invoked when a browser closes, crashes, or the pool is destroyed. Receives the
|
|
78
|
+
* `context` returned by a function endpoint. Errors are caught and logged. Ignored when `endpoint`
|
|
79
|
+
* is a {@link RemoteBrowserProvider} (its own `release()` is used instead).
|
|
80
|
+
*/
|
|
81
|
+
release?: (info: {
|
|
82
|
+
endpoint: string;
|
|
83
|
+
context?: Record<string, unknown>;
|
|
84
|
+
}) => unknown;
|
|
85
|
+
/**
|
|
86
|
+
* Maximum number of remote browsers open at once. When reached, {@link RemoteBrowserPool.newPage|newPage}
|
|
87
|
+
* waits for a browser to close before connecting a new one. Set it to your service's concurrent-session limit
|
|
88
|
+
* to avoid `429` errors. Defaults to the {@link RemoteBrowserProvider.maxOpenBrowsers|provider's value}, or
|
|
89
|
+
* `Infinity`.
|
|
90
|
+
*/
|
|
91
|
+
maxOpenBrowsers?: number;
|
|
92
|
+
/** Static connect() parameters (Playwright protocol selection, headers, timeouts, …). */
|
|
93
|
+
connection?: RemoteConnectionParameters;
|
|
94
|
+
/** Extra {@link BrowserPool} options (lifecycle hooks, page limits, fingerprinting, …). */
|
|
95
|
+
browserPoolOptions?: Omit<BrowserPoolOptions, 'browserPlugins'> & BrowserPoolHooks<any, any, any>;
|
|
96
|
+
/** Fallback poll interval (ms) while waiting for a free browser slot. The wait is event-driven; this only bounds it. @default 500 */
|
|
97
|
+
slotPollIntervalMillis?: number;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* The remote-connection configuration a browser crawler accepts on its `remoteBrowser` option. It is the
|
|
101
|
+
* {@link RemoteBrowserPoolOptions} a user supplies *minus* the parts the crawler provides itself — the
|
|
102
|
+
* `browserPlugins` (the crawler builds the correct one for its browser) and `browserPoolOptions` (taken from
|
|
103
|
+
* the crawler's own `browserPoolOptions`). This is what makes the crawler path both terse and mismatch-proof.
|
|
104
|
+
*/
|
|
105
|
+
export type CrawlerRemoteBrowserOptions = Omit<RemoteBrowserPoolOptions, 'browserPlugins' | 'browserPoolOptions'>;
|
|
106
|
+
/**
|
|
107
|
+
* An {@link IBrowserPool} implementation for remote browser services.
|
|
108
|
+
*
|
|
109
|
+
* Unlike configuring a remote browser through a crawler's `launchContext`, this pool is the single owner
|
|
110
|
+
* of all remote-session concerns:
|
|
111
|
+
* - **endpoint resolution** — static URL, per-launch function, or {@link RemoteBrowserProvider};
|
|
112
|
+
* - **release lifecycle** — `release()` fires exactly once per session on close/crash/teardown (no leaks,
|
|
113
|
+
* no double-release);
|
|
114
|
+
* - **concurrency** — {@link RemoteBrowserPoolOptions.maxOpenBrowsers|maxOpenBrowsers} is enforced inside
|
|
115
|
+
* {@link RemoteBrowserPool.newPage|newPage}, which waits for a free slot rather than overshooting.
|
|
116
|
+
*
|
|
117
|
+
* The wrapped {@link BrowserPool} and its plugin only perform the library-specific `connect()` call.
|
|
118
|
+
*
|
|
119
|
+
* Pass an instance as the crawler's `browserPool` option:
|
|
120
|
+
*
|
|
121
|
+
* ```typescript
|
|
122
|
+
* import { PlaywrightPlugin, RemoteBrowserPool } from '@crawlee/browser-pool';
|
|
123
|
+
* import { PlaywrightCrawler } from 'crawlee';
|
|
124
|
+
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
125
|
+
* import playwright from 'playwright';
|
|
126
|
+
*
|
|
127
|
+
* const browserPool = new RemoteBrowserPool({
|
|
128
|
+
* browserPlugins: [new PlaywrightPlugin(playwright.chromium)],
|
|
129
|
+
* endpoint: 'wss://production-sfo.browserless.io?token=xxx',
|
|
130
|
+
* maxOpenBrowsers: 2,
|
|
131
|
+
* });
|
|
132
|
+
*
|
|
133
|
+
* const crawler = new PlaywrightCrawler({ browserPool });
|
|
134
|
+
* ```
|
|
135
|
+
*
|
|
136
|
+
* @category Browser management
|
|
137
|
+
*/
|
|
138
|
+
export declare class RemoteBrowserPool<Page = unknown> implements IBrowserPool<Page> {
|
|
139
|
+
/** The wrapped pool that performs the remote connections and serves pages. */
|
|
140
|
+
readonly browserPool: BrowserPool;
|
|
141
|
+
/** The wrapped pool viewed through the {@link IBrowserPool} contract (the bare type widens pages to `never`). */
|
|
142
|
+
private readonly pool;
|
|
143
|
+
private readonly registry;
|
|
144
|
+
private readonly slotPollIntervalMillis;
|
|
145
|
+
private readonly log;
|
|
146
|
+
/** Shared by all `newPage` callers waiting for a free slot, so they don't each register their own listeners. */
|
|
147
|
+
private _capacityChange?;
|
|
148
|
+
constructor(options: RemoteBrowserPoolOptions);
|
|
149
|
+
/** Maximum number of remote browsers that may be open at the same time. */
|
|
150
|
+
get maxOpenBrowsers(): number;
|
|
151
|
+
set maxOpenBrowsers(value: number);
|
|
152
|
+
/**
|
|
153
|
+
* Opens a new page, waiting first until {@link RemoteBrowserPoolOptions.maxOpenBrowsers|maxOpenBrowsers}
|
|
154
|
+
* allows it (either a new browser slot is free, or an active browser still has page capacity).
|
|
155
|
+
*/
|
|
156
|
+
newPage(options?: NewPageOptions): Promise<Page>;
|
|
157
|
+
closePage(page: Page, options?: {
|
|
158
|
+
error?: Error;
|
|
159
|
+
}): Promise<void>;
|
|
160
|
+
extractPageState(page: Page): Promise<PageState>;
|
|
161
|
+
injectPageState(page: Page, state: PageState): Promise<void>;
|
|
162
|
+
/** Closes all browsers, releases any still-open remote sessions, and tears down the wrapped pool. */
|
|
163
|
+
destroy(): Promise<void>;
|
|
164
|
+
/** Resolves once the wrapped pool can serve another page without exceeding `maxOpenBrowsers`. */
|
|
165
|
+
private _waitForFreeSlot;
|
|
166
|
+
/**
|
|
167
|
+
* Resolves on the next browser-retired / page-closed event, or after `slotPollIntervalMillis`. All
|
|
168
|
+
* concurrently-waiting `newPage` calls share a single promise (and a single pair of event listeners)
|
|
169
|
+
* per tick, so a fleet of saturated callers doesn't fan out into N listener pairs on the pool.
|
|
170
|
+
*/
|
|
171
|
+
private _nextCapacityChange;
|
|
172
|
+
}
|