Commit f6247fe1 by Archer Committed by GitHub

remove isolate vm (#1299)

parent 613699fe
import { addLog } from '../../../../common/system/log'; // import { addLog } from '../../../../common/system/log';
const ivm = require('isolated-vm'); // const ivm = require('isolated-vm');
export const runJsCode = ({ // export const runJsCode = ({
code, // code,
variables // variables
}: { // }: {
code: string; // code: string;
variables: Record<string, any>; // variables: Record<string, any>;
}) => { // }) => {
const isolate = new ivm.Isolate({ memoryLimit: 16 }); // const isolate = new ivm.Isolate({ memoryLimit: 16 });
const context = isolate.createContextSync(); // const context = isolate.createContextSync();
const jail = context.global; // const jail = context.global;
return new Promise((resolve, reject) => { // return new Promise((resolve, reject) => {
// custom log function // // custom log function
jail.setSync('responseData', function (args: any): any { // jail.setSync('responseData', function (args: any): any {
if (typeof args === 'object') { // if (typeof args === 'object') {
resolve(args); // resolve(args);
} else { // } else {
reject('Not an invalid response'); // reject('Not an invalid response');
} // }
}); // });
// Add global variables // // Add global variables
jail.setSync('variables', new ivm.ExternalCopy(variables).copyInto()); // jail.setSync('variables', new ivm.ExternalCopy(variables).copyInto());
try { // try {
const scriptCode = ` // const scriptCode = `
${code} // ${code}
responseData(main(variables))`; // responseData(main(variables))`;
context.evalSync(scriptCode, { timeout: 2000 }); // context.evalSync(scriptCode, { timeout: 2000 });
} catch (err) { // } catch (err) {
addLog.error('Error during script execution:', err); // addLog.error('Error during script execution:', err);
reject(err); // reject(err);
} // }
}); // });
}; // };
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
"encoding": "^0.1.13", "encoding": "^0.1.13",
"file-type": "^19.0.0", "file-type": "^19.0.0",
"iconv-lite": "^0.6.3", "iconv-lite": "^0.6.3",
"isolated-vm": "4.7.2",
"joplin-turndown-plugin-gfm": "^1.0.12", "joplin-turndown-plugin-gfm": "^1.0.12",
"js-tiktoken": "^1.0.7", "js-tiktoken": "^1.0.7",
"json5": "^2.2.3", "json5": "^2.2.3",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment