-
Notifications
You must be signed in to change notification settings - Fork 765
/
Copy pathpuppeteer-plugin.ts
245 lines (205 loc) · 8.44 KB
/
puppeteer-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
import { readFile } from 'fs/promises';
import type { Dictionary } from '@crawlee/types';
import type Puppeteer from 'puppeteer';
import type * as PuppeteerTypes from 'puppeteer';
import type { PuppeteerNewPageOptions } from './puppeteer-controller';
import { PuppeteerController } from './puppeteer-controller';
import type { BrowserController } from '../abstract-classes/browser-controller';
import { BrowserPlugin } from '../abstract-classes/browser-plugin';
import { anonymizeProxySugar } from '../anonymize-proxy';
import type { LaunchContext } from '../launch-context';
import { log } from '../logger';
import { noop } from '../utils';
const PROXY_SERVER_ARG = '--proxy-server=';
export class PuppeteerPlugin extends BrowserPlugin<
typeof Puppeteer,
PuppeteerTypes.LaunchOptions,
PuppeteerTypes.Browser,
PuppeteerNewPageOptions
> {
protected async _launch(
launchContext: LaunchContext<
typeof Puppeteer,
PuppeteerTypes.LaunchOptions,
PuppeteerTypes.Browser,
PuppeteerNewPageOptions
>,
): Promise<PuppeteerTypes.Browser> {
let oldPuppeteerVersion = false;
try {
const jsonPath = require.resolve('puppeteer/package.json');
const parsed = JSON.parse(await readFile(jsonPath, 'utf-8'));
const version = +parsed.version.split('.')[0];
oldPuppeteerVersion = version < 22;
} catch {
// ignore
}
const { launchOptions, userDataDir, useIncognitoPages, experimentalContainers, proxyUrl } = launchContext;
if (experimentalContainers) {
throw new Error('Experimental containers are only available with Playwright');
}
launchOptions!.userDataDir = launchOptions!.userDataDir ?? userDataDir;
if (launchOptions!.headless === false) {
if (Array.isArray(launchOptions!.args)) {
launchOptions!.args.push('--disable-site-isolation-trials');
} else {
launchOptions!.args = ['--disable-site-isolation-trials'];
}
}
if (launchOptions!.headless === true && oldPuppeteerVersion) {
launchOptions!.headless = 'new' as any;
}
let browser: PuppeteerTypes.Browser;
{
const [anonymizedProxyUrl, close] = await anonymizeProxySugar(proxyUrl);
if (proxyUrl) {
const proxyArg = `${PROXY_SERVER_ARG}${anonymizedProxyUrl ?? proxyUrl}`;
if (Array.isArray(launchOptions!.args)) {
launchOptions!.args.push(proxyArg);
} else {
launchOptions!.args = [proxyArg];
}
}
try {
browser = await this.library.launch(launchOptions);
if (anonymizedProxyUrl) {
browser.on('disconnected', async () => {
await close();
});
}
} catch (error: any) {
await close();
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)",
);
}
}
browser.on('targetcreated', async (target: PuppeteerTypes.Target) => {
try {
const page = await target.page();
if (page) {
page.on('error', (error) => {
log.exception(error, 'Page crashed.');
page.close().catch(noop);
});
}
} catch (error: any) {
log.exception(error, 'Failed to retrieve page from target.');
}
});
const boundMethods = (
[
'newPage',
'close',
'userAgent',
'createIncognitoBrowserContext',
'createBrowserContext',
'version',
'on',
'process',
] as const
).reduce((map, method) => {
map[method] = browser[method as 'close']?.bind(browser);
return map;
}, {} as Dictionary);
const method = oldPuppeteerVersion ? 'createIncognitoBrowserContext' : 'createBrowserContext';
browser = new Proxy(browser, {
get: (target, property: keyof typeof browser, receiver) => {
if (property === 'newPage') {
return async (...args: Parameters<PuppeteerTypes.BrowserContext['newPage']>) => {
let page: PuppeteerTypes.Page;
if (useIncognitoPages) {
const [anonymizedProxyUrl, close] = await anonymizeProxySugar(proxyUrl);
try {
const context = (await (browser as any)[method]({
proxyServer: anonymizedProxyUrl ?? proxyUrl,
})) as PuppeteerTypes.BrowserContext;
page = await context.newPage(...args);
if (anonymizedProxyUrl) {
page.on('close', async () => {
await close();
});
}
} catch (error) {
await close();
throw error;
}
} else {
page = await boundMethods.newPage(...args);
}
/*
// DO NOT USE YET! DOING SO DISABLES CACHE WHICH IS 50% PERFORMANCE HIT!
if (useIncognitoPages) {
const context = await browser.createIncognitoBrowserContext({
proxyServer: proxyUrl,
});
page = await context.newPage(...args);
} else {
page = await newPage(...args);
}
if (proxyCredentials) {
await page.authenticate(proxyCredentials as Credentials);
}
*/
return page;
};
}
if (property in boundMethods) {
return boundMethods[property];
}
return Reflect.get(target, property, receiver);
},
});
return browser;
}
protected _createController(): BrowserController<
typeof Puppeteer,
PuppeteerTypes.LaunchOptions,
PuppeteerTypes.Browser,
PuppeteerNewPageOptions
> {
return new PuppeteerController(this);
}
protected async _addProxyToLaunchOptions(
_launchContext: LaunchContext<
typeof Puppeteer,
PuppeteerTypes.LaunchOptions,
PuppeteerTypes.Browser,
PuppeteerNewPageOptions
>,
): Promise<void> {
/*
// DO NOT USE YET! DOING SO DISABLES CACHE WHICH IS 50% PERFORMANCE HIT!
launchContext.launchOptions ??= {};
const { launchOptions, proxyUrl } = launchContext;
if (proxyUrl) {
const url = new URL(proxyUrl);
if (url.username || url.password) {
launchContext.proxyCredentials = {
username: decodeURIComponent(url.username),
password: decodeURIComponent(url.password),
};
}
const proxyArg = `${PROXY_SERVER_ARG}${url.origin}`;
if (Array.isArray(launchOptions.args)) {
launchOptions.args.push(proxyArg);
} else {
launchOptions.args = [proxyArg];
}
}
*/
}
protected _isChromiumBasedBrowser(
_launchContext: LaunchContext<
typeof Puppeteer,
PuppeteerTypes.LaunchOptions,
PuppeteerTypes.Browser,
PuppeteerNewPageOptions
>,
): boolean {
return true;
}
}