mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-15 10:11:22 +00:00
chore: remove peer-info usage
BREAKING CHANGE: all API methods with peer-info parameters or return values were changed. You can check the API.md document, in order to check the new values to use
This commit is contained in:
@ -6,9 +6,7 @@ const log = debug('libp2p:peer-store')
|
||||
log.error = debug('libp2p:peer-store:error')
|
||||
|
||||
const { EventEmitter } = require('events')
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
const PeerInfo = require('peer-info')
|
||||
|
||||
const AddressBook = require('./address-book')
|
||||
const ProtoBook = require('./proto-book')
|
||||
@ -25,8 +23,9 @@ const {
|
||||
*/
|
||||
class PeerStore extends EventEmitter {
|
||||
/**
|
||||
* PeerInfo object
|
||||
* @typedef {Object} peerInfo
|
||||
* PeerData object
|
||||
* @typedef {Object} PeerData
|
||||
* @property {PeerId} id peer's peer-id instance.
|
||||
* @property {Array<multiaddrInfo>} multiaddrsInfos peer's information of the multiaddrs.
|
||||
* @property {Array<string>} protocols peer's supported protocols.
|
||||
*/
|
||||
@ -54,49 +53,35 @@ class PeerStore extends EventEmitter {
|
||||
|
||||
/**
|
||||
* Get all the stored information of every peer.
|
||||
* @returns {Map<string, peerInfo>}
|
||||
* @returns {Map<string, PeerData>}
|
||||
*/
|
||||
get peers () {
|
||||
const peerInfos = new Map()
|
||||
const peersData = new Map()
|
||||
|
||||
// AddressBook
|
||||
for (const [idStr, multiaddrInfos] of this.addressBook.data.entries()) {
|
||||
// TODO: Remove peerInfo and its usage on peer-info deprecate
|
||||
const peerInfo = new PeerInfo(PeerId.createFromCID(idStr))
|
||||
|
||||
multiaddrInfos.forEach((mi) => peerInfo.multiaddrs.add((mi.multiaddr)))
|
||||
|
||||
const protocols = this.protoBook.data.get(idStr) || []
|
||||
protocols.forEach((p) => peerInfo.protocols.add(p))
|
||||
|
||||
peerInfos.set(idStr, peerInfo)
|
||||
// TODO
|
||||
// peerInfos.set(idStr, {
|
||||
// id: PeerId.createFromCID(idStr),
|
||||
// multiaddrInfos,
|
||||
// protocols: this.protoBook.data.get(idStr) || []
|
||||
// })
|
||||
const id = PeerId.createFromCID(idStr)
|
||||
peersData.set(idStr, {
|
||||
id,
|
||||
multiaddrInfos,
|
||||
protocols: this.protoBook.get(id) || []
|
||||
})
|
||||
}
|
||||
|
||||
// ProtoBook
|
||||
for (const [idStr, protocols] of this.protoBook.data.entries()) {
|
||||
// TODO: Remove peerInfo and its usage on peer-info deprecate
|
||||
const peerInfo = peerInfos.get(idStr)
|
||||
const pData = peersData.get(idStr)
|
||||
|
||||
if (!peerInfo) {
|
||||
const peerInfo = new PeerInfo(PeerId.createFromCID(idStr))
|
||||
|
||||
protocols.forEach((p) => peerInfo.protocols.add(p))
|
||||
peerInfos.set(idStr, peerInfo)
|
||||
// peerInfos.set(idStr, {
|
||||
// id: PeerId.createFromCID(idStr),
|
||||
// multiaddrInfos: [],
|
||||
// protocols: protocols
|
||||
// })
|
||||
if (!pData) {
|
||||
peersData.set(idStr, {
|
||||
id: PeerId.createFromCID(idStr),
|
||||
multiaddrInfos: [],
|
||||
protocols: Array.from(protocols)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return peerInfos
|
||||
return peersData
|
||||
}
|
||||
|
||||
/**
|
||||
@ -113,7 +98,7 @@ class PeerStore extends EventEmitter {
|
||||
/**
|
||||
* Get the stored information of a given peer.
|
||||
* @param {PeerId} peerId
|
||||
* @returns {peerInfo}
|
||||
* @returns {PeerData}
|
||||
*/
|
||||
get (peerId) {
|
||||
if (!PeerId.isPeerId(peerId)) {
|
||||
|
Reference in New Issue
Block a user