ee-core 2.0.0-beta.4 → 2.0.0-beta.6

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 (75) hide show
  1. package/addon/index.js +30 -0
  2. package/config/index.js +54 -0
  3. package/{module/const → const}/channel.js +4 -1
  4. package/{module/const → const}/index.js +1 -5
  5. package/{module/core → core}/lib/loader/ee_loader.js +1 -1
  6. package/{module/core → core}/lib/loader/file_loader.js +1 -0
  7. package/{module/core → core}/lib/loader/mixin/addon.js +1 -1
  8. package/{module/app → ee}/baseApp.js +2 -1
  9. package/{module/app → ee}/eeApp.js +20 -46
  10. package/ee/index.js +30 -0
  11. package/electron/index.js +32 -0
  12. package/electron/window.js +45 -0
  13. package/index.js +12 -13
  14. package/{module/jobs → jobs}/child/app.js +3 -3
  15. package/{module/jobs → jobs}/childPool/app.js +3 -3
  16. package/{module/message → message}/index.js +0 -2
  17. package/package.json +2 -2
  18. package/{module/ps → ps}/index.js +10 -3
  19. package/{module/socket → socket}/httpServer.js +12 -18
  20. package/socket/index.js +82 -0
  21. package/{module/socket → socket}/io.js +4 -0
  22. package/{module/socket → socket}/ipcServer.js +0 -2
  23. package/socket/socketServer.js +66 -0
  24. package/{module/storage → storage}/jsondbStorage.js +37 -0
  25. package/module/app/index.js +0 -5
  26. package/module/socket/socketClient.js +0 -49
  27. package/module/socket/socketServer.js +0 -76
  28. package/module/socket/start.js +0 -22
  29. /package/{module/addon → addon}/window/index.js +0 -0
  30. /package/{module/bin → bin}/tools.js +0 -0
  31. /package/{module/config → config}/config.default.js +0 -0
  32. /package/{module/controller → controller}/index.js +0 -0
  33. /package/{module/core → core}/index.js +0 -0
  34. /package/{module/core → core}/lib/ee.js +0 -0
  35. /package/{module/core → core}/lib/loader/context_loader.js +0 -0
  36. /package/{module/core → core}/lib/loader/mixin/config.js +0 -0
  37. /package/{module/core → core}/lib/loader/mixin/controller.js +0 -0
  38. /package/{module/core → core}/lib/loader/mixin/service.js +0 -0
  39. /package/{module/core → core}/lib/utils/base_context_class.js +0 -0
  40. /package/{module/core → core}/lib/utils/function.js +0 -0
  41. /package/{module/core → core}/lib/utils/index.js +0 -0
  42. /package/{module/core → core}/lib/utils/sequencify.js +0 -0
  43. /package/{module/core → core}/lib/utils/timing.js +0 -0
  44. /package/{module/app → ee}/appLoader.js +0 -0
  45. /package/{module/app → ee}/application.js +0 -0
  46. /package/{module/exception → exception}/index.js +0 -0
  47. /package/{module/httpclient → httpclient}/index.js +0 -0
  48. /package/{module/jobs → jobs}/baseJobClass.js +0 -0
  49. /package/{module/jobs → jobs}/child/forkProcess.js +0 -0
  50. /package/{module/jobs → jobs}/child/index.js +0 -0
  51. /package/{module/jobs → jobs}/childPool/forkProcess.js +0 -0
  52. /package/{module/jobs → jobs}/childPool/index.js +0 -0
  53. /package/{module/jobs → jobs}/childPool/pool.js +0 -0
  54. /package/{module/jobs → jobs}/index.js +0 -0
  55. /package/{module/jobs → jobs}/renderer/index.js +0 -0
  56. /package/{module/jobs → jobs}/renderer/loadView.js +0 -0
  57. /package/{module/jobs → jobs}/unification.js +0 -0
  58. /package/{module/loader → loader}/index.js +0 -0
  59. /package/{module/log → log}/index.js +0 -0
  60. /package/{module/log → log}/logger.js +0 -0
  61. /package/{module/message → message}/childMessage.js +0 -0
  62. /package/{module/oldUtils → oldUtils}/index.js +0 -0
  63. /package/{module/service → service}/index.js +0 -0
  64. /package/{module/storage → storage}/index.js +0 -0
  65. /package/{module/storage → storage}/jsondb/adapters/Base.js +0 -0
  66. /package/{module/storage → storage}/jsondb/adapters/FileSync.js +0 -0
  67. /package/{module/storage → storage}/jsondb/main.js +0 -0
  68. /package/{module/storage → storage}/sqliteStorage.js +0 -0
  69. /package/{module/tools → tools}/encrypt.js +0 -0
  70. /package/{module/tools → tools}/replaceDist.js +0 -0
  71. /package/{module/utils → utils}/copyto.js +0 -0
  72. /package/{module/utils → utils}/helper.js +0 -0
  73. /package/{module/utils → utils}/index.js +0 -0
  74. /package/{module/utils → utils}/json.js +0 -0
  75. /package/{module/utils → utils}/wrap.js +0 -0
