rjweb-server
Version:
Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS
98 lines (97 loc) • 3.58 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var parseURL_exports = {};
__export(parseURL_exports, {
default: () => parseURL
});
module.exports = __toCommonJS(parseURL_exports);
function parseURL(path) {
path = "/".concat(path);
let queryFirst = false;
const returns = {
href: "",
path: "",
query: "",
fragments: ""
};
let progress = 0, mode = "path";
while (progress < path.length) {
switch (mode) {
case "path": {
const slicedPath = path.slice(progress);
let splitterPos = slicedPath.indexOf("/");
if (splitterPos === -1) {
const queryPos = slicedPath.indexOf("?"), fragmentPos = slicedPath.indexOf("#");
if (queryPos >= 0 && queryPos < (fragmentPos === -1 ? Infinity : fragmentPos)) {
splitterPos = queryPos;
} else if (fragmentPos >= 0) {
splitterPos = fragmentPos;
} else
splitterPos = path.length - progress;
}
if (path[progress + splitterPos] === "?" || path[progress + splitterPos] === "#") {
mode = path[progress + splitterPos] === "?" ? "query" : "fragments";
if (mode === "query")
queryFirst = true;
}
if (splitterPos > 0)
returns.path += "/";
returns.path += path.slice(progress, progress + splitterPos);
progress += splitterPos + 1;
break;
}
case "fragments":
case "query": {
const symbol = mode === "fragments" ? "?" : "#";
let splitterPos = path.slice(progress).indexOf(symbol);
if (splitterPos === -1)
splitterPos = path.length - progress;
returns[mode] = path.slice(progress, progress + splitterPos);
if (splitterPos !== path.length - progress)
mode = mode === "fragments" ? "query" : "fragments";
progress += splitterPos + 1;
break;
}
}
}
if (!returns.path)
returns.path = "/";
if (returns.path.length > 1 && returns.path[returns.path.length - 1] === "/")
returns.path = returns.path.slice(0, -1);
returns.href = returns.path;
if (returns.query || returns.fragments) {
if (queryFirst && returns.query) {
returns.href = returns.href.concat("?", returns.query);
if (returns.fragments) {
returns.href = returns.href.concat("#", returns.fragments);
}
} else if (!queryFirst && returns.query) {
if (returns.fragments) {
returns.href = returns.href.concat("#", returns.fragments, "?", returns.query);
} else {
returns.href = returns.href.concat("?", returns.query);
}
} else if (returns.fragments) {
returns.href = returns.href.concat("#", returns.fragments);
}
}
returns.path = decodeURI(returns.path);
returns.href = decodeURI(returns.href);
return returns;
}