Skip to content

Commit

Permalink
feat: default to brave node (#968)
Browse files Browse the repository at this point in the history
This is a small change that will default to endpoint provided by Brave
if the current endpoint (by default 127.0.0.1:8080) is offline.

This means user won't need to go to preferences and switch the backend
to "Provided by Brave", new users will have that set for them
automatically.

It also fixes a bug where "Welcome" page did not update when enabling
Brave node required user interaction – we now reload internal pages
when a new backend finished its init chores.
  • Loading branch information
lidel authored Jan 20, 2021
1 parent 7e938e6 commit 80dea75
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
10 changes: 9 additions & 1 deletion add-on/src/lib/ipfs-client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,24 @@ function _isWebuiTab (url) {
return bundled || ipns
}

function _isInternalTab (url, extensionOrigin) {
return url.startsWith(extensionOrigin)
}

async function _reloadIpfsClientDependents (browser, instance, opts) {
// online || offline
if (browser.tabs && browser.tabs.query) {
const tabs = await browser.tabs.query({})
if (tabs) {
const extensionOrigin = browser.runtime.getURL('/')
tabs.forEach((tab) => {
// detect bundled webui in any of open tabs
if (_isWebuiTab(tab.url)) {
log(`reloading webui at ${tab.url}`)
browser.tabs.reload(tab.id)
} else if (_isInternalTab(tab.url, extensionOrigin)) {
log(`reloading internal extension page at ${tab.url}`)
browser.tabs.reload(tab.id)
log('reloading bundled webui')
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions add-on/src/lib/ipfs-companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const { createRuntimeChecks } = require('./runtime-checks')
const { createContextMenus, findValueForContext, contextMenuCopyAddressAtPublicGw, contextMenuCopyRawCid, contextMenuCopyCanonicalAddress, contextMenuViewOnGateway, contextMenuCopyPermalink, contextMenuCopyCidAddress } = require('./context-menus')
const createIpfsProxy = require('./ipfs-proxy')
const { registerSubdomainProxy } = require('./http-proxy')
const { showPendingLandingPages } = require('./on-installed')
const { runPendingOnInstallTasks } = require('./on-installed')

// init happens on addon load in background/background.js
module.exports = async function init () {
Expand Down Expand Up @@ -89,7 +89,7 @@ module.exports = async function init () {
await registerSubdomainProxy(getState, runtime, notify)
log('init done')
setApiStatusUpdateInterval(options.ipfsApiPollMs)
await showPendingLandingPages()
await runPendingOnInstallTasks()
} catch (error) {
log.error('Unable to initialize addon due to error', error)
if (notify) notify('notify_addonIssueTitle', 'notify_addonIssueMsg')
Expand Down
40 changes: 32 additions & 8 deletions add-on/src/lib/on-installed.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,48 @@ exports.updatePage = 'https://github.com/ipfs-shipyard/ipfs-companion/releases/t
exports.onInstalled = async (details) => {
// details.temporary === run via `npm run firefox`
if (details.reason === 'install' || details.temporary) {
await browser.storage.local.set({ showLandingPage: 'onInstallWelcome' })
await browser.storage.local.set({ onInstallTasks: 'onFirstInstall' })
} else if (details.reason === 'update' || details.temporary) {
await browser.storage.local.set({ showLandingPage: 'onVersionUpdate' })
await browser.storage.local.set({ onInstallTasks: 'onVersionUpdate' })
}
}

exports.showPendingLandingPages = async () => {
const { showLandingPage, displayReleaseNotes } = await browser.storage.local.get(['showLandingPage', 'displayReleaseNotes'])
switch (showLandingPage) {
case 'onInstallWelcome':
await browser.storage.local.remove('showLandingPage')
exports.runPendingOnInstallTasks = async () => {
const { onInstallTasks, displayReleaseNotes } = await browser.storage.local.get(['onInstallTasks', 'displayReleaseNotes'])
await browser.storage.local.remove('onInstallTasks')
switch (onInstallTasks) {
case 'onFirstInstall':
await useNativeNodeIfFeasible(browser)
return browser.tabs.create({
url: exports.welcomePage
})
case 'onVersionUpdate':
await browser.storage.local.remove('showLandingPage')
if (!displayReleaseNotes) return
await browser.storage.local.set({ dismissedUpdate: version })
return browser.tabs.create({ url: exports.updatePage + version })
}
}

async function useNativeNodeIfFeasible (browser) {
// lazy-loaded dependencies due to debug package
// depending on the value of localStorage.debug, which is set later
const debug = require('debug')
const log = debug('ipfs-companion:on-installed')
log.error = debug('ipfs-companion:on-installed:error')
const { brave, braveNodeType } = require('./ipfs-client/brave')
const { ipfsNodeType, ipfsApiUrl } = await browser.storage.local.get(['ipfsNodeType', 'ipfsApiUrl'])

// Brave >= v1.19 (https://brave.com/ipfs-support/)
if (typeof brave !== 'undefined' && ipfsNodeType !== braveNodeType) {
try {
log(`brave detected, but node type is ${ipfsNodeType}. testing external endpoint at ${ipfsApiUrl}`)
const response = await (await fetch(`${ipfsApiUrl}/api/v0/id`, { method: 'post' })).json()
if (typeof response.ID === 'undefined') throw new Error(`unable to read PeerID from API at ${ipfsApiUrl}`)
log(`endpoint is online, PeerID is ${response.ID}, nothing to do`)
} catch (e) {
log.error(`endpoint ${ipfsApiUrl} does not work`, e)
log('switching node type to one provided by brave')
await browser.storage.local.set({ ipfsNodeType: braveNodeType })
}
}
}

0 comments on commit 80dea75

Please sign in to comment.