Skip to content

Commit af68fda

Browse files
author
Serhii Larchenko
committed
Reverted: wait-until option; prettified
1 parent bff5a25 commit af68fda

File tree

2 files changed

+50
-36
lines changed

2 files changed

+50
-36
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
yarn-error.log
3+
.vscode

src/puppeteer_utils.js

+49-36
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ const path = require("path");
66
const fs = require("fs");
77
const { createTracker, augmentTimeoutError } = require("./tracker");
88

9-
const errorToString = jsHandle =>
10-
jsHandle.executionContext().evaluate(e => e.toString(), jsHandle);
9+
const errorToString = (jsHandle) =>
10+
jsHandle.executionContext().evaluate((e) => e.toString(), jsHandle);
1111

12-
const objectToJson = jsHandle => jsHandle.jsonValue();
12+
const objectToJson = (jsHandle) => jsHandle.jsonValue();
1313

1414
/**
1515
* @param {{page: Page, options: {skipThirdPartyRequests: true}, basePath: string }} opt
1616
* @return {Promise<void>}
1717
*/
18-
const skipThirdPartyRequests = async opt => {
18+
const skipThirdPartyRequests = async (opt) => {
1919
const { page, options, basePath } = opt;
2020
if (!options.skipThirdPartyRequests) return;
2121
await page.setRequestInterception(true);
22-
page.on("request", request => {
22+
page.on("request", (request) => {
2323
if (request.url().startsWith(basePath)) {
2424
request.continue();
2525
} else {
@@ -32,37 +32,37 @@ const skipThirdPartyRequests = async opt => {
3232
* @param {{page: Page, options: {sourceMaps: boolean}, route: string, onError: ?function }} opt
3333
* @return {void}
3434
*/
35-
const enableLogging = opt => {
35+
const enableLogging = (opt) => {
3636
const { page, options, route, onError, sourcemapStore } = opt;
37-
page.on("console", msg => {
37+
page.on("console", (msg) => {
3838
const text = msg.text();
3939
if (text === "JSHandle@object") {
40-
Promise.all(msg.args().map(objectToJson)).then(args =>
40+
Promise.all(msg.args().map(objectToJson)).then((args) =>
4141
console.log(`💬 console.log at ${route}:`, ...args)
4242
);
4343
} else if (text === "JSHandle@error") {
44-
Promise.all(msg.args().map(errorToString)).then(args =>
44+
Promise.all(msg.args().map(errorToString)).then((args) =>
4545
console.log(`💬 console.log at ${route}:`, ...args)
4646
);
4747
} else {
4848
console.log(`️️️💬 console.log at ${route}:`, text);
4949
}
5050
});
51-
page.on("error", msg => {
51+
page.on("error", (msg) => {
5252
console.log(`🔥 error at ${route}:`, msg);
5353
onError && onError();
5454
});
55-
page.on("pageerror", e => {
55+
page.on("pageerror", (e) => {
5656
if (options.sourceMaps) {
5757
mapStackTrace(e.stack || e.message, {
5858
isChromeOrEdge: true,
59-
store: sourcemapStore || {}
59+
store: sourcemapStore || {},
6060
})
61-
.then(result => {
61+
.then((result) => {
6262
// TODO: refactor mapStackTrace: return array not a string, return first row too
6363
const stackRows = result.split("\n");
6464
const puppeteerLine =
65-
stackRows.findIndex(x => x.includes("puppeteer")) ||
65+
stackRows.findIndex((x) => x.includes("puppeteer")) ||
6666
stackRows.length - 1;
6767

6868
console.log(
@@ -71,7 +71,7 @@ const enableLogging = opt => {
7171
)[0] + "\n"}${stackRows.slice(0, puppeteerLine).join("\n")}`
7272
);
7373
})
74-
.catch(e2 => {
74+
.catch((e2) => {
7575
console.log(`🔥 pageerror at ${route}:`, e);
7676
console.log(
7777
`️️️⚠️ warning at ${route} (error in source maps):`,
@@ -83,7 +83,7 @@ const enableLogging = opt => {
8383
}
8484
onError && onError();
8585
});
86-
page.on("response", response => {
86+
page.on("response", (response) => {
8787
if (response.status() >= 400) {
8888
let route = "";
8989
try {
@@ -105,21 +105,23 @@ const enableLogging = opt => {
105105
* @param {{page: Page}} opt
106106
* @return {Promise<Array<string>>}
107107
*/
108-
const getLinks = async opt => {
108+
const getLinks = async (opt) => {
109109
const { page } = opt;
110110
const anchors = await page.evaluate(() =>
111-
Array.from(document.querySelectorAll("a,link[rel='alternate']")).map(anchor => {
112-
if (anchor.href.baseVal) {
113-
const a = document.createElement("a");
114-
a.href = anchor.href.baseVal;
115-
return a.href;
111+
Array.from(document.querySelectorAll("a,link[rel='alternate']")).map(
112+
(anchor) => {
113+
if (anchor.href.baseVal) {
114+
const a = document.createElement("a");
115+
a.href = anchor.href.baseVal;
116+
return a.href;
117+
}
118+
return anchor.href;
116119
}
117-
return anchor.href;
118-
})
120+
)
119121
);
120122

121123
const iframes = await page.evaluate(() =>
122-
Array.from(document.querySelectorAll("iframe")).map(iframe => iframe.src)
124+
Array.from(document.querySelectorAll("iframe")).map((iframe) => iframe.src)
123125
);
124126
return anchors.concat(iframes);
125127
};
@@ -130,15 +132,15 @@ const getLinks = async opt => {
130132
* @param {{options: *, basePath: string, beforeFetch: ?(function({ page: Page, route: string }):Promise), afterFetch: ?(function({ page: Page, browser: Browser, route: string }):Promise), onEnd: ?(function():void)}} opt
131133
* @return {Promise}
132134
*/
133-
const crawl = async opt => {
135+
const crawl = async (opt) => {
134136
const {
135137
options,
136138
basePath,
137139
beforeFetch,
138140
afterFetch,
139141
onEnd,
140142
publicPath,
141-
sourceDir
143+
sourceDir,
142144
} = opt;
143145
let shuttingDown = false;
144146
let streamClosed = false;
@@ -155,7 +157,7 @@ const crawl = async opt => {
155157
};
156158
process.on("SIGINT", onSigint);
157159

158-
const onUnhandledRejection = error => {
160+
const onUnhandledRejection = (error) => {
159161
console.log("🔥 UnhandledPromiseRejectionWarning", error);
160162
shuttingDown = true;
161163
};
@@ -172,7 +174,7 @@ const crawl = async opt => {
172174
* @param {string} path
173175
* @returns {void}
174176
*/
175-
const addToQueue = newUrl => {
177+
const addToQueue = (newUrl) => {
176178
const { hostname, search, hash, port } = url.parse(newUrl);
177179
newUrl = newUrl.replace(`${search || ""}${hash || ""}`, "");
178180

@@ -184,7 +186,12 @@ const crawl = async opt => {
184186
// Port can be null, therefore we need the null check
185187
const isOnAppPort = port && port.toString() === options.port.toString();
186188

187-
if (hostname === "localhost" && isOnAppPort && !uniqueUrls.has(newUrl) && !streamClosed) {
189+
if (
190+
hostname === "localhost" &&
191+
isOnAppPort &&
192+
!uniqueUrls.has(newUrl) &&
193+
!streamClosed
194+
) {
188195
uniqueUrls.add(newUrl);
189196
enqued++;
190197
queue.write(newUrl);
@@ -194,15 +201,18 @@ const crawl = async opt => {
194201
}
195202
};
196203

197-
console.log('Puppeteer is starting with following options: ', options.puppeteer);
204+
console.log(
205+
"Puppeteer is starting with following options: ",
206+
options.puppeteer
207+
);
198208

199209
const browser = await puppeteer.launch(options.puppeteer);
200210

201211
/**
202212
* @param {string} pageUrl
203213
* @returns {Promise<string>}
204214
*/
205-
const fetchPage = async pageUrl => {
215+
const fetchPage = async (pageUrl) => {
206216
const route = pageUrl.replace(basePath, "");
207217

208218
let skipExistingFile = false;
@@ -228,13 +238,16 @@ const crawl = async opt => {
228238
onError: () => {
229239
shuttingDown = true;
230240
},
231-
sourcemapStore
241+
sourcemapStore,
232242
});
233243
beforeFetch && beforeFetch({ page, route });
234244
await page.setUserAgent(options.userAgent);
235245
const tracker = createTracker(page);
236246
try {
237-
await page.goto(pageUrl, { timeout: options.puppeteer.timeout, waitUntil: "domcontentloaded" });
247+
await page.goto(pageUrl, {
248+
timeout: options.puppeteer.timeout,
249+
waitUntil: "networkidle0",
250+
});
238251
} catch (e) {
239252
e.message = augmentTimeoutError(e.message, tracker);
240253
throw e;
@@ -268,12 +281,12 @@ const crawl = async opt => {
268281
};
269282

270283
if (options.include) {
271-
options.include.map(x => addToQueue(`${basePath}${x}`));
284+
options.include.map((x) => addToQueue(`${basePath}${x}`));
272285
}
273286

274287
return new Promise((resolve, reject) => {
275288
queue
276-
.map(x => _(fetchPage(x)))
289+
.map((x) => _(fetchPage(x)))
277290
.mergeWithLimit(options.concurrency)
278291
.toArray(async () => {
279292
process.removeListener("SIGINT", onSigint);

0 commit comments

Comments
 (0)