Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
tarmolov committed Dec 27, 2011
0 parents commit 1fb470f
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
node-jsonrpc-client
================

Simple JSON-RPC client.

Installation
------------

Simply use npm to install it

npm install jsonrpc-ws

or download the code from the repo and stick it in your project folder.

Usage
------------

var JSONRPCClient = require('node-jsonrpc-client');
var client = JSONRPCClient.create("https://my-json-rpc-server/path-to-something/");
client.call('add', [1, 2], function (result) {
console.log(result);
});
99 changes: 99 additions & 0 deletions jsonrpc-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* Simple JSON-RPC Client
*/

var http, https, JSONRPCClient, URL_REGEXP;

// Take url in parts: scheme, host, root zone, port, and path of request
URL_REGEXP = /(http|https):\/\/([\w\-_]+(?:\.([\w\-_]+))+)(?::([\d]+))*([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/;

http = require('http');
https = require('https');

/**
* JSON-RPC client
* @param {String} scheme Scheme of request (http|https)
* @param {String} host JSON-RPC server
* @param {Number} port Port
* @param {String} path Path of request
*/
SimpleJSONRPCClient = function (scheme, host, port, path) {
this.scheme = scheme;
this.host = host;
this.port = port;
this.path = path;

/**
* Call method of JSON-RPC API
* @param {String} method Method
* @param {Array} params Params of request
* @param {Function} [onSuccessCallback] Callback for success result
* @param {Function} [onErrorCallback] Callback for error
*/
this.call = function(method, params, onSuccessCallback, onErrorCallback) {
var requestJSON, requestOptions, request;

requestJSON = JSON.stringify({
'id': '' + (new Date()).getTime(),
'method': method,
'params': params
});

requestOptions = {
host: this.host,
port: this.port,
path: this.path,
method: 'POST',
headers: {
'Content-Length': requestJSON.length
}
}

// Send request
request = require(this.scheme).request(requestOptions, onComplete);
request.write(requestJSON);
request.end();

function onComplete (res) {
var buffer = '';

res.setEncoding('utf8');

res.on('data', function(chunk) {
buffer = buffer + chunk;
});

res.on('end', function () {
var decoded = JSON.parse(buffer);
if(decoded.result) {
onSuccessCallback && onSuccessCallback(decoded.result);
} else {
onErrorCallback && onErrorCallback(decoded.error);
}
});

res.on('error', function (error) {
onErrorCallback && onErrorCallback(error);
});
}
};
}

module.exports = {

/**
* Create JSON-RPC client
* @param {String} url Server url
* @return {JSONRPCClient} client
*/
create: function (url) {
var urlParts = url.match(URL_REGEXP),
host = urlParts[2],
port = urlParts[4] || urlParts[1] === 'https' ? 443 : 8080,
path = urlParts[5],
scheme = urlParts[1] || 'http';

return new JSONRPCClient(scheme, host, port, path);
}

};
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name" : "jsonrpc-client",
"version" : "0.0.1",
"description" : "A simple JSON-RPC client.",
"author" : "Alexander Tarmolov <alexander@tarmolov.ru>",
"main" : "./jsonrpc-client.js",
"repository" : {
"type" : "git",
"url" : "http://github.com/tarmolov/node-jsonrpc-client"
},
"licenses" : [
{
"type": "AS IS"
}
],
"engine" : { "node" : ">=0.2.0" }
}

0 comments on commit 1fb470f

Please sign in to comment.