ee-core 2.12.0 → 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 +19 -23
- package/config/default_config.js +110 -0
- package/config/index.js +20 -37
- 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 +10 -10
- 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 -143
- package/log/index.js +74 -57
- package/log/logger.js +75 -80
- package/message/childMessage.js +13 -13
- package/message/index.js +7 -16
- package/package.json +2 -2
- package/ps/index.js +136 -229
- 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 -62
- 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 -101
- package/ee/baseApp.js +0 -99
- package/ee/eeApp.js +0 -406
- 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,223 +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
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* 校验模式
|
|
21
|
-
*/
|
|
22
|
-
exports.verifyMode = function(mode) {
|
|
23
|
-
if (['framework', 'module'].includes(mode)) {
|
|
24
|
-
return true;
|
|
25
|
-
}
|
|
26
|
-
return false;
|
|
27
|
-
}
|
|
1
|
+
'use strict';
|
|
28
2
|
|
|
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.appVersion = function() {
|
|
69
|
+
// app version
|
|
70
|
+
function appVersion() {
|
|
141
71
|
return process.env.EE_APP_VERSION;
|
|
142
72
|
}
|
|
143
73
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
return
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* 获取数据存储路径
|
|
153
|
-
*/
|
|
154
|
-
exports.getStorageDir = function () {
|
|
155
|
-
const storageDir = path.join(this.getRootDir(), 'data');
|
|
156
|
-
return storageDir;
|
|
74
|
+
// 获取数据存储路径
|
|
75
|
+
function getDataDir() {
|
|
76
|
+
const base = isDev() ? getBaseDir() : getUserHomeHiddenAppDir();
|
|
77
|
+
const dataDir = path.join(base, 'data');
|
|
78
|
+
return dataDir;
|
|
157
79
|
}
|
|
158
80
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
const dir = path.join(this.getRootDir(), 'logs');
|
|
81
|
+
// 获取日志存储路径
|
|
82
|
+
function getLogDir() {
|
|
83
|
+
const base = isDev() ? getBaseDir() : getUserHomeHiddenAppDir();
|
|
84
|
+
const dir = path.join(base, 'logs');
|
|
164
85
|
return dir;
|
|
165
86
|
}
|
|
166
87
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
*/
|
|
170
|
-
exports.getEncryptDir = function (basePath) {
|
|
88
|
+
// 获取bundle文件路径
|
|
89
|
+
function getBundleDir(basePath) {
|
|
171
90
|
const base = basePath || process.cwd();
|
|
172
91
|
const dir = path.join(base, 'public', 'electron');
|
|
173
92
|
return dir;
|
|
174
93
|
}
|
|
175
94
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
exports.getRootDir = function () {
|
|
180
|
-
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();
|
|
181
98
|
return appDir;
|
|
182
99
|
}
|
|
183
100
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
*/
|
|
187
|
-
exports.getBaseDir = function() {
|
|
101
|
+
// 获取base目录
|
|
102
|
+
function getBaseDir() {
|
|
188
103
|
return process.env.EE_BASE_DIR;
|
|
189
104
|
}
|
|
190
105
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
exports.getElectronDir = function() {
|
|
195
|
-
return process.env.EE_BASE_DIR;
|
|
106
|
+
// 获取electron目录
|
|
107
|
+
function getElectronDir() {
|
|
108
|
+
return process.env.EE_ELECTRON_DIR;
|
|
196
109
|
}
|
|
197
110
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
exports.getPublicDir = function() {
|
|
202
|
-
const dir = path.join(process.env.EE_HOME, "public");
|
|
111
|
+
// 获取public目录
|
|
112
|
+
function getPublicDir() {
|
|
113
|
+
const dir = path.join(getBaseDir(), "public");
|
|
203
114
|
return dir;
|
|
204
115
|
}
|
|
205
116
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
const execDir = this.getExecDir();
|
|
211
|
-
const isPackaged = this.isPackaged();
|
|
212
|
-
|
|
117
|
+
// 获取 额外资源目录
|
|
118
|
+
function getExtraResourcesDir() {
|
|
119
|
+
const execDir = getExecDir();
|
|
120
|
+
const packaged = isPackaged();
|
|
213
121
|
|
|
214
122
|
// 资源路径不同
|
|
215
123
|
let dir = '';
|
|
216
|
-
if (
|
|
124
|
+
if (packaged) {
|
|
217
125
|
// 打包后 execDir为 应用程序 exe\dmg\dep软件所在目录;打包前该值是项目根目录
|
|
218
126
|
// windows和MacOs不一样
|
|
219
127
|
dir = path.join(execDir, "resources", "extraResources");
|
|
220
|
-
if (
|
|
128
|
+
if (is.macOS()) {
|
|
221
129
|
dir = path.join(execDir, "..", "Resources", "extraResources");
|
|
222
130
|
}
|
|
223
131
|
} else {
|
|
@@ -227,105 +135,64 @@ exports.getExtraResourcesDir = function() {
|
|
|
227
135
|
return dir;
|
|
228
136
|
}
|
|
229
137
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
*/
|
|
233
|
-
exports.getAppUserDataDir = function() {
|
|
138
|
+
// 获取 appUserData目录
|
|
139
|
+
function getAppUserDataDir() {
|
|
234
140
|
return process.env.EE_APP_USER_DATA;
|
|
235
141
|
}
|
|
236
142
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
*/
|
|
240
|
-
exports.getExecDir = function() {
|
|
143
|
+
// 获取 exec目录
|
|
144
|
+
function getExecDir() {
|
|
241
145
|
return process.env.EE_EXEC_DIR;
|
|
242
146
|
}
|
|
243
147
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
*/
|
|
247
|
-
exports.getUserHomeDir = function() {
|
|
148
|
+
// 获取操作系统用户目录
|
|
149
|
+
function getUserHomeDir() {
|
|
248
150
|
return process.env.EE_USER_HOME;
|
|
249
151
|
}
|
|
250
152
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
// if (!fs.existsSync(filePath)) {
|
|
257
|
-
// throw new Error(filePath + ' is not found');
|
|
258
|
-
// }
|
|
259
|
-
// const pkg = JSON.parse(fs.readFileSync(filePath));
|
|
260
|
-
// if (!pkg.name || pkg.name == "") {
|
|
261
|
-
// throw new Error(`name is required from ${filePath}`);
|
|
262
|
-
// }
|
|
263
|
-
const appname = this.appName();
|
|
264
|
-
const cfgDir = path.join(this.getUserHomeDir(), ".config", appname);
|
|
265
|
-
return cfgDir;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* 获取基础数据路径
|
|
270
|
-
*/
|
|
271
|
-
exports.getUserHomeAppFilePath = function() {
|
|
272
|
-
const p = path.join(this.getUserHomeConfigDir(), "app.json");
|
|
273
|
-
return p;
|
|
153
|
+
// 获取用户家目录中的隐藏的app目录
|
|
154
|
+
function getUserHomeHiddenAppDir() {
|
|
155
|
+
const appnameDir = "." + appName();
|
|
156
|
+
const dir = path.join(getUserHomeDir(), appnameDir);
|
|
157
|
+
return dir;
|
|
274
158
|
}
|
|
275
159
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
return
|
|
160
|
+
// 获取用户家目录中的app目录
|
|
161
|
+
function getUserHomeAppDir() {
|
|
162
|
+
const appnameDir = appName();
|
|
163
|
+
const dir = path.join(getUserHomeDir(), appnameDir);
|
|
164
|
+
return dir;
|
|
281
165
|
}
|
|
282
166
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
*/
|
|
286
|
-
exports.getSocketPort = function () {
|
|
167
|
+
// 获取内置socket端口
|
|
168
|
+
function getSocketPort() {
|
|
287
169
|
return parseInt(process.env.EE_SOCKET_PORT) || 0;
|
|
288
170
|
}
|
|
289
171
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
*/
|
|
293
|
-
exports.getHttpPort = function () {
|
|
172
|
+
// 获取内置http端口
|
|
173
|
+
function getHttpPort() {
|
|
294
174
|
return parseInt(process.env.EE_HTTP_PORT) || 0;
|
|
295
175
|
}
|
|
296
176
|
|
|
297
177
|
/**
|
|
298
178
|
* 是否打包
|
|
299
179
|
*/
|
|
300
|
-
|
|
180
|
+
function isPackaged() {
|
|
301
181
|
return process.env.EE_IS_PACKAGED === 'true';
|
|
302
182
|
}
|
|
303
183
|
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
*/
|
|
307
|
-
exports.isEncrypted = function () {
|
|
308
|
-
return process.env.EE_IS_ENCRYPTED === 'true';
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
/**
|
|
312
|
-
* 是否热重启
|
|
313
|
-
*/
|
|
314
|
-
exports.isHotReload = function () {
|
|
184
|
+
// 是否热重启
|
|
185
|
+
function isHotReload() {
|
|
315
186
|
return process.env.HOT_RELOAD === 'true';
|
|
316
187
|
}
|
|
317
188
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
*/
|
|
321
|
-
exports.exit = function(code = 0) {
|
|
189
|
+
// 进程退出
|
|
190
|
+
function exit(code = 0) {
|
|
322
191
|
return process.exit(code);
|
|
323
192
|
}
|
|
324
193
|
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
*/
|
|
328
|
-
exports.makeMessage = function(msg = {}) {
|
|
194
|
+
// 格式化message
|
|
195
|
+
function makeMessage(msg = {}) {
|
|
329
196
|
let message = Object.assign({
|
|
330
197
|
channel: '',
|
|
331
198
|
event: '',
|
|
@@ -335,10 +202,8 @@ exports.makeMessage = function(msg = {}) {
|
|
|
335
202
|
return message;
|
|
336
203
|
}
|
|
337
204
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
*/
|
|
341
|
-
exports.exitChildJob = function(code = 0) {
|
|
205
|
+
// 退出ChildJob进程
|
|
206
|
+
function exitChildJob(code = 0) {
|
|
342
207
|
try {
|
|
343
208
|
let args = JSON.parse(process.argv[2]);
|
|
344
209
|
if (args.type == 'childJob') {
|
|
@@ -349,11 +214,8 @@ exports.exitChildJob = function(code = 0) {
|
|
|
349
214
|
}
|
|
350
215
|
}
|
|
351
216
|
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
* 任务类型 ChildJob
|
|
355
|
-
*/
|
|
356
|
-
exports.isChildJob = function() {
|
|
217
|
+
// 任务类型 ChildJob
|
|
218
|
+
function isChildJob() {
|
|
357
219
|
try {
|
|
358
220
|
let args = JSON.parse(process.argv[2]);
|
|
359
221
|
if (args.type == 'childJob') {
|
|
@@ -364,10 +226,8 @@ exports.isChildJob = function() {
|
|
|
364
226
|
}
|
|
365
227
|
}
|
|
366
228
|
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
*/
|
|
370
|
-
exports.isChildPoolJob = function() {
|
|
229
|
+
// 任务类型 ChildPoolJob
|
|
230
|
+
function isChildPoolJob() {
|
|
371
231
|
try {
|
|
372
232
|
let args = JSON.parse(process.argv[2]);
|
|
373
233
|
if (args.type == 'childPoolJob') {
|
|
@@ -376,4 +236,51 @@ exports.isChildPoolJob = function() {
|
|
|
376
236
|
} catch (e) {
|
|
377
237
|
return false;
|
|
378
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
|
|
379
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
|
+
};
|