diff --git a/index.js b/index.js index dcdbaf96bb..8bc2b72430 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,7 @@ var net = require("net"), util = require("util"), utils = require("./lib/utils"), Queue = require("./lib/queue"), + Command = require("./lib/command"), events = require("events"), parsers = [], // This static list of commands is updated from time to time. @@ -277,14 +278,14 @@ RedisClient.prototype.init_parser = function () { // That way the result / error won't stay in a try catch block and catch user things this.reply_parser.send_error = function (data) { process.nextTick(function() { - this.return_error(data); - }.bind(this)); - }.bind(this); + self.return_error(data); + }); + }; this.reply_parser.send_reply = function (data) { process.nextTick(function() { - this.return_reply(data); - }.bind(this)); - }.bind(this); + self.return_reply(data); + }); + }; }; RedisClient.prototype.on_ready = function () { @@ -498,7 +499,6 @@ RedisClient.prototype.connection_gone = function (why) { this.retry_timer = setTimeout(retry_connection, this.retry_delay, this); }; -var err_code = /^([A-Z]+)\s+(.+)$/; RedisClient.prototype.return_error = function (err) { var command_obj = this.command_queue.shift(), queue_len = this.command_queue.length; // send_command might have been used wrong => catch those cases too @@ -508,7 +508,7 @@ RedisClient.prototype.return_error = function (err) { err.command = command_obj.command; } - var match = err.message.match(err_code); + var match = err.message.match(utils.errCode); // LUA script could return user errors that don't behave like all other errors! if (match) { err.code = match[1]; @@ -537,7 +537,7 @@ RedisClient.prototype.return_reply = function (reply) { // If the "reply" here is actually a message received asynchronously due to a // pubsub subscription, don't pop the command queue as we'll only be consuming // the head command prematurely. - if (this.pub_sub_mode && Array.isArray(reply) && reply.length > 0 && reply[0]) { + if (this.pub_sub_mode && Array.isArray(reply) && reply[0]) { type = reply[0].toString(); } @@ -613,6 +613,7 @@ RedisClient.prototype.return_reply = function (reply) { if (Buffer.isBuffer(reply)) { reply = reply.toString(); } + // If in monitoring mode only two commands are valid ones: AUTH and MONITOR wich reply with OK len = reply.indexOf(" "); timestamp = reply.slice(0, len); argindex = reply.indexOf('"'); @@ -627,16 +628,6 @@ RedisClient.prototype.return_reply = function (reply) { } }; -// This Command constructor is ever so slightly faster than using an object literal, but more importantly, using -// a named constructor helps it show up meaningfully in the V8 CPU profiler and in heap snapshots. -function Command(command, args, sub_command, buffer_args, callback) { - this.command = command; - this.args = args; - this.sub_command = sub_command; - this.buffer_args = buffer_args; - this.callback = callback; -} - RedisClient.prototype.send_command = function (command, args, callback) { var arg, command_obj, i, elem_count, buffer_args, stream = this.stream, command_str = "", buffered_writes = 0, err; @@ -842,8 +833,6 @@ function Multi(client, args) { } } -exports.Multi = Multi; - commands.forEach(function (fullCommand) { var command = fullCommand.split(' ')[0]; @@ -1045,7 +1034,7 @@ Multi.prototype.execute_callback = function (err, replies) { // If we asked for strings, even in detect_buffers mode, then return strings: if (replies[i] instanceof Error) { - var match = replies[i].message.match(err_code); + var match = replies[i].message.match(utils.errCode); // LUA script could return user errors that don't behave like all other errors! if (match) { replies[i].code = match[1]; @@ -1132,10 +1121,5 @@ exports.createClient = function(port_arg, host_arg, options) { throw new Error('Unknown type of connection in createClient()'); }; -exports.print = function (err, reply) { - if (err) { - console.log("Error: " + err); - } else { - console.log("Reply: " + reply); - } -}; +exports.print = utils.print; +exports.Multi = Multi; diff --git a/lib/command.js b/lib/command.js new file mode 100644 index 0000000000..4aa795b696 --- /dev/null +++ b/lib/command.js @@ -0,0 +1,13 @@ +'use strict'; + +// This Command constructor is ever so slightly faster than using an object literal, but more importantly, using +// a named constructor helps it show up meaningfully in the V8 CPU profiler and in heap snapshots. +function Command(command, args, sub_command, buffer_args, callback) { + this.command = command; + this.args = args; + this.sub_command = sub_command; + this.buffer_args = buffer_args; + this.callback = callback; +} + +module.exports = Command; diff --git a/lib/parsers/javascript.js b/lib/parsers/javascript.js index dbbe658b6b..029080ec29 100644 --- a/lib/parsers/javascript.js +++ b/lib/parsers/javascript.js @@ -130,23 +130,23 @@ ReplyParser.prototype.execute = function (buffer) { try { type = this._buffer[this._offset++]; - if (type === 43) { // + + if (type === 43) { // Strings + ret = this._parseResult(type); this.send_reply(ret); - } else if (type === 45) { // - + } else if (type === 45) { // Errors - ret = this._parseResult(type); this.send_error(ret); - } else if (type === 58) { // : + } else if (type === 58) { // Integers : ret = this._parseResult(type); this.send_reply(ret); - } else if (type === 36) { // $ + } else if (type === 36) { // Bulk strings $ ret = this._parseResult(type); this.send_reply(ret); - } else if (type === 42) { // 42 * + } else if (type === 42) { // Arrays * // set a rewind point. if a failure occurs, // wait for the next execute()/append() and try again offset = this._offset - 1; diff --git a/lib/utils.js b/lib/utils.js index 42ae211c3a..c749380858 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -46,8 +46,20 @@ function toArray(args) { return arr; } +function print (err, reply) { + if (err) { + console.log("Error: " + err); + } else { + console.log("Reply: " + reply); + } +} + +var redisErrCode = /^([A-Z]+)\s+(.+)$/; + module.exports = { reply_to_strings: replyToStrings, reply_to_object: replyToObject, - to_array: toArray + to_array: toArray, + print: print, + errCode: redisErrCode }; diff --git a/test/commands/sync.spec.js b/test/commands/sync.spec.js index c8aa5df40e..a2d9009abe 100644 --- a/test/commands/sync.spec.js +++ b/test/commands/sync.spec.js @@ -5,7 +5,7 @@ var config = require("../lib/config"); var helper = require("../helper"); var redis = config.redis; -describe("The 'sync' method", function () { +describe.skip("The 'sync' method", function () { helper.allTests(function(parser, ip, args) { @@ -23,7 +23,7 @@ describe("The 'sync' method", function () { // "Protocol error, got "K" as reply type byte" // I'm uncertain if this is correct behavior or not // TODO: Fix the command queue state error occuring - it.skip('try to sync with the server and fail other commands', function (done) { + it('try to sync with the server and fail other commands', function (done) { client.on('error', function(err) { assert.equal(err.message, 'Protocol error, got "K" as reply type byte'); assert.equal(err.command, 'SET'); diff --git a/test/helper.js b/test/helper.js index a9f66d4a40..5f4f791b95 100644 --- a/test/helper.js +++ b/test/helper.js @@ -16,15 +16,15 @@ function startRedis (conf, done) { // don't start redis every time we // include this helper file! if (!process.env.REDIS_TESTS_STARTED) { - process.env.REDIS_TESTS_STARTED = true; + process.env.REDIS_TESTS_STARTED = true; - before(function (done) { - startRedis('./conf/redis.conf', done); - }); + before(function (done) { + startRedis('./conf/redis.conf', done); + }); - after(function (done) { - if (rp) rp.stop(done); - }); + after(function (done) { + if (rp) rp.stop(done); + }); } module.exports = {