ee-core 1.2.8 → 1.2.9

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.
Files changed (44) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +2 -2
  3. package/bin/tools.js +22 -22
  4. package/config/config.default.js +262 -250
  5. package/core/index.js +12 -12
  6. package/core/lib/ee.js +209 -209
  7. package/core/lib/loader/context_loader.js +105 -105
  8. package/core/lib/loader/ee_loader.js +447 -451
  9. package/core/lib/loader/file_loader.js +262 -262
  10. package/core/lib/loader/mixin/config.js +138 -138
  11. package/core/lib/loader/mixin/controller.js +123 -123
  12. package/core/lib/loader/mixin/service.js +29 -29
  13. package/core/lib/utils/base_context_class.js +34 -34
  14. package/core/lib/utils/index.js +100 -100
  15. package/core/lib/utils/sequencify.js +59 -59
  16. package/core/lib/utils/timing.js +77 -77
  17. package/index.js +49 -49
  18. package/lib/appLoader.js +45 -45
  19. package/lib/application.js +81 -80
  20. package/lib/baseApp.js +133 -118
  21. package/lib/constant.js +28 -28
  22. package/lib/eeApp.js +321 -326
  23. package/lib/helper.js +51 -51
  24. package/lib/httpclient.js +136 -136
  25. package/lib/logger.js +46 -46
  26. package/lib/socket/httpServer.js +134 -102
  27. package/lib/socket/io.js +23 -23
  28. package/lib/socket/ipcServer.js +128 -128
  29. package/lib/socket/socketClient.js +50 -50
  30. package/lib/socket/socketServer.js +76 -76
  31. package/lib/socket/start.js +22 -22
  32. package/lib/storage/index.js +33 -33
  33. package/lib/storage/lowdbStorage.js +98 -98
  34. package/lib/storage/sqliteStorage.js +58 -58
  35. package/package.json +45 -45
  36. package/tools/codeCompress.js +204 -204
  37. package/tools/replaceDist.js +76 -76
  38. package/utils/common.js +90 -90
  39. package/utils/index.js +151 -207
  40. package/utils/wrap.js +37 -37
  41. package/resource/images/loding.gif +0 -0
  42. package/resource/images/tray_logo.png +0 -0
  43. package/resource/loading.html +0 -22
  44. package/resource/view_example.html +0 -22
