Skip to content

Commit 861749f

Browse files
author
Ruben Bridgewater
committed
Fix send_command working with hooked internal functions
1 parent 3c2e6b4 commit 861749f

File tree

5 files changed

+21
-25
lines changed

5 files changed

+21
-25
lines changed

index.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -391,14 +391,14 @@ RedisClient.prototype.on_ready = function () {
391391

392392
// Restore modal commands from previous connection. The order of the commands is important
393393
if (this.selected_db !== undefined) {
394-
this.send_command('select', [this.selected_db]);
394+
this.internal_send_command('select', [this.selected_db]);
395395
}
396396
if (this.old_state !== null) {
397397
this.monitoring = this.old_state.monitoring;
398398
this.pub_sub_mode = this.old_state.pub_sub_mode;
399399
}
400400
if (this.monitoring) { // Monitor has to be fired before pub sub commands
401-
this.send_command('monitor', []);
401+
this.internal_send_command('monitor', []);
402402
}
403403
var callback_count = Object.keys(this.subscription_set).length;
404404
if (!this.options.disable_resubscribing && callback_count) {
@@ -415,7 +415,7 @@ RedisClient.prototype.on_ready = function () {
415415
for (var key in this.subscription_set) { // jshint ignore: line
416416
var command = key.slice(0, key.indexOf('_'));
417417
var args = self.subscription_set[key];
418-
self.send_command(command, [args], callback);
418+
self.internal_send_command(command, [args], callback);
419419
}
420420
this.send_offline_queue();
421421
return;
@@ -478,7 +478,7 @@ RedisClient.prototype.ready_check = function () {
478478
RedisClient.prototype.send_offline_queue = function () {
479479
for (var command_obj = this.offline_queue.shift(); command_obj; command_obj = this.offline_queue.shift()) {
480480
debug('Sending offline command: ' + command_obj.command);
481-
this.send_command(command_obj.command, command_obj.args, command_obj.callback);
481+
this.internal_send_command(command_obj.command, command_obj.args, command_obj.callback);
482482
}
483483
this.drain();
484484
// Even though items were shifted off, Queue backing store still uses memory until next add, so just get a new Queue
@@ -760,13 +760,14 @@ function handle_offline_command (self, command_obj) {
760760
self.should_buffer = true;
761761
}
762762

763-
RedisClient.prototype.send_command = function (command, args, callback) {
764-
var args_copy, arg, prefix_keys;
763+
RedisClient.prototype.internal_send_command = function (command, args, callback) {
764+
var arg, prefix_keys;
765765
var i = 0;
766766
var command_str = '';
767-
var len = 0;
767+
var len = args.length;
768768
var big_data = false;
769769
var buffer_args = false;
770+
var args_copy = new Array(len);
770771

771772
if (process.domain && callback) {
772773
callback = process.domain.bind(callback);
@@ -778,13 +779,6 @@ RedisClient.prototype.send_command = function (command, args, callback) {
778779
return false; // Indicate buffering
779780
}
780781

781-
if (typeof args === 'undefined') {
782-
args_copy = [];
783-
} else {
784-
len = args.length;
785-
args_copy = new Array(len);
786-
}
787-
788782
for (i = 0; i < len; i += 1) {
789783
if (typeof args[i] === 'string') {
790784
// 30000 seemed to be a good value to switch to buffers after testing and checking the pros and cons

lib/commands.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ commands.list.forEach(function (command) {
4242
arr[i] = arguments[i];
4343
}
4444
}
45-
return this.send_command(command, arr, callback);
45+
return this.internal_send_command(command, arr, callback);
4646
};
4747
}
4848

lib/individualCommands.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ RedisClient.prototype.batch = RedisClient.prototype.BATCH = function batch (args
2525
// Store db in this.select_db to restore it on reconnect
2626
RedisClient.prototype.select = RedisClient.prototype.SELECT = function select (db, callback) {
2727
var self = this;
28-
return this.send_command('select', [db], function (err, res) {
28+
db = Array.isArray(db) && db.length === 1 ? db[0] : db;
29+
return this.internal_send_command('select', [db], function (err, res) {
2930
if (err === null) {
3031
self.selected_db = db;
3132
}
@@ -36,7 +37,7 @@ RedisClient.prototype.select = RedisClient.prototype.SELECT = function select (d
3637
RedisClient.prototype.monitor = RedisClient.prototype.MONITOR = function (callback) {
3738
// Use a individual command, as this is a special case that does not has to be checked for any other command
3839
var self = this;
39-
return this.send_command('monitor', [], function (err, res) {
40+
return this.internal_send_command('monitor', [], function (err, res) {
4041
if (err === null) {
4142
self.reply_parser.returnReply = function (reply) {
4243
// If in monitor mode, all normal commands are still working and we only want to emit the streamlined commands
@@ -86,7 +87,7 @@ RedisClient.prototype.quit = RedisClient.prototype.QUIT = function (callback) {
8687
}
8788
utils.callback_or_emit(self, callback, err, res);
8889
};
89-
var backpressure_indicator = this.send_command('quit', [], callback_hook);
90+
var backpressure_indicator = this.internal_send_command('quit', [], callback_hook);
9091
// Calling quit should always end the connection, no matter if there's a connection or not
9192
this.closing = true;
9293
return backpressure_indicator;
@@ -103,7 +104,7 @@ RedisClient.prototype.info = RedisClient.prototype.INFO = function info (section
103104
args = Array.isArray(section) ? section : [section];
104105
}
105106
this.ready = ready || this.offline_queue.length === 0; // keep the execution order intakt
106-
var tmp = this.send_command('info', args, function (err, res) {
107+
var tmp = this.internal_send_command('info', args, function (err, res) {
107108
if (res) {
108109
var obj = {};
109110
var lines = res.toString().split('\r\n');
@@ -148,9 +149,10 @@ RedisClient.prototype.auth = RedisClient.prototype.AUTH = function auth (pass, c
148149
debug('Sending auth to ' + self.address + ' id ' + self.connection_id);
149150

150151
// Stash auth for connect and reconnect.
152+
pass = Array.isArray(pass) && pass.length === 1 ? pass[0] : pass;
151153
this.auth_pass = pass;
152154
this.ready = this.offline_queue.length === 0; // keep the execution order intakt
153-
var tmp = this.send_command('auth', [pass], function (err, res) {
155+
var tmp = this.internal_send_command('auth', [pass], function (err, res) {
154156
if (err) {
155157
if (no_password_is_set.test(err.message)) {
156158
self.warn('Warning: Redis server does not require a password, but a password was supplied.');
@@ -207,5 +209,5 @@ RedisClient.prototype.hmset = RedisClient.prototype.HMSET = function hmset () {
207209
arr[i] = arguments[i];
208210
}
209211
}
210-
return this.send_command('hmset', arr, callback);
212+
return this.internal_send_command('hmset', arr, callback);
211213
};

lib/multi.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ Multi.prototype.exec_transaction = function exec_transaction (callback) {
147147
pipeline_transaction_command(self, command, args[1], index, cb);
148148
}
149149

150-
self._client.send_command('exec', [], function (err, replies) {
150+
self._client.internal_send_command('exec', [], function (err, replies) {
151151
multi_callback(self, err, replies);
152152
});
153153
self._client.uncork();
@@ -207,10 +207,10 @@ Multi.prototype.exec = Multi.prototype.EXEC = Multi.prototype.exec_batch = funct
207207
} else {
208208
cb = callback_without_own_cb;
209209
}
210-
if (callback && index === len - 1) {
210+
if (typeof callback === 'function' && index === len - 1) {
211211
cb = last_callback(cb);
212212
}
213-
self._client.send_command(command, args[1], cb);
213+
self._client.internal_send_command(command, args[1], cb);
214214
index++;
215215
}
216216
self.queue = new Queue();

test/commands/info.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe("The 'info' method", function () {
5656

5757
it('check redis v.2.4 support', function (done) {
5858
var end = helper.callFuncAfter(done, 2);
59-
client.send_command = function (command, args, callback) {
59+
client.internal_send_command = function (command, args, callback) {
6060
assert.strictEqual(args.length, 0);
6161
assert.strictEqual(command, 'info');
6262
end();

0 commit comments

Comments
 (0)