ee-core 2.8.0-beta.2 → 2.8.0

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.
@@ -284,7 +284,7 @@ module.exports = appInfo => {
284
284
  * 硬件加速
285
285
  */
286
286
  config.hardGpu = {
287
- enable: false
287
+ enable: true
288
288
  };
289
289
 
290
290
  /**
package/cross/index.js CHANGED
@@ -1,16 +1,12 @@
1
1
  const fs = require('fs');
2
2
  const EventEmitter = require('events');
3
- const path = require('path');
4
- const is = require('is-type-of');
5
3
  const Conf = require('../config/cache');
6
- const UtilsHelper = require('../utils/helper');
7
- const UtilsIs = require('../utils/is');
8
- const UtilsPargv = require('../utils/pargv');
4
+ const Helper = require('../utils/helper');
9
5
  const Ps = require('../ps');
10
- const Log = require('../log');
11
- const GetPort = require('../utils/get-port');
12
6
  const SpawnProcess = require('./spawnProcess');
13
7
  const Channel = require('../const/channel');
8
+ const extend = require('../utils/extend');
9
+ const GetPort = require('../utils/get-port');
14
10
 
15
11
  /**
16
12
  * Cross-language service
@@ -39,7 +35,7 @@ const CrossLanguageService = {
39
35
 
40
36
  // boot services
41
37
  const servicesCfg = Conf.getValue('cross');
42
- //await UtilsHelper.sleep(5 * 1000);
38
+ //await Helper.sleep(5 * 1000);
43
39
 
44
40
  for (let key of Object.keys(servicesCfg)) {
45
41
  let cfg = servicesCfg[key];
@@ -52,19 +48,17 @@ const CrossLanguageService = {
52
48
  /**
53
49
  * _initEventEmitter
54
50
  */
55
- async _initEventEmitter() {
51
+ _initEventEmitter() {
56
52
  if (this.emitter) {
57
53
  return
58
54
  }
59
55
  this.emitter = new EventEmitter();
60
56
  this.emitter.on(Channel.events.childProcessExit, (data) => {
61
- console.log("------childProcessExit:", data)
62
57
  const child = this.children[data.pid];
63
58
  delete this.childrenMap[child.name];
64
59
  delete this.children[data.pid];
65
60
  });
66
61
  this.emitter.on(Channel.events.childProcessError, (data) => {
67
- console.log("------childProcessError:", data)
68
62
  const child = this.children[data.pid];
69
63
  delete this.childrenMap[child.name];
70
64
  delete this.children[data.pid];
@@ -74,39 +68,32 @@ const CrossLanguageService = {
74
68
  /**
75
69
  * run
76
70
  */
77
- async run(service) {
71
+ async run(service, opt = {}) {
78
72
  // init dir
79
73
  this._initPath();
80
74
 
81
75
  const allConfig = Conf.all();
82
- if (!allConfig.cross.hasOwnProperty(service)) {
76
+ const defaultOpt = allConfig.cross[service] || {};
77
+ const targetConf = extend(true, {}, defaultOpt, opt);
78
+ if (Object.keys(targetConf).length == 0) {
83
79
  throw new Error(`[ee-core] [cross] The service [${service}] config does not exit`);
84
80
  }
85
81
 
86
- const targetConf = Object.assign({}, allConfig.cross[service]);
87
-
88
82
  // eventEmitter
89
83
  this._initEventEmitter();
90
84
 
91
- let cmdPath = this._getCmdPath(targetConf.name);
92
- let cmdArgs = is.string(targetConf.args) ? [targetConf.args] : targetConf.args;
93
- if (targetConf.hasOwnProperty('cmd')) {
94
- cmdPath = targetConf.cmd;
95
- }
96
-
97
- let confPort = this._getValueFromArgs(cmdArgs, 'port');
98
- if (UtilsHelper.validValue(confPort)) {
99
- // 动态生成port
85
+ // format params
86
+ let tmpArgs = targetConf.args;
87
+ let confPort = parseInt(Helper.getValueFromArgv(tmpArgs, 'port'));
88
+ if (confPort > 0) {
89
+ // 动态生成port,传入的端口必须为int
100
90
  confPort = await GetPort({ port: confPort });
101
91
  // 替换port
102
- cmdArgs = this._replaceValue(cmdArgs, "--port=", confPort)
103
- targetConf.args = cmdArgs;
92
+ targetConf.args = Helper.replaceArgsValue(tmpArgs, "port", String(confPort));
104
93
  }
105
94
 
106
- Log.coreLogger.info(`[ee-core] [cross/run] cmd: ${cmdPath}, args: ${cmdArgs}`);
107
-
108
95
  // 创建进程
109
- const subProcess = new SpawnProcess(this, { cmdPath, cmdArgs, targetConf });
96
+ const subProcess = new SpawnProcess(this, { targetConf, port: confPort });
110
97
  let uniqueName = targetConf.name;
111
98
  if (this.childrenMap.hasOwnProperty(uniqueName)) {
112
99
  uniqueName = uniqueName + "-" + String(subProcess.pid);
@@ -123,7 +110,6 @@ const CrossLanguageService = {
123
110
 
124
111
  killAll() {
125
112
  Object.keys(this.children).forEach(pid => {
126
- console.log("-----killAll: ", pid);
127
113
  this.kill(pid)
128
114
  });
129
115
  },
@@ -142,35 +128,9 @@ const CrossLanguageService = {
142
128
  }
143
129
  },
144
130
 
145
- _getValueFromArgs(argv, key) {
146
- // parse args
147
- const argsObj = UtilsPargv(argv);
148
- const value = argsObj[key];
149
-
150
- return value;
151
- },
152
-
153
- _replaceValue(arr, key, value) {
154
- arr = arr.map(item => {
155
- if (item.startsWith(key)) {
156
- let newItem = key + value;
157
- return newItem;
158
- } else {
159
- return item;
160
- }
161
- });
162
- return arr;
163
- },
164
-
165
131
  getUrl(name) {
166
132
  const entity = this.getProcByName(name);
167
- const args = entity.getArgsObj();
168
- let protocol = 'http://';
169
- if (args.hasOwnProperty('ssl') && (args.ssl == 'true' || args.ssl == '1')) {
170
- protocol = 'https://';
171
- }
172
- const hostname = args.hostname ? args.hostname : '127.0.0.1';
173
- const url = protocol + hostname + ":" + args.port;
133
+ const url = entity.getUrl();
174
134
 
175
135
  return url;
176
136
  },
@@ -204,21 +164,16 @@ const CrossLanguageService = {
204
164
  return pids;
205
165
  },
206
166
 
207
- _getCmdPath(name) {
208
- const coreName = UtilsIs.windows() ? name + ".exe" : name;
209
- const p = path.join(Ps.getExtraResourcesDir(), coreName);
210
- return p;
211
- },
212
-
213
167
  /**
214
168
  * init path
215
169
  */
216
170
  _initPath() {
217
171
  const pathname = Ps.getUserHomeConfigDir();
218
172
  if (!fs.existsSync(pathname)) {
219
- UtilsHelper.mkdir(pathname, {mode: 0o755});
173
+ Helper.mkdir(pathname, {mode: 0o755});
220
174
  }
221
- }
175
+ },
176
+
222
177
  }
223
178
 
224
179
  module.exports = CrossLanguageService;
@@ -4,9 +4,10 @@ const crossSpawn = require('cross-spawn');
4
4
  const Log = require('../log');
5
5
  const Ps = require('../ps');
6
6
  const Channel = require('../const/channel');
7
+ const EE = require('../ee');
7
8
  const Helper = require('../utils/helper');
9
+ const UtilsIs = require('../utils/is');
8
10
  const UtilsPargv = require('../utils/pargv');
9
- const EE = require('../ee');
10
11
 
11
12
  class SpawnProcess {
12
13
  constructor(host, opt = {}) {
@@ -14,30 +15,64 @@ class SpawnProcess {
14
15
  this.host = host;
15
16
  this.child = undefined;
16
17
  this.pid = 0;
18
+ this.port = 0;
17
19
  this.name = "";
18
20
  this.config = {};
19
21
  this._init(opt);
20
22
  }
21
23
 
22
-
23
24
  /**
24
25
  * 初始化子进程
25
26
  */
26
27
  _init(options = {}) {
27
- const { cmdPath, cmdArgs, targetConf } = options;
28
+ const { targetConf, port } = options;
28
29
  this.config = targetConf;
29
- this.name = this.config.name;
30
+ this.port = port;
31
+ // 该名称如果在childrenMap重复,会被重写
32
+ this.name = targetConf.name;
30
33
 
31
34
  // Launch executable program
32
- let standardOutput = ['ignore', 'ignore', 'ignore', 'ipc'];
33
- if (!Ps.isPackaged()) {
34
- standardOutput = ['inherit', 'inherit', 'inherit', 'ipc'];
35
- }
35
+ let cmdPath = '';
36
+ let cmdArgs = targetConf.args;
36
37
  let execDir = Ps.getExtraResourcesDir();
37
- if (this.config.hasOwnProperty('directory')) {
38
- execDir = path.join(process.cwd(), this.config.directory);
38
+ let standardOutput = ['inherit', 'inherit', 'inherit', 'ipc'];
39
+ if (Ps.isPackaged()) {
40
+ standardOutput = ['ignore', 'ignore', 'ignore', 'ipc'];
41
+ }
42
+
43
+ const { cmd, directory } = targetConf;
44
+ // use cmd first
45
+ if (cmd) {
46
+ if (!directory) {
47
+ throw new Error(`[ee-core] [cross] The config [directory] attribute does not exist`);
48
+ }
49
+ cmdPath = cmd;
50
+ if (!path.isAbsolute(cmd) && !Ps.isDev()) {
51
+ cmdPath = path.join(Ps.getExtraResourcesDir(), cmd);
52
+ }
53
+ } else {
54
+ cmdPath = path.join(Ps.getExtraResourcesDir(), targetConf.name);
39
55
  }
40
56
 
57
+ // windonw
58
+ if (UtilsIs.windows() && path.extname(cmdPath) != '.exe') {
59
+ cmdPath += ".exe";
60
+ }
61
+
62
+ // executable program directory
63
+ if (directory && path.isAbsolute(directory)) {
64
+ execDir = directory;
65
+ } else if (directory && !path.isAbsolute(directory)) {
66
+ if (Ps.isDev()) {
67
+ execDir = path.join(Ps.getHomeDir(), directory);
68
+ } else {
69
+ execDir = path.join(Ps.getExtraResourcesDir(), directory);
70
+ }
71
+ } else {
72
+ execDir = Ps.getExtraResourcesDir();
73
+ }
74
+
75
+ Log.coreLogger.info(`[ee-core] [cross/run] cmd: ${cmdPath}, args: ${cmdArgs}`);
41
76
  const coreProcess = crossSpawn(cmdPath, cmdArgs, {
42
77
  stdio: standardOutput,
43
78
  detached: false,
@@ -70,7 +105,6 @@ class SpawnProcess {
70
105
  * kill
71
106
  */
72
107
  kill(timeout = 1000) {
73
- console.log("----- spawnProcess kill ---- ");
74
108
  this.child.kill('SIGINT');
75
109
  setTimeout(() => {
76
110
  if (this.child.killed) return;
@@ -78,12 +112,42 @@ class SpawnProcess {
78
112
  }, timeout)
79
113
  }
80
114
 
81
- send(message) {
82
- return this.sendByType(message, 'message');
115
+ // send(message) {
116
+ // return this.sendByType(message, 'message');
117
+ // }
118
+
119
+ // close() {
120
+ // return this.sendByType('close', 'close');
121
+ // }
122
+
123
+ // async sendByType(message, type) {
124
+ // const msg = typeof message === 'string' ? message : JSON.stringify(message);
125
+ // const id = this._generateId();
126
+
127
+ // this.child.send({
128
+ // id,
129
+ // type,
130
+ // data: msg,
131
+ // });
132
+ // return;
133
+ // }
134
+
135
+ getUrl() {
136
+ const ssl = Helper.getValueFromArgv(this.config.args, 'ssl');
137
+ let hostname = Helper.getValueFromArgv(this.config.args, 'hostname')
138
+ let protocol = 'http://';
139
+ if (ssl && (ssl == 'true' || ssl == '1')) {
140
+ protocol = 'https://';
141
+ }
142
+ hostname = hostname ? hostname : '127.0.0.1';
143
+ const url = protocol + hostname + ":" + this.port;
144
+
145
+ return url;
83
146
  }
84
147
 
85
- close() {
86
- return this.sendByType('close', 'close');
148
+ getArgsObj() {
149
+ const obj = UtilsPargv(this.config.args);
150
+ return obj;
87
151
  }
88
152
 
89
153
  _generateId() {
@@ -91,23 +155,6 @@ class SpawnProcess {
91
155
  return `node:${this.pid}:${rid}`;
92
156
  }
93
157
 
94
- async sendByType(message, type) {
95
- const msg = typeof message === 'string' ? message : JSON.stringify(message);
96
- const id = this._generateId();
97
-
98
- this.child.send({
99
- id,
100
- type,
101
- data: msg,
102
- });
103
- return;
104
- }
105
-
106
- getArgsObj() {
107
- const obj = UtilsPargv(this.config.args);
108
- return obj;
109
- }
110
-
111
158
  /**
112
159
  * exit electron
113
160
  */
package/ee/eeApp.js CHANGED
@@ -32,19 +32,19 @@ class EeApp extends BaseApp {
32
32
  */
33
33
  async createPorts() {
34
34
  if (Ps.isFrameworkMode() && Conf.isWebProtocol(this.config.mainServer)) {
35
- const mainPort = await GetPort({port: this.config.mainServer.port});
35
+ const mainPort = await GetPort({port: parseInt(this.config.mainServer.port)});
36
36
  process.env.EE_MAIN_PORT = mainPort;
37
37
  this.config.mainServer.port = mainPort;
38
38
  }
39
39
 
40
40
  if (this.config.socketServer.enable) {
41
- const socketPort = await GetPort({port: this.config.socketServer.port});
41
+ const socketPort = await GetPort({port: parseInt(this.config.socketServer.port)});
42
42
  process.env.EE_SOCKET_PORT = socketPort;
43
43
  this.config.socketServer.port = socketPort;
44
44
  }
45
45
 
46
46
  if (this.config.httpServer.enable) {
47
- const httpPort = await GetPort({port: this.config.httpServer.port});
47
+ const httpPort = await GetPort({port: parseInt(this.config.httpServer.port)});
48
48
  process.env.EE_HTTP_PORT = httpPort;
49
49
  this.config.httpServer.port = httpPort;
50
50
  }
package/main/index.js CHANGED
@@ -30,8 +30,6 @@ class ElectronEgg {
30
30
  if ( argsObj['env'] == 'development' || argsObj['env'] === 'dev' || argsObj['env'] === 'local' ) {
31
31
  isDev = true;
32
32
  }
33
- console.log("argsObj env:", argsObj)
34
- console.log("isDev:", isDev)
35
33
 
36
34
  // module mode
37
35
  if (Ps.isModuleMode()) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ee-core",
3
- "version": "2.8.0-beta.2",
3
+ "version": "2.8.0",
4
4
  "description": "ee core",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/utils/helper.js CHANGED
@@ -5,6 +5,7 @@ const is = require('is-type-of');
5
5
  const co = require('./co');
6
6
  const path = require('path');
7
7
  const chalk = require('chalk');
8
+ const Pargv = require('./pargv');
8
9
 
9
10
  const _basePath = process.cwd();
10
11
 
@@ -172,4 +173,42 @@ exports.loadConfig = function(prop) {
172
173
 
173
174
  exports.sleep = function(ms) {
174
175
  return new Promise(resolve => setTimeout(resolve, ms));
176
+ };
177
+
178
+ exports.replaceArgsValue = function(argv, key, value) {
179
+ key = key + "=";
180
+ for (let i = 0; i < argv.length; i++) {
181
+ let item = argv[i];
182
+ let pos = item.indexOf(key);
183
+ if (pos !== -1) {
184
+ pos = pos + key.length;
185
+ let tmpStr = item.substring(0, pos);
186
+ argv[i] = tmpStr + value;
187
+ break;
188
+ }
189
+ }
190
+
191
+ return argv;
192
+ };
193
+
194
+ exports.getValueFromArgv = function(argv, key) {
195
+ const argvObj = Pargv(argv);
196
+ if (argvObj.hasOwnProperty(key)) {
197
+ return argvObj[key];
198
+ }
199
+
200
+ // match search
201
+ key = key + "=";
202
+ let value;
203
+ for (let i = 0; i < argv.length; i++) {
204
+ let item = argv[i];
205
+ let pos = item.indexOf(key);
206
+ if (pos !== -1) {
207
+ pos = pos + key.length;
208
+ value = item.substring(pos);
209
+ break;
210
+ }
211
+ }
212
+
213
+ return value;
175
214
  };