mqtt 5.0.0-beta.4 → 5.0.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 (38) hide show
  1. package/README.md +35 -0
  2. package/build/package.json +1 -1
  3. package/build/src/lib/client.d.ts +32 -78
  4. package/build/src/lib/client.js +70 -13
  5. package/build/src/lib/client.js.map +1 -1
  6. package/build/src/lib/connect/ali.js.map +1 -1
  7. package/build/src/lib/connect/index.d.ts +4 -0
  8. package/build/src/lib/connect/index.js +36 -1
  9. package/build/src/lib/connect/index.js.map +1 -1
  10. package/build/src/lib/connect/tcp.js.map +1 -1
  11. package/build/src/lib/connect/tls.js.map +1 -1
  12. package/build/src/lib/connect/ws.js +1 -1
  13. package/build/src/lib/connect/ws.js.map +1 -1
  14. package/build/src/lib/connect/wx.js.map +1 -1
  15. package/build/src/lib/handlers/publish.js.map +1 -1
  16. package/build/src/lib/shared.d.ts +5 -4
  17. package/build/src/lib/unique-message-id-provider.d.ts +3 -2
  18. package/build/src/lib/unique-message-id-provider.js.map +1 -1
  19. package/build/src/mqtt.d.ts +4 -2
  20. package/build/src/mqtt.js +18 -2
  21. package/build/src/mqtt.js.map +1 -1
  22. package/build/tsconfig.build.tsbuildinfo +1 -1
  23. package/dist/mqtt.js +126 -18
  24. package/dist/mqtt.min.js +4 -4
  25. package/doc/subscribe.txt +1 -1
  26. package/package.json +1 -1
  27. package/src/lib/client.ts +202 -170
  28. package/src/lib/connect/ali.ts +2 -2
  29. package/src/lib/connect/index.ts +66 -5
  30. package/src/lib/connect/tcp.ts +1 -1
  31. package/src/lib/connect/tls.ts +1 -3
  32. package/src/lib/connect/ws.ts +4 -5
  33. package/src/lib/connect/wx.ts +2 -2
  34. package/src/lib/default-message-id-provider.ts +2 -2
  35. package/src/lib/handlers/publish.ts +2 -2
  36. package/src/lib/shared.ts +4 -4
  37. package/src/lib/unique-message-id-provider.ts +3 -2
  38. package/src/mqtt.ts +4 -1
package/dist/mqtt.js CHANGED
@@ -221,6 +221,7 @@ class MqttClient extends TypedEmitter_1.TypedEventEmitter {
221
221
  return this.messageIdProvider.getLastAllocated();
222
222
  }
223
223
  connect() {
224
+ var _a;
224
225
  const writable = new readable_stream_1.Writable();
225
226
  const parser = mqtt_packet_1.default.parser(this.options);
226
227
  let completeParse = null;
@@ -285,8 +286,20 @@ class MqttClient extends TypedEmitter_1.TypedEventEmitter {
285
286
  this.emit('close');
286
287
  });
287
288
  this.log('connect: sending packet `connect`');
