Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

Commit

Permalink
chore: normalize msg before emit
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Aug 19, 2020
1 parent c9cc4ec commit ec5ae25
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
7 changes: 4 additions & 3 deletions src/pubsub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,12 +576,13 @@ class PubsubBaseProtocol extends EventEmitter {
topicIDs: [topic]
}

// ensure that any operations performed on the message will include the signature
const outMsg = await this._buildMessage(msgObject)
msgObject = utils.normalizeInRpcMessage(outMsg)

// Emit to self if I'm interested and emitSelf enabled
this.emitSelf && this._emitMessage(msgObject)

// ensure that any operations performed on the message will include the signature
msgObject = await this._buildMessage(msgObject)

// send to all the other peers
await this._publish(msgObject)
}
Expand Down
20 changes: 12 additions & 8 deletions src/pubsub/tests/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,25 @@ module.exports = (common) => {
await common.teardown()
})

it('should emit non normalized messages on publish', async () => {
it('should emit normalized signed messages on publish', async () => {
sinon.spy(pubsub, '_emitMessage')
sinon.spy(utils, 'randomSeqno')

await pubsub.publish(topic, data)
expect(pubsub._emitMessage.callCount).to.eql(1)

const [messageToEmit] = pubsub._emitMessage.getCall(0).args
expect(messageToEmit).to.eql({
receivedFrom: pubsub.peerId.toB58String(),
from: pubsub.peerId.toB58String(),
data,
seqno: utils.randomSeqno.getCall(0).returnValue,
topicIDs: [topic]
})

const expected = utils.normalizeInRpcMessage(
await pubsub._buildMessage({
receivedFrom: pubsub.peerId.toB58String(),
from: pubsub.peerId.toB58String(),
data,
seqno: utils.randomSeqno.getCall(0).returnValue,
topicIDs: [topic]
}))

expect(messageToEmit).to.eql(expected)
})

it('should drop unsigned messages', async () => {
Expand Down

0 comments on commit ec5ae25

Please sign in to comment.