Permalink
Browse files

Upgraded everything to work with node 0.4.0. Added default support fo…

…r handling utf8 strings (but you can turn this off if you want for better average performance). All tests pass.
  • Loading branch information...
1 parent 2125ddb commit 93876b1da7eac60574c88d30d9f54ae39e51a656 @bnoguchi committed Feb 11, 2011
View
@@ -29,7 +29,7 @@ var commandConfig = require("./commandConfig"),
CRLF = "\r\n";
var net = require("net"),
- sys = require("sys"),
+ util = require("util"),
EventEmitter = require("events").EventEmitter,
ErrorReply = require("./replies/errorReply").ErrorReply;
@@ -100,11 +100,19 @@ Object.defineProperty(Queue.prototype, 'length', {
* -noconnection: when a connection or reconnection fails
* @options is a hash with the following optional keys:
* -maxReconnectionAttempts
+ * -utf8 - defaults to `true`, which automatically converts utf8 to Buffer,
+ * so you don't have to do so explicitly. When set to `false`,
+ * performance is higher, but you have to explicitly detect and
+ * convert utf8 strings to Buffer.
+ * e.g.,
+ * if (isUtf8(string)) string = new Buffer(string);
*/
var Client = exports.Client = function Client (port, host, options) {
// Inherit from EventEmitter
EventEmitter.call(this);
+ // Defaults
+ this.utf8 = true;
// Configure according to options
options = options || {};
[this.DEFAULT_OPTIONS, options].forEach( function (obj) {
@@ -182,7 +190,7 @@ var Client = exports.Client = function Client (port, host, options) {
client.expectingClose = false;
if (client.connectionsMade > 1 && client.commandHistory.length > 0) {
- sys.log("[RECONNECTION] some commands orphaned (" + client.commandHistory.length + "). notifying...");
+ util.log("[RECONNECTION] some commands orphaned (" + client.commandHistory.length + "). notifying...");
client.callbackOrphanedCommandsWithError();
}
if (client.currDB) client.select(client.currDB);
@@ -215,7 +223,7 @@ var Client = exports.Client = function Client (port, host, options) {
}
});
};
-sys.inherits(Client, EventEmitter);
+util.inherits(Client, EventEmitter);
Client.prototype.DEFAULT_OPTIONS = {
maxReconnectionAttempts: 10,
@@ -245,7 +253,7 @@ Client.prototype.handleData = function (data) {
};
Client.prototype.handleReply = function (reply, isParsingExecReply) {
-// sys.log(sys.inspect(reply)); // Uncomment this to see the reply
+// util.log(util.inspect(reply)); // Uncomment this to see the reply
/* Handle special case of PubSub */
var pubSubCallback, replyValue;
if (reply.isMessage || reply.isPMessage) {
@@ -272,7 +280,7 @@ Client.prototype.handleReply = function (reply, isParsingExecReply) {
replyValue = reply.replyValue;
}
-// sys.log(sys.inspect(commandForReply)); // Uncomment this to see which command corresponds to this
+// util.log(util.inspect(commandForReply)); // Uncomment this to see which command corresponds to this
/* Handle Errors */
if (reply instanceof ErrorReply) {
@@ -504,7 +512,7 @@ Client.prototype.writeCmdToStream = function (commandAsArray) {
numArgs--;
}
for (i = 0; i < numArgs; i++) {
- if (commandAsArray[i] instanceof Buffer) {
+ if (Buffer.isBuffer(commandAsArray[i])) {
useBuffer = true;
break;
}
@@ -518,7 +526,7 @@ Client.prototype.writeCmdToStream = function (commandAsArray) {
stream.write(cmdStr);
for (i = 0; i < numArgs; i++) {
arg = commandAsArray[i];
- if (arg instanceof Buffer) {
+ if (Buffer.isBuffer(arg)) {
stream.write("$" + arg.length + CRLF);
stream.write(arg);
stream.write(CRLF);
@@ -580,7 +588,7 @@ Client.prototype.transaction = function (doStuffInsideTransaction) {
Client.prototype.sendMultiToServer = function () {
this.multi( function (err, reply) {
if (err) throw err;
- if (reply !== true) throw new Error("Expected 'OK'. Reply is " + sys.inspect(reply));
+ if (reply !== true) throw new Error("Expected 'OK'. Reply is " + util.inspect(reply));
});
};
View
@@ -22,7 +22,7 @@ THE SOFTWARE.
*/
-var sys = require("sys");
+var util = require("util");
var Buffer = require("buffer").Buffer,
CRLF = "\r\n";
@@ -67,13 +67,13 @@ toHash: function () {
// Default Callback in case a commandCallback is not explicitly
// set on the command instance
commandCallback: function (err, reply) {
- if (err) sys.log(err);
+ if (err) util.log(err);
},
hasBufferArgs: function () {
var args = this.commandArgs;
for (var i = 0, len = args.length; i < len; i++) {
- if (args[i] instanceof Buffer) {
+ if (Buffer.isBuffer(args[i])) {
return true;
}
}
@@ -98,7 +98,7 @@ writeToStream: function () {
stream.write(cmdStr);
for (i = 0; i < numArgs; i++) {
arg = commandArgs[i];
- if (arg instanceof Buffer) {
+ if (Buffer.isBuffer(arg)) {
stream.write("$" + arg.length + CRLF);
stream.write(arg);
stream.write(CRLF);
@@ -107,13 +107,22 @@ writeToStream: function () {
stream.write("$" + arg.length + CRLF + arg + CRLF);
}
}
+ } else if (this.client.utf8) {
+ stream.write(cmdStr);
+ for (i = 0; i < numArgs; i++) {
+ arg = commandArgs[i] + '';
+ if (!Buffer.isBuffer(arg)) arg = new Buffer(arg);
+ stream.write("$" + arg.length + CRLF);
+ stream.write(arg);
+ stream.write(CRLF);
+ }
} else {
for (i = 0; i < numArgs; i++) {
arg = commandArgs[i] + '';
cmdStr += "$" + arg.length + CRLF + arg + CRLF;
}
stream.write(cmdStr);
}
-// delete this.client; // Removes client from command, so command's easier on the eyes when sys inspecting
+// delete this.client; // Removes client from command, so command's easier on the eyes when util inspecting
}
};
View
@@ -22,8 +22,7 @@ THE SOFTWARE.
*/
-var sys = require("sys"),
- Buffer = require("buffer").Buffer,
+var Buffer = require("buffer").Buffer,
CR = require("../reply").CR, // \r
IntegerReply = require("./integerReply").IntegerReply;
@@ -22,8 +22,7 @@ THE SOFTWARE.
*/
-var sys = require("sys"),
- CR = require("../reply").CR,
+var CR = require("../reply").CR,
LF = require("../reply").LF,
InlineReply = require("./inlineReply").InlineReply;
@@ -22,8 +22,7 @@ THE SOFTWARE.
*/
-var sys = require("sys"),
- CR = require("../reply").CR;
+var CR = require("../reply").CR;
var toSmallString = require("../perf").toSmallString;
@@ -22,8 +22,7 @@ THE SOFTWARE.
*/
-var sys = require("sys"),
- CR = require("../reply").CR,
+var CR = require("../reply").CR,
LF = require("../reply").LF,
InlineReply = require("./inlineReply").InlineReply;
@@ -22,8 +22,7 @@ THE SOFTWARE.
*/
-var sys = require("sys"),
- Reply = require("../reply").Reply,
+var Reply = require("../reply").Reply,
CR = require("../reply").CR,
LF = require("../reply").LF,
BulkReply = require("./bulkReply").BulkReply,
View
@@ -22,7 +22,7 @@ THE SOFTWARE.
*/
-var sys = require("sys");
+var util = require("util");
var PLUS = 0x2B, // +
MINUS = 0x2D, // -
@@ -74,7 +74,7 @@ Reply.fromTypeCode = function (typeCode, client, context) {
}
}
} else if (!replyClass) {
-// throw new Error("Invalid type code: " + sys.inspect(String.fromCharCode(typeCode)));
+// throw new Error("Invalid type code: " + util.inspect(String.fromCharCode(typeCode)));
return replyClass;
}
return new replyClass(client, newContext);
View
@@ -48,7 +48,7 @@ vows.describe("Redis PubSub Commands").addBatch({
assert.deepEqual(triple, ["subscribe", "channel-4", 2]);
},
'and then unsubscribing': {
- topic: function (_, client) {
+ topic: function (_, triple, client) {
client.unsubscribe("channel-3");
client.unsubscribe("channel-4", this.callback);
},
@@ -68,7 +68,7 @@ vows.describe("Redis PubSub Commands").addBatch({
assert.deepEqual(triple, ["psubscribe", "channel-5.*", 1]);
},
'and then punsubscribing': {
- topic: function (_, client) {
+ topic: function (_, triple, client) {
client.punsubscribe("channel-5.*", this.callback);
},
'should return [command type, channel, num channels subscribed to]': function (err, triple) {
@@ -51,6 +51,8 @@ vows.describe("Redis String Commands").addBatch({
'should return the same special characters': function (err, result) {
var specialJson = JSON.stringify({'a': 'ö'});
+ console.log(specialJson);
+ console.log(specialJson.length);
assert.equal(result, specialJson);
}
}),
@@ -106,7 +108,7 @@ vows.describe("Redis String Commands").addBatch({
path = paths.shift();
}
}
- var imageBuffer = new Buffer(Buffer.byteLength(fileContents));
+ var imageBuffer = new Buffer(Buffer.byteLength(fileContents, 'binary'));
imageBuffer.write(fileContents, 0, "binary");
client.set('png_image', imageBuffer, this.callback);
@@ -129,7 +131,7 @@ vows.describe("Redis String Commands").addBatch({
path = paths.shift();
}
}
- var imageBuffer = new Buffer(Buffer.byteLength(fileContents));
+ var imageBuffer = new Buffer(Buffer.byteLength(fileContents, 'binary'));
imageBuffer.write(fileContents, 0, "binary");
// TODO Make value binary a Buffer vs the current binary string?

0 comments on commit 93876b1

Please sign in to comment.