refactor: async identify and identify push (#473)

* chore: add missing dep

* feat: import from identify push branch

https://github.com/libp2p/js-libp2p-identify/tree/feat/identify-push

* feat: add the connection to stream handlers

* refactor: identify to async/await

* chore: fix lint

* test: add identify tests

* refactor: add identify to the dialer flow

* feat: connect identify to the registrar

* fix: resolve review feedback

* fix: perform identify push when our protocols change
This commit is contained in:
Jacob Heun
2019-11-07 12:11:50 +01:00
parent 9d52b80c45
commit c7a54f34f7
15 changed files with 674 additions and 174 deletions

View File

@ -30,6 +30,10 @@ const TransportManager = require('./transport-manager')
const Upgrader = require('./upgrader')
const PeerStore = require('./peer-store')
const Registrar = require('./registrar')
const {
IdentifyService,
multicodecs: IDENTIFY_PROTOCOLS
} = require('./identify')
const notStarted = (action, state) => {
return errCode(
@ -83,6 +87,11 @@ class Libp2p extends EventEmitter {
}
})
// Create the Registrar
this.registrar = new Registrar({ peerStore: this.peerStore })
this.handle = this.handle.bind(this)
this.registrar.handle = this.handle
// Setup the transport manager
this.transportManager = new TransportManager({
libp2p: this,
@ -100,22 +109,26 @@ class Libp2p extends EventEmitter {
})
}
this.dialer = new Dialer({
transportManager: this.transportManager
})
// Attach stream multiplexers
if (this._modules.streamMuxer) {
const muxers = this._modules.streamMuxer
muxers.forEach((muxer) => {
this.upgrader.muxers.set(muxer.multicodec, muxer)
})
// Add the identify service since we can multiplex
this.dialer.identifyService = new IdentifyService({
registrar: this.registrar,
peerInfo: this.peerInfo,
protocols: this.upgrader.protocols
})
this.handle(Object.values(IDENTIFY_PROTOCOLS), this.dialer.identifyService.handleMessage)
}
this.dialer = new Dialer({
transportManager: this.transportManager
})
this.registrar = new Registrar({ peerStore: this.peerStore })
this.handle = this.handle.bind(this)
this.registrar.handle = this.handle
// Attach private network protector
if (this._modules.connProtector) {
this.upgrader.protector = this._modules.connProtector
@ -338,13 +351,15 @@ class Libp2p extends EventEmitter {
/**
* Registers the `handler` for each protocol
* @param {string[]|string} protocols
* @param {function({ stream:*, protocol:string })} handler
* @param {function({ connection:*, stream:*, protocol:string })} handler
*/
handle (protocols, handler) {
protocols = Array.isArray(protocols) ? protocols : [protocols]
protocols.forEach(protocol => {
this.upgrader.protocols.set(protocol, handler)
})
this.dialer.identifyService.pushToPeerStore(this.peerStore)
}
/**
@ -357,6 +372,8 @@ class Libp2p extends EventEmitter {
protocols.forEach(protocol => {
this.upgrader.protocols.delete(protocol)
})
this.dialer.identifyService.pushToPeerStore(this.peerStore)
}
async _onStarting () {