-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(brave): dedicated libp2p bundle
- Loading branch information
Showing
6 changed files
with
513 additions
and
192 deletions.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
add-on/src/lib/ipfs-client/chrome-sockets/libp2p-bundle.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
'use strict' | ||
|
||
const get = require('dlv') | ||
const mergeOptions = require('merge-options') | ||
const errCode = require('err-code') | ||
|
||
const ipns = require('ipns') | ||
const multiaddr = require('multiaddr') | ||
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing') | ||
const DelegatedContentRouter = require('libp2p-delegated-content-routing') | ||
const PubsubRouters = { gossipsub: require('libp2p-gossipsub') } | ||
const Libp2pChromeSockets = require('./libp2p') | ||
|
||
// libp2p bundle customized for chrome.sockets | ||
// Loosely follows https://github.com/ipfs/js-ipfs/blob/master/src/core/components/libp2p.js | ||
module.exports = function chromeSocketsBundle ({ datastore, peerInfo, peerBook, options, config }) { | ||
// TODO: Set up Delegate Routing based on the presence of Delegates in the config? | ||
let contentRouting | ||
let peerRouting | ||
|
||
const delegateHosts = get(options, 'config.Addresses.Delegates', | ||
get(config, 'Addresses.Delegates', []) | ||
) | ||
if (delegateHosts.length > 0) { | ||
// Pick a random delegate host | ||
const delegateString = delegateHosts[Math.floor(Math.random() * delegateHosts.length)] | ||
const delegateAddr = multiaddr(delegateString).toOptions() | ||
const delegatedApiOptions = { | ||
host: delegateAddr.host, | ||
// port is a string atm, so we need to convert for the check | ||
protocol: parseInt(delegateAddr.port) === 443 ? 'https' : 'http', | ||
port: delegateAddr.port | ||
} | ||
contentRouting = [new DelegatedContentRouter(peerInfo.id, delegatedApiOptions)] | ||
peerRouting = [new DelegatedPeerRouter(delegatedApiOptions)] | ||
} | ||
|
||
const getPubsubRouter = () => { | ||
const router = get(config, 'Pubsub.Router', 'gossipsub') | ||
|
||
if (!PubsubRouters[router]) { | ||
throw errCode(new Error(`Router unavailable. Configure libp2p.modules.pubsub to use the ${router} router.`), 'ERR_NOT_SUPPORTED') | ||
} | ||
|
||
return PubsubRouters[router] | ||
} | ||
|
||
const libp2pDefaults = { | ||
datastore, | ||
peerInfo, | ||
peerBook, | ||
modules: { | ||
contentRouting, | ||
peerRouting, | ||
pubsub: getPubsubRouter() | ||
}, | ||
config: { | ||
peerDiscovery: { | ||
bootstrap: { | ||
list: get(options, 'config.Bootstrap', | ||
get(config, 'Bootstrap', [])) | ||
} | ||
}, | ||
relay: { | ||
enabled: get(options, 'relay.enabled', | ||
get(config, 'relay.enabled', true)), | ||
hop: { | ||
enabled: get(options, 'relay.hop.enabled', | ||
get(config, 'relay.hop.enabled', false)), | ||
active: get(options, 'relay.hop.active', | ||
get(config, 'relay.hop.active', false)) | ||
} | ||
}, | ||
dht: { | ||
kBucketSize: get(options, 'dht.kBucketSize', 20), | ||
// enabled: !get(options, 'offline', false), // disable if offline, on by default | ||
enabled: false, | ||
randomWalk: { | ||
enabled: false // disabled waiting for https://github.com/libp2p/js-libp2p-kad-dht/issues/86 | ||
}, | ||
validators: { | ||
ipns: { | ||
func: (key, record, cb) => ipns.validator.validate(record, key, cb) | ||
} | ||
}, | ||
selectors: { | ||
ipns: (k, records) => ipns.validator.select(records[0], records[1]) | ||
} | ||
}, | ||
pubsub: { | ||
enabled: get(config, 'Pubsub.Enabled', true) | ||
} | ||
}, | ||
connectionManager: get(options, 'connectionManager', | ||
{ | ||
maxPeers: get(config, 'Swarm.ConnMgr.HighWater'), | ||
minPeers: get(config, 'Swarm.ConnMgr.LowWater') | ||
}) | ||
} | ||
let libp2pOptions | ||
if (typeof options.libp2p !== 'function') { | ||
libp2pOptions = mergeOptions(libp2pDefaults, get(options, 'libp2p', {})) | ||
} else { | ||
libp2pOptions = libp2pDefaults | ||
} | ||
return new Libp2pChromeSockets(libp2pOptions) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
const TCP = require('libp2p-tcp') | ||
const MulticastDNS = require('libp2p-mdns') | ||
const WS = require('libp2p-websockets') | ||
const WebSocketStarMulti = require('libp2p-websocket-star-multi') | ||
const Bootstrap = require('libp2p-bootstrap') | ||
const KadDHT = require('libp2p-kad-dht') | ||
const GossipSub = require('libp2p-gossipsub') | ||
const Multiplex = require('pull-mplex') | ||
const SECIO = require('libp2p-secio') | ||
const libp2p = require('libp2p') | ||
const mergeOptions = require('merge-options') | ||
const multiaddr = require('multiaddr') | ||
|
||
// libp2p class with constructor tweaked for use with chrome.sockets | ||
// loosely follows: | ||
// - https://github.com/ipfs/js-ipfs/blob/master/src/core/runtime/libp2p-nodejs.js | ||
// - https://github.com/ipfs/js-ipfs/blob/master/src/core/runtime/libp2p-browser.js | ||
class Libp2pChromeSockets extends libp2p { | ||
constructor (_options) { | ||
// this can be replaced once optional listening is supported with the below code. ref: https://github.com/libp2p/interface-transport/issues/41 | ||
// const wsstar = new WebSocketStar({ id: _options.peerInfo.id }) | ||
const wsstarServers = _options.peerInfo.multiaddrs.toArray().map(String).filter(addr => addr.includes('p2p-websocket-star')) | ||
_options.peerInfo.multiaddrs.replace(wsstarServers.map(multiaddr), '/p2p-websocket-star') // the ws-star-multi module will replace this with the chosen ws-star servers | ||
const wsstar = new WebSocketStarMulti({ servers: wsstarServers, id: _options.peerInfo.id, ignore_no_online: !wsstarServers.length || _options.wsStarIgnoreErrors }) | ||
|
||
const defaults = { | ||
switch: { | ||
denyTTL: 2 * 60 * 1e3, // 2 minute base | ||
denyAttempts: 5, // back off 5 times | ||
maxParallelDials: 150, | ||
maxColdCalls: 50, | ||
dialTimeout: 10e3 // Be strict with dial time | ||
}, | ||
modules: { | ||
transport: [ | ||
TCP, | ||
WS, | ||
wsstar | ||
], | ||
streamMuxer: [ | ||
Multiplex | ||
], | ||
connEncryption: [ | ||
SECIO | ||
], | ||
peerDiscovery: [ | ||
MulticastDNS, | ||
Bootstrap, | ||
wsstar.discovery | ||
], | ||
dht: KadDHT, | ||
pubsub: GossipSub | ||
}, | ||
config: { | ||
peerDiscovery: { | ||
autoDial: true, | ||
mdns: { | ||
enabled: true | ||
}, | ||
bootstrap: { | ||
enabled: true | ||
}, | ||
websocketStar: { | ||
enabled: true | ||
} | ||
}, | ||
dht: { | ||
kBucketSize: 20, | ||
enabled: false, | ||
randomWalk: { | ||
enabled: false | ||
} | ||
}, | ||
pubsub: { | ||
enabled: true, | ||
emitSelf: true | ||
} | ||
} | ||
} | ||
|
||
super(mergeOptions(defaults, _options)) | ||
} | ||
} | ||
|
||
module.exports = Libp2pChromeSockets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.