1
0
Fork 0
forked from svrjs/svrjs

Update to SVR.JS 3.15.2

This commit is contained in:
Dorian Niemiec 2024-05-20 17:42:16 +02:00
parent 25280b310c
commit 78e7c8c967
4 changed files with 52 additions and 59 deletions

View file

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>SVR.JS 3.15.1</title> <title>SVR.JS 3.15.2</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<style> <style>
@ -76,7 +76,7 @@
</style> </style>
</head> </head>
<body> <body>
<h1>Welcome to SVR.JS 3.15.1</h1> <h1>Welcome to SVR.JS 3.15.2</h1>
<br /> <br />
<img src="/logo.png" style="width: 224px; max-width: 100%;" /> <img src="/logo.png" style="width: 224px; max-width: 100%;" />
<br /> <br />
@ -148,9 +148,7 @@
</code> </code>
<p>Changes:</p> <p>Changes:</p>
<ul style="display: inline-block; margin: 0;"> <ul style="display: inline-block; margin: 0;">
<li>Added Content-Range support for HTML files.</li> <li>Removed the limit of 16 workers.</li>
<li>MIME type lookups are now performed once, not twice.</li>
<li>Optimized static file serving function.</li>
</ul> </ul>
<p> <p>
<a href="/tests.html">Tests</a><br /> <a href="/tests.html">Tests</a><br />

View file

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>SVR.JS 3.15.1 Licenses</title> <title>SVR.JS 3.15.2 Licenses</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<style> <style>
@ -76,8 +76,8 @@
</style> </style>
</head> </head>
<body> <body>
<h1>SVR.JS 3.15.1 Licenses</h1> <h1>SVR.JS 3.15.2 Licenses</h1>
<h2>SVR.JS 3.15.1</h2> <h2>SVR.JS 3.15.2</h2>
<div style="display: inline-block; text-align: left; border-width: 2px; border-style: solid; border-color: gray; padding: 8px;"> <div style="display: inline-block; text-align: left; border-width: 2px; border-style: solid; border-color: gray; padding: 8px;">
MIT License<br/> MIT License<br/>
<br/> <br/>
@ -101,7 +101,7 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE<br/> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE<br/>
SOFTWARE.<br/> SOFTWARE.<br/>
</div> </div>
<h2>Packages used by SVR.JS 3.15.1</h2> <h2>Packages used by SVR.JS 3.15.2</h2>
<div style="width: 100%; max-width: 1280px; margin: auto"> <div style="width: 100%; max-width: 1280px; margin: auto">
<div style="width: 100%; background-color: #ccc; background-color: rgba(200, 200, 200, 0.3); border: 1px solid green; text-align: left; margin: 10px 0;"> <div style="width: 100%; background-color: #ccc; background-color: rgba(200, 200, 200, 0.3); border: 1px solid green; text-align: left; margin: 10px 0;">
<div style="float: right;">License: MIT</div> <div style="float: right;">License: MIT</div>

91
svr.js
View file

