Aktueller Stand

This commit is contained in:
2026-01-23 01:33:35 +01:00
parent 082dc5e110
commit 2766dd12c5
10109 changed files with 1578841 additions and 77685 deletions

View File

@@ -0,0 +1,28 @@
"use strict";
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 linear_router_exports = {};
__export(linear_router_exports, {
LinearRouter: () => import_router.LinearRouter
});
module.exports = __toCommonJS(linear_router_exports);
var import_router = require("./router");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
LinearRouter
});

View File

@@ -0,0 +1,141 @@
"use strict";
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 router_exports = {};
__export(router_exports, {
LinearRouter: () => LinearRouter
});
module.exports = __toCommonJS(router_exports);
var import_router = require("../../router");
var import_url = require("../../utils/url");
const emptyParams = /* @__PURE__ */ Object.create(null);
const splitPathRe = /\/(:\w+(?:{(?:(?:{[\d,]+})|[^}])+})?)|\/[^\/\?]+|(\?)/g;
const splitByStarRe = /\*/;
class LinearRouter {
name = "LinearRouter";
#routes = [];
add(method, path, handler) {
for (let i = 0, paths = (0, import_url.checkOptionalParameter)(path) || [path], len = paths.length; i < len; i++) {
this.#routes.push([method, paths[i], handler]);
}
}
match(method, path) {
const handlers = [];
ROUTES_LOOP: for (let i = 0, len = this.#routes.length; i < len; i++) {
const [routeMethod, routePath, handler] = this.#routes[i];
if (routeMethod === method || routeMethod === import_router.METHOD_NAME_ALL) {
if (routePath === "*" || routePath === "/*") {
handlers.push([handler, emptyParams]);
continue;
}
const hasStar = routePath.indexOf("*") !== -1;
const hasLabel = routePath.indexOf(":") !== -1;
if (!hasStar && !hasLabel) {
if (routePath === path || routePath + "/" === path) {
handlers.push([handler, emptyParams]);
}
} else if (hasStar && !hasLabel) {
const endsWithStar = routePath.charCodeAt(routePath.length - 1) === 42;
const parts = (endsWithStar ? routePath.slice(0, -2) : routePath).split(splitByStarRe);
const lastIndex = parts.length - 1;
for (let j = 0, pos = 0, len2 = parts.length; j < len2; j++) {
const part = parts[j];
const index = path.indexOf(part, pos);
if (index !== pos) {
continue ROUTES_LOOP;
}
pos += part.length;
if (j === lastIndex) {
if (!endsWithStar && pos !== path.length && !(pos === path.length - 1 && path.charCodeAt(pos) === 47)) {
continue ROUTES_LOOP;
}
} else {
const index2 = path.indexOf("/", pos);
if (index2 === -1) {
continue ROUTES_LOOP;
}
pos = index2;
}
}
handlers.push([handler, emptyParams]);
} else if (hasLabel && !hasStar) {
const params = /* @__PURE__ */ Object.create(null);
const parts = routePath.match(splitPathRe);
const lastIndex = parts.length - 1;
for (let j = 0, pos = 0, len2 = parts.length; j < len2; j++) {
if (pos === -1 || pos >= path.length) {
continue ROUTES_LOOP;
}
const part = parts[j];
if (part.charCodeAt(1) === 58) {
if (path.charCodeAt(pos) !== 47) {
continue ROUTES_LOOP;
}
let name = part.slice(2);
let value;
if (name.charCodeAt(name.length - 1) === 125) {
const openBracePos = name.indexOf("{");
const next = parts[j + 1];
const lookahead = next && next[1] !== ":" && next[1] !== "*" ? `(?=${next})` : "";
const pattern = name.slice(openBracePos + 1, -1) + lookahead;
const restPath = path.slice(pos + 1);
const match = new RegExp(pattern, "d").exec(restPath);
if (!match || match.indices[0][0] !== 0 || match.indices[0][1] === 0) {
continue ROUTES_LOOP;
}
name = name.slice(0, openBracePos);
value = restPath.slice(...match.indices[0]);
pos += match.indices[0][1] + 1;
} else {
let endValuePos = path.indexOf("/", pos + 1);
if (endValuePos === -1) {
if (pos + 1 === path.length) {
continue ROUTES_LOOP;
}
endValuePos = path.length;
}
value = path.slice(pos + 1, endValuePos);
pos = endValuePos;
}
params[name] ||= value;
} else {
const index = path.indexOf(part, pos);
if (index !== pos) {
continue ROUTES_LOOP;
}
pos += part.length;
}
if (j === lastIndex) {
if (pos !== path.length && !(pos === path.length - 1 && path.charCodeAt(pos) === 47)) {
continue ROUTES_LOOP;
}
}
}
handlers.push([handler, params]);
} else if (hasLabel && hasStar) {
throw new import_router.UnsupportedPathError();
}
}
}
return [handlers];
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
LinearRouter
});

