-
Notifications
You must be signed in to change notification settings - Fork 13
/
jsonrpc.ts
498 lines (445 loc) · 12.6 KB
/
jsonrpc.ts
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// **Github:** https://github.com/teambition/jsonrpc-lite
//
// http://www.jsonrpc.org/specification
// **License:** MIT
'use strict'
export type ID = string | number | null
export type Defined = string | number | boolean | object | null
export type RpcParams = object | Defined[]
const hasOwnProperty = Object.prototype.hasOwnProperty
const isInteger: (num: number) => boolean = typeof Number.isSafeInteger === 'function'
? Number.isSafeInteger // ECMAScript 2015
: function (num) {
return typeof num === 'number' && isFinite(num) && num === Math.floor(num) && Math.abs(num) <= 9007199254740991
}
/**
* JsonRpc Class
*
* @return {Object} JsonRpc object
* @api public
*/
export interface IJsonRpcType {
readonly jsonrpc: string
}
export class JsonRpc implements IJsonRpcType {
static VERSION: string = '2.0'
readonly jsonrpc: string
constructor () {
this.jsonrpc = '2.0'
}
serialize () {
return JSON.stringify(this)
}
}
export class RequestObject extends JsonRpc {
public id: ID
public method: string
public params?: RpcParams
constructor (id: ID, method: string, params?: RpcParams) {
super()
this.id = id
this.method = method
if (params !== undefined ) {
this.params = params
}
}
}
export class NotificationObject extends JsonRpc {
public method: string
public params?: RpcParams
constructor (method: string, params?: RpcParams) {
super()
this.method = method
if (params !== undefined ) {
this.params = params
}
}
}
export class SuccessObject extends JsonRpc {
public id: ID
public result: Defined
constructor (id: ID, result: Defined) {
super()
this.id = id
this.result = result
}
}
export class ErrorObject extends JsonRpc {
// tslint:disable-next-line:no-shadowed-variable
constructor (public id: ID, public error: JsonRpcError) {
super()
this.id = id
this.error = error
}
}
/**
* JsonRpcParsed Class
*
* @param {JsonRpc|JsonRpcError} payload
* @param {type: <Enum, 'request'|'notification'|'success'|'error'|'invalid'>} type
* @api public
*/
export const enum RpcStatusType {
request = 'request',
notification = 'notification',
success = 'success',
error = 'error',
invalid = 'invalid',
}
export class JsonRpcParsed {
constructor (
public payload: JsonRpc | JsonRpcError,
public type: RpcStatusType,
) {
this.payload = payload
this.type = type
}
}
/**
* JsonRpcError Class
*
* @param {String} message
* @param {Integer} code
* @return {String} name: optional
* @api public
*/
export class JsonRpcError {
static invalidRequest = function (data: any): JsonRpcError {
return new JsonRpcError('Invalid request', -32600, data)
}
static methodNotFound = function (data: any): JsonRpcError {
return new JsonRpcError('Method not found', -32601, data)
}
static invalidParams = function (data: any): JsonRpcError {
return new JsonRpcError('Invalid params', -32602, data)
}
static internalError = function (data: any): JsonRpcError {
return new JsonRpcError('Internal error', -32603, data)
}
static parseError = function (data: any): JsonRpcError {
return new JsonRpcError('Parse error', -32700, data)
}
public message: string
public code: number
public data?: any
constructor (message: string, code: number, data?: any) {
this.message = message
this.code = isInteger(code) ? code : 0
if (data != null ) {
this.data = 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
*/
export function request (
id: ID,
method: string,
params?: RpcParams,
): RequestObject {
const object = new RequestObject(id, method, params)
validateMessage(object, true)
return object
}
/**
* Creates a JSON-RPC 2.0 notification object
*
* @param {String} method
* @param {Object|Array} [params]: optional
* @return {Object} JsonRpc object
* @api public
*/
export function notification (
method: string,
params?: RpcParams,
): NotificationObject {
const object = new NotificationObject(method, params)
validateMessage(object, true)
return object
}
/**
* Creates a JSON-RPC 2.0 success response object
*
* @param {String|Integer} id
* @param {Mixed} result
* @return {Object} JsonRpc object
* @api public
*/
export function success (id: ID, result: Defined): SuccessObject {
const object = new SuccessObject(id, result)
validateMessage(object, true)
return object
}
/**
* Creates a JSON-RPC 2.0 error response object
*
* @param {String|Integer} id
* @param {Object} JsonRpcError error
* @return {Object} JsonRpc object
* @api public
*/
export function error (id: ID, err: JsonRpcError): ErrorObject {
const object = new ErrorObject(id, err)
validateMessage(object, true)
return object
}
export interface IParsedObjectSuccess {
type: RpcStatusType.success,
payload: SuccessObject
}
export interface IParsedObjectNotification {
type: RpcStatusType.notification,
payload: NotificationObject
}
export interface IParsedObjectRequest {
type: RpcStatusType.request,
payload: RequestObject
}
export interface IParsedObjectError {
type: RpcStatusType.error,
payload: ErrorObject
}
export interface IParsedObjectInvalid {
type: RpcStatusType.invalid,
payload: JsonRpcError
}
/**
* Takes a JSON-RPC 2.0 payload (String) 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 {String} msg
* @return {Object|Array} an array, or an object of this format:
*
* {
* type: <Enum, 'request'|'notification'|'success'|'error'|'invalid'>
* payload: <JsonRpc|JsonRpcError>
* }
*
* @api public
*/
export type IParsedObject = IParsedObjectSuccess | IParsedObjectNotification |
IParsedObjectRequest | IParsedObjectError| IParsedObjectInvalid;
export function parse (
message: string,
): IParsedObject | IParsedObject[] {
if (!isString(message)) {
return new JsonRpcParsed(
JsonRpcError.invalidRequest(message),
RpcStatusType.invalid,
) as IParsedObject
}
let jsonrpcObj: JsonRpc | JsonRpc[]
try {
jsonrpcObj = JSON.parse(message)
} catch (err) {
return new JsonRpcParsed(
JsonRpcError.parseError(message),
RpcStatusType.invalid,
) as IParsedObject
}
return parseJsonRpcObject(jsonrpcObj)
}
/**
* 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
*/
export function parseJsonRpcObject (jsonrpcObj: JsonRpc | JsonRpc[]): IParsedObject | IParsedObject[] {
if (!Array.isArray(jsonrpcObj)) {
return parseObject(jsonrpcObj)
}
if (jsonrpcObj.length === 0) {
return new JsonRpcParsed(
JsonRpcError.invalidRequest(jsonrpcObj),
RpcStatusType.invalid,
) as IParsedObject
}
const parsedObjectArray: IParsedObject[] = []
for (let i = 0, len = jsonrpcObj.length; i < len; i++) {
parsedObjectArray[i] = parseObject(jsonrpcObj[i])
}
return parsedObjectArray
}
/**
* Alias for `parse` method.
* Takes a JSON-RPC 2.0 payload (String) and tries to parse it into a JSON.
* @api public
*/
export const 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
*/
export function parseObject (obj: any): IParsedObject {
let err: JsonRpcError | null = null
let payload: JsonRpc | JsonRpcError | null = null
let payloadType: RpcStatusType = RpcStatusType.invalid
if (obj == null || obj.jsonrpc !== JsonRpc.VERSION) {
err = JsonRpcError.invalidRequest(obj)
payloadType = RpcStatusType.invalid
} else if (!hasOwnProperty.call(obj, 'id')) {
const tmp = obj as NotificationObject
payload = new NotificationObject(tmp.method, tmp.params)
err = validateMessage(payload)
payloadType = RpcStatusType.notification
} else if (hasOwnProperty.call(obj, 'method')) {
const tmp = obj as RequestObject
payload = new RequestObject(tmp.id, tmp.method, tmp.params)
err = validateMessage(payload)
payloadType = RpcStatusType.request
} else if (hasOwnProperty.call(obj, 'result')) {
const tmp = obj as SuccessObject
payload = new SuccessObject(tmp.id, tmp.result)
err = validateMessage(payload)
payloadType = RpcStatusType.success
} else if (hasOwnProperty.call(obj, 'error')) {
const tmp = obj as ErrorObject
payloadType = RpcStatusType.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) as IParsedObject
}
return new JsonRpcParsed(
err != null ? err : JsonRpcError.invalidRequest(obj),
RpcStatusType.invalid,
) as IParsedObject
}
// if error, return error, else return null
function validateMessage (obj: JsonRpc, throwIt?: boolean): JsonRpcError | null {
let err: JsonRpcError | null = 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 as JsonRpcError)
}
}
if (throwIt && err != null) {
throw err
}
return err
}
function checkId (id: ID, maybeNull?: boolean): JsonRpcError | null {
if (maybeNull && id === null) {
return null
}
return isString(id) || isInteger(id as number)
? null
: JsonRpcError.internalError('"id" must be provided, a string or an integer.')
}
function checkMethod (method: string): JsonRpcError | null {
return isString(method) ? null : JsonRpcError.invalidRequest(method)
}
function checkResult (result: Defined): JsonRpcError | null {
return result === undefined
? JsonRpcError.internalError('Result must exist for success Response objects')
: null
}
function checkParams (params?: RpcParams): JsonRpcError | null {
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: JsonRpcError): JsonRpcError | null {
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: any): boolean {
return obj !== '' && typeof obj === 'string'
}
function isObject (obj: any): boolean {
return obj != null && typeof obj === 'object' && !Array.isArray(obj)
}
const jsonrpc = {
JsonRpc,
JsonRpcError,
request,
notification,
success,
error,
parse,
parseObject,
parseJsonRpcObject,
parseJsonRpcString,
}
export default jsonrpc
export { jsonrpc }