47 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-04-18 23:26:46 +02:00
'use strict'
const debug = require('debug')
const log = debug('libp2p:addresses')
log.error = debug('libp2p:addresses:error')
const multiaddr = require('multiaddr')
/**
* Responsible for managing the peer addresses.
* Peers can specify their listen and announce addresses.
2020-04-18 23:26:46 +02:00
* The listen addresses will be used by the libp2p transports to listen for new connections,
* while the announce addresses will be used for the peer addresses' to other peers in the network.
2020-04-18 23:26:46 +02:00
*/
class AddressManager {
/**
* @class
2020-04-18 23:26:46 +02:00
* @param {object} [options]
* @param {Array<string>} [options.listen = []] - list of multiaddrs string representation to listen.
* @param {Array<string>} [options.announce = []] - list of multiaddrs string representation to announce.
2020-04-18 23:26:46 +02:00
*/
constructor ({ listen = [], announce = [] } = {}) {
2020-04-18 23:26:46 +02:00
this.listen = new Set(listen)
this.announce = new Set(announce)
}
/**
* Get peer listen multiaddrs.
*
* @returns {Array<Multiaddr>}
2020-04-18 23:26:46 +02:00
*/
getListenAddrs () {
2020-04-18 23:26:46 +02:00
return Array.from(this.listen).map((a) => multiaddr(a))
}
/**
* Get peer announcing multiaddrs.
*
* @returns {Array<Multiaddr>}
2020-04-18 23:26:46 +02:00
*/
getAnnounceAddrs () {
2020-04-18 23:26:46 +02:00
return Array.from(this.announce).map((a) => multiaddr(a))
}
}
module.exports = AddressManager