Skip to content

Commit

Permalink
Merge 32bde26 into 8f9f9d2
Browse files Browse the repository at this point in the history
  • Loading branch information
dfellis committed Aug 29, 2018
2 parents 8f9f9d2 + 32bde26 commit 81e117e
Show file tree
Hide file tree
Showing 31 changed files with 2,277 additions and 2,274 deletions.
30 changes: 30 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"env": {
"browser": true,
"commonjs": true,
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 2016
},
"rules": {
"indent": [
"error",
2
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"never"
]
}
}
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules/
coverage/
coverage/
.nyc_output/
package-lock.json
17 changes: 7 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
language: node_js
node_js:
- 0.8
- 0.10
- 0.12
before_install:
npm install -g npm@1.4.x
- 8
- 10

after_script:
npm run coveralls

notifications:
email:
recipients:
- dispatch@uber.com
on_success: change
on_failure: change
email:
recipients:
- isv.damocles@gmail.com
on_success: change
on_failure: change
25 changes: 25 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
The MIT License

Portions Copyright (C) 2018 by David Ellis

Portions Copyright (C) 2013 by Uber Technologies, Inc, David Ellis

Portions Copyright (C) 2011 by Agrosica, Inc, David Ellis, Alain Rodriguez, Hector Lugo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
24 changes: 0 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,27 +346,3 @@ The transport is expected to have two methods: ``request`` and ``shutdown``.
The transport is expected to have a ``shutdown`` method that behaves exactly the same as the method described above.

It is also expected to make use of a ``handler`` method that the server attaches to it. This method expects two arguments, the first is a JSON-RPC object (not a string), but if the input is not valid JSON will handle the unparsed data just fine. The second argument is a callback that it provides with the response JSON-RPC object (not a string).

## License (MIT)

Portions Copyright (C) 2013 by Uber Technologies, Inc, David Ellis

Portions Copyright (C) 2011 by Agrosica, Inc, David Ellis, Alain Rodriguez, Hector Lugo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
44 changes: 23 additions & 21 deletions bin/jsonrpc-repl
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
#!/usr/bin/env node

var repl = require('repl');
var commander = require('commander');
var l = require('lambda-js');
var jsonrpc = require('../lib/index');
var Client = jsonrpc.client;
var TcpTransport = jsonrpc.transports.client.tcp;
var HttpTransport = jsonrpc.transports.client.http;
var repl = require('repl')
var commander = require('commander')
var l = require('lambda-js')
var jsonrpc = require('../lib/index')
var Client = jsonrpc.client
var TcpTransport = jsonrpc.transports.client.tcp
var HttpTransport = jsonrpc.transports.client.http

commander
.option('-s, --server <hostname>', 'The hostname the server is located on. (Default: "localhost")', 'localhost')
.option('-p, --port <portnumber>', 'The port the server is bound to. (Default: 80)', 80)
.option('-t, --tcp', 'Connects to the server via TCP instead of HTTP (Default: false)', false)
.option('--path <path>', 'The path part of the URL, e.g. "/rpc" (Default: "/")', '/')
.parse(process.argv);
.option('-s, --server <hostname>', 'The hostname the server is located on. (Default: "localhost")', 'localhost')
.option('-p, --port <portnumber>', 'The port the server is bound to. (Default: 80)', 80)
.option('-t, --tcp', 'Connects to the server via TCP instead of HTTP (Default: false)', false)
.option('--path <path>', 'The path part of the URL, e.g. "/rpc" (Default: "/")', '/')
.parse(process.argv)

var Transport = commander.tcp ? TcpTransport : HttpTransport;
var Transport = commander.tcp ? TcpTransport : HttpTransport

new Client(new Transport(commander.server, commander.port, { path: commander.path }), {}, function(client) {
console.log('Connected to ' + commander.server + ':' + commander.port + commander.path + ' over ' + (commander.tcp ? 'TCP' : 'HTTP'));
console.log('The server reported the following methods:');
console.log(Object.getOwnPropertyNames(client).filter(l('name', 'name !== "transport"')).join(', '));
console.log('Access them with `rpc.foo(arg1, arg2, callbackFunction)`');
var r = repl.start({ prompt: 'jsonrpc> ', useGlobal: true });
r.context.rpc = client;
r.on('exit', process.exit.bind(process));
});
/* eslint-disable no-console */
console.log('Connected to ' + commander.server + ':' + commander.port + commander.path + ' over ' + (commander.tcp ? 'TCP' : 'HTTP'))
console.log('The server reported the following methods:')
console.log(Object.getOwnPropertyNames(client).filter(l('name', 'name !== "transport"')).join(', '))
console.log('Access them with `rpc.foo(arg1, arg2, callbackFunction)`')
/* eslint-enable no-console */
var r = repl.start({ prompt: 'jsonrpc> ', useGlobal: true })
r.context.rpc = client
r.on('exit', process.exit.bind(process))
})
154 changes: 77 additions & 77 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,29 @@

