mm_os 4.3.7 → 5.0.1

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 (231) hide show
  1. package/README.md +102 -504
  2. package/index.js +26 -19
  3. package/lib/os.js +536 -0
  4. package/package.json +60 -84
  5. package/LICENSE +0 -21
  6. package/README_EN.md +0 -505
  7. package/adapter/adapter.js +0 -484
  8. package/adapter/custom_persistence.js +0 -661
  9. package/adapter/mqtt.js +0 -254
  10. package/adapter/socket.js +0 -244
  11. package/adapter/web.js +0 -92
  12. package/adapter/websocket.js +0 -43
  13. package/ai.js +0 -130
  14. package/com/api/README.md +0 -3
  15. package/com/api/com.json +0 -5
  16. package/com/api/config.tpl.json +0 -41
  17. package/com/api/drive.js +0 -732
  18. package/com/api/index.js +0 -201
  19. package/com/api/oauth.js +0 -200
  20. package/com/api/script.tpl.js +0 -32
  21. package/com/cmd/README.md +0 -11
  22. package/com/cmd/com.json +0 -5
  23. package/com/cmd/config.tpl.json +0 -48
  24. package/com/cmd/drive.js +0 -176
  25. package/com/cmd/index.js +0 -287
  26. package/com/cmd/job/config.json +0 -66
  27. package/com/cmd/job/index.js +0 -241
  28. package/com/cmd/job/test.js +0 -188
  29. package/com/cmd/msg.json +0 -48
  30. package/com/cmd/script.tpl.js +0 -12
  31. package/com/db/README.md +0 -3
  32. package/com/db/com.json +0 -5
  33. package/com/db/drive.js +0 -2206
  34. package/com/db/index.js +0 -241
  35. package/com/event/README.md +0 -4
  36. package/com/event/com.json +0 -5
  37. package/com/event/config.tpl.json +0 -20
  38. package/com/event/drive.js +0 -61
  39. package/com/event/index.js +0 -438
  40. package/com/event/script.tpl.js +0 -23
  41. package/com/mqtt/README.md +0 -47
  42. package/com/mqtt/com.json +0 -5
  43. package/com/mqtt/config.tpl.json +0 -42
  44. package/com/mqtt/drive.js +0 -704
  45. package/com/mqtt/index.js +0 -1052
  46. package/com/mqtt/index.test.js +0 -503
  47. package/com/mqtt/script.tpl.js +0 -719
  48. package/com/nav/README.md +0 -2
  49. package/com/nav/com.json +0 -5
  50. package/com/nav/config.tpl.json +0 -84
  51. package/com/nav/drive.js +0 -702
  52. package/com/nav/index.js +0 -228
  53. package/com/nav/tpl/admin_pc/page_channel.vue +0 -305
  54. package/com/nav/tpl/admin_pc/page_config.vue +0 -280
  55. package/com/nav/tpl/admin_pc/page_config_form.vue +0 -194
  56. package/com/nav/tpl/admin_pc/page_default.vue +0 -247
  57. package/com/nav/tpl/admin_pc/page_form.vue +0 -180
  58. package/com/nav/tpl/admin_pc/page_lang.vue +0 -264
  59. package/com/nav/tpl/admin_pc/page_nav.vue +0 -321
  60. package/com/nav/tpl/admin_pc/page_table.vue +0 -296
  61. package/com/nav/tpl/admin_pc/page_type.vue +0 -321
  62. package/com/nav/tpl/admin_pc/page_view.vue +0 -124
  63. package/com/nav/tpl/dev_pc/page_channel.vue +0 -253
  64. package/com/nav/tpl/dev_pc/page_config.vue +0 -253
  65. package/com/nav/tpl/dev_pc/page_default.vue +0 -247
  66. package/com/nav/tpl/dev_pc/page_form.vue +0 -162
  67. package/com/nav/tpl/dev_pc/page_nav.vue +0 -253
  68. package/com/nav/tpl/dev_pc/page_table.vue +0 -247
  69. package/com/nav/tpl/dev_pc/page_type.vue +0 -313
  70. package/com/nav/tpl/home_pc/page_channel.vue +0 -234
  71. package/com/nav/tpl/home_pc/page_default.vue +0 -234
  72. package/com/nav/tpl/home_pc/page_form.vue +0 -137
  73. package/com/nav/tpl/home_pc/page_list.vue +0 -234
  74. package/com/nav/tpl/home_pc/page_nav.vue +0 -221
  75. package/com/nav/tpl/home_pc/page_type.vue +0 -234
  76. package/com/nav/tpl/home_pc/page_view.vue +0 -125
  77. package/com/nav/tpl/home_phone/page_channel.vue +0 -234
  78. package/com/nav/tpl/home_phone/page_default.vue +0 -234
  79. package/com/nav/tpl/home_phone/page_form.vue +0 -137
  80. package/com/nav/tpl/home_phone/page_list.vue +0 -234
  81. package/com/nav/tpl/home_phone/page_nav.vue +0 -237
  82. package/com/nav/tpl/home_phone/page_type.vue +0 -234
  83. package/com/nav/tpl/home_phone/page_view.vue +0 -125
  84. package/com/nav/viewmodel.js +0 -446
  85. package/com/param/README.md +0 -2
  86. package/com/param/com.json +0 -5
  87. package/com/param/config.tpl.json +0 -136
  88. package/com/param/drive.js +0 -502
  89. package/com/param/index.js +0 -158
  90. package/com/param/script.tpl.js +0 -12
  91. package/com/pendant/README.md +0 -3
  92. package/com/pendant/com.json +0 -5
  93. package/com/pendant/config.tpl.json +0 -32
  94. package/com/pendant/drive.js +0 -204
  95. package/com/pendant/index.js +0 -467
  96. package/com/pendant/pendant.html +0 -16
  97. package/com/pendant/script.tpl.js +0 -18
  98. package/com/socket/README.md +0 -47
  99. package/com/socket/com.json +0 -5
  100. package/com/socket/config.tpl.json +0 -12
  101. package/com/socket/drive.js +0 -679
  102. package/com/socket/index.js +0 -402
  103. package/com/socket/script.tpl.js +0 -41
  104. package/com/sql/README.md +0 -3
  105. package/com/sql/com.json +0 -5
  106. package/com/sql/config.tpl.json +0 -74
  107. package/com/sql/drive.js +0 -1328
  108. package/com/sql/index.js +0 -152
  109. package/com/sql/script.tpl.js +0 -47
  110. package/com/static/README.md +0 -2
  111. package/com/static/com.json +0 -5
  112. package/com/static/config.tpl.json +0 -24
  113. package/com/static/drive.js +0 -194
  114. package/com/static/index.js +0 -237
  115. package/com/static/script.tpl.js +0 -28
  116. package/com/task/README.md +0 -3
  117. package/com/task/com.json +0 -5
  118. package/com/task/config.tpl.json +0 -24
  119. package/com/task/drive.js +0 -428
  120. package/com/task/index.js +0 -159
  121. package/com/task/script.tpl.js +0 -40
  122. package/com/template/com.json +0 -5
  123. package/com/template/config.tpl.json +0 -16
  124. package/com/template/drive.js +0 -81
  125. package/com/template/index.js +0 -141
  126. package/com.js +0 -237
  127. package/common/README.md +0 -2
  128. package/common/middleware/web_after/index.js +0 -26
  129. package/common/middleware/web_after/middleware.json +0 -9
  130. package/common/middleware/web_base/index.js +0 -113
  131. package/common/middleware/web_base/middleware.json +0 -19
  132. package/common/middleware/web_before/index.js +0 -33
  133. package/common/middleware/web_before/middleware.json +0 -9
  134. package/common/middleware/web_cors/index.js +0 -87
  135. package/common/middleware/web_cors/middleware.json +0 -24
  136. package/common/middleware/web_error/index.js +0 -131
  137. package/common/middleware/web_error/middleware.json +0 -19
  138. package/common/middleware/web_ip/index.js +0 -15
  139. package/common/middleware/web_ip/middleware.json +0 -14
  140. package/common/middleware/web_logger/index.js +0 -156
  141. package/common/middleware/web_logger/middleware.json +0 -14
  142. package/common/middleware/web_main/index.js +0 -26
  143. package/common/middleware/web_main/middleware.json +0 -9
  144. package/common/middleware/web_static/index.js +0 -73
  145. package/common/middleware/web_static/middleware.json +0 -54
  146. package/common/middleware/web_waf/index.js +0 -395
  147. package/common/middleware/web_waf/middleware.json +0 -13
  148. package/core/app/config.tpl.json +0 -67
  149. package/core/app/index.js +0 -711
  150. package/core/app/script.tpl.js +0 -52
  151. package/core/channel/index.js +0 -899
  152. package/core/channel/matcher.js +0 -585
  153. package/core/com/config.tpl.json +0 -16
  154. package/core/com/index.js +0 -77
  155. package/core/com/script.tpl.js +0 -5
  156. package/core/component/component.js +0 -42
  157. package/core/component/config.tpl.json +0 -63
  158. package/core/component/index.js +0 -273
  159. package/core/component/script.tpl.js +0 -19
  160. package/core/controller/config.tpl.json +0 -14
  161. package/core/controller/index.js +0 -371
  162. package/core/controller/script.tpl.js +0 -27
  163. package/core/factory/config.tpl.json +0 -14
  164. package/core/factory/entity.js +0 -275
  165. package/core/factory/index.js +0 -241
  166. package/core/factory/script.tpl.js +0 -16
  167. package/core/game/bat/index.js +0 -137
  168. package/core/game/bat/world.js +0 -622
  169. package/core/game/config.tpl.json +0 -16
  170. package/core/game/entity_admin.js +0 -230
  171. package/core/game/index.js +0 -186
  172. package/core/handler/config.tpl.json +0 -22
  173. package/core/handler/index.js +0 -181
  174. package/core/handler/script.tpl.js +0 -23
  175. package/core/logic/config.tpl.json +0 -14
  176. package/core/logic/index.js +0 -59
  177. package/core/logic/script.tpl.js +0 -19
  178. package/core/middleware/config.tpl.json +0 -16
  179. package/core/middleware/index.js +0 -117
  180. package/core/middleware/script.tpl.js +0 -37
  181. package/core/mod/config.tpl.json +0 -22
  182. package/core/mod/index.js +0 -136
  183. package/core/mod/script.tpl.js +0 -34
  184. package/core/model/config.tpl.json +0 -219
  185. package/core/model/index.js +0 -272
  186. package/core/model/model.js +0 -27
  187. package/core/model/script.tpl.js +0 -20
  188. package/core/notifier/config.tpl.json +0 -14
  189. package/core/notifier/index.js +0 -77
  190. package/core/notifier/script.tpl.js +0 -20
  191. package/core/plugin/config.tpl.json +0 -36
  192. package/core/plugin/index.js +0 -267
  193. package/core/plugin/script.tpl.js +0 -51
  194. package/core/pusher/config.tpl.json +0 -14
  195. package/core/pusher/index.js +0 -158
  196. package/core/pusher/script.tpl.js +0 -20
  197. package/core/room/bat/index.js +0 -170
  198. package/core/room/bat/room.js +0 -524
  199. package/core/room/config.tpl.json +0 -20
  200. package/core/room/index.js +0 -249
  201. package/core/room/room.js +0 -61
  202. package/core/scene/config.tpl.json +0 -14
  203. package/core/scene/index.js +0 -467
  204. package/core/scene/loop.js +0 -1306
  205. package/core/scene/map.js +0 -28
  206. package/core/scene/script.tpl.js +0 -22
  207. package/core/sender/config.tpl.json +0 -14
  208. package/core/sender/index.js +0 -79
  209. package/core/sender/script.tpl.js +0 -20
  210. package/core/service/config.tpl.json +0 -14
  211. package/core/service/index.js +0 -100
  212. package/core/service/script.tpl.js +0 -25
  213. package/core/store/config.tpl.json +0 -26
  214. package/core/store/index.js +0 -1794
  215. package/core/store/script.tpl.js +0 -22
  216. package/core/system/config.tpl.json +0 -18
  217. package/core/system/index.js +0 -312
  218. package/core/system/script.tpl.js +0 -77
  219. package/core/view/config.tpl.json +0 -14
  220. package/core/view/index.js +0 -91
  221. package/core/view/script.tpl.js +0 -20
  222. package/core/zone/bat/index.js +0 -744
  223. package/core/zone/config.tpl.json +0 -54
  224. package/core/zone/index.js +0 -655
  225. package/core/zone/script.tpl.js +0 -10
  226. package/core/zone/zone_bat.js +0 -136
  227. package/core//345/237/272/347/261/273/346/250/241/345/235/227/346/270/205/345/215/225.md +0 -24
  228. package/os.js +0 -53
  229. package/server.js +0 -602
  230. package/ulits/id_gen.js +0 -134
  231. package/ulits/word.js +0 -206
