diesel-core 0.0.21 → 0.0.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTRIBUTING.md +2 -2
- package/LICENSE +8 -8
- package/README.md +276 -275
- package/build.js +32 -32
- package/dist/ctx.d.ts +1 -0
- package/dist/ctx.js +1 -1
- package/dist/handleRequest.d.ts +1 -0
- package/dist/handleRequest.js +1 -1
- package/dist/main.d.ts +1 -0
- package/dist/main.js +1 -1
- package/dist/route.d.ts +0 -11
- package/dist/types.d.ts +1 -3
- package/dist/utils.js +1 -1
- package/example/bun.lockb +0 -0
- package/example/index.html +13 -13
- package/example/main.ts +86 -75
- package/example/package-lock.json +177 -177
- package/example/package.json +21 -21
- package/example/route.ts +39 -39
- package/example/test.js +27 -27
- package/example/tester.js +88 -88
- package/example/tsconfig.json +16 -16
- package/index.d.ts +5 -5
- package/index.js +8 -9
- package/jsconfig.json +28 -28
- package/package.json +1 -1
- package/src/ctx.ts +251 -252
- package/src/handleRequest.ts +144 -144
- package/src/main.ts +361 -358
- package/src/trie.ts +148 -148
- package/src/types.ts +157 -157
- package/src/utils.ts +70 -70
- package/test/README.md +15 -15
- package/test/bun.lockb +0 -0
- package/test/index.ts +4 -4
- package/test/package.json +13 -13
- package/test/tsconfig.json +27 -27
- package/tsconfig.json +16 -16
- package/.prettierignore +0 -7
- package/.prettierrc +0 -7
- package/src/route.ts +0 -35
package/dist/ctx.d.ts
CHANGED
package/dist/ctx.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function
|
|
1
|
+
function x(F,L,G){let z=new Headers,M={},U=!1,I,Y=null,_,Z,$={};return{req:F,server:L,url:G,getUser(){return $},setUser(w){if(w)$=w},getIP(){return this.server.requestIP(this.req)},async getBody(){if(!Z)Z=await R(F);if(Z.error)return new Response(JSON.stringify({error:Z.error}),{status:400});return Z},setHeader(w,E){return z.set(w,E),this},set(w,E){if(typeof w!=="string")throw new Error("Key must be string type!");if(!E)throw new Error("value paramter is missing pls pass value after key");return M[w]=E,this},get(w){return w?M[w]:null},setAuth(w){return U=w,this},getAuth(){return U},text(w,E){return new Response(w,{status:E,headers:z})},json(w,E){return new Response(JSON.stringify(w),{status:E,headers:z})},file(w,E){return new Response(Bun.file(w),{status:E,headers:z})},redirect(w,E){return z.set("Location",w),new Response(null,{status:E??302,headers:z})},setCookie(w,E,J={}){let X=`${encodeURIComponent(w)}=${encodeURIComponent(E)}`;if(J.maxAge)X+=`; Max-Age=${J.maxAge}`;if(J.expires)X+=`; Expires=${J.expires.toUTCString()}`;if(J.path)X+=`; Path=${J.path}`;if(J.domain)X+=`; Domain=${J.domain}`;if(J.secure)X+="; Secure";if(J.httpOnly)X+="; HttpOnly";if(J.sameSite)X+=`; SameSite=${J.sameSite}`;return z?.append("Set-Cookie",X),this},getParams(w){if(!_&&F?.routePattern)_=O(F?.routePattern,G?.pathname);return w?_[w]||{}:_},getQuery(w){try{if(!I)I=Object.fromEntries(G.searchParams);return w?I[w]||{}:I}catch(E){return{}}},getCookie(w){if(!Y){let E=F.headers.get("cookie");if(E)Y=K(E);else return null}if(!Y)return null;if(w)return Y[w]??null;else return Y}}}function K(F){let L={},G=F?.split(";");for(let z=0;z<G?.length;z++){let[M,...U]=G[z].trim().split("="),I=U?.join("=").trim();if(M)L[M.trim()]=decodeURIComponent(I)}return L}function O(F,L){let G={},z=F.split("/"),[M]=L.split("?"),U=M.split("/");if(z.length!==U.length)return null;for(let I=0;I<z.length;I++)if(z[I].startsWith(":"))G[z[I].slice(1)]=U[I];return G}async function R(F){let L=F.headers.get("Content-Type");if(!L)return{};try{if(L.startsWith("application/json"))return await F.json();if(L.startsWith("application/x-www-form-urlencoded")){let G=await F.text();return Object.fromEntries(new URLSearchParams(G))}if(L.startsWith("multipart/form-data")){let G=await F.formData(),z={};for(let[M,U]of G.entries())z[M]=U;return z}return{error:"Unknown request body type"}}catch(G){return{error:"Invalid request body format"}}}export{x as default};
|
package/dist/handleRequest.d.ts
CHANGED
package/dist/handleRequest.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function D(I,
|
|
1
|
+
function D(I,E,G){let z=new Headers,J={},L=!1,Z,U=null,X,_,K={};return{req:I,server:E,url:G,getUser(){return K},setUser(F){if(F)K=F},getIP(){return this.server.requestIP(this.req)},async getBody(){if(!_)_=await j(I);if(_.error)return new Response(JSON.stringify({error:_.error}),{status:400});return _},setHeader(F,Y){return z.set(F,Y),this},set(F,Y){if(typeof F!=="string")throw new Error("Key must be string type!");if(!Y)throw new Error("value paramter is missing pls pass value after key");return J[F]=Y,this},get(F){return F?J[F]:null},setAuth(F){return L=F,this},getAuth(){return L},text(F,Y){return new Response(F,{status:Y,headers:z})},json(F,Y){return new Response(JSON.stringify(F),{status:Y,headers:z})},file(F,Y){return new Response(Bun.file(F),{status:Y,headers:z})},redirect(F,Y){return z.set("Location",F),new Response(null,{status:Y??302,headers:z})},setCookie(F,Y,$={}){let W=`${encodeURIComponent(F)}=${encodeURIComponent(Y)}`;if($.maxAge)W+=`; Max-Age=${$.maxAge}`;if($.expires)W+=`; Expires=${$.expires.toUTCString()}`;if($.path)W+=`; Path=${$.path}`;if($.domain)W+=`; Domain=${$.domain}`;if($.secure)W+="; Secure";if($.httpOnly)W+="; HttpOnly";if($.sameSite)W+=`; SameSite=${$.sameSite}`;return z?.append("Set-Cookie",W),this},getParams(F){if(!X&&I?.routePattern)X=T(I?.routePattern,G?.pathname);return F?X[F]||{}:X},getQuery(F){try{if(!Z)Z=Object.fromEntries(G.searchParams);return F?Z[F]||{}:Z}catch(Y){return{}}},getCookie(F){if(!U){let Y=I.headers.get("cookie");if(Y)U=A(Y);else return null}if(!U)return null;if(F)return U[F]??null;else return U}}}function A(I){let E={},G=I?.split(";");for(let z=0;z<G?.length;z++){let[J,...L]=G[z].trim().split("="),Z=L?.join("=").trim();if(J)E[J.trim()]=decodeURIComponent(Z)}return E}function T(I,E){let G={},z=I.split("/"),[J]=E.split("?"),L=J.split("/");if(z.length!==L.length)return null;for(let Z=0;Z<z.length;Z++)if(z[Z].startsWith(":"))G[z[Z].slice(1)]=L[Z];return G}async function j(I){let E=I.headers.get("Content-Type");if(!E)return{};try{if(E.startsWith("application/json"))return await I.json();if(E.startsWith("application/x-www-form-urlencoded")){let G=await I.text();return Object.fromEntries(new URLSearchParams(G))}if(E.startsWith("multipart/form-data")){let G=await I.formData(),z={};for(let[J,L]of G.entries())z[J]=L;return z}return{error:"Unknown request body type"}}catch(G){return{error:"Invalid request body format"}}}async function M(I,E,G,z){let J=z.trie.search(G.pathname,I.method);if(J?.isDynamic)I.routePattern=J.path;let L=D(I,E,G);if(z.corsConfig){let U=Q(I,L,z.corsConfig);if(U)return U}if(z.hasOnReqHook&&z.hooks.onRequest)z.hooks.onRequest(I,G,E);if(z.hasFilterEnabled){let U=I.routePattern??G.pathname;if(!z.filters.has(U))if(z.filterFunction)try{let X=await z.filterFunction(L,E);if(X)return X}catch(X){return console.error("Error in filterFunction:",X),L.json({message:"Internal Server Error",error:X.message},500)}else return L.json({message:"Authentication required"},400)}if(z.hasMiddleware){let U=z.globalMiddlewares;for(let _=0;_<U.length;_++){let K=await U[_](L,E);if(K)return K}let X=z.middlewares.get(G.pathname)||[];for(let _=0;_<X.length;_++){let K=await X[_](L,E);if(K)return K}}if(!J||J.method!==I.method){let U=J?"Method not allowed":`Route not found for ${G.pathname}`,X=J?405:404;return new Response(JSON.stringify({message:U}),{status:X})}if(z.hasPreHandlerHook&&z.hooks.preHandler){let U=await z.hooks.preHandler(L);if(U)return U}let Z=await J.handler(L);if(z.hasPostHandlerHook&&z.hooks.postHandler)await z.hooks.postHandler(L);if(z.hasOnSendHook&&z.hooks.onSend){let U=await z.hooks.onSend(L,Z);if(U)return U}return Z??L.json({message:"No response from this handler"},204)}function Q(I,E,G={}){let z=I.headers.get("origin")??"*",J=G?.origin,L=G?.allowedHeaders??["Content-Type","Authorization"],Z=G?.methods??["GET","POST","PUT","DELETE","OPTIONS"],U=G?.credentials??!1,X=G?.exposedHeaders??[];if(E.setHeader("Access-Control-Allow-Methods",Z),E.setHeader("Access-Control-Allow-Headers",L),E.setHeader("Access-Control-Allow-Credentials",U),X.length)E.setHeader("Access-Control-Expose-Headers",X);if(J==="*")E.setHeader("Access-Control-Allow-Origin","*");else if(Array.isArray(J))if(z&&J.includes(z))E.setHeader("Access-Control-Allow-Origin",z);else if(J.includes("*"))E.setHeader("Access-Control-Allow-Origin","*");else return E.json({message:"CORS not allowed"},403);else if(typeof J==="string")if(z===J)E.setHeader("Access-Control-Allow-Origin",z);else return E.json({message:"CORS not allowed"},403);else return E.json({message:"CORS not allowed"},403);if(E.setHeader("Access-Control-Allow-Origin",z),I.method==="OPTIONS")return E.setHeader("Access-Control-Max-Age","86400"),E.text("",204);return null}export{M as default};
|
package/dist/main.d.ts
CHANGED
package/dist/main.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class V{children;isEndOfWord;handler;isDynamic;pattern;path;method;subMiddlewares;constructor(){this.children={},this.isEndOfWord=!1,this.handler=[],this.isDynamic=!1,this.pattern="",this.path="",this.method=[],this.subMiddlewares=new Map}}class Q{root;constructor(){this.root=new V}insert(F,z){let G=this.root,J=F.split("/").filter(Boolean);if(F==="/"){G.isEndOfWord=!0,G.handler=[z.handler],G.path=F,G.method=[z.method];return}for(let L=0;L<J.length;L++){let U=J[L],Z=!1,X=U;if(U.startsWith(":"))Z=!0,X=":";if(!G.children[X])G.children[X]=new V;if(G=G.children[X],G.isDynamic=Z,G.pattern=U,L===J.length-1)G.handler=[z.handler],G.method=[z.method],G.isEndOfWord=!0,G.path=F}}search(F,z){let G=this.root,J=F.split("/").filter(Boolean);for(let U of J){let Z=U;if(!G.children[Z])if(G.children[":"])G=G.children[":"];else return null;else G=G.children[Z]}let L=G.method.indexOf(z);if(G.isEndOfWord&&L!==-1)return{path:G.path,handler:G.handler[L],isDynamic:G.isDynamic,pattern:G.pattern,method:G.method[L]};return null}}function B(F,z,G){let J=new Headers,L={},U=!1,Z,X=null,_,W,K=200,D={};return{req:F,server:z,url:G,getUser(){return D},setUser(Y){if(Y)D=Y},status(Y){return K=Y,this},getIP(){return this.server.requestIP(this.req)},async getBody(){if(!W)W=await N(F);if(W.error)return new Response(JSON.stringify({error:W.error}),{status:400});return W},setHeader(Y,$){return J.set(Y,$),this},set(Y,$){return L[Y]=$,this},get(Y){return L[Y]||null},setAuth(Y){return U=Y,this},getAuth(){return U},text(Y,$){return new Response(Y,{status:$??K,headers:J})},send(Y,$){return new Response(Y,{status:$??K,headers:J})},json(Y,$){return new Response(JSON.stringify(Y),{status:$??K,headers:J})},html(Y,$){return new Response(Bun.file(Y),{status:$??K,headers:J})},file(Y,$){return new Response(Bun.file(Y),{status:$??K,headers:J})},redirect(Y,$){return J.set("Location",Y),new Response(null,{status:$??302,headers:J})},setCookie(Y,$,E={}){let A=`${encodeURIComponent(Y)}=${encodeURIComponent($)}`;if(E.maxAge)A+=`; Max-Age=${E.maxAge}`;if(E.expires)A+=`; Expires=${E.expires.toUTCString()}`;if(E.path)A+=`; Path=${E.path}`;if(E.domain)A+=`; Domain=${E.domain}`;if(E.secure)A+="; Secure";if(E.httpOnly)A+="; HttpOnly";if(E.sameSite)A+=`; SameSite=${E.sameSite}`;return J?.append("Set-Cookie",A),this},getParams(Y){if(!_&&F?.routePattern)_=T(F?.routePattern,G?.pathname);return Y?_[Y]||{}:_},getQuery(Y){try{if(!Z)Z=Object.fromEntries(G.searchParams);return Y?Z[Y]||{}:Z}catch($){return{}}},getCookie(Y){if(!X){let $=F.headers.get("cookie");if($)X=I($);else return null}if(!X)return null;if(Y)return X[Y]??null;else return X}}}function I(F){let z={},G=F?.split(";");for(let J=0;J<G?.length;J++){let[L,...U]=G[J].trim().split("="),Z=U?.join("=").trim();if(L)z[L.trim()]=decodeURIComponent(Z)}return z}function T(F,z){let G={},J=F.split("/"),[L]=z.split("?"),U=L.split("/");if(J.length!==U.length)return null;for(let Z=0;Z<J.length;Z++)if(J[Z].startsWith(":"))G[J[Z].slice(1)]=U[Z];return G}async function N(F){let z=F.headers.get("Content-Type");if(!z)return{};try{if(z.startsWith("application/json"))return await F.json();if(z.startsWith("application/x-www-form-urlencoded")){let G=await F.text();return Object.fromEntries(new URLSearchParams(G))}if(z.startsWith("multipart/form-data")){let G=await F.formData(),J={};for(let[L,U]of G.entries())J[L]=U;return J}return{error:"Unknown request body type"}}catch(G){return{error:"Invalid request body format"}}}async function j(F,z,G,J){let L=J.trie.search(G.pathname,F.method);if(L?.isDynamic)F.routePattern=L.path;let U=B(F,z,G);if(J.corsConfig){let X=C(F,U,J.corsConfig);if(X)return X}if(J.hasOnReqHook&&J.hooks.onRequest)J.hooks.onRequest(F,G,z);if(J.hasFilterEnabled){let X=F.routePattern??G.pathname;if(!J.filters.has(X))if(J.filterFunction)try{let _=await J.filterFunction(U,z);if(_)return _}catch(_){return console.error("Error in filterFunction:",_),U.status(500).json({message:"Internal Server Error",error:_.message})}else return U.status(400).json({message:"Authentication required"})}if(J.hasMiddleware){let X=J.globalMiddlewares;for(let W=0;W<X.length;W++){let K=await X[W](U,z);if(K)return K}let _=J.middlewares.get(G.pathname)||[];for(let W=0;W<_.length;W++){let K=await _[W](U,z);if(K)return K}}if(!L||L.method!==F.method){let X=L?"Method not allowed":`Route not found for ${G.pathname}`,_=L?405:404;return new Response(JSON.stringify({message:X}),{status:_})}if(J.hasPreHandlerHook&&J.hooks.preHandler){let X=await J.hooks.preHandler(U);if(X)return X}let Z=await L.handler(U);if(J.hasPostHandlerHook&&J.hooks.postHandler)await J.hooks.postHandler(U);if(J.hasOnSendHook&&J.hooks.onSend){let X=await J.hooks.onSend(U,Z);if(X)return X}return Z??U.status(204).json({message:"No response from this handler"})}function C(F,z,G={}){let J=F.headers.get("origin")??"*",L=G?.origin,U=G?.allowedHeaders??["Content-Type","Authorization"],Z=G?.methods??["GET","POST","PUT","DELETE","OPTIONS"],X=G?.credentials??!1,_=G?.exposedHeaders??[];if(z.setHeader("Access-Control-Allow-Methods",Z),z.setHeader("Access-Control-Allow-Headers",U),z.setHeader("Access-Control-Allow-Credentials",X),_.length)z.setHeader("Access-Control-Expose-Headers",_);if(L==="*")z.setHeader("Access-Control-Allow-Origin","*");else if(Array.isArray(L))if(J&&L.includes(J))z.setHeader("Access-Control-Allow-Origin",J);else if(L.includes("*"))z.setHeader("Access-Control-Allow-Origin","*");else return z.status(403).json({message:"CORS not allowed"});else if(typeof L==="string")if(J===L)z.setHeader("Access-Control-Allow-Origin",J);else return z.status(403).json({message:"CORS not allowed"});else return z.status(403).json({message:"CORS not allowed"});if(z.setHeader("Access-Control-Allow-Origin",J),F.method==="OPTIONS")return z.setHeader("Access-Control-Max-Age","86400"),z.status(204).text("");return null}class M{tempRoutes;globalMiddlewares;middlewares;trie;hasOnReqHook;hasMiddleware;hasPreHandlerHook;hasPostHandlerHook;hasOnSendHook;hasOnError;hooks;corsConfig;FilterRoutes;filters;filterFunction;hasFilterEnabled;constructor(){this.tempRoutes=new Map,this.globalMiddlewares=[],this.middlewares=new Map,this.trie=new Q,this.corsConfig=null,this.hasMiddleware=!1,this.hasOnReqHook=!1,this.hasPreHandlerHook=!1,this.hasPostHandlerHook=!1,this.hasOnSendHook=!1,this.hasOnError=!1,this.hooks={onRequest:null,preHandler:null,postHandler:null,onSend:null,onError:null,onClose:null},this.FilterRoutes=[],this.filters=new Set,this.filterFunction=null,this.hasFilterEnabled=!1}filter(){return this.hasFilterEnabled=!0,{routeMatcher:(...F)=>{return this.FilterRoutes=F,this.filter()},permitAll:()=>{for(let F of this?.FilterRoutes)this.filters.add(F);return this.FilterRoutes=null,this.filter()},require:(F)=>{if(F)this.filterFunction=F}}}cors(F){return this.corsConfig=F,this}addHooks(F,z){if(typeof F!=="string")throw new Error("hookName must be a string");if(typeof z!=="function")throw new Error("callback must be a instance of function");switch(F){case"onRequest":this.hooks.onRequest=z,this.hasOnReqHook=!0;break;case"preHandler":this.hooks.preHandler=z,this.hasPreHandlerHook=!0;break;case"postHandler":this.hooks.postHandler=z,this.hasPostHandlerHook=!0;break;case"onSend":this.hooks.onSend=z,this.hasOnSendHook=!0;break;case"onError":this.hooks.onError=z,this.hasOnError=!0;break;case"onClose":this.hooks.onClose=z;break;default:throw new Error(`Unknown hook type: ${F}`)}return this}compile(){if(this.globalMiddlewares.length>0)this.hasMiddleware=!0;for(let[F,z]of this.middlewares.entries())if(z.length>0){this.hasMiddleware=!0;break}if(this.hooks.onRequest)this.hasOnReqHook=!0;if(this.hooks.preHandler)this.hasPreHandlerHook=!0;if(this.hooks.postHandler)this.hasPostHandlerHook=!0;if(this.hooks.onSend)this.hasOnSendHook=!0;if(this.hooks.onError)this.hasOnError=!0;this.tempRoutes=new Map}listen(...F){if(typeof Bun==="undefined")throw new Error(".listen() is designed to run on Bun only...");let z=3000,G="0.0.0.0",J=void 0,L={};for(let X of F)if(typeof X==="number")z=X;else if(typeof X==="string")G=X;else if(typeof X==="function")J=X;else if(typeof X==="object"&&X!==null)L=X;this.compile();let U={port:z,hostname:G,fetch:async(X,_)=>{let W=new URL(X.url);try{return await j(X,_,W,this)}catch(K){if(this.hasOnError&&this.hooks.onError){let D=await this.hooks.onError(K,X,W,_);if(D)return D}return new Response(JSON.stringify({message:"Internal Server Error",error:K.message}),{status:500})}}};if(L.sslCert&&L.sslKey)U.certFile=L.sslCert,U.keyFile=L.sslKey;let Z=Bun?.serve(U);if(J)return J();if(L.sslCert&&L.sslKey)console.log(`HTTPS server is running on https://localhost:${z}`);else console.log(`HTTP server is running on http://localhost:${z}`);return Z}route(F,z){if(!F||typeof F!=="string")throw new Error("Path must be a string");let G=Object.fromEntries(z.tempRoutes);return Object.entries(G).forEach(([L,U])=>{let Z=`${F}${L}`;if(!this.middlewares.has(Z))this.middlewares.set(Z,[]);U.handlers.slice(0,-1).forEach((K)=>{if(!this.middlewares.get(Z)?.includes(K))this.middlewares.get(Z)?.push(K)});let _=U.handlers[U.handlers.length-1],W=U.method;try{this.trie.insert(Z,{handler:_,method:W})}catch(K){console.error(`Error inserting ${Z}:`,K)}}),z=null,this}register(F,z){return this.route(F,z),this}addRoute(F,z,G){if(typeof z!=="string")throw new Error("Path must be a string");if(typeof F!=="string")throw new Error("Method must be a string");this.tempRoutes.set(z,{method:F,handlers:G});let J=G.slice(0,-1),L=G[G.length-1];if(!this.middlewares.has(z))this.middlewares.set(z,[]);J.forEach((U)=>{if(z==="/")this.globalMiddlewares=[...new Set([...this.globalMiddlewares,...J])];else if(!this.middlewares.get(z)?.includes(U))this.middlewares.get(z)?.push(U)});try{this.trie.insert(z,{handler:L,method:F})}catch(U){console.error(`Error inserting ${z}:`,U)}}use(F,...z){if(typeof F==="function"){if(!this.globalMiddlewares.includes(F))this.globalMiddlewares.push(F);z.forEach((J)=>{if(!this.globalMiddlewares.includes(J))this.globalMiddlewares.push(J)});return}let G=F;if(!this.middlewares.has(G))this.middlewares.set(G,[]);if(z)z.forEach((J)=>{if(!this.middlewares.get(G)?.includes(J))this.middlewares.get(G)?.push(J)});return this}get(F,...z){return this.addRoute("GET",F,z),this}post(F,...z){return this.addRoute("POST",F,z),this}put(F,...z){return this.addRoute("PUT",F,z),this}patch(F,...z){return this.addRoute("PATCH",F,z),this}delete(F,...z){return this.addRoute("DELETE",F,z),this}}export{M as default};
|
|
1
|
+
class Q{children;isEndOfWord;handler;isDynamic;pattern;path;method;subMiddlewares;constructor(){this.children={},this.isEndOfWord=!1,this.handler=[],this.isDynamic=!1,this.pattern="",this.path="",this.method=[],this.subMiddlewares=new Map}}class D{root;constructor(){this.root=new Q}insert(F,z){let G=this.root,J=F.split("/").filter(Boolean);if(F==="/"){G.isEndOfWord=!0,G.handler=[z.handler],G.path=F,G.method=[z.method];return}for(let L=0;L<J.length;L++){let U=J[L],Z=!1,X=U;if(U.startsWith(":"))Z=!0,X=":";if(!G.children[X])G.children[X]=new Q;if(G=G.children[X],G.isDynamic=Z,G.pattern=U,L===J.length-1)G.handler=[z.handler],G.method=[z.method],G.isEndOfWord=!0,G.path=F}}search(F,z){let G=this.root,J=F.split("/").filter(Boolean);for(let U of J){let Z=U;if(!G.children[Z])if(G.children[":"])G=G.children[":"];else return null;else G=G.children[Z]}let L=G.method.indexOf(z);if(G.isEndOfWord&&L!==-1)return{path:G.path,handler:G.handler[L],isDynamic:G.isDynamic,pattern:G.pattern,method:G.method[L]};return null}}function V(F,z,G){let J=new Headers,L={},U=!1,Z,X=null,_,$,W={};return{req:F,server:z,url:G,getUser(){return W},setUser(Y){if(Y)W=Y},getIP(){return this.server.requestIP(this.req)},async getBody(){if(!$)$=await T(F);if($.error)return new Response(JSON.stringify({error:$.error}),{status:400});return $},setHeader(Y,K){return J.set(Y,K),this},set(Y,K){if(typeof Y!=="string")throw new Error("Key must be string type!");if(!K)throw new Error("value paramter is missing pls pass value after key");return L[Y]=K,this},get(Y){return Y?L[Y]:null},setAuth(Y){return U=Y,this},getAuth(){return U},text(Y,K){return new Response(Y,{status:K,headers:J})},json(Y,K){return new Response(JSON.stringify(Y),{status:K,headers:J})},file(Y,K){return new Response(Bun.file(Y),{status:K,headers:J})},redirect(Y,K){return J.set("Location",Y),new Response(null,{status:K??302,headers:J})},setCookie(Y,K,E={}){let A=`${encodeURIComponent(Y)}=${encodeURIComponent(K)}`;if(E.maxAge)A+=`; Max-Age=${E.maxAge}`;if(E.expires)A+=`; Expires=${E.expires.toUTCString()}`;if(E.path)A+=`; Path=${E.path}`;if(E.domain)A+=`; Domain=${E.domain}`;if(E.secure)A+="; Secure";if(E.httpOnly)A+="; HttpOnly";if(E.sameSite)A+=`; SameSite=${E.sameSite}`;return J?.append("Set-Cookie",A),this},getParams(Y){if(!_&&F?.routePattern)_=I(F?.routePattern,G?.pathname);return Y?_[Y]||{}:_},getQuery(Y){try{if(!Z)Z=Object.fromEntries(G.searchParams);return Y?Z[Y]||{}:Z}catch(K){return{}}},getCookie(Y){if(!X){let K=F.headers.get("cookie");if(K)X=M(K);else return null}if(!X)return null;if(Y)return X[Y]??null;else return X}}}function M(F){let z={},G=F?.split(";");for(let J=0;J<G?.length;J++){let[L,...U]=G[J].trim().split("="),Z=U?.join("=").trim();if(L)z[L.trim()]=decodeURIComponent(Z)}return z}function I(F,z){let G={},J=F.split("/"),[L]=z.split("?"),U=L.split("/");if(J.length!==U.length)return null;for(let Z=0;Z<J.length;Z++)if(J[Z].startsWith(":"))G[J[Z].slice(1)]=U[Z];return G}async function T(F){let z=F.headers.get("Content-Type");if(!z)return{};try{if(z.startsWith("application/json"))return await F.json();if(z.startsWith("application/x-www-form-urlencoded")){let G=await F.text();return Object.fromEntries(new URLSearchParams(G))}if(z.startsWith("multipart/form-data")){let G=await F.formData(),J={};for(let[L,U]of G.entries())J[L]=U;return J}return{error:"Unknown request body type"}}catch(G){return{error:"Invalid request body format"}}}async function B(F,z,G,J){let L=J.trie.search(G.pathname,F.method);if(L?.isDynamic)F.routePattern=L.path;let U=V(F,z,G);if(J.corsConfig){let X=N(F,U,J.corsConfig);if(X)return X}if(J.hasOnReqHook&&J.hooks.onRequest)J.hooks.onRequest(F,G,z);if(J.hasFilterEnabled){let X=F.routePattern??G.pathname;if(!J.filters.has(X))if(J.filterFunction)try{let _=await J.filterFunction(U,z);if(_)return _}catch(_){return console.error("Error in filterFunction:",_),U.json({message:"Internal Server Error",error:_.message},500)}else return U.json({message:"Authentication required"},400)}if(J.hasMiddleware){let X=J.globalMiddlewares;for(let $=0;$<X.length;$++){let W=await X[$](U,z);if(W)return W}let _=J.middlewares.get(G.pathname)||[];for(let $=0;$<_.length;$++){let W=await _[$](U,z);if(W)return W}}if(!L||L.method!==F.method){let X=L?"Method not allowed":`Route not found for ${G.pathname}`,_=L?405:404;return new Response(JSON.stringify({message:X}),{status:_})}if(J.hasPreHandlerHook&&J.hooks.preHandler){let X=await J.hooks.preHandler(U);if(X)return X}let Z=await L.handler(U);if(J.hasPostHandlerHook&&J.hooks.postHandler)await J.hooks.postHandler(U);if(J.hasOnSendHook&&J.hooks.onSend){let X=await J.hooks.onSend(U,Z);if(X)return X}return Z??U.json({message:"No response from this handler"},204)}function N(F,z,G={}){let J=F.headers.get("origin")??"*",L=G?.origin,U=G?.allowedHeaders??["Content-Type","Authorization"],Z=G?.methods??["GET","POST","PUT","DELETE","OPTIONS"],X=G?.credentials??!1,_=G?.exposedHeaders??[];if(z.setHeader("Access-Control-Allow-Methods",Z),z.setHeader("Access-Control-Allow-Headers",U),z.setHeader("Access-Control-Allow-Credentials",X),_.length)z.setHeader("Access-Control-Expose-Headers",_);if(L==="*")z.setHeader("Access-Control-Allow-Origin","*");else if(Array.isArray(L))if(J&&L.includes(J))z.setHeader("Access-Control-Allow-Origin",J);else if(L.includes("*"))z.setHeader("Access-Control-Allow-Origin","*");else return z.json({message:"CORS not allowed"},403);else if(typeof L==="string")if(J===L)z.setHeader("Access-Control-Allow-Origin",J);else return z.json({message:"CORS not allowed"},403);else return z.json({message:"CORS not allowed"},403);if(z.setHeader("Access-Control-Allow-Origin",J),F.method==="OPTIONS")return z.setHeader("Access-Control-Max-Age","86400"),z.text("",204);return null}class j{tempRoutes;globalMiddlewares;middlewares;trie;hasOnReqHook;hasMiddleware;hasPreHandlerHook;hasPostHandlerHook;hasOnSendHook;hasOnError;hooks;corsConfig;FilterRoutes;filters;filterFunction;hasFilterEnabled;constructor(){this.tempRoutes=new Map,this.globalMiddlewares=[],this.middlewares=new Map,this.trie=new D,this.corsConfig=null,this.hasMiddleware=!1,this.hasOnReqHook=!1,this.hasPreHandlerHook=!1,this.hasPostHandlerHook=!1,this.hasOnSendHook=!1,this.hasOnError=!1,this.hooks={onRequest:null,preHandler:null,postHandler:null,onSend:null,onError:null,onClose:null},this.FilterRoutes=[],this.filters=new Set,this.filterFunction=null,this.hasFilterEnabled=!1}filter(){return this.hasFilterEnabled=!0,{routeMatcher:(...F)=>{return this.FilterRoutes=F,this.filter()},permitAll:()=>{for(let F of this?.FilterRoutes)this.filters.add(F);return this.FilterRoutes=null,this.filter()},require:(F)=>{if(F)this.filterFunction=F}}}cors(F){return this.corsConfig=F,this}addHooks(F,z){if(typeof F!=="string")throw new Error("hookName must be a string");if(typeof z!=="function")throw new Error("callback must be a instance of function");switch(F){case"onRequest":this.hooks.onRequest=z,this.hasOnReqHook=!0;break;case"preHandler":this.hooks.preHandler=z,this.hasPreHandlerHook=!0;break;case"postHandler":this.hooks.postHandler=z,this.hasPostHandlerHook=!0;break;case"onSend":this.hooks.onSend=z,this.hasOnSendHook=!0;break;case"onError":this.hooks.onError=z,this.hasOnError=!0;break;case"onClose":this.hooks.onClose=z;break;default:throw new Error(`Unknown hook type: ${F}`)}return this}compile(){if(this.globalMiddlewares.length>0)this.hasMiddleware=!0;for(let[F,z]of this.middlewares.entries())if(z.length>0){this.hasMiddleware=!0;break}if(this.hooks.onRequest)this.hasOnReqHook=!0;if(this.hooks.preHandler)this.hasPreHandlerHook=!0;if(this.hooks.postHandler)this.hasPostHandlerHook=!0;if(this.hooks.onSend)this.hasOnSendHook=!0;if(this.hooks.onError)this.hasOnError=!0;this.tempRoutes=new Map}listen(...F){if(typeof Bun==="undefined")throw new Error(".listen() is designed to run on Bun only...");let z=3000,G="0.0.0.0",J=void 0,L={};for(let X of F)if(typeof X==="number")z=X;else if(typeof X==="string")G=X;else if(typeof X==="function")J=X;else if(typeof X==="object"&&X!==null)L=X;this.compile();let U={port:z,hostname:G,fetch:async(X,_)=>{let $=new URL(X.url);try{return await B(X,_,$,this)}catch(W){if(this.hasOnError&&this.hooks.onError){let Y=await this.hooks.onError(W,X,$,_);if(Y)return Y}return new Response(JSON.stringify({message:"Internal Server Error",error:W.message}),{status:500})}}};if(L.sslCert&&L.sslKey)U.certFile=L.sslCert,U.keyFile=L.sslKey;let Z=Bun?.serve(U);if(J)return J();if(L.sslCert&&L.sslKey)console.log(`HTTPS server is running on https://localhost:${z}`);else console.log(`HTTP server is running on http://localhost:${z}`);return Z}route(F,z){if(!F||typeof F!=="string")throw new Error("Path must be a string");let G=Object.fromEntries(z.tempRoutes);return Object.entries(G).forEach(([L,U])=>{let Z=`${F}${L}`;if(!this.middlewares.has(Z))this.middlewares.set(Z,[]);U.handlers.slice(0,-1).forEach((W)=>{if(!this.middlewares.get(Z)?.includes(W))this.middlewares.get(Z)?.push(W)});let _=U.handlers[U.handlers.length-1],$=U.method;try{this.trie.insert(Z,{handler:_,method:$})}catch(W){console.error(`Error inserting ${Z}:`,W)}}),z=null,this}register(F,z){return this.route(F,z),this}addRoute(F,z,G){if(typeof z!=="string")throw new Error(`Error in ${G[G.length-1]}: Path must be a string. Received: ${typeof z}`);if(typeof F!=="string")throw new Error(`Error in addRoute: Method must be a string. Received: ${typeof F}`);this.tempRoutes.set(z,{method:F,handlers:G});let J=G.slice(0,-1),L=G[G.length-1];if(!this.middlewares.has(z))this.middlewares.set(z,[]);J.forEach((U)=>{if(z==="/")this.globalMiddlewares=[...new Set([...this.globalMiddlewares,...J])];else if(!this.middlewares.get(z)?.includes(U))this.middlewares.get(z)?.push(U)});try{this.trie.insert(z,{handler:L,method:F})}catch(U){console.error(`Error inserting ${z}:`,U)}}use(F,...z){if(typeof F==="function"){if(!this.globalMiddlewares.includes(F))this.globalMiddlewares.push(F);z.forEach((J)=>{if(!this.globalMiddlewares.includes(J))this.globalMiddlewares.push(J)});return}let G=F;if(!this.middlewares.has(G))this.middlewares.set(G,[]);if(z)z.forEach((J)=>{if(!this.middlewares.get(G)?.includes(J))this.middlewares.get(G)?.push(J)});return this}get(F,...z){return this.addRoute("GET",F,z),this}post(F,...z){return this.addRoute("POST",F,z),this}put(F,...z){return this.addRoute("PUT",F,z),this}patch(F,...z){return this.addRoute("PATCH",F,z),this}delete(F,...z){return this.addRoute("DELETE",F,z),this}}export{j as default};
|
package/dist/route.d.ts
CHANGED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import Diesel from "./main";
|
|
2
|
-
import type { handlerFunction } from "./types";
|
|
3
|
-
declare class Router extends Diesel {
|
|
4
|
-
constructor();
|
|
5
|
-
get(path: string, ...handlers: handlerFunction[]): this;
|
|
6
|
-
post(path: string, ...handlers: handlerFunction[]): this;
|
|
7
|
-
put(path: string, ...handlers: handlerFunction[]): this;
|
|
8
|
-
patch(path: string, ...handlers: handlerFunction[]): this;
|
|
9
|
-
delete(path: string, ...handlers: handlerFunction[]): this;
|
|
10
|
-
}
|
|
11
|
-
export default Router;
|
package/dist/types.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/// <reference types="bun-types" />
|
|
1
2
|
import { Server } from "bun";
|
|
2
3
|
export type listenCalllBackType = () => void;
|
|
3
4
|
export type handlerFunction = (ctx: ContextType, server?: Server) => Response | Promise<Response | null | void>;
|
|
@@ -25,7 +26,6 @@ export interface ContextType {
|
|
|
25
26
|
url: URL;
|
|
26
27
|
setUser: (data?: any) => void;
|
|
27
28
|
getUser: () => any;
|
|
28
|
-
status: (status: number) => this;
|
|
29
29
|
getIP: () => any;
|
|
30
30
|
getBody: () => Promise<any>;
|
|
31
31
|
setHeader: (key: string, value: any) => this;
|
|
@@ -35,8 +35,6 @@ export interface ContextType {
|
|
|
35
35
|
getAuth: () => boolean;
|
|
36
36
|
json: (data: Object, status?: number) => Response;
|
|
37
37
|
text: (data: string, status?: number) => Response;
|
|
38
|
-
send: (data: string, status?: number) => Response;
|
|
39
|
-
html: (filePath: string, status?: number) => Response;
|
|
40
38
|
file: (filePath: string, status?: number) => Response;
|
|
41
39
|
redirect: (path: string, status?: number) => Response;
|
|
42
40
|
getParams: (props?: any) => any;
|
package/dist/utils.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function H(v){let{time:z=60000,max:A=100,message:C="Rate limit exceeded. Please try again later."}=v,j=new Map;return(F)=>{let D=new Date,E=F.getIP().address;if(!j.has(E))j.set(E,{count:0,startTime:D});let l=j.get(E);if(l)if(D-l.startTime>z)l.count=1,l.startTime=D;else l.count++;if(l&&l.count>A)return F.
|
|
1
|
+
function H(v){let{time:z=60000,max:A=100,message:C="Rate limit exceeded. Please try again later."}=v,j=new Map;return(F)=>{let D=new Date,E=F.getIP().address;if(!j.has(E))j.set(E,{count:0,startTime:D});let l=j.get(E);if(l)if(D-l.startTime>z)l.count=1,l.startTime=D;else l.count++;if(l&&l.count>A)return F.json({error:C},429)}}var G=(v,z,A,C)=>{if(A>C)return!1;let j=A+(C-A)/2;if(v[j]==z)return!0;if(v[j]>z)return G(v,z,A,j-1);return G(v,z,j+1,C)};export{H as default,G as binaryS};
|
package/example/bun.lockb
CHANGED
|
Binary file
|
package/example/index.html
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
<!doctype html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
<head>
|
|
4
|
-
<meta charset="UTF-8" />
|
|
5
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
-
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
|
|
7
|
-
<title>HTML 5 Boilerplate</title>
|
|
8
|
-
<link rel="stylesheet" href="style.css" />
|
|
9
|
-
</head>
|
|
10
|
-
<body>
|
|
11
|
-
Hello world!
|
|
12
|
-
</body>
|
|
13
|
-
</html>
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
+
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
|
|
7
|
+
<title>HTML 5 Boilerplate</title>
|
|
8
|
+
<link rel="stylesheet" href="style.css" />
|
|
9
|
+
</head>
|
|
10
|
+
<body>
|
|
11
|
+
Hello world!
|
|
12
|
+
</body>
|
|
13
|
+
</html>
|
package/example/main.ts
CHANGED
|
@@ -1,75 +1,86 @@
|
|
|
1
|
-
import Diesel from "../
|
|
2
|
-
import jwt from "jsonwebtoken";
|
|
3
|
-
import { ContextType, CookieOptions, middlewareFunc } from "../src/types";
|
|
4
|
-
import { Server } from "bun";
|
|
5
|
-
import { userRoute } from "./route";
|
|
6
|
-
|
|
7
|
-
const app = new Diesel();
|
|
8
|
-
const SECRET_KEY = "linux";
|
|
9
|
-
|
|
10
|
-
// Authentication Middleware
|
|
11
|
-
export async function authJwt(ctx: ContextType): Promise<void | null | Response> {
|
|
12
|
-
const token = ctx.getCookie("accessToken");
|
|
13
|
-
if (!token) {
|
|
14
|
-
return ctx.
|
|
15
|
-
}
|
|
16
|
-
try {
|
|
17
|
-
const user = jwt.verify(token, SECRET_KEY);
|
|
18
|
-
ctx.setUser(user);
|
|
19
|
-
} catch (error) {
|
|
20
|
-
return ctx.
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
.
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
1
|
+
import {Diesel} from "../index.js";
|
|
2
|
+
import jwt from "jsonwebtoken";
|
|
3
|
+
import { ContextType, CookieOptions, middlewareFunc } from "../src/types";
|
|
4
|
+
import { Server } from "bun";
|
|
5
|
+
import { newRoute, userRoute } from "./route";
|
|
6
|
+
|
|
7
|
+
const app = new Diesel();
|
|
8
|
+
const SECRET_KEY = "linux";
|
|
9
|
+
|
|
10
|
+
// Authentication Middleware
|
|
11
|
+
export async function authJwt(ctx: ContextType): Promise<void | null | Response> {
|
|
12
|
+
const token = ctx.getCookie("accessToken");
|
|
13
|
+
if (!token) {
|
|
14
|
+
return ctx.json({ message: "Authentication token missing" },401);
|
|
15
|
+
}
|
|
16
|
+
try {
|
|
17
|
+
const user = jwt.verify(token, SECRET_KEY);
|
|
18
|
+
ctx.setUser(user);
|
|
19
|
+
} catch (error) {
|
|
20
|
+
return ctx.json({ message: "Invalid token" },403);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
app
|
|
25
|
+
.filter()
|
|
26
|
+
.routeMatcher("/cookie")
|
|
27
|
+
.permitAll()
|
|
28
|
+
.require(authJwt as middlewareFunc)
|
|
29
|
+
|
|
30
|
+
// Error Handling Hook
|
|
31
|
+
app.addHooks("onError", (error: any, req: Request, url: URL) => {
|
|
32
|
+
console.error(`Error occurred: ${error.message}`);
|
|
33
|
+
console.error(`Request Method: ${req.method}, Request URL: ${url}`);
|
|
34
|
+
return new Response("Internal Server Error", { status: 500 });
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Routes
|
|
38
|
+
app
|
|
39
|
+
.get("/", async (ctx) => {
|
|
40
|
+
const user = ctx.getUser();
|
|
41
|
+
return ctx.json({
|
|
42
|
+
msg: "Hello World",
|
|
43
|
+
user,
|
|
44
|
+
});
|
|
45
|
+
})
|
|
46
|
+
.get("/error", async () => {
|
|
47
|
+
throw new Error("This is a test error to demonstrate error handling");
|
|
48
|
+
})
|
|
49
|
+
.get("/redirect",async(ctx:ContextType) => {
|
|
50
|
+
return ctx.redirect("/")
|
|
51
|
+
})
|
|
52
|
+
.get("/test/:id/:name", async (ctx) => {
|
|
53
|
+
const query = ctx.getQuery();
|
|
54
|
+
const params = ctx.getParams();
|
|
55
|
+
return ctx.json({ msg: "Hello World", query, params });
|
|
56
|
+
})
|
|
57
|
+
.get("/ok", (ctx:ContextType) => {
|
|
58
|
+
return ctx.text("How are you?");
|
|
59
|
+
})
|
|
60
|
+
.get("/cookie", async (ctx) => {
|
|
61
|
+
const user = { name: "pk", age: 22 };
|
|
62
|
+
|
|
63
|
+
const accessToken = jwt.sign(user, SECRET_KEY, { expiresIn: "1d" });
|
|
64
|
+
const refreshToken = jwt.sign(user, SECRET_KEY, { expiresIn: "10d" });
|
|
65
|
+
|
|
66
|
+
const cookieOptions: CookieOptions = {
|
|
67
|
+
httpOnly: true,
|
|
68
|
+
secure: true,
|
|
69
|
+
maxAge: 24 * 60 * 60 * 1000,
|
|
70
|
+
sameSite: "Strict",
|
|
71
|
+
path: "/",
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
return ctx
|
|
75
|
+
.setCookie("accessToken", accessToken, cookieOptions)
|
|
76
|
+
.setCookie("refreshToken", refreshToken, cookieOptions)
|
|
77
|
+
.json({ msg: "Cookies set successfully" });
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// Register Additional Routes
|
|
81
|
+
app.register("/api/user", userRoute);
|
|
82
|
+
|
|
83
|
+
app.route('/api/route', newRoute)
|
|
84
|
+
|
|
85
|
+
// Start the Server
|
|
86
|
+
app.listen(3000)
|