Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: peer store operations has, get and remove should support peer id instances #491

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/peer-store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ log.error = debug('libp2p:peer-store:error')

const { EventEmitter } = require('events')

const PeerId = require('peer-id')
const PeerInfo = require('peer-info')

/**
Expand Down Expand Up @@ -160,10 +161,15 @@ class PeerStore extends EventEmitter {

/**
* Get the info to the given id.
* @param {string} peerId b58str id
* @param {PeerId|string} peerId b58str id
* @returns {PeerInfo}
*/
get (peerId) {
// TODO: deprecate this and just accept `PeerId` instances
if (PeerId.isPeerId(peerId)) {
peerId = peerId.toB58String()
}

const peerInfo = this.peers.get(peerId)

if (peerInfo) {
Expand All @@ -175,19 +181,29 @@ class PeerStore extends EventEmitter {

/**
* Has the info to the given id.
* @param {string} peerId b58str id
* @param {PeerId|string} peerId b58str id
* @returns {boolean}
*/
has (peerId) {
// TODO: deprecate this and just accept `PeerId` instances
if (PeerId.isPeerId(peerId)) {
peerId = peerId.toB58String()
}

return this.peers.has(peerId)
}

/**
* 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
*/
remove (peerId) {
// TODO: deprecate this and just accept `PeerId` instances
if (PeerId.isPeerId(peerId)) {
peerId = peerId.toB58String()
}

return this.peers.delete(peerId)
}

Expand Down
4 changes: 2 additions & 2 deletions test/peer-store/peer-store.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe('peer-store', () => {

it('should be able to retrieve a peer from store through its b58str id', async () => {
const [peerInfo] = await peerUtils.createPeerInfo()
const id = peerInfo.id.toB58String()
const id = peerInfo.id

let retrievedPeer = peerStore.get(id)
expect(retrievedPeer).to.not.exist()
Expand All @@ -155,7 +155,7 @@ describe('peer-store', () => {

it('should be able to remove a peer from store through its b58str id', async () => {
const [peerInfo] = await peerUtils.createPeerInfo()
const id = peerInfo.id.toB58String()
const id = peerInfo.id

let removed = peerStore.remove(id)
expect(removed).to.eql(false)
Expand Down