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

Move multi; commands; createClient code into separate files

This commit is contained in:
Ruben Bridgewater
2016-02-22 19:38:07 +01:00
parent ce80569bfe
commit 614e35ab57
7 changed files with 586 additions and 26 deletions

View File

@@ -1,22 +1,24 @@
'use strict';
// hgetall converts its replies to an Object. If the reply is empty, null is returned.
// These function are only called with internal data and have therefor always the same instanceof X
function replyToObject(reply) {
if (reply.length === 0 || !Array.isArray(reply)) { // TODO: Check why the isArray check is needed and what value reply has in that case
// The reply might be a string or a buffer if this is called in a transaction (multi)
if (reply.length === 0 || !(reply instanceof Array)) {
return null;
}
var obj = {};
for (var j = 0; j < reply.length; j += 2) {
obj[reply[j].toString('binary')] = reply[j + 1];
for (var i = 0; i < reply.length; i += 2) {
obj[reply[i].toString('binary')] = reply[i + 1];
}
return obj;
}
function replyToStrings(reply) {
if (Buffer.isBuffer(reply)) {
if (reply instanceof Buffer) {
return reply.toString();
}
if (Array.isArray(reply)) {
if (reply instanceof Array) {
var res = new Array(reply.length);
for (var i = 0; i < reply.length; i++) {
// Recusivly call the function as slowlog returns deep nested replies
@@ -39,35 +41,68 @@ function print (err, reply) {
var redisErrCode = /^([A-Z]+)\s+(.+)$/;
// Deep clone arbitrary objects with arrays. Can't handle cyclic structures (results in a range error)
// Any attribute with a non primitive value besides object and array will be passed by reference (e.g. Buffers, Maps, Functions)
function clone (obj) {
if (obj) {
var copy;
if (obj.constructor === Array) {
copy = new Array(obj.length);
for (var i = 0; i < obj.length; i++) {
copy[i] = clone(obj[i]);
}
return copy;
var copy;
if (Array.isArray(obj)) {
copy = new Array(obj.length);
for (var i = 0; i < obj.length; i++) {
copy[i] = clone(obj[i]);
}
if (obj.constructor === Object) {
copy = {};
for (var elem in obj) {
if (!obj.hasOwnProperty(elem)) {
// Do not add non own properties to the cloned object
continue;
}
copy[elem] = clone(obj[elem]);
}
return copy;
return copy;
}
if (Object.prototype.toString.call(obj) === '[object Object]') {
copy = {};
var elems = Object.keys(obj);
var elem;
while (elem = elems.pop()) {
copy[elem] = clone(obj[elem]);
}
return copy;
}
return obj;
}
function convenienceClone (obj) {
return clone(obj) || {};
}
function callbackOrEmit (self, callback, err, res) {
if (callback) {
callback(err, res);
} else if (err) {
self.emit('error', err);
}
}
function replyInOrder (self, callback, err, res) {
var command_obj = self.command_queue.peekBack() || self.offline_queue.peekBack();
if (!command_obj) {
process.nextTick(function () {
callbackOrEmit(self, callback, err, res);
});
} else {
var tmp = command_obj.callback;
command_obj.callback = tmp ?
function (e, r) {
tmp(e, r);
callbackOrEmit(self, callback, err, res);
} :
function (e, r) {
if (e) {
self.emit('error', e);
}
callbackOrEmit(self, callback, err, res);
};
}
}
module.exports = {
reply_to_strings: replyToStrings,
reply_to_object: replyToObject,
print: print,
err_code: redisErrCode,
clone: clone
clone: convenienceClone,
callback_or_emit: callbackOrEmit,
reply_in_order: replyInOrder
};