49 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-04-18 23:26:46 +02:00
'use strict'
const multiaddr = require('multiaddr')
/**
2020-12-10 14:48:14 +01:00
* @typedef {import('multiaddr')} Multiaddr
*/
/**
* @typedef {Object} AddressManagerOptions
* @property {string[]} [listen = []] - list of multiaddrs string representation to listen.
* @property {string[]} [announce = []] - list of multiaddrs string representation to announce.
2020-04-18 23:26:46 +02:00
*/
class AddressManager {
/**
2020-12-10 14:48:14 +01:00
* Responsible for managing the peer addresses.
* Peers can specify their listen and announce addresses.
* 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.
*
* @class
2020-12-10 14:48:14 +01:00
* @param {AddressManagerOptions} [options]
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.
*
2020-12-10 14:48:14 +01:00
* @returns {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.
*
2020-12-10 14:48:14 +01:00
* @returns {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