Skip to content

Commit bfb4ee3

Browse files
committed
Don't do dnslookups when API is down
- adds `.isUp` property to `api.js` - also closes #54
1 parent b391dc9 commit bfb4ee3

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

lib/api.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,15 @@ const { Request } = require('sdk/request')
77
const { XMLHttpRequest } = require('sdk/net/xhr')
88
const { URL } = require('sdk/url')
99

10+
const pw = require('./peer-watch.js')
1011
const dnsCache = require('./dns-cache.js')
1112

13+
Object.defineProperty(exports, 'isUp', {
14+
get: function () {
15+
return pw.peerCount != null
16+
}
17+
})
18+
1219
function apiUrl (path) {
1320
return URL('http://' + prefs.customGatewayHost + ':' + prefs.customApiPort + '/api/v0/' + path).toString()
1421
}
@@ -69,7 +76,7 @@ function isDnslinkPresent (fqdn) {
6976

7077
function isDnslinkPresentCached (fqdn) {
7178
let ipfsSupport = dnsCache.get(fqdn)
72-
if (typeof ipfsSupport === 'undefined') {
79+
if (typeof ipfsSupport === 'undefined' && exports.isUp) {
7380
ipfsSupport = isDnslinkPresent(fqdn)
7481
if (ipfsSupport !== null) { // dont cache is API was down
7582
dnsCache.put(fqdn, ipfsSupport)

lib/gui.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ panel.port.on('open-preferences', hidePanelAnd(function () {
171171
onReady: function (tab) {
172172
tab.attach({
173173
contentScriptWhen: 'end',
174-
contentScript: 'AddonManager.getAddonByID("' + self.id + '", function(aAddon) {' +
174+
contentScript: 'if (typeof AddonManager !== \'undefined\'){AddonManager.getAddonByID("' + self.id + '", function(aAddon) {' +
175175
'unsafeWindow.gViewController.commands.cmd_showItemDetails.doCommand(aAddon, true);' +
176-
'});'
176+
'});}'
177177
})
178178
}
179179
})

lib/peer-watch.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ const gui = require('./gui.js')
88
const gw = require('./gateways.js')
99
const _ = require('sdk/l10n').get
1010

11+
let peerCount = null
12+
1113
function ipfsHealthCheck () {
1214
api.getSwarmPeers((peers) => {
13-
const peerCount = (peers && peers.Strings) ? peers.Strings.length : null
15+
peerCount = (peers && peers.Strings) ? peers.Strings.length : null
1416
gui.setButtonBadge(peerCount)
1517

1618
if (prefs.automatic) {
@@ -38,3 +40,9 @@ require('sdk/system/unload').when(() => {
3840
})
3941

4042
exports.triggerManualCheck = ipfsHealthCheck
43+
44+
Object.defineProperty(exports, 'peerCount', {
45+
get: function () {
46+
return peerCount
47+
}
48+
})

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
"description": "Access IPFS resources via custom HTTP2IPFS gateway",
66
"author": "Marcin Rataj",
7-
"version": "1.5.0",
7+
"version": "1.5.1",
88
"license": "CC0-1.0",
99
"homepage": "https://github.com/lidel/ipfs-firefox-addon",
1010
"icon": "resource://ipfs-firefox-addon-at-lidel-dot-org/data/icon-on-64.png",

0 commit comments

Comments
 (0)