@azteam/express 1.2.487 → 1.2.489
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 +71 -55
- package/package.json +3 -2
- package/src/Server.js +77 -57
package/lib/Server.js
CHANGED
|
@@ -13,14 +13,18 @@ var _express = _interopRequireDefault(require("express"));
|
|
|
13
13
|
require("express-async-errors");
|
|
14
14
|
var _fs = _interopRequireDefault(require("fs"));
|
|
15
15
|
var _helmet = _interopRequireDefault(require("helmet"));
|
|
16
|
-
var _http =
|
|
16
|
+
var _http = _interopRequireWildcard(require("http"));
|
|
17
17
|
var _lodash = _interopRequireDefault(require("lodash"));
|
|
18
18
|
var _methodOverride = _interopRequireDefault(require("method-override"));
|
|
19
19
|
var _morgan = _interopRequireDefault(require("morgan"));
|
|
20
|
+
var _pako = _interopRequireDefault(require("pako"));
|
|
20
21
|
var _path = _interopRequireDefault(require("path"));
|
|
21
22
|
var _psl = _interopRequireDefault(require("psl"));
|
|
23
|
+
var _socket = require("socket.io");
|
|
22
24
|
var _error = require("@azteam/error");
|
|
23
25
|
var _util = require("@azteam/util");
|
|
26
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
|
|
27
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
|
|
24
28
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
25
29
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
26
30
|
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
@@ -45,6 +49,9 @@ var RES_TYPE = {
|
|
|
45
49
|
OBJECT: 'OBJECT',
|
|
46
50
|
DOCS: 'DOCS'
|
|
47
51
|
};
|
|
52
|
+
function compressJson(message) {
|
|
53
|
+
return _pako["default"].deflate(JSON.stringify(message));
|
|
54
|
+
}
|
|
48
55
|
var Server = /*#__PURE__*/function () {
|
|
49
56
|
function Server(serviceDirs) {
|
|
50
57
|
var _this = this;
|
|
@@ -100,9 +107,9 @@ var Server = /*#__PURE__*/function () {
|
|
|
100
107
|
if (_fs["default"].statSync("".concat(apiDir, "/").concat(dirName)).isDirectory()) {
|
|
101
108
|
var versionDirs = _lodash["default"].differenceWith(_fs["default"].readdirSync("".concat(apiDir, "/").concat(dirName)), excludeList);
|
|
102
109
|
for (var j = 0; j < versionDirs.length; j += 1) {
|
|
103
|
-
var versionName = versionDirs[j]
|
|
104
|
-
|
|
105
|
-
|
|
110
|
+
var versionName = versionDirs[j];
|
|
111
|
+
// eslint-disable-next-line global-require,import/no-dynamic-require
|
|
112
|
+
var controller = require("".concat(apiDir, "/").concat(dirName, "/").concat(versionName, "/controller"))["default"];
|
|
106
113
|
this.addController(dirName, versionName, controller);
|
|
107
114
|
}
|
|
108
115
|
}
|
|
@@ -110,52 +117,58 @@ var Server = /*#__PURE__*/function () {
|
|
|
110
117
|
}
|
|
111
118
|
return this;
|
|
112
119
|
}
|
|
120
|
+
}, {
|
|
121
|
+
key: "createExpressApp",
|
|
122
|
+
value: function createExpressApp() {
|
|
123
|
+
var _this$options = this.options,
|
|
124
|
+
whiteList = _this$options.whiteList,
|
|
125
|
+
systemUser = _this$options.systemUser;
|
|
126
|
+
var app = (0, _express["default"])();
|
|
127
|
+
app.use((0, _helmet["default"])({
|
|
128
|
+
frameguard: false
|
|
129
|
+
}));
|
|
130
|
+
app.set('view engine', 'ejs');
|
|
131
|
+
app.use((0, _methodOverride["default"])());
|
|
132
|
+
app.use(_bodyParser["default"].urlencoded({
|
|
133
|
+
limit: '5mb',
|
|
134
|
+
extended: true
|
|
135
|
+
}));
|
|
136
|
+
app.use(_bodyParser["default"].json({
|
|
137
|
+
limit: '5mb'
|
|
138
|
+
}));
|
|
139
|
+
app.set('trust proxy', 1);
|
|
140
|
+
app.use((0, _cookieParser["default"])(process.env.SECRET_KEY));
|
|
141
|
+
app.use((0, _compression["default"])());
|
|
142
|
+
app.use((0, _cors["default"])(function (req, callback) {
|
|
143
|
+
var origin = req.get('Origin');
|
|
144
|
+
var authorization = req.get('Authorization');
|
|
145
|
+
var agent = req.get('User-Agent') || 'null';
|
|
146
|
+
var error = null;
|
|
147
|
+
if (!authorization && !agent.startsWith(systemUser)) {
|
|
148
|
+
if (origin && whiteList && !whiteList.some(function (re) {
|
|
149
|
+
return origin.endsWith(re);
|
|
150
|
+
})) {
|
|
151
|
+
console.error("CORS ".concat(agent, " ").concat(origin));
|
|
152
|
+
error = new _error.ErrorException(_error.CORS, "".concat(origin, " Not allowed by CORS"));
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
callback(error, {
|
|
156
|
+
credentials: true,
|
|
157
|
+
origin: true
|
|
158
|
+
});
|
|
159
|
+
}));
|
|
160
|
+
if (this.options.debug) {
|
|
161
|
+
app.use((0, _morgan["default"])('dev'));
|
|
162
|
+
}
|
|
163
|
+
return app;
|
|
164
|
+
}
|
|
113
165
|
}, {
|
|
114
166
|
key: "startAPI",
|
|
115
167
|
value: function startAPI(port) {
|
|
116
168
|
var _this4 = this;
|
|
117
169
|
if (!_lodash["default"].isEmpty(this.controllers)) {
|
|
118
|
-
var
|
|
119
|
-
|
|
120
|
-
cookieOption = _this$options.cookieOption,
|
|
121
|
-
systemUser = _this$options.systemUser,
|
|
122
|
-
app = (0, _express["default"])();
|
|
123
|
-
app.use((0, _helmet["default"])({
|
|
124
|
-
frameguard: false
|
|
125
|
-
}));
|
|
126
|
-
app.set('view engine', 'ejs');
|
|
127
|
-
app.use((0, _methodOverride["default"])());
|
|
128
|
-
app.use(_bodyParser["default"].urlencoded({
|
|
129
|
-
limit: '5mb',
|
|
130
|
-
extended: true
|
|
131
|
-
}));
|
|
132
|
-
app.use(_bodyParser["default"].json({
|
|
133
|
-
limit: '5mb'
|
|
134
|
-
}));
|
|
135
|
-
app.set('trust proxy', 1);
|
|
136
|
-
app.use((0, _cookieParser["default"])(process.env.SECRET_KEY));
|
|
137
|
-
app.use((0, _compression["default"])());
|
|
138
|
-
app.use((0, _cors["default"])(function (req, callback) {
|
|
139
|
-
var origin = req.get('Origin'),
|
|
140
|
-
authorization = req.get('Authorization'),
|
|
141
|
-
agent = req.get('User-Agent') || 'null';
|
|
142
|
-
var error = null;
|
|
143
|
-
if (!authorization && !agent.startsWith(systemUser)) {
|
|
144
|
-
if (origin && whiteList && !whiteList.some(function (re) {
|
|
145
|
-
return origin.endsWith(re);
|
|
146
|
-
})) {
|
|
147
|
-
console.error("CORS ".concat(agent, " ").concat(origin));
|
|
148
|
-
error = new _error.ErrorException(_error.CORS, "".concat(origin, " Not allowed by CORS"));
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
callback(error, {
|
|
152
|
-
credentials: true,
|
|
153
|
-
origin: true
|
|
154
|
-
});
|
|
155
|
-
}));
|
|
156
|
-
if (this.options.debug) {
|
|
157
|
-
app.use((0, _morgan["default"])('dev'));
|
|
158
|
-
}
|
|
170
|
+
var cookieOption = this.options.cookieOption;
|
|
171
|
+
var app = this.createExpressApp();
|
|
159
172
|
app.get('/robots.txt', function (req, res) {
|
|
160
173
|
res.type('text/plain');
|
|
161
174
|
res.send('User-agent: *\nDisallow: /');
|
|
@@ -275,14 +288,14 @@ var Server = /*#__PURE__*/function () {
|
|
|
275
288
|
});
|
|
276
289
|
var msg = [];
|
|
277
290
|
_lodash["default"].map(this.controllers, function (data) {
|
|
278
|
-
var controller = data.controller
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
291
|
+
var controller = data.controller;
|
|
292
|
+
var controllerName = data.name;
|
|
293
|
+
var controllerVersion = data.version;
|
|
294
|
+
var listPublicRouter = controller.publicRouter();
|
|
282
295
|
_lodash["default"].map(listPublicRouter, function (method) {
|
|
283
296
|
var name = method.name,
|
|
284
|
-
type = method.type
|
|
285
|
-
|
|
297
|
+
type = method.type;
|
|
298
|
+
var router = controller[name]();
|
|
286
299
|
if (!router.disabled) {
|
|
287
300
|
router.path = "/".concat(method.path, "/").concat(router.path);
|
|
288
301
|
router.path = controller.pathName ? "/".concat(controller.pathName).concat(router.path) : router.path;
|
|
@@ -341,6 +354,7 @@ var Server = /*#__PURE__*/function () {
|
|
|
341
354
|
});
|
|
342
355
|
app.use(function (err, req, res, next) {
|
|
343
356
|
var error = (0, _error.errorCatch)(err);
|
|
357
|
+
error.url = req.originalUrl;
|
|
344
358
|
if (process.env.NODE_ENV === 'development') {
|
|
345
359
|
console.log(error.errors);
|
|
346
360
|
}
|
|
@@ -353,9 +367,6 @@ var Server = /*#__PURE__*/function () {
|
|
|
353
367
|
if (error.errors[0].code === _error.UNKNOWN) {
|
|
354
368
|
error.errors[0].message = '';
|
|
355
369
|
}
|
|
356
|
-
if (error.errors[0].code === _error.NOT_FOUND) {
|
|
357
|
-
error.errors[0].url = req.originalUrl;
|
|
358
|
-
}
|
|
359
370
|
return res.status(error.status).json({
|
|
360
371
|
success: false,
|
|
361
372
|
errors: error.errors
|
|
@@ -395,7 +406,12 @@ var Server = /*#__PURE__*/function () {
|
|
|
395
406
|
key: "startSocket",
|
|
396
407
|
value: function startSocket(port) {
|
|
397
408
|
if (!_lodash["default"].isEmpty(this.controllers)) {
|
|
398
|
-
|
|
409
|
+
var httpServer = (0, _http.createServer)();
|
|
410
|
+
var app = this.createExpressApp();
|
|
411
|
+
httpServer.on('request', app);
|
|
412
|
+
var io = new _socket.Server(httpServer);
|
|
413
|
+
httpServer.listen(port);
|
|
414
|
+
return io;
|
|
399
415
|
}
|
|
400
416
|
throw new Error('No controllers in use SOCKET');
|
|
401
417
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@azteam/express",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.489",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "toda <sp.azsolution.net@gmail.com>",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
"pluralize": "8.0.0",
|
|
38
38
|
"psl": "1.8.0",
|
|
39
39
|
"socket.io": "4.4.1",
|
|
40
|
-
"ua-parser-js": "1.0.32"
|
|
40
|
+
"ua-parser-js": "1.0.32",
|
|
41
|
+
"pako": "2.0.4"
|
|
41
42
|
}
|
|
42
43
|
}
|
package/src/Server.js
CHANGED
|
@@ -7,12 +7,14 @@ import express from 'express';
|
|
|
7
7
|
import 'express-async-errors';
|
|
8
8
|
import fs from 'fs';
|
|
9
9
|
import helmet from 'helmet';
|
|
10
|
-
import http from 'http';
|
|
10
|
+
import http, {createServer} from 'http';
|
|
11
11
|
import _ from 'lodash';
|
|
12
12
|
import methodOverride from 'method-override';
|
|
13
13
|
import morgan from 'morgan';
|
|
14
|
+
import pako from 'pako';
|
|
14
15
|
import path from 'path';
|
|
15
16
|
import psl from 'psl';
|
|
17
|
+
import {Server as SocketIO} from 'socket.io';
|
|
16
18
|
|
|
17
19
|
import {CORS, ErrorException, NOT_FOUND, UNKNOWN, errorCatch} from '@azteam/error';
|
|
18
20
|
import {omitArrayItem, omitItem} from '@azteam/util';
|
|
@@ -23,6 +25,10 @@ const RES_TYPE = {
|
|
|
23
25
|
DOCS: 'DOCS',
|
|
24
26
|
};
|
|
25
27
|
|
|
28
|
+
function compressJson(message) {
|
|
29
|
+
return pako.deflate(JSON.stringify(message));
|
|
30
|
+
}
|
|
31
|
+
|
|
26
32
|
class Server {
|
|
27
33
|
constructor(serviceDirs, options = {}, errorCallback = () => {}) {
|
|
28
34
|
this.options = {
|
|
@@ -77,9 +83,9 @@ class Server {
|
|
|
77
83
|
if (fs.statSync(`${apiDir}/${dirName}`).isDirectory()) {
|
|
78
84
|
const versionDirs = _.differenceWith(fs.readdirSync(`${apiDir}/${dirName}`), excludeList);
|
|
79
85
|
for (let j = 0; j < versionDirs.length; j += 1) {
|
|
80
|
-
const versionName = versionDirs[j]
|
|
81
|
-
|
|
82
|
-
|
|
86
|
+
const versionName = versionDirs[j];
|
|
87
|
+
// eslint-disable-next-line global-require,import/no-dynamic-require
|
|
88
|
+
const controller = require(`${apiDir}/${dirName}/${versionName}/controller`).default;
|
|
83
89
|
this.addController(dirName, versionName, controller);
|
|
84
90
|
}
|
|
85
91
|
}
|
|
@@ -88,50 +94,57 @@ class Server {
|
|
|
88
94
|
return this;
|
|
89
95
|
}
|
|
90
96
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
if (
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
error = new ErrorException(CORS, `${origin} Not allowed by CORS`);
|
|
123
|
-
}
|
|
97
|
+
createExpressApp() {
|
|
98
|
+
const {whiteList, systemUser} = this.options;
|
|
99
|
+
|
|
100
|
+
const app = express();
|
|
101
|
+
|
|
102
|
+
app.use(
|
|
103
|
+
helmet({
|
|
104
|
+
frameguard: false,
|
|
105
|
+
})
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
app.set('view engine', 'ejs');
|
|
109
|
+
app.use(methodOverride());
|
|
110
|
+
app.use(bodyParser.urlencoded({limit: '5mb', extended: true}));
|
|
111
|
+
app.use(bodyParser.json({limit: '5mb'}));
|
|
112
|
+
|
|
113
|
+
app.set('trust proxy', 1);
|
|
114
|
+
|
|
115
|
+
app.use(cookieParser(process.env.SECRET_KEY));
|
|
116
|
+
app.use(compression());
|
|
117
|
+
app.use(
|
|
118
|
+
cors(function (req, callback) {
|
|
119
|
+
const origin = req.get('Origin');
|
|
120
|
+
const authorization = req.get('Authorization');
|
|
121
|
+
const agent = req.get('User-Agent') || 'null';
|
|
122
|
+
|
|
123
|
+
let error = null;
|
|
124
|
+
if (!authorization && !agent.startsWith(systemUser)) {
|
|
125
|
+
if (origin && whiteList && !whiteList.some((re) => origin.endsWith(re))) {
|
|
126
|
+
console.error(`CORS ${agent} ${origin}`);
|
|
127
|
+
error = new ErrorException(CORS, `${origin} Not allowed by CORS`);
|
|
124
128
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
})
|
|
130
|
-
)
|
|
129
|
+
}
|
|
130
|
+
callback(error, {
|
|
131
|
+
credentials: true,
|
|
132
|
+
origin: true,
|
|
133
|
+
});
|
|
134
|
+
})
|
|
135
|
+
);
|
|
131
136
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
137
|
+
if (this.options.debug) {
|
|
138
|
+
app.use(morgan('dev'));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return app;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
startAPI(port) {
|
|
145
|
+
if (!_.isEmpty(this.controllers)) {
|
|
146
|
+
const {cookieOption} = this.options;
|
|
147
|
+
const app = this.createExpressApp();
|
|
135
148
|
|
|
136
149
|
app.get('/robots.txt', function (req, res) {
|
|
137
150
|
res.type('text/plain');
|
|
@@ -268,14 +281,14 @@ class Server {
|
|
|
268
281
|
|
|
269
282
|
const msg = [];
|
|
270
283
|
_.map(this.controllers, (data) => {
|
|
271
|
-
const {controller} = data
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
284
|
+
const {controller} = data;
|
|
285
|
+
const controllerName = data.name;
|
|
286
|
+
const controllerVersion = data.version;
|
|
287
|
+
const listPublicRouter = controller.publicRouter();
|
|
275
288
|
|
|
276
289
|
_.map(listPublicRouter, (method) => {
|
|
277
|
-
const {name, type} = method
|
|
278
|
-
|
|
290
|
+
const {name, type} = method;
|
|
291
|
+
const router = controller[name]();
|
|
279
292
|
if (!router.disabled) {
|
|
280
293
|
router.path = `/${method.path}/${router.path}`;
|
|
281
294
|
router.path = controller.pathName ? `/${controller.pathName}${router.path}` : router.path;
|
|
@@ -314,6 +327,9 @@ class Server {
|
|
|
314
327
|
|
|
315
328
|
app.use((err, req, res, next) => {
|
|
316
329
|
const error = errorCatch(err);
|
|
330
|
+
|
|
331
|
+
error.url = req.originalUrl;
|
|
332
|
+
|
|
317
333
|
if (process.env.NODE_ENV === 'development') {
|
|
318
334
|
console.log(error.errors);
|
|
319
335
|
}
|
|
@@ -328,10 +344,6 @@ class Server {
|
|
|
328
344
|
error.errors[0].message = '';
|
|
329
345
|
}
|
|
330
346
|
|
|
331
|
-
if (error.errors[0].code === NOT_FOUND) {
|
|
332
|
-
error.errors[0].url = req.originalUrl;
|
|
333
|
-
}
|
|
334
|
-
|
|
335
347
|
return res.status(error.status).json({success: false, errors: error.errors});
|
|
336
348
|
});
|
|
337
349
|
|
|
@@ -374,8 +386,16 @@ class Server {
|
|
|
374
386
|
|
|
375
387
|
startSocket(port) {
|
|
376
388
|
if (!_.isEmpty(this.controllers)) {
|
|
377
|
-
|
|
389
|
+
const httpServer = createServer();
|
|
390
|
+
const app = this.createExpressApp();
|
|
391
|
+
httpServer.on('request', app);
|
|
392
|
+
|
|
393
|
+
const io = new SocketIO(httpServer);
|
|
394
|
+
httpServer.listen(port);
|
|
395
|
+
|
|
396
|
+
return io;
|
|
378
397
|
}
|
|
398
|
+
|
|
379
399
|
throw new Error('No controllers in use SOCKET');
|
|
380
400
|
}
|
|
381
401
|
}
|