diesel-core 1.2.0 → 1.2.1
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/dist/main.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import Trie from "./trie.js";
|
|
2
2
|
import { corsT, FilterMethods, HookFunction, HookType, listenArgsT, middlewareFunc, onError, onRequest, type handlerFunction, type Hooks } from "./types.js";
|
|
3
3
|
import { Server } from "bun";
|
|
4
|
+
import { AdvancedLoggerOptions, LoggerOptions } from "./middlewares/logger/logger.js";
|
|
4
5
|
export default class Diesel {
|
|
5
6
|
routes: Record<string, Function>;
|
|
6
7
|
private tempRoutes;
|
|
@@ -35,13 +36,13 @@ export default class Diesel {
|
|
|
35
36
|
setupFilter(): FilterMethods;
|
|
36
37
|
redirect(incomingPath: string, redirectPath: string, statusCode?: 302): this;
|
|
37
38
|
serveStatic(filePath: string): this;
|
|
38
|
-
|
|
39
|
+
staticHtml(args: Record<string, string>): this;
|
|
39
40
|
addHooks(typeOfHook: HookType, fnc: HookFunction | onError | onRequest): this;
|
|
40
41
|
private compile;
|
|
41
42
|
private registerFileRoutes;
|
|
42
43
|
private loadRoutes;
|
|
43
|
-
useLogger(
|
|
44
|
-
useAdvancedLogger(
|
|
44
|
+
useLogger(options: LoggerOptions): this;
|
|
45
|
+
useAdvancedLogger(options: AdvancedLoggerOptions): this;
|
|
45
46
|
BunRoute(method: string, path: string, ...handlers: any[]): void;
|
|
46
47
|
listen(port: any, ...args: listenArgsT[]): Server | void;
|
|
47
48
|
close(callback?: () => void): void;
|
package/dist/main.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
var P=Object.create;var{getPrototypeOf:l,defineProperty:Y,getOwnPropertyNames:k}=Object;var e=Object.prototype.hasOwnProperty;var p=(n,i,f)=>{f=n!=null?P(l(n)):{};let g=i||!n||!n.__esModule?Y(f,"default",{value:n,enumerable:!0}):f;for(let t of k(n))if(!e.call(g,t))Y(g,t,{get:()=>n[t],enumerable:!0});return g};var h=((n)=>typeof require!=="undefined"?require:typeof Proxy!=="undefined"?new Proxy(n,{get:(i,f)=>(typeof require!=="undefined"?require:i)[f]}):n)(function(n){if(typeof require!=="undefined")return require.apply(this,arguments);throw Error('Dynamic require of "'+n+'" is not supported')});class S{children;isEndOfWord;handler;isDynamic;pattern;path;method;constructor(){this.children={},this.isEndOfWord=!1,this.handler=[],this.isDynamic=!1,this.pattern="",this.path="",this.method=[]}}class z{root;constructor(){this.root=new S}insert(n,i){let f=this.root,g=n.split("/").filter(Boolean);if(n==="/"){f.isEndOfWord=!0,f.handler.push(i.handler),f.path=n,f.method.push(i.method);return}for(let t of g){let m=!1,o=t;if(t.startsWith(":"))m=!0,o=":";if(!f.children[o])f.children[o]=new S;f=f.children[o],f.isDynamic=m,f.pattern=t}f.isEndOfWord=!0,f.path=n,f.method.push(i.method),f.handler.push(i.handler)}search(n,i){let f=this.root,g=n.split("/").filter(Boolean),t=g.length;for(let u of g){let r=u;if(!f.children[r])if(f.children[":"])f=f.children[":"];else return null;else f=f.children[r]}let m=f.path.split("/").filter(Boolean);if(t!==m.length)return null;let o=f.method.indexOf(i);if(o!==-1)return{path:f.path,handler:f.handler[o],isDynamic:f.isDynamic,pattern:f.pattern,method:f.method[o]};return{path:f.path,handler:f.handler,isDynamic:f.isDynamic,pattern:f.pattern,method:f.method[o]}}}function w(n){switch(n.split(".").pop()?.toLowerCase()){case"js":return"application/javascript";case"css":return"text/css";case"html":return"text/html";case"json":return"application/json";case"png":return"image/png";case"jpg":case"jpeg":return"image/jpeg";case"svg":return"image/svg+xml";case"gif":return"image/gif";case"woff":return"font/woff";case"woff2":return"font/woff2";default:return"application/octet-stream"}}function G(n,i,f){let g=null,t=null,m=null,o=null,u={};return{req:n,server:i,url:f,status:200,headers:new Headers({"Cache-Control":"no-cache"}),setHeader(r,c){return this.headers.set(r,c),this},removeHeader(r){return this.headers.delete(r),this},get ip(){return this.server.requestIP(this.req)?.address??null},get query(){if(!g)try{g=Object.fromEntries(this.url.searchParams)}catch(r){throw new Error("Failed to parse query parameters")}return g},get params(){if(!t&&this.req.routePattern)try{t=a(this.req.routePattern,this.url.pathname)}catch(r){throw new Error("Failed to extract route parameters")}return t??{}},get body(){if(this.req.method==="GET")return Promise.resolve({});if(!o)o=(async()=>{let r=await nn(this.req);if(r.error)throw new Error(r.error);return Object.keys(r).length===0?null:r})();return o},set(r,c){return u[r]=c,this},get(r){return u[r]},text(r,c){if(c)this.status=c;if(!this.headers.has("Content-Type"))this.headers.set("Content-Type","text/plain; charset=utf-8");return new Response(r,{status:this.status,headers:this.headers})},send(r,c){if(c)this.status=c;let v=new Map([["string","text/plain; charset=utf-8"],["object","application/json; charset=utf-8"],["Uint8Array","application/octet-stream"],["ArrayBuffer","application/octet-stream"]]),C=r instanceof Uint8Array?"Uint8Array":r instanceof ArrayBuffer?"ArrayBuffer":typeof r;if(!this.headers.has("Content-Type"))this.headers.set("Content-Type",v.get(C)??"text/plain; charset=utf-8");let A=C==="object"&&r!==null?JSON.stringify(r):r;return new Response(A,{status:this.status,headers:this.headers})},json(r,c){if(c)this.status=c;if(!this.headers.has("Content-Type"))this.headers.set("Content-Type","application/json; charset=utf-8");return Response.json(r,{status:this.status,headers:this.headers})},file(r,c,v){if(v)this.status=v;let C=Bun.file(r);if(!this.headers.has("Content-Type"))this.headers.set("Content-Type",c??w(r));return new Response(C,{status:this.status,headers:this.headers})},async ejs(r,c={},v){if(v)this.status=v;let C;try{C=await import("ejs"),C=C.default||C}catch(A){return console.error("EJS not installed! Please run `bun add ejs`"),new Response("EJS not installed! Please run `bun add ejs`",{status:500})}try{let A=await Bun.file(r).text(),$=C.render(A,c),F=new Headers({"Content-Type":"text/html; charset=utf-8"});return new Response($,{status:this.status,headers:F})}catch(A){return console.error("EJS Rendering Error:",A),new Response("Error rendering template",{status:500})}},redirect(r,c){if(c)this.status=c;else this.status=302;return this.headers.set("Location",r),new Response(null,{status:this.status,headers:this.headers})},stream(r){let c=new Headers(this.headers),v=new ReadableStream({async start(C){await r(C),C.close()}});return new Response(v,{headers:c})},yieldStream(r){return new Response({async*[Symbol.asyncIterator](){yield*r()}},{headers:this.headers})},setCookie(r,c,v={}){let C=`${encodeURIComponent(r)}=${encodeURIComponent(c)}`;if(v.maxAge)C+=`; Max-Age=${v.maxAge}`;if(v.expires)C+=`; Expires=${v.expires.toUTCString()}`;if(v.path)C+=`; Path=${v.path}`;if(v.domain)C+=`; Domain=${v.domain}`;if(v.secure)C+="; Secure";if(v.httpOnly)C+="; HttpOnly";if(v.sameSite)C+=`; SameSite=${v.sameSite}`;return this.headers.append("Set-Cookie",C),this},get cookies(){if(!m){let r=this.req.headers.get("cookie");m=r?d(r):{}}return m}}}function d(n){return Object.fromEntries(n.split(";").map((i)=>{let[f,...g]=i.trim().split("=");return[f,decodeURIComponent(g.join("="))]}))}function a(n,i){let f={},g=n.split("/"),[t]=i.split("?"),m=t.split("/");if(g.length!==m.length)return null;for(let o=0;o<g.length;o++)if(g[o].startsWith(":"))f[g[o].slice(1)]=m[o];return f}async function nn(n){let i=n.headers.get("Content-Type");if(!i)return{};if(n.headers.get("Content-Length")==="0"||!n.body)return{};try{if(i.startsWith("application/json"))return await n.json();if(i.startsWith("application/x-www-form-urlencoded")){let g=await n.text();return Object.fromEntries(new URLSearchParams(g))}if(i.startsWith("multipart/form-data")){let g=await n.formData(),t={};for(let[m,o]of g.entries())t[m]=o;return t}return{error:"Unknown request body type"}}catch(g){return{error:"Invalid request body format"}}}async function R(n,i,f,g){let t=G(n,i,f),m=g.trie.search(f.pathname,n.method);n.routePattern=m?.path;try{if(f.pathname.startsWith("/favicon"))return t.text("");if(g.hasFilterEnabled){let r=n.routePattern??f.pathname,c=await on(g,r,t,i);if(c)return c}if(g.hasMiddleware){if(g.globalMiddlewares.length){let c=await Z(g.globalMiddlewares,t,i);if(c)return c}let r=g.middlewares.get(f.pathname)||[];if(r?.length){let c=await Z(r,t,i);if(c)return c}}if(!m?.handler||m.method!==n.method){if(g.staticPath){let r=await fn(g,f.pathname,t);if(r)return r;let c=g.trie.search("*",n.method);if(c?.handler)return await c.handler(t)}if(g.hooks.routeNotFound&&Array.isArray(g.hooks.routeNotFound)&&!m?.handler){let r=g.hooks.routeNotFound;for(let c=0;c<r.length;c++){let v=await r[c](t);if(v)return v}}if(!m||!m?.handler?.length)return D(404,`Route not found for ${f.pathname}`);if(m?.method!==n.method)return D(405,"Method not allowed")}if(g.hooks.preHandler?.length&&Array.isArray(g.hooks.preHandler)){let r=g.hooks.preHandler;for(let c=0;c<r.length;c++){let v=await r[c](t);if(v)return v}}let o=m.handler(t);return(o instanceof Promise?await o:o)??D(204,"")}catch(o){if(g.hooks.onError&&Array.isArray(g.hooks.onError)){let u=g.hooks.onError;for(let r=0;r<u.length;r++){let c=g.hooks.onError[r](o,n,f,i);if(c)return c}}return D(500,"Internal Server Error")}finally{if(g.hooks.onSend&&Array.isArray(g.hooks.onSend)){let o=g.hooks.onSend;for(let u=0;u<o.length;u++){let r=await o[u](t);if(r)return r}}}}async function Z(n,i,f){for(let g of n){let t=await g(i,f);if(t)return t}return null}async function N(n,i,f){for(let g of n){let t=await g(i,f);if(t)return t}}async function on(n,i,f,g){if(!n.filters.has(i))if(n.filterFunction.length)for(let t of n.filterFunction){let m=await t(f,g);if(m)return m}else return Response.json({error:!0,message:"Protected route, authentication required",status:401},{status:401})}async function Tn(n,i,f,g){if(!n.filters.has(i))if(n.filterFunction.length)for(let t of n.filterFunction){let m=await t(f,g);if(m)return m}else return Response.json({error:!0,message:"Protected route, authentication required",status:401},{status:401})}function D(n,i){return new Response(JSON.stringify({error:!0,message:i,status:n}),{status:n,headers:{"Content-Type":"application/json"}})}async function fn(n,i,f){if(!n.staticPath)return null;let g=`${n.staticPath}${i}`;if(await Bun.file(g).exists()){let m=w(g);return f.file(g,m,200)}return null}var{create:rn,defineProperty:W,getOwnPropertyDescriptor:gn,getOwnPropertyNames:un,getPrototypeOf:tn}=Object,cn=Object.prototype.hasOwnProperty,mn=(n,i)=>()=>(i||n((i={exports:{}}).exports,i),i.exports),Cn=(n,i,f,g)=>{if(i&&typeof i=="object"||typeof i=="function")for(let t of un(i))!cn.call(n,t)&&t!==f&&W(n,t,{get:()=>i[t],enumerable:!(g=gn(i,t))||g.enumerable});return n},vn=(n,i,f)=>(f=n!=null?rn(tn(n)):{},Cn(i||!n||!n.__esModule?W(f,"default",{value:n,enumerable:!0}):f,n)),$n=mn((n,i)=>{function f(o){if(typeof o!="string")throw new TypeError("Path must be a string. Received "+JSON.stringify(o))}function g(o,u){for(var r="",c=0,v=-1,C=0,A,$=0;$<=o.length;++$){if($<o.length)A=o.charCodeAt($);else{if(A===47)break;A=47}if(A===47){if(!(v===$-1||C===1))if(v!==$-1&&C===2){if(r.length<2||c!==2||r.charCodeAt(r.length-1)!==46||r.charCodeAt(r.length-2)!==46){if(r.length>2){var F=r.lastIndexOf("/");if(F!==r.length-1){F===-1?(r="",c=0):(r=r.slice(0,F),c=r.length-1-r.lastIndexOf("/")),v=$,C=0;continue}}else if(r.length===2||r.length===1){r="",c=0,v=$,C=0;continue}}u&&(r.length>0?r+="/..":r="..",c=2)}else r.length>0?r+="/"+o.slice(v+1,$):r=o.slice(v+1,$),c=$-v-1;v=$,C=0}else A===46&&C!==-1?++C:C=-1}return r}function t(o,u){var r=u.dir||u.root,c=u.base||(u.name||"")+(u.ext||"");return r?r===u.root?r+c:r+o+c:c}var m={resolve:function(){for(var o="",u=!1,r,c=arguments.length-1;c>=-1&&!u;c--){var v;c>=0?v=arguments[c]:(r===void 0&&(r=process.cwd()),v=r),f(v),v.length!==0&&(o=v+"/"+o,u=v.charCodeAt(0)===47)}return o=g(o,!u),u?o.length>0?"/"+o:"/":o.length>0?o:"."},normalize:function(o){if(f(o),o.length===0)return".";var u=o.charCodeAt(0)===47,r=o.charCodeAt(o.length-1)===47;return o=g(o,!u),o.length===0&&!u&&(o="."),o.length>0&&r&&(o+="/"),u?"/"+o:o},isAbsolute:function(o){return f(o),o.length>0&&o.charCodeAt(0)===47},join:function(){if(arguments.length===0)return".";for(var o,u=0;u<arguments.length;++u){var r=arguments[u];f(r),r.length>0&&(o===void 0?o=r:o+="/"+r)}return o===void 0?".":m.normalize(o)},relative:function(o,u){if(f(o),f(u),o===u||(o=m.resolve(o),u=m.resolve(u),o===u))return"";for(var r=1;r<o.length&&o.charCodeAt(r)===47;++r);for(var c=o.length,v=c-r,C=1;C<u.length&&u.charCodeAt(C)===47;++C);for(var A=u.length,$=A-C,F=v<$?v:$,y=-1,L=0;L<=F;++L){if(L===F){if($>F){if(u.charCodeAt(C+L)===47)return u.slice(C+L+1);if(L===0)return u.slice(C+L)}else v>F&&(o.charCodeAt(r+L)===47?y=L:L===0&&(y=0));break}var X=o.charCodeAt(r+L),q=u.charCodeAt(C+L);if(X!==q)break;X===47&&(y=L)}var x="";for(L=r+y+1;L<=c;++L)(L===c||o.charCodeAt(L)===47)&&(x.length===0?x+="..":x+="/..");return x.length>0?x+u.slice(C+y):(C+=y,u.charCodeAt(C)===47&&++C,u.slice(C))},_makeLong:function(o){return o},dirname:function(o){if(f(o),o.length===0)return".";for(var u=o.charCodeAt(0),r=u===47,c=-1,v=!0,C=o.length-1;C>=1;--C)if(u=o.charCodeAt(C),u===47){if(!v){c=C;break}}else v=!1;return c===-1?r?"/":".":r&&c===1?"//":o.slice(0,c)},basename:function(o,u){if(u!==void 0&&typeof u!="string")throw new TypeError('"ext" argument must be a string');f(o);var r=0,c=-1,v=!0,C;if(u!==void 0&&u.length>0&&u.length<=o.length){if(u.length===o.length&&u===o)return"";var A=u.length-1,$=-1;for(C=o.length-1;C>=0;--C){var F=o.charCodeAt(C);if(F===47){if(!v){r=C+1;break}}else $===-1&&(v=!1,$=C+1),A>=0&&(F===u.charCodeAt(A)?--A===-1&&(c=C):(A=-1,c=$))}return r===c?c=$:c===-1&&(c=o.length),o.slice(r,c)}else{for(C=o.length-1;C>=0;--C)if(o.charCodeAt(C)===47){if(!v){r=C+1;break}}else c===-1&&(v=!1,c=C+1);return c===-1?"":o.slice(r,c)}},extname:function(o){f(o);for(var u=-1,r=0,c=-1,v=!0,C=0,A=o.length-1;A>=0;--A){var $=o.charCodeAt(A);if($===47){if(!v){r=A+1;break}continue}c===-1&&(v=!1,c=A+1),$===46?u===-1?u=A:C!==1&&(C=1):u!==-1&&(C=-1)}return u===-1||c===-1||C===0||C===1&&u===c-1&&u===r+1?"":o.slice(u,c)},format:function(o){if(o===null||typeof o!="object")throw new TypeError('The "pathObject" argument must be of type Object. Received type '+typeof o);return t("/",o)},parse:function(o){f(o);var u={root:"",dir:"",base:"",ext:"",name:""};if(o.length===0)return u;var r=o.charCodeAt(0),c=r===47,v;c?(u.root="/",v=1):v=0;for(var C=-1,A=0,$=-1,F=!0,y=o.length-1,L=0;y>=v;--y){if(r=o.charCodeAt(y),r===47){if(!F){A=y+1;break}continue}$===-1&&(F=!1,$=y+1),r===46?C===-1?C=y:L!==1&&(L=1):C!==-1&&(L=-1)}return C===-1||$===-1||L===0||L===1&&C===$-1&&C===A+1?$!==-1&&(A===0&&c?u.base=u.name=o.slice(1,$):u.base=u.name=o.slice(A,$)):(A===0&&c?(u.name=o.slice(1,C),u.base=o.slice(1,$)):(u.name=o.slice(A,C),u.base=o.slice(A,$)),u.ext=o.slice(C,$)),A>0?u.dir=o.slice(0,A-1):c&&(u.dir="/"),u},sep:"/",delimiter:":",win32:null,posix:null};m.posix=m,i.exports=m}),B=vn($n()),E=B,An=B,U=function(n){return n},J=function(){throw new Error("Not implemented")};E.parse??=J;An.parse??=J;var s={resolve:E.resolve.bind(E),normalize:E.normalize.bind(E),isAbsolute:E.isAbsolute.bind(E),join:E.join.bind(E),relative:E.relative.bind(E),toNamespacedPath:U,dirname:E.dirname.bind(E),basename:E.basename.bind(E),extname:E.extname.bind(E),format:E.format.bind(E),parse:E.parse.bind(E),sep:"/",delimiter:":",win32:void 0,posix:void 0,_makeLong:U},V={sep:"\\",delimiter:";",win32:void 0,...s,posix:s};s.win32=V.win32=V;s.posix=s;var T=s;var{default:K}=(()=>({}));var b={reset:"\x1B[0m",info:"\x1B[34m",warn:"\x1B[33m",error:"\x1B[31m",method:{GET:"\x1B[32m",POST:"\x1B[36m",PUT:"\x1B[35m",DELETE:"\x1B[31m"}},I=(n,i,f)=>{let g=b[n]||b.reset,t=f?.method?b.method[f.method]||b.reset:b.reset,m=f?.status?f.status>=500?b.error:f.status>=400?b.warn:b.info:b.reset;console.log(`
|
|
2
|
-
${
|
|
3
|
-
`)},Q=(n)=>{n.addHooks("onRequest",async(i,f)=>{i.startTime=Date.now(),I("info","Incoming Request",{method:i.method,url:f.toString(),headers:{"user-agent":i.headers.get("user-agent"),"content-type":i.headers.get("Content-Type")}})}),n.addHooks("onSend",(i)=>{let f=`${Date.now()-i.req.startTime}ms`;I("info","Response Sent",{method:i.req.method,url:i.url,status:i.status,duration:f,headers:{"content-type":i.headers.get("Content-Type")}})})},O=(n,i,f,g=0,t)=>{let m=b.method[i]||b.reset,o=g>=500?b.error:g>=400?b.warn:b.info,u=n==="<--"?`${n} ${m}${i}${b.reset} ${f}`:`${n} ${m}${i}${b.reset} ${f} ${o}${g}${b.reset} ${t||""}`;console.log(u)},Ln=(n)=>{let i=Date.now()-n;return i<1000?`${i}ms`:`${Math.round(i/1000)}s`},_=(n)=>{n.addHooks("onRequest",(i,f)=>{i.startTime=Date.now(),O("<--",i.method,new URL(f).pathname)}),n.addHooks("onSend",async(i)=>{let{method:f,url:g}=i.req,t=new URL(g).pathname;O("-->",f,t,i.status,Ln(i.req.startTime))}),n.addHooks("routeNotFound",(i)=>{O("[routeNotFound]",i.req.method,i.url.pathname,404)}),n.addHooks("onError",(i,f,g)=>{O(i?.message,f.method,g.toString(),500)})};function j(n,i){if(!n)throw new Error("JWT library is not defined, please provide jwt to authenticateJwt Function");return(f)=>{try{let g=f.cookies?.accessToken||f.req?.headers?.get("Authorization");if(!g)return f.json({message:"Unauthorized: No token provided"},401);if(g.startsWith("Bearer "))g=g.slice(7);let t=n?.verify(g,i);if(!t)return f.json({message:"Unauthorized: Invalid token"},401);f.set("user",t)}catch(g){return console.error("JWT verification error:",g),f.json({message:"Unauthorized: Invalid token",error:g?.message},401)}}}function H(n,i,f){if(!n)throw new Error("JWT library is not defined, please provide jwt to authenticateJwtDB Function");if(!i)throw new Error("User model is not defined, please provide UserModel to authenticateJwtDB Function");return async(g)=>{try{let t=g.cookies?.accessToken||g.req?.headers?.get("Authorization");if(!t)return g.json({message:"Unauthorized: No token provided"},401);if(t.startsWith("Bearer "))t=t.slice(7);let m=n?.verify(t,f);if(!m)return g.json({message:"Unauthorized: Invalid token"},401);let o=await i.findById(m._id).select("-password -refreshToken");if(!o)return g.json({message:"Unauthorized: User not found"},401);g.set("user",o);return}catch(t){return g.json({message:"Unauthorized: Authentication failed",error:t?.message},401)}}}class M{routes;tempRoutes;globalMiddlewares;middlewares;trie;hasOnReqHook;hasMiddleware;hasPreHandlerHook;hasPostHandlerHook;hasOnSendHook;hasOnError;hooks;corsConfig;FilterRoutes;filters;filterFunction;hasFilterEnabled;serverInstance;staticPath;staticFiles;user_jwt_secret;baseApiUrl;enableFileRouter;idleTimeOut;constructor({jwtSecret:n,baseApiUrl:i,enableFileRouting:f,idleTimeOut:g}={}){this.routes={},this.idleTimeOut=g??10,this.enableFileRouter=f??!1,this.baseApiUrl=i||"",this.user_jwt_secret=n||process.env.DIESEL_JWT_SECRET||"feault_diesel_secret_for_jwt",this.tempRoutes=new Map,this.globalMiddlewares=[],this.middlewares=new Map,this.trie=new z,this.corsConfig=null,this.hasMiddleware=!1,this.hasOnReqHook=!1,this.hasPreHandlerHook=!1,this.hasPostHandlerHook=!1,this.hasOnSendHook=!1,this.hasOnError=!1,this.hooks={onRequest:[],preHandler:[],postHandler:[],onSend:[],onError:[],onClose:[],routeNotFound:[]},this.FilterRoutes=[],this.filters=new Set,this.filterFunction=[],this.hasFilterEnabled=!1,this.serverInstance=null,this.staticPath=null,this.staticFiles={}}setupFilter(){return this.hasFilterEnabled=!0,{routeMatcher:(...n)=>{return this.FilterRoutes=n,this.setupFilter()},permitAll:()=>{for(let n of this?.FilterRoutes)this.filters.add(n);return this.FilterRoutes=null,this.setupFilter()},authenticate:(n)=>{if(n?.length)for(let i of n)this.filterFunction.push(i)},authenticateJwt:(n)=>{this.filterFunction.push(j(n,this.user_jwt_secret))},authenticateJwtDB:(n,i)=>{this.filterFunction.push(H(n,i,this.user_jwt_secret))}}}redirect(n,i,f){return this.any(n,(g)=>{let t=g.params,m=i;if(t)for(let u in t)m=m.replace(`:${u}`,t[u]);let o=g.url.search;if(o)m+=o;return g.redirect(m,f)}),this}serveStatic(n){return this.staticPath=n,this}static(n){return this.staticFiles={...this.staticFiles,...n},this}addHooks(n,i){if(typeof n!=="string")throw new Error("hookName must be a string");if(typeof i!=="function")throw new Error("callback must be a instance of function");switch(n){case"onRequest":this.hooks.onRequest?.push(i);break;case"preHandler":this.hooks.preHandler?.push(i);break;case"postHandler":this.hooks.postHandler?.push(i);break;case"onSend":this.hooks.onSend?.push(i);break;case"onError":this.hooks.onError?.push(i);break;case"onClose":this.hooks.onClose?.push(i);break;case"routeNotFound":this.hooks.routeNotFound?.push(i);break;default:throw new Error(`Unknown hook type: ${n}`)}return this}compile(){if(this.globalMiddlewares.length>0)this.hasMiddleware=!0;for(let[n,i]of this.middlewares.entries())if(i.length>0){this.hasMiddleware=!0;break}if(this.enableFileRouter){let n=process.cwd(),i=T.join(n,"src","routes");if(K?.existsSync(i))this.loadRoutes(i,"")}setTimeout(()=>{this.tempRoutes=null},2000)}async registerFileRoutes(n,i,f){let g=await import(n),t;if(f===".ts")t=T.basename(n,".ts");else if(f===".js")t=T.basename(n,".js");let m=i+"/"+t;if(m.endsWith("/index"))m=i;else if(m.endsWith("/api"))m=i;m=m.replace(/\[(.*?)\]/g,":$1");let o=["GET","POST","PUT","PATCH","DELETE","ANY","HEAD","OPTIONS","PROPFIND"];for(let u of o)if(g[u]){let r=u.toLowerCase(),c=g[u];this[r](`${this.baseApiUrl}${m}`,c)}}async loadRoutes(n,i){let f=await K.promises.readdir(n);for(let g of f){let t=T.join(n,g);if((await K.promises.stat(t)).isDirectory())this.loadRoutes(t,i+"/"+g);else if(g.endsWith(".ts"))this.registerFileRoutes(t,i,".ts");else if(g.endsWith(".js"))this.registerFileRoutes(t,i,".js")}}useLogger(n){return _(n),this}useAdvancedLogger(n){return Q(n),this}BunRoute(n,i,...f){if(!i||typeof i!=="string")throw new Error("give a path in string format");if(f.length===1){let g=f[0];this.routes[i]=async(t,m)=>{if(this.hasMiddleware){if(this.globalMiddlewares.length){let r=await N(this.globalMiddlewares,t,m);if(r)return r}let u=this.middlewares.get(i)||[];if(u?.length){let r=await N(u,t,m);if(r)return r}}if(n!==t.method)return new Response("Method Not Allowed",{status:405});let o=await g(t,m);if(o instanceof Promise)return await o??new Response("Not Found",{status:404});return o??new Response("Not Found",{status:404})}}else this.routes[i]=async(g,t)=>{if(this.hasMiddleware){if(this.globalMiddlewares.length){let o=await N(this.globalMiddlewares,g,t);if(o)return o}let m=this.middlewares.get(i)||[];if(m?.length){let o=await N(m,g,t);if(o)return o}}if(n!==g.method)return new Response("Method Not Allowed",{status:405});for(let m=0;m<f.length;m++){let o=f[m](g,t);if(o instanceof Promise)return await o??new Response("Not Found",{status:404});return o??new Response("Not Found",{status:404})}}}listen(n,...i){if(typeof Bun==="undefined")throw new Error(".listen() is designed to run on Bun only...");let f="0.0.0.0",g=void 0,t={};for(let o of i)if(typeof o==="string")f=o;else if(typeof o==="function")g=o;else if(typeof o==="object"&&o!==null)t=o;this.compile();let m={port:n,hostname:f,idleTimeOut:this.idleTimeOut,fetch:async(o,u)=>{let r=new URL(o.url);if(this.hooks.onRequest){let c=this.hooks.onRequest;for(let v=0;v<c.length;v++)await c[v](o,r,u)}return R(o,u,r,this)},static:this.staticFiles,routes:this.routes};if(t.sslCert&&t.sslKey)m.certFile=t.sslCert,m.keyFile=t.sslKey;if(this.serverInstance=Bun?.serve(m),t.sslCert&&t.sslKey)console.log(`HTTPS server is running on https://localhost:${n}`);if(g)return g();return this.serverInstance}close(n){if(this.serverInstance)this.serverInstance.stop(!0),this.serverInstance=null,n?n():console.log("Server has been stopped");else console.warn("Server is not running.")}route(n,i){if(!n||typeof n!=="string")throw new Error("Path must be a string");let f=Object.fromEntries(i.tempRoutes);return Object.entries(f).forEach(([t,m])=>{let o=t.replace(/::\w+$/,""),u=`${n}${o}`;if(!this.middlewares.has(u))this.middlewares.set(u,[]);m.handlers.slice(0,-1).forEach((C)=>{if(!this.middlewares.get(u)?.includes(C))this.middlewares.get(u)?.push(C)});let c=m.handlers[m.handlers.length-1],v=m.method;try{this.trie.insert(u,{handler:c,method:v})}catch(C){console.error(`Error inserting ${u}:`,C)}}),i=null,this}register(n,i){return this.route(n,i)}addRoute(n,i,f){if(typeof i!=="string")throw new Error(`Error in ${f[f.length-1]}: Path must be a string. Received: ${typeof i}`);if(typeof n!=="string")throw new Error(`Error in addRoute: Method must be a string. Received: ${typeof n}`);this.tempRoutes?.set(i+"::"+n,{method:n,handlers:f});let g=f.slice(0,-1),t=f[f.length-1];if(!this.middlewares.has(i))this.middlewares.set(i,[]);g.forEach((m)=>{if(i==="/")this.globalMiddlewares=[...new Set([...this.globalMiddlewares,...g])];else if(!this.middlewares.get(i)?.includes(m))this.middlewares.get(i)?.push(m)});try{if(n==="ANY"){let m=["GET","POST","PUT","DELETE","PATCH","OPTIONS","HEAD","PROPFIND"];for(let o of m)this.trie.insert(i,{handler:t,method:o})}this.trie.insert(i,{handler:t,method:n})}catch(m){console.error(`Error inserting ${i}:`,m)}}use(n,i){if(Array.isArray(n))n.forEach((g)=>{if(typeof g==="function")this.globalMiddlewares.push(g)});if(typeof n==="function"){if(this.globalMiddlewares.push(n),Array.isArray(i))i.forEach((g)=>{this.globalMiddlewares.push(g)});return}return(Array.isArray(n)?n.filter((g)=>typeof g==="string"):[n].filter((g)=>typeof g==="string")).forEach((g)=>{if(!this.middlewares.has(g))this.middlewares.set(g,[]);if(i)(Array.isArray(i)?i:[i]).forEach((m)=>{this.middlewares.get(g)?.push(m)})}),this}get(n,...i){return this.addRoute("GET",n,i),this}post(n,...i){return this.addRoute("POST",n,i),this}put(n,...i){return this.addRoute("PUT",n,i),this}patch(n,...i){return this.addRoute("PATCH",n,i),this}delete(n,...i){return this.addRoute("DELETE",n,i),this}any(n,...i){return this.addRoute("ANY",n,i),this}head(n,...i){return this.addRoute("HEAD",n,i),this}options(n,...i){return this.addRoute("OPTIONS",n,i),this}propfind(n,...i){return this.addRoute("PROPFIND",n,i),this}}export{M as default};
|
|
1
|
+
var l=Object.create;var{getPrototypeOf:q,defineProperty:X,getOwnPropertyNames:P}=Object;var k=Object.prototype.hasOwnProperty;var p=(n,i,t)=>{t=n!=null?l(q(n)):{};let f=i||!n||!n.__esModule?X(t,"default",{value:n,enumerable:!0}):t;for(let s of P(n))if(!k.call(f,s))X(f,s,{get:()=>n[s],enumerable:!0});return f};var h=((n)=>typeof require!=="undefined"?require:typeof Proxy!=="undefined"?new Proxy(n,{get:(i,t)=>(typeof require!=="undefined"?require:i)[t]}):n)(function(n){if(typeof require!=="undefined")return require.apply(this,arguments);throw Error('Dynamic require of "'+n+'" is not supported')});class z{children;isEndOfWord;handler;isDynamic;pattern;path;method;constructor(){this.children={},this.isEndOfWord=!1,this.handler=[],this.isDynamic=!1,this.pattern="",this.path="",this.method=[]}}class N{root;constructor(){this.root=new z}insert(n,i){let t=this.root,f=n.split("/").filter(Boolean);if(n==="/"){t.isEndOfWord=!0,t.handler.push(i.handler),t.path=n,t.method.push(i.method);return}for(let s of f){let c=!1,o=s;if(s.startsWith(":"))c=!0,o=":";if(!t.children[o])t.children[o]=new z;t=t.children[o],t.isDynamic=c,t.pattern=s}t.isEndOfWord=!0,t.path=n,t.method.push(i.method),t.handler.push(i.handler)}search(n,i){let t=this.root,f=n.split("/").filter(Boolean),s=f.length;for(let u of f){let r=u;if(!t.children[r])if(t.children[":"])t=t.children[":"];else return null;else t=t.children[r]}let c=t.path.split("/").filter(Boolean);if(s!==c.length)return null;let o=t.method.indexOf(i);if(o!==-1)return{path:t.path,handler:t.handler[o],isDynamic:t.isDynamic,pattern:t.pattern,method:t.method[o]};return{path:t.path,handler:t.handler,isDynamic:t.isDynamic,pattern:t.pattern,method:t.method[o]}}}function T(n){switch(n.split(".").pop()?.toLowerCase()){case"js":return"application/javascript";case"css":return"text/css";case"html":return"text/html";case"json":return"application/json";case"png":return"image/png";case"jpg":case"jpeg":return"image/jpeg";case"svg":return"image/svg+xml";case"gif":return"image/gif";case"woff":return"font/woff";case"woff2":return"font/woff2";default:return"application/octet-stream"}}function D(n,i,t){let f=null,s=null,c=null,o=null,u={};return{req:n,server:i,url:t,status:200,headers:new Headers({"Cache-Control":"no-cache"}),setHeader(r,g){return this.headers.set(r,g),this},removeHeader(r){return this.headers.delete(r),this},get ip(){return this.server.requestIP(this.req)?.address??null},get query(){if(!f)try{f=Object.fromEntries(this.url.searchParams)}catch(r){throw new Error("Failed to parse query parameters")}return f},get params(){if(!s&&this.req.routePattern)try{s=a(this.req.routePattern,this.url.pathname)}catch(r){throw new Error("Failed to extract route parameters")}return s??{}},get body(){if(this.req.method==="GET")return Promise.resolve({});if(!o)o=(async()=>{let r=await nn(this.req);if(r.error)throw new Error(r.error);return Object.keys(r).length===0?null:r})();return o},set(r,g){return u[r]=g,this},get(r){return u[r]},text(r,g){if(g)this.status=g;if(!this.headers.has("Content-Type"))this.headers.set("Content-Type","text/plain; charset=utf-8");return new Response(r,{status:this.status,headers:this.headers})},send(r,g){if(g)this.status=g;let e=new Map([["string","text/plain; charset=utf-8"],["object","application/json; charset=utf-8"],["Uint8Array","application/octet-stream"],["ArrayBuffer","application/octet-stream"]]),m=r instanceof Uint8Array?"Uint8Array":r instanceof ArrayBuffer?"ArrayBuffer":typeof r;if(!this.headers.has("Content-Type"))this.headers.set("Content-Type",e.get(m)??"text/plain; charset=utf-8");let C=m==="object"&&r!==null?JSON.stringify(r):r;return new Response(C,{status:this.status,headers:this.headers})},json(r,g){if(g)this.status=g;if(!this.headers.has("Content-Type"))this.headers.set("Content-Type","application/json; charset=utf-8");return Response.json(r,{status:this.status,headers:this.headers})},file(r,g,e){if(e)this.status=e;let m=Bun.file(r);if(!this.headers.has("Content-Type"))this.headers.set("Content-Type",g??T(r));return new Response(m,{status:this.status,headers:this.headers})},async ejs(r,g={},e){if(e)this.status=e;let m;try{m=await import("ejs"),m=m.default||m}catch(C){return console.error("EJS not installed! Please run `bun add ejs`"),new Response("EJS not installed! Please run `bun add ejs`",{status:500})}try{let C=await Bun.file(r).text(),v=m.render(C,g),$=new Headers({"Content-Type":"text/html; charset=utf-8"});return new Response(v,{status:this.status,headers:$})}catch(C){return console.error("EJS Rendering Error:",C),new Response("Error rendering template",{status:500})}},redirect(r,g){if(g)this.status=g;else this.status=302;return this.headers.set("Location",r),new Response(null,{status:this.status,headers:this.headers})},stream(r){let g=new Headers(this.headers),e=new ReadableStream({async start(m){await r(m),m.close()}});return new Response(e,{headers:g})},yieldStream(r){return new Response({async*[Symbol.asyncIterator](){yield*r()}},{headers:this.headers})},setCookie(r,g,e={}){let m=`${encodeURIComponent(r)}=${encodeURIComponent(g)}`;if(e.maxAge)m+=`; Max-Age=${e.maxAge}`;if(e.expires)m+=`; Expires=${e.expires.toUTCString()}`;if(e.path)m+=`; Path=${e.path}`;if(e.domain)m+=`; Domain=${e.domain}`;if(e.secure)m+="; Secure";if(e.httpOnly)m+="; HttpOnly";if(e.sameSite)m+=`; SameSite=${e.sameSite}`;return this.headers.append("Set-Cookie",m),this},get cookies(){if(!c){let r=this.req.headers.get("cookie");c=r?d(r):{}}return c}}}function d(n){return Object.fromEntries(n.split(";").map((i)=>{let[t,...f]=i.trim().split("=");return[t,decodeURIComponent(f.join("="))]}))}function a(n,i){let t={},f=n.split("/"),[s]=i.split("?"),c=s.split("/");if(f.length!==c.length)return null;for(let o=0;o<f.length;o++)if(f[o].startsWith(":"))t[f[o].slice(1)]=c[o];return t}async function nn(n){let i=n.headers.get("Content-Type");if(!i)return{};if(n.headers.get("Content-Length")==="0"||!n.body)return{};try{if(i.startsWith("application/json"))return await n.json();if(i.startsWith("application/x-www-form-urlencoded")){let f=await n.text();return Object.fromEntries(new URLSearchParams(f))}if(i.startsWith("multipart/form-data")){let f=await n.formData(),s={};for(let[c,o]of f.entries())s[c]=o;return s}return{error:"Unknown request body type"}}catch(f){return{error:"Invalid request body format"}}}async function U(n,i,t,f){let s=D(n,i,t),c=f.trie.search(t.pathname,n.method);n.routePattern=c?.path;try{if(t.pathname.startsWith("/favicon"))return s.text("");if(f.hasFilterEnabled){let r=n.routePattern??t.pathname,g=await on(f,r,s,i);if(g)return g}if(f.hasMiddleware){if(f.globalMiddlewares.length){let g=await Y(f.globalMiddlewares,s,i);if(g)return g}let r=f.middlewares.get(t.pathname)||[];if(r?.length){let g=await Y(r,s,i);if(g)return g}}if(!c?.handler||c.method!==n.method){if(f.staticPath){let r=await rn(f,t.pathname,s);if(r)return r;let g=f.trie.search("*",n.method);if(g?.handler)return await g.handler(s)}if(f.hooks.routeNotFound&&Array.isArray(f.hooks.routeNotFound)&&!c?.handler){let r=f.hooks.routeNotFound;for(let g=0;g<r.length;g++){let e=await r[g](s);if(e)return e}}if(!c||!c?.handler?.length)return w(404,`Route not found for ${t.pathname}`);if(c?.method!==n.method)return w(405,"Method not allowed")}if(f.hooks.preHandler?.length&&Array.isArray(f.hooks.preHandler)){let r=f.hooks.preHandler;for(let g=0;g<r.length;g++){let e=await r[g](s);if(e)return e}}let o=c.handler(s);return(o instanceof Promise?await o:o)??w(204,"")}catch(o){if(f.hooks.onError&&Array.isArray(f.hooks.onError)){let u=f.hooks.onError;for(let r=0;r<u.length;r++){let g=f.hooks.onError[r](o,n,t,i);if(g)return g}}return w(500,"Internal Server Error")}finally{if(f.hooks.onSend&&Array.isArray(f.hooks.onSend)){let o=f.hooks.onSend;for(let u=0;u<o.length;u++){let r=await o[u](s);if(r)return r}}}}async function Y(n,i,t){for(let f of n){let s=await f(i,t);if(s)return s}return null}async function b(n,i,t){for(let f of n){let s=await f(i,t);if(s)return s}}async function on(n,i,t,f){if(!n.filters.has(i))if(n.filterFunction.length)for(let s of n.filterFunction){let c=await s(t,f);if(c)return c}else return Response.json({error:!0,message:"Protected route, authentication required",status:401},{status:401})}async function xn(n,i,t,f){if(!n.filters.has(i))if(n.filterFunction.length)for(let s of n.filterFunction){let c=await s(t,f);if(c)return c}else return Response.json({error:!0,message:"Protected route, authentication required",status:401},{status:401})}function w(n,i){return new Response(JSON.stringify({error:!0,message:i,status:n}),{status:n,headers:{"Content-Type":"application/json"}})}async function rn(n,i,t){if(!n.staticPath)return null;let f=`${n.staticPath}${i}`;if(await Bun.file(f).exists()){let c=T(f);return t.file(f,c,200)}return null}var{create:tn,defineProperty:W,getOwnPropertyDescriptor:fn,getOwnPropertyNames:un,getPrototypeOf:gn}=Object,sn=Object.prototype.hasOwnProperty,cn=(n,i)=>()=>(i||n((i={exports:{}}).exports,i),i.exports),mn=(n,i,t,f)=>{if(i&&typeof i=="object"||typeof i=="function")for(let s of un(i))!sn.call(n,s)&&s!==t&&W(n,s,{get:()=>i[s],enumerable:!(f=fn(i,s))||f.enumerable});return n},en=(n,i,t)=>(t=n!=null?tn(gn(n)):{},mn(i||!n||!n.__esModule?W(t,"default",{value:n,enumerable:!0}):t,n)),vn=cn((n,i)=>{function t(o){if(typeof o!="string")throw new TypeError("Path must be a string. Received "+JSON.stringify(o))}function f(o,u){for(var r="",g=0,e=-1,m=0,C,v=0;v<=o.length;++v){if(v<o.length)C=o.charCodeAt(v);else{if(C===47)break;C=47}if(C===47){if(!(e===v-1||m===1))if(e!==v-1&&m===2){if(r.length<2||g!==2||r.charCodeAt(r.length-1)!==46||r.charCodeAt(r.length-2)!==46){if(r.length>2){var $=r.lastIndexOf("/");if($!==r.length-1){$===-1?(r="",g=0):(r=r.slice(0,$),g=r.length-1-r.lastIndexOf("/")),e=v,m=0;continue}}else if(r.length===2||r.length===1){r="",g=0,e=v,m=0;continue}}u&&(r.length>0?r+="/..":r="..",g=2)}else r.length>0?r+="/"+o.slice(e+1,v):r=o.slice(e+1,v),g=v-e-1;e=v,m=0}else C===46&&m!==-1?++m:m=-1}return r}function s(o,u){var r=u.dir||u.root,g=u.base||(u.name||"")+(u.ext||"");return r?r===u.root?r+g:r+o+g:g}var c={resolve:function(){for(var o="",u=!1,r,g=arguments.length-1;g>=-1&&!u;g--){var e;g>=0?e=arguments[g]:(r===void 0&&(r=process.cwd()),e=r),t(e),e.length!==0&&(o=e+"/"+o,u=e.charCodeAt(0)===47)}return o=f(o,!u),u?o.length>0?"/"+o:"/":o.length>0?o:"."},normalize:function(o){if(t(o),o.length===0)return".";var u=o.charCodeAt(0)===47,r=o.charCodeAt(o.length-1)===47;return o=f(o,!u),o.length===0&&!u&&(o="."),o.length>0&&r&&(o+="/"),u?"/"+o:o},isAbsolute:function(o){return t(o),o.length>0&&o.charCodeAt(0)===47},join:function(){if(arguments.length===0)return".";for(var o,u=0;u<arguments.length;++u){var r=arguments[u];t(r),r.length>0&&(o===void 0?o=r:o+="/"+r)}return o===void 0?".":c.normalize(o)},relative:function(o,u){if(t(o),t(u),o===u||(o=c.resolve(o),u=c.resolve(u),o===u))return"";for(var r=1;r<o.length&&o.charCodeAt(r)===47;++r);for(var g=o.length,e=g-r,m=1;m<u.length&&u.charCodeAt(m)===47;++m);for(var C=u.length,v=C-m,$=e<v?e:v,E=-1,A=0;A<=$;++A){if(A===$){if(v>$){if(u.charCodeAt(m+A)===47)return u.slice(m+A+1);if(A===0)return u.slice(m+A)}else e>$&&(o.charCodeAt(r+A)===47?E=A:A===0&&(E=0));break}var K=o.charCodeAt(r+A),M=u.charCodeAt(m+A);if(K!==M)break;K===47&&(E=A)}var F="";for(A=r+E+1;A<=g;++A)(A===g||o.charCodeAt(A)===47)&&(F.length===0?F+="..":F+="/..");return F.length>0?F+u.slice(m+E):(m+=E,u.charCodeAt(m)===47&&++m,u.slice(m))},_makeLong:function(o){return o},dirname:function(o){if(t(o),o.length===0)return".";for(var u=o.charCodeAt(0),r=u===47,g=-1,e=!0,m=o.length-1;m>=1;--m)if(u=o.charCodeAt(m),u===47){if(!e){g=m;break}}else e=!1;return g===-1?r?"/":".":r&&g===1?"//":o.slice(0,g)},basename:function(o,u){if(u!==void 0&&typeof u!="string")throw new TypeError('"ext" argument must be a string');t(o);var r=0,g=-1,e=!0,m;if(u!==void 0&&u.length>0&&u.length<=o.length){if(u.length===o.length&&u===o)return"";var C=u.length-1,v=-1;for(m=o.length-1;m>=0;--m){var $=o.charCodeAt(m);if($===47){if(!e){r=m+1;break}}else v===-1&&(e=!1,v=m+1),C>=0&&($===u.charCodeAt(C)?--C===-1&&(g=m):(C=-1,g=v))}return r===g?g=v:g===-1&&(g=o.length),o.slice(r,g)}else{for(m=o.length-1;m>=0;--m)if(o.charCodeAt(m)===47){if(!e){r=m+1;break}}else g===-1&&(e=!1,g=m+1);return g===-1?"":o.slice(r,g)}},extname:function(o){t(o);for(var u=-1,r=0,g=-1,e=!0,m=0,C=o.length-1;C>=0;--C){var v=o.charCodeAt(C);if(v===47){if(!e){r=C+1;break}continue}g===-1&&(e=!1,g=C+1),v===46?u===-1?u=C:m!==1&&(m=1):u!==-1&&(m=-1)}return u===-1||g===-1||m===0||m===1&&u===g-1&&u===r+1?"":o.slice(u,g)},format:function(o){if(o===null||typeof o!="object")throw new TypeError('The "pathObject" argument must be of type Object. Received type '+typeof o);return s("/",o)},parse:function(o){t(o);var u={root:"",dir:"",base:"",ext:"",name:""};if(o.length===0)return u;var r=o.charCodeAt(0),g=r===47,e;g?(u.root="/",e=1):e=0;for(var m=-1,C=0,v=-1,$=!0,E=o.length-1,A=0;E>=e;--E){if(r=o.charCodeAt(E),r===47){if(!$){C=E+1;break}continue}v===-1&&($=!1,v=E+1),r===46?m===-1?m=E:A!==1&&(A=1):m!==-1&&(A=-1)}return m===-1||v===-1||A===0||A===1&&m===v-1&&m===C+1?v!==-1&&(C===0&&g?u.base=u.name=o.slice(1,v):u.base=u.name=o.slice(C,v)):(C===0&&g?(u.name=o.slice(1,m),u.base=o.slice(1,v)):(u.name=o.slice(C,m),u.base=o.slice(C,v)),u.ext=o.slice(m,v)),C>0?u.dir=o.slice(0,C-1):g&&(u.dir="/"),u},sep:"/",delimiter:":",win32:null,posix:null};c.posix=c,i.exports=c}),B=en(vn()),L=B,Cn=B,Z=function(n){return n},J=function(){throw new Error("Not implemented")};L.parse??=J;Cn.parse??=J;var y={resolve:L.resolve.bind(L),normalize:L.normalize.bind(L),isAbsolute:L.isAbsolute.bind(L),join:L.join.bind(L),relative:L.relative.bind(L),toNamespacedPath:Z,dirname:L.dirname.bind(L),basename:L.basename.bind(L),extname:L.extname.bind(L),format:L.format.bind(L),parse:L.parse.bind(L),sep:"/",delimiter:":",win32:void 0,posix:void 0,_makeLong:Z},V={sep:"\\",delimiter:";",win32:void 0,...y,posix:y};y.win32=V.win32=V;y.posix=y;var x=y;var{default:G}=(()=>({}));var R={reset:"\x1B[0m",info:"\x1B[36m",warn:"\x1B[33m",error:"\x1B[31m",method:{GET:"\x1B[32m",POST:"\x1B[34m",PUT:"\x1B[35m",DELETE:"\x1B[31m",PATCH:"\x1B[36m"}},S=(n,i,t)=>{let f=R[n]||R.reset,s=t?.method?R.method[t.method]||R.reset:R.reset,c=t?.status?t.status>=500?R.error:t.status>=400?R.warn:R.info:R.reset;console.log(`
|
|
2
|
+
${f}[${n.toUpperCase()}]${R.reset} ${i} - ${s}${t?.method||""}${R.reset}`);let o={timestamp:new Date().toISOString(),...t,status:t?.status?`${c}${t.status}${R.reset}`:void 0,method:t?.method?`${s}${t.method}${R.reset}`:void 0};console.log(JSON.stringify(o,null,2)+`
|
|
3
|
+
`)},I=(n)=>{let{app:i,logger:t,logLevel:f="info",onRequest:s,onSend:c,onError:o,routeNotFound:u}=n||{};i?.addHooks("onRequest",(r,g)=>{r.startTime=Date.now(),t?.()??S(f,"Incoming Request",{method:r.method,url:g.toString(),headers:{"user-agent":r.headers.get("user-agent"),"content-type":r.headers.get("content-type")}}),s?.(r,g)}),i?.addHooks("onSend",async(r)=>{let g=`${Date.now()-r.req.startTime}ms`;t?.()??S(f,"Response Sent",{method:r.req.method,url:r.url.toString(),status:r.status,duration:g,headers:{"content-type":r.headers.get("content-type")}});let e=await c?.(r);if(e instanceof Response)return e}),i?.addHooks("routeNotFound",async(r)=>{t?.()??S("warn","Route Not Found",{method:r.req.method,url:r.url.toString(),status:404});let g=await u?.(r);if(g instanceof Response)return g}),i?.addHooks("onError",async(r,g,e)=>{t?.()??S("error","Unhandled Error",{method:g.method,url:e.toString(),status:500,error:r.message});let m=await o?.(r,g,e);if(m instanceof Response)return m})},O=(n,i,t,f=0,s)=>{let c=R.method[i]||R.reset,o=f>=500?R.error:f>=400?R.warn:R.info,u=n==="<--"?`${n} ${c}${i}${R.reset} ${t}`:`${n} ${c}${i}${R.reset} ${t} ${o}${f}${R.reset} ${s||""}`;console.log(u)},An=(n)=>{let i=Date.now()-n;return i<1000?`${i}ms`:`${Math.round(i/1000)}s`},Q=(n)=>{let{app:i,log:t,onRequest:f,onSend:s,onError:c,routeNotFound:o}=n;i.addHooks("onRequest",(u,r)=>{u.startTime=Date.now(),t?.()??O("<--",u.method,r.pathname),f?.(u,r)}),i.addHooks("onSend",async(u)=>{let{method:r,url:g}=u.req,e=new URL(g).pathname;t?.()??O("-->",r,e,u.status,An(u.req.startTime));let m=await s?.(u);if(m instanceof Response)return m}),i.addHooks("routeNotFound",async(u)=>{t?.()??O("[routeNotFound]",u.req.method,u.url.pathname,404);let r=await o?.(u);if(r instanceof Response)return r}),i.addHooks("onError",async(u,r,g)=>{t?.()??O(u.message,r.method,g.toString(),500);let e=await c?.(u,r,g);if(e instanceof Response)return e})};function _(n,i){if(!n)throw new Error("JWT library is not defined, please provide jwt to authenticateJwt Function");return(t)=>{try{let f=t.cookies?.accessToken||t.req?.headers?.get("Authorization");if(!f)return t.json({message:"Unauthorized: No token provided"},401);if(f.startsWith("Bearer "))f=f.slice(7);let s=n?.verify(f,i);if(!s)return t.json({message:"Unauthorized: Invalid token"},401);t.set("user",s)}catch(f){return console.error("JWT verification error:",f),t.json({message:"Unauthorized: Invalid token",error:f?.message},401)}}}function j(n,i,t){if(!n)throw new Error("JWT library is not defined, please provide jwt to authenticateJwtDB Function");if(!i)throw new Error("User model is not defined, please provide UserModel to authenticateJwtDB Function");return async(f)=>{try{let s=f.cookies?.accessToken||f.req?.headers?.get("Authorization");if(!s)return f.json({message:"Unauthorized: No token provided"},401);if(s.startsWith("Bearer "))s=s.slice(7);let c=n?.verify(s,t);if(!c)return f.json({message:"Unauthorized: Invalid token"},401);let o=await i.findById(c._id).select("-password -refreshToken");if(!o)return f.json({message:"Unauthorized: User not found"},401);f.set("user",o);return}catch(s){return f.json({message:"Unauthorized: Authentication failed",error:s?.message},401)}}}class H{routes;tempRoutes;globalMiddlewares;middlewares;trie;hasOnReqHook;hasMiddleware;hasPreHandlerHook;hasPostHandlerHook;hasOnSendHook;hasOnError;hooks;corsConfig;FilterRoutes;filters;filterFunction;hasFilterEnabled;serverInstance;staticPath;staticFiles;user_jwt_secret;baseApiUrl;enableFileRouter;idleTimeOut;constructor({jwtSecret:n,baseApiUrl:i,enableFileRouting:t,idleTimeOut:f}={}){this.routes={},this.idleTimeOut=f??10,this.enableFileRouter=t??!1,this.baseApiUrl=i||"",this.user_jwt_secret=n||process.env.DIESEL_JWT_SECRET||"feault_diesel_secret_for_jwt",this.tempRoutes=new Map,this.globalMiddlewares=[],this.middlewares=new Map,this.trie=new N,this.corsConfig=null,this.hasMiddleware=!1,this.hasOnReqHook=!1,this.hasPreHandlerHook=!1,this.hasPostHandlerHook=!1,this.hasOnSendHook=!1,this.hasOnError=!1,this.hooks={onRequest:[],preHandler:[],postHandler:[],onSend:[],onError:[],onClose:[],routeNotFound:[]},this.FilterRoutes=[],this.filters=new Set,this.filterFunction=[],this.hasFilterEnabled=!1,this.serverInstance=null,this.staticPath=null,this.staticFiles={}}setupFilter(){return this.hasFilterEnabled=!0,{routeMatcher:(...n)=>{return this.FilterRoutes=n,this.setupFilter()},permitAll:()=>{for(let n of this?.FilterRoutes)this.filters.add(n);return this.FilterRoutes=null,this.setupFilter()},authenticate:(n)=>{if(n?.length)for(let i of n)this.filterFunction.push(i)},authenticateJwt:(n)=>{this.filterFunction.push(_(n,this.user_jwt_secret))},authenticateJwtDB:(n,i)=>{this.filterFunction.push(j(n,i,this.user_jwt_secret))}}}redirect(n,i,t){return this.any(n,(f)=>{let s=f.params,c=i;if(s)for(let u in s)c=c.replace(`:${u}`,s[u]);let o=f.url.search;if(o)c+=o;return f.redirect(c,t)}),this}serveStatic(n){return this.staticPath=n,this}staticHtml(n){return this.staticFiles={...this.staticFiles,...n},this}addHooks(n,i){if(typeof n!=="string")throw new Error("hookName must be a string");if(typeof i!=="function")throw new Error("callback must be a instance of function");switch(n){case"onRequest":this.hooks.onRequest?.push(i);break;case"preHandler":this.hooks.preHandler?.push(i);break;case"postHandler":this.hooks.postHandler?.push(i);break;case"onSend":this.hooks.onSend?.push(i);break;case"onError":this.hooks.onError?.push(i);break;case"onClose":this.hooks.onClose?.push(i);break;case"routeNotFound":this.hooks.routeNotFound?.push(i);break;default:throw new Error(`Unknown hook type: ${n}`)}return this}compile(){if(this.globalMiddlewares.length>0)this.hasMiddleware=!0;for(let[n,i]of this.middlewares.entries())if(i.length>0){this.hasMiddleware=!0;break}if(this.enableFileRouter){let n=process.cwd(),i=x.join(n,"src","routes");if(G?.existsSync(i))this.loadRoutes(i,"")}setTimeout(()=>{this.tempRoutes=null},2000)}async registerFileRoutes(n,i,t){let f=await import(n),s;if(t===".ts")s=x.basename(n,".ts");else if(t===".js")s=x.basename(n,".js");let c=i+"/"+s;if(c.endsWith("/index"))c=i;else if(c.endsWith("/api"))c=i;c=c.replace(/\[(.*?)\]/g,":$1");let o=["GET","POST","PUT","PATCH","DELETE","ANY","HEAD","OPTIONS","PROPFIND"];for(let u of o)if(f[u]){let r=u.toLowerCase(),g=f[u];this[r](`${this.baseApiUrl}${c}`,g)}}async loadRoutes(n,i){let t=await G.promises.readdir(n);for(let f of t){let s=x.join(n,f);if((await G.promises.stat(s)).isDirectory())this.loadRoutes(s,i+"/"+f);else if(f.endsWith(".ts"))this.registerFileRoutes(s,i,".ts");else if(f.endsWith(".js"))this.registerFileRoutes(s,i,".js")}}useLogger(n){return Q(n),this}useAdvancedLogger(n){return I(n),this}BunRoute(n,i,...t){if(!i||typeof i!=="string")throw new Error("give a path in string format");if(t.length===1){let f=t[0];this.routes[i]=async(s,c)=>{if(this.hasMiddleware){if(this.globalMiddlewares.length){let r=await b(this.globalMiddlewares,s,c);if(r)return r}let u=this.middlewares.get(i)||[];if(u?.length){let r=await b(u,s,c);if(r)return r}}if(n!==s.method)return new Response("Method Not Allowed",{status:405});let o=await f(s,c);if(o instanceof Promise)return await o??new Response("Not Found",{status:404});return o??new Response("Not Found",{status:404})}}else this.routes[i]=async(f,s)=>{if(this.hasMiddleware){if(this.globalMiddlewares.length){let o=await b(this.globalMiddlewares,f,s);if(o)return o}let c=this.middlewares.get(i)||[];if(c?.length){let o=await b(c,f,s);if(o)return o}}if(n!==f.method)return new Response("Method Not Allowed",{status:405});for(let c=0;c<t.length;c++){let o=t[c](f,s);if(o instanceof Promise)return await o??new Response("Not Found",{status:404});return o??new Response("Not Found",{status:404})}}}listen(n,...i){if(typeof Bun==="undefined")throw new Error(".listen() is designed to run on Bun only...");let t="0.0.0.0",f=void 0,s={};for(let o of i)if(typeof o==="string")t=o;else if(typeof o==="function")f=o;else if(typeof o==="object"&&o!==null)s=o;this.compile();let c={port:n,hostname:t,idleTimeOut:this.idleTimeOut,fetch:async(o,u)=>{let r=new URL(o.url);if(this.hooks.onRequest){let g=this.hooks.onRequest;for(let e=0;e<g.length;e++)await g[e](o,r,u)}return U(o,u,r,this)},static:this.staticFiles,routes:this.routes};if(s.sslCert&&s.sslKey)c.certFile=s.sslCert,c.keyFile=s.sslKey;if(this.serverInstance=Bun?.serve(c),s.sslCert&&s.sslKey)console.log(`HTTPS server is running on https://localhost:${n}`);if(f)return f();return this.serverInstance}close(n){if(this.serverInstance)this.serverInstance.stop(!0),this.serverInstance=null,n?n():console.log("Server has been stopped");else console.warn("Server is not running.")}route(n,i){if(!n||typeof n!=="string")throw new Error("Path must be a string");let t=Object.fromEntries(i.tempRoutes);return Object.entries(t).forEach(([s,c])=>{let o=s.replace(/::\w+$/,""),u=`${n}${o}`;if(!this.middlewares.has(u))this.middlewares.set(u,[]);c.handlers.slice(0,-1).forEach((m)=>{if(!this.middlewares.get(u)?.includes(m))this.middlewares.get(u)?.push(m)});let g=c.handlers[c.handlers.length-1],e=c.method;try{this.trie.insert(u,{handler:g,method:e})}catch(m){console.error(`Error inserting ${u}:`,m)}}),i=null,this}register(n,i){return this.route(n,i)}addRoute(n,i,t){if(typeof i!=="string")throw new Error(`Error in ${t[t.length-1]}: Path must be a string. Received: ${typeof i}`);if(typeof n!=="string")throw new Error(`Error in addRoute: Method must be a string. Received: ${typeof n}`);this.tempRoutes?.set(i+"::"+n,{method:n,handlers:t});let f=t.slice(0,-1),s=t[t.length-1];if(!this.middlewares.has(i))this.middlewares.set(i,[]);f.forEach((c)=>{if(i==="/")this.globalMiddlewares=[...new Set([...this.globalMiddlewares,...f])];else if(!this.middlewares.get(i)?.includes(c))this.middlewares.get(i)?.push(c)});try{if(n==="ANY"){let c=["GET","POST","PUT","DELETE","PATCH","OPTIONS","HEAD","PROPFIND"];for(let o of c)this.trie.insert(i,{handler:s,method:o})}this.trie.insert(i,{handler:s,method:n})}catch(c){console.error(`Error inserting ${i}:`,c)}}use(n,i){if(Array.isArray(n))n.forEach((f)=>{if(typeof f==="function")this.globalMiddlewares.push(f)});if(typeof n==="function"){if(this.globalMiddlewares.push(n),Array.isArray(i))i.forEach((f)=>{this.globalMiddlewares.push(f)});return}return(Array.isArray(n)?n.filter((f)=>typeof f==="string"):[n].filter((f)=>typeof f==="string")).forEach((f)=>{if(!this.middlewares.has(f))this.middlewares.set(f,[]);if(i)(Array.isArray(i)?i:[i]).forEach((c)=>{this.middlewares.get(f)?.push(c)})}),this}get(n,...i){return this.addRoute("GET",n,i),this}post(n,...i){return this.addRoute("POST",n,i),this}put(n,...i){return this.addRoute("PUT",n,i),this}patch(n,...i){return this.addRoute("PATCH",n,i),this}delete(n,...i){return this.addRoute("DELETE",n,i),this}any(n,...i){return this.addRoute("ANY",n,i),this}head(n,...i){return this.addRoute("HEAD",n,i),this}options(n,...i){return this.addRoute("OPTIONS",n,i),this}propfind(n,...i){return this.addRoute("PROPFIND",n,i),this}}export{H as default};
|
|
@@ -1,2 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import Diesel from "../../main";
|
|
2
|
+
import { ContextType } from "../../types";
|
|
3
|
+
type LogLevel = 'info' | 'warn' | 'error';
|
|
4
|
+
export type AdvancedLoggerOptions = {
|
|
5
|
+
app: Diesel;
|
|
6
|
+
logger?: () => void;
|
|
7
|
+
logLevel?: LogLevel;
|
|
8
|
+
onRequest?: (req: Request, url: URL) => void;
|
|
9
|
+
onSend?: (ctx: ContextType) => Response | void | Promise<Response | void>;
|
|
10
|
+
onError?: (error: Error, req: Request, url: URL) => Response | void | Promise<Response | void>;
|
|
11
|
+
routeNotFound?: (ctx: ContextType) => Response | void | Promise<Response | void>;
|
|
12
|
+
};
|
|
13
|
+
export declare const advancedLogger: (options?: AdvancedLoggerOptions) => void;
|
|
14
|
+
export type LoggerOptions = {
|
|
15
|
+
app: Diesel;
|
|
16
|
+
log?: () => void;
|
|
17
|
+
onRequest?: (req: Request, url: URL) => void;
|
|
18
|
+
onSend?: (ctx: ContextType) => Response | Promise<Response> | void;
|
|
19
|
+
onError?: (error: Error, req: Request, url: URL) => Response | Promise<Response> | void;
|
|
20
|
+
routeNotFound?: (ctx: ContextType) => Response | Promise<Response> | void;
|
|
21
|
+
};
|
|
22
|
+
export declare const logger: (options: LoggerOptions) => void;
|
|
23
|
+
export {};
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
var
|
|
2
|
-
${
|
|
3
|
-
`)},h=(
|
|
1
|
+
var t={reset:"\x1B[0m",info:"\x1B[36m",warn:"\x1B[33m",error:"\x1B[31m",method:{GET:"\x1B[32m",POST:"\x1B[34m",PUT:"\x1B[35m",DELETE:"\x1B[31m",PATCH:"\x1B[36m"}},R=(d,s,o)=>{let a=t[d]||t.reset,u=o?.method?t.method[o.method]||t.reset:t.reset,g=o?.status?o.status>=500?t.error:o.status>=400?t.warn:t.info:t.reset;console.log(`
|
|
2
|
+
${a}[${d.toUpperCase()}]${t.reset} ${s} - ${u}${o?.method||""}${t.reset}`);let p={timestamp:new Date().toISOString(),...o,status:o?.status?`${g}${o.status}${t.reset}`:void 0,method:o?.method?`${u}${o.method}${t.reset}`:void 0};console.log(JSON.stringify(p,null,2)+`
|
|
3
|
+
`)},h=(d)=>{let{app:s,logger:o,logLevel:a="info",onRequest:u,onSend:g,onError:p,routeNotFound:n}=d||{};s?.addHooks("onRequest",(e,r)=>{e.startTime=Date.now(),o?.()??R(a,"Incoming Request",{method:e.method,url:r.toString(),headers:{"user-agent":e.headers.get("user-agent"),"content-type":e.headers.get("content-type")}}),u?.(e,r)}),s?.addHooks("onSend",async(e)=>{let r=`${Date.now()-e.req.startTime}ms`;o?.()??R(a,"Response Sent",{method:e.req.method,url:e.url.toString(),status:e.status,duration:r,headers:{"content-type":e.headers.get("content-type")}});let i=await g?.(e);if(i instanceof Response)return i}),s?.addHooks("routeNotFound",async(e)=>{o?.()??R("warn","Route Not Found",{method:e.req.method,url:e.url.toString(),status:404});let r=await n?.(e);if(r instanceof Response)return r}),s?.addHooks("onError",async(e,r,i)=>{o?.()??R("error","Unhandled Error",{method:r.method,url:i.toString(),status:500,error:e.message});let m=await p?.(e,r,i);if(m instanceof Response)return m})},c=(d,s,o,a=0,u)=>{let g=t.method[s]||t.reset,p=a>=500?t.error:a>=400?t.warn:t.info,n=d==="<--"?`${d} ${g}${s}${t.reset} ${o}`:`${d} ${g}${s}${t.reset} ${o} ${p}${a}${t.reset} ${u||""}`;console.log(n)},l=(d)=>{let s=Date.now()-d;return s<1000?`${s}ms`:`${Math.round(s/1000)}s`},L=(d)=>{let{app:s,log:o,onRequest:a,onSend:u,onError:g,routeNotFound:p}=d;s.addHooks("onRequest",(n,e)=>{n.startTime=Date.now(),o?.()??c("<--",n.method,e.pathname),a?.(n,e)}),s.addHooks("onSend",async(n)=>{let{method:e,url:r}=n.req,i=new URL(r).pathname;o?.()??c("-->",e,i,n.status,l(n.req.startTime));let m=await u?.(n);if(m instanceof Response)return m}),s.addHooks("routeNotFound",async(n)=>{o?.()??c("[routeNotFound]",n.req.method,n.url.pathname,404);let e=await p?.(n);if(e instanceof Response)return e}),s.addHooks("onError",async(n,e,r)=>{o?.()??c(n.message,e.method,r.toString(),500);let i=await g?.(n,e,r);if(i instanceof Response)return i})};export{L as logger,h as advancedLogger};
|