json-rpc-core
Advanced tools
Comparing version 1.2.0 to 1.2.1
16
index.js
@@ -62,9 +62,18 @@ var _ = require('highland') | ||
function callMethod(method,params,context){ | ||
if(lodash.isFunction(method)){ | ||
return method.apply(context,params) | ||
} | ||
return method | ||
} | ||
function callFromRequest(method,params){ | ||
return Promise.try(function(){ | ||
if(lodash.has(methods,method)){ | ||
return methods[method].apply(methods,params) | ||
var func = lodash.get(methods,method) | ||
return callMethod(func,params,methods) | ||
} | ||
if(lodash.has(extensions,method)){ | ||
return extensions[method].apply(extensions,params) | ||
var func = lodash.get(extensions,method) | ||
return callMethod(func,params,extensions) | ||
} | ||
@@ -163,2 +172,5 @@ throw new jrs.err.MethodNotFoundError(method) | ||
var argumentList = Array.prototype.slice.call(arguments,1) | ||
if(lodash.isArray(method)){ | ||
method = method.join('.') | ||
} | ||
message = jrs.request(id,method,argumentList) | ||
@@ -165,0 +177,0 @@ outstream.write(message) |
{ | ||
"name": "json-rpc-core", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Transport agnostic JSON RPC message handling API meant for streams and promises. ", | ||
"main": "index.js", | ||
"keywords": ["rpc","json","json rpc","stream", "streams","promises","agnostic","highland","client","server","messaging","message passing"], | ||
"keywords": [ | ||
"rpc", | ||
"json", | ||
"json rpc", | ||
"stream", | ||
"streams", | ||
"promises", | ||
"agnostic", | ||
"highland", | ||
"client", | ||
"server", | ||
"messaging", | ||
"notify", | ||
"notifications", | ||
"message passing", | ||
"redis", | ||
"pub", | ||
"sub", | ||
"mqtt", | ||
"rabbitmq", | ||
"socketio", | ||
"udp" | ||
], | ||
"directories": { | ||
@@ -25,11 +47,11 @@ "example": "examples", | ||
"dependencies": { | ||
"bluebird": "^3.4.1", | ||
"highland": "^2.10.1", | ||
"jsonrpc-serializer": "^0.1.8", | ||
"lodash": "^4.15.0", | ||
"bluebird": "^3.4.7", | ||
"highland": "^2.10.2", | ||
"jsonrpc-serializer": "^0.2.1", | ||
"lodash": "^4.17.4", | ||
"shortid": "^2.2.6" | ||
}, | ||
"devDependencies": { | ||
"tape": "^4.6.0" | ||
"tape": "^4.6.3" | ||
} | ||
} |
@@ -9,3 +9,3 @@ # json-rpc-core | ||
This library is meant to be used with some transport layer, it can be any type, Redis, TCP, UDP, SocketIO, etc. | ||
All this library does is create a node steam compatible interface to pipe in incoming messages and pipe out outgoing messages. | ||
All this library does is create a node stream compatible interface to pipe in incoming messages and pipe out outgoing messages. | ||
As long as your server and client classes are syncronous or return promises then you can wrap them with JSON-RPC-Core add a transport layer | ||
@@ -95,2 +95,5 @@ and you have a working RPC interface. | ||
Call a remote function by name. Allows any number of parameters. Returns a promise which can resolve or reject. | ||
Nested functions are now supported using [lodash's "get" syntax](https://lodash.com/docs/4.17.4#get). Seperate nested paths as an array or using | ||
"dot" notation. Be aware that nested calls get serialized to a string with '.'. Call can also access non functions, | ||
and will try to return them as is. This will allow you to remotely access to the prototype or other parts of your class. | ||
```js | ||
@@ -100,2 +103,18 @@ rpc.call(remoteFunction,param1,param2, etc...).then(function(result){ | ||
}) | ||
//ways to call nested functions, these will call a function on the object | ||
//{ | ||
// deeply:{ | ||
// nested:{ | ||
// method:function(){ return true} | ||
// } | ||
// } | ||
//} | ||
rpc.call(['deeply','nested','method']).then(function(result)... | ||
rpc.call('deeply.nested.method').then(function(result)... | ||
//call will work on non functions, and will just try to return them as is | ||
rpc.call('plainObject').then(function(result)... | ||
//can also use nested notation | ||
``` | ||
@@ -132,3 +151,3 @@ | ||
JSON RPC protocol allows for custom methods signified by the "rpc" prefix. If a conflict is found with a local method | ||
the local method will override the extension. | ||
the local method will override the extension. Discover will not return nested methods. | ||
@@ -190,3 +209,3 @@ ```js | ||
message:'your error message', | ||
data:[] | ||
data:['functionThatThrowsError','stackTrace'] | ||
} | ||
@@ -201,3 +220,3 @@ }) | ||
message:'The JSON-RPC method does not exist, or is an invalid one.', | ||
data:[] | ||
data:['functionThatDoesNotExist'] | ||
} | ||
@@ -207,3 +226,3 @@ }) | ||
You should always add a catch block to your function calls. The message field is typically the most descriptive part | ||
of the error. The data field can contain additial data about the error such as stack trace but currently is unused. | ||
of the error. The most recent version of json-rpc-core adds stack trace data when available to the data parameters. | ||
@@ -210,0 +229,0 @@ |
@@ -29,2 +29,11 @@ var EmitterRPC = require('../examples/emitter-rpc') | ||
return Promise.resolve(msg) | ||
}, | ||
nested:{ | ||
call:function(){ | ||
return true | ||
} | ||
}, | ||
plainObject:{ | ||
array:[1,2,3,], | ||
string:'hello world' | ||
} | ||
@@ -116,20 +125,24 @@ } | ||
t.test('no method',function(t){ | ||
t.plan(1) | ||
t.plan(2) | ||
client.call('nomethod').then(t.end).catch(function(err){ | ||
t.ok(err) | ||
t.ok(err.data.length) | ||
}) | ||
}) | ||
t.test('fake error',function(t){ | ||
t.plan(2) | ||
t.plan(4) | ||
client.call('fakeError','fakeError').then(t.end).catch(function(err){ | ||
t.ok(err) | ||
t.ok(err.data.length) | ||
}) | ||
server.call('fakeError').then(t.end).catch(function(err){ | ||
t.ok(err) | ||
t.ok(err.data.length) | ||
}) | ||
}) | ||
t.test('runtime error',function(t){ | ||
t.plan(1) | ||
t.plan(2) | ||
client.call('runtimeError').then(t.end).catch(function(err){ | ||
t.ok(err) | ||
t.ok(err.data.length) | ||
}) | ||
@@ -175,2 +188,21 @@ }) | ||
}) | ||
t.test('nested call',function(t){ | ||
t.plan(2) | ||
client.call('nested.call').then(function(result){ | ||
t.ok(result) | ||
}).catch(t.end) | ||
client.call(['nested','call']).then(function(result){ | ||
t.ok(result) | ||
}).catch(t.end) | ||
}) | ||
t.test('plain object',function(t){ | ||
t.plan(2) | ||
client.call('plainObject').then(function(result){ | ||
t.deepEqual(result,methods.plainObject) | ||
}) | ||
client.call('plainObject.array.1').then(function(result){ | ||
t.deepEqual(result,2) | ||
}) | ||
}) | ||
t.test('discover',function(t){ | ||
@@ -177,0 +209,0 @@ t.plan(2) |
var test = require('tape') | ||
require('./emitter-rpc')(test) | ||
require('./stress')(test) |
@@ -5,3 +5,2 @@ var EmitterRPC = require('../examples/emitter-rpc') | ||
var Promise = require('bluebird') | ||
var test = require('tape') | ||
@@ -23,25 +22,27 @@ var methods = { | ||
test('stress test',function(t){ | ||
t.test('send a bunch',function(t){ | ||
t.plan(sends) | ||
module.exports = function(test){ | ||
test('stress test',function(t){ | ||
t.test('send a bunch',function(t){ | ||
t.plan(sends) | ||
send() | ||
function send(){ | ||
sent+=1 | ||
var str = 'client'+sent | ||
client.call('slowEcho',str).then(function(result){ | ||
t.equal(str,result) | ||
}).catch(t.end) | ||
send() | ||
function send(){ | ||
sent+=1 | ||
var str = 'client'+sent | ||
client.call('slowEcho',str).then(function(result){ | ||
t.equal(str,result) | ||
}).catch(t.end) | ||
if(sent < sends){ | ||
setTimeout(send,1) | ||
if(sent < sends){ | ||
setTimeout(send,1) | ||
} | ||
} | ||
} | ||
}) | ||
t.test('end',function(t){ | ||
client.end() | ||
server.end() | ||
t.end() | ||
}) | ||
}) | ||
t.test('end',function(t){ | ||
client.end() | ||
server.end() | ||
t.end() | ||
}) | ||
}) | ||
} | ||
@@ -48,0 +49,0 @@ // setInterval(function(){ |
26333
619
229
+ Addedinherits@2.0.4(transitive)
+ Addedjsonrpc-serializer@0.2.1(transitive)
- Removedjsonrpc-serializer@0.1.8(transitive)
Updatedbluebird@^3.4.7
Updatedhighland@^2.10.2
Updatedjsonrpc-serializer@^0.2.1
Updatedlodash@^4.17.4