Keep the Node server alive on invalid requests and Cards failures

An invalid URL or Host header rejected inside the async request handler and
ended the process, even with Sokko TCG disabled. Requests now parse against a
fixed base and any unexpected failure answers 500. An invalid Cards setup
disables the game instead of the site, SIGTERM closes the database cleanly,
and the rate limiter forgets past minutes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Shinuwa 2026-11-05 17:52:18 +01:00
parent 5ce35cd7ba
commit d72dd38317
4 changed files with 202 additions and 17 deletions

View file

@ -67,7 +67,7 @@ function getCacheControl(root, file) {
}
export function createStaticServer({ root = defaultRoot, publicRoot = defaultPublicRoot, cardsApi = null } = {}) {
const server = createServer(async (req, res) => {
async function handleRequest(req, res) {
if (cardsApi && await cardsApi(req, res)) return;
const file = resolvePath(req.url || "/", root, publicRoot);
if (!file || !existsSync(file)) {
@ -83,16 +83,40 @@ export function createStaticServer({ root = defaultRoot, publicRoot = defaultPub
"Content-Type": types[extname(file)] || "application/octet-stream",
"Cache-Control": getCacheControl(root, file)
});
createReadStream(file).pipe(res);
// Un fichier remplacé pendant une livraison ne doit pas lever d'erreur non gérée.
createReadStream(file).on("error", () => res.destroy()).pipe(res);
}
// Une promesse rejetée arrêterait tout le process : aucune requête ne doit pouvoir le faire.
const server = createServer((req, res) => {
handleRequest(req, res).catch((error) => {
console.error(error);
if (res.headersSent) {
res.destroy();
return;
}
res.writeHead(500, { "Content-Type": "text/plain; charset=utf-8", "Cache-Control": "no-store" });
res.end("Internal server error");
});
});
if (cardsApi?.close) server.once("close", cardsApi.close);
return server;
}
// Une configuration Sokko TCG invalide désactive le jeu sans priver le site de ses toolboxes et guides.
export function createCardsApi(options = {}) {
try {
return createCardsApiHandler({ projectRoot, ...options });
} catch (error) {
console.error("Sokko TCG is disabled because its configuration is invalid:", error);
return createCardsApiHandler({ projectRoot, ...options, enabled: false });
}
}
export function startServer({ cardsApiOptions = {} } = {}) {
loadProjectEnv();
const port = Number(process.env.PORT || 5173);
const cardsApi = createCardsApiHandler({ projectRoot, ...cardsApiOptions });
const cardsApi = createCardsApi(cardsApiOptions);
const server = createStaticServer({ cardsApi });
server.on("error", (error) => {
@ -111,4 +135,15 @@ export function startServer({ cardsApiOptions = {} } = {}) {
return server;
}
if (process.argv[1] && resolve(process.argv[1]) === modulePath) startServer();
// Ferme la base à l'arrêt du service pour vider son journal WAL ; les requêtes en cours disposent d'un court délai.
function stopOnSignal(server) {
const stop = () => {
server.close(() => process.exit(0));
server.closeIdleConnections();
setTimeout(() => server.closeAllConnections(), 3000).unref();
};
process.once("SIGTERM", stop);
process.once("SIGINT", stop);
}
if (process.argv[1] && resolve(process.argv[1]) === modulePath) stopOnSignal(startServer());