@heritageai/messaging 1.0.25 → 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.
@@ -20,12 +20,26 @@ class Listener {
20
20
  this.onMessage(data, msg);
21
21
  });
22
22
  }
23
- parseMessageData(data) {
24
- if (data instanceof String) {
25
- return JSON.parse(data.valueOf());
23
+ parseMessageData(msgData) {
24
+ if (typeof msgData === "string") {
25
+ return JSON.parse(msgData);
26
26
  }
27
- else if (Buffer.isBuffer(data)) {
28
- return JSON.parse(data.toString("utf8"));
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
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "author": "future_platform",
3
3
  "name": "@heritageai/messaging",
4
4
  "license": "ISC",
5
- "version": "1.0.25",
5
+ "version": "1.0.26",
6
6
  "description": "",
7
7
  "main": "dist/index.js",
8
8
  "types": "dist/index.d.ts",
@@ -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(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"));
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
  }