@heritageai/messaging 1.0.2 → 1.0.4

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.
@@ -1,8 +1,8 @@
1
- import nats from 'node-nats-streaming';
1
+ import nats from "node-nats-streaming";
2
2
  declare class NatsWrapper {
3
3
  private _client?;
4
4
  get client(): nats.Stan;
5
- connect(clusterId: string, clientId: string, url: string): Promise<void>;
5
+ connect(clusterId: string, clientId: string, url: string, retries?: number, delayMs?: number): Promise<void>;
6
6
  }
7
7
  export declare const natsWrapper: NatsWrapper;
8
8
  export {};
@@ -4,27 +4,37 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.natsWrapper = void 0;
7
- // src/nats-wrapper.ts
8
7
  const node_nats_streaming_1 = __importDefault(require("node-nats-streaming"));
9
8
  class NatsWrapper {
10
9
  get client() {
11
10
  if (!this._client) {
12
- throw new Error('Cannot access NATS client before connecting');
11
+ throw new Error("Cannot access NATS client before connecting");
13
12
  }
14
13
  return this._client;
15
14
  }
16
- connect(clusterId, clientId, url) {
17
- this._client = node_nats_streaming_1.default.connect(clusterId, clientId, { url });
15
+ connect(clusterId, clientId, url, retries = 5, delayMs = 3000) {
18
16
  return new Promise((resolve, reject) => {
19
- // @ts-ignore
20
- this.client.on('connect', () => {
21
- console.log('Connected to NATS');
22
- resolve();
23
- });
24
- // @ts-ignore
25
- this.client.on('error', (err) => {
26
- reject(err);
27
- });
17
+ const attempt = (remaining) => {
18
+ console.log(`🔌 Connecting to NATS (${remaining} retries left)...`);
19
+ this._client = node_nats_streaming_1.default.connect(clusterId, clientId, { url });
20
+ // @ts-ignore
21
+ this._client.on("connect", () => {
22
+ console.log("✅ Connected to NATS");
23
+ resolve();
24
+ });
25
+ // @ts-ignore
26
+ this._client.on("error", (err) => {
27
+ console.error("❌ NATS connection error:", err.message);
28
+ this._client?.close();
29
+ this._client = undefined;
30
+ if (remaining <= 0) {
31
+ reject(err);
32
+ return;
33
+ }
34
+ setTimeout(() => attempt(remaining - 1), delayMs);
35
+ });
36
+ };
37
+ attempt(retries);
28
38
  });
29
39
  }
30
40
  }
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.2",
5
+ "version": "1.0.4",
6
6
  "description": "",
7
7
  "main": "dist/index.js",
8
8
  "types": "dist/index.d.ts",
@@ -1,30 +1,51 @@
1
- // src/nats-wrapper.ts
2
- import nats, { Stan } from 'node-nats-streaming';
1
+ import nats, { Stan } from "node-nats-streaming";
3
2
 
4
3
  class NatsWrapper {
5
4
  private _client?: Stan;
6
5
 
7
6
  get client() {
8
7
  if (!this._client) {
9
- throw new Error('Cannot access NATS client before connecting');
8
+ throw new Error("Cannot access NATS client before connecting");
10
9
  }
11
10
  return this._client;
12
11
  }
13
12
 
14
- connect(clusterId: string, clientId: string, url: string): Promise<void> {
15
- this._client = nats.connect(clusterId, clientId, { url });
16
-
13
+ connect(
14
+ clusterId: string,
15
+ clientId: string,
16
+ url: string,
17
+ retries = 5,
18
+ delayMs = 3000,
19
+ ): Promise<void> {
17
20
  return new Promise((resolve, reject) => {
18
- // @ts-ignore
19
- this.client.on('connect', () => {
20
- console.log('Connected to NATS');
21
- resolve();
22
- });
23
-
24
- // @ts-ignore
25
- this.client.on('error', (err) => {
26
- reject(err);
27
- });
21
+ const attempt = (remaining: number) => {
22
+ console.log(`🔌 Connecting to NATS (${remaining} retries left)...`);
23
+
24
+ this._client = nats.connect(clusterId, clientId, { url });
25
+
26
+ // @ts-ignore
27
+ this._client.on("connect", () => {
28
+ console.log("✅ Connected to NATS");
29
+ resolve();
30
+ });
31
+
32
+ // @ts-ignore
33
+ this._client.on("error", (err) => {
34
+ console.error("❌ NATS connection error:", err.message);
35
+
36
+ this._client?.close();
37
+ this._client = undefined;
38
+
39
+ if (remaining <= 0) {
40
+ reject(err);
41
+ return;
42
+ }
43
+
44
+ setTimeout(() => attempt(remaining - 1), delayMs);
45
+ });
46
+ };
47
+
48
+ attempt(retries);
28
49
  });
29
50
  }
30
51
  }