-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
140 lines (121 loc) · 4.03 KB
/
Copy pathserver.js
File metadata and controls
140 lines (121 loc) · 4.03 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { createReadStream } from "node:fs";
import { access, stat } from "node:fs/promises";
import { createServer } from "node:http";
import { extname, resolve, sep } from "node:path";
import { fileURLToPath } from "node:url";
const root = fileURLToPath(new URL(".", import.meta.url));
const port = Number.parseInt(process.env.PORT ?? "3000", 10);
const host = process.env.HOST ?? "0.0.0.0";
const homePage = "Swiss-Invest v2.dc.html";
const publicFiles = new Set([
homePage,
"support.js",
"assets/logo-dark.svg",
"assets/logo-light.svg",
"node_modules/react/umd/react.production.min.js",
"node_modules/react-dom/umd/react-dom.production.min.js"
]);
const contentTypes = {
".css": "text/css; charset=utf-8",
".html": "text/html; charset=utf-8",
".js": "text/javascript; charset=utf-8",
".json": "application/json; charset=utf-8",
".svg": "image/svg+xml",
".webp": "image/webp"
};
function setSecurityHeaders(response) {
response.setHeader("X-Content-Type-Options", "nosniff");
response.setHeader("X-Frame-Options", "SAMEORIGIN");
response.setHeader("Referrer-Policy", "strict-origin-when-cross-origin");
response.setHeader(
"Content-Security-Policy",
[
"default-src 'self'",
"script-src 'self' 'unsafe-eval'",
"style-src 'self' 'unsafe-inline' https://fonts.googleapis.com",
"font-src 'self' https://fonts.gstatic.com",
"img-src 'self' data:",
"connect-src 'self'",
"frame-ancestors 'self'",
"base-uri 'self'",
"form-action 'self'"
].join("; ")
);
}
function sendText(response, status, body, contentType = "text/plain; charset=utf-8") {
response.writeHead(status, {
"Content-Type": contentType,
"Content-Length": Buffer.byteLength(body)
});
response.end(body);
}
async function sendFile(response, relativePath) {
if (!publicFiles.has(relativePath)) {
sendText(response, 404, "Not found\n");
return;
}
const absolutePath = resolve(root, relativePath);
if (!absolutePath.startsWith(`${resolve(root)}${sep}`)) {
sendText(response, 403, "Forbidden\n");
return;
}
try {
await access(absolutePath);
const details = await stat(absolutePath);
const extension = extname(absolutePath).toLowerCase();
response.writeHead(200, {
"Content-Type": contentTypes[extension] ?? "application/octet-stream",
"Content-Length": details.size,
"Cache-Control":
extension === ".html" ? "no-cache" : "public, max-age=604800"
});
createReadStream(absolutePath).pipe(response);
} catch {
sendText(response, 404, "Not found\n");
}
}
const server = createServer(async (request, response) => {
setSecurityHeaders(response);
if (!request.url || !["GET", "HEAD"].includes(request.method ?? "")) {
sendText(response, 405, "Method not allowed\n");
return;
}
const pathname = decodeURIComponent(new URL(request.url, "http://localhost").pathname);
if (pathname === "/health" || pathname === "/healthz") {
sendText(
response,
200,
JSON.stringify({ status: "ok", node: process.version }),
"application/json; charset=utf-8"
);
return;
}
const routes = {
"/": homePage,
"/index.html": homePage,
"/support.js": "support.js",
"/assets/logo-dark.svg": "assets/logo-dark.svg",
"/assets/logo-light.svg": "assets/logo-light.svg",
"/node_modules/react/umd/react.production.min.js":
"node_modules/react/umd/react.production.min.js",
"/node_modules/react-dom/umd/react-dom.production.min.js":
"node_modules/react-dom/umd/react-dom.production.min.js"
};
const file = routes[pathname];
if (!file) {
sendText(response, 404, "Not found\n");
return;
}
await sendFile(response, file);
});
server.listen(port, host, () => {
console.log(`XR-SWISSINVEST listening on http://${host}:${port}`);
});
function shutdown(signal) {
console.log(`${signal} received; shutting down`);
server.close((error) => {
process.exitCode = error ? 1 : 0;
});
}
process.on("SIGTERM", () => shutdown("SIGTERM"));
process.on("SIGINT", () => shutdown("SIGINT"));