@icgio/icg-exchanges-wrapper 1.14.255 → 1.14.256

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.
@@ -25,11 +25,48 @@ const get_exchanges_rates = function (Exchanges, exchange_pair_dict, cb, ws_enab
25
25
  }
26
26
  if (Exchange.ws_market && ws_enabled) {
27
27
  let ws_promise = new Promise((resolve, reject) => {
28
+ if (!Exchange.ws || !Exchange.ws.market) {
29
+ console.error(`${Exchange_name} WebSocket not properly initialized`)
30
+ reject(new Error(`WebSocket not initialized for ${Exchange_name}`))
31
+ return
32
+ }
33
+
28
34
  Exchange.ws_market(exchange_pair_dict[Exchange_name])
29
35
 
36
+ const WS_TIMEOUT_MS = 30000 // 30 seconds timeout
37
+ const start_time = Date.now()
38
+ let check_count = 0
39
+ const MAX_CHECKS = 300 // 30 seconds / 100ms
40
+
30
41
  const check_status = () => {
42
+ check_count++
43
+ const elapsed = Date.now() - start_time
44
+
45
+ // Timeout check
46
+ if (elapsed > WS_TIMEOUT_MS || check_count > MAX_CHECKS) {
47
+ console.error(`WebSocket timeout for ${Exchange_name} after ${elapsed}ms`)
48
+ reject(new Error(`WebSocket timeout for ${Exchange_name}`))
49
+ return
50
+ }
51
+
52
+ // Safety check for WebSocket object
53
+ if (!Exchange.ws || !Exchange.ws.market) {
54
+ console.error(`${Exchange_name} WebSocket object missing during check`)
55
+ reject(new Error(`WebSocket object missing for ${Exchange_name}`))
56
+ return
57
+ }
58
+
31
59
  let all_pairs_opened = exchange_pair_dict[Exchange_name].every((pair) => {
32
- return Exchange.ws.market.pair_status[pair] === 'opened' && Exchange.ws.market.asks_dict[pair].length > 0 && Exchange.ws.market.bids_dict[pair].length > 0
60
+ return (
61
+ Exchange.ws.market.pair_status &&
62
+ Exchange.ws.market.pair_status[pair] === 'opened' &&
63
+ Exchange.ws.market.asks_dict &&
64
+ Exchange.ws.market.asks_dict[pair] &&
65
+ Exchange.ws.market.asks_dict[pair].length > 0 &&
66
+ Exchange.ws.market.bids_dict &&
67
+ Exchange.ws.market.bids_dict[pair] &&
68
+ Exchange.ws.market.bids_dict[pair].length > 0
69
+ )
33
70
  })
34
71
  if (all_pairs_opened) {
35
72
  let rate_promises = exchange_pair_dict[Exchange_name].map((pair) => {
@@ -129,6 +166,7 @@ const get_exchanges_rates = function (Exchanges, exchange_pair_dict, cb, ws_enab
129
166
  })
130
167
  .catch((error) => {
131
168
  console.error('Error in fetching exchange rates:', error)
169
+ // Still call callback with available data even if some promises failed
132
170
  cb({ exchange_pair_rates, pair_exchange_rates })
133
171
  })
134
172
  }
@@ -154,13 +192,48 @@ const get_exchanges_market_history = function (Exchanges, exchange_pair_dict, ti
154
192
  }
155
193
  if (Exchange.ws_market && ws_enabled) {
156
194
  let ws_promise = new Promise((resolve, reject) => {
195
+ if (!Exchange.ws || !Exchange.ws.market) {
196
+ console.error(`${Exchange_name} WebSocket not properly initialized`)
197
+ reject(new Error(`WebSocket not initialized for ${Exchange_name}`))
198
+ return
199
+ }
200
+
157
201
  if (!first_time[Exchange_name]) {
158
202
  Exchange.ws_market(exchange_pair_dict[Exchange_name])
159
203
  first_time[Exchange_name] = true
160
204
  }
205
+
206
+ const WS_TIMEOUT_MS = 30000 // 30 seconds timeout
207
+ const start_time = Date.now()
208
+ let check_count = 0
209
+ const MAX_CHECKS = 300 // 30 seconds / 100ms
210
+
161
211
  const check_status = () => {
212
+ check_count++
213
+ const elapsed = Date.now() - start_time
214
+
215
+ // Timeout check
216
+ if (elapsed > WS_TIMEOUT_MS || check_count > MAX_CHECKS) {
217
+ console.error(`WebSocket timeout for ${Exchange_name} market history after ${elapsed}ms`)
218
+ reject(new Error(`WebSocket timeout for ${Exchange_name} market history`))
219
+ return
220
+ }
221
+
222
+ // Safety check for WebSocket object
223
+ if (!Exchange.ws || !Exchange.ws.market) {
224
+ console.error(`${Exchange_name} WebSocket object missing during check`)
225
+ reject(new Error(`WebSocket object missing for ${Exchange_name}`))
226
+ return
227
+ }
228
+
162
229
  let all_pairs_opened = exchange_pair_dict[Exchange_name].every((pair) => {
163
- return Exchange.ws.market.pair_status[pair] === 'opened' && Exchange.ws.market.market_history_dict[pair].length >= 0
230
+ return (
231
+ Exchange.ws.market.pair_status &&
232
+ Exchange.ws.market.pair_status[pair] === 'opened' &&
233
+ Exchange.ws.market.market_history_dict &&
234
+ Exchange.ws.market.market_history_dict[pair] &&
235
+ Exchange.ws.market.market_history_dict[pair].length >= 0
236
+ )
164
237
  })
165
238
  if (all_pairs_opened) {
166
239
  let market_history_promises = exchange_pair_dict[Exchange_name].map((pair) => {
@@ -237,6 +310,8 @@ let checked_ohlcv_exchange = {},
237
310
  pair_exchange_ohlcv = {}
238
311
  const get_exchanges_ohlcv = function (Exchanges, exchange_pair_dict, from_in_ms, to_in_ms, cb) {
239
312
  checked_ohlcv_exchange = {}
313
+ ohlcv_status = {}
314
+ ohlcv_status._callback_fired = false // Reset callback flag
240
315
  if (!Array.isArray(Exchanges)) {
241
316
  console.error('Exchanges should be an array')
242
317
  return
@@ -267,13 +342,17 @@ const get_exchanges_ohlcv = function (Exchanges, exchange_pair_dict, from_in_ms,
267
342
  }
268
343
  }
269
344
  _.set(ohlcv_status, [Exchange_name + pair], true)
270
- if (!_.includes(_.values(ohlcv_status), false)) {
345
+ // Use a flag to prevent multiple callback calls
346
+ if (!_.includes(_.values(ohlcv_status), false) && !ohlcv_status._callback_fired) {
347
+ ohlcv_status._callback_fired = true
271
348
  cb({ exchange_pair_ohlcv, pair_exchange_ohlcv })
272
349
  }
273
350
  })
274
351
  } else {
275
352
  _.set(ohlcv_status, [Exchange_name + pair], true)
276
- if (!_.includes(_.values(ohlcv_status), false)) {
353
+ // Use a flag to prevent multiple callback calls
354
+ if (!_.includes(_.values(ohlcv_status), false) && !ohlcv_status._callback_fired) {
355
+ ohlcv_status._callback_fired = true
277
356
  cb({ exchange_pair_ohlcv, pair_exchange_ohlcv })
278
357
  }
279
358
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-exchanges-wrapper",
3
- "version": "1.14.255",
3
+ "version": "1.14.256",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {