From 38326fc45235a11730c226a66d3da6f4c6b9f9aa Mon Sep 17 00:00:00 2001 From: Sainan <63328889+Sainan@users.noreply.github.com> Date: Sun, 2 Nov 2025 23:27:38 -0800 Subject: [PATCH] chore: avoid using sendFile as it may spontaneously fail (#2977) Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/2977 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com> --- src/routes/webui.ts | 61 +++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/src/routes/webui.ts b/src/routes/webui.ts index 6b6386ed..ea9f4b80 100644 --- a/src/routes/webui.ts +++ b/src/routes/webui.ts @@ -1,5 +1,6 @@ import express from "express"; import path from "path"; +import fs from "fs/promises"; import { repoDir, rootDir } from "../helpers/pathHelper.ts"; import { args } from "../helpers/commandLineArguments.ts"; @@ -21,50 +22,62 @@ webuiRouter.use("/webui", (req, res, next) => { }); // Serve virtual routes -webuiRouter.get("/webui/inventory", (_req, res) => { - res.sendFile(path.join(baseDir, "static/webui/index.html")); +webuiRouter.get("/webui/inventory", async (_req, res) => { + res.set("Content-Type", "text/html;charset=utf8"); + res.send(await fs.readFile(path.join(baseDir, "static/webui/index.html"))); }); -webuiRouter.get("/webui/detailedView", (_req, res) => { - res.sendFile(path.join(baseDir, "static/webui/index.html")); +webuiRouter.get("/webui/detailedView", async (_req, res) => { + res.set("Content-Type", "text/html;charset=utf8"); + res.send(await fs.readFile(path.join(baseDir, "static/webui/index.html"))); }); -webuiRouter.get("/webui/mods", (_req, res) => { - res.sendFile(path.join(baseDir, "static/webui/index.html")); +webuiRouter.get("/webui/mods", async (_req, res) => { + res.set("Content-Type", "text/html;charset=utf8"); + res.send(await fs.readFile(path.join(baseDir, "static/webui/index.html"))); }); -webuiRouter.get("/webui/settings", (_req, res) => { - res.sendFile(path.join(baseDir, "static/webui/index.html")); +webuiRouter.get("/webui/settings", async (_req, res) => { + res.set("Content-Type", "text/html;charset=utf8"); + res.send(await fs.readFile(path.join(baseDir, "static/webui/index.html"))); }); -webuiRouter.get("/webui/quests", (_req, res) => { - res.sendFile(path.join(baseDir, "static/webui/index.html")); +webuiRouter.get("/webui/quests", async (_req, res) => { + res.set("Content-Type", "text/html;charset=utf8"); + res.send(await fs.readFile(path.join(baseDir, "static/webui/index.html"))); }); -webuiRouter.get("/webui/cheats", (_req, res) => { - res.sendFile(path.join(baseDir, "static/webui/index.html")); +webuiRouter.get("/webui/cheats", async (_req, res) => { + res.set("Content-Type", "text/html;charset=utf8"); + res.send(await fs.readFile(path.join(baseDir, "static/webui/index.html"))); }); -webuiRouter.get("/webui/import", (_req, res) => { - res.sendFile(path.join(baseDir, "static/webui/index.html")); +webuiRouter.get("/webui/import", async (_req, res) => { + res.set("Content-Type", "text/html;charset=utf8"); + res.send(await fs.readFile(path.join(baseDir, "static/webui/index.html"))); }); -webuiRouter.get("/webui/guildView", (_req, res) => { - res.sendFile(path.join(baseDir, "static/webui/index.html")); +webuiRouter.get("/webui/guildView", async (_req, res) => { + res.set("Content-Type", "text/html;charset=utf8"); + res.send(await fs.readFile(path.join(baseDir, "static/webui/index.html"))); }); // Serve static files webuiRouter.use("/webui", express.static(path.join(baseDir, "static/webui"))); // Serve favicon -webuiRouter.get("/favicon.ico", (_req, res) => { - res.sendFile(path.join(repoDir, "static/fixed_responses/favicon.ico")); +webuiRouter.get("/favicon.ico", async (_req, res) => { + res.set("Content-Type", "image/vnd.microsoft.icon"); + res.send(await fs.readFile(path.join(repoDir, "static/fixed_responses/favicon.ico"))); }); // Serve warframe-riven-info -webuiRouter.get("/webui/riven-tool/", (_req, res) => { - res.sendFile(path.join(repoDir, "node_modules/warframe-riven-info/index.html")); +webuiRouter.get("/webui/riven-tool/", async (_req, res) => { + res.set("Content-Type", "text/html;charset=utf8"); + res.send(await fs.readFile(path.join(repoDir, "node_modules/warframe-riven-info/index.html"))); }); -webuiRouter.get("/webui/riven-tool/RivenParser.js", (_req, res) => { - res.sendFile(path.join(repoDir, "node_modules/warframe-riven-info/RivenParser.js")); +webuiRouter.get("/webui/riven-tool/RivenParser.js", async (_req, res) => { + res.set("Content-Type", "text/javascript;charset=utf8"); + res.send(await fs.readFile(path.join(repoDir, "node_modules/warframe-riven-info/RivenParser.js"))); }); // Serve translations -webuiRouter.get("/translations/:file", (req, res) => { - res.sendFile(path.join(baseDir, `static/webui/translations/${req.params.file}`)); +webuiRouter.get("/translations/:file", async (req, res) => { + res.set("Content-Type", "text/javascript;charset=utf8"); + res.send(await fs.readFile(path.join(baseDir, `static/webui/translations/${req.params.file}`))); }); export { webuiRouter };