forked from svrjs/svrjs
Lint out the codebase
This commit is contained in:
parent
adfd3ff809
commit
a55b123926
2 changed files with 144 additions and 71 deletions
|
@ -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();
|
||||||
|
|
133
src/index.js
133
src/index.js
|
@ -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(
|
||||||
|
(function (i) {
|
||||||
return function () {
|
return function () {
|
||||||
SVRJSFork();
|
SVRJSFork();
|
||||||
if (i >= workersToFork - 1) callback();
|
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
|
||||||
|
.get(
|
||||||
|
{
|
||||||
|
hostname:
|
||||||
|
typeof process.serverConfig.sport == "number" &&
|
||||||
|
sListenAddress
|
||||||
|
? sListenAddress
|
||||||
|
: "localhost",
|
||||||
|
port:
|
||||||
|
typeof process.serverConfig.sport == "number"
|
||||||
|
? process.serverConfig.sport
|
||||||
|
: undefined,
|
||||||
|
socketPath:
|
||||||
|
typeof process.serverConfig.sport == "number"
|
||||||
|
? undefined
|
||||||
|
: process.serverConfig.sport,
|
||||||
headers: {
|
headers: {
|
||||||
"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),
|
||||||
},
|
},
|
||||||
timeout: 1620,
|
timeout: 1620,
|
||||||
rejectUnauthorized: false
|
rejectUnauthorized: false,
|
||||||
}, function (res) {
|
},
|
||||||
|
function (res) {
|
||||||
chksocket.removeAllListeners("timeout");
|
chksocket.removeAllListeners("timeout");
|
||||||
res.destroy();
|
res.destroy();
|
||||||
res.on("data", function () {});
|
res.on("data", function () {});
|
||||||
res.on("end", function () {});
|
res.on("end", function () {});
|
||||||
crashed = false;
|
crashed = false;
|
||||||
}).on("error", function () {
|
},
|
||||||
|
)
|
||||||
|
.on("error", function () {
|
||||||
if (!exiting) {
|
if (!exiting) {
|
||||||
if (!crashed) SVRJSFork();
|
if (!crashed) SVRJSFork();
|
||||||
else crashed = false;
|
else crashed = false;
|
||||||
}
|
}
|
||||||
}).on("timeout", function () {
|
})
|
||||||
|
.on("timeout", function () {
|
||||||
if (!exiting) SVRJSFork();
|
if (!exiting) SVRJSFork();
|
||||||
crashed = true;
|
crashed = true;
|
||||||
});
|
});
|
||||||
} else if ((process.serverConfig.enableHTTP2 == undefined ? false : process.serverConfig.enableHTTP2) && !process.serverConfig.secure) {
|
} 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,27 +1881,43 @@ 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:
|
||||||
|
typeof process.serverConfig.port == "number" &&
|
||||||
|
listenAddress
|
||||||
|
? listenAddress
|
||||||
|
: "localhost",
|
||||||
|
port:
|
||||||
|
typeof process.serverConfig.port == "number"
|
||||||
|
? process.serverConfig.port
|
||||||
|
: undefined,
|
||||||
|
socketPath:
|
||||||
|
typeof process.serverConfig.port == "number"
|
||||||
|
? undefined
|
||||||
|
: process.serverConfig.port,
|
||||||
headers: {
|
headers: {
|
||||||
"X-SVR-JS-From-Main-Thread": "true",
|
"X-SVR-JS-From-Main-Thread": "true",
|
||||||
"User-Agent": generateServerString(true)
|
"User-Agent": generateServerString(true),
|
||||||
},
|
},
|
||||||
timeout: 1620
|
timeout: 1620,
|
||||||
}, function (res) {
|
},
|
||||||
|
function (res) {
|
||||||
chksocket.removeAllListeners("timeout");
|
chksocket.removeAllListeners("timeout");
|
||||||
res.destroy();
|
res.destroy();
|
||||||
res.on("data", function () {});
|
res.on("data", function () {});
|
||||||
res.on("end", function () {});
|
res.on("end", function () {});
|
||||||
crashed = false;
|
crashed = false;
|
||||||
}).on("error", function () {
|
},
|
||||||
|
)
|
||||||
|
.on("error", function () {
|
||||||
if (!exiting) {
|
if (!exiting) {
|
||||||
if (!crashed) SVRJSFork();
|
if (!crashed) SVRJSFork();
|
||||||
else crashed = false;
|
else crashed = false;
|
||||||
}
|
}
|
||||||
}).on("timeout", function () {
|
})
|
||||||
|
.on("timeout", function () {
|
||||||
if (!exiting) SVRJSFork();
|
if (!exiting) SVRJSFork();
|
||||||
crashed = true;
|
crashed = true;
|
||||||
});
|
});
|
||||||
|
|
Reference in a new issue