@roomkit/gateway 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.
- package/README.md +93 -0
- package/dist/src/admin/admin.controller.d.ts +187 -0
- package/dist/src/admin/admin.controller.d.ts.map +1 -0
- package/dist/src/admin/admin.controller.js +187 -0
- package/dist/src/admin/admin.controller.js.map +1 -0
- package/dist/src/admin/admin.module.d.ts +3 -0
- package/dist/src/admin/admin.module.d.ts.map +1 -0
- package/dist/src/admin/admin.module.js +28 -0
- package/dist/src/admin/admin.module.js.map +1 -0
- package/dist/src/admin/admin.service.d.ts +196 -0
- package/dist/src/admin/admin.service.d.ts.map +1 -0
- package/dist/src/admin/admin.service.js +322 -0
- package/dist/src/admin/admin.service.js.map +1 -0
- package/dist/src/config/config.module.d.ts +3 -0
- package/dist/src/config/config.module.d.ts.map +1 -0
- package/dist/src/config/config.module.js +22 -0
- package/dist/src/config/config.module.js.map +1 -0
- package/dist/src/config/config.service.d.ts +19 -0
- package/dist/src/config/config.service.d.ts.map +1 -0
- package/dist/src/config/config.service.js +55 -0
- package/dist/src/config/config.service.js.map +1 -0
- package/dist/src/config/gateway.config.d.ts +64 -0
- package/dist/src/config/gateway.config.d.ts.map +1 -0
- package/dist/src/config/gateway.config.js +43 -0
- package/dist/src/config/gateway.config.js.map +1 -0
- package/dist/src/connection/connection.module.d.ts +3 -0
- package/dist/src/connection/connection.module.d.ts.map +1 -0
- package/dist/src/connection/connection.module.js +22 -0
- package/dist/src/connection/connection.module.js.map +1 -0
- package/dist/src/connection/connection.service.d.ts +93 -0
- package/dist/src/connection/connection.service.d.ts.map +1 -0
- package/dist/src/connection/connection.service.js +257 -0
- package/dist/src/connection/connection.service.js.map +1 -0
- package/dist/src/gateway.module.d.ts +8 -0
- package/dist/src/gateway.module.d.ts.map +1 -0
- package/dist/src/gateway.module.js +44 -0
- package/dist/src/gateway.module.js.map +1 -0
- package/dist/src/index.d.ts +38 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +97 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/lifecycle/graceful-shutdown.service.d.ts +65 -0
- package/dist/src/lifecycle/graceful-shutdown.service.d.ts.map +1 -0
- package/dist/src/lifecycle/graceful-shutdown.service.js +187 -0
- package/dist/src/lifecycle/graceful-shutdown.service.js.map +1 -0
- package/dist/src/lifecycle/index.d.ts +2 -0
- package/dist/src/lifecycle/index.d.ts.map +1 -0
- package/dist/src/lifecycle/index.js +18 -0
- package/dist/src/lifecycle/index.js.map +1 -0
- package/dist/src/main.d.ts +2 -0
- package/dist/src/main.d.ts.map +1 -0
- package/dist/src/main.js +56 -0
- package/dist/src/main.js.map +1 -0
- package/dist/src/metrics/metrics.controller.d.ts +13 -0
- package/dist/src/metrics/metrics.controller.d.ts.map +1 -0
- package/dist/src/metrics/metrics.controller.js +42 -0
- package/dist/src/metrics/metrics.controller.js.map +1 -0
- package/dist/src/metrics/metrics.module.d.ts +3 -0
- package/dist/src/metrics/metrics.module.d.ts.map +1 -0
- package/dist/src/metrics/metrics.module.js +26 -0
- package/dist/src/metrics/metrics.module.js.map +1 -0
- package/dist/src/metrics/metrics.service.d.ts +81 -0
- package/dist/src/metrics/metrics.service.d.ts.map +1 -0
- package/dist/src/metrics/metrics.service.js +255 -0
- package/dist/src/metrics/metrics.service.js.map +1 -0
- package/dist/src/ratelimit/rate-limit.module.d.ts +3 -0
- package/dist/src/ratelimit/rate-limit.module.d.ts.map +1 -0
- package/dist/src/ratelimit/rate-limit.module.js +23 -0
- package/dist/src/ratelimit/rate-limit.module.js.map +1 -0
- package/dist/src/ratelimit/rate-limiter.service.d.ts +68 -0
- package/dist/src/ratelimit/rate-limiter.service.d.ts.map +1 -0
- package/dist/src/ratelimit/rate-limiter.service.js +201 -0
- package/dist/src/ratelimit/rate-limiter.service.js.map +1 -0
- package/dist/src/redis/redis.module.d.ts +3 -0
- package/dist/src/redis/redis.module.d.ts.map +1 -0
- package/dist/src/redis/redis.module.js +22 -0
- package/dist/src/redis/redis.module.js.map +1 -0
- package/dist/src/redis/redis.service.d.ts +68 -0
- package/dist/src/redis/redis.service.d.ts.map +1 -0
- package/dist/src/redis/redis.service.js +281 -0
- package/dist/src/redis/redis.service.js.map +1 -0
- package/dist/src/session/session.module.d.ts +3 -0
- package/dist/src/session/session.module.d.ts.map +1 -0
- package/dist/src/session/session.module.js +21 -0
- package/dist/src/session/session.module.js.map +1 -0
- package/dist/src/session/session.service.d.ts +64 -0
- package/dist/src/session/session.service.d.ts.map +1 -0
- package/dist/src/session/session.service.js +206 -0
- package/dist/src/session/session.service.js.map +1 -0
- package/dist/src/ws/message-router.service.d.ts +133 -0
- package/dist/src/ws/message-router.service.d.ts.map +1 -0
- package/dist/src/ws/message-router.service.js +715 -0
- package/dist/src/ws/message-router.service.js.map +1 -0
- package/dist/src/ws/response-handler.service.d.ts +53 -0
- package/dist/src/ws/response-handler.service.d.ts.map +1 -0
- package/dist/src/ws/response-handler.service.js +282 -0
- package/dist/src/ws/response-handler.service.js.map +1 -0
- package/dist/src/ws/ws.gateway.d.ts +46 -0
- package/dist/src/ws/ws.gateway.d.ts.map +1 -0
- package/dist/src/ws/ws.gateway.js +244 -0
- package/dist/src/ws/ws.gateway.js.map +1 -0
- package/dist/src/ws/ws.module.d.ts +8 -0
- package/dist/src/ws/ws.module.d.ts.map +1 -0
- package/dist/src/ws/ws.module.js +39 -0
- package/dist/src/ws/ws.module.js.map +1 -0
- package/dist/test/connection/connection.service.spec.d.ts +2 -0
- package/dist/test/connection/connection.service.spec.d.ts.map +1 -0
- package/dist/test/connection/connection.service.spec.js +204 -0
- package/dist/test/connection/connection.service.spec.js.map +1 -0
- package/dist/test/e2e/gateway-worker.e2e.spec.d.ts +2 -0
- package/dist/test/e2e/gateway-worker.e2e.spec.d.ts.map +1 -0
- package/dist/test/e2e/gateway-worker.e2e.spec.js +412 -0
- package/dist/test/e2e/gateway-worker.e2e.spec.js.map +1 -0
- package/dist/test/integration/admin-api.spec.d.ts +2 -0
- package/dist/test/integration/admin-api.spec.d.ts.map +1 -0
- package/dist/test/integration/admin-api.spec.js +218 -0
- package/dist/test/integration/admin-api.spec.js.map +1 -0
- package/dist/test/ratelimit/rate-limiter.service.spec.d.ts +2 -0
- package/dist/test/ratelimit/rate-limiter.service.spec.d.ts.map +1 -0
- package/dist/test/ratelimit/rate-limiter.service.spec.js +139 -0
- package/dist/test/ratelimit/rate-limiter.service.spec.js.map +1 -0
- package/dist/test/setup.d.ts +2 -0
- package/dist/test/setup.d.ts.map +1 -0
- package/dist/test/setup.js +56 -0
- package/dist/test/setup.js.map +1 -0
- package/dist/test/ws/message-router.service.spec.d.ts +2 -0
- package/dist/test/ws/message-router.service.spec.d.ts.map +1 -0
- package/dist/test/ws/message-router.service.spec.js +403 -0
- package/dist/test/ws/message-router.service.spec.js.map +1 -0
- package/package.json +78 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gateway Configuration Types
|
|
3
|
+
*/
|
|
4
|
+
export interface RedisConfig {
|
|
5
|
+
/** Redis URL (e.g., redis://localhost:6379 or redis://:password@host:port) */
|
|
6
|
+
url?: string;
|
|
7
|
+
/** Redis host */
|
|
8
|
+
host?: string;
|
|
9
|
+
/** Redis port */
|
|
10
|
+
port?: number;
|
|
11
|
+
/** Redis password */
|
|
12
|
+
password?: string;
|
|
13
|
+
/** Redis database index */
|
|
14
|
+
db?: number;
|
|
15
|
+
}
|
|
16
|
+
export interface WebSocketConfig {
|
|
17
|
+
/** WebSocket server port */
|
|
18
|
+
port?: number;
|
|
19
|
+
/** Maximum connections per gateway */
|
|
20
|
+
maxConnections?: number;
|
|
21
|
+
/** Heartbeat interval in milliseconds */
|
|
22
|
+
heartbeatInterval?: number;
|
|
23
|
+
/** Heartbeat timeout in milliseconds */
|
|
24
|
+
heartbeatTimeout?: number;
|
|
25
|
+
}
|
|
26
|
+
export interface RateLimitConfig {
|
|
27
|
+
/** New connections per second */
|
|
28
|
+
connections?: number;
|
|
29
|
+
/** Messages per connection per second */
|
|
30
|
+
messages?: number;
|
|
31
|
+
/** Message burst limit */
|
|
32
|
+
burst?: number;
|
|
33
|
+
/** Global messages per second */
|
|
34
|
+
global?: number;
|
|
35
|
+
}
|
|
36
|
+
export interface AdminConfig {
|
|
37
|
+
/** Admin API port */
|
|
38
|
+
port?: number;
|
|
39
|
+
/** Enable admin API */
|
|
40
|
+
enabled?: boolean;
|
|
41
|
+
}
|
|
42
|
+
export interface GatewayConfig {
|
|
43
|
+
/** Gateway unique identifier (auto-generated if not provided) */
|
|
44
|
+
id?: string;
|
|
45
|
+
/** Redis configuration */
|
|
46
|
+
redis: RedisConfig | string;
|
|
47
|
+
/** WebSocket configuration */
|
|
48
|
+
ws?: WebSocketConfig;
|
|
49
|
+
/** Rate limiting configuration */
|
|
50
|
+
rateLimit?: RateLimitConfig;
|
|
51
|
+
/** Admin API configuration */
|
|
52
|
+
admin?: AdminConfig;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Normalize Redis config from string or object
|
|
56
|
+
*/
|
|
57
|
+
export declare function normalizeRedisConfig(config: RedisConfig | string): RedisConfig;
|
|
58
|
+
/**
|
|
59
|
+
* Default configurations
|
|
60
|
+
*/
|
|
61
|
+
export declare const DEFAULT_WS_CONFIG: Required<WebSocketConfig>;
|
|
62
|
+
export declare const DEFAULT_RATE_LIMIT_CONFIG: Required<RateLimitConfig>;
|
|
63
|
+
export declare const DEFAULT_ADMIN_CONFIG: Required<AdminConfig>;
|
|
64
|
+
//# sourceMappingURL=gateway.config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gateway.config.d.ts","sourceRoot":"","sources":["../../../src/config/gateway.config.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,WAAW,WAAW;IAC1B,8EAA8E;IAC9E,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,iBAAiB;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;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AAM,yBAAyB,EAAE,QAAQ,CAAC,eAAe,CAK/D,CAAC;AAEF,eAAO,MAAM,oBAAoB,EAAE,QAAQ,CAAC,WAAW,CAGtD,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Gateway Configuration Types
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.DEFAULT_ADMIN_CONFIG = exports.DEFAULT_RATE_LIMIT_CONFIG = exports.DEFAULT_WS_CONFIG = void 0;
|
|
7
|
+
exports.normalizeRedisConfig = normalizeRedisConfig;
|
|
8
|
+
/**
|
|
9
|
+
* Normalize Redis config from string or object
|
|
10
|
+
*/
|
|
11
|
+
function normalizeRedisConfig(config) {
|
|
12
|
+
if (typeof config === 'string') {
|
|
13
|
+
// Parse redis://[:password@]host:port[/db]
|
|
14
|
+
const url = new URL(config);
|
|
15
|
+
return {
|
|
16
|
+
host: url.hostname,
|
|
17
|
+
port: parseInt(url.port) || 6379,
|
|
18
|
+
password: url.password || undefined,
|
|
19
|
+
db: url.pathname ? parseInt(url.pathname.slice(1)) : 0,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
return config;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Default configurations
|
|
26
|
+
*/
|
|
27
|
+
exports.DEFAULT_WS_CONFIG = {
|
|
28
|
+
port: 27100,
|
|
29
|
+
maxConnections: 10000,
|
|
30
|
+
heartbeatInterval: 30000,
|
|
31
|
+
heartbeatTimeout: 90000,
|
|
32
|
+
};
|
|
33
|
+
exports.DEFAULT_RATE_LIMIT_CONFIG = {
|
|
34
|
+
connections: 100,
|
|
35
|
+
messages: 50,
|
|
36
|
+
burst: 100,
|
|
37
|
+
global: 50000,
|
|
38
|
+
};
|
|
39
|
+
exports.DEFAULT_ADMIN_CONFIG = {
|
|
40
|
+
port: 27200,
|
|
41
|
+
enabled: true,
|
|
42
|
+
};
|
|
43
|
+
//# sourceMappingURL=gateway.config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gateway.config.js","sourceRoot":"","sources":["../../../src/config/gateway.config.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AA4DH,oDAYC;AAfD;;GAEG;AACH,SAAgB,oBAAoB,CAAC,MAA4B;IAC/D,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QAC/B,2CAA2C;QAC3C,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,CAAC;QAC5B,OAAO;YACL,IAAI,EAAE,GAAG,CAAC,QAAQ;YAClB,IAAI,EAAE,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,IAAI;YAChC,QAAQ,EAAE,GAAG,CAAC,QAAQ,IAAI,SAAS;YACnC,EAAE,EAAE,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;SACvD,CAAC;IACJ,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;GAEG;AACU,QAAA,iBAAiB,GAA8B;IAC1D,IAAI,EAAE,KAAK;IACX,cAAc,EAAE,KAAK;IACrB,iBAAiB,EAAE,KAAK;IACxB,gBAAgB,EAAE,KAAK;CACxB,CAAC;AAEW,QAAA,yBAAyB,GAA8B;IAClE,WAAW,EAAE,GAAG;IAChB,QAAQ,EAAE,EAAE;IACZ,KAAK,EAAE,GAAG;IACV,MAAM,EAAE,KAAK;CACd,CAAC;AAEW,QAAA,oBAAoB,GAA0B;IACzD,IAAI,EAAE,KAAK;IACX,OAAO,EAAE,IAAI;CACd,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection.module.d.ts","sourceRoot":"","sources":["../../../src/connection/connection.module.ts"],"names":[],"mappings":"AAGA,qBAKa,gBAAgB;CAAG"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.ConnectionModule = void 0;
|
|
10
|
+
const common_1 = require("@nestjs/common");
|
|
11
|
+
const connection_service_1 = require("./connection.service");
|
|
12
|
+
let ConnectionModule = class ConnectionModule {
|
|
13
|
+
};
|
|
14
|
+
exports.ConnectionModule = ConnectionModule;
|
|
15
|
+
exports.ConnectionModule = ConnectionModule = __decorate([
|
|
16
|
+
(0, common_1.Global)(),
|
|
17
|
+
(0, common_1.Module)({
|
|
18
|
+
providers: [connection_service_1.ConnectionService],
|
|
19
|
+
exports: [connection_service_1.ConnectionService],
|
|
20
|
+
})
|
|
21
|
+
], ConnectionModule);
|
|
22
|
+
//# sourceMappingURL=connection.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection.module.js","sourceRoot":"","sources":["../../../src/connection/connection.module.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAAgD;AAChD,6DAAyD;AAOlD,IAAM,gBAAgB,GAAtB,MAAM,gBAAgB;CAAG,CAAA;AAAnB,4CAAgB;2BAAhB,gBAAgB;IAL5B,IAAA,eAAM,GAAE;IACR,IAAA,eAAM,EAAC;QACN,SAAS,EAAE,CAAC,sCAAiB,CAAC;QAC9B,OAAO,EAAE,CAAC,sCAAiB,CAAC;KAC7B,CAAC;GACW,gBAAgB,CAAG"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { WebSocket } from 'ws';
|
|
2
|
+
import { Connection, ConnectionStatus } from '@roomkit/core';
|
|
3
|
+
export declare class ConnectionService {
|
|
4
|
+
private readonly logger;
|
|
5
|
+
private connections;
|
|
6
|
+
private sockets;
|
|
7
|
+
private userConnections;
|
|
8
|
+
/**
|
|
9
|
+
* 创建新连接
|
|
10
|
+
*/
|
|
11
|
+
createConnection(socket: WebSocket): Connection;
|
|
12
|
+
/**
|
|
13
|
+
* 获取连接
|
|
14
|
+
*/
|
|
15
|
+
getConnection(connectionId: string): Connection | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* 获取连接的WebSocket
|
|
18
|
+
*/
|
|
19
|
+
getSocket(connectionId: string): WebSocket | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* 根据userId获取连接ID
|
|
22
|
+
*/
|
|
23
|
+
getConnectionIdByUserId(userId: string): string | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* 更新连接状态
|
|
26
|
+
*/
|
|
27
|
+
updateStatus(connectionId: string, status: ConnectionStatus): void;
|
|
28
|
+
/**
|
|
29
|
+
* 设置连接的userId
|
|
30
|
+
*/
|
|
31
|
+
setUserId(connectionId: string, userId: string): void;
|
|
32
|
+
/**
|
|
33
|
+
* 添加房间到连接(支持多房间)
|
|
34
|
+
*/
|
|
35
|
+
addRoom(connectionId: string, roomId: string): void;
|
|
36
|
+
/**
|
|
37
|
+
* 从连接移除房间
|
|
38
|
+
*/
|
|
39
|
+
removeRoom(connectionId: string, roomId: string): void;
|
|
40
|
+
/**
|
|
41
|
+
* 检查连接是否在指定房间
|
|
42
|
+
*/
|
|
43
|
+
isInRoom(connectionId: string, roomId: string): boolean;
|
|
44
|
+
/**
|
|
45
|
+
* 获取连接所在的所有房间
|
|
46
|
+
*/
|
|
47
|
+
getRooms(connectionId: string): string[];
|
|
48
|
+
/**
|
|
49
|
+
* 设置连接的房间(兼容旧代码,推荐使用 addRoom/removeRoom)
|
|
50
|
+
* @deprecated 使用 addRoom/removeRoom 代替
|
|
51
|
+
*/
|
|
52
|
+
setRoom(connectionId: string, roomId: string | undefined): void;
|
|
53
|
+
/**
|
|
54
|
+
* 发送消息给连接(旧格式,使用 msgId)
|
|
55
|
+
*/
|
|
56
|
+
send(connectionId: string, msgId: number, payload: unknown): boolean;
|
|
57
|
+
/**
|
|
58
|
+
* 发送消息给连接(使用 msgType 字符串格式)
|
|
59
|
+
*/
|
|
60
|
+
sendByType(connectionId: string, msgType: string, payload: unknown, reqId?: string): boolean;
|
|
61
|
+
/**
|
|
62
|
+
* 关闭并移除连接
|
|
63
|
+
*/
|
|
64
|
+
removeConnection(connectionId: string, reason?: string): void;
|
|
65
|
+
/**
|
|
66
|
+
* 更新最后活动时间
|
|
67
|
+
*/
|
|
68
|
+
touch(connectionId: string): void;
|
|
69
|
+
/**
|
|
70
|
+
* 获取所有连接数
|
|
71
|
+
*/
|
|
72
|
+
getConnectionCount(): number;
|
|
73
|
+
/**
|
|
74
|
+
* 获取已认证的连接数
|
|
75
|
+
*/
|
|
76
|
+
getAuthenticatedCount(): number;
|
|
77
|
+
/**
|
|
78
|
+
* 检查超时连接
|
|
79
|
+
*/
|
|
80
|
+
checkTimeouts(timeoutMs: number): string[];
|
|
81
|
+
/**
|
|
82
|
+
* 检查空闲连接(认证后但未加入任何房间)
|
|
83
|
+
*
|
|
84
|
+
* @param idleTimeoutMs 空闲超时毫秒数
|
|
85
|
+
* @returns 需要清理的连接ID列表
|
|
86
|
+
*/
|
|
87
|
+
checkIdleConnections(idleTimeoutMs: number): string[];
|
|
88
|
+
/**
|
|
89
|
+
* 获取所有连接(用于广播处理)
|
|
90
|
+
*/
|
|
91
|
+
getAllConnections(): Map<string, Connection>;
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=connection.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"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G;IACH,IAAI,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,OAAO;IAUpE;;OAEG;IACH,UAAU,CAAC,YAAY,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO;IAY5F;;OAEG;IACH,gBAAgB,CAAC,YAAY,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI;IAmB7D;;OAEG;IACH,KAAK,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI;IAOjC;;OAEG;IACH,kBAAkB,IAAI,MAAM;IAI5B;;OAEG;IACH,qBAAqB,IAAI,MAAM;IAU/B;;OAEG;IACH,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE;IAa1C;;;;;OAKG;IACH,oBAAoB,CAAC,aAAa,EAAE,MAAM,GAAG,MAAM,EAAE;IAmBrD;;OAEG;IACH,iBAAiB,IAAI,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC;CAG7C"}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var ConnectionService_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.ConnectionService = void 0;
|
|
11
|
+
const common_1 = require("@nestjs/common");
|
|
12
|
+
const ws_1 = require("ws");
|
|
13
|
+
const uuid_1 = require("uuid");
|
|
14
|
+
let ConnectionService = ConnectionService_1 = class ConnectionService {
|
|
15
|
+
logger = new common_1.Logger(ConnectionService_1.name);
|
|
16
|
+
connections = new Map();
|
|
17
|
+
sockets = new Map();
|
|
18
|
+
userConnections = new Map(); // userId -> connectionId
|
|
19
|
+
/**
|
|
20
|
+
* 创建新连接
|
|
21
|
+
*/
|
|
22
|
+
createConnection(socket) {
|
|
23
|
+
const id = (0, uuid_1.v4)();
|
|
24
|
+
const now = Date.now();
|
|
25
|
+
const connection = {
|
|
26
|
+
id,
|
|
27
|
+
status: 'connected',
|
|
28
|
+
rooms: new Set(), // 初始化空房间集合
|
|
29
|
+
lastActivity: now,
|
|
30
|
+
createdAt: now,
|
|
31
|
+
};
|
|
32
|
+
this.connections.set(id, connection);
|
|
33
|
+
this.sockets.set(id, socket);
|
|
34
|
+
this.logger.debug(`New connection: ${id}`);
|
|
35
|
+
return connection;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 获取连接
|
|
39
|
+
*/
|
|
40
|
+
getConnection(connectionId) {
|
|
41
|
+
return this.connections.get(connectionId);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* 获取连接的WebSocket
|
|
45
|
+
*/
|
|
46
|
+
getSocket(connectionId) {
|
|
47
|
+
return this.sockets.get(connectionId);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* 根据userId获取连接ID
|
|
51
|
+
*/
|
|
52
|
+
getConnectionIdByUserId(userId) {
|
|
53
|
+
return this.userConnections.get(userId);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 更新连接状态
|
|
57
|
+
*/
|
|
58
|
+
updateStatus(connectionId, status) {
|
|
59
|
+
const conn = this.connections.get(connectionId);
|
|
60
|
+
if (conn) {
|
|
61
|
+
conn.status = status;
|
|
62
|
+
conn.lastActivity = Date.now();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* 设置连接的userId
|
|
67
|
+
*/
|
|
68
|
+
setUserId(connectionId, userId) {
|
|
69
|
+
const conn = this.connections.get(connectionId);
|
|
70
|
+
if (conn) {
|
|
71
|
+
conn.userId = userId;
|
|
72
|
+
conn.identifiedAt = Date.now(); // 记录认证时间
|
|
73
|
+
this.userConnections.set(userId, connectionId);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* 添加房间到连接(支持多房间)
|
|
78
|
+
*/
|
|
79
|
+
addRoom(connectionId, roomId) {
|
|
80
|
+
const conn = this.connections.get(connectionId);
|
|
81
|
+
if (conn) {
|
|
82
|
+
conn.rooms.add(roomId);
|
|
83
|
+
// 兼容旧代码:currentRoom 指向最新加入的房间
|
|
84
|
+
conn.currentRoom = roomId;
|
|
85
|
+
conn.status = 'in-room';
|
|
86
|
+
conn.lastActivity = Date.now();
|
|
87
|
+
this.logger.debug(`Connection ${connectionId} joined room ${roomId}, rooms: [${[...conn.rooms].join(', ')}]`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* 从连接移除房间
|
|
92
|
+
*/
|
|
93
|
+
removeRoom(connectionId, roomId) {
|
|
94
|
+
const conn = this.connections.get(connectionId);
|
|
95
|
+
if (conn) {
|
|
96
|
+
conn.rooms.delete(roomId);
|
|
97
|
+
// 更新 currentRoom:如果还有其他房间,指向第一个;否则清空
|
|
98
|
+
if (conn.rooms.size > 0) {
|
|
99
|
+
conn.currentRoom = [...conn.rooms][0];
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
conn.currentRoom = undefined;
|
|
103
|
+
conn.status = 'identified';
|
|
104
|
+
}
|
|
105
|
+
conn.lastActivity = Date.now();
|
|
106
|
+
this.logger.debug(`Connection ${connectionId} left room ${roomId}, rooms: [${[...conn.rooms].join(', ')}]`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* 检查连接是否在指定房间
|
|
111
|
+
*/
|
|
112
|
+
isInRoom(connectionId, roomId) {
|
|
113
|
+
const conn = this.connections.get(connectionId);
|
|
114
|
+
return conn?.rooms.has(roomId) ?? false;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* 获取连接所在的所有房间
|
|
118
|
+
*/
|
|
119
|
+
getRooms(connectionId) {
|
|
120
|
+
const conn = this.connections.get(connectionId);
|
|
121
|
+
return conn ? [...conn.rooms] : [];
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* 设置连接的房间(兼容旧代码,推荐使用 addRoom/removeRoom)
|
|
125
|
+
* @deprecated 使用 addRoom/removeRoom 代替
|
|
126
|
+
*/
|
|
127
|
+
setRoom(connectionId, roomId) {
|
|
128
|
+
const conn = this.connections.get(connectionId);
|
|
129
|
+
if (conn) {
|
|
130
|
+
if (roomId) {
|
|
131
|
+
this.addRoom(connectionId, roomId);
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
// 清空所有房间
|
|
135
|
+
conn.rooms.clear();
|
|
136
|
+
conn.currentRoom = undefined;
|
|
137
|
+
conn.status = 'identified';
|
|
138
|
+
conn.lastActivity = Date.now();
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* 发送消息给连接(旧格式,使用 msgId)
|
|
144
|
+
*/
|
|
145
|
+
send(connectionId, msgId, payload) {
|
|
146
|
+
const socket = this.sockets.get(connectionId);
|
|
147
|
+
if (socket && socket.readyState === ws_1.WebSocket.OPEN) {
|
|
148
|
+
const message = JSON.stringify({ msgId, payload });
|
|
149
|
+
socket.send(message);
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* 发送消息给连接(使用 msgType 字符串格式)
|
|
156
|
+
*/
|
|
157
|
+
sendByType(connectionId, msgType, payload, reqId) {
|
|
158
|
+
const socket = this.sockets.get(connectionId);
|
|
159
|
+
if (socket && socket.readyState === ws_1.WebSocket.OPEN) {
|
|
160
|
+
const message = reqId
|
|
161
|
+
? JSON.stringify({ msgType, payload, reqId })
|
|
162
|
+
: JSON.stringify({ msgType, payload });
|
|
163
|
+
socket.send(message);
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* 关闭并移除连接
|
|
170
|
+
*/
|
|
171
|
+
removeConnection(connectionId, reason) {
|
|
172
|
+
const conn = this.connections.get(connectionId);
|
|
173
|
+
const socket = this.sockets.get(connectionId);
|
|
174
|
+
if (conn?.userId) {
|
|
175
|
+
this.userConnections.delete(conn.userId);
|
|
176
|
+
}
|
|
177
|
+
if (socket) {
|
|
178
|
+
if (socket.readyState === ws_1.WebSocket.OPEN) {
|
|
179
|
+
socket.close(1000, reason || 'Connection closed');
|
|
180
|
+
}
|
|
181
|
+
this.sockets.delete(connectionId);
|
|
182
|
+
}
|
|
183
|
+
this.connections.delete(connectionId);
|
|
184
|
+
this.logger.debug(`Connection removed: ${connectionId}, reason: ${reason}`);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* 更新最后活动时间
|
|
188
|
+
*/
|
|
189
|
+
touch(connectionId) {
|
|
190
|
+
const conn = this.connections.get(connectionId);
|
|
191
|
+
if (conn) {
|
|
192
|
+
conn.lastActivity = Date.now();
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* 获取所有连接数
|
|
197
|
+
*/
|
|
198
|
+
getConnectionCount() {
|
|
199
|
+
return this.connections.size;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* 获取已认证的连接数
|
|
203
|
+
*/
|
|
204
|
+
getAuthenticatedCount() {
|
|
205
|
+
let count = 0;
|
|
206
|
+
for (const conn of this.connections.values()) {
|
|
207
|
+
if (conn.status === 'identified' || conn.status === 'in-room') {
|
|
208
|
+
count++;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return count;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* 检查超时连接
|
|
215
|
+
*/
|
|
216
|
+
checkTimeouts(timeoutMs) {
|
|
217
|
+
const now = Date.now();
|
|
218
|
+
const timedOut = [];
|
|
219
|
+
for (const [id, conn] of this.connections) {
|
|
220
|
+
if (now - conn.lastActivity > timeoutMs) {
|
|
221
|
+
timedOut.push(id);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return timedOut;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* 检查空闲连接(认证后但未加入任何房间)
|
|
228
|
+
*
|
|
229
|
+
* @param idleTimeoutMs 空闲超时毫秒数
|
|
230
|
+
* @returns 需要清理的连接ID列表
|
|
231
|
+
*/
|
|
232
|
+
checkIdleConnections(idleTimeoutMs) {
|
|
233
|
+
const now = Date.now();
|
|
234
|
+
const idle = [];
|
|
235
|
+
for (const [id, conn] of this.connections) {
|
|
236
|
+
// 只检查已认证但未在任何房间的连接
|
|
237
|
+
if (conn.status === 'identified' &&
|
|
238
|
+
conn.identifiedAt &&
|
|
239
|
+
conn.rooms.size === 0 &&
|
|
240
|
+
now - conn.identifiedAt > idleTimeoutMs) {
|
|
241
|
+
idle.push(id);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return idle;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* 获取所有连接(用于广播处理)
|
|
248
|
+
*/
|
|
249
|
+
getAllConnections() {
|
|
250
|
+
return this.connections;
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
exports.ConnectionService = ConnectionService;
|
|
254
|
+
exports.ConnectionService = ConnectionService = ConnectionService_1 = __decorate([
|
|
255
|
+
(0, common_1.Injectable)()
|
|
256
|
+
], ConnectionService);
|
|
257
|
+
//# sourceMappingURL=connection.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection.service.js","sour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AC,EAAE,CAAC,CAAC;YAChB,CAAC;QACH,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;OAEG;IACH,iBAAiB;QACf,OAAO,IAAI,CAAC,WAAW,CAAC;IAC1B,CAAC;CACF,CAAA;AA7QY,8CAAiB;4BAAjB,iBAAiB;IAD7B,IAAA,mBAAU,GAAE;GACA,iBAAiB,CA6Q7B"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { DynamicModule } from '@nestjs/common';
|
|
2
|
+
export interface GatewayModuleOptions {
|
|
3
|
+
wsPort?: number;
|
|
4
|
+
}
|
|
5
|
+
export declare class GatewayModule {
|
|
6
|
+
static forRoot(options?: GatewayModuleOptions): DynamicModule;
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=gateway.module.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gateway.module.d.ts","sourceRoot":"","sources":["../../src/gateway.module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAWvD,MAAM,WAAW,oBAAoB;IACnC,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,qBACa,aAAa;IACxB,MAAM,CAAC,OAAO,CAAC,OAAO,GAAE,oBAAyB,GAAG,aAAa;CAiBlE"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var GatewayModule_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.GatewayModule = void 0;
|
|
11
|
+
const common_1 = require("@nestjs/common");
|
|
12
|
+
const config_module_1 = require("./config/config.module");
|
|
13
|
+
const redis_module_1 = require("./redis/redis.module");
|
|
14
|
+
const connection_module_1 = require("./connection/connection.module");
|
|
15
|
+
const session_module_1 = require("./session/session.module");
|
|
16
|
+
const ws_module_1 = require("./ws/ws.module");
|
|
17
|
+
const admin_module_1 = require("./admin/admin.module");
|
|
18
|
+
const rate_limit_module_1 = require("./ratelimit/rate-limit.module");
|
|
19
|
+
const metrics_module_1 = require("./metrics/metrics.module");
|
|
20
|
+
const graceful_shutdown_service_1 = require("./lifecycle/graceful-shutdown.service");
|
|
21
|
+
let GatewayModule = GatewayModule_1 = class GatewayModule {
|
|
22
|
+
static forRoot(options = {}) {
|
|
23
|
+
return {
|
|
24
|
+
module: GatewayModule_1,
|
|
25
|
+
imports: [
|
|
26
|
+
config_module_1.ConfigModule,
|
|
27
|
+
redis_module_1.RedisModule,
|
|
28
|
+
connection_module_1.ConnectionModule,
|
|
29
|
+
session_module_1.SessionModule,
|
|
30
|
+
ws_module_1.WsGatewayModule.forRoot({ port: options.wsPort }),
|
|
31
|
+
admin_module_1.AdminModule,
|
|
32
|
+
rate_limit_module_1.RateLimitModule,
|
|
33
|
+
metrics_module_1.MetricsModule,
|
|
34
|
+
],
|
|
35
|
+
providers: [graceful_shutdown_service_1.GracefulShutdownService],
|
|
36
|
+
exports: [graceful_shutdown_service_1.GracefulShutdownService],
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
exports.GatewayModule = GatewayModule;
|
|
41
|
+
exports.GatewayModule = GatewayModule = GatewayModule_1 = __decorate([
|
|
42
|
+
(0, common_1.Module)({})
|
|
43
|
+
], GatewayModule);
|
|
44
|
+
//# sourceMappingURL=gateway.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gateway.module.js","sourceRoot":"","sources":["../../src/gateway.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAAuD;AACvD,0DAAsD;AACtD,uDAAmD;AACnD,sEAAkE;AAClE,6DAAyD;AACzD,8CAAiD;AACjD,uDAAmD;AACnD,qEAAgE;AAChE,6DAAyD;AACzD,qFAAgF;AAOzE,IAAM,aAAa,qBAAnB,MAAM,aAAa;IACxB,MAAM,CAAC,OAAO,CAAC,UAAgC,EAAE;QAC/C,OAAO;YACL,MAAM,EAAE,eAAa;YACrB,OAAO,EAAE;gBACP,4BAAY;gBACZ,0BAAW;gBACX,oCAAgB;gBAChB,8BAAa;gBACb,2BAAe,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC;gBACjD,0BAAW;gBACX,mCAAe;gBACf,8BAAa;aACd;YACD,SAAS,EAAE,CAAC,mDAAuB,CAAC;YACpC,OAAO,EAAE,CAAC,mDAAuB,CAAC;SACnC,CAAC;IACJ,CAAC;CACF,CAAA;AAlBY,sCAAa;wBAAb,aAAa;IADzB,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,aAAa,CAkBzB"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @roomkit/gateway - Gateway Server for Gateway-Worker Framework
|
|
3
|
+
*
|
|
4
|
+
* Creates a WebSocket gateway server that handles client connections
|
|
5
|
+
* and routes messages to workers via Redis.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { createGateway } from '@roomkit/gateway';
|
|
10
|
+
*
|
|
11
|
+
* const gateway = await createGateway({
|
|
12
|
+
* redis: 'redis://localhost:6379',
|
|
13
|
+
* ws: { port: 18080 },
|
|
14
|
+
* });
|
|
15
|
+
*
|
|
16
|
+
* await gateway.listen();
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
import { GatewayConfig } from './config/gateway.config';
|
|
20
|
+
export interface GatewayInstance {
|
|
21
|
+
/** Start the gateway server */
|
|
22
|
+
listen(): Promise<void>;
|
|
23
|
+
/** Stop the gateway server */
|
|
24
|
+
close(): Promise<void>;
|
|
25
|
+
/** Get gateway ID */
|
|
26
|
+
readonly id: string;
|
|
27
|
+
/** Get WebSocket port */
|
|
28
|
+
readonly wsPort: number;
|
|
29
|
+
/** Get Admin API port */
|
|
30
|
+
readonly adminPort: number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Create a Gateway server instance
|
|
34
|
+
*/
|
|
35
|
+
export declare function createGateway(config: GatewayConfig): Promise<GatewayInstance>;
|
|
36
|
+
export * from './config/gateway.config';
|
|
37
|
+
export { GatewayModule } from './gateway.module';
|
|
38
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAMH,OAAO,EACL,aAAa,EAKd,MAAM,yBAAyB,CAAC;AAGjC,MAAM,WAAW,eAAe;IAC9B,+BAA+B;IAC/B,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACxB,8BAA8B;IAC9B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACvB,qBAAqB;IACrB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,yBAAyB;IACzB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,yBAAyB;IACzB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B;AAED;;GAEG;AACH,wBAAsB,aAAa,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,eAAe,CAAC,CAoDnF;AAGD,cAAc,yBAAyB,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC"}
|