@oox/client 1.0.0

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.
@@ -0,0 +1,14 @@
1
+ export interface ConfigAllow {
2
+ untrusted: boolean;
3
+ ip: string[];
4
+ caller: string[];
5
+ }
6
+ interface ConfigInterface {
7
+ id: string;
8
+ name: string;
9
+ }
10
+ export declare class Config implements ConfigInterface {
11
+ id: string;
12
+ name: string;
13
+ }
14
+ export {};
@@ -0,0 +1,18 @@
1
+ import { KeepAliveConnection, KeepAliveNativeConnection } from './keepalive-connection.js';
2
+ export declare class AppContext {
3
+ [x: string]: any;
4
+ traceId?: string | undefined;
5
+ }
6
+ export interface KeepAliveRPCContext {
7
+ traceId?: string;
8
+ sourceIP?: string;
9
+ }
10
+ export declare class Context extends AppContext {
11
+ sourceIP: string;
12
+ ip: string;
13
+ caller: string;
14
+ callerId: string;
15
+ connection?: KeepAliveConnection<KeepAliveNativeConnection>;
16
+ constructor(context?: Partial<Context>);
17
+ toJSON?(): {} & this;
18
+ }
@@ -0,0 +1,23 @@
1
+ import { Context } from "./context.js";
2
+ import { Config } from './config.js';
3
+ import { KeepAliveConnection, KeepAliveNativeConnection } from "./keepalive-connection.js";
4
+ export declare const config: Config;
5
+ export declare function setGenTraceIdFunction(fn: () => string): void;
6
+ /**
7
+ * 生成随机不重复id
8
+ */
9
+ export declare function genTraceId(): string;
10
+ export declare function genContext(context?: Context): Context;
11
+ export declare function getContext(): Context;
12
+ export declare function addKeepAliveConnection(connection: KeepAliveConnection<KeepAliveNativeConnection>): void;
13
+ export declare function removeKeepAliveConnection(connection: KeepAliveConnection<KeepAliveNativeConnection>): void;
14
+ export declare function removeKeepAliveConnection(name: string, id: string): void;
15
+ export declare function setLoadBalancePolicy(policy: (name: string) => KeepAliveConnection<KeepAliveNativeConnection>): void;
16
+ export declare function rpc(appName: string, action: string, params: any[], context?: Context): Promise<any>;
17
+ export declare function rpc(connection: KeepAliveConnection<KeepAliveNativeConnection>, action: string, params: any[], context?: Context): Promise<any>;
18
+ export * from './app.js';
19
+ export * from './context.js';
20
+ export * from './config.js';
21
+ export * as logger from './logger.js';
22
+ export * from './keepalive-connection.js';
23
+ export * from './keepalive-connection-sample.js';
@@ -0,0 +1,56 @@
1
+ import { Context, KeepAliveRPCContext } from './context.js';
2
+ import { KeepAliveConnection, KeepAliveConnectionData, KeepAliveNativeConnection, KeepAliveConnectionAdapter, KeepAliveSyncConnectionsQuery } from './keepalive-connection.js';
3
+ import { ReturnsBody } from './index.js';
4
+ export interface SampleKeepAliveNativeConnection extends KeepAliveNativeConnection {
5
+ connected: boolean;
6
+ connect?(): void;
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;
12
+ }
13
+ export declare abstract class SampleKeepAliveConnectionAdapter<NativeConnection extends SampleKeepAliveNativeConnection> implements KeepAliveConnectionAdapter<NativeConnection> {
14
+ abstract name: string;
15
+ OOXEvent: {
16
+ CONNECT: string;
17
+ DISCONNECT: string;
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;
26
+ };
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>;
39
+ /**
40
+ * 发送事件
41
+ * @param socket
42
+ * @param event
43
+ * @param params
44
+ */
45
+ emit(socket: NativeConnection, event: string, params: any[]): Promise<unknown>;
46
+ /**
47
+ * RPC
48
+ */
49
+ rpc(connection: KeepAliveConnection<NativeConnection>, action: string, params: any[], context?: KeepAliveRPCContext): Promise<any>;
50
+ /**
51
+ * 绑定 call 事件
52
+ * @param connection
53
+ */
54
+ bindCall(connection: KeepAliveConnection<NativeConnection>): void;
55
+ call(action: string, params: any[], context: Context, callback?: (returns: any) => void): Promise<ReturnsBody<any>>;
56
+ }
@@ -0,0 +1,104 @@
1
+ import EventEmitter from 'events';
2
+ import { Context } from './context.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 {};
@@ -0,0 +1,5 @@
1
+ export declare function info(...content: any[]): void;
2
+ export declare function warn(...content: any[]): void;
3
+ export declare function error(...content: any[]): void;
4
+ export declare function tag(name: string): void;
5
+ export declare function trace(name?: string): void;
@@ -0,0 +1,7 @@
1
+ import { AppContext } from './context.js';
2
+ export type OOX_TRACER = Record<string, (...args: any[]) => any>;
3
+ export declare const contexts: Record<string, AppContext>;
4
+ export declare function parseTraceIdByStack(stack: string): string | null;
5
+ export declare function enterWith(context: AppContext): void;
6
+ export declare function exitWith(context: AppContext): void;
7
+ export declare function getContext(): AppContext;
@@ -0,0 +1,5 @@
1
+ export declare function getAllCallablePropertyNames(obj: any): string[];
2
+ export declare function genKVMethods(methods: any, kvMethods?: Map<string, Function>, sourceKVMethods?: Map<string, Function>, nameStack?: string[]): {
3
+ kvMethods: Map<string, Function>;
4
+ sourceKVMethods: Map<string, Function>;
5
+ };
package/utils.js ADDED
@@ -0,0 +1,40 @@
1
+ export function getAllCallablePropertyNames(obj) {
2
+ if (!obj)
3
+ return [];
4
+ let props = [], tmpProps = [], index = 0, size = 0, tmpProp = '';
5
+ const bans = ["constructor", "__defineGetter__", "__defineSetter__",
6
+ "hasOwnProperty", "__lookupGetter__", "__lookupSetter__",
7
+ "isPrototypeOf", "propertyIsEnumerable", "toString",
8
+ "valueOf", "__proto__", "toLocaleString"];
9
+ do {
10
+ tmpProps = Object.getOwnPropertyNames(obj);
11
+ index = -1, size = tmpProps.length;
12
+ while (++index < size) {
13
+ tmpProp = tmpProps[index];
14
+ if (!props.includes(tmpProp) && !bans.includes(tmpProp)) {
15
+ props.push(tmpProp);
16
+ }
17
+ }
18
+ } while (obj = Object.getPrototypeOf(obj));
19
+ return props;
20
+ }
21
+ export function genKVMethods(methods, kvMethods = new Map(), sourceKVMethods = new Map(), nameStack = []) {
22
+ let keys = getAllCallablePropertyNames(methods);
23
+ for (const key of keys) {
24
+ /**
25
+ * @type {Function}
26
+ */
27
+ let val = methods[key];
28
+ if ('function' === typeof val) {
29
+ const action = nameStack.concat(key).join('.');
30
+ // 原函数绑定
31
+ sourceKVMethods.set(action, val);
32
+ // 壳函数绑定
33
+ kvMethods.set(action, val.bind(methods));
34
+ }
35
+ else {
36
+ genKVMethods(val, kvMethods, sourceKVMethods, nameStack.concat(key));
37
+ }
38
+ }
39
+ return { kvMethods, sourceKVMethods };
40
+ }