mirror of
https://github.com/OneUptime/oneuptime.git
synced 2026-01-16 23:00:51 +00:00
Refactor JSONFunctions and JavaScriptCode classes
This commit is contained in:
parent
8282669fbd
commit
f57596391d
3 changed files with 28 additions and 17 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue