@jucie.io/engine-server 1.0.19 → 1.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/dist/main.js CHANGED
@@ -64,7 +64,7 @@ Please see the 3.x to 4.x migration guide for details on how to update your app.
64
64
  `:`
65
65
  `)+t,n=i+1,i=a.indexOf(`
66
66
  `,n)}while(i!==-1);return r+=a.slice(n),r}var{stdout:Vo,stderr:Xo}=mv,Zo=Symbol("GENERATOR"),Ua=Symbol("STYLER"),Gt=Symbol("IS_EMPTY"),xv=["ansi","ansi","ansi256","ansi16m"],$a=Object.create(null),J2=(a,e={})=>{if(e.level&&!(Number.isInteger(e.level)&&e.level>=0&&e.level<=3))throw new Error("The `level` option should be an integer from 0 to 3");let t=Vo?Vo.level:0;a.level=e.level===void 0?t:e.level},gv=class{constructor(e){return bv(e)}},bv=a=>{let e=(...t)=>t.join(" ");return J2(e,a),Object.setPrototypeOf(e,Wt.prototype),e};function Wt(a){return bv(a)}Object.setPrototypeOf(Wt.prototype,Function.prototype);for(let[a,e]of Object.entries(be))$a[a]={get(){let t=cn(this,Jo(e.open,e.close,this[Ua]),this[Gt]);return Object.defineProperty(this,a,{value:t}),t}};$a.visible={get(){let a=cn(this,this[Ua],!0);return Object.defineProperty(this,"visible",{value:a}),a}};var Ko=(a,e,t,...i)=>a==="rgb"?e==="ansi16m"?be[t].ansi16m(...i):e==="ansi256"?be[t].ansi256(be.rgbToAnsi256(...i)):be[t].ansi(be.rgbToAnsi(...i)):a==="hex"?Ko("rgb",e,t,...be.hexToRgb(...i)):be[t][a](...i),Q2=["rgb","hex","ansi256"];for(let a of Q2){$a[a]={get(){let{level:t}=this;return function(...i){let n=Jo(Ko(a,xv[t],"color",...i),be.color.close,this[Ua]);return cn(this,n,this[Gt])}}};let e="bg"+a[0].toUpperCase()+a.slice(1);$a[e]={get(){let{level:t}=this;return function(...i){let n=Jo(Ko(a,xv[t],"bgColor",...i),be.bgColor.close,this[Ua]);return cn(this,n,this[Gt])}}}}var Y2=Object.defineProperties(()=>{},{...$a,level:{enumerable:!0,get(){return this[Zo].level},set(a){this[Zo].level=a}}}),Jo=(a,e,t)=>{let i,n;return t===void 0?(i=a,n=e):(i=t.openAll+a,n=e+t.closeAll),{open:a,close:e,openAll:i,closeAll:n,parent:t}},cn=(a,e,t)=>{let i=(...n)=>e6(i,n.length===1?""+n[0]:n.join(" "));return Object.setPrototypeOf(i,Y2),i[Zo]=a,i[Ua]=e,i[Gt]=t,i},e6=(a,e)=>{if(a.level<=0||!e)return a[Gt]?"":e;let t=a[Ua];if(t===void 0)return e;let{openAll:i,closeAll:n}=t;if(e.includes("\x1B"))for(;t!==void 0;)e=vv(e,t.close,t.open),t=t.parent;let r=e.indexOf(`
