ee-core 2.11.1 → 4.0.0-beta.1
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/app/application.js +31 -0
- package/app/boot.js +81 -0
- package/app/dir.js +27 -0
- package/app/events.js +56 -0
- package/app/index.js +7 -0
- package/config/config_loader.js +66 -0
- package/config/default_config.js +110 -0
- package/config/index.js +21 -83
- package/const/channel.js +26 -16
- package/const/index.js +0 -4
- package/controller/controller_loader.js +78 -0
- package/controller/index.js +18 -19
- package/core/index.js +6 -5
- package/core/{lib/loader → loader}/file_loader.js +39 -156
- package/core/utils/index.js +83 -0
- package/core/{lib/utils → utils}/timing.js +3 -2
- package/cross/cross.js +152 -0
- package/cross/index.js +6 -181
- package/cross/spawnProcess.js +34 -51
- package/electron/app/index.js +38 -54
- package/electron/index.js +13 -17
- package/electron/window/index.js +260 -65
- package/exception/index.js +34 -32
- package/html/index.js +7 -10
- package/index.js +4 -52
- package/jobs/child/app.js +9 -9
- package/jobs/child/forkProcess.js +29 -45
- package/jobs/child/index.js +13 -10
- package/jobs/child-pool/index.js +13 -11
- package/jobs/index.js +2 -4
- package/loader/index.js +91 -144
- package/log/index.js +74 -57
- package/log/logger.js +76 -84
- package/message/childMessage.js +12 -13
- package/message/index.js +7 -16
- package/package.json +2 -2
- package/ps/index.js +137 -223
- package/socket/httpServer.js +46 -43
- package/socket/index.js +52 -69
- package/socket/ipcServer.js +80 -94
- package/socket/socketServer.js +31 -24
- package/storage/index.js +5 -37
- package/storage/sqliteStorage.js +18 -18
- package/utils/extend.js +10 -5
- package/utils/helper.js +38 -42
- package/utils/index.js +40 -23
- package/utils/ip.js +5 -45
- package/utils/is.js +107 -141
- package/utils/json.js +15 -7
- package/utils/pargv.js +5 -1
- package/utils/{get-port → port}/index.js +4 -26
- package/utils/wrap.js +8 -3
- package/addon/index.js +0 -35
- package/addon/window/index.js +0 -99
- package/bin/tools.js +0 -8
- package/config/cache.js +0 -42
- package/config/config.default.js +0 -331
- package/controller/baseContextClass.js +0 -25
- package/core/lib/ee.js +0 -216
- package/core/lib/loader/context_loader.js +0 -106
- package/core/lib/loader/ee_loader.js +0 -435
- package/core/lib/loader/mixin/addon.js +0 -32
- package/core/lib/loader/mixin/config.js +0 -130
- package/core/lib/loader/mixin/controller.js +0 -125
- package/core/lib/loader/mixin/service.js +0 -28
- package/core/lib/utils/base_context_class.js +0 -34
- package/core/lib/utils/function.js +0 -30
- package/core/lib/utils/index.js +0 -133
- package/core/lib/utils/sequencify.js +0 -59
- package/ee/appLoader.js +0 -48
- package/ee/application.js +0 -100
- package/ee/baseApp.js +0 -104
- package/ee/eeApp.js +0 -409
- package/ee/index.js +0 -58
- package/electron/window/winState.js +0 -186
- package/httpclient/index.js +0 -161
- package/jobs/baseJobClass.js +0 -16
- package/jobs/renderer/index.js +0 -141
- package/jobs/renderer/loadView.js +0 -41
- package/jobs/unification.js +0 -64
- package/main/index.js +0 -57
- package/old-utils/index.js +0 -91
- package/services/baseContextClass.js +0 -24
- package/services/index.js +0 -41
- package/socket/io.js +0 -28
- package/storage/jsondb/adapters/Base.js +0 -23
- package/storage/jsondb/adapters/FileSync.js +0 -64
- package/storage/jsondb/main.js +0 -55
- package/storage/jsondbStorage.js +0 -196
- package/utils/co.js +0 -237
- package/utils/copyto.js +0 -161
- package/utils/depd/index.js +0 -538
- package/utils/depd/lib/browser/index.js +0 -77
- package/utils/get-port/index.d.ts +0 -64
- package/utils/time/index.js +0 -20
- package/utils/time/ms.js +0 -162
package/ps/index.js
CHANGED
|
@@ -1,216 +1,131 @@
|
|
|
1
|
-
|
|
2
|
-
const eis = require('../utils/is');
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* 初始化模式
|
|
6
|
-
*/
|
|
7
|
-
exports.initMode = function(mode) {
|
|
8
|
-
if(process.env.EE_MODE !== undefined)return
|
|
9
|
-
return process.env.EE_MODE = mode ? mode : 'framework';
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* 当前模式 - framework | module
|
|
14
|
-
*/
|
|
15
|
-
exports.mode = function() {
|
|
16
|
-
return process.env.EE_MODE;
|
|
17
|
-
}
|
|
1
|
+
'use strict';
|
|
18
2
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
*/
|
|
22
|
-
exports.verifyMode = function(mode) {
|
|
23
|
-
if (['framework', 'module'].includes(mode)) {
|
|
24
|
-
return true;
|
|
25
|
-
}
|
|
26
|
-
return false;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* 是否为框架模式
|
|
31
|
-
*/
|
|
32
|
-
exports.isFrameworkMode = function() {
|
|
33
|
-
return (process.env.EE_MODE === 'framework');
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* 是否为模块模式
|
|
38
|
-
*/
|
|
39
|
-
exports.isModuleMode = function() {
|
|
40
|
-
return (process.env.EE_MODE === 'module');
|
|
41
|
-
}
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const is = require('../utils/is');
|
|
42
5
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
*/
|
|
46
|
-
exports.allEnv = function() {
|
|
6
|
+
// 当前进程的所有env
|
|
7
|
+
function allEnv() {
|
|
47
8
|
return process.env;
|
|
48
9
|
}
|
|
49
10
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
exports.env = function() {
|
|
54
|
-
return process.env.EE_SERVER_ENV;
|
|
11
|
+
// 当前环境 - local | prod
|
|
12
|
+
function env() {
|
|
13
|
+
return process.env.EE_ENV;
|
|
55
14
|
}
|
|
56
15
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
exports.getEnv = this.env
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* 是否生产环境
|
|
64
|
-
*/
|
|
65
|
-
exports.isProd = function() {
|
|
66
|
-
return (process.env.EE_SERVER_ENV === 'prod');
|
|
16
|
+
// 是否生产环境
|
|
17
|
+
function isProd() {
|
|
18
|
+
return (process.env.EE_ENV === 'prod');
|
|
67
19
|
}
|
|
68
20
|
|
|
69
21
|
/**
|
|
70
22
|
* 是否为开发环境
|
|
71
23
|
*/
|
|
72
|
-
|
|
73
|
-
if ( process.env.
|
|
74
|
-
process.env.EE_SERVER_ENV === 'dev' ||
|
|
75
|
-
process.env.EE_SERVER_ENV === 'local'
|
|
76
|
-
) {
|
|
77
|
-
return true;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
if ( process.env.NODE_ENV === 'development' ||
|
|
81
|
-
process.env.NODE_ENV === 'dev' ||
|
|
82
|
-
process.env.NODE_ENV === 'local'
|
|
83
|
-
) {
|
|
24
|
+
function isDev() {
|
|
25
|
+
if (process.env.EE_ENV === 'dev' || process.env.EE_ENV === 'local') {
|
|
84
26
|
return true;
|
|
85
27
|
}
|
|
86
28
|
|
|
87
29
|
return false;
|
|
88
30
|
};
|
|
89
31
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
*/
|
|
93
|
-
exports.isRenderer = function() {
|
|
32
|
+
// 是否为渲染进程
|
|
33
|
+
function isRenderer() {
|
|
94
34
|
return (typeof process === 'undefined' ||
|
|
95
35
|
!process ||
|
|
96
36
|
process.type === 'renderer');
|
|
97
37
|
};
|
|
98
38
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
*/
|
|
102
|
-
exports.isMain = function() {
|
|
39
|
+
// 是否为主进程
|
|
40
|
+
function isMain() {
|
|
103
41
|
return ( typeof process !== 'undefined' &&
|
|
104
42
|
process.type === 'browser');
|
|
105
43
|
};
|
|
106
44
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
*/
|
|
110
|
-
exports.isForkedChild = function() {
|
|
45
|
+
// 是否为node子进程
|
|
46
|
+
function isForkedChild() {
|
|
111
47
|
return (Number(process.env.ELECTRON_RUN_AS_NODE) === 1);
|
|
112
48
|
};
|
|
113
49
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
*/
|
|
117
|
-
exports.processType = function() {
|
|
50
|
+
// 当前进程类型
|
|
51
|
+
function processType() {
|
|
118
52
|
let type = '';
|
|
119
|
-
if (
|
|
53
|
+
if (isMain()) {
|
|
120
54
|
type = 'browser';
|
|
121
|
-
} else if (
|
|
55
|
+
} else if (isRenderer()) {
|
|
122
56
|
type = 'renderer';
|
|
123
|
-
} else if (
|
|
57
|
+
} else if (isForkedChild()) {
|
|
124
58
|
type = 'child';
|
|
125
59
|
}
|
|
126
60
|
|
|
127
61
|
return type;
|
|
128
62
|
};
|
|
129
63
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
*/
|
|
133
|
-
exports.appName = function() {
|
|
64
|
+
// app name
|
|
65
|
+
function appName() {
|
|
134
66
|
return process.env.EE_APP_NAME;
|
|
135
67
|
}
|
|
136
68
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
exports.getHomeDir = function () {
|
|
141
|
-
return process.env.EE_HOME;
|
|
69
|
+
// app version
|
|
70
|
+
function appVersion() {
|
|
71
|
+
return process.env.EE_APP_VERSION;
|
|
142
72
|
}
|
|
143
73
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
return storageDir;
|
|
74
|
+
// 获取数据存储路径
|
|
75
|
+
function getDataDir() {
|
|
76
|
+
const base = isDev() ? getBaseDir() : getUserHomeHiddenAppDir();
|
|
77
|
+
const dataDir = path.join(base, 'data');
|
|
78
|
+
return dataDir;
|
|
150
79
|
}
|
|
151
80
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
const dir = path.join(this.getRootDir(), 'logs');
|
|
81
|
+
// 获取日志存储路径
|
|
82
|
+
function getLogDir() {
|
|
83
|
+
const base = isDev() ? getBaseDir() : getUserHomeHiddenAppDir();
|
|
84
|
+
const dir = path.join(base, 'logs');
|
|
157
85
|
return dir;
|
|
158
86
|
}
|
|
159
87
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
*/
|
|
163
|
-
exports.getEncryptDir = function (basePath) {
|
|
88
|
+
// 获取bundle文件路径
|
|
89
|
+
function getBundleDir(basePath) {
|
|
164
90
|
const base = basePath || process.cwd();
|
|
165
91
|
const dir = path.join(base, 'public', 'electron');
|
|
166
92
|
return dir;
|
|
167
93
|
}
|
|
168
94
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
exports.getRootDir = function () {
|
|
173
|
-
const appDir = this.isDev() ? process.env.EE_HOME : process.env.EE_APP_USER_DATA;
|
|
95
|
+
// 获取root目录 (dev-项目根目录,pro-app user data目录)
|
|
96
|
+
function getRootDir() {
|
|
97
|
+
const appDir = isDev() ? getBaseDir() : getAppUserDataDir();
|
|
174
98
|
return appDir;
|
|
175
99
|
}
|
|
176
100
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
*/
|
|
180
|
-
exports.getBaseDir = function() {
|
|
101
|
+
// 获取base目录
|
|
102
|
+
function getBaseDir() {
|
|
181
103
|
return process.env.EE_BASE_DIR;
|
|
182
104
|
}
|
|
183
105
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
exports.getElectronDir = function() {
|
|
188
|
-
return process.env.EE_BASE_DIR;
|
|
106
|
+
// 获取electron目录
|
|
107
|
+
function getElectronDir() {
|
|
108
|
+
return process.env.EE_ELECTRON_DIR;
|
|
189
109
|
}
|
|
190
110
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
exports.getPublicDir = function() {
|
|
195
|
-
const dir = path.join(process.env.EE_HOME, "public");
|
|
111
|
+
// 获取public目录
|
|
112
|
+
function getPublicDir() {
|
|
113
|
+
const dir = path.join(getBaseDir(), "public");
|
|
196
114
|
return dir;
|
|
197
115
|
}
|
|
198
116
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
const execDir = this.getExecDir();
|
|
204
|
-
const isPackaged = this.isPackaged();
|
|
205
|
-
|
|
117
|
+
// 获取 额外资源目录
|
|
118
|
+
function getExtraResourcesDir() {
|
|
119
|
+
const execDir = getExecDir();
|
|
120
|
+
const packaged = isPackaged();
|
|
206
121
|
|
|
207
122
|
// 资源路径不同
|
|
208
123
|
let dir = '';
|
|
209
|
-
if (
|
|
124
|
+
if (packaged) {
|
|
210
125
|
// 打包后 execDir为 应用程序 exe\dmg\dep软件所在目录;打包前该值是项目根目录
|
|
211
126
|
// windows和MacOs不一样
|
|
212
127
|
dir = path.join(execDir, "resources", "extraResources");
|
|
213
|
-
if (
|
|
128
|
+
if (is.macOS()) {
|
|
214
129
|
dir = path.join(execDir, "..", "Resources", "extraResources");
|
|
215
130
|
}
|
|
216
131
|
} else {
|
|
@@ -220,105 +135,64 @@ exports.getExtraResourcesDir = function() {
|
|
|
220
135
|
return dir;
|
|
221
136
|
}
|
|
222
137
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
*/
|
|
226
|
-
exports.getAppUserDataDir = function() {
|
|
138
|
+
// 获取 appUserData目录
|
|
139
|
+
function getAppUserDataDir() {
|
|
227
140
|
return process.env.EE_APP_USER_DATA;
|
|
228
141
|
}
|
|
229
142
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
*/
|
|
233
|
-
exports.getExecDir = function() {
|
|
143
|
+
// 获取 exec目录
|
|
144
|
+
function getExecDir() {
|
|
234
145
|
return process.env.EE_EXEC_DIR;
|
|
235
146
|
}
|
|
236
147
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
*/
|
|
240
|
-
exports.getUserHomeDir = function() {
|
|
148
|
+
// 获取操作系统用户目录
|
|
149
|
+
function getUserHomeDir() {
|
|
241
150
|
return process.env.EE_USER_HOME;
|
|
242
151
|
}
|
|
243
152
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
// if (!fs.existsSync(filePath)) {
|
|
250
|
-
// throw new Error(filePath + ' is not found');
|
|
251
|
-
// }
|
|
252
|
-
// const pkg = JSON.parse(fs.readFileSync(filePath));
|
|
253
|
-
// if (!pkg.name || pkg.name == "") {
|
|
254
|
-
// throw new Error(`name is required from ${filePath}`);
|
|
255
|
-
// }
|
|
256
|
-
const appname = this.appName();
|
|
257
|
-
const cfgDir = path.join(this.getUserHomeDir(), ".config", appname);
|
|
258
|
-
return cfgDir;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* 获取基础数据路径
|
|
263
|
-
*/
|
|
264
|
-
exports.getUserHomeAppFilePath = function() {
|
|
265
|
-
const p = path.join(this.getUserHomeConfigDir(), "app.json");
|
|
266
|
-
return p;
|
|
153
|
+
// 获取用户家目录中的隐藏的app目录
|
|
154
|
+
function getUserHomeHiddenAppDir() {
|
|
155
|
+
const appnameDir = "." + appName();
|
|
156
|
+
const dir = path.join(getUserHomeDir(), appnameDir);
|
|
157
|
+
return dir;
|
|
267
158
|
}
|
|
268
159
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
return
|
|
160
|
+
// 获取用户家目录中的app目录
|
|
161
|
+
function getUserHomeAppDir() {
|
|
162
|
+
const appnameDir = appName();
|
|
163
|
+
const dir = path.join(getUserHomeDir(), appnameDir);
|
|
164
|
+
return dir;
|
|
274
165
|
}
|
|
275
166
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
*/
|
|
279
|
-
exports.getSocketPort = function () {
|
|
167
|
+
// 获取内置socket端口
|
|
168
|
+
function getSocketPort() {
|
|
280
169
|
return parseInt(process.env.EE_SOCKET_PORT) || 0;
|
|
281
170
|
}
|
|
282
171
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
*/
|
|
286
|
-
exports.getHttpPort = function () {
|
|
172
|
+
// 获取内置http端口
|
|
173
|
+
function getHttpPort() {
|
|
287
174
|
return parseInt(process.env.EE_HTTP_PORT) || 0;
|
|
288
175
|
}
|
|
289
176
|
|
|
290
177
|
/**
|
|
291
178
|
* 是否打包
|
|
292
179
|
*/
|
|
293
|
-
|
|
180
|
+
function isPackaged() {
|
|
294
181
|
return process.env.EE_IS_PACKAGED === 'true';
|
|
295
182
|
}
|
|
296
183
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
*/
|
|
300
|
-
exports.isEncrypted = function () {
|
|
301
|
-
return process.env.EE_IS_ENCRYPTED === 'true';
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
/**
|
|
305
|
-
* 是否热重启
|
|
306
|
-
*/
|
|
307
|
-
exports.isHotReload = function () {
|
|
184
|
+
// 是否热重启
|
|
185
|
+
function isHotReload() {
|
|
308
186
|
return process.env.HOT_RELOAD === 'true';
|
|
309
187
|
}
|
|
310
188
|
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
*/
|
|
314
|
-
exports.exit = function(code = 0) {
|
|
189
|
+
// 进程退出
|
|
190
|
+
function exit(code = 0) {
|
|
315
191
|
return process.exit(code);
|
|
316
192
|
}
|
|
317
193
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
*/
|
|
321
|
-
exports.makeMessage = function(msg = {}) {
|
|
194
|
+
// 格式化message
|
|
195
|
+
function makeMessage(msg = {}) {
|
|
322
196
|
let message = Object.assign({
|
|
323
197
|
channel: '',
|
|
324
198
|
event: '',
|
|
@@ -328,10 +202,8 @@ exports.makeMessage = function(msg = {}) {
|
|
|
328
202
|
return message;
|
|
329
203
|
}
|
|
330
204
|
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
*/
|
|
334
|
-
exports.exitChildJob = function(code = 0) {
|
|
205
|
+
// 退出ChildJob进程
|
|
206
|
+
function exitChildJob(code = 0) {
|
|
335
207
|
try {
|
|
336
208
|
let args = JSON.parse(process.argv[2]);
|
|
337
209
|
if (args.type == 'childJob') {
|
|
@@ -342,11 +214,8 @@ exports.exitChildJob = function(code = 0) {
|
|
|
342
214
|
}
|
|
343
215
|
}
|
|
344
216
|
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
* 任务类型 ChildJob
|
|
348
|
-
*/
|
|
349
|
-
exports.isChildJob = function() {
|
|
217
|
+
// 任务类型 ChildJob
|
|
218
|
+
function isChildJob() {
|
|
350
219
|
try {
|
|
351
220
|
let args = JSON.parse(process.argv[2]);
|
|
352
221
|
if (args.type == 'childJob') {
|
|
@@ -357,10 +226,8 @@ exports.isChildJob = function() {
|
|
|
357
226
|
}
|
|
358
227
|
}
|
|
359
228
|
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
*/
|
|
363
|
-
exports.isChildPoolJob = function() {
|
|
229
|
+
// 任务类型 ChildPoolJob
|
|
230
|
+
function isChildPoolJob() {
|
|
364
231
|
try {
|
|
365
232
|
let args = JSON.parse(process.argv[2]);
|
|
366
233
|
if (args.type == 'childPoolJob') {
|
|
@@ -369,4 +236,51 @@ exports.isChildPoolJob = function() {
|
|
|
369
236
|
} catch (e) {
|
|
370
237
|
return false;
|
|
371
238
|
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Get cmd parameter by name
|
|
242
|
+
function getArgumentByName(name) {
|
|
243
|
+
for (let i = 0; i < process.argv.length; i++) {
|
|
244
|
+
const item = process.argv[i]
|
|
245
|
+
const prefixKey = `--${name}=`;
|
|
246
|
+
if (item.indexOf(prefixKey) !== -1) {
|
|
247
|
+
return item.substring(prefixKey.length);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
module.exports = {
|
|
253
|
+
allEnv,
|
|
254
|
+
env,
|
|
255
|
+
isProd,
|
|
256
|
+
isDev,
|
|
257
|
+
isRenderer,
|
|
258
|
+
isMain,
|
|
259
|
+
isForkedChild,
|
|
260
|
+
processType,
|
|
261
|
+
appName,
|
|
262
|
+
appVersion,
|
|
263
|
+
getDataDir,
|
|
264
|
+
getLogDir,
|
|
265
|
+
getBundleDir,
|
|
266
|
+
getRootDir,
|
|
267
|
+
getBaseDir,
|
|
268
|
+
getElectronDir,
|
|
269
|
+
getPublicDir,
|
|
270
|
+
getExtraResourcesDir,
|
|
271
|
+
getAppUserDataDir,
|
|
272
|
+
getExecDir,
|
|
273
|
+
getUserHomeDir,
|
|
274
|
+
getUserHomeAppDir,
|
|
275
|
+
getUserHomeHiddenAppDir,
|
|
276
|
+
getSocketPort,
|
|
277
|
+
getHttpPort,
|
|
278
|
+
isPackaged,
|
|
279
|
+
isHotReload,
|
|
280
|
+
exit,
|
|
281
|
+
makeMessage,
|
|
282
|
+
exitChildJob,
|
|
283
|
+
isChildJob,
|
|
284
|
+
isChildPoolJob,
|
|
285
|
+
getArgumentByName
|
|
372
286
|
}
|
package/socket/httpServer.js
CHANGED
|
@@ -9,25 +9,33 @@ const https = require('https');
|
|
|
9
9
|
const fs = require('fs');
|
|
10
10
|
const path = require('path');
|
|
11
11
|
const _ = require('lodash');
|
|
12
|
-
const
|
|
13
|
-
const
|
|
12
|
+
const { coreLogger } = require('../log');
|
|
13
|
+
const { getBaseDir } = require('../ps');
|
|
14
|
+
const { getController } = require('../controller');
|
|
15
|
+
const { getConfig } = require('../config');
|
|
16
|
+
const { getPort } = require('../utils/port');
|
|
14
17
|
|
|
15
18
|
/**
|
|
16
19
|
* http server
|
|
17
20
|
*/
|
|
18
21
|
class HttpServer {
|
|
19
|
-
constructor (
|
|
20
|
-
this.
|
|
21
|
-
this.
|
|
22
|
+
constructor () {
|
|
23
|
+
this.config = getConfig().httpServer;
|
|
24
|
+
this.httpApp = undefined;
|
|
25
|
+
this.init();
|
|
26
|
+
}
|
|
22
27
|
|
|
23
|
-
|
|
28
|
+
async init() {
|
|
29
|
+
if (this.config.enable == false) {
|
|
24
30
|
return;
|
|
25
31
|
}
|
|
26
32
|
|
|
27
|
-
|
|
33
|
+
const port = await getPort({port: parseInt(this.config.port)});
|
|
28
34
|
if (!port) {
|
|
29
35
|
throw new Error('[ee-core] [socket/HttpServer] http port required, and must be a number !');
|
|
30
36
|
}
|
|
37
|
+
process.env.EE_HTTP_PORT = port;
|
|
38
|
+
this.config.port = port;
|
|
31
39
|
|
|
32
40
|
this._create();
|
|
33
41
|
}
|
|
@@ -36,72 +44,64 @@ class HttpServer {
|
|
|
36
44
|
* 创建服务
|
|
37
45
|
*/
|
|
38
46
|
_create () {
|
|
39
|
-
const
|
|
40
|
-
const
|
|
41
|
-
const
|
|
42
|
-
let sslOptions = {};
|
|
47
|
+
const config = this.config;
|
|
48
|
+
const isHttps = config?.https?.enable ?? false;
|
|
49
|
+
const sslOptions = {};
|
|
43
50
|
|
|
44
51
|
if (isHttps === true) {
|
|
45
|
-
|
|
46
|
-
const keyFile = path.join(
|
|
47
|
-
const certFile = path.join(
|
|
52
|
+
config.protocol = 'https://';
|
|
53
|
+
const keyFile = path.join(getBaseDir(), config.https.key);
|
|
54
|
+
const certFile = path.join(getBaseDir(), config.https.cert);
|
|
48
55
|
assert(fs.existsSync(keyFile), 'ssl key file is required');
|
|
49
56
|
assert(fs.existsSync(certFile), 'ssl cert file is required');
|
|
50
57
|
|
|
51
58
|
sslOptions.key = fs.readFileSync(keyFile);
|
|
52
59
|
sslOptions.cert = fs.readFileSync(certFile);
|
|
53
60
|
}
|
|
54
|
-
const url =
|
|
55
|
-
const corsOptions =
|
|
61
|
+
const url = config.protocol + config.host + ':' + config.port;
|
|
62
|
+
const corsOptions = config.cors;
|
|
56
63
|
|
|
57
64
|
const koaApp = new Koa();
|
|
58
65
|
koaApp
|
|
59
66
|
.use(cors(corsOptions))
|
|
60
|
-
.use(koaBody(
|
|
61
|
-
.use(async (ctx, next) => {
|
|
62
|
-
ctx.eeApp = app;
|
|
63
|
-
await next();
|
|
64
|
-
})
|
|
67
|
+
.use(koaBody(config.body))
|
|
65
68
|
.use(this._dispatch);
|
|
66
69
|
|
|
67
70
|
let msg = '[ee-core] [socket/http] server is: ' + url;
|
|
68
|
-
|
|
69
71
|
const listenOpt = {
|
|
70
|
-
host:
|
|
71
|
-
port:
|
|
72
|
+
host: config.host,
|
|
73
|
+
port: config.port
|
|
72
74
|
}
|
|
73
75
|
if (isHttps) {
|
|
74
76
|
https.createServer(sslOptions, koaApp.callback()).listen(listenOpt, (err) => {
|
|
75
77
|
msg = err ? err : msg;
|
|
76
|
-
|
|
78
|
+
coreLogger.info(msg);
|
|
77
79
|
});
|
|
78
80
|
} else {
|
|
79
81
|
koaApp.listen(listenOpt, (e) => {
|
|
80
82
|
msg = e ? e : msg;
|
|
81
|
-
|
|
83
|
+
coreLogger.info(msg);
|
|
82
84
|
});
|
|
83
|
-
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
this.httpApp = koaApp;
|
|
84
88
|
}
|
|
85
89
|
|
|
86
90
|
/**
|
|
87
91
|
* 路由分发
|
|
88
92
|
*/
|
|
89
93
|
async _dispatch (ctx, next) {
|
|
90
|
-
const
|
|
94
|
+
const controller = getController();
|
|
95
|
+
const { filterRequest } = getConfig().httpServer;
|
|
91
96
|
let uriPath = ctx.request.path;
|
|
92
97
|
const method = ctx.request.method;
|
|
93
98
|
let params = ctx.request.query;
|
|
94
99
|
params = is.object(params) ? JSON.parse(JSON.stringify(params)) : {};
|
|
95
100
|
const body = ctx.request.body;
|
|
96
|
-
const files = ctx.request.files;
|
|
97
101
|
|
|
98
102
|
// 默认
|
|
99
103
|
ctx.response.status = 200;
|
|
100
104
|
|
|
101
|
-
// 添加到全局属性
|
|
102
|
-
ctx.eeApp.request = ctx.request;
|
|
103
|
-
ctx.eeApp.response = ctx.response;
|
|
104
|
-
|
|
105
105
|
try {
|
|
106
106
|
// 找函数
|
|
107
107
|
// 去除开头的 '/'
|
|
@@ -109,8 +109,8 @@ class HttpServer {
|
|
|
109
109
|
uriPath = uriPath.substring(1);
|
|
110
110
|
}
|
|
111
111
|
// 过滤
|
|
112
|
-
if (_.includes(
|
|
113
|
-
ctx.response.body =
|
|
112
|
+
if (_.includes(filterRequest.uris, uriPath)) {
|
|
113
|
+
ctx.response.body = filterRequest.returnData;
|
|
114
114
|
await next();
|
|
115
115
|
return
|
|
116
116
|
}
|
|
@@ -118,14 +118,11 @@ class HttpServer {
|
|
|
118
118
|
uriPath = 'controller/' + uriPath;
|
|
119
119
|
}
|
|
120
120
|
const cmd = uriPath.split('/').join('.');
|
|
121
|
-
const args = (method == 'POST') ?
|
|
122
|
-
args.files = ctx.request.files;
|
|
123
|
-
args.body = ctx.request.body;
|
|
124
|
-
args.query = ctx.request.query;
|
|
121
|
+
const args = (method == 'POST') ? body: params;
|
|
125
122
|
let fn = null;
|
|
126
123
|
if (is.string(cmd)) {
|
|
127
124
|
const actions = cmd.split('.');
|
|
128
|
-
let obj =
|
|
125
|
+
let obj = { controller };
|
|
129
126
|
actions.forEach(key => {
|
|
130
127
|
obj = obj[key];
|
|
131
128
|
if (!obj) throw new Error(`class or function '${key}' not exists`);
|
|
@@ -134,14 +131,20 @@ class HttpServer {
|
|
|
134
131
|
}
|
|
135
132
|
if (!fn) throw new Error('function not exists');
|
|
136
133
|
|
|
137
|
-
const result = await fn.call(
|
|
134
|
+
const result = await fn.call(controller, args, ctx);
|
|
138
135
|
ctx.response.body = result;
|
|
139
136
|
} catch (err) {
|
|
140
|
-
|
|
137
|
+
coreLogger.error('[ee-core/httpServer] throw error:', err);
|
|
141
138
|
}
|
|
142
139
|
|
|
143
140
|
await next();
|
|
144
141
|
}
|
|
142
|
+
|
|
143
|
+
getHttpApp() {
|
|
144
|
+
return this.httpApp;
|
|
145
|
+
}
|
|
145
146
|
}
|
|
146
147
|
|
|
147
|
-
module.exports =
|
|
148
|
+
module.exports = {
|
|
149
|
+
HttpServer
|
|
150
|
+
};
|