oox 0.3.4 → 0.3.6
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 -7
- package/bin/cli.js +45 -12
- package/bin/configurer.js +6 -9
- package/bin/proxy-require.js +1 -23
- package/bin/register.js +48 -15
- package/bin/starter.js +8 -0
- package/config.js +145 -0
- package/context.js +58 -0
- package/index.js +14 -133
- package/keepalive-connection.js +73 -0
- package/logger.js +2 -1
- package/modules/http/index.js +52 -63
- package/modules/http/router.js +9 -4
- package/modules/index.js +78 -74
- package/modules/module.js +1 -10
- package/modules/socketio/adapter.js +24 -13
- package/modules/socketio/server.js +37 -16
- package/modules/socketio/utils.js +12 -7
- package/package.json +13 -6
- package/proxy.js +30 -0
- package/registry.js +91 -7
- package/samples/index.js +1 -1
- package/samples/keepalive-connection-sample.js +62 -41
- package/types/app.d.ts +18 -24
- package/types/bin/configurer.d.ts +3 -1
- package/types/bin/register.d.ts +2 -1
- package/types/config.d.ts +66 -0
- package/types/context.d.ts +30 -0
- package/types/index.d.ts +10 -53
- package/types/keepalive-connection.d.ts +52 -16
- package/types/modules/http/index.d.ts +5 -8
- package/types/modules/index.d.ts +37 -22
- package/types/modules/module.d.ts +9 -9
- package/types/modules/socketio/adapter.d.ts +9 -4
- package/types/modules/socketio/server.d.ts +6 -4
- package/types/modules/socketio/utils.d.ts +12 -6
- package/types/proxy.d.ts +1 -0
- package/types/registry.d.ts +28 -3
- package/types/samples/index.d.ts +1 -1
- package/types/samples/keepalive-connection-sample.d.ts +33 -23
- package/utils.js +2 -2
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
import { KeepAliveConnection, KeepAliveNativeConnection } from './keepalive-connection.js';
|
|
3
|
+
export declare const asyncStore: AsyncLocalStorage<AppContext>;
|
|
4
|
+
export declare class AppContext {
|
|
5
|
+
[x: string]: any;
|
|
6
|
+
traceId?: string | undefined;
|
|
7
|
+
}
|
|
8
|
+
export interface KeepAliveRPCContext {
|
|
9
|
+
traceId?: string;
|
|
10
|
+
sourceIP?: string;
|
|
11
|
+
}
|
|
12
|
+
export declare class Context extends AppContext {
|
|
13
|
+
sourceIP: string;
|
|
14
|
+
ip: string;
|
|
15
|
+
caller: string;
|
|
16
|
+
callerId: string;
|
|
17
|
+
connection?: KeepAliveConnection<KeepAliveNativeConnection>;
|
|
18
|
+
constructor(context?: Partial<Context>);
|
|
19
|
+
toJSON?(): {} & this;
|
|
20
|
+
}
|
|
21
|
+
export declare function setGenTraceIdFunction(fn: () => string): void;
|
|
22
|
+
/**
|
|
23
|
+
* 生成随机不重复id
|
|
24
|
+
*/
|
|
25
|
+
export declare function genTraceId(): string;
|
|
26
|
+
/**
|
|
27
|
+
* 获取链路跟踪上下文
|
|
28
|
+
*/
|
|
29
|
+
export declare function genContext(context?: Context): Context;
|
|
30
|
+
export declare function getContext(): Context;
|
package/types/index.d.ts
CHANGED
|
@@ -1,56 +1,13 @@
|
|
|
1
|
-
import * as app from './app.js';
|
|
2
|
-
import * as registry from './registry.js';
|
|
3
|
-
import Module, { ModuleConfig } from './modules/module.js';
|
|
4
|
-
import { KeepAliveConnectionReadyParams, KeepAliveConnectionAdapter, KeepAliveConnection, KeepAliveConnectionData, keepAliveConnectionAdapters, keepAliveConnections, enabledKeepAliveConnections } from './keepalive-connection.js';
|
|
5
|
-
export declare class Context extends app.Context {
|
|
6
|
-
sourceIP: string;
|
|
7
|
-
ip: string;
|
|
8
|
-
caller: string;
|
|
9
|
-
callerId: string;
|
|
10
|
-
connection?: KeepAliveConnection<any>;
|
|
11
|
-
toJSON?(): {} & this;
|
|
12
|
-
}
|
|
13
|
-
export declare class Config {
|
|
14
|
-
[x: string]: any;
|
|
15
|
-
id: `${string}-${string}-${string}-${string}-${string}`;
|
|
16
|
-
name: string;
|
|
17
|
-
group: string;
|
|
18
|
-
ignore: string[];
|
|
19
|
-
entryInfo: {
|
|
20
|
-
path: string;
|
|
21
|
-
group: string;
|
|
22
|
-
};
|
|
23
|
-
host: string;
|
|
24
|
-
port: number;
|
|
25
|
-
origin: string | string[];
|
|
26
|
-
errorStack: boolean;
|
|
27
|
-
isRegistry: boolean;
|
|
28
|
-
registry: string[];
|
|
29
|
-
registryAdapter: string;
|
|
30
|
-
}
|
|
31
|
-
export declare const config: Config;
|
|
32
|
-
export declare function setGenTraceIdFunction(fn: () => string): void;
|
|
33
|
-
/**
|
|
34
|
-
* 生成随机不重复id
|
|
35
|
-
*/
|
|
36
|
-
export declare function genTraceId(): string;
|
|
37
|
-
/**
|
|
38
|
-
* 获取链路跟踪上下文
|
|
39
|
-
*/
|
|
40
|
-
export declare function genContext(context?: Context): Context;
|
|
41
|
-
export declare function getContext(): Context;
|
|
42
1
|
export declare function serve(): Promise<void>;
|
|
43
2
|
export declare function stop(): Promise<void>;
|
|
44
|
-
export
|
|
45
|
-
export
|
|
46
|
-
export
|
|
47
|
-
export
|
|
48
|
-
export
|
|
49
|
-
export
|
|
50
|
-
export
|
|
51
|
-
|
|
52
|
-
export { Module, ModuleConfig };
|
|
3
|
+
export * from './app.js';
|
|
4
|
+
export * from './config.js';
|
|
5
|
+
export * from './context.js';
|
|
6
|
+
export * from './keepalive-connection.js';
|
|
7
|
+
export * from './modules/module.js';
|
|
8
|
+
export * as logger from './logger.js';
|
|
9
|
+
export * from './proxy.js';
|
|
10
|
+
import * as registry from './registry.js';
|
|
53
11
|
export { registry };
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
export declare const modules: Modules;
|
|
12
|
+
import * as modules from './modules/index.js';
|
|
13
|
+
export { modules };
|
|
@@ -1,14 +1,33 @@
|
|
|
1
1
|
import EventEmitter from 'node:events';
|
|
2
|
-
import { Context } from './
|
|
2
|
+
import { Context } from './context.js';
|
|
3
3
|
type MakeOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
4
|
-
export
|
|
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> {
|
|
5
16
|
/**
|
|
6
17
|
* 连接适配器名称
|
|
7
18
|
*/
|
|
8
19
|
name: string;
|
|
9
|
-
open(
|
|
20
|
+
open(identify: string | URL | KeepAliveConnectionData): Promise<KeepAliveConnection<T>>;
|
|
10
21
|
close(connection: KeepAliveConnection<T>): Promise<void>;
|
|
11
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;
|
|
12
31
|
}
|
|
13
32
|
export interface KeepAliveConnectionData {
|
|
14
33
|
/**
|
|
@@ -18,7 +37,7 @@ export interface KeepAliveConnectionData {
|
|
|
18
37
|
/**
|
|
19
38
|
* 连接主机地址
|
|
20
39
|
*/
|
|
21
|
-
|
|
40
|
+
ip: string;
|
|
22
41
|
/**
|
|
23
42
|
* 连接服务名称
|
|
24
43
|
*/
|
|
@@ -31,21 +50,26 @@ export interface KeepAliveConnectionData {
|
|
|
31
50
|
* 目标服务网络唯一ID
|
|
32
51
|
*/
|
|
33
52
|
id: string;
|
|
53
|
+
/**
|
|
54
|
+
* 验证令牌
|
|
55
|
+
*/
|
|
56
|
+
token: string;
|
|
34
57
|
}
|
|
35
58
|
export interface KeepAliveConnectionReadyParams {
|
|
36
59
|
[x: string]: any;
|
|
37
60
|
id: KeepAliveConnectionData['id'];
|
|
38
61
|
name: KeepAliveConnectionData['name'];
|
|
39
62
|
}
|
|
40
|
-
|
|
41
|
-
"enabled":
|
|
42
|
-
"disabled":
|
|
43
|
-
"connect":
|
|
44
|
-
"disconnect":
|
|
45
|
-
'error':
|
|
46
|
-
}
|
|
47
|
-
export declare class KeepAliveConnection<T> extends EventEmitter<KeepAliveConnectionEventMap
|
|
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>>> {
|
|
48
71
|
#private;
|
|
72
|
+
isRegistry: boolean;
|
|
49
73
|
data: KeepAliveConnectionData;
|
|
50
74
|
nativeConnection: T;
|
|
51
75
|
adapter: KeepAliveConnectionAdapter<T>;
|
|
@@ -62,8 +86,9 @@ export declare class KeepAliveConnection<T> extends EventEmitter<KeepAliveConnec
|
|
|
62
86
|
* @returns
|
|
63
87
|
*/
|
|
64
88
|
ready(params: KeepAliveConnectionReadyParams): void;
|
|
89
|
+
disconnect(): void;
|
|
65
90
|
}
|
|
66
|
-
export declare class KeepAliveConnectionStore<T> {
|
|
91
|
+
export declare class KeepAliveConnectionStore<T extends KeepAliveNativeConnection> {
|
|
67
92
|
#private;
|
|
68
93
|
entries(): MapIterator<[string, Map<string, KeepAliveConnection<T>>]>;
|
|
69
94
|
getConnectionsOfService(name: string): Map<string, KeepAliveConnection<T>>;
|
|
@@ -73,7 +98,18 @@ export declare class KeepAliveConnectionStore<T> {
|
|
|
73
98
|
remove(connection: KeepAliveConnection<T>): void;
|
|
74
99
|
remove(name: string, id: string): void;
|
|
75
100
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
101
|
+
/**
|
|
102
|
+
* 添加长连接
|
|
103
|
+
* @throws {KeepAliveConnectionError} 连接已存在
|
|
104
|
+
* @param connection
|
|
105
|
+
*/
|
|
106
|
+
export declare function addKeepAliveConnection(connection: KeepAliveConnection<KeepAliveNativeConnection>): void;
|
|
107
|
+
export declare function removeKeepAliveConnection(connection: KeepAliveConnection<KeepAliveNativeConnection>): void;
|
|
108
|
+
export declare function removeKeepAliveConnection(name: string, id: string): void;
|
|
109
|
+
export declare function setLoadBalancePolicy(policy: (name: string) => KeepAliveConnection<KeepAliveNativeConnection>): void;
|
|
110
|
+
export declare function rpc(appName: string, action: string, params: any[], context?: Context): Promise<any>;
|
|
111
|
+
export declare function rpc(connection: KeepAliveConnection<KeepAliveNativeConnection>, action: string, params: any[], context?: Context): Promise<any>;
|
|
112
|
+
export declare const keepAliveConnectionAdapters: Map<string, KeepAliveConnectionAdapter<KeepAliveNativeConnection>>;
|
|
113
|
+
export declare const keepAliveConnections: KeepAliveConnectionStore<KeepAliveNativeConnection>;
|
|
114
|
+
export declare const enabledKeepAliveConnections: KeepAliveConnectionStore<KeepAliveNativeConnection>;
|
|
79
115
|
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
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
import { Module, ModuleConfig } from '../module.js';
|
|
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
|
-
import Module from './module.js';
|
|
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
|
|
5
|
-
config: ModuleConfig;
|
|
6
|
-
name: string;
|
|
7
|
-
setConfig(config: any): void;
|
|
8
|
-
getConfig(): ModuleConfig;
|
|
9
|
-
serve(): Promise<void>;
|
|
10
|
-
stop(): Promise<void>;
|
|
4
|
+
export declare 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,14 +1,19 @@
|
|
|
1
1
|
import * as oox from '../../index.js';
|
|
2
2
|
import { Socket } from './socket.js';
|
|
3
|
-
import { OOXEvent } from './utils.js';
|
|
4
3
|
import { SampleKeepAliveConnectionAdapter } from '../../samples/index.js';
|
|
5
4
|
export default class SocketIOAdapter extends SampleKeepAliveConnectionAdapter<Socket> {
|
|
6
5
|
name: string;
|
|
7
|
-
OOXEvent:
|
|
8
|
-
nativeEvent: {
|
|
6
|
+
OOXEvent: {
|
|
9
7
|
CONNECT: string;
|
|
10
8
|
DISCONNECT: string;
|
|
11
9
|
ERROR: string;
|
|
10
|
+
CALL: string;
|
|
11
|
+
READY: string;
|
|
12
|
+
ENABLED: string;
|
|
13
|
+
DISABLED: string;
|
|
14
|
+
REGISTRY_SYNC_CONNECTIONS: string;
|
|
15
|
+
REGISTRY_SUBSCRIBE: string;
|
|
16
|
+
REGISTRY_NOTIFY: string;
|
|
12
17
|
};
|
|
13
|
-
newConnection(
|
|
18
|
+
newConnection(identify: string | URL | oox.KeepAliveConnectionData): oox.KeepAliveConnection<Socket>;
|
|
14
19
|
}
|
|
@@ -2,10 +2,11 @@ import * as http from 'node:http';
|
|
|
2
2
|
import * as https from 'node:https';
|
|
3
3
|
import { Server, ServerOptions } from 'socket.io';
|
|
4
4
|
import * as oox from '../../index.js';
|
|
5
|
-
import { Module, ModuleConfig } from '
|
|
5
|
+
import { Module, ModuleConfig } from '../module.js';
|
|
6
6
|
import { Socket } from './socket.js';
|
|
7
7
|
import SocketIOAdapter from './adapter.js';
|
|
8
|
-
export declare class SocketIOConfig
|
|
8
|
+
export declare class SocketIOConfig implements ModuleConfig {
|
|
9
|
+
enabled: boolean;
|
|
9
10
|
port: number;
|
|
10
11
|
path: string;
|
|
11
12
|
origin: string | string[];
|
|
@@ -20,9 +21,10 @@ export default class SocketIOServer extends Module {
|
|
|
20
21
|
#private;
|
|
21
22
|
name: string;
|
|
22
23
|
config: SocketIOConfig;
|
|
23
|
-
server: http.Server | https.Server;
|
|
24
|
-
socketServer: Server;
|
|
24
|
+
server: http.Server | https.Server | null;
|
|
25
|
+
socketServer: Server | null;
|
|
25
26
|
adapter: SocketIOAdapter;
|
|
27
|
+
constructor();
|
|
26
28
|
getURL(): URL;
|
|
27
29
|
setConfig(config: SocketIOConfig): void;
|
|
28
30
|
getConfig(): SocketIOConfig;
|
|
@@ -1,8 +1,14 @@
|
|
|
1
|
-
export declare
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
export declare const OOXEvent: {
|
|
2
|
+
CONNECT: string;
|
|
3
|
+
DISCONNECT: string;
|
|
4
|
+
ERROR: string;
|
|
5
|
+
CALL: string;
|
|
6
|
+
READY: string;
|
|
7
|
+
ENABLED: string;
|
|
8
|
+
DISABLED: string;
|
|
9
|
+
REGISTRY_SYNC_CONNECTIONS: string;
|
|
10
|
+
REGISTRY_SUBSCRIBE: string;
|
|
11
|
+
REGISTRY_NOTIFY: string;
|
|
12
|
+
};
|
|
7
13
|
export declare function isWebSocketURL(url: string | URL): boolean;
|
|
8
14
|
export declare function genWebSocketURL(url: string): URL;
|
package/types/proxy.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function proxy(name: string, action?: string): () => void;
|
package/types/registry.d.ts
CHANGED
|
@@ -1,16 +1,41 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ReturnsBody } from './app.js';
|
|
2
|
+
import { KeepAliveConnection, KeepAliveConnectionData, KeepAliveNativeConnection } from './keepalive-connection.js';
|
|
2
3
|
export interface SyncConnectionsQuery {
|
|
3
4
|
name?: string | string[];
|
|
4
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;
|
|
5
30
|
/**
|
|
6
31
|
* 服务器发送连接列表
|
|
7
32
|
* @param connection
|
|
8
33
|
* @param query
|
|
9
34
|
* @param callback
|
|
10
35
|
*/
|
|
11
|
-
export declare function onSyncConnections(connection: KeepAliveConnection<
|
|
36
|
+
export declare function onSyncConnections(connection: KeepAliveConnection<KeepAliveNativeConnection>, query: SyncConnectionsQuery, callback: (returns: ReturnsBody<KeepAliveConnectionData[]>) => void): void;
|
|
12
37
|
/**
|
|
13
38
|
* 同步连接列表
|
|
14
39
|
* @param datas 连接参数列表
|
|
15
40
|
*/
|
|
16
|
-
export declare function syncConnections(
|
|
41
|
+
export declare function syncConnections(returns: ReturnsBody<KeepAliveConnectionData[]>): void;
|
package/types/samples/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from './keepalive-connection-sample.js';
|
|
@@ -1,31 +1,41 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import { Context, KeepAliveRPCContext } from '../context.js';
|
|
2
|
+
import { KeepAliveConnection, KeepAliveConnectionData, KeepAliveNativeConnection, KeepAliveConnectionAdapter, KeepAliveSyncConnectionsQuery } from '../keepalive-connection.js';
|
|
3
|
+
import { ReturnsBody } from '../app.js';
|
|
4
|
+
export interface SampleKeepAliveNativeConnection extends KeepAliveNativeConnection {
|
|
3
5
|
connected: boolean;
|
|
4
6
|
connect?(): void;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
removeAllListeners(event?: string | symbol): any;
|
|
7
|
+
on(event: string, listener: (...args: any[]) => any): any;
|
|
8
|
+
once(event: string, listener: (...args: any[]) => any): any;
|
|
9
|
+
off(event: string, listener?: (...args: any[]) => any): any;
|
|
10
|
+
emit(event: string, ...args: any[]): any;
|
|
11
|
+
removeAllListeners(event?: string): any;
|
|
11
12
|
}
|
|
12
|
-
export declare abstract class SampleKeepAliveConnectionAdapter<NativeConnection extends SampleKeepAliveNativeConnection> implements
|
|
13
|
-
name: string;
|
|
13
|
+
export declare abstract class SampleKeepAliveConnectionAdapter<NativeConnection extends SampleKeepAliveNativeConnection> implements KeepAliveConnectionAdapter<NativeConnection> {
|
|
14
|
+
abstract name: string;
|
|
14
15
|
OOXEvent: {
|
|
15
|
-
READY: string;
|
|
16
|
-
ENABLED: string;
|
|
17
|
-
DISABLED: string;
|
|
18
|
-
SYNC_CONNECTIONS: string;
|
|
19
|
-
};
|
|
20
|
-
nativeEvent: {
|
|
21
16
|
CONNECT: string;
|
|
22
17
|
DISCONNECT: string;
|
|
23
18
|
ERROR: string;
|
|
19
|
+
CALL: string;
|
|
20
|
+
READY: string;
|
|
21
|
+
ENABLED: string;
|
|
22
|
+
DISABLED: string;
|
|
23
|
+
REGISTRY_SYNC_CONNECTIONS: string;
|
|
24
|
+
REGISTRY_SUBSCRIBE: string;
|
|
25
|
+
REGISTRY_NOTIFY: string;
|
|
24
26
|
};
|
|
25
|
-
abstract newConnection(
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
abstract newConnection(identify: string | URL | KeepAliveConnectionData): KeepAliveConnection<NativeConnection>;
|
|
28
|
+
/**
|
|
29
|
+
* 通知连接列表
|
|
30
|
+
*/
|
|
31
|
+
['registry:notify'](connection: KeepAliveConnection<NativeConnection>, datas: KeepAliveConnectionData[]): void;
|
|
32
|
+
/**
|
|
33
|
+
* 订阅连接列表
|
|
34
|
+
*/
|
|
35
|
+
['registry:subscribe'](connection: KeepAliveConnection<NativeConnection>, query: KeepAliveSyncConnectionsQuery): void;
|
|
36
|
+
bindConnectionEvents(connection: KeepAliveConnection<NativeConnection>): void;
|
|
37
|
+
open(identify: string | URL | KeepAliveConnectionData): Promise<KeepAliveConnection<NativeConnection>>;
|
|
38
|
+
close(connection: KeepAliveConnection<NativeConnection>): Promise<void>;
|
|
29
39
|
/**
|
|
30
40
|
* 发送事件
|
|
31
41
|
* @param socket
|
|
@@ -36,11 +46,11 @@ export declare abstract class SampleKeepAliveConnectionAdapter<NativeConnection
|
|
|
36
46
|
/**
|
|
37
47
|
* RPC
|
|
38
48
|
*/
|
|
39
|
-
rpc(connection:
|
|
49
|
+
rpc(connection: KeepAliveConnection<NativeConnection>, action: string, params: any[], context?: KeepAliveRPCContext): Promise<any>;
|
|
40
50
|
/**
|
|
41
51
|
* 绑定 call 事件
|
|
42
52
|
* @param connection
|
|
43
53
|
*/
|
|
44
|
-
bindCall(connection:
|
|
45
|
-
call(action: string, params: any[], context:
|
|
54
|
+
bindCall(connection: KeepAliveConnection<NativeConnection>): void;
|
|
55
|
+
call(action: string, params: any[], context: Context, callback?: (returns: any) => void): Promise<ReturnsBody<any>>;
|
|
46
56
|
}
|
package/utils.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { networkInterfaces } from 'os';
|
|
1
|
+
import { networkInterfaces } from 'node:os';
|
|
2
2
|
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);
|