/
browser-launcher.ts
260 lines (226 loc) · 9.13 KB
/
browser-launcher.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import fs from 'node:fs';
import os from 'node:os';
import { Configuration } from '@crawlee/basic';
import type { BrowserPlugin, BrowserPluginOptions } from '@crawlee/browser-pool';
import type { Dictionary, Constructor } from '@crawlee/utils';
import ow from 'ow';
const DEFAULT_VIEWPORT = {
width: 1366,
height: 768,
};
export interface BrowserLaunchContext<TOptions, Launcher> extends BrowserPluginOptions<TOptions> {
/**
* URL to an HTTP proxy server. It must define the port number,
* and it may also contain proxy username and password.
*
* @example
* `http://bob:pass123@proxy.example.com:1234`.
*/
proxyUrl?: string;
/**
* If `true` and the `executablePath` option of {@apilink BrowserLaunchContext.launchOptions|`launchOptions`} is not set,
* the launcher will launch full Google Chrome browser available on the machine
* rather than the bundled Chromium. The path to Chrome executable
* is taken from the `CRAWLEE_CHROME_EXECUTABLE_PATH` environment variable if provided,
* or defaults to the typical Google Chrome executable location specific for the operating system.
* @default false
*/
useChrome?: boolean;
/**
* If set to `true`, the crawler respects the proxy url generated for the given request.
* This aligns the browser-based crawlers with the `HttpCrawler`.
*
* Might cause performance issues, as Crawlee might launch too many browser instances.
*/
browserPerProxy?: boolean;
/**
* With this option selected, all pages will be opened in a new incognito browser context.
* This means they will not share cookies nor cache and their resources will not be throttled by one another.
* @default false
*/
useIncognitoPages?: boolean;
/**
* @experimental
* Like `useIncognitoPages`, but for persistent contexts, so cache is used for faster loading.
* Works best with Firefox. Unstable on Chromium.
*/
experimentalContainers?: boolean;
/**
* Sets the [User Data Directory](https://chromium.googlesource.com/chromium/src/+/master/docs/user_data_dir.md) path.
* The user data directory contains profile data such as history, bookmarks, and cookies, as well as other per-installation local state.
* If not specified, a temporary directory is used instead.
*/
userDataDir?: string;
/**
* The `User-Agent` HTTP header used by the browser.
* If not provided, the function sets `User-Agent` to a reasonable default
* to reduce the chance of detection of the crawler.
*/
userAgent?: string;
/**
* The type of browser to be launched.
* By default, `chromium` is used. Other browsers like `webkit` or `firefox` can be used.
*
* @example
* ```ts
* // import the browser from the library first
* import { firefox } from 'playwright';
* ```
*
* For more details, check out the [example](https://crawlee.dev/docs/examples/playwright-crawler-firefox).
*/
launcher?: Launcher;
}
/**
* Abstract class for creating browser launchers, such as `PlaywrightLauncher` and `PuppeteerLauncher`.
* @ignore
*/
export abstract class BrowserLauncher<
Plugin extends BrowserPlugin,
Launcher = Plugin['library'],
T extends Constructor<Plugin> = Constructor<Plugin>,
LaunchOptions extends Dictionary<any> | undefined = Partial<Parameters<Plugin['launch']>[0]>,
LaunchResult extends ReturnType<Plugin['launch']> = ReturnType<Plugin['launch']>,
> {
launcher: Launcher;
proxyUrl?: string;
useChrome?: boolean;
launchOptions: Dictionary;
otherLaunchContextProps: Dictionary;
// to be provided by child classes;
Plugin!: T;
userAgent?: string;
protected static optionsShape = {
proxyUrl: ow.optional.string.url,
useChrome: ow.optional.boolean,
useIncognitoPages: ow.optional.boolean,
browserPerProxy: ow.optional.boolean,
experimentalContainers: ow.optional.boolean,
userDataDir: ow.optional.string,
launchOptions: ow.optional.object,
userAgent: ow.optional.string,
};
static requireLauncherOrThrow<T>(launcher: string, apifyImageName: string): T {
try {
return require(launcher); // eslint-disable-line
} catch (err) {
const e = err as Error & { code: string };
if (e.code === 'MODULE_NOT_FOUND') {
const msg = `Cannot find module '${launcher}'. Did you you install the '${launcher}' package?\n`
+ `Make sure you have '${launcher}' in your package.json dependencies and in your package-lock.json, if you use it.`;
if (process.env.APIFY_IS_AT_HOME) {
e.message = `${msg}\nOn the Apify platform, '${launcher}' can only be used with the ${apifyImageName} Docker image.`;
}
}
throw err;
}
}
/**
* All `BrowserLauncher` parameters are passed via an launchContext object.
*/
constructor(launchContext: BrowserLaunchContext<LaunchOptions, Launcher>, readonly config = Configuration.getGlobalConfig()) {
const {
launcher,
proxyUrl,
useChrome,
userAgent,
launchOptions = {},
...otherLaunchContextProps
} = launchContext;
this._validateProxyUrlProtocol(proxyUrl);
// those need to be reassigned otherwise they are {} in types
this.launcher = launcher!;
this.proxyUrl = proxyUrl;
this.useChrome = useChrome;
this.userAgent = userAgent;
this.launchOptions = launchOptions;
this.otherLaunchContextProps = otherLaunchContextProps as Dictionary;
}
/**
* @ignore
*/
createBrowserPlugin(): Plugin {
return new this.Plugin(this.launcher, {
proxyUrl: this.proxyUrl,
launchOptions: this.createLaunchOptions(),
...this.otherLaunchContextProps,
});
}
/**
* Launches a browser instance based on the plugin.
* @returns Browser instance.
*/
launch(): LaunchResult {
const plugin = this.createBrowserPlugin();
const context = plugin.createLaunchContext();
return plugin.launch(context) as LaunchResult;
}
createLaunchOptions(): Dictionary {
const launchOptions: { args: string[] } & Dictionary = {
args: [],
defaultViewport: DEFAULT_VIEWPORT,
...this.launchOptions,
};
if (this.config.get('disableBrowserSandbox')) {
launchOptions.args.push('--no-sandbox');
}
if (this.userAgent) {
launchOptions.args.push(`--user-agent=${this.userAgent}`);
}
if (launchOptions.headless == null) {
launchOptions.headless = this._getDefaultHeadlessOption();
}
if (this.useChrome && !launchOptions.executablePath) {
launchOptions.executablePath = this._getChromeExecutablePath();
}
return launchOptions;
}
protected _getDefaultHeadlessOption(): boolean {
return this.config.get('headless')! && !this.config.get('xvfb', false);
}
protected _getChromeExecutablePath(): string {
return this.config.get('chromeExecutablePath', this._getTypicalChromeExecutablePath());
}
/**
* Gets a typical path to Chrome executable, depending on the current operating system.
*/
protected _getTypicalChromeExecutablePath(): string {
/**
* Returns path of Chrome executable by its OS environment variable to deal with non-english language OS.
* Taking also into account the old [chrome 380177 issue](https://bugs.chromium.org/p/chromium/issues/detail?id=380177).
*
* @ignore
*/
const getWin32Path = () => {
let chromeExecutablePath = 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe';
const path00 = `${process.env.ProgramFiles}\\Google\\Chrome\\Application\\chrome.exe`;
const path86 = `${process.env['ProgramFiles(x86)']}\\Google\\Chrome\\Application\\chrome.exe`;
if (fs.existsSync(path00)) {
chromeExecutablePath = path00;
} else if (fs.existsSync(path86)) {
chromeExecutablePath = path86;
}
return chromeExecutablePath;
};
switch (os.platform()) {
case 'darwin':
return '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome';
case 'win32':
return getWin32Path();
default:
return '/usr/bin/google-chrome';
}
}
protected _validateProxyUrlProtocol(proxyUrl?: string): void {
if (!proxyUrl) return;
if (!/^(http|https|socks4|socks5)/i.test(proxyUrl)) {
throw new Error(`Invalid "proxyUrl". Unsupported protocol: ${proxyUrl}.`);
}
const url = new URL(proxyUrl);
if (url.username || url.password) {
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
throw new Error('Invalid "proxyUrl" option: authentication is only supported for HTTP proxy type.');
}
}
}
}