@mysetup/mqtt 1.4.0 → 1.5.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 +113 -1
- package/package.json +6 -6
- package/dist/index.d.ts +0 -2
- package/dist/index.d.ts.map +0 -1
- package/dist/mqttClient.d.ts +0 -17
- package/dist/mqttClient.d.ts.map +0 -1
- package/dist/mqttClient.js +0 -133
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mysetup/mqtt",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"files": [
|
|
@@ -8,14 +8,13 @@
|
|
|
8
8
|
],
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
|
-
"types": "./dist/index.d.ts",
|
|
12
11
|
"import": "./dist/index.js",
|
|
13
12
|
"require": "./dist/index.js"
|
|
14
13
|
},
|
|
15
14
|
"./package.json": "./package.json"
|
|
16
15
|
},
|
|
17
16
|
"scripts": {
|
|
18
|
-
"build": "rm -rf ./dist &&
|
|
17
|
+
"build": "rm -rf ./dist && tsup",
|
|
19
18
|
"lint": "eslint .",
|
|
20
19
|
"typecheck": "tsc --noEmit",
|
|
21
20
|
"format": "prettier --write \"**/*.{ts,tsx,md,js}\"",
|
|
@@ -23,12 +22,13 @@
|
|
|
23
22
|
"clean": "rm -rf node_modules .swc dist pnpm-lock.yaml && echo \"✅ Successfully removed \""
|
|
24
23
|
},
|
|
25
24
|
"dependencies": {
|
|
26
|
-
"mqtt": "^5.10.1"
|
|
25
|
+
"mqtt": "^5.10.1",
|
|
26
|
+
"tsup": "8.1.0",
|
|
27
|
+
"@mysetup/cache": "latest",
|
|
28
|
+
"@mysetup/logger": "latest"
|
|
27
29
|
},
|
|
28
30
|
"devDependencies": {
|
|
29
|
-
"@mysetup/cache": "latest",
|
|
30
31
|
"@mysetup/eslint-config": "latest",
|
|
31
|
-
"@mysetup/logger": "latest",
|
|
32
32
|
"@mysetup/prettier-config": "latest",
|
|
33
33
|
"@mysetup/tsconfig": "latest",
|
|
34
34
|
"@types/node": "^22.8.6",
|
package/dist/index.d.ts
DELETED
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC"}
|
package/dist/mqttClient.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
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
|
package/dist/mqttClient.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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"}
|
package/dist/mqttClient.js
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
import { connect } from "mqtt";
|
|
2
|
-
import { logger } from "@mysetup/logger";
|
|
3
|
-
import { setCacheData } from "@mysetup/cache";
|
|
4
|
-
const serverConnectOptions = {
|
|
5
|
-
protocol: "mqtt",
|
|
6
|
-
port: 1883,
|
|
7
|
-
host: "test.mosquitto.org",
|
|
8
|
-
};
|
|
9
|
-
const webConnectOptions = {
|
|
10
|
-
protocol: "wss",
|
|
11
|
-
port: 8081,
|
|
12
|
-
host: "test.mosquitto.org",
|
|
13
|
-
};
|
|
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, {
|
|
19
|
-
clientId: props.value,
|
|
20
|
-
clean: true,
|
|
21
|
-
rejectUnauthorized: false,
|
|
22
|
-
resubscribe: true,
|
|
23
|
-
keepalive: 300,
|
|
24
|
-
});
|
|
25
|
-
mqttClientId = props.value;
|
|
26
|
-
client.setMaxListeners(100);
|
|
27
|
-
client.on("connect", () => {
|
|
28
|
-
logger.info("MQTT connection successful");
|
|
29
|
-
setCacheData("mqttClientStatus", "connect");
|
|
30
|
-
mqttSub(props.topicList, props.messageCallback, client);
|
|
31
|
-
});
|
|
32
|
-
client.on("error", (err) => {
|
|
33
|
-
logger.error(`MQTT Connection error: ${JSON.stringify(err)}`);
|
|
34
|
-
setCacheData("mqttClientStatus", "error");
|
|
35
|
-
mqttDisconnect(props, true, client);
|
|
36
|
-
});
|
|
37
|
-
client.on("reconnect", () => {
|
|
38
|
-
setCacheData("mqttClientStatus", "reconnect");
|
|
39
|
-
logger.info("MQTT Reconnecting");
|
|
40
|
-
});
|
|
41
|
-
client.on("close", () => {
|
|
42
|
-
setCacheData("mqttClientStatus", "close");
|
|
43
|
-
logger.info("MQTT connection closed");
|
|
44
|
-
});
|
|
45
|
-
client.on("offline", () => {
|
|
46
|
-
setCacheData("mqttClientStatus", "offline");
|
|
47
|
-
logger.info("MQTT connection offline");
|
|
48
|
-
});
|
|
49
|
-
return client;
|
|
50
|
-
};
|
|
51
|
-
const mqttSub = (topicList, messageCallback, client) => {
|
|
52
|
-
try {
|
|
53
|
-
client === null || client === void 0 ? void 0 : client.subscribe(topicList, { qos: 0 }, (error, granted) => {
|
|
54
|
-
if (error) {
|
|
55
|
-
logger.error(`Subscribe to topics error: ${error.message}`);
|
|
56
|
-
}
|
|
57
|
-
const grantedList = [];
|
|
58
|
-
granted === null || granted === void 0 ? void 0 : granted.forEach((element) => {
|
|
59
|
-
grantedList.push(element.topic);
|
|
60
|
-
logger.info(`Subscribed to topics: ${element.topic}`);
|
|
61
|
-
});
|
|
62
|
-
setCacheData("subscribedTopic", grantedList);
|
|
63
|
-
messageCallback && mqttOnMessage(messageCallback, client);
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
catch (error) {
|
|
67
|
-
logger.error(`Subscribed error ${JSON.stringify(error)}`);
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
const mqttUnSub = (topicList, client) => {
|
|
71
|
-
try {
|
|
72
|
-
client === null || client === void 0 ? void 0 : client.unsubscribe(topicList, (error) => {
|
|
73
|
-
if (error) {
|
|
74
|
-
logger.error(`Unsubscribe to topics error: ${error.message}`);
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
topicList.forEach((topic) => {
|
|
78
|
-
logger.info(`unsubscribed to topic: ${topic}`);
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
catch (error) {
|
|
83
|
-
logger.error(`unsubscribed error: ${JSON.stringify(error)}`);
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
const mqttDisconnect = (props, reinit, client) => {
|
|
87
|
-
try {
|
|
88
|
-
client === null || client === void 0 ? void 0 : client.end(false, () => {
|
|
89
|
-
logger.info("MQTT disconnected successfully");
|
|
90
|
-
if (reinit) {
|
|
91
|
-
cachedConnection({
|
|
92
|
-
...props,
|
|
93
|
-
value: `client${Math.random().toString(36).substring(7)}`,
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
catch (error) {
|
|
99
|
-
logger.error(`disconnect error: ${JSON.stringify(error)}`);
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
const mqttOnMessage = (messageCallback, client) => {
|
|
103
|
-
try {
|
|
104
|
-
client === null || client === void 0 ? void 0 : client.on("message", (topic, message) => {
|
|
105
|
-
messageCallback(topic, message.toString());
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
catch (error) {
|
|
109
|
-
logger.error(`MQTT onmessage error: ${JSON.stringify(error)}`);
|
|
110
|
-
}
|
|
111
|
-
};
|
|
112
|
-
const memoizedDb = () => {
|
|
113
|
-
const cache = {};
|
|
114
|
-
return (props) => {
|
|
115
|
-
if (props.value in cache) {
|
|
116
|
-
logger.info("fetching mqtt cache");
|
|
117
|
-
return cache[props.value];
|
|
118
|
-
}
|
|
119
|
-
logger.info("Creating new mqtt cache");
|
|
120
|
-
const result = checkInitConnect(props);
|
|
121
|
-
cache[props.value] = result;
|
|
122
|
-
return result;
|
|
123
|
-
};
|
|
124
|
-
};
|
|
125
|
-
// returned function from memoizedDb
|
|
126
|
-
const cachedConnection = memoizedDb();
|
|
127
|
-
const mqttCreateConnection = (props) => {
|
|
128
|
-
return cachedConnection(props);
|
|
129
|
-
};
|
|
130
|
-
const getMqttClient = () => {
|
|
131
|
-
return mqttClientId;
|
|
132
|
-
};
|
|
133
|
-
export { mqttCreateConnection, mqttSub, mqttUnSub, mqttDisconnect, mqttOnMessage, getMqttClient, };
|