Commenting and reformatting changes

This commit is contained in:
00Fjongl 2024-08-07 17:47:11 -05:00
parent 192495d44a
commit 4f79f92de9
2 changed files with 136 additions and 110 deletions

View file

@ -22,7 +22,7 @@ const shutdown = fileURLToPath(new URL("./src/.shutdown", import.meta.url));
// Run each command line argument passed after node run-command.mjs. // Run each command line argument passed after node run-command.mjs.
// Commands are defined in the switch case statement below. // Commands are defined in the switch case statement below.
for(let i = 2; i < process.argv.length; i++) for (let i = 2; i < process.argv.length; i++)
switch (process.argv[i]) { switch (process.argv[i]) {
// Commmand to boot up the server. Use PM2 to run if production is true in the // Commmand to boot up the server. Use PM2 to run if production is true in the
// config file. // config file.
@ -54,6 +54,9 @@ for(let i = 2; i < process.argv.length; i++)
case "stop": case "stop":
await writeFile(shutdown, ""); await writeFile(shutdown, "");
try { try {
// Give the server 5 seconds to respond, otherwise cancel this and throw an
// error to the console. The fetch request will also throw an error immediately
// if checking the server on localhost and the port is unused.
let timeoutId = undefined; let timeoutId = undefined;
const response = await Promise.race([ const response = await Promise.race([
fetch(new URL("/test-shutdown", serverUrl)), fetch(new URL("/test-shutdown", serverUrl)),
@ -64,8 +67,11 @@ for(let i = 2; i < process.argv.length; i++)
}) })
]); ]);
clearTimeout(timeoutId); clearTimeout(timeoutId);
if(response === "Error") throw new Error("Server is unresponsive."); if (response === "Error") throw new Error("Server is unresponsive.");
} catch (e) {await unlink(shutdown)} } catch (e) {
console.error(e);
await unlink(shutdown);
}
if (config.production) if (config.production)
exec("npm run pm2-stop", (error, stdout) => { exec("npm run pm2-stop", (error, stdout) => {
if (error) throw error; if (error) throw error;

View file

@ -16,8 +16,14 @@ import { paintSource, tryReadFile } from './randomization.mjs';
import loadTemplates from './templates.mjs'; import loadTemplates from './templates.mjs';
import { fileURLToPath } from 'node:url'; import { fileURLToPath } from 'node:url';
import { existsSync, unlinkSync } from 'node:fs'; import { existsSync, unlinkSync } from 'node:fs';
const config = Object.freeze(JSON.parse(await readFile(new URL("./config.json", import.meta.url)))), { pages, text404 } = pkg;
const __dirname = path.resolve(); const config = Object.freeze(JSON.parse(
await readFile(new URL("./config.json", import.meta.url))
)),
{ pages, text404 } = pkg,
__dirname = path.resolve();
// Record the server's location as a URL object, including its host and port.
const serverUrl = (base => { const serverUrl = (base => {
try { try {
base = new URL(config.host); base = new URL(config.host);
@ -30,6 +36,8 @@ const serverUrl = (base => {
})(); })();
console.log(serverUrl); console.log(serverUrl);
// The server will check for the existence of this file when a shutdown is requested.
// The shutdown script in run-command.js will temporarily produce this file.
const shutdown = fileURLToPath(new URL("./.shutdown", import.meta.url)); const shutdown = fileURLToPath(new URL("./.shutdown", import.meta.url));
const rh = createRammerhead(); const rh = createRammerhead();
@ -50,40 +58,36 @@ const rammerheadScopes = [
"/api/shuffleDict", "/api/shuffleDict",
"/mainport", "/mainport",
]; ];
const rammerheadSession = /^\/[a-z0-9]{32}/;
const shouldRouteRh = req => { const rammerheadSession = /^\/[a-z0-9]{32}/,
shouldRouteRh = req => {
const url = new URL(req.url, serverUrl); const url = new URL(req.url, serverUrl);
return ( return (
rammerheadScopes.includes(url.pathname) || rammerheadScopes.includes(url.pathname) ||
rammerheadSession.test(url.pathname) rammerheadSession.test(url.pathname)
); );
}; },
const routeRhRequest = (req, res) => { routeRhRequest = (req, res) => {
rh.emit("request", req, res); rh.emit("request", req, res);
}; },
const routeRhUpgrade = (req, socket, head) => { routeRhUpgrade = (req, socket, head) => {
rh.emit("upgrade", req, socket, head); rh.emit("upgrade", req, socket, head);
}; };
// Create a server factory for RH, and wisp (and bare if you please). // Create a server factory for RH, and wisp (and bare if you please).
const serverFactory = (handler) => { const serverFactory = (handler) => {
return createServer() return createServer()
.on('request', (req, res) => { .on('request', (req, res) => {
if (shouldRouteRh(req)) { if (shouldRouteRh(req))
routeRhRequest(req, res); routeRhRequest(req, res);
} else handler(req, res);
else {
handler(req, res);
}
}) })
.on('upgrade', (req, socket, head) => { .on('upgrade', (req, socket, head) => {
if (shouldRouteRh(req)) { if (shouldRouteRh(req))
routeRhUpgrade(req, socket, head); routeRhUpgrade(req, socket, head);
} else if (req.url.endsWith('/wisp/'))
else if (req.url.endsWith('/wisp/')) {
wisp.routeRequest(req, socket, head); wisp.routeRequest(req, socket, head);
} });
})
}; };
// Set logger to true for logs // Set logger to true for logs
@ -95,6 +99,7 @@ app.register(fastifyHelmet, {
xPoweredBy: false xPoweredBy: false
}); });
// Assign server file paths to different paths, for serving content on the website.
app.register(fastifyStatic, { app.register(fastifyStatic, {
root: fileURLToPath(new URL("../views/pages", import.meta.url)), root: fileURLToPath(new URL("../views/pages", import.meta.url)),
decorateReply: false decorateReply: false
@ -114,6 +119,7 @@ app.register(fastifyStatic, {
app.register(fastifyStatic, { app.register(fastifyStatic, {
root: fileURLToPath(new URL( root: fileURLToPath(new URL(
// Use the pre-compiled, minified scripts instead, if enabled in config.
config.minifyScripts ? "../views/dist/assets/js" : "../views/assets/js", config.minifyScripts ? "../views/dist/assets/js" : "../views/assets/js",
import.meta.url import.meta.url
)), )),
@ -123,6 +129,7 @@ app.register(fastifyStatic, {
app.register(fastifyStatic, { app.register(fastifyStatic, {
root: fileURLToPath(new URL( root: fileURLToPath(new URL(
// Use the pre-compiled, minified stylesheets instead, if enabled in config.
config.minifyScripts ? "../views/dist/assets/css" : "../views/assets/css", config.minifyScripts ? "../views/dist/assets/css" : "../views/assets/css",
import.meta.url import.meta.url
)), )),
@ -130,30 +137,40 @@ app.register(fastifyStatic, {
decorateReply: false decorateReply: false
}); });
// This combines scripts from the official UV repository with local UV scripts into
// one directory path. Local versions of files override the official versions.
app.register(fastifyStatic, { app.register(fastifyStatic, {
root: [fileURLToPath(new URL( root: [
fileURLToPath(new URL(
// Use the pre-compiled, minified scripts instead, if enabled in config.
config.minifyScripts ? "../views/dist/uv" : "../views/uv", config.minifyScripts ? "../views/dist/uv" : "../views/uv",
import.meta.url import.meta.url
)), uvPath], )),
uvPath
],
prefix: "/uv/", prefix: "/uv/",
decorateReply: false decorateReply: false
}); });
// Register proxy paths to the website.
app.register(fastifyStatic, { app.register(fastifyStatic, {
root: epoxyPath, root: epoxyPath,
prefix: "/epoxy/", prefix: "/epoxy/",
decorateReply: false decorateReply: false
}); });
app.register(fastifyStatic, { app.register(fastifyStatic, {
root: libcurlPath, root: libcurlPath,
prefix: "/libcurl/", prefix: "/libcurl/",
decorateReply: false decorateReply: false
}); });
app.register(fastifyStatic, { app.register(fastifyStatic, {
root: bareModulePath, root: bareModulePath,
prefix: "/bareasmodule/", prefix: "/bareasmodule/",
decorateReply: false decorateReply: false
}); });
app.register(fastifyStatic, { app.register(fastifyStatic, {
root: baremuxPath, root: baremuxPath,
prefix: "/baremux/", prefix: "/baremux/",
@ -167,6 +184,8 @@ app.register(fastifyStatic, {
// back here. Which path converts to what is defined in routes.mjs. // back here. Which path converts to what is defined in routes.mjs.
app.get("/:file", (req, reply) => { app.get("/:file", (req, reply) => {
// If a GET request is sent to /test-shutdown and a script-generated shutdown file
// is present, gracefully shut the server down.
if (req.params.file === "test-shutdown" && existsSync(shutdown)) { if (req.params.file === "test-shutdown" && existsSync(shutdown)) {
console.log("Holy Unblocker is shutting down."); console.log("Holy Unblocker is shutting down.");
app.close(); app.close();
@ -182,10 +201,11 @@ app.get("/:file", (req, reply) => {
paintSource( paintSource(
loadTemplates( loadTemplates(
tryReadFile( tryReadFile(
path.join(__dirname, path.join(
__dirname,
"views", "views",
// Return the error page if the query is not found in // Return the error page if the query is not found in routes.mjs. Also set
// routes.mjs. Also set index as the default page. // the index the as the default page.
req.params.file req.params.file
? pages[req.params.file] || "error.html" ? pages[req.params.file] || "error.html"
: pages.index : pages.index