mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 19:46:43 +00:00
74b5affa75
## Description: Update from Express 4.22 > 5.2.1. And @types/express 4.17 > 5.0.6. ### CodeQL errors unjustified: Please dismiss the unjustified [CodeQL scanning results](https://github.com/openfrontio/OpenFrontIO/security/code-scanning?query=pr%3A3549+tool%3ACodeQL+is%3Aopen) for playground/server.ts: they were flagged for this PR but i didn't touch those specific parts of the file. More importantly: it is a test server, created by Mole/ @Aareksio. I made requests to dismiss the alerts but don't have the permissions to actually dismiss them myself Version 5 was the first major upgrade in 10 years when it was released in Sept 2024. 5.21 is from Dec 2025 so v5 teething problems should be over by now. Many of its dependencies also updated by some major versions. So it seems a worthy update but that is for you to decide. v4 will be EOL when v6 arrives, however that could be a year from now still maybe. - Migration: -- Updated package.json, ran `npm install "express@5"` and `npm install "@types/express@5.0.6"`. -- Used https://expressjs.com/en/guide/migrating-5.html -- Ran the codemods from the migration guide `npx codemod@latest @expressjs/v5-migration-recipe`. -- Checked manually. -- Checked again with help of Gemini 3.1 Pro based on same guide. -- Master.ts: use `*splat` instead of `*`, tested and going to non-existing URL lands back on index.html like it should. -- Worker.ts: MIME type _webp_ is now supported natively so remove added config. -- playground/server.ts: fix type error after upgrading types/express for `name` in `req.params`. And `app.listen` handles user provided callback on error, use that. The latter may not be not needed per se. -- While v5 does this now "When an error is thrown in an async function or a rejected promise is awaited inside an async function, those errors will be passed to the error handler as if calling next(err).", choose to leave our try/catch'es be. Since we use specific errors, probably easier for consistency in log searches and user reporting. - About performance: -- While [Express 5 seems a bit slower than 4](https://www.repoflow.io/blog/express-4-vs-express-5-benchmark-node-18-24), it is not by much especially on Node24 which we're on. Also there's a working group dedicated to improving Express performance, albeit they expect v6/7 to benefit from that more than v5 will. -- While there are faster alternatives in benchmarks, [in real-world usage Express still holds up as one of the best and even beats most 'faster' alternatives](https://medium.com/deno-the-complete-reference/node-js-the-fastest-web-framework-in-2025-static-file-server-case-1df462ad38cd). ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file - [x] I have added relevant tests to the test directory - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced ## Please put your Discord username so you can be contacted if a bug or regression is found: tryout33
170 lines
4.4 KiB
TypeScript
170 lines
4.4 KiB
TypeScript
import cluster from "cluster";
|
|
import crypto from "crypto";
|
|
import express from "express";
|
|
import rateLimit from "express-rate-limit";
|
|
import http from "http";
|
|
import path from "path";
|
|
import { fileURLToPath } from "url";
|
|
import { GameEnv } from "../core/configuration/Config";
|
|
import { getServerConfigFromServer } from "../core/configuration/ConfigLoader";
|
|
import { logger } from "./Logger";
|
|
import { MapPlaylist } from "./MapPlaylist";
|
|
import { MasterLobbyService } from "./MasterLobbyService";
|
|
import { setNoStoreHeaders } from "./NoStoreHeaders";
|
|
import { renderAppShell } from "./RenderHtml";
|
|
import { applyStaticAssetCacheControl } from "./StaticAssetCache";
|
|
|
|
const config = getServerConfigFromServer();
|
|
const playlist = new MapPlaylist();
|
|
let lobbyService: MasterLobbyService;
|
|
|
|
const app = express();
|
|
const server = http.createServer(app);
|
|
|
|
const log = logger.child({ comp: "m" });
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
app.use(express.json());
|
|
|
|
// Serve the shared app shell for the root document.
|
|
app.use(async (req, res, next) => {
|
|
if (req.path === "/") {
|
|
try {
|
|
await renderAppShell(
|
|
res,
|
|
path.join(__dirname, "../../static/index.html"),
|
|
);
|
|
} catch (error) {
|
|
log.error("Error rendering index.html:", error);
|
|
res.status(500).send("Internal Server Error");
|
|
}
|
|
} else {
|
|
next();
|
|
}
|
|
});
|
|
|
|
app.use(
|
|
express.static(path.join(__dirname, "../../static"), {
|
|
maxAge: "1y", // Set max-age to 1 year for all static assets
|
|
setHeaders: (res) => {
|
|
applyStaticAssetCacheControl(
|
|
res.setHeader.bind(res),
|
|
res.req.originalUrl,
|
|
);
|
|
},
|
|
}),
|
|
);
|
|
|
|
app.set("trust proxy", 3);
|
|
app.use(
|
|
rateLimit({
|
|
windowMs: 1000, // 1 second
|
|
max: 20, // 20 requests per IP per second
|
|
}),
|
|
);
|
|
|
|
app.use("/api", (_req, res, next) => {
|
|
setNoStoreHeaders(res);
|
|
next();
|
|
});
|
|
|
|
// Start the master process
|
|
export async function startMaster() {
|
|
if (!cluster.isPrimary) {
|
|
throw new Error(
|
|
"startMaster() should only be called in the primary process",
|
|
);
|
|
}
|
|
|
|
log.info(`Primary ${process.pid} is running`);
|
|
log.info(`Setting up ${config.numWorkers()} workers...`);
|
|
|
|
lobbyService = new MasterLobbyService(config, playlist, log);
|
|
|
|
// Generate admin token for worker authentication
|
|
const ADMIN_TOKEN = crypto.randomBytes(16).toString("hex");
|
|
process.env.ADMIN_TOKEN = ADMIN_TOKEN;
|
|
|
|
const INSTANCE_ID =
|
|
config.env() === GameEnv.Dev
|
|
? "DEV_ID"
|
|
: crypto.randomBytes(4).toString("hex");
|
|
process.env.INSTANCE_ID = INSTANCE_ID;
|
|
|
|
log.info(`Instance ID: ${INSTANCE_ID}`);
|
|
|
|
// Fork workers
|
|
for (let i = 0; i < config.numWorkers(); i++) {
|
|
const worker = cluster.fork({
|
|
WORKER_ID: i,
|
|
ADMIN_TOKEN,
|
|
INSTANCE_ID,
|
|
});
|
|
|
|
lobbyService.registerWorker(i, worker);
|
|
log.info(`Started worker ${i} (PID: ${worker.process.pid})`);
|
|
}
|
|
|
|
// Handle worker crashes
|
|
cluster.on("exit", (worker, code, signal) => {
|
|
const workerId = (worker as any).process?.env?.WORKER_ID;
|
|
if (workerId === undefined) {
|
|
log.error(`worker crashed could not find id`);
|
|
return;
|
|
}
|
|
|
|
const workerIdNum = parseInt(workerId);
|
|
lobbyService.removeWorker(workerIdNum);
|
|
|
|
log.warn(
|
|
`Worker ${workerId} (PID: ${worker.process.pid}) died with code: ${code} and signal: ${signal}`,
|
|
);
|
|
log.info(`Restarting worker ${workerId}...`);
|
|
|
|
// Restart the worker with the same ID
|
|
const newWorker = cluster.fork({
|
|
WORKER_ID: workerId,
|
|
ADMIN_TOKEN,
|
|
INSTANCE_ID,
|
|
});
|
|
|
|
lobbyService.registerWorker(workerIdNum, newWorker);
|
|
log.info(
|
|
`Restarted worker ${workerId} (New PID: ${newWorker.process.pid})`,
|
|
);
|
|
});
|
|
|
|
const PORT = 3000;
|
|
server.listen(PORT, () => {
|
|
log.info(`Master HTTP server listening on port ${PORT}`);
|
|
});
|
|
}
|
|
|
|
app.get("/api/health", (_req, res) => {
|
|
const ready = lobbyService?.isHealthy() ?? false;
|
|
if (ready) {
|
|
res.json({ status: "ok" });
|
|
} else {
|
|
res.status(503).json({ status: "unavailable" });
|
|
}
|
|
});
|
|
|
|
app.get("/api/instance", (_req, res) => {
|
|
res.json({
|
|
instanceId: process.env.INSTANCE_ID ?? "undefined",
|
|
});
|
|
});
|
|
|
|
// SPA fallback route
|
|
app.get("/{*splat}", async function (_req, res) {
|
|
try {
|
|
const htmlPath = path.join(__dirname, "../../static/index.html");
|
|
await renderAppShell(res, htmlPath);
|
|
} catch (error) {
|
|
log.error("Error rendering SPA fallback:", error);
|
|
res.status(500).send("Internal Server Error");
|
|
}
|
|
});
|