-
Notifications
You must be signed in to change notification settings - Fork 13
/
jsonrpc.js
375 lines (375 loc) · 11.1 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// **Github:** https://github.com/teambition/jsonrpc-lite
//
// http://www.jsonrpc.org/specification
// **License:** MIT
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
exports.jsonrpc = exports.parseObject = exports.parseJsonRpcString = exports.parseJsonRpcObject = exports.parse = exports.error = exports.success = exports.notification = exports.request = exports.JsonRpcError = exports.JsonRpcParsed = exports.ErrorObject = exports.SuccessObject = exports.NotificationObject = exports.RequestObject = exports.JsonRpc = void 0;
const hasOwnProperty = Object.prototype.hasOwnProperty;
const isInteger = typeof Number.isSafeInteger === 'function'
? Number.isSafeInteger // ECMAScript 2015
: function (num) {
return typeof num === 'number' && isFinite(num) && num === Math.floor(num) && Math.abs(num) <= 9007199254740991;
};
class JsonRpc {
constructor() {
this.jsonrpc = '2.0';
}
serialize() {
return JSON.stringify(this);
}
}
exports.JsonRpc = JsonRpc;
JsonRpc.VERSION = '2.0';
class RequestObject extends JsonRpc {
constructor(id, method, params) {
super();
this.id = id;
this.method = method;
if (params !== undefined) {
this.params = params;
}
}
}
exports.RequestObject = RequestObject;
class NotificationObject extends JsonRpc {
constructor(method, params) {
super();
this.method = method;
if (params !== undefined) {
this.params = params;
}
}
}
exports.NotificationObject = NotificationObject;
class SuccessObject extends JsonRpc {
constructor(id, result) {
super();
this.id = id;
this.result = result;
}
}
exports.SuccessObject = SuccessObject;
class ErrorObject extends JsonRpc {
// tslint:disable-next-line:no-shadowed-variable
constructor(id, error) {
super();
this.id = id;
this.error = error;
this.id = id;
this.error = error;
}
}
exports.ErrorObject = ErrorObject;
class JsonRpcParsed {
constructor(payload, type) {
this.payload = payload;
this.type = type;
this.payload = payload;
this.type = type;
}
}
exports.JsonRpcParsed = JsonRpcParsed;
/**
* JsonRpcError Class
*
* @param {String} message
* @param {Integer} code
* @return {String} name: optional
* @api public
*/
class JsonRpcError {
constructor(message, code, data) {
this.message = message;
this.code = isInteger(code) ? code : 0;
if (data != null) {
this.data = data;
}
}
}
exports.JsonRpcError = JsonRpcError;
JsonRpcError.invalidRequest = function (data) {
return new JsonRpcError('Invalid request', -32600, data);
};
JsonRpcError.methodNotFound = function (data) {
return new JsonRpcError('Method not found', -32601, data);
};
JsonRpcError.invalidParams = function (data) {
return new JsonRpcError('Invalid params', -32602, data);
};
JsonRpcError.internalError = function (data) {
return new JsonRpcError('Internal error', -32603, data);
};
JsonRpcError.parseError = function (data) {
return new JsonRpcError('Parse error', -32700, data);
};
/**
* Creates a JSON-RPC 2.0 request object
*
* @param {String|Integer} id
* @param {String} method
* @param {Object|Array} [params]: optional
* @return {Object} JsonRpc object
* @api public
*/
function request(id, method, params) {
const object = new RequestObject(id, method, params);
validateMessage(object, true);
return object;
}
exports.request = request;
/**
* Creates a JSON-RPC 2.0 notification object
*
* @param {String} method
* @param {Object|Array} [params]: optional
* @return {Object} JsonRpc object
* @api public
*/
function notification(method, params) {
const object = new NotificationObject(method, params);
validateMessage(object, true);
return object;
}
exports.notification = notification;
/**
* Creates a JSON-RPC 2.0 success response object
*
* @param {String|Integer} id
* @param {Mixed} result
* @return {Object} JsonRpc object
* @api public
*/
function success(id, result) {
const object = new SuccessObject(id, result);
validateMessage(object, true);
return object;
}
exports.success = success;
/**
* Creates a JSON-RPC 2.0 error response object
*
* @param {String|Integer} id
* @param {Object} JsonRpcError error
* @return {Object} JsonRpc object
* @api public
*/
function error(id, err) {
const object = new ErrorObject(id, err);
validateMessage(object, true);
return object;
}
exports.error = error;
function parse(message) {
if (!isString(message)) {
return new JsonRpcParsed(JsonRpcError.invalidRequest(message), "invalid" /* invalid */);
}
let jsonrpcObj;
try {
jsonrpcObj = JSON.parse(message);
}
catch (err) {
return new JsonRpcParsed(JsonRpcError.parseError(message), "invalid" /* invalid */);
}
return parseJsonRpcObject(jsonrpcObj);
}
exports.parse = parse;
/**
* Takes a JSON-RPC 2.0 payload (Object) or batch (Object[]) and tries to parse it.
* If successful, determine what objects are inside (response, notification,
* success, error, or invalid), and return their types and properly formatted objects.
*
* @param {Object|Array} jsonrpcObj
* @return {Object|Array} a single object or an array of `JsonRpcParsed` objects with `type` and `payload`:
*
* {
* type: <Enum, 'request'|'notification'|'success'|'error'|'invalid'>
* payload: <JsonRpc|JsonRpcError>
* }
*
* @api public
*/
function parseJsonRpcObject(jsonrpcObj) {
if (!Array.isArray(jsonrpcObj)) {
return parseObject(jsonrpcObj);
}
if (jsonrpcObj.length === 0) {
return new JsonRpcParsed(JsonRpcError.invalidRequest(jsonrpcObj), "invalid" /* invalid */);
}
const parsedObjectArray = [];
for (let i = 0, len = jsonrpcObj.length; i < len; i++) {
parsedObjectArray[i] = parseObject(jsonrpcObj[i]);
}
return parsedObjectArray;
}
exports.parseJsonRpcObject = parseJsonRpcObject;
/**
* Alias for `parse` method.
* Takes a JSON-RPC 2.0 payload (String) and tries to parse it into a JSON.
* @api public
*/
exports.parseJsonRpcString = parse;
/**
* Takes a JSON-RPC 2.0 payload (Object) and tries to parse it into a JSON.
* If successful, determine what object is it (response, notification,
* success, error, or invalid), and return it's type and properly formatted object.
*
* @param {Object} obj
* @return {Object} an `JsonRpcParsed` object with `type` and `payload`:
*
* {
* type: <Enum, 'request'|'notification'|'success'|'error'|'invalid'>
* payload: <JsonRpc|JsonRpcError>
* }
*
* @api public
*/
function parseObject(obj) {
let err = null;
let payload = null;
let payloadType = "invalid" /* invalid */;
if (obj == null || obj.jsonrpc !== JsonRpc.VERSION) {
err = JsonRpcError.invalidRequest(obj);
payloadType = "invalid" /* invalid */;
}
else if (!hasOwnProperty.call(obj, 'id')) {
const tmp = obj;
payload = new NotificationObject(tmp.method, tmp.params);
err = validateMessage(payload);
payloadType = "notification" /* notification */;
}
else if (hasOwnProperty.call(obj, 'method')) {
const tmp = obj;
payload = new RequestObject(tmp.id, tmp.method, tmp.params);
err = validateMessage(payload);
payloadType = "request" /* request */;
}
else if (hasOwnProperty.call(obj, 'result')) {
const tmp = obj;
payload = new SuccessObject(tmp.id, tmp.result);
err = validateMessage(payload);
payloadType = "success" /* success */;
}
else if (hasOwnProperty.call(obj, 'error')) {
const tmp = obj;
payloadType = "error" /* error */;
if (tmp.error == null) {
err = JsonRpcError.internalError(tmp);
}
else {
const errorObj = new JsonRpcError(tmp.error.message, tmp.error.code, tmp.error.data);
if (errorObj.message !== tmp.error.message || errorObj.code !== tmp.error.code) {
err = JsonRpcError.internalError(tmp);
}
else {
payload = new ErrorObject(tmp.id, errorObj);
err = validateMessage(payload);
}
}
}
if (err == null && payload != null) {
return new JsonRpcParsed(payload, payloadType);
}
return new JsonRpcParsed(err != null ? err : JsonRpcError.invalidRequest(obj), "invalid" /* invalid */);
}
exports.parseObject = parseObject;
// if error, return error, else return null
function validateMessage(obj, throwIt) {
let err = null;
if (obj instanceof RequestObject) {
err = checkId(obj.id);
if (err == null) {
err = checkMethod(obj.method);
}
if (err == null) {
err = checkParams(obj.params);
}
}
else if (obj instanceof NotificationObject) {
err = checkMethod(obj.method);
if (err == null) {
err = checkParams(obj.params);
}
}
else if (obj instanceof SuccessObject) {
err = checkId(obj.id);
if (err == null) {
err = checkResult(obj.result);
}
}
else if (obj instanceof ErrorObject) {
err = checkId(obj.id, true);
if (err == null) {
err = checkError(obj.error);
}
}
if (throwIt && err != null) {
throw err;
}
return err;
}
function checkId(id, maybeNull) {
if (maybeNull && id === null) {
return null;
}
return isString(id) || isInteger(id)
? null
: JsonRpcError.internalError('"id" must be provided, a string or an integer.');
}
function checkMethod(method) {
return isString(method) ? null : JsonRpcError.invalidRequest(method);
}
function checkResult(result) {
return result === undefined
? JsonRpcError.internalError('Result must exist for success Response objects')
: null;
}
function checkParams(params) {
if (params === undefined) {
return null;
}
if (Array.isArray(params) || isObject(params)) {
// ensure params can be stringify
try {
JSON.stringify(params);
return null;
}
catch (err) {
return JsonRpcError.parseError(params);
}
}
return JsonRpcError.invalidParams(params);
}
function checkError(err) {
if (!(err instanceof JsonRpcError)) {
return JsonRpcError.internalError('Error must be an instance of JsonRpcError');
}
if (!isInteger(err.code)) {
return JsonRpcError.internalError('Invalid error code. It must be an integer.');
}
if (!isString(err.message)) {
return JsonRpcError.internalError('Message must exist or must be a string.');
}
return null;
}
function isString(obj) {
return obj !== '' && typeof obj === 'string';
}
function isObject(obj) {
return obj != null && typeof obj === 'object' && !Array.isArray(obj);
}
const jsonrpc = {
JsonRpc,
JsonRpcError,
request,
notification,
success,
error,
parse,
parseObject,
parseJsonRpcObject,
parseJsonRpcString: exports.parseJsonRpcString,
};
exports.jsonrpc = jsonrpc;
exports.default = jsonrpc;
//# sourceMappingURL=jsonrpc.js.map