@heritageai/messaging 1.0.24 → 1.0.26
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/base-listener.js +19 -5
- package/dist/services/ws-notifier.js +5 -3
- package/package.json +1 -1
- package/src/base-listener.ts +24 -6
- package/src/services/ws-notifier.ts +10 -3
package/dist/base-listener.js
CHANGED
|
@@ -20,12 +20,26 @@ class Listener {
|
|
|
20
20
|
this.onMessage(data, msg);
|
|
21
21
|
});
|
|
22
22
|
}
|
|
23
|
-
parseMessageData(
|
|
24
|
-
if (
|
|
25
|
-
return JSON.parse(
|
|
23
|
+
parseMessageData(msgData) {
|
|
24
|
+
if (typeof msgData === "string") {
|
|
25
|
+
return JSON.parse(msgData);
|
|
26
26
|
}
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
// Buffer in Node
|
|
28
|
+
if (Buffer.isBuffer(msgData)) {
|
|
29
|
+
return JSON.parse(msgData.toString("utf8"));
|
|
30
|
+
}
|
|
31
|
+
// Uint8Array support
|
|
32
|
+
if (msgData instanceof Uint8Array) {
|
|
33
|
+
return JSON.parse(Buffer.from(msgData).toString("utf8"));
|
|
34
|
+
}
|
|
35
|
+
// Fallback: sometimes data can be an object already
|
|
36
|
+
if (typeof msgData === "object") {
|
|
37
|
+
try {
|
|
38
|
+
return JSON.parse(JSON.stringify(msgData));
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return msgData; // Just return as-is if parsing fails
|
|
42
|
+
}
|
|
29
43
|
}
|
|
30
44
|
throw new Error("Invalid data type");
|
|
31
45
|
}
|
|
@@ -12,9 +12,11 @@ const pushNotificationToWS = async (payload) => {
|
|
|
12
12
|
const WS_GATEWAY_URL = process.env.WS_GATEWAY_URL;
|
|
13
13
|
if (!WS_GATEWAY_URL)
|
|
14
14
|
throw new Error("WS_GATEWAY_URL not defined");
|
|
15
|
-
await
|
|
16
|
-
|
|
17
|
-
});
|
|
15
|
+
// await axios.post(`${WS_GATEWAY_URL}/internal/notify`, payload, {
|
|
16
|
+
// headers: { "Content-Type": "application/json" },
|
|
17
|
+
// });
|
|
18
|
+
const { userId, notification } = payload;
|
|
19
|
+
await axios_1.default.post(`${process.env.WS_GATEWAY_URL}/internal/notify`, { userId, notification }, { headers: { "x-internal-key": process.env.INTERNAL_API_KEY } });
|
|
18
20
|
}
|
|
19
21
|
catch (err) {
|
|
20
22
|
logger_1.logger.error("Failed to push notification to WS-Gateway", err);
|
package/package.json
CHANGED
package/src/base-listener.ts
CHANGED
|
@@ -23,7 +23,7 @@ export abstract class Listener<T> {
|
|
|
23
23
|
const subscription = this.client.subscribe(
|
|
24
24
|
this.subject,
|
|
25
25
|
this.queueGroupName,
|
|
26
|
-
this.subscriptionOptions()
|
|
26
|
+
this.subscriptionOptions(),
|
|
27
27
|
);
|
|
28
28
|
|
|
29
29
|
// @ts-ignore (node-nats-streaming typing bug)
|
|
@@ -33,12 +33,30 @@ export abstract class Listener<T> {
|
|
|
33
33
|
});
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
private parseMessageData(
|
|
37
|
-
if (
|
|
38
|
-
return JSON.parse(
|
|
39
|
-
} else if (Buffer.isBuffer(data)) {
|
|
40
|
-
return JSON.parse(data.toString("utf8"));
|
|
36
|
+
private parseMessageData(msgData: any) {
|
|
37
|
+
if (typeof msgData === "string") {
|
|
38
|
+
return JSON.parse(msgData);
|
|
41
39
|
}
|
|
40
|
+
|
|
41
|
+
// Buffer in Node
|
|
42
|
+
if (Buffer.isBuffer(msgData)) {
|
|
43
|
+
return JSON.parse(msgData.toString("utf8"));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Uint8Array support
|
|
47
|
+
if (msgData instanceof Uint8Array) {
|
|
48
|
+
return JSON.parse(Buffer.from(msgData).toString("utf8"));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Fallback: sometimes data can be an object already
|
|
52
|
+
if (typeof msgData === "object") {
|
|
53
|
+
try {
|
|
54
|
+
return JSON.parse(JSON.stringify(msgData));
|
|
55
|
+
} catch {
|
|
56
|
+
return msgData; // Just return as-is if parsing fails
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
42
60
|
throw new Error("Invalid data type");
|
|
43
61
|
}
|
|
44
62
|
}
|
|
@@ -17,9 +17,16 @@ export const pushNotificationToWS = async (payload: WSNotificationPayload) => {
|
|
|
17
17
|
const WS_GATEWAY_URL = process.env.WS_GATEWAY_URL;
|
|
18
18
|
if (!WS_GATEWAY_URL) throw new Error("WS_GATEWAY_URL not defined");
|
|
19
19
|
|
|
20
|
-
await axios.post(`${WS_GATEWAY_URL}/internal/notify`, payload, {
|
|
21
|
-
|
|
22
|
-
});
|
|
20
|
+
// await axios.post(`${WS_GATEWAY_URL}/internal/notify`, payload, {
|
|
21
|
+
// headers: { "Content-Type": "application/json" },
|
|
22
|
+
// });
|
|
23
|
+
|
|
24
|
+
const { userId, notification } = payload;
|
|
25
|
+
await axios.post(
|
|
26
|
+
`${process.env.WS_GATEWAY_URL}/internal/notify`,
|
|
27
|
+
{ userId, notification },
|
|
28
|
+
{ headers: { "x-internal-key": process.env.INTERNAL_API_KEY! } },
|
|
29
|
+
);
|
|
23
30
|
} catch (err) {
|
|
24
31
|
logger.error("Failed to push notification to WS-Gateway", err);
|
|
25
32
|
throw err;
|