Skip to content

Commit

Permalink
breakout functions
Browse files Browse the repository at this point in the history
  • Loading branch information
zoe-codez committed Apr 13, 2024
1 parent 9b774cb commit 0f3b49d
Show file tree
Hide file tree
Showing 7 changed files with 262 additions and 209 deletions.
395 changes: 211 additions & 184 deletions package-lock.json

Large diffs are not rendered by default.

32 changes: 16 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@digital-alchemy/mqtt-extension",
"repository": "https://github.com/Digital-Alchemy-TS/mqtt",
"homepage": "https://docs.digital-alchemy.app/MQTT",
"version": "0.3.6",
"version": "0.3.7",
"scripts": {
"build": "rm -rf dist/; tsc",
"test": "./scripts/test.sh",
Expand All @@ -25,41 +25,41 @@
},
"license": "MIT",
"dependencies": {
"@digital-alchemy/core": "^0.3.8",
"mqtt": "^5.5.0",
"@digital-alchemy/core": "^0.3.9",
"mqtt": "^5.5.2",
"mqtt-packet": "^9.0.0",
"prom-client": "^15.1.0"
"prom-client": "^15.1.1"
},
"devDependencies": {
"@cspell/eslint-plugin": "^8.6.1",
"@cspell/eslint-plugin": "^8.7.0",
"@types/figlet": "^1.5.8",
"@types/jest": "^29.5.12",
"@types/js-yaml": "^4.0.9",
"@types/minimist": "^1.2.5",
"@types/mute-stream": "^0.0.4",
"@types/node": "^20.11.30",
"@types/node": "^20.12.7",
"@types/uuid": "^9.0.8",
"@types/ws": "^8.5.10",
"@typescript-eslint/eslint-plugin": "7.4.0",
"@typescript-eslint/parser": "7.4.0",
"@typescript-eslint/eslint-plugin": "7.6.0",
"@typescript-eslint/parser": "7.6.0",
"eslint": "8.57.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jsonc": "^2.14.1",
"eslint-plugin-jsonc": "^2.15.1",
"eslint-plugin-no-unsanitized": "^4.0.2",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-security": "^2.1.1",
"eslint-plugin-simple-import-sort": "^12.0.0",
"eslint-plugin-sonarjs": "^0.24.0",
"eslint-plugin-security": "^3.0.0",
"eslint-plugin-simple-import-sort": "^12.1.0",
"eslint-plugin-sonarjs": "^0.25.1",
"eslint-plugin-sort-keys-fix": "^1.1.2",
"eslint-plugin-unicorn": "^51.0.1",
"eslint-plugin-unicorn": "^52.0.0",
"jest": "^29.7.0",
"jest-environment-node": "^29.7.0",
"npm-check-updates": "^16.14.18",
"prettier": "^3.2.5",
"ts-jest": "^29.1.2",
"tsx": "^4.7.1",
"type-fest": "^4.14.0",
"typescript": "^5.4.3"
"tsx": "^4.7.2",
"type-fest": "^4.15.0",
"typescript": "^5.4.5"
}
}
16 changes: 8 additions & 8 deletions src/extensions/bindings.extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,30 @@ export function MQTT_Bindings({
client = await connectAsync({
...config.mqtt.CLIENT_OPTIONS,
});
logger.info("MQTT Connected");
logger.info("mqtt connected");
logEvents();
initRouting();
});

lifecycle.onShutdownStart(async () => {
logger.info(`Terminating MQTT connection`);
logger.info(`terminating mqtt connection`);
await new Promise<void>(done => client.end(() => done()));
client = undefined;
});

function logEvents() {
client.on("reconnect", () => {
logger.info("MQTT Reconnect");
logger.info("mqtt reconnect");
});
client.on("end", () => {
logger.info("MQTT End");
logger.info("mqtt end");
});
client.on("error", error => {
logger.error({ error }, "MQTT Error");
logger.error({ error }, "mqtt error");
event.emit(MQTT_RECONNECT);
});
client.on("close", () => {
logger.info("MQTT Close");
logger.info("mqtt close");
});
}

Expand Down Expand Up @@ -139,7 +139,7 @@ export function MQTT_Bindings({
topic,
topicMatches,
},
`Message has multiple topic matches, with different parse formats`,
`message has multiple topic matches, with different parse formats`,
);
}
await each(CALLBACKS.get(topicMatch), async exec => {
Expand Down Expand Up @@ -217,7 +217,7 @@ export function MQTT_Bindings({
}
return message;
} catch (error) {
logger.error({ error, format }, `Failed to parse MQTT message`);
logger.error({ error, format }, `failed to parse mqtt message`);
return message;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/extensions/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from "./bindings.extension";
export * from "./publish.extension";
export * from "./subscribe.extension";
12 changes: 12 additions & 0 deletions src/extensions/publish.extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { TServiceParams } from "@digital-alchemy/core";
import { IClientPublishOptions, Packet } from "mqtt";

export function Publish({ mqtt }: TServiceParams) {
return async function (
topic: string,
message?: string | Buffer | object | Array<unknown>,
options?: IClientPublishOptions,
): Promise<Packet> {
return await mqtt.bindings.publish(topic, message, options);
};
}
9 changes: 9 additions & 0 deletions src/extensions/subscribe.extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { TServiceParams } from "@digital-alchemy/core";

import { MQTTSubscribeOptions } from "..";

export function Subscribe({ mqtt }: TServiceParams) {
return function <DATA = unknown>(options: MQTTSubscribeOptions<DATA>): void {
mqtt.bindings.subscribe(options);
};
}
5 changes: 4 additions & 1 deletion src/mqtt.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CreateLibrary } from "@digital-alchemy/core";
import { IClientOptions } from "mqtt";

import { MQTT_Bindings } from "./extensions";
import { MQTT_Bindings, Publish, Subscribe } from "./extensions";

export const LIB_MQTT = CreateLibrary({
configuration: {
Expand All @@ -16,8 +16,11 @@ export const LIB_MQTT = CreateLibrary({
},
},
name: "mqtt",
priorityInit: ["bindings"],
services: {
bindings: MQTT_Bindings,
publish: Publish,
subscribe: Subscribe,
},
});

Expand Down

0 comments on commit 0f3b49d

Please sign in to comment.