@@ -24,22 +24,13 @@ import { HttpServer, HttpClientFactory, HttpsClientFactory } from "@node-wot/bin
2424import { CoapServer , CoapClientFactory , CoapsClientFactory } from "@node-wot/binding-coap" ;
2525import { MqttBrokerServer , MqttClientFactory } from "@node-wot/binding-mqtt" ;
2626import { FileClientFactory } from "@node-wot/binding-file" ;
27- import { ThingModelHelpers } from "@thingweb/thing-model" ;
28- import { createContext , Script } from "vm" ;
29- import { CompilerFunction } from "./compiler-function" ;
30- import { LogLevel , setLogLevel } from "./utils/set-log-level" ;
27+ import { LogLevel , setLogLevel } from "./utils" ;
3128import { ConfigurationAfterDefaults } from "./configuration" ;
3229
33- const { debug, error , info } = createLoggers ( "cli" , "cli-default-servient" ) ;
30+ const { debug, info } = createLoggers ( "cli" , "cli-default-servient" ) ;
3431
35- export interface ScriptOptions {
36- argv ?: Array < string > ;
37- compiler ?: CompilerFunction ;
38- env ?: Record < string , string > ;
39- }
4032export default class DefaultServient extends Servient {
4133 private uncaughtListeners : Array < NodeJS . UncaughtExceptionListener > = [ ] ;
42- private runtime : typeof WoT | undefined ;
4334 public readonly config : ConfigurationAfterDefaults ;
4435 // current log level
4536 public logLevel = "info" ;
@@ -99,78 +90,11 @@ export default class DefaultServient extends Servient {
9990 this . addClientFactory ( new MqttClientFactory ( ) ) ;
10091 }
10192
102- /**
103- * Runs the script in privileged context (dangerous). In practice, this means that the script can
104- * require system modules.
105- * @param {string } code - the script to run
106- * @param {string } filename - the filename of the script
107- * @param {object } options - pass cli variables or envs to the script
108- */
109- public runScript ( code : string , filename = "script" , options : ScriptOptions = { } ) : unknown {
110- if ( ! this . runtime ) {
111- throw new Error ( "WoT runtime not loaded; have you called start()?" ) ;
112- }
113- const helpers = new Helpers ( this ) ;
114-
115- options . compiler ??= ( code ) => code ;
116-
117- const compiledCode = options . compiler ( code , filename ) ;
118- const script = new Script ( compiledCode , filename ) ;
119- process . argv = options . argv ?? [ ] ;
120- process . env = options . env ?? process . env ;
121- const context = createContext ( {
122- ...globalThis ,
123- process,
124- require,
125- console,
126- exports : { } ,
127- WoT : this . runtime ,
128- WoTHelpers : helpers ,
129- ModelHelpers : new ThingModelHelpers ( helpers ) ,
130- } ) ;
131-
132- const listener = ( err : Error ) => {
133- this . logScriptError ( `Asynchronous script error '${ filename } '` , err ) ;
134- // TODO: clean up script resources
135- process . exit ( 1 ) ;
136- } ;
137-
138- process . prependListener ( "uncaughtException" , listener ) ;
139- this . uncaughtListeners . push ( listener ) ;
140-
141- try {
142- return script . runInContext ( context , { displayErrors : true } ) ;
143- } catch ( err ) {
144- if ( err instanceof Error ) {
145- this . logScriptError ( `Servient found error in privileged script '${ filename } '` , err ) ;
146- } else {
147- error ( `Servient found error in privileged script '${ filename } ' ${ err } ` ) ;
148- }
149- return undefined ;
150- }
151- }
152-
153- private logScriptError ( description : string , err : Error ) : void {
154- let message : string ;
155- if ( typeof err === "object" && err . stack != null ) {
156- const match = err . stack . match ( / e v a l m a c h i n e \. < a n o n y m o u s > : ( [ 0 - 9 ] + : [ 0 - 9 ] + ) / ) ;
157- if ( Array . isArray ( match ) ) {
158- message = `and halted at line ${ match [ 1 ] } \n ${ err } ` ;
159- } else {
160- message = `and halted with ${ err . stack } ` ;
161- }
162- } else {
163- message = `that threw ${ typeof err } instead of Error\n ${ err } ` ;
164- }
165- error ( `Servient caught ${ description } ${ message } ` ) ;
166- }
167-
16893 /**
16994 * start
17095 */
17196 public async start ( ) : Promise < typeof WoT > {
17297 const superWoT = await super . start ( ) ;
173- this . runtime = superWoT ;
17498
17599 info ( "DefaultServient started" ) ;
176100
@@ -198,15 +122,6 @@ export default class DefaultServient extends Servient {
198122 description : "Stop servient" ,
199123 output : { type : "string" } ,
200124 } ,
201- ...( this . config . servient . scriptAction === true
202- ? {
203- runScript : {
204- description : "Run script" ,
205- input : { type : "string" } ,
206- output : { type : "string" } ,
207- } ,
208- }
209- : { } ) ,
210125 } ,
211126 } ) ;
212127
@@ -221,14 +136,6 @@ export default class DefaultServient extends Servient {
221136 await this . shutdown ( ) ;
222137 return undefined ;
223138 } ) ;
224- if ( this . config . servient . scriptAction === true ) {
225- servientProducedThing . setActionHandler ( "runScript" , async ( script ) => {
226- const scriptv = await Helpers . parseInteractionOutput ( script ) ;
227- debug ( "running script" , scriptv ) ;
228- this . runScript ( scriptv as string ) ;
229- return undefined ;
230- } ) ;
231- }
232139 servientProducedThing . setPropertyReadHandler ( "things" , async ( ) => {
233140 debug ( "returning things" ) ;
234141 return this . getThings ( ) ;
0 commit comments