oox 0.3.2 → 0.3.4
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/cli.js +40 -13
- package/bin/configurer.js +3 -1
- package/bin/register.js +9 -23
- package/bin/starter.js +2 -2
- package/index.js +32 -48
- package/keepalive-connection.js +96 -0
- package/modules/http/index.js +2 -2
- package/modules/socketio/adapter.js +37 -0
- package/modules/socketio/index.js +4 -152
- package/modules/socketio/server.js +34 -40
- package/modules/socketio/socket.js +1 -1
- package/modules/socketio/utils.js +39 -0
- package/package.json +4 -2
- package/registry.js +61 -0
- package/samples/index.js +1 -0
- package/samples/keepalive-connection-sample.js +130 -0
- package/types/app.d.ts +1 -1
- package/types/index.d.ts +19 -41
- package/types/keepalive-connection.d.ts +79 -0
- package/types/modules/http/index.d.ts +2 -2
- package/types/modules/socketio/adapter.d.ts +14 -0
- package/types/modules/socketio/index.d.ts +2 -35
- package/types/modules/socketio/server.d.ts +10 -12
- package/types/modules/socketio/socket.d.ts +8 -11
- package/types/modules/socketio/utils.d.ts +8 -0
- package/types/registry.d.ts +16 -0
- package/types/samples/index.d.ts +1 -0
- package/types/samples/keepalive-connection-sample.d.ts +46 -0
- package/modules/socketio/client.js +0 -97
- package/types/modules/socketio/client.d.ts +0 -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 { 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.keepAliveConnectionAdapters.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,39 @@ 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.KeepAliveConnection(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.READY, {
|
|
161
|
+
id: oox.config.id,
|
|
162
|
+
name: oox.config.name
|
|
163
|
+
});
|
|
175
164
|
}
|
|
176
|
-
serverOnConnection(socket) { }
|
|
177
165
|
/**
|
|
178
|
-
*
|
|
179
|
-
* @param
|
|
180
|
-
* @param {Error} reason
|
|
166
|
+
* 绑定服务器连接事件
|
|
167
|
+
* @param connection
|
|
181
168
|
*/
|
|
182
|
-
|
|
183
|
-
socket
|
|
184
|
-
|
|
185
|
-
|
|
169
|
+
bindServerConnectionEvents(connection) {
|
|
170
|
+
const socket = connection.nativeConnection;
|
|
171
|
+
socket.on(OOXEvent.SYNC_CONNECTIONS, async (fn) => {
|
|
172
|
+
if ('function' !== typeof fn)
|
|
173
|
+
return;
|
|
174
|
+
oox.registry.onSyncConnections(connection, {}, fn);
|
|
175
|
+
});
|
|
176
|
+
socket.on('disconnecting', (reason) => {
|
|
177
|
+
oox.removeKeepAliveConnection(connection);
|
|
178
|
+
socket.removeAllListeners();
|
|
179
|
+
connection.emit('disconnect');
|
|
180
|
+
});
|
|
186
181
|
}
|
|
187
|
-
serverOnDisconnect(socket, reason) { }
|
|
188
182
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export var OOXEvent;
|
|
2
|
+
(function (OOXEvent) {
|
|
3
|
+
OOXEvent["READY"] = "oox:ready";
|
|
4
|
+
OOXEvent["ENABLED"] = "oox:enabled";
|
|
5
|
+
OOXEvent["DISABLED"] = "oox:disabled";
|
|
6
|
+
OOXEvent["SYNC_CONNECTIONS"] = "oox:sync_connections";
|
|
7
|
+
})(OOXEvent || (OOXEvent = {}));
|
|
8
|
+
export function isWebSocketURL(url) {
|
|
9
|
+
if ('string' === typeof url) {
|
|
10
|
+
return /^wss?:\/\/.+$/.test(url);
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
return url.protocol === 'ws:' || url.protocol === 'wss:';
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export function genWebSocketURL(url) {
|
|
17
|
+
// :6000
|
|
18
|
+
if (url.startsWith(':'))
|
|
19
|
+
url = 'ws://localhost' + url;
|
|
20
|
+
// 127.0.0.1:6000
|
|
21
|
+
if (!/^\w+?:\/.+$/.test(url))
|
|
22
|
+
url = 'ws://' + url;
|
|
23
|
+
const urlObject = new URL(url);
|
|
24
|
+
// :8000 => :8000/socket.io
|
|
25
|
+
const notSetPath = !urlObject.pathname || (urlObject.pathname === '/'
|
|
26
|
+
&& !url.endsWith('/')
|
|
27
|
+
&& !urlObject.search
|
|
28
|
+
&& !urlObject.hash);
|
|
29
|
+
if (notSetPath) {
|
|
30
|
+
urlObject.pathname = '/socket.io';
|
|
31
|
+
}
|
|
32
|
+
if (urlObject.protocol !== 'wss:') {
|
|
33
|
+
if (urlObject.port === '443')
|
|
34
|
+
urlObject.protocol = 'wss:';
|
|
35
|
+
else
|
|
36
|
+
urlObject.protocol = 'ws:';
|
|
37
|
+
}
|
|
38
|
+
return urlObject;
|
|
39
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "oox",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.4",
|
|
4
4
|
"description": "OOX Service Engine",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"http",
|
|
@@ -22,7 +22,9 @@
|
|
|
22
22
|
"types": "./types/index.d.ts"
|
|
23
23
|
},
|
|
24
24
|
"./loader": "./bin/loader.mjs",
|
|
25
|
-
"./cli": "./bin/cli.js"
|
|
25
|
+
"./cli": "./bin/cli.js",
|
|
26
|
+
"./samples": "./samples/index.js",
|
|
27
|
+
"./samples/types": "./types/samples/index.d.ts"
|
|
26
28
|
},
|
|
27
29
|
"bin": {
|
|
28
30
|
"oox": "./bin/cli.js"
|
package/registry.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { config } from './index.js';
|
|
2
|
+
import { enabledKeepAliveConnections, keepAliveConnectionAdapters, keepAliveConnections } from './keepalive-connection.js';
|
|
3
|
+
/**
|
|
4
|
+
* 服务器发送连接列表
|
|
5
|
+
* @param connection
|
|
6
|
+
* @param query
|
|
7
|
+
* @param callback
|
|
8
|
+
*/
|
|
9
|
+
export function onSyncConnections(connection, query, callback) {
|
|
10
|
+
// 检查是否开启服务注册功能
|
|
11
|
+
if (!config.isRegistry)
|
|
12
|
+
return callback([]);
|
|
13
|
+
const datas = [];
|
|
14
|
+
if (query.name) {
|
|
15
|
+
const names = Array.isArray(query.name) ? query.name : [query.name];
|
|
16
|
+
for (const [name, connections] of enabledKeepAliveConnections.entries()) {
|
|
17
|
+
if (names.includes(name))
|
|
18
|
+
continue;
|
|
19
|
+
for (const conn of Array.from(connections.values())) {
|
|
20
|
+
if (!conn.data.url)
|
|
21
|
+
continue;
|
|
22
|
+
datas.push(conn.data);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
for (const [name, connections] of enabledKeepAliveConnections.entries()) {
|
|
28
|
+
if (name === connection.data.name)
|
|
29
|
+
continue;
|
|
30
|
+
for (const conn of Array.from(connections.values())) {
|
|
31
|
+
if (!conn.data.url)
|
|
32
|
+
continue;
|
|
33
|
+
datas.push(conn.data);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
callback(datas);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* 同步连接列表
|
|
41
|
+
* @param datas 连接参数列表
|
|
42
|
+
*/
|
|
43
|
+
export function syncConnections(datas) {
|
|
44
|
+
for (const data of datas) {
|
|
45
|
+
// 获取适配器
|
|
46
|
+
const adapter = keepAliveConnectionAdapters.get(data.adapter);
|
|
47
|
+
if (!adapter)
|
|
48
|
+
continue;
|
|
49
|
+
// 忽略自己
|
|
50
|
+
if (data.name === config.name)
|
|
51
|
+
continue;
|
|
52
|
+
// 忽略无效URL
|
|
53
|
+
if (!data.url)
|
|
54
|
+
continue;
|
|
55
|
+
// 忽略已存在连接
|
|
56
|
+
if (keepAliveConnections.has(data.name, data.id))
|
|
57
|
+
continue;
|
|
58
|
+
// 建立连接
|
|
59
|
+
adapter.open(data.url).catch((error) => console.error(error));
|
|
60
|
+
}
|
|
61
|
+
}
|
package/samples/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { SampleKeepAliveConnectionAdapter } from './keepalive-connection-sample.js';
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import * as oox from '../index.js';
|
|
2
|
+
export class SampleKeepAliveConnectionAdapter {
|
|
3
|
+
name;
|
|
4
|
+
OOXEvent = {
|
|
5
|
+
READY: 'oox:ready',
|
|
6
|
+
ENABLED: 'oox:enabled',
|
|
7
|
+
DISABLED: 'oox:disabled',
|
|
8
|
+
SYNC_CONNECTIONS: 'oox:sync_connections',
|
|
9
|
+
};
|
|
10
|
+
nativeEvent = {
|
|
11
|
+
CONNECT: 'connect',
|
|
12
|
+
DISCONNECT: 'disconnect',
|
|
13
|
+
ERROR: 'error',
|
|
14
|
+
};
|
|
15
|
+
bindConnectionEvents(connection) {
|
|
16
|
+
const socket = connection.nativeConnection;
|
|
17
|
+
socket.on(this.OOXEvent.READY, (params) => {
|
|
18
|
+
connection.ready(params);
|
|
19
|
+
socket.emit(this.OOXEvent.SYNC_CONNECTIONS, oox.registry.syncConnections);
|
|
20
|
+
});
|
|
21
|
+
socket.on(this.OOXEvent.ENABLED, () => {
|
|
22
|
+
connection.enabled = true;
|
|
23
|
+
});
|
|
24
|
+
socket.on(this.OOXEvent.DISABLED, () => {
|
|
25
|
+
connection.enabled = false;
|
|
26
|
+
});
|
|
27
|
+
socket.on(this.nativeEvent.DISCONNECT, (reason) => {
|
|
28
|
+
connection.emit('disconnect');
|
|
29
|
+
oox.removeKeepAliveConnection(connection);
|
|
30
|
+
socket.removeAllListeners();
|
|
31
|
+
socket.disconnect();
|
|
32
|
+
});
|
|
33
|
+
socket.on(this.nativeEvent.CONNECT, () => {
|
|
34
|
+
connection.emit('connect');
|
|
35
|
+
});
|
|
36
|
+
socket.on(this.nativeEvent.ERROR, (error) => {
|
|
37
|
+
connection.emit('error', error);
|
|
38
|
+
oox.removeKeepAliveConnection(connection);
|
|
39
|
+
socket.removeAllListeners();
|
|
40
|
+
socket.disconnect();
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
async open(url) {
|
|
44
|
+
const connection = this.newConnection(url);
|
|
45
|
+
oox.addKeepAliveConnection(connection);
|
|
46
|
+
this.bindConnectionEvents(connection);
|
|
47
|
+
this.bindCall(connection);
|
|
48
|
+
await new Promise((resolve, reject) => {
|
|
49
|
+
connection.once('enabled', resolve);
|
|
50
|
+
connection.once('error', reject);
|
|
51
|
+
connection.once('disconnect', () => {
|
|
52
|
+
reject(new Error('disconnect'));
|
|
53
|
+
});
|
|
54
|
+
if (connection.nativeConnection.connect) {
|
|
55
|
+
connection.nativeConnection.connect();
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
return connection;
|
|
59
|
+
}
|
|
60
|
+
async close(connection) {
|
|
61
|
+
connection.nativeConnection.disconnect();
|
|
62
|
+
return Promise.resolve();
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* 发送事件
|
|
66
|
+
* @param socket
|
|
67
|
+
* @param event
|
|
68
|
+
* @param params
|
|
69
|
+
*/
|
|
70
|
+
async emit(socket, event, params) {
|
|
71
|
+
if (!socket.connected)
|
|
72
|
+
throw new Error('Socket not connected');
|
|
73
|
+
return await new Promise((resolve, reject) => {
|
|
74
|
+
const onError = (reason) => {
|
|
75
|
+
const message = 'string' === typeof reason ? reason : reason instanceof Error ? reason.message : 'connect error';
|
|
76
|
+
reject(new Error(message));
|
|
77
|
+
};
|
|
78
|
+
// RPC 执行时中断连接
|
|
79
|
+
socket.once(this.nativeEvent.DISCONNECT, onError);
|
|
80
|
+
socket.emit(event, ...params, (returns) => {
|
|
81
|
+
socket.off(this.nativeEvent.DISCONNECT, onError);
|
|
82
|
+
resolve(returns);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* RPC
|
|
88
|
+
*/
|
|
89
|
+
async rpc(connection, action, params, context) {
|
|
90
|
+
if (!context)
|
|
91
|
+
context = oox.getContext();
|
|
92
|
+
const { success, error, body } = await this.emit(connection.nativeConnection, 'call', [action, params, context]);
|
|
93
|
+
if (success)
|
|
94
|
+
return body;
|
|
95
|
+
else if (error)
|
|
96
|
+
throw new Error(error.message);
|
|
97
|
+
else
|
|
98
|
+
throw new Error('[RPC] Unknown Error');
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* 绑定 call 事件
|
|
102
|
+
* @param connection
|
|
103
|
+
*/
|
|
104
|
+
bindCall(connection) {
|
|
105
|
+
const { id, name, host } = connection.data;
|
|
106
|
+
const connectionContext = {
|
|
107
|
+
sourceIP: '',
|
|
108
|
+
ip: host,
|
|
109
|
+
caller: name,
|
|
110
|
+
callerId: id,
|
|
111
|
+
connection
|
|
112
|
+
};
|
|
113
|
+
connection.nativeConnection.on('call', async (action, params, context, callback) => {
|
|
114
|
+
if ('object' !== typeof context)
|
|
115
|
+
context = oox.genContext(connectionContext);
|
|
116
|
+
else
|
|
117
|
+
context = oox.genContext(Object.assign(context, connectionContext));
|
|
118
|
+
this.call(action, params, context, callback);
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
async call(action, params, context, callback) {
|
|
122
|
+
const returns = await oox.call(action, params, context);
|
|
123
|
+
if (returns.error && !oox.config.errorStack) {
|
|
124
|
+
// 不返回错误调用栈信息
|
|
125
|
+
delete returns.error.stack;
|
|
126
|
+
}
|
|
127
|
+
'function' === typeof callback && callback(returns);
|
|
128
|
+
return returns;
|
|
129
|
+
}
|
|
130
|
+
}
|
package/types/app.d.ts
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
1
|
import * as app from './app.js';
|
|
2
|
+
import * as registry from './registry.js';
|
|
2
3
|
import Module, { ModuleConfig } from './modules/module.js';
|
|
3
|
-
import
|
|
4
|
-
export { ReturnsBody } from './app.js';
|
|
5
|
-
export { Module, ModuleConfig };
|
|
6
|
-
export declare const modules: Modules;
|
|
7
|
-
export declare const asyncStore: import("async_hooks").AsyncLocalStorage<app.Context>, setMethods: typeof app.setMethods, getMethods: typeof app.getMethods, kvMethods: Map<string, Function>, sourceKVMethods: Map<string, Function>, call: typeof app.call, execute: typeof app.execute, logger: typeof app.logger, on: typeof app.on, once: typeof app.once, off: typeof app.off, emit: typeof app.emit;
|
|
4
|
+
import { KeepAliveConnectionReadyParams, KeepAliveConnectionAdapter, KeepAliveConnection, KeepAliveConnectionData, keepAliveConnectionAdapters, keepAliveConnections, enabledKeepAliveConnections } from './keepalive-connection.js';
|
|
8
5
|
export declare class Context extends app.Context {
|
|
9
6
|
sourceIP: string;
|
|
10
7
|
ip: string;
|
|
11
8
|
caller: string;
|
|
12
9
|
callerId: string;
|
|
13
|
-
connection?:
|
|
10
|
+
connection?: KeepAliveConnection<any>;
|
|
14
11
|
toJSON?(): {} & this;
|
|
15
12
|
}
|
|
16
13
|
export declare class Config {
|
|
17
14
|
[x: string]: any;
|
|
15
|
+
id: `${string}-${string}-${string}-${string}-${string}`;
|
|
18
16
|
name: string;
|
|
19
17
|
group: string;
|
|
20
18
|
ignore: string[];
|
|
@@ -24,8 +22,11 @@ export declare class Config {
|
|
|
24
22
|
};
|
|
25
23
|
host: string;
|
|
26
24
|
port: number;
|
|
27
|
-
origin: string;
|
|
25
|
+
origin: string | string[];
|
|
28
26
|
errorStack: boolean;
|
|
27
|
+
isRegistry: boolean;
|
|
28
|
+
registry: string[];
|
|
29
|
+
registryAdapter: string;
|
|
29
30
|
}
|
|
30
31
|
export declare const config: Config;
|
|
31
32
|
export declare function setGenTraceIdFunction(fn: () => string): void;
|
|
@@ -40,39 +41,16 @@ export declare function genContext(context?: Context): Context;
|
|
|
40
41
|
export declare function getContext(): Context;
|
|
41
42
|
export declare function serve(): Promise<void>;
|
|
42
43
|
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;
|
|
44
|
+
export declare function addKeepAliveConnection(connection: KeepAliveConnection<any>): void;
|
|
45
|
+
export declare function removeKeepAliveConnection(connection: KeepAliveConnection<any>): void;
|
|
75
46
|
export declare function removeKeepAliveConnection(name: string, id: string): void;
|
|
76
|
-
export declare function setLoadBalancePolicy(policy: (name: string) =>
|
|
47
|
+
export declare function setLoadBalancePolicy(policy: (name: string) => KeepAliveConnection<any>): void;
|
|
77
48
|
export declare function rpc(appName: string, action: string, params: any[], context?: Context): Promise<any>;
|
|
78
|
-
export declare function rpc(connection:
|
|
49
|
+
export declare function rpc(connection: KeepAliveConnection<any>, action: string, params: any[], context?: Context): Promise<any>;
|
|
50
|
+
export { KeepAliveConnectionReadyParams, KeepAliveConnectionAdapter, KeepAliveConnection, KeepAliveConnectionData, keepAliveConnectionAdapters, keepAliveConnections, enabledKeepAliveConnections };
|
|
51
|
+
export { ReturnsBody } from './app.js';
|
|
52
|
+
export { Module, ModuleConfig };
|
|
53
|
+
export { registry };
|
|
54
|
+
export declare const asyncStore: import("async_hooks").AsyncLocalStorage<app.Context>, setMethods: typeof app.setMethods, getMethods: typeof app.getMethods, kvMethods: Map<string, Function>, sourceKVMethods: Map<string, Function>, call: typeof app.call, execute: typeof app.execute, logger: typeof app.logger, on: typeof app.on, once: typeof app.once, off: typeof app.off, emit: typeof app.emit;
|
|
55
|
+
import Modules from './modules/index.js';
|
|
56
|
+
export declare const modules: Modules;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import EventEmitter from 'node:events';
|
|
2
|
+
import { Context } from './index.js';
|
|
3
|
+
type MakeOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
4
|
+
export interface KeepAliveConnectionAdapter<T> {
|
|
5
|
+
/**
|
|
6
|
+
* 连接适配器名称
|
|
7
|
+
*/
|
|
8
|
+
name: string;
|
|
9
|
+
open(url: string | URL): Promise<KeepAliveConnection<T>>;
|
|
10
|
+
close(connection: KeepAliveConnection<T>): Promise<void>;
|
|
11
|
+
rpc(connection: KeepAliveConnection<T>, action: string, params: any[], context: Context): Promise<any>;
|
|
12
|
+
}
|
|
13
|
+
export interface KeepAliveConnectionData {
|
|
14
|
+
/**
|
|
15
|
+
* 连接适配器名称
|
|
16
|
+
*/
|
|
17
|
+
adapter: string;
|
|
18
|
+
/**
|
|
19
|
+
* 连接主机地址
|
|
20
|
+
*/
|
|
21
|
+
host: string;
|
|
22
|
+
/**
|
|
23
|
+
* 连接服务名称
|
|
24
|
+
*/
|
|
25
|
+
name: string;
|
|
26
|
+
/**
|
|
27
|
+
* 连接URL
|
|
28
|
+
*/
|
|
29
|
+
url?: string | URL;
|
|
30
|
+
/**
|
|
31
|
+
* 目标服务网络唯一ID
|
|
32
|
+
*/
|
|
33
|
+
id: string;
|
|
34
|
+
}
|
|
35
|
+
export interface KeepAliveConnectionReadyParams {
|
|
36
|
+
[x: string]: any;
|
|
37
|
+
id: KeepAliveConnectionData['id'];
|
|
38
|
+
name: KeepAliveConnectionData['name'];
|
|
39
|
+
}
|
|
40
|
+
export type KeepAliveConnectionEventMap = Record<keyof {
|
|
41
|
+
"enabled": any;
|
|
42
|
+
"disabled": any;
|
|
43
|
+
"connect": any;
|
|
44
|
+
"disconnect": any;
|
|
45
|
+
'error': any;
|
|
46
|
+
}, any[]>;
|
|
47
|
+
export declare class KeepAliveConnection<T> extends EventEmitter<KeepAliveConnectionEventMap> {
|
|
48
|
+
#private;
|
|
49
|
+
data: KeepAliveConnectionData;
|
|
50
|
+
nativeConnection: T;
|
|
51
|
+
adapter: KeepAliveConnectionAdapter<T>;
|
|
52
|
+
constructor(adapter: KeepAliveConnectionAdapter<T>, nativeConnection: T, data: MakeOptional<KeepAliveConnectionData, 'adapter'>);
|
|
53
|
+
/**
|
|
54
|
+
* 设置连接是否可用,并自动触发enabled或disabled事件
|
|
55
|
+
* @param enabled 是否可用
|
|
56
|
+
*/
|
|
57
|
+
set enabled(enabled: boolean);
|
|
58
|
+
get enabled(): boolean;
|
|
59
|
+
/**
|
|
60
|
+
* enable & mount connection
|
|
61
|
+
* @param params
|
|
62
|
+
* @returns
|
|
63
|
+
*/
|
|
64
|
+
ready(params: KeepAliveConnectionReadyParams): void;
|
|
65
|
+
}
|
|
66
|
+
export declare class KeepAliveConnectionStore<T> {
|
|
67
|
+
#private;
|
|
68
|
+
entries(): MapIterator<[string, Map<string, KeepAliveConnection<T>>]>;
|
|
69
|
+
getConnectionsOfService(name: string): Map<string, KeepAliveConnection<T>>;
|
|
70
|
+
has(name: string, id: string): boolean;
|
|
71
|
+
get(name: string, id: string): KeepAliveConnection<T> | null;
|
|
72
|
+
add(connection: KeepAliveConnection<T>): void;
|
|
73
|
+
remove(connection: KeepAliveConnection<T>): void;
|
|
74
|
+
remove(name: string, id: string): void;
|
|
75
|
+
}
|
|
76
|
+
export declare const keepAliveConnectionAdapters: Map<string, KeepAliveConnectionAdapter<any>>;
|
|
77
|
+
export declare const keepAliveConnections: KeepAliveConnectionStore<any>;
|
|
78
|
+
export declare const enabledKeepAliveConnections: KeepAliveConnectionStore<any>;
|
|
79
|
+
export {};
|
|
@@ -5,7 +5,7 @@ import Module, { ModuleConfig } from '../module.js';
|
|
|
5
5
|
export declare class HTTPConfig extends ModuleConfig {
|
|
6
6
|
port: number;
|
|
7
7
|
path: string;
|
|
8
|
-
origin: string;
|
|
8
|
+
origin: string | string[];
|
|
9
9
|
ssl: {
|
|
10
10
|
enabled: boolean;
|
|
11
11
|
cert: string;
|
|
@@ -18,7 +18,7 @@ export default class HTTPModule extends Module {
|
|
|
18
18
|
config: HTTPConfig;
|
|
19
19
|
server: http.Server;
|
|
20
20
|
router: Router;
|
|
21
|
-
|
|
21
|
+
getURL(): URL;
|
|
22
22
|
get(path: string, ...args: any[]): this;
|
|
23
23
|
post(path: string, ...args: any[]): this;
|
|
24
24
|
put(path: string, ...args: any[]): this;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import * as oox from '../../index.js';
|
|
2
|
+
import { Socket } from './socket.js';
|
|
3
|
+
import { OOXEvent } from './utils.js';
|
|
4
|
+
import { SampleKeepAliveConnectionAdapter } from '../../samples/index.js';
|
|
5
|
+
export default class SocketIOAdapter extends SampleKeepAliveConnectionAdapter<Socket> {
|
|
6
|
+
name: string;
|
|
7
|
+
OOXEvent: typeof OOXEvent;
|
|
8
|
+
nativeEvent: {
|
|
9
|
+
CONNECT: string;
|
|
10
|
+
DISCONNECT: string;
|
|
11
|
+
ERROR: string;
|
|
12
|
+
};
|
|
13
|
+
newConnection(url: string | URL): oox.KeepAliveConnection<Socket>;
|
|
14
|
+
}
|
|
@@ -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
|
}
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import * as http from 'node:http';
|
|
2
2
|
import * as https from 'node:https';
|
|
3
3
|
import { Server, ServerOptions } from 'socket.io';
|
|
4
|
+
import * as oox from '../../index.js';
|
|
4
5
|
import { Module, ModuleConfig } from '../../index.js';
|
|
5
|
-
import {
|
|
6
|
+
import { Socket } from './socket.js';
|
|
7
|
+
import SocketIOAdapter from './adapter.js';
|
|
6
8
|
export declare class SocketIOConfig extends ModuleConfig {
|
|
7
9
|
port: number;
|
|
8
10
|
path: string;
|
|
9
|
-
origin: string;
|
|
11
|
+
origin: string | string[];
|
|
10
12
|
ssl: {
|
|
11
13
|
enabled: boolean;
|
|
12
14
|
cert: string;
|
|
@@ -20,9 +22,8 @@ export default class SocketIOServer extends Module {
|
|
|
20
22
|
config: SocketIOConfig;
|
|
21
23
|
server: http.Server | https.Server;
|
|
22
24
|
socketServer: Server;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
getUrl(): string;
|
|
25
|
+
adapter: SocketIOAdapter;
|
|
26
|
+
getURL(): URL;
|
|
26
27
|
setConfig(config: SocketIOConfig): void;
|
|
27
28
|
getConfig(): SocketIOConfig;
|
|
28
29
|
serve(): Promise<void>;
|
|
@@ -32,13 +33,10 @@ export default class SocketIOServer extends Module {
|
|
|
32
33
|
/**
|
|
33
34
|
* 服务端Socket连接事件
|
|
34
35
|
*/
|
|
35
|
-
serverOnSocketConnection(socket: Socket): void;
|
|
36
|
-
serverOnConnection(socket: Socket): void;
|
|
36
|
+
serverOnSocketConnection(socket: Socket<'server'>): void;
|
|
37
37
|
/**
|
|
38
|
-
*
|
|
39
|
-
* @param
|
|
40
|
-
* @param {Error} reason
|
|
38
|
+
* 绑定服务器连接事件
|
|
39
|
+
* @param connection
|
|
41
40
|
*/
|
|
42
|
-
|
|
43
|
-
serverOnDisconnect(socket: Socket, reason: string): void;
|
|
41
|
+
bindServerConnectionEvents(connection: oox.KeepAliveConnection<Socket<'server'>>): void;
|
|
44
42
|
}
|