@clonegod/ttd-core 2.0.76 → 2.0.78

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.
@@ -3,7 +3,6 @@ export interface WebSocketClientOptions {
3
3
  [key: string]: string;
4
4
  };
5
5
  reconnectInterval?: number;
6
- maxReconnectAttempts?: number;
7
6
  rejectUnauthorized?: boolean;
8
7
  keepAlive?: boolean;
9
8
  keepAliveInterval?: number;
@@ -14,18 +13,16 @@ export declare class WebSocketClient {
14
13
  private ws;
15
14
  private onMessageCallback;
16
15
  private onOpenCallback;
17
- private reconnectAttempts;
18
- private maxReconnectAttempts;
19
16
  private keepAliveTimer;
20
17
  constructor(url: string, options?: WebSocketClientOptions);
21
18
  connect(): void;
19
+ private reconnect;
22
20
  onMessage(callback: (data: any) => void): void;
23
21
  onOpen(callback: () => void): void;
24
22
  send(data: string): boolean;
25
23
  disconnect(): void;
26
- private handleReconnect;
27
- private cleanup;
28
24
  isConnected(): boolean;
29
25
  private startKeepAlive;
30
26
  private stopKeepAlive;
27
+ private setupGlobalErrorHandlers;
31
28
  }
@@ -1,4 +1,13 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
2
11
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
13
  };
@@ -11,41 +20,52 @@ class WebSocketClient {
11
20
  this.ws = null;
12
21
  this.onMessageCallback = null;
13
22
  this.onOpenCallback = null;
14
- this.reconnectAttempts = 0;
15
- this.maxReconnectAttempts = 10;
16
23
  this.keepAliveTimer = null;
17
24
  this.url = url;
18
- this.options = Object.assign({ reconnectInterval: 3000, maxReconnectAttempts: 10, rejectUnauthorized: false, keepAlive: false, keepAliveInterval: 30000 }, options);
19
- this.maxReconnectAttempts = this.options.maxReconnectAttempts;
25
+ this.options = Object.assign({ reconnectInterval: 3000, rejectUnauthorized: false, keepAlive: false, keepAliveInterval: 30000 }, options);
26
+ this.setupGlobalErrorHandlers();
20
27
  (0, dist_1.log_info)(`WebSocketClient constructor, url: ${this.url}`, this.options);
21
28
  }
22
29
  connect() {
23
30
  (0, dist_1.log_info)(`Connecting to ${this.url}`);
24
- this.ws = new ws_1.default(this.url, { headers: this.options.headers });
25
- this.ws.on('open', () => {
26
- (0, dist_1.log_info)('ws connected');
27
- this.reconnectAttempts = 0;
28
- this.startKeepAlive();
29
- if (this.onOpenCallback)
30
- this.onOpenCallback();
31
- });
32
- this.ws.on('message', (message) => {
33
- try {
34
- const data = JSON.parse(message.toString());
35
- if (this.onMessageCallback)
36
- this.onMessageCallback(data);
37
- }
38
- catch (err) {
39
- (0, dist_1.log_error)('Invalid message format:', err);
40
- }
41
- });
42
- this.ws.on('close', (code, reason) => {
43
- (0, dist_1.log_warn)(`ws disconnected: ${code} - ${reason.toString()}`);
44
- this.handleReconnect();
45
- });
46
- this.ws.on('error', (err) => {
47
- (0, dist_1.log_error)('ws error:', err);
48
- this.handleReconnect();
31
+ try {
32
+ this.ws = new ws_1.default(this.url, { headers: this.options.headers });
33
+ this.ws.on('open', () => {
34
+ (0, dist_1.log_info)('ws connected');
35
+ this.startKeepAlive();
36
+ if (this.onOpenCallback)
37
+ this.onOpenCallback();
38
+ });
39
+ this.ws.on('message', (message) => {
40
+ try {
41
+ const data = JSON.parse(message.toString());
42
+ if (this.onMessageCallback)
43
+ this.onMessageCallback(data);
44
+ }
45
+ catch (err) {
46
+ (0, dist_1.log_error)('Invalid message format:', err);
47
+ }
48
+ });
49
+ this.ws.on('close', (code, reason) => {
50
+ (0, dist_1.log_warn)(`ws disconnected: ${code} - ${reason.toString()}`);
51
+ this.reconnect();
52
+ });
53
+ this.ws.on('error', (err) => {
54
+ (0, dist_1.log_error)('ws error:', err);
55
+ this.reconnect();
56
+ });
57
+ }
58
+ catch (error) {
59
+ (0, dist_1.log_error)('Failed to create WebSocket:', error);
60
+ this.reconnect();
61
+ }
62
+ }
63
+ reconnect() {
64
+ return __awaiter(this, void 0, void 0, function* () {
65
+ (0, dist_1.log_warn)(`Reconnecting in ${this.options.reconnectInterval}ms`);
66
+ yield (0, dist_1.sleep)(this.options.reconnectInterval);
67
+ this.disconnect();
68
+ this.connect();
49
69
  });
50
70
  }
51
71
  onMessage(callback) {
@@ -65,21 +85,6 @@ class WebSocketClient {
65
85
  }
66
86
  }
67
87
  disconnect() {
68
- this.cleanup();
69
- }
70
- handleReconnect() {
71
- if (this.reconnectAttempts >= this.maxReconnectAttempts) {
72
- (0, dist_1.log_warn)(`Max reconnection attempts (${this.maxReconnectAttempts}) reached!`);
73
- return;
74
- }
75
- this.reconnectAttempts++;
76
- (0, dist_1.log_warn)(`Reconnecting in ${this.options.reconnectInterval}ms (attempt ${this.reconnectAttempts}/${this.maxReconnectAttempts})`);
77
- setTimeout(() => {
78
- this.cleanup();
79
- this.connect();
80
- }, this.options.reconnectInterval);
81
- }
82
- cleanup() {
83
88
  this.stopKeepAlive();
84
89
  if (this.ws) {
85
90
  this.ws.removeAllListeners();
@@ -89,7 +94,7 @@ class WebSocketClient {
89
94
  }
90
95
  }
91
96
  catch (error) {
92
- (0, dist_1.log_warn)(`Error closing WebSocket connection: ${error.message}`);
97
+ (0, dist_1.log_warn)(`Error closing WebSocket: ${error.message}`);
93
98
  }
94
99
  this.ws = null;
95
100
  }
@@ -118,5 +123,13 @@ class WebSocketClient {
118
123
  (0, dist_1.log_info)('KeepAlive stopped');
119
124
  }
120
125
  }
126
+ setupGlobalErrorHandlers() {
127
+ process.on('uncaughtException', (error) => {
128
+ (0, dist_1.log_error)('Uncaught Exception in WebSocketClient:', error);
129
+ });
130
+ process.on('unhandledRejection', (reason, promise) => {
131
+ (0, dist_1.log_error)('Unhandled Rejection in WebSocketClient:', reason instanceof Error ? reason : new Error(String(reason)));
132
+ });
133
+ }
121
134
  }
122
135
  exports.WebSocketClient = WebSocketClient;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-core",
3
- "version": "2.0.76",
3
+ "version": "2.0.78",
4
4
  "description": "Common types and utilities for trading systems - use `npm run push` to publish",
5
5
  "main": "dist/index.js",
6
6
  "types": "types/index.d.ts",