// The JSONRPC constructor *must* receive a server URL on initialization
function JSONRPC(transport, options, done) {
this.transport = transport;
// Parse any *options* provided to the client
// If no *options* object provided, create an empty one
if(typeof(options) !== "object" || options === null) {
options = {};
}
//add custom request id generator if provided in options
if(options.hasOwnProperty("idGenerator") && typeof(options.idGenerator) === "function") {
this.idGenerator = options.idGenerator;
}
//default
else this.idGenerator = Math.random;
this.transport = transport
// Parse any *options* provided to the client
// If no *options* object provided, create an empty one
if(typeof(options) !== 'object' || options === null) {
options = {}
}
//add custom request id generator if provided in options
if(options.hasOwnProperty('idGenerator') && typeof(options.idGenerator) === 'function') {
this.idGenerator = options.idGenerator
}
//default
else this.idGenerator = Math.random

// *autoRegister* methods from the server unless explicitly told otherwise
if(!options.hasOwnProperty("autoRegister") || options.autoRegister) {
this.request('rpc.methodList', [], function(err, result) {
if(!err) this.register(result);
if(done) done(this);
}.bind(this));
}
// Once the JSONRPC object has been properly initialized, return the object
// to the developer
return this;
// *autoRegister* methods from the server unless explicitly told otherwise
if(!options.hasOwnProperty('autoRegister') || options.autoRegister) {
this.request('rpc.methodList', [], function(err, result) {
if(!err) this.register(result)
if(done) done(this)
}.bind(this))
}
// Once the JSONRPC object has been properly initialized, return the object
// to the developer
return this
}

// ### The *request* function
Expand All @@ -46,71 +46,71 @@ function JSONRPC(transport, options, done) {
// results, and all of the arguments in between are the values passed to the
// remote method.
JSONRPC.prototype.request = function(method, args, callback) {
// The *contents* variable contains the JSON-RPC 1.0 POST string.
var requestId = this.idGenerator();
if(!requestId || requestId === null) {
if(callback instanceof Function) {
callback(new Error('Request id generator function should return an id'));
return;
}
// The *contents* variable contains the JSON-RPC 1.0 POST string.
var requestId = this.idGenerator()
if(!requestId || requestId === null) {
if(callback instanceof Function) {
callback(new Error('Request id generator function should return an id'))
return
}
}

var contents = {
method: method,
params: args,
id: requestId
};
this.transport.request(contents, function(response) {
if(!response && callback instanceof Function) {
callback(new Error("Server did not return valid JSON-RPC response"));
return;
}
if(callback instanceof Function) {
if (response instanceof Error){
callback(response);
} else if(response.error) {
if(response.error.message) {
var err = new Error(response.error.message);
Object.keys(response.error).forEach(function(key) {
if(key !== 'message') err[key] = response.error[key];
});
callback(err);
} else if (typeof response.error === 'string') {
callback(new Error(response.error));
} else {
callback(response.error);
}
} else {
callback(undefined, response.result);
}
var contents = {
method: method,
params: args,
id: requestId
}
this.transport.request(contents, function(response) {
if(!response && callback instanceof Function) {
callback(new Error('Server did not return valid JSON-RPC response'))
return
}
if(callback instanceof Function) {
if (response instanceof Error){
callback(response)
} else if(response.error) {
if(response.error.message) {
var err = new Error(response.error.message)
Object.keys(response.error).forEach(function(key) {
if(key !== 'message') err[key] = response.error[key]
})
callback(err)
} else if (typeof response.error === 'string') {
callback(new Error(response.error))
} else {
callback(response.error)
}
});
};
} else {
callback(undefined, response.result)
}
}
})
}

// ### The *register* function
// is a simple blocking function that takes a method name or array of
// method names and directly modifies the
JSONRPC.prototype.register = function(methods) {
if(!(methods instanceof Array)) {
methods = [methods];
}
methods.forEach(function(method) {
if(method !== "transport" && method !== "request" && method !== "register" && method !== "shutdown") {
this[method] = function() {
var theArgs = [];
for(var i = 0; i < arguments.length-1; i++) {
theArgs[i] = arguments[i];
}
var callback = arguments[arguments.length-1];
this.request(method, theArgs, callback);
};
if(!(methods instanceof Array)) {
methods = [methods]
}
methods.forEach(function(method) {
if(method !== 'transport' && method !== 'request' && method !== 'register' && method !== 'shutdown') {
this[method] = function() {
var theArgs = []
for(var i = 0; i < arguments.length-1; i++) {
theArgs[i] = arguments[i]
}
}.bind(this));
};
var callback = arguments[arguments.length-1]
this.request(method, theArgs, callback)
}
}
}.bind(this))
}

// Cleanly shutdown the JSONRPC client
JSONRPC.prototype.shutdown = function(done) {
this.transport.shutdown(done);
};
this.transport.shutdown(done)
}

module.exports = JSONRPC;
module.exports = JSONRPC
16 changes: 8 additions & 8 deletions lib/errorcode.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module.exports = {
paserError: -32700,
invalidRequest: -32600,
methodNotFound: -32601,
invalidParams: -32602,
internalError: -32603,
serverErrorStart: -32000,
serverErrorLast: -32099
};
paserError: -32700,
invalidRequest: -32600,
methodNotFound: -32601,
invalidParams: -32602,
internalError: -32603,
serverErrorStart: -32000,
serverErrorLast: -32099
}

0 comments on commit 81e117e

Please sign in to comment.