oox 0.3.0-beta9 → 0.3.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 +29 -32
- package/app.js +131 -143
- package/bin/argv.js +63 -70
- package/bin/cli.js +57 -43
- package/bin/configurer.js +60 -62
- package/bin/loader.mjs +392 -279
- package/bin/proxy-import.js +12 -0
- package/bin/proxy-require.js +88 -0
- package/bin/register.js +46 -55
- package/bin/starter.js +63 -66
- package/index.js +155 -168
- package/index.mjs +4 -4
- package/logger.js +25 -40
- package/modules/http/index.js +234 -192
- package/modules/http/utils.js +74 -73
- package/modules/index.js +86 -88
- package/modules/module.js +11 -16
- package/modules/socketio/client.js +97 -101
- package/modules/socketio/index.js +171 -168
- package/modules/socketio/server.js +188 -136
- package/modules/socketio/socket.js +1 -4
- package/package.json +14 -12
- package/types/app.d.ts +50 -51
- package/types/bin/argv.d.ts +8 -8
- package/types/bin/cli.d.ts +6 -2
- package/types/bin/configurer.d.ts +3 -1
- package/types/bin/proxy-import.d.ts +4 -0
- package/types/bin/proxy-require.d.ts +5 -0
- package/types/bin/register.d.ts +1 -1
- package/types/bin/starter.d.ts +5 -1
- package/types/index.d.ts +78 -76
- package/types/logger.d.ts +5 -4
- package/types/modules/http/index.d.ts +58 -47
- package/types/modules/http/utils.d.ts +14 -17
- package/types/modules/index.d.ts +24 -24
- package/types/modules/module.d.ts +11 -13
- package/types/modules/socketio/client.d.ts +23 -23
- package/types/modules/socketio/index.d.ts +37 -37
- package/types/modules/socketio/server.d.ts +44 -35
- package/types/modules/socketio/socket.d.ts +11 -11
- package/types/utils.d.ts +6 -6
- package/utils.js +57 -63
- package/bin/proxyer.js +0 -61
- package/types/bin/proxyer.d.ts +0 -1
|
@@ -1,168 +1,171 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
context = oox.genContext(connectionContext);
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
return body;
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
1
|
+
import * as path from 'node:path';
|
|
2
|
+
import SocketIOClient from './client.js';
|
|
3
|
+
import * as oox from '../../index.js';
|
|
4
|
+
import { RPCKeepAliveConnection, removeKeepAliveConnection, addKeepAliveConnection, getKeepAliveConnections } from '../../index.js';
|
|
5
|
+
import { sockets } from './socket.js';
|
|
6
|
+
export { sockets };
|
|
7
|
+
export default class SocketIOModule extends SocketIOClient {
|
|
8
|
+
sockets = sockets;
|
|
9
|
+
async serve() {
|
|
10
|
+
await this.stop();
|
|
11
|
+
const _http = oox.modules.builtins.http;
|
|
12
|
+
const httpConfig = _http.getConfig(), config = this.getConfig();
|
|
13
|
+
let isShareServer = false;
|
|
14
|
+
// 都没设置端口
|
|
15
|
+
isShareServer = !httpConfig.port && !config.port;
|
|
16
|
+
// 都设置相同端口
|
|
17
|
+
isShareServer ||= httpConfig.port === config.port;
|
|
18
|
+
// http 模块未被禁用
|
|
19
|
+
isShareServer &&= httpConfig.enabled;
|
|
20
|
+
if (isShareServer) {
|
|
21
|
+
config.path = path.posix.join(httpConfig.path, config.path);
|
|
22
|
+
this.server = _http.server;
|
|
23
|
+
this.config.ssl = _http.config.ssl;
|
|
24
|
+
}
|
|
25
|
+
await super.serve();
|
|
26
|
+
}
|
|
27
|
+
onSyncConnection(socket) {
|
|
28
|
+
const mSockets = Array.from(sockets.values())
|
|
29
|
+
.filter(s => s !== socket &&
|
|
30
|
+
s.data.name !== socket.data.name &&
|
|
31
|
+
/^wss?:\/\/.+$/.test(s.data.id));
|
|
32
|
+
return mSockets.map(s => s.data);
|
|
33
|
+
}
|
|
34
|
+
serverOnDisconnect(socket, reason) {
|
|
35
|
+
super.serverOnDisconnect(socket, reason);
|
|
36
|
+
removeKeepAliveConnection(socket.data.name, socket.data.id);
|
|
37
|
+
}
|
|
38
|
+
clientOnDisconnect(socket, reason) {
|
|
39
|
+
super.clientOnDisconnect(socket, reason);
|
|
40
|
+
removeKeepAliveConnection(socket.data.name, socket.data.id);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
*
|
|
44
|
+
* @param socket 是由哪个通道发送过来的
|
|
45
|
+
* @param connectionDatas
|
|
46
|
+
*/
|
|
47
|
+
clientOnSyncConnection(socket, connectionDatas) {
|
|
48
|
+
for (const data of connectionDatas)
|
|
49
|
+
if (!sockets.has(data.id))
|
|
50
|
+
this.connect(data.id).catch((error) => console.error(error));
|
|
51
|
+
}
|
|
52
|
+
onFetchActions(socket, search) {
|
|
53
|
+
const data = [];
|
|
54
|
+
for (const key of oox.kvMethods.keys())
|
|
55
|
+
if (!key.endsWith('_proxy') && key.includes(search))
|
|
56
|
+
data.push(key);
|
|
57
|
+
return data;
|
|
58
|
+
}
|
|
59
|
+
fetchActions(id, search = '') {
|
|
60
|
+
let socket = sockets.get(id);
|
|
61
|
+
if (!socket) {
|
|
62
|
+
const connections = getKeepAliveConnections(id);
|
|
63
|
+
if (!connections || !connections.size)
|
|
64
|
+
throw new Error(`Unknown service identify<${id}>`);
|
|
65
|
+
id = connections.keys().next().value;
|
|
66
|
+
socket = sockets.get(id);
|
|
67
|
+
}
|
|
68
|
+
if (!socket)
|
|
69
|
+
throw new Error(`Unknown service identify<${id}>`);
|
|
70
|
+
return this.emit(socket.data.id, 'fetchActions', [search]);
|
|
71
|
+
}
|
|
72
|
+
onConnection(socket) {
|
|
73
|
+
const { id, name, host } = socket.data;
|
|
74
|
+
const connection = new RPCKeepAliveConnection(this, id, socket.data);
|
|
75
|
+
addKeepAliveConnection(connection);
|
|
76
|
+
const connectionContext = {
|
|
77
|
+
sourceIP: '',
|
|
78
|
+
ip: host,
|
|
79
|
+
caller: name,
|
|
80
|
+
callerId: id,
|
|
81
|
+
connection
|
|
82
|
+
};
|
|
83
|
+
socket.on('fetchActions', async (search, fn) => {
|
|
84
|
+
if ('function' !== typeof fn)
|
|
85
|
+
return;
|
|
86
|
+
const data = await this.onFetchActions(socket, search);
|
|
87
|
+
fn(data);
|
|
88
|
+
});
|
|
89
|
+
socket.on('call', async (action, params, context, callback) => {
|
|
90
|
+
if ('object' !== typeof context)
|
|
91
|
+
context = oox.genContext(connectionContext);
|
|
92
|
+
else
|
|
93
|
+
context = oox.genContext(Object.assign(context, connectionContext));
|
|
94
|
+
this.call(action, params, context, callback);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
*
|
|
99
|
+
* @param {Socket} socket
|
|
100
|
+
*/
|
|
101
|
+
serverOnConnection(socket) {
|
|
102
|
+
super.serverOnConnection(socket);
|
|
103
|
+
socket.setMaxListeners(0);
|
|
104
|
+
socket.on('syncConnection', async (fn) => {
|
|
105
|
+
if ('function' !== typeof fn)
|
|
106
|
+
return;
|
|
107
|
+
const data = this.onSyncConnection(socket);
|
|
108
|
+
fn(data);
|
|
109
|
+
});
|
|
110
|
+
this.onConnection(socket);
|
|
111
|
+
}
|
|
112
|
+
async call(action, params, context, callback) {
|
|
113
|
+
const returns = await oox.call(action, params, context);
|
|
114
|
+
if (!oox.config.errorStack && returns.error) {
|
|
115
|
+
// 不返回错误调用栈信息
|
|
116
|
+
delete returns.error.stack;
|
|
117
|
+
}
|
|
118
|
+
'function' === typeof callback && callback(returns);
|
|
119
|
+
return returns;
|
|
120
|
+
}
|
|
121
|
+
clientOnConnection(socket) {
|
|
122
|
+
super.clientOnConnection(socket);
|
|
123
|
+
socket.emit('syncConnection', (socketDatas) => this.clientOnSyncConnection(socket, socketDatas));
|
|
124
|
+
this.onConnection(socket);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* socketio emit
|
|
128
|
+
*/
|
|
129
|
+
async emit(url, event, params) {
|
|
130
|
+
let socket = null;
|
|
131
|
+
try {
|
|
132
|
+
socket = await this.connect(url);
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
// try again
|
|
136
|
+
socket = await this.connect(url);
|
|
137
|
+
}
|
|
138
|
+
try {
|
|
139
|
+
return await new Promise((resolve, reject) => {
|
|
140
|
+
const onError = (reason) => {
|
|
141
|
+
const message = 'string' === typeof reason ? reason : reason instanceof Error ? reason.message : 'connect error';
|
|
142
|
+
reject(new Error(message));
|
|
143
|
+
};
|
|
144
|
+
// RPC 执行时中断连接
|
|
145
|
+
socket.once('disconnect', onError);
|
|
146
|
+
socket.emit(event, ...params, (returns) => {
|
|
147
|
+
socket.off('disconnect', onError);
|
|
148
|
+
resolve(returns);
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
catch (error) {
|
|
153
|
+
throw new Error(error.message);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* RPC
|
|
158
|
+
*/
|
|
159
|
+
async rpc(url, action, params, context) {
|
|
160
|
+
if (!context || !context.traceId) {
|
|
161
|
+
context = oox.getContext();
|
|
162
|
+
}
|
|
163
|
+
const { success, error, body } = await this.emit(url, 'call', [action, params, context]);
|
|
164
|
+
if (success)
|
|
165
|
+
return body;
|
|
166
|
+
else if (error)
|
|
167
|
+
throw new Error(error.message);
|
|
168
|
+
else
|
|
169
|
+
throw new Error('[RPC] Unknown Error');
|
|
170
|
+
}
|
|
171
|
+
}
|