Skip to content

Commit 11ed6bd

Browse files
vasco-santosjacobheun
authored andcommitted
feat: support peer-id instances in peer store operations (#491)
1 parent fc22c36 commit 11ed6bd

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/peer-store/index.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ log.error = debug('libp2p:peer-store:error')
77

88
const { EventEmitter } = require('events')
99

10+
const PeerId = require('peer-id')
1011
const PeerInfo = require('peer-info')
1112

1213
/**
@@ -160,10 +161,15 @@ class PeerStore extends EventEmitter {
160161

161162
/**
162163
* Get the info to the given id.
163-
* @param {string} peerId b58str id
164+
* @param {PeerId|string} peerId b58str id
164165
* @returns {PeerInfo}
165166
*/
166167
get (peerId) {
168+
// TODO: deprecate this and just accept `PeerId` instances
169+
if (PeerId.isPeerId(peerId)) {
170+
peerId = peerId.toB58String()
171+
}
172+
167173
const peerInfo = this.peers.get(peerId)
168174

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

176182
/**
177183
* Has the info to the given id.
178-
* @param {string} peerId b58str id
184+
* @param {PeerId|string} peerId b58str id
179185
* @returns {boolean}
180186
*/
181187
has (peerId) {
188+
// TODO: deprecate this and just accept `PeerId` instances
189+
if (PeerId.isPeerId(peerId)) {
190+
peerId = peerId.toB58String()
191+
}
192+
182193
return this.peers.has(peerId)
183194
}
184195

185196
/**
186197
* Removes the Peer with the matching `peerId` from the PeerStore
187-
* @param {string} peerId b58str id
198+
* @param {PeerId|string} peerId b58str id
188199
* @returns {boolean} true if found and removed
189200
*/
190201
remove (peerId) {
202+
// TODO: deprecate this and just accept `PeerId` instances
203+
if (PeerId.isPeerId(peerId)) {
204+
peerId = peerId.toB58String()
205+
}
206+
191207
return this.peers.delete(peerId)
192208
}
193209

test/peer-store/peer-store.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ describe('peer-store', () => {
138138

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

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

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

160160
let removed = peerStore.remove(id)
161161
expect(removed).to.eql(false)

0 commit comments

Comments
 (0)