oox 0.3.0-beta9 → 0.3.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/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 +286 -192
- package/modules/http/router.js +205 -0
- package/modules/http/utils.js +75 -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 +65 -47
- package/types/modules/http/router.d.ts +49 -0
- 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,101 +1,97 @@
|
|
|
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
|
-
socket
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
socket.once('
|
|
84
|
-
socket.once('
|
|
85
|
-
socket.once('
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
this.clientOnSocketConnection(socket);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
exports.default = SocketIOCore;
|
|
1
|
+
import * as SocketIOClient from 'socket.io-client';
|
|
2
|
+
import { sockets } from './socket.js';
|
|
3
|
+
import SocketIOServer from './server.js';
|
|
4
|
+
import * as oox from '../../index.js';
|
|
5
|
+
export default class SocketIOCore extends SocketIOServer {
|
|
6
|
+
/**
|
|
7
|
+
* connect to <SocketIO RPC> service
|
|
8
|
+
*/
|
|
9
|
+
async connect(url) {
|
|
10
|
+
let socket = sockets.get(url);
|
|
11
|
+
// 已经连接的直接返回
|
|
12
|
+
if (socket) {
|
|
13
|
+
try {
|
|
14
|
+
await this.clientWaitConnection(socket);
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
this.clientOnSocketDisconnect(socket, error.message);
|
|
18
|
+
throw error;
|
|
19
|
+
}
|
|
20
|
+
return socket;
|
|
21
|
+
}
|
|
22
|
+
const headers = {
|
|
23
|
+
'x-caller': oox.config.name
|
|
24
|
+
};
|
|
25
|
+
const { host } = oox.config;
|
|
26
|
+
headers['x-ip'] = host;
|
|
27
|
+
headers['x-caller-id'] = this.getUrl();
|
|
28
|
+
// create socket handler
|
|
29
|
+
const mURL = new URL(url);
|
|
30
|
+
socket = SocketIOClient.io(mURL.origin, {
|
|
31
|
+
extraHeaders: headers,
|
|
32
|
+
path: mURL.pathname
|
|
33
|
+
});
|
|
34
|
+
socket.data = { name: 'anonymous', connected: false, id: url, host: mURL.host };
|
|
35
|
+
sockets.set(url, socket);
|
|
36
|
+
try {
|
|
37
|
+
await this.clientWaitConnection(socket);
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
this.clientOnSocketDisconnect(socket, error);
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
return socket;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* 客户端Socket连接事件
|
|
47
|
+
*/
|
|
48
|
+
clientOnSocketConnection(socket) {
|
|
49
|
+
socket.data.connected = true;
|
|
50
|
+
socket.once('disconnect', reason => this.clientOnSocketDisconnect(socket, reason));
|
|
51
|
+
this.clientOnConnection(socket);
|
|
52
|
+
}
|
|
53
|
+
clientOnDisconnect(socket, reason) { }
|
|
54
|
+
clientOnConnection(socket) { }
|
|
55
|
+
/**
|
|
56
|
+
* 客户端Socket断开事件
|
|
57
|
+
* @param {Socket} socket
|
|
58
|
+
*/
|
|
59
|
+
clientOnSocketDisconnect(socket, reason) {
|
|
60
|
+
socket.data.connected = false;
|
|
61
|
+
socket.disconnect();
|
|
62
|
+
sockets.delete(socket.data.id);
|
|
63
|
+
this.clientOnDisconnect(socket, reason);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* 等待socket连接
|
|
67
|
+
*/
|
|
68
|
+
async clientWaitConnection(socket) {
|
|
69
|
+
if (socket.data.connected)
|
|
70
|
+
return;
|
|
71
|
+
if (socket.connect)
|
|
72
|
+
socket.connect();
|
|
73
|
+
try {
|
|
74
|
+
await new Promise((resolve, reject) => {
|
|
75
|
+
const onError = (reason) => {
|
|
76
|
+
socket.offAny(onError);
|
|
77
|
+
const message = 'string' === typeof reason ? reason : reason instanceof Error ? reason.message : 'connect error';
|
|
78
|
+
reject(new Error(message));
|
|
79
|
+
};
|
|
80
|
+
socket.once('disconnect', onError);
|
|
81
|
+
socket.once('connect_error', onError);
|
|
82
|
+
socket.once('connect_timeout', onError);
|
|
83
|
+
socket.once('reconnect_error', onError);
|
|
84
|
+
socket.once('reconnect_failed', onError);
|
|
85
|
+
socket.once('oox_connected', ({ name }) => {
|
|
86
|
+
socket.offAny(onError);
|
|
87
|
+
socket.data.name = name;
|
|
88
|
+
resolve();
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
throw new Error(error.message);
|
|
94
|
+
}
|
|
95
|
+
this.clientOnSocketConnection(socket);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -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
|
+
}
|