@@ -2,6 +2,7 @@ import {EventEmitter} from 'events';
2
2
import { URL } from 'url' ;
3
3
import * as http from 'http' ;
4
4
import * as path from 'path' ;
5
+ import { Readable } from 'stream' ;
5
6
6
7
import { request , GaxiosResponse } from 'gaxios' ;
7
8
@@ -222,16 +223,15 @@ export class LinkChecker extends EventEmitter {
222
223
// Perform a HEAD or GET request based on the need to crawl
223
224
let status = 0 ;
224
225
let state = LinkState . BROKEN ;
225
- let data = '' ;
226
226
let shouldRecurse = false ;
227
- let res : GaxiosResponse < string > | undefined = undefined ;
227
+ let res : GaxiosResponse < Readable > | undefined = undefined ;
228
228
const failures : { } [ ] = [ ] ;
229
229
try {
230
- res = await request < string > ( {
230
+ res = await request < Readable > ( {
231
231
method : opts . crawl ? 'GET' : 'HEAD' ,
232
232
url : opts . url . href ,
233
233
headers,
234
- responseType : opts . crawl ? 'text' : 'stream' ,
234
+ responseType : 'stream' ,
235
235
validateStatus : ( ) => true ,
236
236
timeout : opts . checkOptions . timeout ,
237
237
} ) ;
@@ -241,7 +241,7 @@ export class LinkChecker extends EventEmitter {
241
241
242
242
// If we got an HTTP 405, the server may not like HEAD. GET instead!
243
243
if ( res . status === 405 ) {
244
- res = await request < string > ( {
244
+ res = await request < Readable > ( {
245
245
method : 'GET' ,
246
246
url : opts . url . href ,
247
247
headers,
@@ -257,7 +257,7 @@ export class LinkChecker extends EventEmitter {
257
257
// request failure: invalid domain name, etc.
258
258
// this also occasionally catches too many redirects, but is still valid (e.g. https://www.ebay.com)
259
259
// for this reason, we also try doing a GET below to see if the link is valid
260
- failures . push ( err ) ;
260
+ failures . push ( err as Error ) ;
261
261
}
262
262
263
263
try {
@@ -266,10 +266,10 @@ export class LinkChecker extends EventEmitter {
266
266
( res === undefined || res . status < 200 || res . status >= 300 ) &&
267
267
! opts . crawl
268
268
) {
269
- res = await request < string > ( {
269
+ res = await request < Readable > ( {
270
270
method : 'GET' ,
271
271
url : opts . url . href ,
272
- responseType : 'text ' ,
272
+ responseType : 'stream ' ,
273
273
validateStatus : ( ) => true ,
274
274
headers,
275
275
timeout : opts . checkOptions . timeout ,
@@ -279,13 +279,12 @@ export class LinkChecker extends EventEmitter {
279
279
}
280
280
}
281
281
} catch ( ex ) {
282
- failures . push ( ex ) ;
282
+ failures . push ( ex as Error ) ;
283
283
// catch the next failure
284
284
}
285
285
286
286
if ( res !== undefined ) {
287
287
status = res . status ;
288
- data = res . data ;
289
288
shouldRecurse = isHtml ( res ) ;
290
289
}
291
290
@@ -309,7 +308,9 @@ export class LinkChecker extends EventEmitter {
309
308
// If we need to go deeper, scan the next level of depth for links and crawl
310
309
if ( opts . crawl && shouldRecurse ) {
311
310
this . emit ( 'pagestart' , opts . url ) ;
312
- const urlResults = getLinks ( data , opts . url . href ) ;
311
+ const urlResults = res ?. data
312
+ ? await getLinks ( res . data , opts . url . href )
313
+ : [ ] ;
313
314
for ( const result of urlResults ) {
314
315
// if there was some sort of problem parsing the link while
315
316
// creating a new URL obj, treat it as a broken link.
0 commit comments