feat: implement session management with SessionService and SessionPlugin; refactor query execution to handle session cookies; update token and query handling for improved session tracking
This commit is contained in:
57
src/vm/plugins/session.plugin.ts
Normal file
57
src/vm/plugins/session.plugin.ts
Normal file
@ -0,0 +1,57 @@
|
||||
import { Query } from "src/query/entities/query.entity";
|
||||
import { Plugin } from "../plugin.class";
|
||||
import { QueryExecuterService } from "src/query/executer/query.executer.service";
|
||||
|
||||
export class SessionPlugin extends Plugin {
|
||||
constructor(
|
||||
name: string,
|
||||
private query: Query,
|
||||
private queryExecuterService: QueryExecuterService,
|
||||
private session: Record<string, any>
|
||||
) {
|
||||
super(name, ["get", "set", "delete"]);
|
||||
}
|
||||
|
||||
static async init(
|
||||
query: Query,
|
||||
queryExecuterService: QueryExecuterService,
|
||||
sessionId: string | null
|
||||
) {
|
||||
const session = await queryExecuterService.sessionService.get(
|
||||
sessionId,
|
||||
query.project.id
|
||||
);
|
||||
|
||||
return new SessionPlugin("session", query, queryExecuterService, session);
|
||||
}
|
||||
|
||||
async get(property: string): Promise<any> {
|
||||
this.session = await this.queryExecuterService.sessionService.get(
|
||||
this.session?.sessionId,
|
||||
this.query.project.id
|
||||
);
|
||||
|
||||
return this.session[property];
|
||||
}
|
||||
|
||||
async set(property: string, data: any): Promise<void> {
|
||||
this.session[property] = data;
|
||||
|
||||
await this.queryExecuterService.sessionService.set(
|
||||
this.session?.sessionId,
|
||||
this.query.project.id,
|
||||
this.session
|
||||
);
|
||||
}
|
||||
|
||||
async delete(): Promise<void> {
|
||||
return await this.queryExecuterService.sessionService.delete(
|
||||
this.session?.sessionId,
|
||||
this.query.project.id
|
||||
);
|
||||
}
|
||||
|
||||
onFinish() {
|
||||
// No resources to clean up
|
||||
}
|
||||
}
|
||||
@ -13,6 +13,7 @@ export class Vm {
|
||||
private isolate: ivm.Isolate;
|
||||
private timeLimit?: bigint;
|
||||
private cpuTimeLimit?: bigint;
|
||||
private sessionId: string | null;
|
||||
|
||||
constructor(configs: {
|
||||
memoryLimit: number;
|
||||
|
||||
@ -4,6 +4,7 @@ import { Query } from "src/query/entities/query.entity";
|
||||
import { QueryPlugin } from "./plugins/query.plugin";
|
||||
import { AxiosPlugin } from "./plugins/axios.plugin";
|
||||
import { RedisPlugin } from "./plugins/redis.plugin";
|
||||
import { SessionPlugin } from "./plugins/session.plugin";
|
||||
|
||||
export const registeredPlugins = {
|
||||
db: async (service: QueryExecuterService, query: Query) => {
|
||||
@ -30,6 +31,13 @@ export const registeredPlugins = {
|
||||
|
||||
return RedisPlugin.init("redis", redisConnection, query.project.id);
|
||||
},
|
||||
session: async (
|
||||
service: QueryExecuterService,
|
||||
query: Query,
|
||||
sessionId: string | null
|
||||
) => {
|
||||
return SessionPlugin.init(query, service, sessionId);
|
||||
},
|
||||
axios: async () => {
|
||||
return AxiosPlugin.init("axios");
|
||||
},
|
||||
@ -50,4 +58,5 @@ export type QueryResponse = {
|
||||
response: any;
|
||||
headers?: Record<string, string>;
|
||||
redirect?: string;
|
||||
cookies?: Record<string, string>;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user