feat: add isAdmin column to token entity; implement migration for isAdmin; enhance logging and error handling in query execution; update query plugin to support new logging structure

This commit is contained in:
lborv
2025-10-11 17:59:06 +03:00
parent 57e4a8b932
commit 08a62fa2c5
13 changed files with 135 additions and 55 deletions

View File

@ -1,5 +1,10 @@
import { TLog } from "src/query/logger/logger.types";
export abstract class Plugin {
protected name: string;
protected log: TLog;
protected cookies: Record<string, string>;
protected headers: Record<string, string>;
constructor(name: string, protected methods: string[] = []) {
this.name = name;
@ -13,6 +18,18 @@ export abstract class Plugin {
return this.methods;
}
setLog(log: TLog) {
this.log = log;
}
setHeaders(headers: Record<string, string>) {
this.headers = headers;
}
setCookies(cookies: Record<string, string>) {
this.cookies = cookies;
}
static init(...args: any[]): any {
return args;
}

View File

@ -35,7 +35,7 @@ export class DatabasePlugin extends Plugin {
enableKeepAlive: true,
});
await dbConnection.query("SET SESSION MAX_EXECUTION_TIME=2000;");
// await dbConnection.query("SET SESSION MAX_EXECUTION_TIME=2000;");
return new DatabasePlugin(name, dbConnection);
}

View File

@ -15,11 +15,7 @@ export class QueryPlugin extends Plugin {
return new QueryPlugin("query", query, queryExecuterService);
}
async run(data: {
token: string;
queryData: any;
headers?: Record<string, any>;
}): Promise<any> {
async run(id: string, data: any): Promise<any> {
const query = await this.QueryExecuterService.queryRepository.findOne({
where: { id: this.query.id },
});
@ -35,9 +31,11 @@ export class QueryPlugin extends Plugin {
}
return await this.QueryExecuterService.runQuery(
query.id,
data.queryData,
data.headers
id,
data,
this.headers,
this.cookies,
this.log
);
}

View File

@ -16,7 +16,9 @@ export class Vm {
private isolate: ivm.Isolate;
private timeLimit?: bigint;
private cpuTimeLimit?: bigint;
private sessionId: string | null;
private log: TLog;
private headers?: Record<string, string>;
private cookies?: Record<string, string>;
constructor(configs: {
memoryLimit: number;
@ -25,6 +27,9 @@ export class Vm {
modules: VModule[];
plugins: Plugin[];
functions: string[];
log: TLog;
headers?: Record<string, string>;
cookies?: Record<string, string>;
}) {
this.memoryLimit = configs.memoryLimit;
this.modules = configs.modules;
@ -32,6 +37,9 @@ export class Vm {
this.timeLimit = configs.timeLimit;
this.cpuTimeLimit = configs.cpuTimeLimit;
this.functions = configs.functions;
this.log = configs.log;
this.headers = configs.headers;
this.cookies = configs.cookies;
}
async init(): Promise<Vm> {
@ -60,7 +68,17 @@ export class Vm {
for (const method of plugin.getMethods()) {
const fnRef = new ivm.Reference(async (...args) => {
return await plugin[method](...args);
plugin.setLog(this.log);
plugin.setHeaders(this.headers);
plugin.setCookies(this.cookies);
const result = await plugin[method](...args);
if (result && result.log) {
this.log = result.log;
delete result.log;
}
return result;
});
await this.context.evalClosure(
@ -86,8 +104,7 @@ export class Vm {
async runScript(
script: string,
args: Record<string, any>,
headers: Record<string, any>,
log: TLog
headers: Record<string, any>
): Promise<QueryResponse> {
let resolvePromise: (value: any) => void;
let rejectPromise: (reason?: any) => void;
@ -98,13 +115,11 @@ export class Vm {
});
this.setFunction("returnResult", (res) => {
resolvePromise({ ...res, log });
resolvePromise({ ...res, log: this.log });
});
this.setFunction("log", (...args) => {
if (!log) {
return;
}
console.log(...args);
const logType = args.find(
(arg) =>
@ -114,7 +129,7 @@ export class Vm {
Object.values(TLogType).includes(arg.type)
);
log = LoggerService.log(log, {
this.log = LoggerService.log(this.log, {
content: args
.map((arg) => (typeof arg === "string" ? arg : JSON.stringify(arg)))
.join(" "),
@ -124,7 +139,12 @@ export class Vm {
});
this.setFunction("error", (error: any) => {
console.error("Script error:", error);
LoggerService.log(this.log, {
content: error?.stack || error?.toString() || "Unknown error",
type: TLogType.error,
timeStamp: new Date().getTime(),
});
rejectPromise(error);
});