Skip to content

Commit e457302

Browse files
committed
fix: HTTP recovery should respect redirect state
This ensures that HTTP recovery will use public gateway instead of local one when redirect to local node is disabled in Preferences.
1 parent 6e7a3a8 commit e457302

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

add-on/src/lib/dnslink.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ module.exports = function createDnslinkResolver (getState) {
5959
// to load the correct path from IPFS
6060
// - https://github.com/ipfs/ipfs-companion/issues/298
6161
const ipnsPath = dnslinkResolver.convertToIpnsPath(url)
62-
const gateway = state.localGwAvailable ? state.gwURLString : state.pubGwURLString
62+
const gateway = state.redirect && state.localGwAvailable ? state.gwURLString : state.pubGwURLString
6363
return pathAtHttpGateway(ipnsPath, gateway)
6464
}
6565
},

test/functional/lib/dnslink.test.js

+22-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict'
2-
const { describe, it, before, after } = require('mocha')
2+
const { describe, it, before, beforeEach, after } = require('mocha')
33
const { expect } = require('chai')
44
const { URL } = require('url')
55
const sinon = require('sinon')
@@ -58,10 +58,15 @@ describe('dnslinkResolver (dnslinkPolicy=detectIpfsPathHeader)', function () {
5858
global.URL = URL
5959
})
6060

61-
const getState = () => Object.assign(initState(testOptions), {
62-
ipfsNodeType: 'external',
63-
dnslinkPolicy: 'detectIpfsPathHeader',
64-
peerCount: 1
61+
let getState
62+
beforeEach(() => {
63+
// ensure each case uses clean state
64+
getState = () => Object.assign(initState(testOptions), {
65+
ipfsNodeType: 'external',
66+
dnslinkPolicy: 'detectIpfsPathHeader',
67+
redirect: true,
68+
peerCount: 1
69+
})
6570
})
6671
const getExternalNodeState = () => Object.assign(getState(), { ipfsNodeType: 'external' })
6772
const getEmbeddedNodeState = () => Object.assign(getState(), { ipfsNodeType: 'embedded' })
@@ -86,6 +91,18 @@ describe('dnslinkResolver (dnslinkPolicy=detectIpfsPathHeader)', function () {
8691
expect(dnslinkResolver.dnslinkAtGateway(url.toString()))
8792
.to.equal('http://localhost:8080/ipns/dnslinksite4.io/foo/barl?a=b#c=d')
8893
})
94+
it('[external node] should return redirect to public gateway if dnslink is present in cache but redirect to local gw is off', function () {
95+
const oldState = getState
96+
getState = () => Object.assign(oldState(), { redirect: false })
97+
const url = new URL('https://dnslinksite4.io/foo/barl?a=b#c=d')
98+
const dnslinkResolver = createDnslinkResolver(getExternalNodeState)
99+
dnslinkResolver.setDnslink(url.hostname, '/ipfs/bafybeigxjv2o4jse2lajbd5c7xxl5rluhyqg5yupln42252e5tcao7hbge')
100+
expectNoDnsTxtRecordLookup(url.hostname, dnslinkResolver)
101+
// note: locahost will redirect to subdomain if its go-ipfs >0.5,
102+
// so companion does not need to handle that
103+
expect(dnslinkResolver.dnslinkAtGateway(url.toString()))
104+
.to.equal('https://gateway.foobar.io/ipns/dnslinksite4.io/foo/barl?a=b#c=d')
105+
})
89106
it('[embedded node] should return redirect to public gateway if dnslink is present in cache', function () {
90107
const url = new URL('https://dnslinksite4.io/foo/barl?a=b#c=d')
91108
const dnslinkResolver = createDnslinkResolver(getEmbeddedNodeState)

0 commit comments

Comments
 (0)