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/lib/client.js CHANGED
@@ -17,10 +17,11 @@ const validations = require('./validations')
17
17
  const xtend = require('xtend')
18
18
  const debug = require('debug')('mqttjs:client')
19
19
  const nextTick = process ? process.nextTick : function (callback) { setTimeout(callback, 0) }
20
- const setImmediate = global.setImmediate || function (callback) {
21
- // works in node v0.8
22
- nextTick(callback)
20
+ const setImmediate = global.setImmediate || function (...args) {
21
+ const callback = args.shift()
22
+ nextTick(callback.bind(null, ...args))
23
23
  }
24
+
24
25
  const defaultConnectOptions = {
25
26
  keepalive: 60,
26
27
  reschedulePings: true,
@@ -29,7 +30,8 @@ const defaultConnectOptions = {
29
30
  reconnectPeriod: 1000,
30
31
  connectTimeout: 30 * 1000,
31
32
  clean: true,
32
- resubscribe: true
33
+ resubscribe: true,
34
+ writeCache: true
33
35
  }
34
36
 
35
37
  const socketErrors = [
@@ -278,6 +280,11 @@ function MqttClient (streamBuilder, options) {
278
280
 
279
281
  this.options.customHandleAcks = (options.protocolVersion === 5 && options.customHandleAcks) ? options.customHandleAcks : function () { arguments[3](0) }
280
282
 
283
+ // Disable pre-generated write cache if requested. Will allocate buffers on-the-fly instead. WARNING: This can affect write performance
284
+ if (!this.options.writeCache) {
285
+ mqttPacket.writeToStream.cacheNumbers = false
286
+ }
287
+
281
288
  this.streamBuilder = streamBuilder
282
289
 
283
290
  this.messageIdProvider = (typeof this.options.messageIdProvider === 'undefined') ? new DefaultMessageIdProvider() : this.options.messageIdProvider
@@ -330,7 +337,7 @@ function MqttClient (streamBuilder, options) {
330
337
 
331
338
  // Send queued packets
332
339
  this.on('connect', function () {
333
- const queue = this.queue
340
+ const queue = that.queue
334
341
 
335
342
  function deliver () {
336
343
  const entry = queue.shift()
@@ -372,10 +379,10 @@ function MqttClient (streamBuilder, options) {
372
379
 
373
380
  this.on('close', function () {
374
381
  debug('close :: connected set to `false`')
375
- this.connected = false
382
+ that.connected = false
376
383
 
377
384
  debug('close :: clearing connackTimer')
378
- clearTimeout(this.connackTimer)
385
+ clearTimeout(that.connackTimer)
379
386
 
380
387
  debug('close :: clearing ping timer')
381
388
  if (that.pingTimer !== null) {
@@ -383,12 +390,12 @@ function MqttClient (streamBuilder, options) {
383
390
  that.pingTimer = null
384
391
  }
385
392
 
386
- if (this.topicAliasRecv) {
387
- this.topicAliasRecv.clear()
393
+ if (that.topicAliasRecv) {
394
+ that.topicAliasRecv.clear()
388
395
  }
389
396
 
390
397
  debug('close :: calling _setupReconnect')
391
- this._setupReconnect()
398
+ that._setupReconnect()
392
399
  })
393
400
  EventEmitter.call(this)
394
401
 
@@ -741,7 +748,7 @@ MqttClient.prototype.subscribe = function () {
741
748
  debug('subscribe: array topic %s', topic)
742
749
  if (!Object.prototype.hasOwnProperty.call(that._resubscribeTopics, topic) ||
743
750
  that._resubscribeTopics[topic].qos < opts.qos ||
744
- resubscribe) {
751
+ resubscribe) {
745
752
  const currentOpts = {
746
753
  topic: topic,
747
754
  qos: opts.qos
@@ -763,7 +770,7 @@ MqttClient.prototype.subscribe = function () {
763
770
  debug('subscribe: object topic %s', k)
764
771
  if (!Object.prototype.hasOwnProperty.call(that._resubscribeTopics, k) ||
765
772
  that._resubscribeTopics[k].qos < obj[k].qos ||
766
- resubscribe) {
773
+ resubscribe) {
767
774
  const currentOpts = {
768
775
  topic: k,
769
776
  qos: obj[k].qos
@@ -1195,9 +1202,10 @@ MqttClient.prototype._cleanUp = function (forced, done) {
1195
1202
  * @param {Object} packet - packet options
1196
1203
  * @param {Function} cb - callback when the packet is sent
1197
1204
  * @param {Function} cbStorePut - called when message is put into outgoingStore
1205
+ * @param {Boolean} noStore - send without put to the store
1198
1206
  * @api private
1199
1207
  */
1200
- MqttClient.prototype._sendPacket = function (packet, cb, cbStorePut) {
1208
+ MqttClient.prototype._sendPacket = function (packet, cb, cbStorePut, noStore) {
1201
1209
  debug('_sendPacket :: (%s) :: start', this.options.clientId)
1202
1210
  cbStorePut = cbStorePut || nop
1203
1211
  cb = cb || nop
@@ -1224,6 +1232,16 @@ MqttClient.prototype._sendPacket = function (packet, cb, cbStorePut) {
1224
1232
  // When sending a packet, reschedule the ping timer
1225
1233
  this._shiftPingInterval()
1226
1234
 
1235
+ // If "noStore" is true, the message is sent without being recorded in the store.
1236
+ // Messages that have not received puback or pubcomp remain in the store after disconnection
1237
+ // and are resent from the store upon reconnection.
1238
+ // For resend upon reconnection, "noStore" is set to true. This is because the message is already stored in the store.
1239
+ // This is to avoid interrupting other processes while recording to the store.
1240
+ if (noStore) {
1241
+ sendPacket(this, packet, cb)
1242
+ return
1243
+ }
1244
+
1227
1245
  switch (packet.cmd) {
1228
1246
  case 'publish':
1229
1247
  break
@@ -1246,7 +1264,7 @@ MqttClient.prototype._sendPacket = function (packet, cb, cbStorePut) {
1246
1264
  * anyway it will result in -1 evaluation
1247
1265
  */
1248
1266
  case 0:
1249
- /* falls through */
1267
+ /* falls through */
1250
1268
  default:
1251
1269
  sendPacket(this, packet, cb)
1252
1270
  break
@@ -1605,7 +1623,7 @@ MqttClient.prototype._handleAck = function (packet) {
1605
1623
  debug('_handleAck :: packet type', type)
1606
1624
  switch (type) {
1607
1625
  case 'pubcomp':
1608
- // same thing as puback for QoS 2
1626
+ // same thing as puback for QoS 2
1609
1627
  case 'puback': {
1610
1628
  const pubackRC = packet.reasonCode
1611
1629
  // Callback - we're done
@@ -1651,6 +1669,7 @@ MqttClient.prototype._handleAck = function (packet) {
1651
1669
  }
1652
1670
  }
1653
1671
  }
1672
+ delete this.messageIdToTopic[messageId]
1654
1673
  this._invokeStoreProcessingQueue()
1655
1674
  cb(null, packet)
1656
1675
  break
@@ -1667,7 +1686,7 @@ MqttClient.prototype._handleAck = function (packet) {
1667
1686
  }
1668
1687
 
1669
1688
  if (this.disconnecting &&
1670
- Object.keys(this.outgoing).length === 0) {
1689
+ Object.keys(this.outgoing).length === 0) {
1671
1690
  this.emit('outgoingEmpty')
1672
1691
  }
1673
1692
  }
@@ -1736,8 +1755,8 @@ MqttClient.prototype._resubscribe = function () {
1736
1755
  debug('_resubscribe')
1737
1756
  const _resubscribeTopicsKeys = Object.keys(this._resubscribeTopics)
1738
1757
  if (!this._firstConnection &&
1739
- (this.options.clean || (this.options.protocolVersion === 5 && !this.connackPacket.sessionPresent)) &&
1740
- _resubscribeTopicsKeys.length > 0) {
1758
+ (this.options.clean || (this.options.protocolVersion === 5 && !this.connackPacket.sessionPresent)) &&
1759
+ _resubscribeTopicsKeys.length > 0) {
1741
1760
  if (this.options.resubscribe) {
1742
1761
  if (this.options.protocolVersion === 5) {
1743
1762
  debug('_resubscribe: protocolVersion 5')
@@ -1806,7 +1825,6 @@ MqttClient.prototype._onConnect = function (packet) {
1806
1825
  if (!outStore) {
1807
1826
  return
1808
1827
  }
1809
- that._storeProcessing = true
1810
1828
 
1811
1829
  const packet = outStore.read(1)
1812
1830
 
@@ -1818,6 +1836,8 @@ MqttClient.prototype._onConnect = function (packet) {
1818
1836
  return
1819
1837
  }
1820
1838
 
1839
+ that._storeProcessing = true
1840
+
1821
1841
  // Skip already processed store packets
1822
1842
  if (that._packetIdsDuringStoreProcessing[packet.messageId]) {
1823
1843
  storeDeliver()
@@ -1840,7 +1860,7 @@ MqttClient.prototype._onConnect = function (packet) {
1840
1860
  }
1841
1861
  that._packetIdsDuringStoreProcessing[packet.messageId] = true
1842
1862
  if (that.messageIdProvider.register(packet.messageId)) {
1843
- that._sendPacket(packet)
1863
+ that._sendPacket(packet, undefined, undefined, true)
1844
1864
  } else {
1845
1865
  debug('messageId: %d has already used.', packet.messageId)
1846
1866
  }
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": "5.0.0-beta.0",
4
+ "version": "5.0.0-beta.2",
5
5
  "contributors": [
6
6
  "Adam Rudd <adamvrr@gmail.com>",
7
7
  "Matteo Collina <matteo.collina@gmail.com> (https://github.com/mcollina)",
@@ -49,9 +49,6 @@
49
49
  "hooks": {
50
50
  "before:init": [
51
51
  "npm run test"
52
- ],
53
- "after:bump": [
54
- "npm run changelog"
55
52
  ]
56
53
  },
57
54
  "npm": {
@@ -126,6 +123,7 @@
126
123
  "mkdirp": "^0.5.1",
127
124
  "mocha": "^9.2.0",
128
125
  "mqtt-connection": "^4.0.0",
126
+ "mqtt-level-store": "^3.1.0",
129
127
  "nyc": "^15.0.1",
130
128
  "pre-commit": "^1.2.2",
131
129
  "release-it": "^15.11.0",