-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrpc.js
127 lines (113 loc) · 3.31 KB
/
rpc.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
import net from 'net';
import FileLogger from 'cln-file-logger';
class LightningRpc {
constructor(socketPath, logger = new FileLogger('[RPC]')) {
if (!socketPath) {
throw new Error('The RPC wrapper needs a socket path.');
}
this.socketPath = socketPath;
this.rpc = net.createConnection({ path: this.socketPath });
this.id = 0;
this.allowedErrors = 10;
this.logger = logger;
this.rpc.on('timeout', async () => {
this.logger.logWarn('timeout')
this.rpc.destroy();
try {
await this.restoreSocket();
} catch (e) {
process.exit();
}
});
this.rpc.on('error', async (e) => {
this.logger.logError(e)
if (this.allowedErrors > 0) {
try {
await this.restoreSocket();
} catch (e) {
process.exit();
}
} else {
throw e;
}
});
this.rpc.on('close', async (hadError) => {
this.logger.logError(`close with error: ${hadError}`)
if (hadError === true && this.allowedErrors <= 0) {
throw new Error('An unexpected failure caused the socket ' + this.socketPath + ' to close.');
} else {
this.rpc.destroy();
try {
await this.restoreSocket();
} catch (e) {
process.exit();
}
}
});
this.rpc.on('error', async (e) => {
this.logger.logError(e)
this.rpc.destroy();
try {
await this.restoreSocket();
} catch (e) {
process.exit();
}
});
setInterval(() => this.allowedErrors++, 1000 * 60 * 30);
this.buffer = '';
}
async readResponse(chunk, resolve, reject) {
this.buffer += chunk;
try {
const res = JSON.parse(this.buffer);
this.logger.logInfo('received response');
this.logger.logInfo(this.buffer);
this.buffer = '';
resolve(res);
} catch (e) {
this.rpc.once('data', (chunk) => {
this.readResponse(chunk, resolve, reject);
});
}
}
async _jsonRpcRequest(data) {
return new Promise((resolve, reject) => {
this.rpc.write(data);
this.rpc.once('data', (chunk) => {
this.readResponse(chunk, resolve, reject);
});
});
}
async call(_method, _params) {
this.logger.logInfo(`calling method: ${_method} with params: ${JSON.stringify(_params)}`);
_params = _params || {};
const request = {
jsonrpc: '2.0',
id: this.id,
method: _method,
params: _params,
};
this.logger.logInfo('sending request')
this.logger.logInfo(request);
const response = await this._jsonRpcRequest(JSON.stringify(request));
if (response.hasOwnProperty('error')) {
this.logger.logError('error response')
this.logger.logError(response.error);
} else if (!response.hasOwnProperty('result')) {
this.logger.logError(`<NO RESULT> error response: ${JSON.stringify(response)}`);
}
return response.result;
}
async restoreSocket() {
this.logger.logInfo('restoring socket');
return new Promise((resolve, reject) => {
this.rpc.destroy();
this.allowedErrors--;
this.rpc = net.createConnection({ path: this.socketPath });
this.rpc.on('connect', () => resolve());
this.rpc.on('error', () => reject());
this.rpc.on('timeout', () => reject());
});
}
}
export default LightningRpc;