MCPcopy Create free account
hub / github.com/pywebio/PyWebIO / ScriptHandler

Class ScriptHandler

webiojs/src/handlers/script.ts:6–49  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4
5
6export class ScriptHandler implements CommandHandler {
7 session: Session;
8
9 accept_command = ['run_script'];
10
11 constructor(session: Session) {
12 this.session = session;
13 }
14
15 handle_message(msg: Command) {
16 let script = msg.spec.code as string;
17 let args = msg.spec.args as { [i: string]: any };
18
19 let arg_names: string[] = [];
20 let arg_vals: any[] = [];
21
22 for (let key in args) {
23 arg_names.push(key);
24 arg_vals.push(args[key]);
25 }
26
27 let res = null;
28 script = `return eval(${JSON.stringify(script)})`; // lgtm [js/bad-code-sanitization]
29 try {
30 const script_func = new Function(...arg_names, script);
31 res = script_func(...arg_vals);
32 } catch (e) {
33 console.log('Exception occurred in user code of `run_script` command: \n%s', e);
34 }
35 if (msg.spec.eval) {
36 // credit: https://stackoverflow.com/questions/27746304/how-do-i-tell-if-an-object-is-a-promise
37 Promise.resolve(res).then(function (value) {
38 state.CurrentSession.send_message({
39 event: "js_yield",
40 task_id: msg.task_id,
41 data: value === undefined ? null : value
42 });
43 }).catch((error) => {
44 console.log('Exception occurred in user code of `run_script` command: \n%s', error);
45 state.CurrentSession.send_message({event: "js_yield", task_id: msg.task_id, data: null});
46 });
47 }
48 }
49}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…