package/lib/eeApp.js CHANGED
@@ -1,327 +1,322 @@
1
- const path = require('path');
2
- const getPort = require('get-port');
3
- const {app, BrowserWindow, BrowserView, Menu} = require('electron');
4
- const BaseApp = require('./baseApp');
5
- const is = require('is-type-of');
6
- const Koa = require('koa');
7
- const koaServe = require('koa-static');
8
- const utilsCommon = require('../utils/common');
9
-
10
- class EeApp extends BaseApp {
11
- constructor(options = {}) {
12
- super(options);
13
-
14
- this.electron = {
15
- mainWindow: null,
16
- tray: null,
17
- extra: {
18
- closeWindow: false,
19
- }
20
- };
21
- }
22
-
23
- /**
24
- * 生成端口
25
- */
26
- async createPorts () {
27
- const mainPort = await getPort({port: this.config.mainServer.port});
28
- process.env.EE_MAIN_PORT = mainPort;
29
- this.config.mainServer.port = mainPort;
30
-
31
- if (this.config.socketServer.enable) {
32
- const socketPort = await getPort({port: this.config.socketServer.port});
33
- process.env.EE_SOCKET_PORT = socketPort;
34
- this.config.socketServer.port = socketPort;
35
- }
36
-
37
- if (this.config.httpServer.enable) {
38
- const httpPort = await getPort({port: this.config.httpServer.port});
39
- process.env.EE_HTTP_PORT = httpPort;
40
- this.config.httpServer.port = httpPort;
41
- }
42
-
43
- // 更新db配置
44
- this.getCoreDB().setItem('config', this.config);
45
- }
46
-
47
- /**
48
- * 启动通信模块
49
- */
50
- startSocket () {
51
- const socket = require('./socket/start');
52
- socket(this);
53
- }
54
-
55
- /**
56
- * 创建electron应用
57
- */
58
- async createElectronApp () {
59
- const self = this;
60
-
61
- const gotTheLock = app.requestSingleInstanceLock();
62
- if (!gotTheLock) {
63
- await this.appQuit();
64
- return;
65
- }
66
-
67
- app.on('second-instance', (event) => {
68
- self.restoreMainWindow();
69
- })
70
-
71
- app.whenReady().then(() => {
72
- self.createWindow();
73
- app.on('activate', function () {
74
- self.restoreMainWindow();
75
- })
76
- })
77
-
78
- app.on('window-all-closed', function () {
79
- if (process.platform !== 'darwin') {
80
- self.coreLogger.info('[Appliaction] [initialize] window-all-closed quit');
81
- self.appQuit();
82
- }
83
- })
84
-
85
- app.on('before-quit', () => {
86
- self.electron.extra.closeWindow = true;
87
- })
88
-
89
- if (this.config.hardGpu.enable == false) {
90
- app.disableHardwareAcceleration();
91
- }
92
-
93
- await this.electronAppReady();
94
- }
95
-
96
- /**
97
- * 创建应用主窗口
98
- */
99
- async createWindow () {
100
- const winOptions = this.config.windowsOption;
101
- this.electron.mainWindow = new BrowserWindow(winOptions);
102
- let win = this.electron.mainWindow;
103
- if (winOptions.show === false) {
104
- win.once('ready-to-show', () => {
105
- win.show();
106
- })
107
- }
108
-
109
- // 菜单显示/隐藏
110
- if (this.config.openAppMenu === 'dev-show'
111
- && this.config.env == 'prod') {
112
- Menu.setApplicationMenu(null);
113
- } else if (this.config.openAppMenu === false) {
114
- Menu.setApplicationMenu(null);
115
- } else {
116
- // nothing
117
- }
118
-
119
- this.loadingView(winOptions);
120
-
121
- await this.windowReady();
122
-
123
- await this.loderPreload();
124
-
125
- this.selectAppType();
126
-
127
- // DevTools
128
- if (!app.isPackaged && this.config.openDevTools) {
129
- win.webContents.openDevTools();
130
- }
131
- }
132
-
133
- /**
134
- * 还原窗口
135
- */
136
- restoreMainWindow () {
137
- if (this.electron.mainWindow) {
138
- if (this.electron.mainWindow.isMinimized()) {
139
- this.electron.mainWindow.restore();
140
- }
141
- this.electron.mainWindow.show()
142
- }
143
- }
144
-
145
- /**
146
- * 加载已经实现的功能
147
- */
148
- async loadPreference () {
149
- const preferences = require('./preferences');
150
- return await preferences(this);
151
- }
152
-
153
- /**
154
- * 加载loading页面
155
- */
156
- loadingView (winOptions) {
157
- let currentV = process.versions.electron;
158
- if (utilsCommon.compareVersion(currentV, '12.2.3') == 1) {
159
- return;
160
- }
161
- if (!this.config.loadingPage) {
162
- return;
163
- }
164
- const self = this;
165
- const loadingBrowserView = new BrowserView();
166
- this.electron.mainWindow.setBrowserView(loadingBrowserView);
167
- loadingBrowserView.setBounds({
168
- x: 0,
169
- y: 0,
170
- width: winOptions.width,
171
- height: winOptions.height
172
- });
173
-
174
- // loading html
175
- const loadingHtml = path.join('file://', this.config.homeDir, 'public', 'html', 'loading.html');
176
- loadingBrowserView.webContents.loadURL(loadingHtml);
177
- this.logger.info('loadingHtml:', loadingHtml);
178
-
179
- this.electron.mainWindow.webContents.on('dom-ready', async (event) => {
180
- self.electron.mainWindow.removeBrowserView(loadingBrowserView);
181
- });
182
- }
183
-
184
- /**
185
- * 应用类型 (远程、html、单页应用)
186
- */
187
- selectAppType () {
188
- let type = '';
189
- let url = '';
190
-
191
- // 远程模式
192
- const remoteConfig = this.config.remoteUrl;
193
- if (remoteConfig.enable == true) {
194
- type = 'remote_web';
195
- url = remoteConfig.url;
196
- this.loadMainUrl(type, url);
197
- return;
198
- }
199
-
200
- const protocol = 'http://';
201
- const developmentModeConfig = this.config.developmentMode;
202
- const selectMode = developmentModeConfig.default;
203
- const modeInfo = developmentModeConfig.mode[selectMode];
204
- let staticDir = null;
205
-
206
- // html模式
207
- if (selectMode == 'html') {
208
- if (this.config.env !== 'prod') {
209
- staticDir = path.join(this.config.homeDir, 'frontend', 'dist');
210
- }
211
- this.loadLocalWeb('html', staticDir, modeInfo);
212
- return;
213
- }
214
-
215
- // 单页应用
216
- url = protocol + modeInfo.hostname + ':' + modeInfo.port;
217
- if (this.config.env !== 'prod') {
218
- this.loadMainUrl('spa', url);
219
- } else {
220
- this.loadLocalWeb('spa');
221
- }
222
- }
223
-
224
- /**
225
- * 加载本地前端资源
226
- */
227
- loadLocalWeb (mode, staticDir, hostInfo) {
228
- const self = this;
229
- if (!staticDir) {
230
- staticDir = path.join(this.config.homeDir, 'public', 'dist')
231
- }
232
-
233
- const koaApp = new Koa();
234
- koaApp.use(koaServe(staticDir));
235
-
236
- const mainServer = this.config.mainServer;
237
- let url = mainServer.protocol + mainServer.host + ':' + mainServer.port;
238
- if (mode == 'html') {
239
- url += '/' + hostInfo.indexPage;
240
- }
241
-
242
- koaApp.listen(mainServer.port, () => {
243
- self.loadMainUrl(mode, url);
244
- });
245
- }
246
-
247
- /**
248
- * 主页面
249
- */
250
- loadMainUrl (type, url) {
251
- this.logger.info('main page is env: %s, type: %s, App running at: %s', this.config.env, type, url);
252
- this.electron.mainWindow.loadURL(url);
253
- }
254
-
255
- /**
256
- * electron app退出
257
- */
258
- async appQuit () {
259
- await this.beforeClose();
260
-
261
- app.quit();
262
- }
263
-
264
- /**
265
- * 预加载模块
266
- */
267
- async loderPreload () {
268
- let filepath = this.loader.resolveModule(path.join(this.config.baseDir, 'preload', 'index'));
269
- if (!filepath) return;
270
- const fileObj = this.loader.loadFile(filepath);
271
- if (is.function(fileObj) && !is.generatorFunction(fileObj) && !is.asyncFunction(fileObj)) {
272
- fileObj();
273
- } else if (is.asyncFunction(fileObj)) {
274
- await fileObj();
275
- }
276
- }
277
-
278
- /**
279
- * 序列化参数
280
- */
281
- stringify(obj, ignore) {
282
- const result = {};
283
- Object.keys(obj).forEach(key => {
284
- if (!ignore.includes(key)) {
285
- result[key] = obj[key];
286
- }
287
- });
288
- return JSON.stringify(result);
289
- }
290
-
291
- /**
292
- * 捕获异常
293
- */
294
- async catchLog () {
295
- const self = this;
296
-
297
- process.on('uncaughtException', function(err) {
298
- self.logger.error(err);
299
- });
300
- }
301
-
302
- /**
303
- * electron app已经准备好,主窗口还未创建
304
- */
305
- async electronAppReady () {
306
- // do some things
307
-
308
- }
309
-
310
- /**
311
- * 主应用窗口已经创建
312
- */
313
- async windowReady () {
314
- // do some things
315
-
316
- }
317
-
318
- /**
319
- * app关闭之前
320
- */
321
- async beforeClose () {
322
- // do some things
323
-
324
- }
325
- }
326
-
1
+ const path = require('path');
2
+ const getPort = require('get-port');
3
+ const {app, BrowserWindow, BrowserView, Menu} = require('electron');
4
+ const BaseApp = require('./baseApp');
5
+ const is = require('is-type-of');
6
+ const Koa = require('koa');
7
+ const koaServe = require('koa-static');
8
+ const utilsCommon = require('../utils/common');
9
+
10
+ class EeApp extends BaseApp {
11
+ constructor(options = {}) {
12
+ super(options);
13
+
14
+ this.electron = {
15
+ mainWindow: null,
16
+ tray: null,
17
+ extra: {
18
+ closeWindow: false,
19
+ }
20
+ };
21
+ }
22
+
23
+ /**
24
+ * 生成端口
25
+ */
26
+ async createPorts () {
27
+ const mainPort = await getPort({port: this.config.mainServer.port});
28
+ process.env.EE_MAIN_PORT = mainPort;
29
+ this.config.mainServer.port = mainPort;
30
+
31
+ if (this.config.socketServer.enable) {
32
+ const socketPort = await getPort({port: this.config.socketServer.port});
33
+ process.env.EE_SOCKET_PORT = socketPort;
34
+ this.config.socketServer.port = socketPort;
35
+ }
36
+
37
+ if (this.config.httpServer.enable) {
38
+ const httpPort = await getPort({port: this.config.httpServer.port});
39
+ process.env.EE_HTTP_PORT = httpPort;
40
+ this.config.httpServer.port = httpPort;
41
+ }
42
+
43
+ // 更新db配置
44
+ this.getCoreDB().setItem('config', this.config);
45
+ }
46
+
47
+ /**
48
+ * 启动通信模块
49
+ */
50
+ async startSocket () {
51
+ const socket = require('./socket/start');
52
+ socket(this);
53
+ }
54
+
55
+ /**
56
+ * 创建electron应用
57
+ */
58
+ async createElectronApp () {
59
+ const self = this;
60
+
61
+ const gotTheLock = app.requestSingleInstanceLock();
62
+ if (!gotTheLock) {
63
+ await this.appQuit();
64
+ return;
65
+ }
66
+
67
+ app.on('second-instance', (event) => {
68
+ self.restoreMainWindow();
69
+ })
70
+
71
+ app.whenReady().then(() => {
72
+ self.createWindow();
73
+ app.on('activate', function () {
74
+ self.restoreMainWindow();
75
+ })
76
+ })
77
+
78
+ app.on('window-all-closed', function () {
79
+ if (process.platform !== 'darwin') {
80
+ self.coreLogger.info('[Appliaction] [initialize] window-all-closed quit');
81
+ self.appQuit();
82
+ }
83
+ })
84
+
85
+ app.on('before-quit', () => {
86
+ self.electron.extra.closeWindow = true;
87
+ })
88
+
89
+ if (this.config.hardGpu.enable == false) {
90
+ app.disableHardwareAcceleration();
91
+ }
92
+
93
+ await this.electronAppReady();
94
+ }
95
+
96
+ /**
97
+ * 创建应用主窗口
98
+ */
99
+ async createWindow () {
100
+ const winOptions = this.config.windowsOption;
101
+ this.electron.mainWindow = new BrowserWindow(winOptions);
102
+ let win = this.electron.mainWindow;
103
+
104
+ // 菜单显示/隐藏
105
+ if (this.config.openAppMenu === 'dev-show'
106
+ && this.config.env == 'prod') {
107
+ Menu.setApplicationMenu(null);
108
+ } else if (this.config.openAppMenu === false) {
109
+ Menu.setApplicationMenu(null);
110
+ } else {
111
+ // nothing
112
+ }
113
+
114
+ this.loadingView(winOptions);
115
+
116
+ await this.windowReady();
117
+
118
+ await this.loderPreload();
119
+
120
+ this.selectAppType();
121
+
122
+ // DevTools
123
+ if (!app.isPackaged && this.config.openDevTools) {
124
+ win.webContents.openDevTools();
125
+ }
126
+ }
127
+
128
+ /**
129
+ * 还原窗口
130
+ */
131
+ restoreMainWindow () {
132
+ if (this.electron.mainWindow) {
133
+ if (this.electron.mainWindow.isMinimized()) {
134
+ this.electron.mainWindow.restore();
135
+ }
136
+ this.electron.mainWindow.show()
137
+ }
138
+ }
139
+
140
+ /**
141
+ * 加载已经实现的功能
142
+ */
143
+ async loadPreference () {
144
+ const preferences = require('./preferences');
145
+ return await preferences(this);
146
+ }
147
+
148
+ /**
149
+ * 加载loading页面
150
+ */
151
+ loadingView (winOptions) {
152
+ let currentV = process.versions.electron;
153
+ if (utilsCommon.compareVersion(currentV, '12.2.3') == 1) {
154
+ return;
155
+ }
156
+ if (!this.config.loadingPage) {
157
+ return;
158
+ }
159
+ const self = this;
160
+ const loadingBrowserView = new BrowserView();
161
+ this.electron.mainWindow.setBrowserView(loadingBrowserView);
162
+ loadingBrowserView.setBounds({
163
+ x: 0,
164
+ y: 0,
165
+ width: winOptions.width,
166
+ height: winOptions.height
167
+ });
168
+
169
+ // loading html
170
+ const loadingHtml = path.join('file://', this.config.homeDir, 'public', 'html', 'loading.html');
171
+ loadingBrowserView.webContents.loadURL(loadingHtml);
172
+ this.logger.info('loadingHtml:', loadingHtml);
173
+
174
+ this.electron.mainWindow.webContents.on('dom-ready', async (event) => {
175
+ self.electron.mainWindow.removeBrowserView(loadingBrowserView);
176
+ });
177
+ }
178
+
179
+ /**
180
+ * 应用类型 (远程、html、单页应用)
181
+ */
182
+ selectAppType () {
183
+ let type = '';
184
+ let url = '';
185
+
186
+ // 远程模式
187
+ const remoteConfig = this.config.remoteUrl;
188
+ if (remoteConfig.enable == true) {
189
+ type = 'remote_web';
190
+ url = remoteConfig.url;
191
+ this.loadMainUrl(type, url);
192
+ return;
193
+ }
194
+
195
+ const protocol = 'http://';
196
+ const developmentModeConfig = this.config.developmentMode;
197
+ const selectMode = developmentModeConfig.default;
198
+ const modeInfo = developmentModeConfig.mode[selectMode];
199
+ let staticDir = null;
200
+
201
+ // html模式
202
+ if (selectMode == 'html') {
203
+ if (this.config.env !== 'prod') {
204
+ staticDir = path.join(this.config.homeDir, 'frontend', 'dist');
205
+ }
206
+ this.loadLocalWeb('html', staticDir, modeInfo);
207
+ return;
208
+ }
209
+
210
+ // 单页应用
211
+ url = protocol + modeInfo.hostname + ':' + modeInfo.port;
212
+ if (this.config.env !== 'prod') {
213
+ this.loadMainUrl('spa', url);
214
+ } else {
215
+ this.loadLocalWeb('spa');
216
+ }
217
+ }
218
+
219
+ /**
220
+ * 加载本地前端资源
221
+ */
222
+ loadLocalWeb (mode, staticDir, hostInfo) {
223
+ const self = this;
224
+ if (!staticDir) {
225
+ staticDir = path.join(this.config.homeDir, 'public', 'dist')
226
+ }
227
+
228
+ const koaApp = new Koa();
229
+ koaApp.use(koaServe(staticDir));
230
+
231
+ const mainServer = this.config.mainServer;
232
+ let url = mainServer.protocol + mainServer.host + ':' + mainServer.port;
233
+ if (mode == 'html') {
234
+ url += '/' + hostInfo.indexPage;
235
+ }
236
+
237
+ koaApp.listen(mainServer.port, () => {
238
+ self.loadMainUrl(mode, url);
239
+ });
240
+ }
241
+
242
+ /**
243
+ * 主页面
244
+ */
245
+ loadMainUrl (type, url) {
246
+ this.logger.info('main page is env: %s, type: %s, App running at: %s', this.config.env, type, url);
247
+ this.electron.mainWindow.loadURL(url);
248
+ }
249
+
250
+ /**
251
+ * electron app退出
252
+ */
253
+ async appQuit () {
254
+ await this.beforeClose();
255
+
256
+ app.quit();
257
+ }
258
+
259
+ /**
260
+ * 预加载模块
261
+ */
262
+ async loderPreload () {
263
+ let filepath = this.loader.resolveModule(path.join(this.config.baseDir, 'preload', 'index'));
264
+ if (!filepath) return;
265
+ const fileObj = this.loader.loadFile(filepath);
266
+ if (is.function(fileObj) && !is.generatorFunction(fileObj) && !is.asyncFunction(fileObj)) {
267
+ fileObj();
268
+ } else if (is.asyncFunction(fileObj)) {
269
+ await fileObj();
270
+ }
271
+ }
272
+
273
+ /**
274
+ * 序列化参数
275
+ */
276
+ stringify(obj, ignore) {
277
+ const result = {};
278
+ Object.keys(obj).forEach(key => {
279
+ if (!ignore.includes(key)) {
280
+ result[key] = obj[key];
281
+ }
282
+ });
283
+ return JSON.stringify(result);
284
+ }
285
+
286
+ /**
287
+ * 捕获异常
288
+ */
289
+ async catchLog () {
290
+ const self = this;
291
+
292
+ process.on('uncaughtException', function(err) {
293
+ self.logger.error(err);
294
+ });
295
+ }
296
+
297
+ /**
298
+ * electron app已经准备好,主窗口还未创建
299
+ */
300
+ async electronAppReady () {
301
+ // do some things
302
+
303
+ }
304
+
305
+ /**
306
+ * 主应用窗口已经创建
307
+ */
308
+ async windowReady () {
309
+ // do some things
310
+
311
+ }
312
+
313
+ /**
314
+ * app关闭之前
315
+ */
316
+ async beforeClose () {
317
+ // do some things
318
+
319
+ }
320
+ }
321
+
327
322
  module.exports = EeApp;