@sumor/app
Advanced tools
Comparing version 17.0.14 to 17.0.15
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("Sumor_App",[],t):"object"==typeof exports?exports.Sumor_App=t():e.Sumor_App=t()}("undefined"!=typeof self?self:this,(function(){return function(e){var t={};function r(o){if(t[o])return t[o].exports;var n=t[o]={i:o,l:!1,exports:{}};return e[o].call(n.exports,n,n.exports,r),n.l=!0,n.exports}return r.m=e,r.c=t,r.d=function(e,t,o){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:o})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var o=Object.create(null);if(r.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var n in e)r.d(o,n,function(t){return e[t]}.bind(null,n));return o},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=10)}([function(e,t){e.exports=require("@sumor/utils")},function(e,t){e.exports=require("@sumor/base")},function(e,t){e.exports=require("body-parser")},function(e,t){e.exports=require("selfsigned")},function(e,t){e.exports=require("express")},function(e,t){e.exports=require("compression")},function(e,t){e.exports=require("zlib")},function(e,t){e.exports=require("serve-static")},function(e,t){e.exports=require("spdy")},function(e,t){e.exports=require("http")},function(e,t,r){"use strict";r.r(t),r.d(t,"App",(function(){return O})),r.d(t,"_meta",(function(){return j}));var o=r(0),n=r.n(o),a=r(3),s=r.n(a),i=async(e,t)=>{const r={key:e+"/domain.key",cert:e+"/domain.cer",ca:e+"/ca.cer"};let o=await n.a.file.exists(r.key)&&await n.a.file.exists(r.cert);if(o||(await(async(e,t)=>{t=t||"selfsigned";const r=await new Promise((e,r)=>{s.a.generate([{name:"commonName",value:t}],{days:3650},(function(t,o){t?r(t):e(o)}))});r.private&&await n.a.file.writeFile(e.key,r.private),r.cert&&await n.a.file.writeFile(e.cert,r.cert)})(r,t),o=!0),o){const e=await n.a.file.readFile(r.key),t=await n.a.file.readFile(r.cert);let o;return await n.a.file.exists(r.ca)&&(o=await n.a.file.readFile(r.ca)),{key:e,cert:t,ca:o}}},c=r(1),l=r.n(c),p=r(4),u=r.n(p),f=r(5),g=r.n(f),m=r(6),h=r.n(m),d=r(7),w=r.n(d),y=r(2),x=r.n(y),b=r(8),v=r.n(b),P=r(9),S=r.n(P),_=async(e,t)=>{const r=u()();return r.disable("x-powered-by"),e.express=r,(async e=>{const t=e.express,r=new o.Logger("HTTP");t.use((async function(e,t,o){r.trace(`${e.method} ${e.originalUrl} "${e.get("user-agent")}"`),await o()}))})(e),(e=>{const t=e.express;e.cors&&t.all("*",(e,t,r)=>{t.header("Access-Control-Allow-Origin","*"),t.header("Access-Control-Allow-Headers","X-Requested-With"),t.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS"),r()})})(e),(e=>{const t=e.express;t.use(g()({filter:function(){return!0},flush:h.a.Z_SYNC_FLUSH})),e.logger.debug("静态文件目录:"+e.path.static),t.use(w()(e.path.static)),t.use(x.a.urlencoded({extended:!1})),t.use(x.a.json()),t.use(x.a.text())})(e),((e,t)=>{const r=e.express,o=t.getList("api");for(let n of o){const o="/"+n.replace(/\./g,"/");e.logger.debug(`API已监听:${e.origin}${o}`),r.all(o,(async function(e,r,o){const a=Date.now();let s;const i={};Object.assign(i,e.query,e.body);const c={};try{const e=t.getProgram("event.context");if(e)for(let t in e){const r=l.a.caller(e[t].handler,e[t].meta,c);await r()}const r=t.caller(n,c);s={code:"OK",data:await r(i)}}catch(e){const r=t.getProgram("event.error");if(r)for(let e in r){const t=l.a.caller(r[e].handler,r[e].meta,c);await t()}s={code:e.code||"INTERNAL_ERROR",message:e.message}}const p=Date.now();r.set("sumor-response-time",p-a+"ms"),r.send(s)}))}})(e,t),await(async e=>{const t=e.express,r=e.path.static+"/index.html";if(await n.a.file.exists(r)){const e=await n.a.file.readFile(r,"utf-8");t.use((t,r)=>{r.set("Content-Type","text/html;charset=utf-8"),r.send(e)})}})(e),await(async e=>{const t=e.express;return await n.a.port.consume(e.port),await new Promise(r=>{let o;o=e.ssl?v.a.createServer(e.ssl,t):S.a.createServer(t);const a=[];o.on("connection",(function(e){a.push(e),e.once("close",(function(){a.splice(a.indexOf(e),1)}))}));const s=async()=>(await n.a.port.release(e.port),await new Promise(e=>{a.forEach((function(e){e.destroy()})),o.close((function(){e()}))}));o.listen(e.port,()=>{e.logger.info("网页服务已运行在:"+e.origin),r(s)})})})(e)};class A{constructor(e){this.program={},this.init(e)}init(e){for(let t in e){const r=e[t];for(let e in r._meta.program){this.program[e]=this.program[e]||[];const t=r._meta.program[e],o=l.a.object.get(r,e);this.program[e].push({library:r._meta.name,meta:t,handler:o})}}}getList(e){const t=[];for(let r in this.program)new RegExp(`^${e}.`).test(r)&&t.push(r);return t}getProgram(e){return this.program[e]}caller(e,t){let r=!1;for(let t in this.program[e]){this.program[e][t].meta.async&&(r=!0)}const n=t.token&&t.token.userId?t.token.userId:"";return r?async r=>{let a;for(let s in this.program[e]){t.logger=new o.Logger(this.program[e][s].library||"APP",{user:n});const i=l.a.caller(this.program[e][s].handler,this.program[e][s].meta,t),c=await i(r);void 0!==c&&(a=c)}return a}:r=>{let a;for(let s in this.program[e]){t.logger=new o.Logger(this.program[e][s].library||"APP",{user:n});const i=l.a.caller(this.program[e][s].handler,this.program[e][s].meta,t)(r);void 0!==i&&(a=i)}return a}}}class O{constructor(e,t){this.root=e||process.cwd(),this.config=t,this.handler=[],this.program={}}async init(){this.runtime||(this.runtime=await(async(e,t)=>{e=e||process.cwd();const r={};r.config=t||await n.a.file.readJson(e+"/config.json")||{};const o=(r.config.SumorLogLevel||r.config.logLevel||"trace").toUpperCase();n.a.Logger.setLevel(o),r.logger=new n.a.Logger("Sumor_App"),r.logger.info("日志记录级别:"+o),r.path={ssl:e+"/ssl",static:e+"/static"};for(let e in r.path)r.path[e]=l.a.name.path.normalize(r.path[e]);r.ssl=await i(r.path.ssl),r.ssl?r.protocol=r.config.protocol||"https":r.protocol="http",r.domain=r.config.domain||"localhost",r.port=await n.a.port.occupy(r.config.port);const a="http"===r.protocol&&80===r.port||"https"===r.protocol&&443===r.port?"":":"+r.port;return r.origin=`${r.protocol}://${r.domain}${a}`,r})(this.root,this.config))}async handle(e){await this.init(),this.handler.push(e)}async static(e){await n.a.file.exists(e)&&(await this.init(),await n.a.file.copy(e,this.runtime.path.static))}async cors(e){await this.init(),this.runtime.cors=e||!0}async serve(){const e=Date.now();await this.init(),this.program=new A(this.handler),this.webServerCloser=await _(this.runtime,this.program);const t=Date.now();this.runtime.logger.info(`服务已启动完成,耗时${t-e}毫秒`)}async close(){this.webServerCloser&&await this.webServerCloser()}}var j={name:"Sumor_App",program:{App:{name:"轻呈云应用",desc:"构建轻呈云应用",arguments:"path",isClass:!0,parameters:{path:{name:"应用目录"}}}}};t.default={App:O,_meta:j}}])})); | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("Sumor_App",[],t):"object"==typeof exports?exports.Sumor_App=t():e.Sumor_App=t()}("undefined"!=typeof self?self:this,(function(){return function(e){var t={};function r(o){if(t[o])return t[o].exports;var n=t[o]={i:o,l:!1,exports:{}};return e[o].call(n.exports,n,n.exports,r),n.l=!0,n.exports}return r.m=e,r.c=t,r.d=function(e,t,o){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:o})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var o=Object.create(null);if(r.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var n in e)r.d(o,n,function(t){return e[t]}.bind(null,n));return o},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=10)}([function(e,t){e.exports=require("@sumor/utils")},function(e,t){e.exports=require("@sumor/base")},function(e,t){e.exports=require("body-parser")},function(e,t){e.exports=require("selfsigned")},function(e,t){e.exports=require("express")},function(e,t){e.exports=require("compression")},function(e,t){e.exports=require("zlib")},function(e,t){e.exports=require("serve-static")},function(e,t){e.exports=require("spdy")},function(e,t){e.exports=require("http")},function(e,t,r){"use strict";r.r(t),r.d(t,"App",(function(){return j})),r.d(t,"_meta",(function(){return O}));var o=r(0),n=r.n(o),a=r(3),s=r.n(a),i=async(e,t)=>{const r={key:e+"/domain.key",cert:e+"/domain.cer",ca:e+"/ca.cer"};let o=await n.a.file.exists(r.key)&&await n.a.file.exists(r.cert);if(o||(await(async(e,t)=>{t=t||"selfsigned";const r=await new Promise((e,r)=>{s.a.generate([{name:"commonName",value:t}],{days:3650},(function(t,o){t?r(t):e(o)}))});r.private&&await n.a.file.writeFile(e.key,r.private),r.cert&&await n.a.file.writeFile(e.cert,r.cert)})(r,t),o=!0),o){const e=await n.a.file.readFile(r.key),t=await n.a.file.readFile(r.cert);let o;return await n.a.file.exists(r.ca)&&(o=await n.a.file.readFile(r.ca)),{key:e,cert:t,ca:o}}},c=r(1),l=r.n(c),u=r(4),p=r.n(u),f=r(5),d=r.n(f),g=r(6),h=r.n(g),m=r(7),w=r.n(m),y=r(2),x=r.n(y),b=r(8),v=r.n(b),S=r(9),_=r.n(S),A=async(e,t)=>{const r=p()();return r.disable("x-powered-by"),e.express=r,(async e=>{const t=e.express,r=new o.Logger("HTTP");t.use((async function(e,t,o){r.trace(`${e.method} ${e.originalUrl} "${e.get("user-agent")}"`),await o()}))})(e),(e=>{const t=e.express;e.cors&&t.all("*",(e,t,r)=>{t.header("Access-Control-Allow-Origin","*"),t.header("Access-Control-Allow-Headers","X-Requested-With"),t.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS"),r()})})(e),(e=>{const t=e.express;t.use(d()({filter:function(){return!0},flush:h.a.Z_SYNC_FLUSH})),e.logger.debug("静态文件目录:"+e.path.static),t.use(w()(e.path.static)),t.use(x.a.urlencoded({extended:!1})),t.use(x.a.json()),t.use(x.a.text())})(e),((e,t)=>{const r=e.express,o=t.getList("api");for(let n of o){const o="/"+n.replace(/\./g,"/");e.logger.debug(`API已监听:${e.origin}${o}`),r.all(o,(async function(e,r,o){const a=Date.now();let s;const i={};Object.assign(i,e.query,e.body);const c={};try{const e=t.caller("event.context",c);await e();const r=t.caller(n,c);s={code:"OK",data:await r(i)}}catch(e){const r=t.caller("event.error",c);await r(),s={code:e.code||"INTERNAL_ERROR",message:e.message}}const l=Date.now();r.set("sumor-response-time",l-a+"ms"),r.send(s)}))}})(e,t),await(async e=>{const t=e.express,r=e.path.static+"/index.html";if(await n.a.file.exists(r)){const e=await n.a.file.readFile(r,"utf-8");t.use((t,r)=>{r.set("Content-Type","text/html;charset=utf-8"),r.send(e)})}})(e),await(async e=>{const t=e.express;return await n.a.port.consume(e.port),await new Promise(r=>{let o;o=e.ssl?v.a.createServer(e.ssl,t):_.a.createServer(t);const a=[];o.on("connection",(function(e){a.push(e),e.once("close",(function(){a.splice(a.indexOf(e),1)}))}));const s=async()=>(await n.a.port.release(e.port),await new Promise(e=>{a.forEach((function(e){e.destroy()})),o.close((function(){e()}))}));o.listen(e.port,()=>{e.logger.info("网页服务已运行在:"+e.origin),r(s)})})})(e)};class P{constructor(e){this.program={},this.context={},this.init(e)}init(e){for(let t in e){const r=e[t];for(let e in r._meta.program){this.program[e]=this.program[e]||[];const t=r._meta.program[e],o=l.a.object.get(r,e);this.program[e].push({library:r._meta.name,meta:t,handler:o})}}}getList(e){const t=[];for(let r in this.program)new RegExp(`^${e}.`).test(r)&&t.push(r);return t}caller(e,t,r){r||Object.assign(t,l.a.object.clone(this.context));const n=this.program[e];let a=!1;for(let e in n){n[e].meta.async&&(a=!0)}const s=t.token&&t.token.userId?t.token.userId:"";return a?async e=>{let a;for(let i in n){r||(t.logger=new o.Logger(n[i].library||"APP",{user:s}));const c=l.a.caller(n[i].handler,n[i].meta,t),u=await c(e);void 0!==u&&(a=u)}return a}:e=>{let a;for(let i in n){r||(t.logger=new o.Logger(n[i].library||"APP",{user:s}));const c=l.a.caller(n[i].handler,n[i].meta,t)(e);void 0!==c&&(a=c)}return a}}}class j{constructor(e,t){this.root=e||process.cwd(),this.config=t,this.handler=[],this.program={}}async init(){this.runtime||(this.runtime=await(async(e,t)=>{e=e||process.cwd();const r={};r.config=t||await n.a.file.readJson(e+"/config.json")||{};const o=(r.config.SumorLogLevel||r.config.logLevel||"trace").toUpperCase();n.a.Logger.setLevel(o),r.logger=new n.a.Logger("Sumor_App"),r.logger.info("日志记录级别:"+o),r.path={ssl:e+"/ssl",static:e+"/static"};for(let e in r.path)r.path[e]=l.a.name.path.normalize(r.path[e]);r.ssl=await i(r.path.ssl),r.ssl?r.protocol=r.config.protocol||"https":r.protocol="http",r.domain=r.config.domain||"localhost",r.port=await n.a.port.occupy(r.config.port);const a="http"===r.protocol&&80===r.port||"https"===r.protocol&&443===r.port?"":":"+r.port;return r.origin=`${r.protocol}://${r.domain}${a}`,r})(this.root,this.config))}async handle(e){await this.init(),this.handler.push(e)}async static(e){await n.a.file.exists(e)&&(await this.init(),await n.a.file.copy(e,this.runtime.path.static))}async cors(e){await this.init(),this.runtime.cors=e||!0}async serve(){const e=Date.now();await this.init(),this.program=new P(this.handler),this.webServerCloser=await A(this.runtime,this.program);const t=Date.now(),r=this.program.caller("event.serve",this.program.context,!0);await r(),this.runtime.logger.info(`服务已启动完成,耗时${t-e}毫秒`)}async close(){this.webServerCloser&&await this.webServerCloser()}}var O={name:"Sumor_App",program:{App:{name:"轻呈云应用",desc:"构建轻呈云应用",arguments:"path",isClass:!0,parameters:{path:{name:"应用目录"}}}}};t.default={App:j,_meta:O}}])})); |
{ | ||
"name": "@sumor/app", | ||
"version": "17.0.14", | ||
"version": "17.0.15", | ||
"dependencies": { | ||
@@ -5,0 +5,0 @@ "@sumor/base": "^17.0.12", |
@@ -49,5 +49,11 @@ # @sumor/app | ||
## 事件增强 | ||
### context | ||
### 全局事件 | ||
#### serve | ||
启动服务后事件,可初始化全局变量等 | ||
### 会话事件 | ||
#### context | ||
交互上下文处理事件,可藉由此拓展上下文内容 | ||
### error | ||
#### error | ||
错误处理,用于拓展错误发生处理额外的操作,如数据库回滚 | ||
@@ -54,0 +60,0 @@ |
94
9655