oox 0.3.5 → 0.3.7

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.
Files changed (42) hide show
  1. package/README.md +1 -1
  2. package/app.js +1 -7
  3. package/bin/cli.js +0 -5
  4. package/bin/proxy-require.js +1 -23
  5. package/bin/starter.js +9 -6
  6. package/config.js +30 -1
  7. package/context.js +58 -0
  8. package/index.js +6 -146
  9. package/keepalive-connection.js +61 -1
  10. package/logger.js +2 -1
  11. package/modules/http/index.js +4 -1
  12. package/modules/index.js +0 -3
  13. package/modules/module.js +1 -1
  14. package/package.json +6 -5
  15. package/proxy.js +30 -0
  16. package/registry.js +1 -1
  17. package/samples/index.js +1 -1
  18. package/samples/keepalive-connection-sample.js +46 -33
  19. package/types/app.d.ts +1 -7
  20. package/types/bin/starter.d.ts +2 -1
  21. package/types/config.d.ts +13 -1
  22. package/types/context.d.ts +30 -0
  23. package/types/index.d.ts +6 -52
  24. package/types/keepalive-connection.d.ts +12 -1
  25. package/types/modules/http/index.d.ts +2 -1
  26. package/types/modules/index.d.ts +1 -3
  27. package/types/modules/module.d.ts +2 -1
  28. package/types/proxy.d.ts +1 -0
  29. package/types/registry.d.ts +1 -1
  30. package/types/samples/index.d.ts +1 -1
  31. package/types/samples/keepalive-connection-sample.d.ts +23 -22
  32. package/utils.js +1 -1
  33. package/modules/socketio/adapter.js +0 -49
  34. package/modules/socketio/index.js +0 -23
  35. package/modules/socketio/server.js +0 -200
  36. package/modules/socketio/socket.js +0 -1
  37. package/modules/socketio/utils.js +0 -41
  38. package/types/modules/socketio/adapter.d.ts +0 -14
  39. package/types/modules/socketio/index.d.ts +0 -4
  40. package/types/modules/socketio/server.d.ts +0 -44
  41. package/types/modules/socketio/socket.d.ts +0 -8
  42. package/types/modules/socketio/utils.d.ts +0 -10
@@ -1,6 +1,14 @@
1
- import * as oox from '../index.js';
1
+ import * as registry from '../registry.js';
2
+ import { config } from '../config.js';
3
+ import { getContext, genContext, } from '../context.js';
4
+ import { KeepAliveConnectionError, addKeepAliveConnection, removeKeepAliveConnection, } from '../keepalive-connection.js';
5
+ import { call, } from '../app.js';
2
6
  export class SampleKeepAliveConnectionAdapter {
3
7
  OOXEvent = {
8
+ CONNECT: 'connect',
9
+ DISCONNECT: 'disconnect',
10
+ ERROR: 'error',
11
+ CALL: 'call',
4
12
  READY: 'oox:ready',
5
13
  ENABLED: 'oox:enabled',
6
14
  DISABLED: 'oox:disabled',
@@ -8,11 +16,6 @@ export class SampleKeepAliveConnectionAdapter {
8
16
  REGISTRY_SUBSCRIBE: 'oox:registry:subscribe',
9
17
  REGISTRY_NOTIFY: 'oox:registry:notify',
10
18
  };
11
- nativeEvent = {
12
- CONNECT: 'connect',
13
- DISCONNECT: 'disconnect',
14
- ERROR: 'error',
15
- };
16
19
  /**
17
20
  * 通知连接列表
18
21
  */
@@ -29,10 +32,10 @@ export class SampleKeepAliveConnectionAdapter {
29
32
  const socket = connection.nativeConnection;
30
33
  socket.on(this.OOXEvent.READY, (params) => {
31
34
  connection.ready(params);
32
- socket.emit(this.OOXEvent.REGISTRY_SYNC_CONNECTIONS, oox.registry.syncConnections);
35
+ socket.emit(this.OOXEvent.REGISTRY_SYNC_CONNECTIONS, registry.syncConnections);
33
36
  });
34
37
  socket.on(this.OOXEvent.REGISTRY_NOTIFY, (datas) => {
35
- oox.registry.syncConnections({
38
+ registry.syncConnections({
36
39
  success: true,
37
40
  body: datas,
38
41
  });
@@ -43,35 +46,42 @@ export class SampleKeepAliveConnectionAdapter {
43
46
  socket.on(this.OOXEvent.DISABLED, () => {
44
47
  connection.enabled = false;
45
48
  });
46
- socket.on(this.nativeEvent.DISCONNECT, (reason) => {
47
- oox.removeKeepAliveConnection(connection);
49
+ socket.on(this.OOXEvent.DISCONNECT, () => {
48
50
  socket.removeAllListeners();
51
+ removeKeepAliveConnection(connection);
49
52
  connection.emit('disconnect');
50
53
  });
51
- socket.on(this.nativeEvent.CONNECT, () => {
54
+ socket.on(this.OOXEvent.CONNECT, () => {
52
55
  connection.emit('connect');
53
56
  });
54
- socket.on(this.nativeEvent.ERROR, (error) => {
55
- oox.removeKeepAliveConnection(connection);
57
+ socket.on(this.OOXEvent.ERROR, (error) => {
56
58
  socket.removeAllListeners();
57
- connection.emit('error', new oox.KeepAliveConnectionError(error.message, connection));
59
+ removeKeepAliveConnection(connection);
60
+ connection.emit('error', new KeepAliveConnectionError(error.message, connection));
58
61
  });
59
62
  }
60
63
  async open(identify) {
61
64
  const connection = this.newConnection(identify);
62
- oox.addKeepAliveConnection(connection);
65
+ addKeepAliveConnection(connection);
63
66
  this.bindConnectionEvents(connection);
64
- this.bindCall(connection);
65
- await new Promise((resolve, reject) => {
66
- connection.once('enabled', resolve);
67
- connection.once('error', reject);
68
- connection.once('disconnect', () => {
69
- reject(new Error('disconnect'));
67
+ try {
68
+ await new Promise((resolve, reject) => {
69
+ connection.once('enabled', resolve);
70
+ connection.once('error', reject);
71
+ connection.once('disconnect', () => {
72
+ reject(new Error('disconnect'));
73
+ });
74
+ if (connection.nativeConnection.connect) {
75
+ connection.nativeConnection.connect();
76
+ }
70
77
  });
71
- if (connection.nativeConnection.connect) {
72
- connection.nativeConnection.connect();
73
- }
74
- });
78
+ }
79
+ catch (error) {
80
+ connection.removeAllListeners();
81
+ removeKeepAliveConnection(connection);
82
+ throw error;
83
+ }
84
+ this.bindCall(connection);
75
85
  return connection;
76
86
  }
77
87
  async close(connection) {
@@ -93,9 +103,9 @@ export class SampleKeepAliveConnectionAdapter {
93
103
  reject(new Error(message));
94
104
  };
95
105
  // RPC 执行时中断连接
96
- socket.once(this.nativeEvent.DISCONNECT, onError);
106
+ socket.once(this.OOXEvent.DISCONNECT, onError);
97
107
  socket.emit(event, ...params, (returns) => {
98
- socket.off(this.nativeEvent.DISCONNECT, onError);
108
+ socket.off(this.OOXEvent.DISCONNECT, onError);
99
109
  resolve(returns);
100
110
  });
101
111
  });
@@ -105,12 +115,12 @@ export class SampleKeepAliveConnectionAdapter {
105
115
  */
106
116
  async rpc(connection, action, params, context) {
107
117
  if (!context)
108
- context = oox.getContext();
118
+ context = getContext();
109
119
  const miniContext = {
110
120
  sourceIP: context.sourceIP,
111
121
  traceId: context.traceId,
112
122
  };
113
- const { success, error, body } = await this.emit(connection.nativeConnection, 'call', [action, params, miniContext]);
123
+ const { success, error, body } = await this.emit(connection.nativeConnection, this.OOXEvent.CALL, [action, params, miniContext]);
114
124
  if (success)
115
125
  return body;
116
126
  else if (error)
@@ -123,9 +133,12 @@ export class SampleKeepAliveConnectionAdapter {
123
133
  * @param connection
124
134
  */
125
135
  bindCall(connection) {
136
+ if (!connection.enabled)
137
+ throw new Error('Connection not enabled');
126
138
  const { id, name, ip } = connection.data;
127
- connection.nativeConnection.on('call', async (action, params, context, callback) => {
128
- const validContext = new oox.Context({
139
+ connection.nativeConnection.on(this.OOXEvent.CALL, async (action, params, context, callback) => {
140
+ const validContext = genContext({
141
+ sourceIP: ip,
129
142
  ...context,
130
143
  ip: ip,
131
144
  caller: name,
@@ -136,8 +149,8 @@ export class SampleKeepAliveConnectionAdapter {
136
149
  });
137
150
  }
138
151
  async call(action, params, context, callback) {
139
- const returns = await oox.call(action, params, context);
140
- if (returns.error && !oox.config.errorStack) {
152
+ const returns = await call(action, params, context);
153
+ if (returns.error && !config.errorStack) {
141
154
  // 不返回错误调用栈信息
142
155
  delete returns.error.stack;
143
156
  }
package/types/app.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { EventEmitter } from 'node:events';
2
- import { AsyncLocalStorage } from 'node:async_hooks';
3
- export * as logger from './logger.js';
2
+ import { AppContext } from './context.js';
4
3
  export interface ReturnsBody<T = any> {
5
4
  traceId?: string;
6
5
  success: boolean;
@@ -10,11 +9,6 @@ export interface ReturnsBody<T = any> {
10
9
  stack?: string;
11
10
  };
12
11
  }
13
- export declare class AppContext {
14
- [x: string]: any;
15
- traceId?: string | undefined;
16
- }
17
- export declare const asyncStore: AsyncLocalStorage<AppContext>;
18
12
  export declare const eventHub: EventEmitter<[never]>;
19
13
  /**
20
14
  * the kvMethods is all actions refs [has bind this]
@@ -1,4 +1,5 @@
1
+ import * as oox from '../index.js';
1
2
  export declare function configure(env?: {
2
3
  [x: string]: any;
3
- }, entryFilename?: string): Promise<import("../config.js").Config>;
4
+ }, entryFilename?: string): Promise<oox.Config>;
4
5
  export declare function startup(): Promise<void>;
package/types/config.d.ts CHANGED
@@ -48,7 +48,19 @@ export declare class Config implements ConfigInterface {
48
48
  type StrictFormatter<T> = {
49
49
  [K in keyof T]: (value: any) => T[K];
50
50
  };
51
- export declare const formatters: StrictFormatter<ConfigInterface> & {
51
+ export declare const configFormatters: StrictFormatter<ConfigInterface> & {
52
52
  [x: string]: (v: any) => any;
53
53
  };
54
+ /**
55
+ * 检查连接是否被允许
56
+ * @param ip 连接主机 IP
57
+ * @param caller 连接服务名称
58
+ * @param token 令牌
59
+ * @returns 是否被允许
60
+ */
61
+ export declare function checkAllow(ip: string, caller: string, token: string): {
62
+ allow: boolean;
63
+ reason?: string;
64
+ };
65
+ export declare const config: Config;
54
66
  export {};
@@ -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,59 +1,13 @@
1
- import { AppContext } from './app.js';
2
- import * as registry from './registry.js';
3
- import { Config } from './config.js';
4
- import Module, { ModuleConfig } from './modules/module.js';
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
- sourceIP: string;
12
- ip: string;
13
- caller: string;
14
- callerId: string;
15
- connection?: KeepAliveConnection<KeepAliveNativeConnection>;
16
- constructor(context?: Partial<Context>);
17
- toJSON?(): {} & this;
18
- }
19
- export declare const config: Config;
20
- export declare function setGenTraceIdFunction(fn: () => string): void;
21
- /**
22
- * 生成随机不重复id
23
- */
24
- export declare function genTraceId(): string;
25
- /**
26
- * 获取链路跟踪上下文
27
- */
28
- export declare function genContext(context?: Context): Context;
29
- export declare function getContext(): Context;
30
1
  export declare function serve(): Promise<void>;
31
2
  export declare function stop(): Promise<void>;
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
- export declare function removeKeepAliveConnection(name: string, id: string): void;
51
- export declare function setLoadBalancePolicy(policy: (name: string) => KeepAliveConnection<KeepAliveNativeConnection>): void;
52
- export declare function rpc(appName: string, action: string, params: any[], context?: Context): Promise<any>;
53
- export declare function rpc(connection: KeepAliveConnection<KeepAliveNativeConnection>, action: string, params: any[], context?: Context): Promise<any>;
54
3
  export * from './app.js';
4
+ export * from './config.js';
5
+ export * from './context.js';
55
6
  export * from './keepalive-connection.js';
56
- export { Module, ModuleConfig };
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';
57
11
  export { registry };
58
12
  import * as modules from './modules/index.js';
59
13
  export { modules };
@@ -1,5 +1,5 @@
1
1
  import EventEmitter from 'node:events';
2
- import { Context } from './index.js';
2
+ import { Context } from './context.js';
3
3
  type MakeOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
4
4
  export declare class KeepAliveConnectionError extends Error {
5
5
  connection: KeepAliveConnection<KeepAliveNativeConnection>;
@@ -98,6 +98,17 @@ export declare class KeepAliveConnectionStore<T extends KeepAliveNativeConnectio
98
98
  remove(connection: KeepAliveConnection<T>): void;
99
99
  remove(name: string, id: string): void;
100
100
  }
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>;
101
112
  export declare const keepAliveConnectionAdapters: Map<string, KeepAliveConnectionAdapter<KeepAliveNativeConnection>>;
102
113
  export declare const keepAliveConnections: KeepAliveConnectionStore<KeepAliveNativeConnection>;
103
114
  export declare const enabledKeepAliveConnections: KeepAliveConnectionStore<KeepAliveNativeConnection>;
@@ -1,7 +1,7 @@
1
1
  import * as http from 'node:http';
2
2
  import * as https from 'node:https';
3
3
  import Router, { Middleware } from './router.js';
4
- import Module, { ModuleConfig } from '../module.js';
4
+ import { Module, ModuleConfig } from '../module.js';
5
5
  export declare class HTTPConfig implements ModuleConfig {
6
6
  enabled: boolean;
7
7
  port: number;
@@ -27,6 +27,7 @@ export default class HTTPModule extends Module {
27
27
  use(middleware: Middleware): this;
28
28
  setConfig(config: HTTPConfig): void;
29
29
  getConfig(): HTTPConfig;
30
+ get serviceURL(): URL;
30
31
  /**
31
32
  * start http service
32
33
  */
@@ -1,6 +1,5 @@
1
- import Module from './module.js';
1
+ import { Module } from './module.js';
2
2
  import HTTP from './http/index.js';
3
- import SocketIO from './socketio/index.js';
4
3
  /**
5
4
  * FIFO queue for modules starting
6
5
  */
@@ -35,5 +34,4 @@ export declare function stop(): Promise<void>;
35
34
  */
36
35
  export declare const builtins: {
37
36
  http: HTTP;
38
- socketio: SocketIO;
39
37
  };
@@ -1,9 +1,10 @@
1
1
  export interface ModuleConfig {
2
2
  enabled?: boolean;
3
3
  }
4
- export default abstract class Module {
4
+ export declare abstract class Module {
5
5
  abstract config: ModuleConfig;
6
6
  abstract name: string;
7
+ abstract serviceURL?: URL;
7
8
  abstract setConfig(config: any): void;
8
9
  abstract getConfig(): ModuleConfig;
9
10
  abstract serve(): Promise<void>;
@@ -0,0 +1 @@
1
+ export declare function proxy(name: string, action?: string): () => void;
@@ -1,4 +1,4 @@
1
- import { ReturnsBody } from './index.js';
1
+ import { ReturnsBody } from './app.js';
2
2
  import { KeepAliveConnection, KeepAliveConnectionData, KeepAliveNativeConnection } from './keepalive-connection.js';
3
3
  export interface SyncConnectionsQuery {
4
4
  name?: string | string[];
@@ -1 +1 @@
1
- export { SampleKeepAliveConnectionAdapter, SampleKeepAliveNativeConnection } from './keepalive-connection-sample.js';
1
+ export * from './keepalive-connection-sample.js';
@@ -1,16 +1,22 @@
1
- import * as oox from '../index.js';
2
- export interface SampleKeepAliveNativeConnection extends oox.KeepAliveNativeConnection {
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
- 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;
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;
10
12
  }
11
- export declare abstract class SampleKeepAliveConnectionAdapter<NativeConnection extends SampleKeepAliveNativeConnection> implements oox.KeepAliveConnectionAdapter<NativeConnection> {
13
+ export declare abstract class SampleKeepAliveConnectionAdapter<NativeConnection extends SampleKeepAliveNativeConnection> implements KeepAliveConnectionAdapter<NativeConnection> {
12
14
  abstract name: string;
13
15
  OOXEvent: {
16
+ CONNECT: string;
17
+ DISCONNECT: string;
18
+ ERROR: string;
19
+ CALL: string;
14
20
  READY: string;
15
21
  ENABLED: string;
16
22
  DISABLED: string;
@@ -18,23 +24,18 @@ export declare abstract class SampleKeepAliveConnectionAdapter<NativeConnection
18
24
  REGISTRY_SUBSCRIBE: string;
19
25
  REGISTRY_NOTIFY: string;
20
26
  };
21
- nativeEvent: {
22
- CONNECT: string;
23
- DISCONNECT: string;
24
- ERROR: string;
25
- };
26
- abstract newConnection(identify: string | URL | oox.KeepAliveConnectionData): oox.KeepAliveConnection<NativeConnection>;
27
+ abstract newConnection(identify: string | URL | KeepAliveConnectionData): KeepAliveConnection<NativeConnection>;
27
28
  /**
28
29
  * 通知连接列表
29
30
  */
30
- ['registry:notify'](connection: oox.KeepAliveConnection<NativeConnection>, datas: oox.KeepAliveConnectionData[]): void;
31
+ ['registry:notify'](connection: KeepAliveConnection<NativeConnection>, datas: KeepAliveConnectionData[]): void;
31
32
  /**
32
33
  * 订阅连接列表
33
34
  */
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>;
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>;
38
39
  /**
39
40
  * 发送事件
40
41
  * @param socket
@@ -45,11 +46,11 @@ export declare abstract class SampleKeepAliveConnectionAdapter<NativeConnection
45
46
  /**
46
47
  * RPC
47
48
  */
48
- rpc(connection: oox.KeepAliveConnection<NativeConnection>, action: string, params: any[], context?: oox.KeepAliveRPCContext): Promise<any>;
49
+ rpc(connection: KeepAliveConnection<NativeConnection>, action: string, params: any[], context?: KeepAliveRPCContext): Promise<any>;
49
50
  /**
50
51
  * 绑定 call 事件
51
52
  * @param connection
52
53
  */
53
- bindCall(connection: oox.KeepAliveConnection<NativeConnection>): void;
54
- call(action: string, params: any[], context: oox.Context, callback?: (returns: any) => void): Promise<oox.ReturnsBody<any>>;
54
+ bindCall(connection: KeepAliveConnection<NativeConnection>): void;
55
+ call(action: string, params: any[], context: Context, callback?: (returns: any) => void): Promise<ReturnsBody<any>>;
55
56
  }
package/utils.js CHANGED
@@ -1,4 +1,4 @@
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 = [];
@@ -1,49 +0,0 @@
1
- import * as SocketIOClient from 'socket.io-client';
2
- import * as oox from '../../index.js';
3
- import { OOXEvent, genWebSocketURL } from './utils.js';
4
- import { randomUUID } from 'node:crypto';
5
- import { SampleKeepAliveConnectionAdapter } from '../../samples/index.js';
6
- export default class SocketIOAdapter extends SampleKeepAliveConnectionAdapter {
7
- name = 'socketio';
8
- OOXEvent = OOXEvent;
9
- nativeEvent = { CONNECT: 'connect', DISCONNECT: 'disconnect', ERROR: 'connect_error' };
10
- newConnection(identify) {
11
- const { id, name } = oox.config;
12
- const headers = {
13
- 'x-caller': name,
14
- 'x-caller-id': id,
15
- };
16
- let mURL;
17
- const connectionData = {
18
- name: 'anonymous',
19
- id: randomUUID(),
20
- adapter: this.name,
21
- ip: '',
22
- token: ''
23
- };
24
- if ('string' === typeof identify) {
25
- mURL = genWebSocketURL(identify);
26
- }
27
- else if (identify instanceof URL) {
28
- mURL = identify;
29
- }
30
- else if (identify.url) {
31
- // KeepAliveConnectionData
32
- Object.assign(connectionData, identify);
33
- mURL = new URL(identify.url);
34
- if (identify.token) {
35
- headers['x-token'] = identify.token;
36
- }
37
- }
38
- else {
39
- throw new Error('identify must be string, URL, or KeepAliveConnectionData');
40
- }
41
- const socket = SocketIOClient.io(mURL.origin, {
42
- extraHeaders: headers,
43
- path: mURL.pathname,
44
- autoConnect: false,
45
- });
46
- const connection = new oox.KeepAliveConnection(this, socket, connectionData);
47
- return connection;
48
- }
49
- }
@@ -1,23 +0,0 @@
1
- import * as PATH from 'node:path';
2
- import * as oox from '../../index.js';
3
- import SocketINServer from './server.js';
4
- export default class SocketIOModule extends SocketINServer {
5
- async serve() {
6
- await this.stop();
7
- const _http = oox.modules.builtins.http;
8
- const httpConfig = _http.getConfig(), config = this.getConfig();
9
- let isShareServer = false;
10
- // 都没设置端口
11
- isShareServer = !httpConfig.port && !config.port;
12
- // 都设置相同端口
13
- isShareServer ||= httpConfig.port === config.port;
14
- // http 模块未被禁用
15
- isShareServer &&= httpConfig.enabled;
16
- if (isShareServer) {
17
- config.path = PATH.posix.join(httpConfig.path, config.path);
18
- this.server = _http.server;
19
- this.config.ssl = _http.config.ssl;
20
- }
21
- await super.serve();
22
- }
23
- }