@doracli/express 0.0.3 → 0.0.5
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/lib/cjs/index.js +3 -170
- package/lib/esm/index.js +3 -165
- package/lib/type/index.d.ts +42 -18
- package/package.json +9 -9
package/lib/cjs/index.js
CHANGED
|
@@ -1,170 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var express = require('express');
|
|
4
|
-
var filePro = require('@dorabag/file-pro');
|
|
5
|
-
var lang = require('@cclr/lang');
|
|
6
|
-
var helper = require('@doracli/helper');
|
|
7
|
-
|
|
8
|
-
var createServer = function createServer(config) {
|
|
9
|
-
var app = express();
|
|
10
|
-
return app;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
function _classCallCheck(a, n) {
|
|
14
|
-
if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
|
|
15
|
-
}
|
|
16
|
-
function _defineProperties(e, r) {
|
|
17
|
-
for (var t = 0; t < r.length; t++) {
|
|
18
|
-
var o = r[t];
|
|
19
|
-
o.enumerable = o.enumerable || false, o.configurable = true, "value" in o && (o.writable = true), Object.defineProperty(e, _toPropertyKey(o.key), o);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
function _createClass(e, r, t) {
|
|
23
|
-
return r && _defineProperties(e.prototype, r), Object.defineProperty(e, "prototype", {
|
|
24
|
-
writable: false
|
|
25
|
-
}), e;
|
|
26
|
-
}
|
|
27
|
-
function _defineProperty(e, r, t) {
|
|
28
|
-
return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
29
|
-
value: t,
|
|
30
|
-
enumerable: true,
|
|
31
|
-
configurable: true,
|
|
32
|
-
writable: true
|
|
33
|
-
}) : e[r] = t, e;
|
|
34
|
-
}
|
|
35
|
-
function ownKeys(e, r) {
|
|
36
|
-
var t = Object.keys(e);
|
|
37
|
-
if (Object.getOwnPropertySymbols) {
|
|
38
|
-
var o = Object.getOwnPropertySymbols(e);
|
|
39
|
-
r && (o = o.filter(function (r) {
|
|
40
|
-
return Object.getOwnPropertyDescriptor(e, r).enumerable;
|
|
41
|
-
})), t.push.apply(t, o);
|
|
42
|
-
}
|
|
43
|
-
return t;
|
|
44
|
-
}
|
|
45
|
-
function _objectSpread2(e) {
|
|
46
|
-
for (var r = 1; r < arguments.length; r++) {
|
|
47
|
-
var t = null != arguments[r] ? arguments[r] : {};
|
|
48
|
-
r % 2 ? ownKeys(Object(t), true).forEach(function (r) {
|
|
49
|
-
_defineProperty(e, r, t[r]);
|
|
50
|
-
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
|
|
51
|
-
Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
return e;
|
|
55
|
-
}
|
|
56
|
-
function _toPrimitive(t, r) {
|
|
57
|
-
if ("object" != typeof t || !t) return t;
|
|
58
|
-
var e = t[Symbol.toPrimitive];
|
|
59
|
-
if (void 0 !== e) {
|
|
60
|
-
var i = e.call(t, r);
|
|
61
|
-
if ("object" != typeof i) return i;
|
|
62
|
-
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
63
|
-
}
|
|
64
|
-
return (String )(t);
|
|
65
|
-
}
|
|
66
|
-
function _toPropertyKey(t) {
|
|
67
|
-
var i = _toPrimitive(t, "string");
|
|
68
|
-
return "symbol" == typeof i ? i : i + "";
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
var RouteManager = /*#__PURE__*/function () {
|
|
72
|
-
function RouteManager(app) {
|
|
73
|
-
_classCallCheck(this, RouteManager);
|
|
74
|
-
_defineProperty(this, "app", void 0);
|
|
75
|
-
_defineProperty(this, "routesMap", {});
|
|
76
|
-
this.app = app;
|
|
77
|
-
}
|
|
78
|
-
return _createClass(RouteManager, [{
|
|
79
|
-
key: "check",
|
|
80
|
-
value: function check(route) {
|
|
81
|
-
return !!this.routesMap[route];
|
|
82
|
-
}
|
|
83
|
-
}, {
|
|
84
|
-
key: "add",
|
|
85
|
-
value: function add(route, method, handler) {
|
|
86
|
-
this.updateMap(route, method, handler);
|
|
87
|
-
this.app.route(route)[method](this.getProxyHandler(route, method));
|
|
88
|
-
}
|
|
89
|
-
}, {
|
|
90
|
-
key: "update",
|
|
91
|
-
value: function update(route, method, handler) {
|
|
92
|
-
if (!this.check(route)) {
|
|
93
|
-
this.add(route, method, handler);
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
this.updateMap(route, method, handler, 'updated');
|
|
97
|
-
}
|
|
98
|
-
}, {
|
|
99
|
-
key: "remove",
|
|
100
|
-
value: function remove(route, method) {
|
|
101
|
-
this.updateMap(route, method, null, 'removed');
|
|
102
|
-
// this.app._router.stack = this.app._router.stack.filter((layer) => {
|
|
103
|
-
// return !(layer.route && layer.route.route === route);
|
|
104
|
-
// });
|
|
105
|
-
}
|
|
106
|
-
}, {
|
|
107
|
-
key: "destroy",
|
|
108
|
-
value: function destroy() {
|
|
109
|
-
this.routesMap = {};
|
|
110
|
-
}
|
|
111
|
-
}, {
|
|
112
|
-
key: "updateMap",
|
|
113
|
-
value: function updateMap(route, method, handler) {
|
|
114
|
-
var state = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'added';
|
|
115
|
-
lang.set(this.routesMap, "".concat(route, ".").concat(method), {
|
|
116
|
-
state: state,
|
|
117
|
-
handler: handler
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
}, {
|
|
121
|
-
key: "getProxyHandler",
|
|
122
|
-
value: function getProxyHandler(route, method) {
|
|
123
|
-
var _this = this;
|
|
124
|
-
return function (req, res) {
|
|
125
|
-
var record = lang.get(_this.routesMap, "".concat(route, ".").concat(method), {});
|
|
126
|
-
if (record.handler) {
|
|
127
|
-
return record.handler(req, res);
|
|
128
|
-
} else {
|
|
129
|
-
return res.status(404).send("".concat(method, " ").concat(route, " route not found"));
|
|
130
|
-
}
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
}]);
|
|
134
|
-
}();
|
|
135
|
-
|
|
136
|
-
var Ctx = /*#__PURE__*/function () {
|
|
137
|
-
function Ctx(configCtx, app) {
|
|
138
|
-
_classCallCheck(this, Ctx);
|
|
139
|
-
_defineProperty(this, "app", void 0);
|
|
140
|
-
_defineProperty(this, "routeManager", void 0);
|
|
141
|
-
_defineProperty(this, "configCtx", void 0);
|
|
142
|
-
_defineProperty(this, "envParams", {
|
|
143
|
-
scan: './',
|
|
144
|
-
workRootDir: filePro.getCwd()
|
|
145
|
-
});
|
|
146
|
-
this.configCtx = configCtx;
|
|
147
|
-
this.app = app;
|
|
148
|
-
this.routeManager = new RouteManager(app);
|
|
149
|
-
}
|
|
150
|
-
return _createClass(Ctx, [{
|
|
151
|
-
key: "updateEnvParams",
|
|
152
|
-
value: function updateEnvParams(params) {
|
|
153
|
-
this.envParams = _objectSpread2(_objectSpread2({}, this.envParams), params);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// getServiceConfig() {
|
|
157
|
-
// return this.configCtx.getDoraConfig().serviceConfig!;
|
|
158
|
-
// }
|
|
159
|
-
}]);
|
|
160
|
-
}();
|
|
161
|
-
|
|
162
|
-
var loggerMiddleware = function loggerMiddleware(req, res, next) {
|
|
163
|
-
helper.cLog.log('ser', 'request', "[".concat(req.method, "] ").concat(req.url));
|
|
164
|
-
next();
|
|
165
|
-
};
|
|
166
|
-
|
|
167
|
-
exports.Ctx = Ctx;
|
|
168
|
-
exports.RouteManager = RouteManager;
|
|
169
|
-
exports.createServer = createServer;
|
|
170
|
-
exports.loggerMiddleware = loggerMiddleware;
|
|
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("crypto"),a=require("fs"),u=require("zlib"),c=require("@doracli/helper"),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 u=e[i](a),c=u.value}catch(e){return void r(e)}u.done?t(c):Promise.resolve(c).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,u,"next",e)}function u(e){h(i,n,o,a,u,"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
|
+
/*! 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)}))}}};
|
package/lib/esm/index.js
CHANGED
|
@@ -1,165 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import { set, get } from '@cclr/lang';
|
|
4
|
-
import { cLog } from '@doracli/helper';
|
|
5
|
-
|
|
6
|
-
var createServer = function createServer(config) {
|
|
7
|
-
var app = express();
|
|
8
|
-
return app;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
function _classCallCheck(a, n) {
|
|
12
|
-
if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
|
|
13
|
-
}
|
|
14
|
-
function _defineProperties(e, r) {
|
|
15
|
-
for (var t = 0; t < r.length; t++) {
|
|
16
|
-
var o = r[t];
|
|
17
|
-
o.enumerable = o.enumerable || false, o.configurable = true, "value" in o && (o.writable = true), Object.defineProperty(e, _toPropertyKey(o.key), o);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
function _createClass(e, r, t) {
|
|
21
|
-
return r && _defineProperties(e.prototype, r), Object.defineProperty(e, "prototype", {
|
|
22
|
-
writable: false
|
|
23
|
-
}), e;
|
|
24
|
-
}
|
|
25
|
-
function _defineProperty(e, r, t) {
|
|
26
|
-
return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
27
|
-
value: t,
|
|
28
|
-
enumerable: true,
|
|
29
|
-
configurable: true,
|
|
30
|
-
writable: true
|
|
31
|
-
}) : e[r] = t, e;
|
|
32
|
-
}
|
|
33
|
-
function ownKeys(e, r) {
|
|
34
|
-
var t = Object.keys(e);
|
|
35
|
-
if (Object.getOwnPropertySymbols) {
|
|
36
|
-
var o = Object.getOwnPropertySymbols(e);
|
|
37
|
-
r && (o = o.filter(function (r) {
|
|
38
|
-
return Object.getOwnPropertyDescriptor(e, r).enumerable;
|
|
39
|
-
})), t.push.apply(t, o);
|
|
40
|
-
}
|
|
41
|
-
return t;
|
|
42
|
-
}
|
|
43
|
-
function _objectSpread2(e) {
|
|
44
|
-
for (var r = 1; r < arguments.length; r++) {
|
|
45
|
-
var t = null != arguments[r] ? arguments[r] : {};
|
|
46
|
-
r % 2 ? ownKeys(Object(t), true).forEach(function (r) {
|
|
47
|
-
_defineProperty(e, r, t[r]);
|
|
48
|
-
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
|
|
49
|
-
Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
return e;
|
|
53
|
-
}
|
|
54
|
-
function _toPrimitive(t, r) {
|
|
55
|
-
if ("object" != typeof t || !t) return t;
|
|
56
|
-
var e = t[Symbol.toPrimitive];
|
|
57
|
-
if (void 0 !== e) {
|
|
58
|
-
var i = e.call(t, r);
|
|
59
|
-
if ("object" != typeof i) return i;
|
|
60
|
-
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
61
|
-
}
|
|
62
|
-
return (String )(t);
|
|
63
|
-
}
|
|
64
|
-
function _toPropertyKey(t) {
|
|
65
|
-
var i = _toPrimitive(t, "string");
|
|
66
|
-
return "symbol" == typeof i ? i : i + "";
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
var RouteManager = /*#__PURE__*/function () {
|
|
70
|
-
function RouteManager(app) {
|
|
71
|
-
_classCallCheck(this, RouteManager);
|
|
72
|
-
_defineProperty(this, "app", void 0);
|
|
73
|
-
_defineProperty(this, "routesMap", {});
|
|
74
|
-
this.app = app;
|
|
75
|
-
}
|
|
76
|
-
return _createClass(RouteManager, [{
|
|
77
|
-
key: "check",
|
|
78
|
-
value: function check(route) {
|
|
79
|
-
return !!this.routesMap[route];
|
|
80
|
-
}
|
|
81
|
-
}, {
|
|
82
|
-
key: "add",
|
|
83
|
-
value: function add(route, method, handler) {
|
|
84
|
-
this.updateMap(route, method, handler);
|
|
85
|
-
this.app.route(route)[method](this.getProxyHandler(route, method));
|
|
86
|
-
}
|
|
87
|
-
}, {
|
|
88
|
-
key: "update",
|
|
89
|
-
value: function update(route, method, handler) {
|
|
90
|
-
if (!this.check(route)) {
|
|
91
|
-
this.add(route, method, handler);
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
this.updateMap(route, method, handler, 'updated');
|
|
95
|
-
}
|
|
96
|
-
}, {
|
|
97
|
-
key: "remove",
|
|
98
|
-
value: function remove(route, method) {
|
|
99
|
-
this.updateMap(route, method, null, 'removed');
|
|
100
|
-
// this.app._router.stack = this.app._router.stack.filter((layer) => {
|
|
101
|
-
// return !(layer.route && layer.route.route === route);
|
|
102
|
-
// });
|
|
103
|
-
}
|
|
104
|
-
}, {
|
|
105
|
-
key: "destroy",
|
|
106
|
-
value: function destroy() {
|
|
107
|
-
this.routesMap = {};
|
|
108
|
-
}
|
|
109
|
-
}, {
|
|
110
|
-
key: "updateMap",
|
|
111
|
-
value: function updateMap(route, method, handler) {
|
|
112
|
-
var state = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'added';
|
|
113
|
-
set(this.routesMap, "".concat(route, ".").concat(method), {
|
|
114
|
-
state: state,
|
|
115
|
-
handler: handler
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
}, {
|
|
119
|
-
key: "getProxyHandler",
|
|
120
|
-
value: function getProxyHandler(route, method) {
|
|
121
|
-
var _this = this;
|
|
122
|
-
return function (req, res) {
|
|
123
|
-
var record = get(_this.routesMap, "".concat(route, ".").concat(method), {});
|
|
124
|
-
if (record.handler) {
|
|
125
|
-
return record.handler(req, res);
|
|
126
|
-
} else {
|
|
127
|
-
return res.status(404).send("".concat(method, " ").concat(route, " route not found"));
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
}]);
|
|
132
|
-
}();
|
|
133
|
-
|
|
134
|
-
var Ctx = /*#__PURE__*/function () {
|
|
135
|
-
function Ctx(configCtx, app) {
|
|
136
|
-
_classCallCheck(this, Ctx);
|
|
137
|
-
_defineProperty(this, "app", void 0);
|
|
138
|
-
_defineProperty(this, "routeManager", void 0);
|
|
139
|
-
_defineProperty(this, "configCtx", void 0);
|
|
140
|
-
_defineProperty(this, "envParams", {
|
|
141
|
-
scan: './',
|
|
142
|
-
workRootDir: getCwd()
|
|
143
|
-
});
|
|
144
|
-
this.configCtx = configCtx;
|
|
145
|
-
this.app = app;
|
|
146
|
-
this.routeManager = new RouteManager(app);
|
|
147
|
-
}
|
|
148
|
-
return _createClass(Ctx, [{
|
|
149
|
-
key: "updateEnvParams",
|
|
150
|
-
value: function updateEnvParams(params) {
|
|
151
|
-
this.envParams = _objectSpread2(_objectSpread2({}, this.envParams), params);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// getServiceConfig() {
|
|
155
|
-
// return this.configCtx.getDoraConfig().serviceConfig!;
|
|
156
|
-
// }
|
|
157
|
-
}]);
|
|
158
|
-
}();
|
|
159
|
-
|
|
160
|
-
var loggerMiddleware = function loggerMiddleware(req, res, next) {
|
|
161
|
-
cLog.log('ser', 'request', "[".concat(req.method, "] ").concat(req.url));
|
|
162
|
-
next();
|
|
163
|
-
};
|
|
164
|
-
|
|
165
|
-
export { Ctx, RouteManager, createServer, loggerMiddleware };
|
|
1
|
+
import t,{join as e}from"path";import{sleep as r,uri as n}from"@cclr/utils";import{mkdirRecursionSync as o,isValidDirSync as i,cleanTargets as a,getCwd as c}from"@dorabag/file-pro";import u from"express";export{default as express}from"express";import{get as s,set as f,isFunction as l}from"@cclr/lang";import p from"crypto";import{createReadStream as h,readFileSync as d,promises as v}from"fs";import m from"zlib";import{cLog as y,HtmlManage as g}from"@doracli/helper";import b from"node:path";import{request as w}from"http";import{request as j}from"https";import{URL as O}from"url";function P(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 C(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 x(t){return function(){var e=this,r=arguments;return new Promise(function(n,o){var i=t.apply(e,r);function a(t){C(i,n,o,a,c,"next",t)}function c(t){C(i,n,o,a,c,"throw",t)}a(void 0)})}}function k(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function S(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,F(n.key),n)}}(t.prototype,e),Object.defineProperty(t,"prototype",{writable:!1}),t}function T(t,e,r){return(e=F(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function E(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 H(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?E(Object(r),!0).forEach(function(e){T(t,e,r[e])}):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):E(Object(r)).forEach(function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))})}return t}function M(){
|
|
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};
|
package/lib/type/index.d.ts
CHANGED
|
@@ -1,26 +1,37 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TOnionFun } from '@cclr/utils';
|
|
2
|
+
import { Application, Request, Response } from 'express';
|
|
3
|
+
export { Application, Request, Response, default as express } from 'express';
|
|
2
4
|
import { ConfigCtx } from '@doracli/helper';
|
|
3
|
-
import {
|
|
5
|
+
import { TDoraConfigService } from '@doracli/type';
|
|
6
|
+
import { TControllerType, TControllerFun, TMethod, Request as Request$1, Response as Response$1 } from '@doracli/define-server';
|
|
4
7
|
import { TNoop } from '@cclr/lang';
|
|
5
8
|
|
|
6
|
-
declare const createServer: (config: {
|
|
7
|
-
port?: number;
|
|
8
|
-
}) => Application;
|
|
9
|
-
|
|
10
9
|
type TEnvParams = {
|
|
11
|
-
/** 服务文件 */
|
|
12
|
-
scan: string;
|
|
13
10
|
/** 工作根目录 */
|
|
14
11
|
workRootDir: string;
|
|
15
12
|
};
|
|
16
13
|
|
|
14
|
+
type THandlerOpt = {
|
|
15
|
+
state: 'added' | 'updated' | 'removed';
|
|
16
|
+
handlerType?: TControllerType;
|
|
17
|
+
handler: TControllerFun | null;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
type TServerConfig = TDoraConfigService & {
|
|
21
|
+
cacheDir: string;
|
|
22
|
+
};
|
|
23
|
+
|
|
17
24
|
declare class RouteManager {
|
|
18
25
|
private app;
|
|
19
26
|
private routesMap;
|
|
20
27
|
constructor(app: Application);
|
|
21
|
-
check(route: string): boolean;
|
|
22
|
-
add(route: string, method: TMethod, handler: TControllerFun
|
|
23
|
-
|
|
28
|
+
check(route: string, method: TMethod): boolean;
|
|
29
|
+
add(route: string, method: TMethod, handler: TControllerFun, options?: {
|
|
30
|
+
handlerType?: TControllerType;
|
|
31
|
+
}): void;
|
|
32
|
+
update(route: string, method: TMethod, handler: TControllerFun, options?: {
|
|
33
|
+
handlerType?: TControllerType;
|
|
34
|
+
}): void;
|
|
24
35
|
remove(route: string, method: TMethod): void;
|
|
25
36
|
destroy(): void;
|
|
26
37
|
private updateMap;
|
|
@@ -32,16 +43,29 @@ declare class Ctx {
|
|
|
32
43
|
routeManager: RouteManager;
|
|
33
44
|
configCtx: ConfigCtx;
|
|
34
45
|
envParams: TEnvParams;
|
|
46
|
+
serverConfig: TServerConfig;
|
|
35
47
|
constructor(configCtx: ConfigCtx, app: Application);
|
|
36
48
|
updateEnvParams(params: Partial<TEnvParams>): void;
|
|
49
|
+
updateServerConfig(config: Partial<TServerConfig>): void;
|
|
37
50
|
}
|
|
38
51
|
|
|
39
|
-
declare const
|
|
52
|
+
declare const serverCacheMw: (cacheDir?: string) => TOnionFun<Ctx>;
|
|
40
53
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
54
|
+
declare const envParamsMw: (envParams: TEnvParams) => TOnionFun<Ctx>;
|
|
55
|
+
|
|
56
|
+
declare const serverPlainMw: () => TOnionFun<Ctx>;
|
|
57
|
+
|
|
58
|
+
declare const serverScanMw: (mainScan?: string) => TOnionFun<Ctx>;
|
|
59
|
+
|
|
60
|
+
declare const createServer: (config: {
|
|
61
|
+
port?: number;
|
|
62
|
+
}) => Application;
|
|
63
|
+
|
|
64
|
+
declare const fileServerMiddleware: (route: string, directory: string) => (req: Request, res: Response, next: TNoop) => void;
|
|
65
|
+
|
|
66
|
+
declare const loggerMiddleware: (req: Request$1, res: Response$1, next: TNoop) => void;
|
|
67
|
+
|
|
68
|
+
declare const proxyMiddleware: (ctx: Ctx) => (req: Request, res: Response, next: TNoop) => void;
|
|
45
69
|
|
|
46
|
-
export { Ctx, RouteManager, createServer, loggerMiddleware };
|
|
47
|
-
export type { TEnvParams, THandlerOpt };
|
|
70
|
+
export { Ctx, RouteManager, createServer, envParamsMw, fileServerMiddleware, loggerMiddleware, proxyMiddleware, serverCacheMw, serverPlainMw, serverScanMw };
|
|
71
|
+
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.5",
|
|
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.
|
|
29
|
+
"@cclr/lang": "^0.1.49",
|
|
30
|
+
"@cclr/utils": "^0.1.49",
|
|
31
31
|
"@dorabag/file-pro": "^1.0.10",
|
|
32
|
-
"@doracli/define-server": "0.0.
|
|
33
|
-
"@doracli/helper": "0.0.
|
|
34
|
-
"@doracli/type": "0.0.
|
|
35
|
-
"@types/express": "^5.0.
|
|
36
|
-
"express": "^5.1
|
|
32
|
+
"@doracli/define-server": "0.0.5",
|
|
33
|
+
"@doracli/helper": "0.0.5",
|
|
34
|
+
"@doracli/type": "0.0.5",
|
|
35
|
+
"@types/express": "^5.0.6",
|
|
36
|
+
"express": "^5.2.1"
|
|
37
37
|
},
|
|
38
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "ac4c6829b2cec621ecf66b6324843773e8b5141f"
|
|
39
39
|
}
|