-
Notifications
You must be signed in to change notification settings - Fork 394
/
Copy pathpuppeteer_utils.js
297 lines (276 loc) · 8.93 KB
/
puppeteer_utils.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
const puppeteer = require("puppeteer");
const _ = require("highland");
const url = require("url");
const mapStackTrace = require("sourcemapped-stacktrace-node").default;
const path = require("path");
const fs = require("fs");
const { createTracker, augmentTimeoutError } = require("./tracker");
const errorToString = jsHandle =>
jsHandle.executionContext().evaluate(e => e.toString(), jsHandle);
const objectToJson = jsHandle => jsHandle.jsonValue();
/**
* @param {{page: Page, options: {skipThirdPartyRequests: true}, basePath: string }} opt
* @return {Promise<void>}
*/
const skipThirdPartyRequests = async opt => {
const { page, options, basePath } = opt;
if (!options.skipThirdPartyRequests) return;
await page.setRequestInterception(true);
page.on("request", request => {
if (request.url().startsWith(basePath)) {
request.continue();
} else {
request.abort();
}
});
};
/**
* @param {{page: Page, options: {sourceMaps: boolean}, route: string, onError: ?function }} opt
* @return {void}
*/
const enableLogging = opt => {
const { page, options, route, onError, sourcemapStore } = opt;
page.on("console", msg => {
const text = msg.text();
if (text === "JSHandle@object") {
Promise.all(msg.args().map(objectToJson)).then(args =>
console.log(`💬 console.log at ${route}:`, ...args)
);
} else if (text === "JSHandle@error") {
Promise.all(msg.args().map(errorToString)).then(args =>
console.log(`💬 console.log at ${route}:`, ...args)
);
} else {
console.log(`️️️💬 console.log at ${route}:`, text);
}
});
page.on("error", msg => {
console.log(`🔥 error at ${route}:`, msg);
onError && onError();
});
page.on("pageerror", e => {
if (options.sourceMaps) {
mapStackTrace(e.stack || e.message, {
isChromeOrEdge: true,
store: sourcemapStore || {}
})
.then(result => {
// TODO: refactor mapStackTrace: return array not a string, return first row too
const stackRows = result.split("\n");
const puppeteerLine =
stackRows.findIndex(x => x.includes("puppeteer")) ||
stackRows.length - 1;
console.log(
`🔥 pageerror at ${route}: ${(e.stack || e.message).split(
"\n"
)[0] + "\n"}${stackRows.slice(0, puppeteerLine).join("\n")}`
);
})
.catch(e2 => {
console.log(`🔥 pageerror at ${route}:`, e);
console.log(
`️️️⚠️ warning at ${route} (error in source maps):`,
e2.message
);
});
} else {
console.log(`🔥 pageerror at ${route}:`, e);
}
onError && onError();
});
page.on("response", response => {
if (response.status() >= 400) {
let route = "";
try {
route = response._request
.headers()
.referer.replace(`http://localhost:${options.port}`, "");
} catch (e) {}
console.log(
`️️️⚠️ warning at ${route}: got ${response.status()} HTTP code for ${response.url()}`
);
}
});
// page.on("requestfailed", msg =>
// console.log(`️️️⚠️ ${route} requestfailed:`, msg)
// );
};
/**
* @param {{page: Page}} opt
* @return {Promise<Array<string>>}
*/
const getLinks = async opt => {
const { page } = opt;
const anchors = await page.evaluate(() =>
Array.from(document.querySelectorAll("a,link[rel='alternate']")).map(anchor => {
if (anchor.href.baseVal) {
const a = document.createElement("a");
a.href = anchor.href.baseVal;
return a.href;
}
return anchor.href;
})
);
const iframes = await page.evaluate(() =>
Array.from(document.querySelectorAll("iframe")).map(iframe => iframe.src)
);
return anchors.concat(iframes);
};
/**
* can not use null as default for function because of TS error https://github.com/Microsoft/TypeScript/issues/14889
*
* @param {{options: *, basePath: string, beforeFetch: ?(function({ page: Page, route: string }):Promise), afterFetch: ?(function({ page: Page, browser: Browser, route: string }):Promise), onEnd: ?(function():void)}} opt
* @return {Promise}
*/
const crawl = async opt => {
const {
options,
basePath,
beforeFetch,
afterFetch,
onEnd,
publicPath,
sourceDir
} = opt;
let shuttingDown = false;
let streamClosed = false;
const onSigint = () => {
if (shuttingDown) {
process.exit(1);
} else {
shuttingDown = true;
console.log(
"\nGracefully shutting down. To exit immediately, press ^C again"
);
}
};
process.on("SIGINT", onSigint);
const onUnhandledRejection = error => {
console.log("🔥 UnhandledPromiseRejectionWarning", error);
shuttingDown = true;
};
process.on("unhandledRejection", onUnhandledRejection);
const queue = _();
let enqued = 0;
let processed = 0;
// use Set instead
const uniqueUrls = new Set();
const sourcemapStore = {};
/**
* @param {string} path
* @returns {void}
*/
const addToQueue = newUrl => {
const { hostname, search, hash, port } = url.parse(newUrl);
newUrl = newUrl.replace(`${search || ""}${hash || ""}`, "");
// Ensures that only link on the same port are crawled
//
// url.parse returns a string,
// but options port is passed by a user and default value is a number
// we are converting both to string to be sure
// Port can be null, therefore we need the null check
const isOnAppPort = port && port.toString() === options.port.toString();
if (hostname === "localhost" && isOnAppPort && !uniqueUrls.has(newUrl) && !streamClosed) {
uniqueUrls.add(newUrl);
enqued++;
queue.write(newUrl);
if (enqued == 2 && options.crawl) {
addToQueue(`${basePath}${publicPath}/404.html`);
}
}
};
const browser = await puppeteer.launch({
headless: options.headless,
args: options.puppeteerArgs,
executablePath: options.puppeteerExecutablePath,
ignoreHTTPSErrors: options.puppeteerIgnoreHTTPSErrors,
handleSIGINT: false
});
/**
* @param {string} pageUrl
* @returns {Promise<string>}
*/
const fetchPage = async pageUrl => {
const route = pageUrl.replace(basePath, "");
let skipExistingFile = false;
const routePath = route.replace(/\//g, path.sep);
const { ext } = path.parse(routePath);
if (ext !== ".html" && ext !== "") {
const filePath = path.join(sourceDir, routePath);
skipExistingFile = fs.existsSync(filePath);
}
if (!shuttingDown && !skipExistingFile) {
try {
const page = await browser.newPage();
const client = await page.target().createCDPSession();
await client("ServiceWorker.disable");
await page.setCacheEnabled(options.puppeteer.cache);
if (options.viewport) await page.setViewport(options.viewport);
if (options.skipThirdPartyRequests)
await skipThirdPartyRequests({ page, options, basePath });
enableLogging({
page,
options,
route,
onError: () => {
shuttingDown = true;
},
sourcemapStore
});
beforeFetch && beforeFetch({ page, route });
await page.setUserAgent(options.userAgent);
const tracker = createTracker(page);
try {
await page.goto(pageUrl, { waitUntil: "networkidle0" });
} catch (e) {
e.message = augmentTimeoutError(e.message, tracker);
throw e;
} finally {
tracker.dispose();
}
if (options.waitFor) await page.waitFor(options.waitFor);
if (options.crawl) {
const links = await getLinks({ page });
links.forEach(addToQueue);
}
afterFetch && (await afterFetch({ page, route, browser, addToQueue }));
await page.close();
console.log(`✅ crawled ${processed + 1} out of ${enqued} (${route})`);
} catch (e) {
if (!shuttingDown) {
console.log(`🔥 error at ${route}`, e);
}
shuttingDown = true;
}
} else {
// this message creates a lot of noise
// console.log(`🚧 skipping (${processed + 1}/${enqued}) ${route}`);
}
processed++;
if (enqued === processed) {
streamClosed = true;
queue.end();
}
return pageUrl;
};
if (options.include) {
options.include.map(x => addToQueue(`${basePath}${x}`));
}
return new Promise((resolve, reject) => {
queue
.map(x => _(fetchPage(x)))
.mergeWithLimit(options.concurrency)
.toArray(async () => {
process.removeListener("SIGINT", onSigint);
process.removeListener("unhandledRejection", onUnhandledRejection);
await browser.close();
onEnd && onEnd();
if (shuttingDown) return reject("");
resolve();
});
});
};
exports.skipThirdPartyRequests = skipThirdPartyRequests;
exports.enableLogging = enableLogging;
exports.getLinks = getLinks;
exports.crawl = crawl;