|
3 | 3 | const browser = require('webextension-polyfill')
|
4 | 4 |
|
5 | 5 | const { optionDefaults } = require('../../options')
|
6 |
| -const chromeSocketsBundle = require('./libp2p-bundle') |
7 | 6 | const mergeOptions = require('merge-options')
|
8 | 7 | const getPort = require('get-port')
|
9 | 8 | const { getIPv4, getIPv6 } = require('webrtc-ips')
|
10 | 9 |
|
| 10 | +const Libp2p = require('libp2p') |
| 11 | +const WebSocketStarMulti = require('libp2p-websocket-star-multi') |
| 12 | +const WebRTCStar = require('libp2p-webrtc-star') |
| 13 | +const WS = require('libp2p-websockets') |
| 14 | +const TCP = require('libp2p-tcp') |
| 15 | +const Bootstrap = require('libp2p-bootstrap') |
| 16 | +const MulticastDNS = require('libp2p-mdns') |
| 17 | + |
11 | 18 | const multiaddr = require('multiaddr')
|
12 | 19 | const maToUri = require('multiaddr-to-uri')
|
13 | 20 | const multiaddr2httpUrl = (ma) => maToUri(ma.includes('/http') ? ma : multiaddr(ma).encapsulate('/http'))
|
14 | 21 |
|
| 22 | +const debug = require('debug') |
| 23 | +const log = debug('ipfs-companion:client:embedded:config') |
| 24 | +log.error = debug('ipfs-companion:client:embedded:config:error') |
| 25 | + |
15 | 26 | // additional default js-ipfs config specific to runtime with chrome.sockets APIs
|
16 | 27 | const chromeDefaultOpts = {
|
17 | 28 | config: {
|
@@ -113,7 +124,36 @@ async function buildConfig (opts, log) {
|
113 | 124 | // merge configs
|
114 | 125 | const finalOpts = {
|
115 | 126 | start: false,
|
116 |
| - libp2p: chromeSocketsBundle |
| 127 | + libp2p: ({ libp2pOptions, peerInfo }) => { |
| 128 | + const wrtcstar = new WebRTCStar({ id: peerInfo.id }) |
| 129 | + |
| 130 | + // TODO: provisional reset in case the same code run already in js-ipfs/src/core/runtime/libp2p-browser.js |
| 131 | + peerInfo.multiaddrs.replace('/p2p-websocket-star', chromeDefaultOpts.config.Addresses.Swarm.find(addr => addr.includes('p2p-websocket-star'))) |
| 132 | + |
| 133 | + // this can be replaced once optional listening is supported with the below code. ref: https://github.com/libp2p/interface-transport/issues/41 |
| 134 | + // const wsstar = new WebSocketStar({ id: _options.peerInfo.id, ignore_no_online: true }) |
| 135 | + const wsstarServers = peerInfo.multiaddrs.toArray().map(String).filter(addr => addr.includes('p2p-websocket-star') && !addr.startsWith('/p2p-websocket-star')) |
| 136 | + log('ws-star servers', wsstarServers) |
| 137 | + peerInfo.multiaddrs.replace(wsstarServers.map(multiaddr), '/p2p-websocket-star') // the ws-star-multi module will replace this with the chosen ws-star servers |
| 138 | + const wsstar = new WebSocketStarMulti({ servers: wsstarServers, id: peerInfo.id, ignore_no_online: true }) // allow scenario when all ws-stars are offline |
| 139 | + |
| 140 | + libp2pOptions.modules.transport = [ |
| 141 | + TCP, |
| 142 | + WS, |
| 143 | + wrtcstar, |
| 144 | + wsstar |
| 145 | + ] |
| 146 | + |
| 147 | + libp2pOptions.modules.peerDiscovery = [ |
| 148 | + wrtcstar.discovery, |
| 149 | + wsstar.discovery, |
| 150 | + MulticastDNS, |
| 151 | + Bootstrap |
| 152 | + ] |
| 153 | + |
| 154 | + log('initializing libp2p with libp2pOptions', libp2pOptions) |
| 155 | + return new Libp2p(libp2pOptions) |
| 156 | + } |
117 | 157 | }
|
118 | 158 | const ipfsNodeConfig = mergeOptions(defaultOpts, userOpts, chromeOpts, finalOpts)
|
119 | 159 |
|
|
0 commit comments