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:
@ -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);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user