UNPKG

tezx

Version:

TezX is a high-performance, lightweight JavaScript framework designed for speed, scalability, and flexibility. It enables efficient routing, middleware management, and static file serving with minimal configuration. Fully compatible with Node.js, Deno, an

64 lines (63 loc) 2.03 kB
import { sanitizePathSplit } from "../utils/url.js"; import { CommonHandler } from "./common.js"; import { GlobalConfig } from "./config.js"; export class TriMiddleware { children = new Map(); middlewares = new Set(); isOptional = false; pathname; constructor(pathname = "/") { this.pathname = pathname; if (GlobalConfig.allowDuplicateMw) { this.middlewares = []; } else { this.middlewares = new Set(); } } } export default class MiddlewareConfigure extends CommonHandler { triMiddlewares = new TriMiddleware(); basePath; constructor(basePath = "/") { super(); this.basePath = basePath; } addMiddleware(pathname, middlewares) { const parts = sanitizePathSplit(this.basePath, pathname); let node = this.triMiddlewares; for (const part of parts) { if (part.startsWith("*")) { if (!node.children.has("*")) { node.children.set("*", new TriMiddleware()); } node = node.children.get("*"); } else if (part.startsWith(":")) { const isOptional = part?.endsWith("?"); if (isOptional) { node.isOptional = isOptional; continue; } if (!node.children.has(":")) { node.children.set(":", new TriMiddleware()); } node = node.children.get(":"); } else { if (!node.children.has(part)) { node.children.set(part, new TriMiddleware()); } node = node.children.get(part); } } if (GlobalConfig.allowDuplicateMw) { node.middlewares.push(...middlewares); } else { for (const middleware of middlewares) { node.middlewares.add(middleware); } } } }