Skip to content

fix: topicToKey should return a buffer not a string #16

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

Merged
merged 1 commit into from
Sep 25, 2019
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
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class DatastorePubsub {
return
}

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

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

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

open () {
Expand Down
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ module.exports.topicToKey = (topic) => {

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

return multibase.decode(key).toString()
return multibase.decode(key)
}
14 changes: 7 additions & 7 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const { Key } = require('interface-datastore')
const { Record } = require('libp2p-record')

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

Expand Down Expand Up @@ -426,8 +426,8 @@ describe('datastore-pubsub', function () {
})

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

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

function messageHandler () {
Expand Down