package/addon/index.js ADDED
@@ -0,0 +1,30 @@
1
+ const EEAddon = Symbol('Ee#Addon');
2
+
3
+ /**
4
+ * todo 插件模块
5
+ */
6
+ const Addon = {
7
+
8
+ /**
9
+ * 设置插件对象
10
+ */
11
+ setAddonObject(name, obj) {
12
+ if (!this[EEAddon]) {
13
+ this[EEAddon] = new Map();
14
+ }
15
+
16
+ if (!this[EEAddon].has(name)) {
17
+ this[EEAddon][name] = obj;
18
+ }
19
+ },
20
+
21
+ /**
22
+ * 获取插件对象
23
+ */
24
+ get(name) {
25
+ let addon = this[EEAddon].has(name) ? this[EEAddon][name]: null;
26
+ return addon;
27
+ },
28
+ }
29
+
30
+ module.exports = Addon;
@@ -0,0 +1,54 @@
1
+ const Storage = require('../storage');
2
+
3
+ const Cfg = {
4
+
5
+ /**
6
+ * 获取 coredb
7
+ */
8
+ _getCoreDB() {
9
+ const coreDB = Storage.connection('system');
10
+ return coreDB;
11
+ },
12
+
13
+ /**
14
+ * all
15
+ */
16
+ all() {
17
+ const cdb = this._getCoreDB();
18
+ const config = cdb.getItem('config');
19
+
20
+ return config;
21
+ },
22
+
23
+ /**
24
+ * setAll
25
+ */
26
+ setAll(value) {
27
+ const cdb = this._getCoreDB();
28
+ cdb.setItem('config', value);
29
+
30
+ return;
31
+ },
32
+
33
+ /**
34
+ * setValue
35
+ */
36
+ setValue(key, value) {
37
+ const cdb = this._getCoreDB();
38
+ cdb.setConfigItem(key, value);
39
+
40
+ return;
41
+ },
42
+
43
+ /**
44
+ * getValue
45
+ */
46
+ getValue(key) {
47
+ const cdb = this._getCoreDB();
48
+ let v = cdb.getConfigItem(key);
49
+
50
+ return v;
51
+ },
52
+ };
53
+
54
+ module.exports = Cfg;
@@ -2,5 +2,8 @@ module.exports = {
2
2
  process: {
3
3
  showException: 'ee#showException',
4
4
  sendToMain: 'ee#sendToMain'
5
- }
5
+ },
6
+ socketIo: {
7
+ partySoftware: 'c1',
8
+ },
6
9
  };
