@xuda.io/xuda-event-javascript-module 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.mjs +159 -0
- package/package.json +8 -0
package/index.mjs
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
export const run_javascript = async function (SESSION_ID, jobNoP, dsSession, script, evaluate, prog_id, params, evt, $container) {
|
|
2
|
+
const _session = SESSION_OBJ[SESSION_ID];
|
|
3
|
+
|
|
4
|
+
const run_native_javascript_program_in_browser = async function () {
|
|
5
|
+
// run in browser
|
|
6
|
+
|
|
7
|
+
///// insert the css
|
|
8
|
+
const path_to_dist = `https://${_session.domain}/studio-drive/${APP_OBJ[_session.app_id].app_replicate || _session.app_id}/scripts/${prog_id}/dist/`;
|
|
9
|
+
|
|
10
|
+
const index_mjs = await import(`${path_to_dist}index.mjs`);
|
|
11
|
+
|
|
12
|
+
if (index_mjs.css) {
|
|
13
|
+
const css_url = `${path_to_dist}index.css`;
|
|
14
|
+
func.utils.load_css_on_demand(css_url);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const code = func.utils.replace_studio_drive_url(SESSION_ID, index_mjs.default);
|
|
18
|
+
|
|
19
|
+
const api_utils = await func.common.get_module(SESSION_ID, 'xuda-api-library.mjs', {
|
|
20
|
+
func,
|
|
21
|
+
glb,
|
|
22
|
+
SESSION_OBJ,
|
|
23
|
+
SESSION_ID,
|
|
24
|
+
APP_OBJ,
|
|
25
|
+
dsSession,
|
|
26
|
+
jobNoP,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const xu = api_utils;
|
|
30
|
+
|
|
31
|
+
const ret = await code(params, xu, SESSION_ID, SESSION_OBJ, jobNoP, $container);
|
|
32
|
+
|
|
33
|
+
if (!code.toString().includes('func.api.callback')) {
|
|
34
|
+
func.events.delete_job(SESSION_ID, jobNoP);
|
|
35
|
+
}
|
|
36
|
+
return ret;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const run_native_javascript_program_in_server = async function () {
|
|
40
|
+
return new Promise(async (resolve, reject) => {
|
|
41
|
+
const studio_drive_script_folder = path.join(_conf.studio_drive_path, app_id, 'scripts');
|
|
42
|
+
|
|
43
|
+
const studio_drive_script_program_folder = path.join(studio_drive_script_folder, prog_id);
|
|
44
|
+
|
|
45
|
+
const dist = path.join(studio_drive_script_program_folder, 'dist');
|
|
46
|
+
const index_mjs = path.join(dist, 'index.mjs');
|
|
47
|
+
|
|
48
|
+
process.on('uncaughtException', (err) => {
|
|
49
|
+
console.error('Asynchronous error caught.', err);
|
|
50
|
+
});
|
|
51
|
+
const resolve_local = function (e) {
|
|
52
|
+
// resolve({ code: 1, data: e });
|
|
53
|
+
|
|
54
|
+
if (!script.toString().includes('func.api.callback')) {
|
|
55
|
+
func.events.delete_job(SESSION_ID, jobNoP);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
resolve(e);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const reject_local = function (e) {
|
|
62
|
+
// reject({ code: -1, data: e });
|
|
63
|
+
func.events.delete_job(SESSION_ID, jobNoP);
|
|
64
|
+
reject(e);
|
|
65
|
+
// reject(e.message);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
const script = new VM.VMScript(
|
|
70
|
+
`
|
|
71
|
+
const index_mjs = await import(${index_mjs});
|
|
72
|
+
|
|
73
|
+
try{index_mjs.default(req,params,setup_doc,resolve,reject);}catch(e){ console.error(e); }`,
|
|
74
|
+
{ filename: dist, dirname: dist },
|
|
75
|
+
);
|
|
76
|
+
let vm = new VM.NodeVM({
|
|
77
|
+
require: {
|
|
78
|
+
external: true,
|
|
79
|
+
},
|
|
80
|
+
sandbox: {
|
|
81
|
+
resolve: resolve_local,
|
|
82
|
+
reject: reject_local,
|
|
83
|
+
},
|
|
84
|
+
timeout: 60000,
|
|
85
|
+
});
|
|
86
|
+
return await vm.run(script, {
|
|
87
|
+
filename: dist,
|
|
88
|
+
dirname: dist,
|
|
89
|
+
});
|
|
90
|
+
} catch (err) {
|
|
91
|
+
console.error('Failed to execute script.', err);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
if (evaluate) {
|
|
97
|
+
var script_ret = (await func.expression.get(SESSION_ID, script, dsSession, 'javascript', null, null, null, null, null, null, null, () => {}, jobNoP)).result;
|
|
98
|
+
} else {
|
|
99
|
+
if (prog_id) {
|
|
100
|
+
if (typeof IS_API_SERVER !== 'undefined' || typeof IS_DOCKER !== 'undefined' || typeof IS_PROCESS_SERVER !== 'undefined') {
|
|
101
|
+
return await run_native_javascript_program_in_server();
|
|
102
|
+
} else {
|
|
103
|
+
return await run_native_javascript_program_in_browser();
|
|
104
|
+
}
|
|
105
|
+
} else {
|
|
106
|
+
await func.expression.secure_eval(SESSION_ID, 'javascript', func.utils.replace_studio_drive_url(SESSION_ID, script), jobNoP, dsSession, () => {}, evt);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (!script.includes('func.api.callback')) {
|
|
111
|
+
func.events.delete_job(SESSION_ID, jobNoP);
|
|
112
|
+
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export const call_javascript = async function (SESSION_ID, jobNoP, refIdP, dsSession, evaluate, $container) {
|
|
118
|
+
const error_log = async function (msg) {
|
|
119
|
+
let _session = SESSION_OBJ[SESSION_ID];
|
|
120
|
+
func.events.delete_job(SESSION_ID, jobNoP);
|
|
121
|
+
if (typeof IS_API_SERVER !== 'undefined') {
|
|
122
|
+
return __.rpi.write_log(_session.app_id, 'error', 'api', msg);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (typeof IS_DOCKER !== 'undefined') {
|
|
126
|
+
return __.rpi.write_log(_session.app_id, 'error', 'worker', msg);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// client
|
|
130
|
+
await func.common.db(SESSION_ID, 'write_log', {
|
|
131
|
+
msg,
|
|
132
|
+
log_type: 'error',
|
|
133
|
+
source: 'runtime',
|
|
134
|
+
});
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
let view_ret = await func.utils.VIEWS_OBJ.get(SESSION_ID, refIdP.prog);
|
|
138
|
+
|
|
139
|
+
if (!view_ret) {
|
|
140
|
+
return error_log('program not found');
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (!view_ret?.scriptData?.value) {
|
|
144
|
+
return error_log('script is empty');
|
|
145
|
+
}
|
|
146
|
+
return await run_javascript(
|
|
147
|
+
SESSION_ID,
|
|
148
|
+
jobNoP,
|
|
149
|
+
dsSession,
|
|
150
|
+
`(async function(${refIdP?.parameters || {}}) {
|
|
151
|
+
${view_ret.scriptData.value}
|
|
152
|
+
})();`,
|
|
153
|
+
evaluate,
|
|
154
|
+
refIdP.prog,
|
|
155
|
+
refIdP?.parameters,
|
|
156
|
+
null,
|
|
157
|
+
$container,
|
|
158
|
+
);
|
|
159
|
+
};
|