Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: the async await extravaganza #811

Merged
merged 20 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor(wip): async iterators
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 Aug 12, 2020
commit f9249b485f415d0d7a86561caaf8e62135edcce4
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