-
Notifications
You must be signed in to change notification settings - Fork 13
/
MoleServer.js
124 lines (100 loc) · 3.41 KB
/
MoleServer.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
const { INTERNAL_METHODS } = require('./constants');
const errorCodes = require('./errorCodes');
const INTERNAL_METHODS_NAMES = Object.values(INTERNAL_METHODS);
class MoleServer {
constructor({ transports }) {
if (!transports) throw new Error('TRANSPORT_REQUIRED');
this.transportsToRegister = transports;
this.methods = {
[INTERNAL_METHODS.PING]: this._handlePing
};
}
expose(methods) {
this.methods = {
...methods,
[INTERNAL_METHODS.PING]: this._handlePing
};
}
async registerTransport(transport) {
await transport.onData(this._processRequest.bind(this, transport));
}
async removeTransport(transport) {
await transport.shutdown(); // TODO
}
async _processRequest(transport, data) {
let requestData;
try {
requestData = JSON.parse(data);
} catch (error) {
// Handle cases when server receives broken JSON
return;
}
const isRequest = requestData.hasOwnProperty('method')
|| (Array.isArray(requestData)
&& requestData[0]
&& requestData[0].hasOwnProperty('method'));
if (!isRequest) return;
let responseData;
if (Array.isArray(requestData)) {
// TODO Batch error handling?
responseData = await Promise.all(
requestData.map(request => this._callMethod(request, transport))
);
} else {
responseData = await this._callMethod(requestData, transport);
}
return JSON.stringify(responseData);
}
async _callMethod(request, transport) {
const { method: methodName, params = [], id } = request;
if (!this._isMethodExposed(methodName)) {
return {
jsonrpc: '2.0',
id,
error: {
code: errorCodes.METHOD_NOT_FOUND,
message: 'Method not found'
}
};
}
this.currentTransport = transport;
try {
const result = await this.methods[methodName].apply(this.methods, params);
if (id !==0 && !id) return; // For notifications do not respond. "" means send nothing
return {
jsonrpc: '2.0',
id,
result: typeof result === 'undefined' ? null : result
};
} catch (error) {
return {
jsonrpc: '2.0',
id,
error: {
code: errorCodes.EXECUTION_ERROR,
message: 'Method has returned error',
data: (error instanceof Error ? error.message : error)
}
};
}
}
_isMethodExposed(methodName) {
return (
this.methods[methodName] &&
typeof this.methods[methodName] === 'function' &&
methodName !== 'constructor' &&
(!methodName.startsWith('_') || INTERNAL_METHODS_NAMES.includes(methodName)) &&
this.methods[methodName] !== Object.prototype[methodName]
);
}
_handlePing() {
return 'pong';
}
async run() {
for (const transport of this.transportsToRegister) {
await this.registerTransport(transport);
}
this.transportsToRegister = [];
}
}
module.exports = MoleServer;