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

@ -54,6 +54,9 @@ for(let i = 2; i < process.argv.length; i++)
case "stop":
await writeFile(shutdown, "");
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;
const response = await Promise.race([
fetch(new URL("/test-shutdown", serverUrl)),
@ -65,7 +68,10 @@ for(let i = 2; i < process.argv.length; i++)
]);
clearTimeout(timeoutId);
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)
exec("npm run pm2-stop", (error, stdout) => {
if (error) throw error;

View file

@ -16,8 +16,14 @@ import { paintSource, tryReadFile } from './randomization.mjs';
import loadTemplates from './templates.mjs';
import { fileURLToPath } from 'node:url';
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 => {
try {
base = new URL(config.host);
@ -30,6 +36,8 @@ const serverUrl = (base => {
})();
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 rh = createRammerhead();
@ -50,18 +58,19 @@ const rammerheadScopes = [
"/api/shuffleDict",
"/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);
return (
rammerheadScopes.includes(url.pathname) ||
rammerheadSession.test(url.pathname)
);
};
const routeRhRequest = (req, res) => {
},
routeRhRequest = (req, res) => {
rh.emit("request", req, res);
};
const routeRhUpgrade = (req, socket, head) => {
},
routeRhUpgrade = (req, socket, head) => {
rh.emit("upgrade", req, socket, head);
};
@ -69,21 +78,16 @@ const routeRhUpgrade = (req, socket, head) => {
const serverFactory = (handler) => {
return createServer()
.on('request', (req, res) => {
if (shouldRouteRh(req)) {
if (shouldRouteRh(req))
routeRhRequest(req, res);
}
else {
handler(req, res);
}
else handler(req, res);
})
.on('upgrade', (req, socket, head) => {
if (shouldRouteRh(req)) {
if (shouldRouteRh(req))
routeRhUpgrade(req, socket, head);
}
else if (req.url.endsWith('/wisp/')) {
else if (req.url.endsWith('/wisp/'))
wisp.routeRequest(req, socket, head);
}
})
});
};
// Set logger to true for logs
@ -95,6 +99,7 @@ app.register(fastifyHelmet, {
xPoweredBy: false
});
// Assign server file paths to different paths, for serving content on the website.
app.register(fastifyStatic, {
root: fileURLToPath(new URL("../views/pages", import.meta.url)),
decorateReply: false
@ -114,6 +119,7 @@ app.register(fastifyStatic, {
app.register(fastifyStatic, {
root: fileURLToPath(new URL(
// Use the pre-compiled, minified scripts instead, if enabled in config.
config.minifyScripts ? "../views/dist/assets/js" : "../views/assets/js",
import.meta.url
)),
@ -123,6 +129,7 @@ app.register(fastifyStatic, {
app.register(fastifyStatic, {
root: fileURLToPath(new URL(
// Use the pre-compiled, minified stylesheets instead, if enabled in config.
config.minifyScripts ? "../views/dist/assets/css" : "../views/assets/css",
import.meta.url
)),
@ -130,30 +137,40 @@ app.register(fastifyStatic, {
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, {
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",
import.meta.url
)), uvPath],
)),
uvPath
],
prefix: "/uv/",
decorateReply: false
});
// Register proxy paths to the website.
app.register(fastifyStatic, {
root: epoxyPath,
prefix: "/epoxy/",
decorateReply: false
});
app.register(fastifyStatic, {
root: libcurlPath,
prefix: "/libcurl/",
decorateReply: false
});
app.register(fastifyStatic, {
root: bareModulePath,
prefix: "/bareasmodule/",
decorateReply: false
});
app.register(fastifyStatic, {
root: baremuxPath,
prefix: "/baremux/",
@ -167,6 +184,8 @@ app.register(fastifyStatic, {
// back here. Which path converts to what is defined in routes.mjs.
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)) {
console.log("Holy Unblocker is shutting down.");
app.close();
@ -182,10 +201,11 @@ app.get("/:file", (req, reply) => {
paintSource(
loadTemplates(
tryReadFile(
path.join(__dirname,
path.join(
__dirname,
"views",
// Return the error page if the query is not found in
// routes.mjs. Also set index as the default page.
// Return the error page if the query is not found in routes.mjs. Also set
// the index the as the default page.
req.params.file
? pages[req.params.file] || "error.html"
: pages.index