-
Notifications
You must be signed in to change notification settings - Fork 13
/
plugin.js
234 lines (218 loc) · 6.79 KB
/
plugin.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
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
const EventEmitter = require('events');
const path = require('path');
const RpcMethod = require('./method.js');
const RpcWrapper = require('./rpc.js');
class Notification extends EventEmitter {};
class Plugin {
constructor (params) {
// name: { type: "", default: "", description: "" }
this.options = {};
// RpcMethods
this.methods = [];
// name: Notification()
this.notifications = {};
// name: callback
this.hooks = {};
this.rpc = undefined;
// Plugins are dynamic by default
this.dynamic = true;
if (typeof params != 'undefined') {
// Backward compat
// TODO: Make sure nobody relies on it anymore
if (typeof params === 'boolean') {
this.dynamic = params;
}
// New behaviour
if (params.hasOwnProperty('dynamic')) {
this.dynamic = params.dynamic;
}
}
}
// Beware with writing on stdout !
// https://nodejs.org/api/process.html#process_a_note_on_process_i_o
async _write (content) {
// We append \n\n, not that is still mandatory but it's way more
// readable to a human debugger !
content += '\n';
if (!process.stdout.write(content)) {
return new Promise((resolve, reject) => {
process.stdout.once('drain', resolve());
process.stdout.once('error', reject());
});
}
return Promise.resolve();
}
// The getmanifest call, all about us !
_getmanifest (params) {
let opts = [];
for (let name in this.options) {
opts.push({
name: name,
type: this.options[name].type,
default: this.options[name].default,
description: this.options[name].description
});
}
let notifs = [];
for (let name in this.notifications) {
notifs.push(name);
}
let hooks = [];
for (let name in this.hooks) {
hooks.push(name);
}
return {
options: opts,
rpcmethods: this.methods.map(function (method) {
return {
name: method.name,
usage: method.usage,
description: method.description,
long_description: method.longDescription
}
}),
subscriptions: notifs,
hooks: hooks,
dynamic: this.dynamic,
}
}
// We are almost done ! Lightningd sends this once it receives our manifest.
_init (params) {
const socketPath = path.join(params.configuration['lightning-dir'],
params.configuration['rpc-file']);
this.rpc = new RpcWrapper(socketPath);
for (let opt in params.options) {
this.options[opt].value = params.options[opt];
}
this.startup = params.configuration['startup'];
this.onInit(params);
// It's not interpreted by lightningd for now.
return {};
}
async _writeJsonrpcNotification (method, params) {
const payload = {
jsonrpc: '2.0',
method: method,
params: params,
}
const notif = JSON.stringify(payload);
await this._write(notif);
}
async _writeJsonrpcResponse (result, id, isError=false) {
const payload = {
jsonrpc: '2.0',
id: id
};
if (isError) payload.error = result;
else payload.result = result;
const response = JSON.stringify(payload);
await this._write(response);
}
// Add a fresh JSONRPC method accessible from lightningd
addMethod (name, callback, usage, description, longDescription) {
if (!name || !callback) {
throw new Error('You need to pass at least a name and a callback to register a method');
}
const method = new RpcMethod(name, usage, description, longDescription);
method.main = callback;
this.methods.push(method);
}
// Add a startup option to lightningd
addOption (name, defaultValue, description, type) {
if (!name || defaultValue === null || !description) {
throw new Error('You need to pass at least a name, default value and description for the option');
}
this.options[name] = {
default: defaultValue,
description: description,
type: type || 'string',
value: defaultValue
};
}
// A hook is a notification which needs a response from our (plugin) side
addHook (name, callback) {
this.hooks[name] = callback;
}
// Notifications are emitted as events
subscribe (name) {
this.notifications[name] = new Notification();
}
// To be overriden to do something special at startup
onInit (params) {
}
// Send logs to lightningd's log
log (message, level) {
level = level || 'info';
if (!message || typeof message !== 'string') {
throw new Error('You need to specify a string to write on lightningd\'s logs.');
}
message.split('\n').forEach((line) => {
if (line) {
this._writeJsonrpcNotification('log', {level: level, message: line});
}
});
}
// Read from stdin and do what master (not Satoshi, lightningd!!) tells until
// we die
async _mainLoop () {
let chunk;
let msg;
while (chunk = process.stdin.read()) {
// Ok so process.stdin.read() can actually return a chunk with multiple
// lines.
// FIXME: don't rely on lightningd's \n\n !!
const lines = chunk.split('\n\n');
for (const i in lines) {
if (!lines[i]) continue;
try {
msg = JSON.parse(lines[i]);
} catch (e) {
this.log(e.message, 'error');
throw e;
}
// JSONRPC2 sanity checks
if (!msg || !msg.method || msg.jsonrpc !== '2.0') {
this.log('Got bad JSONRPC2', 'error');
throw new Error('Bad JSONRPC(2)!');
}
if (!msg.id && msg.method in this.notifications) {
this.notifications[msg.method].emit(msg.method, msg.params);
}
if (msg.method === 'getmanifest') {
await this._writeJsonrpcResponse(this._getmanifest(msg.params),
msg.id);
continue;
}
if (msg.method === 'init') {
await this._writeJsonrpcResponse(this._init(msg.params),
msg.id);
continue;
}
if (msg.method in this.hooks) {
Promise.resolve(this.hooks[msg.method](msg.params)).then(async (response) => {
await this._writeJsonrpcResponse(response, msg.id);
});
continue;
}
this.methods.forEach(async (m) => {
if (m.name === msg.method) {
try {
const response = await m.main(msg.params)
await this._writeJsonrpcResponse(response, msg.id);
} catch (error) {
await this._writeJsonrpcResponse({code: -32603, message: error.message}, msg.id, true);
}
}
});
}
}
}
// Start plugining !
start () {
process.stdin.setEncoding('utf8');
process.stdin.on('readable', () => {
this._mainLoop();
});
}
}
module.exports = Plugin;