1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-09 00:22:08 +03:00

feat: always return strings from "message" listener

Listening to "messageBuffer" returns Buffers
This commit is contained in:
Ruben Bridgewater
2017-05-28 08:41:01 +02:00
parent 6796f8806a
commit 5d6f072e56
3 changed files with 5 additions and 3 deletions

View File

@@ -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

View File

@@ -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 {

View File

@@ -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())