ee-core 1.5.2-beta.2 → 2.0.0-beta.2
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 +23 -45
- package/core/lib/loader/mixin/config.js +6 -11
- package/core/lib/loader/mixin/controller.js +3 -2
- package/core/lib/utils/function.js +30 -0
- package/core/lib/utils/index.js +6 -0
- package/index.js +2 -2
- package/lib/appLoader.js +0 -5
- package/lib/application.js +12 -9
- package/lib/baseApp.js +9 -26
- package/lib/eeApp.js +18 -52
- package/{lib/constant.js → module/const/index.js} +3 -0
- package/module/exception/index.js +16 -0
- package/{lib/httpclient.js → module/httpclient/index.js} +45 -11
- package/module/jobs/child/app.js +23 -0
- package/module/jobs/child/forkProcess.js +104 -0
- package/module/jobs/child/index.js +35 -0
- package/module/jobs/index.js +56 -0
- package/module/jobs/renderer/index.js +140 -0
- package/module/jobs/renderer/loadView.js +40 -0
- package/module/loader/index.js +130 -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/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 +16 -13
- 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} +12 -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 +196 -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
- /package/{lib → module}/socket/httpServer.js +0 -0
- /package/{lib → module}/socket/io.js +0 -0
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
1
|
const Agent = require('agentkeepalive');
|
|
4
2
|
const HttpsAgent = require('agentkeepalive').HttpsAgent;
|
|
5
3
|
const urllib = require('urllib');
|
|
6
4
|
const ms = require('humanize-ms');
|
|
7
5
|
const { FrameworkBaseError } = require('egg-errors');
|
|
6
|
+
const Storage = require('../storage');
|
|
7
|
+
const Log = require('../log');
|
|
8
8
|
|
|
9
9
|
class HttpClientError extends FrameworkBaseError {
|
|
10
10
|
get module() {
|
|
@@ -13,18 +13,52 @@ class HttpClientError extends FrameworkBaseError {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
class HttpClient extends urllib.HttpClient2 {
|
|
16
|
-
constructor(
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
constructor(options = {}) {
|
|
17
|
+
|
|
18
|
+
if (Object.keys(options).length == 0) {
|
|
19
|
+
const sysConfig = this._getCoreDB().getItem('config');
|
|
20
|
+
options = sysConfig.httpclient;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const config = Object.assign({
|
|
24
|
+
enableDNSCache: false,
|
|
25
|
+
dnsCacheLookupInterval: 10000,
|
|
26
|
+
dnsCacheMaxLength: 1000,
|
|
27
|
+
request: {
|
|
28
|
+
timeout: 5000,
|
|
29
|
+
},
|
|
30
|
+
httpAgent: {
|
|
31
|
+
keepAlive: true,
|
|
32
|
+
freeSocketTimeout: 4000,
|
|
33
|
+
maxSockets: Number.MAX_SAFE_INTEGER,
|
|
34
|
+
maxFreeSockets: 256,
|
|
35
|
+
},
|
|
36
|
+
httpsAgent: {
|
|
37
|
+
keepAlive: true,
|
|
38
|
+
freeSocketTimeout: 4000,
|
|
39
|
+
maxSockets: Number.MAX_SAFE_INTEGER,
|
|
40
|
+
maxFreeSockets: 256,
|
|
41
|
+
},
|
|
42
|
+
}, options);
|
|
43
|
+
|
|
44
|
+
normalizeConfig(config);
|
|
45
|
+
|
|
19
46
|
super({
|
|
20
|
-
app,
|
|
21
47
|
defaultArgs: config.request,
|
|
22
48
|
agent: new Agent(config.httpAgent),
|
|
23
49
|
httpsAgent: new HttpsAgent(config.httpsAgent),
|
|
24
50
|
});
|
|
25
|
-
this.
|
|
51
|
+
this.config = config;
|
|
26
52
|
}
|
|
27
53
|
|
|
54
|
+
/**
|
|
55
|
+
* 获取 coredb
|
|
56
|
+
*/
|
|
57
|
+
_getCoreDB() {
|
|
58
|
+
const coreDB = Storage.connection('system');
|
|
59
|
+
return coreDB;
|
|
60
|
+
}
|
|
61
|
+
|
|
28
62
|
request(url, args, callback) {
|
|
29
63
|
if (typeof args === 'function') {
|
|
30
64
|
callback = args;
|
|
@@ -74,8 +108,8 @@ class HttpClient extends urllib.HttpClient2 {
|
|
|
74
108
|
}
|
|
75
109
|
}
|
|
76
110
|
|
|
77
|
-
function normalizeConfig(
|
|
78
|
-
const config =
|
|
111
|
+
function normalizeConfig(httpConfig) {
|
|
112
|
+
const config = httpConfig;
|
|
79
113
|
|
|
80
114
|
// compatibility
|
|
81
115
|
if (typeof config.keepAlive === 'boolean') {
|
|
@@ -118,12 +152,12 @@ function normalizeConfig(app) {
|
|
|
118
152
|
}
|
|
119
153
|
|
|
120
154
|
if (config.httpAgent.timeout < 30000) {
|
|
121
|
-
|
|
155
|
+
Log.coreLogger.warn('[ee:httpclient] config.httpclient.httpAgent.timeout(%s) can\'t below 30000, auto reset to 30000',
|
|
122
156
|
config.httpAgent.timeout);
|
|
123
157
|
config.httpAgent.timeout = 30000;
|
|
124
158
|
}
|
|
125
159
|
if (config.httpsAgent.timeout < 30000) {
|
|
126
|
-
|
|
160
|
+
Log.coreLogger.warn('[ee:httpclient] config.httpclient.httpsAgent.timeout(%s) can\'t below 30000, auto reset to 30000',
|
|
127
161
|
config.httpsAgent.timeout);
|
|
128
162
|
config.httpsAgent.timeout = 30000;
|
|
129
163
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const Exception = require('ee-core/module/exception');
|
|
2
|
+
Exception.start();
|
|
3
|
+
|
|
4
|
+
const Loader = require('ee-core/module/loader');
|
|
5
|
+
const Log = require('ee-core/module/log');
|
|
6
|
+
|
|
7
|
+
class ChildApp {
|
|
8
|
+
constructor() {
|
|
9
|
+
const args = process.argv[2];
|
|
10
|
+
this.opt = args ? JSON.parse(args) : {};
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
run () {
|
|
14
|
+
Log.info('[child-process] run');
|
|
15
|
+
|
|
16
|
+
const jobFile = this.opt.jobPath;
|
|
17
|
+
|
|
18
|
+
Loader.loadJobFile(jobFile);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const app = new ChildApp()
|
|
23
|
+
app.run();
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const { fork } = require('child_process');
|
|
3
|
+
|
|
4
|
+
class ForkProcess {
|
|
5
|
+
constructor(host, modulePath, processArgs = [], processOptions = {}) {
|
|
6
|
+
this.host = host;
|
|
7
|
+
this.modulePath = modulePath;
|
|
8
|
+
this.args;
|
|
9
|
+
this.options = processOptions;
|
|
10
|
+
this.sleeping = false;
|
|
11
|
+
this.activitiesCount = 0;
|
|
12
|
+
this.activitiesMap = new Map();
|
|
13
|
+
|
|
14
|
+
// 传递给子进程的参数
|
|
15
|
+
let scriptArgs = {
|
|
16
|
+
jobPath: modulePath
|
|
17
|
+
}
|
|
18
|
+
processArgs.push(JSON.stringify(scriptArgs));
|
|
19
|
+
this.args = processArgs;
|
|
20
|
+
|
|
21
|
+
const appPath = path.join(__dirname, 'app.js');
|
|
22
|
+
this.child = fork(appPath, this.args, this.options);
|
|
23
|
+
|
|
24
|
+
this.pid = this.child.pid;
|
|
25
|
+
this._init();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 进程挂起
|
|
30
|
+
*/
|
|
31
|
+
sleep() {
|
|
32
|
+
if (this.activitiesCount) {
|
|
33
|
+
if (this.sleeping) return;
|
|
34
|
+
process.kill(this.pid, 'SIGSTOP');
|
|
35
|
+
this.sleeping = true;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 进程唤醒
|
|
41
|
+
*/
|
|
42
|
+
wakeup() {
|
|
43
|
+
if (!this.sleeping) return;
|
|
44
|
+
process.kill(this.pid, 'SIGCONT');
|
|
45
|
+
this.sleeping = false;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 进程初始化
|
|
50
|
+
*/
|
|
51
|
+
_init() {
|
|
52
|
+
this.child.on('message', (data) => {
|
|
53
|
+
const id = data.id;
|
|
54
|
+
this.connectionsCountMinus(id);
|
|
55
|
+
delete data.id;
|
|
56
|
+
delete data.action;
|
|
57
|
+
//this.host.emit('forked_message', {data, id});
|
|
58
|
+
});
|
|
59
|
+
this.child.on('exit', (code, signal) => {
|
|
60
|
+
// if (code !== 0 && code !== null) {
|
|
61
|
+
// this.host.emit('forked_error', code, this.pid);
|
|
62
|
+
// } else {
|
|
63
|
+
// this.host.emit('forked_exit', this.pid);
|
|
64
|
+
// }
|
|
65
|
+
});
|
|
66
|
+
this.child.on('error', (err) => {
|
|
67
|
+
console.log('forked error: ', err);
|
|
68
|
+
// this.host.emit('forked_error', err, this.pid);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 向进程发消息
|
|
74
|
+
*/
|
|
75
|
+
send(params) {
|
|
76
|
+
if (this.sleeping) {
|
|
77
|
+
this.wakeup();
|
|
78
|
+
}
|
|
79
|
+
this.connectionsCountPlus(params.id);
|
|
80
|
+
this.child.send(params);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 连接数+
|
|
85
|
+
*/
|
|
86
|
+
_connectionsCountPlus(id) {
|
|
87
|
+
this.activitiesMap.set(id, 1);
|
|
88
|
+
this.activitiesCount += 1;
|
|
89
|
+
this.host.connectionsMap[this.pid] = this.activitiesCount;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* 连接数-
|
|
94
|
+
*/
|
|
95
|
+
_connectionsCountMinus(id) {
|
|
96
|
+
if (this.activitiesMap.has(id)) {
|
|
97
|
+
this.activitiesCount = (this.activitiesCount > 0) ? (this.activitiesCount - 1) : 0;
|
|
98
|
+
this.activitiesMap.delete(id);
|
|
99
|
+
}
|
|
100
|
+
this.host.connectionsMap[this.pid] = this.activitiesCount;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
module.exports = ForkProcess;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
//require('bytenode');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const ForkProcess = require('./forkProcess');
|
|
4
|
+
const Ps = require('../../utils/ps');
|
|
5
|
+
const Constants = require('../../const');
|
|
6
|
+
|
|
7
|
+
class ChildJob {
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* constructor
|
|
11
|
+
* @param {String} name - job name
|
|
12
|
+
* @param {String} filepath - filepath
|
|
13
|
+
* @param {Object} opt - child process options
|
|
14
|
+
*/
|
|
15
|
+
constructor(name, filepath, opt = {}) {
|
|
16
|
+
// todo
|
|
17
|
+
//processArgs: Ps.isDev() ? [`--inspect=${Constants.jobs.inspectStartIndex}`] : [],
|
|
18
|
+
let options = Object.assign({
|
|
19
|
+
processArgs: Ps.isDev() ? [] : [],
|
|
20
|
+
processOptions: {
|
|
21
|
+
//cwd: path.dirname(filepath),
|
|
22
|
+
env: Ps.allEnv(),
|
|
23
|
+
stdio: 'pipe'
|
|
24
|
+
}
|
|
25
|
+
}, opt);
|
|
26
|
+
|
|
27
|
+
this.childProcess = new ForkProcess(this, filepath, options.processArgs, options.processOptions);
|
|
28
|
+
|
|
29
|
+
this.jobReady = false;
|
|
30
|
+
this.exec = filepath;
|
|
31
|
+
this.name = name;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
module.exports = ChildJob;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const RendererJob = require('./renderer');
|
|
4
|
+
const ChildJob = require('./child');
|
|
5
|
+
const Utils = require('../utils');
|
|
6
|
+
const Loader = require('../loader');
|
|
7
|
+
|
|
8
|
+
class Jobs {
|
|
9
|
+
constructor() {
|
|
10
|
+
this.type = undefined;
|
|
11
|
+
this.instance = undefined;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 创建 job
|
|
16
|
+
*/
|
|
17
|
+
create (name, opt = {}) {
|
|
18
|
+
this.type = opt.type || 'child';
|
|
19
|
+
this.dev = opt.dev || false;
|
|
20
|
+
this.winOptions = opt.winOptions || {};
|
|
21
|
+
this.childOptions = opt.childOptions || {};
|
|
22
|
+
this.path = opt.path || null;
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
const isAbsolute = path.isAbsolute(this.path);
|
|
26
|
+
if (!isAbsolute) {
|
|
27
|
+
this.path = path.join(Utils.getBaseDir(), this.path);
|
|
28
|
+
}
|
|
29
|
+
const filepath = Loader.resolveModule(this.path);
|
|
30
|
+
|
|
31
|
+
if (!fs.existsSync(filepath)) {
|
|
32
|
+
throw new Error(`[ee-core] [jobs-create] file ${this.path} not exists`);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
this.path = filepath;
|
|
36
|
+
if (this.type == 'child') {
|
|
37
|
+
this.instance = new ChildJob(name, filepath, this.childOptions);
|
|
38
|
+
} else if (this.type == 'renderer') {
|
|
39
|
+
this.instance = new RendererJob(name, filepath, this.winOptions);
|
|
40
|
+
if (this.dev) {
|
|
41
|
+
this.openDevTools();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 显示开发者工具栏(仅支持 RendererJob)
|
|
50
|
+
*/
|
|
51
|
+
openDevTools () {
|
|
52
|
+
this.instance.openDevTools();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
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,130 @@
|
|
|
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
|
+
const Log = require('../log');
|
|
7
|
+
|
|
8
|
+
module.exports = {
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 加载单个文件(如果是函数,将被执行)
|
|
12
|
+
*
|
|
13
|
+
* @param {String} filepath - fullpath
|
|
14
|
+
* @param {Array} inject - pass rest arguments into the function when invoke
|
|
15
|
+
* @return {Object} exports
|
|
16
|
+
* @since 1.0.0
|
|
17
|
+
*/
|
|
18
|
+
loadOneFile (filepath, ...inject) {
|
|
19
|
+
const isAbsolute = path.isAbsolute(filepath);
|
|
20
|
+
if (!isAbsolute) {
|
|
21
|
+
filepath = path.join(Ps.getBaseDir(), filepath);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
filepath = filepath && this.resolveModule(filepath);
|
|
25
|
+
if (!fs.existsSync(filepath)) {
|
|
26
|
+
let errorMsg = `[ee-core] [module/loader/index] loadOneFile ${filepath} does not exist`;
|
|
27
|
+
Log.coreLogger.error(errorMsg);
|
|
28
|
+
throw new Error(errorMsg);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const ret = UtilsCore.loadFile(filepath);
|
|
32
|
+
if (is.function(ret) && !is.class(ret) && !UtilsCore.isBytecodeClass(ret)) {
|
|
33
|
+
ret = ret(...inject);
|
|
34
|
+
}
|
|
35
|
+
return ret;
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 加载job文件
|
|
40
|
+
*
|
|
41
|
+
* @param {String} filepath - fullpath
|
|
42
|
+
* @param {Array} inject - pass rest arguments into the function when invoke
|
|
43
|
+
* @return {Object} exports
|
|
44
|
+
* @since 1.0.0
|
|
45
|
+
*/
|
|
46
|
+
loadJobFile (filepath, ...inject) {
|
|
47
|
+
if (!fs.existsSync(filepath)) {
|
|
48
|
+
let warnMsg = `[ee-core] [module/loader/index] loadJobFile ${filepath} does not exist`;
|
|
49
|
+
Log.coreLogger.error(warnMsg);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const ret = UtilsCore.loadFile(filepath);
|
|
53
|
+
if (is.function(ret) && !is.class(ret) && !UtilsCore.isBytecodeClass(ret)) {
|
|
54
|
+
ret = ret(...inject);
|
|
55
|
+
}
|
|
56
|
+
return ret;
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 模块的绝对路径
|
|
61
|
+
* @param {String} filepath - fullpath
|
|
62
|
+
*/
|
|
63
|
+
resolveModule(filepath) {
|
|
64
|
+
let fullpath;
|
|
65
|
+
try {
|
|
66
|
+
fullpath = require.resolve(filepath);
|
|
67
|
+
} catch (e) {
|
|
68
|
+
|
|
69
|
+
// 特殊后缀处理
|
|
70
|
+
if (filepath && (filepath.endsWith('.defalut') || filepath.endsWith('.prod'))) {
|
|
71
|
+
fullpath = filepath + '.jsc';
|
|
72
|
+
} else if (filepath && filepath.endsWith('.js')) {
|
|
73
|
+
fullpath = filepath + 'c';
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (!fs.existsSync(filepath) && !fs.existsSync(fullpath)) {
|
|
77
|
+
let files = { filepath, fullpath }
|
|
78
|
+
Log.coreLogger.warn(`[ee-core] [module-loader-resolveModule] unknow filepath: ${files}`)
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return fullpath;
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 加载模块(子进程中使用)
|
|
88
|
+
*
|
|
89
|
+
* @param {String} filepath - fullpath
|
|
90
|
+
* @return {Object} exports
|
|
91
|
+
* @since 1.0.0
|
|
92
|
+
*/
|
|
93
|
+
requireModule (filepath, type = '') {
|
|
94
|
+
let fullpath;
|
|
95
|
+
const isAbsolute = path.isAbsolute(filepath);
|
|
96
|
+
if (!isAbsolute) {
|
|
97
|
+
filepath = path.join(Ps.getBaseDir(), type, filepath);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
fullpath = this.resolveModule(filepath);
|
|
101
|
+
if (!fs.existsSync(fullpath)) {
|
|
102
|
+
let errorMsg = `[ee-core] [module-loader-index] requireModule filepath: ${filepath} does not exist`;
|
|
103
|
+
Log.coreLogger.error(errorMsg);
|
|
104
|
+
}
|
|
105
|
+
const ret = UtilsCore.loadFile(fullpath);
|
|
106
|
+
|
|
107
|
+
return ret;
|
|
108
|
+
},
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* 加载jobs模块(子进程中使用)
|
|
112
|
+
*
|
|
113
|
+
* @param {String} filepath - fullpath
|
|
114
|
+
* @return {Object} exports
|
|
115
|
+
* @since 1.0.0
|
|
116
|
+
*/
|
|
117
|
+
requireJobsModule (filepath) {
|
|
118
|
+
const ret = this.requireModule(filepath, 'jobs');
|
|
119
|
+
|
|
120
|
+
return ret;
|
|
121
|
+
},
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
|
|
@@ -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;
|