mqtt 5.5.5 → 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.
Files changed (39) hide show
  1. package/build/lib/KeepaliveManager.d.ts +20 -0
  2. package/build/lib/KeepaliveManager.js +68 -0
  3. package/build/lib/KeepaliveManager.js.map +1 -0
  4. package/build/lib/client.d.ts +7 -8
  5. package/build/lib/client.js +23 -34
  6. package/build/lib/client.js.map +1 -1
  7. package/build/lib/connect/tcp.js.map +1 -1
  8. package/build/lib/connect/ws.js +1 -3
  9. package/build/lib/connect/ws.js.map +1 -1
  10. package/build/lib/get-timer.d.ts +3 -3
  11. package/build/lib/get-timer.js +4 -4
  12. package/build/lib/get-timer.js.map +1 -1
  13. package/build/lib/handlers/connack.js +0 -1
  14. package/build/lib/handlers/connack.js.map +1 -1
  15. package/build/lib/handlers/index.js +5 -4
  16. package/build/lib/handlers/index.js.map +1 -1
  17. package/build/lib/is-browser.js +12 -1
  18. package/build/lib/is-browser.js.map +1 -1
  19. package/build/mqtt.d.ts +2 -2
  20. package/build/mqtt.js +3 -3
  21. package/build/mqtt.js.map +1 -1
  22. package/build/tsconfig.build.tsbuildinfo +1 -1
  23. package/dist/mqtt.esm.js +3 -3
  24. package/dist/mqtt.js +86 -62
  25. package/dist/mqtt.min.js +3 -3
  26. package/package.json +1 -1
  27. package/src/lib/KeepaliveManager.ts +107 -0
  28. package/src/lib/client.ts +29 -55
  29. package/src/lib/connect/tcp.ts +0 -1
  30. package/src/lib/connect/ws.ts +5 -3
  31. package/src/lib/get-timer.ts +7 -7
  32. package/src/lib/handlers/connack.ts +1 -1
  33. package/src/lib/handlers/index.ts +8 -9
  34. package/src/lib/is-browser.ts +25 -2
  35. package/src/mqtt.ts +2 -2
  36. package/build/lib/PingTimer.d.ts +0 -12
  37. package/build/lib/PingTimer.js +0 -40
  38. package/build/lib/PingTimer.js.map +0 -1
  39. package/src/lib/PingTimer.ts +0 -56
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.5" };
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 (packet.cmd !== "pingresp") {
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":
@@ -17267,7 +17267,18 @@ var mqtt = (() => {
17267
17267
  init_navigator();
17268
17268
  Object.defineProperty(exports5, "__esModule", { value: true });
17269
17269
  exports5.isReactNativeBrowser = exports5.isWebWorker = void 0;
17270
- var isStandardBrowserEnv = () => typeof window !== "undefined" && typeof window.document !== "undefined";
17270
+ var isStandardBrowserEnv = () => {
17271
+ var _a;
17272
+ if (typeof window !== "undefined") {
17273
+ const electronRenderCheck = ((_a = navigator === null || navigator === void 0 ? void 0 : navigator.userAgent) === null || _a === void 0 ? void 0 : _a.toLowerCase().indexOf(" electron/")) > -1;
17274
+ if (electronRenderCheck && (process_exports === null || process_exports === void 0 ? void 0 : process_exports.versions)) {
17275
+ const electronMainCheck = Object.prototype.hasOwnProperty.call(process_exports.versions, "electron");
17276
+ return !electronMainCheck;
17277
+ }
17278
+ return typeof window.document !== "undefined";
17279
+ }
17280
+ return false;
17281
+ };
17271
17282
  var isWebWorkerEnv = () => {
17272
17283
  var _a, _b;
17273
17284
  return Boolean(typeof self === "object" && ((_b = (_a = self === null || self === void 0 ? void 0 : self.constructor) === null || _a === void 0 ? void 0 : _a.name) === null || _b === void 0 ? void 0 : _b.includes("WorkerGlobalScope")));
@@ -17593,12 +17604,12 @@ var mqtt = (() => {
17593
17604
  var is_browser_1 = __importStar(require_is_browser());
17594
17605
  var worker_timers_1 = require_bundle3();
17595
17606
  var workerTimer = {
17596
- set: worker_timers_1.setTimeout,
17597
- clear: worker_timers_1.clearTimeout
17607
+ set: worker_timers_1.setInterval,
17608
+ clear: worker_timers_1.clearInterval
17598
17609
  };
17599
17610
  var nativeTimer = {
17600
- set: (func, time) => setTimeout(func, time),
17601
- clear: (timerId) => clearTimeout(timerId)
17611
+ set: (func, time) => setInterval(func, time),
17612
+ clear: (timerId) => clearInterval(timerId)
17602
17613
  };
17603
17614
  var getTimer = (variant) => {
17604
17615
  switch (variant) {
@@ -17618,9 +17629,9 @@ var mqtt = (() => {
17618
17629
  }
17619
17630
  });
17620
17631
 
17621
- // build/lib/PingTimer.js
17622
- var require_PingTimer = __commonJS({
17623
- "build/lib/PingTimer.js"(exports5) {
17632
+ // build/lib/KeepaliveManager.js
17633
+ var require_KeepaliveManager = __commonJS({
17634
+ "build/lib/KeepaliveManager.js"(exports5) {
17624
17635
  "use strict";
17625
17636
  init_buffer2();
17626
17637
  init_process2();
@@ -17630,13 +17641,21 @@ var mqtt = (() => {
17630
17641
  };
17631
17642
  Object.defineProperty(exports5, "__esModule", { value: true });
17632
17643
  var get_timer_1 = __importDefault(require_get_timer());
17633
- var PingTimer = class {
17634
- 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) {
17635
17655
  this.destroyed = false;
17636
- this.keepalive = keepalive * 1e3;
17637
- this.checkPing = checkPing;
17656
+ this.client = client;
17638
17657
  this.timer = (0, get_timer_1.default)(variant);
17639
- this.reschedule();
17658
+ this.setKeepalive(client.options.keepalive);
17640
17659
  }
17641
17660
  clear() {
17642
17661
  if (this.timerId) {
@@ -17644,6 +17663,15 @@ var mqtt = (() => {
17644
17663
  this.timerId = null;
17645
17664
  }
17646
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
+ }
17647
17675
  destroy() {
17648
17676
  this.clear();
17649
17677
  this.destroyed = true;
@@ -17653,16 +17681,24 @@ var mqtt = (() => {
17653
17681
  return;
17654
17682
  }
17655
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);
17656
17688
  this.timerId = this.timer.set(() => {
17657
17689
  if (this.destroyed) {
17658
17690
  return;
17659
17691
  }
17660
- this.checkPing();
17661
- this.reschedule();
17662
- }, 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);
17663
17699
  }
17664
17700
  };
17665
- exports5.default = PingTimer;
17701
+ exports5.default = KeepaliveManager;
17666
17702
  }
17667
17703
  });
17668
17704
 
@@ -17720,7 +17756,7 @@ var mqtt = (() => {
17720
17756
  var handlers_1 = __importDefault(require_handlers());
17721
17757
  var shared_1 = require_shared();
17722
17758
  var TypedEmitter_1 = require_TypedEmitter();
17723
- var PingTimer_1 = __importDefault(require_PingTimer());
17759
+ var KeepaliveManager_1 = __importDefault(require_KeepaliveManager());
17724
17760
  var is_browser_1 = __importStar(require_is_browser());
17725
17761
  var setImmediate2 = globalThis.setImmediate || ((...args) => {
17726
17762
  const callback = args.shift();
@@ -17784,7 +17820,7 @@ var mqtt = (() => {
17784
17820
  this.queueQoSZero = options.queueQoSZero === void 0 ? true : options.queueQoSZero;
17785
17821
  this._resubscribeTopics = {};
17786
17822
  this.messageIdToTopic = {};
17787
- this.pingTimer = null;
17823
+ this.keepaliveManager = null;
17788
17824
  this.connected = false;
17789
17825
  this.disconnecting = false;
17790
17826
  this.reconnecting = false;
@@ -17841,7 +17877,7 @@ var mqtt = (() => {
17841
17877
  this.connected = false;
17842
17878
  this.log("close :: clearing connackTimer");
17843
17879
  clearTimeout(this.connackTimer);
17844
- this._destroyPingTimer();
17880
+ this._destroyKeepaliveManager();
17845
17881
  if (this.topicAliasRecv) {
17846
17882
  this.topicAliasRecv.clear();
17847
17883
  }
@@ -18470,7 +18506,7 @@ var mqtt = (() => {
18470
18506
  this._clearReconnect();
18471
18507
  this._setupReconnect();
18472
18508
  }
18473
- this._destroyPingTimer();
18509
+ this._destroyKeepaliveManager();
18474
18510
  if (done && !this.connected) {
18475
18511
  this.log("_cleanUp :: (%s) :: removing stream `done` callback `close` listener", this.options.clientId);
18476
18512
  this.stream.removeListener("close", done);
@@ -18631,47 +18667,37 @@ var mqtt = (() => {
18631
18667
  cb(new Error("No connection to broker"));
18632
18668
  }
18633
18669
  }
18634
- _setupPingTimer() {
18635
- this.log("_setupPingTimer :: keepalive %d (seconds)", this.options.keepalive);
18636
- if (!this.pingTimer && this.options.keepalive) {
18637
- this.pingTimer = new PingTimer_1.default(this.options.keepalive, () => {
18638
- this._checkPing();
18639
- }, this.options.timerVariant);
18640
- 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);
18641
18674
  }
18642
18675
  }
18643
- _destroyPingTimer() {
18644
- if (this.pingTimer) {
18645
- this.log("_destroyPingTimer :: destroying ping timer");
18646
- this.pingTimer.destroy();
18647
- this.pingTimer = null;
18676
+ _destroyKeepaliveManager() {
18677
+ if (this.keepaliveManager) {
18678
+ this.log("_destroyKeepaliveManager :: destroying keepalive manager");
18679
+ this.keepaliveManager.destroy();
18680
+ this.keepaliveManager = null;
18648
18681
  }
18649
18682
  }
18650
- _shiftPingInterval() {
18651
- if (this.pingTimer && this.options.keepalive && this.options.reschedulePings) {
18683
+ reschedulePing() {
18684
+ if (this.keepaliveManager && this.options.keepalive && this.options.reschedulePings) {
18652
18685
  this._reschedulePing();
18653
18686
  }
18654
18687
  }
18655
18688
  _reschedulePing() {
18656
18689
  this.log("_reschedulePing :: rescheduling ping");
18657
- this.pingTimer.reschedule();
18658
- }
18659
- _checkPing() {
18660
- this.log("_checkPing :: checking ping...");
18661
- const timeSincePing = Date.now() - this.pingResp - 100;
18662
- if (timeSincePing <= this.options.keepalive * 1e3) {
18663
- this.log("_checkPing :: ping response received in time");
18664
- this._sendPing();
18665
- } else {
18666
- this.emit("error", new Error("Keepalive timeout"));
18667
- this.log("_checkPing :: calling _cleanUp with force true");
18668
- this._cleanUp(true);
18669
- }
18690
+ this.keepaliveManager.reschedule();
18670
18691
  }
18671
- _sendPing() {
18692
+ sendPing() {
18672
18693
  this.log("_sendPing :: sending pingreq");
18673
18694
  this._sendPacket({ cmd: "pingreq" });
18674
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
+ }
18675
18701
  _resubscribe() {
18676
18702
  this.log("_resubscribe");
18677
18703
  const _resubscribeTopicsKeys = Object.keys(this._resubscribeTopics);
@@ -18704,7 +18730,7 @@ var mqtt = (() => {
18704
18730
  }
18705
18731
  this.connackPacket = packet;
18706
18732
  this.messageIdProvider.clear();
18707
- this._setupPingTimer();
18733
+ this._setupKeepaliveManager();
18708
18734
  this.connected = true;
18709
18735
  const startStreamProcess = () => {
18710
18736
  let outStore = this.outgoingStore.createStream();
@@ -21147,9 +21173,6 @@ var mqtt = (() => {
21147
21173
  }
21148
21174
  function setDefaultOpts(opts) {
21149
21175
  const options = opts;
21150
- if (!opts.hostname) {
21151
- options.hostname = "localhost";
21152
- }
21153
21176
  if (!opts.port) {
21154
21177
  if (opts.protocol === "wss") {
21155
21178
  options.port = 443;
@@ -21220,6 +21243,7 @@ var mqtt = (() => {
21220
21243
  var streamBuilder = (client, opts) => {
21221
21244
  debug("streamBuilder");
21222
21245
  const options = setDefaultOpts(opts);
21246
+ options.hostname = options.hostname || options.host || "localhost";
21223
21247
  const url = buildUrl(options, client);
21224
21248
  const socket = createWebSocket(client, url, options);
21225
21249
  const webSocketStream = ws_1.default.createWebSocketStream(socket, options.wsOptions);
@@ -21545,7 +21569,7 @@ var mqtt = (() => {
21545
21569
  return mod && mod.__esModule ? mod : { "default": mod };
21546
21570
  };
21547
21571
  Object.defineProperty(exports5, "__esModule", { value: true });
21548
- 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;
21549
21573
  var client_1 = __importDefault(require_client());
21550
21574
  exports5.MqttClient = client_1.default;
21551
21575
  var default_message_id_provider_1 = __importDefault(require_default_message_id_provider());
@@ -21559,8 +21583,8 @@ var mqtt = (() => {
21559
21583
  Object.defineProperty(exports5, "connectAsync", { enumerable: true, get: function() {
21560
21584
  return connect_1.connectAsync;
21561
21585
  } });
21562
- var PingTimer_1 = __importDefault(require_PingTimer());
21563
- exports5.PingTimer = PingTimer_1.default;
21586
+ var KeepaliveManager_1 = __importDefault(require_KeepaliveManager());
21587
+ exports5.KeepaliveManager = KeepaliveManager_1.default;
21564
21588
  exports5.Client = client_1.default;
21565
21589
  __exportStar(require_client(), exports5);
21566
21590
  __exportStar(require_shared(), exports5);