mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-04 13:39:01 +01:00 
			
		
		
		
	fix(desktop): CLS failing due to lack of listeners
This commit is contained in:
		
							parent
							
								
									ebeabe2b54
								
							
						
					
					
						commit
						9907f7f60f
					
				@ -3,10 +3,19 @@ import type { Application } from "express";
 | 
			
		||||
import type { ParamsDictionary, Request, Response } from "express-serve-static-core";
 | 
			
		||||
import type QueryString from "qs";
 | 
			
		||||
import { Session, SessionData } from "express-session";
 | 
			
		||||
import EventEmitter from "events";
 | 
			
		||||
 | 
			
		||||
type MockedResponse = Response<any, Record<string, any>, number>;
 | 
			
		||||
 | 
			
		||||
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> = {
 | 
			
		||||
    id: "session-id", // Placeholder for session ID
 | 
			
		||||
    cookie: {
 | 
			
		||||
@ -33,52 +42,79 @@ function init(app: Application) {
 | 
			
		||||
    touch: () => fakeSession
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
    electron.ipcMain.on("server-request", (event, arg) => {
 | 
			
		||||
        const req: Pick<Request<ParamsDictionary, any, any, QueryString.ParsedQs, Record<string, any>>, "url" | "method" | "body" | "headers" | "session"> = {
 | 
			
		||||
            url: arg.url,
 | 
			
		||||
            method: arg.method,
 | 
			
		||||
            body: arg.data,
 | 
			
		||||
            headers: arg.headers,
 | 
			
		||||
            session: fakeSession
 | 
			
		||||
        };
 | 
			
		||||
interface Arg {
 | 
			
		||||
    url: string;
 | 
			
		||||
    method: string;
 | 
			
		||||
    data: any;
 | 
			
		||||
    headers: Record<string, string>
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
        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[]) {
 | 
			
		||||
                respHeaders[name] = value ?? "";
 | 
			
		||||
                return res as MockedResponse;
 | 
			
		||||
            },
 | 
			
		||||
            status: (statusCode) => {
 | 
			
		||||
                res.statusCode = statusCode;
 | 
			
		||||
                return res as MockedResponse;
 | 
			
		||||
            },
 | 
			
		||||
            send: (obj) => {
 | 
			
		||||
                event.sender.send("server-response", {
 | 
			
		||||
                    url: arg.url,
 | 
			
		||||
                    method: arg.method,
 | 
			
		||||
                    requestId: arg.requestId,
 | 
			
		||||
                    statusCode: res.statusCode,
 | 
			
		||||
                    headers: respHeaders,
 | 
			
		||||
        this.respHeaders[name] = value ?? "";
 | 
			
		||||
        return this as unknown as MockedResponse;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    status(statusCode) {
 | 
			
		||||
        this.statusCode = statusCode;
 | 
			
		||||
        return this as unknown as MockedResponse;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    send(obj) {
 | 
			
		||||
        this.event.sender.send("server-response", {
 | 
			
		||||
            url: this.arg.url,
 | 
			
		||||
            method: this.arg.method,
 | 
			
		||||
            requestId: this.arg.requestId,
 | 
			
		||||
            statusCode: this.statusCode,
 | 
			
		||||
            headers: this.respHeaders,
 | 
			
		||||
            body: obj
 | 
			
		||||
        });
 | 
			
		||||
                return res as MockedResponse;
 | 
			
		||||
            },
 | 
			
		||||
            locals: {},
 | 
			
		||||
            json: (obj) => {
 | 
			
		||||
                res.send(JSON.stringify(obj));
 | 
			
		||||
                return res as MockedResponse;
 | 
			
		||||
        return this as unknown 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;
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user