mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-22 20:37:48 +00:00
remove isolate vm (#1299)
This commit is contained in:
@@ -1,38 +1,38 @@
|
||||
import { addLog } from '../../../../common/system/log';
|
||||
const ivm = require('isolated-vm');
|
||||
// import { addLog } from '../../../../common/system/log';
|
||||
// const ivm = require('isolated-vm');
|
||||
|
||||
export const runJsCode = ({
|
||||
code,
|
||||
variables
|
||||
}: {
|
||||
code: string;
|
||||
variables: Record<string, any>;
|
||||
}) => {
|
||||
const isolate = new ivm.Isolate({ memoryLimit: 16 });
|
||||
const context = isolate.createContextSync();
|
||||
const jail = context.global;
|
||||
// export const runJsCode = ({
|
||||
// code,
|
||||
// variables
|
||||
// }: {
|
||||
// code: string;
|
||||
// variables: Record<string, any>;
|
||||
// }) => {
|
||||
// const isolate = new ivm.Isolate({ memoryLimit: 16 });
|
||||
// const context = isolate.createContextSync();
|
||||
// const jail = context.global;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
// custom log function
|
||||
jail.setSync('responseData', function (args: any): any {
|
||||
if (typeof args === 'object') {
|
||||
resolve(args);
|
||||
} else {
|
||||
reject('Not an invalid response');
|
||||
}
|
||||
});
|
||||
// return new Promise((resolve, reject) => {
|
||||
// // custom log function
|
||||
// jail.setSync('responseData', function (args: any): any {
|
||||
// if (typeof args === 'object') {
|
||||
// resolve(args);
|
||||
// } else {
|
||||
// reject('Not an invalid response');
|
||||
// }
|
||||
// });
|
||||
|
||||
// Add global variables
|
||||
jail.setSync('variables', new ivm.ExternalCopy(variables).copyInto());
|
||||
// // Add global variables
|
||||
// jail.setSync('variables', new ivm.ExternalCopy(variables).copyInto());
|
||||
|
||||
try {
|
||||
const scriptCode = `
|
||||
${code}
|
||||
responseData(main(variables))`;
|
||||
context.evalSync(scriptCode, { timeout: 2000 });
|
||||
} catch (err) {
|
||||
addLog.error('Error during script execution:', err);
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
};
|
||||
// try {
|
||||
// const scriptCode = `
|
||||
// ${code}
|
||||
// responseData(main(variables))`;
|
||||
// context.evalSync(scriptCode, { timeout: 2000 });
|
||||
// } catch (err) {
|
||||
// addLog.error('Error during script execution:', err);
|
||||
// reject(err);
|
||||
// }
|
||||
// });
|
||||
// };
|
||||
|
Reference in New Issue
Block a user