Browse files

As a lib.

Signed-off-by: Mathieu Lecarme <mathieu.lecarme@af83.com>
  • Loading branch information...
1 parent b165df7 commit 3c8996c99b15c2ce86d9f92a709e2a3714f18cca @athoune committed Jan 6, 2012
Showing with 64 additions and 38 deletions.
  1. +16 −3 README.md
  2. +21 −19 lib/encoder.js
  3. +1 −0 lib/index.js
  4. +16 −16 lib/redisd.js
  5. +10 −0 simple_server.js
View
19 README.md
@@ -5,11 +5,24 @@ Implement your own server using the redis protocol.
Build a fake redis or a redis slave.
-Status
-------
+Install
+-------
+
+ npm install
+
+Use
+---
-Too early to be usable.
+ var redisd = require('redisd');
+ var server = redisd.createServer(function(command) {
+ console.log('query', command);
+ this.encode(['pim', 'pam']);// the answer
+ });
+ server.listen(6379, function() {
+ console.log('fake redis started');
+ });
+You can talk to it with any redis tools. Try *redis-cli*.
Licence
-------
View
40 lib/encoder.js
@@ -1,42 +1,44 @@
-var bulk = function(writer, value) {
+
+var Response = function(writer) {
+ this.writer = writer;
+};
+
+Response.prototype.bulk = function(value) {
var b = new Buffer(value.toString());
- writer.write('$' + b.length + '\r\n');
- writer.write(b);
- writer.write('\r\n');
+ this.writer.write('$' + b.length + '\r\n');
+ this.writer.write(b);
+ this.writer.write('\r\n');
};
// Automatic encoding. Binary safe.
-var encode = function(writer, value) {
+Response.prototype.encode = function(value) {
+ var that = this;
if(Array.isArray(value)) {
- writer.write('*' + value.length + '\r\n');
- value.forEach(function(v) { bulk(writer, v); });
+ this.writer.write('*' + value.length + '\r\n');
+ value.forEach(function(v) { that.bulk(v); });
} else {
switch(typeof value) {
case 'number':
- writer.write(':' + value + '\r\n');
+ this.writer.write(':' + value + '\r\n');
break;
case 'boolean':
- writer.write(':' + (value ? '1':'0') + '\r\n');
+ this.writer.write(':' + (value ? '1':'0') + '\r\n');
break;
default:
- bulk(writer, value);
+ this.bulk(value);
break;
}
}
};
// An error.
-var error = function(writer, msg) {
- writer.write('-' + msg + '\r\n');
+Response.prototype.error = function(msg) {
+ this.writer.write('-' + msg + '\r\n');
};
// A simple response.
-var singleline = function(writer, line) {
- writer.write('+' + line + '\r\n');
+Response.prototype.singleline = function(line) {
+ this.writer.write('+' + line + '\r\n');
};
-module.exports = {
- encode: encode,
- error: error,
- singleline: singleline
-};
+exports.Response = Response;
View
1 lib/index.js
@@ -0,0 +1 @@
+exports.redisd = require('./redisd');
View
32 lib/redisd.js
@@ -1,5 +1,5 @@
var net = require('net'),
- encoder = require('./encoder');
+ Response = require('./encoder').Response;
var Parser;
// hiredis might not be installed
@@ -12,20 +12,20 @@ try {
Parser = require('../node_modules/redis/lib/parser/javascript').Parser;
}
-var server = net.createServer({}, function(c) {
- var parser = new Parser();
- parser.on('error', function(err) {
- console.log(err);
+var createServer = function(onCommand) {
+ return net.createServer({}, function(connection) {
+ var parser = new Parser();
+ var response = new Response(connection);
+ parser.on('error', function(err) {
+ console.log(err);
+ });
+ parser.on('reply', function(reply) { // Parser was build for clients, not servers.
+ onCommand.call(response, reply);
+ });
+ connection.on('data', function(data) {
+ parser.execute(data);
+ });
});
- parser.on('reply', function(reply) { // Parser was build for clients, not servers.
- encoder.encode(c, ['pim', 'pam']);
- console.log('query', reply);
- });
- c.on('data', function(data) {
- parser.execute(data);
- });
-});
+};
-server.listen(6379, function() {
- console.log('fake redis started');
-});
+exports.createServer = createServer;
View
10 simple_server.js
@@ -0,0 +1,10 @@
+var redisd = require('./lib/redisd');
+
+var server = redisd.createServer(function(command) {
+ this.encode(['pim', 'pam']);
+ console.log('query', command);
+});
+
+server.listen(6379, function() {
+ console.log('fake redis started');
+});

0 comments on commit 3c8996c

Please sign in to comment.