-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedNode.js
49 lines (38 loc) · 1.24 KB
/
RedNode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
var net = require('net')
var stream = require ('stream')
function RedNode(host, port) {
this.host = host;
this.port = port;
}
RedNode.prototype.connect = function() {
if (this.host && this.host === '')
throw Error('Host cannot be empty');
if (this.port && this.port === 0)
throw Error('Port cannot be zero');
}
var Command = {
parse: function (cmd) {
if (typeof cmd !== 'object')
throw {message:"Command should be object", name:"ObjectNotFound"};
var commands = '';
var command = cmd;
if (command.key && command.value)
commands += '\r\n$'+ command.command.length + '\r\n' + command.command + '\r\n$' + command.key.length + '\r\n' +
command.key + '\r\n$' + command.value.length + '\r\n' + command.value;
else
commands += '\r\n$'+ command.command.length + '\r\n' + command.command;
return '*' + commands.length + commands;
}
};
exports.testInstance = function(test) {
test.ok(true, new RedNode(127,0,0,1));
test.done();
}
exports.testCommandParserOutput = function(test) {
test.throws(Command.parse("aaaa"), "ObjectNotFound");
test.strictEqual("*1$4INFO\r\n", Command.parse({"command":"INFO"}));
test.done();
}
exports.testCommandInfo = function(test) {
test.done();
}