summary refs log tree commit diff
path: root/src/Server.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/Server.ts')
-rw-r--r--src/Server.ts52
1 files changed, 37 insertions, 15 deletions
diff --git a/src/Server.ts b/src/Server.ts

index b1fe3c90..5ae65918 100644 --- a/src/Server.ts +++ b/src/Server.ts
@@ -9,8 +9,8 @@ import i18nextMiddleware, { I18next } from "i18next-http-middleware"; import i18nextBackend from "i18next-node-fs-backend"; import { ErrorHandler } from "./middlewares/ErrorHandler"; import { BodyParser } from "./middlewares/BodyParser"; -import express, { Router } from "express"; -import fetch from "node-fetch"; +import express, { Router, Request, Response } from "express"; +import fetch, { Response as FetchResponse } from "node-fetch"; import mongoose from "mongoose"; import path from "path"; @@ -28,8 +28,16 @@ declare global { } } +const assetCache = new Map< + string, + { + response: FetchResponse; + buffer: Buffer; + } +>(); + export class FosscordServer extends Server { - public options: FosscordServerOptions; + public declare options: FosscordServerOptions; constructor(opts?: Partial<FosscordServerOptions>) { // @ts-ignore @@ -60,7 +68,7 @@ export class FosscordServer extends Server { this.app.use(GlobalRateLimit); this.app.use(Authentication); this.app.use(CORS); - this.app.use(BodyParser({ inflate: true })); + this.app.use(BodyParser({ inflate: true, limit: 1024 * 1024 * 2 })); const languages = await fs.readdir(path.join(__dirname, "..", "locales")); const namespaces = await fs.readdir(path.join(__dirname, "..", "locales", "en")); const ns = namespaces.filter((x) => x.endsWith(".json")).map((x) => x.slice(0, x.length - 5)); @@ -89,19 +97,27 @@ export class FosscordServer extends Server { app.use("/api/v8", prefix); this.app = app; this.app.use(ErrorHandler); - const indexHTML = await fs.readFile(path.join(__dirname, "..", "client_test", "index.html")); + const indexHTML = await fs.readFile(path.join(__dirname, "..", "client_test", "index.html"), { encoding: "utf8" }); this.app.use("/assets", express.static(path.join(__dirname, "..", "assets"))); - this.app.get("/assets/:file", async (req, res) => { + this.app.get("/assets/:file", async (req: Request, res: Response) => { delete req.headers.host; - const response = await fetch(`https://discord.com/assets/${req.params.file}`, { - // @ts-ignore - headers: { - ...req.headers - } - }); - const buffer = await response.buffer(); + 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 ( @@ -120,13 +136,19 @@ export class FosscordServer extends Server { } res.set(name, value); }); + assetCache.set(req.params.file, { buffer, response }); return res.send(buffer); }); - this.app.get("*", (req, res) => { + this.app.get("*", (req: Request, res: Response) => { res.set("Cache-Control", "public, max-age=" + 60 * 60 * 24); res.set("content-type", "text/html"); - res.send(indexHTML); + res.send( + indexHTML.replace( + /CDN_HOST: ".+"/, + `CDN_HOST: "${(Config.get().cdn.endpoint || "http://localhost:3003").replace(/https?:/, "")}"` + ) + ); }); return super.start(); }