@dongdev/fca-unofficial 3.0.27 → 3.0.28
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/.gitattributes +2 -0
- package/CHANGELOG.md +196 -193
- package/DOCS.md +3 -6
- package/README.md +1 -1
- package/index.d.ts +745 -746
- package/module/config.js +29 -29
- package/module/login.js +133 -134
- package/module/loginHelper.js +1240 -1240
- package/module/options.js +44 -45
- package/package.json +81 -81
- package/src/api/messaging/changeAdminStatus.js +56 -56
- package/src/api/messaging/changeThreadEmoji.js +47 -47
- package/src/api/messaging/createPoll.js +46 -46
- package/src/api/messaging/forwardAttachment.js +28 -28
- package/src/api/messaging/sendTypingIndicator.js +23 -23
- package/src/api/messaging/setMessageReaction.js +91 -91
- package/src/api/messaging/setTitle.js +47 -47
- package/src/api/socket/core/connectMqtt.js +250 -250
- package/src/api/socket/core/emitAuth.js +1 -1
- package/src/api/socket/core/getSeqID.js +322 -322
- package/src/api/socket/core/parseDelta.js +368 -377
- package/src/api/socket/listenMqtt.js +371 -372
- package/src/utils/cookies.js +68 -68
- package/src/utils/loginParser.js +347 -347
- package/src/utils/messageFormat.js +1173 -1173
- package/src/api/socket/core/markDelivery.js +0 -12
|
@@ -1,372 +1,371 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
const mqtt = require("mqtt");
|
|
3
|
-
const WebSocket = require("ws");
|
|
4
|
-
const HttpsProxyAgent = require("https-proxy-agent");
|
|
5
|
-
const EventEmitter = require("events");
|
|
6
|
-
const logger = require("../../../func/logger");
|
|
7
|
-
const { parseAndCheckLogin } = require("../../utils/client");
|
|
8
|
-
const { buildProxy, buildStream } = require("./detail/buildStream");
|
|
9
|
-
const { topics } = require("./detail/constants");
|
|
10
|
-
const createParseDelta = require("./core/parseDelta");
|
|
11
|
-
const createListenMqtt = require("./core/connectMqtt");
|
|
12
|
-
const createGetSeqID = require("./core/getSeqID");
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
ctx._mqttOpt
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
if
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
ctx.
|
|
160
|
-
ctx.
|
|
161
|
-
ctx.
|
|
162
|
-
ctx.
|
|
163
|
-
ctx.
|
|
164
|
-
ctx.
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
ctx.
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
//
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
ctx.
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
if (
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
api.
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
let
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
const
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
const mqtt = require("mqtt");
|
|
3
|
+
const WebSocket = require("ws");
|
|
4
|
+
const HttpsProxyAgent = require("https-proxy-agent");
|
|
5
|
+
const EventEmitter = require("events");
|
|
6
|
+
const logger = require("../../../func/logger");
|
|
7
|
+
const { parseAndCheckLogin } = require("../../utils/client");
|
|
8
|
+
const { buildProxy, buildStream } = require("./detail/buildStream");
|
|
9
|
+
const { topics } = require("./detail/constants");
|
|
10
|
+
const createParseDelta = require("./core/parseDelta");
|
|
11
|
+
const createListenMqtt = require("./core/connectMqtt");
|
|
12
|
+
const createGetSeqID = require("./core/getSeqID");
|
|
13
|
+
const getTaskResponseData = require("./core/getTaskResponseData");
|
|
14
|
+
const createEmitAuth = require("./core/emitAuth");
|
|
15
|
+
const createMiddlewareSystem = require("./middleware");
|
|
16
|
+
const parseDelta = createParseDelta({ parseAndCheckLogin });
|
|
17
|
+
// Create emitAuth first so it can be injected into both factories
|
|
18
|
+
const emitAuth = createEmitAuth({ logger });
|
|
19
|
+
// Pass emitAuth into connectMqtt so errors there can signal auth state
|
|
20
|
+
const listenMqtt = createListenMqtt({ WebSocket, mqtt, HttpsProxyAgent, buildStream, buildProxy, topics, parseDelta, getTaskResponseData, logger, emitAuth });
|
|
21
|
+
// Inject emitAuth into getSeqID so its catch handler can notify properly
|
|
22
|
+
const getSeqIDFactory = createGetSeqID({ parseAndCheckLogin, listenMqtt, logger, emitAuth });
|
|
23
|
+
|
|
24
|
+
const MQTT_DEFAULTS = { cycleMs: 60 * 60 * 1000, reconnectDelayMs: 2000, autoReconnect: true, reconnectAfterStop: false };
|
|
25
|
+
function mqttConf(ctx, overrides) {
|
|
26
|
+
ctx._mqttOpt = Object.assign({}, MQTT_DEFAULTS, ctx._mqttOpt || {}, overrides || {});
|
|
27
|
+
if (typeof ctx._mqttOpt.autoReconnect === "boolean") ctx.globalOptions.autoReconnect = ctx._mqttOpt.autoReconnect;
|
|
28
|
+
return ctx._mqttOpt;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
module.exports = function (defaultFuncs, api, ctx, opts) {
|
|
32
|
+
const identity = function () { };
|
|
33
|
+
let globalCallback = identity;
|
|
34
|
+
|
|
35
|
+
// Initialize middleware system if not already initialized
|
|
36
|
+
if (!ctx._middleware) {
|
|
37
|
+
ctx._middleware = createMiddlewareSystem();
|
|
38
|
+
}
|
|
39
|
+
const middleware = ctx._middleware;
|
|
40
|
+
|
|
41
|
+
function installPostGuard() {
|
|
42
|
+
if (ctx._postGuarded) return defaultFuncs.post;
|
|
43
|
+
const rawPost = defaultFuncs.post && defaultFuncs.post.bind(defaultFuncs);
|
|
44
|
+
if (!rawPost) return defaultFuncs.post;
|
|
45
|
+
|
|
46
|
+
function postSafe(...args) {
|
|
47
|
+
return rawPost(...args).catch(err => {
|
|
48
|
+
const msg = (err && err.error) || (err && err.message) || String(err || "");
|
|
49
|
+
if (/Not logged in|blocked the login/i.test(msg)) {
|
|
50
|
+
emitAuth(
|
|
51
|
+
ctx,
|
|
52
|
+
api,
|
|
53
|
+
globalCallback,
|
|
54
|
+
/blocked/i.test(msg) ? "login_blocked" : "not_logged_in",
|
|
55
|
+
msg
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
throw err;
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
defaultFuncs.post = postSafe;
|
|
62
|
+
ctx._postGuarded = true;
|
|
63
|
+
return postSafe;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
let conf = mqttConf(ctx, opts);
|
|
67
|
+
|
|
68
|
+
function getSeqIDWrapper() {
|
|
69
|
+
if (ctx._ending && !ctx._cycling) {
|
|
70
|
+
logger("mqtt getSeqID skipped - ending", "warn");
|
|
71
|
+
return Promise.resolve();
|
|
72
|
+
}
|
|
73
|
+
const form = {
|
|
74
|
+
av: ctx.globalOptions.pageID,
|
|
75
|
+
queries: JSON.stringify({
|
|
76
|
+
o0: {
|
|
77
|
+
doc_id: "3336396659757871",
|
|
78
|
+
query_params: {
|
|
79
|
+
limit: 1, before: null, tags: ["INBOX"],
|
|
80
|
+
includeDeliveryReceipts: false, includeSeqID: true
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
})
|
|
84
|
+
};
|
|
85
|
+
logger("mqtt getSeqID call", "info");
|
|
86
|
+
return getSeqIDFactory(defaultFuncs, api, ctx, globalCallback, form)
|
|
87
|
+
.then(() => {
|
|
88
|
+
logger("mqtt getSeqID done", "info");
|
|
89
|
+
ctx._cycling = false;
|
|
90
|
+
})
|
|
91
|
+
.catch(e => {
|
|
92
|
+
ctx._cycling = false;
|
|
93
|
+
const errMsg = e && e.message ? e.message : String(e || "Unknown error");
|
|
94
|
+
logger(`mqtt getSeqID error: ${errMsg}`, "error");
|
|
95
|
+
// Don't reconnect if we're ending
|
|
96
|
+
if (ctx._ending) return;
|
|
97
|
+
// Retry after delay if autoReconnect is enabled
|
|
98
|
+
if (ctx.globalOptions.autoReconnect) {
|
|
99
|
+
const d = conf.reconnectDelayMs;
|
|
100
|
+
logger(`mqtt getSeqID will retry in ${d}ms`, "warn");
|
|
101
|
+
setTimeout(() => {
|
|
102
|
+
if (!ctx._ending) getSeqIDWrapper();
|
|
103
|
+
}, d);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function isConnected() {
|
|
109
|
+
return !!(ctx.mqttClient && ctx.mqttClient.connected);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function unsubAll(cb) {
|
|
113
|
+
if (!isConnected()) {
|
|
114
|
+
if (cb) setTimeout(cb, 0);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
let pending = topics.length;
|
|
118
|
+
if (!pending) {
|
|
119
|
+
if (cb) setTimeout(cb, 0);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
let fired = false;
|
|
123
|
+
const timeout = setTimeout(() => {
|
|
124
|
+
if (!fired) {
|
|
125
|
+
fired = true;
|
|
126
|
+
logger("unsubAll timeout, proceeding anyway", "warn");
|
|
127
|
+
if (cb) cb();
|
|
128
|
+
}
|
|
129
|
+
}, 5000); // 5 second timeout
|
|
130
|
+
|
|
131
|
+
topics.forEach(t => {
|
|
132
|
+
try {
|
|
133
|
+
ctx.mqttClient.unsubscribe(t, () => {
|
|
134
|
+
if (--pending === 0 && !fired) {
|
|
135
|
+
clearTimeout(timeout);
|
|
136
|
+
fired = true;
|
|
137
|
+
if (cb) cb();
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
} catch (err) {
|
|
141
|
+
logger(`unsubAll error for topic ${t}: ${err && err.message ? err.message : String(err)}`, "warn");
|
|
142
|
+
if (--pending === 0 && !fired) {
|
|
143
|
+
clearTimeout(timeout);
|
|
144
|
+
fired = true;
|
|
145
|
+
if (cb) cb();
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function endQuietly(next) {
|
|
152
|
+
const finish = () => {
|
|
153
|
+
try {
|
|
154
|
+
if (ctx.mqttClient) {
|
|
155
|
+
ctx.mqttClient.removeAllListeners();
|
|
156
|
+
}
|
|
157
|
+
} catch (_) { }
|
|
158
|
+
ctx.mqttClient = undefined;
|
|
159
|
+
ctx.lastSeqId = null;
|
|
160
|
+
ctx.syncToken = undefined;
|
|
161
|
+
ctx.t_mqttCalled = false;
|
|
162
|
+
ctx._ending = false;
|
|
163
|
+
ctx._cycling = false;
|
|
164
|
+
if (ctx._reconnectTimer) {
|
|
165
|
+
clearTimeout(ctx._reconnectTimer);
|
|
166
|
+
ctx._reconnectTimer = null;
|
|
167
|
+
}
|
|
168
|
+
if (ctx._rTimeout) {
|
|
169
|
+
clearTimeout(ctx._rTimeout);
|
|
170
|
+
ctx._rTimeout = null;
|
|
171
|
+
}
|
|
172
|
+
// Clean up tasks Map to prevent memory leak
|
|
173
|
+
if (ctx.tasks && ctx.tasks instanceof Map) {
|
|
174
|
+
ctx.tasks.clear();
|
|
175
|
+
}
|
|
176
|
+
// Clean up userInfo intervals
|
|
177
|
+
if (ctx._userInfoIntervals && Array.isArray(ctx._userInfoIntervals)) {
|
|
178
|
+
ctx._userInfoIntervals.forEach(interval => {
|
|
179
|
+
try {
|
|
180
|
+
clearInterval(interval);
|
|
181
|
+
} catch (_) { }
|
|
182
|
+
});
|
|
183
|
+
ctx._userInfoIntervals = [];
|
|
184
|
+
}
|
|
185
|
+
// Clean up autoSave intervals
|
|
186
|
+
if (ctx._autoSaveInterval && Array.isArray(ctx._autoSaveInterval)) {
|
|
187
|
+
ctx._autoSaveInterval.forEach(interval => {
|
|
188
|
+
try {
|
|
189
|
+
clearInterval(interval);
|
|
190
|
+
} catch (_) { }
|
|
191
|
+
});
|
|
192
|
+
ctx._autoSaveInterval = [];
|
|
193
|
+
}
|
|
194
|
+
// Clean up scheduler
|
|
195
|
+
if (ctx._scheduler && typeof ctx._scheduler.destroy === "function") {
|
|
196
|
+
try {
|
|
197
|
+
ctx._scheduler.destroy();
|
|
198
|
+
} catch (_) { }
|
|
199
|
+
ctx._scheduler = undefined;
|
|
200
|
+
}
|
|
201
|
+
next && next();
|
|
202
|
+
};
|
|
203
|
+
try {
|
|
204
|
+
if (ctx.mqttClient) {
|
|
205
|
+
if (isConnected()) {
|
|
206
|
+
try {
|
|
207
|
+
ctx.mqttClient.publish("/browser_close", "{}", { qos: 0 });
|
|
208
|
+
} catch (_) { }
|
|
209
|
+
}
|
|
210
|
+
ctx.mqttClient.end(true, finish);
|
|
211
|
+
} else finish();
|
|
212
|
+
} catch (_) { finish(); }
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function delayedReconnect() {
|
|
216
|
+
const d = conf.reconnectDelayMs;
|
|
217
|
+
logger(`mqtt reconnect in ${d}ms`, "info");
|
|
218
|
+
setTimeout(() => getSeqIDWrapper(), d);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function forceCycle() {
|
|
222
|
+
if (ctx._cycling) {
|
|
223
|
+
logger("mqtt force cycle already in progress", "warn");
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
ctx._cycling = true;
|
|
227
|
+
ctx._ending = true;
|
|
228
|
+
logger("mqtt force cycle begin", "warn");
|
|
229
|
+
unsubAll(() => endQuietly(() => delayedReconnect()));
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return function (callback) {
|
|
233
|
+
class MessageEmitter extends EventEmitter {
|
|
234
|
+
stopListening(callback2) {
|
|
235
|
+
const cb = callback2 || function () { };
|
|
236
|
+
logger("mqtt stop requested", "info");
|
|
237
|
+
globalCallback = identity;
|
|
238
|
+
|
|
239
|
+
if (ctx._autoCycleTimer) {
|
|
240
|
+
clearInterval(ctx._autoCycleTimer);
|
|
241
|
+
ctx._autoCycleTimer = null;
|
|
242
|
+
logger("mqtt auto-cycle cleared", "info");
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (ctx._reconnectTimer) {
|
|
246
|
+
clearTimeout(ctx._reconnectTimer);
|
|
247
|
+
ctx._reconnectTimer = null;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
ctx._ending = true;
|
|
251
|
+
unsubAll(() => endQuietly(() => {
|
|
252
|
+
logger("mqtt stopped", "info");
|
|
253
|
+
cb();
|
|
254
|
+
conf = mqttConf(ctx, conf);
|
|
255
|
+
if (conf.reconnectAfterStop) delayedReconnect();
|
|
256
|
+
}));
|
|
257
|
+
}
|
|
258
|
+
async stopListeningAsync() {
|
|
259
|
+
return new Promise(resolve => { this.stopListening(resolve); });
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const msgEmitter = new MessageEmitter();
|
|
264
|
+
|
|
265
|
+
// Original callback without middleware
|
|
266
|
+
const originalCallback = callback || function (error, message) {
|
|
267
|
+
if (error) { logger("mqtt emit error", "error"); return msgEmitter.emit("error", error); }
|
|
268
|
+
msgEmitter.emit("message", message);
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
// Only wrap callback with middleware if middleware exists
|
|
272
|
+
// If no middleware, use callback directly for better performance
|
|
273
|
+
if (middleware.count > 0) {
|
|
274
|
+
globalCallback = middleware.wrapCallback(originalCallback);
|
|
275
|
+
} else {
|
|
276
|
+
globalCallback = originalCallback;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
conf = mqttConf(ctx, conf);
|
|
280
|
+
|
|
281
|
+
installPostGuard();
|
|
282
|
+
|
|
283
|
+
if (!ctx.firstListen) ctx.lastSeqId = null;
|
|
284
|
+
ctx.syncToken = undefined;
|
|
285
|
+
ctx.t_mqttCalled = false;
|
|
286
|
+
|
|
287
|
+
if (ctx._autoCycleTimer) { clearInterval(ctx._autoCycleTimer); ctx._autoCycleTimer = null; }
|
|
288
|
+
if (conf.cycleMs && conf.cycleMs > 0) {
|
|
289
|
+
ctx._autoCycleTimer = setInterval(forceCycle, conf.cycleMs);
|
|
290
|
+
logger(`mqtt auto-cycle enabled ${conf.cycleMs}ms`, "info");
|
|
291
|
+
} else {
|
|
292
|
+
logger("mqtt auto-cycle disabled", "info");
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (!ctx.firstListen || !ctx.lastSeqId) getSeqIDWrapper();
|
|
296
|
+
else {
|
|
297
|
+
logger("mqtt starting listenMqtt", "info");
|
|
298
|
+
listenMqtt(defaultFuncs, api, ctx, globalCallback);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
api.stopListening = msgEmitter.stopListening;
|
|
302
|
+
api.stopListeningAsync = msgEmitter.stopListeningAsync;
|
|
303
|
+
|
|
304
|
+
// Store original callback for re-wrapping when middleware is added/removed
|
|
305
|
+
let currentOriginalCallback = originalCallback;
|
|
306
|
+
let currentGlobalCallback = globalCallback;
|
|
307
|
+
|
|
308
|
+
// Function to re-wrap callback when middleware changes
|
|
309
|
+
function rewrapCallbackIfNeeded() {
|
|
310
|
+
if (!ctx.mqttClient || ctx._ending) return; // Not listening or ending
|
|
311
|
+
|
|
312
|
+
const hasMiddleware = middleware.count > 0;
|
|
313
|
+
const isWrapped = currentGlobalCallback !== currentOriginalCallback;
|
|
314
|
+
|
|
315
|
+
// If middleware exists but callback is not wrapped, wrap it
|
|
316
|
+
if (hasMiddleware && !isWrapped) {
|
|
317
|
+
currentGlobalCallback = middleware.wrapCallback(currentOriginalCallback);
|
|
318
|
+
globalCallback = currentGlobalCallback;
|
|
319
|
+
logger("Middleware added - callback re-wrapped", "info");
|
|
320
|
+
}
|
|
321
|
+
// If no middleware but callback is wrapped, unwrap it
|
|
322
|
+
else if (!hasMiddleware && isWrapped) {
|
|
323
|
+
currentGlobalCallback = currentOriginalCallback;
|
|
324
|
+
globalCallback = currentGlobalCallback;
|
|
325
|
+
logger("All middleware removed - callback unwrapped", "info");
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// Expose middleware API with re-wrapping support
|
|
330
|
+
api.useMiddleware = function (middlewareFn, fn) {
|
|
331
|
+
const result = middleware.use(middlewareFn, fn);
|
|
332
|
+
rewrapCallbackIfNeeded();
|
|
333
|
+
return result;
|
|
334
|
+
};
|
|
335
|
+
api.removeMiddleware = function (identifier) {
|
|
336
|
+
const result = middleware.remove(identifier);
|
|
337
|
+
rewrapCallbackIfNeeded();
|
|
338
|
+
return result;
|
|
339
|
+
};
|
|
340
|
+
api.clearMiddleware = function () {
|
|
341
|
+
const result = middleware.clear();
|
|
342
|
+
rewrapCallbackIfNeeded();
|
|
343
|
+
return result;
|
|
344
|
+
};
|
|
345
|
+
api.listMiddleware = function () {
|
|
346
|
+
return middleware.list();
|
|
347
|
+
};
|
|
348
|
+
api.setMiddlewareEnabled = function (name, enabled) {
|
|
349
|
+
const result = middleware.setEnabled(name, enabled);
|
|
350
|
+
rewrapCallbackIfNeeded();
|
|
351
|
+
return result;
|
|
352
|
+
};
|
|
353
|
+
// Avoid crashing on restart: defineProperty throws if already defined and non-configurable.
|
|
354
|
+
const existingMiddlewareCount = Object.getOwnPropertyDescriptor(api, "middlewareCount");
|
|
355
|
+
if (!existingMiddlewareCount) {
|
|
356
|
+
Object.defineProperty(api, "middlewareCount", {
|
|
357
|
+
configurable: true,
|
|
358
|
+
enumerable: false,
|
|
359
|
+
get: function () { return (ctx._middleware && ctx._middleware.count) || 0; }
|
|
360
|
+
});
|
|
361
|
+
} else if (existingMiddlewareCount.configurable) {
|
|
362
|
+
Object.defineProperty(api, "middlewareCount", {
|
|
363
|
+
configurable: true,
|
|
364
|
+
enumerable: existingMiddlewareCount.enumerable,
|
|
365
|
+
get: function () { return (ctx._middleware && ctx._middleware.count) || 0; }
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
return msgEmitter;
|
|
370
|
+
};
|
|
371
|
+
};
|