mqtt 2.18.4 → 2.18.8

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/lib/client.js CHANGED
@@ -136,38 +136,56 @@ function MqttClient (streamBuilder, options) {
136
136
  }
137
137
 
138
138
  this.connected = true
139
- var outStore = null
140
- outStore = this.outgoingStore.createStream()
139
+ var outStore = this.outgoingStore.createStream()
141
140
 
142
- // Control of stored messages
143
- outStore.once('readable', function () {
144
- function storeDeliver () {
145
- var packet = outStore.read(1)
146
- var cb
141
+ this.once('close', remove)
142
+ outStore.on('end', function () {
143
+ that.removeListener('close', remove)
144
+ })
145
+ outStore.on('error', function (err) {
146
+ that.removeListener('close', remove)
147
+ that.emit('error', err)
148
+ })
147
149
 
148
- if (!packet) {
149
- return
150
- }
150
+ function remove () {
151
+ outStore.destroy()
152
+ outStore = null
153
+ }
154
+
155
+ function storeDeliver () {
156
+ // edge case, we wrapped this twice
157
+ if (!outStore) {
158
+ return
159
+ }
160
+
161
+ var packet = outStore.read(1)
162
+ var cb
163
+
164
+ if (!packet) {
165
+ // read when data is available in the future
166
+ outStore.once('readable', storeDeliver)
167
+ return
168
+ }
151
169
 
152
- // Avoid unnecessary stream read operations when disconnected
153
- if (!that.disconnecting && !that.reconnectTimer) {
154
- outStore.read(0)
155
- cb = that.outgoing[packet.messageId]
156
- that.outgoing[packet.messageId] = function (err, status) {
157
- // Ensure that the original callback passed in to publish gets invoked
158
- if (cb) {
159
- cb(err, status)
160
- }
161
-
162
- storeDeliver()
170
+ // Avoid unnecessary stream read operations when disconnected
171
+ if (!that.disconnecting && !that.reconnectTimer) {
172
+ cb = that.outgoing[packet.messageId]
173
+ that.outgoing[packet.messageId] = function (err, status) {
174
+ // Ensure that the original callback passed in to publish gets invoked
175
+ if (cb) {
176
+ cb(err, status)
163
177
  }
164
- that._sendPacket(packet)
165
- } else if (outStore.destroy) {
166
- outStore.destroy()
178
+
179
+ storeDeliver()
167
180
  }
181
+ that._sendPacket(packet)
182
+ } else if (outStore.destroy) {
183
+ outStore.destroy()
168
184
  }
169
- storeDeliver()
170
- }).on('error', this.emit.bind(this, 'error'))
185
+ }
186
+
187
+ // start flowing
188
+ storeDeliver()
171
189
  })
172
190
 
173
191
  // Mark disconnected on stream close
@@ -936,6 +954,7 @@ default:
936
954
  for now i just suppressed the warnings
937
955
  */
938
956
  MqttClient.prototype._handlePublish = function (packet, done) {
957
+ done = typeof done !== 'undefined' ? done : nop
939
958
  var topic = packet.topic.toString()
940
959
  var message = packet.payload
941
960
  var qos = packet.qos
@@ -944,7 +963,10 @@ MqttClient.prototype._handlePublish = function (packet, done) {
944
963
 
945
964
  switch (qos) {
946
965
  case 2:
947
- this.incomingStore.put(packet, function () {
966
+ this.incomingStore.put(packet, function (err) {
967
+ if (err) {
968
+ return done(err)
969
+ }
948
970
  that._sendPacket({cmd: 'pubrec', messageId: mid}, done)
949
971
  })
950
972
  break
@@ -953,7 +975,7 @@ MqttClient.prototype._handlePublish = function (packet, done) {
953
975
  this.emit('message', topic, message, packet)
954
976
  this.handleMessage(packet, function (err) {
955
977
  if (err) {
956
- return done && done(err)
978
+ return done(err)
957
979
  }
958
980
  // send 'puback' if the above 'handleMessage' method executed
959
981
  // successfully.
@@ -1056,6 +1078,7 @@ MqttClient.prototype._handleAck = function (packet) {
1056
1078
  * @api private
1057
1079
  */
1058
1080
  MqttClient.prototype._handlePubrel = function (packet, callback) {
1081
+ callback = typeof callback !== 'undefined' ? callback : nop
1059
1082
  var mid = packet.messageId
1060
1083
  var that = this
1061
1084
 
@@ -1064,12 +1087,16 @@ MqttClient.prototype._handlePubrel = function (packet, callback) {
1064
1087
  that.incomingStore.get(packet, function (err, pub) {
1065
1088
  if (!err && pub.cmd !== 'pubrel') {
1066
1089
  that.emit('message', pub.topic, pub.payload, pub)
1067
- that.incomingStore.put(packet)
1068
- that.handleMessage(pub, function (err) {
1090
+ that.incomingStore.put(packet, function (err) {
1069
1091
  if (err) {
1070
- return callback && callback(err)
1092
+ return callback(err)
1071
1093
  }
1072
- that._sendPacket(comp, callback)
1094
+ that.handleMessage(pub, function (err) {
1095
+ if (err) {
1096
+ return callback(err)
1097
+ }
1098
+ that._sendPacket(comp, callback)
1099
+ })
1073
1100
  })
1074
1101
  } else {
1075
1102
  that._sendPacket(comp, callback)
package/lib/store.js CHANGED
@@ -11,6 +11,18 @@ var defaultStoreOptions = {
11
11
  clean: true
12
12
  }
13
13
 
14
+ /**
15
+ * es6-map can preserve insertion order even if ES version is older.
16
+ *
17
+ * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#Description
18
+ * It should be noted that a Map which is a map of an object, especially
19
+ * a dictionary of dictionaries, will only map to the object's insertion
20
+ * order. In ES2015 this is ordered for objects but for older versions of
21
+ * ES, this may be random and not ordered.
22
+ *
23
+ */
24
+ var Map = require('es6-map')
25
+
14
26
  /**
15
27
  * In-memory implementation of the message store
16
28
  * This can actually be saved into files.
@@ -27,7 +39,7 @@ function Store (options) {
27
39
  // Defaults
28
40
  this.options = xtend(defaultStoreOptions, options)
29
41
 
30
- this._inflights = {}
42
+ this._inflights = new Map()
31
43
  }
32
44
 
33
45
  /**
@@ -36,7 +48,7 @@ function Store (options) {
36
48
  *
37
49
  */
38
50
  Store.prototype.put = function (packet, cb) {
39
- this._inflights[packet.messageId] = packet
51
+ this._inflights.set(packet.messageId, packet)
40
52
 
41
53
  if (cb) {
42
54
  cb()
@@ -51,14 +63,17 @@ Store.prototype.put = function (packet, cb) {
51
63
  */
52
64
  Store.prototype.createStream = function () {
53
65
  var stream = new Readable(streamsOpts)
54
- var inflights = this._inflights
55
- var ids = Object.keys(this._inflights)
56
66
  var destroyed = false
67
+ var values = []
57
68
  var i = 0
58
69
 
70
+ this._inflights.forEach(function (value, key) {
71
+ values.push(value)
72
+ })
73
+
59
74
  stream._read = function () {
60
- if (!destroyed && i < ids.length) {
61
- this.push(inflights[ids[i++]])
75
+ if (!destroyed && i < values.length) {
76
+ this.push(values[i++])
62
77
  } else {
63
78
  this.push(null)
64
79
  }
@@ -85,9 +100,9 @@ Store.prototype.createStream = function () {
85
100
  * deletes a packet from the store.
86
101
  */
87
102
  Store.prototype.del = function (packet, cb) {
88
- packet = this._inflights[packet.messageId]
103
+ packet = this._inflights.get(packet.messageId)
89
104
  if (packet) {
90
- delete this._inflights[packet.messageId]
105
+ this._inflights.delete(packet.messageId)
91
106
  cb(null, packet)
92
107
  } else if (cb) {
93
108
  cb(new Error('missing packet'))
@@ -100,7 +115,7 @@ Store.prototype.del = function (packet, cb) {
100
115
  * get a packet from the store.
101
116
  */
102
117
  Store.prototype.get = function (packet, cb) {
103
- packet = this._inflights[packet.messageId]
118
+ packet = this._inflights.get(packet.messageId)
104
119
  if (packet) {
105
120
  cb(null, packet)
106
121
  } else if (cb) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mqtt",
3
3
  "description": "A library for the MQTT protocol",
4
- "version": "2.18.4",
4
+ "version": "2.18.8",
5
5
  "contributors": [
6
6
  "Adam Rudd <adamvrr@gmail.com>",
7
7
  "Matteo Collina <matteo.collina@gmail.com> (https://github.com/mcollina)"
@@ -26,7 +26,7 @@
26
26
  "typescript-compile-test": "tsc -p test/typescript/tsconfig.json",
27
27
  "typescript-compile-execute": "node test/typescript/*.js",
28
28
  "typescript-test": "npm run typescript-compile-test && npm run typescript-compile-execute",
29
- "prepare": "nsp check && npm run browser-build",
29
+ "prepare": "npm run browser-build",
30
30
  "browser-build": "rimraf dist/ && mkdirp dist/ && browserify mqtt.js -s mqtt > dist/mqtt.js && uglifyjs < dist/mqtt.js > dist/mqtt.min.js",
31
31
  "browser-test": "zuul --server test/browser/server.js --local --open test/browser/test.js",
32
32
  "weapp-test": "zuul --server test/browser/server.js --local --open test/browser/wx.js",
@@ -66,6 +66,7 @@
66
66
  "commist": "^1.0.0",
67
67
  "concat-stream": "^1.6.2",
68
68
  "end-of-stream": "^1.4.1",
69
+ "es6-map": "^0.1.5",
69
70
  "help-me": "^1.0.1",
70
71
  "inherits": "^2.0.3",
71
72
  "minimist": "^1.2.0",
@@ -86,7 +87,6 @@
86
87
  "mkdirp": "^0.5.1",
87
88
  "mocha": "^4.1.0",
88
89
  "mqtt-connection": "^3.0.0",
89
- "nsp": "^3.2.1",
90
90
  "pre-commit": "^1.2.2",
91
91
  "rimraf": "^2.6.2",
92
92
  "safe-buffer": "^5.1.2",
@@ -973,6 +973,115 @@ module.exports = function (server, config) {
973
973
  }
974
974
  })
975
975
 
976
+ it('should handle error with async incoming store in QoS 2 `handlePublish` method', function (done) {
977
+ function AsyncStore () {
978
+ if (!(this instanceof AsyncStore)) {
979
+ return new AsyncStore()
980
+ }
981
+ }
982
+ AsyncStore.prototype.put = function (packet, cb) {
983
+ process.nextTick(function () {
984
+ cb(new Error('Error'))
985
+ })
986
+ }
987
+ var store = new AsyncStore()
988
+ var client = connect({incomingStore: store})
989
+
990
+ client._handlePublish({
991
+ messageId: 1,
992
+ topic: 'test',
993
+ payload: 'test',
994
+ qos: 2
995
+ }, function () {
996
+ done()
997
+ client.end()
998
+ })
999
+ })
1000
+
1001
+ it('should handle error with async incoming store in QoS 2 `handlePubrel` method', function (done) {
1002
+ function AsyncStore () {
1003
+ if (!(this instanceof AsyncStore)) {
1004
+ return new AsyncStore()
1005
+ }
1006
+ }
1007
+ AsyncStore.prototype.put = function (packet, cb) {
1008
+ process.nextTick(function () {
1009
+ cb(new Error('Error'))
1010
+ })
1011
+ }
1012
+ AsyncStore.prototype.get = function (packet, cb) {
1013
+ process.nextTick(function () {
1014
+ cb(null, {cmd: 'publish'})
1015
+ })
1016
+ }
1017
+ var store = new AsyncStore()
1018
+ var client = connect({incomingStore: store})
1019
+
1020
+ client._handlePubrel({
1021
+ messageId: 1,
1022
+ qos: 2
1023
+ }, function () {
1024
+ done()
1025
+ client.end()
1026
+ })
1027
+ })
1028
+
1029
+ it('should handle success with async incoming store in QoS 2 `handlePubrel` method', function (done) {
1030
+ var putComplete = false
1031
+ function AsyncStore () {
1032
+ if (!(this instanceof AsyncStore)) {
1033
+ return new AsyncStore()
1034
+ }
1035
+ }
1036
+ AsyncStore.prototype.put = function (packet, cb) {
1037
+ process.nextTick(function () {
1038
+ putComplete = true
1039
+ cb(null)
1040
+ })
1041
+ }
1042
+ AsyncStore.prototype.get = function (packet, cb) {
1043
+ process.nextTick(function () {
1044
+ cb(null, {cmd: 'publish'})
1045
+ })
1046
+ }
1047
+ var store = new AsyncStore()
1048
+ var client = connect({incomingStore: store})
1049
+
1050
+ client._handlePubrel({
1051
+ messageId: 1,
1052
+ qos: 2
1053
+ }, function () {
1054
+ putComplete.should.equal(true)
1055
+ done()
1056
+ client.end()
1057
+ })
1058
+ })
1059
+
1060
+ it('should handle error with async incoming store in QoS 1 `handlePublish` method', function (done) {
1061
+ function AsyncStore () {
1062
+ if (!(this instanceof AsyncStore)) {
1063
+ return new AsyncStore()
1064
+ }
1065
+ }
1066
+ AsyncStore.prototype.put = function (packet, cb) {
1067
+ process.nextTick(function () {
1068
+ cb(null, 'Error')
1069
+ })
1070
+ }
1071
+ var store = new AsyncStore()
1072
+ var client = connect({incomingStore: store})
1073
+
1074
+ client._handlePublish({
1075
+ messageId: 1,
1076
+ topic: 'test',
1077
+ payload: 'test',
1078
+ qos: 1
1079
+ }, function () {
1080
+ done()
1081
+ client.end()
1082
+ })
1083
+ })
1084
+
976
1085
  it('should not send a `pubcomp` if the execution of `handleMessage` fails for messages with QoS `2`', function (done) {
977
1086
  var store = new Store()
978
1087
  var client = connect({incomingStore: store})
@@ -1911,6 +2020,31 @@ module.exports = function (server, config) {
1911
2020
  }
1912
2021
  })
1913
2022
 
2023
+ it('should not resend in-flight publish messages if disconnecting', function (done) {
2024
+ var client = connect({reconnectPeriod: 200})
2025
+ var serverPublished = false
2026
+ var clientCalledBack = false
2027
+ server.once('client', function (serverClient) {
2028
+ serverClient.on('connect', function () {
2029
+ setImmediate(function () {
2030
+ serverClient.stream.destroy()
2031
+ client.end()
2032
+ serverPublished.should.be.false()
2033
+ clientCalledBack.should.be.false()
2034
+ done()
2035
+ })
2036
+ })
2037
+ server.once('client', function (serverClientNew) {
2038
+ serverClientNew.on('publish', function () {
2039
+ serverPublished = true
2040
+ })
2041
+ })
2042
+ })
2043
+ client.publish('hello', 'world', { qos: 1 }, function () {
2044
+ clientCalledBack = true
2045
+ })
2046
+ })
2047
+
1914
2048
  it('should resend in-flight QoS 2 publish messages from the client', function (done) {
1915
2049
  var client = connect({reconnectPeriod: 200})
1916
2050
  var serverPublished = false
@@ -1970,10 +2104,10 @@ module.exports = function (server, config) {
1970
2104
  should(err.message).be.equal('Message removed')
1971
2105
  })
1972
2106
  should(Object.keys(client.outgoing).length).be.equal(1)
1973
- should(Object.keys(client.outgoingStore._inflights).length).be.equal(1)
2107
+ should(client.outgoingStore._inflights.size).be.equal(1)
1974
2108
  client.removeOutgoingMessage(client.getLastMessageId())
1975
2109
  should(Object.keys(client.outgoing).length).be.equal(0)
1976
- should(Object.keys(client.outgoingStore._inflights).length).be.equal(0)
2110
+ should(client.outgoingStore._inflights.size).be.equal(0)
1977
2111
  clientCalledBack.should.be.true()
1978
2112
  client.end()
1979
2113
  done()
@@ -2003,10 +2137,10 @@ module.exports = function (server, config) {
2003
2137
  should(err.message).be.equal('Message removed')
2004
2138
  })
2005
2139
  should(Object.keys(client.outgoing).length).be.equal(1)
2006
- should(Object.keys(client.outgoingStore._inflights).length).be.equal(1)
2140
+ should(client.outgoingStore._inflights.size).be.equal(1)
2007
2141
  client.removeOutgoingMessage(client.getLastMessageId())
2008
2142
  should(Object.keys(client.outgoing).length).be.equal(0)
2009
- should(Object.keys(client.outgoingStore._inflights).length).be.equal(0)
2143
+ should(client.outgoingStore._inflights.size).be.equal(0)
2010
2144
  clientCalledBack.should.be.true()
2011
2145
  client.end()
2012
2146
  done()
@@ -2323,6 +2457,76 @@ module.exports = function (server, config) {
2323
2457
  })
2324
2458
  })
2325
2459
 
2460
+ it('should resend in-flight publish messages by published order', function (done) {
2461
+ var publishCount = 0
2462
+ var reconnect = false
2463
+ var disconnectOnce = true
2464
+ var client = {}
2465
+ var incomingStore = new mqtt.Store({ clean: false })
2466
+ var outgoingStore = new mqtt.Store({ clean: false })
2467
+ var server2 = new Server(function (c) {
2468
+ // errors are not interesting for this test
2469
+ // but they might happen on some platforms
2470
+ c.on('error', function () {})
2471
+
2472
+ c.on('connect', function (packet) {
2473
+ c.connack({returnCode: 0})
2474
+ })
2475
+ c.on('publish', function (packet) {
2476
+ c.puback({messageId: packet.messageId})
2477
+ if (reconnect) {
2478
+ switch (publishCount++) {
2479
+ case 0:
2480
+ packet.payload.toString().should.equal('payload1')
2481
+ break
2482
+ case 1:
2483
+ packet.payload.toString().should.equal('payload2')
2484
+ break
2485
+ case 2:
2486
+ packet.payload.toString().should.equal('payload3')
2487
+ server2.close()
2488
+ done()
2489
+ break
2490
+ }
2491
+ } else {
2492
+ if (disconnectOnce) {
2493
+ client.end(true, function () {
2494
+ reconnect = true
2495
+ client.reconnect({
2496
+ incomingStore: incomingStore,
2497
+ outgoingStore: outgoingStore
2498
+ })
2499
+ })
2500
+ disconnectOnce = false
2501
+ }
2502
+ }
2503
+ })
2504
+ })
2505
+
2506
+ server2.listen(port + 50, function () {
2507
+ client = mqtt.connect({
2508
+ port: port + 50,
2509
+ host: 'localhost',
2510
+ clean: false,
2511
+ clientId: 'cid1',
2512
+ reconnectPeriod: 0,
2513
+ incomingStore: incomingStore,
2514
+ outgoingStore: outgoingStore
2515
+ })
2516
+
2517
+ client.nextId = 65535
2518
+
2519
+ client.on('connect', function () {
2520
+ if (!reconnect) {
2521
+ client.publish('topic', 'payload1', {qos: 1})
2522
+ client.publish('topic', 'payload2', {qos: 1})
2523
+ client.publish('topic', 'payload3', {qos: 1})
2524
+ }
2525
+ })
2526
+ client.on('error', function () {})
2527
+ })
2528
+ })
2529
+
2326
2530
  it('should be able to pub/sub if reconnect() is called at close handler', function (done) {
2327
2531
  var client = connect({ reconnectPeriod: 0 })
2328
2532
  var tryReconnect = true
@@ -71,12 +71,14 @@ module.exports = function abstractStoreTest (build) {
71
71
 
72
72
  it('should replace a packet when doing put with the same messageId', function (done) {
73
73
  var packet1 = {
74
+ cmd: 'publish', // added
74
75
  topic: 'hello',
75
76
  payload: 'world',
76
77
  qos: 2,
77
78
  messageId: 42
78
79
  }
79
80
  var packet2 = {
81
+ cmd: 'pubrel', // added
80
82
  qos: 2,
81
83
  messageId: 42
82
84
  }