laplace-api 1.3.0 → 1.3.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "laplace-api",
3
- "version": "1.3.0",
3
+ "version": "1.3.2",
4
4
  "description": "Client library for Laplace API for the US stock market and BIST (Istanbul stock market) fundamental financial data.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,3 +1,5 @@
1
+ import { WebSocket } from "ws";
2
+
1
3
  export interface BISTStockLiveData {
2
4
  symbol: string;
3
5
  cl: number; // Close
@@ -62,12 +64,13 @@ export class LivePriceWebSocketClient {
62
64
  private closedReason: WebSocketCloseReason | null = null;
63
65
  private wsUrl: string | null = null;
64
66
  private readonly options: Required<WebSocketOptions>;
67
+ private connectPromise: Promise<void> | null = null;
65
68
 
66
69
  constructor(options: WebSocketOptions = {}) {
67
70
  this.options = {
68
71
  enableLogging: true,
69
72
  reconnectAttempts: 5,
70
- reconnectDelay: 1000,
73
+ reconnectDelay: 5000,
71
74
  maxReconnectDelay: 30000,
72
75
  ...options,
73
76
  };
@@ -97,7 +100,12 @@ export class LivePriceWebSocketClient {
97
100
 
98
101
  if (!this.ws || this.ws.readyState === WebSocket.CLOSED) {
99
102
  this.ws = new WebSocket(url);
100
- await this.setupWebSocket();
103
+ this.connectPromise = this.setupWebSocket();
104
+
105
+ await this.connectPromise
106
+
107
+ this.connectPromise = null;
108
+
101
109
  }
102
110
 
103
111
  return this.ws;
@@ -130,7 +138,7 @@ export class LivePriceWebSocketClient {
130
138
  this.ws.onerror = (error) => {
131
139
  reject(
132
140
  new WebSocketError(
133
- `WebSocket connection error: ${error}`,
141
+ `WebSocket connection error: ${error.error}`,
134
142
  WebSocketErrorType.CONNECTION_ERROR
135
143
  )
136
144
  );
@@ -258,7 +266,11 @@ export class LivePriceWebSocketClient {
258
266
  } catch (error) {
259
267
  this.attemptReconnect();
260
268
  }
261
- }, delay).unref();
269
+ }, delay);
270
+
271
+ if (typeof process !== "undefined") {
272
+ this.reconnectTimeout.unref();
273
+ }
262
274
  }
263
275
 
264
276
  subscribe(
@@ -304,6 +316,10 @@ export class LivePriceWebSocketClient {
304
316
  );
305
317
  }
306
318
 
319
+ if (this.connectPromise) {
320
+ await this.connectPromise
321
+ }
322
+
307
323
  if (this.ws.readyState !== WebSocket.OPEN) {
308
324
  throw new WebSocketError(
309
325
  "WebSocket is not connected",
@@ -328,6 +344,10 @@ export class LivePriceWebSocketClient {
328
344
  );
329
345
  }
330
346
 
347
+ if (this.connectPromise) {
348
+ await this.connectPromise
349
+ }
350
+
331
351
  if (this.ws.readyState !== WebSocket.OPEN) {
332
352
  throw new WebSocketError(
333
353
  "WebSocket is not connected",