@@ -1,11 +1,7 @@
1
1
  module.exports = {
2
2
  storageKey: {
3
3
  cache: 'cache',
4
- },
5
- socketIo: {
6
- channel: {
7
- partySoftware: 'c1',
8
- }
4
+ cacheConfig: 'cache.config',
9
5
  },
10
6
  jobs: {
11
7
  inspectStartIndex: 5858
@@ -397,7 +397,7 @@ class EeLoader {
397
397
  }
398
398
 
399
399
  if (!fs.existsSync(filepath) && !fs.existsSync(fullpath)) {
400
- this.options.logger.warn(`[ee-core] [core/lib/loader/ee_loader] resolveModule unknow filepath: ${filepath}`)
400
+ //this.options.logger.warn(`[ee-core] [core/lib/loader/ee_loader] resolveModule unknow filepath: ${filepath}`)
401
401
  return undefined;
402
402
  }
403
403
  }
@@ -10,6 +10,7 @@ const deprecate = require('depd')('ee');
10
10
  const Utils = require('../utils');
11
11
  const FULLPATH = Symbol('EE_LOADER_ITEM_FULLPATH');
12
12
  const EXPORTS = Symbol('EE_LOADER_ITEM_EXPORTS');
13
+ const Addon = require('../../../addon');
13
14
 
14
15
  const defaults = {
15
16
  directory: null,
@@ -13,7 +13,7 @@ module.exports = {
13
13
 
14
14
  // 加载ee-core的插件 和 用户插件
15
15
  const directorys = [
16
- path.join(this.options.framework, 'module', 'addon'),
16
+ path.join(this.options.framework, 'addon'),
17
17
  path.join(this.options.baseDir, 'addon'),
18
18
  ]
19
19
  opt = Object.assign({
@@ -8,6 +8,7 @@ const HTTPCLIENT = Symbol('EeApplication#httpclient');
8
8
  const LOGGERS = Symbol('EeApplication#loggers');
9
9
  const Log = require('../log');
10
10
  const Storage = require('../storage');
11
+ const Conf = require('../config');
11
12
 
12
13
  class BaseApp extends EeAppCore {
13
14
  constructor (options = {}) {
@@ -17,7 +18,7 @@ class BaseApp extends EeAppCore {
17
18
  this.loader.loadConfig();
18
19
 
19
20
  // 缓存配置
20
- this.getCoreDB().setItem('config', this.config);
21
+ Conf.setAll(this.config);
21
22
 
22
23
  this.loader.load();
23
24
 
@@ -2,25 +2,24 @@ const path = require('path');
2
2
  const fs = require('fs');
3
3
  const assert = require('assert');
4
4
  const getPort = require('get-port');
5
- const {app, BrowserWindow, Menu, protocol} = require('electron');
5
+ const { app } = require('electron');
6
6
  const is = require('is-type-of');
7
7
  const Koa = require('koa');
8
8
  const koaServe = require('koa-static');
9
9
  const https = require('https');
10
10
  const BaseApp = require('./baseApp');
11
11
  const Log = require('../log');
12
+ const Electron = require('../electron');
13
+ const Conf = require('../config');
14
+ const Ps = require('../ps');
15
+ const Socket = require('../socket')
12
16
 
13
17
  class EeApp extends BaseApp {
14
18
  constructor(options = {}) {
15
19
  super(options);
16
20
 
17
- this.electron = {
18
- mainWindow: null,
19
- tray: null,
20
- extra: {
21
- closeWindow: false,
22
- },
23
- };
21
+ this.electron = Electron;
22
+ this.mainWindow;
24
23
  }
25
24
 
26
25
  /**
@@ -44,15 +43,14 @@ class EeApp extends BaseApp {
44
43
  }
45
44
 
46
45
  // 更新db配置
47
- this.getCoreDB().setItem('config', this.config);
46
+ Conf.setAll(this.config);
48
47
  }
49
48
 
50
49
  /**
51
50
  * 启动通信模块
52
51
  */
53
52
  async startSocket () {
54
- const socket = require('../socket/start');
55
- socket(this);
53
+ Socket.startAll(this);
56
54
  }
57
55
 
58
56
  /**
@@ -86,7 +84,7 @@ class EeApp extends BaseApp {
86
84
  })
87
85
 
88
86
  app.on('before-quit', () => {
89
- self.electron.extra.closeWindow = true;
87
+ Electron.extra.closeWindow = true;
90
88
  })
91
89
 
92
90
  if (this.config.hardGpu.enable == false) {
@@ -101,27 +99,8 @@ class EeApp extends BaseApp {
101
99
  */
102
100
  async createWindow () {
103
101
 
104
- // todo
105
- const protocolName = 'eefile';
106
- protocol.registerFileProtocol(protocolName, (request, callback) => {
107
- const url = request.url.substring(protocolName.length + 3);
108
- console.log('[ee-core] [lib/eeApp] registerFileProtocol ----url: ', url);
109
- callback({ path: path.normalize(decodeURIComponent(url)) })
110
- });
111
-
112
- const winOptions = this.config.windowsOption;
113
- this.electron.mainWindow = new BrowserWindow(winOptions);
114
- let win = this.electron.mainWindow;
115
-
116
- // 菜单显示/隐藏
117
- if (this.config.openAppMenu === 'dev-show'
118
- && this.config.env == 'prod') {
119
- Menu.setApplicationMenu(null);
120
- } else if (this.config.openAppMenu === false) {
121
- Menu.setApplicationMenu(null);
122
- } else {
123
- // nothing
124
- }
102
+ // 初始化一个主窗口
103
+ this.mainWindow = Electron.getMainWindow();
125
104
 
126
105
  await this.windowReady();
127
106
 
@@ -130,22 +109,17 @@ class EeApp extends BaseApp {
130
109
  await this._loderPreload();
131
110
 
132
111
  this.selectAppType();
133
-
134
- // DevTools
135
- if (!app.isPackaged && this.config.openDevTools) {
136
- win.webContents.openDevTools();
137
- }
138
112
  }
139
113
 
140
114
  /**
141
115
  * 还原窗口
142
116
  */
143
117
  restoreMainWindow () {
144
- if (this.electron.mainWindow) {
145
- if (this.electron.mainWindow.isMinimized()) {
146
- this.electron.mainWindow.restore();
118
+ if (this.mainWindow) {
119
+ if (this.mainWindow.isMinimized()) {
120
+ this.mainWindow.restore();
147
121
  }
148
- this.electron.mainWindow.show()
122
+ this.mainWindow.show()
149
123
  }
150
124
  }
151
125
 
@@ -172,7 +146,7 @@ class EeApp extends BaseApp {
172
146
 
173
147
  // html模式
174
148
  if (selectMode == 'html') {
175
- if (this.config.env !== 'prod') {
149
+ if (Ps.isDev()) {
176
150
  staticDir = path.join(this.config.homeDir, 'frontend', 'dist');
177
151
  }
178
152
  this.loadLocalWeb('html', staticDir, modeInfo);
@@ -182,7 +156,7 @@ class EeApp extends BaseApp {
182
156
  // 单页应用
183
157
  const protocol = modeInfo.protocol || 'http://';
184
158
  url = protocol + modeInfo.hostname + ':' + modeInfo.port;
185
- if (this.config.env !== 'prod') {
159
+ if (Ps.isDev()) {
186
160
  this.loadMainUrl('spa', url);
187
161
  } else {
188
162
  this.loadLocalWeb('spa');
@@ -239,7 +213,7 @@ class EeApp extends BaseApp {
239
213
  const mainServer = this.config.mainServer;
240
214
  Log.coreLogger.info('[ee-core] [main] Env: %s, Type: %s', this.config.env, type);
241
215
  Log.coreLogger.info('[ee-core] [main] App running at: %s', url);
242
- this.electron.mainWindow.loadURL(url, mainServer.options);
216
+ this.mainWindow.loadURL(url, mainServer.options);
243
217
  }
244
218
 
245
219
  /**
@@ -259,7 +233,7 @@ class EeApp extends BaseApp {
259
233
  // 注册主窗口Contents id
260
234
  const addonsCfg = this.config.addons;
261
235
  if (addonsCfg.window.enable) {
262
- const win = this.electron.mainWindow;
236
+ const win = this.mainWindow;
263
237
  const addonWindow = this.addon.window;
264
238
  addonWindow.registerWCid('main', win.webContents.id);
265
239
  }
package/ee/index.js ADDED
@@ -0,0 +1,30 @@
1
+ const EEApplication = Symbol('Ee#Application');
2
+
3
+ const EE = {
4
+
5
+ /**
6
+ * 兼容1.x版本api
7
+ */
8
+ get Appliaction() {
9
+ const appClass = require('./application');
10
+ return appClass;
11
+ },
12
+
13
+ /**
14
+ * 设置app对象
15
+ */
16
+ set app(appObject) {
17
+ if (!this[EEApplication]) {
18
+ this[EEApplication] = appObject;
19
+ }
20
+ },
21
+
22
+ /**
23
+ * 获取app对象
24
+ */
25
+ get app() {
26
+ return this[EEApplication] || null;
27
+ },
28
+ }
29
+
30
+ module.exports = EE;
@@ -0,0 +1,32 @@
1
+ const Window = require('./window');
2
+ const EEMainWindow = Symbol('Ee#electron#mainWindow');
3
+
4
+ const Electron = {
5
+
6
+ /**
7
+ * 获取 mainWindow
8
+ */
9
+ getMainWindow() {
10
+ if (!this[EEMainWindow]) {
11
+ this[EEMainWindow] = Window.createWindow();
12
+ }
13
+
14
+ return this[EEMainWindow] || null;
15
+ },
16
+
17
+ /**
18
+ * 兼容1.x版本api
19
+ */
20
+ get mainWindow() {
21
+ return this.getMainWindow();
22
+ },
23
+
24
+ /**
25
+ * extra
26
+ */
27
+ extra: {
28
+ closeWindow: false,
29
+ },
30
+ };
31
+
32
+ module.exports = Electron;
@@ -0,0 +1,45 @@
1
+ const {app, BrowserWindow, Menu} = require('electron');
2
+ const Conf = require('../config');
3
+ const Ps = require('../ps');
4
+
5
+ const Window = {
6
+
7
+ /**
8
+ * 创建应用主窗口
9
+ */
10
+ createWindow () {
11
+
12
+ // todo
13
+ // const protocolName = 'eefile';
14
+ // protocol.registerFileProtocol(protocolName, (request, callback) => {
15
+ // const url = request.url.substring(protocolName.length + 3);
16
+ // console.log('[ee-core] [lib/eeApp] registerFileProtocol ----url: ', url);
17
+ // callback({ path: path.normalize(decodeURIComponent(url)) })
18
+ // });
19
+
20
+ const config = Conf.all();
21
+ let win = new BrowserWindow(config.windowsOption);
22
+
23
+ // 菜单显示/隐藏
24
+ if (config.openAppMenu === 'dev-show' && Ps.isProd()) {
25
+ Menu.setApplicationMenu(null);
26
+ } else if (config.openAppMenu === false) {
27
+ Menu.setApplicationMenu(null);
28
+ } else {
29
+ // nothing
30
+ }
31
+
32
+ // DevTools
33
+ if (!app.isPackaged && config.openDevTools) {
34
+ win.webContents.openDevTools({
35
+ mode: 'undocked'
36
+ });
37
+ }
38
+
39
+ return win;
40
+ }
41
+
42
+
43
+ };
44
+
45
+ module.exports = Window;
package/index.js CHANGED
@@ -1,46 +1,45 @@
1
1
  /**
2
2
  * @namespace EeCore
3
3
  */
4
- let app;
4
+
5
+ /**
6
+ * @member {app} EeCore#app
7
+ * @since 1.0.0
8
+ */
9
+ const EE = require('./ee');
5
10
 
6
11
  /**
7
12
  * @member {Controller} EeCore#Controller
8
13
  * @since 1.0.0
9
14
  */
10
- const Controller = require('./module/controller');
15
+ const Controller = require('./controller');
11
16
 
12
17
  /**
13
18
  * @member {Service} EeCore#Service
14
19
  * @since 1.0.0
15
20
  */
16
- const Service = require('./module/service');
21
+ const Service = require('./service');
17
22
 
18
23
  /**
19
24
  * @member {Storage}
20
25
  * @since 1.0.0
21
26
  */
22
- const Storage = require('./module/storage');
27
+ const Storage = require('./storage');
23
28
 
24
29
  /**
25
30
  * @member {Utils}
26
31
  * @since 1.0.0
27
32
  */
28
- const Utils = require('./module/oldUtils');
33
+ const Utils = require('./oldUtils');
29
34
 
30
35
  /**
31
36
  * @member {Socket}
32
37
  * @since 1.0.0
33
38
  */
34
- const Socket = require('./module/socket/io');
39
+ const Socket = require('./socket');
35
40
 
36
41
  module.exports = {
37
- get Appliaction () {
38
- if (app) {
39
- return app;
40
- }
41
- app = require('./module/app/application');
42
- return app;
43
- },
42
+ Appliaction: EE.Appliaction,
44
43
  Controller,
45
44
  Service,
46
45
  Storage,
@@ -12,9 +12,9 @@ if (__dirname.indexOf("node_modules") == -1) {
12
12
  Log = require('../../log');
13
13
  UtilsCore = require('../../core/lib/utils');
14
14
  } else {
15
- Exception = require('ee-core/module/exception');
16
- Loader = require('ee-core/module/loader');
17
- Log = require('ee-core/module/log');
15
+ Exception = require('ee-core/exception');
16
+ Loader = require('ee-core/loader');
17
+ Log = require('ee-core/log');
18
18
  UtilsCore = require('../../core/lib/utils');
19
19
  }
20
20
 
@@ -12,9 +12,9 @@ if (__dirname.indexOf("node_modules") == -1) {
12
12
  Log = require('../../log');
13
13
  UtilsCore = require('../../core/lib/utils');
14
14
  } else {
15
- Exception = require('ee-core/module/exception');
16
- Loader = require('ee-core/module/loader');
17
- Log = require('ee-core/module/log');
15
+ Exception = require('ee-core/exception');
16
+ Loader = require('ee-core/loader');
17
+ Log = require('ee-core/log');
18
18
  UtilsCore = require('../../core/lib/utils');
19
19
  }
20
20
 
@@ -16,6 +16,4 @@ const message = {
16
16
 
17
17
  };
18
18
 
19
-
20
-
21
19
  module.exports = message;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ee-core",
3
- "version": "2.0.0-beta.4",
3
+ "version": "2.0.0-beta.6",
4
4
  "description": "ee core",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -9,7 +9,7 @@
9
9
  "author": "",
10
10
  "license": "ISC",
11
11
  "bin": {
12
- "ee-core": "./module/bin/tools.js"
12
+ "ee-core": "./bin/tools.js"
13
13
  },
14
14
  "dependencies": {
15
15
  "agentkeepalive": "^4.2.0",
@@ -20,6 +20,13 @@ exports.env = function() {
20
20
  */
21
21
  exports.getEnv = this.env
22
22
 
23
+ /**
24
+ * 是否生产环境
25
+ */
26
+ exports.isProd = function() {
27
+ return (process.env.EE_SERVER_ENV === 'prod');
28
+ }
29
+
23
30
  /**
24
31
  * 是否为开发环境
25
32
  */
@@ -182,21 +189,21 @@ exports.getUserHomeDir = function () {
182
189
  * 获取主进程端口
183
190
  */
184
191
  exports.getMainPort = function () {
185
- return process.env.EE_MAIN_PORT;
192
+ return parseInt(process.env.EE_MAIN_PORT) || 0;
186
193
  }
187
194
 
188
195
  /**
189
196
  * 获取内置socket端口
190
197
  */
191
198
  exports.getSocketPort = function () {
192
- return process.env.EE_SOCKET_PORT;
199
+ return parseInt(process.env.EE_SOCKET_PORT) || 0;
193
200
  }
194
201
 
195
202
  /**
196
203
  * 获取内置http端口
197
204
  */
198
205
  exports.getHttpPort = function () {
199
- return process.env.EE_HTTP_PORT;
206
+ return parseInt(process.env.EE_HTTP_PORT) || 0;
200
207
  }
201
208
 
202
209
  /**
@@ -10,6 +10,7 @@ const fs = require('fs');
10
10
  const path = require('path');
11
11
  const _ = require('lodash');
12
12
  const Log = require('../log');
13
+ const Ps = require('../ps');
13
14
 
14
15
  /**
15
16
  * http server
@@ -19,12 +20,14 @@ class HttpServer {
19
20
  this.app = app;
20
21
  this.options = this.app.config.httpServer;
21
22
 
22
- if (!this.options.enable) {
23
+ if (this.options.enable == false) {
23
24
  return;
24
25
  }
25
26
 
26
- let port = process.env.EE_HTTP_PORT ? parseInt(process.env.EE_HTTP_PORT) : parseInt(this.getHttpPort());
27
- assert(typeof port === 'number', 'http port required, and must be a number');
27
+ let port = Ps.getHttpPort();
28
+ if (!port) {
29
+ throw new Error('[ee-core] [socket/HttpServer] http port required, and must be a number !');
30
+ }
28
31
 
29
32
  this.create();
30
33
  }
@@ -33,15 +36,15 @@ class HttpServer {
33
36
  * 创建服务
34
37
  */
35
38
  create () {
36
- const self = this;
39
+ const app = this.app;
37
40
  const httpServer = this.options;
38
41
  const isHttps = httpServer?.https?.enable ?? false;
39
42
  let sslOptions = {};
40
43
 
41
44
  if (isHttps === true) {
42
45
  httpServer.protocol = 'https://';
43
- const keyFile = path.join(this.app.config.homeDir, httpServer.https.key);
44
- const certFile = path.join(this.app.config.homeDir, httpServer.https.cert);
46
+ const keyFile = path.join(app.config.homeDir, httpServer.https.key);
47
+ const certFile = path.join(app.config.homeDir, httpServer.https.cert);
45
48
  assert(fs.existsSync(keyFile), 'ssl key file is required');
46
49
  assert(fs.existsSync(certFile), 'ssl cert file is required');
47
50
 
@@ -56,12 +59,12 @@ class HttpServer {
56
59
  .use(cors(corsOptions))
57
60
  .use(koaBody(httpServer.body))
58
61
  .use(async (ctx, next) => {
59
- ctx.eeApp = self.app;
62
+ ctx.eeApp = app;
60
63
  await next();
61
64
  })
62
65
  .use(this.dispatch);
63
66
 
64
- let msg = '[ee-core] [socket/httpServer] server is: ' + url;
67
+ let msg = '[ee-core] [socket/http] server is: ' + url;
65
68
  if (isHttps) {
66
69
  https.createServer(sslOptions, koaApp.callback()).listen(httpServer.port, (err) => {
67
70
  msg = err ? err : msg;
@@ -125,20 +128,11 @@ class HttpServer {
125
128
  const result = await fn.call(ctx.eeApp, args);
126
129
  ctx.response.body = result;
127
130
  } catch (err) {
128
- ctx.eeApp.console.error('[ee-core:http:server] throw error:', err);
131
+ Log.coreLogger.error('[ee-core/httpServer] throw error:', err);
129
132
  }
130
133
 
131
134
  await next();
132
135
  }
133
-
134
- /**
135
- * 获取http端口
136
- */
137
- getHttpPort () {
138
- const cdb = this.getCoreDB();
139
- const port = cdb.getItem('config').httpServer.port;
140
- return parseInt(port);
141
- }
142
136
  }
143
137
 
144
138
  module.exports = HttpServer;
@@ -0,0 +1,82 @@
1
+ 'use strict';
2
+
3
+ const IoServer = require('socket.io');
4
+ const IoClient = require('socket.io-client');
5
+ const Koa = require('koa');
6
+ const EESocketServer = Symbol('Ee#SocketServer');
7
+ const EEIpcServer = Symbol('Ee#IpcServer');
8
+ const EEHttpServer = Symbol('Ee#HttpServer');
9
+
10
+ const EeSocket = {
11
+
12
+ /**
13
+ * 提供基础库 - 避免用户重复安装
14
+ */
15
+ Koa,
16
+ IoServer,
17
+ IoClient,
18
+
19
+ /**
20
+ * 启动所有服务
21
+ */
22
+ startAll(app) {
23
+ this._createSocketServer(app);
24
+ this._createHttpServer(app);
25
+ this._createIpcServer(app);
26
+ },
27
+
28
+ /**
29
+ * 创建SocketServer
30
+ */
31
+ _createSocketServer(app) {
32
+ const SocketServer = require('./socketServer');
33
+ this[EESocketServer] = new SocketServer(app);
34
+
35
+ return this[EESocketServer];
36
+ },
37
+
38
+ /**
39
+ * 获取 Socket Server
40
+ */
41
+ getSocketServer() {
42
+ return this[EESocketServer] || null;
43
+ },
44
+
45
+ /**
46
+ * 创建 Http Server
47
+ */
48
+ _createHttpServer(app) {
49
+ const HttpServer = require('./httpServer');
50
+ this[EEHttpServer] = new HttpServer(app);
51
+
52
+ return this[EEHttpServer];
53
+ },
54
+
55
+ /**
56
+ * 获取 Http Server
57
+ */
58
+ getHttpServer() {
59
+ return this[EEHttpServer] || null;
60
+ },
61
+
62
+ /**
63
+ * 创建 IPC Server
64
+ */
65
+ _createIpcServer(app) {
66
+ const IpcServer = require('./ipcServer');
67
+ this[EEIpcServer] = new IpcServer(app);
68
+
69
+ return this[EEIpcServer];
70
+ },
71
+
72
+ /**
73
+ * 获取 IPC Server
74
+ */
75
+ getIpcServer() {
76
+ return this[EEIpcServer] || null;
77
+ },
78
+
79
+ }
80
+
81
+
82
+ module.exports = EeSocket;
@@ -1,5 +1,9 @@
1
1
  'use strict';
2
2
 
3
+ /**
4
+ * 该文件将废弃
5
+ */
6
+
3
7
  const IoServer = require('socket.io');
4
8
  const IoClient = require('socket.io-client');
5
9
  //const socketServer = require('./socketServer');
@@ -15,8 +15,6 @@ class IpcServer {
15
15
  }
16
16
 
17
17
  register () {
18
- console.log('[ee-core] [socket/IpcServer] register channels');
19
-
20
18
  const self = this;
21
19
  // 遍历方法
22
20
  const files = Utils.filePatterns();
@@ -0,0 +1,66 @@
1
+ 'use strict';
2
+
3
+ const { Server } = require('socket.io');
4
+ const is = require('is-type-of');
5
+ const Log = require('../log');
6
+ const Conf = require('../config');
7
+ const Ps = require('../ps');
8
+ const Channel = require('../const/channel');
9
+
10
+ /**
11
+ * socket server
12
+ */
13
+ class SocketServer {
14
+ constructor (app) {
15
+ this.app = app;
16
+ const options = Conf.getValue('socketServer');
17
+
18
+ if (options.enable == false) {
19
+ return;
20
+ }
21
+
22
+ let port = Ps.getSocketPort();
23
+ if (!port) {
24
+ throw new Error('[ee-core] [socket/socketServer] socekt port required, and must be a number !');
25
+ }
26
+
27
+ Log.coreLogger.info('[ee-core] [socket/socketServer] port is:', port);
28
+
29
+ this.io = new Server(port, options);
30
+ this.connec();
31
+ }
32
+
33
+ connec () {
34
+ const app = this.app;
35
+ this.io.on('connection', (socket) => {
36
+ const channel = Channel.socketIo.partySoftware;
37
+ socket.on(channel, async (message, callback) => {
38
+ Log.coreLogger.info('[ee-core] [socket/socketServer] socket id:' + socket.id + ' message cmd: ' + message.cmd);
39
+
40
+ try {
41
+ // 找函数
42
+ const cmd = message.cmd;
43
+ const args = message.params;
44
+ let fn = null;
45
+ if (is.string(cmd)) {
46
+ const actions = cmd.split('.');
47
+ let obj = app;
48
+ actions.forEach(key => {
49
+ obj = obj[key];
50
+ if (!obj) throw new Error(`class or function '${key}' not exists`);
51
+ });
52
+ fn = obj;
53
+ }
54
+ if (!fn) throw new Error('function not exists');
55
+
56
+ const result = await fn.call(app, args);
57
+ callback(result);
58
+ } catch (err) {
59
+ Log.coreLogger.error('[ee-core] [socket/socketServer] throw error:', err);
60
+ }
61
+ });
62
+ });
63
+ }
64
+ }
65
+
66
+ module.exports = SocketServer;
@@ -90,6 +90,43 @@ class JsondbStorage {
90
90
  .get(keyId)
91
91
  .value();
92
92
 
93
+ return data;
94
+ }
95
+
96
+ /**
97
+ * 设置config对象key属性的值
98
+ */
99
+ setConfigItem (key, value) {
100
+ assert(_.isString(key), `key must be a string`);
101
+ assert(key.length != 0, `key cannot be empty`);
102
+ assert(!this.storageKey.hasOwnProperty(key), `${key} is not allowed`);
103
+
104
+ let cacheKey = this.storageKey.cacheConfig;
105
+ if (!this.db.has(cacheKey).value()) {
106
+ this.db.set(cacheKey, {}).write();
107
+ }
108
+
109
+ let keyId = cacheKey + "." + key;
110
+ this.db
111
+ .set(keyId, value)
112
+ .write();
113
+
114
+ return true;
115
+ }
116
+
117
+ /**
118
+ * 获取config对象key属性的值
119
+ */
120
+ getConfigItem (key) {
121
+ assert(_.isString(key), `key must be a string`);
122
+ assert(key.length != 0, `key cannot be empty`);
123
+
124
+ let cacheKey = this.storageKey.cacheConfig;
125
+ let keyId = cacheKey + "." + key;
126
+ const data = this.db
127
+ .get(keyId)
128
+ .value();
129
+
93
130
  return data;
94
131
  }
95
132
  }
@@ -1,5 +0,0 @@
1
- const App = {
2
-
3
- }
4
-
5
- module.exports = App;
@@ -1,49 +0,0 @@
1
- 'use strict';
2
-
3
- const assert = require('assert');
4
- const IoClient = require('socket.io-client');
5
- const Constants = require('../const');
6
- const Storage = require('../storage');
7
-
8
- class SocketClient {
9
- constructor (port) {
10
- port = port ? parseInt(port) : this.getSocketcPort();
11
-
12
- assert(typeof port === 'number', 'port required, and must be a number');
13
-
14
- const url = 'http://localhost:' + port;
15
- console.log('[ee-core] [socket/socketClient] url:', url);
16
- this.client = IoClient(url);
17
- }
18
-
19
- static getInstance (port) {
20
- if (typeof this.instance === 'object') {
21
- return this.instance;
22
- }
23
- this.instance = new SocketClient(port);
24
- return this.instance;
25
- }
26
-
27
- call (method = '', ...params) {
28
- return new Promise((resolve, reject) => {
29
- // 获取通信频道
30
- const channel = Constants.socketIo.channel.partySoftware;
31
- this.client.emit(channel, { cmd: method, params: params }, (response) => {
32
- resolve(response);
33
- });
34
- });
35
- }
36
-
37
- getCoreDB () {
38
- const coreDB = Storage.connection('system');
39
- return coreDB;
40
- }
41
-
42
- getSocketcPort () {
43
- const cdb = this.getCoreDB();
44
- const port = cdb.getItem('config').socketServer.port;
45
- return parseInt(port);
46
- }
47
- }
48
-
49
- module.exports = SocketClient;
@@ -1,76 +0,0 @@
1
- 'use strict';
2
-
3
- const assert = require('assert');
4
- const { Server } = require('socket.io');
5
- const is = require('is-type-of');
6
- const Storage = require('../storage');
7
- const Constants = require('../const');
8
- const Log = require('../log');
9
-
10
- /**
11
- * socket server
12
- */
13
- class SocketServer {
14
- constructor (app) {
15
- this.app = app;
16
- const options = this.app.config.socketServer;
17
-
18
- if (!options.enable) {
19
- return;
20
- }
21
-
22
- let port = process.env.EE_SOCKET_PORT ? parseInt(process.env.EE_SOCKET_PORT) : parseInt(this.getSocketPort());
23
- assert(typeof port === 'number', 'socekt port required, and must be a number');
24
- this.consoleLogger.info('[ee-core] [socket/socketServer] port is:', port);
25
-
26
- // let opt = Object.assign({}, options);
27
- // delete opt.enable;
28
- this.io = new Server(port, options);
29
- this.connec();
30
- }
31
-
32
- connec () {
33
- const self = this;
34
- this.io.on('connection', (socket) => {
35
- const channel = Constants.socketIo.channel.partySoftware;
36
- socket.on(channel, async (message, callback) => {
37
- console.log('[ee-core] [socket/socketServer] socket id:' + socket.id + ' message cmd: ' + message.cmd);
38
-
39
- try {
40
- // 找函数
41
- const cmd = message.cmd;
42
- const args = message.params;
43
- let fn = null;
44
- if (is.string(cmd)) {
45
- const actions = cmd.split('.');
46
- let obj = self.app;
47
- actions.forEach(key => {
48
- obj = obj[key];
49
- if (!obj) throw new Error(`class or function '${key}' not exists`);
50
- });
51
- fn = obj;
52
- }
53
- if (!fn) throw new Error('function not exists');
54
-
55
- const result = await fn.call(self.app, args);
56
- callback(result);
57
- } catch (err) {
58
- Log.coreLogger.error('[ee-core] [socket/socketServer] throw error:', err);
59
- }
60
- });
61
- });
62
- }
63
-
64
- getCoreDB () {
65
- const coreDB = Storage.connection('system');
66
- return coreDB;
67
- }
68
-
69
- getSocketPort () {
70
- const cdb = this.getCoreDB();
71
- const port = cdb.getItem('config').socketServer.port;
72
- return port;
73
- }
74
- }
75
-
76
- module.exports = SocketServer;
@@ -1,22 +0,0 @@
1
- 'use strict';
2
-
3
- const SocketServer = require('./socketServer');
4
- const IpcServer = require('./ipcServer');
5
- const HttpServer = require('./httpServer');
6
-
7
- /**
8
- * server
9
- */
10
- module.exports = (app) => {
11
-
12
- // 启动 socket server
13
- new SocketServer(app);
14
-
15
- // 启动 http server
16
- new HttpServer(app);
17
-
18
- // 启动 electron ipc server
19
- new IpcServer(app);
20
-
21
- }
22
-
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes