BlueGit

timfa questward

Files

questward / server / src / gateway / http.js

http.js

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