@@ -6,20 +6,20 @@ const path = require("path");
6
6
const fs = require ( "fs" ) ;
7
7
const { createTracker, augmentTimeoutError } = require ( "./tracker" ) ;
8
8
9
- const errorToString = jsHandle =>
10
- jsHandle . executionContext ( ) . evaluate ( e => e . toString ( ) , jsHandle ) ;
9
+ const errorToString = ( jsHandle ) =>
10
+ jsHandle . executionContext ( ) . evaluate ( ( e ) => e . toString ( ) , jsHandle ) ;
11
11
12
- const objectToJson = jsHandle => jsHandle . jsonValue ( ) ;
12
+ const objectToJson = ( jsHandle ) => jsHandle . jsonValue ( ) ;
13
13
14
14
/**
15
15
* @param {{page: Page, options: {skipThirdPartyRequests: true}, basePath: string } } opt
16
16
* @return {Promise<void> }
17
17
*/
18
- const skipThirdPartyRequests = async opt => {
18
+ const skipThirdPartyRequests = async ( opt ) => {
19
19
const { page, options, basePath } = opt ;
20
20
if ( ! options . skipThirdPartyRequests ) return ;
21
21
await page . setRequestInterception ( true ) ;
22
- page . on ( "request" , request => {
22
+ page . on ( "request" , ( request ) => {
23
23
if ( request . url ( ) . startsWith ( basePath ) ) {
24
24
request . continue ( ) ;
25
25
} else {
@@ -32,37 +32,37 @@ const skipThirdPartyRequests = async opt => {
32
32
* @param {{page: Page, options: {sourceMaps: boolean}, route: string, onError: ?function } } opt
33
33
* @return {void }
34
34
*/
35
- const enableLogging = opt => {
35
+ const enableLogging = ( opt ) => {
36
36
const { page, options, route, onError, sourcemapStore } = opt ;
37
- page . on ( "console" , msg => {
37
+ page . on ( "console" , ( msg ) => {
38
38
const text = msg . text ( ) ;
39
39
if ( text === "JSHandle@object" ) {
40
- Promise . all ( msg . args ( ) . map ( objectToJson ) ) . then ( args =>
40
+ Promise . all ( msg . args ( ) . map ( objectToJson ) ) . then ( ( args ) =>
41
41
console . log ( `💬 console.log at ${ route } :` , ...args )
42
42
) ;
43
43
} else if ( text === "JSHandle@error" ) {
44
- Promise . all ( msg . args ( ) . map ( errorToString ) ) . then ( args =>
44
+ Promise . all ( msg . args ( ) . map ( errorToString ) ) . then ( ( args ) =>
45
45
console . log ( `💬 console.log at ${ route } :` , ...args )
46
46
) ;
47
47
} else {
48
48
console . log ( `️️️💬 console.log at ${ route } :` , text ) ;
49
49
}
50
50
} ) ;
51
- page . on ( "error" , msg => {
51
+ page . on ( "error" , ( msg ) => {
52
52
console . log ( `🔥 error at ${ route } :` , msg ) ;
53
53
onError && onError ( ) ;
54
54
} ) ;
55
- page . on ( "pageerror" , e => {
55
+ page . on ( "pageerror" , ( e ) => {
56
56
if ( options . sourceMaps ) {
57
57
mapStackTrace ( e . stack || e . message , {
58
58
isChromeOrEdge : true ,
59
- store : sourcemapStore || { }
59
+ store : sourcemapStore || { } ,
60
60
} )
61
- . then ( result => {
61
+ . then ( ( result ) => {
62
62
// TODO: refactor mapStackTrace: return array not a string, return first row too
63
63
const stackRows = result . split ( "\n" ) ;
64
64
const puppeteerLine =
65
- stackRows . findIndex ( x => x . includes ( "puppeteer" ) ) ||
65
+ stackRows . findIndex ( ( x ) => x . includes ( "puppeteer" ) ) ||
66
66
stackRows . length - 1 ;
67
67
68
68
console . log (
@@ -71,7 +71,7 @@ const enableLogging = opt => {
71
71
) [ 0 ] + "\n" } ${ stackRows . slice ( 0 , puppeteerLine ) . join ( "\n" ) } `
72
72
) ;
73
73
} )
74
- . catch ( e2 => {
74
+ . catch ( ( e2 ) => {
75
75
console . log ( `🔥 pageerror at ${ route } :` , e ) ;
76
76
console . log (
77
77
`️️️⚠️ warning at ${ route } (error in source maps):` ,
@@ -83,7 +83,7 @@ const enableLogging = opt => {
83
83
}
84
84
onError && onError ( ) ;
85
85
} ) ;
86
- page . on ( "response" , response => {
86
+ page . on ( "response" , ( response ) => {
87
87
if ( response . status ( ) >= 400 ) {
88
88
let route = "" ;
89
89
try {
@@ -105,21 +105,23 @@ const enableLogging = opt => {
105
105
* @param {{page: Page} } opt
106
106
* @return {Promise<Array<string>> }
107
107
*/
108
- const getLinks = async opt => {
108
+ const getLinks = async ( opt ) => {
109
109
const { page } = opt ;
110
110
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 ;
116
119
}
117
- return anchor . href ;
118
- } )
120
+ )
119
121
) ;
120
122
121
123
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 )
123
125
) ;
124
126
return anchors . concat ( iframes ) ;
125
127
} ;
@@ -130,15 +132,15 @@ const getLinks = async opt => {
130
132
* @param {{options: *, basePath: string, beforeFetch: ?(function({ page: Page, route: string }):Promise), afterFetch: ?(function({ page: Page, browser: Browser, route: string }):Promise), onEnd: ?(function():void)} } opt
131
133
* @return {Promise }
132
134
*/
133
- const crawl = async opt => {
135
+ const crawl = async ( opt ) => {
134
136
const {
135
137
options,
136
138
basePath,
137
139
beforeFetch,
138
140
afterFetch,
139
141
onEnd,
140
142
publicPath,
141
- sourceDir
143
+ sourceDir,
142
144
} = opt ;
143
145
let shuttingDown = false ;
144
146
let streamClosed = false ;
@@ -155,7 +157,7 @@ const crawl = async opt => {
155
157
} ;
156
158
process . on ( "SIGINT" , onSigint ) ;
157
159
158
- const onUnhandledRejection = error => {
160
+ const onUnhandledRejection = ( error ) => {
159
161
console . log ( "🔥 UnhandledPromiseRejectionWarning" , error ) ;
160
162
shuttingDown = true ;
161
163
} ;
@@ -172,7 +174,7 @@ const crawl = async opt => {
172
174
* @param {string } path
173
175
* @returns {void }
174
176
*/
175
- const addToQueue = newUrl => {
177
+ const addToQueue = ( newUrl ) => {
176
178
const { hostname, search, hash, port } = url . parse ( newUrl ) ;
177
179
newUrl = newUrl . replace ( `${ search || "" } ${ hash || "" } ` , "" ) ;
178
180
@@ -184,7 +186,12 @@ const crawl = async opt => {
184
186
// Port can be null, therefore we need the null check
185
187
const isOnAppPort = port && port . toString ( ) === options . port . toString ( ) ;
186
188
187
- if ( hostname === "localhost" && isOnAppPort && ! uniqueUrls . has ( newUrl ) && ! streamClosed ) {
189
+ if (
190
+ hostname === "localhost" &&
191
+ isOnAppPort &&
192
+ ! uniqueUrls . has ( newUrl ) &&
193
+ ! streamClosed
194
+ ) {
188
195
uniqueUrls . add ( newUrl ) ;
189
196
enqued ++ ;
190
197
queue . write ( newUrl ) ;
@@ -194,15 +201,18 @@ const crawl = async opt => {
194
201
}
195
202
} ;
196
203
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
+ ) ;
198
208
199
209
const browser = await puppeteer . launch ( options . puppeteer ) ;
200
210
201
211
/**
202
212
* @param {string } pageUrl
203
213
* @returns {Promise<string> }
204
214
*/
205
- const fetchPage = async pageUrl => {
215
+ const fetchPage = async ( pageUrl ) => {
206
216
const route = pageUrl . replace ( basePath , "" ) ;
207
217
208
218
let skipExistingFile = false ;
@@ -228,13 +238,16 @@ const crawl = async opt => {
228
238
onError : ( ) => {
229
239
shuttingDown = true ;
230
240
} ,
231
- sourcemapStore
241
+ sourcemapStore,
232
242
} ) ;
233
243
beforeFetch && beforeFetch ( { page, route } ) ;
234
244
await page . setUserAgent ( options . userAgent ) ;
235
245
const tracker = createTracker ( page ) ;
236
246
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
+ } ) ;
238
251
} catch ( e ) {
239
252
e . message = augmentTimeoutError ( e . message , tracker ) ;
240
253
throw e ;
@@ -268,12 +281,12 @@ const crawl = async opt => {
268
281
} ;
269
282
270
283
if ( options . include ) {
271
- options . include . map ( x => addToQueue ( `${ basePath } ${ x } ` ) ) ;
284
+ options . include . map ( ( x ) => addToQueue ( `${ basePath } ${ x } ` ) ) ;
272
285
}
273
286
274
287
return new Promise ( ( resolve , reject ) => {
275
288
queue
276
- . map ( x => _ ( fetchPage ( x ) ) )
289
+ . map ( ( x ) => _ ( fetchPage ( x ) ) )
277
290
. mergeWithLimit ( options . concurrency )
278
291
. toArray ( async ( ) => {
279
292
process . removeListener ( "SIGINT" , onSigint ) ;
0 commit comments