mqtt 2.18.5 → 2.18.9

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 (66) hide show
  1. package/CONTRIBUTING.md +27 -27
  2. package/LICENSE.md +15 -15
  3. package/README.md +612 -612
  4. package/bin/pub.js +146 -146
  5. package/bin/sub.js +123 -123
  6. package/dist/mqtt.js +14275 -10729
  7. package/dist/mqtt.min.js +1 -1
  8. package/doc/help.txt +8 -8
  9. package/doc/publish.txt +26 -26
  10. package/doc/subscribe.txt +26 -26
  11. package/examples/client/secure-client.js +24 -24
  12. package/examples/client/simple-both.js +13 -13
  13. package/examples/client/simple-publish.js +7 -7
  14. package/examples/client/simple-subscribe.js +9 -9
  15. package/examples/tls client/crt.ca.cg.pem +34 -34
  16. package/examples/tls client/mqttclient.js +48 -48
  17. package/examples/tls client/tls-cert.pem +13 -13
  18. package/examples/tls client/tls-key.pem +15 -15
  19. package/examples/wss/client.js +49 -49
  20. package/examples/wss/client_with_proxy.js +58 -58
  21. package/lib/client.js +1129 -1102
  22. package/lib/connect/index.js +149 -149
  23. package/lib/connect/tcp.js +19 -19
  24. package/lib/connect/tls.js +41 -41
  25. package/lib/connect/ws.js +92 -92
  26. package/lib/connect/wx.js +110 -110
  27. package/lib/store.js +140 -140
  28. package/lib/validations.js +52 -52
  29. package/mqtt.js +41 -41
  30. package/package.json +107 -111
  31. package/test/abstract_client.js +2667 -2533
  32. package/test/abstract_store.js +135 -133
  33. package/test/browser/server.js +132 -132
  34. package/test/browser/test.js +92 -92
  35. package/test/browser/wx.js +93 -93
  36. package/test/client.js +544 -544
  37. package/test/helpers/private-csr.pem +16 -16
  38. package/test/helpers/private-key.pem +27 -27
  39. package/test/helpers/public-cert.pem +19 -19
  40. package/test/helpers/public-key.pem +9 -9
  41. package/test/helpers/server.js +52 -52
  42. package/test/helpers/server_process.js +9 -9
  43. package/test/helpers/tls-cert.pem +14 -14
  44. package/test/helpers/tls-csr.pem +11 -11
  45. package/test/helpers/tls-key.pem +15 -15
  46. package/test/helpers/wrong-cert.pem +13 -13
  47. package/test/helpers/wrong-csr.pem +11 -11
  48. package/test/helpers/wrong-key.pem +15 -15
  49. package/test/mocha.opts +4 -4
  50. package/test/mqtt.js +230 -230
  51. package/test/mqtt_store.js +9 -9
  52. package/test/secure_client.js +157 -157
  53. package/test/server.js +65 -65
  54. package/test/store.js +10 -10
  55. package/test/typescript/broker-connect-subscribe-and-publish.ts +22 -22
  56. package/test/typescript/tsconfig.json +14 -14
  57. package/test/util.js +13 -13
  58. package/test/websocket_client.js +144 -144
  59. package/types/index.d.ts +27 -27
  60. package/types/lib/client-options.d.ts +137 -137
  61. package/types/lib/client.d.ts +196 -196
  62. package/types/lib/connect/index.d.ts +10 -10
  63. package/types/lib/store-options.d.ts +6 -6
  64. package/types/lib/store.d.ts +46 -46
  65. package/test/typescript/broker-connect-subscribe-and-publish.js +0 -24
  66. package/test/typescript/broker-connect-subscribe-and-publish.js.map +0 -1
