@doracli/express 0.0.5 → 0.0.6
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/LICENSE +20 -20
- package/lib/cjs/index.js +2 -2
- package/lib/esm/index.js +2 -2
- package/lib/type/index.d.ts +8 -6
- package/package.json +8 -8
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2020-2029 zhangzhiguo
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020-2029 zhangzhiguo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
21
|
SOFTWARE.
|
package/lib/cjs/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";var e=require("path"),t=require("@cclr/utils"),r=require("@dorabag/file-pro"),n=require("express"),o=require("@cclr/lang"),i=require("
|
|
1
|
+
"use strict";var e=require("path"),t=require("@cclr/utils"),r=require("@dorabag/file-pro"),n=require("express"),o=require("@cclr/lang"),i=require("@doracli/helper"),a=require("crypto"),c=require("fs"),u=require("zlib"),s=require("node:path"),f=require("http"),l=require("https"),p=require("url");function d(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=Array(t);r<t;r++)n[r]=e[r];return n}function h(e,t,r,n,o,i,a){try{var c=e[i](a),u=c.value}catch(e){return void r(e)}c.done?t(u):Promise.resolve(u).then(n,o)}function v(e){return function(){var t=this,r=arguments;return new Promise(function(n,o){var i=e.apply(t,r);function a(e){h(i,n,o,a,c,"next",e)}function c(e){h(i,n,o,a,c,"throw",e)}a(void 0)})}}function g(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function y(e,t,r){return t&&function(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,O(n.key),n)}}(e.prototype,t),Object.defineProperty(e,"prototype",{writable:!1}),e}function m(e,t,r){return(t=O(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function b(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),r.push.apply(r,n)}return r}function w(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?b(Object(r),!0).forEach(function(t){m(e,t,r[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):b(Object(r)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))})}return e}function j(){
|
|
2
2
|
/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */
|
|
3
|
-
var e,t,r="function"==typeof Symbol?Symbol:{},n=r.iterator||"@@iterator",o=r.toStringTag||"@@toStringTag";function i(r,n,o,i){var c=n&&n.prototype instanceof u?n:u,s=Object.create(c.prototype);return x(s,"_invoke",function(r,n,o){var i,u,c,s=0,f=o||[],l=!1,p={p:0,n:0,v:e,a:d,f:d.bind(e,4),d:function(t,r){return i=t,u=0,c=e,p.n=r,a}};function d(r,n){for(u=r,c=n,t=0;!l&&s&&!o&&t<f.length;t++){var o,i=f[t],d=p.p,h=i[2];r>3?(o=h===n)&&(c=i[(u=i[4])?5:(u=3,3)],i[4]=i[5]=e):i[0]<=d&&((o=r<2&&d<i[1])?(u=0,p.v=n,p.n=i[1]):d<h&&(o=r<3||i[0]>n||n>h)&&(i[4]=r,i[5]=n,p.n=h,u=0))}if(o||r>1)return a;throw l=!0,n}return function(o,f,h){if(s>1)throw TypeError("Generator is already running");for(l&&1===f&&d(f,h),u=f,c=h;(t=u<2?e:c)||!l;){i||(u?u<3?(u>1&&(p.n=-1),d(u,c)):p.n=c:p.v=c);try{if(s=2,i){if(u||(o="next"),t=i[o]){if(!(t=t.call(i,c)))throw TypeError("iterator result is not an object");if(!t.done)return t;c=t.value,u<2&&(u=0)}else 1===u&&(t=i.return)&&t.call(i),u<2&&(c=TypeError("The iterator does not provide a '"+o+"' method"),u=1);i=e}else if((t=(l=p.n<0)?c:r.call(n,p))!==a)break}catch(t){i=e,u=1,c=t}finally{s=1}}return{value:t,done:l}}}(r,o,i),!0),s}var a={};function u(){}function c(){}function s(){}t=Object.getPrototypeOf;var f=[][n]?t(t([][n]())):(x(t={},n,function(){return this}),t),l=s.prototype=u.prototype=Object.create(f);function p(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,s):(e.__proto__=s,x(e,o,"GeneratorFunction")),e.prototype=Object.create(l),e}return c.prototype=s,x(l,"constructor",s),x(s,"constructor",c),c.displayName="GeneratorFunction",x(s,o,"GeneratorFunction"),x(l),x(l,o,"Generator"),x(l,n,function(){return this}),x(l,"toString",function(){return"[object Generator]"}),(j=function(){return{w:i,m:p}})()}function x(e,t,r,n){var o=Object.defineProperty;try{o({},"",{})}catch(e){o=0}x=function(e,t,r,n){function i(t,r){x(e,t,function(e){return this._invoke(t,r,e)})}t?o?o(e,t,{value:r,enumerable:!n,configurable:!n,writable:!n}):e[t]=r:(i("next",0),i("throw",1),i("return",2))},x(e,t,r,n)}function C(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,o,i,a,u=[],c=!0,s=!1;try{if(i=(r=r.call(e)).next,0===t);else for(;!(c=(n=i.call(r)).done)&&(u.push(n.value),u.length!==t);c=!0);}catch(e){s=!0,o=e}finally{try{if(!c&&null!=r.return&&(a=r.return(),Object(a)!==a))return}finally{if(s)throw o}}return u}}(e,t)||function(e,t){if(e){if("string"==typeof e)return d(e,t);var r={}.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?d(e,t):void 0}}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function O(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t);if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:t+""}var P=function(){return y(function e(t){g(this,e),m(this,"app",void 0),m(this,"routesMap",{}),this.app=t},[{key:"check",value:function(e,t){return!!o.get(this.routesMap,"".concat(e,".").concat(t))}},{key:"add",value:function(e,t,r,n){this.updateMap(e,t,r,"added",n),this.app.route(e)[t](this.getProxyHandler(e,t))}},{key:"update",value:function(e,t,r,n){this.check(e,t)?this.updateMap(e,t,r,"updated",n):this.add(e,t,r,n)}},{key:"remove",value:function(e,t){this.updateMap(e,t,null,"removed")}},{key:"destroy",value:function(){this.routesMap={}}},{key:"updateMap",value:function(e,t,r){var n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:"added",i=arguments.length>4?arguments[4]:void 0;o.set(this.routesMap,"".concat(e,".").concat(t),{state:n,handler:r,handlerType:null==i?void 0:i.handlerType})}},{key:"getProxyHandler",value:function(e,t){var r=this;return function(){var n=v(j().m(function n(i,a){var u,c;return j().w(function(n){for(;;)switch(n.n){case 0:if(!(u=o.get(r.routesMap,"".concat(e,".").concat(t),{})).handler){n.n=3;break}if("REST"!==u.handlerType){n.n=2;break}return n.n=1,u.handler(i,a);case 1:return c=n.v,n.a(2,a.status(200).json(c));case 2:return n.a(2,u.handler(i,a));case 3:return n.a(2,a.status(404).send("".concat(t," ").concat(e," route not found")))}},n)}));return function(e,t){return n.apply(this,arguments)}}()}}])}(),S=function(){return y(function e(t,n){g(this,e),m(this,"app",void 0),m(this,"routeManager",void 0),m(this,"configCtx",void 0),m(this,"envParams",{scan:"./",workRootDir:r.getCwd()}),m(this,"serverConfig",{}),this.configCtx=t,this.app=n,this.routeManager=new P(n)},[{key:"updateEnvParams",value:function(e){this.envParams=w(w({},this.envParams),e)}},{key:"updateServerConfig",value:function(e){this.serverConfig=w(w({},this.serverConfig),e)}}])}(),k={".html":"text/html; charset=utf-8",".htm":"text/html; charset=utf-8",".css":"text/css; charset=utf-8",".js":"application/javascript; charset=utf-8",".mjs":"application/javascript; charset=utf-8",".json":"application/json; charset=utf-8",".png":"image/png",".jpg":"image/jpeg",".jpeg":"image/jpeg",".gif":"image/gif",".svg":"image/svg+xml",".ico":"image/x-icon",".webp":"image/webp",".mp4":"video/mp4",".webm":"video/webm",".woff":"font/woff",".woff2":"font/woff2",".ttf":"font/ttf",".otf":"font/otf",".txt":"text/plain; charset=utf-8",".xml":"application/xml; charset=utf-8",".pdf":"application/pdf"};var M=function(){return y(function e(t,r){g(this,e),this.directory=t,this.publicPath=r,m(this,"htmlManage",new c.HtmlManage(c.HtmlManage.parseHtml("\n <html>\n <head>\n <title>File List</title>\n <style>\n .file-list {\n list-style: none;\n padding: 0;\n margin: 0;\n }\n </style>\n </head>\n <body>\n <h1>File List</h1>\n </body>\n </html>\n")))},[{key:"handleRequest",value:(n=v(j().m(function r(n,o){var i,u,s;return j().w(function(r){for(;;)switch(r.p=r.n){case 0:if("/favicon.ico"!==(i=t.uri.getPathStr(n.url)||"")){r.n=1;break}return r.a(2,o.send("no file !!"));case 1:return i=(i=decodeURIComponent(i||"")).replace(this.publicPath,""),u=e.join(this.directory,i),o.setHeader("Access-Control-Allow-Origin","*"),o.setHeader("Access-Control-Allow-Headers","Content-Type"),o.setHeader("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS"),o.setHeader("Content-Type","text/html;charset=utf-8"),r.p=2,r.n=3,a.promises.stat(u);case 3:(s=r.v).isDirectory()?this.sendDir(u,n,o):this.sendFile(u,n,o,s),r.n=5;break;case 4:r.p=4,r.v,c.cLog.error("fs","not_found",i),o.send("File not found");case 5:return r.a(2)}},r,this,[[2,4]])})),function(e,t){return n.apply(this,arguments)})},{key:"sendDir",value:(r=v(j().m(function r(n,o,i){var u,c,s;return j().w(function(r){for(;;)switch(r.n){case 0:return r.n=1,a.promises.readdir(n);case 1:u=r.v,c=t.uri.getPathStr(o.url)||"",s=this.htmlManage.copy().addBody({tagName:"ul",attrs:[{name:"class",value:"file-list"}],children:u.map(function(t){return{tagName:"li",children:[{tagName:"a",attrs:[{name:"href",value:e.join(c,t)}],children:[t]}]}})}).generateHtml(),i.end(s);case 2:return r.a(2)}},r,this)})),function(e,t,n){return r.apply(this,arguments)})},{key:"sendFile",value:function(e,t,r,n){return this.cacheFile(t,r,e,n)?(r.statusCode=304,r.end()):(r.setHeader("Content-Type",(o=e,i=s.extname(o).toLowerCase(),(k[i]||"application/octet-stream")+";charset=utf-8")),(u=this.gzipFile(t,r))?a.createReadStream(e).pipe(u).pipe(r):void 0);var o,i,u}},{key:"cacheFile",value:function(e,t,r,n){t.setHeader("Cache-Control","max-age=10000"),t.setHeader("Expires",new Date(Date.now()+1e4).toUTCString());var o=n.ctime.toUTCString(),u=i.createHash("md5").update(a.readFileSync(r)).digest("base64");t.setHeader("Last-Modified",o),t.setHeader("Etag",u);var c=e.headers["if-modified-since"],s=e.headers["if-none-match"];return o===c&&s===u}},{key:"gzipFile",value:function(e,t){var r=e.headers["accept-encoding"];if(r){if(r.includes("gzip"))return t.setHeader("Content-Encoding","gzip"),u.createGzip();if(r.includes("deflage"))return t.setHeader("Content-Encoding","deflage"),u.createDeflate()}return!1}}]);var r,n}();exports.express=n,exports.Ctx=S,exports.RouteManager=P,exports.createServer=function(e){return n()},exports.envParamsMw=function(e){return function(t){return function(r){return v(j().m(function n(){return j().w(function(n){for(;;)if(0===n.n)return t.updateEnvParams(e),n.a(2,r())},n)}))}}},exports.fileServerMiddleware=function(e,r){var n=new M(r,e);return function(r,o,i){(t.uri.getPathStr(r.url)||"").startsWith(e)?n.handleRequest(r,o):i()}},exports.loggerMiddleware=function(e,t,r){c.cLog.log("ser","request","[".concat(e.method,"] ").concat(e.url)),r()},exports.proxyMiddleware=function(e){return function(r,n,i){var a=e.serverConfig.proxy,u=t.uri.getPathStr(r.url)||"",s=Object.keys(a||{}).find(function(e){return u.startsWith(e)});if(!s)return i();var d=a[s],h=d.router,v=d.headers,g=d.pathRewrite,y=o.isFunction(h)?h():h,m=u;if(g)for(var b=0,j=Object.entries(g);b<j.length;b++){var x=C(j[b],2),O=x[0],P=x[1];m=m.replace(new RegExp(O),P)}var S=new p.URL(m,y),k="https:"===S.protocol?l.request:f.request;c.cLog.log("ser","proxy","".concat(u," => ").concat(S));var M=k(S,{method:r.method,headers:w(w({},r.headers),v)},function(e){n.writeHead(e.statusCode||500,e.headers),e.pipe(n)});M.on("error",function(e){c.cLog.debug("ser","proxy",e),n.status(500).json({message:e.message,code:e.code})}),r.pipe(M)}},exports.serverCacheMw=function(n){return function(o){return function(i){return v(j().m(function a(){var u;return j().w(function(a){for(;;)switch(a.n){case 0:if(u=e.join(o.envParams.workRootDir,n||"./.cache/serverCache"),!r.isValidDirSync(u)){a.n=3;break}return a.n=1,r.cleanTargets(u);case 1:return a.n=2,t.sleep(300);case 2:a.n=4;break;case 3:r.mkdirRecursionSync(u);case 4:return o.updateServerConfig({cacheDir:u}),a.a(2,i())}},a)}))}}},exports.serverPlainMw=function(){return function(e){return function(t){return v(j().m(function r(){var n,o,i,a,u,c,s;return j().w(function(r){for(;;)if(0===r.n)return n=e.configCtx.getDoraConfig(),o=n.serviceConfig,a=(i=o).https,u=i.pathRouteMap,c=i.port,s=i.proxy,e.updateServerConfig({https:a,pathRouteMap:u,port:c,proxy:s,static:o.static}),r.a(2,t())},r)}))}}},exports.serverScanMw=function(e){return function(t){return function(r){return v(j().m(function n(){var o,i,a;return j().w(function(n){for(;;)if(0===n.n)return o=t.configCtx.getDoraConfig(),i=o.serviceConfig,a=i.scan,t.updateServerConfig({scan:e||a}),n.a(2,r())},n)}))}}};
|
|
3
|
+
var e,t,r="function"==typeof Symbol?Symbol:{},n=r.iterator||"@@iterator",o=r.toStringTag||"@@toStringTag";function i(r,n,o,i){var u=n&&n.prototype instanceof c?n:c,s=Object.create(u.prototype);return x(s,"_invoke",function(r,n,o){var i,c,u,s=0,f=o||[],l=!1,p={p:0,n:0,v:e,a:d,f:d.bind(e,4),d:function(t,r){return i=t,c=0,u=e,p.n=r,a}};function d(r,n){for(c=r,u=n,t=0;!l&&s&&!o&&t<f.length;t++){var o,i=f[t],d=p.p,h=i[2];r>3?(o=h===n)&&(u=i[(c=i[4])?5:(c=3,3)],i[4]=i[5]=e):i[0]<=d&&((o=r<2&&d<i[1])?(c=0,p.v=n,p.n=i[1]):d<h&&(o=r<3||i[0]>n||n>h)&&(i[4]=r,i[5]=n,p.n=h,c=0))}if(o||r>1)return a;throw l=!0,n}return function(o,f,h){if(s>1)throw TypeError("Generator is already running");for(l&&1===f&&d(f,h),c=f,u=h;(t=c<2?e:u)||!l;){i||(c?c<3?(c>1&&(p.n=-1),d(c,u)):p.n=u:p.v=u);try{if(s=2,i){if(c||(o="next"),t=i[o]){if(!(t=t.call(i,u)))throw TypeError("iterator result is not an object");if(!t.done)return t;u=t.value,c<2&&(c=0)}else 1===c&&(t=i.return)&&t.call(i),c<2&&(u=TypeError("The iterator does not provide a '"+o+"' method"),c=1);i=e}else if((t=(l=p.n<0)?u:r.call(n,p))!==a)break}catch(t){i=e,c=1,u=t}finally{s=1}}return{value:t,done:l}}}(r,o,i),!0),s}var a={};function c(){}function u(){}function s(){}t=Object.getPrototypeOf;var f=[][n]?t(t([][n]())):(x(t={},n,function(){return this}),t),l=s.prototype=c.prototype=Object.create(f);function p(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,s):(e.__proto__=s,x(e,o,"GeneratorFunction")),e.prototype=Object.create(l),e}return u.prototype=s,x(l,"constructor",s),x(s,"constructor",u),u.displayName="GeneratorFunction",x(s,o,"GeneratorFunction"),x(l),x(l,o,"Generator"),x(l,n,function(){return this}),x(l,"toString",function(){return"[object Generator]"}),(j=function(){return{w:i,m:p}})()}function x(e,t,r,n){var o=Object.defineProperty;try{o({},"",{})}catch(e){o=0}x=function(e,t,r,n){function i(t,r){x(e,t,function(e){return this._invoke(t,r,e)})}t?o?o(e,t,{value:r,enumerable:!n,configurable:!n,writable:!n}):e[t]=r:(i("next",0),i("throw",1),i("return",2))},x(e,t,r,n)}function C(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,o,i,a,c=[],u=!0,s=!1;try{if(i=(r=r.call(e)).next,0===t);else for(;!(u=(n=i.call(r)).done)&&(c.push(n.value),c.length!==t);u=!0);}catch(e){s=!0,o=e}finally{try{if(!u&&null!=r.return&&(a=r.return(),Object(a)!==a))return}finally{if(s)throw o}}return c}}(e,t)||function(e,t){if(e){if("string"==typeof e)return d(e,t);var r={}.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?d(e,t):void 0}}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function O(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t);if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:t+""}var P=function(){return y(function e(t){g(this,e),m(this,"app",void 0),m(this,"routesMap",{}),this.app=t},[{key:"check",value:function(e,t){return!!o.get(this.routesMap,"".concat(e,".").concat(t))}},{key:"add",value:function(e,t,r,n){this.updateMap(e,t,r,"added",n),this.app.route(e)[t](this.getProxyHandler(e,t))}},{key:"update",value:function(e,t,r,n){this.check(e,t)?this.updateMap(e,t,r,"updated",n):this.add(e,t,r,n)}},{key:"remove",value:function(e,t){this.updateMap(e,t,null,"removed")}},{key:"destroy",value:function(){this.routesMap={}}},{key:"updateMap",value:function(e,t,r){var n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:"added",i=arguments.length>4?arguments[4]:void 0;o.set(this.routesMap,"".concat(e,".").concat(t),{state:n,handler:r,handlerType:null==i?void 0:i.handlerType})}},{key:"getProxyHandler",value:function(e,t){var r=this;return function(){var n=v(j().m(function n(i,a){var c,u;return j().w(function(n){for(;;)switch(n.n){case 0:if(!(c=o.get(r.routesMap,"".concat(e,".").concat(t),{})).handler){n.n=3;break}if("REST"!==c.handlerType){n.n=2;break}return n.n=1,c.handler(i,a);case 1:return u=n.v,n.a(2,a.status(200).json(u));case 2:return n.a(2,c.handler(i,a));case 3:return n.a(2,a.status(404).send("".concat(t," ").concat(e," route not found")))}},n)}));return function(e,t){return n.apply(this,arguments)}}()}}])}(),S=function(){return y(function e(t,n){g(this,e),m(this,"app",void 0),m(this,"routeManager",void 0),m(this,"configCtx",void 0),m(this,"envParams",{scan:"./",workRootDir:r.getCwd()}),m(this,"serverConfig",{}),this.configCtx=t,this.app=n,this.routeManager=new P(n)},[{key:"updateEnvParams",value:function(e){this.envParams=w(w({},this.envParams),e)}},{key:"updateServerConfig",value:function(e){this.serverConfig=w(w({},this.serverConfig),e)}}])}(),k={".html":"text/html; charset=utf-8",".htm":"text/html; charset=utf-8",".css":"text/css; charset=utf-8",".js":"application/javascript; charset=utf-8",".mjs":"application/javascript; charset=utf-8",".json":"application/json; charset=utf-8",".png":"image/png",".jpg":"image/jpeg",".jpeg":"image/jpeg",".gif":"image/gif",".svg":"image/svg+xml",".ico":"image/x-icon",".webp":"image/webp",".mp4":"video/mp4",".webm":"video/webm",".woff":"font/woff",".woff2":"font/woff2",".ttf":"font/ttf",".otf":"font/otf",".txt":"text/plain; charset=utf-8",".xml":"application/xml; charset=utf-8",".pdf":"application/pdf"};var T=function(){return y(function e(t,r){g(this,e),this.directory=t,this.publicPath=r,m(this,"htmlManage",new i.HtmlManage(i.HtmlManage.parseHtml("\n <html>\n <head>\n <title>File List</title>\n <style>\n .file-list {\n list-style: none;\n padding: 0;\n margin: 0;\n }\n </style>\n </head>\n <body>\n <h1>File List</h1>\n </body>\n </html>\n")))},[{key:"handleRequest",value:(n=v(j().m(function r(n,o){var a,u,s;return j().w(function(r){for(;;)switch(r.p=r.n){case 0:if("/favicon.ico"!==(a=t.uri.getPathStr(n.url)||"")){r.n=1;break}return r.a(2,o.send("no file !!"));case 1:return a=(a=decodeURIComponent(a||"")).replace(this.publicPath,""),u=e.join(this.directory,a),o.setHeader("Access-Control-Allow-Origin","*"),o.setHeader("Access-Control-Allow-Headers","Content-Type"),o.setHeader("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS"),o.setHeader("Content-Type","text/html;charset=utf-8"),r.p=2,r.n=3,c.promises.stat(u);case 3:(s=r.v).isDirectory()?this.sendDir(u,n,o):this.sendFile(u,n,o,s),r.n=5;break;case 4:r.p=4,r.v,i.cLog.error("fs","not_found",a),o.send("File not found");case 5:return r.a(2)}},r,this,[[2,4]])})),function(e,t){return n.apply(this,arguments)})},{key:"sendDir",value:(r=v(j().m(function r(n,o,i){var a,u,s;return j().w(function(r){for(;;)switch(r.n){case 0:return r.n=1,c.promises.readdir(n);case 1:a=r.v,u=t.uri.getPathStr(o.url)||"",s=this.htmlManage.copy().addBody({tagName:"ul",attrs:[{name:"class",value:"file-list"}],children:a.map(function(t){return{tagName:"li",children:[{tagName:"a",attrs:[{name:"href",value:e.join(u,t)}],children:[t]}]}})}).generateHtml(),i.end(s);case 2:return r.a(2)}},r,this)})),function(e,t,n){return r.apply(this,arguments)})},{key:"sendFile",value:function(e,t,r,n){return this.cacheFile(t,r,e,n)?(r.statusCode=304,r.end()):(r.setHeader("Content-Type",(o=e,i=s.extname(o).toLowerCase(),(k[i]||"application/octet-stream")+";charset=utf-8")),(a=this.gzipFile(t,r))?c.createReadStream(e).pipe(a).pipe(r):void 0);var o,i,a}},{key:"cacheFile",value:function(e,t,r,n){t.setHeader("Cache-Control","max-age=10000"),t.setHeader("Expires",new Date(Date.now()+1e4).toUTCString());var o=n.ctime.toUTCString(),i=a.createHash("md5").update(c.readFileSync(r)).digest("base64");t.setHeader("Last-Modified",o),t.setHeader("Etag",i);var u=e.headers["if-modified-since"],s=e.headers["if-none-match"];return o===u&&s===i}},{key:"gzipFile",value:function(e,t){var r=e.headers["accept-encoding"];if(r){if(r.includes("gzip"))return t.setHeader("Content-Encoding","gzip"),u.createGzip();if(r.includes("deflage"))return t.setHeader("Content-Encoding","deflage"),u.createDeflate()}return!1}}]);var r,n}();exports.express=n,exports.Ctx=S,exports.RouteManager=P,exports.corsMiddleware=function(e,t,r){t.setHeader("Access-Control-Allow-Origin","*"),t.setHeader("Access-Control-Allow-Methods","GET, POST, PUT, DELETE, OPTIONS"),t.setHeader("Access-Control-Allow-Headers","Content-Type, Authorization"),t.setHeader("Access-Control-Allow-Credentials","true"),"OPTIONS"!==e.method?r():t.sendStatus(200)},exports.createServer=function(e){return n()},exports.envParamsMw=function(e){return function(t){return function(r){return v(j().m(function n(){return j().w(function(n){for(;;)if(0===n.n)return t.updateEnvParams(e),n.a(2,r())},n)}))}}},exports.fileServerMiddleware=function(n,o){var a=new T(o,n);return i.cLog.info("fs","added","".concat(n," => ").concat(e.relative(r.getCwd(),o))),function(e,r,o){(t.uriGetPathStr(e.url)||"").startsWith(n)?a.handleRequest(e,r):o()}},exports.loggerMiddleware=function(e,t,r){i.cLog.log("ser","request","[".concat(e.method,"] ").concat(e.url)),r()},exports.proxyMiddleware=function(e){return function(r,n,a){var c=e.serverConfig.proxy,u=t.uri.getPathStr(r.url)||"",s=Object.keys(c||{}).find(function(e){return u.startsWith(e)});if(!s)return a();var d=c[s],h=d.router,v=d.headers,g=d.pathRewrite,y=o.isFunction(h)?h():h,m=u;if(g)for(var b=0,j=Object.entries(g);b<j.length;b++){var x=C(j[b],2),O=x[0],P=x[1];m=m.replace(new RegExp(O),P)}var S=new p.URL(m,y),k="https:"===S.protocol?l.request:f.request;i.cLog.log("ser","proxy","".concat(u," => ").concat(S));var T=k(S,{method:r.method,headers:w(w({},r.headers),v)},function(e){n.writeHead(e.statusCode||500,e.headers),e.pipe(n)});T.on("error",function(e){i.cLog.debug("ser","proxy",e),n.status(500).json({message:e.message,code:e.code})}),r.pipe(T)}},exports.serverCacheMw=function(n){return function(o){return function(i){return v(j().m(function a(){var c;return j().w(function(a){for(;;)switch(a.n){case 0:if(c=e.join(o.envParams.workRootDir,n||"./.cache/serverCache"),!r.isValidDirSync(c)){a.n=3;break}return a.n=1,r.cleanTargets(c);case 1:return a.n=2,t.sleep(300);case 2:a.n=4;break;case 3:r.mkdirRecursionSync(c);case 4:return o.updateServerConfig({cacheDir:c}),a.a(2,i())}},a)}))}}},exports.serverPlainMw=function(){return function(e){return function(t){return v(j().m(function r(){var n,o,i,a,c,u,s;return j().w(function(r){for(;;)if(0===r.n)return n=e.configCtx.getDoraConfig(),o=n.serviceConfig,a=(i=o).https,c=i.pathRouteMap,u=i.port,s=i.proxy,e.updateServerConfig({https:a,pathRouteMap:c,port:u,proxy:s,static:o.static}),r.a(2,t())},r)}))}}},exports.serverScanMw=function(e){return function(t){return function(r){return v(j().m(function n(){var o,i,a;return j().w(function(n){for(;;)if(0===n.n)return o=t.configCtx.getDoraConfig(),i=o.serviceConfig,a=i.scan,t.updateServerConfig({scan:e||a}),n.a(2,r())},n)}))}}};
|
package/lib/esm/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import t,{join as e}from"path";import{sleep as
|
|
1
|
+
import t,{join as e,relative as r}from"path";import{sleep as n,uri as o,uriGetPathStr as i}from"@cclr/utils";import{mkdirRecursionSync as a,isValidDirSync as c,cleanTargets as u,getCwd as s}from"@dorabag/file-pro";import f from"express";export{default as express}from"express";import{get as l,set as p,isFunction as d}from"@cclr/lang";import{cLog as h,HtmlManage as v}from"@doracli/helper";import m from"crypto";import{createReadStream as y,readFileSync as g,promises as b}from"fs";import w from"zlib";import j from"node:path";import{request as O}from"http";import{request as C}from"https";import{URL as P}from"url";function x(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=Array(e);r<e;r++)n[r]=t[r];return n}function k(t,e,r,n,o,i,a){try{var c=t[i](a),u=c.value}catch(t){return void r(t)}c.done?e(u):Promise.resolve(u).then(n,o)}function S(t){return function(){var e=this,r=arguments;return new Promise(function(n,o){var i=t.apply(e,r);function a(t){k(i,n,o,a,c,"next",t)}function c(t){k(i,n,o,a,c,"throw",t)}a(void 0)})}}function T(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function E(t,e,r){return e&&function(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,G(n.key),n)}}(t.prototype,e),Object.defineProperty(t,"prototype",{writable:!1}),t}function H(t,e,r){return(e=G(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function A(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})),r.push.apply(r,n)}return r}function M(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?A(Object(r),!0).forEach(function(e){H(t,e,r[e])}):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):A(Object(r)).forEach(function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))})}return t}function D(){
|
|
2
2
|
/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */
|
|
3
|
-
var t,e,r="function"==typeof Symbol?Symbol:{},n=r.iterator||"@@iterator",o=r.toStringTag||"@@toStringTag";function i(r,n,o,i){var u=n&&n.prototype instanceof c?n:c,s=Object.create(u.prototype);return D(s,"_invoke",function(r,n,o){var i,c,u,s=0,f=o||[],l=!1,p={p:0,n:0,v:t,a:h,f:h.bind(t,4),d:function(e,r){return i=e,c=0,u=t,p.n=r,a}};function h(r,n){for(c=r,u=n,e=0;!l&&s&&!o&&e<f.length;e++){var o,i=f[e],h=p.p,d=i[2];r>3?(o=d===n)&&(u=i[(c=i[4])?5:(c=3,3)],i[4]=i[5]=t):i[0]<=h&&((o=r<2&&h<i[1])?(c=0,p.v=n,p.n=i[1]):h<d&&(o=r<3||i[0]>n||n>d)&&(i[4]=r,i[5]=n,p.n=d,c=0))}if(o||r>1)return a;throw l=!0,n}return function(o,f,d){if(s>1)throw TypeError("Generator is already running");for(l&&1===f&&h(f,d),c=f,u=d;(e=c<2?t:u)||!l;){i||(c?c<3?(c>1&&(p.n=-1),h(c,u)):p.n=u:p.v=u);try{if(s=2,i){if(c||(o="next"),e=i[o]){if(!(e=e.call(i,u)))throw TypeError("iterator result is not an object");if(!e.done)return e;u=e.value,c<2&&(c=0)}else 1===c&&(e=i.return)&&e.call(i),c<2&&(u=TypeError("The iterator does not provide a '"+o+"' method"),c=1);i=t}else if((e=(l=p.n<0)?u:r.call(n,p))!==a)break}catch(e){i=t,c=1,u=e}finally{s=1}}return{value:e,done:l}}}(r,o,i),!0),s}var a={};function c(){}function u(){}function s(){}e=Object.getPrototypeOf;var f=[][n]?e(e([][n]())):(D(e={},n,function(){return this}),e),l=s.prototype=c.prototype=Object.create(f);function p(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,s):(t.__proto__=s,D(t,o,"GeneratorFunction")),t.prototype=Object.create(l),t}return u.prototype=s,D(l,"constructor",s),D(s,"constructor",u),u.displayName="GeneratorFunction",D(s,o,"GeneratorFunction"),D(l),D(l,o,"Generator"),D(l,n,function(){return this}),D(l,"toString",function(){return"[object Generator]"}),(M=function(){return{w:i,m:p}})()}function D(t,e,r,n){var o=Object.defineProperty;try{o({},"",{})}catch(t){o=0}D=function(t,e,r,n){function i(e,r){D(t,e,function(t){return this._invoke(e,r,t)})}e?o?o(t,e,{value:r,enumerable:!n,configurable:!n,writable:!n}):t[e]=r:(i("next",0),i("throw",1),i("return",2))},D(t,e,r,n)}function A(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var r=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=r){var n,o,i,a,c=[],u=!0,s=!1;try{if(i=(r=r.call(t)).next,0===e);else for(;!(u=(n=i.call(r)).done)&&(c.push(n.value),c.length!==e);u=!0);}catch(t){s=!0,o=t}finally{try{if(!u&&null!=r.return&&(a=r.return(),Object(a)!==a))return}finally{if(s)throw o}}return c}}(t,e)||function(t,e){if(t){if("string"==typeof t)return P(t,e);var r={}.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?P(t,e):void 0}}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function F(t){var e=function(t,e){if("object"!=typeof t||!t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var n=r.call(t,e);if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:e+""}var R=function(t){return function(n){return function(c){return x(M().m(function u(){var s;return M().w(function(u){for(;;)switch(u.n){case 0:if(s=e(n.envParams.workRootDir,t||"./.cache/serverCache"),!i(s)){u.n=3;break}return u.n=1,a(s);case 1:return u.n=2,r(300);case 2:u.n=4;break;case 3:o(s);case 4:return n.updateServerConfig({cacheDir:s}),u.a(2,c())}},u)}))}}},G=function(t){return function(e){return function(r){return x(M().m(function n(){return M().w(function(n){for(;;)if(0===n.n)return e.updateEnvParams(t),n.a(2,r())},n)}))}}},_=function(){return function(t){return function(e){return x(M().m(function r(){var n,o,i,a,c,u,s;return M().w(function(r){for(;;)if(0===r.n)return n=t.configCtx.getDoraConfig(),o=n.serviceConfig,a=(i=o).https,c=i.pathRouteMap,u=i.port,s=i.proxy,t.updateServerConfig({https:a,pathRouteMap:c,port:u,proxy:s,static:o.static}),r.a(2,e())},r)}))}}},z=function(t){return function(e){return function(r){return x(M().m(function n(){var o,i,a;return M().w(function(n){for(;;)if(0===n.n)return o=e.configCtx.getDoraConfig(),i=o.serviceConfig,a=i.scan,e.updateServerConfig({scan:t||a}),n.a(2,r())},n)}))}}},N=function(t){return u()},I=function(){return S(function t(e){k(this,t),T(this,"app",void 0),T(this,"routesMap",{}),this.app=e},[{key:"check",value:function(t,e){return!!s(this.routesMap,"".concat(t,".").concat(e))}},{key:"add",value:function(t,e,r,n){this.updateMap(t,e,r,"added",n),this.app.route(t)[e](this.getProxyHandler(t,e))}},{key:"update",value:function(t,e,r,n){this.check(t,e)?this.updateMap(t,e,r,"updated",n):this.add(t,e,r,n)}},{key:"remove",value:function(t,e){this.updateMap(t,e,null,"removed")}},{key:"destroy",value:function(){this.routesMap={}}},{key:"updateMap",value:function(t,e,r){var n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:"added",o=arguments.length>4?arguments[4]:void 0;f(this.routesMap,"".concat(t,".").concat(e),{state:n,handler:r,handlerType:null==o?void 0:o.handlerType})}},{key:"getProxyHandler",value:function(t,e){var r=this;return function(){var n=x(M().m(function n(o,i){var a,c;return M().w(function(n){for(;;)switch(n.n){case 0:if(!(a=s(r.routesMap,"".concat(t,".").concat(e),{})).handler){n.n=3;break}if("REST"!==a.handlerType){n.n=2;break}return n.n=1,a.handler(o,i);case 1:return c=n.v,n.a(2,i.status(200).json(c));case 2:return n.a(2,a.handler(o,i));case 3:return n.a(2,i.status(404).send("".concat(e," ").concat(t," route not found")))}},n)}));return function(t,e){return n.apply(this,arguments)}}()}}])}(),L=function(){return S(function t(e,r){k(this,t),T(this,"app",void 0),T(this,"routeManager",void 0),T(this,"configCtx",void 0),T(this,"envParams",{scan:"./",workRootDir:c()}),T(this,"serverConfig",{}),this.configCtx=e,this.app=r,this.routeManager=new I(r)},[{key:"updateEnvParams",value:function(t){this.envParams=H(H({},this.envParams),t)}},{key:"updateServerConfig",value:function(t){this.serverConfig=H(H({},this.serverConfig),t)}}])}(),U={".html":"text/html; charset=utf-8",".htm":"text/html; charset=utf-8",".css":"text/css; charset=utf-8",".js":"application/javascript; charset=utf-8",".mjs":"application/javascript; charset=utf-8",".json":"application/json; charset=utf-8",".png":"image/png",".jpg":"image/jpeg",".jpeg":"image/jpeg",".gif":"image/gif",".svg":"image/svg+xml",".ico":"image/x-icon",".webp":"image/webp",".mp4":"video/mp4",".webm":"video/webm",".woff":"font/woff",".woff2":"font/woff2",".ttf":"font/ttf",".otf":"font/otf",".txt":"text/plain; charset=utf-8",".xml":"application/xml; charset=utf-8",".pdf":"application/pdf"};var q=function(){return S(function t(e,r){k(this,t),this.directory=e,this.publicPath=r,T(this,"htmlManage",new g(g.parseHtml("\n <html>\n <head>\n <title>File List</title>\n <style>\n .file-list {\n list-style: none;\n padding: 0;\n margin: 0;\n }\n </style>\n </head>\n <body>\n <h1>File List</h1>\n </body>\n </html>\n")))},[{key:"handleRequest",value:(r=x(M().m(function e(r,o){var i,a,c;return M().w(function(e){for(;;)switch(e.p=e.n){case 0:if("/favicon.ico"!==(i=n.getPathStr(r.url)||"")){e.n=1;break}return e.a(2,o.send("no file !!"));case 1:return i=(i=decodeURIComponent(i||"")).replace(this.publicPath,""),a=t.join(this.directory,i),o.setHeader("Access-Control-Allow-Origin","*"),o.setHeader("Access-Control-Allow-Headers","Content-Type"),o.setHeader("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS"),o.setHeader("Content-Type","text/html;charset=utf-8"),e.p=2,e.n=3,v.stat(a);case 3:(c=e.v).isDirectory()?this.sendDir(a,r,o):this.sendFile(a,r,o,c),e.n=5;break;case 4:e.p=4,e.v,y.error("fs","not_found",i),o.send("File not found");case 5:return e.a(2)}},e,this,[[2,4]])})),function(t,e){return r.apply(this,arguments)})},{key:"sendDir",value:(e=x(M().m(function e(r,o,i){var a,c,u;return M().w(function(e){for(;;)switch(e.n){case 0:return e.n=1,v.readdir(r);case 1:a=e.v,c=n.getPathStr(o.url)||"",u=this.htmlManage.copy().addBody({tagName:"ul",attrs:[{name:"class",value:"file-list"}],children:a.map(function(e){return{tagName:"li",children:[{tagName:"a",attrs:[{name:"href",value:t.join(c,e)}],children:[e]}]}})}).generateHtml(),i.end(u);case 2:return e.a(2)}},e,this)})),function(t,r,n){return e.apply(this,arguments)})},{key:"sendFile",value:function(t,e,r,n){return this.cacheFile(e,r,t,n)?(r.statusCode=304,r.end()):(r.setHeader("Content-Type",(o=t,i=b.extname(o).toLowerCase(),(U[i]||"application/octet-stream")+";charset=utf-8")),(a=this.gzipFile(e,r))?h(t).pipe(a).pipe(r):void 0);var o,i,a}},{key:"cacheFile",value:function(t,e,r,n){e.setHeader("Cache-Control","max-age=10000"),e.setHeader("Expires",new Date(Date.now()+1e4).toUTCString());var o=n.ctime.toUTCString(),i=p.createHash("md5").update(d(r)).digest("base64");e.setHeader("Last-Modified",o),e.setHeader("Etag",i);var a=t.headers["if-modified-since"],c=t.headers["if-none-match"];return o===a&&c===i}},{key:"gzipFile",value:function(t,e){var r=t.headers["accept-encoding"];if(r){if(r.includes("gzip"))return e.setHeader("Content-Encoding","gzip"),m.createGzip();if(r.includes("deflage"))return e.setHeader("Content-Encoding","deflage"),m.createDeflate()}return!1}}]);var e,r}(),W=function(t,e){var r=new q(e,t);return function(e,o,i){(n.getPathStr(e.url)||"").startsWith(t)?r.handleRequest(e,o):i()}},B=function(t,e,r){y.log("ser","request","[".concat(t.method,"] ").concat(t.url)),r()},$=function(t){return function(e,r,o){var i=t.serverConfig.proxy,a=n.getPathStr(e.url)||"",c=Object.keys(i||{}).find(function(t){return a.startsWith(t)});if(!c)return o();var u=i[c],s=u.router,f=u.headers,p=u.pathRewrite,h=l(s)?s():s,d=a;if(p)for(var v=0,m=Object.entries(p);v<m.length;v++){var g=A(m[v],2),b=g[0],P=g[1];d=d.replace(new RegExp(b),P)}var C=new O(d,h),x="https:"===C.protocol?j:w;y.log("ser","proxy","".concat(a," => ").concat(C));var k=x(C,{method:e.method,headers:H(H({},e.headers),f)},function(t){r.writeHead(t.statusCode||500,t.headers),t.pipe(r)});k.on("error",function(t){y.debug("ser","proxy",t),r.status(500).json({message:t.message,code:t.code})}),e.pipe(k)}};export{L as Ctx,I as RouteManager,N as createServer,G as envParamsMw,W as fileServerMiddleware,B as loggerMiddleware,$ as proxyMiddleware,R as serverCacheMw,_ as serverPlainMw,z as serverScanMw};
|
|
3
|
+
var t,e,r="function"==typeof Symbol?Symbol:{},n=r.iterator||"@@iterator",o=r.toStringTag||"@@toStringTag";function i(r,n,o,i){var u=n&&n.prototype instanceof c?n:c,s=Object.create(u.prototype);return F(s,"_invoke",function(r,n,o){var i,c,u,s=0,f=o||[],l=!1,p={p:0,n:0,v:t,a:d,f:d.bind(t,4),d:function(e,r){return i=e,c=0,u=t,p.n=r,a}};function d(r,n){for(c=r,u=n,e=0;!l&&s&&!o&&e<f.length;e++){var o,i=f[e],d=p.p,h=i[2];r>3?(o=h===n)&&(u=i[(c=i[4])?5:(c=3,3)],i[4]=i[5]=t):i[0]<=d&&((o=r<2&&d<i[1])?(c=0,p.v=n,p.n=i[1]):d<h&&(o=r<3||i[0]>n||n>h)&&(i[4]=r,i[5]=n,p.n=h,c=0))}if(o||r>1)return a;throw l=!0,n}return function(o,f,h){if(s>1)throw TypeError("Generator is already running");for(l&&1===f&&d(f,h),c=f,u=h;(e=c<2?t:u)||!l;){i||(c?c<3?(c>1&&(p.n=-1),d(c,u)):p.n=u:p.v=u);try{if(s=2,i){if(c||(o="next"),e=i[o]){if(!(e=e.call(i,u)))throw TypeError("iterator result is not an object");if(!e.done)return e;u=e.value,c<2&&(c=0)}else 1===c&&(e=i.return)&&e.call(i),c<2&&(u=TypeError("The iterator does not provide a '"+o+"' method"),c=1);i=t}else if((e=(l=p.n<0)?u:r.call(n,p))!==a)break}catch(e){i=t,c=1,u=e}finally{s=1}}return{value:e,done:l}}}(r,o,i),!0),s}var a={};function c(){}function u(){}function s(){}e=Object.getPrototypeOf;var f=[][n]?e(e([][n]())):(F(e={},n,function(){return this}),e),l=s.prototype=c.prototype=Object.create(f);function p(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,s):(t.__proto__=s,F(t,o,"GeneratorFunction")),t.prototype=Object.create(l),t}return u.prototype=s,F(l,"constructor",s),F(s,"constructor",u),u.displayName="GeneratorFunction",F(s,o,"GeneratorFunction"),F(l),F(l,o,"Generator"),F(l,n,function(){return this}),F(l,"toString",function(){return"[object Generator]"}),(D=function(){return{w:i,m:p}})()}function F(t,e,r,n){var o=Object.defineProperty;try{o({},"",{})}catch(t){o=0}F=function(t,e,r,n){function i(e,r){F(t,e,function(t){return this._invoke(e,r,t)})}e?o?o(t,e,{value:r,enumerable:!n,configurable:!n,writable:!n}):t[e]=r:(i("next",0),i("throw",1),i("return",2))},F(t,e,r,n)}function R(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var r=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=r){var n,o,i,a,c=[],u=!0,s=!1;try{if(i=(r=r.call(t)).next,0===e);else for(;!(u=(n=i.call(r)).done)&&(c.push(n.value),c.length!==e);u=!0);}catch(t){s=!0,o=t}finally{try{if(!u&&null!=r.return&&(a=r.return(),Object(a)!==a))return}finally{if(s)throw o}}return c}}(t,e)||function(t,e){if(t){if("string"==typeof t)return x(t,e);var r={}.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?x(t,e):void 0}}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function G(t){var e=function(t,e){if("object"!=typeof t||!t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var n=r.call(t,e);if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:e+""}var N=function(t){return function(r){return function(o){return S(D().m(function i(){var s;return D().w(function(i){for(;;)switch(i.n){case 0:if(s=e(r.envParams.workRootDir,t||"./.cache/serverCache"),!c(s)){i.n=3;break}return i.n=1,u(s);case 1:return i.n=2,n(300);case 2:i.n=4;break;case 3:a(s);case 4:return r.updateServerConfig({cacheDir:s}),i.a(2,o())}},i)}))}}},z=function(t){return function(e){return function(r){return S(D().m(function n(){return D().w(function(n){for(;;)if(0===n.n)return e.updateEnvParams(t),n.a(2,r())},n)}))}}},I=function(){return function(t){return function(e){return S(D().m(function r(){var n,o,i,a,c,u,s;return D().w(function(r){for(;;)if(0===r.n)return n=t.configCtx.getDoraConfig(),o=n.serviceConfig,a=(i=o).https,c=i.pathRouteMap,u=i.port,s=i.proxy,t.updateServerConfig({https:a,pathRouteMap:c,port:u,proxy:s,static:o.static}),r.a(2,e())},r)}))}}},_=function(t){return function(e){return function(r){return S(D().m(function n(){var o,i,a;return D().w(function(n){for(;;)if(0===n.n)return o=e.configCtx.getDoraConfig(),i=o.serviceConfig,a=i.scan,e.updateServerConfig({scan:t||a}),n.a(2,r())},n)}))}}},L=function(t){return f()},U=function(){return E(function t(e){T(this,t),H(this,"app",void 0),H(this,"routesMap",{}),this.app=e},[{key:"check",value:function(t,e){return!!l(this.routesMap,"".concat(t,".").concat(e))}},{key:"add",value:function(t,e,r,n){this.updateMap(t,e,r,"added",n),this.app.route(t)[e](this.getProxyHandler(t,e))}},{key:"update",value:function(t,e,r,n){this.check(t,e)?this.updateMap(t,e,r,"updated",n):this.add(t,e,r,n)}},{key:"remove",value:function(t,e){this.updateMap(t,e,null,"removed")}},{key:"destroy",value:function(){this.routesMap={}}},{key:"updateMap",value:function(t,e,r){var n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:"added",o=arguments.length>4?arguments[4]:void 0;p(this.routesMap,"".concat(t,".").concat(e),{state:n,handler:r,handlerType:null==o?void 0:o.handlerType})}},{key:"getProxyHandler",value:function(t,e){var r=this;return function(){var n=S(D().m(function n(o,i){var a,c;return D().w(function(n){for(;;)switch(n.n){case 0:if(!(a=l(r.routesMap,"".concat(t,".").concat(e),{})).handler){n.n=3;break}if("REST"!==a.handlerType){n.n=2;break}return n.n=1,a.handler(o,i);case 1:return c=n.v,n.a(2,i.status(200).json(c));case 2:return n.a(2,a.handler(o,i));case 3:return n.a(2,i.status(404).send("".concat(e," ").concat(t," route not found")))}},n)}));return function(t,e){return n.apply(this,arguments)}}()}}])}(),q=function(){return E(function t(e,r){T(this,t),H(this,"app",void 0),H(this,"routeManager",void 0),H(this,"configCtx",void 0),H(this,"envParams",{scan:"./",workRootDir:s()}),H(this,"serverConfig",{}),this.configCtx=e,this.app=r,this.routeManager=new U(r)},[{key:"updateEnvParams",value:function(t){this.envParams=M(M({},this.envParams),t)}},{key:"updateServerConfig",value:function(t){this.serverConfig=M(M({},this.serverConfig),t)}}])}(),W=function(t,e,r){e.setHeader("Access-Control-Allow-Origin","*"),e.setHeader("Access-Control-Allow-Methods","GET, POST, PUT, DELETE, OPTIONS"),e.setHeader("Access-Control-Allow-Headers","Content-Type, Authorization"),e.setHeader("Access-Control-Allow-Credentials","true"),"OPTIONS"!==t.method?r():e.sendStatus(200)},B={".html":"text/html; charset=utf-8",".htm":"text/html; charset=utf-8",".css":"text/css; charset=utf-8",".js":"application/javascript; charset=utf-8",".mjs":"application/javascript; charset=utf-8",".json":"application/json; charset=utf-8",".png":"image/png",".jpg":"image/jpeg",".jpeg":"image/jpeg",".gif":"image/gif",".svg":"image/svg+xml",".ico":"image/x-icon",".webp":"image/webp",".mp4":"video/mp4",".webm":"video/webm",".woff":"font/woff",".woff2":"font/woff2",".ttf":"font/ttf",".otf":"font/otf",".txt":"text/plain; charset=utf-8",".xml":"application/xml; charset=utf-8",".pdf":"application/pdf"};var $=function(){return E(function t(e,r){T(this,t),this.directory=e,this.publicPath=r,H(this,"htmlManage",new v(v.parseHtml("\n <html>\n <head>\n <title>File List</title>\n <style>\n .file-list {\n list-style: none;\n padding: 0;\n margin: 0;\n }\n </style>\n </head>\n <body>\n <h1>File List</h1>\n </body>\n </html>\n")))},[{key:"handleRequest",value:(r=S(D().m(function e(r,n){var i,a,c;return D().w(function(e){for(;;)switch(e.p=e.n){case 0:if("/favicon.ico"!==(i=o.getPathStr(r.url)||"")){e.n=1;break}return e.a(2,n.send("no file !!"));case 1:return i=(i=decodeURIComponent(i||"")).replace(this.publicPath,""),a=t.join(this.directory,i),n.setHeader("Access-Control-Allow-Origin","*"),n.setHeader("Access-Control-Allow-Headers","Content-Type"),n.setHeader("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS"),n.setHeader("Content-Type","text/html;charset=utf-8"),e.p=2,e.n=3,b.stat(a);case 3:(c=e.v).isDirectory()?this.sendDir(a,r,n):this.sendFile(a,r,n,c),e.n=5;break;case 4:e.p=4,e.v,h.error("fs","not_found",i),n.send("File not found");case 5:return e.a(2)}},e,this,[[2,4]])})),function(t,e){return r.apply(this,arguments)})},{key:"sendDir",value:(e=S(D().m(function e(r,n,i){var a,c,u;return D().w(function(e){for(;;)switch(e.n){case 0:return e.n=1,b.readdir(r);case 1:a=e.v,c=o.getPathStr(n.url)||"",u=this.htmlManage.copy().addBody({tagName:"ul",attrs:[{name:"class",value:"file-list"}],children:a.map(function(e){return{tagName:"li",children:[{tagName:"a",attrs:[{name:"href",value:t.join(c,e)}],children:[e]}]}})}).generateHtml(),i.end(u);case 2:return e.a(2)}},e,this)})),function(t,r,n){return e.apply(this,arguments)})},{key:"sendFile",value:function(t,e,r,n){return this.cacheFile(e,r,t,n)?(r.statusCode=304,r.end()):(r.setHeader("Content-Type",(o=t,i=j.extname(o).toLowerCase(),(B[i]||"application/octet-stream")+";charset=utf-8")),(a=this.gzipFile(e,r))?y(t).pipe(a).pipe(r):void 0);var o,i,a}},{key:"cacheFile",value:function(t,e,r,n){e.setHeader("Cache-Control","max-age=10000"),e.setHeader("Expires",new Date(Date.now()+1e4).toUTCString());var o=n.ctime.toUTCString(),i=m.createHash("md5").update(g(r)).digest("base64");e.setHeader("Last-Modified",o),e.setHeader("Etag",i);var a=t.headers["if-modified-since"],c=t.headers["if-none-match"];return o===a&&c===i}},{key:"gzipFile",value:function(t,e){var r=t.headers["accept-encoding"];if(r){if(r.includes("gzip"))return e.setHeader("Content-Encoding","gzip"),w.createGzip();if(r.includes("deflage"))return e.setHeader("Content-Encoding","deflage"),w.createDeflate()}return!1}}]);var e,r}(),J=function(t,e){var n=new $(e,t);return h.info("fs","added","".concat(t," => ").concat(r(s(),e))),function(e,r,o){(i(e.url)||"").startsWith(t)?n.handleRequest(e,r):o()}},K=function(t,e,r){h.log("ser","request","[".concat(t.method,"] ").concat(t.url)),r()},Q=function(t){return function(e,r,n){var i=t.serverConfig.proxy,a=o.getPathStr(e.url)||"",c=Object.keys(i||{}).find(function(t){return a.startsWith(t)});if(!c)return n();var u=i[c],s=u.router,f=u.headers,l=u.pathRewrite,p=d(s)?s():s,v=a;if(l)for(var m=0,y=Object.entries(l);m<y.length;m++){var g=R(y[m],2),b=g[0],w=g[1];v=v.replace(new RegExp(b),w)}var j=new P(v,p),x="https:"===j.protocol?C:O;h.log("ser","proxy","".concat(a," => ").concat(j));var k=x(j,{method:e.method,headers:M(M({},e.headers),f)},function(t){r.writeHead(t.statusCode||500,t.headers),t.pipe(r)});k.on("error",function(t){h.debug("ser","proxy",t),r.status(500).json({message:t.message,code:t.code})}),e.pipe(k)}};export{q as Ctx,U as RouteManager,W as corsMiddleware,L as createServer,z as envParamsMw,J as fileServerMiddleware,K as loggerMiddleware,Q as proxyMiddleware,N as serverCacheMw,I as serverPlainMw,_ as serverScanMw};
|
package/lib/type/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { TOnionFun } from '@cclr/utils';
|
|
2
|
-
import { Application, Request, Response } from 'express';
|
|
2
|
+
import { Application, Request as Request$1, Response as Response$1 } from 'express';
|
|
3
3
|
export { Application, Request, Response, default as express } from 'express';
|
|
4
4
|
import { ConfigCtx } from '@doracli/helper';
|
|
5
5
|
import { TDoraConfigService } from '@doracli/type';
|
|
6
|
-
import { TControllerType, TControllerFun, TMethod, Request
|
|
6
|
+
import { TControllerType, TControllerFun, TMethod, Request, Response } from '@doracli/define-server';
|
|
7
7
|
import { TNoop } from '@cclr/lang';
|
|
8
8
|
|
|
9
9
|
type TEnvParams = {
|
|
@@ -61,11 +61,13 @@ declare const createServer: (config: {
|
|
|
61
61
|
port?: number;
|
|
62
62
|
}) => Application;
|
|
63
63
|
|
|
64
|
-
declare const
|
|
64
|
+
declare const corsMiddleware: (req: Request, res: Response, next: TNoop) => void;
|
|
65
65
|
|
|
66
|
-
declare const
|
|
66
|
+
declare const fileServerMiddleware: (route: string, directory: string) => (req: Request$1, res: Response$1, next: TNoop) => void;
|
|
67
67
|
|
|
68
|
-
declare const
|
|
68
|
+
declare const loggerMiddleware: (req: Request, res: Response, next: TNoop) => void;
|
|
69
69
|
|
|
70
|
-
|
|
70
|
+
declare const proxyMiddleware: (ctx: Ctx) => (req: Request$1, res: Response$1, next: TNoop) => void;
|
|
71
|
+
|
|
72
|
+
export { Ctx, RouteManager, corsMiddleware, createServer, envParamsMw, fileServerMiddleware, loggerMiddleware, proxyMiddleware, serverCacheMw, serverPlainMw, serverScanMw };
|
|
71
73
|
export type { TEnvParams, THandlerOpt, TServerConfig };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@doracli/express",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"description": "use express",
|
|
5
5
|
"author": "cclr <18843152354@163.com>",
|
|
6
6
|
"homepage": "",
|
|
@@ -26,14 +26,14 @@
|
|
|
26
26
|
"g:build": "drr build"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@cclr/lang": "^0.1.
|
|
30
|
-
"@cclr/utils": "^0.1.
|
|
31
|
-
"@dorabag/file-pro": "^1.0.
|
|
32
|
-
"@doracli/define-server": "0.0.
|
|
33
|
-
"@doracli/helper": "0.0.
|
|
34
|
-
"@doracli/type": "0.0.
|
|
29
|
+
"@cclr/lang": "^0.1.50",
|
|
30
|
+
"@cclr/utils": "^0.1.50",
|
|
31
|
+
"@dorabag/file-pro": "^1.0.18",
|
|
32
|
+
"@doracli/define-server": "0.0.6",
|
|
33
|
+
"@doracli/helper": "0.0.6",
|
|
34
|
+
"@doracli/type": "0.0.6",
|
|
35
35
|
"@types/express": "^5.0.6",
|
|
36
36
|
"express": "^5.2.1"
|
|
37
37
|
},
|
|
38
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "b387668fa2986c56f245bc53bcb7072fd53b62e1"
|
|
39
39
|
}
|