mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-23 22:11:35 +00:00
refactor: circuit relay to async (#477)
* refactor: add dialing over relay support * chore: fix lint * fix: dont clear listeners on close * fix: if dial errors already have codes, just rethrow them * fix: clear the registrar when libp2p stops * fix: improve connection maintenance with circuit * chore: correct feedback * test: use chai as promised * test(fix): reset multiaddrs on dial test
This commit is contained in:
21
src/index.js
21
src/index.js
@ -16,6 +16,7 @@ const { getPeerInfo, getPeerInfoRemote } = require('./get-peer-info')
|
||||
const { validate: validateConfig } = require('./config')
|
||||
const { codes } = require('./errors')
|
||||
|
||||
const Circuit = require('./circuit')
|
||||
const Dialer = require('./dialer')
|
||||
const TransportManager = require('./transport-manager')
|
||||
const Upgrader = require('./upgrader')
|
||||
@ -78,9 +79,6 @@ class Libp2p extends EventEmitter {
|
||||
libp2p: this,
|
||||
upgrader: this.upgrader
|
||||
})
|
||||
this._modules.transport.forEach((Transport) => {
|
||||
this.transportManager.add(Transport.prototype[Symbol.toStringTag], Transport)
|
||||
})
|
||||
|
||||
// Attach crypto channels
|
||||
if (this._modules.connEncryption) {
|
||||
@ -95,6 +93,12 @@ class Libp2p extends EventEmitter {
|
||||
peerStore: this.peerStore
|
||||
})
|
||||
|
||||
this._modules.transport.forEach((Transport) => {
|
||||
this.transportManager.add(Transport.prototype[Symbol.toStringTag], Transport)
|
||||
})
|
||||
// TODO: enable relay if enabled
|
||||
this.transportManager.add(Circuit.prototype[Symbol.toStringTag], Circuit)
|
||||
|
||||
// Attach stream multiplexers
|
||||
if (this._modules.streamMuxer) {
|
||||
const muxers = this._modules.streamMuxer
|
||||
@ -182,6 +186,7 @@ class Libp2p extends EventEmitter {
|
||||
this.pubsub && await this.pubsub.stop()
|
||||
this._dht && await this._dht.stop()
|
||||
await this.transportManager.close()
|
||||
await this.registrar.close()
|
||||
} catch (err) {
|
||||
if (err) {
|
||||
log.error(err)
|
||||
@ -282,7 +287,10 @@ class Libp2p extends EventEmitter {
|
||||
this.upgrader.protocols.set(protocol, handler)
|
||||
})
|
||||
|
||||
this.dialer.identifyService.pushToPeerStore(this.peerStore)
|
||||
// Only push if libp2p is running
|
||||
if (this.isStarted()) {
|
||||
this.dialer.identifyService.pushToPeerStore(this.peerStore)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -296,7 +304,10 @@ class Libp2p extends EventEmitter {
|
||||
this.upgrader.protocols.delete(protocol)
|
||||
})
|
||||
|
||||
this.dialer.identifyService.pushToPeerStore(this.peerStore)
|
||||
// Only push if libp2p is running
|
||||
if (this.isStarted()) {
|
||||
this.dialer.identifyService.pushToPeerStore(this.peerStore)
|
||||
}
|
||||
}
|
||||
|
||||
async _onStarting () {
|
||||
|
Reference in New Issue
Block a user