1
0
Fork 0
forked from svrjs/svrjs

Lint out the codebase

This commit is contained in:
Dorian Niemiec 2024-08-25 19:46:17 +02:00
parent adfd3ff809
commit a55b123926
2 changed files with 144 additions and 71 deletions

View file

@ -126,7 +126,17 @@ function requestHandler(req, res) {
} }
} }
if (req.headers["x-svr-js-from-main-thread"] == "true" && req.socket && (!req.socket.remoteAddress || req.socket.remoteAddress == "::1" || req.socket.remoteAddress == "::ffff:127.0.0.1" || req.socket.remoteAddress == "127.0.0.1" || req.socket.remoteAddress == "localhost" || req.socket.remoteAddress == host || req.socket.remoteAddress == "::ffff:" + host)) { if (
req.headers["x-svr-js-from-main-thread"] == "true" &&
req.socket &&
(!req.socket.remoteAddress ||
req.socket.remoteAddress == "::1" ||
req.socket.remoteAddress == "::ffff:127.0.0.1" ||
req.socket.remoteAddress == "127.0.0.1" ||
req.socket.remoteAddress == "localhost" ||
req.socket.remoteAddress == host ||
req.socket.remoteAddress == "::ffff:" + host)
) {
let headers = config.getCustomHeaders(); let headers = config.getCustomHeaders();
res.writeHead(204, http.STATUS_CODES[204], headers); res.writeHead(204, http.STATUS_CODES[204], headers);
res.end(); res.end();

View file

@ -378,7 +378,9 @@ if (typeof process.serverConfig.sport === "string") {
const serverconsole = require("./utils/serverconsole.js"); const serverconsole = require("./utils/serverconsole.js");
function addListenersToWorker(worker) { function addListenersToWorker(worker) {
process.messageEventListeners.forEach((messageEventListener) => worker.on("message", messageEventListener(worker, serverconsole))); process.messageEventListeners.forEach((messageEventListener) =>
worker.on("message", messageEventListener(worker, serverconsole)),
);
} }
let inspectorURL = undefined; let inspectorURL = undefined;
@ -1028,7 +1030,11 @@ let commands = {
}, },
stop: (args, log) => { stop: (args, log) => {
let retcode = args[0]; let retcode = args[0];
if ((!cluster.isPrimary && cluster.isPrimary !== undefined) && server.listening) { if (
!cluster.isPrimary &&
cluster.isPrimary !== undefined &&
server.listening
) {
try { try {
server.close(function () { server.close(function () {
if (server2.listening) { if (server2.listening) {
@ -1120,7 +1126,8 @@ function SVRJSFork() {
) { ) {
threadLimitWarned = true; threadLimitWarned = true;
serverconsole.locwarnmessage( serverconsole.locwarnmessage(
name + " limited the number of workers to one, because of startup problems in Bun 1.0 and newer with shimmed (not native) clustering module. Reliability may suffer.", name +
" limited the number of workers to one, because of startup problems in Bun 1.0 and newer with shimmed (not native) clustering module. Reliability may suffer.",
); );
} }
if ( if (
@ -1136,7 +1143,8 @@ function SVRJSFork() {
} else { } else {
if (SVRJSInitialized) if (SVRJSInitialized)
serverconsole.locwarnmessage( serverconsole.locwarnmessage(
name + " limited the number of workers to one, because of startup problems in Bun 1.0 and newer with shimmed (not native) clustering module. Reliability may suffer.", name +
" limited the number of workers to one, because of startup problems in Bun 1.0 and newer with shimmed (not native) clustering module. Reliability may suffer.",
); );
} }
} catch (err) { } catch (err) {
@ -1152,7 +1160,8 @@ function SVRJSFork() {
) { ) {
threadLimitWarned = true; threadLimitWarned = true;
serverconsole.locwarnmessage( serverconsole.locwarnmessage(
name + " limited the number of workers to one, because of startup problems in Bun 1.0 and newer with shimmed (not native) clustering module. Reliability may suffer.", name +
" limited the number of workers to one, because of startup problems in Bun 1.0 and newer with shimmed (not native) clustering module. Reliability may suffer.",
); );
} }
if ( if (
@ -1168,7 +1177,8 @@ function SVRJSFork() {
} else { } else {
if (SVRJSInitialized) if (SVRJSInitialized)
serverconsole.locwarnmessage( serverconsole.locwarnmessage(
name + " limited the number of workers to one, because of startup problems in Bun 1.0 and newer with shimmed (not native) clustering module. Reliability may suffer.", name +
" limited the number of workers to one, because of startup problems in Bun 1.0 and newer with shimmed (not native) clustering module. Reliability may suffer.",
); );
} }
} else { } else {
@ -1199,9 +1209,11 @@ function SVRJSFork() {
} }
function getWorkerCountToFork() { function getWorkerCountToFork() {
let workersToFork = os.availableParallelism ? os.availableParallelism() : os.cpus().length; let workersToFork = os.availableParallelism
? os.availableParallelism()
: os.cpus().length;
try { try {
const useAvailableCores = Math.round((os.freemem()) / 50000000) - 1; // 1 core deleted for safety... const useAvailableCores = Math.round(os.freemem() / 50000000) - 1; // 1 core deleted for safety...
if (workersToFork > useAvailableCores) workersToFork = useAvailableCores; if (workersToFork > useAvailableCores) workersToFork = useAvailableCores;
} catch (err) { } catch (err) {
// Nevermind... Don't want SVR.JS to fail starting, because os.freemem function is not working. // Nevermind... Don't want SVR.JS to fail starting, because os.freemem function is not working.
@ -1215,12 +1227,15 @@ function forkWorkers(workersToFork, callback) {
if (i == 0) { if (i == 0) {
SVRJSFork(); SVRJSFork();
} else { } else {
setTimeout((function (i) { setTimeout(
return function () { (function (i) {
SVRJSFork(); return function () {
if (i >= workersToFork - 1) callback(); SVRJSFork();
}; if (i >= workersToFork - 1) callback();
})(i), i * 6.6); };
})(i),
i * 6.6,
);
} }
} }
} }
@ -1229,7 +1244,7 @@ function forkWorkers(workersToFork, callback) {
process.messageEventListeners.push((worker, serverconsole) => { process.messageEventListeners.push((worker, serverconsole) => {
return (message) => { return (message) => {
console.log("Message received from worker: " + message); console.log("Message received from worker: " + message);
} };
}); });
// Starting function // Starting function
@ -1770,44 +1785,76 @@ function start(init) {
} }
});*/ });*/
// Hangup check and restart // Hangup check and restart
setInterval(function () { setInterval(function () {
if (!closedMaster && !exiting) { if (!closedMaster && !exiting) {
let chksocket = {}; let chksocket = {};
if (process.serverConfig.secure && process.serverConfig.disableNonEncryptedServer) { if (
chksocket = https.get({ process.serverConfig.secure &&
hostname: (typeof process.serverConfig.sport == "number" && sListenAddress) ? sListenAddress : "localhost", process.serverConfig.disableNonEncryptedServer
port: (typeof process.serverConfig.sport == "number") ? process.serverConfig.sport : undefined, ) {
socketPath: (typeof process.serverConfig.sport == "number") ? undefined : process.serverConfig.sport, chksocket = https
headers: { .get(
"X-SVR-JS-From-Main-Thread": "true", {
"User-Agent": (exposeServerVersion ? "SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" : "SVR.JS") hostname:
}, typeof process.serverConfig.sport == "number" &&
timeout: 1620, sListenAddress
rejectUnauthorized: false ? sListenAddress
}, function (res) { : "localhost",
chksocket.removeAllListeners("timeout"); port:
res.destroy(); typeof process.serverConfig.sport == "number"
res.on("data", function () {}); ? process.serverConfig.sport
res.on("end", function () {}); : undefined,
crashed = false; socketPath:
}).on("error", function () { typeof process.serverConfig.sport == "number"
if (!exiting) { ? undefined
if (!crashed) SVRJSFork(); : process.serverConfig.sport,
else crashed = false; headers: {
} "X-SVR-JS-From-Main-Thread": "true",
}).on("timeout", function () { "User-Agent": generateServerString(true),
if (!exiting) SVRJSFork(); },
crashed = true; timeout: 1620,
}); rejectUnauthorized: false,
} else if ((process.serverConfig.enableHTTP2 == undefined ? false : process.serverConfig.enableHTTP2) && !process.serverConfig.secure) { },
function (res) {
chksocket.removeAllListeners("timeout");
res.destroy();
res.on("data", function () {});
res.on("end", function () {});
crashed = false;
},
)
.on("error", function () {
if (!exiting) {
if (!crashed) SVRJSFork();
else crashed = false;
}
})
.on("timeout", function () {
if (!exiting) SVRJSFork();
crashed = true;
});
} else if (
(process.serverConfig.enableHTTP2 == undefined
? false
: process.serverConfig.enableHTTP2) &&
!process.serverConfig.secure
) {
// It doesn't support through Unix sockets or Windows named pipes // It doesn't support through Unix sockets or Windows named pipes
var address = ((typeof process.serverConfig.port == "number" && listenAddress) ? listenAddress : "localhost").replace(/\/@/g, ""); var address = (
typeof process.serverConfig.port == "number" && listenAddress
? listenAddress
: "localhost"
).replace(/\/@/g, "");
if (address.indexOf(":") > -1) { if (address.indexOf(":") > -1) {
address = "[" + address + "]"; address = "[" + address + "]";
} }
var connection = http2.connect("http://" + address + ":" + process.serverConfig.port.toString()); var connection = http2.connect(
"http://" +
address +
":" +
process.serverConfig.port.toString(),
);
connection.on("error", function () { connection.on("error", function () {
if (!exiting) { if (!exiting) {
if (!crashed) SVRJSFork(); if (!crashed) SVRJSFork();
@ -1821,7 +1868,7 @@ function start(init) {
chksocket = connection.request({ chksocket = connection.request({
":path": "/", ":path": "/",
"x-svr-js-from-main-thread": "true", "x-svr-js-from-main-thread": "true",
"user-agent": (exposeServerVersion ? "SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" : "SVR.JS") "user-agent": generateServerString(true),
}); });
chksocket.on("response", function () { chksocket.on("response", function () {
connection.close(); connection.close();
@ -1834,30 +1881,46 @@ function start(init) {
} }
}); });
} else { } else {
chksocket = http.get({ chksocket = http
hostname: (typeof process.serverConfig.port == "number" && listenAddress) ? listenAddress : "localhost", .get(
port: (typeof process.serverConfig.port == "number") ? process.serverConfig.port : undefined, {
socketPath: (typeof process.serverConfig.port == "number") ? undefined : process.serverConfig.port, hostname:
headers: { typeof process.serverConfig.port == "number" &&
"X-SVR-JS-From-Main-Thread": "true", listenAddress
"User-Agent": generateServerString(true) ? listenAddress
}, : "localhost",
timeout: 1620 port:
}, function (res) { typeof process.serverConfig.port == "number"
chksocket.removeAllListeners("timeout"); ? process.serverConfig.port
res.destroy(); : undefined,
res.on("data", function () {}); socketPath:
res.on("end", function () {}); typeof process.serverConfig.port == "number"
crashed = false; ? undefined
}).on("error", function () { : process.serverConfig.port,
if (!exiting) { headers: {
if (!crashed) SVRJSFork(); "X-SVR-JS-From-Main-Thread": "true",
else crashed = false; "User-Agent": generateServerString(true),
} },
}).on("timeout", function () { timeout: 1620,
if (!exiting) SVRJSFork(); },
crashed = true; function (res) {
}); chksocket.removeAllListeners("timeout");
res.destroy();
res.on("data", function () {});
res.on("end", function () {});
crashed = false;
},
)
.on("error", function () {
if (!exiting) {
if (!crashed) SVRJSFork();
else crashed = false;
}
})
.on("timeout", function () {
if (!exiting) SVRJSFork();
crashed = true;
});
} }
} }
}, 4550); }, 4550);