-
Notifications
You must be signed in to change notification settings - Fork 49
/
crawler.js
340 lines (293 loc) · 12.3 KB
/
crawler.js
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/* eslint-disable max-lines */
const puppeteer = require('puppeteer');
const chalk = require('chalk').default;
const {createTimer} = require('./helpers/timer');
const wait = require('./helpers/wait');
const tldts = require('tldts');
const DEFAULT_USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36';
const MOBILE_USER_AGENT = 'Mozilla/5.0 (Linux; Android 10; Pixel 2 XL) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Mobile Safari/537.36';
const DEFAULT_VIEWPORT = {
width: 1440,//px
height: 812//px
};
const MOBILE_VIEWPORT = {
width: 412,
height: 691,
deviceScaleFactor: 2,
isMobile: true,
hasTouch: true
};
// for debugging: will lunch in window mode instad of headless, open devtools and don't close windows after process finishes
const VISUAL_DEBUG = false;
/**
* @param {function(...any):void} log
* @param {string} proxyHost
* @param {string} executablePath path to chromium executable to use
*/
function openBrowser(log, proxyHost, executablePath) {
/**
* @type {import('puppeteer').BrowserLaunchArgumentOptions}
*/
const args = {
args: [
// enable FLoC
'--enable-blink-features=InterestCohortAPI',
'--enable-features="FederatedLearningOfCohorts:update_interval/10s/minimum_history_domain_size_required/1,FlocIdSortingLshBasedComputation,InterestCohortFeaturePolicy"',
'--js-flags="--async-stack-traces --stack-trace-limit 32"'
]
};
if (VISUAL_DEBUG) {
args.headless = false;
args.devtools = true;
}
if (proxyHost) {
let url;
try {
url = new URL(proxyHost);
} catch {
log('Invalid proxy URL');
}
args.args.push(`--proxy-server=${proxyHost}`);
args.args.push(`--host-resolver-rules="MAP * ~NOTFOUND , EXCLUDE ${url.hostname}"`);
}
if (executablePath) {
// @ts-ignore there is no single object that encapsulates properties of both BrowserLaunchArgumentOptions and LaunchOptions that are allowed here
args.executablePath = executablePath;
}
return puppeteer.launch(args);
}
/**
* @param {puppeteer.BrowserContext} context
* @param {URL} url
* @param {{collectors: import('./collectors/BaseCollector')[], log: function(...any):void, urlFilter: function(string, string):boolean, emulateMobile: boolean, emulateUserAgent: boolean, runInEveryFrame: function():void, maxLoadTimeMs: number, extraExecutionTimeMs: number, collectorFlags: Object.<string, string>}} data
*
* @returns {Promise<CollectResult>}
*/
async function getSiteData(context, url, {
collectors,
log,
urlFilter,
emulateUserAgent,
emulateMobile,
runInEveryFrame,
maxLoadTimeMs,
extraExecutionTimeMs,
collectorFlags,
}) {
const testStarted = Date.now();
/**
* @type {{cdpClient: import('puppeteer').CDPSession, type: string, url: string}[]}
*/
const targets = [];
const collectorOptions = {
context,
url,
log,
collectorFlags
};
for (let collector of collectors) {
const timer = createTimer();
try {
// eslint-disable-next-line no-await-in-loop
await collector.init(collectorOptions);
log(`${collector.id()} init took ${timer.getElapsedTime()}s`);
} catch (e) {
log(chalk.yellow(`${collector.id()} init failed`), chalk.gray(e.message), chalk.gray(e.stack));
}
}
let pageTargetCreated = false;
// initiate collectors for all contexts (main page, web worker, service worker etc.)
context.on('targetcreated', async target => {
// we have already initiated collectors for the main page, so we ignore the first page target
if (target.type() === 'page' && !pageTargetCreated) {
pageTargetCreated = true;
return;
}
const timer = createTimer();
let cdpClient = null;
try {
cdpClient = await target.createCDPSession();
} catch (e) {
log(chalk.yellow(`Failed to connect to "${target.url()}"`), chalk.gray(e.message), chalk.gray(e.stack));
return;
}
const simpleTarget = {url: target.url(), type: target.type(), cdpClient};
targets.push(simpleTarget);
try {
// we have to pause new targets and attach to them as soon as they are created not to miss any data
await cdpClient.send('Target.setAutoAttach', {autoAttach: true, waitForDebuggerOnStart: true});
} catch (e) {
log(chalk.yellow(`Failed to set "${target.url()}" up.`), chalk.gray(e.message), chalk.gray(e.stack));
return;
}
for (let collector of collectors) {
try {
// eslint-disable-next-line no-await-in-loop
await collector.addTarget(simpleTarget);
} catch (e) {
log(chalk.yellow(`${collector.id()} failed to attach to "${target.url()}"`), chalk.gray(e.message), chalk.gray(e.stack));
}
}
try {
// resume target when all collectors are ready
await cdpClient.send('Runtime.enable');
await cdpClient.send('Runtime.runIfWaitingForDebugger');
} catch (e) {
log(chalk.yellow(`Failed to resume target "${target.url()}"`), chalk.gray(e.message), chalk.gray(e.stack));
return;
}
log(`${target.url()} (${target.type()}) context initiated in ${timer.getElapsedTime()}s`);
});
// Create a new page in a pristine context.
const page = await context.newPage();
// optional function that should be run on every page (and subframe) in the browser context
if (runInEveryFrame) {
page.evaluateOnNewDocument(runInEveryFrame);
}
// We are creating CDP connection before page target is created, if we create it only after
// new target is created we will miss some requests, API calls, etc.
const cdpClient = await page.target().createCDPSession();
// without this, we will miss the initial request for the web worker or service worker file
await cdpClient.send('Target.setAutoAttach', {autoAttach: true, waitForDebuggerOnStart: true});
const initPageTimer = createTimer();
for (let collector of collectors) {
try {
// eslint-disable-next-line no-await-in-loop
await collector.addTarget({url: url.toString(), type: 'page', cdpClient});
} catch (e) {
log(chalk.yellow(`${collector.id()} failed to attach to page`), chalk.gray(e.message), chalk.gray(e.stack));
}
}
log(`page context initiated in ${initPageTimer.getElapsedTime()}s`);
if (emulateUserAgent) {
await page.setUserAgent(emulateMobile ? MOBILE_USER_AGENT : DEFAULT_USER_AGENT);
}
await page.setViewport(emulateMobile ? MOBILE_VIEWPORT : DEFAULT_VIEWPORT);
// if any prompts open on page load, they'll make the page hang unless closed
page.on('dialog', dialog => dialog.dismiss());
// catch and report crash errors
page.on('error', e => log(chalk.red(e.message)));
let timeout = false;
try {
await page.goto(url.toString(), {timeout: maxLoadTimeMs, waitUntil: 'networkidle0'});
} catch (e) {
if (e instanceof puppeteer.errors.TimeoutError || (e.name && e.name === 'TimeoutError')) {
log(chalk.yellow('Navigation timeout exceeded.'));
for (let target of targets) {
if (target.type === 'page') {
// eslint-disable-next-line no-await-in-loop
await target.cdpClient.send('Page.stopLoading');
}
}
timeout = true;
} else {
throw e;
}
}
for (let collector of collectors) {
const postLoadTimer = createTimer();
try {
// eslint-disable-next-line no-await-in-loop
await collector.postLoad();
log(`${collector.id()} postLoad took ${postLoadTimer.getElapsedTime()}s`);
} catch (e) {
log(chalk.yellow(`${collector.id()} postLoad failed`), chalk.gray(e.message), chalk.gray(e.stack));
}
}
// give website a bit more time for things to settle
await page.waitForTimeout(extraExecutionTimeMs);
const finalUrl = page.url();
/**
* @type {Object<string, Object>}
*/
const data = {};
for (let collector of collectors) {
const getDataTimer = createTimer();
try {
// eslint-disable-next-line no-await-in-loop
const collectorData = await collector.getData({
finalUrl,
urlFilter: urlFilter && urlFilter.bind(null, finalUrl)
});
data[collector.id()] = collectorData;
log(`getting ${collector.id()} data took ${getDataTimer.getElapsedTime()}s`);
} catch (e) {
log(chalk.yellow(`getting ${collector.id()} data failed`), chalk.gray(e.message), chalk.gray(e.stack));
data[collector.id()] = null;
}
}
for (let target of targets) {
try {
// eslint-disable-next-line no-await-in-loop
await target.cdpClient.detach();
} catch {
// we don't care that much because in most cases an error here means that target already detached
}
}
if (!VISUAL_DEBUG) {
await page.close();
}
return {
initialUrl: url.toString(),
finalUrl,
timeout,
testStarted,
testFinished: Date.now(),
data
};
}
/**
* @param {string} documentUrl
* @param {string} requestUrl
* @returns {boolean}
*/
function isThirdPartyRequest(documentUrl, requestUrl) {
const mainPageDomain = tldts.getDomain(documentUrl);
return tldts.getDomain(requestUrl) !== mainPageDomain;
}
/**
* @param {URL} url
* @param {{collectors?: import('./collectors/BaseCollector')[], log?: function(...any):void, filterOutFirstParty?: boolean, emulateMobile?: boolean, emulateUserAgent?: boolean, proxyHost?: string, browserContext?: puppeteer.BrowserContext, runInEveryFrame?: function():void, executablePath?: string, maxLoadTimeMs?: number, extraExecutionTimeMs?: number, collectorFlags?: Object.<string, string>}} options
* @returns {Promise<CollectResult>}
*/
module.exports = async (url, options) => {
const log = options.log || (() => {});
const browser = options.browserContext ? null : await openBrowser(log, options.proxyHost, options.executablePath);
// Create a new incognito browser context.
const context = options.browserContext || await browser.createIncognitoBrowserContext();
let data = null;
const maxLoadTimeMs = options.maxLoadTimeMs || 30000;
const extraExecutionTimeMs = options.extraExecutionTimeMs || 2500;
const maxTotalTimeMs = maxLoadTimeMs * 2;
try {
data = await wait(getSiteData(context, url, {
collectors: options.collectors || [],
log,
urlFilter: options.filterOutFirstParty === true ? isThirdPartyRequest.bind(null) : null,
emulateUserAgent: options.emulateUserAgent !== false, // true by default
emulateMobile: options.emulateMobile,
runInEveryFrame: options.runInEveryFrame,
maxLoadTimeMs,
extraExecutionTimeMs,
collectorFlags: options.collectorFlags
}), maxTotalTimeMs);
} catch(e) {
log(chalk.red('Crawl failed'), e.message, chalk.gray(e.stack));
throw e;
} finally {
// only close the browser if it was created here and not debugging
if (browser && !VISUAL_DEBUG) {
await browser.close();
}
}
return data;
};
/**
* @typedef {Object} CollectResult
* @property {string} initialUrl URL from which the crawler began the crawl (as provided by the caller)
* @property {string} finalUrl URL after page has loaded (can be different from initialUrl if e.g. there was a redirect)
* @property {boolean} timeout true if page didn't fully load before the timeout and loading had to be stopped by the crawler
* @property {number} testStarted time when the crawl started (unix timestamp)
* @property {number} testFinished time when the crawl finished (unix timestamp)
* @property {import('./helpers/collectorsList').CollectorData} data object containing output from all collectors
*/