Skip to content

Commit

Permalink
fix(dnslink): try to recover only from final errors
Browse files Browse the repository at this point in the history
This change ignores most of errors
and attempts to recover via dnslink only if error is in a safe set.

- browser vendors use different error codes
  and need to be added to the safe set
- dnslink recovery ignores non-whitelisted error codes
  • Loading branch information
lidel committed Aug 27, 2018
1 parent 44f6854 commit a433684
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
20 changes: 15 additions & 5 deletions add-on/src/lib/ipfs-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
const IsIpfs = require('is-ipfs')
const { urlAtPublicGw } = require('./ipfs-path')
const redirectOptOutHint = 'x-ipfs-companion-no-redirect'
const recoverableErrors = new Set([
// Firefox
'NS_ERROR_NET_RESET', // failed to load because the server kept reseting the connection
'NS_ERROR_NET_ON_RESOLVED', // no network
// Chrome
'net::ERR_INTERNET_DISCONNECTED' // no network
])

// Tracking late redirects for edge cases such as https://github.com/ipfs-shipyard/ipfs-companion/issues/436
const onHeadersReceivedRedirect = new Set()
Expand Down Expand Up @@ -166,11 +173,14 @@ function createRequestModifier (getState, dnslinkResolver, ipfsPathValidator, ru
const state = getState()

if (state.active) {
// Identify first request
const mainRequest = request.type === 'main_frame'

// Try to recover via DNSLink
if (mainRequest && dnslinkResolver.canLookupURL(request.url)) {
console.log('onErrorOccurred:' + request.error)
console.dir('onErrorOccurred', request)
// Check if error is final and can be recovered via DNSLink
const recoverableViaDnslink =
state.dnslinkPolicy &&
request.type === 'main_frame' &&
recoverableErrors.has(request.error)
if (recoverableViaDnslink && dnslinkResolver.canLookupURL(request.url)) {
// Explicit call to ignore global DNSLink policy and force DNS TXT lookup
const cachedDnslink = dnslinkResolver.readAndCacheDnslink(new URL(request.url).hostname)
const dnslinkRedirect = dnslinkResolver.dnslinkRedirect(request.url, cachedDnslink)
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
},
"dependencies": {
"choo": "6.13.0",
"debug": "3.1.0",
"doc-sniff": "1.0.1",
"drag-and-drop-files": "0.0.1",
"file-type": "9.0.0",
Expand Down

0 comments on commit a433684

Please sign in to comment.