ee-core 4.1.1 → 4.1.3

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.
@@ -1,6 +1,5 @@
1
1
  'use strict';
2
2
 
3
- const debug = require('debug')('ee-core:electron:app');
4
3
  const { app: electronApp } = require('electron');
5
4
  const { coreLogger } = require('../../log');
6
5
  const { is } = require('../../utils');
@@ -18,6 +17,7 @@ function createElectron() {
18
17
  const gotTheLock = electronApp.requestSingleInstanceLock();
19
18
  if (singleLock && !gotTheLock) {
20
19
  electronApp.quit();
20
+ return;
21
21
  }
22
22
 
23
23
  electronApp.whenReady().then(() => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ee-core",
3
- "version": "4.1.1",
3
+ "version": "4.1.3",
4
4
  "description": "ee core",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -8,9 +8,6 @@
8
8
  },
9
9
  "author": "",
10
10
  "license": "ISC",
11
- "bin": {
12
- "ee-core": "./bin/tools.js"
13
- },
14
11
  "dependencies": {
15
12
  "agentkeepalive": "^4.2.0",
16
13
  "axios": "^1.7.9",
@@ -50,6 +50,9 @@ class HttpServer {
50
50
  */
51
51
  _create () {
52
52
  const config = this.config;
53
+ // config.default 增加koaConfig配置项
54
+ const koaConfig = config.koaConfig || {};
55
+ const { preMiddleware = [], postMiddleware = [], errorHandler = null } = koaConfig;
53
56
  const isHttps = config?.https?.enable ?? false;
54
57
  const sslOptions = {};
55
58
 
@@ -67,11 +70,22 @@ class HttpServer {
67
70
  const corsOptions = config.cors;
68
71
 
69
72
  const koaApp = new Koa();
73
+
74
+ // 设置错误处理器,便于统一错误代码处理
75
+ this._setupErrorHandler(koaApp, errorHandler);
76
+
77
+ // 加载前置中间件
78
+ this._loadMiddlewares(koaApp, preMiddleware);
79
+
80
+ // 核心中间件
70
81
  koaApp
71
82
  .use(cors(corsOptions))
72
83
  .use(koaBody(config.body))
73
84
  .use(this._dispatch);
74
85
 
86
+ // 加载后置中间件
87
+ this._loadMiddlewares(koaApp, postMiddleware, 'post');
88
+
75
89
  let msg = '[ee-core] [socket/http] server is: ' + url;
76
90
  const listenOpt = {
77
91
  host: config.host,
@@ -88,7 +102,7 @@ class HttpServer {
88
102
  coreLogger.info(msg);
89
103
  });
90
104
  }
91
-
105
+
92
106
  this.httpApp = koaApp;
93
107
  }
94
108
 
@@ -150,6 +164,34 @@ class HttpServer {
150
164
  getHttpApp() {
151
165
  return this.httpApp;
152
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
+ }
153
195
  }
154
196
 
155
197
  module.exports = {