oox 0.3.1 → 0.3.3
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/bin/argv.js +10 -1
- package/bin/configurer.js +3 -1
- package/bin/register.js +9 -23
- package/bin/starter.js +2 -2
- package/index.js +22 -43
- package/modules/http/index.js +55 -3
- package/modules/http/router.js +205 -0
- package/modules/http/utils.js +5 -4
- package/modules/socketio/adapter.js +158 -0
- package/modules/socketio/index.js +4 -152
- package/modules/socketio/server.js +53 -40
- package/modules/socketio/socket.js +1 -1
- package/modules/socketio/utils.js +38 -0
- package/package.json +1 -1
- package/rpc-keepalive-connection.js +89 -0
- package/types/app.d.ts +1 -1
- package/types/index.d.ts +11 -36
- package/types/modules/http/index.d.ts +9 -2
- package/types/modules/http/router.d.ts +49 -0
- package/types/modules/socketio/adapter.d.ts +28 -0
- package/types/modules/socketio/index.d.ts +2 -35
- package/types/modules/socketio/server.d.ts +16 -12
- package/types/modules/socketio/socket.d.ts +8 -11
- package/types/modules/socketio/utils.d.ts +7 -0
- package/types/rpc-keepalive-connection.d.ts +57 -0
- package/modules/socketio/client.js +0 -97
- package/types/modules/socketio/client.d.ts +0 -23
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
import * as
|
|
2
|
-
import SocketIOClient from './client.js';
|
|
1
|
+
import * as PATH from 'node:path';
|
|
3
2
|
import * as oox from '../../index.js';
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
export { sockets };
|
|
7
|
-
export default class SocketIOModule extends SocketIOClient {
|
|
8
|
-
sockets = sockets;
|
|
3
|
+
import SocketINServer from './server.js';
|
|
4
|
+
export default class SocketIOModule extends SocketINServer {
|
|
9
5
|
async serve() {
|
|
10
6
|
await this.stop();
|
|
11
7
|
const _http = oox.modules.builtins.http;
|
|
@@ -18,154 +14,10 @@ export default class SocketIOModule extends SocketIOClient {
|
|
|
18
14
|
// http 模块未被禁用
|
|
19
15
|
isShareServer &&= httpConfig.enabled;
|
|
20
16
|
if (isShareServer) {
|
|
21
|
-
config.path =
|
|
17
|
+
config.path = PATH.posix.join(httpConfig.path, config.path);
|
|
22
18
|
this.server = _http.server;
|
|
23
19
|
this.config.ssl = _http.config.ssl;
|
|
24
20
|
}
|
|
25
21
|
await super.serve();
|
|
26
22
|
}
|
|
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
23
|
}
|
|
@@ -3,7 +3,8 @@ import * as https from 'node:https';
|
|
|
3
3
|
import { Server } from 'socket.io';
|
|
4
4
|
import * as oox from '../../index.js';
|
|
5
5
|
import { Module, ModuleConfig } from '../../index.js';
|
|
6
|
-
import
|
|
6
|
+
import SocketIOAdapter from './adapter.js';
|
|
7
|
+
import { isWebSocketURL, OOXEvent } from './utils.js';
|
|
7
8
|
export class SocketIOConfig extends ModuleConfig {
|
|
8
9
|
// listen port
|
|
9
10
|
port = 0;
|
|
@@ -32,30 +33,12 @@ export default class SocketIOServer extends Module {
|
|
|
32
33
|
#isSelfServer = false;
|
|
33
34
|
server = null;
|
|
34
35
|
socketServer = null;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
static genWebSocketUrl(url) {
|
|
39
|
-
// :6000
|
|
40
|
-
if (url.startsWith(':'))
|
|
41
|
-
url = oox.config.host + url;
|
|
42
|
-
// 127.0.0.1:6000
|
|
43
|
-
if (!/^\w+?:\/.+$/.test(url))
|
|
44
|
-
url = 'ws://' + url;
|
|
45
|
-
const urlObject = new URL(url);
|
|
46
|
-
if (urlObject.protocol !== 'wss:') {
|
|
47
|
-
if (urlObject.port === '443')
|
|
48
|
-
urlObject.protocol = 'wss:';
|
|
49
|
-
else
|
|
50
|
-
urlObject.protocol = 'ws:';
|
|
51
|
-
}
|
|
52
|
-
return urlObject.toString();
|
|
53
|
-
}
|
|
54
|
-
getUrl() {
|
|
36
|
+
adapter = new SocketIOAdapter();
|
|
37
|
+
getURL() {
|
|
55
38
|
const { host } = oox.config;
|
|
56
39
|
const { port, path } = this.config;
|
|
57
40
|
const protocol = this.config.ssl.enabled ? 'wss:' : 'ws:';
|
|
58
|
-
return `${protocol}//${host}:${port}${path}
|
|
41
|
+
return new URL(`${protocol}//${host}:${port}${path}`);
|
|
59
42
|
}
|
|
60
43
|
setConfig(config) {
|
|
61
44
|
Object.assign(this.config, config);
|
|
@@ -70,6 +53,7 @@ export default class SocketIOServer extends Module {
|
|
|
70
53
|
return this.config;
|
|
71
54
|
}
|
|
72
55
|
async serve() {
|
|
56
|
+
oox.rpcKeepAliveConnectionAdapters.set(this.name, this.adapter);
|
|
73
57
|
await this.stop();
|
|
74
58
|
const { port, ssl } = this.config;
|
|
75
59
|
const isSelfServer = this.#isSelfServer = this.server ? true : false;
|
|
@@ -149,6 +133,7 @@ export default class SocketIOServer extends Module {
|
|
|
149
133
|
this.serverOnSocketConnection(socket);
|
|
150
134
|
}
|
|
151
135
|
catch (error) {
|
|
136
|
+
console.error(error);
|
|
152
137
|
socket.send(error.message).disconnect(true);
|
|
153
138
|
}
|
|
154
139
|
});
|
|
@@ -159,30 +144,58 @@ export default class SocketIOServer extends Module {
|
|
|
159
144
|
serverOnSocketConnection(socket) {
|
|
160
145
|
const headers = socket.handshake.headers;
|
|
161
146
|
const callerId = String(headers['x-caller-id'] || '') || socket.id;
|
|
162
|
-
// 已经存在相同的连接
|
|
163
|
-
if (sockets.has(callerId))
|
|
164
|
-
throw new Error('Connection Exists');
|
|
165
147
|
// client ip or caller service ip
|
|
166
148
|
const ip = String(headers['x-real-ip'] || headers['x-ip'] || socket.handshake.address);
|
|
167
149
|
// service name
|
|
168
150
|
const caller = String(headers['x-caller'] || 'anonymous');
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
151
|
+
const connection = new oox.RPCKeepAliveConnection(this.adapter, socket, {
|
|
152
|
+
host: ip,
|
|
153
|
+
name: caller,
|
|
154
|
+
id: callerId,
|
|
155
|
+
});
|
|
156
|
+
oox.addKeepAliveConnection(connection);
|
|
157
|
+
this.bindServerConnectionEvents(connection);
|
|
158
|
+
this.adapter.bindCall(connection);
|
|
159
|
+
connection.enabled = true;
|
|
160
|
+
socket.emit(OOXEvent.CONNECTED, {
|
|
161
|
+
id: oox.config.id,
|
|
162
|
+
name: oox.config.name
|
|
163
|
+
});
|
|
175
164
|
}
|
|
176
|
-
serverOnConnection(socket) { }
|
|
177
165
|
/**
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
166
|
+
* 服务器发送连接列表
|
|
167
|
+
* @param connection
|
|
168
|
+
*/
|
|
169
|
+
[OOXEvent.SYNC_CONNECTIONS](connection) {
|
|
170
|
+
const socket = connection.nativeConnection;
|
|
171
|
+
const datas = [];
|
|
172
|
+
for (const [name, connections] of oox.enabledKeepAliveConnections.entries()) {
|
|
173
|
+
if (name === connection.data.name)
|
|
174
|
+
continue;
|
|
175
|
+
for (const conn of Array.from(connections.values())) {
|
|
176
|
+
if (!conn.data.url || !isWebSocketURL(conn.data.url))
|
|
177
|
+
continue;
|
|
178
|
+
datas.push(conn.data);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return datas;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* 绑定服务器连接事件
|
|
185
|
+
* @param connection
|
|
181
186
|
*/
|
|
182
|
-
|
|
183
|
-
socket
|
|
184
|
-
|
|
185
|
-
|
|
187
|
+
bindServerConnectionEvents(connection) {
|
|
188
|
+
const socket = connection.nativeConnection;
|
|
189
|
+
socket.on(OOXEvent.SYNC_CONNECTIONS, async (fn) => {
|
|
190
|
+
if ('function' !== typeof fn)
|
|
191
|
+
return;
|
|
192
|
+
const datas = this[OOXEvent.SYNC_CONNECTIONS](connection);
|
|
193
|
+
fn(datas);
|
|
194
|
+
});
|
|
195
|
+
socket.on('disconnecting', (reason) => {
|
|
196
|
+
oox.removeKeepAliveConnection(connection);
|
|
197
|
+
socket.removeAllListeners();
|
|
198
|
+
connection.emit('disconnect');
|
|
199
|
+
});
|
|
186
200
|
}
|
|
187
|
-
serverOnDisconnect(socket, reason) { }
|
|
188
201
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export var OOXEvent;
|
|
2
|
+
(function (OOXEvent) {
|
|
3
|
+
OOXEvent["CONNECTED"] = "oox:connected";
|
|
4
|
+
OOXEvent["DISABLED"] = "oox:disabled";
|
|
5
|
+
OOXEvent["SYNC_CONNECTIONS"] = "oox:sync_connections";
|
|
6
|
+
})(OOXEvent || (OOXEvent = {}));
|
|
7
|
+
export function isWebSocketURL(url) {
|
|
8
|
+
if ('string' === typeof url) {
|
|
9
|
+
return /^wss?:\/\/.+$/.test(url);
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
return url.protocol === 'ws:' || url.protocol === 'wss:';
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export function genWebSocketURL(url) {
|
|
16
|
+
// :6000
|
|
17
|
+
if (url.startsWith(':'))
|
|
18
|
+
url = 'ws://localhost' + url;
|
|
19
|
+
// 127.0.0.1:6000
|
|
20
|
+
if (!/^\w+?:\/.+$/.test(url))
|
|
21
|
+
url = 'ws://' + url;
|
|
22
|
+
const urlObject = new URL(url);
|
|
23
|
+
// :8000 => :8000/socket.io
|
|
24
|
+
const notSetPath = !urlObject.pathname || (urlObject.pathname === '/'
|
|
25
|
+
&& !url.endsWith('/')
|
|
26
|
+
&& !urlObject.search
|
|
27
|
+
&& !urlObject.hash);
|
|
28
|
+
if (notSetPath) {
|
|
29
|
+
urlObject.pathname = '/socket.io';
|
|
30
|
+
}
|
|
31
|
+
if (urlObject.protocol !== 'wss:') {
|
|
32
|
+
if (urlObject.port === '443')
|
|
33
|
+
urlObject.protocol = 'wss:';
|
|
34
|
+
else
|
|
35
|
+
urlObject.protocol = 'ws:';
|
|
36
|
+
}
|
|
37
|
+
return urlObject;
|
|
38
|
+
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import EventEmitter from 'node:events';
|
|
2
|
+
export class RPCKeepAliveConnection extends EventEmitter {
|
|
3
|
+
// 连接是否可用
|
|
4
|
+
#enabled = false;
|
|
5
|
+
data;
|
|
6
|
+
nativeConnection;
|
|
7
|
+
adapter;
|
|
8
|
+
constructor(adapter, nativeConnection, data) {
|
|
9
|
+
super();
|
|
10
|
+
this.adapter = adapter;
|
|
11
|
+
this.nativeConnection = nativeConnection;
|
|
12
|
+
this.data = data;
|
|
13
|
+
}
|
|
14
|
+
set enabled(enabled) {
|
|
15
|
+
if (enabled !== this.#enabled) {
|
|
16
|
+
this.#enabled = enabled;
|
|
17
|
+
this.emit(enabled ? 'enabled' : 'disabled');
|
|
18
|
+
if (enabled) {
|
|
19
|
+
enabledKeepAliveConnections.add(this);
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
enabledKeepAliveConnections.remove(this);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
get enabled() {
|
|
27
|
+
return this.#enabled;
|
|
28
|
+
}
|
|
29
|
+
updateId(newId) {
|
|
30
|
+
keepAliveConnections.remove(this);
|
|
31
|
+
this.data.id = newId;
|
|
32
|
+
keepAliveConnections.add(this);
|
|
33
|
+
}
|
|
34
|
+
updateName(newName) {
|
|
35
|
+
keepAliveConnections.remove(this);
|
|
36
|
+
this.data.name = newName;
|
|
37
|
+
keepAliveConnections.add(this);
|
|
38
|
+
}
|
|
39
|
+
updateIdAndName(newId, newName) {
|
|
40
|
+
keepAliveConnections.remove(this);
|
|
41
|
+
this.data.id = newId;
|
|
42
|
+
this.data.name = newName;
|
|
43
|
+
keepAliveConnections.add(this);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export class KeepAliveConnectionStore {
|
|
47
|
+
#map = new Map();
|
|
48
|
+
entries() {
|
|
49
|
+
return this.#map.entries();
|
|
50
|
+
}
|
|
51
|
+
getConnectionsOfService(name) {
|
|
52
|
+
return this.#map.get(name) || new Map();
|
|
53
|
+
}
|
|
54
|
+
has(name, id) {
|
|
55
|
+
const connections = this.#map.get(name);
|
|
56
|
+
if (!connections)
|
|
57
|
+
return false;
|
|
58
|
+
return connections.has(id);
|
|
59
|
+
}
|
|
60
|
+
get(name, id) {
|
|
61
|
+
const connections = this.#map.get(name);
|
|
62
|
+
if (!connections)
|
|
63
|
+
return null;
|
|
64
|
+
return connections.get(id) || null;
|
|
65
|
+
}
|
|
66
|
+
add(connection) {
|
|
67
|
+
const { name, id } = connection.data;
|
|
68
|
+
const connections = this.#map.get(name);
|
|
69
|
+
if (connections) {
|
|
70
|
+
connections.set(id, connection);
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
this.#map.set(name, new Map([[id, connection]]));
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
remove(name, id) {
|
|
77
|
+
if (name instanceof RPCKeepAliveConnection) {
|
|
78
|
+
id = name.data.id;
|
|
79
|
+
name = name.data.name;
|
|
80
|
+
}
|
|
81
|
+
const connections = this.#map.get(name);
|
|
82
|
+
if (connections && id !== undefined) {
|
|
83
|
+
connections.delete(id);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
export const rpcKeepAliveConnectionAdapters = new Map();
|
|
88
|
+
export const keepAliveConnections = new KeepAliveConnectionStore();
|
|
89
|
+
export const enabledKeepAliveConnections = new KeepAliveConnectionStore();
|
package/types/app.d.ts
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import * as app from './app.js';
|
|
2
2
|
import Module, { ModuleConfig } from './modules/module.js';
|
|
3
3
|
import Modules from './modules/index.js';
|
|
4
|
+
import { RPCKeepAliveConnectionAdapter, RPCKeepAliveConnection, RPCKeepAliveConnectionData, rpcKeepAliveConnectionAdapters, keepAliveConnections, enabledKeepAliveConnections } from './rpc-keepalive-connection.js';
|
|
5
|
+
export { RPCKeepAliveConnectionAdapter, RPCKeepAliveConnection, RPCKeepAliveConnectionData, rpcKeepAliveConnectionAdapters, keepAliveConnections, enabledKeepAliveConnections };
|
|
4
6
|
export { ReturnsBody } from './app.js';
|
|
5
7
|
export { Module, ModuleConfig };
|
|
6
8
|
export declare const modules: Modules;
|
|
@@ -10,11 +12,12 @@ export declare class Context extends app.Context {
|
|
|
10
12
|
ip: string;
|
|
11
13
|
caller: string;
|
|
12
14
|
callerId: string;
|
|
13
|
-
connection?: RPCKeepAliveConnection
|
|
15
|
+
connection?: RPCKeepAliveConnection<any>;
|
|
14
16
|
toJSON?(): {} & this;
|
|
15
17
|
}
|
|
16
18
|
export declare class Config {
|
|
17
19
|
[x: string]: any;
|
|
20
|
+
id: `${string}-${string}-${string}-${string}-${string}`;
|
|
18
21
|
name: string;
|
|
19
22
|
group: string;
|
|
20
23
|
ignore: string[];
|
|
@@ -24,8 +27,10 @@ export declare class Config {
|
|
|
24
27
|
};
|
|
25
28
|
host: string;
|
|
26
29
|
port: number;
|
|
27
|
-
origin: string;
|
|
30
|
+
origin: string | string[];
|
|
28
31
|
errorStack: boolean;
|
|
32
|
+
registry: string[];
|
|
33
|
+
registryAdapter: string;
|
|
29
34
|
}
|
|
30
35
|
export declare const config: Config;
|
|
31
36
|
export declare function setGenTraceIdFunction(fn: () => string): void;
|
|
@@ -40,39 +45,9 @@ export declare function genContext(context?: Context): Context;
|
|
|
40
45
|
export declare function getContext(): Context;
|
|
41
46
|
export declare function serve(): Promise<void>;
|
|
42
47
|
export declare function stop(): Promise<void>;
|
|
43
|
-
export
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
export interface RPCKeepAliveConnectionData {
|
|
47
|
-
/**
|
|
48
|
-
* 是否连接成功
|
|
49
|
-
*/
|
|
50
|
-
connected: boolean;
|
|
51
|
-
/**
|
|
52
|
-
* 连接主机地址
|
|
53
|
-
*/
|
|
54
|
-
host: string;
|
|
55
|
-
/**
|
|
56
|
-
* 连接服务名称
|
|
57
|
-
*/
|
|
58
|
-
name: string;
|
|
59
|
-
/**
|
|
60
|
-
* 目标服务网络唯一ID
|
|
61
|
-
*/
|
|
62
|
-
id: string;
|
|
63
|
-
}
|
|
64
|
-
export declare class RPCKeepAliveConnection {
|
|
65
|
-
data: RPCKeepAliveConnectionData;
|
|
66
|
-
nativeConnection: any;
|
|
67
|
-
adapter: RPCConnectionAdapter;
|
|
68
|
-
constructor(adapter: RPCConnectionAdapter, nativeConnection: any, data?: RPCKeepAliveConnectionData);
|
|
69
|
-
}
|
|
70
|
-
export declare const keepAliveConnections: Map<string, Map<string, RPCKeepAliveConnection>>;
|
|
71
|
-
export declare function getKeepAliveConnections(name: string): Map<string, RPCKeepAliveConnection>;
|
|
72
|
-
export declare function getKeepAliveConnection(name: string, id: string): RPCKeepAliveConnection | null;
|
|
73
|
-
export declare function addKeepAliveConnection(connection: RPCKeepAliveConnection): void;
|
|
74
|
-
export declare function removeKeepAliveConnection(connection: RPCKeepAliveConnection): void;
|
|
48
|
+
export declare function addKeepAliveConnection(connection: RPCKeepAliveConnection<any>): void;
|
|
49
|
+
export declare function removeKeepAliveConnection(connection: RPCKeepAliveConnection<any>): void;
|
|
75
50
|
export declare function removeKeepAliveConnection(name: string, id: string): void;
|
|
76
|
-
export declare function setLoadBalancePolicy(policy: (name: string) => RPCKeepAliveConnection): void;
|
|
51
|
+
export declare function setLoadBalancePolicy(policy: (name: string) => RPCKeepAliveConnection<any>): void;
|
|
77
52
|
export declare function rpc(appName: string, action: string, params: any[], context?: Context): Promise<any>;
|
|
78
|
-
export declare function rpc(connection: RPCKeepAliveConnection
|
|
53
|
+
export declare function rpc(connection: RPCKeepAliveConnection<any>, action: string, params: any[], context?: Context): Promise<any>;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import * as http from 'node:http';
|
|
2
|
+
import Router, { Middleware } from './router.js';
|
|
2
3
|
import * as oox from '../../index.js';
|
|
3
4
|
import Module, { ModuleConfig } from '../module.js';
|
|
4
5
|
export declare class HTTPConfig extends ModuleConfig {
|
|
5
6
|
port: number;
|
|
6
7
|
path: string;
|
|
7
|
-
origin: string;
|
|
8
|
+
origin: string | string[];
|
|
8
9
|
ssl: {
|
|
9
10
|
enabled: boolean;
|
|
10
11
|
cert: string;
|
|
@@ -16,7 +17,13 @@ export default class HTTPModule extends Module {
|
|
|
16
17
|
name: string;
|
|
17
18
|
config: HTTPConfig;
|
|
18
19
|
server: http.Server;
|
|
19
|
-
|
|
20
|
+
router: Router;
|
|
21
|
+
getURL(): URL;
|
|
22
|
+
get(path: string, ...args: any[]): this;
|
|
23
|
+
post(path: string, ...args: any[]): this;
|
|
24
|
+
put(path: string, ...args: any[]): this;
|
|
25
|
+
delete(path: string, ...args: any[]): this;
|
|
26
|
+
use(middleware: Middleware): this;
|
|
20
27
|
setConfig(config: HTTPConfig): void;
|
|
21
28
|
getConfig(): HTTPConfig;
|
|
22
29
|
/**
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import * as http from 'node:http';
|
|
2
|
+
export type RouteHandler = (req: Request, res: Response) => Promise<any> | any;
|
|
3
|
+
export type Middleware = (req: Request, res: Response, next: () => void) => Promise<any> | any;
|
|
4
|
+
export interface Route {
|
|
5
|
+
path: string;
|
|
6
|
+
method: string;
|
|
7
|
+
handler: RouteHandler;
|
|
8
|
+
middleware: Middleware[];
|
|
9
|
+
regex: RegExp;
|
|
10
|
+
paramNames: string[];
|
|
11
|
+
}
|
|
12
|
+
export interface Request {
|
|
13
|
+
originalRequest: http.IncomingMessage;
|
|
14
|
+
url: URL;
|
|
15
|
+
method: string;
|
|
16
|
+
path: string;
|
|
17
|
+
query: Record<string, string>;
|
|
18
|
+
params: Record<string, string>;
|
|
19
|
+
headers: Record<string, string>;
|
|
20
|
+
body: any;
|
|
21
|
+
}
|
|
22
|
+
export interface Response {
|
|
23
|
+
originalResponse: http.ServerResponse;
|
|
24
|
+
status(code: number): Response;
|
|
25
|
+
json(data: any): void;
|
|
26
|
+
send(data: any): void;
|
|
27
|
+
end(data?: any): void;
|
|
28
|
+
}
|
|
29
|
+
export default class Router {
|
|
30
|
+
private routes;
|
|
31
|
+
private globalMiddleware;
|
|
32
|
+
get(path: string, ...args: any[]): void;
|
|
33
|
+
post(path: string, ...args: any[]): void;
|
|
34
|
+
put(path: string, ...args: any[]): void;
|
|
35
|
+
delete(path: string, ...args: any[]): void;
|
|
36
|
+
private parseRouteArgs;
|
|
37
|
+
use(middleware: Middleware): void;
|
|
38
|
+
usePath(path: string, middleware: Middleware): void;
|
|
39
|
+
private addRoute;
|
|
40
|
+
private compilePath;
|
|
41
|
+
match(method: string, path: string): {
|
|
42
|
+
route: Route;
|
|
43
|
+
params: Record<string, string>;
|
|
44
|
+
} | null;
|
|
45
|
+
createRequestObject(originalRequest: http.IncomingMessage, url: URL): Request;
|
|
46
|
+
createResponseObject(originalResponse: http.ServerResponse): Response;
|
|
47
|
+
private executeMiddleware;
|
|
48
|
+
handleRoute(route: Route, params: Record<string, string>, req: Request, res: Response): Promise<void>;
|
|
49
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import * as oox from '../../index.js';
|
|
2
|
+
import { Socket } from './socket.js';
|
|
3
|
+
import { OOXEvent } from './utils.js';
|
|
4
|
+
export default class SocketIOAdapter implements oox.RPCKeepAliveConnectionAdapter<Socket> {
|
|
5
|
+
/**
|
|
6
|
+
* 客户端发送连接列表
|
|
7
|
+
* @param datas
|
|
8
|
+
*/
|
|
9
|
+
[OOXEvent.SYNC_CONNECTIONS](datas: oox.RPCKeepAliveConnectionData[]): void;
|
|
10
|
+
bindClientConnectionEvents(connection: oox.RPCKeepAliveConnection<Socket<'client'>>): void;
|
|
11
|
+
newConnection(url: string | URL): oox.RPCKeepAliveConnection<Socket>;
|
|
12
|
+
open(url: string | URL): Promise<oox.RPCKeepAliveConnection<Socket>>;
|
|
13
|
+
close(connection: oox.RPCKeepAliveConnection<Socket>): Promise<void>;
|
|
14
|
+
/**
|
|
15
|
+
* socketio emit
|
|
16
|
+
*/
|
|
17
|
+
emit(socket: Socket, event: string, params: any[]): Promise<unknown>;
|
|
18
|
+
/**
|
|
19
|
+
* RPC
|
|
20
|
+
*/
|
|
21
|
+
rpc(connection: oox.RPCKeepAliveConnection<Socket>, action: string, params: [], context?: oox.Context): Promise<any>;
|
|
22
|
+
/**
|
|
23
|
+
* 绑定 call 事件
|
|
24
|
+
* @param connection
|
|
25
|
+
*/
|
|
26
|
+
bindCall(connection: oox.RPCKeepAliveConnection<Socket>): void;
|
|
27
|
+
call(action: string, params: any[], context: oox.Context, callback?: (returns: any) => void): Promise<oox.ReturnsBody>;
|
|
28
|
+
}
|
|
@@ -1,37 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import { RPCKeepAliveConnectionData, RPCConnectionAdapter } from '../../index.js';
|
|
4
|
-
import { Socket, sockets, ServerSocket, ClientSocket } from './socket.js';
|
|
5
|
-
export { Socket, sockets };
|
|
6
|
-
export default class SocketIOModule extends SocketIOClient implements RPCConnectionAdapter {
|
|
7
|
-
sockets: Map<string, Socket>;
|
|
1
|
+
import SocketINServer from './server.js';
|
|
2
|
+
export default class SocketIOModule extends SocketINServer {
|
|
8
3
|
serve(): Promise<void>;
|
|
9
|
-
onSyncConnection(socket: Socket): oox.RPCKeepAliveConnectionData[];
|
|
10
|
-
serverOnDisconnect(socket: ServerSocket, reason: string): void;
|
|
11
|
-
clientOnDisconnect(socket: ClientSocket, reason: any): void;
|
|
12
|
-
/**
|
|
13
|
-
*
|
|
14
|
-
* @param socket 是由哪个通道发送过来的
|
|
15
|
-
* @param connectionDatas
|
|
16
|
-
*/
|
|
17
|
-
clientOnSyncConnection(socket: Socket, connectionDatas: RPCKeepAliveConnectionData[]): void;
|
|
18
|
-
onFetchActions(socket: Socket, search: string): string[];
|
|
19
|
-
fetchActions(url: string, search?: string): Promise<RPCKeepAliveConnectionData[]>;
|
|
20
|
-
fetchActions(name: string, search?: string): Promise<RPCKeepAliveConnectionData[]>;
|
|
21
|
-
onConnection(socket: Socket): void;
|
|
22
|
-
/**
|
|
23
|
-
*
|
|
24
|
-
* @param {Socket} socket
|
|
25
|
-
*/
|
|
26
|
-
serverOnConnection(socket: ServerSocket): void;
|
|
27
|
-
call(action: string, params: any[], context: oox.Context, callback?: (returns: any) => void): Promise<oox.ReturnsBody>;
|
|
28
|
-
clientOnConnection(socket: ClientSocket): void;
|
|
29
|
-
/**
|
|
30
|
-
* socketio emit
|
|
31
|
-
*/
|
|
32
|
-
emit(url: string, event: string, params: any[]): Promise<unknown>;
|
|
33
|
-
/**
|
|
34
|
-
* RPC
|
|
35
|
-
*/
|
|
36
|
-
rpc(url: string, action: string, params: [], context?: oox.Context): Promise<any>;
|
|
37
4
|
}
|