Skip to content

Commit

Permalink
refactor(wip): async iterators
Browse files Browse the repository at this point in the history
Switches JS API to async iterators where possible.

Includes switch to libp2p config override via  ipfs/js-ipfs#2591

BREAKING CHANGE: switched to Async Iterators version of JS API
https://blog.ipfs.io/2020-02-01-async-await-refactor/
  • Loading branch information
lidel committed Apr 26, 2020
1 parent 9467fd2 commit 01c1925
Show file tree
Hide file tree
Showing 21 changed files with 2,024 additions and 2,810 deletions.
79 changes: 0 additions & 79 deletions add-on/src/lib/dir-view.js

This file was deleted.

49 changes: 42 additions & 7 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 @@ -25,11 +32,10 @@ const chromeDefaultOpts = {
Swarm: [
// optional ws-star signaling 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-star.discovery.libp2p.io/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/node1.delegate.ipfs.io/tcp/443/https',
'/dns4/node0.delegate.ipfs.io/tcp/443/https'
]
Expand All @@ -42,8 +48,8 @@ const chromeDefaultOpts = {
},
Swarm: {
ConnMgr: {
LowWater: 100,
HighWater: 250
LowWater: 50,
HighWater: 150
}
},
Bootstrap: [
Expand Down Expand Up @@ -113,7 +119,36 @@ 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
}
}
})

libp2pOptions.metrics = { enabled: false }

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

Expand Down
51 changes: 8 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,15 @@ exports.init = async function init (opts) {
log('init embedded:chromesockets')

const ipfsOpts = await buildConfig(opts, log)

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

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()
await syncConfig(node, opts, log)
return node
}

exports.destroy = async function () {
Expand All @@ -74,21 +53,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
}
}
107 changes: 0 additions & 107 deletions add-on/src/lib/ipfs-client/embedded-chromesockets/libp2p-bundle.js

This file was deleted.

Loading

0 comments on commit 01c1925

Please sign in to comment.