fix: use a function that normalizes the webroot
This commit is contained in:
parent
313e1c16d8
commit
0ca6ec5437
5 changed files with 60 additions and 6 deletions
|
@ -6,14 +6,15 @@ const generateErrorStack = require("../utils/generateErrorStack.js");
|
|||
const serverHTTPErrorDescs = require("../res/httpErrorDescriptions.js");
|
||||
const generateServerString = require("../utils/generateServerString.js");
|
||||
const deepClone = require("../utils/deepClone.js");
|
||||
const normalizeWebroot = require("../utils/normalizeWebroot.js");
|
||||
|
||||
let serverconsole = {};
|
||||
|
||||
function clientErrorHandler(err, socket) {
|
||||
const config = deepClone(process.serverConfig);
|
||||
|
||||
// Determine the webroot from the current working directory if it is not configured
|
||||
if (config.wwwroot === undefined) config.wwwroot = process.cwd();
|
||||
// Normalize the webroot
|
||||
config.wwwroot = normalizeWebroot(config.wwwroot);
|
||||
|
||||
config.generateServerString = () =>
|
||||
generateServerString(config.exposeServerVersion);
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
const generateServerString = require("../utils/generateServerString");
|
||||
const deepClone = require("../utils/deepClone.js");
|
||||
const normalizeWebroot = require("../utils/normalizeWebroot.js");
|
||||
const svrjsInfo = require("../../svrjs.json");
|
||||
const { name } = svrjsInfo;
|
||||
|
||||
|
@ -32,8 +33,8 @@ function proxyHandler(req, socket, head) {
|
|||
// SVR.JS configuration object (modified)
|
||||
const config = deepClone(process.serverConfig);
|
||||
|
||||
// Determine the webroot from the current working directory if it is not configured
|
||||
if (config.wwwroot === undefined) config.wwwroot = process.cwd();
|
||||
// Normalize the webroot
|
||||
config.wwwroot = normalizeWebroot(config.wwwroot);
|
||||
|
||||
config.generateServerString = () =>
|
||||
generateServerString(config.exposeServerVersion);
|
||||
|
|
|
@ -9,6 +9,7 @@ const matchHostname = require("../utils/matchHostname.js");
|
|||
const generateServerString = require("../utils/generateServerString.js");
|
||||
const parseURL = require("../utils/urlParser.js");
|
||||
const deepClone = require("../utils/deepClone.js");
|
||||
const normalizeWebroot = require("../utils/normalizeWebroot.js");
|
||||
const statusCodes = require("../res/statusCodes.js");
|
||||
|
||||
let serverconsole = {};
|
||||
|
@ -37,8 +38,8 @@ function requestHandler(req, res) {
|
|||
config.generateServerString = () =>
|
||||
generateServerString(config.exposeServerVersion);
|
||||
|
||||
// Determine the webroot from the current working directory if it is not configured
|
||||
if (config.wwwroot === undefined) config.wwwroot = process.cwd();
|
||||
// Normalize the webroot
|
||||
config.wwwroot = normalizeWebroot(config.wwwroot);
|
||||
|
||||
// getCustomHeaders() in SVR.JS 3.x
|
||||
config.getCustomHeaders = () => {
|
||||
|
|
16
src/utils/normalizeWebroot.js
Normal file
16
src/utils/normalizeWebroot.js
Normal file
|
@ -0,0 +1,16 @@
|
|||
const path = require("path");
|
||||
const os = require("os");
|
||||
|
||||
function normalizeWebroot(currentWebroot) {
|
||||
if (currentWebroot === undefined) {
|
||||
return process.cwd();
|
||||
} else if (!path.isAbsolute(currentWebroot)) {
|
||||
return (
|
||||
process.cwd() + (os.platform() == "win32" ? "\\" : "/") + currentWebroot
|
||||
);
|
||||
} else {
|
||||
return currentWebroot;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = normalizeWebroot;
|
35
tests/utils/normalizeWebroot.test.js
Normal file
35
tests/utils/normalizeWebroot.test.js
Normal file
|
@ -0,0 +1,35 @@
|
|||
const normalizeWebroot = require("../../src/utils/normalizeWebroot.js");
|
||||
const path = require("path");
|
||||
const os = require("os");
|
||||
|
||||
jest.mock("os");
|
||||
jest.mock("path");
|
||||
|
||||
describe("normalizeWebroot", () => {
|
||||
test("should return process.cwd() when currentWebroot is undefined", () => {
|
||||
const cwdSpy = jest.spyOn(process, "cwd").mockReturnValue("/test");
|
||||
expect(normalizeWebroot(undefined)).toBe("/test");
|
||||
cwdSpy.mockRestore();
|
||||
});
|
||||
|
||||
test("should return the absolute path when currentWebroot is absolute", () => {
|
||||
path.isAbsolute.mockReturnValue(true);
|
||||
expect(normalizeWebroot("/absolute/path")).toBe("/absolute/path");
|
||||
});
|
||||
|
||||
test("should return the relative path with process.cwd() prepended on Unix", () => {
|
||||
path.isAbsolute.mockReturnValue(false);
|
||||
os.platform.mockReturnValue("linux");
|
||||
const cwdSpy = jest.spyOn(process, "cwd").mockReturnValue("/test");
|
||||
expect(normalizeWebroot("relative/path")).toBe("/test/relative/path");
|
||||
cwdSpy.mockRestore();
|
||||
});
|
||||
|
||||
test("should return the relative path with process.cwd() prepended on Windows", () => {
|
||||
path.isAbsolute.mockReturnValue(false);
|
||||
os.platform.mockReturnValue("win32");
|
||||
const cwdSpy = jest.spyOn(process, "cwd").mockReturnValue("C:\\test");
|
||||
expect(normalizeWebroot("relative\\path")).toBe("C:\\test\\relative\\path");
|
||||
cwdSpy.mockRestore();
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue