fix and optimize (except WTVIRC, it needs a lot of work)

This commit is contained in:
zefie
2025-08-12 17:44:26 -04:00
parent 733200c897
commit a85efc9968
22 changed files with 504 additions and 518 deletions

View File

@@ -189,7 +189,7 @@ class WTVShared {
if (isNaN(inbyte)) return '00';
for (let ii = 0; ii < 8; ii++) {
let mix = (crc ^ inbyte) & 1;
const mix = (crc ^ inbyte) & 1;
crc >>= 1;
if (mix) crc ^= 0x8C;
inbyte >>= 1;
@@ -448,7 +448,6 @@ class WTVShared {
* @return {object} Headers object
*/
headerStringToObj(headers, response = false) {
let inc_headers = 0;
const headers_obj = {};
headers_obj.raw_headers = headers;
const headers_obj_pre = headers.split("\n");
@@ -474,7 +473,7 @@ class WTVShared {
headers_obj.request_url = decodeURI(request_url).trim("\r");
} else if (d.indexOf(":") > 0) {
const d_split = d.split(':');
let header_name = d_split[0];
const header_name = d_split[0];
if (typeof headers_obj[header_name] === 'string') {
headers_obj[header_name] = [headers_obj[header_name]];
headers_obj[header_name].push((d_split.slice(1).join(':')).trim("\r"));
@@ -590,9 +589,7 @@ class WTVShared {
* @returns {string} The decoded string
*/
decodeBufferText(buf) {
var out = "";
out = this.utf8Decode(this.iconv.decode(Buffer.from(buf),'ISO-8859-1'));
return out;
return this.utf8Decode(this.iconv.decode(Buffer.from(buf),'ISO-8859-1'));;
}
/**
@@ -636,17 +633,16 @@ class WTVShared {
* @notice If the file exists but cannot be parsed, it will terminate the process with an error message
*/
getUserConfig() {
let minisrv_user_config = {};
const user_config_filename = this.getAbsolutePath("user_config.json", this.appdir);
try {
var user_config_filename = this.getAbsolutePath("user_config.json", this.appdir);
if (this.fs.lstatSync(user_config_filename)) {
try {
var minisrv_user_config = this.parseJSON(this.fs.readFileSync(user_config_filename));
minisrv_user_config = this.parseJSON(this.fs.readFileSync(user_config_filename));
} catch (f) {
console.error("ERROR: Could not read user_config.json", "\n\nReason:\n\n", f);
this.process.exit(1);
}
} else {
var minisrv_user_config = {}
}
return minisrv_user_config;
} catch (e) {
@@ -724,6 +720,7 @@ class WTVShared {
* @returns {object} The MiniSrv configuration object
*/
readMiniSrvConfig(user_config = true, notices = true, reload_notice = false) {
let minisrv_config = {};
const log = (msg) => {
if (notices || reload_notice) console.log(msg);
};
@@ -738,7 +735,7 @@ class WTVShared {
log(" *** Reading global configuration...");
try {
var minisrv_config = this.parseJSON(this.fs.readFileSync(this.getAbsolutePath("includes" + this.path.sep + "config.json", this.appdir)));
minisrv_config = this.parseJSON(this.fs.readFileSync(this.getAbsolutePath("includes" + this.path.sep + "config.json", this.appdir)));
} catch (e) {
throw new Error("ERROR: Could not read config.json", e);
}
@@ -746,7 +743,7 @@ class WTVShared {
if (user_config) {
log(" *** Reading user configuration...");
try {
let minisrv_user_config = this.getUserConfig();
const minisrv_user_config = this.getUserConfig();
minisrv_config = this.integrateConfig(minisrv_config, minisrv_user_config);
} catch (e) {
logError("ERROR: Could not integrate user_config.json", e);
@@ -804,11 +801,11 @@ class WTVShared {
writeToUserConfig(config) {
if (config) {
try {
var minisrv_user_config = this.getUserConfig();
const minisrv_user_config = this.getUserConfig();
// write back
try {
var new_user_config = {};
const new_user_config = {};
Object.assign(new_user_config, minisrv_user_config, config);
if (this.minisrv_config.config.debug_flags.debug) console.log(" * Writing new user configuration...");
this.fs.writeFileSync(this.getAbsolutePath("user_config.json", this.appdir), JSON.stringify(new_user_config, null, "\t"));
@@ -999,7 +996,7 @@ class WTVShared {
}
// Allocate a buffer of the correct size
let decoded = Buffer.alloc(bufferLength);
const decoded = Buffer.alloc(bufferLength);
let bufferIndex = 0;
for (let i = 0; i < encoded.length; i++) {
@@ -1082,11 +1079,11 @@ class WTVShared {
const filterPasswords = this.minisrv_config.config.filter_passwords_in_logs === true;
try {
// Assuming CryptoJS.enc.Utf8 exists and has a stringify method
let post_text = CryptoJS.enc.Utf8.stringify(obj.post_data);
let params = new URLSearchParams(post_text);
const post_text = CryptoJS.enc.Utf8.stringify(obj.post_data);
const params = new URLSearchParams(post_text);
if (filterPasswords) {
for (let [key, value] of params) {
for (const [key, value] of params) {
const lowerKey = key.toLowerCase();
if (/passw(or)?d|^pass$/.test(lowerKey)) {
params.set(key, '*'.repeat(value.length));
@@ -1115,7 +1112,7 @@ class WTVShared {
// Prevent usage
return;
// Search for the module in the require cache
let resolvedPath = require.resolve(moduleName);
const resolvedPath = require.resolve(moduleName);
// Remove the module from the cache
if (require.cache[resolvedPath]) {
@@ -1460,8 +1457,8 @@ class WTVShared {
* @returns {object} The modified object
*/
moveObjectKey(currentKey, destKey, obj, case_insensitive = false) {
let keys = Object.keys(obj);
let values = Object.values(obj);
const keys = Object.keys(obj);
const values = Object.values(obj);
const currentIndex = typeof currentKey === 'string' ? this.findObjectKeyIndex(currentKey, obj, case_insensitive) : +currentKey;
if (currentIndex === -1) return obj;