mqtt 5.5.6 → 5.6.0

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
@@ -12634,7 +12634,7 @@ var mqtt = (() => {
12634
12634
  // package-json:../../package.json
12635
12635
  var require_package = __commonJS({
12636
12636
  "package-json:../../package.json"(exports5, module) {
12637
- module.exports = { version: "5.5.6" };
12637
+ module.exports = { version: "5.6.0" };
12638
12638
  }
12639
12639
  });
12640
12640
 
@@ -17112,7 +17112,6 @@ var mqtt = (() => {
17112
17112
  }
17113
17113
  if (packet.properties.serverKeepAlive && options.keepalive) {
17114
17114
  options.keepalive = packet.properties.serverKeepAlive;
17115
- client["_shiftPingInterval"]();
17116
17115
  }
17117
17116
  if (packet.properties.maximumPacketSize) {
17118
17117
  if (!options.properties) {
@@ -17191,10 +17190,6 @@ var mqtt = (() => {
17191
17190
  });
17192
17191
  return client;
17193
17192
  }
17194
- client.pingResp = Date.now();
17195
- if (!["pingresp", "publish"].includes(packet.cmd)) {
17196
- client["_shiftPingInterval"]();
17197
- }
17198
17193
  client.log("_handlePacket :: emitting packetreceive");
17199
17194
  client.emit("packetreceive", packet);
17200
17195
  switch (packet.cmd) {
@@ -17206,10 +17201,12 @@ var mqtt = (() => {
17206
17201
  case "pubcomp":
17207
17202
  case "suback":
17208
17203
  case "unsuback":
17204
+ client.reschedulePing();
17209
17205
  (0, ack_1.default)(client, packet);
17210
17206
  done();
17211
17207
  break;
17212
17208
  case "pubrel":
17209
+ client.reschedulePing();
17213
17210
  (0, pubrel_1.default)(client, packet, done);
17214
17211
  break;
17215
17212
  case "connack":
@@ -17217,10 +17214,13 @@ var mqtt = (() => {
17217
17214
  done();
17218
17215
  break;
17219
17216
  case "auth":
17217
+ client.reschedulePing();
17220
17218
  (0, auth_1.default)(client, packet);
17221
17219
  done();
17222
17220
  break;
17223
17221
  case "pingresp":
17222
+ client.log("_handlePacket :: received pingresp");
17223
+ client.reschedulePing();
17224
17224
  done();
17225
17225
  break;
17226
17226
  case "disconnect":
@@ -17604,12 +17604,12 @@ var mqtt = (() => {
17604
17604
  var is_browser_1 = __importStar(require_is_browser());
17605
17605
  var worker_timers_1 = require_bundle3();
17606
17606
  var workerTimer = {
17607
- set: worker_timers_1.setTimeout,
17608
- clear: worker_timers_1.clearTimeout
17607
+ set: worker_timers_1.setInterval,
17608
+ clear: worker_timers_1.clearInterval
17609
17609
  };
17610
17610
  var nativeTimer = {
17611
- set: (func, time) => setTimeout(func, time),
17612
- clear: (timerId) => clearTimeout(timerId)
17611
+ set: (func, time) => setInterval(func, time),
17612
+ clear: (timerId) => clearInterval(timerId)
17613
17613
  };
17614
17614
  var getTimer = (variant) => {
17615
17615
  switch (variant) {
@@ -17629,9 +17629,9 @@ var mqtt = (() => {
17629
17629
  }
17630
17630
  });
17631
17631
 
17632
- // build/lib/PingTimer.js
17633
- var require_PingTimer = __commonJS({
17634
- "build/lib/PingTimer.js"(exports5) {
17632
+ // build/lib/KeepaliveManager.js
17633
+ var require_KeepaliveManager = __commonJS({
17634
+ "build/lib/KeepaliveManager.js"(exports5) {
17635
17635
  "use strict";
17636
17636
  init_buffer2();
17637
17637
  init_process2();
@@ -17641,13 +17641,21 @@ var mqtt = (() => {
17641
17641
  };
17642
17642
  Object.defineProperty(exports5, "__esModule", { value: true });
17643
17643
  var get_timer_1 = __importDefault(require_get_timer());
17644
- var PingTimer = class {
17645
- constructor(keepalive, checkPing, variant) {
17644
+ var KeepaliveManager = class {
17645
+ get keepaliveTimeoutTimestamp() {
17646
+ return this._keepaliveTimeoutTimestamp;
17647
+ }
17648
+ get intervalEvery() {
17649
+ return this._intervalEvery;
17650
+ }
17651
+ get keepalive() {
17652
+ return this._keepalive;
17653
+ }
17654
+ constructor(client, variant) {
17646
17655
  this.destroyed = false;
17647
- this.keepalive = keepalive * 1e3;
17648
- this.checkPing = checkPing;
17656
+ this.client = client;
17649
17657
  this.timer = (0, get_timer_1.default)(variant);
17650
- this.reschedule();
17658
+ this.setKeepalive(client.options.keepalive);
17651
17659
  }
17652
17660
  clear() {
17653
17661
  if (this.timerId) {
@@ -17655,6 +17663,15 @@ var mqtt = (() => {
17655
17663
  this.timerId = null;
17656
17664
  }
17657
17665
  }
17666
+ setKeepalive(value) {
17667
+ value *= 1e3;
17668
+ if (isNaN(value) || value <= 0 || value > 2147483647) {
17669
+ throw new Error(`Keepalive value must be an integer between 0 and 2147483647. Provided value is ${value}`);
17670
+ }
17671
+ this._keepalive = value;
17672
+ this.reschedule();
17673
+ this.client["log"](`KeepaliveManager: set keepalive to ${value}ms`);
17674
+ }
17658
17675
  destroy() {
17659
17676
  this.clear();
17660
17677
  this.destroyed = true;
@@ -17664,16 +17681,24 @@ var mqtt = (() => {
17664
17681
  return;
17665
17682
  }
17666
17683
  this.clear();
17684
+ this.counter = 0;
17685
+ const keepAliveTimeout = Math.ceil(this._keepalive * 1.5);
17686
+ this._keepaliveTimeoutTimestamp = Date.now() + keepAliveTimeout;
17687
+ this._intervalEvery = Math.ceil(this._keepalive / 2);
17667
17688
  this.timerId = this.timer.set(() => {
17668
17689
  if (this.destroyed) {
17669
17690
  return;
17670
17691
  }
17671
- this.checkPing();
17672
- this.reschedule();
17673
- }, this.keepalive);
17692
+ this.counter += 1;
17693
+ if (this.counter === 2) {
17694
+ this.client.sendPing();
17695
+ } else if (this.counter > 2) {
17696
+ this.client.onKeepaliveTimeout();
17697
+ }
17698
+ }, this._intervalEvery);
17674
17699
  }
17675
17700
  };
17676
- exports5.default = PingTimer;
17701
+ exports5.default = KeepaliveManager;
17677
17702
  }
17678
17703
  });
17679
17704
 
@@ -17731,7 +17756,7 @@ var mqtt = (() => {
17731
17756
  var handlers_1 = __importDefault(require_handlers());
17732
17757
  var shared_1 = require_shared();
17733
17758
  var TypedEmitter_1 = require_TypedEmitter();
17734
- var PingTimer_1 = __importDefault(require_PingTimer());
17759
+ var KeepaliveManager_1 = __importDefault(require_KeepaliveManager());
17735
17760
  var is_browser_1 = __importStar(require_is_browser());
17736
17761
  var setImmediate2 = globalThis.setImmediate || ((...args) => {
17737
17762
  const callback = args.shift();
@@ -17795,7 +17820,7 @@ var mqtt = (() => {
17795
17820
  this.queueQoSZero = options.queueQoSZero === void 0 ? true : options.queueQoSZero;
17796
17821
  this._resubscribeTopics = {};
17797
17822
  this.messageIdToTopic = {};
17798
- this.pingTimer = null;
17823
+ this.keepaliveManager = null;
17799
17824
  this.connected = false;
17800
17825
  this.disconnecting = false;
17801
17826
  this.reconnecting = false;
@@ -17852,7 +17877,7 @@ var mqtt = (() => {
17852
17877
  this.connected = false;
17853
17878
  this.log("close :: clearing connackTimer");
17854
17879
  clearTimeout(this.connackTimer);
17855
- this._destroyPingTimer();
17880
+ this._destroyKeepaliveManager();
17856
17881
  if (this.topicAliasRecv) {
17857
17882
  this.topicAliasRecv.clear();
17858
17883
  }
@@ -18481,7 +18506,7 @@ var mqtt = (() => {
18481
18506
  this._clearReconnect();
18482
18507
  this._setupReconnect();
18483
18508
  }
18484
- this._destroyPingTimer();
18509
+ this._destroyKeepaliveManager();
18485
18510
  if (done && !this.connected) {
18486
18511
  this.log("_cleanUp :: (%s) :: removing stream `done` callback `close` listener", this.options.clientId);
18487
18512
  this.stream.removeListener("close", done);
@@ -18642,47 +18667,37 @@ var mqtt = (() => {
18642
18667
  cb(new Error("No connection to broker"));
18643
18668
  }
18644
18669
  }
18645
- _setupPingTimer() {
18646
- this.log("_setupPingTimer :: keepalive %d (seconds)", this.options.keepalive);
18647
- if (!this.pingTimer && this.options.keepalive) {
18648
- this.pingTimer = new PingTimer_1.default(this.options.keepalive, () => {
18649
- this._checkPing();
18650
- }, this.options.timerVariant);
18651
- this.pingResp = Date.now();
18670
+ _setupKeepaliveManager() {
18671
+ this.log("_setupKeepaliveManager :: keepalive %d (seconds)", this.options.keepalive);
18672
+ if (!this.keepaliveManager && this.options.keepalive) {
18673
+ this.keepaliveManager = new KeepaliveManager_1.default(this, this.options.timerVariant);
18652
18674
  }
18653
18675
  }
18654
- _destroyPingTimer() {
18655
- if (this.pingTimer) {
18656
- this.log("_destroyPingTimer :: destroying ping timer");
18657
- this.pingTimer.destroy();
18658
- this.pingTimer = null;
18676
+ _destroyKeepaliveManager() {
18677
+ if (this.keepaliveManager) {
18678
+ this.log("_destroyKeepaliveManager :: destroying keepalive manager");
18679
+ this.keepaliveManager.destroy();
18680
+ this.keepaliveManager = null;
18659
18681
  }
18660
18682
  }
18661
- _shiftPingInterval() {
18662
- if (this.pingTimer && this.options.keepalive && this.options.reschedulePings) {
18683
+ reschedulePing() {
18684
+ if (this.keepaliveManager && this.options.keepalive && this.options.reschedulePings) {
18663
18685
  this._reschedulePing();
18664
18686
  }
18665
18687
  }
18666
18688
  _reschedulePing() {
18667
18689
  this.log("_reschedulePing :: rescheduling ping");
18668
- this.pingTimer.reschedule();
18669
- }
18670
- _checkPing() {
18671
- this.log("_checkPing :: checking ping...");
18672
- const timeSincePing = Date.now() - this.pingResp - 100;
18673
- if (timeSincePing <= this.options.keepalive * 1e3) {
18674
- this.log("_checkPing :: ping response received in time");
18675
- this._sendPing();
18676
- } else {
18677
- this.emit("error", new Error("Keepalive timeout"));
18678
- this.log("_checkPing :: calling _cleanUp with force true");
18679
- this._cleanUp(true);
18680
- }
18690
+ this.keepaliveManager.reschedule();
18681
18691
  }
18682
- _sendPing() {
18692
+ sendPing() {
18683
18693
  this.log("_sendPing :: sending pingreq");
18684
18694
  this._sendPacket({ cmd: "pingreq" });
18685
18695
  }
18696
+ onKeepaliveTimeout() {
18697
+ this.emit("error", new Error("Keepalive timeout"));
18698
+ this.log("onKeepaliveTimeout :: calling _cleanUp with force true");
18699
+ this._cleanUp(true);
18700
+ }
18686
18701
  _resubscribe() {
18687
18702
  this.log("_resubscribe");
18688
18703
  const _resubscribeTopicsKeys = Object.keys(this._resubscribeTopics);
@@ -18715,7 +18730,7 @@ var mqtt = (() => {
18715
18730
  }
18716
18731
  this.connackPacket = packet;
18717
18732
  this.messageIdProvider.clear();
18718
- this._setupPingTimer();
18733
+ this._setupKeepaliveManager();
18719
18734
  this.connected = true;
18720
18735
  const startStreamProcess = () => {
18721
18736
  let outStore = this.outgoingStore.createStream();
@@ -21554,7 +21569,7 @@ var mqtt = (() => {
21554
21569
  return mod && mod.__esModule ? mod : { "default": mod };
21555
21570
  };
21556
21571
  Object.defineProperty(exports5, "__esModule", { value: true });
21557
- exports5.ReasonCodes = exports5.PingTimer = exports5.UniqueMessageIdProvider = exports5.DefaultMessageIdProvider = exports5.Store = exports5.MqttClient = exports5.connectAsync = exports5.connect = exports5.Client = void 0;
21572
+ exports5.ReasonCodes = exports5.KeepaliveManager = exports5.UniqueMessageIdProvider = exports5.DefaultMessageIdProvider = exports5.Store = exports5.MqttClient = exports5.connectAsync = exports5.connect = exports5.Client = void 0;
21558
21573
  var client_1 = __importDefault(require_client());
21559
21574
  exports5.MqttClient = client_1.default;
21560
21575
  var default_message_id_provider_1 = __importDefault(require_default_message_id_provider());
@@ -21568,8 +21583,8 @@ var mqtt = (() => {
21568
21583
  Object.defineProperty(exports5, "connectAsync", { enumerable: true, get: function() {
21569
21584
  return connect_1.connectAsync;
21570
21585
  } });
21571
- var PingTimer_1 = __importDefault(require_PingTimer());
21572
- exports5.PingTimer = PingTimer_1.default;
21586
+ var KeepaliveManager_1 = __importDefault(require_KeepaliveManager());
21587
+ exports5.KeepaliveManager = KeepaliveManager_1.default;
21573
21588
  exports5.Client = client_1.default;
21574
21589
  __exportStar(require_client(), exports5);
21575
21590
  __exportStar(require_shared(), exports5);