@pisell/core 0.0.2

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 (296) hide show
  1. package/README.md +1 -0
  2. package/es/app/app.d.ts +101 -0
  3. package/es/app/app.js +177 -0
  4. package/es/app/const.d.ts +4 -0
  5. package/es/app/const.js +6 -0
  6. package/es/app/index.d.ts +14 -0
  7. package/es/app/index.js +52 -0
  8. package/es/applicationManager/application.d.ts +197 -0
  9. package/es/applicationManager/application.js +469 -0
  10. package/es/applicationManager/index.d.ts +19 -0
  11. package/es/applicationManager/index.js +206 -0
  12. package/es/aws/index.d.ts +16 -0
  13. package/es/aws/index.js +76 -0
  14. package/es/cmd/const.d.ts +11 -0
  15. package/es/cmd/const.js +13 -0
  16. package/es/cmd/index.d.ts +13 -0
  17. package/es/cmd/index.js +91 -0
  18. package/es/cmd/type.d.ts +5 -0
  19. package/es/cmd/type.js +1 -0
  20. package/es/communicationManager/index.d.ts +59 -0
  21. package/es/communicationManager/index.js +174 -0
  22. package/es/config.d.ts +3 -0
  23. package/es/config.js +14 -0
  24. package/es/cookie/index.d.ts +13 -0
  25. package/es/cookie/index.js +47 -0
  26. package/es/css/global.less +73 -0
  27. package/es/css/index.less +31 -0
  28. package/es/css/variables.css +84 -0
  29. package/es/data/index.d.ts +8 -0
  30. package/es/data/index.js +16 -0
  31. package/es/history/config.d.ts +24 -0
  32. package/es/history/config.js +43 -0
  33. package/es/history/index.d.ts +26 -0
  34. package/es/history/index.js +80 -0
  35. package/es/history/type.d.ts +2 -0
  36. package/es/history/type.js +1 -0
  37. package/es/hooks/index.d.ts +12 -0
  38. package/es/hooks/index.js +13 -0
  39. package/es/hooks/useDelayedValue/index.d.ts +2 -0
  40. package/es/hooks/useDelayedValue/index.js +26 -0
  41. package/es/hooks/useDispatch/index.d.ts +2 -0
  42. package/es/hooks/useDispatch/index.js +2 -0
  43. package/es/hooks/useLowCode/index.d.ts +13 -0
  44. package/es/hooks/useLowCode/index.js +74 -0
  45. package/es/hooks/useStore/index.d.ts +6 -0
  46. package/es/hooks/useStore/index.js +11 -0
  47. package/es/index.d.ts +8 -0
  48. package/es/index.js +10 -0
  49. package/es/indexDB/index.d.ts +296 -0
  50. package/es/indexDB/index.js +1619 -0
  51. package/es/locales/en.d.ts +3 -0
  52. package/es/locales/en.js +5 -0
  53. package/es/locales/index.d.ts +39 -0
  54. package/es/locales/index.js +199 -0
  55. package/es/locales/original.d.ts +3 -0
  56. package/es/locales/original.js +7 -0
  57. package/es/locales/type.d.ts +19 -0
  58. package/es/locales/type.js +1 -0
  59. package/es/locales/zh-CN.d.ts +3 -0
  60. package/es/locales/zh-CN.js +5 -0
  61. package/es/locales/zh-HK.d.ts +3 -0
  62. package/es/locales/zh-HK.js +5 -0
  63. package/es/logger/feishu.d.ts +11 -0
  64. package/es/logger/feishu.js +50 -0
  65. package/es/logger/index.d.ts +135 -0
  66. package/es/logger/index.js +870 -0
  67. package/es/menuManager/hooks.d.ts +17 -0
  68. package/es/menuManager/hooks.js +136 -0
  69. package/es/menuManager/index.d.ts +28 -0
  70. package/es/menuManager/index.js +165 -0
  71. package/es/models/global.d.ts +32 -0
  72. package/es/models/global.js +66 -0
  73. package/es/models/index.d.ts +45 -0
  74. package/es/models/index.js +65 -0
  75. package/es/models/type.d.ts +2 -0
  76. package/es/models/type.js +1 -0
  77. package/es/plugin/index.d.ts +0 -0
  78. package/es/plugin/index.js +0 -0
  79. package/es/pubsub/example.d.ts +5 -0
  80. package/es/pubsub/example.js +92 -0
  81. package/es/pubsub/index.d.ts +73 -0
  82. package/es/pubsub/index.js +166 -0
  83. package/es/render/index.d.ts +0 -0
  84. package/es/render/index.js +0 -0
  85. package/es/request/cache.d.ts +46 -0
  86. package/es/request/cache.js +372 -0
  87. package/es/request/cancelToken.d.ts +38 -0
  88. package/es/request/cancelToken.js +59 -0
  89. package/es/request/config.d.ts +3 -0
  90. package/es/request/config.js +63 -0
  91. package/es/request/constants.d.ts +2 -0
  92. package/es/request/constants.js +6 -0
  93. package/es/request/index.d.ts +24 -0
  94. package/es/request/index.js +267 -0
  95. package/es/request/pisell2Request.d.ts +6 -0
  96. package/es/request/pisell2Request.js +62 -0
  97. package/es/request/type.d.ts +57 -0
  98. package/es/request/type.js +10 -0
  99. package/es/request/utils.d.ts +46 -0
  100. package/es/request/utils.js +147 -0
  101. package/es/routes/config.d.ts +7 -0
  102. package/es/routes/config.js +17 -0
  103. package/es/routes/index.d.ts +28 -0
  104. package/es/routes/index.js +151 -0
  105. package/es/socket/components/SocketMonitorPage.d.ts +6 -0
  106. package/es/socket/components/SocketMonitorPage.js +485 -0
  107. package/es/socket/components/index.d.ts +2 -0
  108. package/es/socket/components/index.js +2 -0
  109. package/es/socket/constants.d.ts +33 -0
  110. package/es/socket/constants.js +39 -0
  111. package/es/socket/events.d.ts +31 -0
  112. package/es/socket/events.js +19 -0
  113. package/es/socket/heartbeat.d.ts +66 -0
  114. package/es/socket/heartbeat.js +184 -0
  115. package/es/socket/index.d.ts +61 -0
  116. package/es/socket/index.js +246 -0
  117. package/es/socket/monitor.d.ts +169 -0
  118. package/es/socket/monitor.js +448 -0
  119. package/es/socket/reconnect.d.ts +61 -0
  120. package/es/socket/reconnect.js +199 -0
  121. package/es/socket/socket.d.ts +130 -0
  122. package/es/socket/socket.js +613 -0
  123. package/es/socket/types.d.ts +85 -0
  124. package/es/socket/types.js +35 -0
  125. package/es/storage/index.d.ts +17 -0
  126. package/es/storage/index.js +46 -0
  127. package/es/tasks/index.d.ts +127 -0
  128. package/es/tasks/index.js +1119 -0
  129. package/es/tasks/scheduledTasksExample.d.ts +61 -0
  130. package/es/tasks/scheduledTasksExample.js +351 -0
  131. package/es/tasks/type.d.ts +100 -0
  132. package/es/tasks/type.js +1 -0
  133. package/es/tasks/useTasks.d.ts +5 -0
  134. package/es/tasks/useTasks.js +25 -0
  135. package/es/type.d.ts +2 -0
  136. package/es/type.js +1 -0
  137. package/es/utils/adaptiveThrottle/index.d.ts +36 -0
  138. package/es/utils/adaptiveThrottle/index.js +136 -0
  139. package/es/variables/VariablesProvider.d.ts +7 -0
  140. package/es/variables/VariablesProvider.js +12 -0
  141. package/es/variables/config.d.ts +3 -0
  142. package/es/variables/config.js +16 -0
  143. package/es/variables/index.d.ts +6 -0
  144. package/es/variables/index.js +5 -0
  145. package/es/variables/type.d.ts +2 -0
  146. package/es/variables/type.js +1 -0
  147. package/es/website/index.d.ts +6 -0
  148. package/es/website/index.js +65 -0
  149. package/lib/app/app.d.ts +101 -0
  150. package/lib/app/app.js +171 -0
  151. package/lib/app/const.d.ts +4 -0
  152. package/lib/app/const.js +33 -0
  153. package/lib/app/index.d.ts +14 -0
  154. package/lib/app/index.js +76 -0
  155. package/lib/applicationManager/application.d.ts +197 -0
  156. package/lib/applicationManager/application.js +280 -0
  157. package/lib/applicationManager/index.d.ts +19 -0
  158. package/lib/applicationManager/index.js +88 -0
  159. package/lib/aws/index.d.ts +16 -0
  160. package/lib/aws/index.js +67 -0
  161. package/lib/cmd/const.d.ts +11 -0
  162. package/lib/cmd/const.js +39 -0
  163. package/lib/cmd/index.d.ts +13 -0
  164. package/lib/cmd/index.js +96 -0
  165. package/lib/cmd/type.d.ts +5 -0
  166. package/lib/cmd/type.js +17 -0
  167. package/lib/communicationManager/index.d.ts +59 -0
  168. package/lib/communicationManager/index.js +102 -0
  169. package/lib/config.d.ts +3 -0
  170. package/lib/config.js +36 -0
  171. package/lib/cookie/index.d.ts +13 -0
  172. package/lib/cookie/index.js +62 -0
  173. package/lib/css/global.less +73 -0
  174. package/lib/css/index.less +31 -0
  175. package/lib/css/variables.css +84 -0
  176. package/lib/data/index.d.ts +8 -0
  177. package/lib/data/index.js +38 -0
  178. package/lib/history/config.d.ts +24 -0
  179. package/lib/history/config.js +41 -0
  180. package/lib/history/index.d.ts +26 -0
  181. package/lib/history/index.js +99 -0
  182. package/lib/history/type.d.ts +2 -0
  183. package/lib/history/type.js +17 -0
  184. package/lib/hooks/index.d.ts +12 -0
  185. package/lib/hooks/index.js +44 -0
  186. package/lib/hooks/useDelayedValue/index.d.ts +2 -0
  187. package/lib/hooks/useDelayedValue/index.js +36 -0
  188. package/lib/hooks/useDispatch/index.d.ts +2 -0
  189. package/lib/hooks/useDispatch/index.js +26 -0
  190. package/lib/hooks/useLowCode/index.d.ts +13 -0
  191. package/lib/hooks/useLowCode/index.js +75 -0
  192. package/lib/hooks/useStore/index.d.ts +6 -0
  193. package/lib/hooks/useStore/index.js +33 -0
  194. package/lib/index.d.ts +8 -0
  195. package/lib/index.js +58 -0
  196. package/lib/indexDB/index.d.ts +296 -0
  197. package/lib/indexDB/index.js +767 -0
  198. package/lib/locales/en.d.ts +3 -0
  199. package/lib/locales/en.js +29 -0
  200. package/lib/locales/index.d.ts +39 -0
  201. package/lib/locales/index.js +159 -0
  202. package/lib/locales/original.d.ts +3 -0
  203. package/lib/locales/original.js +31 -0
  204. package/lib/locales/type.d.ts +19 -0
  205. package/lib/locales/type.js +17 -0
  206. package/lib/locales/zh-CN.d.ts +3 -0
  207. package/lib/locales/zh-CN.js +29 -0
  208. package/lib/locales/zh-HK.d.ts +3 -0
  209. package/lib/locales/zh-HK.js +29 -0
  210. package/lib/logger/feishu.d.ts +11 -0
  211. package/lib/logger/feishu.js +52 -0
  212. package/lib/logger/index.d.ts +135 -0
  213. package/lib/logger/index.js +476 -0
  214. package/lib/menuManager/hooks.d.ts +17 -0
  215. package/lib/menuManager/hooks.js +135 -0
  216. package/lib/menuManager/index.d.ts +28 -0
  217. package/lib/menuManager/index.js +125 -0
  218. package/lib/models/global.d.ts +32 -0
  219. package/lib/models/global.js +62 -0
  220. package/lib/models/index.d.ts +45 -0
  221. package/lib/models/index.js +69 -0
  222. package/lib/models/type.d.ts +2 -0
  223. package/lib/models/type.js +17 -0
  224. package/lib/plugin/index.d.ts +0 -0
  225. package/lib/plugin/index.js +0 -0
  226. package/lib/pubsub/example.d.ts +5 -0
  227. package/lib/pubsub/example.js +61 -0
  228. package/lib/pubsub/index.d.ts +73 -0
  229. package/lib/pubsub/index.js +141 -0
  230. package/lib/render/index.d.ts +0 -0
  231. package/lib/render/index.js +0 -0
  232. package/lib/request/cache.d.ts +46 -0
  233. package/lib/request/cache.js +195 -0
  234. package/lib/request/cancelToken.d.ts +38 -0
  235. package/lib/request/cancelToken.js +59 -0
  236. package/lib/request/config.d.ts +3 -0
  237. package/lib/request/config.js +69 -0
  238. package/lib/request/constants.d.ts +2 -0
  239. package/lib/request/constants.js +34 -0
  240. package/lib/request/index.d.ts +24 -0
  241. package/lib/request/index.js +210 -0
  242. package/lib/request/pisell2Request.d.ts +6 -0
  243. package/lib/request/pisell2Request.js +75 -0
  244. package/lib/request/type.d.ts +57 -0
  245. package/lib/request/type.js +36 -0
  246. package/lib/request/utils.d.ts +46 -0
  247. package/lib/request/utils.js +113 -0
  248. package/lib/routes/config.d.ts +7 -0
  249. package/lib/routes/config.js +50 -0
  250. package/lib/routes/index.d.ts +28 -0
  251. package/lib/routes/index.js +121 -0
  252. package/lib/socket/components/SocketMonitorPage.d.ts +6 -0
  253. package/lib/socket/components/SocketMonitorPage.js +340 -0
  254. package/lib/socket/components/index.d.ts +2 -0
  255. package/lib/socket/components/index.js +39 -0
  256. package/lib/socket/constants.d.ts +33 -0
  257. package/lib/socket/constants.js +62 -0
  258. package/lib/socket/events.d.ts +31 -0
  259. package/lib/socket/events.js +44 -0
  260. package/lib/socket/heartbeat.d.ts +66 -0
  261. package/lib/socket/heartbeat.js +157 -0
  262. package/lib/socket/index.d.ts +61 -0
  263. package/lib/socket/index.js +198 -0
  264. package/lib/socket/monitor.d.ts +169 -0
  265. package/lib/socket/monitor.js +350 -0
  266. package/lib/socket/reconnect.d.ts +61 -0
  267. package/lib/socket/reconnect.js +157 -0
  268. package/lib/socket/socket.d.ts +130 -0
  269. package/lib/socket/socket.js +426 -0
  270. package/lib/socket/types.d.ts +85 -0
  271. package/lib/socket/types.js +36 -0
  272. package/lib/storage/index.d.ts +17 -0
  273. package/lib/storage/index.js +64 -0
  274. package/lib/tasks/index.d.ts +127 -0
  275. package/lib/tasks/index.js +710 -0
  276. package/lib/tasks/scheduledTasksExample.d.ts +61 -0
  277. package/lib/tasks/scheduledTasksExample.js +267 -0
  278. package/lib/tasks/type.d.ts +100 -0
  279. package/lib/tasks/type.js +17 -0
  280. package/lib/tasks/useTasks.d.ts +5 -0
  281. package/lib/tasks/useTasks.js +37 -0
  282. package/lib/type.d.ts +2 -0
  283. package/lib/type.js +17 -0
  284. package/lib/utils/adaptiveThrottle/index.d.ts +36 -0
  285. package/lib/utils/adaptiveThrottle/index.js +121 -0
  286. package/lib/variables/VariablesProvider.d.ts +7 -0
  287. package/lib/variables/VariablesProvider.js +51 -0
  288. package/lib/variables/config.d.ts +3 -0
  289. package/lib/variables/config.js +38 -0
  290. package/lib/variables/index.d.ts +6 -0
  291. package/lib/variables/index.js +29 -0
  292. package/lib/variables/type.d.ts +2 -0
  293. package/lib/variables/type.js +17 -0
  294. package/lib/website/index.d.ts +6 -0
  295. package/lib/website/index.js +75 -0
  296. package/package.json +49 -0
