Skip to content

Commit dffa8a6

Browse files
author
Ruben Bridgewater
committed
Fix parser being reset in case (p)message_buffer is attached
without the parser set to return buffers. This might result in corrupt data if the listener is attached while the parser holds partial data.
1 parent 670b774 commit dffa8a6

File tree

3 files changed

+66
-42
lines changed

3 files changed

+66
-42
lines changed

changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Changelog
22
=========
33

4+
## v.2.6.5 - 15 Jan, 2017
5+
6+
Bugfixes
7+
8+
- Fixed parser reset if (p)message_buffer listener is attached
9+
410
## v.2.6.4 - 12 Jan, 2017
511

612
Bugfixes

index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,16 @@ function RedisClient (options, stream) {
171171
'The drain event listener is deprecated and will be removed in v.3.0.0.\n' +
172172
'If you want to keep on listening to this event please listen to the stream drain event directly.'
173173
);
174-
} else if (event === 'message_buffer' || event === 'pmessage_buffer' || event === 'messageBuffer' || event === 'pmessageBuffer' && !this.buffers) {
174+
} else if ((event === 'message_buffer' || event === 'pmessage_buffer' || event === 'messageBuffer' || event === 'pmessageBuffer') && !this.buffers && !this.message_buffers) {
175+
if (this.reply_parser.name !== 'javascript') {
176+
return this.warn(
177+
'You attached the ' + event + ' without the hiredis parser without the returnBuffers option set to true.\n' +
178+
'Please use the JavaScript parser or set the returnBuffers option to true to return buffers.'
179+
);
180+
}
181+
this.reply_parser.optionReturnBuffers = true;
175182
this.message_buffers = true;
176183
this.handle_reply = handle_detect_buffers_reply;
177-
this.reply_parser = create_parser(this);
178184
}
179185
});
180186
}

test/pubsub.spec.js

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -508,52 +508,64 @@ describe('publish/subscribe', function () {
508508
});
509509

510510
it('allows to listen to pmessageBuffer and pmessage', function (done) {
511-
var batch = sub.batch();
512511
var end = helper.callFuncAfter(done, 6);
513-
assert.strictEqual(sub.message_buffers, false);
514-
batch.psubscribe('*');
515-
batch.subscribe('/foo');
516-
batch.unsubscribe('/foo');
517-
batch.unsubscribe(helper.isNull());
518-
batch.subscribe(['/foo'], helper.isString('/foo'));
519-
batch.exec();
520-
assert.strictEqual(sub.shouldBuffer, false);
521-
sub.on('pmessageBuffer', function (pattern, channel, message) {
522-
assert.strictEqual(pattern.inspect(), new Buffer('*').inspect());
523-
assert.strictEqual(channel.inspect(), new Buffer('/foo').inspect());
524-
sub.quit(end);
525-
});
526-
// Either message_buffers or buffers has to be true, but not both at the same time
527-
assert.notStrictEqual(sub.message_buffers, sub.buffers);
528-
sub.on('pmessage', function (pattern, channel, message) {
529-
assert.strictEqual(pattern, '*');
530-
assert.strictEqual(channel, '/foo');
531-
assert.strictEqual(message, 'hello world');
532-
end();
533-
});
534-
sub.on('message', function (channel, message) {
535-
assert.strictEqual(channel, '/foo');
536-
assert.strictEqual(message, 'hello world');
537-
end();
538-
});
539-
setTimeout(function () {
540-
pub.pubsub('numsub', '/foo', function (err, res) {
541-
// There's one subscriber to this channel
542-
assert.deepEqual(res, ['/foo', 1]);
543-
end();
512+
var data = Array(10000).join('äüs^öéÉÉ`e');
513+
sub.set('foo', data, function () {
514+
sub.get('foo');
515+
sub.stream.once('data', function () {
516+
assert.strictEqual(sub.message_buffers, false);
517+
assert.strictEqual(sub.shouldBuffer, false);
518+
sub.on('pmessageBuffer', function (pattern, channel, message) {
519+
if (parser !== 'javascript' && typeof pattern === 'string') {
520+
pattern = new Buffer(pattern);
521+
channel = new Buffer(channel);
522+
}
523+
assert.strictEqual(pattern.inspect(), new Buffer('*').inspect());
524+
assert.strictEqual(channel.inspect(), new Buffer('/foo').inspect());
525+
sub.quit(end);
526+
});
527+
if (parser === 'javascript') {
528+
assert.notStrictEqual(sub.message_buffers, sub.buffers);
529+
}
530+
544531
});
545-
pub.pubsub('channels', function (err, res) {
546-
// There's exactly one channel that is listened too
547-
assert.deepEqual(res, ['/foo']);
532+
var batch = sub.batch();
533+
batch.psubscribe('*');
534+
batch.subscribe('/foo');
535+
batch.unsubscribe('/foo');
536+
batch.unsubscribe(helper.isNull());
537+
batch.subscribe(['/foo'], helper.isString('/foo'));
538+
batch.exec(function () {
539+
pub.pubsub('numsub', '/foo', function (err, res) {
540+
// There's one subscriber to this channel
541+
assert.deepEqual(res, ['/foo', 1]);
542+
end();
543+
});
544+
pub.pubsub('channels', function (err, res) {
545+
// There's exactly one channel that is listened too
546+
assert.deepEqual(res, ['/foo']);
547+
end();
548+
});
549+
pub.pubsub('numpat', function (err, res) {
550+
// One pattern is active
551+
assert.strictEqual(res, 1);
552+
end();
553+
});
554+
pub.publish('/foo', 'hello world', helper.isNumber(2));
555+
});
556+
// Either message_buffers or buffers has to be true, but not both at the same time
557+
sub.on('pmessage', function (pattern, channel, message) {
558+
assert.strictEqual(pattern, '*');
559+
assert.strictEqual(channel, '/foo');
560+
assert.strictEqual(message, 'hello world');
548561
end();
549562
});
550-
pub.pubsub('numpat', function (err, res) {
551-
// One pattern is active
552-
assert.strictEqual(res, 1);
563+
sub.on('message', function (channel, message) {
564+
assert.strictEqual(channel, '/foo');
565+
assert.strictEqual(message, 'hello world');
553566
end();
554567
});
555-
pub.publish('/foo', 'hello world', helper.isNumber(2));
556-
}, 50);
568+
});
557569
});
558570
});
559571

0 commit comments

Comments
 (0)