BlueGit

timfa questward

Files

questward / server / src / gateway / http.js

http.js

http.js 2.8 KiB Download Raw
1import { createServer } from "node:http";
2import { createReadStream, existsSync, statSync } from "node:fs";
3import { extname, join, normalize, relative, sep } from "node:path";
4import { fileURLToPath } from "node:url";
5import { buildSpriteCatalog } from "../assets/spriteCatalog.js";
6
7const MIME = {
8 ".html": "text/html; charset=utf-8",
9 ".js": "text/javascript; charset=utf-8",
10 ".css": "text/css; charset=utf-8",
11 ".png": "image/png",
12 ".json": "application/json; charset=utf-8",
13 ".svg": "image/svg+xml",
14 ".ico": "image/x-icon"
15};
16
17export function createHttpServer (rootDir)
18{
19 const clientDir = join(rootDir, "client");
20 const sharedDir = join(rootDir, "shared");
21 const frameworkDir = join(clientDir, "frameworkv3", "js");
22
23 return createServer((req, res) =>
24 {
25 try
26 {
27 const url = new URL(req.url ?? "/", "http://localhost");
28 if (url.pathname === "/assets/catalog.json")
29 {
30 const catalog = spriteCatalogFor(clientDir);
31 res.writeHead(200, { "content-type": "application/json; charset=utf-8" });
32 res.end(JSON.stringify(catalog));
33 return;
34 }
35 const filePath = resolvePublicPath(url.pathname, clientDir, sharedDir, frameworkDir);
36 if (!filePath)
37 {
38 res.writeHead(404, { "content-type": "text/plain; charset=utf-8" });
39 res.end("Not found");
40 return;
41 }
42 res.writeHead(200, { "content-type": MIME[extname(filePath)] ?? "application/octet-stream" });
43 createReadStream(filePath).pipe(res);
44 }
45 catch (error)
46 {
47 res.writeHead(500, { "content-type": "text/plain; charset=utf-8" });
48 res.end(error.message);
49 }
50 });
51}
52
53function resolvePublicPath (pathname, clientDir, sharedDir, frameworkDir)
54{
55 if (pathname === "/" || pathname === "/index.html")
56 { return join(clientDir, "index.html"); }
57 if (pathname === "/editor" || pathname === "/editor.html")
58 { return join(clientDir, "editor.html"); }
59
60 if (pathname.startsWith("/shared/"))
61 { return safeJoin(sharedDir, pathname.slice("/shared/".length)); }
62 if (pathname.startsWith("/framework/"))
63 { return safeJoin(frameworkDir, pathname.slice("/framework/".length)); }
64 if (pathname.startsWith("/src/") || pathname.startsWith("/assets/") || pathname.startsWith("/ui/"))
65 { return safeJoin(clientDir, pathname.slice(1)); }
66
67 return null;
68}
69
70function safeJoin (root, relativePath)
71{
72 const resolved = normalize(join(root, relativePath));
73 const rel = relative(root, resolved);
74 if (rel.startsWith("..") || rel.startsWith(sep) || rel.includes(`..${sep}`))
75 { return null; }
76 if (!existsSync(resolved) || !statSync(resolved).isFile())
77 { return null; }
78 return resolved;
79}
80
81function spriteCatalogFor (clientDir)
82{
83 // Rebuild each request so deleting optional companions (e.g. *-bloom) is picked up
84 // without a process restart.
85 return buildSpriteCatalog(join(clientDir, "assets"));
86}
87
88export function repoRootFrom (url)
89{
90 return join(fileURLToPath(new URL(".", url)), "../..");
91}