package/test/client.js CHANGED
@@ -1,544 +1,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 port = 9876
16
- var server
17
-
18
- function connOnlyServer () {
19
- return new Server(function (client) {
20
- client.on('connect', function (packet) {
21
- client.connack({returnCode: 0})
22
- })
23
- })
24
- }
25
-
26
- /**
27
- * Test server
28
- */
29
- function buildServer () {
30
- return new Server(function (client) {
31
- client.on('connect', function (packet) {
32
- if (packet.clientId === 'invalid') {
33
- client.connack({returnCode: 2})
34
- } else {
35
- client.connack({returnCode: 0})
36
- }
37
- })
38
-
39
- client.on('publish', function (packet) {
40
- setImmediate(function () {
41
- switch (packet.qos) {
42
- case 0:
43
- break
44
- case 1:
45
- client.puback(packet)
46
- break
47
- case 2:
48
- client.pubrec(packet)
49
- break
50
- }
51
- })
52
- })
53
-
54
- client.on('pubrel', function (packet) {
55
- client.pubcomp(packet)
56
- })
57
-
58
- client.on('pubrec', function (packet) {
59
- client.pubrel(packet)
60
- })
61
-
62
- client.on('pubcomp', function () {
63
- // Nothing to be done
64
- })
65
-
66
- client.on('subscribe', function (packet) {
67
- client.suback({
68
- messageId: packet.messageId,
69
- granted: packet.subscriptions.map(function (e) {
70
- return e.qos
71
- })
72
- })
73
- })
74
-
75
- client.on('unsubscribe', function (packet) {
76
- client.unsuback(packet)
77
- })
78
-
79
- client.on('pingreq', function () {
80
- client.pingresp()
81
- })
82
- })
83
- }
84
-
85
- server = buildServer().listen(port)
86
-
87
- describe('MqttClient', function () {
88
- describe('creating', function () {
89
- it('should allow instantiation of MqttClient without the \'new\' operator', function (done) {
90
- should(function () {
91
- var client
92
- try {
93
- client = mqtt.MqttClient(function () {
94
- throw Error('break')
95
- }, {})
96
- client.end()
97
- } catch (err) {
98
- if (err.message !== 'break') {
99
- throw err
100
- }
101
- done()
102
- }
103
- }).not.throw('Object #<Object> has no method \'_setupStream\'')
104
- })
105
- })
106
-
107
- var config = { protocol: 'mqtt', port: port }
108
- abstractClientTests(server, config)
109
-
110
- describe('message ids', function () {
111
- it('should increment the message id', function () {
112
- var client = mqtt.connect(config)
113
- var currentId = client._nextId()
114
-
115
- client._nextId().should.equal(currentId + 1)
116
- client.end()
117
- })
118
-
119
- it('should return 1 once the internal counter reached limit', function () {
120
- var client = mqtt.connect(config)
121
- client.nextId = 65535
122
-
123
- client._nextId().should.equal(65535)
124
- client._nextId().should.equal(1)
125
- client.end()
126
- })
127
-
128
- it('should return 65535 for last message id once the internal counter reached limit', function () {
129
- var client = mqtt.connect(config)
130
- client.nextId = 65535
131
-
132
- client._nextId().should.equal(65535)
133
- client.getLastMessageId().should.equal(65535)
134
- client._nextId().should.equal(1)
135
- client.getLastMessageId().should.equal(1)
136
- client.end()
137
- })
138
-
139
- it('should not throw an error if packet\'s messageId is not found when receiving a pubrel packet', function (done) {
140
- var server2 = new Server(function (c) {
141
- c.on('connect', function (packet) {
142
- c.connack({returnCode: 0})
143
- c.pubrel({ messageId: Math.floor(Math.random() * 9000) + 1000 })
144
- })
145
- })
146
-
147
- server2.listen(port + 49, function () {
148
- var client = mqtt.connect({
149
- port: port + 49,
150
- host: 'localhost'
151
- })
152
-
153
- client.on('packetsend', function (packet) {
154
- if (packet.cmd === 'pubcomp') {
155
- client.end()
156
- server2.close()
157
- done()
158
- }
159
- })
160
- })
161
- })
162
-
163
- it('should not go overflow if the TCP frame contains a lot of PUBLISH packets', function (done) {
164
- var parser = mqttPacket.parser()
165
- var count = 0
166
- var max = 1000
167
- var duplex = new Duplex({
168
- read: function (n) {},
169
- write: function (chunk, enc, cb) {
170
- parser.parse(chunk)
171
- cb() // nothing to do
172
- }
173
- })
174
- var client = new mqtt.MqttClient(function () {
175
- return duplex
176
- }, {})
177
-
178
- client.on('message', function (t, p, packet) {
179
- if (++count === max) {
180
- done()
181
- }
182
- })
183
-
184
- parser.on('packet', function (packet) {
185
- var packets = []
186
-
187
- if (packet.cmd === 'connect') {
188
- duplex.push(mqttPacket.generate({
189
- cmd: 'connack',
190
- sessionPresent: false,
191
- returnCode: 0
192
- }))
193
-
194
- for (var i = 0; i < max; i++) {
195
- packets.push(mqttPacket.generate({
196
- cmd: 'publish',
197
- topic: Buffer.from('hello'),
198
- payload: Buffer.from('world'),
199
- retain: false,
200
- dup: false,
201
- messageId: i + 1,
202
- qos: 1
203
- }))
204
- }
205
-
206
- duplex.push(Buffer.concat(packets))
207
- }
208
- })
209
- })
210
- })
211
-
212
- describe('flushing', function () {
213
- it('should attempt to complete pending unsub and send on ping timeout', function (done) {
214
- this.timeout(10000)
215
- var server3 = connOnlyServer().listen(port + 72)
216
- var pubCallbackCalled = false
217
- var unsubscribeCallbackCalled = false
218
- var client = mqtt.connect({
219
- port: port + 72,
220
- host: 'localhost',
221
- keepalive: 1,
222
- connectTimeout: 350,
223
- reconnectPeriod: 0
224
- })
225
- client.once('connect', () => {
226
- client.publish('fakeTopic', 'fakeMessage', {qos: 1}, (err, result) => {
227
- should.exist(err)
228
- pubCallbackCalled = true
229
- })
230
- client.unsubscribe('fakeTopic', (err, result) => {
231
- should.exist(err)
232
- unsubscribeCallbackCalled = true
233
- })
234
- setTimeout(() => {
235
- client.end(() => {
236
- should.equal(pubCallbackCalled && unsubscribeCallbackCalled, true, 'callbacks not invoked')
237
- server3.close()
238
- done()
239
- })
240
- }, 5000)
241
- })
242
- })
243
- })
244
-
245
- describe('reconnecting', function () {
246
- it('should attempt to reconnect once server is down', function (done) {
247
- this.timeout(15000)
248
-
249
- var innerServer = fork(path.join(__dirname, 'helpers', 'server_process.js'))
250
- var client = mqtt.connect({ port: 3000, host: 'localhost', keepalive: 1 })
251
-
252
- client.once('connect', function () {
253
- innerServer.kill('SIGINT') // mocks server shutdown
254
-
255
- client.once('close', function () {
256
- should.exist(client.reconnectTimer)
257
- client.end()
258
- done()
259
- })
260
- })
261
- })
262
-
263
- it('should reconnect to multiple host-ports-protocol combinations if servers is passed', function (done) {
264
- this.timeout(15000)
265
-
266
- var server2 = buildServer().listen(port + 42)
267
-
268
- server2.on('listening', function () {
269
- var client = mqtt.connect({
270
- protocol: 'wss',
271
- servers: [
272
- { port: port + 42, host: 'localhost', protocol: 'ws' },
273
- { port: port, host: 'localhost' }
274
- ],
275
- keepalive: 50
276
- })
277
- server2.on('client', function (c) {
278
- should.equal(client.stream.socket.url, 'ws://localhost:9918/', 'Protocol for first connection should use ws.')
279
- c.stream.destroy()
280
- server2.close()
281
- })
282
-
283
- server.once('client', function () {
284
- should.equal(client.stream.socket.url, 'wss://localhost:9876/', 'Protocol for second client should use the default protocol: wss, on port: port + 42.')
285
- client.end()
286
- done()
287
- })
288
-
289
- client.once('connect', function () {
290
- client.stream.destroy()
291
- })
292
- })
293
- })
294
-
295
- it('should reconnect if a connack is not received in an interval', function (done) {
296
- this.timeout(2000)
297
-
298
- var server2 = net.createServer().listen(port + 43)
299
-
300
- server2.on('connection', function (c) {
301
- eos(c, function () {
302
- server2.close()
303
- })
304
- })
305
-
306
- server2.on('listening', function () {
307
- var client = mqtt.connect({
308
- servers: [
309
- { port: port + 43, host: 'localhost_fake' },
310
- { port: port, host: 'localhost' }
311
- ],
312
- connectTimeout: 500
313
- })
314
-
315
- server.once('client', function () {
316
- client.end()
317
- done()
318
- })
319
-
320
- client.once('connect', function () {
321
- client.stream.destroy()
322
- })
323
- })
324
- })
325
-
326
- it('should not be cleared by the connack timer', function (done) {
327
- this.timeout(4000)
328
-
329
- var server2 = net.createServer().listen(port + 44)
330
-
331
- server2.on('connection', function (c) {
332
- c.destroy()
333
- })
334
-
335
- server2.once('listening', function () {
336
- var reconnects = 0
337
- var connectTimeout = 1000
338
- var reconnectPeriod = 100
339
- var expectedReconnects = Math.floor(connectTimeout / reconnectPeriod)
340
- var client = mqtt.connect({
341
- port: port + 44,
342
- host: 'localhost',
343
- connectTimeout: connectTimeout,
344
- reconnectPeriod: reconnectPeriod
345
- })
346
-
347
- client.on('reconnect', function () {
348
- reconnects++
349
- if (reconnects >= expectedReconnects) {
350
- client.end()
351
- done()
352
- }
353
- })
354
- })
355
- })
356
-
357
- it('should not keep requeueing the first message when offline', function (done) {
358
- this.timeout(2500)
359
-
360
- var server2 = buildServer().listen(port + 45)
361
- var client = mqtt.connect({
362
- port: port + 45,
363
- host: 'localhost',
364
- connectTimeout: 350,
365
- reconnectPeriod: 300
366
- })
367
-
368
- server2.on('client', function (c) {
369
- client.publish('hello', 'world', { qos: 1 }, function () {
370
- c.destroy()
371
- server2.close()
372
- client.publish('hello', 'world', { qos: 1 })
373
- })
374
- })
375
-
376
- setTimeout(function () {
377
- if (client.queue.length === 0) {
378
- client.end(true)
379
- done()
380
- } else {
381
- client.end(true)
382
- }
383
- }, 2000)
384
- })
385
-
386
- it('should not send the same subscribe multiple times on a flaky connection', function (done) {
387
- this.timeout(3500)
388
-
389
- var KILL_COUNT = 4
390
- var killedConnections = 0
391
- var subIds = {}
392
- var client = mqtt.connect({
393
- port: port + 46,
394
- host: 'localhost',
395
- connectTimeout: 350,
396
- reconnectPeriod: 300
397
- })
398
-
399
- var server2 = new Server(function (client) {
400
- client.on('error', function () {})
401
- client.on('connect', function (packet) {
402
- if (packet.clientId === 'invalid') {
403
- client.connack({returnCode: 2})
404
- } else {
405
- client.connack({returnCode: 0})
406
- }
407
- })
408
- }).listen(port + 46)
409
-
410
- server2.on('client', function (c) {
411
- client.subscribe('topic', function () {
412
- done()
413
- client.end()
414
- c.destroy()
415
- server2.close()
416
- })
417
-
418
- c.on('subscribe', function (packet) {
419
- if (killedConnections < KILL_COUNT) {
420
- // Kill the first few sub attempts to simulate a flaky connection
421
- killedConnections++
422
- c.destroy()
423
- } else {
424
- // Keep track of acks
425
- if (!subIds[packet.messageId]) {
426
- subIds[packet.messageId] = 0
427
- }
428
- subIds[packet.messageId]++
429
- if (subIds[packet.messageId] > 1) {
430
- done(new Error('Multiple duplicate acked subscriptions received for messageId ' + packet.messageId))
431
- client.end(true)
432
- c.destroy()
433
- server2.destroy()
434
- }
435
-
436
- c.suback({
437
- messageId: packet.messageId,
438
- granted: packet.subscriptions.map(function (e) {
439
- return e.qos
440
- })
441
- })
442
- }
443
- })
444
- })
445
- })
446
-
447
- it('should not fill the queue of subscribes if it cannot connect', function (done) {
448
- this.timeout(2500)
449
-
450
- var port2 = port + 48
451
-
452
- var server2 = net.createServer(function (stream) {
453
- var client = new Connection(stream)
454
-
455
- client.on('error', function () {})
456
- client.on('connect', function (packet) {
457
- client.connack({returnCode: 0})
458
- client.destroy()
459
- })
460
- })
461
-
462
- server2.listen(port2, function () {
463
- var client = mqtt.connect({
464
- port: port2,
465
- host: 'localhost',
466
- connectTimeout: 350,
467
- reconnectPeriod: 300
468
- })
469
-
470
- client.subscribe('hello')
471
-
472
- setTimeout(function () {
473
- client.queue.length.should.equal(1)
474
- client.end()
475
- done()
476
- }, 1000)
477
- })
478
- })
479
-
480
- it('should not send the same publish multiple times on a flaky connection', function (done) {
481
- this.timeout(3500)
482
-
483
- var KILL_COUNT = 4
484
- var killedConnections = 0
485
- var pubIds = {}
486
- var client = mqtt.connect({
487
- port: port + 47,
488
- host: 'localhost',
489
- connectTimeout: 350,
490
- reconnectPeriod: 300
491
- })
492
-
493
- var server2 = net.createServer(function (stream) {
494
- var client = new Connection(stream)
495
- client.on('error', function () {})
496
- client.on('connect', function (packet) {
497
- if (packet.clientId === 'invalid') {
498
- client.connack({returnCode: 2})
499
- } else {
500
- client.connack({returnCode: 0})
501
- }
502
- })
503
-
504
- this.emit('client', client)
505
- }).listen(port + 47)
506
-
507
- server2.on('client', function (c) {
508
- client.publish('topic', 'data', { qos: 1 }, function () {
509
- done()
510
- client.end()
511
- c.destroy()
512
- server2.destroy()
513
- })
514
-
515
- c.on('publish', function onPublish (packet) {
516
- if (killedConnections < KILL_COUNT) {
517
- // Kill the first few pub attempts to simulate a flaky connection
518
- killedConnections++
519
- c.destroy()
520
-
521
- // to avoid receiving inflight messages
522
- c.removeListener('publish', onPublish)
523
- } else {
524
- // Keep track of acks
525
- if (!pubIds[packet.messageId]) {
526
- pubIds[packet.messageId] = 0
527
- }
528
-
529
- pubIds[packet.messageId]++
530
-
531
- if (pubIds[packet.messageId] > 1) {
532
- done(new Error('Multiple duplicate acked publishes received for messageId ' + packet.messageId))
533
- client.end(true)
534
- c.destroy()
535
- server2.destroy()
536
- }
537
-
538
- c.puback(packet)
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 port = 9876
16
+ var server
17
+
18
+ function connOnlyServer () {
19
+ return new Server(function (client) {
20
+ client.on('connect', function (packet) {
21
+ client.connack({returnCode: 0})
22
+ })
23
+ })
24
+ }
25
+
26
+ /**
27
+ * Test server
28
+ */
29
+ function buildServer () {
30
+ return new Server(function (client) {
31
+ client.on('connect', function (packet) {
32
+ if (packet.clientId === 'invalid') {
33
+ client.connack({returnCode: 2})
34
+ } else {
35
+ client.connack({returnCode: 0})
36
+ }
37
+ })
38
+
39
+ client.on('publish', function (packet) {
40
+ setImmediate(function () {
41
+ switch (packet.qos) {
42
+ case 0:
43
+ break
44
+ case 1:
45
+ client.puback(packet)
46
+ break
47
+ case 2:
48
+ client.pubrec(packet)
49
+ break
50
+ }
51
+ })
52
+ })
53
+
54
+ client.on('pubrel', function (packet) {
55
+ client.pubcomp(packet)
56
+ })
57
+
58
+ client.on('pubrec', function (packet) {
59
+ client.pubrel(packet)
60
+ })
61
+
62
+ client.on('pubcomp', function () {
63
+ // Nothing to be done
64
+ })
65
+
66
+ client.on('subscribe', function (packet) {
67
+ client.suback({
68
+ messageId: packet.messageId,
69
+ granted: packet.subscriptions.map(function (e) {
70
+ return e.qos
71
+ })
72
+ })
73
+ })
74
+
75
+ client.on('unsubscribe', function (packet) {
76
+ client.unsuback(packet)
77
+ })
78
+
79
+ client.on('pingreq', function () {
80
+ client.pingresp()
81
+ })
82
+ })
83
+ }
84
+
85
+ server = buildServer().listen(port)
86
+
87
+ describe('MqttClient', function () {
88
+ describe('creating', function () {
89
+ it('should allow instantiation of MqttClient without the \'new\' operator', function (done) {
90
+ should(function () {
91
+ var client
92
+ try {
93
+ client = mqtt.MqttClient(function () {
94
+ throw Error('break')
95
+ }, {})
96
+ client.end()
97
+ } catch (err) {
98
+ if (err.message !== 'break') {
99
+ throw err
100
+ }
101
+ done()
102
+ }
103
+ }).not.throw('Object #<Object> has no method \'_setupStream\'')
104
+ })
105
+ })
106
+
107
+ var config = { protocol: 'mqtt', port: port }
108
+ abstractClientTests(server, config)
109
+
110
+ describe('message ids', function () {
111
+ it('should increment the message id', function () {
112
+ var client = mqtt.connect(config)
113
+ var currentId = client._nextId()
114
+
115
+ client._nextId().should.equal(currentId + 1)
116
+ client.end()
117
+ })
118
+
119
+ it('should return 1 once the internal counter reached limit', function () {
120
+ var client = mqtt.connect(config)
121
+ client.nextId = 65535
122
+
123
+ client._nextId().should.equal(65535)
124
+ client._nextId().should.equal(1)
125
+ client.end()
126
+ })
127
+
128
+ it('should return 65535 for last message id once the internal counter reached limit', function () {
129
+ var client = mqtt.connect(config)
130
+ client.nextId = 65535
131
+
132
+ client._nextId().should.equal(65535)
133
+ client.getLastMessageId().should.equal(65535)
134
+ client._nextId().should.equal(1)
135
+ client.getLastMessageId().should.equal(1)
136
+ client.end()
137
+ })
138
+
139
+ it('should not throw an error if packet\'s messageId is not found when receiving a pubrel packet', function (done) {
140
+ var server2 = new Server(function (c) {
141
+ c.on('connect', function (packet) {
142
+ c.connack({returnCode: 0})
143
+ c.pubrel({ messageId: Math.floor(Math.random() * 9000) + 1000 })
144
+ })
145
+ })
146
+
147
+ server2.listen(port + 49, function () {
148
+ var client = mqtt.connect({
149
+ port: port + 49,
150
+ host: 'localhost'
151
+ })
152
+
153
+ client.on('packetsend', function (packet) {
154
+ if (packet.cmd === 'pubcomp') {
155
+ client.end()
156
+ server2.close()
157
+ done()
158
+ }
159
+ })
160
+ })
161
+ })
162
+
163
+ it('should not go overflow if the TCP frame contains a lot of PUBLISH packets', function (done) {
164
+ var parser = mqttPacket.parser()
165
+ var count = 0
166
+ var max = 1000
167
+ var duplex = new Duplex({
168
+ read: function (n) {},
169
+ write: function (chunk, enc, cb) {
170
+ parser.parse(chunk)
171
+ cb() // nothing to do
172
+ }
173
+ })
174
+ var client = new mqtt.MqttClient(function () {
175
+ return duplex
176
+ }, {})
177
+
178
+ client.on('message', function (t, p, packet) {
179
+ if (++count === max) {
180
+ done()
181
+ }
182
+ })
183
+
184
+ parser.on('packet', function (packet) {
185
+ var packets = []
186
+
187
+ if (packet.cmd === 'connect') {
188
+ duplex.push(mqttPacket.generate({
189
+ cmd: 'connack',
190
+ sessionPresent: false,
191
+ returnCode: 0
192
+ }))
193
+
194
+ for (var i = 0; i < max; i++) {
195
+ packets.push(mqttPacket.generate({
196
+ cmd: 'publish',
197
+ topic: Buffer.from('hello'),
198
+ payload: Buffer.from('world'),
199
+ retain: false,
200
+ dup: false,
201
+ messageId: i + 1,
202
+ qos: 1
203
+ }))
204
+ }
205
+
206
+ duplex.push(Buffer.concat(packets))
207
+ }
208
+ })
209
+ })
210
+ })
211
+
212
+ describe('flushing', function () {
213
+ it('should attempt to complete pending unsub and send on ping timeout', function (done) {
214
+ this.timeout(10000)
215
+ var server3 = connOnlyServer().listen(port + 72)
216
+ var pubCallbackCalled = false
217
+ var unsubscribeCallbackCalled = false
218
+ var client = mqtt.connect({
219
+ port: port + 72,
220
+ host: 'localhost',
221
+ keepalive: 1,
222
+ connectTimeout: 350,
223
+ reconnectPeriod: 0
224
+ })
225
+ client.once('connect', () => {
226
+ client.publish('fakeTopic', 'fakeMessage', {qos: 1}, (err, result) => {
227
+ should.exist(err)
228
+ pubCallbackCalled = true
229
+ })
230
+ client.unsubscribe('fakeTopic', (err, result) => {
231
+ should.exist(err)
232
+ unsubscribeCallbackCalled = true
233
+ })
234
+ setTimeout(() => {
235
+ client.end(() => {
236
+ should.equal(pubCallbackCalled && unsubscribeCallbackCalled, true, 'callbacks not invoked')
237
+ server3.close()
238
+ done()
239
+ })
240
+ }, 5000)
241
+ })
242
+ })
243
+ })
244
+
245
+ describe('reconnecting', function () {
246
+ it('should attempt to reconnect once server is down', function (done) {
247
+ this.timeout(15000)
248
+
249
+ var innerServer = fork(path.join(__dirname, 'helpers', 'server_process.js'))
250
+ var client = mqtt.connect({ port: 3000, host: 'localhost', keepalive: 1 })
251
+
252
+ client.once('connect', function () {
253
+ innerServer.kill('SIGINT') // mocks server shutdown
254
+
255
+ client.once('close', function () {
256
+ should.exist(client.reconnectTimer)
257
+ client.end()
258
+ done()
259
+ })
260
+ })
261
+ })
262
+
263
+ it('should reconnect to multiple host-ports-protocol combinations if servers is passed', function (done) {
264
+ this.timeout(15000)
265
+
266
+ var server2 = buildServer().listen(port + 42)
267
+
268
+ server2.on('listening', function () {
269
+ var client = mqtt.connect({
270
+ protocol: 'wss',
271
+ servers: [
272
+ { port: port + 42, host: 'localhost', protocol: 'ws' },
273
+ { port: port, host: 'localhost' }
274
+ ],
275
+ keepalive: 50
276
+ })
277
+ server2.on('client', function (c) {
278
+ should.equal(client.stream.socket.url, 'ws://localhost:9918/', 'Protocol for first connection should use ws.')
279
+ c.stream.destroy()
280
+ server2.close()
281
+ })
282
+
283
+ server.once('client', function () {
284
+ should.equal(client.stream.socket.url, 'wss://localhost:9876/', 'Protocol for second client should use the default protocol: wss, on port: port + 42.')
285
+ client.end()
286
+ done()
287
+ })
288
+
289
+ client.once('connect', function () {
290
+ client.stream.destroy()
291
+ })
292
+ })
293
+ })
294
+
295
+ it('should reconnect if a connack is not received in an interval', function (done) {
296
+ this.timeout(2000)
297
+
298
+ var server2 = net.createServer().listen(port + 43)
299
+
300
+ server2.on('connection', function (c) {
301
+ eos(c, function () {
302
+ server2.close()
303
+ })
304
+ })
305
+
306
+ server2.on('listening', function () {
307
+ var client = mqtt.connect({
308
+ servers: [
309
+ { port: port + 43, host: 'localhost_fake' },
310
+ { port: port, host: 'localhost' }
311
+ ],
312
+ connectTimeout: 500
313
+ })
314
+
315
+ server.once('client', function () {
316
+ client.end()
317
+ done()
318
+ })
319
+
320
+ client.once('connect', function () {
321
+ client.stream.destroy()
322
+ })
323
+ })
324
+ })
325
+
326
+ it('should not be cleared by the connack timer', function (done) {
327
+ this.timeout(4000)
328
+
329
+ var server2 = net.createServer().listen(port + 44)
330
+
331
+ server2.on('connection', function (c) {
332
+ c.destroy()
333
+ })
334
+
335
+ server2.once('listening', function () {
336
+ var reconnects = 0
337
+ var connectTimeout = 1000
338
+ var reconnectPeriod = 100
339
+ var expectedReconnects = Math.floor(connectTimeout / reconnectPeriod)
340
+ var client = mqtt.connect({
341
+ port: port + 44,
342
+ host: 'localhost',
343
+ connectTimeout: connectTimeout,
344
+ reconnectPeriod: reconnectPeriod
345
+ })
346
+
347
+ client.on('reconnect', function () {
348
+ reconnects++
349
+ if (reconnects >= expectedReconnects) {
350
+ client.end()
351
+ done()
352
+ }
353
+ })
354
+ })
355
+ })
356
+
357
+ it('should not keep requeueing the first message when offline', function (done) {
358
+ this.timeout(2500)
359
+
360
+ var server2 = buildServer().listen(port + 45)
361
+ var client = mqtt.connect({
362
+ port: port + 45,
363
+ host: 'localhost',
364
+ connectTimeout: 350,
365
+ reconnectPeriod: 300
366
+ })
367
+
368
+ server2.on('client', function (c) {
369
+ client.publish('hello', 'world', { qos: 1 }, function () {
370
+ c.destroy()
371
+ server2.close()
372
+ client.publish('hello', 'world', { qos: 1 })
373
+ })
374
+ })
375
+
376
+ setTimeout(function () {
377
+ if (client.queue.length === 0) {
378
+ client.end(true)
379
+ done()
380
+ } else {
381
+ client.end(true)
382
+ }
383
+ }, 2000)
384
+ })
385
+
386
+ it('should not send the same subscribe multiple times on a flaky connection', function (done) {
387
+ this.timeout(3500)
388
+
389
+ var KILL_COUNT = 4
390
+ var killedConnections = 0
391
+ var subIds = {}
392
+ var client = mqtt.connect({
393
+ port: port + 46,
394
+ host: 'localhost',
395
+ connectTimeout: 350,
396
+ reconnectPeriod: 300
397
+ })
398
+
399
+ var server2 = new Server(function (client) {
400
+ client.on('error', function () {})
401
+ client.on('connect', function (packet) {
402
+ if (packet.clientId === 'invalid') {
403
+ client.connack({returnCode: 2})
404
+ } else {
405
+ client.connack({returnCode: 0})
406
+ }
407
+ })
408
+ }).listen(port + 46)
409
+
410
+ server2.on('client', function (c) {
411
+ client.subscribe('topic', function () {
412
+ done()
413
+ client.end()
414
+ c.destroy()
415
+ server2.close()
416
+ })
417
+
418
+ c.on('subscribe', function (packet) {
419
+ if (killedConnections < KILL_COUNT) {
420
+ // Kill the first few sub attempts to simulate a flaky connection
421
+ killedConnections++
422
+ c.destroy()
423
+ } else {
424
+ // Keep track of acks
425
+ if (!subIds[packet.messageId]) {
426
+ subIds[packet.messageId] = 0
427
+ }
428
+ subIds[packet.messageId]++
429
+ if (subIds[packet.messageId] > 1) {
430
+ done(new Error('Multiple duplicate acked subscriptions received for messageId ' + packet.messageId))
431
+ client.end(true)
432
+ c.destroy()
433
+ server2.destroy()
434
+ }
435
+
436
+ c.suback({
437
+ messageId: packet.messageId,
438
+ granted: packet.subscriptions.map(function (e) {
439
+ return e.qos
440
+ })
441
+ })
442
+ }
443
+ })
444
+ })
445
+ })
446
+
447
+ it('should not fill the queue of subscribes if it cannot connect', function (done) {
448
+ this.timeout(2500)
449
+
450
+ var port2 = port + 48
451
+
452
+ var server2 = net.createServer(function (stream) {
453
+ var client = new Connection(stream)
454
+
455
+ client.on('error', function () {})
456
+ client.on('connect', function (packet) {
457
+ client.connack({returnCode: 0})
458
+ client.destroy()
459
+ })
460
+ })
461
+
462
+ server2.listen(port2, function () {
463
+ var client = mqtt.connect({
464
+ port: port2,
465
+ host: 'localhost',
466
+ connectTimeout: 350,
467
+ reconnectPeriod: 300
468
+ })
469
+
470
+ client.subscribe('hello')
471
+
472
+ setTimeout(function () {
473
+ client.queue.length.should.equal(1)
474
+ client.end()
475
+ done()
476
+ }, 1000)
477
+ })
478
+ })
479
+
480
+ it('should not send the same publish multiple times on a flaky connection', function (done) {
481
+ this.timeout(3500)
482
+
483
+ var KILL_COUNT = 4
484
+ var killedConnections = 0
485
+ var pubIds = {}
486
+ var client = mqtt.connect({
487
+ port: port + 47,
488
+ host: 'localhost',
489
+ connectTimeout: 350,
490
+ reconnectPeriod: 300
491
+ })
492
+
493
+ var server2 = net.createServer(function (stream) {
494
+ var client = new Connection(stream)
495
+ client.on('error', function () {})
496
+ client.on('connect', function (packet) {
497
+ if (packet.clientId === 'invalid') {
498
+ client.connack({returnCode: 2})
499
+ } else {
500
+ client.connack({returnCode: 0})
501
+ }
502
+ })
503
+
504
+ this.emit('client', client)
505
+ }).listen(port + 47)
506
+
507
+ server2.on('client', function (c) {
508
+ client.publish('topic', 'data', { qos: 1 }, function () {
509
+ done()
510
+ client.end()
511
+ c.destroy()
512
+ server2.destroy()
513
+ })
514
+
515
+ c.on('publish', function onPublish (packet) {
516
+ if (killedConnections < KILL_COUNT) {
517
+ // Kill the first few pub attempts to simulate a flaky connection
518
+ killedConnections++
519
+ c.destroy()
520
+
521
+ // to avoid receiving inflight messages
522
+ c.removeListener('publish', onPublish)
523
+ } else {
524
+ // Keep track of acks
525
+ if (!pubIds[packet.messageId]) {
526
+ pubIds[packet.messageId] = 0
527
+ }
528
+
529
+ pubIds[packet.messageId]++
530
+
531
+ if (pubIds[packet.messageId] > 1) {
532
+ done(new Error('Multiple duplicate acked publishes received for messageId ' + packet.messageId))
533
+ client.end(true)
534
+ c.destroy()
535
+ server2.destroy()
536
+ }
537
+
538
+ c.puback(packet)
539
+ }
540
+ })
541
+ })
542
+ })
543
+ })
544
+ })