From 5d6f072e5603cdeb5c95c9588cb1acbadf35fe9e Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sun, 28 May 2017 08:41:01 +0200 Subject: [PATCH] feat: always return strings from "message" listener Listening to "messageBuffer" returns Buffers --- changelog.md | 2 ++ lib/pubsub.js | 4 ++-- test/pubsub.spec.js | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/changelog.md b/changelog.md index d87157fd7e..a6ed10ae8b 100644 --- a/changelog.md +++ b/changelog.md @@ -58,6 +58,8 @@ Breaking Changes - `host:` is now `host` - Changed the `serverInfo` into a nested object and to parse numbers - Changed the `serverInfo.versions` to `serverInfo.version` +- Changed the `message` and `pmessage` listener to always return a string + If you want to receive a buffer, please listen to the `messageBuffer` or `pmessageBuffer` - Using `.end` without the flush parameter is now going to throw an TypeError - Only emit ready when all commands were truly send to Redis diff --git a/lib/pubsub.js b/lib/pubsub.js index 7a9a455467..ad30eb3d72 100644 --- a/lib/pubsub.js +++ b/lib/pubsub.js @@ -62,14 +62,14 @@ function returnPubSub (client, reply) { // It would be more straight forward to only listen to a single "message" // and in case of a "pmessage" a third argument would be passed (the pattern). if (type === 'message') { // Channel, message - if (!client._options.returnBuffers || client.messageBuffers) { // Backwards compatible. Refactor this in v.3 to always return a string on the normal emitter + if (typeof reply[1] !== 'string') { client.emit('message', reply[1].toString(), reply[2].toString()) client.emit('messageBuffer', reply[1], reply[2]) } else { client.emit('message', reply[1], reply[2]) } } else if (type === 'pmessage') { // Pattern, channel, message - if (!client._options.returnBuffers || client.messageBuffers) { // Backwards compatible. Refactor this in v.3 to always return a string on the normal emitter + if (typeof reply[1] !== 'string') { client.emit('pmessage', reply[1].toString(), reply[2].toString(), reply[3].toString()) client.emit('pmessageBuffer', reply[1], reply[2], reply[3]) } else { diff --git a/test/pubsub.spec.js b/test/pubsub.spec.js index 6deb53b9a7..e6a5785b8b 100644 --- a/test/pubsub.spec.js +++ b/test/pubsub.spec.js @@ -444,7 +444,7 @@ describe('publish/subscribe', () => { // sub2 is counted twice as it subscribed with psubscribe and subscribe pub.publish('/foo', 'hello world').then(helper.isNumber(3)) }) - sub2.on('pmessage', (pattern, channel, message) => { + sub2.on('pmessageBuffer', (pattern, channel, message) => { assert.strictEqual(pattern.inspect(), Buffer.from('*').inspect()) assert.strictEqual(channel.inspect(), Buffer.from('/foo').inspect()) assert.strictEqual(message.inspect(), Buffer.from('hello world').inspect())