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.
Files changed (72) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +2 -2
  3. package/addon/window/index.js +91 -91
  4. package/bin/tools.js +18 -18
  5. package/config/config.default.js +287 -280
  6. package/core/index.js +12 -12
  7. package/core/lib/ee.js +218 -218
  8. package/core/lib/loader/context_loader.js +106 -106
  9. package/core/lib/loader/ee_loader.js +461 -457
  10. package/core/lib/loader/file_loader.js +325 -325
  11. package/core/lib/loader/mixin/addon.js +32 -32
  12. package/core/lib/loader/mixin/config.js +135 -135
  13. package/core/lib/loader/mixin/controller.js +125 -124
  14. package/core/lib/loader/mixin/service.js +28 -28
  15. package/core/lib/utils/base_context_class.js +34 -34
  16. package/core/lib/utils/function.js +30 -0
  17. package/core/lib/utils/index.js +127 -127
  18. package/core/lib/utils/sequencify.js +59 -59
  19. package/core/lib/utils/timing.js +77 -77
  20. package/index.js +49 -49
  21. package/lib/appLoader.js +48 -53
  22. package/lib/application.js +85 -84
  23. package/lib/baseApp.js +114 -131
  24. package/lib/eeApp.js +325 -359
  25. package/{lib/constant.js → module/const/index.js} +12 -9
  26. package/{lib/httpclient.js → module/httpclient/index.js} +170 -136
  27. package/module/jobs/child/forkProcess.js +99 -0
  28. package/module/jobs/child/index.js +33 -0
  29. package/module/jobs/index.js +55 -0
  30. package/module/jobs/renderer/index.js +140 -0
  31. package/module/jobs/renderer/loadView.js +40 -0
  32. package/module/loader/index.js +78 -0
  33. package/module/log/index.js +53 -0
  34. package/module/log/logger.js +61 -0
  35. package/module/message/index.js +13 -0
  36. package/module/message/ipcMain.js +160 -0
  37. package/module/message/ipcRender.js +0 -0
  38. package/{lib → module}/socket/httpServer.js +142 -142
  39. package/{lib → module}/socket/io.js +23 -23
  40. package/{lib → module}/socket/ipcServer.js +106 -108
  41. package/{lib → module}/socket/socketClient.js +51 -50
  42. package/{lib → module}/socket/socketServer.js +77 -76
  43. package/module/socket/start.js +22 -0
  44. package/{lib → module}/storage/index.js +35 -34
  45. package/module/storage/jsondb/adapters/Base.js +14 -0
  46. package/module/storage/jsondb/adapters/FileSync.js +32 -0
  47. package/{lib/storage/lowdb → module/storage/jsondb}/main.js +42 -46
  48. package/{lib/storage/lowdbStorage.js → module/storage/jsondbStorage.js} +98 -99
  49. package/{lib → module}/storage/sqliteStorage.js +123 -127
  50. package/module/utils/copyto.js +161 -0
  51. package/module/utils/helper.js +117 -0
  52. package/module/utils/index.js +120 -0
  53. package/module/utils/json.js +72 -0
  54. package/module/utils/ps.js +175 -0
  55. package/{utils → module/utils}/wrap.js +35 -37
  56. package/package.json +44 -48
  57. package/tools/encrypt.js +274 -274
  58. package/tools/replaceDist.js +61 -61
  59. package/utils/index.js +128 -246
  60. package/lib/logger.js +0 -47
  61. package/lib/socket/start.js +0 -22
  62. package/lib/storage/lowdb/adapters/Base.js +0 -15
  63. package/lib/storage/lowdb/adapters/FileAsync.js +0 -41
  64. package/lib/storage/lowdb/adapters/FileSync.js +0 -39
  65. package/lib/storage/lowdb/adapters/LocalStorage.js +0 -20
  66. package/lib/storage/lowdb/adapters/Memory.js +0 -8
  67. package/lib/storage/lowdb/adapters/_stringify.js +0 -4
  68. package/lib/storage/lowdb/common.js +0 -33
  69. package/lib/storage/lowdb/fp.js +0 -23
  70. package/lib/storage/lowdb/is-promise.js +0 -6
  71. package/lib/storage/lowdb/nano.js +0 -5
  72. 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