288
- const connectPacket = Object.create(this.options);
289
- connectPacket.cmd = 'connect';
289
+ const connectPacket = {
290
+ cmd: 'connect',
291
+ protocolId: this.options.protocolId,
292
+ protocolVersion: this.options.protocolVersion,
293
+ clean: this.options.clean,
294
+ clientId: this.options.clientId,
295
+ keepalive: this.options.keepalive,
296
+ username: this.options.username,
297
+ password: this.options.password,
298
+ properties: this.options.properties,
299
+ };
300
+ if (this.options.will) {
301
+ connectPacket.will = Object.assign(Object.assign({}, this.options.will), { payload: (_a = this.options.will) === null || _a === void 0 ? void 0 : _a.payload });
302
+ }
290
303
  if (this.topicAliasRecv) {
291
304
  if (!connectPacket.properties) {
292
305
  connectPacket.properties = {};
@@ -324,8 +337,9 @@ class MqttClient extends TypedEmitter_1.TypedEventEmitter {
324
337
  const { options } = this;
325
338
  if (typeof opts === 'function') {
326
339
  callback = opts;
327
- opts = {};
340
+ opts = null;
328
341
  }
342
+ opts = opts || {};
329
343
  const defaultOpts = {
330
344
  qos: 0,
331
345
  retain: false,
@@ -386,6 +400,18 @@ class MqttClient extends TypedEmitter_1.TypedEventEmitter {
386
400
  }
387
401
  return this;
388
402
  }
403
+ publishAsync(topic, message, opts) {
404
+ return new Promise((resolve, reject) => {
405
+ this.publish(topic, message, opts, (err) => {
406
+ if (err) {
407
+ reject(err);
408
+ }
409
+ else {
410
+ resolve();
411
+ }
412
+ });
413
+ });
414
+ }
389
415
  subscribe(topicObject, opts, callback) {
390
416
  const version = this.options.protocolVersion;
391
417
  if (typeof opts === 'function') {
@@ -519,6 +545,18 @@ class MqttClient extends TypedEmitter_1.TypedEventEmitter {
519
545
  }
520
546
  return this;
521
547
  }
548
+ subscribeAsync(topicObject, opts) {
549
+ return new Promise((resolve, reject) => {
550
+ this.subscribe(topicObject, opts, (err, granted) => {
551
+ if (err) {
552
+ reject(err);
553
+ }
554
+ else {
555
+ resolve(granted);
556
+ }
557
+ });
558
+ });
559
+ }
522
560
  unsubscribe(topic, opts, callback) {
523
561
  if (typeof topic === 'string') {
524
562
  topic = [topic];
@@ -578,26 +616,33 @@ class MqttClient extends TypedEmitter_1.TypedEventEmitter {
578
616
  }
579
617
  return this;
580
618
  }
619
+ unsubscribeAsync(topic, opts) {
620
+ return new Promise((resolve, reject) => {
621
+ this.unsubscribe(topic, opts, (err) => {
622
+ if (err) {
623
+ reject(err);
624
+ }
625
+ else {
626
+ resolve();
627
+ }
628
+ });
629
+ });
630
+ }
581
631
  end(force, opts, cb) {
582
632
  this.log('end :: (%s)', this.options.clientId);
583
633
  if (force == null || typeof force !== 'boolean') {
584
- cb = (opts || this.noop);
634
+ cb = cb || opts;
585
635
  opts = force;
586
636
  force = false;
587
- if (typeof opts !== 'object') {
588
- cb = opts;
589
- opts = null;
590
- if (typeof cb !== 'function') {
591
- cb = this.noop;
592
- }
593
- }
594
637
  }
595
638
  if (typeof opts !== 'object') {
596
- cb = opts;
639
+ cb = cb || opts;
597
640
  opts = null;
598
641
  }
599
642
  this.log('end :: cb? %s', !!cb);
600
- cb = cb || this.noop;
643
+ if (!cb || typeof cb !== 'function') {
644
+ cb = this.noop;
645
+ }
601
646
  const closeStores = () => {
602
647
  this.log('end :: closeStores: closing incoming and outgoing stores');
603
648
  this.disconnected = true;
@@ -639,6 +684,18 @@ class MqttClient extends TypedEmitter_1.TypedEventEmitter {
639
684
  }
640
685
  return this;
641
686
  }
687
+ endAsync(force, opts) {
688
+ return new Promise((resolve, reject) => {
689
+ this.end(force, opts, (err) => {
690
+ if (err) {
691
+ reject(err);
692
+ }
693
+ else {
694
+ resolve();
695
+ }
696
+ });
697
+ });
698
+ }
642
699
  removeOutgoingMessage(messageId) {
643
700
  if (this.outgoing[messageId]) {
644
701
  const { cb } = this.outgoing[messageId];
@@ -1282,10 +1339,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
1282
1339
  return (mod && mod.__esModule) ? mod : { "default": mod };
1283
1340
  };
1284
1341
  Object.defineProperty(exports, "__esModule", { value: true });
1342
+ exports.connectAsync = void 0;
1343
+ const debug_1 = __importDefault(require("debug"));
1285
1344
  const url_1 = __importDefault(require("url"));
1286
1345
  const client_1 = __importDefault(require("../client"));
1287
1346
  const is_browser_1 = __importDefault(require("../is-browser"));
1288
- const debug_1 = __importDefault(require("debug"));
1289
1347
  const debug = (0, debug_1.default)('mqttjs');
1290
1348
  const protocols = {};
1291
1349
  if (!is_browser_1.default) {
@@ -1409,6 +1467,40 @@ function connect(brokerUrl, opts) {
1409
1467
  });
1410
1468
  return client;
1411
1469
  }
1470
+ function connectAsync(brokerUrl, opts, allowRetries = true) {
1471
+ return new Promise((resolve, reject) => {
1472
+ const client = connect(brokerUrl, opts);
1473
+ const promiseResolutionListeners = {
1474
+ connect: (connack) => {
1475
+ removePromiseResolutionListeners();
1476
+ resolve(client);
1477
+ },
1478
+ end: () => {
1479
+ removePromiseResolutionListeners();
1480
+ resolve(client);
1481
+ },
1482
+ error: (err) => {
1483
+ removePromiseResolutionListeners();
1484
+ client.end();
1485
+ reject(err);
1486
+ },
1487
+ };
1488
+ if (allowRetries === false) {
1489
+ promiseResolutionListeners.close = () => {
1490
+ promiseResolutionListeners.error(new Error("Couldn't connect to server"));
1491
+ };
1492
+ }
1493
+ function removePromiseResolutionListeners() {
1494
+ Object.keys(promiseResolutionListeners).forEach((eventName) => {
1495
+ client.off(eventName, promiseResolutionListeners[eventName]);
1496
+ });
1497
+ }
1498
+ Object.keys(promiseResolutionListeners).forEach((eventName) => {
1499
+ client.on(eventName, promiseResolutionListeners[eventName]);
1500
+ });
1501
+ });
1502
+ }
1503
+ exports.connectAsync = connectAsync;
1412
1504
  exports.default = connect;
1413
1505
 
1414
1506
  },{"../client":2,"../is-browser":16,"./ali":3,"./tcp":5,"./tls":6,"./ws":7,"./wx":8,"debug":30,"url":137}],5:[function(require,module,exports){
@@ -1571,7 +1663,7 @@ const streamBuilder = (client, opts) => {
1571
1663
  const url = buildUrl(options, client);
1572
1664
  const socket = createWebSocket(client, url, options);
1573
1665
  const webSocketStream = ws_1.default.createWebSocketStream(socket, options.wsOptions);
1574
- webSocketStream.url = url;
1666
+ webSocketStream['url'] = url;
1575
1667
  socket.on('close', () => {
1576
1668
  webSocketStream.destroy();
1577
1669
  });
@@ -2516,6 +2608,18 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
2516
2608
  if (k2 === undefined) k2 = k;
2517
2609
  o[k2] = m[k];
2518
2610
  }));
2611
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
2612
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
2613
+ }) : function(o, v) {
2614
+ o["default"] = v;
2615
+ });
2616
+ var __importStar = (this && this.__importStar) || function (mod) {
2617
+ if (mod && mod.__esModule) return mod;
2618
+ var result = {};
2619
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
2620
+ __setModuleDefault(result, mod);
2621
+ return result;
2622
+ };
2519
2623
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
2520
2624
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
2521
2625
  };