@@ -0,0 +1,350 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/socket/monitor.ts
20
+ var monitor_exports = {};
21
+ __export(monitor_exports, {
22
+ SocketMonitor: () => SocketMonitor
23
+ });
24
+ module.exports = __toCommonJS(monitor_exports);
25
+ var import_events = require("./events");
26
+ var import_types = require("./types");
27
+ var SocketMonitor = class {
28
+ socket;
29
+ options;
30
+ isMonitoring = false;
31
+ // 历史记录存储
32
+ messageHistory = [];
33
+ statusHistory = [];
34
+ // 统计数据
35
+ stats = {
36
+ totalSent: 0,
37
+ totalReceived: 0,
38
+ bytesSent: 0,
39
+ bytesReceived: 0,
40
+ connectionCount: 0,
41
+ reconnectionCount: 0,
42
+ errorCount: 0,
43
+ heartbeatCount: 0,
44
+ heartbeatTimeoutCount: 0,
45
+ firstConnectTime: null,
46
+ lastConnectTime: null,
47
+ currentConnectionDuration: null,
48
+ averageMessageSize: 0
49
+ };
50
+ // 用于计算连接持续时间
51
+ connectStartTime = null;
52
+ // 事件句柄映射
53
+ eventHandlers = /* @__PURE__ */ new Map();
54
+ /**
55
+ * 创建Socket监控器
56
+ * @param socket 要监控的Socket实例
57
+ * @param options 监控配置选项
58
+ */
59
+ constructor(socket, options = {}) {
60
+ this.socket = socket;
61
+ this.options = {
62
+ maxHistoryItems: 100,
63
+ maxStatusItems: 50,
64
+ recordMessageContent: true,
65
+ autoStart: true,
66
+ excludeEvents: ["ping", "pong", "heartbeat"],
67
+ ...options
68
+ };
69
+ if (this.options.autoStart) {
70
+ this.start();
71
+ }
72
+ }
73
+ /**
74
+ * 开始监控
75
+ */
76
+ start() {
77
+ if (this.isMonitoring) {
78
+ return;
79
+ }
80
+ this.isMonitoring = true;
81
+ this.setupEventListeners();
82
+ this.recordStatusChange(import_types.SocketStatus.CLOSED, this.socket.getStatus());
83
+ }
84
+ /**
85
+ * 停止监控
86
+ */
87
+ stop() {
88
+ if (!this.isMonitoring) {
89
+ return;
90
+ }
91
+ this.isMonitoring = false;
92
+ this.removeEventListeners();
93
+ }
94
+ /**
95
+ * 检查监控器是否处于活跃状态
96
+ */
97
+ isActive() {
98
+ return this.isMonitoring;
99
+ }
100
+ /**
101
+ * 清除历史记录
102
+ */
103
+ clearHistory() {
104
+ this.messageHistory = [];
105
+ this.statusHistory = [];
106
+ }
107
+ /**
108
+ * 重置统计数据
109
+ */
110
+ resetStats() {
111
+ this.stats = {
112
+ totalSent: 0,
113
+ totalReceived: 0,
114
+ bytesSent: 0,
115
+ bytesReceived: 0,
116
+ connectionCount: 0,
117
+ reconnectionCount: 0,
118
+ errorCount: 0,
119
+ heartbeatCount: 0,
120
+ heartbeatTimeoutCount: 0,
121
+ firstConnectTime: null,
122
+ lastConnectTime: null,
123
+ currentConnectionDuration: null,
124
+ averageMessageSize: 0
125
+ };
126
+ this.connectStartTime = null;
127
+ }
128
+ /**
129
+ * 获取消息历史记录
130
+ * @param limit 返回的最大记录数
131
+ * @param direction 消息方向过滤
132
+ */
133
+ getMessageHistory(limit, direction) {
134
+ let history = this.messageHistory;
135
+ if (direction) {
136
+ history = history.filter((item) => item.direction === direction);
137
+ }
138
+ if (limit && limit > 0) {
139
+ history = history.slice(-limit);
140
+ }
141
+ return history;
142
+ }
143
+ /**
144
+ * 获取状态变更历史
145
+ * @param limit 返回的最大记录数
146
+ */
147
+ getStatusHistory(limit) {
148
+ let history = this.statusHistory;
149
+ if (limit && limit > 0) {
150
+ history = history.slice(-limit);
151
+ }
152
+ return history;
153
+ }
154
+ /**
155
+ * 获取统计信息
156
+ */
157
+ getStats() {
158
+ if (this.socket.isConnected() && this.connectStartTime) {
159
+ this.stats.currentConnectionDuration = Date.now() - this.connectStartTime;
160
+ }
161
+ return { ...this.stats };
162
+ }
163
+ /**
164
+ * 设置监控事件监听
165
+ */
166
+ setupEventListeners() {
167
+ const onSend = (message) => {
168
+ this.recordMessageSent(message);
169
+ };
170
+ this.eventHandlers.set(import_events.SocketEvents.SEND, onSend);
171
+ this.socket.on(import_events.SocketEvents.SEND, onSend);
172
+ const onMessage = (message) => {
173
+ this.recordMessageReceived(message);
174
+ };
175
+ this.eventHandlers.set(import_events.SocketEvents.MESSAGE, onMessage);
176
+ this.socket.on(import_events.SocketEvents.MESSAGE, onMessage);
177
+ const onConnect = () => {
178
+ const now = Date.now();
179
+ this.connectStartTime = now;
180
+ if (!this.stats.firstConnectTime) {
181
+ this.stats.firstConnectTime = now;
182
+ }
183
+ this.stats.lastConnectTime = now;
184
+ this.stats.connectionCount++;
185
+ this.recordStatusChange(
186
+ this.socket.getStatus() === import_types.SocketStatus.RECONNECTING ? import_types.SocketStatus.RECONNECTING : import_types.SocketStatus.CONNECTING,
187
+ import_types.SocketStatus.OPEN,
188
+ import_events.SocketEvents.CONNECT
189
+ );
190
+ };
191
+ this.eventHandlers.set(import_events.SocketEvents.CONNECT, onConnect);
192
+ this.socket.on(import_events.SocketEvents.CONNECT, onConnect);
193
+ const onDisconnect = () => {
194
+ this.connectStartTime = null;
195
+ this.stats.currentConnectionDuration = null;
196
+ this.recordStatusChange(
197
+ this.socket.getStatus() === import_types.SocketStatus.CLOSING ? import_types.SocketStatus.CLOSING : import_types.SocketStatus.OPEN,
198
+ import_types.SocketStatus.CLOSED,
199
+ import_events.SocketEvents.DISCONNECT
200
+ );
201
+ };
202
+ this.eventHandlers.set(import_events.SocketEvents.DISCONNECT, onDisconnect);
203
+ this.socket.on(import_events.SocketEvents.DISCONNECT, onDisconnect);
204
+ const onReconnect = () => {
205
+ this.stats.reconnectionCount++;
206
+ };
207
+ this.eventHandlers.set(import_events.SocketEvents.RECONNECT, onReconnect);
208
+ this.socket.on(import_events.SocketEvents.RECONNECT, onReconnect);
209
+ const onReconnectAttempt = () => {
210
+ this.recordStatusChange(
211
+ import_types.SocketStatus.CLOSED,
212
+ import_types.SocketStatus.RECONNECTING,
213
+ import_events.SocketEvents.RECONNECT_ATTEMPT
214
+ );
215
+ };
216
+ this.eventHandlers.set(import_events.SocketEvents.RECONNECT_ATTEMPT, onReconnectAttempt);
217
+ this.socket.on(import_events.SocketEvents.RECONNECT_ATTEMPT, onReconnectAttempt);
218
+ const onError = (error) => {
219
+ this.stats.errorCount++;
220
+ this.recordStatusChange(
221
+ this.socket.getStatus(),
222
+ this.socket.getStatus(),
223
+ import_events.SocketEvents.ERROR,
224
+ error
225
+ );
226
+ };
227
+ this.eventHandlers.set(import_events.SocketEvents.ERROR, onError);
228
+ this.socket.on(import_events.SocketEvents.ERROR, onError);
229
+ const onHeartbeat = () => {
230
+ this.stats.heartbeatCount++;
231
+ };
232
+ this.eventHandlers.set(import_events.SocketEvents.HEARTBEAT, onHeartbeat);
233
+ this.socket.on(import_events.SocketEvents.HEARTBEAT, onHeartbeat);
234
+ const onHeartbeatTimeout = () => {
235
+ this.stats.heartbeatTimeoutCount++;
236
+ };
237
+ this.eventHandlers.set(import_events.SocketEvents.HEARTBEAT_TIMEOUT, onHeartbeatTimeout);
238
+ this.socket.on(import_events.SocketEvents.HEARTBEAT_TIMEOUT, onHeartbeatTimeout);
239
+ }
240
+ /**
241
+ * 移除监控事件监听
242
+ */
243
+ removeEventListeners() {
244
+ for (const [event, handler] of this.eventHandlers.entries()) {
245
+ this.socket.off(event, handler);
246
+ }
247
+ this.eventHandlers.clear();
248
+ }
249
+ /**
250
+ * 记录已发送的消息
251
+ */
252
+ recordMessageSent(message) {
253
+ var _a;
254
+ if ((_a = this.options.excludeEvents) == null ? void 0 : _a.includes(message.event)) {
255
+ return;
256
+ }
257
+ const messageStr = JSON.stringify(message);
258
+ const size = new Blob([messageStr]).size;
259
+ this.stats.totalSent++;
260
+ this.stats.bytesSent += size;
261
+ this.updateAverageMessageSize(size);
262
+ this.addToMessageHistory({
263
+ id: message.id || `out_${Date.now()}`,
264
+ timestamp: Date.now(),
265
+ direction: "out",
266
+ //@ts-ignore
267
+ message: this.options.recordMessageContent ? message : { event: message.event },
268
+ size
269
+ });
270
+ }
271
+ /**
272
+ * 记录已接收的消息
273
+ */
274
+ recordMessageReceived(message) {
275
+ var _a;
276
+ if ((_a = this.options.excludeEvents) == null ? void 0 : _a.includes(message.event)) {
277
+ return;
278
+ }
279
+ const messageStr = JSON.stringify(message);
280
+ const size = new Blob([messageStr]).size;
281
+ this.stats.totalReceived++;
282
+ this.stats.bytesReceived += size;
283
+ this.updateAverageMessageSize(size);
284
+ this.addToMessageHistory({
285
+ id: message.id || `in_${Date.now()}`,
286
+ timestamp: Date.now(),
287
+ direction: "in",
288
+ //@ts-ignore
289
+ message: this.options.recordMessageContent ? message : { event: message.event },
290
+ size
291
+ });
292
+ }
293
+ /**
294
+ * 记录状态变化
295
+ */
296
+ recordStatusChange(fromStatus, toStatus, event, info) {
297
+ if (fromStatus === toStatus && !event) {
298
+ return;
299
+ }
300
+ const statusItem = {
301
+ timestamp: Date.now(),
302
+ from: fromStatus,
303
+ to: toStatus,
304
+ event,
305
+ info
306
+ };
307
+ this.addToStatusHistory(statusItem);
308
+ }
309
+ /**
310
+ * 添加消息到历史记录,并控制记录数量
311
+ */
312
+ addToMessageHistory(item) {
313
+ this.messageHistory.push(item);
314
+ if (this.messageHistory.length > (this.options.maxHistoryItems || 100)) {
315
+ this.messageHistory.shift();
316
+ }
317
+ }
318
+ /**
319
+ * 添加状态到历史记录,并控制记录数量
320
+ */
321
+ addToStatusHistory(item) {
322
+ this.statusHistory.push(item);
323
+ if (this.statusHistory.length > (this.options.maxStatusItems || 50)) {
324
+ this.statusHistory.shift();
325
+ }
326
+ }
327
+ /**
328
+ * 更新平均消息大小
329
+ */
330
+ updateAverageMessageSize(newSize) {
331
+ const totalMessages = this.stats.totalSent + this.stats.totalReceived;
332
+ if (totalMessages <= 1) {
333
+ this.stats.averageMessageSize = newSize;
334
+ } else {
335
+ this.stats.averageMessageSize = (this.stats.averageMessageSize * (totalMessages - 1) + newSize) / totalMessages;
336
+ }
337
+ }
338
+ /**
339
+ * 释放资源
340
+ */
341
+ destroy() {
342
+ this.stop();
343
+ this.messageHistory = [];
344
+ this.statusHistory = [];
345
+ }
346
+ };
347
+ // Annotate the CommonJS export names for ESM import in node:
348
+ 0 && (module.exports = {
349
+ SocketMonitor
350
+ });
@@ -0,0 +1,61 @@
1
+ import { ReconnectOptions } from "./types";
2
+ /**
3
+ * Socket重连管理器
4
+ * 处理断线重连逻辑
5
+ */
6
+ export declare class ReconnectManager {
7
+ private options;
8
+ private attempts;
9
+ private timer;
10
+ private isReconnecting;
11
+ private reconnectCallback;
12
+ private onAttempt;
13
+ private onSuccess;
14
+ private onFail;
15
+ private onMaxAttemptsExceeded;
16
+ /**
17
+ * 创建重连管理器实例
18
+ * @param options 重连配置
19
+ * @param reconnectCallback 重连执行函数
20
+ * @param callbacks 回调函数集合
21
+ */
22
+ constructor(options: ReconnectOptions, reconnectCallback: () => Promise<boolean>, callbacks?: {
23
+ onAttempt?: (attempt: number) => void;
24
+ onSuccess?: () => void;
25
+ onFail?: (error: Error) => void;
26
+ onMaxAttemptsExceeded?: () => void;
27
+ });
28
+ /**
29
+ * 启动重连过程
30
+ */
31
+ startReconnect(): void;
32
+ /**
33
+ * 停止重连过程
34
+ */
35
+ stopReconnect(): void;
36
+ /**
37
+ * 获取当前重连尝试次数
38
+ */
39
+ getReconnectAttempts(): number;
40
+ /**
41
+ * 获取当前是否正在重连
42
+ */
43
+ isActive(): boolean;
44
+ /**
45
+ * 重置重连状态
46
+ */
47
+ reset(): void;
48
+ /**
49
+ * 安排下一次重连尝试
50
+ */
51
+ private scheduleReconnect;
52
+ /**
53
+ * 执行重连操作
54
+ */
55
+ private executeReconnect;
56
+ /**
57
+ * 计算重连延迟时间
58
+ * 支持指数退避算法,增加随机抖动
59
+ */
60
+ private getReconnectDelay;
61
+ }
@@ -0,0 +1,157 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/socket/reconnect.ts
20
+ var reconnect_exports = {};
21
+ __export(reconnect_exports, {
22
+ ReconnectManager: () => ReconnectManager
23
+ });
24
+ module.exports = __toCommonJS(reconnect_exports);
25
+ var import_constants = require("./constants");
26
+ var ReconnectManager = class {
27
+ options;
28
+ attempts = 0;
29
+ timer = null;
30
+ isReconnecting = false;
31
+ // 重连回调函数
32
+ reconnectCallback;
33
+ onAttempt;
34
+ onSuccess;
35
+ onFail;
36
+ onMaxAttemptsExceeded;
37
+ /**
38
+ * 创建重连管理器实例
39
+ * @param options 重连配置
40
+ * @param reconnectCallback 重连执行函数
41
+ * @param callbacks 回调函数集合
42
+ */
43
+ constructor(options, reconnectCallback, callbacks = {}) {
44
+ this.options = {
45
+ ...options,
46
+ delay: Math.max(options.delay, import_constants.MIN_RECONNECTION_DELAY)
47
+ };
48
+ this.reconnectCallback = reconnectCallback;
49
+ this.onAttempt = callbacks.onAttempt || (() => {
50
+ });
51
+ this.onSuccess = callbacks.onSuccess || (() => {
52
+ });
53
+ this.onFail = callbacks.onFail || (() => {
54
+ });
55
+ this.onMaxAttemptsExceeded = callbacks.onMaxAttemptsExceeded || (() => {
56
+ });
57
+ }
58
+ /**
59
+ * 启动重连过程
60
+ */
61
+ startReconnect() {
62
+ if (this.isReconnecting || !this.options.enabled) {
63
+ return;
64
+ }
65
+ this.isReconnecting = true;
66
+ this.attempts = 0;
67
+ this.scheduleReconnect();
68
+ }
69
+ /**
70
+ * 停止重连过程
71
+ */
72
+ stopReconnect() {
73
+ this.isReconnecting = false;
74
+ if (this.timer) {
75
+ clearTimeout(this.timer);
76
+ this.timer = null;
77
+ }
78
+ }
79
+ /**
80
+ * 获取当前重连尝试次数
81
+ */
82
+ getReconnectAttempts() {
83
+ return this.attempts;
84
+ }
85
+ /**
86
+ * 获取当前是否正在重连
87
+ */
88
+ isActive() {
89
+ return this.isReconnecting;
90
+ }
91
+ /**
92
+ * 重置重连状态
93
+ */
94
+ reset() {
95
+ this.stopReconnect();
96
+ this.attempts = 0;
97
+ }
98
+ /**
99
+ * 安排下一次重连尝试
100
+ */
101
+ scheduleReconnect() {
102
+ if (!this.isReconnecting) {
103
+ return;
104
+ }
105
+ if (this.options.attempts !== void 0 && this.attempts >= this.options.attempts) {
106
+ this.isReconnecting = false;
107
+ this.onMaxAttemptsExceeded();
108
+ return;
109
+ }
110
+ const delay = this.getReconnectDelay();
111
+ this.timer = setTimeout(() => {
112
+ this.attempts++;
113
+ this.onAttempt(this.attempts);
114
+ this.executeReconnect().then((success) => {
115
+ if (success) {
116
+ this.isReconnecting = false;
117
+ this.onSuccess();
118
+ } else {
119
+ this.scheduleReconnect();
120
+ }
121
+ }).catch((error) => {
122
+ this.onFail(error);
123
+ this.scheduleReconnect();
124
+ });
125
+ }, delay);
126
+ }
127
+ /**
128
+ * 执行重连操作
129
+ */
130
+ async executeReconnect() {
131
+ try {
132
+ return await this.reconnectCallback();
133
+ } catch (error) {
134
+ this.onFail(error instanceof Error ? error : new Error("Reconnect failed"));
135
+ return false;
136
+ }
137
+ }
138
+ /**
139
+ * 计算重连延迟时间
140
+ * 支持指数退避算法,增加随机抖动
141
+ */
142
+ getReconnectDelay() {
143
+ const { delay, delayMax, jitter } = this.options;
144
+ let calculatedDelay = delay;
145
+ if (this.attempts > 0) {
146
+ calculatedDelay = Math.min(delayMax || Infinity, delay * Math.pow(1.5, this.attempts - 1));
147
+ }
148
+ if (jitter) {
149
+ calculatedDelay = Math.random() * calculatedDelay;
150
+ }
151
+ return Math.max(import_constants.MIN_RECONNECTION_DELAY, Math.floor(calculatedDelay));
152
+ }
153
+ };
154
+ // Annotate the CommonJS export names for ESM import in node:
155
+ 0 && (module.exports = {
156
+ ReconnectManager
157
+ });
@@ -0,0 +1,130 @@
1
+ import { SocketOptions, SocketStatus } from "./types";
2
+ import { SocketMonitor, SocketMonitorOptions } from "./monitor";
3
+ /**
4
+ * Socket主类
5
+ * 负责管理WebSocket连接、消息收发、事件处理、心跳检测和断线重连
6
+ */
7
+ export declare class Socket {
8
+ private ws;
9
+ private url;
10
+ private options;
11
+ private status;
12
+ private eventHandlers;
13
+ private heartbeatManager;
14
+ private reconnectManager;
15
+ private monitor;
16
+ private messageIdCounter;
17
+ /**
18
+ * 创建Socket实例
19
+ * @param options Socket配置选项
20
+ */
21
+ constructor(options: SocketOptions);
22
+ /**
23
+ * 建立WebSocket连接
24
+ */
25
+ connect(): Promise<void>;
26
+ /**
27
+ * 关闭WebSocket连接
28
+ * @param isManual 是否手动关闭
29
+ */
30
+ disconnect(isManual?: boolean): void;
31
+ /**
32
+ * 发送消息到服务器
33
+ * @param event 事件名称
34
+ * @param data 消息数据
35
+ * @param id 消息ID(可选,默认自动生成)
36
+ */
37
+ send(type: string, data?: any, id?: string): void;
38
+ /**
39
+ * 发送原始消息
40
+ * @param message 消息对象
41
+ */
42
+ private sendRaw;
43
+ /**
44
+ * 注册事件监听器
45
+ * @param event 事件名称
46
+ * @param handler 事件处理函数
47
+ */
48
+ on(event: string, handler: (...args: any[]) => void): void;
49
+ /**
50
+ * 移除事件监听器
51
+ * @param event 事件名称
52
+ * @param handler 事件处理函数(可选,不提供则移除所有该事件的处理函数)
53
+ */
54
+ off(event: string, handler?: (...args: any[]) => void): void;
55
+ /**
56
+ * 触发事件
57
+ * @param event 事件名称
58
+ * @param args 事件参数
59
+ */
60
+ private emit;
61
+ /**
62
+ * 获取当前连接状态
63
+ */
64
+ getStatus(): SocketStatus;
65
+ /**
66
+ * 获取当前状态描述
67
+ */
68
+ getStatusText(): string;
69
+ /**
70
+ * 检查是否已连接
71
+ */
72
+ isConnected(): boolean;
73
+ /**
74
+ * 设置连接状态
75
+ */
76
+ private setStatus;
77
+ /**
78
+ * 设置WebSocket事件监听器
79
+ */
80
+ private setupWebSocketListeners;
81
+ /**
82
+ * 处理接收到的消息
83
+ */
84
+ private handleMessage;
85
+ /**
86
+ * 处理连接关闭
87
+ */
88
+ private handleConnectionClosed;
89
+ /**
90
+ * 处理连接失败
91
+ */
92
+ private handleConnectionFailure;
93
+ /**
94
+ * 处理心跳响应
95
+ */
96
+ private handleHeartbeatResponse;
97
+ /**
98
+ * 初始化心跳检测
99
+ */
100
+ private initHeartbeat;
101
+ /**
102
+ * 初始化重连管理
103
+ */
104
+ private initReconnect;
105
+ /**
106
+ * 开始重连过程
107
+ */
108
+ private startReconnection;
109
+ /**
110
+ * 生成消息ID
111
+ */
112
+ private generateMessageId;
113
+ /**
114
+ * 启用监控功能
115
+ * @param options 监控配置选项
116
+ */
117
+ enableMonitoring(options?: SocketMonitorOptions): SocketMonitor;
118
+ /**
119
+ * 禁用监控功能
120
+ */
121
+ disableMonitoring(): void;
122
+ /**
123
+ * 获取监控器实例
124
+ */
125
+ getMonitor(): SocketMonitor | null;
126
+ /**
127
+ * 释放资源
128
+ */
129
+ destroy(): void;
130
+ }