UNPKG

diesel-core

Version:

Web framework built on Web Standards

2 lines (1 loc) 2.34 kB
var O=Object.create;var{getPrototypeOf:P,defineProperty:C,getOwnPropertyNames:K,getOwnPropertyDescriptor:Q}=Object,L=Object.prototype.hasOwnProperty;var V=(q,b,v)=>{v=q!=null?O(P(q)):{};let x=b||!q||!q.__esModule?C(v,"default",{value:q,enumerable:!0}):v;for(let z of K(q))if(!L.call(x,z))C(x,z,{get:()=>q[z],enumerable:!0});return x},J=new WeakMap,W=(q)=>{var b=J.get(q),v;if(b)return b;if(b=C({},"__esModule",{value:!0}),q&&typeof q==="object"||typeof q==="function")K(q).map((x)=>!L.call(b,x)&&C(b,x,{get:()=>q[x],enumerable:!(v=Q(q,x))||v.enumerable}));return J.set(q,b),b},X=(q,b)=>()=>(b||q((b={exports:{}}).exports,b),b.exports);var Y=(q,b)=>{for(var v in b)C(q,v,{get:b[v],enumerable:!0,configurable:!0,set:(x)=>b[v]=()=>x})};var Z=(q,b)=>()=>(q&&(b=q(q=0)),b);class D{children;isEndOfWord;handlers;paramName;middlewares;constructor(){this.children={},this.handlers=new Map,this.isEndOfWord=!1,this.paramName=[],this.middlewares=[]}}class M{root;globalMiddlewares;constructor(){this.root=new D,this.globalMiddlewares=[]}pushMiddleware(q,b){if(!Array.isArray(b))b=[b];if(q==="/"){this.globalMiddlewares.push(...b);return}let v=this.root,x=q.split("/").filter(Boolean);for(let z of x){let A=z;if(z.startsWith(":"))A=":";else if(z.startsWith("*"))v.middlewares.push(...b);if(!v.children[A])v.children[A]=new D;v=v.children[A]}v.middlewares.push(...b)}insert(q,b,v){let x=this.root,z=b.split("/").filter(Boolean);if(b==="/"){x.isEndOfWord=!0,x.handlers.set(q,v),x.paramName=[];return}for(let A of z){let B=A;if(A.startsWith(":"))B=":";if(!x.children[B])x.children[B]=new D;x=x.children[B]}x.paramName=z.filter((A)=>A.startsWith(":")).map((A)=>A.slice(1)),x.handlers.set(q,v),x.isEndOfWord=!0}search(q,b){let v=this.root,x=b.split("/").filter(Boolean),z=this.globalMiddlewares.slice();for(let B of x){if(v.children[B])v=v.children[B];else if(v.children[":"])v=v.children[":"];else if(v.children["*"]){v=v.children["*"];break}else return{handler:z};if(v.middlewares.length>0){let I=v.middlewares;for(let F=0;F<I.length;F++)z.push(I[F])}}let A=v.handlers.get(q);if(A)z.push(A);return{params:v.paramName,handler:z}}}class U{trie;cache=new Map;constructor(){this.trie=new M}add(q,b,v){this.trie.insert(q,b,v)}addMiddleware(q,b){this.trie.pushMiddleware(q,b)}find(q,b){return this.trie.search(q,b)}}export{U as TrieRouter2,M as TrieRouter};