summary refs log tree commit diff
path: root/api/src/middlewares/TestClient.ts
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-08-12 20:09:35 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-08-12 20:09:35 +0200
commit08e837bf5559e9680fc8cb99bd05b93f8eb2cac5 (patch)
tree1eadc038773b025275d7b751265f741b09ca92ab /api/src/middlewares/TestClient.ts
parentnpm i @fosscord/server-util@1.3.52 (diff)
downloadserver-08e837bf5559e9680fc8cb99bd05b93f8eb2cac5.tar.xz
:sparkles: api
Diffstat (limited to 'api/src/middlewares/TestClient.ts')
-rw-r--r--api/src/middlewares/TestClient.ts66
1 files changed, 66 insertions, 0 deletions
diff --git a/api/src/middlewares/TestClient.ts b/api/src/middlewares/TestClient.ts
new file mode 100644
index 00000000..4e3c9de3
--- /dev/null
+++ b/api/src/middlewares/TestClient.ts
@@ -0,0 +1,66 @@
+import bodyParser, { OptionsJson } from "body-parser";
+import express, { NextFunction, Request, Response, Application } from "express";
+import { HTTPError } from "lambert-server";
+import fs from "fs";
+import path from "path";
+import fetch, { Response as FetchResponse } from "node-fetch";
+import { Config } from "@fosscord/server-util";
+
+export default function TestClient(app: Application) {
+	const assetCache = new Map<string, { response: FetchResponse; buffer: Buffer }>();
+	const indexHTML = fs.readFileSync(path.join(__dirname, "..", "..", "client_test", "index.html"), { encoding: "utf8" });
+
+	app.use("/assets", express.static(path.join(__dirname, "..", "assets")));
+
+	app.get("/assets/:file", async (req: Request, res: Response) => {
+		delete req.headers.host;
+		var response: FetchResponse;
+		var buffer: Buffer;
+		const cache = assetCache.get(req.params.file);
+		if (!cache) {
+			response = await fetch(`https://discord.com/assets/${req.params.file}`, {
+				// @ts-ignore
+				headers: {
+					...req.headers
+				}
+			});
+			buffer = await response.buffer();
+		} else {
+			response = cache.response;
+			buffer = cache.buffer;
+		}
+
+		response.headers.forEach((value, name) => {
+			if (
+				[
+					"content-length",
+					"content-security-policy",
+					"strict-transport-security",
+					"set-cookie",
+					"transfer-encoding",
+					"expect-ct",
+					"access-control-allow-origin",
+					"content-encoding"
+				].includes(name.toLowerCase())
+			) {
+				return;
+			}
+			res.set(name, value);
+		});
+		assetCache.set(req.params.file, { buffer, response });
+
+		return res.send(buffer);
+	});
+	app.get("*", (req: Request, res: Response) => {
+		res.set("Cache-Control", "public, max-age=" + 60 * 60 * 24);
+		res.set("content-type", "text/html");
+		var html = indexHTML;
+		const CDN_ENDPOINT = (Config.get()?.cdn.endpoint || process.env.CDN || "").replace(/(https?)?(:\/\/?)/g, "");
+		const GATEWAY_ENDPOINT = Config.get()?.gateway.endpoint || process.env.GATEWAY || "";
+
+		if (CDN_ENDPOINT) html = html.replace(/CDN_HOST: .+/, `CDN_HOST: "${CDN_ENDPOINT}",`);
+		if (GATEWAY_ENDPOINT) html = html.replace(/GATEWAY_ENDPOINT: .+/, `GATEWAY_ENDPOINT: "${GATEWAY_ENDPOINT}",`);
+
+		res.send(html);
+	});
+}