67
- `);return r!==-1&&(e=hv(e,n,i,r)),i+e+n};Object.defineProperties(Wt.prototype,$a);var a6=Wt(),y3=Wt({level:Xo?Xo.level:0}),Y=a6;import{exec as t6}from"child_process";import{promisify as i6}from"util";var n6=i6(t6);async function yv({port:a,host:e,mode:t,open:i},n){let o=`${t==="production"?"https":"http"}://${e==="0.0.0.0"?"localhost":e}:${a}`;["",Y.bold.green("Clovie Server Ready"),"",` ${Y.gray("Mode")} ${Y.white(t.toUpperCase())}`,` ${Y.gray("URL")} ${Y.cyan.underline(o)}`,` ${Y.gray("Host")} ${Y.white(e)}:${Y.white(a)}`,""].forEach(l=>n.info(l));let p=i!==void 0?i:t==="development";if(p!==!1){let l=typeof p=="string"?p:o;try{await r6(l),n.info(Y.green("\u{1F310} Browser opened automatically"))}catch(u){n.debug("Could not open browser automatically:",u.message)}}["",Y.gray("\u{1F4A1} Quick Commands:"),Y.gray(` \u2022 Press ${Y.yellow("Ctrl+C")} to stop the server`),Y.gray(` \u2022 Visit ${Y.cyan(o)} to view your site`),Y.gray(" \u2022 Check the console for live reload updates"),""].forEach(l=>n.info(l))}async function r6(a){let e=process.platform,t;switch(e){case"darwin":t=`open "${a}"`;break;case"win32":t=`start "${a}"`;break;default:t=`xdg-open "${a}"`;break}try{await n6(t)}catch(i){throw new Error(`Failed to open browser: ${i.message}`)}}import{ServiceProvider as o6,definitionType as s6}from"@jucie.io/engine";var Qo=class extends o6{static manifest={name:"Clovie Server",namespace:"server",version:"1.0.0"};#i=[];#a=null;#t={onRequest:null,preHandler:null,onSend:null,onError:null};#e=!1;#n={};#s=[];actions(e){return{use:(...t)=>(this.#s.push(...t),this),add:(t,i,n,r={})=>{this.#o(null,t,i,n,r)},useAdapter:t=>(this.#a=t,this),hooks:t=>(this.#t={...this.#t,...t},this),listen:async(t={})=>{this.#n={...this.config,...t},this.#e=!0,this.#d();let i=e("relay"),n=this.#n.port||3e3,r=this.#n.host||"0.0.0.0",o=e("log"),s=new rn;if(s.hooks(this.#t),s.registerRoutes(this.#i),s.registerRoutes([{method:"GET",path:"/health",handler:l=>l.respond.json({status:"ok",timestamp:new Date().toISOString(),mode:this.#n.mode||"production"})},{method:"GET",path:"/api/info",handler:l=>l.respond.json({name:"Clovie",version:"1.0.0",mode:this.#n.mode||"production",routes:this.#i.length})}]),this.#n.outputDir&&s.setStaticFallback(this.#n.outputDir,(l,u)=>this.#f(l,u)),!this.#a)if(this.#n.adapter==="express"||this.#n.middleware?.length>0){let{ExpressAdapter:l}=await Promise.resolve().then(()=>(Nm(),zm));this.#a=l.create(),o.debug("Using Express adapter for middleware support")}else this.#a=on.create(),o.debug("Using HTTP adapter");await this.#a.initialize(s,this.#n,o),typeof this.#a.registerKernelHandler=="function"&&this.#a.registerKernelHandler(this.#n);let p=await this.#a.start({port:n,host:r}),c=p.address()?.port||n;return await yv({port:c,host:r,mode:this.#n.mode||"production",open:this.#n.open},o),i.broadcast("server:ready",p),p},getRoutes:()=>this.#i,clearRoutes:()=>{this.#i=[]},stop:async()=>{this.#a&&await this.#a.stop()},isRunning:()=>this.#a&&this.#a.isRunning(),getHttpServer:()=>this.#a?this.#a.getHttpServer():null}}#o(e="",t,i,n,r={},o=[]){if(e&&(i=e+i),Array.isArray(o))for(let s of o)typeof s=="function"?this.#e?this.#l(s):this.#s.push(s):this.#o(i,s.method,s.path,s.handler,s.meta,s.children);else typeof o=="function"&&(this.#e?this.#l(o):this.#s.push(o));this.#i.push({method:t.toUpperCase(),path:i,handler:n,meta:r})}#d(){this.#e&&this.#s.length!==0&&(this.#l(...this.#s),this.#s=[])}#l(...e){try{for(let t of e){let i=s6(t);switch(i){case"routes":let n=t(this.useContext,this.#n);if(Array.isArray(n))for(let p of n)this.#o(null,p.method,p.path,p.handler,p.meta,p.children);else throw new Error("Routes must be an array");break;case"route":let r=t(this.useContext,this.#n);if(typeof r=="object"&&r!==null)this.#o(null,r.method,r.path,r.handler,r.meta,r.children);else throw new Error("Route must be an object");break;case"hooks":let o=t(this.useContext,this.#n);if(typeof o=="object"&&o!==null)this.#t={...this.#t,...o};else throw new Error("Hooks must be an object");break;case"hook":let s=t(this.useContext,this.#n);if(typeof s=="function")this.#t={...this.#t,...s};else throw new Error("Hook must be a function");break;default:throw new Error(`Invalid factory type: ${i}`)}}}catch(t){throw console.error(`
67
+ `);return r!==-1&&(e=hv(e,n,i,r)),i+e+n};Object.defineProperties(Wt.prototype,$a);var a6=Wt(),y3=Wt({level:Xo?Xo.level:0}),Y=a6;import{exec as t6}from"child_process";import{promisify as i6}from"util";var n6=i6(t6);async function yv({port:a,host:e,mode:t,open:i},n){let o=`${t==="production"?"https":"http"}://${e==="0.0.0.0"?"localhost":e}:${a}`;["",Y.bold.green("\u{1F680} Server Ready! \u{1F680}"),"",` ${Y.gray("Mode")} ${Y.white(t.toUpperCase())}`,` ${Y.gray("URL")} ${Y.cyan.underline(o)}`,` ${Y.gray("Host")} ${Y.white(e)}:${Y.white(a)}`,""].forEach(l=>n.info(l));let p=i!==void 0?i:t==="development";if(p!==!1){let l=typeof p=="string"?p:o;try{await r6(l),n.info(Y.green("\u{1F310} Browser opened automatically"))}catch(u){n.debug("Could not open browser automatically:",u.message)}}["",Y.gray("\u{1F4A1} Quick Commands:"),Y.gray(` \u2022 Press ${Y.yellow("Ctrl+C")} to stop the server`),Y.gray(` \u2022 Visit ${Y.cyan(o)} to view your site`),Y.gray(" \u2022 Check the console for live reload updates"),""].forEach(l=>n.info(l))}async function r6(a){let e=process.platform,t;switch(e){case"darwin":t=`open "${a}"`;break;case"win32":t=`start "${a}"`;break;default:t=`xdg-open "${a}"`;break}try{await n6(t)}catch(i){throw new Error(`Failed to open browser: ${i.message}`)}}import{ServiceProvider as o6,definitionType as s6}from"@jucie.io/engine";var Qo=class extends o6{static manifest={name:"Server",namespace:"server",version:"1.0.0"};#i=[];#a=null;#t={onRequest:null,preHandler:null,onSend:null,onError:null};#e=!1;#n={};#s=[];actions(e){return{use:(...t)=>(this.#s.push(...t),this),add:(t,i,n,r={})=>{this.#o(null,t,i,n,r)},useAdapter:t=>(this.#a=t,this),hooks:t=>(this.#t={...this.#t,...t},this),listen:async(t={})=>{this.#n={...this.config,...t},this.#e=!0,this.#d();let i=e("relay"),n=this.#n.port||3e3,r=this.#n.host||"0.0.0.0",o=e("log"),s=new rn;if(s.hooks(this.#t),s.registerRoutes(this.#i),s.registerRoutes([{method:"GET",path:"/health",handler:l=>l.respond.json({status:"ok",timestamp:new Date().toISOString(),mode:this.#n.mode||"production"})},{method:"GET",path:"/api/info",handler:l=>l.respond.json({name:"Info",version:"1.0.0",mode:this.#n.mode||"production",routes:this.#i.length})}]),this.#n.outputDir&&s.setStaticFallback(this.#n.outputDir,(l,u)=>this.#f(l,u)),!this.#a)if(this.#n.adapter==="express"||this.#n.middleware?.length>0){let{ExpressAdapter:l}=await Promise.resolve().then(()=>(Nm(),zm));this.#a=l.create(),o.debug("Using Express adapter for middleware support")}else this.#a=on.create(),o.debug("Using HTTP adapter");await this.#a.initialize(s,this.#n,o),typeof this.#a.registerKernelHandler=="function"&&this.#a.registerKernelHandler(this.#n);let p=await this.#a.start({port:n,host:r}),c=p.address()?.port||n;return await yv({port:c,host:r,mode:this.#n.mode||"production",open:this.#n.open},o),i.broadcast("server:ready",p),p},getRoutes:()=>this.#i,clearRoutes:()=>{this.#i=[]},stop:async()=>{this.#a&&await this.#a.stop()},isRunning:()=>this.#a&&this.#a.isRunning(),getHttpServer:()=>this.#a?this.#a.getHttpServer():null}}#o(e="",t,i,n,r={},o=[]){if(e&&(i=e+i),Array.isArray(o))for(let s of o)typeof s=="function"?this.#e?this.#l(s):this.#s.push(s):this.#o(i,s.method,s.path,s.handler,s.meta,s.children);else typeof o=="function"&&(this.#e?this.#l(o):this.#s.push(o));this.#i.push({method:t.toUpperCase(),path:i,handler:n,meta:r})}#d(){this.#e&&this.#s.length!==0&&(this.#l(...this.#s),this.#s=[])}#l(...e){try{for(let t of e){let i=s6(t);switch(i){case"routes":let n=t(this.useContext,this.#n);if(Array.isArray(n))for(let p of n)this.#o(null,p.method,p.path,p.handler,p.meta,p.children);else throw new Error("Routes must be an array");break;case"route":let r=t(this.useContext,this.#n);if(typeof r=="object"&&r!==null)this.#o(null,r.method,r.path,r.handler,r.meta,r.children);else throw new Error("Route must be an object");break;case"hooks":let o=t(this.useContext,this.#n);if(typeof o=="object"&&o!==null)this.#t={...this.#t,...o};else throw new Error("Hooks must be an object");break;case"hook":let s=t(this.useContext,this.#n);if(typeof s=="function")this.#t={...this.#t,...s};else throw new Error("Hook must be a function");break;default:throw new Error(`Invalid factory type: ${i}`)}}}catch(t){throw console.error(`
68
68
  \u274C Error using factories: ${t.message}
