Refactor JSONFunctions and JavaScriptCode classes

This commit is contained in:
Simon Larsen 2024-04-06 21:10:29 +01:00
parent 8282669fbd
commit f57596391d
No known key found for this signature in database
GPG key ID: AB45983AA9C81CDE
3 changed files with 28 additions and 17 deletions

View file

@ -18,6 +18,26 @@ export default class JSONFunctions {
return Object.keys(obj).length === 0;
}
public static removeCircularReferences(obj: JSONObject): JSONObject {
const cache: any[] = [];
const returnValue: string = JSON.stringify(
obj,
(_key: string, value: any) => {
if (typeof value === 'object' && value !== null) {
if (cache.includes(value)) {
return;
}
cache.push(value);
}
return value;
}
);
return JSON.parse(returnValue);
}
public static isEqualObject(
obj1: JSONObject | undefined,
obj2: JSONObject | undefined

View file

@ -63,21 +63,7 @@ export default class JavaScriptCode extends ComponentCode {
scriptArgs = JSON.parse(scriptArgs);
}
const codeLines: Array<string> = ((args['code'] as string) || '')
.trim()
.split('\n');
const uncommentedLines: Array<string> = codeLines
.map((line: string) => {
// remove comment lines and remove comments from lines
return line.replace(/\/\/.*$/, '').trim();
})
.filter((line: string) => {
// remove empty lines
return line.trim().length > 0;
});
const code: string = uncommentedLines.join(' ');
const code: string = (args['code'] as string) || '';
const returnResult: ReturnResult = await VMUtil.runCodeInSandbox({
code,

View file

@ -10,6 +10,7 @@ import VMUtil from '../Utils/VM';
import BadDataException from 'Common/Types/Exception/BadDataException';
import ReturnResult from 'Common/Types/IsolatedVM/ReturnResult';
import logger from 'CommonServer/Utils/Logger';
import JSONFunctions from 'Common/Types/JSONFunctions';
const router: ExpressRouter = Express.getRouter();
@ -43,8 +44,6 @@ router.post(
args: req.body?.['options']?.['args'] || {},
},
});
} catch (err) {
logger.error(err);
throw new BadDataException((err as Error).message);
@ -56,6 +55,12 @@ router.post(
logger.info('Code Logs ');
logger.info(result.logMessages);
if (typeof result.returnValue === 'object') {
result.returnValue = JSONFunctions.removeCircularReferences(
result.returnValue
);
}
return Response.sendJsonObjectResponse(req, res, {
returnValue: result.returnValue,
logMessages: result.logMessages,