mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-03-31 14:51:05 +00:00
feat: support peer-id instances in peer store operations (#491)
This commit is contained in:
parent
a020db183a
commit
8da9fc97d4
@ -7,6 +7,7 @@ log.error = debug('libp2p:peer-store:error')
|
|||||||
|
|
||||||
const { EventEmitter } = require('events')
|
const { EventEmitter } = require('events')
|
||||||
|
|
||||||
|
const PeerId = require('peer-id')
|
||||||
const PeerInfo = require('peer-info')
|
const PeerInfo = require('peer-info')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -160,10 +161,15 @@ class PeerStore extends EventEmitter {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the info to the given id.
|
* Get the info to the given id.
|
||||||
* @param {string} peerId b58str id
|
* @param {PeerId|string} peerId b58str id
|
||||||
* @returns {PeerInfo}
|
* @returns {PeerInfo}
|
||||||
*/
|
*/
|
||||||
get (peerId) {
|
get (peerId) {
|
||||||
|
// TODO: deprecate this and just accept `PeerId` instances
|
||||||
|
if (PeerId.isPeerId(peerId)) {
|
||||||
|
peerId = peerId.toB58String()
|
||||||
|
}
|
||||||
|
|
||||||
const peerInfo = this.peers.get(peerId)
|
const peerInfo = this.peers.get(peerId)
|
||||||
|
|
||||||
if (peerInfo) {
|
if (peerInfo) {
|
||||||
@ -175,19 +181,29 @@ class PeerStore extends EventEmitter {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Has the info to the given id.
|
* Has the info to the given id.
|
||||||
* @param {string} peerId b58str id
|
* @param {PeerId|string} peerId b58str id
|
||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
*/
|
*/
|
||||||
has (peerId) {
|
has (peerId) {
|
||||||
|
// TODO: deprecate this and just accept `PeerId` instances
|
||||||
|
if (PeerId.isPeerId(peerId)) {
|
||||||
|
peerId = peerId.toB58String()
|
||||||
|
}
|
||||||
|
|
||||||
return this.peers.has(peerId)
|
return this.peers.has(peerId)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes the Peer with the matching `peerId` from the PeerStore
|
* Removes the Peer with the matching `peerId` from the PeerStore
|
||||||
* @param {string} peerId b58str id
|
* @param {PeerId|string} peerId b58str id
|
||||||
* @returns {boolean} true if found and removed
|
* @returns {boolean} true if found and removed
|
||||||
*/
|
*/
|
||||||
remove (peerId) {
|
remove (peerId) {
|
||||||
|
// TODO: deprecate this and just accept `PeerId` instances
|
||||||
|
if (PeerId.isPeerId(peerId)) {
|
||||||
|
peerId = peerId.toB58String()
|
||||||
|
}
|
||||||
|
|
||||||
return this.peers.delete(peerId)
|
return this.peers.delete(peerId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,7 +138,7 @@ describe('peer-store', () => {
|
|||||||
|
|
||||||
it('should be able to retrieve a peer from store through its b58str id', async () => {
|
it('should be able to retrieve a peer from store through its b58str id', async () => {
|
||||||
const [peerInfo] = await peerUtils.createPeerInfo()
|
const [peerInfo] = await peerUtils.createPeerInfo()
|
||||||
const id = peerInfo.id.toB58String()
|
const id = peerInfo.id
|
||||||
|
|
||||||
let retrievedPeer = peerStore.get(id)
|
let retrievedPeer = peerStore.get(id)
|
||||||
expect(retrievedPeer).to.not.exist()
|
expect(retrievedPeer).to.not.exist()
|
||||||
@ -155,7 +155,7 @@ describe('peer-store', () => {
|
|||||||
|
|
||||||
it('should be able to remove a peer from store through its b58str id', async () => {
|
it('should be able to remove a peer from store through its b58str id', async () => {
|
||||||
const [peerInfo] = await peerUtils.createPeerInfo()
|
const [peerInfo] = await peerUtils.createPeerInfo()
|
||||||
const id = peerInfo.id.toB58String()
|
const id = peerInfo.id
|
||||||
|
|
||||||
let removed = peerStore.remove(id)
|
let removed = peerStore.remove(id)
|
||||||
expect(removed).to.eql(false)
|
expect(removed).to.eql(false)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user