diesel-core
Version:
Web framework built on Web Standards
2 lines (1 loc) • 5.24 kB
JavaScript
var j=Object.create;var{getPrototypeOf:I,defineProperty:X,getOwnPropertyNames:M}=Object;var N=Object.prototype.hasOwnProperty;var R=(w,A,E)=>{E=w!=null?j(I(w)):{};let z=A||!w||!w.__esModule?X(E,"default",{value:w,enumerable:!0}):E;for(let F of M(w))if(!N.call(z,F))X(z,F,{get:()=>w[F],enumerable:!0});return z};var U=((w)=>typeof require!=="undefined"?require:typeof Proxy!=="undefined"?new Proxy(w,{get:(A,E)=>(typeof require!=="undefined"?require:A)[E]}):w)(function(w){if(typeof require!=="undefined")return require.apply(this,arguments);throw Error('Dynamic require of "'+w+'" is not supported')});function Y(w){switch(w.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 V(w,A,E){let z=null,F=null,J=null,G=null,O={};return{req:w,server:A,url:E,status:200,headers:new Headers,setHeader(f,g){return this.headers.set(f,g),this},removeHeader(f){return this.headers.delete(f),this},get ip(){return this.server.requestIP(this.req)?.address??null},get query(){if(!z)try{z=Object.fromEntries(this.url.searchParams)}catch(f){throw new Error("Failed to parse query parameters")}return z},get params(){if(!F&&this.req.routePattern)try{F=_(this.req.routePattern,this.url.pathname)}catch(f){throw new Error("Failed to extract route parameters")}return F??{}},get body(){if(this.req.method==="GET")return Promise.resolve({});if(!G)G=(async()=>{let f=await B(this.req);if(f.error)throw new Error(f.error);return Object.keys(f).length===0?null:f})();return G},set(f,g){return O[f]=g,this},get(f){return O[f]},text(f,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(f,{status:this.status,headers:this.headers})},send(f,g){if(g)this.status=g;let L=new Map([["string","text/plain; charset=utf-8"],["object","application/json; charset=utf-8"],["Uint8Array","application/octet-stream"],["ArrayBuffer","application/octet-stream"]]),x;if(f instanceof Uint8Array)x="Uint8Array";else if(f instanceof ArrayBuffer)x="ArrayBuffer";else x=typeof f;if(!this.headers.has("Content-Type"))this.headers.set("Content-Type",L.get(x)??"text/plain; charset=utf-8");let K=x==="object"&&f!==null?JSON.stringify(f):f;return new Response(K,{status:this.status,headers:this.headers})},json(f,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(f,{status:this.status,headers:this.headers})},file(f,g,L){if(L)this.status=L;let x=Bun.file(f);if(!this.headers.has("Content-Type"))this.headers.set("Content-Type",g??Y(f));return new Response(x,{status:this.status,headers:this.headers})},async ejs(f,g={},L){if(L)this.status=L;let x;try{x=await import("ejs"),x=x.default||x}catch(K){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 K=await Bun.file(f).text(),Z=x.render(K,g),$=new Headers({"Content-Type":"text/html; charset=utf-8"});return new Response(Z,{status:this.status,headers:$})}catch(K){return console.error("EJS Rendering Error:",K),new Response("Error rendering template",{status:500})}},redirect(f,g){if(g)this.status=g;else this.status=302;return this.headers.set("Location",f),new Response(null,{status:this.status,headers:this.headers})},stream(f){let g=new Headers(this.headers),L=new ReadableStream({async start(x){await f(x),x.close()}});return new Response(L,{headers:g})},yieldStream(f){return new Response({async*[Symbol.asyncIterator](){yield*f()}},{headers:this.headers})},setCookie(f,g,L={}){let x=`${encodeURIComponent(f)}=${encodeURIComponent(g)}`;if(L.maxAge)x+=`; Max-Age=${L.maxAge}`;if(L.expires)x+=`; Expires=${L.expires.toUTCString()}`;if(L.path)x+=`; Path=${L.path}`;if(L.domain)x+=`; Domain=${L.domain}`;if(L.secure)x+="; Secure";if(L.httpOnly)x+="; HttpOnly";if(L.sameSite)x+=`; SameSite=${L.sameSite}`;return this.headers.append("Set-Cookie",x),this},get cookies(){if(!J){let f=this.req.headers.get("cookie");J=f?W(f):{}}return J}}}function W(w){return Object.fromEntries(w.split(";").map((A)=>{let[E,...z]=A.trim().split("=");return[E,decodeURIComponent(z.join("="))]}))}function _(w,A){let E={},z=w.split("/"),[F]=A.split("?"),J=F.split("/");if(z.length!==J.length)return null;for(let G=0;G<z.length;G++)if(z[G].startsWith(":"))E[z[G].slice(1)]=J[G];return E}async function B(w){let A=w.headers.get("Content-Type");if(!A)return{};if(w.headers.get("Content-Length")==="0"||!w.body)return{};try{if(A.startsWith("application/json"))return await w.json();if(A.startsWith("application/x-www-form-urlencoded")){let z=await w.text();return Object.fromEntries(new URLSearchParams(z))}if(A.startsWith("multipart/form-data")){let z=await w.formData(),F={};for(let[J,G]of z.entries())F[J]=G;return F}return{error:"Unknown request body type"}}catch(z){return{error:"Invalid request body format"}}}export{V as default};