forked from svrjs/svrjs
Lint out the codebase.
This commit is contained in:
parent
55d5efe54f
commit
695816b9d0
4 changed files with 172 additions and 53 deletions
|
@ -5,7 +5,15 @@ module.exports = (req, res, logFacilities, config, next) => {
|
||||||
let eheaders = config.getCustomHeaders();
|
let eheaders = config.getCustomHeaders();
|
||||||
eheaders["Content-Type"] = "text/html; charset=utf-8";
|
eheaders["Content-Type"] = "text/html; charset=utf-8";
|
||||||
res.writeHead(501, http.STATUS_CODES[501], eheaders);
|
res.writeHead(501, http.STATUS_CODES[501], eheaders);
|
||||||
res.write("<!DOCTYPE html><html><head><title>Proxy not implemented</title><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" /><style>html{background-color:#dfffdf;color:#000000;font-family:FreeSans, Helvetica, Tahoma, Verdana, Arial, sans-serif;margin:0.75em}body{background-color:#ffffff;padding:0.5em 0.5em 0.1em;margin:0.5em auto;width:90%;max-width:800px;-webkit-box-shadow:0 5px 10px 0 rgba(0, 0, 0, 0.15);-moz-box-shadow:0 5px 10px 0 rgba(0, 0, 0, 0.15);box-shadow:0 5px 10px 0 rgba(0, 0, 0, 0.15)}h1{text-align:center;font-size:2.25em;margin:0.3em 0 0.5em}code{background-color:#dfffdf;-webkit-box-shadow:0 2px 4px 0 rgba(0, 0, 0, 0.1);-moz-box-shadow:0 2px 4px 0 rgba(0, 0, 0, 0.1);box-shadow:0 2px 4px 0 rgba(0, 0, 0, 0.1);display:block;padding:0.2em;font-family:\"DejaVu Sans Mono\", \"Bitstream Vera Sans Mono\", Hack, Menlo, Consolas, Monaco, monospace;font-size:0.85em;margin:auto;width:95%;max-width:600px}table{width:95%;border-collapse:collapse;margin:auto;overflow-wrap:break-word;word-wrap:break-word;word-break:break-all;word-break:break-word;position:relative;z-index:0}table tbody{background-color:#ffffff;color:#000000}table tbody:after{-webkit-box-shadow:0 4px 8px 0 rgba(0, 0, 0, 0.175);-moz-box-shadow:0 4px 8px 0 rgba(0, 0, 0, 0.175);box-shadow:0 4px 8px 0 rgba(0, 0, 0, 0.175);content:' ';position:absolute;top:0;left:0;right:0;bottom:0;z-index:-1}table img{margin:0;display:inline}th,tr{padding:0.15em;text-align:center}th{background-color:#007000;color:#ffffff}th a{color:#ffffff}td,th{padding:0.225em}td{text-align:left}tr:nth-child(odd){background-color:#dfffdf}hr{color:#ffffff}@media screen and (prefers-color-scheme: dark){html{background-color:#002000;color:#ffffff}body{background-color:#000f00;-webkit-box-shadow:0 5px 10px 0 rgba(127, 127, 127, 0.15);-moz-box-shadow:0 5px 10px 0 rgba(127, 127, 127, 0.15);box-shadow:0 5px 10px 0 rgba(127, 127, 127, 0.15)}code{background-color:#002000;-webkit-box-shadow:0 2px 4px 0 rgba(127, 127, 127, 0.1);-moz-box-shadow:0 2px 4px 0 rgba(127, 127, 127, 0.1);box-shadow:0 2px 4px 0 rgba(127, 127, 127, 0.1)}a{color:#ffffff}a:hover{color:#00ff00}table tbody{background-color:#000f00;color:#ffffff}table tbody:after{-webkit-box-shadow:0 4px 8px 0 rgba(127, 127, 127, 0.175);-moz-box-shadow:0 4px 8px 0 rgba(127, 127, 127, 0.175);box-shadow:0 4px 8px 0 rgba(127, 127, 127, 0.175)}tr:nth-child(odd){background-color:#002000}}</style></head><body><h1>Proxy not implemented</h1><p>SVR.JS doesn't support proxy without proxy mod. If you're administator of this server, then install this mod in order to use SVR.JS as a proxy.</p><p><i>" + config.generateServerString().replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">") + "</i></p></body></html>");
|
res.write(
|
||||||
|
'<!DOCTYPE html><html><head><title>Proxy not implemented</title><meta name="viewport" content="width=device-width, initial-scale=1.0" /><style>html{background-color:#dfffdf;color:#000000;font-family:FreeSans, Helvetica, Tahoma, Verdana, Arial, sans-serif;margin:0.75em}body{background-color:#ffffff;padding:0.5em 0.5em 0.1em;margin:0.5em auto;width:90%;max-width:800px;-webkit-box-shadow:0 5px 10px 0 rgba(0, 0, 0, 0.15);-moz-box-shadow:0 5px 10px 0 rgba(0, 0, 0, 0.15);box-shadow:0 5px 10px 0 rgba(0, 0, 0, 0.15)}h1{text-align:center;font-size:2.25em;margin:0.3em 0 0.5em}code{background-color:#dfffdf;-webkit-box-shadow:0 2px 4px 0 rgba(0, 0, 0, 0.1);-moz-box-shadow:0 2px 4px 0 rgba(0, 0, 0, 0.1);box-shadow:0 2px 4px 0 rgba(0, 0, 0, 0.1);display:block;padding:0.2em;font-family:"DejaVu Sans Mono", "Bitstream Vera Sans Mono", Hack, Menlo, Consolas, Monaco, monospace;font-size:0.85em;margin:auto;width:95%;max-width:600px}table{width:95%;border-collapse:collapse;margin:auto;overflow-wrap:break-word;word-wrap:break-word;word-break:break-all;word-break:break-word;position:relative;z-index:0}table tbody{background-color:#ffffff;color:#000000}table tbody:after{-webkit-box-shadow:0 4px 8px 0 rgba(0, 0, 0, 0.175);-moz-box-shadow:0 4px 8px 0 rgba(0, 0, 0, 0.175);box-shadow:0 4px 8px 0 rgba(0, 0, 0, 0.175);content:\' \';position:absolute;top:0;left:0;right:0;bottom:0;z-index:-1}table img{margin:0;display:inline}th,tr{padding:0.15em;text-align:center}th{background-color:#007000;color:#ffffff}th a{color:#ffffff}td,th{padding:0.225em}td{text-align:left}tr:nth-child(odd){background-color:#dfffdf}hr{color:#ffffff}@media screen and (prefers-color-scheme: dark){html{background-color:#002000;color:#ffffff}body{background-color:#000f00;-webkit-box-shadow:0 5px 10px 0 rgba(127, 127, 127, 0.15);-moz-box-shadow:0 5px 10px 0 rgba(127, 127, 127, 0.15);box-shadow:0 5px 10px 0 rgba(127, 127, 127, 0.15)}code{background-color:#002000;-webkit-box-shadow:0 2px 4px 0 rgba(127, 127, 127, 0.1);-moz-box-shadow:0 2px 4px 0 rgba(127, 127, 127, 0.1);box-shadow:0 2px 4px 0 rgba(127, 127, 127, 0.1)}a{color:#ffffff}a:hover{color:#00ff00}table tbody{background-color:#000f00;color:#ffffff}table tbody:after{-webkit-box-shadow:0 4px 8px 0 rgba(127, 127, 127, 0.175);-moz-box-shadow:0 4px 8px 0 rgba(127, 127, 127, 0.175);box-shadow:0 4px 8px 0 rgba(127, 127, 127, 0.175)}tr:nth-child(odd){background-color:#002000}}</style></head><body><h1>Proxy not implemented</h1><p>SVR.JS doesn\'t support proxy without proxy mod. If you\'re administator of this server, then install this mod in order to use SVR.JS as a proxy.</p><p><i>' +
|
||||||
|
config
|
||||||
|
.generateServerString()
|
||||||
|
.replace(/&/g, "&")
|
||||||
|
.replace(/</g, "<")
|
||||||
|
.replace(/>/g, ">") +
|
||||||
|
"</i></p></body></html>",
|
||||||
|
);
|
||||||
res.end();
|
res.end();
|
||||||
logFacilities.errmessage("SVR.JS doesn't support proxy without proxy mod.");
|
logFacilities.errmessage("SVR.JS doesn't support proxy without proxy mod.");
|
||||||
return;
|
return;
|
||||||
|
@ -17,11 +25,15 @@ module.exports = (req, res, logFacilities, config, next) => {
|
||||||
res.writeHead(204, http.STATUS_CODES[204], hdss);
|
res.writeHead(204, http.STATUS_CODES[204], hdss);
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
} else if (req.method != "GET" && req.method != "POST" && req.method != "HEAD") {
|
} else if (
|
||||||
|
req.method != "GET" &&
|
||||||
|
req.method != "POST" &&
|
||||||
|
req.method != "HEAD"
|
||||||
|
) {
|
||||||
res.error(405);
|
res.error(405);
|
||||||
logFacilities.errmessage("Invaild method: " + req.method);
|
logFacilities.errmessage("Invaild method: " + req.method);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
next();
|
next();
|
||||||
}
|
};
|
||||||
|
|
|
@ -2,36 +2,130 @@ const http = require("http");
|
||||||
const os = require("os");
|
const os = require("os");
|
||||||
const sizify = require("../utils/sizify.js");
|
const sizify = require("../utils/sizify.js");
|
||||||
const svrjsInfo = require("../../svrjs.json");
|
const svrjsInfo = require("../../svrjs.json");
|
||||||
const {name} = svrjsInfo;
|
const { name } = svrjsInfo;
|
||||||
|
|
||||||
module.exports = (req, res, logFacilities, config, next) => {
|
module.exports = (req, res, logFacilities, config, next) => {
|
||||||
if (config.allowStatus && (req.parsedURL.pathname == "/svrjsstatus.svr" || (os.platform() == "win32" && req.parsedURL.pathname.toLowerCase() == "/svrjsstatus.svr"))) {
|
if (
|
||||||
|
config.allowStatus &&
|
||||||
|
(req.parsedURL.pathname == "/svrjsstatus.svr" ||
|
||||||
|
(os.platform() == "win32" &&
|
||||||
|
req.parsedURL.pathname.toLowerCase() == "/svrjsstatus.svr"))
|
||||||
|
) {
|
||||||
const formatRelativeTime = (relativeTime) => {
|
const formatRelativeTime = (relativeTime) => {
|
||||||
const days = Math.floor(relativeTime / 60 / (60 * 24));
|
const days = Math.floor(relativeTime / 60 / (60 * 24));
|
||||||
const dateDiff = new Date(relativeTime * 1000);
|
const dateDiff = new Date(relativeTime * 1000);
|
||||||
return days + " days, " + dateDiff.getUTCHours() + " hours, " + dateDiff.getUTCMinutes() + " minutes, " + dateDiff.getUTCSeconds() + " seconds";
|
return (
|
||||||
}
|
days +
|
||||||
|
" days, " +
|
||||||
|
dateDiff.getUTCHours() +
|
||||||
|
" hours, " +
|
||||||
|
dateDiff.getUTCMinutes() +
|
||||||
|
" minutes, " +
|
||||||
|
dateDiff.getUTCSeconds() +
|
||||||
|
" seconds"
|
||||||
|
);
|
||||||
|
};
|
||||||
let statusBody = "";
|
let statusBody = "";
|
||||||
statusBody += "Server version: " + config.generateServerString() + "<br/><hr/>";
|
statusBody +=
|
||||||
|
"Server version: " + config.generateServerString() + "<br/><hr/>";
|
||||||
|
|
||||||
//Those entries are just dates and numbers converted/formatted to strings, so no escaping is needed.
|
//Those entries are just dates and numbers converted/formatted to strings, so no escaping is needed.
|
||||||
statusBody += "Current time: " + new Date().toString() + "<br/>Thread start time: " + new Date(new Date() - (process.uptime() * 1000)).toString() + "<br/>Thread uptime: " + formatRelativeTime(Math.floor(process.uptime())) + "<br/>";
|
statusBody +=
|
||||||
|
"Current time: " +
|
||||||
|
new Date().toString() +
|
||||||
|
"<br/>Thread start time: " +
|
||||||
|
new Date(new Date() - process.uptime() * 1000).toString() +
|
||||||
|
"<br/>Thread uptime: " +
|
||||||
|
formatRelativeTime(Math.floor(process.uptime())) +
|
||||||
|
"<br/>";
|
||||||
statusBody += "OS uptime: " + formatRelativeTime(os.uptime()) + "<br/>";
|
statusBody += "OS uptime: " + formatRelativeTime(os.uptime()) + "<br/>";
|
||||||
statusBody += "Total request count: " + process.reqcounter + "<br/>";
|
statusBody += "Total request count: " + process.reqcounter + "<br/>";
|
||||||
statusBody += "Average request rate: " + (Math.round((process.reqcounter / process.uptime()) * 100) / 100) + " requests/s<br/>";
|
statusBody +=
|
||||||
|
"Average request rate: " +
|
||||||
|
Math.round((process.reqcounter / process.uptime()) * 100) / 100 +
|
||||||
|
" requests/s<br/>";
|
||||||
statusBody += "Client errors (4xx): " + process.err4xxcounter + "<br/>";
|
statusBody += "Client errors (4xx): " + process.err4xxcounter + "<br/>";
|
||||||
statusBody += "Server errors (5xx): " + process.err5xxcounter + "<br/>";
|
statusBody += "Server errors (5xx): " + process.err5xxcounter + "<br/>";
|
||||||
statusBody += "Average error rate: " + (Math.round(((process.err4xxcounter + process.err5xxcounter) / process.reqcounter) * 10000) / 100) + "%<br/>";
|
statusBody +=
|
||||||
|
"Average error rate: " +
|
||||||
|
Math.round(
|
||||||
|
((process.err4xxcounter + process.err5xxcounter) / process.reqcounter) *
|
||||||
|
10000,
|
||||||
|
) /
|
||||||
|
100 +
|
||||||
|
"%<br/>";
|
||||||
statusBody += "Malformed HTTP requests: " + process.malformedcounter;
|
statusBody += "Malformed HTTP requests: " + process.malformedcounter;
|
||||||
if (process.memoryUsage) statusBody += "<br/>Memory usage of thread: " + sizify(process.memoryUsage().rss, true) + "B";
|
if (process.memoryUsage)
|
||||||
if (process.cpuUsage) statusBody += "<br/>Total CPU usage by thread: u" + (process.cpuUsage().user / 1000) + "ms s" + (process.cpuUsage().system / 1000) + "ms - " + (Math.round((((process.cpuUsage().user + process.cpuUsage().system) / 1000000) / process.uptime()) * 1000) / 1000) + "%";
|
statusBody +=
|
||||||
|
"<br/>Memory usage of thread: " +
|
||||||
|
sizify(process.memoryUsage().rss, true) +
|
||||||
|
"B";
|
||||||
|
if (process.cpuUsage)
|
||||||
|
statusBody +=
|
||||||
|
"<br/>Total CPU usage by thread: u" +
|
||||||
|
process.cpuUsage().user / 1000 +
|
||||||
|
"ms s" +
|
||||||
|
process.cpuUsage().system / 1000 +
|
||||||
|
"ms - " +
|
||||||
|
Math.round(
|
||||||
|
((process.cpuUsage().user + process.cpuUsage().system) /
|
||||||
|
1000000 /
|
||||||
|
process.uptime()) *
|
||||||
|
1000,
|
||||||
|
) /
|
||||||
|
1000 +
|
||||||
|
"%";
|
||||||
statusBody += "<br/>Thread PID: " + process.pid + "<br/>";
|
statusBody += "<br/>Thread PID: " + process.pid + "<br/>";
|
||||||
|
|
||||||
res.writeHead(200, http.STATUS_CODES[200], {
|
res.writeHead(200, http.STATUS_CODES[200], {
|
||||||
"Content-Type": "text/html; charset=utf-8"
|
"Content-Type": "text/html; charset=utf-8",
|
||||||
});
|
});
|
||||||
res.end((res.head == "" ? "<!DOCTYPE html><html><head><title>SVR.JS status" + (req.headers.host == undefined ? "" : " for " + String(req.headers.host).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">")) + "</title><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" /><style>html{background-color:#dfffdf;color:#000000;font-family:FreeSans, Helvetica, Tahoma, Verdana, Arial, sans-serif;margin:0.75em}body{background-color:#ffffff;padding:0.5em 0.5em 0.1em;margin:0.5em auto;width:90%;max-width:800px;-webkit-box-shadow:0 5px 10px 0 rgba(0, 0, 0, 0.15);-moz-box-shadow:0 5px 10px 0 rgba(0, 0, 0, 0.15);box-shadow:0 5px 10px 0 rgba(0, 0, 0, 0.15)}h1{text-align:center;font-size:2.25em;margin:0.3em 0 0.5em}code{background-color:#dfffdf;-webkit-box-shadow:0 2px 4px 0 rgba(0, 0, 0, 0.1);-moz-box-shadow:0 2px 4px 0 rgba(0, 0, 0, 0.1);box-shadow:0 2px 4px 0 rgba(0, 0, 0, 0.1);display:block;padding:0.2em;font-family:\"DejaVu Sans Mono\", \"Bitstream Vera Sans Mono\", Hack, Menlo, Consolas, Monaco, monospace;font-size:0.85em;margin:auto;width:95%;max-width:600px}table{width:95%;border-collapse:collapse;margin:auto;overflow-wrap:break-word;word-wrap:break-word;word-break:break-all;word-break:break-word;position:relative;z-index:0}table tbody{background-color:#ffffff;color:#000000}table tbody:after{-webkit-box-shadow:0 4px 8px 0 rgba(0, 0, 0, 0.175);-moz-box-shadow:0 4px 8px 0 rgba(0, 0, 0, 0.175);box-shadow:0 4px 8px 0 rgba(0, 0, 0, 0.175);content:' ';position:absolute;top:0;left:0;right:0;bottom:0;z-index:-1}table img{margin:0;display:inline}th,tr{padding:0.15em;text-align:center}th{background-color:#007000;color:#ffffff}th a{color:#ffffff}td,th{padding:0.225em}td{text-align:left}tr:nth-child(odd){background-color:#dfffdf}hr{color:#ffffff}@media screen and (prefers-color-scheme: dark){html{background-color:#002000;color:#ffffff}body{background-color:#000f00;-webkit-box-shadow:0 5px 10px 0 rgba(127, 127, 127, 0.15);-moz-box-shadow:0 5px 10px 0 rgba(127, 127, 127, 0.15);box-shadow:0 5px 10px 0 rgba(127, 127, 127, 0.15)}code{background-color:#002000;-webkit-box-shadow:0 2px 4px 0 rgba(127, 127, 127, 0.1);-moz-box-shadow:0 2px 4px 0 rgba(127, 127, 127, 0.1);box-shadow:0 2px 4px 0 rgba(127, 127, 127, 0.1)}a{color:#ffffff}a:hover{color:#00ff00}table tbody{background-color:#000f00;color:#ffffff}table tbody:after{-webkit-box-shadow:0 4px 8px 0 rgba(127, 127, 127, 0.175);-moz-box-shadow:0 4px 8px 0 rgba(127, 127, 127, 0.175);box-shadow:0 4px 8px 0 rgba(127, 127, 127, 0.175)}tr:nth-child(odd){background-color:#002000}}</style></head><body>" : res.head.replace(/<head>/i, "<head><title>" + name.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">") + " status" + (req.headers.host == undefined ? "" : " for " + String(req.headers.host).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">")) + "</title>")) + "<h1>" + name.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">") + " status" + (req.headers.host == undefined ? "" : " for " + String(req.headers.host).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">")) + "</h1>" + statusBody + (res.foot == "" ? "</body></html>" : res.foot));
|
res.end(
|
||||||
|
(res.head == ""
|
||||||
|
? "<!DOCTYPE html><html><head><title>SVR.JS status" +
|
||||||
|
(req.headers.host == undefined
|
||||||
|
? ""
|
||||||
|
: " for " +
|
||||||
|
String(req.headers.host)
|
||||||
|
.replace(/&/g, "&")
|
||||||
|
.replace(/</g, "<")
|
||||||
|
.replace(/>/g, ">")) +
|
||||||
|
'</title><meta name="viewport" content="width=device-width, initial-scale=1.0" /><style>html{background-color:#dfffdf;color:#000000;font-family:FreeSans, Helvetica, Tahoma, Verdana, Arial, sans-serif;margin:0.75em}body{background-color:#ffffff;padding:0.5em 0.5em 0.1em;margin:0.5em auto;width:90%;max-width:800px;-webkit-box-shadow:0 5px 10px 0 rgba(0, 0, 0, 0.15);-moz-box-shadow:0 5px 10px 0 rgba(0, 0, 0, 0.15);box-shadow:0 5px 10px 0 rgba(0, 0, 0, 0.15)}h1{text-align:center;font-size:2.25em;margin:0.3em 0 0.5em}code{background-color:#dfffdf;-webkit-box-shadow:0 2px 4px 0 rgba(0, 0, 0, 0.1);-moz-box-shadow:0 2px 4px 0 rgba(0, 0, 0, 0.1);box-shadow:0 2px 4px 0 rgba(0, 0, 0, 0.1);display:block;padding:0.2em;font-family:"DejaVu Sans Mono", "Bitstream Vera Sans Mono", Hack, Menlo, Consolas, Monaco, monospace;font-size:0.85em;margin:auto;width:95%;max-width:600px}table{width:95%;border-collapse:collapse;margin:auto;overflow-wrap:break-word;word-wrap:break-word;word-break:break-all;word-break:break-word;position:relative;z-index:0}table tbody{background-color:#ffffff;color:#000000}table tbody:after{-webkit-box-shadow:0 4px 8px 0 rgba(0, 0, 0, 0.175);-moz-box-shadow:0 4px 8px 0 rgba(0, 0, 0, 0.175);box-shadow:0 4px 8px 0 rgba(0, 0, 0, 0.175);content:\' \';position:absolute;top:0;left:0;right:0;bottom:0;z-index:-1}table img{margin:0;display:inline}th,tr{padding:0.15em;text-align:center}th{background-color:#007000;color:#ffffff}th a{color:#ffffff}td,th{padding:0.225em}td{text-align:left}tr:nth-child(odd){background-color:#dfffdf}hr{color:#ffffff}@media screen and (prefers-color-scheme: dark){html{background-color:#002000;color:#ffffff}body{background-color:#000f00;-webkit-box-shadow:0 5px 10px 0 rgba(127, 127, 127, 0.15);-moz-box-shadow:0 5px 10px 0 rgba(127, 127, 127, 0.15);box-shadow:0 5px 10px 0 rgba(127, 127, 127, 0.15)}code{background-color:#002000;-webkit-box-shadow:0 2px 4px 0 rgba(127, 127, 127, 0.1);-moz-box-shadow:0 2px 4px 0 rgba(127, 127, 127, 0.1);box-shadow:0 2px 4px 0 rgba(127, 127, 127, 0.1)}a{color:#ffffff}a:hover{color:#00ff00}table tbody{background-color:#000f00;color:#ffffff}table tbody:after{-webkit-box-shadow:0 4px 8px 0 rgba(127, 127, 127, 0.175);-moz-box-shadow:0 4px 8px 0 rgba(127, 127, 127, 0.175);box-shadow:0 4px 8px 0 rgba(127, 127, 127, 0.175)}tr:nth-child(odd){background-color:#002000}}</style></head><body>'
|
||||||
|
: res.head.replace(
|
||||||
|
/<head>/i,
|
||||||
|
"<head><title>" +
|
||||||
|
name
|
||||||
|
.replace(/&/g, "&")
|
||||||
|
.replace(/</g, "<")
|
||||||
|
.replace(/>/g, ">") +
|
||||||
|
" status" +
|
||||||
|
(req.headers.host == undefined
|
||||||
|
? ""
|
||||||
|
: " for " +
|
||||||
|
String(req.headers.host)
|
||||||
|
.replace(/&/g, "&")
|
||||||
|
.replace(/</g, "<")
|
||||||
|
.replace(/>/g, ">")) +
|
||||||
|
"</title>",
|
||||||
|
)) +
|
||||||
|
"<h1>" +
|
||||||
|
name
|
||||||
|
.replace(/&/g, "&")
|
||||||
|
.replace(/</g, "<")
|
||||||
|
.replace(/>/g, ">") +
|
||||||
|
" status" +
|
||||||
|
(req.headers.host == undefined
|
||||||
|
? ""
|
||||||
|
: " for " +
|
||||||
|
String(req.headers.host)
|
||||||
|
.replace(/&/g, "&")
|
||||||
|
.replace(/</g, "<")
|
||||||
|
.replace(/>/g, ">")) +
|
||||||
|
"</h1>" +
|
||||||
|
statusBody +
|
||||||
|
(res.foot == "" ? "</body></html>" : res.foot),
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
next();
|
next();
|
||||||
}
|
};
|
||||||
|
|
|
@ -2,12 +2,25 @@ function sizify(bytes, addI) {
|
||||||
if (bytes == 0) return "0";
|
if (bytes == 0) return "0";
|
||||||
if (bytes < 0) bytes = -bytes;
|
if (bytes < 0) bytes = -bytes;
|
||||||
const prefixes = ["", "K", "M", "G", "T", "P", "E", "Z", "Y", "R", "Q"];
|
const prefixes = ["", "K", "M", "G", "T", "P", "E", "Z", "Y", "R", "Q"];
|
||||||
let prefixIndex = Math.floor(Math.log2 ? Math.log2(bytes) / 10 : (Math.log(bytes) / (Math.log(2) * 10)));
|
let prefixIndex = Math.floor(
|
||||||
|
Math.log2 ? Math.log2(bytes) / 10 : Math.log(bytes) / (Math.log(2) * 10),
|
||||||
|
);
|
||||||
if (prefixIndex >= prefixes.length - 1) prefixIndex = prefixes.length - 1;
|
if (prefixIndex >= prefixes.length - 1) prefixIndex = prefixes.length - 1;
|
||||||
let prefixIndexTranslated = Math.pow(2, 10 * prefixIndex);
|
let prefixIndexTranslated = Math.pow(2, 10 * prefixIndex);
|
||||||
let decimalPoints = 2 - Math.floor(Math.log10 ? Math.log10(bytes / prefixIndexTranslated) : (Math.log(bytes / prefixIndexTranslated) / Math.log(10)));
|
let decimalPoints =
|
||||||
|
2 -
|
||||||
|
Math.floor(
|
||||||
|
Math.log10
|
||||||
|
? Math.log10(bytes / prefixIndexTranslated)
|
||||||
|
: Math.log(bytes / prefixIndexTranslated) / Math.log(10),
|
||||||
|
);
|
||||||
if (decimalPoints < 0) decimalPoints = 0;
|
if (decimalPoints < 0) decimalPoints = 0;
|
||||||
return (Math.ceil((bytes / prefixIndexTranslated) * Math.pow(10, decimalPoints)) / Math.pow(10, decimalPoints)) + prefixes[prefixIndex] + ((prefixIndex > 0 && addI) ? "i" : "");
|
return (
|
||||||
|
Math.ceil((bytes / prefixIndexTranslated) * Math.pow(10, decimalPoints)) /
|
||||||
|
Math.pow(10, decimalPoints) +
|
||||||
|
prefixes[prefixIndex] +
|
||||||
|
(prefixIndex > 0 && addI ? "i" : "")
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = sizify;
|
module.exports = sizify;
|
||||||
|
|
|
@ -1,57 +1,57 @@
|
||||||
const sizify = require('../../src/utils/sizify');
|
const sizify = require("../../src/utils/sizify");
|
||||||
|
|
||||||
describe('"sizify" function', () => {
|
describe('"sizify" function', () => {
|
||||||
test('should return "0" for 0 bytes', () => {
|
test('should return "0" for 0 bytes', () => {
|
||||||
expect(sizify(0)).toBe('0');
|
expect(sizify(0)).toBe("0");
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should handle negative bytes', () => {
|
test("should handle negative bytes", () => {
|
||||||
expect(sizify(-1024)).toBe('1K');
|
expect(sizify(-1024)).toBe("1K");
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should return correct size for small values', () => {
|
test("should return correct size for small values", () => {
|
||||||
expect(sizify(1000)).toBe('1000');
|
expect(sizify(1000)).toBe("1000");
|
||||||
expect(sizify(1024)).toBe('1K');
|
expect(sizify(1024)).toBe("1K");
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should return correct size for larger values', () => {
|
test("should return correct size for larger values", () => {
|
||||||
expect(sizify(1048576)).toBe('1M');
|
expect(sizify(1048576)).toBe("1M");
|
||||||
expect(sizify(1073741824)).toBe('1G');
|
expect(sizify(1073741824)).toBe("1G");
|
||||||
expect(sizify(1099511627776)).toBe('1T');
|
expect(sizify(1099511627776)).toBe("1T");
|
||||||
expect(sizify(1125899906842624)).toBe('1P');
|
expect(sizify(1125899906842624)).toBe("1P");
|
||||||
expect(sizify(1152921504606846976)).toBe('1E');
|
expect(sizify(1152921504606846976)).toBe("1E");
|
||||||
expect(sizify(1180591620717411303424)).toBe('1Z');
|
expect(sizify(1180591620717411303424)).toBe("1Z");
|
||||||
expect(sizify(1208925819614629174706176)).toBe('1Y');
|
expect(sizify(1208925819614629174706176)).toBe("1Y");
|
||||||
expect(sizify(1237940039285380274899124224)).toBe('1R');
|
expect(sizify(1237940039285380274899124224)).toBe("1R");
|
||||||
expect(sizify(1267650600228229401496703205376)).toBe('1Q');
|
expect(sizify(1267650600228229401496703205376)).toBe("1Q");
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should handle very large values', () => {
|
test("should handle very large values", () => {
|
||||||
const largeValue = 2 ** 100; // A very large number
|
const largeValue = 2 ** 100; // A very large number
|
||||||
expect(sizify(largeValue)).toBe('1Q');
|
expect(sizify(largeValue)).toBe("1Q");
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should add "i" suffix when addI is true', () => {
|
test('should add "i" suffix when addI is true', () => {
|
||||||
expect(sizify(1024, true)).toBe('1Ki');
|
expect(sizify(1024, true)).toBe("1Ki");
|
||||||
expect(sizify(1048576, true)).toBe('1Mi');
|
expect(sizify(1048576, true)).toBe("1Mi");
|
||||||
expect(sizify(1073741824, true)).toBe('1Gi');
|
expect(sizify(1073741824, true)).toBe("1Gi");
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should not add "i" suffix when addI is false', () => {
|
test('should not add "i" suffix when addI is false', () => {
|
||||||
expect(sizify(1024, false)).toBe('1K');
|
expect(sizify(1024, false)).toBe("1K");
|
||||||
expect(sizify(1048576, false)).toBe('1M');
|
expect(sizify(1048576, false)).toBe("1M");
|
||||||
expect(sizify(1073741824, false)).toBe('1G');
|
expect(sizify(1073741824, false)).toBe("1G");
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should handle decimal points correctly', () => {
|
test("should handle decimal points correctly", () => {
|
||||||
expect(sizify(1500)).toBe('1.47K');
|
expect(sizify(1500)).toBe("1.47K");
|
||||||
expect(sizify(1500000)).toBe('1.44M');
|
expect(sizify(1500000)).toBe("1.44M");
|
||||||
expect(sizify(1500000000)).toBe('1.4G');
|
expect(sizify(1500000000)).toBe("1.4G");
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should handle edge cases', () => {
|
test("should handle edge cases", () => {
|
||||||
expect(sizify(1)).toBe('1');
|
expect(sizify(1)).toBe("1");
|
||||||
expect(sizify(1023)).toBe('1023');
|
expect(sizify(1023)).toBe("1023");
|
||||||
expect(sizify(1025)).toBe('1.01K');
|
expect(sizify(1025)).toBe("1.01K");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Reference in a new issue