This repository has been archived by the owner on Aug 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.mjs
65 lines (63 loc) · 1.83 KB
/
index.mjs
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import http from "http";
import fs from "fs";
import path from "path";
const files = fs.readdirSync("assets").map((file) => `/${file}`);
http
.createServer((request, response) => {
const URL = (request.url ?? "").replace("/koj/image/upload", "");
if (files.includes(URL)) {
response.setHeader(
"Cache-Control",
"public, max-age=31536000, immutable"
);
response.setHeader("Server", "KojCDN");
response.setHeader(
"Strict-Transport-Security",
"max-age=63072000; includeSubDomains; preload"
);
return fs.createReadStream(path.join(".", "assets", URL)).pipe(response);
}
if (URL === "/favicon.ico") {
response.writeHead(302, {
Location: "https://koj.co/favicon.ico",
});
return response.end();
}
if (URL === "/") {
response.writeHead(200, {
"Content-Type": "application/json",
});
return response.end("{ ok: true }");
}
let result = [];
var options = {
host: "res.cloudinary.com",
port: 80,
path: `/koj/image/upload${URL}`,
};
http
.get(options, (res) => {
res.on("data", (chunk) => {
result.push(chunk);
});
res.on("end", () => {
Object.keys(res.headers).forEach((header) => {
response.setHeader(header, res.headers[header]);
});
response.setHeader(
"Cache-Control",
"public, max-age=31536000, immutable"
);
response.setHeader("Server", "KojCDN");
response.setHeader(
"Strict-Transport-Security",
"max-age=63072000; includeSubDomains; preload"
);
response.write(Buffer.concat(result));
result = [];
response.end();
});
})
.on("error", () => {});
})
.listen(80);