From 4edf3533ffbb3e58402c814aad8b1907f29cf1aa Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Thu, 11 Jul 2019 15:12:14 +0200 Subject: [PATCH] chore: review addressed --- src/pubsub.js | 6 +++++- test/pubsub.node.js | 22 +++++++--------------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/pubsub.js b/src/pubsub.js index c1a21945de..1457e8bd58 100644 --- a/src/pubsub.js +++ b/src/pubsub.js @@ -56,8 +56,12 @@ module.exports = (node) => { return nextTick(callback, errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED)) } + if (!Buffer.isBuffer(data) && typeof data !== 'string') { + return nextTick(callback, errCode(new Error('data must be a Buffer or a String'), 'ERR_DATA_IS_NOT_VALID')) + } + if (!Buffer.isBuffer(data)) { - return nextTick(callback, errCode(new Error('data must be a Buffer'), 'ERR_DATA_IS_NOT_A_BUFFER')) + data = Buffer.from(data) } pubsub.publish(topic, data, callback) diff --git a/test/pubsub.node.js b/test/pubsub.node.js index 8c043b85db..5806ae0be4 100644 --- a/test/pubsub.node.js +++ b/test/pubsub.node.js @@ -62,10 +62,10 @@ describe('.pubsub', () => { expect(2).checks(done) let nodes - const data = Buffer.from('test') + const data = 'test' const handler = (msg) => { // verify the data is correct and mark the expect - expect(msg.data).to.eql(data).mark() + expect(msg.data.toString()).to.eql(data).mark() } series([ @@ -80,10 +80,6 @@ describe('.pubsub', () => { (cb) => setTimeout(cb, 500), // publish on the second (cb) => nodes[1].pubsub.publish('pubsub', data, cb), - // ls subscripts - (cb) => nodes[1].pubsub.ls(cb), - // get subscribed peers - (cb) => nodes[1].pubsub.peers('pubsub', cb), // Wait a moment before unsubscribing (cb) => setTimeout(cb, 500), // unsubscribe on the first @@ -140,7 +136,7 @@ describe('.pubsub', () => { expect(err).to.not.exist().mark() }) }) - it('publish should fail if data is not a buffer', (done) => { + it('publish should fail if data is not a buffer nor a string', (done) => { createNode('/ip4/0.0.0.0/tcp/0', { config: { peerDiscovery: { @@ -158,9 +154,9 @@ describe('.pubsub', () => { node.start((err) => { expect(err).to.not.exist() - node.pubsub.publish('pubsub', 'datastr', (err) => { + node.pubsub.publish('pubsub', 10, (err) => { expect(err).to.exist() - expect(err.code).to.equal('ERR_DATA_IS_NOT_A_BUFFER') + expect(err.code).to.equal('ERR_DATA_IS_NOT_VALID') done() }) @@ -197,10 +193,6 @@ describe('.pubsub', () => { (cb) => setTimeout(cb, 500), // publish on the second (cb) => nodes[1].pubsub.publish('pubsub', data, cb), - // ls subscripts - (cb) => nodes[1].pubsub.ls(cb), - // get subscribed peers - (cb) => nodes[1].pubsub.peers('pubsub', cb), // Wait a moment before unsubscribing (cb) => setTimeout(cb, 500), // unsubscribe on the first @@ -282,9 +274,9 @@ describe('.pubsub', () => { node.start((err) => { expect(err).to.not.exist() - node.pubsub.publish('pubsub', 'datastr', (err) => { + node.pubsub.publish('pubsub', 10, (err) => { expect(err).to.exist() - expect(err.code).to.equal('ERR_DATA_IS_NOT_A_BUFFER') + expect(err.code).to.equal('ERR_DATA_IS_NOT_VALID') done() })