@ -69,7 +69,7 @@ function deleteFolderRecursive(path) {
} }
var os = require("os"); var os = require("os");
var version = "3.15.1"; var version = "3.15.2";
var singlethreaded = false; var singlethreaded = false;
if (process.versions) process.versions.svrjs = version; // Inject SVR.JS into process.versions if (process.versions) process.versions.svrjs = version; // Inject SVR.JS into process.versions
@ -5331,25 +5331,25 @@ function start(init) {
}, 300000); }, 300000);
} else if (cluster.isPrimary) { } else if (cluster.isPrimary) {
setInterval(function () { setInterval(function () {
var allClusters = Object.keys(cluster.workers); var allWorkers = Object.keys(cluster.workers);
var goodWorkers = []; var goodWorkers = [];
function checkWorker(callback, _id) { function checkWorker(callback, _id) {
if (typeof _id === "undefined") _id = 0; if (typeof _id === "undefined") _id = 0;
if (_id >= allClusters.length) { if (_id >= allWorkers.length) {
callback(); callback();
return; return;
} }
try { try {
if (cluster.workers[allClusters[_id]]) { if (cluster.workers[allWorkers[_id]]) {
isWorkerHungUpBuff2 = true; isWorkerHungUpBuff2 = true;
cluster.workers[allClusters[_id]].on("message", msgListener); cluster.workers[allWorkers[_id]].on("message", msgListener);
cluster.workers[allClusters[_id]].send("\x14PINGPING"); cluster.workers[allWorkers[_id]].send("\x14PINGPING");
setTimeout(function () { setTimeout(function () {
if (isWorkerHungUpBuff2) { if (isWorkerHungUpBuff2) {
checkWorker(callback, _id + 1); checkWorker(callback, _id + 1);
} else { } else {
goodWorkers.push(allClusters[_id]); goodWorkers.push(allWorkers[_id]);
checkWorker(callback, _id + 1); checkWorker(callback, _id + 1);
} }
}, 250); }, 250);
@ -5357,10 +5357,10 @@ function start(init) {
checkWorker(callback, _id + 1); checkWorker(callback, _id + 1);
} }
} catch (err) { } catch (err) {
if (cluster.workers[allClusters[_id]]) { if (cluster.workers[allWorkers[_id]]) {
cluster.workers[allClusters[_id]].removeAllListeners("message"); cluster.workers[allWorkers[_id]].removeAllListeners("message");
cluster.workers[allClusters[_id]].on("message", bruteForceListenerWrapper(cluster.workers[allClusters[_id]])); cluster.workers[allWorkers[_id]].on("message", bruteForceListenerWrapper(cluster.workers[allWorkers[_id]]));
cluster.workers[allClusters[_id]].on("message", listenConnListener); cluster.workers[allWorkers[_id]].on("message", listenConnListener);
} }
checkWorker(callback, _id + 1); checkWorker(callback, _id + 1);
} }
@ -5457,16 +5457,16 @@ function start(init) {
var command = argss.shift(); var command = argss.shift();
if (line != "") { if (line != "") {
if (cluster.isPrimary !== undefined) { if (cluster.isPrimary !== undefined) {
var allClusters = Object.keys(cluster.workers); var allWorkers = Object.keys(cluster.workers);
if (command == "block") commands.block(argss); if (command == "block") commands.block(argss);
if (command == "unblock") commands.unblock(argss); if (command == "unblock") commands.unblock(argss);
if (command == "restart") { if (command == "restart") {
var stopError = false; var stopError = false;
exiting = true; exiting = true;
for (var i = 0; i < allClusters.length; i++) { for (var i = 0; i < allWorkers.length; i++) {
try { try {
if (cluster.workers[allClusters[i]]) { if (cluster.workers[allWorkers[i]]) {
cluster.workers[allClusters[i]].kill(); cluster.workers[allWorkers[i]].kill();
} }
} catch (err) { } catch (err) {
stopError = true; stopError = true;
@ -5476,7 +5476,6 @@ function start(init) {
SVRJSInitialized = false; SVRJSInitialized = false;
closedMaster = true; closedMaster = true;
var cpus = os.availableParallelism ? os.availableParallelism() : os.cpus().length; var cpus = os.availableParallelism ? os.availableParallelism() : os.cpus().length;
if (cpus > 16) cpus = 16;
try { try {
var useAvailableCores = Math.round((os.freemem()) / 50000000) - 1; // 1 core deleted for safety... var useAvailableCores = Math.round((os.freemem()) / 50000000) - 1; // 1 core deleted for safety...
if (cpus > useAvailableCores) cpus = useAvailableCores; if (cpus > useAvailableCores) cpus = useAvailableCores;
@ -5504,9 +5503,9 @@ function start(init) {
} }
if (command == "stop") { if (command == "stop") {
exiting = true; exiting = true;
allClusters = Object.keys(cluster.workers); allWorkers = Object.keys(cluster.workers);
} }
allClusters.forEach(function (clusterID) { allWorkers.forEach(function (clusterID) {
try { try {
if (cluster.workers[clusterID]) { if (cluster.workers[clusterID]) {
cluster.workers[clusterID].on("message", msgListener); cluster.workers[clusterID].on("message", msgListener);
@ -5547,7 +5546,6 @@ function start(init) {
// Cluster forking code // Cluster forking code
if (cluster.isPrimary !== undefined && init) { if (cluster.isPrimary !== undefined && init) {
var cpus = os.availableParallelism ? os.availableParallelism() : os.cpus().length; var cpus = os.availableParallelism ? os.availableParallelism() : os.cpus().length;
if (cpus > 16) cpus = 16;
try { try {
var useAvailableCores = Math.round((os.freemem()) / 50000000) - 1; // 1 core deleted for safety... var useAvailableCores = Math.round((os.freemem()) / 50000000) - 1; // 1 core deleted for safety...
if (cpus > useAvailableCores) cpus = useAvailableCores; if (cpus > useAvailableCores) cpus = useAvailableCores;
@ -5676,28 +5674,28 @@ function start(init) {
setTimeout(function () { setTimeout(function () {
setInterval(function () { setInterval(function () {
if (!closedMaster && !exiting) { if (!closedMaster && !exiting) {
var allClusters = Object.keys(cluster.workers); var allWorkers = Object.keys(cluster.workers);
var minClusters = 0; var minWorkers = 0;
minClusters = Math.ceil(cpus * 0.625); minWorkers = Math.ceil(cpus * 0.625);
if (minClusters < 2) minClusters = 2; if (minWorkers < 2) minWorkers = 2;
var goodWorkers = []; var goodWorkers = [];
function checkWorker(callback, _id) { function checkWorker(callback, _id) {
if (typeof _id === "undefined") _id = 0; if (typeof _id === "undefined") _id = 0;
if (_id >= allClusters.length) { if (_id >= allWorkers.length) {
callback(); callback();
return; return;
} }
try { try {
if (cluster.workers[allClusters[_id]]) { if (cluster.workers[allWorkers[_id]]) {
isWorkerHungUpBuff = true; isWorkerHungUpBuff = true;
cluster.workers[allClusters[_id]].on("message", msgListener); cluster.workers[allWorkers[_id]].on("message", msgListener);
cluster.workers[allClusters[_id]].send("\x14KILLPING"); cluster.workers[allWorkers[_id]].send("\x14KILLPING");
setTimeout(function () { setTimeout(function () {
if (isWorkerHungUpBuff) { if (isWorkerHungUpBuff) {
checkWorker(callback, _id + 1); checkWorker(callback, _id + 1);
} else { } else {
goodWorkers.push(allClusters[_id]); goodWorkers.push(allWorkers[_id]);
checkWorker(callback, _id + 1); checkWorker(callback, _id + 1);
} }
}, 250); }, 250);
@ -5705,16 +5703,16 @@ function start(init) {
checkWorker(callback, _id + 1); checkWorker(callback, _id + 1);
} }
} catch (err) { } catch (err) {
if (cluster.workers[allClusters[_id]]) { if (cluster.workers[allWorkers[_id]]) {
cluster.workers[allClusters[_id]].removeAllListeners("message"); cluster.workers[allWorkers[_id]].removeAllListeners("message");
cluster.workers[allClusters[_id]].on("message", bruteForceListenerWrapper(cluster.workers[allClusters[_id]])); cluster.workers[allWorkers[_id]].on("message", bruteForceListenerWrapper(cluster.workers[allWorkers[_id]]));
cluster.workers[allClusters[_id]].on("message", listenConnListener); cluster.workers[allWorkers[_id]].on("message", listenConnListener);
} }
checkWorker(callback, _id + 1); checkWorker(callback, _id + 1);
} }
} }
checkWorker(function () { checkWorker(function () {
if (goodWorkers.length > minClusters) { if (goodWorkers.length > minWorkers) {
var wN = Math.floor(Math.random() * goodWorkers.length); var wN = Math.floor(Math.random() * goodWorkers.length);
if (wN == goodWorkers.length) return; if (wN == goodWorkers.length) return;
try { try {
@ -5806,20 +5804,17 @@ function saveConfig() {
// Process event listeners // Process event listeners
if (cluster.isPrimary || cluster.isPrimary === undefined) { if (cluster.isPrimary || cluster.isPrimary === undefined) {
process.on("uncaughtException", function (err) { // Crash handler
// CRASH HANDLER function crashHandlerMaster(err) {
serverconsole.locerrmessage("SVR.JS master process just crashed!!!"); serverconsole.locerrmessage("SVR.JS worker just crashed!!!");
serverconsole.locerrmessage("Stack:");
serverconsole.locerrmessage(generateErrorStack(err));
process.exit(err.errno);
});
process.on("unhandledRejection", function (err) {
// CRASH HANDLER
serverconsole.locerrmessage("SVR.JS master process just crashed!!!");
serverconsole.locerrmessage("Stack:"); serverconsole.locerrmessage("Stack:");
serverconsole.locerrmessage(err.stack ? generateErrorStack(err) : String(err)); serverconsole.locerrmessage(err.stack ? generateErrorStack(err) : String(err));
process.exit(err.errno); process.exit(err.errno);
}); }
process.on("uncaughtException", crashHandlerMaster);
process.on("unhandledRejection", crashHandlerMaster);
process.on("exit", function (code) { process.on("exit", function (code) {
try { try {
if (!configJSONRErr && !configJSONPErr) { if (!configJSONRErr && !configJSONPErr) {
@ -5858,11 +5853,11 @@ if (cluster.isPrimary || cluster.isPrimary === undefined) {
reallyExiting = true; reallyExiting = true;
if (cluster.isPrimary !== undefined) { if (cluster.isPrimary !== undefined) {
exiting = true; exiting = true;
var allClusters = Object.keys(cluster.workers); var allWorkers = Object.keys(cluster.workers);
for (var i = 0; i < allClusters.length; i++) { for (var i = 0; i < allWorkers.length; i++) {
try { try {
if (cluster.workers[allClusters[i]]) { if (cluster.workers[allWorkers[i]]) {
cluster.workers[allClusters[i]].send("stop"); cluster.workers[allWorkers[i]].send("stop");
} }
} catch (err) { } catch (err) {
// Worker will crash with EPIPE anyway. // Worker will crash with EPIPE anyway.

View file

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>SVR.JS 3.15.1 Tests</title> <title>SVR.JS 3.15.2 Tests</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<style> <style>
@ -76,7 +76,7 @@
</style> </style>
</head> </head>
<body> <body>
<h1>SVR.JS 3.15.1 Tests</h1> <h1>SVR.JS 3.15.2 Tests</h1>
<h2>Directory (without trailing slash)</h2> <h2>Directory (without trailing slash)</h2>
<iframe src="/testdir" width="75%" height="300px"></iframe> <iframe src="/testdir" width="75%" height="300px"></iframe>
<h2>Directory (with query)</h2> <h2>Directory (with query)</h2>