diesel-core 0.0.18 → 0.0.20

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 CHANGED
@@ -1,3 +1,3 @@
1
1
  only i know folder structure and codebase , if u dont understand it dont worry , just ping me.
2
2
 
3
- if you changes code OR fixing bugs so after doing make sure to run tsc and bun build.js then manually delete the main folder
3
+ if you changes code OR fixing bugs so after doing make sure to run tsc and bun build.js
package/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Diesel Bunjs License
1
+ Diesel.js License
2
2
  Copyright (c) 2024 Pradeep Sahu
3
3
 
4
4
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
package/README.md CHANGED
@@ -141,6 +141,7 @@ DieselJS allows you to enhance your request handling by utilizing hooks at vario
141
141
  2. **preHandler**: Invoked just before the request handler executes.
142
142
  3. **postHandler**: Executed after the request handler completes but before sending the response.
143
143
  4. **onSend**: Called just before the response is sent to the client.
144
+ 5. **onError** : Executes if any error occurs
144
145
 
145
146
  ### How to Define Hooks
146
147
 
@@ -149,10 +150,19 @@ To define hooks in your DieselJS application, you can add them directly to your
149
150
  ### Example Usage
150
151
 
151
152
  ```javascript
153
+
154
+ // define and onError hook
155
+
156
+ app.addHooks("onError",(error,req,url,server) => {
157
+ console.log(`error occured ${error.message}`)
158
+ // retunr new Response(......)
159
+ })
160
+
152
161
  // Define an onRequest hook
153
- app.addHooks("onRequest",(xl) =>{
154
- console.log(`Request received: ${xl.req.method} ${xl.req.url}`);
162
+ app.addHooks("onRequest",(req,url,server) =>{
163
+ console.log(`Request received: ${req.method} ${url}`);
155
164
  })
165
+ // you get req,url & server instance in onReq
156
166
 
157
167
  // Define a preHandler hook
158
168
  app.addHooks("preHandler",(xl) =>{
@@ -226,9 +236,9 @@ app.get("/set-cookie", async(xl) => {
226
236
  path: "/",
227
237
  }
228
238
 
229
- await xl.cookie("accessToken", accessToken, options)
230
-
231
- await xl.cookie("refreshToken", refreshToken, options)
239
+ xl
240
+ .setCookie("accessToken", accessToken, options)
241
+ .setCookie("refreshToken", refreshToken, options)
232
242
 
233
243
  return xl.json({msg:"setting cookies"})
234
244
  })
package/build.js CHANGED
@@ -1,11 +1,11 @@
1
1
  import path from 'path';
2
2
  const entryPoints = [
3
- './main/main.js',
4
- './main/ctx.js',
5
- './main/handleRequest.js',
6
- './main/trie.js',
7
- './main/router.js',
8
- './main/utils.js'
3
+ './src/main.ts',
4
+ './src/ctx.ts',
5
+ './src/handleRequest.ts',
6
+ './src/trie.ts',
7
+ './src/router.ts',
8
+ './src/utils.ts'
9
9
  ];
10
10
 
11
11
  entryPoints.forEach(entry => {
package/dist/ctx.js CHANGED
@@ -1 +1 @@
1
- var w=function(I,J,G,F){function Y(R){return R instanceof G?R:new G(function(L){L(R)})}return new(G||(G=Promise))(function(R,L){function X(U){try{Z(F.next(U))}catch(W){L(W)}}function $(U){try{Z(F.throw(U))}catch(W){L(W)}}function Z(U){U.done?R(U.value):Y(U.value).then(X,$)}Z((F=F.apply(I,J||[])).next())})};function O(I,J,G){let F=new Headers,Y={},R=!1,L,X=null,$,Z,U=200,W={};return{req:I,server:J,url:G,getUser(){return W},setUser(z){if(z)W=z},status(z){return U=z,this},getIP(){return this.server.requestIP(this.req)},body(){return w(this,void 0,void 0,function*(){if(!Z)Z=yield V(I);if(Z.error)return new Response(JSON.stringify({error:Z.error}),{status:400});return Z})},setHeader(z,E){return F.set(z,E),this},set(z,E){return Y[z]=E,this},get(z){return Y[z]||null},setAuth(z){return R=z,this},getAuth(){return R},text(z,E){return new Response(z,{status:E!==null&&E!==void 0?E:U,headers:F})},send(z,E){return new Response(z,{status:E!==null&&E!==void 0?E:U,headers:F})},json(z,E){return new Response(JSON.stringify(z),{status:E!==null&&E!==void 0?E:U,headers:F})},html(z,E){return new Response(Bun.file(z),{status:E!==null&&E!==void 0?E:U,headers:F})},file(z,E){return new Response(Bun.file(z),{status:E!==null&&E!==void 0?E:U,headers:F})},redirect(z,E){return F.set("Location",z),new Response(null,{status:E!==null&&E!==void 0?E:302,headers:F})},getParams(z){if(!$)$=x(I===null||I===void 0?void 0:I.routePattern,G===null||G===void 0?void 0:G.pathname);return z?$[z]||{}:$},getQuery(z){try{if(!L)L=Object.fromEntries(G.searchParams);return z?L[z]||{}:L}catch(E){return{}}},cookie(z,E,M={}){let K=`${encodeURIComponent(z)}=${encodeURIComponent(E)}`;if(M.maxAge)K+=`; Max-Age=${M.maxAge}`;if(M.expires)K+=`; Expires=${M.expires.toUTCString()}`;if(M.path)K+=`; Path=${M.path}`;if(M.domain)K+=`; Domain=${M.domain}`;if(M.secure)K+="; Secure";if(M.httpOnly)K+="; HttpOnly";if(M.sameSite)K+=`; SameSite=${M.sameSite}`;return F===null||F===void 0||F.append("Set-Cookie",K),this},getCookie(z){var E;if(!X||Object.keys(X).length===0){let M=(E=I.headers)===null||E===void 0?void 0:E.get("cookie");if(M)X=H(M);else return null}if(!X)return null;return z?X[z]!==void 0?X[z]:null:X}}}function H(I){let J={};if(!I)return J;return I.split(";").forEach((F)=>{var Y;let[R,L]=(Y=F===null||F===void 0?void 0:F.trim())===null||Y===void 0?void 0:Y.split("=");if(R&&L)J[R.trim()]=L.split(" ")[0].trim()}),J}function x(I,J){let G={},F=I.split("/"),[Y]=J.split("?"),R=Y.split("/");if(F.length!==R.length)return null;return F.forEach((L,X)=>{if(L.startsWith(":")){let $=L.slice(1);G[$]=R[X]}}),G}function V(I){return w(this,void 0,void 0,function*(){let J=I.headers.get("Content-Type")||"";if(!J)return{};try{if(J.startsWith("application/json"))return yield I.json();if(J.startsWith("application/x-www-form-urlencoded")){let G=yield I.text();return Object.fromEntries(new URLSearchParams(G))}if(J.startsWith("multipart/form-data")){let G=yield I.formData();return A(G)}return{error:"Unknown request body type"}}catch(G){return{error:"Invalid request body format"}}})}function A(I){let J={};for(let[G,F]of I.entries())J[G]=F;return J}export{O as default};
1
+ function K(F,L,G){let z=new Headers,M={},U=!1,I,Z=null,$,_,Y=200,x={};return{req:F,server:L,url:G,getUser(){return x},setUser(w){if(w)x=w},status(w){return Y=w,this},getIP(){return this.server.requestIP(this.req)},async getBody(){if(!_)_=await W(F);if(_.error)return new Response(JSON.stringify({error:_.error}),{status:400});return _},setHeader(w,E){return z.set(w,E),this},set(w,E){return M[w]=E,this},get(w){return M[w]||null},setAuth(w){return U=w,this},getAuth(){return U},text(w,E){return new Response(w,{status:E??Y,headers:z})},send(w,E){return new Response(w,{status:E??Y,headers:z})},json(w,E){return new Response(JSON.stringify(w),{status:E??Y,headers:z})},html(w,E){return new Response(Bun.file(w),{status:E??Y,headers:z})},file(w,E){return new Response(Bun.file(w),{status:E??Y,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)$=R(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(!Z){let E=F.headers.get("cookie");if(E)Z=O(E);else return null}if(!Z)return null;if(w)return Z[w]??null;else return Z}}}function O(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 R(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 W(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{K as default};
@@ -1 +1 @@
1
- var Q=function(L,G,F,z){function K(Y){return Y instanceof F?Y:new F(function(U){U(Y)})}return new(F||(F=Promise))(function(Y,U){function Z(I){try{$(z.next(I))}catch(W){U(W)}}function X(I){try{$(z.throw(I))}catch(W){U(W)}}function $(I){I.done?Y(I.value):K(I.value).then(Z,X)}$((z=z.apply(L,G||[])).next())})};function D(L,G,F){let z=new Headers,K={},Y=!1,U,Z=null,X,$,I=200,W={};return{req:L,server:G,url:F,getUser(){return W},setUser(E){if(E)W=E},status(E){return I=E,this},getIP(){return this.server.requestIP(this.req)},body(){return Q(this,void 0,void 0,function*(){if(!$)$=yield A(L);if($.error)return new Response(JSON.stringify({error:$.error}),{status:400});return $})},setHeader(E,J){return z.set(E,J),this},set(E,J){return K[E]=J,this},get(E){return K[E]||null},setAuth(E){return Y=E,this},getAuth(){return Y},text(E,J){return new Response(E,{status:J!==null&&J!==void 0?J:I,headers:z})},send(E,J){return new Response(E,{status:J!==null&&J!==void 0?J:I,headers:z})},json(E,J){return new Response(JSON.stringify(E),{status:J!==null&&J!==void 0?J:I,headers:z})},html(E,J){return new Response(Bun.file(E),{status:J!==null&&J!==void 0?J:I,headers:z})},file(E,J){return new Response(Bun.file(E),{status:J!==null&&J!==void 0?J:I,headers:z})},redirect(E,J){return z.set("Location",E),new Response(null,{status:J!==null&&J!==void 0?J:302,headers:z})},getParams(E){if(!X)X=B(L===null||L===void 0?void 0:L.routePattern,F===null||F===void 0?void 0:F.pathname);return E?X[E]||{}:X},getQuery(E){try{if(!U)U=Object.fromEntries(F.searchParams);return E?U[E]||{}:U}catch(J){return{}}},cookie(E,J,M={}){let V=`${encodeURIComponent(E)}=${encodeURIComponent(J)}`;if(M.maxAge)V+=`; Max-Age=${M.maxAge}`;if(M.expires)V+=`; Expires=${M.expires.toUTCString()}`;if(M.path)V+=`; Path=${M.path}`;if(M.domain)V+=`; Domain=${M.domain}`;if(M.secure)V+="; Secure";if(M.httpOnly)V+="; HttpOnly";if(M.sameSite)V+=`; SameSite=${M.sameSite}`;return z===null||z===void 0||z.append("Set-Cookie",V),this},getCookie(E){var J;if(!Z||Object.keys(Z).length===0){let M=(J=L.headers)===null||J===void 0?void 0:J.get("cookie");if(M)Z=T(M);else return null}if(!Z)return null;return E?Z[E]!==void 0?Z[E]:null:Z}}}function T(L){let G={};if(!L)return G;return L.split(";").forEach((z)=>{var K;let[Y,U]=(K=z===null||z===void 0?void 0:z.trim())===null||K===void 0?void 0:K.split("=");if(Y&&U)G[Y.trim()]=U.split(" ")[0].trim()}),G}function B(L,G){let F={},z=L.split("/"),[K]=G.split("?"),Y=K.split("/");if(z.length!==Y.length)return null;return z.forEach((U,Z)=>{if(U.startsWith(":")){let X=U.slice(1);F[X]=Y[Z]}}),F}function A(L){return Q(this,void 0,void 0,function*(){let G=L.headers.get("Content-Type")||"";if(!G)return{};try{if(G.startsWith("application/json"))return yield L.json();if(G.startsWith("application/x-www-form-urlencoded")){let F=yield L.text();return Object.fromEntries(new URLSearchParams(F))}if(G.startsWith("multipart/form-data")){let F=yield L.formData();return N(F)}return{error:"Unknown request body type"}}catch(F){return{error:"Invalid request body format"}}})}function N(L){let G={};for(let[F,z]of L.entries())G[F]=z;return G}var O=function(L,G,F,z){function K(Y){return Y instanceof F?Y:new F(function(U){U(Y)})}return new(F||(F=Promise))(function(Y,U){function Z(I){try{$(z.next(I))}catch(W){U(W)}}function X(I){try{$(z.throw(I))}catch(W){U(W)}}function $(I){I.done?Y(I.value):K(I.value).then(Z,X)}$((z=z.apply(L,G||[])).next())})};function R(L,G,F,z){return O(this,void 0,void 0,function*(){var K,Y,U;let Z=z.trie.search(F.pathname,L.method);if(!Z||Z.method!==L.method)return new Response(Z?"Method not allowed":`Route not found for ${F.pathname}`,{status:Z?405:404});if(Z.isDynamic)L.routePattern=Z.path;let X=D(L,G,F);if(z.corsConfig){let I=C(L,X,z.corsConfig);if(I)return I}if(z.hasOnReqHook&&z.hooks.onRequest)z.hooks.onRequest(X,G);if(z.hasFilterEnabled){let I=(K=L.routePattern)!==null&&K!==void 0?K:F.pathname;if(z.filters.includes(I)===!1)if(z.filterFunction)try{let E=yield z.filterFunction(X,G);if(E)return E}catch(E){return console.error("Error in filterFunction:",E),new Response(JSON.stringify({message:"Internal Server Error"}),{status:500})}else return new Response(JSON.stringify({message:"Authentication required"}),{status:400})}if(z.hasMiddleware){for(let W of z.globalMiddlewares){let E=yield W(X,G);if(E)return E}let I=z.middlewares.get(F.pathname)||[];for(let W of I){let E=yield W(X,G);if(E)return E}}if(z.hasPreHandlerHook&&z.hooks.preHandler){let I=yield z.hooks.preHandler(X);if(I)return I}let $=z.hasPreHandlerHook?yield(U=(Y=z.hooks).preHandler)===null||U===void 0?void 0:U.call(Y,X):null;if($)return $;try{let I=yield Z.handler(X);if(z.hasPostHandlerHook&&z.hooks.postHandler)yield z.hooks.postHandler(X);if(z.hasOnSendHook&&z.hooks.onSend){let W=yield z.hooks.onSend(X,I);if(W)return W}return I!==null&&I!==void 0?I:new Response("No response from handler",{status:204})}catch(I){return new Response("Internal Server Error",{status:500})}})}function C(L,G,F={}){var z,K,Y,U,Z;let X=(z=L.headers.get("origin"))!==null&&z!==void 0?z:"*",$=F===null||F===void 0?void 0:F.origin,I=(K=F===null||F===void 0?void 0:F.allowedHeaders)!==null&&K!==void 0?K:["Content-Type","Authorization"],W=(Y=F===null||F===void 0?void 0:F.methods)!==null&&Y!==void 0?Y:["GET","POST","PUT","DELETE","OPTIONS"],E=(U=F===null||F===void 0?void 0:F.credentials)!==null&&U!==void 0?U:!1,J=(Z=F===null||F===void 0?void 0:F.exposedHeaders)!==null&&Z!==void 0?Z:[];if(G.setHeader("Access-Control-Allow-Methods",W),G.setHeader("Access-Control-Allow-Headers",I),G.setHeader("Access-Control-Allow-Credentials",E),J.length)G.setHeader("Access-Control-Expose-Headers",J);if($==="*")G.setHeader("Access-Control-Allow-Origin","*");else if(Array.isArray($))if(X&&$.includes(X))G.setHeader("Access-Control-Allow-Origin",X);else if($.includes("*"))G.setHeader("Access-Control-Allow-Origin","*");else return G.status(403).json({message:"CORS not allowed"});else if(typeof $==="string")if(X===$)G.setHeader("Access-Control-Allow-Origin",X);else return G.status(403).json({message:"CORS not allowed"});else return G.status(403).json({message:"CORS not allowed"});if(G.setHeader("Access-Control-Allow-Origin",X),L.method==="OPTIONS")return G.setHeader("Access-Control-Max-Age","86400"),G.status(204).text("");return null}export{R as default};
1
+ function D(I,F,G){let z=new Headers,J={},L=!1,Z,U=null,Y,_,$=200,A={};return{req:I,server:F,url:G,getUser(){return A},setUser(E){if(E)A=E},status(E){return $=E,this},getIP(){return this.server.requestIP(this.req)},async getBody(){if(!_)_=await M(I);if(_.error)return new Response(JSON.stringify({error:_.error}),{status:400});return _},setHeader(E,X){return z.set(E,X),this},set(E,X){return J[E]=X,this},get(E){return J[E]||null},setAuth(E){return L=E,this},getAuth(){return L},text(E,X){return new Response(E,{status:X??$,headers:z})},send(E,X){return new Response(E,{status:X??$,headers:z})},json(E,X){return new Response(JSON.stringify(E),{status:X??$,headers:z})},html(E,X){return new Response(Bun.file(E),{status:X??$,headers:z})},file(E,X){return new Response(Bun.file(E),{status:X??$,headers:z})},redirect(E,X){return z.set("Location",E),new Response(null,{status:X??302,headers:z})},setCookie(E,X,K={}){let W=`${encodeURIComponent(E)}=${encodeURIComponent(X)}`;if(K.maxAge)W+=`; Max-Age=${K.maxAge}`;if(K.expires)W+=`; Expires=${K.expires.toUTCString()}`;if(K.path)W+=`; Path=${K.path}`;if(K.domain)W+=`; Domain=${K.domain}`;if(K.secure)W+="; Secure";if(K.httpOnly)W+="; HttpOnly";if(K.sameSite)W+=`; SameSite=${K.sameSite}`;return z?.append("Set-Cookie",W),this},getParams(E){if(!Y&&I?.routePattern)Y=j(I?.routePattern,G?.pathname);return E?Y[E]||{}:Y},getQuery(E){try{if(!Z)Z=Object.fromEntries(G.searchParams);return E?Z[E]||{}:Z}catch(X){return{}}},getCookie(E){if(!U){let X=I.headers.get("cookie");if(X)U=T(X);else return null}if(!U)return null;if(E)return U[E]??null;else return U}}}function T(I){let F={},G=I?.split(";");for(let z=0;z<G?.length;z++){let[J,...L]=G[z].trim().split("="),Z=L?.join("=").trim();if(J)F[J.trim()]=decodeURIComponent(Z)}return F}function j(I,F){let G={},z=I.split("/"),[J]=F.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 M(I){let F=I.headers.get("Content-Type");if(!F)return{};try{if(F.startsWith("application/json"))return await I.json();if(F.startsWith("application/x-www-form-urlencoded")){let G=await I.text();return Object.fromEntries(new URLSearchParams(G))}if(F.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 Q(I,F,G,z){let J=z.trie.search(G.pathname,I.method);if(J?.isDynamic)I.routePattern=J.path;let L=D(I,F,G);if(z.corsConfig){let U=V(I,L,z.corsConfig);if(U)return U}if(z.hasOnReqHook&&z.hooks.onRequest)z.hooks.onRequest(I,G,F);if(z.hasFilterEnabled){let U=I.routePattern??G.pathname;if(!z.filters.has(U))if(z.filterFunction)try{let Y=await z.filterFunction(L,F);if(Y)return Y}catch(Y){return console.error("Error in filterFunction:",Y),L.status(500).json({message:"Internal Server Error",error:Y.message})}else return L.status(400).json({message:"Authentication required"})}if(z.hasMiddleware){let U=z.globalMiddlewares;for(let _=0;_<U.length;_++){let $=await U[_](L,F);if($)return $}let Y=z.middlewares.get(G.pathname)||[];for(let _=0;_<Y.length;_++){let $=await Y[_](L,F);if($)return $}}if(!J||J.method!==I.method){let U=J?"Method not allowed":`Route not found for ${G.pathname}`,Y=J?405:404;return new Response(JSON.stringify({message:U}),{status:Y})}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.status(204).json({message:"No response from this handler"})}function V(I,F,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,Y=G?.exposedHeaders??[];if(F.setHeader("Access-Control-Allow-Methods",Z),F.setHeader("Access-Control-Allow-Headers",L),F.setHeader("Access-Control-Allow-Credentials",U),Y.length)F.setHeader("Access-Control-Expose-Headers",Y);if(J==="*")F.setHeader("Access-Control-Allow-Origin","*");else if(Array.isArray(J))if(z&&J.includes(z))F.setHeader("Access-Control-Allow-Origin",z);else if(J.includes("*"))F.setHeader("Access-Control-Allow-Origin","*");else return F.status(403).json({message:"CORS not allowed"});else if(typeof J==="string")if(z===J)F.setHeader("Access-Control-Allow-Origin",z);else return F.status(403).json({message:"CORS not allowed"});else return F.status(403).json({message:"CORS not allowed"});if(F.setHeader("Access-Control-Allow-Origin",z),I.method==="OPTIONS")return F.setHeader("Access-Control-Max-Age","86400"),F.status(204).text("");return null}export{Q as default};
package/dist/main.d.ts CHANGED
@@ -1,9 +1,8 @@
1
1
  import Trie from "./trie.js";
2
- import rateLimit from "./utils.js";
3
- import { corsT, FilterMethods, HookFunction, HookType, middlewareFunc, type handlerFunction, type Hooks, type HttpMethod, type listenCalllBackType } from "./types.js";
2
+ import { corsT, FilterMethods, HookFunction, HookType, middlewareFunc, onError, onRequest, type handlerFunction, type Hooks, type HttpMethod, type listenCalllBackType } from "./types.js";
4
3
  import { Server } from "bun";
5
4
  export default class Diesel {
6
- routes: string[] | undefined;
5
+ tempRoutes: Map<string, any>;
7
6
  globalMiddlewares: middlewareFunc[];
8
7
  middlewares: Map<string, middlewareFunc[]>;
9
8
  trie: Trie;
@@ -12,25 +11,26 @@ export default class Diesel {
12
11
  hasPreHandlerHook: boolean;
13
12
  hasPostHandlerHook: boolean;
14
13
  hasOnSendHook: boolean;
14
+ hasOnError: boolean;
15
15
  hooks: Hooks;
16
16
  corsConfig: corsT;
17
- filters: string[];
17
+ FilterRoutes: string[] | null | undefined;
18
+ filters: Set<string>;
18
19
  filterFunction: middlewareFunc | null;
19
20
  hasFilterEnabled: boolean;
20
- wss: WebSocket | null | undefined;
21
21
  constructor();
22
22
  filter(): FilterMethods;
23
23
  cors(corsConfig: corsT): void;
24
- addHooks(typeOfHook: HookType, fnc: HookFunction): void;
24
+ addHooks(typeOfHook: HookType, fnc: HookFunction | onError | onRequest): void;
25
25
  compile(): void;
26
26
  listen(port: number, callback?: listenCalllBackType, { sslCert, sslKey }?: any): Server | void;
27
+ route(basePath: string, routerInstance: any): void;
27
28
  register(pathPrefix: string, handlerInstance: any): void;
28
29
  addRoute(method: HttpMethod, path: string, handlers: handlerFunction[]): void;
29
- use(pathORHandler?: string | middlewareFunc, handler?: middlewareFunc): void;
30
+ use(pathORHandler?: string | middlewareFunc, ...handlers: middlewareFunc[]): void;
30
31
  get(path: string, ...handlers: handlerFunction[]): this;
31
32
  post(path: string, ...handlers: handlerFunction[]): this;
32
33
  put(path: string, ...handlers: handlerFunction[]): this;
33
34
  patch(path: string, ...handlers: handlerFunction[]): this;
34
35
  delete(path: any, ...handlers: handlerFunction[]): this;
35
36
  }
36
- export { rateLimit, };
package/dist/main.js CHANGED
@@ -1 +1 @@
1
- class A{constructor(){this.children={},this.isEndOfWord=!1,this.handler=[],this.isDynamic=!1,this.pattern="",this.path="",this.method=[],this.subMiddlewares=new Map}}class K{constructor(){this.root=new A}insert(G,J){let z=this.root,U=G.split("/").filter(Boolean);if(G==="/"){z.isEndOfWord=!0,z.handler.push(J.handler),z.path=G,z.method.push(J.method);return}for(let Y of U){let Z=!1,X=Y;if(Y.startsWith(":"))Z=!0,X=":";if(!z.children[X])z.children[X]=new A;z=z.children[X],z.isDynamic=Z,z.pattern=Y,z.method.push(J.method),z.handler.push(J.handler),z.path=G}z.isEndOfWord=!0,z.method.push(J.method),z.handler.push(J.handler),z.path=G}search(G,J){let z=this.root,U=G.split("/").filter(Boolean);for(let Z of U){let X=Z;if(!z.children[X])if(z.children[":"])z=z.children[":"];else return null;else z=z.children[X]}let Y=z.method.indexOf(J);if(Y!==-1)return{path:z.path,handler:z.handler[Y],isDynamic:z.isDynamic,pattern:z.pattern,method:z.method[Y]};return{path:z.path,handler:z.handler,isDynamic:z.isDynamic,pattern:z.pattern,method:z.method[Y]}}}var N=function(G,J,z,U){function Y(Z){return Z instanceof z?Z:new z(function(X){X(Z)})}return new(z||(z=Promise))(function(Z,X){function F(E){try{V(U.next(E))}catch(Q){X(Q)}}function L(E){try{V(U.throw(E))}catch(Q){X(Q)}}function V(E){E.done?Z(E.value):Y(E.value).then(F,L)}V((U=U.apply(G,J||[])).next())})};function M(G,J,z){let U=new Headers,Y={},Z=!1,X,F=null,L,V,E=200,Q={};return{req:G,server:J,url:z,getUser(){return Q},setUser($){if($)Q=$},status($){return E=$,this},getIP(){return this.server.requestIP(this.req)},body(){return N(this,void 0,void 0,function*(){if(!V)V=yield O(G);if(V.error)return new Response(JSON.stringify({error:V.error}),{status:400});return V})},setHeader($,W){return U.set($,W),this},set($,W){return Y[$]=W,this},get($){return Y[$]||null},setAuth($){return Z=$,this},getAuth(){return Z},text($,W){return new Response($,{status:W!==null&&W!==void 0?W:E,headers:U})},send($,W){return new Response($,{status:W!==null&&W!==void 0?W:E,headers:U})},json($,W){return new Response(JSON.stringify($),{status:W!==null&&W!==void 0?W:E,headers:U})},html($,W){return new Response(Bun.file($),{status:W!==null&&W!==void 0?W:E,headers:U})},file($,W){return new Response(Bun.file($),{status:W!==null&&W!==void 0?W:E,headers:U})},redirect($,W){return U.set("Location",$),new Response(null,{status:W!==null&&W!==void 0?W:302,headers:U})},getParams($){if(!L)L=I(G===null||G===void 0?void 0:G.routePattern,z===null||z===void 0?void 0:z.pathname);return $?L[$]||{}:L},getQuery($){try{if(!X)X=Object.fromEntries(z.searchParams);return $?X[$]||{}:X}catch(W){return{}}},cookie($,W,B={}){let D=`${encodeURIComponent($)}=${encodeURIComponent(W)}`;if(B.maxAge)D+=`; Max-Age=${B.maxAge}`;if(B.expires)D+=`; Expires=${B.expires.toUTCString()}`;if(B.path)D+=`; Path=${B.path}`;if(B.domain)D+=`; Domain=${B.domain}`;if(B.secure)D+="; Secure";if(B.httpOnly)D+="; HttpOnly";if(B.sameSite)D+=`; SameSite=${B.sameSite}`;return U===null||U===void 0||U.append("Set-Cookie",D),this},getCookie($){var W;if(!F||Object.keys(F).length===0){let B=(W=G.headers)===null||W===void 0?void 0:W.get("cookie");if(B)F=j(B);else return null}if(!F)return null;return $?F[$]!==void 0?F[$]:null:F}}}function j(G){let J={};if(!G)return J;return G.split(";").forEach((U)=>{var Y;let[Z,X]=(Y=U===null||U===void 0?void 0:U.trim())===null||Y===void 0?void 0:Y.split("=");if(Z&&X)J[Z.trim()]=X.split(" ")[0].trim()}),J}function I(G,J){let z={},U=G.split("/"),[Y]=J.split("?"),Z=Y.split("/");if(U.length!==Z.length)return null;return U.forEach((X,F)=>{if(X.startsWith(":")){let L=X.slice(1);z[L]=Z[F]}}),z}function O(G){return N(this,void 0,void 0,function*(){let J=G.headers.get("Content-Type")||"";if(!J)return{};try{if(J.startsWith("application/json"))return yield G.json();if(J.startsWith("application/x-www-form-urlencoded")){let z=yield G.text();return Object.fromEntries(new URLSearchParams(z))}if(J.startsWith("multipart/form-data")){let z=yield G.formData();return S(z)}return{error:"Unknown request body type"}}catch(z){return{error:"Invalid request body format"}}})}function S(G){let J={};for(let[z,U]of G.entries())J[z]=U;return J}var b=function(G,J,z,U){function Y(Z){return Z instanceof z?Z:new z(function(X){X(Z)})}return new(z||(z=Promise))(function(Z,X){function F(E){try{V(U.next(E))}catch(Q){X(Q)}}function L(E){try{V(U.throw(E))}catch(Q){X(Q)}}function V(E){E.done?Z(E.value):Y(E.value).then(F,L)}V((U=U.apply(G,J||[])).next())})};function T(G,J,z,U){return b(this,void 0,void 0,function*(){var Y,Z,X;let F=U.trie.search(z.pathname,G.method);if(!F||F.method!==G.method)return new Response(F?"Method not allowed":`Route not found for ${z.pathname}`,{status:F?405:404});if(F.isDynamic)G.routePattern=F.path;let L=M(G,J,z);if(U.corsConfig){let E=w(G,L,U.corsConfig);if(E)return E}if(U.hasOnReqHook&&U.hooks.onRequest)U.hooks.onRequest(L,J);if(U.hasFilterEnabled){let E=(Y=G.routePattern)!==null&&Y!==void 0?Y:z.pathname;if(U.filters.includes(E)===!1)if(U.filterFunction)try{let $=yield U.filterFunction(L,J);if($)return $}catch($){return console.error("Error in filterFunction:",$),new Response(JSON.stringify({message:"Internal Server Error"}),{status:500})}else return new Response(JSON.stringify({message:"Authentication required"}),{status:400})}if(U.hasMiddleware){for(let Q of U.globalMiddlewares){let $=yield Q(L,J);if($)return $}let E=U.middlewares.get(z.pathname)||[];for(let Q of E){let $=yield Q(L,J);if($)return $}}if(U.hasPreHandlerHook&&U.hooks.preHandler){let E=yield U.hooks.preHandler(L);if(E)return E}let V=U.hasPreHandlerHook?yield(X=(Z=U.hooks).preHandler)===null||X===void 0?void 0:X.call(Z,L):null;if(V)return V;try{let E=yield F.handler(L);if(U.hasPostHandlerHook&&U.hooks.postHandler)yield U.hooks.postHandler(L);if(U.hasOnSendHook&&U.hooks.onSend){let Q=yield U.hooks.onSend(L,E);if(Q)return Q}return E!==null&&E!==void 0?E:new Response("No response from handler",{status:204})}catch(E){return new Response("Internal Server Error",{status:500})}})}function w(G,J,z={}){var U,Y,Z,X,F;let L=(U=G.headers.get("origin"))!==null&&U!==void 0?U:"*",V=z===null||z===void 0?void 0:z.origin,E=(Y=z===null||z===void 0?void 0:z.allowedHeaders)!==null&&Y!==void 0?Y:["Content-Type","Authorization"],Q=(Z=z===null||z===void 0?void 0:z.methods)!==null&&Z!==void 0?Z:["GET","POST","PUT","DELETE","OPTIONS"],$=(X=z===null||z===void 0?void 0:z.credentials)!==null&&X!==void 0?X:!1,W=(F=z===null||z===void 0?void 0:z.exposedHeaders)!==null&&F!==void 0?F:[];if(J.setHeader("Access-Control-Allow-Methods",Q),J.setHeader("Access-Control-Allow-Headers",E),J.setHeader("Access-Control-Allow-Credentials",$),W.length)J.setHeader("Access-Control-Expose-Headers",W);if(V==="*")J.setHeader("Access-Control-Allow-Origin","*");else if(Array.isArray(V))if(L&&V.includes(L))J.setHeader("Access-Control-Allow-Origin",L);else if(V.includes("*"))J.setHeader("Access-Control-Allow-Origin","*");else return J.status(403).json({message:"CORS not allowed"});else if(typeof V==="string")if(L===V)J.setHeader("Access-Control-Allow-Origin",L);else return J.status(403).json({message:"CORS not allowed"});else return J.status(403).json({message:"CORS not allowed"});if(J.setHeader("Access-Control-Allow-Origin",L),G.method==="OPTIONS")return J.setHeader("Access-Control-Max-Age","86400"),J.status(204).text("");return null}function C(G){let{time:J=60000,max:z=100,message:U="Rate limit exceeded. Please try again later."}=G,Y=new Map;return(Z)=>{let X=new Date,F=Z.getIP().address;if(!Y.has(F))Y.set(F,{count:0,startTime:X});let L=Y.get(F);if(L)if(X-L.startTime>J)L.count=1,L.startTime=X;else L.count++;if(L&&L.count>z)return Z.status(429).json({error:U})}}var H=function(G,J,z,U){function Y(Z){return Z instanceof z?Z:new z(function(X){X(Z)})}return new(z||(z=Promise))(function(Z,X){function F(E){try{V(U.next(E))}catch(Q){X(Q)}}function L(E){try{V(U.throw(E))}catch(Q){X(Q)}}function V(E){E.done?Z(E.value):Y(E.value).then(F,L)}V((U=U.apply(G,J||[])).next())})};class R{constructor(){this.routes=[],this.globalMiddlewares=[],this.middlewares=new Map,this.trie=new K,this.corsConfig=null,this.hasMiddleware=!1,this.hasOnReqHook=!1,this.hasPreHandlerHook=!1,this.hasPostHandlerHook=!1,this.hasOnSendHook=!1,this.hooks={onRequest:null,preHandler:null,postHandler:null,onSend:null,onError:null,onClose:null},this.filters=[],this.filterFunction=null,this.hasFilterEnabled=!1,this.wss=null}filter(){return this.hasFilterEnabled=!0,{routeMatcher:(...G)=>{return this.routes=G.sort(),this.filter()},permitAll:()=>{for(let G of this===null||this===void 0?void 0:this.routes)this.filters.push(G);return this.filter()},require:(G)=>{if(G)this.filterFunction=G}}}cors(G){this.corsConfig=G}addHooks(G,J){if(typeof G!=="string")throw new Error("hookName must be a string");if(typeof J!=="function")throw new Error("callback must be a instance of function");if(this.hooks.hasOwnProperty(G))this.hooks[G]=J;else throw new Error(`Unknown hook type: ${G}`)}compile(){if(this.globalMiddlewares.length>0)this.hasMiddleware=!0;for(let[G,J]of this.middlewares.entries())if(J.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}listen(G,J,{sslCert:z=null,sslKey:U=null}={}){if(typeof Bun==="undefined")throw new Error(".listen() is designed to run on Bun only...");if(typeof G!=="number")throw new Error("Port must be a numeric value");this.compile();let Y={port:G,fetch:(X,F)=>H(this,void 0,void 0,function*(){let L=new URL(X.url);try{return yield T(X,F,L,this)}catch(V){return new Response("Internal Server Error",{status:500})}}),onClose(){console.log("Server is shutting down...")}};if(z&&U)Y.certFile=z,Y.keyFile=U;let Z=Bun.serve(Y);if(Bun===null||Bun===void 0||Bun.gc(!1),typeof J==="function")return J();if(z&&U)console.log(`HTTPS server is running on https://localhost:${G}`);else console.log(`HTTP server is running on http://localhost:${G}`);return Z}register(G,J){if(typeof G!=="string")throw new Error("path must be a string");if(typeof J!=="object")throw new Error("handler parameter should be a instance of router object",J);let z=Object.entries(J.trie.root.children);J.trie.root.subMiddlewares.forEach((U,Y)=>{if(!this.middlewares.has(G+Y))this.middlewares.set(G+Y,[]);U===null||U===void 0||U.forEach((Z)=>{var X,F;if(!((X=this.middlewares.get(G+Y))===null||X===void 0?void 0:X.includes(Z)))(F=this.middlewares.get(G+Y))===null||F===void 0||F.push(Z)})});for(let[U,Y]of z){let Z=G+(Y===null||Y===void 0?void 0:Y.path),X=Y.handler[0],F=Y.method[0];this.trie.insert(Z,{handler:X,method:F})}J.trie=new K}addRoute(G,J,z){if(typeof J!=="string")throw new Error("Path must be a string type");if(typeof G!=="string")throw new Error("method must be a string type");let U=z.slice(0,-1),Y=z[z.length-1];if(!this.middlewares.has(J))this.middlewares.set(J,[]);U.forEach((Z)=>{var X,F;if(J==="/"){if(!this.globalMiddlewares.includes(Z))this.globalMiddlewares.push(Z)}else if(!((X=this.middlewares.get(J))===null||X===void 0?void 0:X.includes(Z)))(F=this.middlewares.get(J))===null||F===void 0||F.push(Z)}),this.trie.insert(J,{handler:Y,method:G})}use(G,J){var z,U;if(typeof G==="function"){if(!this.globalMiddlewares.includes(G))this.globalMiddlewares.push(G);return}let Y=G;if(!this.middlewares.has(Y))this.middlewares.set(Y,[]);if(J){if(!((z=this.middlewares.get(Y))===null||z===void 0?void 0:z.includes(J)))(U=this.middlewares.get(Y))===null||U===void 0||U.push(J)}}get(G,...J){return this.addRoute("GET",G,J),this}post(G,...J){return this.addRoute("POST",G,J),this}put(G,...J){return this.addRoute("PUT",G,J),this}patch(G,...J){return this.addRoute("PATCH",G,J),this}delete(G,...J){return this.addRoute("DELETE",G,J),this}}export{C as rateLimit,R as default};
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],Y=!1,Z=U;if(U.startsWith(":"))Y=!0,Z=":";if(!G.children[Z])G.children[Z]=new V;if(G=G.children[Z],G.isDynamic=Y,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 Y=U;if(!G.children[Y])if(G.children[":"])G=G.children[":"];else return null;else G=G.children[Y]}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,Y,Z=null,_,E,W=200,j={};return{req:F,server:z,url:G,getUser(){return j},setUser(X){if(X)j=X},status(X){return W=X,this},getIP(){return this.server.requestIP(this.req)},async getBody(){if(!E)E=await N(F);if(E.error)return new Response(JSON.stringify({error:E.error}),{status:400});return E},setHeader(X,$){return J.set(X,$),this},set(X,$){return L[X]=$,this},get(X){return L[X]||null},setAuth(X){return U=X,this},getAuth(){return U},text(X,$){return new Response(X,{status:$??W,headers:J})},send(X,$){return new Response(X,{status:$??W,headers:J})},json(X,$){return new Response(JSON.stringify(X),{status:$??W,headers:J})},html(X,$){return new Response(Bun.file(X),{status:$??W,headers:J})},file(X,$){return new Response(Bun.file(X),{status:$??W,headers:J})},redirect(X,$){return J.set("Location",X),new Response(null,{status:$??302,headers:J})},setCookie(X,$,A={}){let D=`${encodeURIComponent(X)}=${encodeURIComponent($)}`;if(A.maxAge)D+=`; Max-Age=${A.maxAge}`;if(A.expires)D+=`; Expires=${A.expires.toUTCString()}`;if(A.path)D+=`; Path=${A.path}`;if(A.domain)D+=`; Domain=${A.domain}`;if(A.secure)D+="; Secure";if(A.httpOnly)D+="; HttpOnly";if(A.sameSite)D+=`; SameSite=${A.sameSite}`;return J?.append("Set-Cookie",D),this},getParams(X){if(!_&&F?.routePattern)_=T(F?.routePattern,G?.pathname);return X?_[X]||{}:_},getQuery(X){try{if(!Y)Y=Object.fromEntries(G.searchParams);return X?Y[X]||{}:Y}catch($){return{}}},getCookie(X){if(!Z){let $=F.headers.get("cookie");if($)Z=I($);else return null}if(!Z)return null;if(X)return Z[X]??null;else return Z}}}function I(F){let z={},G=F?.split(";");for(let J=0;J<G?.length;J++){let[L,...U]=G[J].trim().split("="),Y=U?.join("=").trim();if(L)z[L.trim()]=decodeURIComponent(Y)}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 Y=0;Y<J.length;Y++)if(J[Y].startsWith(":"))G[J[Y].slice(1)]=U[Y];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 K(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 Z=C(F,U,J.corsConfig);if(Z)return Z}if(J.hasOnReqHook&&J.hooks.onRequest)J.hooks.onRequest(F,G,z);if(J.hasFilterEnabled){let Z=F.routePattern??G.pathname;if(!J.filters.has(Z))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 Z=J.globalMiddlewares;for(let E=0;E<Z.length;E++){let W=await Z[E](U,z);if(W)return W}let _=J.middlewares.get(G.pathname)||[];for(let E=0;E<_.length;E++){let W=await _[E](U,z);if(W)return W}}if(!L||L.method!==F.method){let Z=L?"Method not allowed":`Route not found for ${G.pathname}`,_=L?405:404;return new Response(JSON.stringify({message:Z}),{status:_})}if(J.hasPreHandlerHook&&J.hooks.preHandler){let Z=await J.hooks.preHandler(U);if(Z)return Z}let Y=await L.handler(U);if(J.hasPostHandlerHook&&J.hooks.postHandler)await J.hooks.postHandler(U);if(J.hasOnSendHook&&J.hooks.onSend){let Z=await J.hooks.onSend(U,Y);if(Z)return Z}return Y??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"],Y=G?.methods??["GET","POST","PUT","DELETE","OPTIONS"],Z=G?.credentials??!1,_=G?.exposedHeaders??[];if(z.setHeader("Access-Control-Allow-Methods",Y),z.setHeader("Access-Control-Allow-Headers",U),z.setHeader("Access-Control-Allow-Credentials",Z),_.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){this.corsConfig=F}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}`)}}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,z,{sslCert:G=null,sslKey:J=null}={}){if(typeof Bun==="undefined")throw new Error(".listen() is designed to run on Bun only...");if(typeof F!=="number")throw new Error("Port must be a numeric value");this.compile();let L={port:F,fetch:async(Y,Z)=>{let _=new URL(Y.url);try{return await K(Y,Z,_,this)}catch(E){if(this.hasOnError&&this.hooks.onError){let W=await this.hooks.onError(E,Y,_,Z);if(W)return W}return new Response(JSON.stringify({message:"Internal Server Error",error:E.message}),{status:500})}}};if(G&&J)L.certFile=G,L.keyFile=J;let U=Bun?.serve(L);if(typeof z==="function")return z();if(G&&J)console.log(`HTTPS server is running on https://localhost:${F}`);else console.log(`HTTP server is running on http://localhost:${F}`);return U}route(F,z){if(!F||typeof F!=="string")throw new Error("Path must be a string");let G=Object.fromEntries(z.tempRoutes);Object.entries(G).forEach(([L,U])=>{let Y=`${F}${L}`;if(!this.middlewares.has(Y))this.middlewares.set(Y,[]);U.handlers.slice(0,-1).forEach((W)=>{if(!this.middlewares.get(Y)?.includes(W))this.middlewares.get(Y)?.push(W)});let _=U.handlers[U.handlers.length-1],E=U.method;try{this.trie.insert(Y,{handler:_,method:E})}catch(W){console.error(`Error inserting ${Y}:`,W)}}),z=null}register(F,z){this.route(F,z)}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)})}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};
package/dist/trie.d.ts CHANGED
@@ -20,12 +20,6 @@ export default class Trie {
20
20
  isDynamic: boolean;
21
21
  pattern: string;
22
22
  method: string;
23
- } | {
24
- path: string;
25
- handler: handlerFunction[];
26
- isDynamic: boolean;
27
- pattern: string;
28
- method: string;
29
23
  } | null;
