@@ -96,8 +96,9 @@ function createRequestModifier (getState, dnslinkResolver, ipfsPathValidator, ru
96
96
}
97
97
98
98
const postNormalizationSkip = ( state , request ) => {
99
- // skip requests to the public gateway if embedded node is running (otherwise we have too much recursion)
100
- if ( state . ipfsNodeType === 'embedded' && sameGateway ( request . url , state . pubGwURL ) ) {
99
+ // skip requests to the public gateway if we can't reedirect them to local
100
+ // node is running (otherwise we have too much recursion)
101
+ if ( ! state . localGwAvailable && sameGateway ( request . url , state . pubGwURL ) ) {
101
102
ignore ( request . requestId )
102
103
// TODO: do not skip and redirect to `ipfs://` and `ipns://` if hasNativeProtocolHandler === true
103
104
}
@@ -351,7 +352,9 @@ function createRequestModifier (getState, dnslinkResolver, ipfsPathValidator, ru
351
352
// All the following requests will be upgraded to IPNS
352
353
const cachedDnslink = dnslinkResolver . readAndCacheDnslink ( new URL ( request . url ) . hostname )
353
354
const redirectUrl = dnslinkResolver . dnslinkAtGateway ( request . url , cachedDnslink )
354
- if ( redirectUrl ) {
355
+ // redirect only if local node is around, as we can't guarantee DNSLink support
356
+ // at a public subdomain gateway (requires more than 1 level of wildcard TLS certs)
357
+ if ( redirectUrl && state . localGwAvailable ) {
355
358
log ( `onHeadersReceived: dnslinkRedirect from ${ request . url } to ${ redirectUrl } ` )
356
359
return { redirectUrl }
357
360
}
@@ -371,8 +374,8 @@ function createRequestModifier (getState, dnslinkResolver, ipfsPathValidator, ru
371
374
const url = new URL ( request . url )
372
375
const pathWithArgs = `${ xIpfsPath } ${ url . search } ${ url . hash } `
373
376
const newUrl = pathAtHttpGateway ( pathWithArgs , state . pubGwURLString )
374
- // redirect only if anything changed
375
- if ( newUrl !== request . url ) {
377
+ // redirect only if local node is around
378
+ if ( newUrl && state . localGwAvailable ) {
376
379
log ( `onHeadersReceived: normalized ${ request . url } to ${ newUrl } ` )
377
380
return redirectToGateway ( request , newUrl , state , ipfsPathValidator )
378
381
}
@@ -407,9 +410,10 @@ function createRequestModifier (getState, dnslinkResolver, ipfsPathValidator, ru
407
410
if ( isRecoverableViaEthDNS ( request , state ) ) {
408
411
const url = new URL ( request . url )
409
412
url . hostname = `${ url . hostname } .link`
410
- const redirect = { redirectUrl : url . toString ( ) }
411
- log ( `onErrorOccurred: attempting to recover from DNS error (${ request . error } ) using EthDNS for ${ request . url } → ${ redirect . redirectUrl } ` , request )
412
- return createTabWithURL ( redirect , browser , recoveredTabs )
413
+ const redirectUrl = url . toString ( )
414
+ log ( `onErrorOccurred: attempting to recover from DNS error (${ request . error } ) using EthDNS for ${ request . url } → ${ redirectUrl } ` , request )
415
+ // TODO: update existing tab
416
+ return createTabWithURL ( request , redirectUrl , browser , recoveredTabs )
413
417
}
414
418
415
419
// Check if error can be recovered via DNSLink
@@ -419,7 +423,7 @@ function createRequestModifier (getState, dnslinkResolver, ipfsPathValidator, ru
419
423
if ( dnslink ) {
420
424
const redirectUrl = dnslinkResolver . dnslinkAtGateway ( request . url , dnslink )
421
425
log ( `onErrorOccurred: attempting to recover from network error (${ request . error } ) using dnslink for ${ request . url } → ${ redirectUrl } ` , request )
422
- return createTabWithURL ( { redirectUrl } , browser , recoveredTabs )
426
+ return createTabWithURL ( request , redirectUrl , browser , recoveredTabs )
423
427
}
424
428
}
425
429
@@ -433,7 +437,7 @@ function createRequestModifier (getState, dnslinkResolver, ipfsPathValidator, ru
433
437
if ( isRecoverable ( request , state , ipfsPathValidator ) ) {
434
438
const redirectUrl = ipfsPathValidator . resolveToPublicUrl ( request . url )
435
439
log ( `onErrorOccurred: attempting to recover from network error (${ request . error } ) for ${ request . url } → ${ redirectUrl } ` , request )
436
- return createTabWithURL ( { redirectUrl } , browser , recoveredTabs )
440
+ return createTabWithURL ( request , redirectUrl , browser , recoveredTabs )
437
441
}
438
442
} ,
439
443
@@ -463,7 +467,7 @@ function createRequestModifier (getState, dnslinkResolver, ipfsPathValidator, ru
463
467
if ( isRecoverable ( request , state , ipfsPathValidator ) ) {
464
468
const redirectUrl = ipfsPathValidator . resolveToPublicUrl ( request . url )
465
469
log ( `onCompleted: attempting to recover from HTTP Error ${ request . statusCode } for ${ request . url } → ${ redirectUrl } ` , request )
466
- return createTabWithURL ( { redirectUrl } , browser , recoveredTabs )
470
+ return createTabWithURL ( request , redirectUrl , browser , recoveredTabs )
467
471
}
468
472
}
469
473
}
@@ -474,11 +478,8 @@ exports.createRequestModifier = createRequestModifier
474
478
475
479
function redirectToGateway ( request , url , state , ipfsPathValidator ) {
476
480
const { resolveToPublicUrl, resolveToLocalUrl } = ipfsPathValidator
477
- const useLocal = state . ipfsNodeType !== 'embedded'
478
- const redirectUrl = useLocal ? resolveToLocalUrl ( url ) : resolveToPublicUrl ( url )
481
+ const redirectUrl = state . localGwAvailable ? resolveToLocalUrl ( url ) : resolveToPublicUrl ( url )
479
482
// redirect only if we actually change anything
480
- console . log ( 'request.url' , request . url )
481
- console . log ( 'redirectUrl' , redirectUrl )
482
483
if ( redirectUrl && request . url !== redirectUrl ) return { redirectUrl }
483
484
}
484
485
@@ -588,11 +589,17 @@ function findHeaderIndex (name, headers) {
588
589
589
590
// Recovery check for onErrorOccurred (request.error) and onCompleted (request.statusCode)
590
591
function isRecoverable ( request , state , ipfsPathValidator ) {
591
- return state . recoverFailedHttpRequests &&
592
+ // Note: we are unable to recover default public gateways without a local one
593
+ const { error, statusCode, url } = request
594
+ const { redirect, localGwAvailable, pubGwURL, pubSubdomainGwURL } = state
595
+ return ( state . recoverFailedHttpRequests &&
592
596
request . type === 'main_frame' &&
593
- ( recoverableNetworkErrors . has ( request . error ) || recoverableHttpError ( request . statusCode ) ) &&
594
- ( ipfsPathValidator . publicIpfsOrIpnsResource ( request . url ) || isIPFS . subdomain ( request . url ) ) &&
595
- ! sameGateway ( request . url , state . pubGwURL ) && ! sameGateway ( request . url , state . pubSubdomainGwURL )
597
+ ( recoverableNetworkErrors . has ( error ) ||
598
+ recoverableHttpError ( statusCode ) ) &&
599
+ ipfsPathValidator . publicIpfsOrIpnsResource ( url ) &&
600
+ ( ( redirect && localGwAvailable ) ||
601
+ ( ! sameGateway ( url , pubGwURL ) &&
602
+ ! sameGateway ( url , pubSubdomainGwURL ) ) ) )
596
603
}
597
604
598
605
// Recovery check for onErrorOccurred (request.error)
@@ -616,8 +623,11 @@ function isRecoverableViaEthDNS (request, state) {
616
623
// We can't redirect in onErrorOccurred/onCompleted
617
624
// Indead, we recover by opening URL in a new tab that replaces the failed one
618
625
// TODO: display an user-friendly prompt when the very first recovery is done
619
- async function createTabWithURL ( redirect , browser , recoveredTabs ) {
620
- const tabKey = redirect . redirectUrl
626
+ async function createTabWithURL ( request , redirectUrl , browser , recoveredTabs ) {
627
+ // Do nothing if the URL remains the same
628
+ if ( request . url === redirectUrl ) return
629
+
630
+ const tabKey = redirectUrl
621
631
// reuse existing tab, if exists
622
632
// (this avoids duplicated tabs - https://github.com/ipfs-shipyard/ipfs-companion/issues/805)
623
633
try {
@@ -635,7 +645,7 @@ async function createTabWithURL (redirect, browser, recoveredTabs) {
635
645
const newTab = await browser . tabs . create ( {
636
646
active : true ,
637
647
openerTabId,
638
- url : redirect . redirectUrl
648
+ url : redirectUrl
639
649
} )
640
650
if ( newTab ) recoveredTabs . set ( tabKey , newTab . id )
641
651
}
0 commit comments