Refactor code structure for improved readability and maintainability
This commit is contained in:
81
src/test/test.controller.ts
Normal file
81
src/test/test.controller.ts
Normal file
@ -0,0 +1,81 @@
|
||||
import { Controller, Get } from "@nestjs/common";
|
||||
import * as ivm from "isolated-vm";
|
||||
import * as fs from "fs";
|
||||
|
||||
@Controller("test")
|
||||
export class TestController {
|
||||
@Get()
|
||||
async test() {
|
||||
const isolate = new ivm.Isolate({ memoryLimit: 128 });
|
||||
|
||||
const context = isolate.createContextSync();
|
||||
const jail = context.global;
|
||||
|
||||
await jail.set("global", jail.derefInto());
|
||||
|
||||
jail.setSync("log", function (...args) {
|
||||
console.log(...args);
|
||||
});
|
||||
|
||||
const squelSource = fs.readFileSync(
|
||||
"node_modules/squel/dist/squel.min.js",
|
||||
"utf8"
|
||||
);
|
||||
|
||||
await context.eval(squelSource);
|
||||
|
||||
jail.setSync(
|
||||
"testAsync",
|
||||
new ivm.Reference(async (arg) => {
|
||||
const result = await this.mockAsync(arg);
|
||||
return result;
|
||||
})
|
||||
);
|
||||
|
||||
jail.setSync("result", (query) => {
|
||||
console.log("final", query);
|
||||
});
|
||||
|
||||
const hostile = isolate.compileScriptSync(`
|
||||
async function main() {
|
||||
async function asyncCall(reference, arg) {
|
||||
return await reference.apply(undefined, [arg], { result: { promise: true } });
|
||||
}
|
||||
|
||||
const a = await asyncCall(testAsync, 'testArg');
|
||||
|
||||
const query = squel.select().from("users").where("id = ?", a);
|
||||
|
||||
log(a);
|
||||
log('testMe');
|
||||
return query.toString();
|
||||
};
|
||||
|
||||
(async () => {
|
||||
const resultQuery = await main();
|
||||
result(resultQuery);
|
||||
})();
|
||||
`);
|
||||
|
||||
hostile
|
||||
.run(context)
|
||||
.catch((err) => console.error(err))
|
||||
.then((result) => {
|
||||
console.log("Script executed successfully", result);
|
||||
});
|
||||
|
||||
return "Test successful!";
|
||||
}
|
||||
|
||||
mockAsync(arg: any) {
|
||||
console.log(arg);
|
||||
|
||||
const result = new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve("Mock async result");
|
||||
}, 1);
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user