@@ -2523,7 +2627,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
2523
2627
  return (mod && mod.__esModule) ? mod : { "default": mod };
2524
2628
  };
2525
2629
  Object.defineProperty(exports, "__esModule", { value: true });
2526
- exports.UniqueMessageIdProvider = exports.DefaultMessageIdProvider = exports.Store = exports.MqttClient = exports.connect = exports.Client = void 0;
2630
+ exports.ReasonCodes = exports.UniqueMessageIdProvider = exports.DefaultMessageIdProvider = exports.Store = exports.MqttClient = exports.connectAsync = exports.connect = exports.Client = void 0;
2527
2631
  const client_1 = __importDefault(require("./lib/client"));
2528
2632
  exports.MqttClient = client_1.default;
2529
2633
  const default_message_id_provider_1 = __importDefault(require("./lib/default-message-id-provider"));
@@ -2532,12 +2636,16 @@ const unique_message_id_provider_1 = __importDefault(require("./lib/unique-messa
2532
2636
  exports.UniqueMessageIdProvider = unique_message_id_provider_1.default;
2533
2637
  const store_1 = __importDefault(require("./lib/store"));
2534
2638
  exports.Store = store_1.default;
2535
- const connect_1 = __importDefault(require("./lib/connect"));
2639
+ const connect_1 = __importStar(require("./lib/connect"));
2536
2640
  exports.connect = connect_1.default;
2641
+ Object.defineProperty(exports, "connectAsync", { enumerable: true, get: function () { return connect_1.connectAsync; } });
2537
2642
  exports.Client = client_1.default;
2538
2643
  __exportStar(require("./lib/client"), exports);
2644
+ __exportStar(require("./lib/shared"), exports);
2645
+ var ack_1 = require("./lib/handlers/ack");
2646
+ Object.defineProperty(exports, "ReasonCodes", { enumerable: true, get: function () { return ack_1.ReasonCodes; } });
2539
2647
 
2540
- },{"./lib/client":2,"./lib/connect":4,"./lib/default-message-id-provider":9,"./lib/store":18,"./lib/unique-message-id-provider":21}],24:[function(require,module,exports){
2648
+ },{"./lib/client":2,"./lib/connect":4,"./lib/default-message-id-provider":9,"./lib/handlers/ack":10,"./lib/shared":17,"./lib/store":18,"./lib/unique-message-id-provider":21}],24:[function(require,module,exports){
2541
2649
  /*globals self, window */
2542
2650
  "use strict"
2543
2651