mqtt-payload-inspector 1.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.
package/LICENSE ADDED
File without changes
package/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # MQTT Payload Inspector
2
+
3
+ Inspect, decode and analyze MQTT payloads.
4
+
5
+ ## Installation
6
+
7
+ npm install @muthukumar/mqtt-payload-inspector
8
+
9
+ ## Usage
10
+
11
+ ```js
12
+ const inspector =
13
+ require("@muthukumar/mqtt-payload-inspector");
14
+
15
+ const payload =
16
+ "7b22766f6c74616765223a3233307d";
17
+
18
+ console.log(
19
+ inspector.inspect(payload)
20
+ );
21
+ ```
22
+
23
+ ## Output
24
+
25
+ ```json
26
+ {
27
+ "format": "HEX",
28
+ "decoded": {
29
+ "voltage": 230
30
+ }
31
+ }
32
+ ```
File without changes
@@ -0,0 +1,6 @@
1
+ const { inspect } = require("../src");
2
+
3
+ const payload =
4
+ "7b22766f6c74616765223a3233302c2263757272656e74223a312e357d";
5
+
6
+ console.log(inspect(payload));
Binary file
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "mqtt-payload-inspector",
3
+ "version": "1.0.0",
4
+ "main": "src/index.js",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1"
7
+ },
8
+ "keywords": [
9
+ "mqtt",
10
+ "iot",
11
+ "payload",
12
+ "hex",
13
+ "base64",
14
+ "decoder",
15
+ "inspector"
16
+ ],
17
+ "author": "Muthukumar",
18
+ "license": "MIT",
19
+ "description": "Inspect, decode and analyze MQTT payloads",
20
+ "dependencies": {
21
+ "chalk": "^5.6.2",
22
+ "commander": "^14.0.3",
23
+ "crypto-js": "^4.2.0",
24
+ "entropy-string": "^4.2.0"
25
+ },
26
+ "devDependencies": {
27
+ "eslint": "^10.5.0",
28
+ "jest": "^30.4.2"
29
+ }
30
+ }
package/src/decode.js ADDED
@@ -0,0 +1,31 @@
1
+ function decode(payload) {
2
+ const str = payload.toString().trim();
3
+
4
+ // HEX
5
+ if (/^[0-9a-fA-F]+$/.test(str)) {
6
+ const decoded = Buffer.from(str, "hex").toString("utf8");
7
+
8
+ try {
9
+ return JSON.parse(decoded);
10
+ } catch {
11
+ return decoded;
12
+ }
13
+ }
14
+
15
+ // BASE64
16
+ if (/^[A-Za-z0-9+/=]+$/.test(str)) {
17
+ try {
18
+ const decoded = Buffer.from(str, "base64").toString("utf8");
19
+
20
+ try {
21
+ return JSON.parse(decoded);
22
+ } catch {
23
+ return decoded;
24
+ }
25
+ } catch {}
26
+ }
27
+
28
+ return payload;
29
+ }
30
+
31
+ module.exports = decode;
package/src/decoder.js ADDED
@@ -0,0 +1,10 @@
1
+ const inspector = require("../src");
2
+
3
+ const payload = JSON.stringify({
4
+ voltage: 230,
5
+ current: 1.5
6
+ });
7
+
8
+ const result = inspector.inspect(payload);
9
+
10
+ console.log(result);
package/src/index.js ADDED
@@ -0,0 +1,18 @@
1
+ const detectFormat = require("./inspect");
2
+ const decode = require("./decode");
3
+ const stats = require("./stats");
4
+
5
+ function inspect(payload) {
6
+
7
+ const format = detectFormat(payload);
8
+
9
+ return {
10
+ format,
11
+ decoded: decode(payload),
12
+ ...stats(payload)
13
+ };
14
+ }
15
+
16
+ module.exports = {
17
+ inspect
18
+ };
package/src/inspect.js ADDED
@@ -0,0 +1,20 @@
1
+ function detectFormat(payload) {
2
+ const str = payload.toString().trim();
3
+
4
+ try {
5
+ JSON.parse(str);
6
+ return "JSON";
7
+ } catch {}
8
+
9
+ if (/^[0-9a-fA-F]+$/.test(str)) {
10
+ return "HEX";
11
+ }
12
+
13
+ if (/^[A-Za-z0-9+/=]+$/.test(str)) {
14
+ return "BASE64";
15
+ }
16
+
17
+ return "TEXT";
18
+ }
19
+
20
+ module.exports = detectFormat;
package/src/stats.js ADDED
@@ -0,0 +1,20 @@
1
+ function detectFormat(payload) {
2
+ const str = payload.toString().trim();
3
+
4
+ try {
5
+ JSON.parse(str);
6
+ return "JSON";
7
+ } catch {}
8
+
9
+ if (/^[0-9a-fA-F]+$/.test(str)) {
10
+ return "HEX";
11
+ }
12
+
13
+ if (/^[A-Za-z0-9+/=]+$/.test(str)) {
14
+ return "BASE64";
15
+ }
16
+
17
+ return "TEXT";
18
+ }
19
+
20
+ module.exports = detectFormat;
@@ -0,0 +1,13 @@
1
+ function parse(topic) {
2
+ const parts = topic.split("/");
3
+
4
+ return {
5
+ project: parts[0],
6
+ country: parts[1],
7
+ state: parts[2],
8
+ city: parts[3],
9
+ deviceId: parts[4]
10
+ };
11
+ }
12
+
13
+ module.exports = parse;