-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.mjs
More file actions
96 lines (83 loc) · 2.25 KB
/
Copy pathserver.mjs
File metadata and controls
96 lines (83 loc) · 2.25 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
import http from "node:http";
import { performance } from "node:perf_hooks";
import process from "node:process";
import handler from "serve-handler";
const port = Number(process.env.PORT) || 3000;
function log(level, msg, fields = {}) {
process.stdout.write(
`${JSON.stringify({ ts: new Date().toISOString(), level, msg, ...fields })}\n`,
);
}
function headerValue(headers, name) {
const value = headers[name];
if (typeof value === "string") {
return value;
}
if (Array.isArray(value) && value.length > 0) {
return value[0];
}
return undefined;
}
function normalizeIp(ip) {
if (!ip) {
return undefined;
}
const trimmed = ip.trim().replace(/^\[|\]$/g, "");
if (trimmed.startsWith("::ffff:")) {
return trimmed.slice(7);
}
return trimmed || undefined;
}
function clientIp(req) {
const forwardedFor = headerValue(req.headers, "x-forwarded-for");
if (forwardedFor) {
return normalizeIp(forwardedFor.split(",")[0]);
}
const realIp = headerValue(req.headers, "x-real-ip");
if (realIp) {
return normalizeIp(realIp);
}
const forwarded = headerValue(req.headers, "forwarded");
if (forwarded) {
const match = forwarded.match(/for=(?:"?\[?)([^\]";,]+)/i);
if (match) {
return normalizeIp(match[1]);
}
}
return normalizeIp(req.socket.remoteAddress);
}
const server = http.createServer(async (req, res) => {
const started = performance.now();
res.on("finish", () => {
log("info", "request", {
method: req.method,
url: req.url,
status: res.statusCode,
duration_ms: Math.round(performance.now() - started),
ip: clientIp(req),
});
});
try {
await handler(req, res, {
public: "dist",
rewrites: [{ source: "**", destination: "/index.html" }],
});
} catch (err) {
log("error", "request_handler_failed", {
method: req.method,
url: req.url,
ip: clientIp(req),
err: err instanceof Error ? err.message : String(err),
});
if (!res.headersSent) {
res.writeHead(500, { "Content-Type": "text/plain" });
res.end("Internal Server Error");
}
}
});
server.on("error", (err) => {
log("error", "server_error", { err: err.message });
});
server.listen(port, () => {
log("info", "listening", { port });
});