-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathindex.js
More file actions
179 lines (140 loc) · 5.04 KB
/
index.js
File metadata and controls
179 lines (140 loc) · 5.04 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
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
'use strict';
const path = require('path');
const net = require('net');
const debug = require('debug')('lightning-client');
const {EventEmitter} = require('events');
const _ = require('lodash');
const methods = require('./methods');
class LightningClient extends EventEmitter {
constructor(rpcPath, debugFlag = false) {
if (!path.isAbsolute(rpcPath)) {
throw new Error('The rpcPath must be an absolute path');
}
rpcPath = path.join(rpcPath, '/lightning-rpc');
debug(`Connecting to ${rpcPath}`);
super();
this.rpcPath = rpcPath;
this.reconnectWait = 0.5;
this.reconnectTimeout = null;
this.reqcount = 0;
this.debug = debugFlag;
const _self = this;
this.client = net.createConnection(rpcPath);
this.clientConnectionPromise = new Promise(resolve => {
_self.client.on('connect', () => {
debug(`Lightning client connected`);
_self.reconnectWait = 1;
resolve();
});
_self.client.on('end', () => {
console.error('Lightning client connection closed, reconnecting');
_self.increaseWaitTime();
_self.reconnect();
});
_self.client.on('error', error => {
console.error(`Lightning client connection error`, error);
_self.increaseWaitTime();
_self.reconnect();
});
});
let buffer = Buffer.from('');
let openCount = 0;
this.client.on('data', data => {
_.each(LightningClient.splitJSON(Buffer.concat([buffer, data]), buffer.length, openCount), partObj => {
if (partObj.partial) {
buffer = partObj.string;
openCount = partObj.openCount;
return;
}
buffer = Buffer.from('');
openCount = 0;
try {
let dataObject = JSON.parse(partObj.string.toString());
_self.emit('res:' + dataObject.id, dataObject);
} catch (err) {
return;
}
});
});
}
static splitJSON(str, startFrom = 0, openCount = 0) {
const parts = [];
let lastSplit = 0;
for (let i = startFrom; i < str.length; i++) {
if (i > 0 && str[i - 1] === 115) { // 115 => backslash, ignore this character
continue;
}
if (str[i] === 123) { // '{'
openCount++;
} else if (str[i] === 125) { // '}'
openCount--;
if (openCount === 0) {
const start = lastSplit;
const end = (i + 1 === str.length) ? undefined : i + 1;
parts.push({partial: false, string: str.slice(start, end), openCount: 0});
lastSplit = end;
}
}
}
if (lastSplit !== undefined) {
parts.push({partial: true, string: str.slice(lastSplit), openCount});
}
return parts;
}
increaseWaitTime() {
if (this.reconnectWait >= 16) {
this.reconnectWait = 16;
} else {
this.reconnectWait *= 2;
}
}
reconnect() {
const _self = this;
if (this.reconnectTimeout) {
return;
}
this.reconnectTimeout = setTimeout(() => {
debug('Trying to reconnect...');
_self.client.connect(_self.rpcPath);
_self.reconnectTimeout = null;
}, this.reconnectWait * 1000);
}
call(method, args = []) {
if (!_.isString(method) || !_.isArray(args)) {
return Promise.reject(new Error('invalid_call'));
}
let stackTrace = null;
if (this.debug === true) { // not really efficient, we skip this step if debug is not enabled
const error = new Error();
stackTrace = error.stack;
}
const _self = this;
const callInt = ++this.reqcount;
const sendObj = {
method,
params: args,
id: callInt.toString()
};
// Wait for the client to connect
return this.clientConnectionPromise
.then(() => new Promise((resolve, reject) => {
// Wait for a response
this.once('res:' + callInt, response => {
if (_.isNil(response.error)) {
resolve(response.result);
return;
}
reject({error: response.error, stack: stackTrace});
});
// Send the command
_self.client.write(JSON.stringify(sendObj));
}));
}
}
const protify = s => s.replace(/-([a-z])/g, m => m[1].toUpperCase());
methods.forEach(k => {
LightningClient.prototype[protify(k)] = function (...args) {
return this.call(k, args);
};
});
module.exports = LightningClient;