@azteam/express 1.2.381 → 1.2.382
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/Server.js +24 -25
- package/lib/middleware/etagMiddleware.js +1 -2
- package/package.json +1 -1
- package/src/Server.js +20 -27
package/lib/Server.js
CHANGED
|
@@ -50,25 +50,22 @@ var Server = /*#__PURE__*/function () {
|
|
|
50
50
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
51
51
|
var errorCallback = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
|
|
52
52
|
_classCallCheck(this, Server);
|
|
53
|
-
this.options = _objectSpread({
|
|
53
|
+
this.options = _objectSpread(_objectSpread({
|
|
54
54
|
redis: null,
|
|
55
55
|
whiteList: null,
|
|
56
|
-
|
|
56
|
+
excludeList: [],
|
|
57
57
|
debug: process.env.NODE_ENV === 'development',
|
|
58
58
|
systemUser: 'toda'
|
|
59
|
-
}, options)
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
signed: true,
|
|
70
|
-
sameSite: 'Lax'
|
|
71
|
-
}, this.options.cookieOption);
|
|
59
|
+
}, options), {}, {
|
|
60
|
+
cookieOption: _objectSpread({
|
|
61
|
+
domain: null,
|
|
62
|
+
path: '/',
|
|
63
|
+
secure: process.env.NODE_ENV !== 'development',
|
|
64
|
+
httpOnly: true,
|
|
65
|
+
signed: true,
|
|
66
|
+
sameSite: 'Lax'
|
|
67
|
+
}, options.cookieOption)
|
|
68
|
+
});
|
|
72
69
|
this.middlewares = [];
|
|
73
70
|
this.controllers = [];
|
|
74
71
|
serviceDirs.map(function (dir) {
|
|
@@ -89,11 +86,12 @@ var Server = /*#__PURE__*/function () {
|
|
|
89
86
|
}, {
|
|
90
87
|
key: "initController",
|
|
91
88
|
value: function initController(apiDir) {
|
|
89
|
+
var excludeList = this.excludeList;
|
|
92
90
|
if (_fs["default"].existsSync(apiDir)) {
|
|
93
91
|
var controllerDirs = _fs["default"].readdirSync(apiDir);
|
|
94
92
|
for (var i = 0; i < controllerDirs.length; i += 1) {
|
|
95
93
|
var dirName = controllerDirs[i];
|
|
96
|
-
if (_fs["default"].statSync("".concat(apiDir, "/").concat(dirName)).isDirectory()) {
|
|
94
|
+
if (!excludeList.include(dirName) && _fs["default"].statSync("".concat(apiDir, "/").concat(dirName)).isDirectory()) {
|
|
97
95
|
var versionDirs = _fs["default"].readdirSync("".concat(apiDir, "/").concat(dirName));
|
|
98
96
|
for (var j = 0; j < versionDirs.length; j += 1) {
|
|
99
97
|
var versionName = versionDirs[j],
|
|
@@ -111,9 +109,10 @@ var Server = /*#__PURE__*/function () {
|
|
|
111
109
|
value: function startAPI(port) {
|
|
112
110
|
var _this4 = this;
|
|
113
111
|
if (!_lodash["default"].isEmpty(this.controllers)) {
|
|
114
|
-
var
|
|
115
|
-
|
|
116
|
-
|
|
112
|
+
var _this$options = this.options,
|
|
113
|
+
whiteList = _this$options.whiteList,
|
|
114
|
+
cookieOption = _this$options.cookieOption,
|
|
115
|
+
systemUser = _this$options.systemUser,
|
|
117
116
|
app = (0, _express["default"])();
|
|
118
117
|
app.use((0, _helmet["default"])({
|
|
119
118
|
frameguard: false
|
|
@@ -133,8 +132,8 @@ var Server = /*#__PURE__*/function () {
|
|
|
133
132
|
authorization = req.header('Authorization'),
|
|
134
133
|
agent = req.header('User-Agent');
|
|
135
134
|
var error = null;
|
|
136
|
-
if (!authorization && !agent.startsWith(
|
|
137
|
-
if (origin &&
|
|
135
|
+
if (!authorization && !agent.startsWith(systemUser)) {
|
|
136
|
+
if (origin && whiteList && !whiteList.some(function (re) {
|
|
138
137
|
return origin.endsWith(re);
|
|
139
138
|
})) {
|
|
140
139
|
error = new _error.ErrorException(_error.CORS, "".concat(origin, " Not allowed by CORS"));
|
|
@@ -145,7 +144,7 @@ var Server = /*#__PURE__*/function () {
|
|
|
145
144
|
origin: true
|
|
146
145
|
});
|
|
147
146
|
}));
|
|
148
|
-
if (this.debug) {
|
|
147
|
+
if (this.options.debug) {
|
|
149
148
|
app.use((0, _morgan["default"])('dev'));
|
|
150
149
|
}
|
|
151
150
|
app.get('/robots.txt', function (req, res) {
|
|
@@ -155,7 +154,7 @@ var Server = /*#__PURE__*/function () {
|
|
|
155
154
|
app.get('/favicon.ico', function (req, res) {
|
|
156
155
|
return res.status(204).json({});
|
|
157
156
|
});
|
|
158
|
-
var redis = this.redis;
|
|
157
|
+
var redis = this.options.redis;
|
|
159
158
|
if (redis) {
|
|
160
159
|
app.request.redis = redis;
|
|
161
160
|
app.response.redis = redis;
|
|
@@ -229,7 +228,7 @@ var Server = /*#__PURE__*/function () {
|
|
|
229
228
|
var _this2 = this;
|
|
230
229
|
_lodash["default"].map(data, function (name) {
|
|
231
230
|
_this2.clearCookie(name, {
|
|
232
|
-
domain:
|
|
231
|
+
domain: cookieOption.domain
|
|
233
232
|
});
|
|
234
233
|
});
|
|
235
234
|
};
|
|
@@ -237,7 +236,7 @@ var Server = /*#__PURE__*/function () {
|
|
|
237
236
|
var _this3 = this;
|
|
238
237
|
_lodash["default"].map(data, function (value, key) {
|
|
239
238
|
var maxAge = 86400000 * 365; // 1 year
|
|
240
|
-
_this3.cookie(key, value, _objectSpread(_objectSpread({},
|
|
239
|
+
_this3.cookie(key, value, _objectSpread(_objectSpread({}, cookieOption), {}, {
|
|
241
240
|
maxAge: maxAge,
|
|
242
241
|
expires: new Date(Date.now() + maxAge)
|
|
243
242
|
}));
|
|
@@ -12,8 +12,7 @@ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try
|
|
|
12
12
|
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
13
13
|
function floorToMinute(time, minutes) {
|
|
14
14
|
var roundSecond = minutes * 60;
|
|
15
|
-
|
|
16
|
-
return time;
|
|
15
|
+
return time - time % (Math.floor(time / roundSecond) * roundSecond);
|
|
17
16
|
}
|
|
18
17
|
function _default() {
|
|
19
18
|
var mTimeout = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 5;
|
package/package.json
CHANGED
package/src/Server.js
CHANGED
|
@@ -26,26 +26,19 @@ class Server {
|
|
|
26
26
|
this.options = {
|
|
27
27
|
redis: null,
|
|
28
28
|
whiteList: null,
|
|
29
|
-
|
|
29
|
+
excludeList: [],
|
|
30
30
|
debug: process.env.NODE_ENV === 'development',
|
|
31
31
|
systemUser: 'toda',
|
|
32
32
|
...options,
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
domain: null,
|
|
43
|
-
path: '/',
|
|
44
|
-
secure: process.env.NODE_ENV !== 'development',
|
|
45
|
-
httpOnly: true,
|
|
46
|
-
signed: true,
|
|
47
|
-
sameSite: 'Lax',
|
|
48
|
-
...this.options.cookieOption,
|
|
33
|
+
cookieOption: {
|
|
34
|
+
domain: null,
|
|
35
|
+
path: '/',
|
|
36
|
+
secure: process.env.NODE_ENV !== 'development',
|
|
37
|
+
httpOnly: true,
|
|
38
|
+
signed: true,
|
|
39
|
+
sameSite: 'Lax',
|
|
40
|
+
...options.cookieOption,
|
|
41
|
+
},
|
|
49
42
|
};
|
|
50
43
|
|
|
51
44
|
this.middlewares = [];
|
|
@@ -68,12 +61,14 @@ class Server {
|
|
|
68
61
|
}
|
|
69
62
|
|
|
70
63
|
initController(apiDir) {
|
|
64
|
+
const {excludeList} = this;
|
|
65
|
+
|
|
71
66
|
if (fs.existsSync(apiDir)) {
|
|
72
67
|
const controllerDirs = fs.readdirSync(apiDir);
|
|
73
68
|
|
|
74
69
|
for (let i = 0; i < controllerDirs.length; i += 1) {
|
|
75
70
|
const dirName = controllerDirs[i];
|
|
76
|
-
if (fs.statSync(`${apiDir}/${dirName}`).isDirectory()) {
|
|
71
|
+
if (!excludeList.include(dirName) && fs.statSync(`${apiDir}/${dirName}`).isDirectory()) {
|
|
77
72
|
const versionDirs = fs.readdirSync(`${apiDir}/${dirName}`);
|
|
78
73
|
|
|
79
74
|
for (let j = 0; j < versionDirs.length; j += 1) {
|
|
@@ -90,9 +85,7 @@ class Server {
|
|
|
90
85
|
|
|
91
86
|
startAPI(port) {
|
|
92
87
|
if (!_.isEmpty(this.controllers)) {
|
|
93
|
-
const
|
|
94
|
-
COOKIE_OPTION = this.cookieOption,
|
|
95
|
-
SYSTEM_USER = this.systemUser,
|
|
88
|
+
const {whiteList, cookieOption, systemUser} = this.options,
|
|
96
89
|
app = express();
|
|
97
90
|
|
|
98
91
|
app.use(
|
|
@@ -116,8 +109,8 @@ class Server {
|
|
|
116
109
|
agent = req.header('User-Agent');
|
|
117
110
|
|
|
118
111
|
let error = null;
|
|
119
|
-
if (!authorization && !agent.startsWith(
|
|
120
|
-
if (origin &&
|
|
112
|
+
if (!authorization && !agent.startsWith(systemUser)) {
|
|
113
|
+
if (origin && whiteList && !whiteList.some((re) => origin.endsWith(re))) {
|
|
121
114
|
error = new ErrorException(CORS, `${origin} Not allowed by CORS`);
|
|
122
115
|
}
|
|
123
116
|
}
|
|
@@ -128,7 +121,7 @@ class Server {
|
|
|
128
121
|
})
|
|
129
122
|
);
|
|
130
123
|
|
|
131
|
-
if (this.debug) {
|
|
124
|
+
if (this.options.debug) {
|
|
132
125
|
app.use(morgan('dev'));
|
|
133
126
|
}
|
|
134
127
|
|
|
@@ -138,7 +131,7 @@ class Server {
|
|
|
138
131
|
});
|
|
139
132
|
app.get('/favicon.ico', (req, res) => res.status(204).json({}));
|
|
140
133
|
|
|
141
|
-
const {redis} = this;
|
|
134
|
+
const {redis} = this.options;
|
|
142
135
|
|
|
143
136
|
if (redis) {
|
|
144
137
|
app.request.redis = redis;
|
|
@@ -218,7 +211,7 @@ class Server {
|
|
|
218
211
|
app.response.cleanCookie = function (data) {
|
|
219
212
|
_.map(data, (name) => {
|
|
220
213
|
this.clearCookie(name, {
|
|
221
|
-
domain:
|
|
214
|
+
domain: cookieOption.domain,
|
|
222
215
|
});
|
|
223
216
|
});
|
|
224
217
|
};
|
|
@@ -227,7 +220,7 @@ class Server {
|
|
|
227
220
|
_.map(data, (value, key) => {
|
|
228
221
|
const maxAge = 86400000 * 365; // 1 year
|
|
229
222
|
this.cookie(key, value, {
|
|
230
|
-
...
|
|
223
|
+
...cookieOption,
|
|
231
224
|
maxAge,
|
|
232
225
|
expires: new Date(Date.now() + maxAge),
|
|
233
226
|
});
|