forked from eclipse-thingweb/node-wot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli-default-servient.ts
More file actions
412 lines (377 loc) · 15.7 KB
/
cli-default-servient.ts
File metadata and controls
412 lines (377 loc) · 15.7 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* eslint-disable @typescript-eslint/no-explicit-any */
/********************************************************************************
* Copyright (c) 2018 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the W3C Software Notice and
* Document License (2015-05-13) which is available at
* https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document.
*
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
********************************************************************************/
/* eslint no-console: "off" */
// global W3C WoT Scripting API definitions
import * as WoT from "wot-typescript-definitions";
// node-wot implementation of W3C WoT Servient
import { Servient, Helpers, createLoggers } from "@node-wot/core";
// protocols used
import { HttpServer, HttpClientFactory, HttpsClientFactory } from "@node-wot/binding-http";
// import { WebSocketServer } from "@node-wot/binding-websockets";
import { CoapServer, CoapClientFactory, CoapsClientFactory } from "@node-wot/binding-coap";
import { MqttBrokerServer, MqttClientFactory } from "@node-wot/binding-mqtt";
import { FileClientFactory } from "@node-wot/binding-file";
import { CompilerFunction, NodeVM } from "vm2";
import { ThingModelHelpers } from "@thingweb/thing-model";
const { debug, error, info } = createLoggers("cli", "cli-default-servient");
// Helper function needed for `mergeConfigs` function
function isObject(item: unknown) {
return item != null && typeof item === "object" && !Array.isArray(item);
}
/**
* Helper function merging default parameters into a custom config file.
*
* @param {object} target - an object containing default config parameters
* @param {object} source - an object containing custom config parameters
*
* @return {object} The new config file containing both custom and default parameters
*/
function mergeConfigs(target: any, source: any): any {
const output = Object.assign({}, target);
Object.keys(source).forEach((key) => {
if (!(key in target)) {
Object.assign(output, { [key]: source[key] });
} else {
if (isObject(target[key]) && isObject(source[key])) {
output[key] = mergeConfigs(target[key], source[key]);
} else {
Object.assign(output, { [key]: source[key] });
}
}
});
return output;
}
export interface ScriptOptions {
argv?: Array<string>;
compiler?: CompilerFunction;
env?: Record<string, string>;
}
export default class DefaultServient extends Servient {
private static readonly defaultConfig = {
servient: {
clientOnly: false,
scriptAction: false,
},
http: {
port: 8080,
allowSelfSigned: false,
},
coap: {
port: 5683,
},
log: {
level: "info",
},
};
private uncaughtListeners: Array<NodeJS.UncaughtExceptionListener> = [];
private runtime: typeof WoT | undefined;
public readonly config: any;
// current log level
public logLevel = "info";
public constructor(clientOnly: boolean, config?: any) {
super();
// init config
this.config =
typeof config === "object"
? mergeConfigs(DefaultServient.defaultConfig, config)
: DefaultServient.defaultConfig;
// apply flags
if (clientOnly) {
this.config.servient ??= {};
this.config.servient.clientOnly = true;
}
// set log level before any output
this.setLogLevel(this.config.log.level);
// load credentials from config
this.addCredentials(this.config.credentials);
// remove secrets from original for displaying config (already added)
if (this.config.credentials != null) {
delete this.config.credentials;
}
// display
debug("DefaultServient configured with");
debug(`${this.config}`);
// apply config
if (typeof this.config.servient.staticAddress === "string") {
Helpers.setStaticAddress(this.config.servient.staticAddress);
}
let coapServer: CoapServer | undefined;
if (this.config.servient.clientOnly === false) {
if (this.config.http != null) {
const httpServer = new HttpServer(this.config.http);
this.addServer(httpServer);
// re-use httpServer (same port)
// this.addServer(new WebSocketServer(httpServer));
}
const coapConfig = this.config.coap;
if (coapConfig != null) {
coapServer = new CoapServer(coapConfig);
this.addServer(coapServer);
}
if (this.config.mqtt != null) {
const mqttBrokerServer = new MqttBrokerServer({
uri: this.config.mqtt.broker,
user: typeof this.config.mqtt.username === "string" ? this.config.mqtt.username : undefined,
psw: typeof this.config.mqtt.password === "string" ? this.config.mqtt.password : undefined,
clientId: typeof this.config.mqtt.clientId === "string" ? this.config.mqtt.clientId : undefined,
protocolVersion:
typeof this.config.mqtt.protocolVersion === "number"
? this.config.mqtt.protocolVersion
: undefined,
});
this.addServer(mqttBrokerServer);
}
}
this.addClientFactory(new FileClientFactory());
this.addClientFactory(new HttpClientFactory(this.config.http));
this.addClientFactory(new HttpsClientFactory(this.config.http));
this.addClientFactory(new CoapClientFactory(coapServer));
this.addClientFactory(new CoapsClientFactory());
this.addClientFactory(new MqttClientFactory());
}
/**
* Runs the script in a new sandbox
* @param {string} code - the script to run
* @param {string} filename - the filename of the script
*/
public runScript(code: string, filename = "script"): unknown {
if (!this.runtime) {
throw new Error("WoT runtime not loaded; have you called start()?");
}
const helpers = new Helpers(this);
const context = {
WoT: this.runtime,
WoTHelpers: helpers,
ModelHelpers: new ThingModelHelpers(helpers),
};
const vm = new NodeVM({
sandbox: context,
});
const listener = (err: Error) => {
this.logScriptError(`Asynchronous script error '${filename}'`, err);
// TODO: clean up script resources
// eslint-disable-next-line n/no-process-exit
process.exit(1);
};
process.prependListener("uncaughtException", listener);
this.uncaughtListeners.push(listener);
try {
return vm.run(code, filename);
} catch (err) {
if (err instanceof Error) {
this.logScriptError(`Servient found error in script '${filename}'`, err);
} else {
error(`Servient found error in script '${filename}' ${err}`);
}
return undefined;
}
}
/**
* Runs the script in privileged context (dangerous). In practice, this means that the script can
* require system modules.
* @param {string} code - the script to run
* @param {string} filename - the filename of the script
* @param {object} options - pass cli variables or envs to the script
*/
public runPrivilegedScript(code: string, filename = "script", options: ScriptOptions = {}): unknown {
if (!this.runtime) {
throw new Error("WoT runtime not loaded; have you called start()?");
}
const helpers = new Helpers(this);
const context = {
WoT: this.runtime,
WoTHelpers: helpers,
ModelHelpers: new ThingModelHelpers(helpers),
};
const vm = new NodeVM({
sandbox: context,
require: {
external: true,
builtin: ["*"],
},
argv: options.argv,
compiler: options.compiler,
env: options.env,
});
const listener = (err: Error) => {
this.logScriptError(`Asynchronous script error '${filename}'`, err);
// TODO: clean up script resources
// eslint-disable-next-line n/no-process-exit
process.exit(1);
};
process.prependListener("uncaughtException", listener);
this.uncaughtListeners.push(listener);
try {
return vm.run(code, filename);
} catch (err) {
if (err instanceof Error) {
this.logScriptError(`Servient found error in privileged script '${filename}'`, err);
} else {
error(`Servient found error in privileged script '${filename}' ${err}`);
}
return undefined;
}
}
private logScriptError(description: string, err: Error): void {
let message: string;
if (typeof err === "object" && err.stack != null) {
const match = err.stack.match(/evalmachine\.<anonymous>:([0-9]+:[0-9]+)/);
if (Array.isArray(match)) {
message = `and halted at line ${match[1]}\n ${err}`;
} else {
message = `and halted with ${err.stack}`;
}
} else {
message = `that threw ${typeof err} instead of Error\n ${err}`;
}
error(`Servient caught ${description} ${message}`);
}
/**
* start
*/
public start(): Promise<typeof WoT> {
return new Promise<typeof WoT>((resolve, reject) => {
super
.start()
.then((myWoT) => {
info("DefaultServient started");
this.runtime = myWoT;
// TODO think about builder pattern that starts with produce() ends with expose(), which exposes/publishes the Thing
myWoT
.produce({
title: "servient",
description: "node-wot CLI Servient",
properties: {
things: {
type: "object",
description: "Get things",
observable: false,
readOnly: true,
},
},
actions: {
setLogLevel: {
description: "Set log level",
input: { oneOf: [{ type: "string" }, { type: "number" }] },
output: { type: "string" },
},
shutdown: {
description: "Stop servient",
output: { type: "string" },
},
runScript: {
description: "Run script",
input: { type: "string" },
output: { type: "string" },
},
},
})
.then((thing) => {
thing.setActionHandler("setLogLevel", async (level) => {
const ll = await Helpers.parseInteractionOutput(level);
if (typeof ll === "number") {
this.setLogLevel(ll as number);
} else if (typeof ll === "string") {
this.setLogLevel(ll as string);
} else {
// try to convert it to strings
this.setLogLevel(ll + "");
}
return `Log level set to '${this.logLevel}'`;
});
thing.setActionHandler("shutdown", async () => {
debug("shutting down by remote");
await this.shutdown();
return undefined;
});
thing.setActionHandler("runScript", async (script) => {
const scriptv = await Helpers.parseInteractionOutput(script);
debug("running script", scriptv);
this.runScript(scriptv as string);
return undefined;
});
thing.setPropertyReadHandler("things", async () => {
debug("returning things");
return this.getThings();
});
thing
.expose()
.then(() => {
// pass on WoTFactory
resolve(myWoT);
})
.catch((err) => reject(err));
});
})
.catch((err) => reject(err));
});
}
public async shutdown(): Promise<void> {
await super.shutdown();
this.uncaughtListeners.forEach((listener) => {
process.removeListener("uncaughtException", listener);
});
}
// Save default loggers (needed when changing log levels)
private readonly loggers: any = {
warn: console.warn,
info: console.info,
debug: console.debug,
};
private setLogLevel(logLevel: string | number): void {
if (logLevel === "error" || logLevel === 0) {
console.warn = () => {
/* nothing */
};
console.info = () => {
/* nothing */
};
console.debug = () => {
/* nothing */
};
this.logLevel = "error";
} else if (logLevel === "warn" || logLevel === "warning" || logLevel === 1) {
console.warn = this.loggers.warn;
console.info = () => {
/* nothing */
};
console.debug = () => {
/* nothing */
};
this.logLevel = "warn";
} else if (logLevel === "info" || logLevel === 2) {
console.warn = this.loggers.warn;
console.info = this.loggers.info;
console.debug = () => {
/* nothing */
};
this.logLevel = "info";
} else if (logLevel === "debug" || logLevel === 3) {
console.warn = this.loggers.warn;
console.info = this.loggers.info;
console.debug = this.loggers.debug;
this.logLevel = "debug";
} else {
// Fallback to default ("info")
console.warn = this.loggers.warn;
console.info = this.loggers.info;
console.debug = () => {
/* nothing */
};
this.logLevel = "info";
}
}
}