summary refs log tree commit diff
path: root/src/routes/assets/index.ts
blob: c2b9f2b04ff7b896a2a518112e48716122942e4f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
 * * patch to redirect requests from cloned client
 * (../../client/index.html)
 */
import { Router } from "express";
import fetch from "node-fetch";

const router = Router();
const cache = new Map();
const assetEndpoint = "https://discord.com/assets/";

export async function getCache(key: string): Promise<Response> {
	let cachedRessource = cache.get(key);

	if (!cachedRessource) {
		const res = await fetch(assetEndpoint + key);
		// @ts-ignore
		res.bufferResponse = await res.buffer();
		cache.set(key, res);
		cachedRessource = res;
	}

	return cachedRessource;
}

router.get("/:hash", async (req, res) => {
	res.set("Cache-Control", "public, max-age=" + 60 * 60 * 24);
	const cache = await getCache(req.params.hash);
	res.set("content-type", <string>cache.headers.get("content-type"));
	// @ts-ignore
	res.send(cache.bufferResponse);
});

export default router;