30
24
  }
31
25
  export {};
package/dist/trie.js CHANGED
@@ -1 +1 @@
1
- class C{constructor(){this.children={},this.isEndOfWord=!1,this.handler=[],this.isDynamic=!1,this.pattern="",this.path="",this.method=[],this.subMiddlewares=new Map}}class E{constructor(){this.root=new C}insert(w,q){let j=this.root,B=w.split("/").filter(Boolean);if(w==="/"){j.isEndOfWord=!0,j.handler.push(q.handler),j.path=w,j.method.push(q.method);return}for(let v of B){let A=!1,z=v;if(v.startsWith(":"))A=!0,z=":";if(!j.children[z])j.children[z]=new C;j=j.children[z],j.isDynamic=A,j.pattern=v,j.method.push(q.method),j.handler.push(q.handler),j.path=w}j.isEndOfWord=!0,j.method.push(q.method),j.handler.push(q.handler),j.path=w}search(w,q){let j=this.root,B=w.split("/").filter(Boolean);for(let A of B){let z=A;if(!j.children[z])if(j.children[":"])j=j.children[":"];else return null;else j=j.children[z]}let v=j.method.indexOf(q);if(v!==-1)return{path:j.path,handler:j.handler[v],isDynamic:j.isDynamic,pattern:j.pattern,method:j.method[v]};return{path:j.path,handler:j.handler,isDynamic:j.isDynamic,pattern:j.pattern,method:j.method[v]}}}export{E as default};
1
+ class E{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 F{root;constructor(){this.root=new E}insert(q,w){let b=this.root,z=q.split("/").filter(Boolean);if(q==="/"){b.isEndOfWord=!0,b.handler=[w.handler],b.path=q,b.method=[w.method];return}for(let j=0;j<z.length;j++){let A=z[j],B=!1,C=A;if(A.startsWith(":"))B=!0,C=":";if(!b.children[C])b.children[C]=new E;if(b=b.children[C],b.isDynamic=B,b.pattern=A,j===z.length-1)b.handler=[w.handler],b.method=[w.method],b.isEndOfWord=!0,b.path=q}}search(q,w){let b=this.root,z=q.split("/").filter(Boolean);for(let A of z){let B=A;if(!b.children[B])if(b.children[":"])b=b.children[":"];else return null;else b=b.children[B]}let j=b.method.indexOf(w);if(b.isEndOfWord&&j!==-1)return{path:b.path,handler:b.handler[j],isDynamic:b.isDynamic,pattern:b.pattern,method:b.method[j]};return null}}export{F as default};
package/dist/types.d.ts CHANGED
@@ -2,22 +2,21 @@ import { Server } from "bun";
2
2
  export type listenCalllBackType = () => void;
3
3
  export type handlerFunction = (ctx: ContextType, server?: Server) => Response | Promise<Response | null | void>;
4
4
  export type middlewareFunc = (ctx: ContextType, server?: Server | undefined) => null | void | Response | Promise<Response | void | null>;
5
- export type HookFunction = (ctx: ContextType, result?: Response | null | void, server?: Server) => Response | Promise<Response | null | void>;
5
+ export type HookFunction = (ctx: ContextType, result?: Response | null | void, server?: Server) => Response | Promise<Response | null | void> | void;
6
6
  export type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS" | "HEAD";
7
- export declare enum HookType {
8
- onRequest = "onRequest",
9
- preHandler = "preHandler",
10
- postHandler = "postHandler",
11
- onSend = "onSend",
12
- onError = "onError",
13
- onClose = "onClose"
7
+ export type HookType = 'onRequest' | 'preHandler' | 'postHandler' | 'onSend' | 'onError' | 'onClose';
8
+ export interface onError {
9
+ (error: Error, req: Request, url: URL, server: Server): void | null | Response | Promise<Response | null | void>;
10
+ }
11
+ export interface onRequest {
12
+ (req: Request, url: URL, server: Server): void | null | Response | Promise<Response | null | void>;
14
13
  }
15
14
  export interface Hooks {
16
- onRequest: HookFunction | null;
15
+ onRequest: onRequest | null;
17
16
  preHandler: HookFunction | null;
18
17
  postHandler: HookFunction | null;
19
18
  onSend: HookFunction | null;
20
- onError: HookFunction | null;
19
+ onError: onError | null;
21
20
  onClose: HookFunction | null;
22
21
  }
23
22
  export interface ContextType {
@@ -28,7 +27,7 @@ export interface ContextType {
28
27
  getUser: () => any;
29
28
  status: (status: number) => this;
30
29
  getIP: () => any;
31
- body: () => Promise<any>;
30
+ getBody: () => Promise<any>;
32
31
  setHeader: (key: string, value: any) => this;
33
32
  set: (key: string, value: any) => this;
34
33
  get: (key: string) => any;
@@ -42,7 +41,7 @@ export interface ContextType {
42
41
  redirect: (path: string, status?: number) => Response;
43
42
  getParams: (props?: any) => any;
44
43
  getQuery: (props?: any) => any;
45
- cookie: (name: string, value: string, options?: CookieOptions) => this;
44
+ setCookie: (name: string, value: string, options?: CookieOptions) => this;
46
45
  getCookie: (cookieName?: string) => any;
47
46
  }
48
47
  export interface CookieOptions {
@@ -72,12 +71,13 @@ export interface DieselT {
72
71
  hasPostHandlerHook: boolean;
73
72
  hasOnSendHook: boolean;
74
73
  hooks: {
75
- onRequest: ((ctx: ContextType, serer?: Server) => void) | null;
76
- preHandler: ((ctx: ContextType, serer?: Server) => Promise<Response | void | null>) | null;
77
- postHandler: ((ctx: ContextType, serer?: Server) => Promise<Response | void | null>) | null;
78
- onSend: ((ctx?: ContextType, result?: Response | null | void, serer?: Server) => Promise<Response | void | null>) | null;
74
+ onRequest: ((req: Request, url: URL, serer: Server) => void) | null;
75
+ preHandler: ((ctx: ContextType, serer?: Server) => Response | Promise<Response | void | null>) | null;
76
+ postHandler: ((ctx: ContextType, serer?: Server) => Response | Promise<Response | void | null>) | null;
77
+ onSend: ((ctx?: ContextType, result?: Response | null | void, serer?: Server) => Response | Promise<Response | void | null>) | null;
78
+ onError: ((error: Error, req: Request, url: URL, server?: Server) => void | Response | Promise<Response | null | void>) | null;
79
79
  };
80
- filters: string[];
80
+ filters: Set<string>;
81
81
  hasFilterEnabled: boolean;
82
82
  filterFunction: (ctx: ContextType, serer?: Server) => void | Response | Promise<Response | void | null>;
83
83
  corsConfig: corsT | null;
package/example/main.ts CHANGED
@@ -1,8 +1,9 @@
1
- import Diesel from "../dist/main";
1
+ import Diesel from "../src/main";
2
2
  import jwt from "jsonwebtoken";
3
- import { ContextType, CookieOptions, middlewareFunc } from "../dist/types";
3
+ import { ContextType, CookieOptions, HookType, middlewareFunc } from "../src/types";
4
+ import { Server } from "bun";
4
5
 
5
- const app = new Diesel();
6
+ const app = new Diesel()
6
7
  const secret = "linux";
7
8
 
8
9
  // app.cors({
@@ -11,37 +12,69 @@ const secret = "linux";
11
12
  // allowedHeaders: 'Content-Type,Authorization'
12
13
  // })
13
14
 
14
- async function authJwt(ctx: ContextType): Promise<void | null | Response> {
15
- const token = ctx.getCookie("accessToken");
15
+ const hello = async () => {
16
+ console.log('hello',Math.random())
17
+ }
18
+ const hello2 = async () => {
19
+ console.log('hello2', Math.random())
20
+ }
21
+ const hello3 = async () => {
22
+ console.log('hello3', Math.random())
23
+ }
24
+
16
25
 
26
+
27
+ export async function authJwt(ctx: ContextType): Promise<void | null | Response> {
28
+
29
+ const token = ctx.getCookie("accessToken");
17
30
  if (!token) {
18
31
  return ctx.status(401).json({ message: "Authentication token missing" });
19
32
  }
20
33
  try {
21
- const user = jwt.verify(token, secret); // Replace with your JWT secret
34
+ const user = await jwt.verify(token, secret);
22
35
  ctx.setUser(user);
23
36
  } catch (error) {
24
37
  return ctx.status(403).json({ message: "Invalid token" });
25
38
  }
26
39
  }
27
40
 
28
- app
29
- .filter()
30
- .routeMatcher("/cookie")
31
- .permitAll()
32
- .require(authJwt as middlewareFunc);
41
+ // app.use('/ttt',hello,hello2,hello3)
42
+
43
+ // app
44
+ // .filter()
45
+ // .routeMatcher("/cookie",'/api/user/login','api/user/register')
46
+ // .permitAll()
47
+ // .require(authJwt as middlewareFunc);
48
+
49
+
50
+ app.addHooks('onError', (error: any, req: Request, url: URL, server: Server) => {
51
+ console.error(`Error occurred: ${error.message}`);
52
+ console.error(`Request Method: ${req.method}, Request URL: ${url}`);
53
+ throw new Error('just error')
54
+ // return new Response('Internal Server Error', { status: 500 });
55
+ });
56
+
33
57
 
34
- // app.use(authJwt)
58
+ app.get("/error", async () => {
59
+ throw new Error("This is a test error to demonstrate error handling");
60
+ });
35
61
 
36
62
  app.get("/", async (xl) => {
37
63
  const user = xl.getUser();
38
-
39
64
  return xl.json({
40
- success: true,
41
65
  user,
42
66
  });
43
67
  });
44
68
 
69
+ app.get("/api/user/u",(ctx) => {
70
+ return ctx.text("j")
71
+ })
72
+
73
+ // app.post("/",async (ctx) => {
74
+ // const body = await ctx.getBody()
75
+ // return ctx.json(body)
76
+ // })
77
+
45
78
  app.get("/test/:id/:name", async (xl) => {
46
79
  const q = xl.getQuery();
47
80
  const params = xl.getParams();
@@ -59,20 +92,26 @@ app.get("/cookie", async (xl) => {
59
92
  };
60
93
 
61
94
  const accessToken = jwt.sign(user, secret, { expiresIn: "1d" });
62
- // const refreshToken = jwt.sign(user, secret, { expiresIn: "10d" });
95
+ const refreshToken = jwt.sign(user, secret, { expiresIn: "10d" });
63
96
  const options: CookieOptions = {
64
- httpOnly: true, // Makes cookie accessible only by the web server (not JS)
65
- secure: true, // Ensures the cookie is sent over HTTPS
66
- maxAge: 24 * 60 * 60 * 1000, // 1 day in milliseconds
67
- sameSite: "Strict", // Prevents CSRF (strict origin policy)
68
- path: "/", // Cookie available for all routes
97
+ httpOnly: true,
98
+ secure: true,
99
+ maxAge: 24 * 60 * 60 * 1000,
100
+ sameSite: "Strict",
101
+ path: "/",
69
102
  };
103
+
70
104
  return (
71
105
  xl
72
- .cookie("accessToken", accessToken, options)
73
- // .cookie("refreshToken", refreshToken, options)
74
- .json({ msg: "setting cookies" })
106
+ .setCookie("accessToken", accessToken, options)
107
+ .setCookie("refreshToken", refreshToken, options)
108
+ .json({ msg: "setted cookies" })
75
109
  );
110
+
76
111
  });
77
112
 
113
+ import {userRoute} from './route'
114
+
115
+ app.route("/api/user",userRoute)
116
+
78
117
  app.listen(3000);
@@ -0,0 +1,40 @@
1
+ import Diesel from "../src/main";
2
+ import Router from "../src/route";
3
+ import { authJwt } from "./main";
4
+
5
+ const userRoute = new Router();
6
+
7
+ const h = () => {
8
+ console.log('object');
9
+ }
10
+
11
+ const s = () =>{
12
+ console.log('s')
13
+ }
14
+
15
+ userRoute.get("/register/:id",(xl) => {
16
+ return xl.text("from register user");
17
+ })
18
+
19
+ userRoute.get("/login",(xl)=>{
20
+ return new Response("hello loin")
21
+ })
22
+
23
+ const newRoute = new Diesel()
24
+
25
+ newRoute.get("/login",(xl)=>{
26
+ return xl.json({message:"from login"})
27
+ })
28
+
29
+ newRoute.get("/tetet",(ctx) =>{
30
+ return ctx.text("from tetet")
31
+ })
32
+
33
+ newRoute.get("/register/:id",(xl) => {
34
+ const param = xl.getParams("id")
35
+ return xl.json({message:"from register",param})
36
+ })
37
+
38
+ export {
39
+ userRoute,newRoute
40
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "diesel-core",
3
- "version": "0.0.18",
3
+ "version": "0.0.20",
4
4
  "main": "index.js",
5
5
  "types": "index.d.ts",
6
6
  "scripts": {
@@ -15,5 +15,8 @@
15
15
  },
16
16
  "peerDependencies": {
17
17
  "typescript": "^5.0.0"
18
+ },
19
+ "dependencies": {
20
+ "cookie": "^1.0.1"
18
21
  }
19
22
  }