fix(desktop): CLS failing due to lack of listeners

This commit is contained in:
Elian Doran 2025-06-07 15:45:56 +03:00
parent ebeabe2b54
commit 9907f7f60f
No known key found for this signature in database

View File

@ -3,10 +3,19 @@ import type { Application } from "express";
import type { ParamsDictionary, Request, Response } from "express-serve-static-core"; import type { ParamsDictionary, Request, Response } from "express-serve-static-core";
import type QueryString from "qs"; import type QueryString from "qs";
import { Session, SessionData } from "express-session"; import { Session, SessionData } from "express-session";
import EventEmitter from "events";
type MockedResponse = Response<any, Record<string, any>, number>; type MockedResponse = Response<any, Record<string, any>, number>;
function init(app: Application) { function init(app: Application) {
electron.ipcMain.on("server-request", (event, arg) => {
const req = new FakeRequest(arg);
const res = new FakeResponse(event, arg);
return app.router(req as any, res as any, () => {});
});
}
const fakeSession: Session & Partial<SessionData> = { const fakeSession: Session & Partial<SessionData> = {
id: "session-id", // Placeholder for session ID id: "session-id", // Placeholder for session ID
cookie: { cookie: {
@ -33,52 +42,79 @@ function init(app: Application) {
touch: () => fakeSession touch: () => fakeSession
}; };
electron.ipcMain.on("server-request", (event, arg) => { interface Arg {
const req: Pick<Request<ParamsDictionary, any, any, QueryString.ParsedQs, Record<string, any>>, "url" | "method" | "body" | "headers" | "session"> = { url: string;
url: arg.url, method: string;
method: arg.method, data: any;
body: arg.data, headers: Record<string, string>
headers: arg.headers, }
session: fakeSession
};
const respHeaders: Record<string, string | string[]> = {}; class FakeRequest extends EventEmitter implements Pick<Request<ParamsDictionary, any, any, QueryString.ParsedQs, Record<string, any>>, "url" | "method" | "body" | "headers" | "session"> {
url: string;
method: string;
body: any;
headers: Record<string, string>;
session: Session & Partial<SessionData>;
constructor(arg: Arg) {
super();
this.url = arg.url;
this.method = arg.method;
this.body = arg.data;
this.headers = arg.headers;
this.session = fakeSession;
}
}
class FakeResponse extends EventEmitter implements Pick<Response<any, Record<string, any>, number>, "status" | "send" | "json" | "setHeader"> {
private respHeaders: Record<string, string | string[]> = {};
private event: Electron.IpcMainEvent;
private arg: Arg & { requestId: string; };
statusCode: number = 200;
headers: Record<string, string> = {};
locals: Record<string, any> = {};
constructor(event: Electron.IpcMainEvent, arg: Arg & { requestId: string; }) {
super();
this.event = event;
this.arg = arg;
}
getHeader(name) {
return this.respHeaders[name];
}
setHeader(name, value) {
this.respHeaders[name] = value.toString();
return this as unknown as MockedResponse;
}
const res: Pick<Response<any, Record<string, any>, number>, "statusCode" | "getHeader" | "setHeader" | "header" | "status" | "send" | "locals" | "json"> = {
statusCode: 200,
getHeader: (name) => respHeaders[name],
setHeader: (name, value) => {
respHeaders[name] = value.toString();
return res as MockedResponse;
},
header(name: string, value?: string | string[]) { header(name: string, value?: string | string[]) {
respHeaders[name] = value ?? ""; this.respHeaders[name] = value ?? "";
return res as MockedResponse; return this as unknown as MockedResponse;
}, }
status: (statusCode) => {
res.statusCode = statusCode; status(statusCode) {
return res as MockedResponse; this.statusCode = statusCode;
}, return this as unknown as MockedResponse;
send: (obj) => { }
event.sender.send("server-response", {
url: arg.url, send(obj) {
method: arg.method, this.event.sender.send("server-response", {
requestId: arg.requestId, url: this.arg.url,
statusCode: res.statusCode, method: this.arg.method,
headers: respHeaders, requestId: this.arg.requestId,
statusCode: this.statusCode,
headers: this.respHeaders,
body: obj body: obj
}); });
return res as MockedResponse; return this as unknown as MockedResponse;
},
locals: {},
json: (obj) => {
res.send(JSON.stringify(obj));
return res as MockedResponse;
} }
};
return app.router(req as any, res as any, () => {}); json(obj) {
}); this.send(JSON.stringify(obj));
return this as unknown as MockedResponse;
}
} }
export default init; export default init;