-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonrpc.js
195 lines (175 loc) · 5.33 KB
/
jsonrpc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* JSON-RPC 2.0
* http://www.jsonrpc.org/specification
*
* Manages RPC-JSON messages
*
* Sample usage:
*
* var http = require('http'),
* RpcService = require('./jsonrpc').RpcService;
*
* http.createServer(function (request, response) {
* if(request.method == 'POST'){
* new RpcService(request, response, RPCMethods, true);
* }else{
* response.end('Hello world!');
* }
* }).listen(80);
*
* RPCMethods = {
* insert: function(rpc, param1, param2){
* if(param1!=param2)
* rpc.error('Params doesn\'t match!');
* else
* rpc.response('Params are OK!');
* },
* _private: function(){
* // leading underscore makes method private
* // and not accessible by public RPC interface
* }
* }
*
* Sample message traffic:
*
* --> {"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}
* <-- {"jsonrpc": "2.0", "result": 19, "id": 1}
*
* --> {"jsonrpc": "2.0", "method": "subtract", "params": [23, 42], "id": 2}
* <-- {"jsonrpc": "2.0", "result": -19, "id": 2}
*
*/
exports.RpcService = RpcService;
/**
* new RpcService(request, response, methods, debug)
* - request (Object): http.ServerRequest object
* - response (Object): http.ServerResponse object
* - methods (Object): available RPC methods.
* methods = {insert: function(rpc, param1, param2, ... paramN){})
*
* Creates an RPC handler object which parses the input, forwards the data
* to a RPC method and outputs response messages.
**/
function RpcService (request, response, methods, debug) {
this.RPCMethods = methods;
this.HTTPRequest = request;
this.HTTPResponse = response;
this.json = false;
this.id = false;
if (typeof this.RPCMethods == 'object' && this.HTTPRequest && this.HTTPResponse) {
// start post body processing
this._processRequest();
} else {
throw new Error('Invalid params');
}
}
//////////// PUBLIC METHODS ////////////
/**
* RpcService.prototype.error = function(error) -> Boolean
* - error (String): Error message
*
* Sends an error message if error occured.
* Returns true if a message was sent and false if blank was sent
**/
RpcService.prototype.error = function (error) {
return this._output(false, error);
}
/**
* RpcService.prototype.response = function(result) -> Boolean
* - result (String): Response message
*
* Sends the response message if everything was successful
* Returns true if a message was sent and false if blank was sent
**/
RpcService.prototype.response = function (result) {
return this._output(result, false);
}
//////////// PRIVATE METHODS ////////////
/**
* RpcService._processRequest() -> undefined
*
* Runs after the initialization. Calls the handler to process request body
**/
RpcService.prototype._processRequest = function () {
this._post_handler();
}
/**
* RpcService._run() -> undefined
*
* Checks if input is correct and passes the params to an actual RPC method
**/
RpcService.prototype._run = function () {
if (!this.json)
return this.error();
if (!this.RPCMethods)
return this.error('No methods', this.id);
if (!this.json.method || // method name must be set
this.json.method.substr(0, 1) == '_' || // method name cant begin with an underscore
!this.json.method in this.RPCMethods || // method needs to be inside this.RPCMethods
typeof this.RPCMethods[this.json.method] != 'function') // method needs to be function
return this.error('Invalid request method', this.id);
// runs the actual RPC method
this.RPCMethods[this.json.method].apply(
this.RPCMethods, [this].concat(this.json.params || []));
}
/**
* RpcService._output(result, error) -> Boolean
* - result (String): response message
* - error (String): error message
*
* Creates the response, outputs it and closes the connection.
* Returns true if a message was sent and false if blank was sent
**/
RpcService.prototype._output = function (result, error) {
this.HTTPResponse.writeHead(error ? 500 : 200, {'Content-Type': 'application/json'});
if (typeof this.id == 'undefined' || this.id === null) {
this.HTTPResponse.end();
return false;
} else {
var resp = {
'jsonrpc': '2.0',
id: this.id
}
if (error) {
resp.error = error
} else {
resp.result = result
}
this.HTTPResponse.end(JSON.stringify(resp));
return true;
}
}
/**
* RpcService._post_handler() -> undefined
*
* Checks if request is valid and handles all errors
*/
RpcService.prototype._post_handler = function () {
this.HTTPRequest.setEncoding('utf8');
var that = this;
this._post_body_handler(function (body) {
try {
that.json = JSON.parse(body);
that.id = that.json && that.json.id;
that._run();
} catch (E) {
console.log(E.message)
that.error('Runtime error', -1);
}
});
}
/**
* RpcService._post_body_handler(callback) -> undefined
* - callback (Function): callback function to be called with the complete body
*
* Parses the request body into one larger string
*/
RpcService.prototype._post_body_handler = function (callback) {
var _CONTENT = '';
this.HTTPRequest.addListener('data', function (chunk) {
_CONTENT += chunk;
});
this.HTTPRequest.addListener('end', function () {
callback(_CONTENT);
});
}