ee-core 4.1.3 → 4.1.4
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 +21 -21
- package/README.md +65 -65
- package/app/application.d.ts +5 -5
- package/app/application.js +30 -30
- package/app/boot.d.ts +5 -5
- package/app/boot.js +80 -80
- package/app/dir.d.ts +1 -1
- package/app/dir.js +26 -26
- package/app/events.d.ts +14 -14
- package/app/events.js +55 -55
- package/app/index.d.ts +2 -2
- package/app/index.js +6 -6
- package/config/config_loader.d.ts +13 -13
- package/config/config_loader.js +65 -65
- package/config/default_config.d.ts +95 -95
- package/config/default_config.js +111 -111
- package/config/index.d.ts +28 -28
- package/config/index.js +24 -24
- package/const/channel.d.ts +16 -16
- package/const/channel.js +27 -27
- package/controller/controller_loader.d.ts +9 -9
- package/controller/controller_loader.js +77 -77
- package/controller/index.d.ts +2 -2
- package/controller/index.js +24 -24
- package/core/index.d.ts +2 -2
- package/core/index.js +11 -11
- package/core/loader/file_loader.d.ts +66 -66
- package/core/loader/file_loader.js +211 -211
- package/core/utils/index.d.ts +12 -12
- package/core/utils/index.js +83 -83
- package/core/utils/timing.d.ts +22 -22
- package/core/utils/timing.js +78 -78
- package/cross/cross.d.ts +19 -19
- package/cross/cross.js +151 -151
- package/cross/crossProcess.d.ts +29 -29
- package/cross/crossProcess.js +172 -172
- package/cross/index.d.ts +2 -2
- package/cross/index.js +8 -8
- package/electron/app/index.d.ts +5 -5
- package/electron/app/index.js +48 -48
- package/electron/index.d.ts +3 -3
- package/electron/index.js +15 -15
- package/electron/window/index.d.ts +6 -6
- package/electron/window/index.js +268 -268
- package/exception/index.d.ts +9 -9
- package/exception/index.js +100 -100
- package/html/boot.html +98 -98
- package/html/cross-failure.html +28 -28
- package/html/failure.html +28 -28
- package/html/index.d.ts +1 -1
- package/html/index.js +10 -10
- package/index.d.ts +2 -2
- package/index.js +6 -6
- package/jobs/child/app.d.ts +1 -1
- package/jobs/child/app.js +69 -69
- package/jobs/child/index.d.ts +13 -13
- package/jobs/child/index.js +85 -85
- package/jobs/child/jobProcess.d.ts +16 -16
- package/jobs/child/jobProcess.js +133 -133
- package/jobs/child-pool/index.d.ts +26 -26
- package/jobs/child-pool/index.js +190 -190
- package/jobs/index.d.ts +3 -3
- package/jobs/index.js +6 -6
- package/jobs/load-balancer/algorithm/index.d.ts +4 -4
- package/jobs/load-balancer/algorithm/index.js +11 -11
- package/jobs/load-balancer/algorithm/minimumConnection.d.ts +2 -2
- package/jobs/load-balancer/algorithm/minimumConnection.js +18 -18
- package/jobs/load-balancer/algorithm/polling.d.ts +2 -2
- package/jobs/load-balancer/algorithm/polling.js +11 -11
- package/jobs/load-balancer/algorithm/random.d.ts +2 -2
- package/jobs/load-balancer/algorithm/random.js +9 -9
- package/jobs/load-balancer/algorithm/specify.d.ts +2 -2
- package/jobs/load-balancer/algorithm/specify.js +14 -14
- package/jobs/load-balancer/algorithm/weights.d.ts +2 -2
- package/jobs/load-balancer/algorithm/weights.js +21 -21
- package/jobs/load-balancer/algorithm/weightsMinimumConnection.d.ts +2 -2
- package/jobs/load-balancer/algorithm/weightsMinimumConnection.js +29 -29
- package/jobs/load-balancer/algorithm/weightsPolling.d.ts +2 -2
- package/jobs/load-balancer/algorithm/weightsPolling.js +22 -22
- package/jobs/load-balancer/algorithm/weightsRandom.d.ts +2 -2
- package/jobs/load-balancer/algorithm/weightsRandom.js +16 -16
- package/jobs/load-balancer/consts.d.ts +8 -8
- package/jobs/load-balancer/consts.js +9 -9
- package/jobs/load-balancer/index.d.ts +96 -96
- package/jobs/load-balancer/index.js +201 -201
- package/jobs/load-balancer/scheduler.d.ts +16 -16
- package/jobs/load-balancer/scheduler.js +31 -31
- package/loader/index.d.ts +9 -9
- package/loader/index.js +99 -98
- package/log/index.d.ts +5 -5
- package/log/index.js +85 -85
- package/log/logger.d.ts +1 -1
- package/log/logger.js +94 -94
- package/message/childMessage.d.ts +10 -10
- package/message/childMessage.js +43 -43
- package/message/index.d.ts +3 -3
- package/message/index.js +9 -9
- package/package.json +42 -42
- package/ps/index.d.ts +39 -39
- package/ps/index.js +294 -294
- package/socket/httpServer.d.ts +8 -8
- package/socket/httpServer.js +199 -199
- package/socket/index.d.ts +10 -10
- package/socket/index.js +64 -64
- package/socket/ipcServer.d.ts +9 -9
- package/socket/ipcServer.js +105 -105
- package/socket/socketServer.d.ts +10 -10
- package/socket/socketServer.js +81 -81
- package/storage/index.d.ts +4 -4
- package/storage/index.js +6 -6
- package/storage/sqliteStorage.d.ts +16 -16
- package/storage/sqliteStorage.js +123 -123
- package/tsconfig.json +13 -13
- package/utils/extend.d.ts +2 -2
- package/utils/extend.js +77 -77
- package/utils/helper.d.ts +14 -14
- package/utils/helper.js +206 -206
- package/utils/index.d.ts +10 -10
- package/utils/index.js +167 -167
- package/utils/ip.d.ts +2 -2
- package/utils/ip.js +221 -221
- package/utils/is.d.ts +14 -14
- package/utils/is.js +111 -111
- package/utils/json.d.ts +5 -5
- package/utils/json.js +79 -79
- package/utils/pargv.d.ts +3 -3
- package/utils/pargv.js +267 -267
- package/utils/port/index.d.ts +1 -1
- package/utils/port/index.js +125 -125
- package/utils/wrap.d.ts +4 -4
- package/utils/wrap.js +40 -40
package/socket/httpServer.js
CHANGED
|
@@ -1,199 +1,199 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const debug = require('debug')('ee-core:socket:httpServer');
|
|
4
|
-
const assert = require('assert');
|
|
5
|
-
const is = require('is-type-of');
|
|
6
|
-
const Koa = require('koa');
|
|
7
|
-
const cors = require('koa2-cors');
|
|
8
|
-
const koaBody = require('koa-body');
|
|
9
|
-
const https = require('https');
|
|
10
|
-
const fs = require('fs');
|
|
11
|
-
const path = require('path');
|
|
12
|
-
const _ = require('lodash');
|
|
13
|
-
const { coreLogger } = require('../log');
|
|
14
|
-
const { getBaseDir } = require('../ps');
|
|
15
|
-
const { getController } = require('../controller');
|
|
16
|
-
const { getConfig } = require('../config');
|
|
17
|
-
const { getPort } = require('../utils/port');
|
|
18
|
-
|
|
19
|
-
let channelSeparator = '/';
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* http server
|
|
23
|
-
*/
|
|
24
|
-
class HttpServer {
|
|
25
|
-
constructor () {
|
|
26
|
-
const { httpServer, mainServer } = getConfig();
|
|
27
|
-
this.config = httpServer;
|
|
28
|
-
channelSeparator = mainServer.channelSeparator;
|
|
29
|
-
this.httpApp = undefined;
|
|
30
|
-
this.init();
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
async init() {
|
|
34
|
-
if (this.config.enable == false) {
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const port = await getPort({port: parseInt(this.config.port)});
|
|
39
|
-
if (!port) {
|
|
40
|
-
throw new Error('[ee-core] [socket/HttpServer] http port required, and must be a number !');
|
|
41
|
-
}
|
|
42
|
-
process.env.EE_HTTP_PORT = port;
|
|
43
|
-
this.config.port = port;
|
|
44
|
-
|
|
45
|
-
this._create();
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* 创建服务
|
|
50
|
-
*/
|
|
51
|
-
_create () {
|
|
52
|
-
const config = this.config;
|
|
53
|
-
// config.default 增加koaConfig配置项
|
|
54
|
-
const koaConfig = config.koaConfig || {};
|
|
55
|
-
const { preMiddleware = [], postMiddleware = [], errorHandler = null } = koaConfig;
|
|
56
|
-
const isHttps = config?.https?.enable ?? false;
|
|
57
|
-
const sslOptions = {};
|
|
58
|
-
|
|
59
|
-
if (isHttps === true) {
|
|
60
|
-
config.protocol = 'https://';
|
|
61
|
-
const keyFile = path.join(getBaseDir(), config.https.key);
|
|
62
|
-
const certFile = path.join(getBaseDir(), config.https.cert);
|
|
63
|
-
assert(fs.existsSync(keyFile), 'ssl key file is required');
|
|
64
|
-
assert(fs.existsSync(certFile), 'ssl cert file is required');
|
|
65
|
-
|
|
66
|
-
sslOptions.key = fs.readFileSync(keyFile);
|
|
67
|
-
sslOptions.cert = fs.readFileSync(certFile);
|
|
68
|
-
}
|
|
69
|
-
const url = config.protocol + config.host + ':' + config.port;
|
|
70
|
-
const corsOptions = config.cors;
|
|
71
|
-
|
|
72
|
-
const koaApp = new Koa();
|
|
73
|
-
|
|
74
|
-
// 设置错误处理器,便于统一错误代码处理
|
|
75
|
-
this._setupErrorHandler(koaApp, errorHandler);
|
|
76
|
-
|
|
77
|
-
// 加载前置中间件
|
|
78
|
-
this._loadMiddlewares(koaApp, preMiddleware);
|
|
79
|
-
|
|
80
|
-
// 核心中间件
|
|
81
|
-
koaApp
|
|
82
|
-
.use(cors(corsOptions))
|
|
83
|
-
.use(koaBody(config.body))
|
|
84
|
-
.use(this._dispatch);
|
|
85
|
-
|
|
86
|
-
// 加载后置中间件
|
|
87
|
-
this._loadMiddlewares(koaApp, postMiddleware, 'post');
|
|
88
|
-
|
|
89
|
-
let msg = '[ee-core] [socket/http] server is: ' + url;
|
|
90
|
-
const listenOpt = {
|
|
91
|
-
host: config.host,
|
|
92
|
-
port: config.port
|
|
93
|
-
}
|
|
94
|
-
if (isHttps) {
|
|
95
|
-
https.createServer(sslOptions, koaApp.callback()).listen(listenOpt, (err) => {
|
|
96
|
-
msg = err ? err : msg;
|
|
97
|
-
coreLogger.info(msg);
|
|
98
|
-
});
|
|
99
|
-
} else {
|
|
100
|
-
koaApp.listen(listenOpt, (e) => {
|
|
101
|
-
msg = e ? e : msg;
|
|
102
|
-
coreLogger.info(msg);
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
this.httpApp = koaApp;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* 路由分发
|
|
111
|
-
*/
|
|
112
|
-
async _dispatch (ctx, next) {
|
|
113
|
-
const controller = getController();
|
|
114
|
-
const { filterRequest } = getConfig().httpServer;
|
|
115
|
-
let uriPath = ctx.request.path;
|
|
116
|
-
const method = ctx.request.method;
|
|
117
|
-
let params = ctx.request.query;
|
|
118
|
-
params = is.object(params) ? JSON.parse(JSON.stringify(params)) : {};
|
|
119
|
-
const body = ctx.request.body;
|
|
120
|
-
|
|
121
|
-
// 默认
|
|
122
|
-
ctx.response.status = 200;
|
|
123
|
-
|
|
124
|
-
try {
|
|
125
|
-
// 找函数
|
|
126
|
-
// 去除开头的 '/'
|
|
127
|
-
if (uriPath.indexOf('/') == 0) {
|
|
128
|
-
uriPath = uriPath.substring(1);
|
|
129
|
-
}
|
|
130
|
-
// 过滤
|
|
131
|
-
if (_.includes(filterRequest.uris, uriPath)) {
|
|
132
|
-
ctx.response.body = filterRequest.returnData;
|
|
133
|
-
await next();
|
|
134
|
-
return
|
|
135
|
-
}
|
|
136
|
-
if (uriPath.slice(0, 10) != 'controller') {
|
|
137
|
-
uriPath = 'controller/' + uriPath;
|
|
138
|
-
}
|
|
139
|
-
const cmd = uriPath.split('/').join(channelSeparator);
|
|
140
|
-
debug('[request] uri %s', cmd);
|
|
141
|
-
const args = (method == 'POST') ? body: params;
|
|
142
|
-
let fn = null;
|
|
143
|
-
if (is.string(cmd)) {
|
|
144
|
-
const actions = cmd.split(channelSeparator);
|
|
145
|
-
debug('[findFn] channel %o', actions);
|
|
146
|
-
let obj = { controller };
|
|
147
|
-
actions.forEach(key => {
|
|
148
|
-
obj = obj[key];
|
|
149
|
-
if (!obj) throw new Error(`class or function '${key}' not exists`);
|
|
150
|
-
});
|
|
151
|
-
fn = obj;
|
|
152
|
-
}
|
|
153
|
-
if (!fn) throw new Error('function not exists');
|
|
154
|
-
|
|
155
|
-
const result = await fn.call(controller, args, ctx);
|
|
156
|
-
ctx.response.body = result;
|
|
157
|
-
} catch (err) {
|
|
158
|
-
coreLogger.error('[ee-core/httpServer] throw error:', err);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
await next();
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
getHttpApp() {
|
|
165
|
-
return this.httpApp;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
// 设置错误处理函数
|
|
169
|
-
_setupErrorHandler(app, errorHandler) {
|
|
170
|
-
if (is.function(errorHandler)) {
|
|
171
|
-
app.on('error', errorHandler)
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* 加载前置、后置中间件
|
|
177
|
-
* @param {*} app koaApp示例
|
|
178
|
-
* @param {*} middlewares 中间件数组
|
|
179
|
-
* @param {*} type 类型,pre/post
|
|
180
|
-
*/
|
|
181
|
-
_loadMiddlewares(app, middlewares = [], type = 'pre') {
|
|
182
|
-
if (is.array(middlewares)) {
|
|
183
|
-
middlewares.forEach((middleware) => {
|
|
184
|
-
if (is.function(middleware)) {
|
|
185
|
-
// middleware是一个方法
|
|
186
|
-
// 返回值是中间件(ctx, next) => {}的异步函数
|
|
187
|
-
// 便于使用async/await进行同步编程
|
|
188
|
-
app.use(middleware());
|
|
189
|
-
} else {
|
|
190
|
-
coreLogger.warn(`[ee-core/httpServer] Invalid ${type} middleware detected, skipping.`);
|
|
191
|
-
}
|
|
192
|
-
})
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
module.exports = {
|
|
198
|
-
HttpServer
|
|
199
|
-
};
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const debug = require('debug')('ee-core:socket:httpServer');
|
|
4
|
+
const assert = require('assert');
|
|
5
|
+
const is = require('is-type-of');
|
|
6
|
+
const Koa = require('koa');
|
|
7
|
+
const cors = require('koa2-cors');
|
|
8
|
+
const koaBody = require('koa-body');
|
|
9
|
+
const https = require('https');
|
|
10
|
+
const fs = require('fs');
|
|
11
|
+
const path = require('path');
|
|
12
|
+
const _ = require('lodash');
|
|
13
|
+
const { coreLogger } = require('../log');
|
|
14
|
+
const { getBaseDir } = require('../ps');
|
|
15
|
+
const { getController } = require('../controller');
|
|
16
|
+
const { getConfig } = require('../config');
|
|
17
|
+
const { getPort } = require('../utils/port');
|
|
18
|
+
|
|
19
|
+
let channelSeparator = '/';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* http server
|
|
23
|
+
*/
|
|
24
|
+
class HttpServer {
|
|
25
|
+
constructor () {
|
|
26
|
+
const { httpServer, mainServer } = getConfig();
|
|
27
|
+
this.config = httpServer;
|
|
28
|
+
channelSeparator = mainServer.channelSeparator;
|
|
29
|
+
this.httpApp = undefined;
|
|
30
|
+
this.init();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async init() {
|
|
34
|
+
if (this.config.enable == false) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const port = await getPort({port: parseInt(this.config.port)});
|
|
39
|
+
if (!port) {
|
|
40
|
+
throw new Error('[ee-core] [socket/HttpServer] http port required, and must be a number !');
|
|
41
|
+
}
|
|
42
|
+
process.env.EE_HTTP_PORT = port;
|
|
43
|
+
this.config.port = port;
|
|
44
|
+
|
|
45
|
+
this._create();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 创建服务
|
|
50
|
+
*/
|
|
51
|
+
_create () {
|
|
52
|
+
const config = this.config;
|
|
53
|
+
// config.default 增加koaConfig配置项
|
|
54
|
+
const koaConfig = config.koaConfig || {};
|
|
55
|
+
const { preMiddleware = [], postMiddleware = [], errorHandler = null } = koaConfig;
|
|
56
|
+
const isHttps = config?.https?.enable ?? false;
|
|
57
|
+
const sslOptions = {};
|
|
58
|
+
|
|
59
|
+
if (isHttps === true) {
|
|
60
|
+
config.protocol = 'https://';
|
|
61
|
+
const keyFile = path.join(getBaseDir(), config.https.key);
|
|
62
|
+
const certFile = path.join(getBaseDir(), config.https.cert);
|
|
63
|
+
assert(fs.existsSync(keyFile), 'ssl key file is required');
|
|
64
|
+
assert(fs.existsSync(certFile), 'ssl cert file is required');
|
|
65
|
+
|
|
66
|
+
sslOptions.key = fs.readFileSync(keyFile);
|
|
67
|
+
sslOptions.cert = fs.readFileSync(certFile);
|
|
68
|
+
}
|
|
69
|
+
const url = config.protocol + config.host + ':' + config.port;
|
|
70
|
+
const corsOptions = config.cors;
|
|
71
|
+
|
|
72
|
+
const koaApp = new Koa();
|
|
73
|
+
|
|
74
|
+
// 设置错误处理器,便于统一错误代码处理
|
|
75
|
+
this._setupErrorHandler(koaApp, errorHandler);
|
|
76
|
+
|
|
77
|
+
// 加载前置中间件
|
|
78
|
+
this._loadMiddlewares(koaApp, preMiddleware);
|
|
79
|
+
|
|
80
|
+
// 核心中间件
|
|
81
|
+
koaApp
|
|
82
|
+
.use(cors(corsOptions))
|
|
83
|
+
.use(koaBody(config.body))
|
|
84
|
+
.use(this._dispatch);
|
|
85
|
+
|
|
86
|
+
// 加载后置中间件
|
|
87
|
+
this._loadMiddlewares(koaApp, postMiddleware, 'post');
|
|
88
|
+
|
|
89
|
+
let msg = '[ee-core] [socket/http] server is: ' + url;
|
|
90
|
+
const listenOpt = {
|
|
91
|
+
host: config.host,
|
|
92
|
+
port: config.port
|
|
93
|
+
}
|
|
94
|
+
if (isHttps) {
|
|
95
|
+
https.createServer(sslOptions, koaApp.callback()).listen(listenOpt, (err) => {
|
|
96
|
+
msg = err ? err : msg;
|
|
97
|
+
coreLogger.info(msg);
|
|
98
|
+
});
|
|
99
|
+
} else {
|
|
100
|
+
koaApp.listen(listenOpt, (e) => {
|
|
101
|
+
msg = e ? e : msg;
|
|
102
|
+
coreLogger.info(msg);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
this.httpApp = koaApp;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* 路由分发
|
|
111
|
+
*/
|
|
112
|
+
async _dispatch (ctx, next) {
|
|
113
|
+
const controller = getController();
|
|
114
|
+
const { filterRequest } = getConfig().httpServer;
|
|
115
|
+
let uriPath = ctx.request.path;
|
|
116
|
+
const method = ctx.request.method;
|
|
117
|
+
let params = ctx.request.query;
|
|
118
|
+
params = is.object(params) ? JSON.parse(JSON.stringify(params)) : {};
|
|
119
|
+
const body = ctx.request.body;
|
|
120
|
+
|
|
121
|
+
// 默认
|
|
122
|
+
ctx.response.status = 200;
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
// 找函数
|
|
126
|
+
// 去除开头的 '/'
|
|
127
|
+
if (uriPath.indexOf('/') == 0) {
|
|
128
|
+
uriPath = uriPath.substring(1);
|
|
129
|
+
}
|
|
130
|
+
// 过滤
|
|
131
|
+
if (_.includes(filterRequest.uris, uriPath)) {
|
|
132
|
+
ctx.response.body = filterRequest.returnData;
|
|
133
|
+
await next();
|
|
134
|
+
return
|
|
135
|
+
}
|
|
136
|
+
if (uriPath.slice(0, 10) != 'controller') {
|
|
137
|
+
uriPath = 'controller/' + uriPath;
|
|
138
|
+
}
|
|
139
|
+
const cmd = uriPath.split('/').join(channelSeparator);
|
|
140
|
+
debug('[request] uri %s', cmd);
|
|
141
|
+
const args = (method == 'POST') ? body: params;
|
|
142
|
+
let fn = null;
|
|
143
|
+
if (is.string(cmd)) {
|
|
144
|
+
const actions = cmd.split(channelSeparator);
|
|
145
|
+
debug('[findFn] channel %o', actions);
|
|
146
|
+
let obj = { controller };
|
|
147
|
+
actions.forEach(key => {
|
|
148
|
+
obj = obj[key];
|
|
149
|
+
if (!obj) throw new Error(`class or function '${key}' not exists`);
|
|
150
|
+
});
|
|
151
|
+
fn = obj;
|
|
152
|
+
}
|
|
153
|
+
if (!fn) throw new Error('function not exists');
|
|
154
|
+
|
|
155
|
+
const result = await fn.call(controller, args, ctx);
|
|
156
|
+
ctx.response.body = result;
|
|
157
|
+
} catch (err) {
|
|
158
|
+
coreLogger.error('[ee-core/httpServer] throw error:', err);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
await next();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
getHttpApp() {
|
|
165
|
+
return this.httpApp;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// 设置错误处理函数
|
|
169
|
+
_setupErrorHandler(app, errorHandler) {
|
|
170
|
+
if (is.function(errorHandler)) {
|
|
171
|
+
app.on('error', errorHandler)
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* 加载前置、后置中间件
|
|
177
|
+
* @param {*} app koaApp示例
|
|
178
|
+
* @param {*} middlewares 中间件数组
|
|
179
|
+
* @param {*} type 类型,pre/post
|
|
180
|
+
*/
|
|
181
|
+
_loadMiddlewares(app, middlewares = [], type = 'pre') {
|
|
182
|
+
if (is.array(middlewares)) {
|
|
183
|
+
middlewares.forEach((middleware) => {
|
|
184
|
+
if (is.function(middleware)) {
|
|
185
|
+
// middleware是一个方法
|
|
186
|
+
// 返回值是中间件(ctx, next) => {}的异步函数
|
|
187
|
+
// 便于使用async/await进行同步编程
|
|
188
|
+
app.use(middleware());
|
|
189
|
+
} else {
|
|
190
|
+
coreLogger.warn(`[ee-core/httpServer] Invalid ${type} middleware detected, skipping.`);
|
|
191
|
+
}
|
|
192
|
+
})
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
module.exports = {
|
|
198
|
+
HttpServer
|
|
199
|
+
};
|
package/socket/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import IoServer = require("socket.io");
|
|
2
|
-
import IoClient = require("socket.io-client");
|
|
3
|
-
import { HttpServer } from './httpServer';
|
|
4
|
-
import { IpcServer } from './ipcServer';
|
|
5
|
-
import { SocketServer } from './socketServer';
|
|
6
|
-
export declare function loadSocket(): void;
|
|
7
|
-
export declare function getSocketServer(): SocketServer;
|
|
8
|
-
export declare function getHttpServer(): HttpServer;
|
|
9
|
-
export declare function getIpcServer(): IpcServer;
|
|
10
|
-
export { IoServer, IoClient };
|
|
1
|
+
import IoServer = require("socket.io");
|
|
2
|
+
import IoClient = require("socket.io-client");
|
|
3
|
+
import { HttpServer } from './httpServer';
|
|
4
|
+
import { IpcServer } from './ipcServer';
|
|
5
|
+
import { SocketServer } from './socketServer';
|
|
6
|
+
export declare function loadSocket(): void;
|
|
7
|
+
export declare function getSocketServer(): SocketServer;
|
|
8
|
+
export declare function getHttpServer(): HttpServer;
|
|
9
|
+
export declare function getIpcServer(): IpcServer;
|
|
10
|
+
export { IoServer, IoClient };
|
package/socket/index.js
CHANGED
|
@@ -1,65 +1,65 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const IoServer = require('socket.io');
|
|
4
|
-
const IoClient = require('socket.io-client');
|
|
5
|
-
const Koa = require('koa');
|
|
6
|
-
const { SocketServer } = require('./socketServer');
|
|
7
|
-
const { HttpServer } = require('./httpServer');
|
|
8
|
-
const { IpcServer } = require('./ipcServer');
|
|
9
|
-
|
|
10
|
-
const Instance = {
|
|
11
|
-
socketServer: null,
|
|
12
|
-
httpServer: null,
|
|
13
|
-
coreLogger: null,
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
// create SocketServer
|
|
17
|
-
function createSocketServer() {
|
|
18
|
-
Instance.socketServer = new SocketServer();
|
|
19
|
-
return Instance.socketServer;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// socketServer
|
|
23
|
-
function getSocketServer() {
|
|
24
|
-
return Instance.socketServer;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// create Http Server
|
|
28
|
-
function createHttpServer() {
|
|
29
|
-
Instance.httpServer = new HttpServer();
|
|
30
|
-
return Instance.httpServer;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// httpServer
|
|
34
|
-
function getHttpServer() {
|
|
35
|
-
return Instance.httpServer;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// create IPC Server
|
|
39
|
-
function createIpcServer() {
|
|
40
|
-
Instance.ipcServer = new IpcServer();
|
|
41
|
-
return Instance.ipcServer;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// ipcServer
|
|
45
|
-
function getIpcServer() {
|
|
46
|
-
return Instance.ipcServer;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
// load socket server
|
|
51
|
-
function loadSocket() {
|
|
52
|
-
createSocketServer();
|
|
53
|
-
createHttpServer();
|
|
54
|
-
createIpcServer();
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
module.exports = {
|
|
58
|
-
Koa,
|
|
59
|
-
IoServer,
|
|
60
|
-
IoClient,
|
|
61
|
-
loadSocket,
|
|
62
|
-
getSocketServer,
|
|
63
|
-
getHttpServer,
|
|
64
|
-
getIpcServer
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const IoServer = require('socket.io');
|
|
4
|
+
const IoClient = require('socket.io-client');
|
|
5
|
+
const Koa = require('koa');
|
|
6
|
+
const { SocketServer } = require('./socketServer');
|
|
7
|
+
const { HttpServer } = require('./httpServer');
|
|
8
|
+
const { IpcServer } = require('./ipcServer');
|
|
9
|
+
|
|
10
|
+
const Instance = {
|
|
11
|
+
socketServer: null,
|
|
12
|
+
httpServer: null,
|
|
13
|
+
coreLogger: null,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// create SocketServer
|
|
17
|
+
function createSocketServer() {
|
|
18
|
+
Instance.socketServer = new SocketServer();
|
|
19
|
+
return Instance.socketServer;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// socketServer
|
|
23
|
+
function getSocketServer() {
|
|
24
|
+
return Instance.socketServer;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// create Http Server
|
|
28
|
+
function createHttpServer() {
|
|
29
|
+
Instance.httpServer = new HttpServer();
|
|
30
|
+
return Instance.httpServer;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// httpServer
|
|
34
|
+
function getHttpServer() {
|
|
35
|
+
return Instance.httpServer;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// create IPC Server
|
|
39
|
+
function createIpcServer() {
|
|
40
|
+
Instance.ipcServer = new IpcServer();
|
|
41
|
+
return Instance.ipcServer;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// ipcServer
|
|
45
|
+
function getIpcServer() {
|
|
46
|
+
return Instance.ipcServer;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
// load socket server
|
|
51
|
+
function loadSocket() {
|
|
52
|
+
createSocketServer();
|
|
53
|
+
createHttpServer();
|
|
54
|
+
createIpcServer();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
module.exports = {
|
|
58
|
+
Koa,
|
|
59
|
+
IoServer,
|
|
60
|
+
IoClient,
|
|
61
|
+
loadSocket,
|
|
62
|
+
getSocketServer,
|
|
63
|
+
getHttpServer,
|
|
64
|
+
getIpcServer
|
|
65
65
|
};
|
package/socket/ipcServer.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
export declare class IpcServer {
|
|
2
|
-
directory: string;
|
|
3
|
-
loop(obj: any, pathname: string): void;
|
|
4
|
-
register(exportObj: any, propertyChain: any): void;
|
|
5
|
-
findFn(controller: any, c: any): {
|
|
6
|
-
controller: any;
|
|
7
|
-
};
|
|
8
|
-
init(): void;
|
|
9
|
-
}
|
|
1
|
+
export declare class IpcServer {
|
|
2
|
+
directory: string;
|
|
3
|
+
loop(obj: any, pathname: string): void;
|
|
4
|
+
register(exportObj: any, propertyChain: any): void;
|
|
5
|
+
findFn(controller: any, c: any): {
|
|
6
|
+
controller: any;
|
|
7
|
+
};
|
|
8
|
+
init(): void;
|
|
9
|
+
}
|