-
Notifications
You must be signed in to change notification settings - Fork 803
/
Copy pathbrowser-plugin.ts
311 lines (268 loc) · 11 KB
/
browser-plugin.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import { CriticalError } from '@crawlee/core';
import type { Dictionary } from '@crawlee/types';
import merge from 'lodash.merge';
import type { BrowserController } from './browser-controller';
import type { LaunchContextOptions } from '../launch-context';
import { LaunchContext } from '../launch-context';
import type { UnwrapPromise } from '../utils';
/**
* The default User Agent used by `PlaywrightCrawler`, `launchPlaywright`, 'PuppeteerCrawler' and 'launchPuppeteer'
* when Chromium/Chrome browser is launched:
* - in headless mode,
* - without using a fingerprint,
* - without specifying a user agent.
* Last updated on 2022-05-05.
*
* After you update it here, please update it also in jsdom-crawler.ts
*/
export const DEFAULT_USER_AGENT =
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36';
/**
* Each plugin expects an instance of the object with the `.launch()` property.
* For Puppeteer, it is the `puppeteer` module itself, whereas for Playwright
* it is one of the browser types, such as `puppeteer.chromium`.
* `BrowserPlugin` does not include the library. You can choose any version
* or fork of the library. It also keeps `browser-pool` installation small.
*/
export interface CommonLibrary {
product?: string;
launch(opts?: Dictionary): Promise<CommonBrowser>;
name?: () => string;
}
/** @internal */
export interface CommonBrowser {
newPage(...args: unknown[]): Promise<CommonPage>;
}
/** @internal */
export interface CommonPage {
close(...args: unknown[]): Promise<unknown>;
url(): string | Promise<string>;
}
export interface BrowserPluginOptions<LibraryOptions> {
/**
* Options that will be passed down to the automation library. E.g.
* `puppeteer.launch(launchOptions);`. This is a good place to set
* options that you want to apply as defaults. To dynamically override
* those options per-browser, see the `preLaunchHooks` of {@apilink BrowserPool}.
*/
launchOptions?: LibraryOptions;
/**
* Automation libraries configure proxies differently. This helper allows you
* to set a proxy URL without worrying about specific implementations.
* It also allows you use an authenticated proxy without extra code.
*/
proxyUrl?: string;
/**
* By default pages share the same browser context.
* If set to true each page uses its own context that is destroyed once the page is closed or crashes.
*
* @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;
/**
* Path to a User Data Directory, which stores browser session data like cookies and local storage.
*/
userDataDir?: string;
/**
* 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;
}
export interface CreateLaunchContextOptions<
Library extends CommonLibrary,
LibraryOptions extends Dictionary | undefined = Parameters<Library['launch']>[0],
LaunchResult extends CommonBrowser = UnwrapPromise<ReturnType<Library['launch']>>,
NewPageOptions = Parameters<LaunchResult['newPage']>[0],
NewPageResult = UnwrapPromise<ReturnType<LaunchResult['newPage']>>,
> extends Partial<
Omit<
LaunchContextOptions<Library, LibraryOptions, LaunchResult, NewPageOptions, NewPageResult>,
'browserPlugin'
>
> {}
/**
* The `BrowserPlugin` serves two purposes. First, it is the base class that
* specialized controllers like `PuppeteerPlugin` or `PlaywrightPlugin` extend.
* Second, it allows the user to configure the automation libraries and
* feed them to {@apilink BrowserPool} for use.
*/
export abstract class BrowserPlugin<
Library extends CommonLibrary = CommonLibrary,
LibraryOptions extends Dictionary | undefined = Parameters<Library['launch']>[0],
LaunchResult extends CommonBrowser = UnwrapPromise<ReturnType<Library['launch']>>,
NewPageOptions = Parameters<LaunchResult['newPage']>[0],
NewPageResult = UnwrapPromise<ReturnType<LaunchResult['newPage']>>,
> {
name = this.constructor.name;
library: Library;
launchOptions: LibraryOptions;
proxyUrl?: string;
userDataDir?: string;
useIncognitoPages: boolean;
experimentalContainers: boolean;
browserPerProxy?: boolean;
constructor(library: Library, options: BrowserPluginOptions<LibraryOptions> = {}) {
const {
launchOptions = {} as LibraryOptions,
proxyUrl,
userDataDir,
useIncognitoPages = false,
experimentalContainers = false,
browserPerProxy = false,
} = options;
this.library = library;
this.launchOptions = launchOptions;
this.proxyUrl = proxyUrl && new URL(proxyUrl).href.slice(0, -1);
this.userDataDir = userDataDir;
this.useIncognitoPages = useIncognitoPages;
this.experimentalContainers = experimentalContainers;
this.browserPerProxy = browserPerProxy;
}
/**
* Creates a `LaunchContext` with all the information needed
* to launch a browser. Aside from library specific launch options,
* it also includes internal properties used by `BrowserPool` for
* management of the pool and extra features.
*/
createLaunchContext(
options: CreateLaunchContextOptions<Library, LibraryOptions, LaunchResult, NewPageOptions, NewPageResult> = {},
): LaunchContext<Library, LibraryOptions, LaunchResult, NewPageOptions, NewPageResult> {
const {
id,
launchOptions = {},
proxyUrl = this.proxyUrl,
useIncognitoPages = this.useIncognitoPages,
userDataDir = this.userDataDir,
experimentalContainers = this.experimentalContainers,
browserPerProxy = this.browserPerProxy,
proxyTier,
} = options;
return new LaunchContext({
id,
launchOptions: merge({}, this.launchOptions, launchOptions),
browserPlugin: this,
proxyUrl,
useIncognitoPages,
experimentalContainers,
userDataDir,
browserPerProxy,
proxyTier,
});
}
createController(): BrowserController<Library, LibraryOptions, LaunchResult, NewPageOptions, NewPageResult> {
return this._createController();
}
/**
* Launches the browser using provided launch context.
*/
async launch(
launchContext: LaunchContext<
Library,
LibraryOptions,
LaunchResult,
NewPageOptions,
NewPageResult
> = this.createLaunchContext(),
): Promise<LaunchResult> {
launchContext.launchOptions ??= {} as LibraryOptions;
const { proxyUrl, launchOptions } = launchContext;
if (proxyUrl) {
await this._addProxyToLaunchOptions(launchContext);
}
if (this._isChromiumBasedBrowser(launchContext)) {
// This will set the args for chromium based browsers to hide the webdriver.
(launchOptions as Dictionary).args = this._mergeArgsToHideWebdriver(launchOptions!.args);
// When User-Agent is not set, and we're using Chromium in headless mode,
// it is better to use DEFAULT_USER_AGENT to reduce chance of detection,
// as otherwise 'HeadlessChrome' is present in User-Agent string.
const userAgent = launchOptions!.args.find((arg: string) => arg.startsWith('--user-agent'));
if (launchOptions!.headless && !launchContext.fingerprint && !userAgent) {
launchOptions!.args.push(`--user-agent=${DEFAULT_USER_AGENT}`);
}
}
return this._launch(launchContext);
}
private _mergeArgsToHideWebdriver(originalArgs?: string[]): string[] {
if (!originalArgs?.length) {
return ['--disable-blink-features=AutomationControlled'];
}
const argumentIndex = originalArgs.findIndex((arg: string) => arg.startsWith('--disable-blink-features='));
if (argumentIndex !== -1) {
originalArgs[argumentIndex] += ',AutomationControlled';
} else {
originalArgs.push('--disable-blink-features=AutomationControlled');
}
return originalArgs;
}
protected _throwAugmentedLaunchError(
cause: unknown,
executablePath: string | undefined,
dockerImage: string,
moduleInstallCommand: string,
): never {
const errorMessage = ['Failed to launch browser. Please check the following:'];
if (executablePath) {
errorMessage.push(`- Check whether the provided executable path "${executablePath}" is correct.`);
}
if (process.env.APIFY_IS_AT_HOME) {
errorMessage.push(`- Make sure your Dockerfile extends ${dockerImage}.`);
}
errorMessage.push(`- ${moduleInstallCommand}`);
errorMessage.push(
'',
'The original error is available in the `cause` property. Below is the error received when trying to launch a browser:',
'',
);
// Add in a zero-width space so we can remove it later when printing the error stack
throw new BrowserLaunchError(`${errorMessage.join('\n')}\u200b`, { cause });
}
/**
* @private
*/
protected abstract _addProxyToLaunchOptions(
launchContext: LaunchContext<Library, LibraryOptions, LaunchResult, NewPageOptions, NewPageResult>,
): Promise<void>;
protected abstract _isChromiumBasedBrowser(
launchContext: LaunchContext<Library, LibraryOptions, LaunchResult, NewPageOptions, NewPageResult>,
): boolean;
/**
* @private
*/
protected abstract _launch(
launchContext: LaunchContext<Library, LibraryOptions, LaunchResult, NewPageOptions, NewPageResult>,
): Promise<LaunchResult>;
/**
* @private
*/
protected abstract _createController(): BrowserController<
Library,
LibraryOptions,
LaunchResult,
NewPageOptions,
NewPageResult
>;
}
export class BrowserLaunchError extends CriticalError {
public constructor(...args: ConstructorParameters<typeof CriticalError>) {
super(...args);
this.name = 'BrowserLaunchError';
const [, oldStack] = this.stack?.split('\u200b') ?? [null, ''];
Object.defineProperty(this, 'stack', {
get: () => {
if (this.cause instanceof Error) {
return `${this.message}\n${this.cause.stack}\nError thrown at:\n${oldStack}`;
}
return `${this.message}\n${oldStack}`;
},
});
}
}