Skip to content

Commit 01c1925

Browse files
committed
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/
1 parent 9467fd2 commit 01c1925

21 files changed

+2024
-2810
lines changed

add-on/src/lib/dir-view.js

Lines changed: 0 additions & 79 deletions
This file was deleted.

add-on/src/lib/ipfs-client/embedded-chromesockets/config.js

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,22 @@
33
const browser = require('webextension-polyfill')
44

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

10+
const Libp2p = require('libp2p')
11+
const TCP = require('libp2p-tcp')
12+
const MulticastDNS = require('libp2p-mdns')
13+
1114
const multiaddr = require('multiaddr')
1215
const maToUri = require('multiaddr-to-uri')
1316
const multiaddr2httpUrl = (ma) => maToUri(ma.includes('/http') ? ma : multiaddr(ma).encapsulate('/http'))
1417

18+
const debug = require('debug')
19+
const log = debug('ipfs-companion:client:embedded:config')
20+
log.error = debug('ipfs-companion:client:embedded:config:error')
21+
1522
// additional default js-ipfs config specific to runtime with chrome.sockets APIs
1623
const chromeDefaultOpts = {
1724
config: {
@@ -25,11 +32,10 @@ const chromeDefaultOpts = {
2532
Swarm: [
2633
// optional ws-star signaling provides a backup for non-LAN peer discovery
2734
// (this will be removed when autorelay and DHT are stable in js-ipfs)
28-
'/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star'
35+
'/dns4/wrtc-star.discovery.libp2p.io/tcp/443/wss/p2p-webrtc-star'
2936
],
3037
// Delegated Content and Peer Routing: https://github.com/ipfs/js-ipfs/pull/2195
31-
Delegates: // [] // TODO: enable delegates
32-
[
38+
Delegates: [
3339
'/dns4/node1.delegate.ipfs.io/tcp/443/https',
3440
'/dns4/node0.delegate.ipfs.io/tcp/443/https'
3541
]
@@ -42,8 +48,8 @@ const chromeDefaultOpts = {
4248
},
4349
Swarm: {
4450
ConnMgr: {
45-
LowWater: 100,
46-
HighWater: 250
51+
LowWater: 50,
52+
HighWater: 150
4753
}
4854
},
4955
Bootstrap: [
@@ -113,7 +119,36 @@ async function buildConfig (opts, log) {
113119
// merge configs
114120
const finalOpts = {
115121
start: false,
116-
libp2p: chromeSocketsBundle
122+
// a function that customizes libp2p config: https://github.com/ipfs/js-ipfs/pull/2591
123+
libp2p: ({ libp2pOptions, peerInfo }) => {
124+
libp2pOptions.modules = mergeOptions.call({ concatArrays: true }, libp2pOptions.modules, {
125+
transports: [TCP]
126+
})
127+
128+
libp2pOptions.modules = mergeOptions.call({ concatArrays: true }, libp2pOptions.modules, {
129+
peerDiscovery: [MulticastDNS]
130+
})
131+
132+
libp2pOptions.config = mergeOptions(libp2pOptions.config, {
133+
peerDiscovery: {
134+
autoDial: true,
135+
mdns: {
136+
enabled: true
137+
},
138+
bootstrap: {
139+
enabled: true
140+
},
141+
webRTCStar: {
142+
enabled: true
143+
}
144+
}
145+
})
146+
147+
libp2pOptions.metrics = { enabled: false }
148+
149+
log('initializing libp2p with libp2pOptions', libp2pOptions)
150+
return new Libp2p(libp2pOptions)
151+
}
117152
}
118153
const ipfsNodeConfig = mergeOptions(defaultOpts, userOpts, chromeOpts, finalOpts)
119154

add-on/src/lib/ipfs-client/embedded-chromesockets/index.js

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,15 @@ exports.init = async function init (opts) {
2727
log('init embedded:chromesockets')
2828

2929
const ipfsOpts = await buildConfig(opts, log)
30+
3031
log('creating js-ipfs with opts: ', ipfsOpts)
31-
node = new Ipfs(ipfsOpts)
32+
node = await Ipfs.create(ipfsOpts)
3233

33-
return new Promise((resolve, reject) => {
34-
node.once('error', (error) => {
35-
log.error('something went terribly wrong during startup of js-ipfs!', error)
36-
reject(error)
37-
})
38-
node.once('ready', async () => {
39-
node.once('start', async () => {
40-
// HttpApi is off in browser context and needs to be started separately
41-
try {
42-
const httpServers = new HttpApi(node, ipfsOpts)
43-
nodeHttpApi = await httpServers.start()
44-
await syncConfig(node, opts, log)
45-
resolve(node)
46-
} catch (err) {
47-
reject(err)
48-
}
49-
})
50-
try {
51-
node.on('error', error => {
52-
log.error('something went terribly wrong in embedded js-ipfs!', error)
53-
})
54-
await node.start()
55-
} catch (err) {
56-
reject(err)
57-
}
58-
})
59-
})
34+
log('starting HTTP servers with opts: ', ipfsOpts)
35+
const httpServers = new HttpApi(node, ipfsOpts)
36+
nodeHttpApi = await httpServers.start()
37+
await syncConfig(node, opts, log)
38+
return node
6039
}
6140

6241
exports.destroy = async function () {
@@ -74,21 +53,7 @@ exports.destroy = async function () {
7453
nodeHttpApi = null
7554
}
7655
if (node) {
77-
const stopped = new Promise((resolve, reject) => {
78-
node.on('stop', resolve)
79-
node.on('error', reject)
80-
})
81-
try {
82-
await node.stop()
83-
} catch (err) {
84-
// TODO: remove when fixed upstream: https://github.com/ipfs/js-ipfs/issues/2257
85-
if (err.message === 'Not able to stop from state: stopping') {
86-
log('destroy: embedded:chromesockets waiting for node.stop()')
87-
await stopped
88-
} else {
89-
throw err
90-
}
91-
}
56+
await node.stop()
9257
node = null
9358
}
9459
}

add-on/src/lib/ipfs-client/embedded-chromesockets/libp2p-bundle.js

Lines changed: 0 additions & 107 deletions
This file was deleted.

0 commit comments

Comments
 (0)