Skip to content

Commit

Permalink
refactor: the async await extravaganza (#811)
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed Oct 5, 2020
1 parent a219d2b commit 1ebf389
Show file tree
Hide file tree
Showing 28 changed files with 7,018 additions and 6,994 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- 10.15.1
- lts/*
matrix:
fast_finish: true
allow_failures:
Expand Down
2 changes: 1 addition & 1 deletion add-on/src/landing-pages/welcome/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const renderResources = (i18n) => {
<p class="${labelClass}">${i18n.getMessage('page_landingWelcome_resources_title_new_ipfs')}</p>
<ul class="${copyClass}">
<li>${renderTranslatedLinks('page_landingWelcome_resources_new_ipfs_companion_features', ['https://github.com/ipfs-shipyard/ipfs-companion#features'], `target="_blank" class="${anchorClass}"`)}</li>
<li>${renderTranslatedLinks('page_landingWelcome_resources_new_ipfs_companion_features', ['https://github.com/ipfs-shipyard/ipfs-companion#ipfs-companion-features'], `target="_blank" class="${anchorClass}"`)}</li>
<li>${renderTranslatedLinks('page_landingWelcome_resources_new_ipfs_concepts', ['https://docs.ipfs.io/concepts/how-ipfs-works/'], `target="_blank" class="${anchorClass}"`)}</li>
<li>${renderTranslatedLinks('page_landingWelcome_resources_new_ipfs_docs', ['https://docs.ipfs.io'], `target="_blank" class="${anchorClass}"`)}</li>
</ul>
Expand Down
79 changes: 0 additions & 79 deletions add-on/src/lib/dir-view.js

This file was deleted.

1 change: 0 additions & 1 deletion add-on/src/lib/dnslink.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ module.exports = function createDnslinkResolver (getState) {
}
} catch (error) {
log.error(`error in readAndCacheDnslink for '${fqdn}'`, error)
console.error(error)
}
} else {
// Most of the time we will hit cache, which makes below line is too noisy
Expand Down
155 changes: 106 additions & 49 deletions add-on/src/lib/ipfs-client/embedded-chromesockets/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@
const browser = require('webextension-polyfill')

const { optionDefaults } = require('../../options')
const chromeSocketsBundle = require('./libp2p-bundle')
const mergeOptions = require('merge-options')
const getPort = require('get-port')
const { getIPv4, getIPv6 } = require('webrtc-ips')

const Libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const MulticastDNS = require('libp2p-mdns')

const multiaddr = require('multiaddr')
const maToUri = require('multiaddr-to-uri')
const multiaddr2httpUrl = (ma) => maToUri(ma.includes('/http') ? ma : multiaddr(ma).encapsulate('/http'))

const debug = require('debug')
const log = debug('ipfs-companion:client:embedded:config')
log.error = debug('ipfs-companion:client:embedded:config:error')

// additional default js-ipfs config specific to runtime with chrome.sockets APIs
const chromeDefaultOpts = {
config: {
Expand All @@ -23,15 +30,17 @@ const chromeDefaultOpts = {
but Web UI needs API (can't use window.ipfs due to sandboxing)
*/
Swarm: [
// optional ws-star signaling provides a backup for non-LAN peer discovery
// additional signaling service provides a backup for non-LAN peer discovery
// (this will be removed when autorelay and DHT are stable in js-ipfs)
'/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star'
'/dns4/wrtc-star1.par.dwebops.pub/tcp/443/wss/p2p-webrtc-star',
'/dns4/wrtc-star2.sjc.dwebops.pub/tcp/443/wss/p2p-webrtc-star'
],
// Delegated Content and Peer Routing: https://github.com/ipfs/js-ipfs/pull/2195
Delegates: // [] // TODO: enable delegates
[
Delegates: [
'/dns4/node0.delegate.ipfs.io/tcp/443/https',
'/dns4/node1.delegate.ipfs.io/tcp/443/https',
'/dns4/node0.delegate.ipfs.io/tcp/443/https'
'/dns4/node2.delegate.ipfs.io/tcp/443/https',
'/dns4/node3.delegate.ipfs.io/tcp/443/https'
]
},
Discovery: {
Expand All @@ -42,48 +51,47 @@ const chromeDefaultOpts = {
},
Swarm: {
ConnMgr: {
LowWater: 100,
HighWater: 250
LowWater: 50,
HighWater: 200
}
},
Bootstrap: [
// Prioritize TCP Bootstrappers from https://github.com/ipfs/js-ipfs/blob/v0.37.1/src/core/runtime/config-nodejs.js#L22
'/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
'/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
'/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
'/ip4/162.243.248.213/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
'/ip4/128.199.219.111/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
'/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
'/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
'/ip4/178.62.61.185/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
'/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx',
'/ip6/2604:a880:1:20::1f9:9001/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
'/ip6/2604:a880:1:20::203:d001/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
'/ip6/2604:a880:0:1010::23:d001/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
'/ip6/2400:6180:0:d0::151:6001/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
'/ip6/2604:a880:800:10::4a:5001/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
'/ip6/2a03:b0c0:0:1010::23:1001/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
'/ip6/2a03:b0c0:1:d0::e7:1/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
'/ip6/2604:a880:1:20::1d9:6001/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx',
// Twist: connect to preload nodes, but over tcp :)
'/dns4/node0.preload.ipfs.io/tcp/4001/ipfs/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic',
'/dns4/node1.preload.ipfs.io/tcp/4001/ipfs/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6',
// WebSockets versions from https://github.com/ipfs/js-ipfs/blob/v0.37.1/src/core/runtime/config-browser.js#L20
'/dns4/ams-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
'/dns4/lon-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
'/dns4/sfo-3.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
'/dns4/sgp-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
'/dns4/nyc-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
'/dns4/nyc-2.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
'/dns4/node0.preload.ipfs.io/tcp/443/wss/ipfs/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic',
'/dns4/node1.preload.ipfs.io/tcp/443/wss/ipfs/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6'
// Prioritize TCP Bootstrappers from https://github.com/ipfs/js-ipfs/blob/ipfs%400.50.2/packages/ipfs/src/core/runtime/config-nodejs.js
'/ip4/104.236.176.52/tcp/4001/p2p/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
'/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
'/ip4/104.236.179.241/tcp/4001/p2p/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
'/ip4/162.243.248.213/tcp/4001/p2p/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
'/ip4/128.199.219.111/tcp/4001/p2p/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
'/ip4/104.236.76.40/tcp/4001/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
'/ip4/178.62.158.247/tcp/4001/p2p/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
'/ip4/178.62.61.185/tcp/4001/p2p/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
'/ip4/104.236.151.122/tcp/4001/p2p/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx',
'/ip6/2604:a880:1:20::1f9:9001/tcp/4001/p2p/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
'/ip6/2604:a880:1:20::203:d001/tcp/4001/p2p/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
'/ip6/2604:a880:0:1010::23:d001/tcp/4001/p2p/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
'/ip6/2400:6180:0:d0::151:6001/tcp/4001/p2p/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
'/ip6/2604:a880:800:10::4a:5001/tcp/4001/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
'/ip6/2a03:b0c0:0:1010::23:1001/tcp/4001/p2p/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
'/ip6/2a03:b0c0:1:d0::e7:1/tcp/4001/p2p/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
'/ip6/2604:a880:1:20::1d9:6001/tcp/4001/p2p/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx',
'/dns4/node0.preload.ipfs.io/tcp/443/wss/p2p/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic',
'/dns4/node1.preload.ipfs.io/tcp/443/wss/p2p/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6',
'/dns4/node2.preload.ipfs.io/tcp/443/wss/p2p/QmV7gnbW5VTcJ3oyM2Xk1rdFBJ3kTkvxc87UFGsun29STS',
'/dns4/node3.preload.ipfs.io/tcp/443/wss/p2p/QmY7JB6MQXhxHvq7dBDh4HpbH29v4yE9JRadAVpndvzySN',
// WebSockets versions from https://github.com/ipfs/js-ipfs/blob/ipfs%400.50.2/packages/ipfs/src/core/runtime/config-browser.js
'/dns4/ams-1.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
'/dns4/lon-1.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
'/dns4/sfo-3.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
'/dns4/sgp-1.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
'/dns4/nyc-1.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
'/dns4/nyc-2.bootstrap.libp2p.io/tcp/443/wss/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64'
]
},
// preload: { enabled: false, addresses: [] }
/* TODO: when we have p2p transport, are preloads still needed? should Brave have own nodes? */
preload: {
enabled: true,
addresses: [
'/dns4/node3.preload.ipfs.io/tcp/443/https',
'/dns4/node2.preload.ipfs.io/tcp/443/https',
'/dns4/node1.preload.ipfs.io/tcp/443/https',
'/dns4/node0.preload.ipfs.io/tcp/443/https'
]
Expand Down Expand Up @@ -113,7 +121,53 @@ async function buildConfig (opts, log) {
// merge configs
const finalOpts = {
start: false,
libp2p: chromeSocketsBundle
// a function that customizes libp2p config: https://github.com/ipfs/js-ipfs/pull/2591
libp2p: ({ libp2pOptions, peerInfo }) => {
libp2pOptions.modules = mergeOptions.call({ concatArrays: true }, libp2pOptions.modules, {
transports: [TCP]
})

libp2pOptions.modules = mergeOptions.call({ concatArrays: true }, libp2pOptions.modules, {
peerDiscovery: [MulticastDNS]
})

libp2pOptions.config = mergeOptions(libp2pOptions.config, {
peerDiscovery: {
autoDial: true,
mdns: {
enabled: true
},
bootstrap: {
enabled: true
},
webRTCStar: {
enabled: true
},
dht: {
kBucketSize: 20,
enabled: false,
clientMode: true,
randomWalk: {
enabled: false
}
},
pubsub: {
enabled: true,
emitSelf: true
}
}
})

libp2pOptions.dialer = {
// https://github.com/ipfs/js-ipfs/blob/ipfs%400.49.0/packages/ipfs/src/core/runtime/libp2p-browser.js#L14
maxParallelDials: 150, // 150 total parallel multiaddr dials
maxDialsPerPeer: 4, // Allow 4 multiaddrs to be dialed per peer in parallel
dialTimeout: 10e3 // 10 second dial timeout per peer dial
}

log('initializing libp2p with libp2pOptions', libp2pOptions)
return new Libp2p(libp2pOptions)
}
}
const ipfsNodeConfig = mergeOptions(defaultOpts, userOpts, chromeOpts, finalOpts)

Expand All @@ -135,11 +189,11 @@ async function buildConfig (opts, log) {
return ipfsNodeConfig
}

async function syncConfig (ipfs, opts, log) {
async function syncConfig (liveConfig, log) {
const storedConfig = await browser.storage.local.get('ipfsNodeConfig')
if (storedConfig && storedConfig.ipfsNodeConfig) {
const maGw = await ipfs.config.get('Addresses.Gateway')
const maApi = await ipfs.config.get('Addresses.API')
const maGw = liveConfig.config.Addresses.Gateway
const maApi = liveConfig.config.Addresses.API
const httpGw = multiaddr2httpUrl(maGw)
const httpApi = multiaddr2httpUrl(maApi)
// update config in browser.storage to ports from js-ipfs instance
Expand All @@ -154,12 +208,15 @@ async function syncConfig (ipfs, opts, log) {
cfg.config.Addresses.Gateway = maGw
cfg.config.Addresses.API = maApi
changes.ipfsNodeConfig = JSON.stringify(cfg, null, 2)
// update runtime config in place
Object.assign(liveConfig, changes)
// save config to browser.storage (triggers async client restart if ports changed)
log(`synchronizing ipfsNodeConfig with customGatewayUrl (${changes.customGatewayUrl}) and ipfsApiUrl (${changes.ipfsApiUrl})`)
await browser.storage.local.set(changes)
log('reloading extension due to TCP port config change')
browser.tabs.reload() // async reload of options page to keep it alive
await browser.runtime.reload()
}
// update runtime config in place
Object.assign(opts, changes)
// save config to browser.storage (triggers async client restart if ports changed)
log(`synchronizing ipfsNodeConfig with customGatewayUrl (${changes.customGatewayUrl}) and ipfsApiUrl (${changes.ipfsApiUrl})`)
await browser.storage.local.set(changes)
}
}

Expand Down
53 changes: 10 additions & 43 deletions add-on/src/lib/ipfs-client/embedded-chromesockets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,17 @@ exports.init = async function init (opts) {
log('init embedded:chromesockets')

const ipfsOpts = await buildConfig(opts, log)
await syncConfig(ipfsOpts, log)

log('creating js-ipfs with opts: ', ipfsOpts)
node = new Ipfs(ipfsOpts)
node = await Ipfs.create(ipfsOpts)
await node.start()

return new Promise((resolve, reject) => {
node.once('error', (error) => {
log.error('something went terribly wrong during startup of js-ipfs!', error)
reject(error)
})
node.once('ready', async () => {
node.once('start', async () => {
// HttpApi is off in browser context and needs to be started separately
try {
const httpServers = new HttpApi(node, ipfsOpts)
nodeHttpApi = await httpServers.start()
await syncConfig(node, opts, log)
resolve(node)
} catch (err) {
reject(err)
}
})
try {
node.on('error', error => {
log.error('something went terribly wrong in embedded js-ipfs!', error)
})
await node.start()
} catch (err) {
reject(err)
}
})
})
log('starting HTTP servers with opts: ', ipfsOpts)
const httpServers = new HttpApi(node, ipfsOpts)
nodeHttpApi = await httpServers.start()

return node
}

exports.destroy = async function () {
Expand All @@ -74,21 +55,7 @@ exports.destroy = async function () {
nodeHttpApi = null
}
if (node) {
const stopped = new Promise((resolve, reject) => {
node.on('stop', resolve)
node.on('error', reject)
})
try {
await node.stop()
} catch (err) {
// TODO: remove when fixed upstream: https://github.com/ipfs/js-ipfs/issues/2257
if (err.message === 'Not able to stop from state: stopping') {
log('destroy: embedded:chromesockets waiting for node.stop()')
await stopped
} else {
throw err
}
}
await node.stop()
node = null
}
}
Loading

0 comments on commit 1ebf389

Please sign in to comment.