@@ -1,661 +0,0 @@
1
- const { Readable } = require('stream');
2
-
3
- /**
4
- * 自定义持久化类 - 为Aedes MQTT broker提供持久化存储支持
5
- */
6
- class Persist {
7
- /**
8
- * 构造函数
9
- * @param {object} config 配置参数
10
- * @param {object} cache 缓存管理器
11
- */
12
- constructor(config, cache) {
13
- // 配置参数
14
- this.config = {
15
- // 缓存配置
16
- cache: true,
17
- // 缓存前缀
18
- prefix: 'mqtt:',
19
- // 缓存过期时间(毫秒)
20
- ttl: 3600000,
21
- ...config
22
- };
23
-
24
- // 缓存管理器,提供set、get、del、keys功能
25
- this._cache = cache;
26
-
27
- if (!this._cache) {
28
- throw new Error('缓存管理器不能为空');
29
- }
30
-
31
- // 验证缓存管理器是否实现了必要的方法
32
- let req_methods = ['set', 'get', 'delete', 'keys'];
33
- for (let method of req_methods) {
34
- if (typeof this._cache[method] !== 'function') {
35
- throw new Error(`缓存管理器必须实现${method}方法`);
36
- }
37
- }
38
- }
39
- }
40
-
41
- /**
42
- * 生成带前缀的缓存键
43
- * @private
44
- * @param {string} key - 原始键
45
- * @returns {string} 带前缀的缓存键
46
- */
47
- Persist.prototype._getCacheKey = function(key) {
48
- return this.config.prefix + key;
49
- };
50
-
51
- /**
52
- * 获取所有匹配模式的键
53
- * @private
54
- * @param {string} pattern - 键模式
55
- * @returns {Array} 匹配的键数组
56
- */
57
- Persist.prototype._getKeysByPattern = function(pattern) {
58
- let prefix = this.config.prefix;
59
- let regex = new RegExp('^' + prefix + pattern.replace(/\*/g, '.*') + '$');
60
- return this._cache.keys().filter(key => regex.test(key)).map(key => key.slice(prefix.length));
61
- };
62
-
63
- // ------------------- 订阅管理 -------------------
64
-
65
- /**
66
- * 添加订阅
67
- * @param {object} client - 客户端对象
68
- * @param {object} sub - 订阅对象
69
- * @param {Function} cb - 回调函数
70
- */
71
- Persist.prototype.addSub = function(client, sub, cb) {
72
- console.log('addSub', client.id);
73
- try {
74
- if (!client || !client.id) {
75
- throw new Error('无效的客户端对象');
76
- }
77
-
78
- if (!sub || !sub.topic) {
79
- throw new Error('无效的订阅对象');
80
- }
81
-
82
- let key = this._getCacheKey(`subscription:${client.id}:${sub.topic}`);
83
- this._cache.set(key, sub, this.config.ttl);
84
-
85
- if (cb) cb(null);
86
- } catch (error) {
87
- if (cb) cb(error);
88
- }
89
- };
90
-
91
- /**
92
- * 删除订阅
93
- * @param {object} client - 客户端对象
94
- * @param {object} sub - 订阅对象
95
- * @param {Function} cb - 回调函数
96
- */
97
- Persist.prototype.removeSub = function(client, sub, cb) {
98
- try {
99
- if (!client || !client.id) {
100
- throw new Error('无效的客户端对象');
101
- }
102
-
103
- if (!sub || !sub.topic) {
104
- throw new Error('无效的订阅对象');
105
- }
106
-
107
- let key = this._getCacheKey(`subscription:${client.id}:${sub.topic}`);
108
- this._cache.delete(key);
109
-
110
- if (cb) cb(null);
111
- } catch (error) {
112
- if (cb) cb(error);
113
- }
114
- };
115
-
116
- /**
117
- * 删除客户端的所有订阅
118
- * @param {object} client - 客户端对象
119
- * @param {Function} cb - 回调函数
120
- */
121
- Persist.prototype.removeSubs = function(client, cb) {
122
- try {
123
- if (!client || !client.id) {
124
- throw new Error('无效的客户端对象');
125
- }
126
-
127
- let pat = `subscription:${client.id}:*`;
128
- let keys = this._getKeysByPattern(pat);
129
-
130
- // 同步执行所有删除操作
131
- keys.forEach(key => {
132
- this._cache.delete(this._getCacheKey(key));
133
- });
134
-
135
- if (cb) cb(null);
136
- } catch (error) {
137
- if (cb) cb(error);
138
- }
139
- };
140
-
141
- /**
142
- * 获取客户端的所有订阅
143
- * @param {object} client - 客户端对象
144
- * @param {Function} cb - 回调函数
145
- */
146
- Persist.prototype.getSubs = function(client, cb) {
147
- try {
148
- if (!client || !client.id) {
149
- throw new Error('无效的客户端对象');
150
- }
151
-
152
- let pattern = `subscription:${client.id}:*`;
153
- let keys = this._getKeysByPattern(pattern);
154
-
155
- // 同步执行所有获取操作
156
- let subs = keys.map(key => {
157
- return this._cache.get(this._getCacheKey(key));
158
- }).filter(Boolean);
159
-
160
- if (cb) cb(null, subs);
161
- } catch (error) {
162
- if (cb) cb(error);
163
- }
164
- };
165
-
166
- /**
167
- * 获取订阅了指定主题的所有客户端
168
- * @param {string} topic - 主题
169
- * @param {Function} cb - 回调函数
170
- */
171
- Persist.prototype.subsByTopic = function(topic, cb) {
172
- try {
173
- if (!topic) {
174
- throw new Error('主题不能为空');
175
- }
176
-
177
- // 简单实现,实际可能需要支持通配符匹配
178
- let pat = `subscription:*:${topic}`;
179
- let keys = this._getKeysByPattern(pat);
180
-
181
- // 同步执行所有获取操作
182
- let subs = keys.map(key => {
183
- let parts = key.split(':');
184
- if (parts.length >= 3) {
185
- let cid = parts[1];
186
- let sub = this._cache.get(this._getCacheKey(key));
187
- if (sub) {
188
- return {
189
- clientId: cid,
190
- sub: sub
191
- };
192
- }
193
- }
194
- return null;
195
- }).filter(Boolean);
196
-
197
- if (cb) cb(null, subs);
198
- } catch (error) {
199
- if (cb) cb(error);
200
- }
201
- };
202
-
203
- // ------------------- 保留消息 -------------------
204
-
205
- /**
206
- * 存储保留消息
207
- * @param {object} packet - MQTT消息包
208
- * @param {Function} callback - 回调函数
209
- */
210
- Persist.prototype.storeRetained = function(packet, callback) {
211
- try {
212
- if (!packet || !packet.topic) {
213
- throw new Error('无效的消息包');
214
- }
215
-
216
- let key = this._getCacheKey(`retained:${packet.topic}`);
217
- this._cache.set(key, packet, this.config.ttl);
218
-
219
- if (callback) callback(null);
220
- } catch (error) {
221
- if (callback) callback(error);
222
- }
223
- };
224
-
225
- /**
226
- * 删除保留消息
227
- * @param {string} topic - 主题
228
- * @param {Function} callback - 回调函数
229
- */
230
- Persist.prototype.cleanRetained = function(topic, callback) {
231
- try {
232
- if (!topic) {
233
- throw new Error('主题不能为空');
234
- }
235
-
236
- let key = this._getCacheKey(`retained:${topic}`);
237
- this._cache.delete(key);
238
-
239
- if (callback) callback(null);
240
- } catch (error) {
241
- if (callback) callback(error);
242
- }
243
- };
244
-
245
- /**
246
- * 获取保留消息
247
- * @param {string} topic - 主题
248
- * @param {Function} cb - 回调函数
249
- */
250
- Persist.prototype.getRetained = function(topic, cb) {
251
- try {
252
- if (!topic) {
253
- throw new Error('主题不能为空');
254
- }
255
-
256
- let key = this._getCacheKey(`retained:${topic}`);
257
- let retained = this._cache.get(key);
258
-
259
- if (cb) cb(null, retained ? [retained] : []);
260
- } catch (error) {
261
- if (cb) cb(error);
262
- }
263
- };
264
-
265
- // ------------------- 传输中消息 (QoS) -------------------
266
-
267
- /**
268
- * 存储传输中消息
269
- * @param {object} client - 客户端对象
270
- * @param {object} packet - MQTT消息包
271
- * @param {Function} callback - 回调函数
272
- */
273
- Persist.prototype.storePacket = function(client, packet, callback) {
274
- try {
275
- if (!client || !client.id) {
276
- throw new Error('无效的客户端对象');
277
- }
278
-
279
- if (!packet || !packet.msgId) {
280
- throw new Error('无效的消息包');
281
- }
282
-
283
- let key = this._getCacheKey(`packet:${client.id}:${packet.msgId}`);
284
- this._cache.set(key, packet, this.config.ttl);
285
-
286
- if (callback) callback(null);
287
- } catch (error) {
288
- if (callback) callback(error);
289
- }
290
- };
291
-
292
- /**
293
- * 删除传输中消息
294
- * @param {object} client - 客户端对象
295
- * @param {object} packet - MQTT消息包
296
- * @param {Function} callback - 回调函数
297
- */
298
- Persist.prototype.removePacket = function(client, packet, callback) {
299
- try {
300
- if (!client || !client.id) {
301
- throw new Error('无效的客户端对象');
302
- }
303
-
304
- if (!packet || !packet.msgId) {
305
- throw new Error('无效的消息包');
306
- }
307
-
308
- let key = this._getCacheKey(`packet:${client.id}:${packet.msgId}`);
309
- this._cache.delete(key);
310
-
311
- if (callback) callback(null);
312
- } catch (error) {
313
- if (callback) callback(error);
314
- }
315
- };
316
-
317
- /**
318
- * 获取传输中消息
319
- * @param {object} client - 客户端对象
320
- * @param {object} packet - MQTT消息包
321
- * @param {Function} callback - 回调函数
322
- */
323
- Persist.prototype.getPacket = function(client, packet, callback) {
324
- try {
325
- if (!client || !client.id) {
326
- throw new Error('无效的客户端对象');
327
- }
328
-
329
- if (!packet || !packet.msgId) {
330
- throw new Error('无效的消息包');
331
- }
332
-
333
- let key = this._getCacheKey(`packet:${client.id}:${packet.msgId}`);
334
-
335
- if (callback) callback(null, this._cache.get(key));
336
- } catch (error) {
337
- if (callback) callback(error);
338
- }
339
- };
340
-
341
- /**
342
- * 获取客户端的所有传输中消息
343
- * @param {object} client - 客户端对象
344
- * @param {Function} callback - 回调函数
345
- */
346
- Persist.prototype.getPackets = function(client, callback) {
347
- try {
348
- if (!client || !client.id) {
349
- throw new Error('无效的客户端对象');
350
- }
351
-
352
- let pattern = `packet:${client.id}:*`;
353
- let keys = this._getKeysByPattern(pattern);
354
-
355
- // 同步执行所有获取操作
356
- let packets = keys.map(key => {
357
- let packet = this._cache.get(this._getCacheKey(key));
358
- return packet;
359
- }).filter(Boolean);
360
-
361
- if (callback) callback(null, packets);
362
- } catch (error) {
363
- if (callback) callback(error);
364
- }
365
- };
366
-
367
- /**
368
- * 删除过期的传输中消息
369
- * @param {object} packet - MQTT消息包
370
- * @param {Function} callback - 回调函数
371
- */
372
- Persist.prototype.delOutdatedPacket = function(packet, callback) {
373
- try {
374
- if (!packet || !packet.msgId) {
375
- throw new Error('无效的消息包');
376
- }
377
-
378
- // 注意:这个方法可能需要客户端信息,这里简单处理
379
- let pattern = `packet:*:${packet.msgId}`;
380
- let keys = this._getKeysByPattern(pattern);
381
-
382
- // 同步执行所有删除操作
383
- keys.forEach(key => {
384
- this._cache.delete(this._getCacheKey(key));
385
- });
386
-
387
- if (callback) callback(null);
388
- } catch (error) {
389
- if (callback) callback(error);
390
- }
391
- };
392
-
393
- // ------------------- 遗嘱消息 -------------------
394
-
395
- /**
396
- * 存储遗嘱消息
397
- * @param {object} client - 客户端对象
398
- * @param {object} packet - MQTT消息包
399
- * @param {Function} callback - 回调函数
400
- */
401
- Persist.prototype.storeWill = function(client, packet, callback) {
402
- try {
403
- if (!client || !client.id) {
404
- throw new Error('无效的客户端对象');
405
- }
406
-
407
- if (!packet) {
408
- throw new Error('无效的消息包');
409
- }
410
-
411
- let key = this._getCacheKey(`will:${client.id}`);
412
- this._cache.set(key, packet, this.config.ttl);
413
-
414
- if (callback) callback(null);
415
- } catch (error) {
416
- if (callback) callback(error);
417
- }
418
- };
419
-
420
- /**
421
- * 删除遗嘱消息
422
- * @param {object} client - 客户端对象
423
- * @param {Function} callback - 回调函数
424
- */
425
- Persist.prototype.removeWill = function(client, callback) {
426
- try {
427
- if (!client || !client.id) {
428
- throw new Error('无效的客户端对象');
429
- }
430
-
431
- let key = this._getCacheKey(`will:${client.id}`);
432
- this._cache.delete(key);
433
-
434
- if (callback) callback(null);
435
- } catch (error) {
436
- if (callback) callback(error);
437
- }
438
- };
439
-
440
- /**
441
- * 获取遗嘱消息
442
- * @param {object} client - 客户端对象
443
- * @param {Function} callback - 回调函数
444
- */
445
- Persist.prototype.getWill = function(client, callback) {
446
- try {
447
- if (!client || !client.id) {
448
- throw new Error('无效的客户端对象');
449
- }
450
-
451
- let key = this._getCacheKey(`will:${client.id}`);
452
- let will = this._cache.get(key);
453
-
454
- if (callback) callback(null, will);
455
- } catch (error) {
456
- if (callback) callback(error);
457
- }
458
- };
459
-
460
- // ------------------- 离线消息 -------------------
461
-
462
- /**
463
- * 存储离线消息
464
- * @param {object} client - 客户端对象
465
- * @param {object} packet - MQTT消息包
466
- * @param {Function} callback - 回调函数
467
- */
468
- Persist.prototype.storeOfflinePacket = function(client, packet, callback) {
469
- try {
470
- if (!client || !client.id) {
471
- throw new Error('无效的客户端对象');
472
- }
473
-
474
- if (!packet || !packet.msgId) {
475
- throw new Error('无效的消息包');
476
- }
477
-
478
- let key = this._getCacheKey(`offline:${client.id}:${packet.msgId}`);
479
- this._cache.set(key, packet, this.config.ttl);
480
-
481
- if (callback) callback(null);
482
- } catch (error) {
483
- if (callback) callback(error);
484
- }
485
- };
486
-
487
- /**
488
- * 获取客户端的所有离线消息
489
- * @param {object} client - 客户端对象
490
- * @param {Function} callback - 回调函数
491
- */
492
- Persist.prototype.getOfflinePackets = function(client, callback) {
493
- try {
494
- if (!client || !client.id) {
495
- throw new Error('无效的客户端对象');
496
- }
497
-
498
- let pattern = `offline:${client.id}:*`;
499
- let keys = this._getKeysByPattern(pattern);
500
-
501
- // 同步执行所有获取操作
502
- let packets = keys.map(key => {
503
- let packet = this._cache.get(this._getCacheKey(key));
504
- return packet;
505
- }).filter(Boolean);
506
-
507
- if (callback) callback(null, packets);
508
- } catch (error) {
509
- if (callback) callback(error);
510
- }
511
- };
512
-
513
- // ------------------- 流式恢复(重要!) -------------------
514
-
515
- /**
516
- * 处理保留消息
517
- * @private
518
- * @param {object} self 当前实例
519
- * @param {object} stream 可读流
520
- */
521
- Persist.prototype._procRetainMsgs = function(self, stream) {
522
- let keys = self._getKeysByPattern('retained:*');
523
- let msgs = keys.map(key => {
524
- let packet = self._cache.get(self._getCacheKey(key));
525
- if (packet) {
526
- return {
527
- type: 'retained',
528
- topic: packet.topic,
529
- payload: packet.payload,
530
- qos: packet.qos,
531
- retain: packet.retain
532
- };
533
- }
534
- return null;
535
- }).filter(Boolean);
536
- msgs.forEach(msg => {
537
- stream.push(msg);
538
- });
539
- };
540
-
541
- /**
542
- * 处理订阅信息
543
- * @private
544
- * @param {object} self 当前实例
545
- * @param {object} stream 可读流
546
- */
547
- Persist.prototype._procSubs = function(self, stream) {
548
- let keys = self._getKeysByPattern('subscription:*:*');
549
- let subs = keys.map(key => {
550
- let parts = key.split(':');
551
- if (parts.length >= 3) {
552
- let cid = parts[1];
553
- let sub = self._cache.get(self._getCacheKey(key));
554
- if (sub) {
555
- return {
556
- type: 'subscription',
557
- clientId: cid,
558
- sub: sub
559
- };
560
- }
561
- }
562
- return null;
563
- }).filter(Boolean);
564
- subs.forEach(sub => {
565
- stream.push(sub);
566
- });
567
- };
568
-
569
- /**
570
- * 处理遗嘱消息
571
- * @private
572
- * @param {object} self 当前实例
573
- * @param {object} stream 可读流
574
- */
575
- Persist.prototype._procWills = function(self, stream) {
576
- let keys = self._getKeysByPattern('will:*');
577
- let wills = keys.map(key => {
578
- let parts = key.split(':');
579
- if (parts.length >= 2) {
580
- let cid = parts[1];
581
- let will = self._cache.get(self._getCacheKey(key));
582
- if (will) {
583
- return {
584
- type: 'will',
585
- clientId: cid,
586
- will: will
587
- };
588
- }
589
- }
590
- return null;
591
- }).filter(Boolean);
592
- wills.forEach(will => {
593
- stream.push(will);
594
- });
595
- };
596
-
597
- /**
598
- * 处理传输中消息
599
- * @private
600
- * @param {object} self 当前实例
601
- * @param {object} stream 可读流
602
- */
603
- Persist.prototype._procInflightMsgs = function(self, stream) {
604
- let keys = self._getKeysByPattern('packet:*:*');
605
- let packets = keys.map(key => {
606
- let parts = key.split(':');
607
- if (parts.length >= 3) {
608
- let cid = parts[1];
609
- let packet = self._cache.get(self._getCacheKey(key));
610
- if (packet) {
611
- return {
612
- type: 'inflight',
613
- clientId: cid,
614
- packet: packet
615
- };
616
- }
617
- }
618
- return null;
619
- }).filter(Boolean);
620
- packets.forEach(packet => {
621
- stream.push(packet);
622
- });
623
- };
624
-
625
- /**
626
- * 创建可读流,用于恢复Aedes状态
627
- * @returns {Readable} 可读流
628
- */
629
- Persist.prototype.createStream = function() {
630
- let self = this;
631
-
632
- return new Readable({
633
- objectMode: true,
634
-
635
- read() {
636
- try {
637
- // 处理保留消息
638
- self._procRetainMsgs(self, this);
639
-
640
- // 处理订阅信息
641
- self._procSubs(self, this);
642
-
643
- // 处理遗嘱消息
644
- self._procWills(self, this);
645
-
646
- // 处理传输中消息
647
- self._procInflightMsgs(self, this);
648
-
649
- // 结束流
650
- this.push(null);
651
- } catch (error) {
652
- self._cache.log && self._cache.log('error', '创建恢复流时出错:', error);
653
- this.push(null);
654
- }
655
- }
656
- });
657
- };
658
-
659
- module.exports = {
660
- Persist
661
- };