oox 0.3.3 → 0.3.5
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/README.md +13 -0
- package/app.js +1 -1
- package/bin/cli.js +75 -15
- package/bin/configurer.js +6 -9
- package/bin/register.js +48 -15
- package/bin/starter.js +8 -0
- package/config.js +116 -0
- package/index.js +70 -44
- package/{rpc-keepalive-connection.js → keepalive-connection.js} +36 -16
- package/modules/http/index.js +52 -63
- package/modules/http/router.js +9 -4
- package/modules/index.js +78 -74
- package/modules/module.js +0 -9
- package/modules/socketio/adapter.js +30 -139
- package/modules/socketio/server.js +37 -38
- package/modules/socketio/utils.js +5 -2
- package/package.json +10 -4
- package/registry.js +145 -0
- package/samples/index.js +1 -0
- package/samples/keepalive-connection-sample.js +147 -0
- package/types/app.d.ts +20 -20
- package/types/bin/configurer.d.ts +3 -1
- package/types/bin/register.d.ts +2 -1
- package/types/bin/starter.d.ts +1 -2
- package/types/config.d.ts +54 -0
- package/types/index.d.ts +37 -31
- package/types/keepalive-connection.d.ts +104 -0
- package/types/modules/http/index.d.ts +4 -7
- package/types/modules/index.d.ts +36 -21
- package/types/modules/module.d.ts +9 -9
- package/types/modules/socketio/adapter.d.ts +10 -24
- package/types/modules/socketio/server.d.ts +6 -10
- package/types/modules/socketio/utils.d.ts +5 -2
- package/types/registry.d.ts +41 -0
- package/types/samples/index.d.ts +1 -0
- package/types/samples/keepalive-connection-sample.d.ts +55 -0
- package/utils.js +1 -1
- package/types/rpc-keepalive-connection.d.ts +0 -57
package/types/index.d.ts
CHANGED
|
@@ -1,37 +1,21 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { AppContext } from './app.js';
|
|
2
|
+
import * as registry from './registry.js';
|
|
3
|
+
import { Config } from './config.js';
|
|
2
4
|
import Module, { ModuleConfig } from './modules/module.js';
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export declare
|
|
9
|
-
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;
|
|
10
|
-
export declare class Context extends app.Context {
|
|
5
|
+
import { KeepAliveConnection, KeepAliveNativeConnection } from './keepalive-connection.js';
|
|
6
|
+
export interface KeepAliveRPCContext {
|
|
7
|
+
traceId?: string;
|
|
8
|
+
sourceIP?: string;
|
|
9
|
+
}
|
|
10
|
+
export declare class Context extends AppContext {
|
|
11
11
|
sourceIP: string;
|
|
12
12
|
ip: string;
|
|
13
13
|
caller: string;
|
|
14
14
|
callerId: string;
|
|
15
|
-
connection?:
|
|
15
|
+
connection?: KeepAliveConnection<KeepAliveNativeConnection>;
|
|
16
|
+
constructor(context?: Partial<Context>);
|
|
16
17
|
toJSON?(): {} & this;
|
|
17
18
|
}
|
|
18
|
-
export declare class Config {
|
|
19
|
-
[x: string]: any;
|
|
20
|
-
id: `${string}-${string}-${string}-${string}-${string}`;
|
|
21
|
-
name: string;
|
|
22
|
-
group: string;
|
|
23
|
-
ignore: string[];
|
|
24
|
-
entryInfo: {
|
|
25
|
-
path: string;
|
|
26
|
-
group: string;
|
|
27
|
-
};
|
|
28
|
-
host: string;
|
|
29
|
-
port: number;
|
|
30
|
-
origin: string | string[];
|
|
31
|
-
errorStack: boolean;
|
|
32
|
-
registry: string[];
|
|
33
|
-
registryAdapter: string;
|
|
34
|
-
}
|
|
35
19
|
export declare const config: Config;
|
|
36
20
|
export declare function setGenTraceIdFunction(fn: () => string): void;
|
|
37
21
|
/**
|
|
@@ -45,9 +29,31 @@ export declare function genContext(context?: Context): Context;
|
|
|
45
29
|
export declare function getContext(): Context;
|
|
46
30
|
export declare function serve(): Promise<void>;
|
|
47
31
|
export declare function stop(): Promise<void>;
|
|
48
|
-
|
|
49
|
-
|
|
32
|
+
/**
|
|
33
|
+
* 检查连接是否被允许
|
|
34
|
+
* @param ip 连接主机 IP
|
|
35
|
+
* @param caller 连接服务名称
|
|
36
|
+
* @param token 令牌
|
|
37
|
+
* @returns 是否被允许
|
|
38
|
+
*/
|
|
39
|
+
export declare function checkAllow(ip: string, caller: string, token: string): {
|
|
40
|
+
allow: boolean;
|
|
41
|
+
reason?: string;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* 添加长连接
|
|
45
|
+
* @throws {KeepAliveConnectionError} 连接已存在
|
|
46
|
+
* @param connection
|
|
47
|
+
*/
|
|
48
|
+
export declare function addKeepAliveConnection(connection: KeepAliveConnection<KeepAliveNativeConnection>): void;
|
|
49
|
+
export declare function removeKeepAliveConnection(connection: KeepAliveConnection<KeepAliveNativeConnection>): void;
|
|
50
50
|
export declare function removeKeepAliveConnection(name: string, id: string): void;
|
|
51
|
-
export declare function setLoadBalancePolicy(policy: (name: string) =>
|
|
51
|
+
export declare function setLoadBalancePolicy(policy: (name: string) => KeepAliveConnection<KeepAliveNativeConnection>): void;
|
|
52
52
|
export declare function rpc(appName: string, action: string, params: any[], context?: Context): Promise<any>;
|
|
53
|
-
export declare function rpc(connection:
|
|
53
|
+
export declare function rpc(connection: KeepAliveConnection<KeepAliveNativeConnection>, action: string, params: any[], context?: Context): Promise<any>;
|
|
54
|
+
export * from './app.js';
|
|
55
|
+
export * from './keepalive-connection.js';
|
|
56
|
+
export { Module, ModuleConfig };
|
|
57
|
+
export { registry };
|
|
58
|
+
import * as modules from './modules/index.js';
|
|
59
|
+
export { modules };
|
|
@@ -0,0 +1,104 @@
|
|
|
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 declare class KeepAliveConnectionError extends Error {
|
|
5
|
+
connection: KeepAliveConnection<KeepAliveNativeConnection>;
|
|
6
|
+
constructor(message: string, connection: KeepAliveConnection<KeepAliveNativeConnection>);
|
|
7
|
+
}
|
|
8
|
+
export interface KeepAliveNativeConnection {
|
|
9
|
+
disconnect(): void;
|
|
10
|
+
}
|
|
11
|
+
export interface KeepAliveSyncConnectionsQuery {
|
|
12
|
+
[x: string]: any;
|
|
13
|
+
name?: string | string[];
|
|
14
|
+
}
|
|
15
|
+
export interface KeepAliveConnectionAdapter<T extends KeepAliveNativeConnection> {
|
|
16
|
+
/**
|
|
17
|
+
* 连接适配器名称
|
|
18
|
+
*/
|
|
19
|
+
name: string;
|
|
20
|
+
open(identify: string | URL | KeepAliveConnectionData): Promise<KeepAliveConnection<T>>;
|
|
21
|
+
close(connection: KeepAliveConnection<T>): Promise<void>;
|
|
22
|
+
rpc(connection: KeepAliveConnection<T>, action: string, params: any[], context: Context): Promise<any>;
|
|
23
|
+
/**
|
|
24
|
+
* 通知连接列表
|
|
25
|
+
*/
|
|
26
|
+
['registry:notify'](connection: KeepAliveConnection<T>, datas: KeepAliveConnectionData[]): void;
|
|
27
|
+
/**
|
|
28
|
+
* 订阅连接列表
|
|
29
|
+
*/
|
|
30
|
+
['registry:subscribe'](connection: KeepAliveConnection<T>, query: KeepAliveSyncConnectionsQuery): void;
|
|
31
|
+
}
|
|
32
|
+
export interface KeepAliveConnectionData {
|
|
33
|
+
/**
|
|
34
|
+
* 连接适配器名称
|
|
35
|
+
*/
|
|
36
|
+
adapter: string;
|
|
37
|
+
/**
|
|
38
|
+
* 连接主机地址
|
|
39
|
+
*/
|
|
40
|
+
ip: string;
|
|
41
|
+
/**
|
|
42
|
+
* 连接服务名称
|
|
43
|
+
*/
|
|
44
|
+
name: string;
|
|
45
|
+
/**
|
|
46
|
+
* 连接URL
|
|
47
|
+
*/
|
|
48
|
+
url?: string | URL;
|
|
49
|
+
/**
|
|
50
|
+
* 目标服务网络唯一ID
|
|
51
|
+
*/
|
|
52
|
+
id: string;
|
|
53
|
+
/**
|
|
54
|
+
* 验证令牌
|
|
55
|
+
*/
|
|
56
|
+
token: string;
|
|
57
|
+
}
|
|
58
|
+
export interface KeepAliveConnectionReadyParams {
|
|
59
|
+
[x: string]: any;
|
|
60
|
+
id: KeepAliveConnectionData['id'];
|
|
61
|
+
name: KeepAliveConnectionData['name'];
|
|
62
|
+
}
|
|
63
|
+
interface KeepAliveConnectionEventMap<T> {
|
|
64
|
+
"enabled": [];
|
|
65
|
+
"disabled": [];
|
|
66
|
+
"connect": [];
|
|
67
|
+
"disconnect": [];
|
|
68
|
+
'error': [KeepAliveConnectionError];
|
|
69
|
+
}
|
|
70
|
+
export declare class KeepAliveConnection<T extends KeepAliveNativeConnection> extends EventEmitter<KeepAliveConnectionEventMap<KeepAliveConnection<T>>> {
|
|
71
|
+
#private;
|
|
72
|
+
isRegistry: boolean;
|
|
73
|
+
data: KeepAliveConnectionData;
|
|
74
|
+
nativeConnection: T;
|
|
75
|
+
adapter: KeepAliveConnectionAdapter<T>;
|
|
76
|
+
constructor(adapter: KeepAliveConnectionAdapter<T>, nativeConnection: T, data: MakeOptional<KeepAliveConnectionData, 'adapter'>);
|
|
77
|
+
/**
|
|
78
|
+
* 设置连接是否可用,并自动触发enabled或disabled事件
|
|
79
|
+
* @param enabled 是否可用
|
|
80
|
+
*/
|
|
81
|
+
set enabled(enabled: boolean);
|
|
82
|
+
get enabled(): boolean;
|
|
83
|
+
/**
|
|
84
|
+
* enable & mount connection
|
|
85
|
+
* @param params
|
|
86
|
+
* @returns
|
|
87
|
+
*/
|
|
88
|
+
ready(params: KeepAliveConnectionReadyParams): void;
|
|
89
|
+
disconnect(): void;
|
|
90
|
+
}
|
|
91
|
+
export declare class KeepAliveConnectionStore<T extends KeepAliveNativeConnection> {
|
|
92
|
+
#private;
|
|
93
|
+
entries(): MapIterator<[string, Map<string, KeepAliveConnection<T>>]>;
|
|
94
|
+
getConnectionsOfService(name: string): Map<string, KeepAliveConnection<T>>;
|
|
95
|
+
has(name: string, id: string): boolean;
|
|
96
|
+
get(name: string, id: string): KeepAliveConnection<T> | null;
|
|
97
|
+
add(connection: KeepAliveConnection<T>): void;
|
|
98
|
+
remove(connection: KeepAliveConnection<T>): void;
|
|
99
|
+
remove(name: string, id: string): void;
|
|
100
|
+
}
|
|
101
|
+
export declare const keepAliveConnectionAdapters: Map<string, KeepAliveConnectionAdapter<KeepAliveNativeConnection>>;
|
|
102
|
+
export declare const keepAliveConnections: KeepAliveConnectionStore<KeepAliveNativeConnection>;
|
|
103
|
+
export declare const enabledKeepAliveConnections: KeepAliveConnectionStore<KeepAliveNativeConnection>;
|
|
104
|
+
export {};
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import * as http from 'node:http';
|
|
2
|
+
import * as https from 'node:https';
|
|
2
3
|
import Router, { Middleware } from './router.js';
|
|
3
|
-
import * as oox from '../../index.js';
|
|
4
4
|
import Module, { ModuleConfig } from '../module.js';
|
|
5
|
-
export declare class HTTPConfig
|
|
5
|
+
export declare class HTTPConfig implements ModuleConfig {
|
|
6
|
+
enabled: boolean;
|
|
6
7
|
port: number;
|
|
7
8
|
path: string;
|
|
8
9
|
origin: string | string[];
|
|
@@ -16,7 +17,7 @@ export declare class HTTPConfig extends ModuleConfig {
|
|
|
16
17
|
export default class HTTPModule extends Module {
|
|
17
18
|
name: string;
|
|
18
19
|
config: HTTPConfig;
|
|
19
|
-
server: http.Server;
|
|
20
|
+
server: http.Server | https.Server | null;
|
|
20
21
|
router: Router;
|
|
21
22
|
getURL(): URL;
|
|
22
23
|
get(path: string, ...args: any[]): this;
|
|
@@ -58,8 +59,4 @@ export default class HTTPModule extends Module {
|
|
|
58
59
|
stack?: any;
|
|
59
60
|
};
|
|
60
61
|
}): void;
|
|
61
|
-
/**
|
|
62
|
-
* HTTP RPC
|
|
63
|
-
*/
|
|
64
|
-
rpc(url: string | URL, action: string, params: Array<any>, context?: oox.Context): Promise<any>;
|
|
65
62
|
}
|
package/types/modules/index.d.ts
CHANGED
|
@@ -1,24 +1,39 @@
|
|
|
1
1
|
import Module from './module.js';
|
|
2
2
|
import HTTP from './http/index.js';
|
|
3
3
|
import SocketIO from './socketio/index.js';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
4
|
+
/**
|
|
5
|
+
* FIFO queue for modules starting
|
|
6
|
+
*/
|
|
7
|
+
export declare const queue: Module[];
|
|
8
|
+
/**
|
|
9
|
+
* all modules map
|
|
10
|
+
*/
|
|
11
|
+
export declare const map: Map<string, Module>;
|
|
12
|
+
/**
|
|
13
|
+
* add module to modules queue
|
|
14
|
+
*/
|
|
15
|
+
export declare function add(module: Module): void;
|
|
16
|
+
/**
|
|
17
|
+
* get module by name
|
|
18
|
+
*/
|
|
19
|
+
export declare function get(name: string): Module | null;
|
|
20
|
+
export declare function remove(name: string): Promise<void>;
|
|
21
|
+
/**
|
|
22
|
+
* set modules config
|
|
23
|
+
*/
|
|
24
|
+
export declare function setConfig(config: any): void;
|
|
25
|
+
/**
|
|
26
|
+
* serve modules
|
|
27
|
+
*/
|
|
28
|
+
export declare function serve(): Promise<void>;
|
|
29
|
+
/**
|
|
30
|
+
* stop all modules
|
|
31
|
+
*/
|
|
32
|
+
export declare function stop(): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* all builtin modules
|
|
35
|
+
*/
|
|
36
|
+
export declare const builtins: {
|
|
37
|
+
http: HTTP;
|
|
38
|
+
socketio: SocketIO;
|
|
39
|
+
};
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
export
|
|
2
|
-
enabled
|
|
1
|
+
export interface ModuleConfig {
|
|
2
|
+
enabled?: boolean;
|
|
3
3
|
}
|
|
4
|
-
export default class Module {
|
|
5
|
-
config: ModuleConfig;
|
|
6
|
-
name: string;
|
|
7
|
-
setConfig(config: any): void;
|
|
8
|
-
getConfig(): ModuleConfig;
|
|
9
|
-
serve(): Promise<void>;
|
|
10
|
-
stop(): Promise<void>;
|
|
4
|
+
export default abstract class Module {
|
|
5
|
+
abstract config: ModuleConfig;
|
|
6
|
+
abstract name: string;
|
|
7
|
+
abstract setConfig(config: any): void;
|
|
8
|
+
abstract getConfig(): ModuleConfig;
|
|
9
|
+
abstract serve(): Promise<void>;
|
|
10
|
+
abstract stop(): Promise<void>;
|
|
11
11
|
}
|
|
@@ -1,28 +1,14 @@
|
|
|
1
1
|
import * as oox from '../../index.js';
|
|
2
2
|
import { Socket } from './socket.js';
|
|
3
3
|
import { OOXEvent } from './utils.js';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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>;
|
|
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(identify: string | URL | oox.KeepAliveConnectionData): oox.KeepAliveConnection<Socket>;
|
|
28
14
|
}
|
|
@@ -5,8 +5,8 @@ import * as oox from '../../index.js';
|
|
|
5
5
|
import { Module, ModuleConfig } from '../../index.js';
|
|
6
6
|
import { Socket } from './socket.js';
|
|
7
7
|
import SocketIOAdapter from './adapter.js';
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
export declare class SocketIOConfig implements ModuleConfig {
|
|
9
|
+
enabled: boolean;
|
|
10
10
|
port: number;
|
|
11
11
|
path: string;
|
|
12
12
|
origin: string | string[];
|
|
@@ -21,9 +21,10 @@ export default class SocketIOServer extends Module {
|
|
|
21
21
|
#private;
|
|
22
22
|
name: string;
|
|
23
23
|
config: SocketIOConfig;
|
|
24
|
-
server: http.Server | https.Server;
|
|
25
|
-
socketServer: Server;
|
|
24
|
+
server: http.Server | https.Server | null;
|
|
25
|
+
socketServer: Server | null;
|
|
26
26
|
adapter: SocketIOAdapter;
|
|
27
|
+
constructor();
|
|
27
28
|
getURL(): URL;
|
|
28
29
|
setConfig(config: SocketIOConfig): void;
|
|
29
30
|
getConfig(): SocketIOConfig;
|
|
@@ -35,14 +36,9 @@ export default class SocketIOServer extends Module {
|
|
|
35
36
|
* 服务端Socket连接事件
|
|
36
37
|
*/
|
|
37
38
|
serverOnSocketConnection(socket: Socket<'server'>): void;
|
|
38
|
-
/**
|
|
39
|
-
* 服务器发送连接列表
|
|
40
|
-
* @param connection
|
|
41
|
-
*/
|
|
42
|
-
[OOXEvent.SYNC_CONNECTIONS](connection: oox.RPCKeepAliveConnection<Socket<'server'>>): oox.RPCKeepAliveConnectionData[];
|
|
43
39
|
/**
|
|
44
40
|
* 绑定服务器连接事件
|
|
45
41
|
* @param connection
|
|
46
42
|
*/
|
|
47
|
-
bindServerConnectionEvents(connection: oox.
|
|
43
|
+
bindServerConnectionEvents(connection: oox.KeepAliveConnection<Socket<'server'>>): void;
|
|
48
44
|
}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
export declare enum OOXEvent {
|
|
2
|
-
|
|
2
|
+
READY = "oox:ready",
|
|
3
|
+
ENABLED = "oox:enabled",
|
|
3
4
|
DISABLED = "oox:disabled",
|
|
4
|
-
|
|
5
|
+
REGISTRY_SYNC_CONNECTIONS = "oox:registry:sync_connections",
|
|
6
|
+
REGISTRY_SUBSCRIBE = "oox:registry:subscribe",
|
|
7
|
+
REGISTRY_NOTIFY = "oox:registry:notify"
|
|
5
8
|
}
|
|
6
9
|
export declare function isWebSocketURL(url: string | URL): boolean;
|
|
7
10
|
export declare function genWebSocketURL(url: string): URL;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { ReturnsBody } from './index.js';
|
|
2
|
+
import { KeepAliveConnection, KeepAliveConnectionData, KeepAliveNativeConnection } from './keepalive-connection.js';
|
|
3
|
+
export interface SyncConnectionsQuery {
|
|
4
|
+
name?: string | string[];
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* 取消订阅连接列表
|
|
8
|
+
* @param connection
|
|
9
|
+
*/
|
|
10
|
+
export declare function unsubscribe(connection: KeepAliveConnection<KeepAliveNativeConnection>): void;
|
|
11
|
+
/**
|
|
12
|
+
* 订阅连接列表
|
|
13
|
+
* @param connection
|
|
14
|
+
* @param query
|
|
15
|
+
* @returns
|
|
16
|
+
*/
|
|
17
|
+
export declare function subscribe(connection: KeepAliveConnection<KeepAliveNativeConnection>, query: SyncConnectionsQuery): void;
|
|
18
|
+
/**
|
|
19
|
+
* 停止广播连接列表
|
|
20
|
+
*/
|
|
21
|
+
export declare function stopBroadcastTimer(): void;
|
|
22
|
+
/**
|
|
23
|
+
* 开始广播连接列表
|
|
24
|
+
*/
|
|
25
|
+
export declare function startBroadcastTimer(): void;
|
|
26
|
+
/**
|
|
27
|
+
* 广播连接列表
|
|
28
|
+
*/
|
|
29
|
+
export declare function broadcast(): void;
|
|
30
|
+
/**
|
|
31
|
+
* 服务器发送连接列表
|
|
32
|
+
* @param connection
|
|
33
|
+
* @param query
|
|
34
|
+
* @param callback
|
|
35
|
+
*/
|
|
36
|
+
export declare function onSyncConnections(connection: KeepAliveConnection<KeepAliveNativeConnection>, query: SyncConnectionsQuery, callback: (returns: ReturnsBody<KeepAliveConnectionData[]>) => void): void;
|
|
37
|
+
/**
|
|
38
|
+
* 同步连接列表
|
|
39
|
+
* @param datas 连接参数列表
|
|
40
|
+
*/
|
|
41
|
+
export declare function syncConnections(returns: ReturnsBody<KeepAliveConnectionData[]>): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { SampleKeepAliveConnectionAdapter, SampleKeepAliveNativeConnection } from './keepalive-connection-sample.js';
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import * as oox from '../index.js';
|
|
2
|
+
export interface SampleKeepAliveNativeConnection extends oox.KeepAliveNativeConnection {
|
|
3
|
+
connected: boolean;
|
|
4
|
+
connect?(): void;
|
|
5
|
+
on(event: string | symbol, listener: (...args: any[]) => any): any;
|
|
6
|
+
once(event: string | symbol, listener: (...args: any[]) => any): any;
|
|
7
|
+
off(event?: string | symbol, listener?: (...args: any[]) => any): any;
|
|
8
|
+
emit(event: string | symbol, ...args: any[]): any;
|
|
9
|
+
removeAllListeners(event?: string | symbol): any;
|
|
10
|
+
}
|
|
11
|
+
export declare abstract class SampleKeepAliveConnectionAdapter<NativeConnection extends SampleKeepAliveNativeConnection> implements oox.KeepAliveConnectionAdapter<NativeConnection> {
|
|
12
|
+
abstract name: string;
|
|
13
|
+
OOXEvent: {
|
|
14
|
+
READY: string;
|
|
15
|
+
ENABLED: string;
|
|
16
|
+
DISABLED: string;
|
|
17
|
+
REGISTRY_SYNC_CONNECTIONS: string;
|
|
18
|
+
REGISTRY_SUBSCRIBE: string;
|
|
19
|
+
REGISTRY_NOTIFY: string;
|
|
20
|
+
};
|
|
21
|
+
nativeEvent: {
|
|
22
|
+
CONNECT: string;
|
|
23
|
+
DISCONNECT: string;
|
|
24
|
+
ERROR: string;
|
|
25
|
+
};
|
|
26
|
+
abstract newConnection(identify: string | URL | oox.KeepAliveConnectionData): oox.KeepAliveConnection<NativeConnection>;
|
|
27
|
+
/**
|
|
28
|
+
* 通知连接列表
|
|
29
|
+
*/
|
|
30
|
+
['registry:notify'](connection: oox.KeepAliveConnection<NativeConnection>, datas: oox.KeepAliveConnectionData[]): void;
|
|
31
|
+
/**
|
|
32
|
+
* 订阅连接列表
|
|
33
|
+
*/
|
|
34
|
+
['registry:subscribe'](connection: oox.KeepAliveConnection<NativeConnection>, query: oox.KeepAliveSyncConnectionsQuery): void;
|
|
35
|
+
bindConnectionEvents(connection: oox.KeepAliveConnection<NativeConnection>): void;
|
|
36
|
+
open(identify: string | URL | oox.KeepAliveConnectionData): Promise<oox.KeepAliveConnection<NativeConnection>>;
|
|
37
|
+
close(connection: oox.KeepAliveConnection<NativeConnection>): Promise<void>;
|
|
38
|
+
/**
|
|
39
|
+
* 发送事件
|
|
40
|
+
* @param socket
|
|
41
|
+
* @param event
|
|
42
|
+
* @param params
|
|
43
|
+
*/
|
|
44
|
+
emit(socket: NativeConnection, event: string, params: any[]): Promise<unknown>;
|
|
45
|
+
/**
|
|
46
|
+
* RPC
|
|
47
|
+
*/
|
|
48
|
+
rpc(connection: oox.KeepAliveConnection<NativeConnection>, action: string, params: any[], context?: oox.KeepAliveRPCContext): Promise<any>;
|
|
49
|
+
/**
|
|
50
|
+
* 绑定 call 事件
|
|
51
|
+
* @param connection
|
|
52
|
+
*/
|
|
53
|
+
bindCall(connection: oox.KeepAliveConnection<NativeConnection>): void;
|
|
54
|
+
call(action: string, params: any[], context: oox.Context, callback?: (returns: any) => void): Promise<oox.ReturnsBody<any>>;
|
|
55
|
+
}
|
package/utils.js
CHANGED
|
@@ -3,7 +3,7 @@ export function getIPAddress(version = 4) {
|
|
|
3
3
|
const interfaces = networkInterfaces();
|
|
4
4
|
const ip = [];
|
|
5
5
|
for (const name of Object.keys(interfaces))
|
|
6
|
-
for (const intf of interfaces[name])
|
|
6
|
+
for (const intf of interfaces[name] || [])
|
|
7
7
|
if (intf.mac !== '00:00:00:00:00:00')
|
|
8
8
|
if ((version !== 4 && version !== 6) || 'IPv' + version === intf.family)
|
|
9
9
|
ip.push(intf.address);
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import EventEmitter from 'node:events';
|
|
2
|
-
import { Context } from './index.js';
|
|
3
|
-
export interface RPCKeepAliveConnectionAdapter<T> {
|
|
4
|
-
open(url: string | URL): Promise<RPCKeepAliveConnection<T>>;
|
|
5
|
-
close(connection: RPCKeepAliveConnection<T>): Promise<void>;
|
|
6
|
-
rpc(connection: RPCKeepAliveConnection<T>, action: string, params: any[], context: Context): Promise<any>;
|
|
7
|
-
}
|
|
8
|
-
export interface RPCKeepAliveConnectionData {
|
|
9
|
-
/**
|
|
10
|
-
* 连接主机地址
|
|
11
|
-
*/
|
|
12
|
-
host: string;
|
|
13
|
-
/**
|
|
14
|
-
* 连接服务名称
|
|
15
|
-
*/
|
|
16
|
-
name: string;
|
|
17
|
-
/**
|
|
18
|
-
* 连接URL
|
|
19
|
-
*/
|
|
20
|
-
url?: string | URL;
|
|
21
|
-
/**
|
|
22
|
-
* 目标服务网络唯一ID
|
|
23
|
-
*/
|
|
24
|
-
id: string;
|
|
25
|
-
}
|
|
26
|
-
export type RPCKeepAliveConnectionEventMap = Record<keyof {
|
|
27
|
-
"enabled": any;
|
|
28
|
-
"disabled": any;
|
|
29
|
-
"connect": any;
|
|
30
|
-
"disconnect": any;
|
|
31
|
-
'error': any;
|
|
32
|
-
}, any[]>;
|
|
33
|
-
export declare class RPCKeepAliveConnection<T> extends EventEmitter<RPCKeepAliveConnectionEventMap> {
|
|
34
|
-
#private;
|
|
35
|
-
data: RPCKeepAliveConnectionData;
|
|
36
|
-
nativeConnection: T;
|
|
37
|
-
adapter: RPCKeepAliveConnectionAdapter<T>;
|
|
38
|
-
constructor(adapter: RPCKeepAliveConnectionAdapter<T>, nativeConnection: T, data: RPCKeepAliveConnectionData);
|
|
39
|
-
set enabled(enabled: boolean);
|
|
40
|
-
get enabled(): boolean;
|
|
41
|
-
updateId(newId: string): void;
|
|
42
|
-
updateName(newName: string): void;
|
|
43
|
-
updateIdAndName(newId: string, newName: string): void;
|
|
44
|
-
}
|
|
45
|
-
export declare class KeepAliveConnectionStore<T> {
|
|
46
|
-
#private;
|
|
47
|
-
entries(): MapIterator<[string, Map<string, RPCKeepAliveConnection<T>>]>;
|
|
48
|
-
getConnectionsOfService(name: string): Map<string, RPCKeepAliveConnection<T>>;
|
|
49
|
-
has(name: string, id: string): boolean;
|
|
50
|
-
get(name: string, id: string): RPCKeepAliveConnection<T> | null;
|
|
51
|
-
add(connection: RPCKeepAliveConnection<T>): void;
|
|
52
|
-
remove(connection: RPCKeepAliveConnection<T>): void;
|
|
53
|
-
remove(name: string, id: string): void;
|
|
54
|
-
}
|
|
55
|
-
export declare const rpcKeepAliveConnectionAdapters: Map<string, RPCKeepAliveConnectionAdapter<any>>;
|
|
56
|
-
export declare const keepAliveConnections: KeepAliveConnectionStore<any>;
|
|
57
|
-
export declare const enabledKeepAliveConnections: KeepAliveConnectionStore<any>;
|