mqtt 2.18.9 → 3.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/CONTRIBUTING.md +27 -27
- package/LICENSE.md +15 -15
- package/README.md +689 -612
- package/bin/pub.js +146 -146
- package/bin/sub.js +123 -123
- package/dist/mqtt.js +11916 -14116
- package/dist/mqtt.min.js +1 -1
- package/doc/help.txt +8 -8
- package/doc/publish.txt +26 -26
- package/doc/subscribe.txt +26 -26
- package/examples/client/secure-client.js +24 -24
- package/examples/client/simple-both.js +13 -13
- package/examples/client/simple-publish.js +7 -7
- package/examples/client/simple-subscribe.js +9 -9
- package/examples/tls client/crt.ca.cg.pem +34 -34
- package/examples/tls client/mqttclient.js +48 -48
- package/examples/tls client/tls-cert.pem +13 -13
- package/examples/tls client/tls-key.pem +15 -15
- package/examples/wss/client.js +49 -49
- package/examples/wss/client_with_proxy.js +58 -58
- package/lib/client.js +1460 -1129
- package/lib/connect/ali.js +130 -0
- package/lib/connect/index.js +157 -149
- package/lib/connect/tcp.js +19 -19
- package/lib/connect/tls.js +41 -41
- package/lib/connect/ws.js +92 -92
- package/lib/connect/wx.js +134 -110
- package/lib/store.js +140 -140
- package/lib/validations.js +52 -52
- package/mqtt.js +41 -41
- package/package.json +112 -107
- package/test/abstract_client.js +3084 -2667
- package/test/abstract_store.js +135 -135
- package/test/browser/server.js +132 -132
- package/test/browser/test.js +92 -92
- package/test/client.js +1129 -544
- package/test/helpers/private-csr.pem +16 -16
- package/test/helpers/private-key.pem +27 -27
- package/test/helpers/public-cert.pem +19 -19
- package/test/helpers/public-key.pem +9 -9
- package/test/helpers/server.js +52 -52
- package/test/helpers/server_process.js +9 -9
- package/test/helpers/tls-cert.pem +14 -14
- package/test/helpers/tls-csr.pem +11 -11
- package/test/helpers/tls-key.pem +15 -15
- package/test/helpers/wrong-cert.pem +13 -13
- package/test/helpers/wrong-csr.pem +11 -11
- package/test/helpers/wrong-key.pem +15 -15
- package/test/mocha.opts +4 -4
- package/test/mqtt.js +230 -230
- package/test/mqtt_store.js +9 -9
- package/test/secure_client.js +157 -157
- package/test/server.js +93 -65
- package/test/store.js +10 -10
- package/test/typescript/broker-connect-subscribe-and-publish.js +24 -0
- package/test/typescript/broker-connect-subscribe-and-publish.js.map +1 -0
- package/test/typescript/broker-connect-subscribe-and-publish.ts +22 -22
- package/test/typescript/tsconfig.json +14 -14
- package/test/util.js +13 -13
- package/test/websocket_client.js +144 -144
- package/types/index.d.ts +27 -27
- package/types/lib/client-options.d.ts +178 -137
- package/types/lib/client.d.ts +231 -196
- package/types/lib/connect/index.d.ts +10 -10
- package/types/lib/store-options.d.ts +6 -6
- package/types/lib/store.d.ts +46 -46
- package/test/browser/wx.js +0 -93
package/test/client.js
CHANGED
|
@@ -1,544 +1,1129 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
var mqtt = require('..')
|
|
4
|
-
var should = require('should')
|
|
5
|
-
var fork = require('child_process').fork
|
|
6
|
-
var path = require('path')
|
|
7
|
-
var abstractClientTests = require('./abstract_client')
|
|
8
|
-
var net = require('net')
|
|
9
|
-
var eos = require('end-of-stream')
|
|
10
|
-
var mqttPacket = require('mqtt-packet')
|
|
11
|
-
var Buffer = require('safe-buffer').Buffer
|
|
12
|
-
var Duplex = require('readable-stream').Duplex
|
|
13
|
-
var Connection = require('mqtt-connection')
|
|
14
|
-
var Server = require('./server')
|
|
15
|
-
var
|
|
16
|
-
var
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
client.on('
|
|
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
|
-
|
|
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
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
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
|
-
|
|
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
|
-
client.
|
|
256
|
-
should.exist(
|
|
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
|
-
client.
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
var
|
|
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
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
var
|
|
391
|
-
var
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
var mqtt = require('..')
|
|
4
|
+
var should = require('should')
|
|
5
|
+
var fork = require('child_process').fork
|
|
6
|
+
var path = require('path')
|
|
7
|
+
var abstractClientTests = require('./abstract_client')
|
|
8
|
+
var net = require('net')
|
|
9
|
+
var eos = require('end-of-stream')
|
|
10
|
+
var mqttPacket = require('mqtt-packet')
|
|
11
|
+
var Buffer = require('safe-buffer').Buffer
|
|
12
|
+
var Duplex = require('readable-stream').Duplex
|
|
13
|
+
var Connection = require('mqtt-connection')
|
|
14
|
+
var Server = require('./server')
|
|
15
|
+
var FastServer = require('./server').FastMqttServer
|
|
16
|
+
var port = 9876
|
|
17
|
+
var server
|
|
18
|
+
|
|
19
|
+
function connOnlyServer () {
|
|
20
|
+
return new Server(function (client) {
|
|
21
|
+
client.on('connect', function (packet) {
|
|
22
|
+
client.connack({returnCode: 0})
|
|
23
|
+
})
|
|
24
|
+
})
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Test server
|
|
29
|
+
*/
|
|
30
|
+
function buildServer (fastFlag) {
|
|
31
|
+
var handler = function (client) {
|
|
32
|
+
client.on('auth', function (packet) {
|
|
33
|
+
var rc = 'reasonCode'
|
|
34
|
+
var connack = {}
|
|
35
|
+
connack[rc] = 0
|
|
36
|
+
client.connack(connack)
|
|
37
|
+
})
|
|
38
|
+
client.on('connect', function (packet) {
|
|
39
|
+
var rc = 'returnCode'
|
|
40
|
+
var connack = {}
|
|
41
|
+
if (client.options && client.options.protocolVersion === 5) {
|
|
42
|
+
rc = 'reasonCode'
|
|
43
|
+
if (packet.clientId === 'invalid') {
|
|
44
|
+
connack[rc] = 128
|
|
45
|
+
} else {
|
|
46
|
+
connack[rc] = 0
|
|
47
|
+
}
|
|
48
|
+
} else {
|
|
49
|
+
if (packet.clientId === 'invalid') {
|
|
50
|
+
connack[rc] = 2
|
|
51
|
+
} else {
|
|
52
|
+
connack[rc] = 0
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (packet.properties && packet.properties.authenticationMethod) {
|
|
56
|
+
return false
|
|
57
|
+
} else {
|
|
58
|
+
client.connack(connack)
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
client.on('publish', function (packet) {
|
|
63
|
+
setImmediate(function () {
|
|
64
|
+
switch (packet.qos) {
|
|
65
|
+
case 0:
|
|
66
|
+
break
|
|
67
|
+
case 1:
|
|
68
|
+
client.puback(packet)
|
|
69
|
+
break
|
|
70
|
+
case 2:
|
|
71
|
+
client.pubrec(packet)
|
|
72
|
+
break
|
|
73
|
+
}
|
|
74
|
+
})
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
client.on('pubrel', function (packet) {
|
|
78
|
+
client.pubcomp(packet)
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
client.on('pubrec', function (packet) {
|
|
82
|
+
client.pubrel(packet)
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
client.on('pubcomp', function () {
|
|
86
|
+
// Nothing to be done
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
client.on('subscribe', function (packet) {
|
|
90
|
+
client.suback({
|
|
91
|
+
messageId: packet.messageId,
|
|
92
|
+
granted: packet.subscriptions.map(function (e) {
|
|
93
|
+
return e.qos
|
|
94
|
+
})
|
|
95
|
+
})
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
client.on('unsubscribe', function (packet) {
|
|
99
|
+
packet.granted = packet.unsubscriptions.map(function () { return 0 })
|
|
100
|
+
client.unsuback(packet)
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
client.on('pingreq', function () {
|
|
104
|
+
client.pingresp()
|
|
105
|
+
})
|
|
106
|
+
}
|
|
107
|
+
if (fastFlag) {
|
|
108
|
+
return new FastServer(handler)
|
|
109
|
+
} else {
|
|
110
|
+
return new Server(handler)
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
server = buildServer().listen(port)
|
|
115
|
+
|
|
116
|
+
describe('MqttClient', function () {
|
|
117
|
+
describe('creating', function () {
|
|
118
|
+
it('should allow instantiation of MqttClient without the \'new\' operator', function (done) {
|
|
119
|
+
should(function () {
|
|
120
|
+
var client
|
|
121
|
+
try {
|
|
122
|
+
client = mqtt.MqttClient(function () {
|
|
123
|
+
throw Error('break')
|
|
124
|
+
}, {})
|
|
125
|
+
client.end()
|
|
126
|
+
} catch (err) {
|
|
127
|
+
if (err.message !== 'break') {
|
|
128
|
+
throw err
|
|
129
|
+
}
|
|
130
|
+
done()
|
|
131
|
+
}
|
|
132
|
+
}).not.throw('Object #<Object> has no method \'_setupStream\'')
|
|
133
|
+
})
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
var config = { protocol: 'mqtt', port: port }
|
|
137
|
+
abstractClientTests(server, config)
|
|
138
|
+
|
|
139
|
+
describe('message ids', function () {
|
|
140
|
+
it('should increment the message id', function () {
|
|
141
|
+
var client = mqtt.connect(config)
|
|
142
|
+
var currentId = client._nextId()
|
|
143
|
+
|
|
144
|
+
client._nextId().should.equal(currentId + 1)
|
|
145
|
+
client.end()
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
it('should return 1 once the internal counter reached limit', function () {
|
|
149
|
+
var client = mqtt.connect(config)
|
|
150
|
+
client.nextId = 65535
|
|
151
|
+
|
|
152
|
+
client._nextId().should.equal(65535)
|
|
153
|
+
client._nextId().should.equal(1)
|
|
154
|
+
client.end()
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
it('should return 65535 for last message id once the internal counter reached limit', function () {
|
|
158
|
+
var client = mqtt.connect(config)
|
|
159
|
+
client.nextId = 65535
|
|
160
|
+
|
|
161
|
+
client._nextId().should.equal(65535)
|
|
162
|
+
client.getLastMessageId().should.equal(65535)
|
|
163
|
+
client._nextId().should.equal(1)
|
|
164
|
+
client.getLastMessageId().should.equal(1)
|
|
165
|
+
client.end()
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
it('should not throw an error if packet\'s messageId is not found when receiving a pubrel packet', function (done) {
|
|
169
|
+
var server2 = new Server(function (c) {
|
|
170
|
+
c.on('connect', function (packet) {
|
|
171
|
+
c.connack({returnCode: 0})
|
|
172
|
+
c.pubrel({ messageId: Math.floor(Math.random() * 9000) + 1000 })
|
|
173
|
+
})
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
server2.listen(port + 49, function () {
|
|
177
|
+
var client = mqtt.connect({
|
|
178
|
+
port: port + 49,
|
|
179
|
+
host: 'localhost'
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
client.on('packetsend', function (packet) {
|
|
183
|
+
if (packet.cmd === 'pubcomp') {
|
|
184
|
+
client.end()
|
|
185
|
+
server2.close()
|
|
186
|
+
done()
|
|
187
|
+
}
|
|
188
|
+
})
|
|
189
|
+
})
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
it('should not go overflow if the TCP frame contains a lot of PUBLISH packets', function (done) {
|
|
193
|
+
var parser = mqttPacket.parser()
|
|
194
|
+
var count = 0
|
|
195
|
+
var max = 1000
|
|
196
|
+
var duplex = new Duplex({
|
|
197
|
+
read: function (n) {},
|
|
198
|
+
write: function (chunk, enc, cb) {
|
|
199
|
+
parser.parse(chunk)
|
|
200
|
+
cb() // nothing to do
|
|
201
|
+
}
|
|
202
|
+
})
|
|
203
|
+
var client = new mqtt.MqttClient(function () {
|
|
204
|
+
return duplex
|
|
205
|
+
}, {})
|
|
206
|
+
|
|
207
|
+
client.on('message', function (t, p, packet) {
|
|
208
|
+
if (++count === max) {
|
|
209
|
+
done()
|
|
210
|
+
}
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
parser.on('packet', function (packet) {
|
|
214
|
+
var packets = []
|
|
215
|
+
|
|
216
|
+
if (packet.cmd === 'connect') {
|
|
217
|
+
duplex.push(mqttPacket.generate({
|
|
218
|
+
cmd: 'connack',
|
|
219
|
+
sessionPresent: false,
|
|
220
|
+
returnCode: 0
|
|
221
|
+
}))
|
|
222
|
+
|
|
223
|
+
for (var i = 0; i < max; i++) {
|
|
224
|
+
packets.push(mqttPacket.generate({
|
|
225
|
+
cmd: 'publish',
|
|
226
|
+
topic: Buffer.from('hello'),
|
|
227
|
+
payload: Buffer.from('world'),
|
|
228
|
+
retain: false,
|
|
229
|
+
dup: false,
|
|
230
|
+
messageId: i + 1,
|
|
231
|
+
qos: 1
|
|
232
|
+
}))
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
duplex.push(Buffer.concat(packets))
|
|
236
|
+
}
|
|
237
|
+
})
|
|
238
|
+
})
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
describe('flushing', function () {
|
|
242
|
+
it('should attempt to complete pending unsub and send on ping timeout', function (done) {
|
|
243
|
+
this.timeout(10000)
|
|
244
|
+
var server3 = connOnlyServer().listen(port + 72)
|
|
245
|
+
var pubCallbackCalled = false
|
|
246
|
+
var unsubscribeCallbackCalled = false
|
|
247
|
+
var client = mqtt.connect({
|
|
248
|
+
port: port + 72,
|
|
249
|
+
host: 'localhost',
|
|
250
|
+
keepalive: 1,
|
|
251
|
+
connectTimeout: 350,
|
|
252
|
+
reconnectPeriod: 0
|
|
253
|
+
})
|
|
254
|
+
client.once('connect', () => {
|
|
255
|
+
client.publish('fakeTopic', 'fakeMessage', {qos: 1}, (err, result) => {
|
|
256
|
+
should.exist(err)
|
|
257
|
+
pubCallbackCalled = true
|
|
258
|
+
})
|
|
259
|
+
client.unsubscribe('fakeTopic', (err, result) => {
|
|
260
|
+
should.exist(err)
|
|
261
|
+
unsubscribeCallbackCalled = true
|
|
262
|
+
})
|
|
263
|
+
setTimeout(() => {
|
|
264
|
+
client.end(() => {
|
|
265
|
+
should.equal(pubCallbackCalled && unsubscribeCallbackCalled, true, 'callbacks not invoked')
|
|
266
|
+
server3.close()
|
|
267
|
+
done()
|
|
268
|
+
})
|
|
269
|
+
}, 5000)
|
|
270
|
+
})
|
|
271
|
+
})
|
|
272
|
+
})
|
|
273
|
+
|
|
274
|
+
describe('reconnecting', function () {
|
|
275
|
+
it('should attempt to reconnect once server is down', function (done) {
|
|
276
|
+
this.timeout(15000)
|
|
277
|
+
|
|
278
|
+
var innerServer = fork(path.join(__dirname, 'helpers', 'server_process.js'))
|
|
279
|
+
var client = mqtt.connect({ port: 3000, host: 'localhost', keepalive: 1 })
|
|
280
|
+
|
|
281
|
+
client.once('connect', function () {
|
|
282
|
+
innerServer.kill('SIGINT') // mocks server shutdown
|
|
283
|
+
|
|
284
|
+
client.once('close', function () {
|
|
285
|
+
should.exist(client.reconnectTimer)
|
|
286
|
+
client.end()
|
|
287
|
+
done()
|
|
288
|
+
})
|
|
289
|
+
})
|
|
290
|
+
})
|
|
291
|
+
|
|
292
|
+
it('should reconnect to multiple host-ports-protocol combinations if servers is passed', function (done) {
|
|
293
|
+
this.timeout(15000)
|
|
294
|
+
|
|
295
|
+
var server = buildServer(true).listen(port + 41)
|
|
296
|
+
var server2 = buildServer(true).listen(port + 42)
|
|
297
|
+
|
|
298
|
+
server2.on('listening', function () {
|
|
299
|
+
var client = mqtt.connect({
|
|
300
|
+
protocol: 'wss',
|
|
301
|
+
servers: [
|
|
302
|
+
{ port: port + 42, host: 'localhost', protocol: 'ws' },
|
|
303
|
+
{ port: port + 41, host: 'localhost' }
|
|
304
|
+
],
|
|
305
|
+
keepalive: 50
|
|
306
|
+
})
|
|
307
|
+
server2.on('client', function (c) {
|
|
308
|
+
should.equal(client.stream.socket.url, 'ws://localhost:9918/', 'Protocol for first connection should use ws.')
|
|
309
|
+
c.stream.destroy()
|
|
310
|
+
server2.close()
|
|
311
|
+
})
|
|
312
|
+
|
|
313
|
+
server.once('client', function () {
|
|
314
|
+
should.equal(client.stream.socket.url, 'wss://localhost:9917/', 'Protocol for second client should use the default protocol: wss, on port: port + 42.')
|
|
315
|
+
client.end()
|
|
316
|
+
done()
|
|
317
|
+
})
|
|
318
|
+
|
|
319
|
+
client.once('connect', function () {
|
|
320
|
+
client.stream.destroy()
|
|
321
|
+
})
|
|
322
|
+
})
|
|
323
|
+
})
|
|
324
|
+
|
|
325
|
+
it('should reconnect if a connack is not received in an interval', function (done) {
|
|
326
|
+
this.timeout(2000)
|
|
327
|
+
|
|
328
|
+
var server2 = net.createServer().listen(port + 43)
|
|
329
|
+
|
|
330
|
+
server2.on('connection', function (c) {
|
|
331
|
+
eos(c, function () {
|
|
332
|
+
server2.close()
|
|
333
|
+
})
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
server2.on('listening', function () {
|
|
337
|
+
var client = mqtt.connect({
|
|
338
|
+
servers: [
|
|
339
|
+
{ port: port + 43, host: 'localhost_fake' },
|
|
340
|
+
{ port: port, host: 'localhost' }
|
|
341
|
+
],
|
|
342
|
+
connectTimeout: 500
|
|
343
|
+
})
|
|
344
|
+
|
|
345
|
+
server.once('client', function () {
|
|
346
|
+
client.end()
|
|
347
|
+
done()
|
|
348
|
+
})
|
|
349
|
+
|
|
350
|
+
client.once('connect', function () {
|
|
351
|
+
client.stream.destroy()
|
|
352
|
+
})
|
|
353
|
+
})
|
|
354
|
+
})
|
|
355
|
+
|
|
356
|
+
it('should not be cleared by the connack timer', function (done) {
|
|
357
|
+
this.timeout(4000)
|
|
358
|
+
|
|
359
|
+
var server2 = net.createServer().listen(port + 44)
|
|
360
|
+
|
|
361
|
+
server2.on('connection', function (c) {
|
|
362
|
+
c.destroy()
|
|
363
|
+
})
|
|
364
|
+
|
|
365
|
+
server2.once('listening', function () {
|
|
366
|
+
var reconnects = 0
|
|
367
|
+
var connectTimeout = 1000
|
|
368
|
+
var reconnectPeriod = 100
|
|
369
|
+
var expectedReconnects = Math.floor(connectTimeout / reconnectPeriod)
|
|
370
|
+
var client = mqtt.connect({
|
|
371
|
+
port: port + 44,
|
|
372
|
+
host: 'localhost',
|
|
373
|
+
connectTimeout: connectTimeout,
|
|
374
|
+
reconnectPeriod: reconnectPeriod
|
|
375
|
+
})
|
|
376
|
+
|
|
377
|
+
client.on('reconnect', function () {
|
|
378
|
+
reconnects++
|
|
379
|
+
if (reconnects >= expectedReconnects) {
|
|
380
|
+
client.end()
|
|
381
|
+
done()
|
|
382
|
+
}
|
|
383
|
+
})
|
|
384
|
+
})
|
|
385
|
+
})
|
|
386
|
+
|
|
387
|
+
it('should not keep requeueing the first message when offline', function (done) {
|
|
388
|
+
this.timeout(2500)
|
|
389
|
+
|
|
390
|
+
var server2 = buildServer().listen(port + 45)
|
|
391
|
+
var client = mqtt.connect({
|
|
392
|
+
port: port + 45,
|
|
393
|
+
host: 'localhost',
|
|
394
|
+
connectTimeout: 350,
|
|
395
|
+
reconnectPeriod: 300
|
|
396
|
+
})
|
|
397
|
+
|
|
398
|
+
server2.on('client', function (c) {
|
|
399
|
+
client.publish('hello', 'world', { qos: 1 }, function () {
|
|
400
|
+
c.destroy()
|
|
401
|
+
server2.close()
|
|
402
|
+
client.publish('hello', 'world', { qos: 1 })
|
|
403
|
+
})
|
|
404
|
+
})
|
|
405
|
+
|
|
406
|
+
setTimeout(function () {
|
|
407
|
+
if (client.queue.length === 0) {
|
|
408
|
+
client.end(true)
|
|
409
|
+
done()
|
|
410
|
+
} else {
|
|
411
|
+
client.end(true)
|
|
412
|
+
}
|
|
413
|
+
}, 2000)
|
|
414
|
+
})
|
|
415
|
+
|
|
416
|
+
it('should not send the same subscribe multiple times on a flaky connection', function (done) {
|
|
417
|
+
this.timeout(3500)
|
|
418
|
+
|
|
419
|
+
var KILL_COUNT = 4
|
|
420
|
+
var killedConnections = 0
|
|
421
|
+
var subIds = {}
|
|
422
|
+
var client = mqtt.connect({
|
|
423
|
+
port: port + 46,
|
|
424
|
+
host: 'localhost',
|
|
425
|
+
connectTimeout: 350,
|
|
426
|
+
reconnectPeriod: 300
|
|
427
|
+
})
|
|
428
|
+
|
|
429
|
+
var server2 = new Server(function (client) {
|
|
430
|
+
client.on('error', function () {})
|
|
431
|
+
client.on('connect', function (packet) {
|
|
432
|
+
if (packet.clientId === 'invalid') {
|
|
433
|
+
client.connack({returnCode: 2})
|
|
434
|
+
} else {
|
|
435
|
+
client.connack({returnCode: 0})
|
|
436
|
+
}
|
|
437
|
+
})
|
|
438
|
+
}).listen(port + 46)
|
|
439
|
+
|
|
440
|
+
server2.on('client', function (c) {
|
|
441
|
+
client.subscribe('topic', function () {
|
|
442
|
+
done()
|
|
443
|
+
client.end()
|
|
444
|
+
c.destroy()
|
|
445
|
+
server2.close()
|
|
446
|
+
})
|
|
447
|
+
|
|
448
|
+
c.on('subscribe', function (packet) {
|
|
449
|
+
if (killedConnections < KILL_COUNT) {
|
|
450
|
+
// Kill the first few sub attempts to simulate a flaky connection
|
|
451
|
+
killedConnections++
|
|
452
|
+
c.destroy()
|
|
453
|
+
} else {
|
|
454
|
+
// Keep track of acks
|
|
455
|
+
if (!subIds[packet.messageId]) {
|
|
456
|
+
subIds[packet.messageId] = 0
|
|
457
|
+
}
|
|
458
|
+
subIds[packet.messageId]++
|
|
459
|
+
if (subIds[packet.messageId] > 1) {
|
|
460
|
+
done(new Error('Multiple duplicate acked subscriptions received for messageId ' + packet.messageId))
|
|
461
|
+
client.end(true)
|
|
462
|
+
c.destroy()
|
|
463
|
+
server2.destroy()
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
c.suback({
|
|
467
|
+
messageId: packet.messageId,
|
|
468
|
+
granted: packet.subscriptions.map(function (e) {
|
|
469
|
+
return e.qos
|
|
470
|
+
})
|
|
471
|
+
})
|
|
472
|
+
}
|
|
473
|
+
})
|
|
474
|
+
})
|
|
475
|
+
})
|
|
476
|
+
|
|
477
|
+
it('should not fill the queue of subscribes if it cannot connect', function (done) {
|
|
478
|
+
this.timeout(2500)
|
|
479
|
+
|
|
480
|
+
var port2 = port + 48
|
|
481
|
+
|
|
482
|
+
var server2 = net.createServer(function (stream) {
|
|
483
|
+
var client = new Connection(stream)
|
|
484
|
+
|
|
485
|
+
client.on('error', function () {})
|
|
486
|
+
client.on('connect', function (packet) {
|
|
487
|
+
client.connack({returnCode: 0})
|
|
488
|
+
client.destroy()
|
|
489
|
+
})
|
|
490
|
+
})
|
|
491
|
+
|
|
492
|
+
server2.listen(port2, function () {
|
|
493
|
+
var client = mqtt.connect({
|
|
494
|
+
port: port2,
|
|
495
|
+
host: 'localhost',
|
|
496
|
+
connectTimeout: 350,
|
|
497
|
+
reconnectPeriod: 300
|
|
498
|
+
})
|
|
499
|
+
|
|
500
|
+
client.subscribe('hello')
|
|
501
|
+
|
|
502
|
+
setTimeout(function () {
|
|
503
|
+
client.queue.length.should.equal(1)
|
|
504
|
+
client.end()
|
|
505
|
+
done()
|
|
506
|
+
}, 1000)
|
|
507
|
+
})
|
|
508
|
+
})
|
|
509
|
+
|
|
510
|
+
it('should not send the same publish multiple times on a flaky connection', function (done) {
|
|
511
|
+
this.timeout(3500)
|
|
512
|
+
|
|
513
|
+
var KILL_COUNT = 4
|
|
514
|
+
var killedConnections = 0
|
|
515
|
+
var pubIds = {}
|
|
516
|
+
var client = mqtt.connect({
|
|
517
|
+
port: port + 47,
|
|
518
|
+
host: 'localhost',
|
|
519
|
+
connectTimeout: 350,
|
|
520
|
+
reconnectPeriod: 300
|
|
521
|
+
})
|
|
522
|
+
|
|
523
|
+
var server2 = net.createServer(function (stream) {
|
|
524
|
+
var client = new Connection(stream)
|
|
525
|
+
client.on('error', function () {})
|
|
526
|
+
client.on('connect', function (packet) {
|
|
527
|
+
if (packet.clientId === 'invalid') {
|
|
528
|
+
client.connack({returnCode: 2})
|
|
529
|
+
} else {
|
|
530
|
+
client.connack({returnCode: 0})
|
|
531
|
+
}
|
|
532
|
+
})
|
|
533
|
+
|
|
534
|
+
this.emit('client', client)
|
|
535
|
+
}).listen(port + 47)
|
|
536
|
+
|
|
537
|
+
server2.on('client', function (c) {
|
|
538
|
+
client.publish('topic', 'data', { qos: 1 }, function () {
|
|
539
|
+
done()
|
|
540
|
+
client.end()
|
|
541
|
+
c.destroy()
|
|
542
|
+
server2.destroy()
|
|
543
|
+
})
|
|
544
|
+
|
|
545
|
+
c.on('publish', function onPublish (packet) {
|
|
546
|
+
if (killedConnections < KILL_COUNT) {
|
|
547
|
+
// Kill the first few pub attempts to simulate a flaky connection
|
|
548
|
+
killedConnections++
|
|
549
|
+
c.destroy()
|
|
550
|
+
|
|
551
|
+
// to avoid receiving inflight messages
|
|
552
|
+
c.removeListener('publish', onPublish)
|
|
553
|
+
} else {
|
|
554
|
+
// Keep track of acks
|
|
555
|
+
if (!pubIds[packet.messageId]) {
|
|
556
|
+
pubIds[packet.messageId] = 0
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
pubIds[packet.messageId]++
|
|
560
|
+
|
|
561
|
+
if (pubIds[packet.messageId] > 1) {
|
|
562
|
+
done(new Error('Multiple duplicate acked publishes received for messageId ' + packet.messageId))
|
|
563
|
+
client.end(true)
|
|
564
|
+
c.destroy()
|
|
565
|
+
server2.destroy()
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
c.puback(packet)
|
|
569
|
+
}
|
|
570
|
+
})
|
|
571
|
+
})
|
|
572
|
+
})
|
|
573
|
+
})
|
|
574
|
+
|
|
575
|
+
it('check emit error on checkDisconnection w/o callback', function (done) {
|
|
576
|
+
this.timeout(15000)
|
|
577
|
+
var server118 = new Server(function (client) {
|
|
578
|
+
client.on('connect', function (packet) {
|
|
579
|
+
client.connack({
|
|
580
|
+
reasonCode: 0
|
|
581
|
+
})
|
|
582
|
+
})
|
|
583
|
+
client.on('publish', function (packet) {
|
|
584
|
+
setImmediate(function () {
|
|
585
|
+
packet.reasonCode = 0
|
|
586
|
+
client.puback(packet)
|
|
587
|
+
})
|
|
588
|
+
})
|
|
589
|
+
}).listen(port + 118)
|
|
590
|
+
var opts = {
|
|
591
|
+
host: 'localhost',
|
|
592
|
+
port: port + 118,
|
|
593
|
+
protocolVersion: 5
|
|
594
|
+
}
|
|
595
|
+
var client = mqtt.connect(opts)
|
|
596
|
+
client.on('error', function (error) {
|
|
597
|
+
should(error.message).be.equal('client disconnecting')
|
|
598
|
+
server118.close()
|
|
599
|
+
done()
|
|
600
|
+
})
|
|
601
|
+
client.on('connect', function () {
|
|
602
|
+
client.end(function () {
|
|
603
|
+
client._checkDisconnecting()
|
|
604
|
+
})
|
|
605
|
+
server118.close()
|
|
606
|
+
})
|
|
607
|
+
})
|
|
608
|
+
|
|
609
|
+
describe('MQTT 5.0', function () {
|
|
610
|
+
var server = buildServer().listen(port + 115)
|
|
611
|
+
var config = { protocol: 'mqtt', port: port + 115, protocolVersion: 5, properties: { maximumPacketSize: 200 } }
|
|
612
|
+
abstractClientTests(server, config)
|
|
613
|
+
it('should has Auth method with Auth data', function (done) {
|
|
614
|
+
this.timeout(5000)
|
|
615
|
+
var opts = {host: 'localhost', port: port + 115, protocolVersion: 5, properties: { authenticationData: Buffer.from([1, 2, 3, 4]) }}
|
|
616
|
+
try {
|
|
617
|
+
mqtt.connect(opts)
|
|
618
|
+
} catch (error) {
|
|
619
|
+
should(error.message).be.equal('Packet has no Authentication Method')
|
|
620
|
+
}
|
|
621
|
+
done()
|
|
622
|
+
})
|
|
623
|
+
it('auth packet', function (done) {
|
|
624
|
+
this.timeout(15000)
|
|
625
|
+
server.once('client', function (client) {
|
|
626
|
+
client.on('auth', function (packet) {
|
|
627
|
+
done()
|
|
628
|
+
})
|
|
629
|
+
})
|
|
630
|
+
var opts = {host: 'localhost', port: port + 115, protocolVersion: 5, properties: { authenticationMethod: 'json' }, authPacket: {}}
|
|
631
|
+
mqtt.connect(opts)
|
|
632
|
+
})
|
|
633
|
+
it('Maximum Packet Size', function (done) {
|
|
634
|
+
this.timeout(15000)
|
|
635
|
+
var opts = {host: 'localhost', port: port + 115, protocolVersion: 5, properties: { maximumPacketSize: 1 }}
|
|
636
|
+
var client = mqtt.connect(opts)
|
|
637
|
+
client.on('error', function (error) {
|
|
638
|
+
should(error.message).be.equal('exceeding packets size connack')
|
|
639
|
+
done()
|
|
640
|
+
})
|
|
641
|
+
})
|
|
642
|
+
describe('Topic Alias', function () {
|
|
643
|
+
it('topicAlias > topicAliasMaximum', function (done) {
|
|
644
|
+
this.timeout(15000)
|
|
645
|
+
var maximum = 15
|
|
646
|
+
var current = 22
|
|
647
|
+
server.once('client', function (client) {
|
|
648
|
+
client.on('publish', function (packet) {
|
|
649
|
+
if (packet.properties && packet.properties.topicAlias) {
|
|
650
|
+
done(new Error('Packet should not have topicAlias'))
|
|
651
|
+
return false
|
|
652
|
+
}
|
|
653
|
+
done()
|
|
654
|
+
})
|
|
655
|
+
})
|
|
656
|
+
var opts = {host: 'localhost', port: port + 115, protocolVersion: 5, properties: { topicAliasMaximum: maximum }}
|
|
657
|
+
var client = mqtt.connect(opts)
|
|
658
|
+
client.publish('t/h', 'Message', { properties: { topicAlias: current } })
|
|
659
|
+
})
|
|
660
|
+
it('topicAlias w/o topicAliasMaximum in settings', function (done) {
|
|
661
|
+
this.timeout(15000)
|
|
662
|
+
server.once('client', function (client) {
|
|
663
|
+
client.on('publish', function (packet) {
|
|
664
|
+
if (packet.properties && packet.properties.topicAlias) {
|
|
665
|
+
done(new Error('Packet should not have topicAlias'))
|
|
666
|
+
return false
|
|
667
|
+
}
|
|
668
|
+
done()
|
|
669
|
+
})
|
|
670
|
+
})
|
|
671
|
+
var opts = {host: 'localhost', port: port + 115, protocolVersion: 5}
|
|
672
|
+
var client = mqtt.connect(opts)
|
|
673
|
+
client.publish('t/h', 'Message', { properties: { topicAlias: 22 } })
|
|
674
|
+
})
|
|
675
|
+
})
|
|
676
|
+
it('Change values of some properties by server response', function (done) {
|
|
677
|
+
this.timeout(15000)
|
|
678
|
+
var server116 = new Server(function (client) {
|
|
679
|
+
client.on('connect', function (packet) {
|
|
680
|
+
client.connack({
|
|
681
|
+
reasonCode: 0,
|
|
682
|
+
properties: {
|
|
683
|
+
topicAliasMaximum: 15,
|
|
684
|
+
serverKeepAlive: 16,
|
|
685
|
+
maximumPacketSize: 95
|
|
686
|
+
}
|
|
687
|
+
})
|
|
688
|
+
})
|
|
689
|
+
}).listen(port + 116)
|
|
690
|
+
var opts = {
|
|
691
|
+
host: 'localhost',
|
|
692
|
+
port: port + 116,
|
|
693
|
+
protocolVersion: 5,
|
|
694
|
+
properties: {
|
|
695
|
+
topicAliasMaximum: 10,
|
|
696
|
+
serverKeepAlive: 11,
|
|
697
|
+
maximumPacketSize: 100
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
var client = mqtt.connect(opts)
|
|
701
|
+
client.on('connect', function () {
|
|
702
|
+
should(client.options.keepalive).be.equal(16)
|
|
703
|
+
should(client.options.properties.topicAliasMaximum).be.equal(15)
|
|
704
|
+
should(client.options.properties.maximumPacketSize).be.equal(95)
|
|
705
|
+
server116.close()
|
|
706
|
+
done()
|
|
707
|
+
})
|
|
708
|
+
})
|
|
709
|
+
|
|
710
|
+
it('should resubscribe when reconnecting with protocolVersion 5 and Session Present flag is false', function (done) {
|
|
711
|
+
this.timeout(15000)
|
|
712
|
+
var tryReconnect = true
|
|
713
|
+
var reconnectEvent = false
|
|
714
|
+
var server316 = new Server(function (client) {
|
|
715
|
+
client.on('connect', function (packet) {
|
|
716
|
+
client.connack({
|
|
717
|
+
reasonCode: 0,
|
|
718
|
+
sessionPresent: false
|
|
719
|
+
})
|
|
720
|
+
client.on('subscribe', function () {
|
|
721
|
+
if (!tryReconnect) {
|
|
722
|
+
client.end()
|
|
723
|
+
server316.close()
|
|
724
|
+
done()
|
|
725
|
+
}
|
|
726
|
+
})
|
|
727
|
+
})
|
|
728
|
+
}).listen(port + 316)
|
|
729
|
+
var opts = {
|
|
730
|
+
host: 'localhost',
|
|
731
|
+
port: port + 316,
|
|
732
|
+
protocolVersion: 5
|
|
733
|
+
}
|
|
734
|
+
var client = mqtt.connect(opts)
|
|
735
|
+
|
|
736
|
+
client.on('reconnect', function () {
|
|
737
|
+
reconnectEvent = true
|
|
738
|
+
})
|
|
739
|
+
|
|
740
|
+
client.on('connect', function (connack) {
|
|
741
|
+
should(connack.sessionPresent).be.equal(false)
|
|
742
|
+
if (tryReconnect) {
|
|
743
|
+
client.subscribe('hello', function () {
|
|
744
|
+
client.stream.end()
|
|
745
|
+
})
|
|
746
|
+
|
|
747
|
+
tryReconnect = false
|
|
748
|
+
} else {
|
|
749
|
+
reconnectEvent.should.equal(true)
|
|
750
|
+
}
|
|
751
|
+
})
|
|
752
|
+
})
|
|
753
|
+
|
|
754
|
+
it('should resubscribe when reconnecting with protocolVersion 5 and properties', function (done) {
|
|
755
|
+
this.timeout(15000)
|
|
756
|
+
var tryReconnect = true
|
|
757
|
+
var reconnectEvent = false
|
|
758
|
+
var server326 = new Server(function (client) {
|
|
759
|
+
client.on('connect', function (packet) {
|
|
760
|
+
client.on('subscribe', function (packet) {
|
|
761
|
+
if (!reconnectEvent) {
|
|
762
|
+
client.suback({
|
|
763
|
+
messageId: packet.messageId,
|
|
764
|
+
granted: packet.subscriptions.map(function (e) {
|
|
765
|
+
return e.qos
|
|
766
|
+
})
|
|
767
|
+
})
|
|
768
|
+
} else {
|
|
769
|
+
if (!tryReconnect) {
|
|
770
|
+
should(packet.properties.userProperties.test).be.equal('test')
|
|
771
|
+
client.end()
|
|
772
|
+
server326.close()
|
|
773
|
+
done()
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
})
|
|
777
|
+
client.connack({
|
|
778
|
+
reasonCode: 0,
|
|
779
|
+
sessionPresent: false
|
|
780
|
+
})
|
|
781
|
+
})
|
|
782
|
+
}).listen(port + 326)
|
|
783
|
+
var opts = {
|
|
784
|
+
host: 'localhost',
|
|
785
|
+
port: port + 326,
|
|
786
|
+
protocolVersion: 5
|
|
787
|
+
}
|
|
788
|
+
var client = mqtt.connect(opts)
|
|
789
|
+
|
|
790
|
+
client.on('reconnect', function () {
|
|
791
|
+
reconnectEvent = true
|
|
792
|
+
})
|
|
793
|
+
|
|
794
|
+
client.on('connect', function (connack) {
|
|
795
|
+
should(connack.sessionPresent).be.equal(false)
|
|
796
|
+
if (tryReconnect) {
|
|
797
|
+
client.subscribe('hello', { properties: { userProperties: { test: 'test' } } }, function () {
|
|
798
|
+
client.stream.end()
|
|
799
|
+
})
|
|
800
|
+
|
|
801
|
+
tryReconnect = false
|
|
802
|
+
} else {
|
|
803
|
+
reconnectEvent.should.equal(true)
|
|
804
|
+
}
|
|
805
|
+
})
|
|
806
|
+
})
|
|
807
|
+
|
|
808
|
+
var serverErr = new Server(function (client) {
|
|
809
|
+
client.on('connect', function (packet) {
|
|
810
|
+
client.connack({
|
|
811
|
+
reasonCode: 0
|
|
812
|
+
})
|
|
813
|
+
})
|
|
814
|
+
client.on('publish', function (packet) {
|
|
815
|
+
setImmediate(function () {
|
|
816
|
+
switch (packet.qos) {
|
|
817
|
+
case 0:
|
|
818
|
+
break
|
|
819
|
+
case 1:
|
|
820
|
+
packet.reasonCode = 142
|
|
821
|
+
delete packet.cmd
|
|
822
|
+
client.puback(packet)
|
|
823
|
+
break
|
|
824
|
+
case 2:
|
|
825
|
+
packet.reasonCode = 142
|
|
826
|
+
delete packet.cmd
|
|
827
|
+
client.pubrec(packet)
|
|
828
|
+
break
|
|
829
|
+
}
|
|
830
|
+
})
|
|
831
|
+
})
|
|
832
|
+
|
|
833
|
+
client.on('pubrel', function (packet) {
|
|
834
|
+
packet.reasonCode = 142
|
|
835
|
+
delete packet.cmd
|
|
836
|
+
client.pubcomp(packet)
|
|
837
|
+
})
|
|
838
|
+
})
|
|
839
|
+
it('Subscribe properties', function (done) {
|
|
840
|
+
this.timeout(15000)
|
|
841
|
+
var opts = {
|
|
842
|
+
host: 'localhost',
|
|
843
|
+
port: port + 119,
|
|
844
|
+
protocolVersion: 5
|
|
845
|
+
}
|
|
846
|
+
var subOptions = { properties: { subscriptionIdentifier: 1234 } }
|
|
847
|
+
var server119 = new Server(function (client) {
|
|
848
|
+
client.on('connect', function (packet) {
|
|
849
|
+
client.connack({
|
|
850
|
+
reasonCode: 0
|
|
851
|
+
})
|
|
852
|
+
})
|
|
853
|
+
client.on('subscribe', function (packet) {
|
|
854
|
+
should(packet.properties.subscriptionIdentifier).be.equal(subOptions.properties.subscriptionIdentifier)
|
|
855
|
+
server119.close()
|
|
856
|
+
done()
|
|
857
|
+
})
|
|
858
|
+
}).listen(port + 119)
|
|
859
|
+
|
|
860
|
+
var client = mqtt.connect(opts)
|
|
861
|
+
client.on('connect', function () {
|
|
862
|
+
client.subscribe('a/b', subOptions)
|
|
863
|
+
})
|
|
864
|
+
})
|
|
865
|
+
|
|
866
|
+
it('puback handling errors check', function (done) {
|
|
867
|
+
this.timeout(15000)
|
|
868
|
+
serverErr.listen(port + 117)
|
|
869
|
+
var opts = {
|
|
870
|
+
host: 'localhost',
|
|
871
|
+
port: port + 117,
|
|
872
|
+
protocolVersion: 5
|
|
873
|
+
}
|
|
874
|
+
var client = mqtt.connect(opts)
|
|
875
|
+
client.once('connect', () => {
|
|
876
|
+
client.publish('a/b', 'message', {qos: 1}, function (err, packet) {
|
|
877
|
+
should(err.message).be.equal('Publish error: Session taken over')
|
|
878
|
+
should(err.code).be.equal(142)
|
|
879
|
+
})
|
|
880
|
+
serverErr.close()
|
|
881
|
+
done()
|
|
882
|
+
})
|
|
883
|
+
})
|
|
884
|
+
it('pubrec handling errors check', function (done) {
|
|
885
|
+
this.timeout(15000)
|
|
886
|
+
serverErr.listen(port + 118)
|
|
887
|
+
var opts = {
|
|
888
|
+
host: 'localhost',
|
|
889
|
+
port: port + 118,
|
|
890
|
+
protocolVersion: 5
|
|
891
|
+
}
|
|
892
|
+
var client = mqtt.connect(opts)
|
|
893
|
+
client.once('connect', () => {
|
|
894
|
+
client.publish('a/b', 'message', {qos: 2}, function (err, packet) {
|
|
895
|
+
should(err.message).be.equal('Publish error: Session taken over')
|
|
896
|
+
should(err.code).be.equal(142)
|
|
897
|
+
})
|
|
898
|
+
serverErr.close()
|
|
899
|
+
done()
|
|
900
|
+
})
|
|
901
|
+
})
|
|
902
|
+
it('puback handling custom reason code', function (done) {
|
|
903
|
+
this.timeout(15000)
|
|
904
|
+
serverErr.listen(port + 117)
|
|
905
|
+
var opts = {
|
|
906
|
+
host: 'localhost',
|
|
907
|
+
port: port + 117,
|
|
908
|
+
protocolVersion: 5,
|
|
909
|
+
customHandleAcks: function (topic, message, packet, cb) {
|
|
910
|
+
var code = 0
|
|
911
|
+
if (topic === 'a/b') {
|
|
912
|
+
code = 128
|
|
913
|
+
}
|
|
914
|
+
cb(code)
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
serverErr.once('client', function (c) {
|
|
919
|
+
c.once('subscribe', function () {
|
|
920
|
+
c.publish({ topic: 'a/b', payload: 'payload', qos: 1, messageId: 1 })
|
|
921
|
+
})
|
|
922
|
+
|
|
923
|
+
c.on('puback', function (packet) {
|
|
924
|
+
should(packet.reasonCode).be.equal(128)
|
|
925
|
+
client.end()
|
|
926
|
+
c.destroy()
|
|
927
|
+
serverErr.close()
|
|
928
|
+
done()
|
|
929
|
+
})
|
|
930
|
+
})
|
|
931
|
+
|
|
932
|
+
var client = mqtt.connect(opts)
|
|
933
|
+
client.once('connect', function () {
|
|
934
|
+
client.subscribe('a/b', {qos: 1})
|
|
935
|
+
})
|
|
936
|
+
})
|
|
937
|
+
it('server side disconnect', function (done) {
|
|
938
|
+
this.timeout(15000)
|
|
939
|
+
var server327 = new Server(function (client) {
|
|
940
|
+
client.on('connect', function (packet) {
|
|
941
|
+
client.connack({
|
|
942
|
+
reasonCode: 0
|
|
943
|
+
})
|
|
944
|
+
client.disconnect({reasonCode: 128})
|
|
945
|
+
server327.close()
|
|
946
|
+
})
|
|
947
|
+
})
|
|
948
|
+
server327.listen(port + 327)
|
|
949
|
+
var opts = {
|
|
950
|
+
host: 'localhost',
|
|
951
|
+
port: port + 327,
|
|
952
|
+
protocolVersion: 5
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
var client = mqtt.connect(opts)
|
|
956
|
+
client.once('disconnect', function (disconnectPacket) {
|
|
957
|
+
should(disconnectPacket.reasonCode).be.equal(128)
|
|
958
|
+
done()
|
|
959
|
+
})
|
|
960
|
+
})
|
|
961
|
+
it('pubrec handling custom reason code', function (done) {
|
|
962
|
+
this.timeout(15000)
|
|
963
|
+
serverErr.listen(port + 117)
|
|
964
|
+
var opts = {
|
|
965
|
+
host: 'localhost',
|
|
966
|
+
port: port + 117,
|
|
967
|
+
protocolVersion: 5,
|
|
968
|
+
customHandleAcks: function (topic, message, packet, cb) {
|
|
969
|
+
var code = 0
|
|
970
|
+
if (topic === 'a/b') {
|
|
971
|
+
code = 128
|
|
972
|
+
}
|
|
973
|
+
cb(code)
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
serverErr.once('client', function (c) {
|
|
978
|
+
c.once('subscribe', function () {
|
|
979
|
+
c.publish({ topic: 'a/b', payload: 'payload', qos: 2, messageId: 1 })
|
|
980
|
+
})
|
|
981
|
+
|
|
982
|
+
c.on('pubrec', function (packet) {
|
|
983
|
+
should(packet.reasonCode).be.equal(128)
|
|
984
|
+
client.end()
|
|
985
|
+
c.destroy()
|
|
986
|
+
serverErr.close()
|
|
987
|
+
done()
|
|
988
|
+
})
|
|
989
|
+
})
|
|
990
|
+
|
|
991
|
+
var client = mqtt.connect(opts)
|
|
992
|
+
client.once('connect', function () {
|
|
993
|
+
client.subscribe('a/b', {qos: 1})
|
|
994
|
+
})
|
|
995
|
+
})
|
|
996
|
+
it('puback handling custom reason code with error', function (done) {
|
|
997
|
+
this.timeout(15000)
|
|
998
|
+
serverErr.listen(port + 117)
|
|
999
|
+
var opts = {
|
|
1000
|
+
host: 'localhost',
|
|
1001
|
+
port: port + 117,
|
|
1002
|
+
protocolVersion: 5,
|
|
1003
|
+
customHandleAcks: function (topic, message, packet, cb) {
|
|
1004
|
+
var code = 0
|
|
1005
|
+
if (topic === 'a/b') {
|
|
1006
|
+
cb(new Error('a/b is not valid'))
|
|
1007
|
+
}
|
|
1008
|
+
cb(code)
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
serverErr.once('client', function (c) {
|
|
1013
|
+
c.once('subscribe', function () {
|
|
1014
|
+
c.publish({ topic: 'a/b', payload: 'payload', qos: 1, messageId: 1 })
|
|
1015
|
+
})
|
|
1016
|
+
})
|
|
1017
|
+
|
|
1018
|
+
var client = mqtt.connect(opts)
|
|
1019
|
+
client.on('error', function (error) {
|
|
1020
|
+
should(error.message).be.equal('a/b is not valid')
|
|
1021
|
+
client.end()
|
|
1022
|
+
serverErr.close()
|
|
1023
|
+
done()
|
|
1024
|
+
})
|
|
1025
|
+
client.once('connect', function () {
|
|
1026
|
+
client.subscribe('a/b', {qos: 1})
|
|
1027
|
+
})
|
|
1028
|
+
})
|
|
1029
|
+
it('pubrec handling custom reason code with error', function (done) {
|
|
1030
|
+
this.timeout(15000)
|
|
1031
|
+
serverErr.listen(port + 117)
|
|
1032
|
+
var opts = {
|
|
1033
|
+
host: 'localhost',
|
|
1034
|
+
port: port + 117,
|
|
1035
|
+
protocolVersion: 5,
|
|
1036
|
+
customHandleAcks: function (topic, message, packet, cb) {
|
|
1037
|
+
var code = 0
|
|
1038
|
+
if (topic === 'a/b') {
|
|
1039
|
+
cb(new Error('a/b is not valid'))
|
|
1040
|
+
}
|
|
1041
|
+
cb(code)
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
serverErr.once('client', function (c) {
|
|
1046
|
+
c.once('subscribe', function () {
|
|
1047
|
+
c.publish({ topic: 'a/b', payload: 'payload', qos: 2, messageId: 1 })
|
|
1048
|
+
})
|
|
1049
|
+
})
|
|
1050
|
+
|
|
1051
|
+
var client = mqtt.connect(opts)
|
|
1052
|
+
client.on('error', function (error) {
|
|
1053
|
+
should(error.message).be.equal('a/b is not valid')
|
|
1054
|
+
client.end()
|
|
1055
|
+
serverErr.close()
|
|
1056
|
+
done()
|
|
1057
|
+
})
|
|
1058
|
+
client.once('connect', function () {
|
|
1059
|
+
client.subscribe('a/b', {qos: 1})
|
|
1060
|
+
})
|
|
1061
|
+
})
|
|
1062
|
+
it('puback handling custom invalid reason code', function (done) {
|
|
1063
|
+
this.timeout(15000)
|
|
1064
|
+
serverErr.listen(port + 117)
|
|
1065
|
+
var opts = {
|
|
1066
|
+
host: 'localhost',
|
|
1067
|
+
port: port + 117,
|
|
1068
|
+
protocolVersion: 5,
|
|
1069
|
+
customHandleAcks: function (topic, message, packet, cb) {
|
|
1070
|
+
var code = 0
|
|
1071
|
+
if (topic === 'a/b') {
|
|
1072
|
+
code = 124124
|
|
1073
|
+
}
|
|
1074
|
+
cb(code)
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
serverErr.once('client', function (c) {
|
|
1079
|
+
c.once('subscribe', function () {
|
|
1080
|
+
c.publish({ topic: 'a/b', payload: 'payload', qos: 1, messageId: 1 })
|
|
1081
|
+
})
|
|
1082
|
+
})
|
|
1083
|
+
|
|
1084
|
+
var client = mqtt.connect(opts)
|
|
1085
|
+
client.on('error', function (error) {
|
|
1086
|
+
should(error.message).be.equal('Wrong reason code for puback')
|
|
1087
|
+
client.end()
|
|
1088
|
+
serverErr.close()
|
|
1089
|
+
done()
|
|
1090
|
+
})
|
|
1091
|
+
client.once('connect', function () {
|
|
1092
|
+
client.subscribe('a/b', {qos: 1})
|
|
1093
|
+
})
|
|
1094
|
+
})
|
|
1095
|
+
it('pubrec handling custom invalid reason code', function (done) {
|
|
1096
|
+
this.timeout(15000)
|
|
1097
|
+
serverErr.listen(port + 117)
|
|
1098
|
+
var opts = {
|
|
1099
|
+
host: 'localhost',
|
|
1100
|
+
port: port + 117,
|
|
1101
|
+
protocolVersion: 5,
|
|
1102
|
+
customHandleAcks: function (topic, message, packet, cb) {
|
|
1103
|
+
var code = 0
|
|
1104
|
+
if (topic === 'a/b') {
|
|
1105
|
+
code = 34535
|
|
1106
|
+
}
|
|
1107
|
+
cb(code)
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
serverErr.once('client', function (c) {
|
|
1112
|
+
c.once('subscribe', function () {
|
|
1113
|
+
c.publish({ topic: 'a/b', payload: 'payload', qos: 2, messageId: 1 })
|
|
1114
|
+
})
|
|
1115
|
+
})
|
|
1116
|
+
|
|
1117
|
+
var client = mqtt.connect(opts)
|
|
1118
|
+
client.on('error', function (error) {
|
|
1119
|
+
should(error.message).be.equal('Wrong reason code for pubrec')
|
|
1120
|
+
client.end()
|
|
1121
|
+
serverErr.close()
|
|
1122
|
+
done()
|
|
1123
|
+
})
|
|
1124
|
+
client.once('connect', function () {
|
|
1125
|
+
client.subscribe('a/b', {qos: 1})
|
|
1126
|
+
})
|
|
1127
|
+
})
|
|
1128
|
+
})
|
|
1129
|
+
})
|