Files
OpenFrontIO/tests/pathfinding/playground/server.ts
VariableVince 74b5affa75 Chore(deps): Migrate Express 4 > 5 (#3549)
## 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
2026-03-31 12:35:40 -07:00

261 lines
6.8 KiB
TypeScript

import compression from "compression";
import express, { Request, Response } from "express";
import { dirname, join } from "path";
import { fileURLToPath } from "url";
import {
clearCache as clearMapCache,
getMapMetadata,
listMaps,
} from "./api/maps.js";
import { clearAdapterCaches, computePath } from "./api/pathfinding.js";
import { computeSpatialQuery } from "./api/spatialQuery.js";
const app = express();
const PORT = process.env.PORT ?? 5555;
// Middleware
app.use(compression()); // gzip compression for large responses
app.use(express.json({ limit: "50mb" })); // JSON body parser with larger limit
// Serve static files from public directory
const publicDir = join(dirname(fileURLToPath(import.meta.url)), "public");
app.use(express.static(publicDir));
// API Routes
/**
* GET /api/maps
* List all available maps
*/
app.get("/api/maps", (req: Request, res: Response) => {
try {
const maps = listMaps();
res.json({ maps });
} catch (error) {
console.error("Error listing maps:", error);
res.status(500).json({
error: "Failed to list maps",
message: error instanceof Error ? error.message : String(error),
});
}
});
/**
* GET /api/maps/:name
* Get map metadata (map data, dimensions)
*/
app.get(
"/api/maps/:name",
async (req: Request<{ name: string }>, res: Response) => {
try {
const { name } = req.params;
const metadata = await getMapMetadata(name);
res.json(metadata);
} catch (error) {
console.error(`Error loading map ${req.params.name}:`, error);
if (error instanceof Error && error.message.includes("ENOENT")) {
res.status(404).json({
error: "Map not found",
message: `Map "${req.params.name}" does not exist`,
});
} else {
res.status(500).json({
error: "Failed to load map",
message: error instanceof Error ? error.message : String(error),
});
}
}
},
);
/**
* GET /api/maps/:name/thumbnail
* Get map thumbnail image
*/
app.get(
"/api/maps/:name/thumbnail",
(req: Request<{ name: string }>, res: Response) => {
try {
const { name } = req.params;
const thumbnailPath = join(
dirname(fileURLToPath(import.meta.url)),
"../../../resources/maps",
name,
"thumbnail.webp",
);
res.sendFile(thumbnailPath);
} catch (error) {
console.error(`Error loading thumbnail for ${req.params.name}:`, error);
res.status(404).json({
error: "Thumbnail not found",
message: error instanceof Error ? error.message : String(error),
});
}
},
);
/**
* POST /api/pathfind
* Compute pathfinding between two points
*
* Request body:
* {
* map: string,
* from: [x, y],
* to: [x, y],
* adapters?: string[] // Optional: which comparison adapters to run
* }
*
* Response:
* {
* primary: { path, length, time, debug: { nodePath, initialPath, timings } },
* comparisons: [{ adapter, path, length, time }, ...]
* }
*/
app.post("/api/pathfind", async (req: Request, res: Response) => {
try {
const { map, from, to, adapters } = req.body;
// Validate request
if (!map || !from || !to) {
return res.status(400).json({
error: "Invalid request",
message: "Missing required fields: map, from, to",
});
}
if (
!Array.isArray(from) ||
from.length !== 2 ||
!Array.isArray(to) ||
to.length !== 2
) {
return res.status(400).json({
error: "Invalid coordinates",
message: "from and to must be [x, y] coordinate arrays",
});
}
// Compute paths
const result = await computePath(
map,
from as [number, number],
to as [number, number],
{ adapters },
);
res.json(result);
} catch (error) {
console.error("Error computing path:", error);
if (error instanceof Error && error.message.includes("is not water")) {
res.status(400).json({
error: "Invalid coordinates",
message: error.message,
});
} else {
res.status(500).json({
error: "Failed to compute path",
message: error instanceof Error ? error.message : String(error),
});
}
}
});
/**
* POST /api/spatial-query
* Compute spatial query for transport ship (closestShoreByWater)
*
* Request body:
* {
* map: string,
* ownedTiles: number[], // Array of tile indices (y * width + x)
* target: [x, y]
* }
*/
app.post("/api/spatial-query", async (req: Request, res: Response) => {
try {
const { map, ownedTiles, target } = req.body;
if (!map || !ownedTiles || !target) {
return res.status(400).json({
error: "Invalid request",
message: "Missing required fields: map, ownedTiles, target",
});
}
if (!Array.isArray(ownedTiles)) {
return res.status(400).json({
error: "Invalid ownedTiles",
message: "ownedTiles must be an array of tile indices",
});
}
if (!Array.isArray(target) || target.length !== 2) {
return res.status(400).json({
error: "Invalid target",
message: "target must be [x, y] coordinate array",
});
}
const result = await computeSpatialQuery(
map,
ownedTiles,
target as [number, number],
);
res.json(result);
} catch (error) {
console.error("Error computing spatial query:", error);
res.status(500).json({
error: "Failed to compute spatial query",
message: error instanceof Error ? error.message : String(error),
});
}
});
/**
* POST /api/cache/clear
* Clear all caches (useful for development)
*/
app.post("/api/cache/clear", (req: Request, res: Response) => {
try {
clearMapCache();
clearAdapterCaches();
res.json({ message: "Caches cleared successfully" });
} catch (error) {
console.error("Error clearing caches:", error);
res.status(500).json({
error: "Failed to clear caches",
message: error instanceof Error ? error.message : String(error),
});
}
});
// Error handling middleware
app.use((err: Error, req: Request, res: Response, next: any) => {
console.error("Unhandled error:", err);
res.status(500).json({
error: "Internal server error",
message: err.message,
});
});
// Start server
app.listen(PORT, (error?: Error) => {
if (error) {
console.error("Failed to start server", error);
process.exit(1);
}
console.log(`
╔════════════════════════════════════════════════════════════╗
║ Pathfinding Playground Server ║
╚════════════════════════════════════════════════════════════╝
Server running at: http://localhost:${PORT}
Press Ctrl+C to stop
`);
});