ee-core 1.5.2-beta.2 → 2.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/config/config.default.js +8 -1
- package/core/lib/ee.js +1 -1
- package/core/lib/loader/ee_loader.js +7 -3
- package/core/lib/loader/mixin/controller.js +3 -2
- package/core/lib/utils/function.js +30 -0
- package/index.js +2 -2
- package/lib/appLoader.js +0 -5
- package/lib/application.js +7 -6
- package/lib/baseApp.js +9 -26
- package/lib/eeApp.js +18 -52
- package/{lib/constant.js → module/const/index.js} +3 -0
- package/{lib/httpclient.js → module/httpclient/index.js} +45 -11
- package/module/jobs/child/forkProcess.js +99 -0
- package/module/jobs/child/index.js +33 -0
- package/module/jobs/index.js +55 -0
- package/module/jobs/renderer/index.js +140 -0
- package/module/jobs/renderer/loadView.js +40 -0
- package/module/loader/index.js +78 -0
- package/module/log/index.js +53 -0
- package/module/log/logger.js +61 -0
- package/module/message/index.js +13 -0
- package/module/message/ipcMain.js +160 -0
- package/module/message/ipcRender.js +0 -0
- package/{lib → module}/socket/httpServer.js +0 -0
- package/{lib → module}/socket/io.js +0 -0
- package/{lib → module}/socket/ipcServer.js +6 -8
- package/{lib → module}/socket/socketClient.js +4 -3
- package/{lib → module}/socket/socketServer.js +4 -3
- package/module/socket/start.js +22 -0
- package/{lib → module}/storage/index.js +13 -12
- package/module/storage/jsondb/adapters/Base.js +14 -0
- package/module/storage/jsondb/adapters/FileSync.js +32 -0
- package/{lib/storage/lowdb → module/storage/jsondb}/main.js +6 -10
- package/{lib/storage/lowdbStorage.js → module/storage/jsondbStorage.js} +13 -14
- package/{lib → module}/storage/sqliteStorage.js +7 -11
- package/module/utils/copyto.js +161 -0
- package/module/utils/helper.js +117 -0
- package/module/utils/index.js +120 -0
- package/module/utils/json.js +72 -0
- package/module/utils/ps.js +175 -0
- package/{utils → module/utils}/wrap.js +0 -2
- package/package.json +3 -7
- package/tools/encrypt.js +2 -2
- package/utils/index.js +17 -135
- package/lib/logger.js +0 -47
- package/lib/socket/start.js +0 -22
- package/lib/storage/lowdb/adapters/Base.js +0 -15
- package/lib/storage/lowdb/adapters/FileAsync.js +0 -41
- package/lib/storage/lowdb/adapters/FileSync.js +0 -39
- package/lib/storage/lowdb/adapters/LocalStorage.js +0 -20
- package/lib/storage/lowdb/adapters/Memory.js +0 -8
- package/lib/storage/lowdb/adapters/_stringify.js +0 -4
- package/lib/storage/lowdb/common.js +0 -33
- package/lib/storage/lowdb/fp.js +0 -23
- package/lib/storage/lowdb/isPromise.js +0 -6
- package/lib/storage/lowdb/nano.js +0 -5
- package/utils/common.js +0 -91
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const assert = require('assert');
|
|
4
|
+
const RendererJob = require('./renderer');
|
|
5
|
+
const ChildJob = require('./child');
|
|
6
|
+
const Utils = require('../utils');
|
|
7
|
+
const Loader = require('../loader');
|
|
8
|
+
|
|
9
|
+
class Jobs {
|
|
10
|
+
constructor() {
|
|
11
|
+
this.type = undefined;
|
|
12
|
+
this.instance = undefined;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 创建 job
|
|
17
|
+
*/
|
|
18
|
+
create (name, opt = {}) {
|
|
19
|
+
this.type = opt.type || 'child';
|
|
20
|
+
this.dev = opt.dev || false;
|
|
21
|
+
this.winOptions = opt.winOptions || {};
|
|
22
|
+
this.childOptions = opt.childOptions || {};
|
|
23
|
+
this.path = opt.path || null;
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
const isAbsolute = path.isAbsolute(this.path);
|
|
27
|
+
if (!isAbsolute) {
|
|
28
|
+
this.path = path.join(Utils.getBaseDir(), 'jobs', this.path);
|
|
29
|
+
}
|
|
30
|
+
const filepath = Loader.resolveModule(this.path);
|
|
31
|
+
|
|
32
|
+
assert(fs.existsSync(filepath), `file ${filepath} not exists`);
|
|
33
|
+
|
|
34
|
+
this.path = filepath;
|
|
35
|
+
if (this.type == 'child') {
|
|
36
|
+
this.instance = new ChildJob(name, filepath, this.childOptions);
|
|
37
|
+
} else if (this.type == 'renderer') {
|
|
38
|
+
this.instance = new RendererJob(name, filepath, this.winOptions);
|
|
39
|
+
if (this.dev) {
|
|
40
|
+
this.openDevTools();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 显示开发者工具栏(仅支持 RendererJob)
|
|
49
|
+
*/
|
|
50
|
+
openDevTools () {
|
|
51
|
+
this.instance.openDevTools();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
module.exports = Jobs;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
//require('bytenode');
|
|
2
|
+
const { BrowserWindow } = require('electron');
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const LoadView = require('./loadView');
|
|
5
|
+
|
|
6
|
+
class RendererJob {
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* constructor
|
|
10
|
+
* @param {String} name - job name
|
|
11
|
+
* @param {String} filepath - filepath to file
|
|
12
|
+
* @param {Object} options - options to create BrowserWindow
|
|
13
|
+
*/
|
|
14
|
+
constructor(name, filepath, opt = {}) {
|
|
15
|
+
let options = Object.assign({
|
|
16
|
+
show: false,
|
|
17
|
+
webPreferences: {
|
|
18
|
+
webSecurity: true,
|
|
19
|
+
nodeIntegration: true,
|
|
20
|
+
contextIsolation: false,
|
|
21
|
+
//enableRemoteModule: true
|
|
22
|
+
}
|
|
23
|
+
}, opt);
|
|
24
|
+
|
|
25
|
+
this.subWin = new BrowserWindow(options);
|
|
26
|
+
|
|
27
|
+
this.jobReady = false;
|
|
28
|
+
this.exec = filepath;
|
|
29
|
+
this.name = name;
|
|
30
|
+
this.listeners = [];
|
|
31
|
+
this.callbacks = [];
|
|
32
|
+
this.fails = [];
|
|
33
|
+
this.id = this.subWin.id;
|
|
34
|
+
this.webSecurity = options.webPreferences.webSecurity;
|
|
35
|
+
|
|
36
|
+
// this.callbacks.push(() => {
|
|
37
|
+
// MessageChannel.registry(name, this.id, this.subWin.webContents.getOSProcessId());
|
|
38
|
+
// });
|
|
39
|
+
|
|
40
|
+
// job state listener
|
|
41
|
+
this.subWin.webContents.on('did-finish-load', this._didFinishLoad);
|
|
42
|
+
this.subWin.webContents.on('did-fail-load', this._didFailLoad);
|
|
43
|
+
|
|
44
|
+
// load job
|
|
45
|
+
this._loadJob(this.exec);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 显示开发者工具栏
|
|
50
|
+
*/
|
|
51
|
+
openDevTools() {
|
|
52
|
+
this.subWin.webContents.openDevTools({
|
|
53
|
+
mode: 'undocked'
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 窗口加载完成,即业务代码执行完毕
|
|
59
|
+
*/
|
|
60
|
+
_didFinishLoad = () => {
|
|
61
|
+
this.jobReady = true;
|
|
62
|
+
this.callbacks.forEach(callback => {
|
|
63
|
+
callback(this.id);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 窗口加载失败,即业务运行失败
|
|
69
|
+
*/
|
|
70
|
+
_didFailLoad = (error) => {
|
|
71
|
+
this.jobReady = false;
|
|
72
|
+
this.fails.forEach(handle => {
|
|
73
|
+
handle(error.toString());
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 加载任务
|
|
80
|
+
*/
|
|
81
|
+
_loadJob(filepath) {
|
|
82
|
+
if (!this.webSecurity) {
|
|
83
|
+
this._loadURLUnsafe(filepath);
|
|
84
|
+
} else {
|
|
85
|
+
this._loadURLSafe(filepath);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 安全的脚本注入
|
|
91
|
+
*/
|
|
92
|
+
_loadURLSafe(filepath) {
|
|
93
|
+
return new Promise((resolve, reject) => {
|
|
94
|
+
fs.readFile(filepath, { encoding: 'utf-8' }, (err, buffer) => {
|
|
95
|
+
if (err) {
|
|
96
|
+
reject(err);
|
|
97
|
+
this._didFailLoad(err);
|
|
98
|
+
return console.error(err);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
let param = {
|
|
102
|
+
webSecurity: true,
|
|
103
|
+
script: buffer.toString(),
|
|
104
|
+
title: `${this.name} job`,
|
|
105
|
+
base: filepath
|
|
106
|
+
}
|
|
107
|
+
const viewData = LoadView(param);
|
|
108
|
+
|
|
109
|
+
this.subWin.loadURL(viewData)
|
|
110
|
+
.then(resolve)
|
|
111
|
+
.catch(err => {
|
|
112
|
+
reject(err);
|
|
113
|
+
this._didFailLoad(err);
|
|
114
|
+
console.error(err);
|
|
115
|
+
});
|
|
116
|
+
})
|
|
117
|
+
})
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* 不安全的脚本注入
|
|
122
|
+
*/
|
|
123
|
+
_loadURLUnsafe(filepath) {
|
|
124
|
+
let param = {
|
|
125
|
+
webSecurity: false,
|
|
126
|
+
src: this.exec,
|
|
127
|
+
title: `${this.name} job`,
|
|
128
|
+
base: filepath
|
|
129
|
+
}
|
|
130
|
+
const viewData = LoadView(param);
|
|
131
|
+
|
|
132
|
+
this.subWin.loadURL(viewData)
|
|
133
|
+
.catch(err => {
|
|
134
|
+
this._didFailLoad(err);
|
|
135
|
+
console.error(err);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
module.exports = RendererJob;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const Ps = require('../../utils/ps');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* loadView 生成BrowserWindow的html content
|
|
5
|
+
*/
|
|
6
|
+
const loadView = function (opt = {}) {
|
|
7
|
+
const webSecurity = opt.webSecurity;
|
|
8
|
+
const src = opt.src;
|
|
9
|
+
const title = opt.title;
|
|
10
|
+
const script = opt.script;
|
|
11
|
+
|
|
12
|
+
//const scriptUrl = new URL('eefile://' + src);
|
|
13
|
+
const scriptUrl = 'eefile://' + src;
|
|
14
|
+
console.log('[ee-core:job] scriptUrl: ', scriptUrl);
|
|
15
|
+
|
|
16
|
+
// 脚本内容
|
|
17
|
+
//const scriptBytenode = Ps.isDev() ? '' : `<script> require('bytenode') </script>`;
|
|
18
|
+
const scriptContent = webSecurity ? `<script> ${ script } </script>` : `<script src='${scriptUrl}'></script>`;
|
|
19
|
+
|
|
20
|
+
// html内容
|
|
21
|
+
const htmlContent = (`
|
|
22
|
+
<!DOCTYPE html>
|
|
23
|
+
<html>
|
|
24
|
+
<head>
|
|
25
|
+
<title>${title}</title>
|
|
26
|
+
<meta charset="UTF-8">
|
|
27
|
+
</head>
|
|
28
|
+
<body>
|
|
29
|
+
${scriptContent}
|
|
30
|
+
</body>
|
|
31
|
+
</html>
|
|
32
|
+
`);
|
|
33
|
+
|
|
34
|
+
const DataURI = 'data:text/html;charset=UTF-8,';
|
|
35
|
+
const data = DataURI + encodeURIComponent(htmlContent);
|
|
36
|
+
|
|
37
|
+
return data;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
module.exports = loadView;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
const is = require('is-type-of');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const UtilsCore = require('../../core/lib/utils');
|
|
5
|
+
const Ps = require('../utils/ps');
|
|
6
|
+
|
|
7
|
+
module.exports = {
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 加载单个文件(如果是函数,将被执行)
|
|
11
|
+
*
|
|
12
|
+
* @param {String} filepath - fullpath
|
|
13
|
+
* @param {Array} inject - pass rest arguments into the function when invoke
|
|
14
|
+
* @return {Object} exports
|
|
15
|
+
* @since 1.0.0
|
|
16
|
+
*/
|
|
17
|
+
loadFile (filepath, ...inject) {
|
|
18
|
+
filepath = filepath && this.resolveModule(filepath);
|
|
19
|
+
if (!filepath) {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const ret = UtilsCore.loadFile(filepath);
|
|
24
|
+
if (is.function(ret) && !is.class(ret) && !UtilsCore.isBytecodeClass(ret)) {
|
|
25
|
+
ret = ret(...inject);
|
|
26
|
+
}
|
|
27
|
+
return ret;
|
|
28
|
+
},
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 模块的绝对路径
|
|
32
|
+
*/
|
|
33
|
+
resolveModule(filepath) {
|
|
34
|
+
const isAbsolute = path.isAbsolute(filepath);
|
|
35
|
+
if (!isAbsolute) {
|
|
36
|
+
filepath = path.join(Ps.getBaseDir(), 'jobs', filepath);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
let fullPath;
|
|
40
|
+
try {
|
|
41
|
+
fullPath = require.resolve(filepath);
|
|
42
|
+
} catch (e) {
|
|
43
|
+
let jscFile = filepath + '.jsc';
|
|
44
|
+
if (fs.existsSync(jscFile)) {
|
|
45
|
+
return jscFile;
|
|
46
|
+
}
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return fullPath;
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 加载模块(子进程中使用)
|
|
55
|
+
*
|
|
56
|
+
* @param {String} filepath - fullpath
|
|
57
|
+
* @return {Object} exports
|
|
58
|
+
* @since 1.0.0
|
|
59
|
+
*/
|
|
60
|
+
requireModule (filepath) {
|
|
61
|
+
filepath = filepath && this.resolveModule(filepath);
|
|
62
|
+
if (!filepath) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
const ret = UtilsCore.loadFile(filepath);
|
|
66
|
+
|
|
67
|
+
return ret;
|
|
68
|
+
},
|
|
69
|
+
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
const Logger = require('./logger');
|
|
2
|
+
const EELoggers = Symbol('EeApplication#EELoggers');
|
|
3
|
+
|
|
4
|
+
const Log = {
|
|
5
|
+
/**
|
|
6
|
+
* 创建日志实例
|
|
7
|
+
*/
|
|
8
|
+
create (config) {
|
|
9
|
+
const eeLog = Logger.create(config);
|
|
10
|
+
|
|
11
|
+
return eeLog;
|
|
12
|
+
},
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* logger
|
|
16
|
+
*/
|
|
17
|
+
get logger() {
|
|
18
|
+
if (!this[EELoggers]) {
|
|
19
|
+
this[EELoggers] = Logger.create();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return this[EELoggers]['logger'] || null;
|
|
23
|
+
},
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* coreLogger
|
|
27
|
+
*/
|
|
28
|
+
get coreLogger () {
|
|
29
|
+
if (!this[EELoggers]) {
|
|
30
|
+
this[EELoggers] = Logger.create();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return this[EELoggers]['coreLogger'] || null;
|
|
34
|
+
},
|
|
35
|
+
|
|
36
|
+
get error() {
|
|
37
|
+
return this.logger.error.bind(this.logger);
|
|
38
|
+
},
|
|
39
|
+
|
|
40
|
+
get warn() {
|
|
41
|
+
return this.logger.warn.bind(this.logger);
|
|
42
|
+
},
|
|
43
|
+
|
|
44
|
+
get info() {
|
|
45
|
+
return this.logger.info.bind(this.logger);
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
get debug() {
|
|
49
|
+
return this.logger.debug.bind(this.logger);
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
module.exports = Log;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
const Loggers = require('egg-logger').EggLoggers;
|
|
2
|
+
const assert = require('assert');
|
|
3
|
+
const Ps = require('../utils/ps');
|
|
4
|
+
const Storage = require('../storage');
|
|
5
|
+
|
|
6
|
+
module.exports = {
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 创建
|
|
10
|
+
*/
|
|
11
|
+
create(config = {}) {
|
|
12
|
+
let opt = {};
|
|
13
|
+
|
|
14
|
+
if (Object.keys(config).length == 0) {
|
|
15
|
+
const defaultConfig = {
|
|
16
|
+
logger: {
|
|
17
|
+
type: 'application',
|
|
18
|
+
dir: Ps.getLogDir(),
|
|
19
|
+
encoding: 'utf8',
|
|
20
|
+
env: Ps.env(),
|
|
21
|
+
level: 'INFO',
|
|
22
|
+
consoleLevel: 'INFO',
|
|
23
|
+
disableConsoleAfterReady: !Ps.isDev(),
|
|
24
|
+
outputJSON: false,
|
|
25
|
+
buffer: true,
|
|
26
|
+
appLogName: `ee.log`,
|
|
27
|
+
coreLogName: 'ee-core.log',
|
|
28
|
+
agentLogName: 'ee-agent.log',
|
|
29
|
+
errorLogName: `ee-error.log`,
|
|
30
|
+
coreLogger: {},
|
|
31
|
+
allowDebugAtProd: false,
|
|
32
|
+
enablePerformanceTimer: false,
|
|
33
|
+
},
|
|
34
|
+
customLogger: {}
|
|
35
|
+
}
|
|
36
|
+
const sysConfig = this._getCoreDB().getItem('config');
|
|
37
|
+
opt = Object.assign(defaultConfig, {
|
|
38
|
+
logger: sysConfig.logger,
|
|
39
|
+
customLogger: sysConfig.customLogger || {}
|
|
40
|
+
});
|
|
41
|
+
} else {
|
|
42
|
+
opt.logger = config.logger;
|
|
43
|
+
opt.customLogger = config.customLogger;
|
|
44
|
+
}
|
|
45
|
+
//console.log('log---------', config);
|
|
46
|
+
|
|
47
|
+
assert(Object.keys(opt).length != 0, `logger config is null`);
|
|
48
|
+
|
|
49
|
+
const loggers = new Loggers(opt);
|
|
50
|
+
|
|
51
|
+
return loggers;
|
|
52
|
+
},
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 获取 coredb
|
|
56
|
+
*/
|
|
57
|
+
_getCoreDB() {
|
|
58
|
+
const coreDB = Storage.connection('system');
|
|
59
|
+
return coreDB;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
|
|
2
|
+
class IpcMain {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.event = new EventEmitter();
|
|
5
|
+
this.services = {};
|
|
6
|
+
/* 根据name获取window id */
|
|
7
|
+
ipcMain.handle('MessageChannel.getIdFromName', (e, args) => {
|
|
8
|
+
return (this.services[args.name] || {}).id;
|
|
9
|
+
});
|
|
10
|
+
/* 使用name和window id注册一个服务 */
|
|
11
|
+
ipcMain.handle('MessageChannel.registryService', (e, args) => {
|
|
12
|
+
const { name, id } = args;
|
|
13
|
+
this.registry(name, id);
|
|
14
|
+
return this.services[name];
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* invoke [在主进程(main)中向另外一个服务进程(service)发送异步请求,并取得回调Promise]
|
|
21
|
+
* @param {[String]} name [服务名]
|
|
22
|
+
* @param {[String]} channel [服务监听的信号名]
|
|
23
|
+
* @param {[Any]} args [携带参数(会被序列化,不会传递对象Proptype信息)]
|
|
24
|
+
* @return {[Promise]} [回调]
|
|
25
|
+
*/
|
|
26
|
+
invoke (name, channel, args={}) {
|
|
27
|
+
const pid = getRandomString();
|
|
28
|
+
const { id } = this.services[name];
|
|
29
|
+
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
if (name === 'main') reject(new Error(`MessageChannel: the main process can not send a message to itself!`))
|
|
32
|
+
if (!id) reject(new Error(`MessageChannel: can not get the id of the window names ${name}`));
|
|
33
|
+
const win = BrowserWindow.fromId(id);
|
|
34
|
+
if (!win) reject(new Error(`MessageChannel: can not find a window with id: ${id}`));
|
|
35
|
+
win.webContents.send(channel, Object.assign(args, { pid, isFromMain: true }));
|
|
36
|
+
ipcMain.once(pid, function(event, rsp) {
|
|
37
|
+
resolve(rsp);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* handle [在主进程中(main)监听来自其它渲染进程(service/window)的请求,将promiseFunc执行的结果返回]
|
|
45
|
+
* @param {[String]} channel [服务监听的信号名]
|
|
46
|
+
* @param {[Function]} promiseFunc [此函数执行的结果会被发送到消息发送者]
|
|
47
|
+
* @return {[Promise]} [回调]
|
|
48
|
+
*/
|
|
49
|
+
handle(channel, promiseFunc) {
|
|
50
|
+
if (!promiseFunc instanceof Function) throw new Error('MessageChannel: promiseFunc must be a function!');
|
|
51
|
+
ipcMain.handle(channel, (event, ...args) => {
|
|
52
|
+
return promiseFunc(event, ...args).then((result) => {
|
|
53
|
+
|
|
54
|
+
return result;
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* handle [在主进程中(main)监听一次来自其它渲染进程(service/window)的请求,将promiseFunc执行的结果返回]
|
|
61
|
+
* @param {[String]} channel [服务监听的信号名]
|
|
62
|
+
* @param {[Function]} promiseFunc [此函数执行的结果会被发送到消息发送者]
|
|
63
|
+
* @return {[Promise]} [回调]
|
|
64
|
+
*/
|
|
65
|
+
handleOnce(channel, promiseFunc) {
|
|
66
|
+
if (!promiseFunc instanceof Function) throw new Error('MessageChannel: promiseFunc must be a function!');
|
|
67
|
+
ipcMain.handleOnce(channel, (event, ...args) => {
|
|
68
|
+
return promiseFunc(event, ...args).then(result => {
|
|
69
|
+
|
|
70
|
+
return result;
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* send [在主进程(main)向另外一个服务进程(service)发送异步请求,不可立即取得值,请配合on监听信号使用]
|
|
77
|
+
* @param {[String]} name [服务名]
|
|
78
|
+
* @param {[String]} channel [服务监听的信号名]
|
|
79
|
+
* @param {[Any]} args [携带参数(会被序列化,不会传递对象Proptype信息)]
|
|
80
|
+
*/
|
|
81
|
+
send(name, channel, args={}) {
|
|
82
|
+
const id = (this.services[name] || {}).id;
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
if (!id) throw new Error(`MessageChannel: can not get the id of the window names ${name}`);
|
|
86
|
+
const win = BrowserWindow.fromId(id);
|
|
87
|
+
if (!win) throw new Error(`MessageChannel: can not find a window with id: ${id}`);
|
|
88
|
+
|
|
89
|
+
win.webContents.send(channel, args);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* send [在主进程中(main)向指定某个id的渲染进程窗口(service/window)发送请求,不可立即取得值,请配合on监听信号使用]
|
|
94
|
+
* @param {[String]} id [window id]
|
|
95
|
+
* @param {[String]} channel [服务监听的信号名]
|
|
96
|
+
* @param {[Any]} args [携带参数(会被序列化,不会传递对象Proptype信息)]
|
|
97
|
+
*/
|
|
98
|
+
sendTo(id, channel, args) {
|
|
99
|
+
|
|
100
|
+
if (!BrowserWindow.fromId(id)) throw new Error(`MessageChannel: can not find a window with id:${id}!`);
|
|
101
|
+
BrowserWindow.fromId(id).webContents.send(channel, args);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* on [在主进程中(main)监听来自其它渲染进程(service/window)的请求]
|
|
106
|
+
* @param {[String]} channel [服务监听的信号名]
|
|
107
|
+
* @param {[Function]} func [消息到达后,此函数会被触发,同于原生ipcRenderer.on]
|
|
108
|
+
*/
|
|
109
|
+
on(channel, func) {
|
|
110
|
+
if (!func instanceof Function) throw new Error('MessageChannel: func must be a function!');
|
|
111
|
+
ipcMain.on(channel, (event, ...args) => {
|
|
112
|
+
|
|
113
|
+
func(event, ...args);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* once [在主进程中(main)监听一次来自其它渲染进程(service/window)的请求]
|
|
119
|
+
* @param {[String]} channel [服务监听的信号名]
|
|
120
|
+
* @param {[Function]} func [消息到达后,此函数会被触发,同于原生ipcRenderer.on]
|
|
121
|
+
*/
|
|
122
|
+
once(channel, func) {
|
|
123
|
+
if (!func instanceof Function) throw new Error('MessageChannel: func must be a function!');
|
|
124
|
+
ipcMain.once(channel, (event, ...args) => {
|
|
125
|
+
|
|
126
|
+
func(event, ...args);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* registry [注册BrowserWindow和BrowserService]
|
|
132
|
+
* @param {[String]} name [唯一的名字]
|
|
133
|
+
* @param {[String]} id [window id]
|
|
134
|
+
* @param {[String]} pid [process id]
|
|
135
|
+
*/
|
|
136
|
+
registry(name, id, pid) {
|
|
137
|
+
if (name === 'main') throw new Error(`MessageChannel: you can not registry a service named:${name}, it's reserved for the main process!`)
|
|
138
|
+
if (this.services[name]) console.warn(`MessageChannel: the service - ${name} has been registeried!`)
|
|
139
|
+
this.services[name] = { name, id, pid };
|
|
140
|
+
this.event.emit('registry', this.services[name]);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* unregistry [注册BrowserWindow和BrowserService]
|
|
145
|
+
* @param {[String]} name [唯一的名字]
|
|
146
|
+
* @param {[String]} id [window id]
|
|
147
|
+
* @param {[String]} pid [process id]
|
|
148
|
+
*/
|
|
149
|
+
unregistry(name) {
|
|
150
|
+
if (name === 'main') throw new Error(`MessageChannel: you can not unregistry a service named:${name}, it's reserved for the main process!`);
|
|
151
|
+
if (this.services[name]) console.warn(`MessageChannel: the service - ${name} will be unregisteried!`);
|
|
152
|
+
if (this.services[name]) {
|
|
153
|
+
this.event.emit('unregistry', this.services[name]);
|
|
154
|
+
delete this.services[name];
|
|
155
|
+
} else {
|
|
156
|
+
console.warn(`MessageChannel: unregistry -> the service - ${name} is not found!`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
1
|
const debug = require('debug')('ee-core:ipcServer');
|
|
4
2
|
const EggConsoleLogger = require('egg-logger').EggConsoleLogger;
|
|
5
3
|
const is = require('is-type-of');
|
|
@@ -7,8 +5,8 @@ const { ipcMain } = require('electron');
|
|
|
7
5
|
const path = require('path');
|
|
8
6
|
const fs = require('fs');
|
|
9
7
|
const globby = require('globby');
|
|
10
|
-
const
|
|
11
|
-
const
|
|
8
|
+
const Utils = require('../../core/lib/utils');
|
|
9
|
+
const Wrap = require('../utils/wrap');
|
|
12
10
|
|
|
13
11
|
class IpcServer {
|
|
14
12
|
constructor (app) {
|
|
@@ -24,7 +22,7 @@ class IpcServer {
|
|
|
24
22
|
|
|
25
23
|
const self = this;
|
|
26
24
|
// 遍历方法
|
|
27
|
-
const files =
|
|
25
|
+
const files = Utils.filePatterns();
|
|
28
26
|
const directory = path.join(this.app.config.baseDir, 'controller');
|
|
29
27
|
const filepaths = globby.sync(files, { cwd: directory });
|
|
30
28
|
|
|
@@ -32,13 +30,13 @@ class IpcServer {
|
|
|
32
30
|
const fullpath = path.join(directory, filepath);
|
|
33
31
|
if (!fs.statSync(fullpath).isFile()) continue;
|
|
34
32
|
|
|
35
|
-
const properties =
|
|
33
|
+
const properties = Wrap.getProperties(filepath, {caseStyle: 'lower'});
|
|
36
34
|
const pathName = directory.split(/[/\\]/).slice(-1) + '.' + properties.join('.');
|
|
37
35
|
|
|
38
|
-
let fileObj =
|
|
36
|
+
let fileObj = Utils.loadFile(fullpath);
|
|
39
37
|
const fns = {};
|
|
40
38
|
// 为了统一,仅支持class文件
|
|
41
|
-
if (is.class(fileObj) ||
|
|
39
|
+
if (is.class(fileObj) || Utils.isBytecodeClass(fileObj)) {
|
|
42
40
|
let proto = fileObj.prototype;
|
|
43
41
|
// 不遍历父类的方法
|
|
44
42
|
//while (proto !== Object.prototype) {
|
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
const assert = require('assert');
|
|
4
4
|
const IoClient = require('socket.io-client');
|
|
5
|
-
const
|
|
5
|
+
const Constants = require('../const');
|
|
6
6
|
const EggConsoleLogger = require('egg-logger').EggConsoleLogger;
|
|
7
|
+
const Storage = require('../storage');
|
|
7
8
|
|
|
8
9
|
class SocketClient {
|
|
9
10
|
constructor (port) {
|
|
@@ -29,7 +30,7 @@ class SocketClient {
|
|
|
29
30
|
call (method = '', ...params) {
|
|
30
31
|
return new Promise((resolve, reject) => {
|
|
31
32
|
// 获取通信频道
|
|
32
|
-
const channel =
|
|
33
|
+
const channel = Constants.socketIo.channel.partySoftware;
|
|
33
34
|
this.client.emit(channel, { cmd: method, params: params }, (response) => {
|
|
34
35
|
resolve(response);
|
|
35
36
|
});
|
|
@@ -37,7 +38,7 @@ class SocketClient {
|
|
|
37
38
|
}
|
|
38
39
|
|
|
39
40
|
getCoreDB () {
|
|
40
|
-
const coreDB =
|
|
41
|
+
const coreDB = Storage.connection('system');
|
|
41
42
|
return coreDB;
|
|
42
43
|
}
|
|
43
44
|
|