feat: add TypeScript compilation support in QueryExecuterService
This commit is contained in:
@ -27,4 +27,7 @@ export class Query {
|
|||||||
|
|
||||||
@Column({ type: "tinyint", default: 0 })
|
@Column({ type: "tinyint", default: 0 })
|
||||||
isCommand: number;
|
isCommand: number;
|
||||||
|
|
||||||
|
@Column({ type: "tinyint", default: 0 })
|
||||||
|
isTypescript: number;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,6 +19,7 @@ import { SessionService } from "../session/session.service";
|
|||||||
import { TLog, TLogType } from "../logger/logger.types";
|
import { TLog, TLogType } from "../logger/logger.types";
|
||||||
import { LoggerService } from "../logger/logger.service";
|
import { LoggerService } from "../logger/logger.service";
|
||||||
import { ProjectSettingService } from "src/project/settings/project.setting.service";
|
import { ProjectSettingService } from "src/project/settings/project.setting.service";
|
||||||
|
import ts from "typescript";
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class QueryExecuterService {
|
export class QueryExecuterService {
|
||||||
@ -89,6 +90,14 @@ export class QueryExecuterService {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private compileTypeScript(tsCode: string) {
|
||||||
|
const jsCode = ts.transpileModule(tsCode, {
|
||||||
|
compilerOptions: { module: ts.ModuleKind.CommonJS },
|
||||||
|
}).outputText;
|
||||||
|
|
||||||
|
return jsCode;
|
||||||
|
}
|
||||||
|
|
||||||
async runQuery(
|
async runQuery(
|
||||||
token: string,
|
token: string,
|
||||||
queryData: any,
|
queryData: any,
|
||||||
@ -122,17 +131,19 @@ export class QueryExecuterService {
|
|||||||
type: TLogType.info,
|
type: TLogType.info,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let script = this.clearImports(query.source);
|
||||||
|
|
||||||
|
if (query.isTypescript) {
|
||||||
|
script = this.compileTypeScript(script);
|
||||||
|
}
|
||||||
|
|
||||||
const vm = await this.createVm(
|
const vm = await this.createVm(
|
||||||
query,
|
query,
|
||||||
log,
|
log,
|
||||||
callStack,
|
callStack,
|
||||||
cookies["x-session-id"]
|
cookies["x-session-id"]
|
||||||
);
|
);
|
||||||
const result = await vm.runScript(
|
const result = await vm.runScript(script, queryData, headers);
|
||||||
this.clearImports(query.source),
|
|
||||||
queryData,
|
|
||||||
headers
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!this.checkResponse(result)) {
|
if (!this.checkResponse(result)) {
|
||||||
throw new Error(`Error initializing VM: ${JSON.stringify(result)}`);
|
throw new Error(`Error initializing VM: ${JSON.stringify(result)}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user