ee-core 2.12.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app/application.d.ts +5 -0
- package/app/application.js +31 -0
- package/app/boot.d.ts +5 -0
- package/app/boot.js +74 -0
- package/app/dir.d.ts +1 -0
- package/app/dir.js +27 -0
- package/app/events.d.ts +14 -0
- package/app/events.js +56 -0
- package/app/index.d.ts +2 -0
- package/app/index.js +7 -0
- package/config/config_loader.d.ts +13 -0
- package/config/config_loader.js +19 -23
- package/config/default_config.d.ts +95 -0
- package/config/default_config.js +111 -0
- package/config/index.d.ts +28 -0
- package/config/index.js +20 -37
- package/const/channel.d.ts +16 -0
- package/const/channel.js +26 -16
- package/controller/controller_loader.d.ts +9 -0
- package/controller/controller_loader.js +78 -0
- package/controller/index.d.ts +2 -0
- package/controller/index.js +18 -19
- package/core/index.d.ts +2 -0
- package/core/index.js +6 -5
- package/core/loader/file_loader.d.ts +66 -0
- package/core/{lib/loader → loader}/file_loader.js +39 -156
- package/core/utils/index.d.ts +12 -0
- package/core/utils/index.js +83 -0
- package/core/utils/timing.d.ts +22 -0
- package/core/{lib/utils → utils}/timing.js +3 -2
- package/cross/cross.d.ts +19 -0
- package/cross/cross.js +152 -0
- package/cross/crossProcess.d.ts +29 -0
- package/cross/{spawnProcess.js → crossProcess.js} +36 -53
- package/cross/index.d.ts +2 -0
- package/cross/index.js +6 -181
- package/electron/app/index.d.ts +5 -0
- package/electron/app/index.js +38 -54
- package/electron/index.d.ts +3 -0
- package/electron/index.js +13 -17
- package/electron/window/index.d.ts +6 -0
- package/electron/window/index.js +260 -65
- package/exception/index.d.ts +9 -0
- package/exception/index.js +42 -54
- package/html/index.d.ts +1 -0
- package/html/index.js +7 -10
- package/index.d.ts +2 -0
- package/index.js +4 -52
- package/jobs/child/app.d.ts +1 -0
- package/jobs/child/app.js +10 -10
- package/jobs/child/index.d.ts +13 -0
- package/jobs/child/index.js +14 -11
- package/jobs/child/jobProcess.d.ts +16 -0
- package/jobs/child/{forkProcess.js → jobProcess.js} +31 -62
- package/jobs/child-pool/index.d.ts +26 -0
- package/jobs/child-pool/index.js +25 -48
- package/jobs/index.d.ts +3 -0
- package/jobs/index.js +2 -4
- package/jobs/load-balancer/algorithm/index.d.ts +4 -0
- package/jobs/load-balancer/algorithm/minimumConnection.d.ts +2 -0
- package/jobs/load-balancer/algorithm/polling.d.ts +2 -0
- package/jobs/load-balancer/algorithm/random.d.ts +2 -0
- package/jobs/load-balancer/algorithm/specify.d.ts +2 -0
- package/jobs/load-balancer/algorithm/weights.d.ts +2 -0
- package/jobs/load-balancer/algorithm/weightsMinimumConnection.d.ts +2 -0
- package/jobs/load-balancer/algorithm/weightsPolling.d.ts +2 -0
- package/jobs/load-balancer/algorithm/weightsRandom.d.ts +2 -0
- package/jobs/load-balancer/consts.d.ts +8 -0
- package/jobs/load-balancer/index.d.ts +96 -0
- package/jobs/load-balancer/scheduler.d.ts +16 -0
- package/loader/index.d.ts +9 -0
- package/loader/index.js +73 -143
- package/log/index.d.ts +5 -0
- package/log/index.js +74 -57
- package/log/logger.d.ts +2 -0
- package/log/logger.js +75 -80
- package/message/childMessage.d.ts +10 -0
- package/message/childMessage.js +15 -25
- package/message/index.d.ts +3 -0
- package/message/index.js +7 -16
- package/package.json +8 -2
- package/ps/index.d.ts +37 -0
- package/ps/index.js +135 -238
- package/socket/httpServer.d.ts +8 -0
- package/socket/httpServer.js +55 -45
- package/socket/index.d.ts +10 -0
- package/socket/index.js +52 -69
- package/socket/ipcServer.d.ts +9 -0
- package/socket/ipcServer.js +87 -94
- package/socket/socketServer.d.ts +10 -0
- package/socket/socketServer.js +37 -25
- package/storage/index.d.ts +5 -0
- package/storage/index.js +5 -37
- package/storage/sqliteStorage.d.ts +16 -0
- package/storage/sqliteStorage.js +20 -20
- package/tsconfig.json +14 -0
- package/utils/extend.d.ts +2 -0
- package/utils/extend.js +10 -5
- package/utils/helper.d.ts +14 -0
- package/utils/helper.js +39 -53
- package/utils/index.d.ts +10 -0
- package/utils/index.js +47 -40
- package/utils/ip.d.ts +2 -0
- package/utils/ip.js +5 -45
- package/utils/is.d.ts +14 -0
- package/utils/is.js +107 -141
- package/utils/json.d.ts +5 -0
- package/utils/json.js +15 -7
- package/utils/pargv.d.ts +3 -0
- package/utils/pargv.js +5 -1
- package/utils/port/index.d.ts +1 -0
- package/utils/{get-port → port}/index.js +4 -26
- package/utils/wrap.d.ts +4 -0
- package/utils/wrap.js +8 -3
- package/addon/index.js +0 -35
- package/addon/window/index.js +0 -99
- package/bin/tools.js +0 -8
- package/config/cache.js +0 -62
- package/config/config.default.js +0 -331
- package/const/index.js +0 -9
- package/controller/baseContextClass.js +0 -25
- package/core/lib/ee.js +0 -216
- package/core/lib/loader/context_loader.js +0 -106
- package/core/lib/loader/ee_loader.js +0 -435
- package/core/lib/loader/mixin/addon.js +0 -32
- package/core/lib/loader/mixin/config.js +0 -130
- package/core/lib/loader/mixin/controller.js +0 -125
- package/core/lib/loader/mixin/service.js +0 -28
- package/core/lib/utils/base_context_class.js +0 -34
- package/core/lib/utils/function.js +0 -30
- package/core/lib/utils/index.js +0 -133
- package/core/lib/utils/sequencify.js +0 -59
- package/ee/appLoader.js +0 -48
- package/ee/application.js +0 -101
- package/ee/baseApp.js +0 -99
- package/ee/eeApp.js +0 -406
- package/ee/index.js +0 -58
- package/electron/window/winState.js +0 -186
- package/httpclient/index.js +0 -161
- package/jobs/baseJobClass.js +0 -16
- package/jobs/renderer/index.js +0 -141
- package/jobs/renderer/loadView.js +0 -41
- package/jobs/unification.js +0 -64
- package/main/index.js +0 -57
- package/old-utils/index.js +0 -91
- package/services/baseContextClass.js +0 -24
- package/services/index.js +0 -41
- package/socket/io.js +0 -28
- package/storage/jsondb/adapters/Base.js +0 -23
- package/storage/jsondb/adapters/FileSync.js +0 -64
- package/storage/jsondb/main.js +0 -55
- package/storage/jsondbStorage.js +0 -196
- package/utils/co.js +0 -237
- package/utils/copyto.js +0 -161
- package/utils/depd/index.js +0 -538
- package/utils/depd/lib/browser/index.js +0 -77
- package/utils/get-port/index.d.ts +0 -64
- package/utils/time/index.js +0 -20
- package/utils/time/ms.js +0 -162
package/socket/httpServer.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const debug = require('debug')('ee-core:socket:httpServer');
|
|
3
4
|
const assert = require('assert');
|
|
4
5
|
const is = require('is-type-of');
|
|
5
6
|
const Koa = require('koa');
|
|
@@ -9,25 +10,37 @@ const https = require('https');
|
|
|
9
10
|
const fs = require('fs');
|
|
10
11
|
const path = require('path');
|
|
11
12
|
const _ = require('lodash');
|
|
12
|
-
const
|
|
13
|
-
const
|
|
13
|
+
const { coreLogger } = require('../log');
|
|
14
|
+
const { getBaseDir } = require('../ps');
|
|
15
|
+
const { getController } = require('../controller');
|
|
16
|
+
const { getConfig } = require('../config');
|
|
17
|
+
const { getPort } = require('../utils/port');
|
|
18
|
+
|
|
19
|
+
let channelSeparator = '/';
|
|
14
20
|
|
|
15
21
|
/**
|
|
16
22
|
* http server
|
|
17
23
|
*/
|
|
18
24
|
class HttpServer {
|
|
19
|
-
constructor (
|
|
20
|
-
|
|
21
|
-
this.
|
|
25
|
+
constructor () {
|
|
26
|
+
const { httpServer, mainServer } = getConfig();
|
|
27
|
+
this.config = httpServer;
|
|
28
|
+
channelSeparator = mainServer.channelSeparator;
|
|
29
|
+
this.httpApp = undefined;
|
|
30
|
+
this.init();
|
|
31
|
+
}
|
|
22
32
|
|
|
23
|
-
|
|
33
|
+
async init() {
|
|
34
|
+
if (this.config.enable == false) {
|
|
24
35
|
return;
|
|
25
36
|
}
|
|
26
37
|
|
|
27
|
-
|
|
38
|
+
const port = await getPort({port: parseInt(this.config.port)});
|
|
28
39
|
if (!port) {
|
|
29
40
|
throw new Error('[ee-core] [socket/HttpServer] http port required, and must be a number !');
|
|
30
41
|
}
|
|
42
|
+
process.env.EE_HTTP_PORT = port;
|
|
43
|
+
this.config.port = port;
|
|
31
44
|
|
|
32
45
|
this._create();
|
|
33
46
|
}
|
|
@@ -36,72 +49,64 @@ class HttpServer {
|
|
|
36
49
|
* 创建服务
|
|
37
50
|
*/
|
|
38
51
|
_create () {
|
|
39
|
-
const
|
|
40
|
-
const
|
|
41
|
-
const
|
|
42
|
-
let sslOptions = {};
|
|
52
|
+
const config = this.config;
|
|
53
|
+
const isHttps = config?.https?.enable ?? false;
|
|
54
|
+
const sslOptions = {};
|
|
43
55
|
|
|
44
56
|
if (isHttps === true) {
|
|
45
|
-
|
|
46
|
-
const keyFile = path.join(
|
|
47
|
-
const certFile = path.join(
|
|
57
|
+
config.protocol = 'https://';
|
|
58
|
+
const keyFile = path.join(getBaseDir(), config.https.key);
|
|
59
|
+
const certFile = path.join(getBaseDir(), config.https.cert);
|
|
48
60
|
assert(fs.existsSync(keyFile), 'ssl key file is required');
|
|
49
61
|
assert(fs.existsSync(certFile), 'ssl cert file is required');
|
|
50
62
|
|
|
51
63
|
sslOptions.key = fs.readFileSync(keyFile);
|
|
52
64
|
sslOptions.cert = fs.readFileSync(certFile);
|
|
53
65
|
}
|
|
54
|
-
const url =
|
|
55
|
-
const corsOptions =
|
|
66
|
+
const url = config.protocol + config.host + ':' + config.port;
|
|
67
|
+
const corsOptions = config.cors;
|
|
56
68
|
|
|
57
69
|
const koaApp = new Koa();
|
|
58
70
|
koaApp
|
|
59
71
|
.use(cors(corsOptions))
|
|
60
|
-
.use(koaBody(
|
|
61
|
-
.use(async (ctx, next) => {
|
|
62
|
-
ctx.eeApp = app;
|
|
63
|
-
await next();
|
|
64
|
-
})
|
|
72
|
+
.use(koaBody(config.body))
|
|
65
73
|
.use(this._dispatch);
|
|
66
74
|
|
|
67
75
|
let msg = '[ee-core] [socket/http] server is: ' + url;
|
|
68
|
-
|
|
69
76
|
const listenOpt = {
|
|
70
|
-
host:
|
|
71
|
-
port:
|
|
77
|
+
host: config.host,
|
|
78
|
+
port: config.port
|
|
72
79
|
}
|
|
73
80
|
if (isHttps) {
|
|
74
81
|
https.createServer(sslOptions, koaApp.callback()).listen(listenOpt, (err) => {
|
|
75
82
|
msg = err ? err : msg;
|
|
76
|
-
|
|
83
|
+
coreLogger.info(msg);
|
|
77
84
|
});
|
|
78
85
|
} else {
|
|
79
86
|
koaApp.listen(listenOpt, (e) => {
|
|
80
87
|
msg = e ? e : msg;
|
|
81
|
-
|
|
88
|
+
coreLogger.info(msg);
|
|
82
89
|
});
|
|
83
|
-
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
this.httpApp = koaApp;
|
|
84
93
|
}
|
|
85
94
|
|
|
86
95
|
/**
|
|
87
96
|
* 路由分发
|
|
88
97
|
*/
|
|
89
98
|
async _dispatch (ctx, next) {
|
|
90
|
-
const
|
|
99
|
+
const controller = getController();
|
|
100
|
+
const { filterRequest } = getConfig().httpServer;
|
|
91
101
|
let uriPath = ctx.request.path;
|
|
92
102
|
const method = ctx.request.method;
|
|
93
103
|
let params = ctx.request.query;
|
|
94
104
|
params = is.object(params) ? JSON.parse(JSON.stringify(params)) : {};
|
|
95
105
|
const body = ctx.request.body;
|
|
96
|
-
const files = ctx.request.files;
|
|
97
106
|
|
|
98
107
|
// 默认
|
|
99
108
|
ctx.response.status = 200;
|
|
100
109
|
|
|
101
|
-
// 添加到全局属性
|
|
102
|
-
ctx.eeApp.request = ctx.request;
|
|
103
|
-
ctx.eeApp.response = ctx.response;
|
|
104
|
-
|
|
105
110
|
try {
|
|
106
111
|
// 找函数
|
|
107
112
|
// 去除开头的 '/'
|
|
@@ -109,23 +114,22 @@ class HttpServer {
|
|
|
109
114
|
uriPath = uriPath.substring(1);
|
|
110
115
|
}
|
|
111
116
|
// 过滤
|
|
112
|
-
if (_.includes(
|
|
113
|
-
ctx.response.body =
|
|
117
|
+
if (_.includes(filterRequest.uris, uriPath)) {
|
|
118
|
+
ctx.response.body = filterRequest.returnData;
|
|
114
119
|
await next();
|
|
115
120
|
return
|
|
116
121
|
}
|
|
117
122
|
if (uriPath.slice(0, 10) != 'controller') {
|
|
118
123
|
uriPath = 'controller/' + uriPath;
|
|
119
124
|
}
|
|
120
|
-
const cmd = uriPath.split('/').join(
|
|
121
|
-
|
|
122
|
-
args
|
|
123
|
-
args.body = ctx.request.body;
|
|
124
|
-
args.query = ctx.request.query;
|
|
125
|
+
const cmd = uriPath.split('/').join(channelSeparator);
|
|
126
|
+
debug('[request] uri %s', cmd);
|
|
127
|
+
const args = (method == 'POST') ? body: params;
|
|
125
128
|
let fn = null;
|
|
126
129
|
if (is.string(cmd)) {
|
|
127
|
-
const actions = cmd.split(
|
|
128
|
-
|
|
130
|
+
const actions = cmd.split(channelSeparator);
|
|
131
|
+
debug('[findFn] channel %o', actions);
|
|
132
|
+
let obj = { controller };
|
|
129
133
|
actions.forEach(key => {
|
|
130
134
|
obj = obj[key];
|
|
131
135
|
if (!obj) throw new Error(`class or function '${key}' not exists`);
|
|
@@ -134,14 +138,20 @@ class HttpServer {
|
|
|
134
138
|
}
|
|
135
139
|
if (!fn) throw new Error('function not exists');
|
|
136
140
|
|
|
137
|
-
const result = await fn.call(
|
|
141
|
+
const result = await fn.call(controller, args, ctx);
|
|
138
142
|
ctx.response.body = result;
|
|
139
143
|
} catch (err) {
|
|
140
|
-
|
|
144
|
+
coreLogger.error('[ee-core/httpServer] throw error:', err);
|
|
141
145
|
}
|
|
142
146
|
|
|
143
147
|
await next();
|
|
144
148
|
}
|
|
149
|
+
|
|
150
|
+
getHttpApp() {
|
|
151
|
+
return this.httpApp;
|
|
152
|
+
}
|
|
145
153
|
}
|
|
146
154
|
|
|
147
|
-
module.exports =
|
|
155
|
+
module.exports = {
|
|
156
|
+
HttpServer
|
|
157
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import IoServer = require("socket.io");
|
|
2
|
+
import IoClient = require("socket.io-client");
|
|
3
|
+
import { HttpServer } from './httpServer';
|
|
4
|
+
import { IpcServer } from './ipcServer';
|
|
5
|
+
import { SocketServer } from './socketServer';
|
|
6
|
+
export declare function loadSocket(): void;
|
|
7
|
+
export declare function getSocketServer(): SocketServer;
|
|
8
|
+
export declare function getHttpServer(): HttpServer;
|
|
9
|
+
export declare function getIpcServer(): IpcServer;
|
|
10
|
+
export { IoServer, IoClient };
|
package/socket/index.js
CHANGED
|
@@ -3,80 +3,63 @@
|
|
|
3
3
|
const IoServer = require('socket.io');
|
|
4
4
|
const IoClient = require('socket.io-client');
|
|
5
5
|
const Koa = require('koa');
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
*/
|
|
22
|
-
startAll(app) {
|
|
23
|
-
this._createSocketServer(app);
|
|
24
|
-
this._createHttpServer(app);
|
|
25
|
-
this._createIpcServer(app);
|
|
26
|
-
},
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* 创建SocketServer
|
|
30
|
-
*/
|
|
31
|
-
_createSocketServer(app) {
|
|
32
|
-
const SocketServer = require('./socketServer');
|
|
33
|
-
this[EESocketServer] = new SocketServer(app);
|
|
34
|
-
|
|
35
|
-
return this[EESocketServer];
|
|
36
|
-
},
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* 获取 Socket Server
|
|
40
|
-
*/
|
|
41
|
-
getSocketServer() {
|
|
42
|
-
return this[EESocketServer] || null;
|
|
43
|
-
},
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* 创建 Http Server
|
|
47
|
-
*/
|
|
48
|
-
_createHttpServer(app) {
|
|
49
|
-
const HttpServer = require('./httpServer');
|
|
50
|
-
this[EEHttpServer] = new HttpServer(app);
|
|
51
|
-
|
|
52
|
-
return this[EEHttpServer];
|
|
53
|
-
},
|
|
6
|
+
const { SocketServer } = require('./socketServer');
|
|
7
|
+
const { HttpServer } = require('./httpServer');
|
|
8
|
+
const { IpcServer } = require('./ipcServer');
|
|
9
|
+
|
|
10
|
+
const Instance = {
|
|
11
|
+
socketServer: null,
|
|
12
|
+
httpServer: null,
|
|
13
|
+
coreLogger: null,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// create SocketServer
|
|
17
|
+
function createSocketServer() {
|
|
18
|
+
Instance.socketServer = new SocketServer();
|
|
19
|
+
return Instance.socketServer;
|
|
20
|
+
}
|
|
54
21
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
return this[EEHttpServer] || null;
|
|
60
|
-
},
|
|
22
|
+
// socketServer
|
|
23
|
+
function getSocketServer() {
|
|
24
|
+
return Instance.socketServer;
|
|
25
|
+
}
|
|
61
26
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
this[EEIpcServer] = new IpcServer(app);
|
|
27
|
+
// create Http Server
|
|
28
|
+
function createHttpServer() {
|
|
29
|
+
Instance.httpServer = new HttpServer();
|
|
30
|
+
return Instance.httpServer;
|
|
31
|
+
}
|
|
68
32
|
|
|
69
|
-
|
|
70
|
-
|
|
33
|
+
// httpServer
|
|
34
|
+
function getHttpServer() {
|
|
35
|
+
return Instance.httpServer;
|
|
36
|
+
}
|
|
71
37
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
},
|
|
38
|
+
// create IPC Server
|
|
39
|
+
function createIpcServer() {
|
|
40
|
+
Instance.ipcServer = new IpcServer();
|
|
41
|
+
return Instance.ipcServer;
|
|
42
|
+
}
|
|
78
43
|
|
|
44
|
+
// ipcServer
|
|
45
|
+
function getIpcServer() {
|
|
46
|
+
return Instance.ipcServer;
|
|
79
47
|
}
|
|
80
48
|
|
|
81
49
|
|
|
82
|
-
|
|
50
|
+
// load socket server
|
|
51
|
+
function loadSocket() {
|
|
52
|
+
createSocketServer();
|
|
53
|
+
createHttpServer();
|
|
54
|
+
createIpcServer();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
module.exports = {
|
|
58
|
+
Koa,
|
|
59
|
+
IoServer,
|
|
60
|
+
IoClient,
|
|
61
|
+
loadSocket,
|
|
62
|
+
getSocketServer,
|
|
63
|
+
getHttpServer,
|
|
64
|
+
getIpcServer
|
|
65
|
+
};
|
package/socket/ipcServer.js
CHANGED
|
@@ -1,113 +1,106 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const debug = require('debug')('ee-core:socket:ipcServer');
|
|
2
4
|
const is = require('is-type-of');
|
|
3
5
|
const { ipcMain } = require('electron');
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const Wrap = require('../utils/wrap');
|
|
9
|
-
const Log = require('../log');
|
|
6
|
+
const { coreLogger } = require('../log');
|
|
7
|
+
const { getController } = require('../controller');
|
|
8
|
+
const { EXPORTS } = require('../core/loader/file_loader');
|
|
9
|
+
const { getConfig } = require('../config');
|
|
10
10
|
|
|
11
11
|
class IpcServer {
|
|
12
|
-
constructor (
|
|
13
|
-
|
|
14
|
-
this.
|
|
12
|
+
constructor () {
|
|
13
|
+
const { mainServer } = getConfig();
|
|
14
|
+
this.channelSeparator = mainServer.channelSeparator;
|
|
15
|
+
this.directory = 'controller';
|
|
16
|
+
this.init();
|
|
17
|
+
}
|
|
18
|
+
init() {
|
|
19
|
+
const controller = getController();
|
|
20
|
+
this.loop(controller, this.directory);
|
|
15
21
|
}
|
|
16
22
|
|
|
17
|
-
|
|
18
|
-
const
|
|
19
|
-
//
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if (
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
let fileObj = Utils.loadFile(fullpath);
|
|
32
|
-
const fns = {};
|
|
33
|
-
// 为了统一,仅支持class文件
|
|
34
|
-
if (is.class(fileObj) || Utils.isBytecodeClass(fileObj)) {
|
|
35
|
-
let proto = fileObj.prototype;
|
|
36
|
-
// 不遍历父类的方法
|
|
37
|
-
//while (proto !== Object.prototype) {
|
|
38
|
-
const keys = Object.getOwnPropertyNames(proto);
|
|
39
|
-
for (const key of keys) {
|
|
40
|
-
if (key === 'constructor') {
|
|
41
|
-
continue;
|
|
42
|
-
}
|
|
43
|
-
const d = Object.getOwnPropertyDescriptor(proto, key);
|
|
44
|
-
if (is.function(d.value) && !fns.hasOwnProperty(key)) {
|
|
45
|
-
fns[key] = 1;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
//proto = Object.getPrototypeOf(proto);
|
|
49
|
-
//}
|
|
23
|
+
loop(obj, pathname) {
|
|
24
|
+
const keys = Object.keys(obj);
|
|
25
|
+
// debug("[loop] keys: %j", keys);
|
|
26
|
+
for (const key of keys) {
|
|
27
|
+
if (key === 'constructor') {
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
let subObj = obj[key];
|
|
31
|
+
let propertyChain = pathname + '.' + key;
|
|
32
|
+
if (subObj && subObj[EXPORTS] === true) {
|
|
33
|
+
this.register(subObj, propertyChain);
|
|
34
|
+
} else if (typeof subObj === 'object') {
|
|
35
|
+
// 如果子对象依然是对象,则递归调用继续判断
|
|
36
|
+
this.loop(subObj, propertyChain);
|
|
50
37
|
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
51
40
|
|
|
52
|
-
|
|
41
|
+
register(exportObj, propertyChain) {
|
|
42
|
+
const controller = getController();
|
|
43
|
+
const keys = Object.keys(exportObj);
|
|
44
|
+
for (const key of keys) {
|
|
45
|
+
// Supports two types of routing separators
|
|
46
|
+
// channel: controller.file.function | controller/file/function
|
|
47
|
+
const tmpChannel = `${propertyChain}.${key}`;
|
|
48
|
+
const channel = tmpChannel.split('.').join(this.channelSeparator);
|
|
49
|
+
debug('[register] channel %s', channel);
|
|
53
50
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
51
|
+
// send/on model
|
|
52
|
+
ipcMain.on(channel, async (event, params) => {
|
|
53
|
+
try {
|
|
54
|
+
const fn = this.findFn(controller, channel);
|
|
55
|
+
const result = await fn.call(controller, params, event);
|
|
57
56
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
let fn = null;
|
|
63
|
-
if (is.string(cmd)) {
|
|
64
|
-
const actions = cmd.split('.');
|
|
65
|
-
let obj = app;
|
|
66
|
-
actions.forEach(key => {
|
|
67
|
-
obj = obj[key];
|
|
68
|
-
if (!obj) throw new Error(`class or function '${key}' not exists`);
|
|
69
|
-
});
|
|
70
|
-
fn = obj;
|
|
71
|
-
}
|
|
72
|
-
if (!fn) throw new Error('function not exists');
|
|
73
|
-
|
|
74
|
-
return fn;
|
|
75
|
-
} catch (err) {
|
|
76
|
-
Log.coreLogger.error('[ee-core] [socket/IpcServer] throw error:', err);
|
|
77
|
-
}
|
|
78
|
-
return null;
|
|
57
|
+
event.returnValue = result;
|
|
58
|
+
event.reply(`${channel}`, result);
|
|
59
|
+
} catch(e) {
|
|
60
|
+
coreLogger.error('[ee-core] [socket/IpcServer] send/on throw error:', e);
|
|
79
61
|
}
|
|
62
|
+
});
|
|
80
63
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
event.returnValue = result;
|
|
88
|
-
event.reply(`${channel}`, result);
|
|
89
|
-
} catch(e) {
|
|
90
|
-
Log.coreLogger.error('[ee-core] [socket/IpcServer] send/on throw error:', e);
|
|
91
|
-
// event.returnValue = e;
|
|
92
|
-
// event.reply(`${channel}`, e);
|
|
93
|
-
}
|
|
94
|
-
});
|
|
64
|
+
// invoke/handle model
|
|
65
|
+
ipcMain.handle(channel, async (event, params) => {
|
|
66
|
+
try {
|
|
67
|
+
const fn = this.findFn(controller, channel);
|
|
68
|
+
const result = await fn.call(controller, params, event);
|
|
95
69
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
70
|
+
return result;
|
|
71
|
+
} catch(e) {
|
|
72
|
+
coreLogger.error('[ee-core] [socket/IpcServer] invoke/handle throw error:', e);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
findFn (controller, c) {
|
|
79
|
+
try {
|
|
80
|
+
// 找函数
|
|
81
|
+
const cmd = c;
|
|
82
|
+
let fn = null;
|
|
83
|
+
if (is.string(cmd)) {
|
|
84
|
+
const actions = cmd.split(this.channelSeparator);
|
|
85
|
+
debug('[findFn] channel %o', actions);
|
|
86
|
+
let obj = { controller };
|
|
87
|
+
actions.forEach(key => {
|
|
88
|
+
obj = obj[key];
|
|
89
|
+
if (!obj) throw new Error(`class or function '${key}' not exists`);
|
|
107
90
|
});
|
|
91
|
+
fn = obj;
|
|
108
92
|
}
|
|
93
|
+
if (!fn) throw new Error('function not exists');
|
|
94
|
+
|
|
95
|
+
return fn;
|
|
96
|
+
} catch (err) {
|
|
97
|
+
coreLogger.error('[ee-core] [socket/IpcServer] throw error:', err);
|
|
109
98
|
}
|
|
99
|
+
return null;
|
|
110
100
|
}
|
|
101
|
+
|
|
111
102
|
}
|
|
112
103
|
|
|
113
|
-
module.exports =
|
|
104
|
+
module.exports = {
|
|
105
|
+
IpcServer
|
|
106
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Server } from "socket.io";
|
|
2
|
+
import { DefaultEventsMap } from "socket.io/dist/typed-events";
|
|
3
|
+
export declare class SocketServer {
|
|
4
|
+
socket: import("socket.io").Socket<DefaultEventsMap, DefaultEventsMap, DefaultEventsMap, any>;
|
|
5
|
+
io: Server<DefaultEventsMap, DefaultEventsMap, DefaultEventsMap, any>;
|
|
6
|
+
config: any;
|
|
7
|
+
init(): Promise<void>;
|
|
8
|
+
connect(): void;
|
|
9
|
+
}
|
|
10
|
+
|
package/socket/socketServer.js
CHANGED
|
@@ -1,52 +1,62 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const debug = require('debug')('ee-core:socket:socketServer');
|
|
4
4
|
const is = require('is-type-of');
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
5
|
+
const { Server } = require('socket.io');
|
|
6
|
+
const { coreLogger } = require('../log');
|
|
7
|
+
const { getConfig } = require('../config');
|
|
8
|
+
const { SocketIO } = require('../const/channel');
|
|
9
|
+
const { getController } = require('../controller');
|
|
10
|
+
const { getPort } = require('../utils/port');
|
|
9
11
|
|
|
10
12
|
/**
|
|
11
13
|
* socket server
|
|
12
14
|
*/
|
|
13
15
|
class SocketServer {
|
|
14
|
-
constructor (
|
|
15
|
-
|
|
16
|
+
constructor () {
|
|
17
|
+
const { socketServer, mainServer } = getConfig();
|
|
18
|
+
this.config = socketServer;
|
|
19
|
+
this.channelSeparator = mainServer.channelSeparator;
|
|
16
20
|
this.socket = undefined;
|
|
17
|
-
|
|
21
|
+
this.io = undefined;
|
|
22
|
+
this.init();
|
|
23
|
+
}
|
|
18
24
|
|
|
19
|
-
|
|
25
|
+
async init() {
|
|
26
|
+
if (this.config.enable == false) {
|
|
20
27
|
return;
|
|
21
28
|
}
|
|
22
29
|
|
|
23
|
-
|
|
30
|
+
const port = await getPort({port: parseInt(this.config.port)});
|
|
24
31
|
if (!port) {
|
|
25
32
|
throw new Error('[ee-core] [socket/socketServer] socekt port required, and must be a number !');
|
|
26
33
|
}
|
|
34
|
+
coreLogger.info('[ee-core] [socket/socketServer] port is:', port);
|
|
27
35
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
this.io = new Server(port,
|
|
31
|
-
this.
|
|
36
|
+
process.env.EE_SOCKET_PORT = port;
|
|
37
|
+
this.config.port = port;
|
|
38
|
+
this.io = new Server(port, this.config);
|
|
39
|
+
this.connect();
|
|
32
40
|
}
|
|
33
41
|
|
|
34
|
-
|
|
35
|
-
const
|
|
42
|
+
connect () {
|
|
43
|
+
const controller = getController();
|
|
36
44
|
this.io.on('connection', (socket) => {
|
|
37
|
-
const channel =
|
|
45
|
+
const channel = this.config.channel || SocketIO.partySoftware;
|
|
38
46
|
this.socket = socket;
|
|
39
47
|
socket.on(channel, async (message, callback) => {
|
|
40
|
-
|
|
48
|
+
coreLogger.info('[ee-core] [socket/socketServer] socket id:' + socket.id + ' message cmd: ' + message.cmd);
|
|
41
49
|
|
|
42
50
|
try {
|
|
43
|
-
//
|
|
51
|
+
// find function
|
|
44
52
|
const cmd = message.cmd;
|
|
45
|
-
const args = message.args
|
|
53
|
+
const args = message.args;
|
|
46
54
|
let fn = null;
|
|
55
|
+
debug('[socket] channel %s', cmd);
|
|
47
56
|
if (is.string(cmd)) {
|
|
48
|
-
const actions = cmd.split(
|
|
49
|
-
|
|
57
|
+
const actions = cmd.split(this.channelSeparator);
|
|
58
|
+
debug('[findFn] channel %o', actions);
|
|
59
|
+
let obj = { controller };
|
|
50
60
|
actions.forEach(key => {
|
|
51
61
|
obj = obj[key];
|
|
52
62
|
if (!obj) throw new Error(`class or function '${key}' not exists`);
|
|
@@ -55,16 +65,18 @@ class SocketServer {
|
|
|
55
65
|
}
|
|
56
66
|
if (!fn) throw new Error('function not exists');
|
|
57
67
|
|
|
58
|
-
const result = await fn.call(
|
|
68
|
+
const result = await fn.call(controller, args);
|
|
59
69
|
if (callback) {
|
|
60
70
|
callback(result);
|
|
61
71
|
}
|
|
62
72
|
} catch (err) {
|
|
63
|
-
|
|
73
|
+
coreLogger.error('[ee-core] [socket/socketServer] throw error:', err);
|
|
64
74
|
}
|
|
65
75
|
});
|
|
66
76
|
});
|
|
67
77
|
}
|
|
68
78
|
}
|
|
69
79
|
|
|
70
|
-
module.exports =
|
|
80
|
+
module.exports = {
|
|
81
|
+
SocketServer,
|
|
82
|
+
};
|