Add middleware for server side lemmy client

This commit is contained in:
SleeplessOne1917 2024-08-25 13:35:15 -04:00
parent 252695ea6a
commit dcfed945c8
4 changed files with 33 additions and 1 deletions

View file

@ -11,7 +11,7 @@ import SecurityHandler from "./handlers/security-handler";
import ServiceWorkerHandler from "./handlers/service-worker-handler"; import ServiceWorkerHandler from "./handlers/service-worker-handler";
import ThemeHandler from "./handlers/theme-handler"; import ThemeHandler from "./handlers/theme-handler";
import ThemesListHandler from "./handlers/themes-list-handler"; import ThemesListHandler from "./handlers/themes-list-handler";
import { setCacheControl, setDefaultCsp } from "./middleware"; import { setCacheControl, setDefaultCsp, setRequestClient } from "./middleware";
import CodeThemeHandler from "./handlers/code-theme-handler"; import CodeThemeHandler from "./handlers/code-theme-handler";
import { verifyDynamicImports } from "../shared/dynamic-imports"; import { verifyDynamicImports } from "../shared/dynamic-imports";
import cookieParser from "cookie-parser"; import cookieParser from "cookie-parser";
@ -47,6 +47,7 @@ if (
server.use(getStaticDir(), express.static(serverPath)); server.use(getStaticDir(), express.static(serverPath));
} }
server.use(setRequestClient);
server.get("/.well-known/security.txt", SecurityHandler); server.get("/.well-known/security.txt", SecurityHandler);
server.get("/robots.txt", RobotsHandler); server.get("/robots.txt", RobotsHandler);
server.get("/service-worker.js", ServiceWorkerHandler); server.get("/service-worker.js", ServiceWorkerHandler);

View file

@ -1,6 +1,10 @@
import * as crypto from "crypto"; import * as crypto from "crypto";
import type { NextFunction, Request, Response } from "express"; import type { NextFunction, Request, Response } from "express";
import { getJwtCookie } from "./utils/has-jwt-cookie"; import { getJwtCookie } from "./utils/has-jwt-cookie";
import { setForwardedHeaders } from "./utils/set-forwarded-headers";
import { wrapClient } from "../shared/services/HttpService";
import { LemmyHttp } from "lemmy-js-client";
import { getHttpBaseInternal } from "@utils/env";
export function setDefaultCsp({ export function setDefaultCsp({
res, res,
@ -63,3 +67,20 @@ export function setCacheControl(
next(); next();
} }
export function setRequestClient({
req,
next,
}: {
req: Request;
next: NextFunction;
}) {
const client = wrapClient(
new LemmyHttp(getHttpBaseInternal(), {
headers: setForwardedHeaders(req.headers),
}),
);
req.client = client;
next();
}

9
src/shared/express-extensions.d.ts vendored Normal file
View file

@ -0,0 +1,9 @@
import { wrapClient } from "./services/HttpService";
declare global {
namespace Express {
export interface Request {
client: ReturnType<typeof wrapClient>;
}
}
}

View file

@ -26,6 +26,7 @@
}, },
"include": [ "include": [
"src/shared/build-config.d.ts", "src/shared/build-config.d.ts",
"src/shared/express-extensions.d.ts",
"src/**/*.ts", "src/**/*.ts",
"src/**/*.tsx", "src/**/*.tsx",
"node_modules/inferno/dist/index.d.ts" "node_modules/inferno/dist/index.d.ts"