View File

@@ -0,0 +1,28 @@
"use strict";
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 pattern_router_exports = {};
__export(pattern_router_exports, {
PatternRouter: () => import_router.PatternRouter
});
module.exports = __toCommonJS(pattern_router_exports);
var import_router = require("./router");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
PatternRouter
});

View File

@@ -0,0 +1,71 @@
"use strict";
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 router_exports = {};
__export(router_exports, {
PatternRouter: () => PatternRouter
});
module.exports = __toCommonJS(router_exports);
var import_router = require("../../router");
const emptyParams = /* @__PURE__ */ Object.create(null);
class PatternRouter {
name = "PatternRouter";
#routes = [];
add(method, path, handler) {
const endsWithWildcard = path.at(-1) === "*";
if (endsWithWildcard) {
path = path.slice(0, -2);
}
if (path.at(-1) === "?") {
path = path.slice(0, -1);
this.add(method, path.replace(/\/[^/]+$/, ""), handler);
}
const parts = (path.match(/\/?(:\w+(?:{(?:(?:{[\d,]+})|[^}])+})?)|\/?[^\/\?]+/g) || []).map(
(part) => {
const match = part.match(/^\/:([^{]+)(?:{(.*)})?/);
return match ? `/(?<${match[1]}>${match[2] || "[^/]+"})` : part === "/*" ? "/[^/]+" : part.replace(/[.\\+*[^\]$()]/g, "\\$&");
}
);
try {
this.#routes.push([
new RegExp(`^${parts.join("")}${endsWithWildcard ? "" : "/?$"}`),
method,
handler
]);
} catch {
throw new import_router.UnsupportedPathError();
}
}
match(method, path) {
const handlers = [];
for (let i = 0, len = this.#routes.length; i < len; i++) {
const [pattern, routeMethod, handler] = this.#routes[i];
if (routeMethod === method || routeMethod === import_router.METHOD_NAME_ALL) {
const match = pattern.exec(path);
if (match) {
handlers.push([handler, match.groups || emptyParams]);
}
}
}
return [handlers];
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
PatternRouter
});

View File

@@ -0,0 +1,35 @@
"use strict";
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 reg_exp_router_exports = {};
__export(reg_exp_router_exports, {
PreparedRegExpRouter: () => import_prepared_router.PreparedRegExpRouter,
RegExpRouter: () => import_router.RegExpRouter,
buildInitParams: () => import_prepared_router.buildInitParams,
serializeInitParams: () => import_prepared_router.serializeInitParams
});
module.exports = __toCommonJS(reg_exp_router_exports);
var import_router = require("./router");
var import_prepared_router = require("./prepared-router");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
PreparedRegExpRouter,
RegExpRouter,
buildInitParams,
serializeInitParams
});

View File

@@ -0,0 +1,49 @@
"use strict";
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 matcher_exports = {};
__export(matcher_exports, {
emptyParam: () => emptyParam,
match: () => match
});
module.exports = __toCommonJS(matcher_exports);
var import_router = require("../../router");
const emptyParam = [];
function match(method, path) {
const matchers = this.buildAllMatchers();
const match2 = ((method2, path2) => {
const matcher = matchers[method2] || matchers[import_router.METHOD_NAME_ALL];
const staticMatch = matcher[2][path2];
if (staticMatch) {
return staticMatch;
}
const match3 = path2.match(matcher[0]);
if (!match3) {
return [[], emptyParam];
}
const index = match3.indexOf("", 1);
return [matcher[1][index], match3];
});
this.match = match2;
return match2(method, path);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
emptyParam,
match
});

View File

@@ -0,0 +1,135 @@
"use strict";
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 node_exports = {};
__export(node_exports, {
Node: () => Node,
PATH_ERROR: () => PATH_ERROR
});
module.exports = __toCommonJS(node_exports);
const LABEL_REG_EXP_STR = "[^/]+";
const ONLY_WILDCARD_REG_EXP_STR = ".*";
const TAIL_WILDCARD_REG_EXP_STR = "(?:|/.*)";
const PATH_ERROR = /* @__PURE__ */ Symbol();
const regExpMetaChars = new Set(".\\+*[^]$()");
function compareKey(a, b) {
if (a.length === 1) {
return b.length === 1 ? a < b ? -1 : 1 : -1;
}
if (b.length === 1) {
return 1;
}
if (a === ONLY_WILDCARD_REG_EXP_STR || a === TAIL_WILDCARD_REG_EXP_STR) {
return 1;
} else if (b === ONLY_WILDCARD_REG_EXP_STR || b === TAIL_WILDCARD_REG_EXP_STR) {
return -1;
}
if (a === LABEL_REG_EXP_STR) {
return 1;
} else if (b === LABEL_REG_EXP_STR) {
return -1;
}
return a.length === b.length ? a < b ? -1 : 1 : b.length - a.length;
}
class Node {
#index;
#varIndex;
#children = /* @__PURE__ */ Object.create(null);
insert(tokens, index, paramMap, context, pathErrorCheckOnly) {
if (tokens.length === 0) {
if (this.#index !== void 0) {
throw PATH_ERROR;
}
if (pathErrorCheckOnly) {
return;
}
this.#index = index;
return;
}
const [token, ...restTokens] = tokens;
const pattern = token === "*" ? restTokens.length === 0 ? ["", "", ONLY_WILDCARD_REG_EXP_STR] : ["", "", LABEL_REG_EXP_STR] : token === "/*" ? ["", "", TAIL_WILDCARD_REG_EXP_STR] : token.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/);
let node;
if (pattern) {
const name = pattern[1];
let regexpStr = pattern[2] || LABEL_REG_EXP_STR;
if (name && pattern[2]) {
if (regexpStr === ".*") {
throw PATH_ERROR;
}
regexpStr = regexpStr.replace(/^\((?!\?:)(?=[^)]+\)$)/, "(?:");
if (/\((?!\?:)/.test(regexpStr)) {
throw PATH_ERROR;
}
}
node = this.#children[regexpStr];
if (!node) {
if (Object.keys(this.#children).some(
(k) => k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR
)) {
throw PATH_ERROR;
}
if (pathErrorCheckOnly) {
return;
}
node = this.#children[regexpStr] = new Node();
if (name !== "") {
node.#varIndex = context.varIndex++;
}
}
if (!pathErrorCheckOnly && name !== "") {
paramMap.push([name, node.#varIndex]);
}
} else {
node = this.#children[token];
if (!node) {
if (Object.keys(this.#children).some(
(k) => k.length > 1 && k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR
)) {
throw PATH_ERROR;
}
if (pathErrorCheckOnly) {
return;
}
node = this.#children[token] = new Node();
}
}
node.insert(restTokens, index, paramMap, context, pathErrorCheckOnly);
}
buildRegExpStr() {
const childKeys = Object.keys(this.#children).sort(compareKey);
const strList = childKeys.map((k) => {
const c = this.#children[k];
return (typeof c.#varIndex === "number" ? `(${k})@${c.#varIndex}` : regExpMetaChars.has(k) ? `\\${k}` : k) + c.buildRegExpStr();
});
if (typeof this.#index === "number") {
strList.unshift(`#${this.#index}`);
}
if (strList.length === 0) {
return "";
}
if (strList.length === 1) {
return strList[0];
}
return "(?:" + strList.join("|") + ")";
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Node,
PATH_ERROR
});

View File

@@ -0,0 +1,167 @@
"use strict";
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 prepared_router_exports = {};
__export(prepared_router_exports, {
PreparedRegExpRouter: () => PreparedRegExpRouter,
buildInitParams: () => buildInitParams,
serializeInitParams: () => serializeInitParams
});
module.exports = __toCommonJS(prepared_router_exports);
var import_router = require("../../router");
var import_matcher = require("./matcher");
var import_router2 = require("./router");
class PreparedRegExpRouter {
name = "PreparedRegExpRouter";
#matchers;
#relocateMap;
constructor(matchers, relocateMap) {
this.#matchers = matchers;
this.#relocateMap = relocateMap;
}
#addWildcard(method, handlerData) {
const matcher = this.#matchers[method];
matcher[1].forEach((list) => list && list.push(handlerData));
Object.values(matcher[2]).forEach((list) => list[0].push(handlerData));
}
#addPath(method, path, handler, indexes, map) {
const matcher = this.#matchers[method];
if (!map) {
matcher[2][path][0].push([handler, {}]);
} else {
indexes.forEach((index) => {
if (typeof index === "number") {
matcher[1][index].push([handler, map]);
} else {
;
matcher[2][index || path][0].push([handler, map]);
}
});
}
}
add(method, path, handler) {
if (!this.#matchers[method]) {
const all = this.#matchers[import_router.METHOD_NAME_ALL];
const staticMap = {};
for (const key in all[2]) {
staticMap[key] = [all[2][key][0].slice(), import_matcher.emptyParam];
}
this.#matchers[method] = [
all[0],
all[1].map((list) => Array.isArray(list) ? list.slice() : 0),
staticMap
];
}
if (path === "/*" || path === "*") {
const handlerData = [handler, {}];
if (method === import_router.METHOD_NAME_ALL) {
for (const m in this.#matchers) {
this.#addWildcard(m, handlerData);
}
} else {
this.#addWildcard(method, handlerData);
}
return;
}
const data = this.#relocateMap[path];
if (!data) {
throw new Error(`Path ${path} is not registered`);
}
for (const [indexes, map] of data) {
if (method === import_router.METHOD_NAME_ALL) {
for (const m in this.#matchers) {
this.#addPath(m, path, handler, indexes, map);
}
} else {
this.#addPath(method, path, handler, indexes, map);
}
}
}
buildAllMatchers() {
return this.#matchers;
}
match = import_matcher.match;
}
const buildInitParams = ({ paths }) => {
const RegExpRouterWithMatcherExport = class extends import_router2.RegExpRouter {
buildAndExportAllMatchers() {
return this.buildAllMatchers();
}
};
const router = new RegExpRouterWithMatcherExport();
for (const path of paths) {
router.add(import_router.METHOD_NAME_ALL, path, path);
}
const matchers = router.buildAndExportAllMatchers();
const all = matchers[import_router.METHOD_NAME_ALL];
const relocateMap = {};
for (const path of paths) {
if (path === "/*" || path === "*") {
continue;
}
all[1].forEach((list, i) => {
list.forEach(([p, map]) => {
if (p === path) {
if (relocateMap[path]) {
relocateMap[path][0][1] = {
...relocateMap[path][0][1],
...map
};
} else {
relocateMap[path] = [[[], map]];
}
if (relocateMap[path][0][0].findIndex((j) => j === i) === -1) {
relocateMap[path][0][0].push(i);
}
}
});
});
for (const path2 in all[2]) {
all[2][path2][0].forEach(([p]) => {
if (p === path) {
relocateMap[path] ||= [[[]]];
const value = path2 === path ? "" : path2;
if (relocateMap[path][0][0].findIndex((v) => v === value) === -1) {
relocateMap[path][0][0].push(value);
}
}
});
}
}
for (let i = 0, len = all[1].length; i < len; i++) {
all[1][i] = all[1][i] ? [] : 0;
}
for (const path in all[2]) {
all[2][path][0] = [];
}
return [matchers, relocateMap];
};
const serializeInitParams = ([matchers, relocateMap]) => {
const matchersStr = JSON.stringify(
matchers,
(_, value) => value instanceof RegExp ? `##${value.toString()}##` : value
).replace(/"##(.+?)##"/g, (_, str) => str.replace(/\\\\/g, "\\"));
const relocateMapStr = JSON.stringify(relocateMap);
return `[${matchersStr},${relocateMapStr}]`;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
PreparedRegExpRouter,
buildInitParams,
serializeInitParams
});

View File

@@ -0,0 +1,209 @@
"use strict";
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 router_exports = {};
__export(router_exports, {
RegExpRouter: () => RegExpRouter
});
module.exports = __toCommonJS(router_exports);
var import_router = require("../../router");
var import_url = require("../../utils/url");
var import_matcher = require("./matcher");
var import_node = require("./node");
var import_trie = require("./trie");
const nullMatcher = [/^$/, [], /* @__PURE__ */ Object.create(null)];
let wildcardRegExpCache = /* @__PURE__ */ Object.create(null);
function buildWildcardRegExp(path) {
return wildcardRegExpCache[path] ??= new RegExp(
path === "*" ? "" : `^${path.replace(
/\/\*$|([.\\+*[^\]$()])/g,
(_, metaChar) => metaChar ? `\\${metaChar}` : "(?:|/.*)"
)}$`
);
}
function clearWildcardRegExpCache() {
wildcardRegExpCache = /* @__PURE__ */ Object.create(null);
}
function buildMatcherFromPreprocessedRoutes(routes) {
const trie = new import_trie.Trie();
const handlerData = [];
if (routes.length === 0) {
return nullMatcher;
}
const routesWithStaticPathFlag = routes.map(
(route) => [!/\*|\/:/.test(route[0]), ...route]
).sort(
([isStaticA, pathA], [isStaticB, pathB]) => isStaticA ? 1 : isStaticB ? -1 : pathA.length - pathB.length
);
const staticMap = /* @__PURE__ */ Object.create(null);
for (let i = 0, j = -1, len = routesWithStaticPathFlag.length; i < len; i++) {
const [pathErrorCheckOnly, path, handlers] = routesWithStaticPathFlag[i];
if (pathErrorCheckOnly) {
staticMap[path] = [handlers.map(([h]) => [h, /* @__PURE__ */ Object.create(null)]), import_matcher.emptyParam];
} else {
j++;
}
let paramAssoc;
try {
paramAssoc = trie.insert(path, j, pathErrorCheckOnly);
} catch (e) {
throw e === import_node.PATH_ERROR ? new import_router.UnsupportedPathError(path) : e;
}
if (pathErrorCheckOnly) {
continue;
}
handlerData[j] = handlers.map(([h, paramCount]) => {
const paramIndexMap = /* @__PURE__ */ Object.create(null);
paramCount -= 1;
for (; paramCount >= 0; paramCount--) {
const [key, value] = paramAssoc[paramCount];
paramIndexMap[key] = value;
}
return [h, paramIndexMap];
});
}
const [regexp, indexReplacementMap, paramReplacementMap] = trie.buildRegExp();
for (let i = 0, len = handlerData.length; i < len; i++) {
for (let j = 0, len2 = handlerData[i].length; j < len2; j++) {
const map = handlerData[i][j]?.[1];
if (!map) {
continue;
}
const keys = Object.keys(map);
for (let k = 0, len3 = keys.length; k < len3; k++) {
map[keys[k]] = paramReplacementMap[map[keys[k]]];
}
}
}
const handlerMap = [];
for (const i in indexReplacementMap) {
handlerMap[i] = handlerData[indexReplacementMap[i]];
}
return [regexp, handlerMap, staticMap];
}
function findMiddleware(middleware, path) {
if (!middleware) {
return void 0;
}
for (const k of Object.keys(middleware).sort((a, b) => b.length - a.length)) {
if (buildWildcardRegExp(k).test(path)) {
return [...middleware[k]];
}
}
return void 0;
}
class RegExpRouter {
name = "RegExpRouter";
#middleware;
#routes;
constructor() {
this.#middleware = { [import_router.METHOD_NAME_ALL]: /* @__PURE__ */ Object.create(null) };
this.#routes = { [import_router.METHOD_NAME_ALL]: /* @__PURE__ */ Object.create(null) };
}
add(method, path, handler) {
const middleware = this.#middleware;
const routes = this.#routes;
if (!middleware || !routes) {
throw new Error(import_router.MESSAGE_MATCHER_IS_ALREADY_BUILT);
}
if (!middleware[method]) {
;
[middleware, routes].forEach((handlerMap) => {
handlerMap[method] = /* @__PURE__ */ Object.create(null);
Object.keys(handlerMap[import_router.METHOD_NAME_ALL]).forEach((p) => {
handlerMap[method][p] = [...handlerMap[import_router.METHOD_NAME_ALL][p]];
});
});
}
if (path === "/*") {
path = "*";
}
const paramCount = (path.match(/\/:/g) || []).length;
if (/\*$/.test(path)) {
const re = buildWildcardRegExp(path);
if (method === import_router.METHOD_NAME_ALL) {
Object.keys(middleware).forEach((m) => {
middleware[m][path] ||= findMiddleware(middleware[m], path) || findMiddleware(middleware[import_router.METHOD_NAME_ALL], path) || [];
});
} else {
middleware[method][path] ||= findMiddleware(middleware[method], path) || findMiddleware(middleware[import_router.METHOD_NAME_ALL], path) || [];
}
Object.keys(middleware).forEach((m) => {
if (method === import_router.METHOD_NAME_ALL || method === m) {
Object.keys(middleware[m]).forEach((p) => {
re.test(p) && middleware[m][p].push([handler, paramCount]);
});
}
});
Object.keys(routes).forEach((m) => {
if (method === import_router.METHOD_NAME_ALL || method === m) {
Object.keys(routes[m]).forEach(
(p) => re.test(p) && routes[m][p].push([handler, paramCount])
);
}
});
return;
}
const paths = (0, import_url.checkOptionalParameter)(path) || [path];
for (let i = 0, len = paths.length; i < len; i++) {
const path2 = paths[i];
Object.keys(routes).forEach((m) => {
if (method === import_router.METHOD_NAME_ALL || method === m) {
routes[m][path2] ||= [
...findMiddleware(middleware[m], path2) || findMiddleware(middleware[import_router.METHOD_NAME_ALL], path2) || []
];
routes[m][path2].push([handler, paramCount - len + i + 1]);
}
});
}
}
match = import_matcher.match;
buildAllMatchers() {
const matchers = /* @__PURE__ */ Object.create(null);
Object.keys(this.#routes).concat(Object.keys(this.#middleware)).forEach((method) => {
matchers[method] ||= this.#buildMatcher(method);
});
this.#middleware = this.#routes = void 0;
clearWildcardRegExpCache();
return matchers;
}
#buildMatcher(method) {
const routes = [];
let hasOwnRoute = method === import_router.METHOD_NAME_ALL;
[this.#middleware, this.#routes].forEach((r) => {
const ownRoute = r[method] ? Object.keys(r[method]).map((path) => [path, r[method][path]]) : [];
if (ownRoute.length !== 0) {
hasOwnRoute ||= true;
routes.push(...ownRoute);
} else if (method !== import_router.METHOD_NAME_ALL) {
routes.push(
...Object.keys(r[import_router.METHOD_NAME_ALL]).map((path) => [path, r[import_router.METHOD_NAME_ALL][path]])
);
}
});
if (!hasOwnRoute) {
return null;
} else {
return buildMatcherFromPreprocessedRoutes(routes);
}
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
RegExpRouter
});

View File

@@ -0,0 +1,82 @@
"use strict";
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 trie_exports = {};
__export(trie_exports, {
Trie: () => Trie
});
module.exports = __toCommonJS(trie_exports);
var import_node = require("./node");
class Trie {
#context = { varIndex: 0 };
#root = new import_node.Node();
insert(path, index, pathErrorCheckOnly) {
const paramAssoc = [];
const groups = [];
for (let i = 0; ; ) {
let replaced = false;
path = path.replace(/\{[^}]+\}/g, (m) => {
const mark = `@\\${i}`;
groups[i] = [mark, m];
i++;
replaced = true;
return mark;
});
if (!replaced) {
break;
}
}
const tokens = path.match(/(?::[^\/]+)|(?:\/\*$)|./g) || [];
for (let i = groups.length - 1; i >= 0; i--) {
const [mark] = groups[i];
for (let j = tokens.length - 1; j >= 0; j--) {
if (tokens[j].indexOf(mark) !== -1) {
tokens[j] = tokens[j].replace(mark, groups[i][1]);
break;
}
}
}
this.#root.insert(tokens, index, paramAssoc, this.#context, pathErrorCheckOnly);
return paramAssoc;
}
buildRegExp() {
let regexp = this.#root.buildRegExpStr();
if (regexp === "") {
return [/^$/, [], []];
}
let captureIndex = 0;
const indexReplacementMap = [];
const paramReplacementMap = [];
regexp = regexp.replace(/#(\d+)|@(\d+)|\.\*\$/g, (_, handlerIndex, paramIndex) => {
if (handlerIndex !== void 0) {
indexReplacementMap[++captureIndex] = Number(handlerIndex);
return "$()";
}
if (paramIndex !== void 0) {
paramReplacementMap[Number(paramIndex)] = ++captureIndex;
return "";
}
return "";
});
return [new RegExp(`^${regexp}`), indexReplacementMap, paramReplacementMap];
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Trie
});

View File

@@ -0,0 +1,28 @@
"use strict";
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 smart_router_exports = {};
__export(smart_router_exports, {
SmartRouter: () => import_router.SmartRouter
});
module.exports = __toCommonJS(smart_router_exports);
var import_router = require("./router");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
SmartRouter
});

View File

@@ -0,0 +1,81 @@
"use strict";
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 router_exports = {};
__export(router_exports, {
SmartRouter: () => SmartRouter
});
module.exports = __toCommonJS(router_exports);
var import_router = require("../../router");
class SmartRouter {
name = "SmartRouter";
#routers = [];
#routes = [];
constructor(init) {
this.#routers = init.routers;
}
add(method, path, handler) {
if (!this.#routes) {
throw new Error(import_router.MESSAGE_MATCHER_IS_ALREADY_BUILT);
}
this.#routes.push([method, path, handler]);
}
match(method, path) {
if (!this.#routes) {
throw new Error("Fatal error");
}
const routers = this.#routers;
const routes = this.#routes;
const len = routers.length;
let i = 0;
let res;
for (; i < len; i++) {
const router = routers[i];
try {
for (let i2 = 0, len2 = routes.length; i2 < len2; i2++) {
router.add(...routes[i2]);
}
res = router.match(method, path);
} catch (e) {
if (e instanceof import_router.UnsupportedPathError) {
continue;
}
throw e;
}
this.match = router.match.bind(router);
this.#routers = [router];
this.#routes = void 0;
break;
}
if (i === len) {
throw new Error("Fatal error");
}
this.name = `SmartRouter + ${this.activeRouter.name}`;
return res;
}
get activeRouter() {
if (this.#routes || this.#routers.length !== 1) {
throw new Error("No active router has been determined yet.");
}
return this.#routers[0];
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
SmartRouter
});

View File

@@ -0,0 +1,28 @@
"use strict";
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 trie_router_exports = {};
__export(trie_router_exports, {
TrieRouter: () => import_router.TrieRouter
});
module.exports = __toCommonJS(trie_router_exports);
var import_router = require("./router");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
TrieRouter
});

View File

@@ -0,0 +1,185 @@
"use strict";
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 node_exports = {};
__export(node_exports, {
Node: () => Node
});
module.exports = __toCommonJS(node_exports);
var import_router = require("../../router");
var import_url = require("../../utils/url");
const emptyParams = /* @__PURE__ */ Object.create(null);
class Node {
#methods;
#children;
#patterns;
#order = 0;
#params = emptyParams;
constructor(method, handler, children) {
this.#children = children || /* @__PURE__ */ Object.create(null);
this.#methods = [];
if (method && handler) {
const m = /* @__PURE__ */ Object.create(null);
m[method] = { handler, possibleKeys: [], score: 0 };
this.#methods = [m];
}
this.#patterns = [];
}
insert(method, path, handler) {
this.#order = ++this.#order;
let curNode = this;
const parts = (0, import_url.splitRoutingPath)(path);
const possibleKeys = [];
for (let i = 0, len = parts.length; i < len; i++) {
const p = parts[i];
const nextP = parts[i + 1];
const pattern = (0, import_url.getPattern)(p, nextP);
const key = Array.isArray(pattern) ? pattern[0] : p;
if (key in curNode.#children) {
curNode = curNode.#children[key];
if (pattern) {
possibleKeys.push(pattern[1]);
}
continue;
}
curNode.#children[key] = new Node();
if (pattern) {
curNode.#patterns.push(pattern);
possibleKeys.push(pattern[1]);
}
curNode = curNode.#children[key];
}
curNode.#methods.push({
[method]: {
handler,
possibleKeys: possibleKeys.filter((v, i, a) => a.indexOf(v) === i),
score: this.#order
}
});
return curNode;
}
#getHandlerSets(node, method, nodeParams, params) {
const handlerSets = [];
for (let i = 0, len = node.#methods.length; i < len; i++) {
const m = node.#methods[i];
const handlerSet = m[method] || m[import_router.METHOD_NAME_ALL];
const processedSet = {};
if (handlerSet !== void 0) {
handlerSet.params = /* @__PURE__ */ Object.create(null);
handlerSets.push(handlerSet);
if (nodeParams !== emptyParams || params && params !== emptyParams) {
for (let i2 = 0, len2 = handlerSet.possibleKeys.length; i2 < len2; i2++) {
const key = handlerSet.possibleKeys[i2];
const processed = processedSet[handlerSet.score];
handlerSet.params[key] = params?.[key] && !processed ? params[key] : nodeParams[key] ?? params?.[key];
processedSet[handlerSet.score] = true;
}
}
}
}
return handlerSets;
}
search(method, path) {
const handlerSets = [];
this.#params = emptyParams;
const curNode = this;
let curNodes = [curNode];
const parts = (0, import_url.splitPath)(path);
const curNodesQueue = [];
for (let i = 0, len = parts.length; i < len; i++) {
const part = parts[i];
const isLast = i === len - 1;
const tempNodes = [];
for (let j = 0, len2 = curNodes.length; j < len2; j++) {
const node = curNodes[j];
const nextNode = node.#children[part];
if (nextNode) {
nextNode.#params = node.#params;
if (isLast) {
if (nextNode.#children["*"]) {
handlerSets.push(
...this.#getHandlerSets(nextNode.#children["*"], method, node.#params)
);
}
handlerSets.push(...this.#getHandlerSets(nextNode, method, node.#params));
} else {
tempNodes.push(nextNode);
}
}
for (let k = 0, len3 = node.#patterns.length; k < len3; k++) {
const pattern = node.#patterns[k];
const params = node.#params === emptyParams ? {} : { ...node.#params };
if (pattern === "*") {
const astNode = node.#children["*"];
if (astNode) {
handlerSets.push(...this.#getHandlerSets(astNode, method, node.#params));
astNode.#params = params;
tempNodes.push(astNode);
}
continue;
}
const [key, name, matcher] = pattern;
if (!part && !(matcher instanceof RegExp)) {
continue;
}
const child = node.#children[key];
const restPathString = parts.slice(i).join("/");
if (matcher instanceof RegExp) {
const m = matcher.exec(restPathString);
if (m) {
params[name] = m[0];
handlerSets.push(...this.#getHandlerSets(child, method, node.#params, params));
if (Object.keys(child.#children).length) {
child.#params = params;
const componentCount = m[0].match(/\//)?.length ?? 0;
const targetCurNodes = curNodesQueue[componentCount] ||= [];
targetCurNodes.push(child);
}
continue;
}
}
if (matcher === true || matcher.test(part)) {
params[name] = part;
if (isLast) {
handlerSets.push(...this.#getHandlerSets(child, method, params, node.#params));
if (child.#children["*"]) {
handlerSets.push(
...this.#getHandlerSets(child.#children["*"], method, params, node.#params)
);
}
} else {
child.#params = params;
tempNodes.push(child);
}
}
}
}
curNodes = tempNodes.concat(curNodesQueue.shift() ?? []);
}
if (handlerSets.length > 1) {
handlerSets.sort((a, b) => {
return a.score - b.score;
});
}
return [handlerSets.map(({ handler, params }) => [handler, params])];
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Node
});

View File

@@ -0,0 +1,49 @@
"use strict";
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 router_exports = {};
__export(router_exports, {
TrieRouter: () => TrieRouter
});
module.exports = __toCommonJS(router_exports);
var import_url = require("../../utils/url");
var import_node = require("./node");
class TrieRouter {
name = "TrieRouter";
#node;
constructor() {
this.#node = new import_node.Node();
}
add(method, path, handler) {
const results = (0, import_url.checkOptionalParameter)(path);
if (results) {
for (let i = 0, len = results.length; i < len; i++) {
this.#node.insert(method, results[i], handler);
}
return;
}
this.#node.insert(method, path, handler);
}
match(method, path) {
return this.#node.search(method, path);
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
TrieRouter
});