- 'use strict';
2
-
3
- const Agent = require('agentkeepalive');
4
- const HttpsAgent = require('agentkeepalive').HttpsAgent;
5
- const urllib = require('urllib');
6
- const ms = require('humanize-ms');
7
- const { FrameworkBaseError } = require('egg-errors');
8
-
9
- class HttpClientError extends FrameworkBaseError {
10
- get module() {
11
- return 'httpclient';
12
- }
13
- }
14
-
15
- class HttpClient extends urllib.HttpClient2 {
16
- constructor(app) {
17
- normalizeConfig(app);
18
- const config = app.config.httpclient;
19
- super({
20
- app,
21
- defaultArgs: config.request,
22
- agent: new Agent(config.httpAgent),
23
- httpsAgent: new HttpsAgent(config.httpsAgent),
24
- });
25
- this.app = app;
26
- }
27
-
28
- request(url, args, callback) {
29
- if (typeof args === 'function') {
30
- callback = args;
31
- args = null;
32
- }
33
-
34
- args = args || {};
35
-
36
- // the callback style
37
- if (callback) {
38
- //this.app.deprecate('[httpclient] We now support async for this function, so callback isn\'t recommended.');
39
- super.request(url, args)
40
- .then(result => process.nextTick(() => callback(null, result.data, result.res)))
41
- .catch(err => process.nextTick(() => callback(err)));
42
- return;
43
- }
44
-
45
- // the Promise style
46
- return super.request(url, args)
47
- .catch(err => {
48
- if (err.code === 'ENETUNREACH') {
49
- throw HttpClientError.create(err.message, err.code);
50
- }
51
- throw err;
52
- });
53
- }
54
-
55
- curl(url, args, callback) {
56
- return this.request(url, args, callback);
57
- }
58
-
59
- requestThunk(url, args) {
60
- //this.app.deprecate('[httpclient] Please use `request()` instead of `requestThunk()`');
61
- return callback => {
62
- this.request(url, args, (err, data, res) => {
63
- if (err) {
64
- return callback(err);
65
- }
66
- callback(null, {
67
- data,
68
- status: res.status,
69
- headers: res.headers,
70
- res,
71
- });
72
- });
73
- };
74
- }
75
- }
76
-
77
- function normalizeConfig(app) {
78
- const config = app.config.httpclient;
79
-
80
- // compatibility
81
- if (typeof config.keepAlive === 'boolean') {
82
- config.httpAgent.keepAlive = config.keepAlive;
83
- config.httpsAgent.keepAlive = config.keepAlive;
84
- }
85
- if (config.timeout) {
86
- config.timeout = ms(config.timeout);
87
- config.httpAgent.timeout = config.timeout;
88
- config.httpsAgent.timeout = config.timeout;
89
- }
90
- // compatibility httpclient.freeSocketKeepAliveTimeout => httpclient.freeSocketTimeout
91
- if (config.freeSocketKeepAliveTimeout && !config.freeSocketTimeout) {
92
- config.freeSocketTimeout = config.freeSocketKeepAliveTimeout;
93
- delete config.freeSocketKeepAliveTimeout;
94
- }
95
- if (config.freeSocketTimeout) {
96
- config.freeSocketTimeout = ms(config.freeSocketTimeout);
97
- config.httpAgent.freeSocketTimeout = config.freeSocketTimeout;
98
- config.httpsAgent.freeSocketTimeout = config.freeSocketTimeout;
99
- } else {
100
- // compatibility agent.freeSocketKeepAliveTimeout
101
- if (config.httpAgent.freeSocketKeepAliveTimeout && !config.httpAgent.freeSocketTimeout) {
102
- config.httpAgent.freeSocketTimeout = config.httpAgent.freeSocketKeepAliveTimeout;
103
- delete config.httpAgent.freeSocketKeepAliveTimeout;
104
- }
105
- if (config.httpsAgent.freeSocketKeepAliveTimeout && !config.httpsAgent.freeSocketTimeout) {
106
- config.httpsAgent.freeSocketTimeout = config.httpsAgent.freeSocketKeepAliveTimeout;
107
- delete config.httpsAgent.freeSocketKeepAliveTimeout;
108
- }
109
- }
110
-
111
- if (typeof config.maxSockets === 'number') {
112
- config.httpAgent.maxSockets = config.maxSockets;
113
- config.httpsAgent.maxSockets = config.maxSockets;
114
- }
115
- if (typeof config.maxFreeSockets === 'number') {
116
- config.httpAgent.maxFreeSockets = config.maxFreeSockets;
117
- config.httpsAgent.maxFreeSockets = config.maxFreeSockets;
118
- }
119
-
120
- if (config.httpAgent.timeout < 30000) {
121
- app.coreLogger.warn('[ee:httpclient] config.httpclient.httpAgent.timeout(%s) can\'t below 30000, auto reset to 30000',
122
- config.httpAgent.timeout);
123
- config.httpAgent.timeout = 30000;
124
- }
125
- if (config.httpsAgent.timeout < 30000) {
126
- app.coreLogger.warn('[ee:httpclient] config.httpclient.httpsAgent.timeout(%s) can\'t below 30000, auto reset to 30000',
127
- config.httpsAgent.timeout);
128
- config.httpsAgent.timeout = 30000;
129
- }
130
-
131
- if (typeof config.request.timeout === 'string') {
132
- config.request.timeout = ms(config.request.timeout);
133
- }
134
- }
135
-
136
- module.exports = HttpClient;
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;