1
0
Fork 0
forked from svrjs/svrjs

Lint out the codebase.

This commit is contained in:
Dorian Niemiec 2024-08-24 17:23:06 +02:00
parent f48125f4f2
commit f552b34878
7 changed files with 51 additions and 68 deletions

View file

@ -464,20 +464,7 @@ module.exports = (req, res, logFacilities, config, next) => {
/{server}/g, /{server}/g,
"" + "" +
( (
(config.exposeServerVersion config.generateServerString() +
? "SVR.JS/" +
version +
" (" +
getOS() +
"; " +
(process.isBun
? "Bun/v" +
process.versions.bun +
"; like Node.JS/" +
process.version
: "Node.JS/" + process.version) +
")"
: "SVR.JS") +
(!config.exposeModsInErrorPages || extName == undefined (!config.exposeModsInErrorPages || extName == undefined
? "" ? ""
: " " + extName) : " " + extName)

View file

@ -1,20 +1,20 @@
const svrjsInfo = require("../../svrjs.json"); const svrjsInfo = require("../../svrjs.json");
const {version, name} = svrjsInfo; const { version, name } = svrjsInfo;
const getOS = require("./getOS.js"); const getOS = require("./getOS.js");
function generateServerString(exposeServerVersion) { function generateServerString(exposeServerVersion) {
return exposeServerVersion return exposeServerVersion
? name + ? name +
"/" + "/" +
version + version +
" (" + " (" +
getOS() + getOS() +
"; " + "; " +
(process.isBun (process.isBun
? "Bun/v" + process.versions.bun + "; like Node.JS/" + process.version ? "Bun/v" + process.versions.bun + "; like Node.JS/" + process.version
: "Node.JS/" + process.version) + : "Node.JS/" + process.version) +
")" ")"
: name; : name;
} }
module.exports = generateServerString; module.exports = generateServerString;

View file

@ -1,24 +1,20 @@
function matchHostname(hostname, reqHostname) { function matchHostname(hostname, reqHostname) {
if (typeof hostname == "undefined" || hostname == "*") { if (typeof hostname == "undefined" || hostname == "*") {
return true; return true;
} else if ( } else if (reqHostname && hostname.indexOf("*.") == 0 && hostname != "*.") {
reqHostname && const hostnamesRoot = hostname.substring(2);
hostname.indexOf("*.") == 0 && if (
hostname != "*." reqHostname == hostnamesRoot ||
(reqHostname.length > hostnamesRoot.length &&
reqHostname.indexOf("." + hostnamesRoot) ==
reqHostname.length - hostnamesRoot.length - 1)
) { ) {
const hostnamesRoot = hostname.substring(2); return true;
if (
reqHostname == hostnamesRoot ||
(reqHostname.length > hostnamesRoot.length &&
reqHostname.indexOf("." + hostnamesRoot) ==
reqHostname.length - hostnamesRoot.length - 1)
) {
return true;
}
} else if (reqHostname && reqHostname == hostname) {
return true;
} }
return false; } else if (reqHostname && reqHostname == hostname) {
}; return true;
}
return false;
}
module.exports = matchHostname; module.exports = matchHostname;

View file

@ -5,7 +5,7 @@ jest.mock("os", () => ({
platform: jest.fn(), platform: jest.fn(),
})); }));
describe("createRegex", () => { describe("Regular expression creation function", () => {
beforeEach(() => { beforeEach(() => {
os.platform.mockReset(); os.platform.mockReset();
}); });

View file

@ -1,6 +1,6 @@
const generateErrorStack = require("../../src/utils/generateErrorStack"); const generateErrorStack = require("../../src/utils/generateErrorStack");
describe("generateErrorStack", () => { describe("Error stack generation function", () => {
test("should return the original stack if it is V8-style", () => { test("should return the original stack if it is V8-style", () => {
const error = new Error("Test error"); const error = new Error("Test error");
error.stack = `Error: Test error error.stack = `Error: Test error

View file

@ -1,6 +1,6 @@
const ipMatch = require("../../src/utils/ipMatch"); const ipMatch = require("../../src/utils/ipMatch");
describe("ipMatch", () => { describe("IP address matching function", () => {
test("should return true if IP1 is empty", () => { test("should return true if IP1 is empty", () => {
expect(ipMatch("", "192.168.1.1")).toBe(true); expect(ipMatch("", "192.168.1.1")).toBe(true);
}); });

View file

@ -1,51 +1,51 @@
const matchHostname = require('../../src/utils/matchHostname'); const matchHostname = require("../../src/utils/matchHostname");
describe('matchHostname', () => { describe("Hostname matching function", () => {
test('should return true if hostname is undefined', () => { test("should return true if hostname is undefined", () => {
expect(matchHostname(undefined, 'example.com')).toBe(true); expect(matchHostname(undefined, "example.com")).toBe(true);
}); });
test('should return true if hostname is "*"', () => { test('should return true if hostname is "*"', () => {
expect(matchHostname('*', 'example.com')).toBe(true); expect(matchHostname("*", "example.com")).toBe(true);
}); });
test('should return true if reqHostname matches hostname exactly', () => { test("should return true if reqHostname matches hostname exactly", () => {
expect(matchHostname('example.com', 'example.com')).toBe(true); expect(matchHostname("example.com", "example.com")).toBe(true);
}); });
test('should return false if reqHostname does not match hostname exactly', () => { test("should return false if reqHostname does not match hostname exactly", () => {
expect(matchHostname('example.com', 'example.org')).toBe(false); expect(matchHostname("example.com", "example.org")).toBe(false);
}); });
test('should return true if hostname starts with "*." and reqHostname matches the root', () => { test('should return true if hostname starts with "*." and reqHostname matches the root', () => {
expect(matchHostname('*.example.com', 'sub.example.com')).toBe(true); expect(matchHostname("*.example.com", "sub.example.com")).toBe(true);
}); });
test('should return false if hostname starts with "*." and reqHostname does not match the root', () => { test('should return false if hostname starts with "*." and reqHostname does not match the root', () => {
expect(matchHostname('*.example.com', 'example.org')).toBe(false); expect(matchHostname("*.example.com", "example.org")).toBe(false);
}); });
test('should return true if hostname starts with "*." and reqHostname is the root', () => { test('should return true if hostname starts with "*." and reqHostname is the root', () => {
expect(matchHostname('*.example.com', 'example.com')).toBe(true); expect(matchHostname("*.example.com", "example.com")).toBe(true);
}); });
test('should return false if hostname is "*."', () => { test('should return false if hostname is "*."', () => {
expect(matchHostname('*.', 'example.com')).toBe(false); expect(matchHostname("*.", "example.com")).toBe(false);
}); });
test('should return false if reqHostname is undefined', () => { test("should return false if reqHostname is undefined", () => {
expect(matchHostname('example.com', undefined)).toBe(false); expect(matchHostname("example.com", undefined)).toBe(false);
}); });
test('should return false if hostname does not start with "*." and reqHostname does not match', () => { test('should return false if hostname does not start with "*." and reqHostname does not match', () => {
expect(matchHostname('sub.example.com', 'example.com')).toBe(false); expect(matchHostname("sub.example.com", "example.com")).toBe(false);
}); });
test('should return true if hostname starts with "*." and reqHostname matches the root with additional subdomains', () => { test('should return true if hostname starts with "*." and reqHostname matches the root with additional subdomains', () => {
expect(matchHostname('*.example.com', 'sub.sub.example.com')).toBe(true); expect(matchHostname("*.example.com", "sub.sub.example.com")).toBe(true);
}); });
test('should return false if hostname starts with "*." and reqHostname does not match the root with additional subdomains', () => { test('should return false if hostname starts with "*." and reqHostname does not match the root with additional subdomains', () => {
expect(matchHostname('*.example.com', 'sub.sub.example.org')).toBe(false); expect(matchHostname("*.example.com", "sub.sub.example.org")).toBe(false);
}); });
}); });