mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-13 01:01:23 +00:00
feat: (BREAKING CHANGE) overhaul libp2p config and constructor
* docs: update chat example and add info to its readme * docs: update echo example * docs: update libp2p in browser example * docs: update pubsub example * docs: update peer and content routing examples * docs: update discovery mechanisms example * docs: update encrypted comms example * docs: update protocol and stream muxing example * feat: add config validation * test: update CI configs, use only node 8
This commit is contained in:
@ -31,7 +31,9 @@ async.parallel([
|
||||
if (err) throw err
|
||||
const peerDialer = new PeerInfo(ids[0])
|
||||
peerDialer.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
const nodeDialer = new Node(peerDialer)
|
||||
const nodeDialer = new Node({
|
||||
peerInfo: peerDialer
|
||||
})
|
||||
|
||||
const peerListener = new PeerInfo(ids[1])
|
||||
idListener = ids[1]
|
||||
|
@ -3,11 +3,12 @@
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MulticastDNS = require('libp2p-mdns')
|
||||
const WS = require('libp2p-websockets')
|
||||
const Railing = require('libp2p-railing')
|
||||
const Bootstrap = require('libp2p-railing')
|
||||
const spdy = require('libp2p-spdy')
|
||||
const KadDHT = require('libp2p-kad-dht')
|
||||
const mplex = require('libp2p-mplex')
|
||||
const secio = require('libp2p-secio')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
const libp2p = require('../../..')
|
||||
|
||||
function mapMuxers (list) {
|
||||
@ -36,44 +37,40 @@ function getMuxers (muxers) {
|
||||
}
|
||||
|
||||
class Node extends libp2p {
|
||||
constructor (peerInfo, peerBook, options) {
|
||||
options = options || {}
|
||||
|
||||
const modules = {
|
||||
transport: [
|
||||
new TCP(),
|
||||
new WS()
|
||||
],
|
||||
connection: {
|
||||
muxer: getMuxers(options.muxer),
|
||||
crypto: [ secio ]
|
||||
constructor (_options) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [
|
||||
TCP,
|
||||
WS
|
||||
],
|
||||
streamMuxer: getMuxers(_options.muxer),
|
||||
connEncryption: [ secio ],
|
||||
peerDiscovery: [
|
||||
MulticastDNS,
|
||||
Bootstrap
|
||||
],
|
||||
dht: KadDHT
|
||||
},
|
||||
discovery: []
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
mdns: {
|
||||
interval: 10000,
|
||||
enabled: false
|
||||
},
|
||||
bootstrap: {
|
||||
interval: 10000,
|
||||
enabled: false,
|
||||
list: _options.bootstrapList
|
||||
}
|
||||
},
|
||||
dht: {
|
||||
kBucketSize: 20
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (options.dht) {
|
||||
modules.DHT = KadDHT
|
||||
}
|
||||
|
||||
if (options.mdns) {
|
||||
const mdns = new MulticastDNS(peerInfo, 'ipfs.local')
|
||||
modules.discovery.push(mdns)
|
||||
}
|
||||
|
||||
if (options.bootstrap) {
|
||||
const r = new Railing(options.bootstrap)
|
||||
modules.discovery.push(r)
|
||||
}
|
||||
|
||||
if (options.modules && options.modules.transport) {
|
||||
options.modules.transport.forEach((t) => modules.transport.push(t))
|
||||
}
|
||||
|
||||
if (options.modules && options.modules.discovery) {
|
||||
options.modules.discovery.forEach((d) => modules.discovery.push(d))
|
||||
}
|
||||
|
||||
super(modules, peerInfo, peerBook, options)
|
||||
super(defaultsDeep(_options, defaults))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,14 +14,16 @@ PeerId.createFromJSON(require('./peer-id-listener'), (err, idListener) => {
|
||||
}
|
||||
const peerListener = new PeerInfo(idListener)
|
||||
peerListener.multiaddrs.add('/ip4/0.0.0.0/tcp/10333')
|
||||
const nodeListener = new Node(peerListener)
|
||||
const nodeListener = new Node({
|
||||
peerInfo: peerListener
|
||||
})
|
||||
|
||||
nodeListener.start((err) => {
|
||||
if (err) {
|
||||
throw err
|
||||
}
|
||||
|
||||
nodeListener.switch.on('peer-mux-established', (peerInfo) => {
|
||||
nodeListener.on('peer:connect', (peerInfo) => {
|
||||
console.log(peerInfo.id.toB58String())
|
||||
})
|
||||
|
||||
|
Reference in New Issue
Block a user