Skip to content

Instantly share code, notes, and snippets.

@fictorial
Created March 31, 2010 00:02
Show Gist options
  • Save fictorial/349765 to your computer and use it in GitHub Desktop.
Save fictorial/349765 to your computer and use it in GitHub Desktop.
TypeError: Cannot call method 'set' of null
at doConnect (net:616:23)
at Stream.connect (net:670:30)
at Object.createConnection (net:379:5)
at Object.createClient (/Users/brian/projects/redis-node-client2/redis.js:20:25)
at Object.<anonymous> (/Users/brian/projects/redis-node-client2/example.js:2:32)
at Module._compile (node.js:704:23)
at node.js:732:20
at fs:51:23
at node.js:813:9
⚡ node -v
v0.1.33-157-g7c77a56
...
function Client(port, host) {
events.EventEmitter.call(this);
this.port = port || 6379;
this.host = host || '127.0.0.1';
this.callbacks = [];
}
sys.inherits(Client, events.EventEmitter);
exports.createClient = function (port, host) {
var client = new Client(port, host);
client.socket = net.createConnection(port, host);
...
var net = require("net"),
sys = require("sys"),
events = require("events");
var CRLF = "\r\n";
function Client(port, host) {
events.EventEmitter.call(this);
this.port = port || 6379;
this.host = host || '127.0.0.1';
this.callbacks = [];
this.replyBuffer = '';
}
sys.inherits(Client, events.EventEmitter);
exports.createClient = function (port, host) {
var client = new Client(port, host);
client.stream = new net.Stream();
client.stream.connect(port, host);
client.stream.addListener("connect", function () {
sys.puts("Connected to Redis.");
client.stream.setEncoding('binary');
client.stream.setNoDelay();
client.stream.setTimeout(0);
});
client.stream.addListener("data", function (chunk) {
client.addReplyChunk(chunk);
});
client.stream.addListener("close", function (inError) {
sys.puts("Lost connection to Redis.");
if (inError) {
sys.puts("Attempting reconnection...");
client.stream.connect(client.port, client.host);
}
});
return client;
}
function debugFilter(what) {
var str = what;
str.replace(/\r\n/g, '<CRLF>');
return str;
}
Client.prototype.sendToRedis = function (formattedRequest, responseCallback) {
sys.puts("Sending to redis:\n" + debugFilter(formattedRequest));
this.callbacks.append(responseCallback);
this.stream.write(formattedRequest);
};
Client.prototype.addReplyChunk = function (chunk) {
this.replyBuffer += chunk;
// TODO now parse the reply buffer looking for Redis replies ...
};
// Returns a function that given some command name, formats a Redis command
// using the multi-bulk request protocol format. Redis will soon (2.0?) only
// support this format.
//
// *3\r\n Number of arguments in the request.
// $3\r\n Length of first argument.
// set\r\n First argument.
// $6\r\n Length of second argument.
// foobar\r\n Second argument.
// $3\r\n Length of third argument.
// baz\r\n Third argument.
function makeCommandMethod(commandName) {
var length = commandName.length;
var self = this;
return function () {
var callback = arguments.length > 0 ? arguments[arguments.length - 1] : null;
var buffer = "*" + arguments.length + CRLF;
for (var i = 0; i < arguments.length; i++) {
var arg = arguments[i].toString();
buffer += "$" + arg.length + CRLF + arg + CRLF;
self.sendToRedis(buffer, function (err, value) {
if (typeof(callback) === 'function') {
if (err) callback(err);
callback(null, parseReply(value));
}
});
}
};
}
var redisCommands = [
"auth", "bgsave", "dbsize", "decr", "decrby", "del", "exists", "expire",
"flushall", "flushdb", "get", "getset", "incr", "incrby", "info", "keys",
"lastsave", "lindex", "llen", "lpop", "lpush", "lrange", "lrem", "lset",
"ltrim", "mget", "move", "mset", "msetnx", "randomkey", "rename",
"renamenx", "rpop", "rpoplpush", "rpush", "sadd", "save", "scard", "sdiff",
"sdiffstore", "select", "set", "setnx", "shutdown", "sinter",
"sinterstore", "sismember", "smembers", "smove", "spop", "srandmember",
"srem", "sunion", "sunionstore", "ttl", "type", "zadd", "zcard", "zrange",
"zrangebyscore", "zrem", "zrevrange", "zscore"
];
redisCommands.forEach(function (commandName) {
sys.puts("Creating command formatter for Redis command '" + commandName + "' ...");
Client.prototype[commandName] = makeCommandMethod(commandName);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment