Files
SnowLuma/.pnpm-store/v11/files/57/c1a4bebeb4ad55cd5c86423c5c81fc78f5b0cb132b8e3e704374e7551cb12e6acc8a67a9dbf341a23c3ef0dec778a29bba4b73e49cd08caa71cfc1e02d82bd
TanTan 2a69b23b47
Dev Build / cleanup (push) Waiting to run
Dev Build / typecheck (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

51 lines
1.4 KiB
Plaintext

Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_router = require("../../router.js");
//#region src/router/smart-router/router.ts
var SmartRouter = class {
name = "SmartRouter";
#routers = [];
#routes = [];
constructor(init) {
this.#routers = init.routers;
}
add(method, path, handler) {
if (!this.#routes) throw new Error(require_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 i = 0, len = routes.length; i < len; i++) router.add(...routes[i]);
res = router.match(method, path);
} catch (e) {
if (e instanceof require_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];
}
};
//#endregion
exports.SmartRouter = SmartRouter;