@mysetup/mqtt 1.2.0 → 1.4.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/index.js CHANGED
@@ -1,17 +1 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./mqttClient"), exports);
1
+ export * from "./mqttClient";
@@ -1,35 +1,21 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.getMqttClient = exports.mqttOnMessage = exports.mqttDisconnect = exports.mqttUnSub = exports.mqttSub = exports.mqttCreateConnection = void 0;
15
- var mqtt_1 = require("mqtt");
16
- var logger_1 = require("@mysetup/logger");
17
- var cache_1 = require("@mysetup/cache");
18
- var serverConnectOptions = {
1
+ import { connect } from "mqtt";
2
+ import { logger } from "@mysetup/logger";
3
+ import { setCacheData } from "@mysetup/cache";
4
+ const serverConnectOptions = {
19
5
  protocol: "mqtt",
20
6
  port: 1883,
21
7
  host: "test.mosquitto.org",
22
8
  };
23
- var webConnectOptions = {
9
+ const webConnectOptions = {
24
10
  protocol: "wss",
25
11
  port: 8081,
26
12
  host: "test.mosquitto.org",
27
13
  };
28
- var mqttClientId = "";
29
- var checkInitConnect = function (props) {
30
- var _a = props.option === "web" ? webConnectOptions : serverConnectOptions, protocol = _a.protocol, host = _a.host, port = _a.port;
31
- var connectUrl = "".concat(protocol, "://").concat(host, ":").concat(port);
32
- var client = (0, mqtt_1.connect)(connectUrl, {
14
+ let mqttClientId = "";
15
+ const checkInitConnect = (props) => {
16
+ const { protocol, host, port } = props.option === "web" ? webConnectOptions : serverConnectOptions;
17
+ const connectUrl = `${protocol}://${host}:${port}`;
18
+ const client = connect(connectUrl, {
33
19
  clientId: props.value,
34
20
  clean: true,
35
21
  rejectUnauthorized: false,
@@ -38,112 +24,110 @@ var checkInitConnect = function (props) {
38
24
  });
39
25
  mqttClientId = props.value;
40
26
  client.setMaxListeners(100);
41
- client.on("connect", function () {
42
- logger_1.logger.info("MQTT connection successful");
43
- (0, cache_1.setCacheData)("mqttClientStatus", "connect");
27
+ client.on("connect", () => {
28
+ logger.info("MQTT connection successful");
29
+ setCacheData("mqttClientStatus", "connect");
44
30
  mqttSub(props.topicList, props.messageCallback, client);
45
31
  });
46
- client.on("error", function (err) {
47
- logger_1.logger.error("MQTT Connection error: ".concat(JSON.stringify(err)));
48
- (0, cache_1.setCacheData)("mqttClientStatus", "error");
32
+ client.on("error", (err) => {
33
+ logger.error(`MQTT Connection error: ${JSON.stringify(err)}`);
34
+ setCacheData("mqttClientStatus", "error");
49
35
  mqttDisconnect(props, true, client);
50
36
  });
51
- client.on("reconnect", function () {
52
- (0, cache_1.setCacheData)("mqttClientStatus", "reconnect");
53
- logger_1.logger.info("MQTT Reconnecting");
37
+ client.on("reconnect", () => {
38
+ setCacheData("mqttClientStatus", "reconnect");
39
+ logger.info("MQTT Reconnecting");
54
40
  });
55
- client.on("close", function () {
56
- (0, cache_1.setCacheData)("mqttClientStatus", "close");
57
- logger_1.logger.info("MQTT connection closed");
41
+ client.on("close", () => {
42
+ setCacheData("mqttClientStatus", "close");
43
+ logger.info("MQTT connection closed");
58
44
  });
59
- client.on("offline", function () {
60
- (0, cache_1.setCacheData)("mqttClientStatus", "offline");
61
- logger_1.logger.info("MQTT connection offline");
45
+ client.on("offline", () => {
46
+ setCacheData("mqttClientStatus", "offline");
47
+ logger.info("MQTT connection offline");
62
48
  });
63
49
  return client;
64
50
  };
65
- var mqttSub = function (topicList, messageCallback, client) {
51
+ const mqttSub = (topicList, messageCallback, client) => {
66
52
  try {
67
- client === null || client === void 0 ? void 0 : client.subscribe(topicList, { qos: 0 }, function (error, granted) {
53
+ client === null || client === void 0 ? void 0 : client.subscribe(topicList, { qos: 0 }, (error, granted) => {
68
54
  if (error) {
69
- logger_1.logger.error("Subscribe to topics error: ".concat(error.message));
55
+ logger.error(`Subscribe to topics error: ${error.message}`);
70
56
  }
71
- var grantedList = [];
72
- granted === null || granted === void 0 ? void 0 : granted.forEach(function (element) {
57
+ const grantedList = [];
58
+ granted === null || granted === void 0 ? void 0 : granted.forEach((element) => {
73
59
  grantedList.push(element.topic);
74
- logger_1.logger.info("Subscribed to topics: ".concat(element.topic));
60
+ logger.info(`Subscribed to topics: ${element.topic}`);
75
61
  });
76
- (0, cache_1.setCacheData)("subscribedTopic", grantedList);
62
+ setCacheData("subscribedTopic", grantedList);
77
63
  messageCallback && mqttOnMessage(messageCallback, client);
78
64
  });
79
65
  }
80
66
  catch (error) {
81
- logger_1.logger.error("Subscribed error ".concat(JSON.stringify(error)));
67
+ logger.error(`Subscribed error ${JSON.stringify(error)}`);
82
68
  }
83
69
  };
84
- exports.mqttSub = mqttSub;
85
- var mqttUnSub = function (topicList, client) {
70
+ const mqttUnSub = (topicList, client) => {
86
71
  try {
87
- client === null || client === void 0 ? void 0 : client.unsubscribe(topicList, function (error) {
72
+ client === null || client === void 0 ? void 0 : client.unsubscribe(topicList, (error) => {
88
73
  if (error) {
89
- logger_1.logger.error("Unsubscribe to topics error: ".concat(error.message));
74
+ logger.error(`Unsubscribe to topics error: ${error.message}`);
90
75
  return;
91
76
  }
92
- topicList.forEach(function (topic) {
93
- logger_1.logger.info("unsubscribed to topic: ".concat(topic));
77
+ topicList.forEach((topic) => {
78
+ logger.info(`unsubscribed to topic: ${topic}`);
94
79
  });
95
80
  });
96
81
  }
97
82
  catch (error) {
98
- logger_1.logger.error("unsubscribed error: ".concat(JSON.stringify(error)));
83
+ logger.error(`unsubscribed error: ${JSON.stringify(error)}`);
99
84
  }
100
85
  };
101
- exports.mqttUnSub = mqttUnSub;
102
- var mqttDisconnect = function (props, reinit, client) {
86
+ const mqttDisconnect = (props, reinit, client) => {
103
87
  try {
104
- client === null || client === void 0 ? void 0 : client.end(false, function () {
105
- logger_1.logger.info("MQTT disconnected successfully");
88
+ client === null || client === void 0 ? void 0 : client.end(false, () => {
89
+ logger.info("MQTT disconnected successfully");
106
90
  if (reinit) {
107
- cachedConnection(__assign(__assign({}, props), { value: "client".concat(Math.random().toString(36).substring(7)) }));
91
+ cachedConnection({
92
+ ...props,
93
+ value: `client${Math.random().toString(36).substring(7)}`,
94
+ });
108
95
  }
109
96
  });
110
97
  }
111
98
  catch (error) {
112
- logger_1.logger.error("disconnect error: ".concat(JSON.stringify(error)));
99
+ logger.error(`disconnect error: ${JSON.stringify(error)}`);
113
100
  }
114
101
  };
115
- exports.mqttDisconnect = mqttDisconnect;
116
- var mqttOnMessage = function (messageCallback, client) {
102
+ const mqttOnMessage = (messageCallback, client) => {
117
103
  try {
118
- client === null || client === void 0 ? void 0 : client.on("message", function (topic, message) {
104
+ client === null || client === void 0 ? void 0 : client.on("message", (topic, message) => {
119
105
  messageCallback(topic, message.toString());
120
106
  });
121
107
  }
122
108
  catch (error) {
123
- logger_1.logger.error("MQTT onmessage error: ".concat(JSON.stringify(error)));
109
+ logger.error(`MQTT onmessage error: ${JSON.stringify(error)}`);
124
110
  }
125
111
  };
126
- exports.mqttOnMessage = mqttOnMessage;
127
- var memoizedDb = function () {
128
- var cache = {};
129
- return function (props) {
112
+ const memoizedDb = () => {
113
+ const cache = {};
114
+ return (props) => {
130
115
  if (props.value in cache) {
131
- logger_1.logger.info("fetching mqtt cache");
116
+ logger.info("fetching mqtt cache");
132
117
  return cache[props.value];
133
118
  }
134
- logger_1.logger.info("Creating new mqtt cache");
135
- var result = checkInitConnect(props);
119
+ logger.info("Creating new mqtt cache");
120
+ const result = checkInitConnect(props);
136
121
  cache[props.value] = result;
137
122
  return result;
138
123
  };
139
124
  };
140
125
  // returned function from memoizedDb
141
- var cachedConnection = memoizedDb();
142
- var mqttCreateConnection = function (props) {
126
+ const cachedConnection = memoizedDb();
127
+ const mqttCreateConnection = (props) => {
143
128
  return cachedConnection(props);
144
129
  };
145
- exports.mqttCreateConnection = mqttCreateConnection;
146
- var getMqttClient = function () {
130
+ const getMqttClient = () => {
147
131
  return mqttClientId;
148
132
  };
149
- exports.getMqttClient = getMqttClient;
133
+ export { mqttCreateConnection, mqttSub, mqttUnSub, mqttDisconnect, mqttOnMessage, getMqttClient, };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mysetup/mqtt",
3
- "version": "1.2.0",
3
+ "version": "1.4.0",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "files": [
@@ -15,9 +15,9 @@
15
15
  "./package.json": "./package.json"
16
16
  },
17
17
  "scripts": {
18
+ "build": "rm -rf ./dist && tsc",
18
19
  "lint": "eslint .",
19
20
  "typecheck": "tsc --noEmit",
20
- "build": "rm -rf ./dist && tsc",
21
21
  "format": "prettier --write \"**/*.{ts,tsx,md,js}\"",
22
22
  "checks": "pnpm typecheck && pnpm lint",
23
23
  "clean": "rm -rf node_modules .swc dist pnpm-lock.yaml && echo \"✅ Successfully removed \""