-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
196 lines (171 loc) · 5.41 KB
/
Copy pathserver.js
File metadata and controls
196 lines (171 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env node
/*
* TCOAAL Browser Player
* Copyright (C) 2026 kidev
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details: <https://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
/*
* TCOAAL Browser Server
*
* Minimal static file server for the app/ directory and mod assets.
* Game files are imported by the user via loader.html into IndexedDB;
* the Service Worker (sw.js) handles decryption in the browser.
*
* Usage:
* node server.js # http://localhost:8080
* PORT=3000 node server.js # custom port
*/
"use strict";
const http = require("http");
const fs = require("fs");
const path = require("path");
const url = require("url");
const PORT = parseInt(process.env.PORT, 10) || 8080;
const APP_DIR = path.join(__dirname, "app");
// MIME types
const MIME = {
".html": "text/html; charset=utf-8",
".js": "application/javascript; charset=utf-8",
".json": "application/json; charset=utf-8",
".css": "text/css; charset=utf-8",
".png": "image/png",
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".webp": "image/webp",
".ogg": "audio/ogg",
".m4a": "audio/mp4",
".mp4": "video/mp4",
".webm": "video/webm",
".ttf": "font/ttf",
".otf": "font/otf",
".woff": "font/woff",
".woff2": "font/woff2",
".eot": "application/vnd.ms-fontobject",
".txt": "text/plain; charset=utf-8",
".csv": "text/csv; charset=utf-8",
".loc": "application/json; charset=utf-8",
".svg": "image/svg+xml",
".ico": "image/x-icon",
};
function mimeFor(filePath) {
return (
MIME[path.extname(filePath).toLowerCase()] || "application/octet-stream"
);
}
// Serve a static file. Returns true if found and served.
function serveFile(req, res, absPath, mime) {
let stat;
try {
stat = fs.statSync(absPath);
} catch {
return false;
}
if (!stat.isFile()) return false;
const etag = `"${stat.mtime.getTime().toString(16)}-${stat.size.toString(16)}"`;
if (req.headers["if-none-match"] === etag) {
res.writeHead(304);
res.end();
return true;
}
res.writeHead(200, {
"Content-Type": mime,
"Content-Length": stat.size,
ETag: etag,
"Cache-Control": "no-cache",
});
if (req.method === "HEAD") {
res.end();
return true;
}
fs.createReadStream(absPath).pipe(res);
return true;
}
// Request handler
function handler(req, res) {
if (req.method !== "GET" && req.method !== "HEAD") {
res.writeHead(405, { Allow: "GET, HEAD" });
res.end();
return;
}
let reqPath;
try {
reqPath = decodeURIComponent(url.parse(req.url).pathname || "/");
} catch {
res.writeHead(400);
res.end();
return;
}
const logicalRel = reqPath.replace(/^\/+/, "") || "index.html";
if (logicalRel.includes("..")) {
res.writeHead(403);
res.end();
return;
}
const mime = mimeFor(logicalRel);
// Mods manifest
if (logicalRel === "mods.json") {
const modsPath = path.join(__dirname, "mods.json");
if (serveFile(req, res, modsPath, "application/json; charset=utf-8"))
return;
res.writeHead(200, {
"Content-Type": "application/json; charset=utf-8",
"Content-Length": 2,
"Cache-Control": "no-cache",
});
res.end("{}");
return;
}
// "Now Loading" image: always swap the engine's canonical request for our
// bundled themed loading.png (mirrors the Service Worker override so Mode A
// shows the same image even before the SW takes control).
if (logicalRel === "img/system/Loading.png") {
if (
serveFile(req, res, path.join(APP_DIR, "img", "loading.png"), "image/png")
)
return;
}
// Project NOTICE. Lives at the repo root (it is the source of truth for
// both the README and /notice.html, which fetches and renders it), so it is
// not under app/ like the rest of the site. The deploy workflow copies it
// into _site for the same reason.
if (logicalRel === "NOTICE.md") {
const noticePath = path.join(__dirname, "NOTICE.md");
if (serveFile(req, res, noticePath, "text/markdown; charset=utf-8")) return;
}
// Mod assets (from mods/ directory at repo root)
if (logicalRel.startsWith("mods/")) {
const modFilePath = path.join(__dirname, logicalRel);
if (serveFile(req, res, modFilePath, mime)) return;
}
// App files (index.html, loader.html, sw.js, shims, etc.)
if (serveFile(req, res, path.join(APP_DIR, logicalRel), mime)) return;
// Not found
res.writeHead(404, { "Content-Type": "text/plain" });
res.end(`Not found: ${logicalRel}`);
}
// Start
const server = http.createServer(handler);
server.listen(PORT, "127.0.0.1", () => {
const gameUrl = `http://localhost:${PORT}`;
console.log(`TCOAAL running at ${gameUrl}`);
console.log("Press Ctrl+C to stop.\n");
const open =
{ darwin: "open", win32: "start", linux: "xdg-open" }[process.platform] ||
"xdg-open";
require("child_process")
.spawn(open, [gameUrl], { detached: true, stdio: "ignore" })
.unref();
});
server.on("error", (err) => {
console.error("Server error:", err.message);
process.exit(1);
});