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

Move command out of the index.js

This commit is contained in:
Ruben Bridgewater
2015-09-30 02:03:37 +02:00
parent 29b31f749a
commit fba050802b
6 changed files with 53 additions and 44 deletions

View File

@@ -5,6 +5,7 @@ var net = require("net"),
util = require("util"), util = require("util"),
utils = require("./lib/utils"), utils = require("./lib/utils"),
Queue = require("./lib/queue"), Queue = require("./lib/queue"),
Command = require("./lib/command"),
events = require("events"), events = require("events"),
parsers = [], parsers = [],
// This static list of commands is updated from time to time. // 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 // That way the result / error won't stay in a try catch block and catch user things
this.reply_parser.send_error = function (data) { this.reply_parser.send_error = function (data) {
process.nextTick(function() { process.nextTick(function() {
this.return_error(data); self.return_error(data);
}.bind(this)); });
}.bind(this); };
this.reply_parser.send_reply = function (data) { this.reply_parser.send_reply = function (data) {
process.nextTick(function() { process.nextTick(function() {
this.return_reply(data); self.return_reply(data);
}.bind(this)); });
}.bind(this); };
}; };
RedisClient.prototype.on_ready = function () { 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); this.retry_timer = setTimeout(retry_connection, this.retry_delay, this);
}; };
var err_code = /^([A-Z]+)\s+(.+)$/;
RedisClient.prototype.return_error = function (err) { RedisClient.prototype.return_error = function (err) {
var command_obj = this.command_queue.shift(), queue_len = this.command_queue.length; var command_obj = this.command_queue.shift(), queue_len = this.command_queue.length;
// send_command might have been used wrong => catch those cases too // 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; 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! // LUA script could return user errors that don't behave like all other errors!
if (match) { if (match) {
err.code = match[1]; 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 // 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 // pubsub subscription, don't pop the command queue as we'll only be consuming
// the head command prematurely. // 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(); type = reply[0].toString();
} }
@@ -613,6 +613,7 @@ RedisClient.prototype.return_reply = function (reply) {
if (Buffer.isBuffer(reply)) { if (Buffer.isBuffer(reply)) {
reply = reply.toString(); reply = reply.toString();
} }
// If in monitoring mode only two commands are valid ones: AUTH and MONITOR wich reply with OK
len = reply.indexOf(" "); len = reply.indexOf(" ");
timestamp = reply.slice(0, len); timestamp = reply.slice(0, len);
argindex = reply.indexOf('"'); 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) { 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; 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) { commands.forEach(function (fullCommand) {
var command = fullCommand.split(' ')[0]; 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 we asked for strings, even in detect_buffers mode, then return strings:
if (replies[i] instanceof Error) { 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! // LUA script could return user errors that don't behave like all other errors!
if (match) { if (match) {
replies[i].code = match[1]; 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()'); throw new Error('Unknown type of connection in createClient()');
}; };
exports.print = function (err, reply) { exports.print = utils.print;
if (err) { exports.Multi = Multi;
console.log("Error: " + err);
} else {
console.log("Reply: " + reply);
}
};

13
lib/command.js Normal file
View File

@@ -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;

View File

@@ -130,23 +130,23 @@ ReplyParser.prototype.execute = function (buffer) {
try { try {
type = this._buffer[this._offset++]; type = this._buffer[this._offset++];
if (type === 43) { // + if (type === 43) { // Strings +
ret = this._parseResult(type); ret = this._parseResult(type);
this.send_reply(ret); this.send_reply(ret);
} else if (type === 45) { // - } else if (type === 45) { // Errors -
ret = this._parseResult(type); ret = this._parseResult(type);
this.send_error(ret); this.send_error(ret);
} else if (type === 58) { // : } else if (type === 58) { // Integers :
ret = this._parseResult(type); ret = this._parseResult(type);
this.send_reply(ret); this.send_reply(ret);
} else if (type === 36) { // $ } else if (type === 36) { // Bulk strings $
ret = this._parseResult(type); ret = this._parseResult(type);
this.send_reply(ret); this.send_reply(ret);
} else if (type === 42) { // 42 * } else if (type === 42) { // Arrays *
// set a rewind point. if a failure occurs, // set a rewind point. if a failure occurs,
// wait for the next execute()/append() and try again // wait for the next execute()/append() and try again
offset = this._offset - 1; offset = this._offset - 1;

View File

@@ -46,8 +46,20 @@ function toArray(args) {
return arr; return arr;
} }
function print (err, reply) {
if (err) {
console.log("Error: " + err);
} else {
console.log("Reply: " + reply);
}
}
var redisErrCode = /^([A-Z]+)\s+(.+)$/;
module.exports = { module.exports = {
reply_to_strings: replyToStrings, reply_to_strings: replyToStrings,
reply_to_object: replyToObject, reply_to_object: replyToObject,
to_array: toArray to_array: toArray,
print: print,
errCode: redisErrCode
}; };

View File

@@ -5,7 +5,7 @@ var config = require("../lib/config");
var helper = require("../helper"); var helper = require("../helper");
var redis = config.redis; var redis = config.redis;
describe("The 'sync' method", function () { describe.skip("The 'sync' method", function () {
helper.allTests(function(parser, ip, args) { helper.allTests(function(parser, ip, args) {
@@ -23,7 +23,7 @@ describe("The 'sync' method", function () {
// "Protocol error, got "K" as reply type byte" // "Protocol error, got "K" as reply type byte"
// I'm uncertain if this is correct behavior or not // I'm uncertain if this is correct behavior or not
// TODO: Fix the command queue state error occuring // 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) { client.on('error', function(err) {
assert.equal(err.message, 'Protocol error, got "K" as reply type byte'); assert.equal(err.message, 'Protocol error, got "K" as reply type byte');
assert.equal(err.command, 'SET'); assert.equal(err.command, 'SET');

View File

@@ -16,15 +16,15 @@ function startRedis (conf, done) {
// don't start redis every time we // don't start redis every time we
// include this helper file! // include this helper file!
if (!process.env.REDIS_TESTS_STARTED) { if (!process.env.REDIS_TESTS_STARTED) {
process.env.REDIS_TESTS_STARTED = true; process.env.REDIS_TESTS_STARTED = true;
before(function (done) { before(function (done) {
startRedis('./conf/redis.conf', done); startRedis('./conf/redis.conf', done);
}); });
after(function (done) { after(function (done) {
if (rp) rp.stop(done); if (rp) rp.stop(done);
}); });
} }
module.exports = { module.exports = {