2019-11-14 11:52:06 +01:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const Topology = require('./index')
|
2020-12-10 14:03:17 +01:00
|
|
|
const multicodecTopologySymbol = Symbol.for('@libp2p/js-interfaces/topology/multicodec-topology')
|
2019-11-14 11:52:06 +01:00
|
|
|
|
|
|
|
class MulticodecTopology extends Topology {
|
|
|
|
/**
|
2020-12-10 14:03:17 +01:00
|
|
|
* @param {TopologyOptions & MulticodecOptions} props
|
2019-11-14 11:52:06 +01:00
|
|
|
*/
|
|
|
|
constructor ({
|
|
|
|
min,
|
|
|
|
max,
|
|
|
|
multicodecs,
|
|
|
|
handlers
|
|
|
|
}) {
|
|
|
|
super({ min, max, handlers })
|
|
|
|
|
2020-02-17 21:19:14 +00:00
|
|
|
if (!multicodecs) {
|
|
|
|
throw new Error('one or more multicodec should be provided')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!handlers) {
|
|
|
|
throw new Error('the handlers should be provided')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof handlers.onConnect !== 'function') {
|
|
|
|
throw new Error('the \'onConnect\' handler must be provided')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof handlers.onDisconnect !== 'function') {
|
|
|
|
throw new Error('the \'onDisconnect\' handler must be provided')
|
|
|
|
}
|
2019-11-14 11:52:06 +01:00
|
|
|
|
|
|
|
this.multicodecs = Array.isArray(multicodecs) ? multicodecs : [multicodecs]
|
|
|
|
this._registrar = undefined
|
|
|
|
|
|
|
|
this._onProtocolChange = this._onProtocolChange.bind(this)
|
2020-07-03 15:48:12 +02:00
|
|
|
this._onPeerConnect = this._onPeerConnect.bind(this)
|
2019-11-14 11:52:06 +01:00
|
|
|
}
|
|
|
|
|
2020-12-10 14:03:17 +01:00
|
|
|
get [Symbol.toStringTag] () {
|
|
|
|
return 'Topology'
|
|
|
|
}
|
|
|
|
|
|
|
|
get [multicodecTopologySymbol] () {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the given value is a `MulticodecTopology` instance.
|
|
|
|
*
|
|
|
|
* @param {any} other
|
|
|
|
* @returns {other is MulticodecTopology}
|
|
|
|
*/
|
|
|
|
static isMulticodecTopology (other) {
|
|
|
|
return Boolean(other && other[multicodecTopologySymbol])
|
|
|
|
}
|
|
|
|
|
|
|
|
set registrar (registrar) { // eslint-disable-line
|
2019-11-14 11:52:06 +01:00
|
|
|
this._registrar = registrar
|
|
|
|
this._registrar.peerStore.on('change:protocols', this._onProtocolChange)
|
2020-07-03 15:48:12 +02:00
|
|
|
this._registrar.connectionManager.on('peer:connect', this._onPeerConnect)
|
2019-11-14 11:52:06 +01:00
|
|
|
|
|
|
|
// Update topology peers
|
|
|
|
this._updatePeers(this._registrar.peerStore.peers.values())
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update topology.
|
2020-12-10 14:03:17 +01:00
|
|
|
*
|
2020-04-17 17:23:31 +02:00
|
|
|
* @param {Array<{id: PeerId, multiaddrs: Array<Multiaddr>, protocols: Array<string>}>} peerDataIterable
|
2019-11-14 11:52:06 +01:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2020-04-17 17:23:31 +02:00
|
|
|
_updatePeers (peerDataIterable) {
|
|
|
|
for (const { id, protocols } of peerDataIterable) {
|
|
|
|
if (this.multicodecs.filter(multicodec => protocols.includes(multicodec)).length) {
|
2019-11-14 11:52:06 +01:00
|
|
|
// Add the peer regardless of whether or not there is currently a connection
|
2020-04-17 17:23:31 +02:00
|
|
|
this.peers.add(id.toB58String())
|
2019-11-14 11:52:06 +01:00
|
|
|
// If there is a connection, call _onConnect
|
2020-04-17 17:23:31 +02:00
|
|
|
const connection = this._registrar.getConnection(id)
|
|
|
|
connection && this._onConnect(id, connection)
|
2019-11-14 11:52:06 +01:00
|
|
|
} else {
|
|
|
|
// Remove any peers we might be tracking that are no longer of value to us
|
2020-04-17 17:23:31 +02:00
|
|
|
this.peers.delete(id.toB58String())
|
2019-11-14 11:52:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a new peer support the multicodecs for this topology.
|
2020-12-10 14:03:17 +01:00
|
|
|
*
|
2019-11-14 11:52:06 +01:00
|
|
|
* @param {Object} props
|
2020-04-17 17:23:31 +02:00
|
|
|
* @param {PeerId} props.peerId
|
2019-11-14 11:52:06 +01:00
|
|
|
* @param {Array<string>} props.protocols
|
|
|
|
*/
|
2020-04-17 17:23:31 +02:00
|
|
|
_onProtocolChange ({ peerId, protocols }) {
|
|
|
|
const hadPeer = this.peers.has(peerId.toB58String())
|
2019-11-14 11:52:06 +01:00
|
|
|
const hasProtocol = protocols.filter(protocol => this.multicodecs.includes(protocol))
|
|
|
|
|
|
|
|
// Not supporting the protocol anymore?
|
2020-04-17 17:23:31 +02:00
|
|
|
if (hadPeer && hasProtocol.length === 0) {
|
|
|
|
this._onDisconnect(peerId)
|
2019-11-14 11:52:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// New to protocol support
|
|
|
|
for (const protocol of protocols) {
|
|
|
|
if (this.multicodecs.includes(protocol)) {
|
2020-04-17 17:23:31 +02:00
|
|
|
const peerData = this._registrar.peerStore.get(peerId)
|
|
|
|
this._updatePeers([peerData])
|
2019-11-14 11:52:06 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-03 15:48:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify if a new connected peer has a topology multicodec and call _onConnect.
|
2020-12-10 14:03:17 +01:00
|
|
|
*
|
2020-07-03 15:48:12 +02:00
|
|
|
* @param {Connection} connection
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onPeerConnect (connection) {
|
2020-12-10 14:03:17 +01:00
|
|
|
// @ts-ignore - remotePeer does not existist on Connection
|
2020-07-03 15:48:12 +02:00
|
|
|
const peerId = connection.remotePeer
|
|
|
|
const protocols = this._registrar.peerStore.protoBook.get(peerId)
|
|
|
|
|
|
|
|
if (!protocols) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.multicodecs.find(multicodec => protocols.includes(multicodec))) {
|
|
|
|
this.peers.add(peerId.toB58String())
|
|
|
|
this._onConnect(peerId, connection)
|
|
|
|
}
|
|
|
|
}
|
2019-11-14 11:52:06 +01:00
|
|
|
}
|
|
|
|
|
2020-09-30 11:21:11 +02:00
|
|
|
/**
|
2020-12-10 14:03:17 +01:00
|
|
|
* @typedef {import('peer-id')} PeerId
|
|
|
|
* @typedef {import('multiaddr')} Multiaddr
|
|
|
|
* @typedef {import('../connection/connection')} Connection
|
|
|
|
* @typedef {import('.').Options} TopologyOptions
|
|
|
|
* @typedef {Object} MulticodecOptions
|
|
|
|
* @property {string[]} multicodecs - protocol multicodecs
|
|
|
|
* @property {Required<Handlers>} handlers
|
|
|
|
* @typedef {import('.').Handlers} Handlers
|
2020-09-30 11:21:11 +02:00
|
|
|
*/
|
2020-12-10 14:03:17 +01:00
|
|
|
module.exports = MulticodecTopology
|