mqtt 5.5.4 → 5.5.5

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/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.5.4",
4
+ "version": "5.5.5",
5
5
  "contributors": [
6
6
  "Adam Rudd <adamvrr@gmail.com>",
7
7
  "Matteo Collina <matteo.collina@gmail.com> (https://github.com/mcollina)",
package/src/lib/client.ts CHANGED
@@ -424,8 +424,6 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
424
424
 
425
425
  public messageIdProvider: IMessageIdProvider
426
426
 
427
- public pingResp: boolean
428
-
429
427
  public outgoing: Record<
430
428
  number,
431
429
  { volatile: boolean; cb: (err: Error, packet?: Packet) => void }
@@ -435,6 +433,9 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
435
433
 
436
434
  public noop: (error?: any) => void
437
435
 
436
+ /** Timestamp of last received control packet */
437
+ public pingResp: number
438
+
438
439
  public pingTimer: PingTimer
439
440
 
440
441
  /**
@@ -659,11 +660,7 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
659
660
  this.log('close :: clearing connackTimer')
660
661
  clearTimeout(this.connackTimer)
661
662
 
662
- this.log('close :: destroy ping timer')
663
- if (this.pingTimer) {
664
- this.pingTimer.destroy()
665
- this.pingTimer = null
666
- }
663
+ this._destroyPingTimer()
667
664
 
668
665
  if (this.topicAliasRecv) {
669
666
  this.topicAliasRecv.clear()
@@ -722,6 +719,7 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
722
719
  public connect() {
723
720
  const writable = new Writable()
724
721
  const parser = mqttPacket.parser(this.options)
722
+
725
723
  let completeParse = null
726
724
  const packets = []
727
725
 
@@ -1782,11 +1780,7 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
1782
1780
  this._setupReconnect()
1783
1781
  }
1784
1782
 
1785
- if (this.pingTimer) {
1786
- this.log('_cleanUp :: destroy pingTimer')
1787
- this.pingTimer.destroy()
1788
- this.pingTimer = null
1789
- }
1783
+ this._destroyPingTimer()
1790
1784
 
1791
1785
  if (done && !this.connected) {
1792
1786
  this.log(
@@ -1924,9 +1918,6 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
1924
1918
 
1925
1919
  this.emit('packetsend', packet)
1926
1920
 
1927
- // When writing a packet, reschedule the ping timer
1928
- this._shiftPingInterval()
1929
-
1930
1921
  this.log('_writePacket :: writing to stream')
1931
1922
  const result = mqttPacket.writeToStream(
1932
1923
  packet,
@@ -2084,7 +2075,6 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
2084
2075
  )
2085
2076
 
2086
2077
  if (!this.pingTimer && this.options.keepalive) {
2087
- this.pingResp = true
2088
2078
  this.pingTimer = new PingTimer(
2089
2079
  this.options.keepalive,
2090
2080
  () => {
@@ -2092,10 +2082,20 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
2092
2082
  },
2093
2083
  this.options.timerVariant,
2094
2084
  )
2085
+ this.pingResp = Date.now()
2086
+ }
2087
+ }
2088
+
2089
+ private _destroyPingTimer() {
2090
+ if (this.pingTimer) {
2091
+ this.log('_destroyPingTimer :: destroying ping timer')
2092
+ this.pingTimer.destroy()
2093
+ this.pingTimer = null
2095
2094
  }
2096
2095
  }
2097
2096
 
2098
2097
  /**
2098
+
2099
2099
  * _shiftPingInterval - reschedule the ping interval
2100
2100
  *
2101
2101
  * @api private
@@ -2106,10 +2106,18 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
2106
2106
  this.options.keepalive &&
2107
2107
  this.options.reschedulePings
2108
2108
  ) {
2109
- this.pingTimer.reschedule()
2109
+ this._reschedulePing()
2110
2110
  }
2111
2111
  }
2112
2112
 
2113
+ /**
2114
+ * Mostly needed for test purposes
2115
+ */
2116
+ private _reschedulePing() {
2117
+ this.log('_reschedulePing :: rescheduling ping')
2118
+ this.pingTimer.reschedule()
2119
+ }
2120
+
2113
2121
  /**
2114
2122
  * _checkPing - check if a pingresp has come back, and ping the server again
2115
2123
  *
@@ -2117,12 +2125,11 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
2117
2125
  */
2118
2126
  private _checkPing() {
2119
2127
  this.log('_checkPing :: checking ping...')
2120
- if (this.pingResp) {
2121
- this.log(
2122
- '_checkPing :: ping response received. Clearing flag and sending `pingreq`',
2123
- )
2124
- this.pingResp = false
2125
- this._sendPacket({ cmd: 'pingreq' })
2128
+ // give 100ms offset to avoid ping timeout when receiving fast responses
2129
+ const timeSincePing = Date.now() - this.pingResp - 100
2130
+ if (timeSincePing <= this.options.keepalive * 1000) {
2131
+ this.log('_checkPing :: ping response received in time')
2132
+ this._sendPing()
2126
2133
  } else {
2127
2134
  // do a forced cleanup since socket will be in bad shape
2128
2135
  this.emit('error', new Error('Keepalive timeout'))
@@ -2131,6 +2138,11 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
2131
2138
  }
2132
2139
  }
2133
2140
 
2141
+ private _sendPing() {
2142
+ this.log('_sendPing :: sending pingreq')
2143
+ this._sendPacket({ cmd: 'pingreq' })
2144
+ }
2145
+
2134
2146
  /**
2135
2147
  * _resubscribe
2136
2148
  * @api private
@@ -21,6 +21,15 @@ const handle: PacketHandler = (client, packet, done) => {
21
21
  })
22
22
  return client
23
23
  }
24
+
25
+ // keep track of last time we received a packet (for keepalive mechanism)
26
+ client.pingResp = Date.now()
27
+
28
+ // do not shift on pingresp otherwise we would skip the pingreq sending
29
+ if (packet.cmd !== 'pingresp') {
30
+ client['_shiftPingInterval']()
31
+ }
32
+
24
33
  client.log('_handlePacket :: emitting packetreceive')
25
34
  client.emit('packetreceive', packet)
26
35
 
@@ -49,7 +58,6 @@ const handle: PacketHandler = (client, packet, done) => {
49
58
  break
50
59
  case 'pingresp':
51
60
  // this will be checked in _checkPing client method every keepalive interval
52
- client.pingResp = true
53
61
  done()
54
62
  break
55
63
  case 'disconnect':