/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/@cxl/cxl@2.2.0/server/cxl-server.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
"use strict";var express=require("express"),bodyParser=require("body-parser"),colors=require("colors/safe"),pathToRegexp=require("path-to-regexp"),path=require("path"),fs=require("fs"),os=require("os"),url=require("url"),protocols={http:require("http"),https:require("https")},__modules={},cxl=module.exports=global.cxl=function e(r){return __modules[r]||(__modules[r]=new e.Module(r))};function promisify(e,r){return function(){var t=arguments;return new Promise((function(s,i){t[t.length++]=function(e,r){e?i(e):s(r)},e.apply(r,t)}))}}require("../shared/cxl-util"),require("../shared/cxl-rx");class Route{constructor(e){"string"==typeof e?this.path=e:Object.assign(this,e)}get path(){return this.__path}set path(e){this.__path=e,this.__keys=[],this.regex=pathToRegexp(e,this.__keys)}match(e){var r=this.regex.exec(e);if(r)return this.params={},this.__keys.forEach(((e,t)=>{this.params[e.name]=r[t+1]})),!0}}class Service{constructor(e,r){this.module=e,Object.assign(this,r),this.route=new Route(r.route),this.initialize&&this.initialize()}middleware(e,r,t){if(this[e.method])return this[e.method](e,r);t()}}Object.assign(cxl,{Route:Route,Service:Service,promisify:promisify,file:{open:promisify(fs.open),read:promisify(fs.readFile),write:promisify(fs.writeFile),unlink:promisify(fs.unlink),readDirectory:promisify(fs.readdir),stat:promisify(fs.stat),mkdir:promisify(fs.mkdir),readJSON:e=>cxl.file.read(e).then(JSON.parse).catch((function(){}))},net:{request:function(e){return new Promise((function(r,t){try{var s="string"==typeof e?0===e.indexOf("https:")?"https":"http":e.protocol;e.url&&Object.assign(e,url.parse(e.url)),protocols[s||"http"].request(e,(function(e){e.body="",e.on("data",(function(r){e.body+=r})),e.once("end",(function(){r(e)})),e.once("error",t)})).once("socket",(function(r){r.setTimeout(e.timeout||5e3),r.on("timeout",t)})).once("error",t).end()}catch(e){t(e)}}))}},bodyParser:bodyParser,enableDebug:function(){cxl.debug=!0,require("./cxl-server-debug")},static:function(e,r){return"string"==typeof e&&(e=path.normalize(e)),express.static.call(this,e,r)},hrtime:function(){var e=process.hrtime();return e[0]+e[1]/1e9},formatTime(e,r){void 0===r&&(r=cxl.hrtime());var t=r-e,s=t.toFixed(4)+"s";return t>.1?t>.5?colors.red(s):colors.yellow(s):s},log:function(e,r,t){var s="string"==typeof e?(r||colors.cyan("cxl"))+" "+e:e;return console[t||"log"](s),cxl}}),cxl.Logger=function(e){var r=function(e){console.log(`${r.prefix} ${e}`)};return r.prefix=e,r.error=function(e){console.error(colors.red(`${r.prefix} ${e}`))},r.dbg=function(e){cxl.debug&&r(e)},r.operation=function(e,t,s){var i=cxl.hrtime(),o="function"==typeof t?t.call(s):t,n=function(){r(`${e} (${cxl.formatTime(i)})`)};return o&&o.then?o=o.then((function(e){return n(),e})):n(),o},r},cxl.EventListener={__listeners:null,stopListening:function(e,r,t){return this.__listeners.filter((s=>s.obj===e&&s.ev===r&&s.callback===t)).forEach((function(e){e.obj.off(e.ev,e.fn)})),this},listenTo:function(e,r,t){var s=this.__listeners||(this.__listeners=[]),i=t.bind(this);return e.on(r,i),s.push({obj:e,ev:r,callback:t,fn:i}),this}},cxl.Operation=class{constructor(e,r,t,s){var i=cxl.hrtime(),o="function"==typeof t?t.call(s):t,n=function(t){return e.log(r+` (${cxl.formatTime(i)})`),t};return o&&o.then?o=o.then(n):n(),o}},cxl.Module=class{constructor(e){this.name=e,this.__services={},this.__models={},this.__routes=[],this.__run=[],this.__config=[]}operation(e,r,t){return new cxl.Operation(this,e,r,t)}createServer(){return this.server=express(),this.server.set("env","production"),this}service(e,r){var t,s,i=this;return r?(i.__routes.push({id:`service "${e}"`,fn:"use",args:function(){return(t=r(i))||i.error("Invalid service definition"),t.model||(t.model=i.model(e)),t.model.prototype.tableName||(t.model.prototype.tableName=e),t.name=e,[(s=i.__services[e]=new cxl.Service(i,t)).middleware.bind(s)]}}),this):this.__services[e]}config(e){return this.__config.push(e),this}run(e){return this.__run.push(e),this}session(e){return this.use(e())}model(e,r){return r?(this.__models[e]=r,this):this.__models[e]}use(e){return this.__routes.push({id:`middleware ${e.name}`,fn:"use",args:[e.bind(this)]}),this}_safeRoute(e,r,t){try{e.call(this,r,t)}catch(e){this.error(e),t.sendStatus(500)}}route(e,r,t){return"string"==typeof t&&(t=this[t]),this.__routes.push({id:`route ${e} ${r}`,fn:e.toLowerCase(),args:[r,this._safeRoute.bind(this,t)]}),this}invoke(e){e.forEach((function(e){e.call(this)}),this)}start(){cxl.appName||(cxl.appName=this.name),this.invoke(this.__config),this.__routes.forEach(this._loadRoute,this),this.server&&!this.server.started&&this.startServer(),this.invoke(this.__run)}__findAddress(e){var r,t=os.networkInterfaces(),s=this,i=require("dns");function o(r,t,i){s.log(`Listening to ${s.secure?"https":"http"}://${i}:${e.port} (${r})`)}function n(e){i.lookupService(e.address,s.port,o.bind(s,e.address))}for(r in t)t[r].forEach(n,this)}onServerStart(){var e=this.__listener.address(),r=e.address;this.port||(this.port=e.port),cxl.appName===this.name&&(process.title=this.name+":"+this.port),"::"===r?this.__findAddress(e):this.log("Listening to "+r+":"+e.port)}onServerError(e){"EACCES"===e.code&&this.error("Could not start server in "+this.host+":"+this.port+". Make sure the host and port are not already in use."),this.error(e)}createSecureServer(e){return e.pfx?e.pfx=fs.readFileSync(e.pfx):(e.key=fs.readFileSync(e.key),e.cert=fs.readFileSync(e.cert)),protocols.https.createServer(e,this.server)}startServer(){var e=this.__listener=this.secure?this.createSecureServer(this.secure):protocols.http.createServer(this.server);e.listen(this.port,this.host,this.onServerStart.bind(this)),this.server.started=!0,process.on("close",e.close.bind(e)),e.on("error",this.onServerError.bind(this))}log(e){return cxl.log(e,this.logColor?colors[this.logColor](this.name):this.name),this}error(e){cxl.log(colors.red("ERROR "+e),colors.red(this.name),"error"),e instanceof Error&&console.error(e.stack)}dbg(){}extend(e,r,t){return Object.assign(this,e,r,t)}_loadRoute(e){var r=cxl.result(e,"args");this.server[e.fn].apply(this.server,r)}},Object.assign(cxl.Module.prototype,{name:null,logColor:"green",port:80,host:"",secure:null,server:null,__listener:null,__services:null,__routes:null,__models:null,__config:null,__run:null});
//# sourceMappingURL=/sm/9f824a2bb733a1278da5052d3aaa0409ac2f1a61954aec55394c3640de6ad16f.map