Permalink
Browse files

docs

  • Loading branch information...
1 parent ec36c10 commit fc631d0c44c691ba71a730cd9b3641e1eadd40ca @tj committed Oct 23, 2012
Showing with 79 additions and 0 deletions.
  1. +28 −0 lib/client.js
  2. +51 −0 lib/server.js
View
@@ -1,11 +1,32 @@
+/**
+ * Expose `Client`.
+ */
+
module.exports = Client;
+/**
+ * Initialize an rpc client with `sock`.
+ *
+ * @param {Socket} sock
+ * @api public
+ */
+
function Client(sock) {
sock.format('json');
this.sock = sock;
}
+/**
+ * Invoke method `name` with args and invoke the
+ * tailing callback function.
+ *
+ * @param {String} name
+ * @param {Mixed} ...
+ * @param {Function} fn
+ * @api public
+ */
+
Client.prototype.call = function(name){
var args = [].slice.call(arguments, 1, -1);
var fn = arguments[arguments.length - 1];
@@ -24,6 +45,13 @@ Client.prototype.call = function(name){
});
};
+/**
+ * Fetch the methods exposed and invoke `fn(err, methods)`.
+ *
+ * @param {Function} fn
+ * @api public
+ */
+
Client.prototype.methods = function(fn){
this.sock.send({
type: 'methods'
View
@@ -5,15 +5,36 @@
var debug = require('debug');
+/**
+ * Expose `Server`.
+ */
+
module.exports = Server;
+/**
+ * Initialize a server with the given `sock`.
+ *
+ * @param {Socket} sock
+ * @api public
+ */
+
function Server(sock) {
sock.format('json');
this.sock = sock;
this.methods = {};
this.sock.on('message', this.onmessage.bind(this));
}
+/**
+ * Return method descriptions with:
+ *
+ * `.name` string
+ * `.params` array
+ *
+ * @return {Object}
+ * @api private
+ */
+
Server.prototype.methodDescriptions = function(){
var obj = {};
var fn;
@@ -29,10 +50,25 @@ Server.prototype.methodDescriptions = function(){
return obj;
};
+/**
+ * Response with the method descriptions.
+ *
+ * @param {Function} fn
+ * @api private
+ */
+
Server.prototype.respondWithMethods = function(reply){
reply({ methods: this.methodDescriptions() });
};
+/**
+ * Handle `msg`.
+ *
+ * @param {Object} msg
+ * @param {Object} fn
+ * @api private
+ */
+
Server.prototype.onmessage = function(msg, reply){
if ('methods' == msg.type) return this.respondWithMethods(reply);
@@ -58,6 +94,13 @@ Server.prototype.onmessage = function(msg, reply){
fn.apply(null, args);
};
+/**
+ * Expose many or a single method.
+ *
+ * @param {String|Object} name
+ * @param {String|Object} fn
+ * @api public
+ */
Server.prototype.expose = function(name, fn){
if (1 == arguments.length) {
@@ -70,6 +113,14 @@ Server.prototype.expose = function(name, fn){
}
};
+/**
+ * Parse params.
+ *
+ * @param {Function} fn
+ * @return {Array}
+ * @api private
+ */
+
function params(fn) {
var ret = fn.toString().match(/^function *(\w*)\((.*?)\)/)[2];
if (ret) return ret.split(/ *, */);

0 comments on commit fc631d0

Please sign in to comment.