69
69
  `),t}}async#f(e,t){let i=await import("fs/promises"),n=await import("path");try{let r=e.req.path==="/"?"/index.html":e.req.path,o=n.join(t,r),s;try{s=await i.stat(o)}catch{if(n.extname(r))return e.respond.text("Not Found",404);{let l=n.join(t,`${r}.html`);try{s=await i.stat(l),o=l}catch{return e.respond.text("Not Found",404)}}}if(s.isFile()){let p=n.extname(o),c=this.#r(p),l=await i.readFile(o);return c==="text/html"?e.respond.html(l.toString(),200):c==="text/css"?e.respond.text(l.toString(),200,{"Content-Type":"text/css"}):c==="application/javascript"?e.respond.text(l.toString(),200,{"Content-Type":"application/javascript"}):c.startsWith("text/")?e.respond.text(l.toString(),200,{"Content-Type":c}):e.respond.file(o,200,{"Content-Type":c})}else return e.respond.text("Not Found",404)}catch{return e.respond.text("Not Found",404)}}#r(e){return{".html":"text/html",".css":"text/css",".js":"application/javascript",".json":"application/json",".png":"image/png",".jpg":"image/jpeg",".jpeg":"image/jpeg",".gif":"image/gif",".svg":"image/svg+xml",".ico":"image/x-icon",".woff":"font/woff",".woff2":"font/woff2",".ttf":"font/ttf",".eot":"application/vnd.ms-fontobject"}[e.toLowerCase()]||"application/octet-stream"}};import{createDefinition as wv}from"@jucie.io/engine";var c6=wv("routes",[Array]),p6=wv("route",[Object]);import{createDefinition as kv}from"@jucie.io/engine";var l6=kv("hooks",[Object]),u6=kv("hook",[Function]);export{Qo as Server,u6 as defineHook,l6 as defineHooks,p6 as defineRoute,c6 as defineRoutes};
70
70
  /*! Bundled license information: