Skip to content

Commit 354714f

Browse files
achingbrainvasco-santos
authored andcommitted
fix: topicToKey should return a buffer not a string (#16)
1 parent dd5175b commit 354714f

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class DatastorePubsub {
166166
return
167167
}
168168

169-
log(`message received for ${key} topic`)
169+
log(`message received for topic ${topicIDs[0]}`)
170170

171171
// Stop if the message is from the peer (it already stored it while publishing to pubsub)
172172
if (from === this._peerId.toB58String()) {
@@ -269,7 +269,7 @@ class DatastorePubsub {
269269
const routingKey = new Key('/' + encodeBase32(key), false)
270270

271271
await this._datastore.put(routingKey, data)
272-
log(`record for ${key.toString()} was stored in the datastore`)
272+
log(`record for ${keyToTopic(key)} was stored in the datastore`)
273273
}
274274

275275
open () {

src/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ module.exports.topicToKey = (topic) => {
2727

2828
const key = `${base64urlCode}${topic.substring(namespace.length)}`
2929

30-
return multibase.decode(key).toString()
30+
return multibase.decode(key)
3131
}

test/index.spec.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const { Key } = require('interface-datastore')
1313
const { Record } = require('libp2p-record')
1414

1515
const DatastorePubsub = require('../src')
16-
const { keyToTopic } = require('../src/utils')
16+
const { keyToTopic, topicToKey } = require('../src/utils')
1717
const { connect, waitFor, waitForPeerToSubscribe, spawnDaemon, stopDaemon } = require('./utils')
1818
const promisify = require('promisify-es6')
1919

@@ -426,8 +426,8 @@ describe('datastore-pubsub', function () {
426426
})
427427

428428
it('should subscribe the topic and after a message being received, discard it using the subscriptionKeyFn', async () => {
429-
const subscriptionKeyFn = (topic) => {
430-
expect(topic).to.equal(`/${keyRef}`)
429+
const subscriptionKeyFn = (key) => {
430+
expect(key.toString()).to.equal(`/${keyRef}`)
431431
throw new Error('DISCARD MESSAGE')
432432
}
433433
const dsPubsubA = new DatastorePubsub(pubsubA, datastoreA, peerIdA, smoothValidator)
@@ -469,14 +469,14 @@ describe('datastore-pubsub', function () {
469469
})
470470

471471
it('should subscribe the topic and after a message being received, change its key using subscriptionKeyFn', async () => {
472-
const subscriptionKeyFn = (topic) => {
473-
expect(topic).to.equal(key.toString())
474-
return `${topic}new`
472+
const subscriptionKeyFn = (key) => {
473+
expect(key.toString()).to.equal(`/${keyRef}`)
474+
return topicToKey(`${keyToTopic(key)}new`)
475475
}
476476
const dsPubsubA = new DatastorePubsub(pubsubA, datastoreA, peerIdA, smoothValidator)
477477
const dsPubsubB = new DatastorePubsub(pubsubB, datastoreB, peerIdB, smoothValidator, subscriptionKeyFn)
478478
const subsTopic = keyToTopic(`/${keyRef}`)
479-
const keyNew = Buffer.from(`${key.toString()}new`)
479+
const keyNew = topicToKey(`${keyToTopic(key)}new`)
480480
let receivedMessage = false
481481

482482
function messageHandler () {

0 commit comments

Comments
 (0)