mqtt 5.0.0-beta.0 → 5.0.0-beta.2

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/dist/mqtt.js CHANGED
@@ -19,10 +19,11 @@ const validations = require('./validations')
19
19
  const xtend = require('xtend')
20
20
  const debug = require('debug')('mqttjs:client')
21
21
  const nextTick = process ? process.nextTick : function (callback) { setTimeout(callback, 0) }
22
- const setImmediate = global.setImmediate || function (callback) {
23
- // works in node v0.8
24
- nextTick(callback)
22
+ const setImmediate = global.setImmediate || function (...args) {
23
+ const callback = args.shift()
24
+ nextTick(callback.bind(null, ...args))
25
25
  }
26
+
26
27
  const defaultConnectOptions = {
27
28
  keepalive: 60,
28
29
  reschedulePings: true,
@@ -31,7 +32,8 @@ const defaultConnectOptions = {
31
32
  reconnectPeriod: 1000,
32
33
  connectTimeout: 30 * 1000,
33
34
  clean: true,
34
- resubscribe: true
35
+ resubscribe: true,
36
+ writeCache: true
35
37
  }
36
38
 
37
39
  const socketErrors = [
@@ -280,6 +282,11 @@ function MqttClient (streamBuilder, options) {
280
282
 
281
283
  this.options.customHandleAcks = (options.protocolVersion === 5 && options.customHandleAcks) ? options.customHandleAcks : function () { arguments[3](0) }
282
284
 
285
+ // Disable pre-generated write cache if requested. Will allocate buffers on-the-fly instead. WARNING: This can affect write performance
286
+ if (!this.options.writeCache) {
287
+ mqttPacket.writeToStream.cacheNumbers = false
288
+ }
289
+
283
290
  this.streamBuilder = streamBuilder
284
291
 
285
292
  this.messageIdProvider = (typeof this.options.messageIdProvider === 'undefined') ? new DefaultMessageIdProvider() : this.options.messageIdProvider
@@ -332,7 +339,7 @@ function MqttClient (streamBuilder, options) {
332
339
 
333
340
  // Send queued packets
334
341
  this.on('connect', function () {
335
- const queue = this.queue
342
+ const queue = that.queue
336
343
 
337
344
  function deliver () {
338
345
  const entry = queue.shift()
@@ -374,10 +381,10 @@ function MqttClient (streamBuilder, options) {
374
381
 
375
382
  this.on('close', function () {
376
383
  debug('close :: connected set to `false`')
377
- this.connected = false
384
+ that.connected = false
378
385
 
379
386
  debug('close :: clearing connackTimer')
380
- clearTimeout(this.connackTimer)
387
+ clearTimeout(that.connackTimer)
381
388
 
382
389
  debug('close :: clearing ping timer')
383
390
  if (that.pingTimer !== null) {
@@ -385,12 +392,12 @@ function MqttClient (streamBuilder, options) {
385
392
  that.pingTimer = null
386
393
  }
387
394
 
388
- if (this.topicAliasRecv) {
389
- this.topicAliasRecv.clear()
395
+ if (that.topicAliasRecv) {
396
+ that.topicAliasRecv.clear()
390
397
  }
391
398
 
392
399
  debug('close :: calling _setupReconnect')
393
- this._setupReconnect()
400
+ that._setupReconnect()
394
401
  })
395
402
  EventEmitter.call(this)
396
403
 
@@ -743,7 +750,7 @@ MqttClient.prototype.subscribe = function () {
743
750
  debug('subscribe: array topic %s', topic)
744
751
  if (!Object.prototype.hasOwnProperty.call(that._resubscribeTopics, topic) ||
745
752
  that._resubscribeTopics[topic].qos < opts.qos ||
746
- resubscribe) {
753
+ resubscribe) {
747
754
  const currentOpts = {
748
755
  topic: topic,
749
756
  qos: opts.qos
@@ -765,7 +772,7 @@ MqttClient.prototype.subscribe = function () {
765
772
  debug('subscribe: object topic %s', k)
766
773
  if (!Object.prototype.hasOwnProperty.call(that._resubscribeTopics, k) ||
767
774
  that._resubscribeTopics[k].qos < obj[k].qos ||
768
- resubscribe) {
775
+ resubscribe) {
769
776
  const currentOpts = {
770
777
  topic: k,
771
778
  qos: obj[k].qos
@@ -1197,9 +1204,10 @@ MqttClient.prototype._cleanUp = function (forced, done) {
1197
1204
  * @param {Object} packet - packet options
1198
1205
  * @param {Function} cb - callback when the packet is sent
1199
1206
  * @param {Function} cbStorePut - called when message is put into outgoingStore
1207
+ * @param {Boolean} noStore - send without put to the store
1200
1208
  * @api private
1201
1209
  */
1202
- MqttClient.prototype._sendPacket = function (packet, cb, cbStorePut) {
1210
+ MqttClient.prototype._sendPacket = function (packet, cb, cbStorePut, noStore) {
1203
1211
  debug('_sendPacket :: (%s) :: start', this.options.clientId)
1204
1212
  cbStorePut = cbStorePut || nop
1205
1213
  cb = cb || nop
@@ -1226,6 +1234,16 @@ MqttClient.prototype._sendPacket = function (packet, cb, cbStorePut) {
1226
1234
  // When sending a packet, reschedule the ping timer
1227
1235
  this._shiftPingInterval()
1228
1236
 
1237
+ // If "noStore" is true, the message is sent without being recorded in the store.
1238
+ // Messages that have not received puback or pubcomp remain in the store after disconnection
1239
+ // and are resent from the store upon reconnection.
1240
+ // For resend upon reconnection, "noStore" is set to true. This is because the message is already stored in the store.
1241
+ // This is to avoid interrupting other processes while recording to the store.
1242
+ if (noStore) {
1243
+ sendPacket(this, packet, cb)
1244
+ return
1245
+ }
1246
+
1229
1247
  switch (packet.cmd) {
1230
1248
  case 'publish':
1231
1249
  break
@@ -1248,7 +1266,7 @@ MqttClient.prototype._sendPacket = function (packet, cb, cbStorePut) {
1248
1266
  * anyway it will result in -1 evaluation
1249
1267
  */
1250
1268
  case 0:
1251
- /* falls through */
1269
+ /* falls through */
1252
1270
  default:
1253
1271
  sendPacket(this, packet, cb)
1254
1272
  break
@@ -1607,7 +1625,7 @@ MqttClient.prototype._handleAck = function (packet) {
1607
1625
  debug('_handleAck :: packet type', type)
1608
1626
  switch (type) {
1609
1627
  case 'pubcomp':
1610
- // same thing as puback for QoS 2
1628
+ // same thing as puback for QoS 2
1611
1629
  case 'puback': {
1612
1630
  const pubackRC = packet.reasonCode
1613
1631
  // Callback - we're done
@@ -1653,6 +1671,7 @@ MqttClient.prototype._handleAck = function (packet) {
1653
1671
  }
1654
1672
  }
1655
1673
  }
1674
+ delete this.messageIdToTopic[messageId]
1656
1675
  this._invokeStoreProcessingQueue()
1657
1676
  cb(null, packet)
1658
1677
  break
@@ -1669,7 +1688,7 @@ MqttClient.prototype._handleAck = function (packet) {
1669
1688
  }
1670
1689
 
1671
1690
  if (this.disconnecting &&
1672
- Object.keys(this.outgoing).length === 0) {
1691
+ Object.keys(this.outgoing).length === 0) {
1673
1692
  this.emit('outgoingEmpty')
1674
1693
  }
1675
1694
  }
@@ -1738,8 +1757,8 @@ MqttClient.prototype._resubscribe = function () {
1738
1757
  debug('_resubscribe')
1739
1758
  const _resubscribeTopicsKeys = Object.keys(this._resubscribeTopics)
1740
1759
  if (!this._firstConnection &&
1741
- (this.options.clean || (this.options.protocolVersion === 5 && !this.connackPacket.sessionPresent)) &&
1742
- _resubscribeTopicsKeys.length > 0) {
1760
+ (this.options.clean || (this.options.protocolVersion === 5 && !this.connackPacket.sessionPresent)) &&
1761
+ _resubscribeTopicsKeys.length > 0) {
1743
1762
  if (this.options.resubscribe) {
1744
1763
  if (this.options.protocolVersion === 5) {
1745
1764
  debug('_resubscribe: protocolVersion 5')
@@ -1808,7 +1827,6 @@ MqttClient.prototype._onConnect = function (packet) {
1808
1827
  if (!outStore) {
1809
1828
  return
1810
1829
  }
1811
- that._storeProcessing = true
1812
1830
 
1813
1831
  const packet = outStore.read(1)
1814
1832
 
@@ -1820,6 +1838,8 @@ MqttClient.prototype._onConnect = function (packet) {
1820
1838
  return
1821
1839
  }
1822
1840
 
1841
+ that._storeProcessing = true
1842
+
1823
1843
  // Skip already processed store packets
1824
1844
  if (that._packetIdsDuringStoreProcessing[packet.messageId]) {
1825
1845
  storeDeliver()
@@ -1842,7 +1862,7 @@ MqttClient.prototype._onConnect = function (packet) {
1842
1862
  }
1843
1863
  that._packetIdsDuringStoreProcessing[packet.messageId] = true
1844
1864
  if (that.messageIdProvider.register(packet.messageId)) {
1845
- that._sendPacket(packet)
1865
+ that._sendPacket(packet, undefined, undefined, true)
1846
1866
  } else {
1847
1867
  debug('messageId: %d has already used.', packet.messageId)
1848
1868
  }