1
0
Fork 0
forked from svrjs/svrjs
This repository has been archived on 2024-11-10. You can view files and clone it, but cannot push or open issues or pull requests.
svrjs/src/index.js

97 lines
3.8 KiB
JavaScript
Raw Normal View History

2024-08-23 19:58:15 +02:00
const http = require("http");
const fs = require("fs");
const cluster = require("./utils/clusterBunShim.js"); // Cluster module with shim for Bun
//const generateErrorStack = require("./utils/generateErrorStack.js");
2024-08-23 19:58:15 +02:00
const getOS = require("./utils/getOS.js");
const svrjsInfo = require("../svrjs.json");
const version = svrjsInfo.version;
//const parseURL = require("./utils/urlParser.js");
//const fixNodeMojibakeURL = require("./utils/urlMojibakeFixer.js");
// Create log, mods and temp directories, if they don't exist.
if (!fs.existsSync(__dirname + "/log")) fs.mkdirSync(__dirname + "/log");
if (!fs.existsSync(__dirname + "/mods")) fs.mkdirSync(__dirname + "/mods");
if (!fs.existsSync(__dirname + "/temp")) fs.mkdirSync(__dirname + "/temp");
const serverconsoleConstructor = require("./utils/serverconsole.js");
let configJSON = {};
2024-08-23 19:58:15 +02:00
// TODO: configuration from config.json
if (!configJSON.page404) configJSON.page404 = "404.html"
if (!configJSON.errorPages) configJSON.errorPages = [];
if (!configJSON.stackHidden) configJSON.stackHidden = true;
if (!configJSON.exposeServerVersion) configJSON.exposeServerVersion = false;
if (!configJSON.exposeModsInErrorPages) configJSON.exposeModsInErrorPages = false;
if (!configJSON.enableLogging) configJSON.enableLogging = true;
if (!configJSON.serverAdministratorEmail) configJSON.serverAdministratorEmail = "webmaster@svrjs.org";
if (!configJSON.customHeaders) configJSON.customHeaders = {};
const serverconsole = serverconsoleConstructor(configJSON.enableLogging);
let middleware = [
require("./middleware/core.js"),
require("./middleware/urlSanitizer.js")
];
function addMiddleware(mw) {
middleware.push(mw);
2024-08-23 19:58:15 +02:00
}
function requestHandler(req, res) {
let reqIdInt = Math.floor(Math.random() * 16777216);
if (reqIdInt == 16777216) reqIdInt = 0;
const reqId = "0".repeat(6 - reqIdInt.toString(16).length) + reqIdInt.toString(16);
// SVR.JS log facilities
const logFacilities = {
climessage: (msg) => serverconsole.climessage(msg, reqId),
reqmessage: (msg) => serverconsole.reqmessage(msg, reqId),
resmessage: (msg) => serverconsole.resmessage(msg, reqId),
errmessage: (msg) => serverconsole.errmessage(msg, reqId),
locerrmessage: (msg) => serverconsole.locerrmessage(msg, reqId),
locwarnmessage: (msg) => serverconsole.locwarnmessage(msg, reqId),
locmessage: (msg) => serverconsole.locmessage(msg, reqId)
};
// SVR.JS configuration object (modified)
const config = Object.assign(configJSON);
2024-08-23 19:58:15 +02:00
let index = 0;
2024-08-23 19:58:15 +02:00
// Call the next middleware function
const next = () => {
const currentMiddleware = middleware[index++];
if (currentMiddleware) {
try {
currentMiddleware(req, res, logFacilities, config, next);
} catch (err) {
if (res.error) res.error(500, err);
2024-08-23 19:58:15 +02:00
else {
logFacilities.errmessage("There was an error while processing the request!");
logFacilities.errmessage("Stack:");
logFacilities.errmessage(err.stack);
res.writeHead(500, "Internal Server Error", {
Server: (config.exposeServerVersion ? "SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" : "SVR.JS")
});
res.end("Error while executing the request handler");
2024-08-23 19:58:15 +02:00
}
}
} else {
if (res.error) res.error(404);
else {
res.writeHead(404, "Not Found", {
Server: (config.exposeServerVersion ? "SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" : "SVR.JS")
2024-08-23 19:58:15 +02:00
});
res.end("Request handler missing");
2024-08-23 19:58:15 +02:00
}
}
}
// Handle middleware
next();
2024-08-23 19:58:15 +02:00
}
// Create HTTP server
http.createServer(requestHandler).listen(3000);