@heritageai/messaging 1.0.21 → 1.0.23

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.
@@ -7,4 +7,5 @@ export declare abstract class Listener<T> {
7
7
  constructor(client: Stan);
8
8
  subscriptionOptions(): import("node-nats-streaming").SubscriptionOptions;
9
9
  listen(): void;
10
+ private parseMessageData;
10
11
  }
@@ -16,11 +16,18 @@ class Listener {
16
16
  const subscription = this.client.subscribe(this.subject, this.queueGroupName, this.subscriptionOptions());
17
17
  // @ts-ignore (node-nats-streaming typing bug)
18
18
  subscription.on("message", (msg) => {
19
- const data = typeof msg.getData() === "string"
20
- ? JSON.parse(msg.getData())
21
- : JSON.parse(msg.getData().toString("utf8"));
19
+ const data = this.parseMessageData(msg.getData());
22
20
  this.onMessage(data, msg);
23
21
  });
24
22
  }
23
+ parseMessageData(data) {
24
+ if (data instanceof String) {
25
+ return JSON.parse(data.valueOf());
26
+ }
27
+ else if (Buffer.isBuffer(data)) {
28
+ return JSON.parse(data.toString("utf8"));
29
+ }
30
+ throw new Error("Invalid data type");
31
+ }
25
32
  }
26
33
  exports.Listener = Listener;
package/dist/index.d.ts CHANGED
@@ -19,3 +19,5 @@ export * from "./publishers/site-deleted-publisher";
19
19
  export * from "./publishers/user-created-publisher";
20
20
  export * from "./publishers/user-deleted-publisher";
21
21
  export * from "./types/notifications/notification-type";
22
+ export * from "./middlewares/verify-ws-jwt";
23
+ export * from "./services/ws-notifier";
package/dist/index.js CHANGED
@@ -17,7 +17,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./nats-wrapper"), exports);
18
18
  __exportStar(require("./base-listener"), exports);
19
19
  __exportStar(require("./base-publisher"), exports);
20
+ // subjects
20
21
  __exportStar(require("./subjects"), exports);
22
+ // events
21
23
  __exportStar(require("./events/asset-created-event"), exports);
22
24
  __exportStar(require("./events/asset-updated-event"), exports);
23
25
  __exportStar(require("./events/asset-deleted-event"), exports);
@@ -26,6 +28,7 @@ __exportStar(require("./events/site-updated-event"), exports);
26
28
  __exportStar(require("./events/site-deleted-event"), exports);
27
29
  __exportStar(require("./events/user-created-event"), exports);
28
30
  __exportStar(require("./events/user-deleted-event"), exports);
31
+ // publishers
29
32
  __exportStar(require("./publishers/asset-created-publisher"), exports);
30
33
  __exportStar(require("./publishers/asset-updated-publisher"), exports);
31
34
  __exportStar(require("./publishers/asset-deleted-publisher"), exports);
@@ -34,4 +37,9 @@ __exportStar(require("./publishers/site-updated-publisher"), exports);
34
37
  __exportStar(require("./publishers/site-deleted-publisher"), exports);
35
38
  __exportStar(require("./publishers/user-created-publisher"), exports);
36
39
  __exportStar(require("./publishers/user-deleted-publisher"), exports);
40
+ // types
37
41
  __exportStar(require("./types/notifications/notification-type"), exports);
42
+ // middlewares
43
+ __exportStar(require("./middlewares/verify-ws-jwt"), exports);
44
+ // services
45
+ __exportStar(require("./services/ws-notifier"), exports);
@@ -0,0 +1,7 @@
1
+ interface WSJwtPayload {
2
+ id: string;
3
+ email?: string;
4
+ role?: string;
5
+ }
6
+ export declare const verifyWSJwt: (token: string) => WSJwtPayload;
7
+ export {};
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.verifyWSJwt = void 0;
7
+ const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
8
+ const verifyWSJwt = (token) => {
9
+ if (!process.env.JWT_KEY)
10
+ throw new Error("JWT_KEY not defined");
11
+ try {
12
+ const payload = jsonwebtoken_1.default.verify(token, process.env.JWT_KEY);
13
+ return payload;
14
+ }
15
+ catch (err) {
16
+ throw new Error("Invalid WS token");
17
+ }
18
+ };
19
+ exports.verifyWSJwt = verifyWSJwt;
@@ -0,0 +1,11 @@
1
+ interface WSNotificationPayload {
2
+ userId: string;
3
+ notification: {
4
+ id: string;
5
+ type: string;
6
+ read: boolean;
7
+ createdAt: string;
8
+ };
9
+ }
10
+ export declare const pushNotificationToWS: (payload: WSNotificationPayload) => Promise<void>;
11
+ export {};
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.pushNotificationToWS = void 0;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ const logger_1 = require("../utils/logger");
9
+ const pushNotificationToWS = async (payload) => {
10
+ try {
11
+ // WS-Gateway URL should be configured in env variables
12
+ const WS_GATEWAY_URL = process.env.WS_GATEWAY_URL;
13
+ if (!WS_GATEWAY_URL)
14
+ throw new Error("WS_GATEWAY_URL not defined");
15
+ await axios_1.default.post(`${WS_GATEWAY_URL}/notify`, payload, {
16
+ headers: { "Content-Type": "application/json" },
17
+ });
18
+ }
19
+ catch (err) {
20
+ logger_1.logger.error("Failed to push notification to WS-Gateway", err);
21
+ throw err;
22
+ }
23
+ };
24
+ exports.pushNotificationToWS = pushNotificationToWS;
package/package.json CHANGED
@@ -2,17 +2,19 @@
2
2
  "author": "future_platform",
