Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 5601c26

Browse files
haoliangyudaviddias
authored andcommitted
fix(pubsub.peers): remove the requirement for a topic (#1125)
* fix(pubsub.peers): remove the requirement for a topic * shorter code
1 parent 5d7e1f2 commit 5601c26

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

src/core/components/pubsub.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,13 @@ module.exports = function pubsub (self) {
7777
return setImmediate(() => callback(new Error(OFFLINE_ERROR)))
7878
}
7979

80+
if (typeof topic === 'function') {
81+
callback = topic
82+
topic = null
83+
}
84+
8085
const peers = Array.from(self._pubsub.peers.values())
81-
.filter((peer) => peer.topics.has(topic))
86+
.filter((peer) => topic ? peer.topics.has(topic) : true)
8287
.map((peer) => peer.info.id.toB58String())
8388

8489
setImmediate(() => callback(null, peers))

src/http/api/resources/pubsub.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,13 @@ exports.peers = {
102102
const topic = request.query.arg
103103
const ipfs = request.server.app.ipfs
104104

105-
if (!topic) {
106-
return reply(new Error('Missing topic'))
107-
}
108-
109105
ipfs.pubsub.peers(topic, (err, peers) => {
110106
if (err) {
111-
return reply(new Error(`Failed to find peers subscribed to ${topic}: ${err}`))
107+
const message = topic
108+
? `Failed to find peers subscribed to ${topic}: ${err}`
109+
: `Failed to find peers: ${err}`
110+
111+
return reply(new Error(message))
112112
}
113113

114114
reply({Strings: peers})

test/http-api/spec/pubsub.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@ module.exports = (http) => {
9191
})
9292

9393
describe('/peers', () => {
94-
it('returns 500 if no topic is provided', (done) => {
94+
it('returns 200 if no topic is provided', (done) => {
9595
api.inject({
9696
method: 'GET',
9797
url: `/api/v0/pubsub/peers`
9898
}, (res) => {
99-
expect(res.statusCode).to.equal(500)
100-
expect(res.result.Code).to.be.eql(1)
99+
expect(res.statusCode).to.equal(200)
100+
expect(res.result.Strings).to.be.eql([])
101101
done()
102102
})
103103
})

0 commit comments

Comments
 (0)