ee-core 1.5.2-beta.1 → 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/LICENSE +21 -21
- package/README.md +2 -2
- package/addon/window/index.js +91 -91
- package/bin/tools.js +18 -18
- package/config/config.default.js +287 -280
- package/core/index.js +12 -12
- package/core/lib/ee.js +218 -218
- package/core/lib/loader/context_loader.js +106 -106
- package/core/lib/loader/ee_loader.js +461 -457
- package/core/lib/loader/file_loader.js +325 -325
- package/core/lib/loader/mixin/addon.js +32 -32
- package/core/lib/loader/mixin/config.js +135 -135
- package/core/lib/loader/mixin/controller.js +125 -124
- package/core/lib/loader/mixin/service.js +28 -28
- package/core/lib/utils/base_context_class.js +34 -34
- package/core/lib/utils/function.js +30 -0
- package/core/lib/utils/index.js +127 -127
- package/core/lib/utils/sequencify.js +59 -59
- package/core/lib/utils/timing.js +77 -77
- package/index.js +49 -49
- package/lib/appLoader.js +48 -53
- package/lib/application.js +85 -84
- package/lib/baseApp.js +114 -131
- package/lib/eeApp.js +325 -359
- package/{lib/constant.js → module/const/index.js} +12 -9
- package/{lib/httpclient.js → module/httpclient/index.js} +170 -136
- 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 +142 -142
- package/{lib → module}/socket/io.js +23 -23
- package/{lib → module}/socket/ipcServer.js +106 -108
- package/{lib → module}/socket/socketClient.js +51 -50
- package/{lib → module}/socket/socketServer.js +77 -76
- package/module/socket/start.js +22 -0
- package/{lib → module}/storage/index.js +35 -34
- 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 +42 -46
- package/{lib/storage/lowdbStorage.js → module/storage/jsondbStorage.js} +98 -99
- package/{lib → module}/storage/sqliteStorage.js +123 -127
- 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 +35 -37
- package/package.json +44 -48
- package/tools/encrypt.js +274 -274
- package/tools/replaceDist.js +61 -61
- package/utils/index.js +128 -246
- 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/is-promise.js +0 -6
- package/lib/storage/lowdb/nano.js +0 -5
- package/utils/common.js +0 -91
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
storageKey: {
|
|
3
|
-
cache: 'cache',
|
|
4
|
-
},
|
|
5
|
-
socketIo: {
|
|
6
|
-
channel: {
|
|
7
|
-
partySoftware: 'c1',
|
|
8
|
-
}
|
|
9
|
-
}
|
|
1
|
+
module.exports = {
|
|
2
|
+
storageKey: {
|
|
3
|
+
cache: 'cache',
|
|
4
|
+
},
|
|
5
|
+
socketIo: {
|
|
6
|
+
channel: {
|
|
7
|
+
partySoftware: 'c1',
|
|
8
|
+
}
|
|
9
|
+
},
|
|
10
|
+
jobs: {
|
|
11
|
+
inspectStartIndex: 5858
|
|
12
|
+
}
|
|
10
13
|
};
|
|
@@ -1,136 +1,170 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
class HttpClientError extends FrameworkBaseError {
|
|
10
|
-
get module() {
|
|
11
|
-
return 'httpclient';
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
class HttpClient extends urllib.HttpClient2 {
|
|
16
|
-
constructor(
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
.
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
if (typeof config.
|
|
116
|
-
config.httpAgent.
|
|
117
|
-
config.httpsAgent.
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
if (config.
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
config.
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
1
|
+
const Agent = require('agentkeepalive');
|
|
2
|
+
const HttpsAgent = require('agentkeepalive').HttpsAgent;
|
|
3
|
+
const urllib = require('urllib');
|
|
4
|
+
const ms = require('humanize-ms');
|
|
5
|
+
const { FrameworkBaseError } = require('egg-errors');
|
|
6
|
+
const Storage = require('../storage');
|
|
7
|
+
const Log = require('../log');
|
|
8
|
+
|
|
9
|
+
class HttpClientError extends FrameworkBaseError {
|
|
10
|
+
get module() {
|
|
11
|
+
return 'httpclient';
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
class HttpClient extends urllib.HttpClient2 {
|
|
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
|
+
|
|
46
|
+
super({
|
|
47
|
+
defaultArgs: config.request,
|
|
48
|
+
agent: new Agent(config.httpAgent),
|
|
49
|
+
httpsAgent: new HttpsAgent(config.httpsAgent),
|
|
50
|
+
});
|
|
51
|
+
this.config = config;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 获取 coredb
|
|
56
|
+
*/
|
|
57
|
+
_getCoreDB() {
|
|
58
|
+
const coreDB = Storage.connection('system');
|
|
59
|
+
return coreDB;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
request(url, args, callback) {
|
|
63
|
+
if (typeof args === 'function') {
|
|
64
|
+
callback = args;
|
|
65
|
+
args = null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
args = args || {};
|
|
69
|
+
|
|
70
|
+
// the callback style
|
|
71
|
+
if (callback) {
|
|
72
|
+
//this.app.deprecate('[httpclient] We now support async for this function, so callback isn\'t recommended.');
|
|
73
|
+
super.request(url, args)
|
|
74
|
+
.then(result => process.nextTick(() => callback(null, result.data, result.res)))
|
|
75
|
+
.catch(err => process.nextTick(() => callback(err)));
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// the Promise style
|
|
80
|
+
return super.request(url, args)
|
|
81
|
+
.catch(err => {
|
|
82
|
+
if (err.code === 'ENETUNREACH') {
|
|
83
|
+
throw HttpClientError.create(err.message, err.code);
|
|
84
|
+
}
|
|
85
|
+
throw err;
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
curl(url, args, callback) {
|
|
90
|
+
return this.request(url, args, callback);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
requestThunk(url, args) {
|
|
94
|
+
//this.app.deprecate('[httpclient] Please use `request()` instead of `requestThunk()`');
|
|
95
|
+
return callback => {
|
|
96
|
+
this.request(url, args, (err, data, res) => {
|
|
97
|
+
if (err) {
|
|
98
|
+
return callback(err);
|
|
99
|
+
}
|
|
100
|
+
callback(null, {
|
|
101
|
+
data,
|
|
102
|
+
status: res.status,
|
|
103
|
+
headers: res.headers,
|
|
104
|
+
res,
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function normalizeConfig(httpConfig) {
|
|
112
|
+
const config = httpConfig;
|
|
113
|
+
|
|
114
|
+
// compatibility
|
|
115
|
+
if (typeof config.keepAlive === 'boolean') {
|
|
116
|
+
config.httpAgent.keepAlive = config.keepAlive;
|
|
117
|
+
config.httpsAgent.keepAlive = config.keepAlive;
|
|
118
|
+
}
|
|
119
|
+
if (config.timeout) {
|
|
120
|
+
config.timeout = ms(config.timeout);
|
|
121
|
+
config.httpAgent.timeout = config.timeout;
|
|
122
|
+
config.httpsAgent.timeout = config.timeout;
|
|
123
|
+
}
|
|
124
|
+
// compatibility httpclient.freeSocketKeepAliveTimeout => httpclient.freeSocketTimeout
|
|
125
|
+
if (config.freeSocketKeepAliveTimeout && !config.freeSocketTimeout) {
|
|
126
|
+
config.freeSocketTimeout = config.freeSocketKeepAliveTimeout;
|
|
127
|
+
delete config.freeSocketKeepAliveTimeout;
|
|
128
|
+
}
|
|
129
|
+
if (config.freeSocketTimeout) {
|
|
130
|
+
config.freeSocketTimeout = ms(config.freeSocketTimeout);
|
|
131
|
+
config.httpAgent.freeSocketTimeout = config.freeSocketTimeout;
|
|
132
|
+
config.httpsAgent.freeSocketTimeout = config.freeSocketTimeout;
|
|
133
|
+
} else {
|
|
134
|
+
// compatibility agent.freeSocketKeepAliveTimeout
|
|
135
|
+
if (config.httpAgent.freeSocketKeepAliveTimeout && !config.httpAgent.freeSocketTimeout) {
|
|
136
|
+
config.httpAgent.freeSocketTimeout = config.httpAgent.freeSocketKeepAliveTimeout;
|
|
137
|
+
delete config.httpAgent.freeSocketKeepAliveTimeout;
|
|
138
|
+
}
|
|
139
|
+
if (config.httpsAgent.freeSocketKeepAliveTimeout && !config.httpsAgent.freeSocketTimeout) {
|
|
140
|
+
config.httpsAgent.freeSocketTimeout = config.httpsAgent.freeSocketKeepAliveTimeout;
|
|
141
|
+
delete config.httpsAgent.freeSocketKeepAliveTimeout;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (typeof config.maxSockets === 'number') {
|
|
146
|
+
config.httpAgent.maxSockets = config.maxSockets;
|
|
147
|
+
config.httpsAgent.maxSockets = config.maxSockets;
|
|
148
|
+
}
|
|
149
|
+
if (typeof config.maxFreeSockets === 'number') {
|
|
150
|
+
config.httpAgent.maxFreeSockets = config.maxFreeSockets;
|
|
151
|
+
config.httpsAgent.maxFreeSockets = config.maxFreeSockets;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (config.httpAgent.timeout < 30000) {
|
|
155
|
+
Log.coreLogger.warn('[ee:httpclient] config.httpclient.httpAgent.timeout(%s) can\'t below 30000, auto reset to 30000',
|
|
156
|
+
config.httpAgent.timeout);
|
|
157
|
+
config.httpAgent.timeout = 30000;
|
|
158
|
+
}
|
|
159
|
+
if (config.httpsAgent.timeout < 30000) {
|
|
160
|
+
Log.coreLogger.warn('[ee:httpclient] config.httpclient.httpsAgent.timeout(%s) can\'t below 30000, auto reset to 30000',
|
|
161
|
+
config.httpsAgent.timeout);
|
|
162
|
+
config.httpsAgent.timeout = 30000;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (typeof config.request.timeout === 'string') {
|
|
166
|
+
config.request.timeout = ms(config.request.timeout);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
module.exports = HttpClient;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
const { fork } = require('child_process');
|
|
2
|
+
|
|
3
|
+
class ForkProcess {
|
|
4
|
+
constructor(host, modulePath, processArgs = [], processOptions = {}) {
|
|
5
|
+
this.host = host;
|
|
6
|
+
this.modulePath = modulePath;
|
|
7
|
+
this.args = processArgs;
|
|
8
|
+
this.options = processOptions;
|
|
9
|
+
this.sleeping = false;
|
|
10
|
+
this.activitiesCount = 0;
|
|
11
|
+
this.activitiesMap = new Map();
|
|
12
|
+
|
|
13
|
+
this.child = fork(
|
|
14
|
+
this.modulePath,
|
|
15
|
+
this.args,
|
|
16
|
+
this.options
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
this.pid = this.child.pid;
|
|
20
|
+
this._init();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 进程挂起
|
|
25
|
+
*/
|
|
26
|
+
sleep() {
|
|
27
|
+
if (this.activitiesCount) {
|
|
28
|
+
if (this.sleeping) return;
|
|
29
|
+
process.kill(this.pid, 'SIGSTOP');
|
|
30
|
+
this.sleeping = true;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 进程唤醒
|
|
36
|
+
*/
|
|
37
|
+
wakeup() {
|
|
38
|
+
if (!this.sleeping) return;
|
|
39
|
+
process.kill(this.pid, 'SIGCONT');
|
|
40
|
+
this.sleeping = false;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 进程初始化
|
|
45
|
+
*/
|
|
46
|
+
_init() {
|
|
47
|
+
this.child.on('message', (data) => {
|
|
48
|
+
const id = data.id;
|
|
49
|
+
this.connectionsCountMinus(id);
|
|
50
|
+
delete data.id;
|
|
51
|
+
delete data.action;
|
|
52
|
+
//this.host.emit('forked_message', {data, id});
|
|
53
|
+
});
|
|
54
|
+
this.child.on('exit', (code, signal) => {
|
|
55
|
+
// if (code !== 0 && code !== null) {
|
|
56
|
+
// this.host.emit('forked_error', code, this.pid);
|
|
57
|
+
// } else {
|
|
58
|
+
// this.host.emit('forked_exit', this.pid);
|
|
59
|
+
// }
|
|
60
|
+
});
|
|
61
|
+
this.child.on('error', (err) => {
|
|
62
|
+
console.log('forked error: ', err);
|
|
63
|
+
// this.host.emit('forked_error', err, this.pid);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 向进程发消息
|
|
69
|
+
*/
|
|
70
|
+
send(params) {
|
|
71
|
+
if (this.sleeping) {
|
|
72
|
+
this.wakeup();
|
|
73
|
+
}
|
|
74
|
+
this.connectionsCountPlus(params.id);
|
|
75
|
+
this.child.send(params);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 连接数+
|
|
80
|
+
*/
|
|
81
|
+
_connectionsCountPlus(id) {
|
|
82
|
+
this.activitiesMap.set(id, 1);
|
|
83
|
+
this.activitiesCount += 1;
|
|
84
|
+
this.host.connectionsMap[this.pid] = this.activitiesCount;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* 连接数-
|
|
89
|
+
*/
|
|
90
|
+
_connectionsCountMinus(id) {
|
|
91
|
+
if (this.activitiesMap.has(id)) {
|
|
92
|
+
this.activitiesCount = (this.activitiesCount > 0) ? (this.activitiesCount - 1) : 0;
|
|
93
|
+
this.activitiesMap.delete(id);
|
|
94
|
+
}
|
|
95
|
+
this.host.connectionsMap[this.pid] = this.activitiesCount;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
module.exports = ForkProcess;
|
|
@@ -0,0 +1,33 @@
|
|
|
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
|
+
let options = Object.assign({
|
|
17
|
+
processArgs: Ps.isDev() ? [`--inspect=${Constants.jobs.inspectStartIndex}`] : [],
|
|
18
|
+
processOptions: {
|
|
19
|
+
//cwd: path.dirname(filepath),
|
|
20
|
+
env: Ps.allEnv(),
|
|
21
|
+
stdio: 'pipe'
|
|
22
|
+
}
|
|
23
|
+
}, opt);
|
|
24
|
+
|
|
25
|
+
this.childProcess = new ForkProcess(this, filepath, options.processArgs, options.processOptions);
|
|
26
|
+
|
|
27
|
+
this.jobReady = false;
|
|
28
|
+
this.exec = filepath;
|
|
29
|
+
this.name = name;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
module.exports = ChildJob;
|
|
@@ -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;
|