@mysetup/mqtt 1.6.0 → 1.7.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.
@@ -0,0 +1,17 @@
1
+ import type { MqttClient } from "mqtt";
2
+ export type { MqttClient };
3
+ type MQttOptionProps = "web" | "server";
4
+ interface ConnectionProps {
5
+ value: string;
6
+ option: MQttOptionProps;
7
+ topicList: string[];
8
+ messageCallback?: (topic: string, message: string) => void;
9
+ }
10
+ declare const mqttSub: (topicList: string[], messageCallback?: (topic: string, message: string) => void, client?: MqttClient) => void;
11
+ declare const mqttUnSub: (topicList: string[], client?: MqttClient) => void;
12
+ declare const mqttDisconnect: (props: ConnectionProps, reinit: boolean, client?: MqttClient) => void;
13
+ declare const mqttOnMessage: (messageCallback: (topic: string, message: string) => void, client?: MqttClient) => void;
14
+ declare const mqttCreateConnection: (props: ConnectionProps) => MqttClient;
15
+ declare const getMqttClient: () => string;
16
+ export { mqttCreateConnection, mqttSub, mqttUnSub, mqttDisconnect, mqttOnMessage, getMqttClient, };
17
+ //# sourceMappingURL=mqttClient.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mqttClient.d.ts","sourceRoot":"","sources":["../mqttClient.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAGvC,YAAY,EAAE,UAAU,EAAE,CAAC;AAE3B,KAAK,eAAe,GAAG,KAAK,GAAG,QAAQ,CAAC;AAcxC,UAAU,eAAe;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,eAAe,CAAC;IACxB,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,eAAe,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;CAC9D;AAkDD,QAAA,MAAM,OAAO,cACE,MAAM,EAAE,oBACD,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,IAAI,WACjD,UAAU,SAoBtB,CAAC;AAEF,QAAA,MAAM,SAAS,cAAe,MAAM,EAAE,WAAW,UAAU,SAc1D,CAAC;AAEF,QAAA,MAAM,cAAc,UACT,eAAe,UACd,OAAO,WACN,UAAU,SAetB,CAAC;AAEF,QAAA,MAAM,aAAa,oBACE,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,IAAI,WAChD,UAAU,SAStB,CAAC;AAmBF,QAAA,MAAM,oBAAoB,UAAW,eAAe,eAEnD,CAAC;AAEF,QAAA,MAAM,aAAa,cAElB,CAAC;AAEF,OAAO,EACH,oBAAoB,EACpB,OAAO,EACP,SAAS,EACT,cAAc,EACd,aAAa,EACb,aAAa,GAChB,CAAC"}
@@ -0,0 +1,149 @@
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 = {
19
+ protocol: "mqtt",
20
+ port: 1883,
21
+ host: "test.mosquitto.org",
22
+ };
23
+ var webConnectOptions = {
24
+ protocol: "wss",
25
+ port: 8081,
26
+ host: "test.mosquitto.org",
27
+ };
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, {
33
+ clientId: props.value,
34
+ clean: true,
35
+ rejectUnauthorized: false,
36
+ resubscribe: true,
37
+ keepalive: 300,
38
+ });
39
+ mqttClientId = props.value;
40
+ client.setMaxListeners(100);
41
+ client.on("connect", function () {
42
+ logger_1.logger.info("MQTT connection successful");
43
+ (0, cache_1.setCacheData)("mqttClientStatus", "connect");
44
+ mqttSub(props.topicList, props.messageCallback, client);
45
+ });
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");
49
+ mqttDisconnect(props, true, client);
50
+ });
51
+ client.on("reconnect", function () {
52
+ (0, cache_1.setCacheData)("mqttClientStatus", "reconnect");
53
+ logger_1.logger.info("MQTT Reconnecting");
54
+ });
55
+ client.on("close", function () {
56
+ (0, cache_1.setCacheData)("mqttClientStatus", "close");
57
+ logger_1.logger.info("MQTT connection closed");
58
+ });
59
+ client.on("offline", function () {
60
+ (0, cache_1.setCacheData)("mqttClientStatus", "offline");
61
+ logger_1.logger.info("MQTT connection offline");
62
+ });
63
+ return client;
64
+ };
65
+ var mqttSub = function (topicList, messageCallback, client) {
66
+ try {
67
+ client === null || client === void 0 ? void 0 : client.subscribe(topicList, { qos: 0 }, function (error, granted) {
68
+ if (error) {
69
+ logger_1.logger.error("Subscribe to topics error: ".concat(error.message));
70
+ }
71
+ var grantedList = [];
72
+ granted === null || granted === void 0 ? void 0 : granted.forEach(function (element) {
73
+ grantedList.push(element.topic);
74
+ logger_1.logger.info("Subscribed to topics: ".concat(element.topic));
75
+ });
76
+ (0, cache_1.setCacheData)("subscribedTopic", grantedList);
77
+ messageCallback && mqttOnMessage(messageCallback, client);
78
+ });
79
+ }
80
+ catch (error) {
81
+ logger_1.logger.error("Subscribed error ".concat(JSON.stringify(error)));
82
+ }
83
+ };
84
+ exports.mqttSub = mqttSub;
85
+ var mqttUnSub = function (topicList, client) {
86
+ try {
87
+ client === null || client === void 0 ? void 0 : client.unsubscribe(topicList, function (error) {
88
+ if (error) {
89
+ logger_1.logger.error("Unsubscribe to topics error: ".concat(error.message));
90
+ return;
91
+ }
92
+ topicList.forEach(function (topic) {
93
+ logger_1.logger.info("unsubscribed to topic: ".concat(topic));
94
+ });
95
+ });
96
+ }
97
+ catch (error) {
98
+ logger_1.logger.error("unsubscribed error: ".concat(JSON.stringify(error)));
99
+ }
100
+ };
101
+ exports.mqttUnSub = mqttUnSub;
102
+ var mqttDisconnect = function (props, reinit, client) {
103
+ try {
104
+ client === null || client === void 0 ? void 0 : client.end(false, function () {
105
+ logger_1.logger.info("MQTT disconnected successfully");
106
+ if (reinit) {
107
+ cachedConnection(__assign(__assign({}, props), { value: "client".concat(Math.random().toString(36).substring(7)) }));
108
+ }
109
+ });
110
+ }
111
+ catch (error) {
112
+ logger_1.logger.error("disconnect error: ".concat(JSON.stringify(error)));
113
+ }
114
+ };
115
+ exports.mqttDisconnect = mqttDisconnect;
116
+ var mqttOnMessage = function (messageCallback, client) {
117
+ try {
118
+ client === null || client === void 0 ? void 0 : client.on("message", function (topic, message) {
119
+ messageCallback(topic, message.toString());
120
+ });
121
+ }
122
+ catch (error) {
123
+ logger_1.logger.error("MQTT onmessage error: ".concat(JSON.stringify(error)));
124
+ }
125
+ };
126
+ exports.mqttOnMessage = mqttOnMessage;
127
+ var memoizedDb = function () {
128
+ var cache = {};
129
+ return function (props) {
130
+ if (props.value in cache) {
131
+ logger_1.logger.info("fetching mqtt cache");
132
+ return cache[props.value];
133
+ }
134
+ logger_1.logger.info("Creating new mqtt cache");
135
+ var result = checkInitConnect(props);
136
+ cache[props.value] = result;
137
+ return result;
138
+ };
139
+ };
140
+ // returned function from memoizedDb
141
+ var cachedConnection = memoizedDb();
142
+ var mqttCreateConnection = function (props) {
143
+ return cachedConnection(props);
144
+ };
145
+ exports.mqttCreateConnection = mqttCreateConnection;
146
+ var getMqttClient = function () {
147
+ return mqttClientId;
148
+ };
149
+ exports.getMqttClient = getMqttClient;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mysetup/mqtt",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "files": [
@@ -15,7 +15,7 @@
15
15
  "./package.json": "./package.json"
16
16
  },
17
17
  "scripts": {
18
- "build": "rm -rf ./dist && tsup",
18
+ "build": "rm -rf ./dist && tsc",
19
19
  "lint": "eslint .",
20
20
  "typecheck": "tsc --noEmit",
21
21
  "format": "prettier --write \"**/*.{ts,tsx,md,js}\"",
@@ -24,7 +24,6 @@
24
24
  },
25
25
  "dependencies": {
26
26
  "mqtt": "^5.10.1",
27
- "tsup": "8.1.0",
28
27
  "@mysetup/cache": "latest",
29
28
  "@mysetup/logger": "latest"
30
29
  },