Skip to content

Commit fba0508

Browse files
author
Ruben Bridgewater
committed
Move command out of the index.js
1 parent 29b31f7 commit fba0508

File tree

6 files changed

+53
-44
lines changed

6 files changed

+53
-44
lines changed

index.js

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var net = require("net"),
55
util = require("util"),
66
utils = require("./lib/utils"),
77
Queue = require("./lib/queue"),
8+
Command = require("./lib/command"),
89
events = require("events"),
910
parsers = [],
1011
// This static list of commands is updated from time to time.
@@ -277,14 +278,14 @@ RedisClient.prototype.init_parser = function () {
277278
// That way the result / error won't stay in a try catch block and catch user things
278279
this.reply_parser.send_error = function (data) {
279280
process.nextTick(function() {
280-
this.return_error(data);
281-
}.bind(this));
282-
}.bind(this);
281+
self.return_error(data);
282+
});
283+
};
283284
this.reply_parser.send_reply = function (data) {
284285
process.nextTick(function() {
285-
this.return_reply(data);
286-
}.bind(this));
287-
}.bind(this);
286+
self.return_reply(data);
287+
});
288+
};
288289
};
289290

290291
RedisClient.prototype.on_ready = function () {
@@ -498,7 +499,6 @@ RedisClient.prototype.connection_gone = function (why) {
498499
this.retry_timer = setTimeout(retry_connection, this.retry_delay, this);
499500
};
500501

501-
var err_code = /^([A-Z]+)\s+(.+)$/;
502502
RedisClient.prototype.return_error = function (err) {
503503
var command_obj = this.command_queue.shift(), queue_len = this.command_queue.length;
504504
// send_command might have been used wrong => catch those cases too
@@ -508,7 +508,7 @@ RedisClient.prototype.return_error = function (err) {
508508
err.command = command_obj.command;
509509
}
510510

511-
var match = err.message.match(err_code);
511+
var match = err.message.match(utils.errCode);
512512
// LUA script could return user errors that don't behave like all other errors!
513513
if (match) {
514514
err.code = match[1];
@@ -537,7 +537,7 @@ RedisClient.prototype.return_reply = function (reply) {
537537
// If the "reply" here is actually a message received asynchronously due to a
538538
// pubsub subscription, don't pop the command queue as we'll only be consuming
539539
// the head command prematurely.
540-
if (this.pub_sub_mode && Array.isArray(reply) && reply.length > 0 && reply[0]) {
540+
if (this.pub_sub_mode && Array.isArray(reply) && reply[0]) {
541541
type = reply[0].toString();
542542
}
543543

@@ -613,6 +613,7 @@ RedisClient.prototype.return_reply = function (reply) {
613613
if (Buffer.isBuffer(reply)) {
614614
reply = reply.toString();
615615
}
616+
// If in monitoring mode only two commands are valid ones: AUTH and MONITOR wich reply with OK
616617
len = reply.indexOf(" ");
617618
timestamp = reply.slice(0, len);
618619
argindex = reply.indexOf('"');
@@ -627,16 +628,6 @@ RedisClient.prototype.return_reply = function (reply) {
627628
}
628629
};
629630

630-
// This Command constructor is ever so slightly faster than using an object literal, but more importantly, using
631-
// a named constructor helps it show up meaningfully in the V8 CPU profiler and in heap snapshots.
632-
function Command(command, args, sub_command, buffer_args, callback) {
633-
this.command = command;
634-
this.args = args;
635-
this.sub_command = sub_command;
636-
this.buffer_args = buffer_args;
637-
this.callback = callback;
638-
}
639-
640631
RedisClient.prototype.send_command = function (command, args, callback) {
641632
var arg, command_obj, i, elem_count, buffer_args, stream = this.stream, command_str = "", buffered_writes = 0, err;
642633

@@ -842,8 +833,6 @@ function Multi(client, args) {
842833
}
843834
}
844835

845-
exports.Multi = Multi;
846-
847836
commands.forEach(function (fullCommand) {
848837
var command = fullCommand.split(' ')[0];
849838

@@ -1045,7 +1034,7 @@ Multi.prototype.execute_callback = function (err, replies) {
10451034

10461035
// If we asked for strings, even in detect_buffers mode, then return strings:
10471036
if (replies[i] instanceof Error) {
1048-
var match = replies[i].message.match(err_code);
1037+
var match = replies[i].message.match(utils.errCode);
10491038
// LUA script could return user errors that don't behave like all other errors!
10501039
if (match) {
10511040
replies[i].code = match[1];
@@ -1132,10 +1121,5 @@ exports.createClient = function(port_arg, host_arg, options) {
11321121
throw new Error('Unknown type of connection in createClient()');
11331122
};
11341123

1135-
exports.print = function (err, reply) {
1136-
if (err) {
1137-
console.log("Error: " + err);
1138-
} else {
1139-
console.log("Reply: " + reply);
1140-
}
1141-
};
1124+
exports.print = utils.print;
1125+
exports.Multi = Multi;

lib/command.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
// This Command constructor is ever so slightly faster than using an object literal, but more importantly, using
4+
// a named constructor helps it show up meaningfully in the V8 CPU profiler and in heap snapshots.
5+
function Command(command, args, sub_command, buffer_args, callback) {
6+
this.command = command;
7+
this.args = args;
8+
this.sub_command = sub_command;
9+
this.buffer_args = buffer_args;
10+
this.callback = callback;
11+
}
12+
13+
module.exports = Command;

lib/parsers/javascript.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,23 +130,23 @@ ReplyParser.prototype.execute = function (buffer) {
130130
try {
131131
type = this._buffer[this._offset++];
132132

133-
if (type === 43) { // +
133+
if (type === 43) { // Strings +
134134
ret = this._parseResult(type);
135135

136136
this.send_reply(ret);
137-
} else if (type === 45) { // -
137+
} else if (type === 45) { // Errors -
138138
ret = this._parseResult(type);
139139

140140
this.send_error(ret);
141-
} else if (type === 58) { // :
141+
} else if (type === 58) { // Integers :
142142
ret = this._parseResult(type);
143143

144144
this.send_reply(ret);
145-
} else if (type === 36) { // $
145+
} else if (type === 36) { // Bulk strings $
146146
ret = this._parseResult(type);
147147

148148
this.send_reply(ret);
149-
} else if (type === 42) { // 42 *
149+
} else if (type === 42) { // Arrays *
150150
// set a rewind point. if a failure occurs,
151151
// wait for the next execute()/append() and try again
152152
offset = this._offset - 1;

lib/utils.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,20 @@ function toArray(args) {
4646
return arr;
4747
}
4848

49+
function print (err, reply) {
50+
if (err) {
51+
console.log("Error: " + err);
52+
} else {
53+
console.log("Reply: " + reply);
54+
}
55+
}
56+
57+
var redisErrCode = /^([A-Z]+)\s+(.+)$/;
58+
4959
module.exports = {
5060
reply_to_strings: replyToStrings,
5161
reply_to_object: replyToObject,
52-
to_array: toArray
62+
to_array: toArray,
63+
print: print,
64+
errCode: redisErrCode
5365
};

test/commands/sync.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var config = require("../lib/config");
55
var helper = require("../helper");
66
var redis = config.redis;
77

8-
describe("The 'sync' method", function () {
8+
describe.skip("The 'sync' method", function () {
99

1010
helper.allTests(function(parser, ip, args) {
1111

@@ -23,7 +23,7 @@ describe("The 'sync' method", function () {
2323
// "Protocol error, got "K" as reply type byte"
2424
// I'm uncertain if this is correct behavior or not
2525
// TODO: Fix the command queue state error occuring
26-
it.skip('try to sync with the server and fail other commands', function (done) {
26+
it('try to sync with the server and fail other commands', function (done) {
2727
client.on('error', function(err) {
2828
assert.equal(err.message, 'Protocol error, got "K" as reply type byte');
2929
assert.equal(err.command, 'SET');

test/helper.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ function startRedis (conf, done) {
1616
// don't start redis every time we
1717
// include this helper file!
1818
if (!process.env.REDIS_TESTS_STARTED) {
19-
process.env.REDIS_TESTS_STARTED = true;
19+
process.env.REDIS_TESTS_STARTED = true;
2020

21-
before(function (done) {
22-
startRedis('./conf/redis.conf', done);
23-
});
21+
before(function (done) {
22+
startRedis('./conf/redis.conf', done);
23+
});
2424

25-
after(function (done) {
26-
if (rp) rp.stop(done);
27-
});
25+
after(function (done) {
26+
if (rp) rp.stop(done);
27+
});
2828
}
2929

3030
module.exports = {

0 commit comments

Comments
 (0)