From b92a56029dc2b96ec58d53a84616405fac3c1f55 Mon Sep 17 00:00:00 2001 From: Jens Hauke Date: Fri, 13 Jun 2014 19:21:19 +0200 Subject: [PATCH 1/3] Fix package.json syntax. --- package.json | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 6e2fff1..08bec05 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,14 @@ { - 'name' : 'msgpack-rpc', - 'version' : '0.0.1', - 'description' : 'node-msgpack-rpc is an implementation of the Msgpack-RPC protocol specification for node.js', - 'homepage' : 'https://github.com/bpot/node-msgpack-rpc', - 'main' : './lib/msgpack-rpc', - 'repository' : { + "name" : "msgpack-rpc", + "version" : "0.0.1", + "description" : "node-msgpack-rpc is an implementation of the Msgpack-RPC protocol specification for node.js", + "homepage" : "https://github.com/bpot/node-msgpack-rpc", + "main" : "./lib/msgpack-rpc", + "repository" : { "type" : "git", "url" : "https://github.com/bpot/node-msgpack-rpc.git" }, - 'directories': { + "directories": { "lib": "lib" } } From fb7ff83d1a1811132d6fce245773bb79476f573b Mon Sep 17 00:00:00 2001 From: Jens Hauke Date: Fri, 13 Jun 2014 20:48:48 +0200 Subject: [PATCH 2/3] Fix error response for "no handler" and "unknown method". --- lib/msgpack-rpc.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/msgpack-rpc.js b/lib/msgpack-rpc.js index 12612f3..4367d44 100644 --- a/lib/msgpack-rpc.js +++ b/lib/msgpack-rpc.js @@ -18,6 +18,11 @@ RPCResponse.prototype.result = function(args) { } RPCResponse.prototype.error = function(error) { + if (error instanceof Error) { + // add 'msg' because 'message' property is not enumerable + // and will not be part of an msgpack encoded message. + error.msg = error.message; + } this.stream.respond(this.seqid, error, null); } @@ -94,7 +99,15 @@ MsgpackRPCStream.prototype.invokeHandler = function(method, params) { if(this.handler[method]) { this.handler[method].apply(this.handler, params); } else { - response.error(new Error("unknown method")); + error(new Error("unknown method")); + } + } else { + error(new Error("no handler")); + } + function error(err) { + var response = params.pop(); + if (response && response instanceof RPCResponse) { + response.error(err); } } } From df1469e76da60afd44abddef4fed4a6545d97d7e Mon Sep 17 00:00:00 2001 From: Jens Hauke Date: Fri, 13 Jun 2014 20:56:13 +0200 Subject: [PATCH 3/3] Add MsgpackRPCStream.prototype.setHandler method. With this a client can set a handler. With this a server rpc can invoke a rpc at the client: // rpc at the server handler.pingpong = function(data, response) { // invoke a rpc on the client response.stream.invoke('pong', data, function (err, response2){ // Send response to the client response.result(response2); }); }; --- lib/msgpack-rpc.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/msgpack-rpc.js b/lib/msgpack-rpc.js index 4367d44..2374da7 100644 --- a/lib/msgpack-rpc.js +++ b/lib/msgpack-rpc.js @@ -161,6 +161,10 @@ MsgpackRPCStream.prototype.close = function() { this.stream.end(); } +MsgpackRPCStream.prototype.setHandler = function(handler) { + this.handler = handler; +} + exports.createClient = function(port, hostname,cb) { var s = new MsgpackRPCStream(new net.createConnection(port, hostname)); if(typeof hostname == 'function') s.on('ready', hostname);