feat: enhance DatabaseManagerService and QueryExecuterService with timeout settings, add AxiosPlugin for HTTP requests, and update DatabasePlugin to use query method
This commit is contained in:
54
src/vm/plugins/axios.plugin.ts
Normal file
54
src/vm/plugins/axios.plugin.ts
Normal file
@ -0,0 +1,54 @@
|
||||
import axios from "axios";
|
||||
import { Plugin } from "../plugin.class";
|
||||
|
||||
export class AxiosPlugin extends Plugin {
|
||||
constructor(name: string) {
|
||||
super(name, ["get", "post", "put", "delete", "head", "options", "patch"]);
|
||||
}
|
||||
|
||||
static init(name: string): AxiosPlugin {
|
||||
return new AxiosPlugin(name);
|
||||
}
|
||||
|
||||
private configure(config) {
|
||||
if (config.timeout > 10e3) {
|
||||
config.timeout = 10e3;
|
||||
} else {
|
||||
config.timeout = 5e3;
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
async get(url: string, config?: any): Promise<any> {
|
||||
return await axios.get(url, this.configure(config));
|
||||
}
|
||||
|
||||
async post(url: string, data?: any, config?: any): Promise<any> {
|
||||
return await axios.post(url, data, this.configure(config));
|
||||
}
|
||||
|
||||
async put(url: string, data?: any, config?: any): Promise<any> {
|
||||
return await axios.put(url, data, this.configure(config));
|
||||
}
|
||||
|
||||
async delete(url: string, config?: any): Promise<any> {
|
||||
return await axios.delete(url, this.configure(config));
|
||||
}
|
||||
|
||||
async head(url: string, config?: any): Promise<any> {
|
||||
return await axios.head(url, this.configure(config));
|
||||
}
|
||||
|
||||
async options(url: string, config?: any): Promise<any> {
|
||||
return await axios.options(url, this.configure(config));
|
||||
}
|
||||
|
||||
async patch(url: string, data?: any, config?: any): Promise<any> {
|
||||
return await axios.patch(url, data, this.configure(config));
|
||||
}
|
||||
|
||||
onFinish() {
|
||||
// No resources to clean up
|
||||
}
|
||||
}
|
||||
@ -9,7 +9,7 @@ export class DatabasePlugin extends Plugin {
|
||||
);
|
||||
}
|
||||
|
||||
super(name, ["execute"]);
|
||||
super(name, ["query"]);
|
||||
}
|
||||
|
||||
static async init(
|
||||
@ -20,6 +20,8 @@ export class DatabasePlugin extends Plugin {
|
||||
user: string;
|
||||
password: string;
|
||||
database: string;
|
||||
idleTimeout: number;
|
||||
connectTimeout: number;
|
||||
}
|
||||
): Promise<DatabasePlugin> {
|
||||
const dbConnection = await mysql.createConnection({
|
||||
@ -28,19 +30,18 @@ export class DatabasePlugin extends Plugin {
|
||||
port: config.port,
|
||||
password: config.password,
|
||||
database: config.database,
|
||||
idleTimeout: config.idleTimeout,
|
||||
connectTimeout: config.connectTimeout,
|
||||
enableKeepAlive: true,
|
||||
});
|
||||
|
||||
await dbConnection.query("SET SESSION MAX_EXECUTION_TIME=2000;");
|
||||
|
||||
return new DatabasePlugin(name, dbConnection);
|
||||
}
|
||||
|
||||
async execute(query): Promise<any> {
|
||||
const [rows, fields] = await this.dbConnection.query(query);
|
||||
|
||||
return {
|
||||
rows: rows,
|
||||
fields: fields ?? [],
|
||||
};
|
||||
async query(query): Promise<any> {
|
||||
return await this.dbConnection.query(query);
|
||||
}
|
||||
|
||||
onFinish() {
|
||||
|
||||
@ -10,15 +10,21 @@ export class Vm {
|
||||
private jail: any;
|
||||
private plugins: Plugin[];
|
||||
private isolate: ivm.Isolate;
|
||||
private timeLimit?: bigint;
|
||||
private cpuTimeLimit?: bigint;
|
||||
|
||||
constructor(configs: {
|
||||
memoryLimit: number;
|
||||
timeLimit?: bigint;
|
||||
cpuTimeLimit?: bigint;
|
||||
modules: VModule[];
|
||||
plugins: Plugin[];
|
||||
}) {
|
||||
this.memoryLimit = configs.memoryLimit;
|
||||
this.modules = configs.modules;
|
||||
this.plugins = configs.plugins;
|
||||
this.timeLimit = configs.timeLimit;
|
||||
this.cpuTimeLimit = configs.cpuTimeLimit;
|
||||
}
|
||||
|
||||
async init(): Promise<Vm> {
|
||||
@ -111,11 +117,23 @@ export class Vm {
|
||||
|
||||
const compiledScript = await this.isolate.compileScript(scriptWithResult);
|
||||
|
||||
const interval = setInterval(() => {
|
||||
if (
|
||||
this.isolate.cpuTime > this.cpuTimeLimit ||
|
||||
this.isolate.wallTime > this.timeLimit
|
||||
) {
|
||||
this.isolate.dispose();
|
||||
|
||||
rejectPromise(new Error("Script execution timed out"));
|
||||
}
|
||||
}, 500);
|
||||
|
||||
compiledScript.run(this.context);
|
||||
|
||||
try {
|
||||
return await resultPromise;
|
||||
} finally {
|
||||
clearInterval(interval);
|
||||
this.onFinish();
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@ import { QueryExecuterService } from "src/query/executer/query.executer.service"
|
||||
import { DatabasePlugin } from "./plugins/database.plugin";
|
||||
import { Query } from "src/query/entities/query.entity";
|
||||
import { QueryPlugin } from "./plugins/query.plugin";
|
||||
import { AxiosPlugin } from "./plugins/axios.plugin";
|
||||
|
||||
export const registeredPlugins = {
|
||||
db: async (service: QueryExecuterService, query: Query) => {
|
||||
@ -17,6 +18,9 @@ export const registeredPlugins = {
|
||||
|
||||
return DatabasePlugin.init("db", databaseConnection);
|
||||
},
|
||||
axios: async () => {
|
||||
return AxiosPlugin.init("axios");
|
||||
},
|
||||
query: async (service: QueryExecuterService, query: Query) => {
|
||||
return QueryPlugin.init(query, service);
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user