3
3
  "name": "@heritageai/messaging",
4
4
  "license": "ISC",
5
- "version": "1.0.21",
5
+ "version": "1.0.23",
6
6
  "description": "",
7
7
  "main": "dist/index.js",
8
8
  "types": "dist/index.d.ts",
9
9
  "scripts": {
10
10
  "build": "tsc",
11
- "prepublishOnly": "npm run build",
12
- "publish": "bash publish.sh"
11
+ "prepublishOnly": "npm run build"
13
12
  },
14
13
  "dependencies": {
14
+ "@types/jsonwebtoken": "^9.0.10",
15
+ "axios": "^1.13.2",
15
16
  "colors": "^1.4.0",
17
+ "jsonwebtoken": "^9.0.3",
16
18
  "node-nats-streaming": "^0.3.2",
17
19
  "typescript": "^5.9.0"
18
20
  },
@@ -28,12 +28,17 @@ export abstract class Listener<T> {
28
28
 
29
29
  // @ts-ignore (node-nats-streaming typing bug)
30
30
  subscription.on("message", (msg: Message) => {
31
- const data =
32
- typeof msg.getData() === "string"
33
- ? JSON.parse(msg.getData())
34
- : JSON.parse(msg.getData().toString("utf8"));
35
-
31
+ const data = this.parseMessageData(msg.getData());
36
32
  this.onMessage(data, msg);
37
33
  });
38
34
  }
35
+
36
+ private parseMessageData(data: String | Buffer<ArrayBufferLike>): T {
37
+ if (data instanceof String) {
38
+ return JSON.parse(data.valueOf());
39
+ } else if (Buffer.isBuffer(data)) {
40
+ return JSON.parse(data.toString("utf8"));
41
+ }
42
+ throw new Error("Invalid data type");
43
+ }
39
44
  }
package/src/index.ts CHANGED
@@ -2,8 +2,10 @@ export * from "./nats-wrapper";
2
2
  export * from "./base-listener";
3
3
  export * from "./base-publisher";
4
4
 
5
+ // subjects
5
6
  export * from "./subjects";
6
7
 
8
+ // events
7
9
  export * from "./events/asset-created-event";
8
10
  export * from "./events/asset-updated-event";
9
11
  export * from "./events/asset-deleted-event";
@@ -13,6 +15,7 @@ export * from "./events/site-deleted-event";
13
15
  export * from "./events/user-created-event";
14
16
  export * from "./events/user-deleted-event";
15
17
 
18
+ // publishers
16
19
  export * from "./publishers/asset-created-publisher";
17
20
  export * from "./publishers/asset-updated-publisher";
18
21
  export * from "./publishers/asset-deleted-publisher";
@@ -22,4 +25,11 @@ export * from "./publishers/site-deleted-publisher";
22
25
  export * from "./publishers/user-created-publisher";
23
26
  export * from "./publishers/user-deleted-publisher";
24
27
 
28
+ // types
25
29
  export * from "./types/notifications/notification-type";
30
+
31
+ // middlewares
32
+ export * from "./middlewares/verify-ws-jwt";
33
+
34
+ // services
35
+ export * from "./services/ws-notifier";
@@ -0,0 +1,17 @@
1
+ import jwt from "jsonwebtoken";
2
+
3
+ interface WSJwtPayload {
4
+ id: string;
5
+ email?: string;
6
+ role?: string;
7
+ }
8
+
9
+ export const verifyWSJwt = (token: string): WSJwtPayload => {
10
+ if (!process.env.JWT_KEY) throw new Error("JWT_KEY not defined");
11
+ try {
12
+ const payload = jwt.verify(token, process.env.JWT_KEY) as WSJwtPayload;
13
+ return payload;
14
+ } catch (err) {
15
+ throw new Error("Invalid WS token");
16
+ }
17
+ };
@@ -0,0 +1,27 @@
1
+ import axios from "axios";
2
+ import { logger } from "../utils/logger";
3
+
4
+ interface WSNotificationPayload {
5
+ userId: string;
6
+ notification: {
7
+ id: string;
8
+ type: string;
9
+ read: boolean;
10
+ createdAt: string;
11
+ };
12
+ }
13
+
14
+ export const pushNotificationToWS = async (payload: WSNotificationPayload) => {
15
+ try {
16
+ // WS-Gateway URL should be configured in env variables
17
+ const WS_GATEWAY_URL = process.env.WS_GATEWAY_URL;
18
+ if (!WS_GATEWAY_URL) throw new Error("WS_GATEWAY_URL not defined");
19
+
20
+ await axios.post(`${WS_GATEWAY_URL}/notify`, payload, {
21
+ headers: { "Content-Type": "application/json" },
22
+ });
23
+ } catch (err) {
24
+ logger.error("Failed to push notification to WS-Gateway", err);
25
+ throw err;
26
+ }
27
+ };
package/publish.sh DELETED
@@ -1,16 +0,0 @@
1
- #!/bin/bash
2
-
3
- # Exit immediately if a command exits with a non-zero status.
4
- set -e
5
-
6
- # Build the package
7
- npm run build
8
-
9
- # Update the version (patch) and get the new version
10
- npm version patch
11
-
12
- # Publish the package
13
- npm publish --access public
14
-
15
- # Final message
16
- echo "Package published successfully!"