Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
121 lines (94 loc) · 2.67 KB
/
Copy pathindex.js
File metadata and controls
121 lines (94 loc) · 2.67 KB
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
function JsonRpcClient(methods, send)
{
const responses = {}
let requestId = 0
function notification(method, ...params)
{
if(params.length === 1)
{
params = params[0]
// Check if params where send as an `Object` or an `Array`
if(params !== undefined && !(params instanceof Object))
{
const error = new Error('params is not an Object or an Array')
error.params = params
throw error
}
}
return {jsonrpc: '2.0', method, params}
}
function request(method, params, callback)
{
params = Array.from(arguments).slice(1)
callback = params[params.length-1] instanceof Function
? params.pop()
: undefined
let promise = new Promise(resolver)
if(callback) promise = promise.then(callback.bind(null, null), callback)
return Object.assign(promise, {id: requestId++},
notification(method, ...params))
}
function reply(id, error, result)
{
if(id !== undefined) return send({error, id, jsonrpc: '2.0', result})
// Log error responses for notifications
if(error) console.warn('Error response for notification:', error)
}
function resolver(resolve, reject)
{
responses[requestId] = {reject, resolve}
}
return {
notification,
request,
async onMessage({error, id, jsonrpc, method, params, result})
{
if(jsonrpc !== '2.0')
return reply(id, {
code: -32600,
data: jsonrpc,
message: `Invalid JsonRPC version '${jsonrpc}'`
})
// Request
if(method)
{
const func = methods[method]
if(!func)
return reply(id, {
code: -32601,
data: method,
message: `Unknown method '${method}'`
})
// Check if params where send as an `Object`
if(!Array.isArray(params)) params = [params]
try {
result = await func.apply(this, params)
}
catch(data)
{
error = data.code != null ? data
: {code: -32500, data, message: data.message || data}
}
return reply(id, error, result)
}
// Response
const response = responses[id]
if(!response)
return console.warn(`Received response for missing request '${id}':`,
error, result)
const {reject, resolve} = response
delete responses[id]
if(error) return reject(error)
resolve(result)
}
}
}
JsonRpcClient.InvalidJSON = function(data)
{
return {
error: {code: -32700, data, message: 'Invalid JSON'},
id: null, // Spec says `id` must be set to `null` if it can't be parsed
jsonrpc: '2.0'
}
}
module.exports = JsonRpcClient