aboutsummaryrefslogtreecommitdiffstats
path: root/server.js
diff options
context:
space:
mode:
Diffstat (limited to 'server.js')
-rw-r--r--server.js72
1 files changed, 27 insertions, 45 deletions
diff --git a/server.js b/server.js
index 2af5649..090d122 100644
--- a/server.js
+++ b/server.js
@@ -2,7 +2,6 @@ import { readFileSync, statSync } from "node:fs";
import { createSecureServer, constants as httpConstants } from "node:http2";
import path from "node:path";
-// Environment variables with fallbacks
const port = process.env.PORT ?? process.argv[2] ?? 8433;
const serverRoot = process.env.SERVER_ROOT ?? "./app";
const keyPath = process.env.KEY_PATH ?? "./server.key";
@@ -10,27 +9,23 @@ const certPath = process.env.CERT_PATH ?? "./server.pem";
const hstsMaxAge = process.env.HSTS_MAX_AGE ?? "31536000";
const corsOrigin = process.env.CORS_ORIGIN ?? "*";
-/**
- * @type {Map<string, string>}
- * Maps file extensions to MIME types for proper content-type headers
- */
-const mimeType = new Map([
- [".ico", "image/x-icon"],
- [".html", "text/html"],
- [".js", "text/javascript"],
- [".json", "application/json"],
- [".css", "text/css"],
- [".png", "image/png"],
- [".jpg", "image/jpeg"],
- [".wav", "audio/wav"],
- [".mp3", "audio/mpeg"],
- [".svg", "image/svg+xml"],
- [".pdf", "application/pdf"],
- [".zip", "application/zip"],
- [".doc", "application/msword"],
- [".eot", "application/vnd.ms-fontobject"],
- [".ttf", "application/x-font-ttf"],
-]);
+const mimeType = {
+ ".css": "text/css",
+ ".doc": "application/msword",
+ ".eot": "application/vnd.ms-fontobject",
+ ".html": "text/html",
+ ".ico": "image/x-icon",
+ ".jpg": "image/jpeg",
+ ".js": "text/javascript",
+ ".json": "application/json",
+ ".mp3": "audio/mpeg",
+ ".pdf": "application/pdf",
+ ".png": "image/png",
+ ".svg": "image/svg+xml",
+ ".ttf": "application/x-font-ttf",
+ ".wav": "audio/wav",
+ ".zip": "application/zip",
+};
const {
HTTP2_HEADER_PATH: HEADER_PATH,
@@ -42,15 +37,15 @@ const {
const HEADER_ORIGIN = "origin";
/**
- * @type {import('node:http2').SecureServerOptions}
- * TLS options for the HTTP/2 server
+ * HTTP/2 secure server instance
+ * @type {import('node:http2').Http2SecureServer}
*/
-let options;
+let server;
try {
- options = {
+ server = createSecureServer({
cert: readFileSync(certPath),
key: readFileSync(keyPath),
- };
+ });
} catch (error) {
if (error && "code" in error && error.code === "ENOENT") {
console.error(`Certificate error: Could not find key or cert files at ${keyPath}, ${certPath}`);
@@ -61,12 +56,6 @@ try {
}
/**
- * HTTP/2 secure server instance
- * @type {import('node:http2').Http2SecureServer}
- */
-const server = createSecureServer(options);
-
-/**
* Handles stream errors and sends appropriate HTTP responses
* @param {unknown} err - The error object
* @param {import('node:http2').ServerHttp2Stream} stream - The HTTP/2 stream to respond on
@@ -116,11 +105,8 @@ const getResponseHeaders = (mimeType, ro) => {
* @param {number} flags - Stream flags
*/
server.on("stream", (stream, headers) => {
- /** @type {string} */
const reqPath = headers[HEADER_PATH] || "/";
- /** @type {string} */
const reqMethod = headers[HEADER_METHOD] || "GET";
- /** @type {string | undefined} */
const requestOrigin =
typeof headers[HEADER_ORIGIN] === "string" ? headers[HEADER_ORIGIN] : undefined;
@@ -159,16 +145,12 @@ server.on("stream", (stream, headers) => {
}
const ext = path.extname(fullPath);
- const responseMimeType = mimeType.get(ext) || "text/plain";
+ const responseMimeType = mimeType[ext] || "text/plain";
stream.respondWithFile(
fullPath,
- {
- ...getResponseHeaders(responseMimeType, requestOrigin),
- },
- {
- onError: (err) => handleStreamError(err, stream, requestOrigin),
- },
+ { ...getResponseHeaders(responseMimeType, requestOrigin) },
+ { onError: (err) => handleStreamError(err, stream, requestOrigin) },
);
});
@@ -192,7 +174,7 @@ const gracefulShutdown = (signal) => {
setTimeout(() => {
console.error("Forcing server closure after timeout");
process.exit(1);
- }, 10000);
+ }, 2000);
};
process.on("SIGTERM", () => gracefulShutdown("SIGTERM"));
@@ -220,7 +202,7 @@ process.on("unhandledRejection", (reason, promise) => {
/**
* Starts the HTTP/2 server and begins listening for connections
*/
-server.listen(Number.parseInt(port.toString(), 10), "0.0.0.0", () => {
+server.listen(parseInt(port, 10), "localhost", () => {
console.log(`
Server running on https://localhost:${port}
Environment: ${process.env.NODE_ENV || "development"}