Skip to content

Commit 835dc40

Browse files
author
Ruben Bridgewater
committed
Explicitly remove a undefined callback from any multi command
1 parent f6f5d91 commit 835dc40

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ coverage
55
*.log
66
*.rdb
77
stunnel.conf
8-
stunnel.pid
8+
stunnel.pid
9+
*.out

index.js

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ RedisClient.prototype.return_reply = function (reply) {
658658
} else if (type === 'pmessage') {
659659
this.emit('pmessage', reply[1].toString(), reply[2], reply[3]); // pattern, channel, message
660660
} else if (type === 'subscribe' || type === 'unsubscribe' || type === 'psubscribe' || type === 'punsubscribe') {
661-
if (reply[2] === 0) {
661+
if (reply[2].toString() === '0') {
662662
this.pub_sub_mode = false;
663663
debug('All subscriptions removed, exiting pub/sub mode');
664664
} else {
@@ -1127,6 +1127,7 @@ Multi.prototype.exec_transaction = function (callback) {
11271127
var self = this;
11281128
var len = this.queue.length;
11291129
var cb;
1130+
var args_len = 1;
11301131
this.errors = [];
11311132
this.callback = callback;
11321133
this._client.cork(len + 2);
@@ -1136,9 +1137,11 @@ Multi.prototype.exec_transaction = function (callback) {
11361137
for (var index = 0; index < len; index++) {
11371138
var args = this.queue.get(index).slice(0);
11381139
var command = args.shift();
1139-
if (typeof args[args.length - 1] === 'function') {
1140+
args_len = args.length - 1;
1141+
if (typeof args[args_len] === 'function' || typeof args[args_len] === 'undefined') {
11401142
cb = args.pop();
11411143
} else {
1144+
// Explicitly set the callback to undefined. Otherwise we might have the callback from the command earlier
11421145
cb = undefined;
11431146
}
11441147
// Keep track of who wants buffer responses:
@@ -1213,14 +1216,12 @@ Multi.prototype.callback = function (cb, i) {
12131216
return function (err, res) {
12141217
if (err) {
12151218
self.results[i] = err;
1219+
// Add the position to the error
1220+
self.results[i].position = i;
12161221
} else {
12171222
self.results[i] = res;
12181223
}
1219-
if (cb) {
1220-
cb(err, res);
1221-
}
1222-
// Do not emit an error here. Otherwise each error would result in one emit.
1223-
// The errors will be returned in the result anyway
1224+
cb(err, res);
12241225
};
12251226
};
12261227

@@ -1229,6 +1230,25 @@ Multi.prototype.exec = Multi.prototype.EXEC = Multi.prototype.exec_batch = funct
12291230
var self = this;
12301231
var index = 0;
12311232
var args;
1233+
var args_len = 1;
1234+
var callbackWithoutCB = function (err, res) {
1235+
if (err) {
1236+
self.results.push(err);
1237+
// Add the position to the error
1238+
var i = self.results.length - 1;
1239+
self.results[i].position = i;
1240+
} else {
1241+
self.results.push(res);
1242+
}
1243+
// Do not emit an error here. Otherwise each error would result in one emit.
1244+
// The errors will be returned in the result anyway
1245+
};
1246+
var lastCallback = function (cb) {
1247+
return function (err, res) {
1248+
cb(err, res);
1249+
callback(null, self.results);
1250+
};
1251+
};
12321252
if (len === 0) {
12331253
if (callback) {
12341254
// The execution order won't be obtained in this case
@@ -1238,21 +1258,21 @@ Multi.prototype.exec = Multi.prototype.EXEC = Multi.prototype.exec_batch = funct
12381258
}
12391259
return true;
12401260
}
1241-
this.results = new Array(len);
1261+
this.results = [];
12421262
this._client.cork(len);
1243-
var lastCallback = function (cb) {
1244-
return function (err, res) {
1245-
cb(err, res);
1246-
callback(null, self.results);
1247-
};
1248-
};
12491263
while (args = this.queue.shift()) {
12501264
var command = args.shift();
12511265
var cb;
1252-
if (typeof args[args.length - 1] === 'function') {
1266+
// Improve the callback function generation by using new Function
1267+
// check https://github.com/petkaantonov/bluebird/blob/master/src/promisify.js
1268+
args_len = args.length - 1;
1269+
if (typeof args[args_len] === 'function') {
12531270
cb = this.callback(args.pop(), index);
12541271
} else {
1255-
cb = this.callback(undefined, index);
1272+
cb = callbackWithoutCB;
1273+
if (typeof args[args_len] === 'undefined') {
1274+
args.pop();
1275+
}
12561276
}
12571277
if (callback && index === len - 1) {
12581278
cb = lastCallback(cb);

0 commit comments

Comments
 (0)