@bit-js/blitz
Advanced tools
Comparing version 1.0.7 to 1.0.8
// @bun | ||
var b=Object.defineProperty;var v=(w,K)=>{for(var z in K)b(w,z,{get:K[z],enumerable:!0,configurable:!0,set:(F)=>K[z]=()=>F})};var N={};v(N,{Tree:()=>{{return S}},Radix:()=>{{return V}}});function M(w){const K=[],z=[];let F=w.indexOf(":"),$=0;while(F!==-1){if(F!==$)K.push(w.substring($,F));if($=w.indexOf("/",F+1),$===-1)return z.push(w.substring(F)),[K,z];z.push(w.substring(F,$)),F=w.indexOf(":",$+1)}return K.push(w.substring($)),[K,z]}var L="f",j="p",_="i",y="c",D="c.params",R="m";function Y(w,K){if(K===0)return w;const z=w.indexOf("+");return z===-1?isNaN(+w)?`${w}+${K.toString()}`:(+w+K).toString():`${w.substring(0,z)}+${+w.substring(z+1)+K}`}class k{paramName;store=null;inert=null;constructor(w){if(w==="$")throw new Error('Parameter name should not be "$" to avoid collision with wildcard parameter');this.paramName=w}}class O{store={};size=0;lastChild;put(w){this.lastChild=w,this.store[w.key()]=w,++this.size}}class Z{part;store=null;inert=null;params=null;wildcardStore=null;key(){return this.part.charCodeAt(0).toString()}constructor(w){this.part=w}reset(w,K){this.part=w,this.inert=new O,this.inert.put(K),this.store=this.params=this.wildcardStore=null}clone(w){const K=new Z(w);return K.store=this.store,K.inert=this.inert,K.params=this.params,K.wildcardStore=this.wildcardStore,K}param(w){if(this.params===null)this.params=new k(w);else if(this.params.paramName!==w)throw new Error(`Cannot create route with parameter "${w}" because a route already exists with a different parameter name ("${this.params.paramName}") in the same location`);return this.params}compile(w,K,z,F){const{builder:$}=w,{part:E}=this,T=E.length!==1,J=Y(K,E.length-1);if(T)$.push(w.createTopLevelCheck(E,K,J)),$.push("{");if(this.store!==null)$.push(`if(path.length===${J})${w.yield(this.store)};`);if(this.inert!==null){const Q=Y(J,1);if(this.inert.size===1){const{lastChild:H}=this.inert;$.push(`if(path.charCodeAt(${J})===${H.key()}){`),H.compile(w,Q,z,F),$.push("}")}else{const{store:H}=this.inert;$.push(`switch(path.charCodeAt(${J})){`);for(let G in H)$.push(`case ${G}:`),H[G].compile(w,Q,z,F),$.push("break;");$.push("}")}}if(this.params!==null){const{params:Q}=this,H=z?j:J;if(z){if(!F)$.push("let ");$.push(`${j}=${J};`)}const G=w.searchPath("/",H),U=Q.inert!==null,X=Q.store!==null,{paramName:W}=Q;if(U){if(!z)$.push("let ");$.push(`${_}=${G};`)}if(X){$.push(`if(${U?_:G}===-1){`);const B=w.slicePath(H);$.push(D),$.push(z?`.${W}=${B};`:`={${W}:${B}};`),$.push(w.yield(Q.store)),$.push("}")}if(U){const B=w.substringPath(H,_);if(!X)$.push(`if(${_}!==-1){`);if($.push(D),$.push(z?`.${W}=${B};`:`={${W}:${B}};`),Q.inert.compile(w,Y(_,1),!0,z),!X)$.push("}")}}if(this.wildcardStore!==null){const Q=w.slicePath(J);$.push(D),$.push(z?`.\$=${Q};`:`={\$:${Q}};`),$.push(w.yield(this.wildcardStore)),$.push(";")}if(T)$.push("}")}}var q=`(${y})`;function f(w){return w.length===0?"()":q}class g{K;paramsKeys=[];paramsValues=[];options;currentID=0;constructor(w,K=[]){this.builder=K;w.invokeResultFunction??=!1,this.options=w}insert(w){const K=L+this.currentID.toString();return++this.currentID,this.paramsKeys.push(K),this.paramsValues.push(w),K}yield(w){if(typeof w==="undefined")return"return";if(typeof w!=="function"){if(typeof w!=="symbol"&&typeof w!=="object")return`return ${JSON.stringify(w)}`}const K=this.insert(w);return this.options.invokeResultFunction?`return ${K}${f(w)}`:`return ${K}`}yieldToken(w){return this.options.invokeResultFunction?`return ${w}${q}`:`return ${w}`}slicePath(w){return w==="0"?"path":`path.substring(${w})`}substringPath(w,K){return`path.substring(${w},${K})`}searchPath(w,K){return`path.indexOf('${w}'${K==="0"?"":","+K})`}createTopLevelCheck(w,K,z){if(w.length<15){const F=[];for(let $=1,{length:E}=w;$<E;++$)F.push(`if(path.charCodeAt(${K})===${w.charCodeAt($)})`),K=Y(K,1);return F.join("")}return`if(path.substring(${K},${z})==='${w.substring(1)}')`}build(){return Function(...this.paramsKeys,`return (${y})=>{${this.builder.join("")}}`)(...this.paramsValues)}}var P=function(w,K){if(K===null)return;if(K instanceof Map){const z={};for(let F of K)z[String.fromCharCode(F[0])]=F[1];return z}if(typeof K==="function")return K.toString();return K};class S{root=null;staticMap=null;store(w,K){if(w.includes(":")||w.charCodeAt(w.length-1)===42)this.storeDynamic(w,K);else this.storeStatic(w,K);return K}storeStatic(w,K){if(w.charCodeAt(0)===47)w=w.slice(1);if(w.charCodeAt(w.length-1)===47)w=w.slice(0,-1);this.staticMap??={},this.staticMap[w]??=K}storeDynamic(w,K){if(w.charCodeAt(0)!==47)w="/"+w;const z=w.charCodeAt(w.length-1)===42;if(z)w=w.slice(0,-1);const[F,$]=M(w);let E=this.root??=new Z("/"),T=0;for(let J=0,{length:Q}=F;J<Q;++J){if(J!==0){const G=E.param($[T].slice(1));if(++T,G.inert===null){E=G.inert=new Z(F[J]);continue}E=G.inert}let H=F[J];for(let G=0;;){if(G===H.length){if(G<E.part.length)E.reset(H,E.clone(E.part.slice(G)));break}if(G===E.part.length){if(E.inert===null)E.inert=new O;else{const X=E.inert.store[H.charCodeAt(G).toString()];if(typeof X!=="undefined"){E=X,H=H.slice(G),G=0;continue}}const U=new Z(H.slice(G));E.inert.put(U),E=U;break}if(H[G]!==E.part[G]){const U=new Z(H.slice(G)),X=E.clone(E.part.slice(G));E.reset(E.part.slice(0,G),X),E.inert.put(U),E=U;break}++G}}if(T<$.length)E.param($[T].slice(1)).store??=K;if(z)E.wildcardStore??=K;else E.store??=K}compile(w,K){const{staticMap:z,root:F}=this;if(F===null){if(w.invokeResultFunction===!0){const E=typeof K==="function"?K:()=>K;return z===null?K:(T)=>(z[T.path]??E)(T)}return z===null?()=>K:(E)=>z[E.path]??K}const $=new g(w,[`const{path}=${y};`]);if(z!==null)$.builder.push(`const ${R}=${$.insert(z)}[path];if(typeof ${R}!=='undefined')${$.yieldToken(R)};`);if(F.compile($,"0",!1,!1),F.wildcardStore===null)$.builder.push($.yield(K));return $.build()}debug(w){return JSON.stringify(this.root,P,w)}}class V{tree=new S;constructor(){}routes(w){for(let K=0,{length:z}=w;K<z;++K)this.put(...w[K]);return this}put(w,K){return this.tree.store(w,K),this}static create(w){return new this().routes(w)}buildMatcher(w,K){return w.invokeResultFunction=!1,this.tree.compile(w,K)}buildCaller(w,K){return w.invokeResultFunction=!0,this.tree.compile(w,K)}}function E0(w){if(typeof w==="undefined")return I;const K=[];for(let z in w)K.push(`${z}=${JSON.stringify(w[z])}`);return Function(`return (C)=>{return class A extends C{${K.join()}}}`)()(A)}class A{w;path;pathStart;pathEnd;params;constructor(w){this.req=w;const K=w.url.indexOf("/",12)+1,z=w.url.indexOf("?",K);if(this.pathStart=K,z===-1)this.path=w.url.substring(K),this.pathEnd=w.url.length;else this.path=w.url.substring(K,z),this.pathEnd=z}headers;status;statusText}class I extends A{constructor(){super(...arguments)}headers={}}class C{w;methodRouter;fallbackRouter;fallback=c;constructor(w={}){this.options=w;this.options=w}put(w,K,z){this.methodRouter??={},this.methodRouter[w]??=new V,this.methodRouter[w].put(K,z)}handle(w,K){this.fallbackRouter??=new V,this.fallbackRouter.put(w,K)}buildFallback(){return typeof this.fallbackRouter==="undefined"?this.fallback:this.fallbackRouter.buildCaller(this.options,this.fallback)}build(w=A){const{methodRouter:K}=this,z=this.buildFallback();if(typeof K==="undefined")return($)=>z(new w($));const F={};for(let $ in K)F[$]=K[$].buildCaller(this.options,z);return($)=>(F[$.method]??z)(new A($))}}var c=()=>null;export{N as internal,E0 as extendContext,C as default,I as DefaultContext,A as Context}; | ||
var C=Object.defineProperty;var b=(w,K)=>{for(var z in K)C(w,z,{get:K[z],enumerable:!0,configurable:!0,set:(E)=>K[z]=()=>E})};var N={};b(N,{Tree:()=>{{return S}},Radix:()=>{{return A}}});function M(w){const K=[],z=[];let E=w.indexOf(":"),$=0;while(E!==-1){if(E!==$)K.push(w.substring($,E));if($=w.indexOf("/",E+1),$===-1)return z.push(w.substring(E)),[K,z];z.push(w.substring(E,$)),E=w.indexOf(":",$+1)}return K.push(w.substring($)),[K,z]}var L="f",j="p",Z="i",_="c",W="c.params",R="m";function X(w,K){if(K===0)return w;const z=w.indexOf("+");return z===-1?isNaN(+w)?`${w}+${K.toString()}`:(+w+K).toString():`${w.substring(0,z)}+${+w.substring(z+1)+K}`}class k{paramName;store=null;inert=null;constructor(w){if(w==="$")throw new Error('Parameter name should not be "$" to avoid collision with wildcard parameter');this.paramName=w}}class O{store={};size=0;lastChild;put(w){this.lastChild=w,this.store[w.key()]=w,++this.size}}class Y{part;store=null;inert=null;params=null;wildcardStore=null;key(){return this.part.charCodeAt(0).toString()}constructor(w){this.part=w}reset(w,K){this.part=w,this.inert=new O,this.inert.put(K),this.store=this.params=this.wildcardStore=null}clone(w){const K=new Y(w);return K.store=this.store,K.inert=this.inert,K.params=this.params,K.wildcardStore=this.wildcardStore,K}param(w){if(this.params===null)this.params=new k(w);else if(this.params.paramName!==w)throw new Error(`Cannot create route with parameter "${w}" because a route already exists with a different parameter name ("${this.params.paramName}") in the same location`);return this.params}compile(w,K,z,E){const{builder:$}=w,{part:D}=this,Q=D.length!==1,H=X(K,D.length-1);if(Q)$.push(w.createTopLevelCheck(D,K,H)),$.push("{");if(this.store!==null)$.push(`if(path.length===${H})${w.yield(this.store)};`);if(this.inert!==null){const J=X(H,1);if(this.inert.size===1){const{lastChild:G}=this.inert;$.push(`if(path.charCodeAt(${H})===${G.key()}){`),G.compile(w,J,z,E),$.push("}")}else{const{store:G}=this.inert;$.push(`switch(path.charCodeAt(${H})){`);for(let F in G)$.push(`case ${F}:`),G[F].compile(w,J,z,E),$.push("break;");$.push("}")}}if(this.params!==null){const{params:J}=this,G=z?j:H;if(z){if(!E)$.push("let ");$.push(`${j}=${H};`)}const F=w.searchPath("/",G),T=J.inert!==null,U=J.store!==null,{paramName:V}=J;if(T){if(!z)$.push("let ");$.push(`${Z}=${F};`)}if(U){$.push(`if(${T?Z:F}===-1){`);const y=w.slicePath(G);$.push(W),$.push(z?`.${V}=${y};`:`={${V}:${y}};`),$.push(w.yield(J.store)),$.push("}")}if(T){const y=w.substringPath(G,Z);if(!U)$.push(`if(${Z}!==-1){`);if($.push(W),$.push(z?`.${V}=${y};`:`={${V}:${y}};`),J.inert.compile(w,X(Z,1),!0,z),!U)$.push("}")}}if(this.wildcardStore!==null){const J=w.slicePath(H);$.push(W),$.push(z?`.\$=${J};`:`={\$:${J}};`),$.push(w.yield(this.wildcardStore)),$.push(";")}if(Q)$.push("}")}}var q=`(${_})`;function f(w){return w.length===0?"()":q}class g{K;paramsKeys=[];paramsValues=[];options;currentID=0;constructor(w,K=[]){this.builder=K;w.invokeResultFunction??=!1,this.options=w}insert(w){const K=L+this.currentID.toString();return++this.currentID,this.paramsKeys.push(K),this.paramsValues.push(w),K}yield(w){if(typeof w==="undefined")return"return";if(typeof w!=="function"){if(typeof w!=="symbol"&&typeof w!=="object")return`return ${JSON.stringify(w)}`}const K=this.insert(w);return this.options.invokeResultFunction?`return ${K}${f(w)}`:`return ${K}`}yieldToken(w){return this.options.invokeResultFunction?`return ${w}${q}`:`return ${w}`}slicePath(w){return w==="0"?"path":`path.substring(${w})`}substringPath(w,K){return`path.substring(${w},${K})`}searchPath(w,K){return`path.indexOf('${w}'${K==="0"?"":","+K})`}createTopLevelCheck(w,K,z){if(w.length<15){const E=[];for(let $=1,{length:D}=w;$<D;++$)E.push(`if(path.charCodeAt(${K})===${w.charCodeAt($)})`),K=X(K,1);return E.join("")}return`if(path.substring(${K},${z})==='${w.substring(1)}')`}build(){return Function(...this.paramsKeys,`return (${_})=>{${this.builder.join("")}}`)(...this.paramsValues)}}var v=function(w,K){if(K===null)return;if(K instanceof Map){const z={};for(let E of K)z[String.fromCharCode(E[0])]=E[1];return z}if(typeof K==="function")return K.toString();return K};class S{root=null;staticMap=null;store(w,K){if(w.includes(":")||w.charCodeAt(w.length-1)===42)this.storeDynamic(w,K);else this.storeStatic(w,K);return K}storeStatic(w,K){if(w.charCodeAt(0)===47)w=w.slice(1);if(w.charCodeAt(w.length-1)===47)w=w.slice(0,-1);this.staticMap??={},this.staticMap[w]??=K}storeDynamic(w,K){if(w.charCodeAt(0)!==47)w="/"+w;const z=w.charCodeAt(w.length-1)===42;if(z)w=w.slice(0,-1);const[E,$]=M(w);let D=this.root??=new Y("/"),Q=0;for(let H=0,{length:J}=E;H<J;++H){if(H!==0){const F=D.param($[Q].slice(1));if(++Q,F.inert===null){D=F.inert=new Y(E[H]);continue}D=F.inert}let G=E[H];for(let F=0;;){if(F===G.length){if(F<D.part.length)D.reset(G,D.clone(D.part.slice(F)));break}if(F===D.part.length){if(D.inert===null)D.inert=new O;else{const U=D.inert.store[G.charCodeAt(F).toString()];if(typeof U!=="undefined"){D=U,G=G.slice(F),F=0;continue}}const T=new Y(G.slice(F));D.inert.put(T),D=T;break}if(G[F]!==D.part[F]){const T=new Y(G.slice(F)),U=D.clone(D.part.slice(F));D.reset(D.part.slice(0,F),U),D.inert.put(T),D=T;break}++F}}if(Q<$.length)D.param($[Q].slice(1)).store??=K;if(z)D.wildcardStore??=K;else D.store??=K}compile(w,K){const{staticMap:z,root:E}=this;if(E===null){if(w.invokeResultFunction===!0){const D=typeof K==="function"?K:()=>K;return z===null?K:(Q)=>(z[Q.path]??D)(Q)}return z===null?()=>K:(D)=>z[D.path]??K}const $=new g(w,[`const{path}=${_};`]);if(z!==null)$.builder.push(`const ${R}=${$.insert(z)}[path];if(typeof ${R}!=='undefined')${$.yieldToken(R)};`);if(E.compile($,"0",!1,!1),E.wildcardStore===null)$.builder.push($.yield(K));return $.build()}debug(w){return JSON.stringify(this.root,v,w)}}class A{tree=new S;constructor(){}routes(w){for(let K=0,{length:z}=w;K<z;++K)this.put(...w[K]);return this}put(w,K){return this.tree.store(w,K),this}static create(w){return new this().routes(w)}buildMatcher(w,K){return w.invokeResultFunction=!1,this.tree.compile(w,K)}buildCaller(w,K){return w.invokeResultFunction=!0,this.tree.compile(w,K)}}function D0(w,K){if(typeof K==="undefined")return w;const z=[];for(let E in K)z.push(`${E}=${JSON.stringify(K[E])}`);return Function(`return (C)=>{return class A extends C{${z.join()}}}`)()(w)}class B{w;path;pathStart;pathEnd;params;constructor(w){this.req=w;const K=w.url.indexOf("/",12)+1,z=w.url.indexOf("?",K);if(this.pathStart=K,z===-1)this.path=w.url.substring(K),this.pathEnd=w.url.length;else this.path=w.url.substring(K,z),this.pathEnd=z}headers;status;statusText}class P extends B{constructor(){super(...arguments)}headers={}}class I{w;methodRouter;fallbackRouter;fallback=c;constructor(w={}){this.options=w;this.options=w}put(w,K,z){this.methodRouter??={},this.methodRouter[w]??=new A,this.methodRouter[w].put(K,z)}handle(w,K){this.fallbackRouter??=new A,this.fallbackRouter.put(w,K)}buildFallback(){return typeof this.fallbackRouter==="undefined"?this.fallback:this.fallbackRouter.buildCaller(this.options,this.fallback)}build(w=B){const{methodRouter:K}=this,z=this.buildFallback();if(typeof K==="undefined")return($)=>z(new w($));const E={};for(let $ in K)E[$]=K[$].buildCaller(this.options,z);return($)=>(E[$.method]??z)(new B($))}}var c=()=>null;export{N as internal,D0 as extendContext,I as default,P as DefaultContext,B as Context}; |
@@ -38,3 +38,3 @@ import { type Context as BaseContext } from './radix/tree/types'; | ||
} | ||
export declare function extendContext(defaultOpts?: ContextOptions): any; | ||
export declare function extendContext(C: any, defaultOpts?: ContextOptions): any; | ||
export {}; |
{ | ||
"name": "@bit-js/blitz", | ||
"main": "lib/index.js", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"type": "module", | ||
@@ -6,0 +6,0 @@ "devDependencies": { |
16688