market-data-tradingview-ws 2.1.2 → 2.1.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.
package/lib/client.js CHANGED
@@ -35,6 +35,7 @@ module.exports = class Client {
35
35
  frame: data.frame,
36
36
  i: data.i,
37
37
  deleted: data.deleted === true,
38
+ broken: data.broken === true,
38
39
  limit: Number(data.limit || 1500),
39
40
  requestedLimit: Number(data.requestedLimit || data.limit || 1500),
40
41
  historyPending: data.historyPending === true,
@@ -56,24 +57,37 @@ module.exports = class Client {
56
57
  data.end = null;
57
58
  return data;
58
59
  },
60
+ break({ key }) {
61
+ const data = this.values[key];
62
+ if (!data) return null;
63
+
64
+ data.deleted = true;
65
+ data.broken = true;
66
+ data.historyPending = false;
67
+ data.historyCompletionsPending = 0;
68
+ data.historyBars = {};
69
+ data.seriesCreated = false;
70
+ data.end = null;
71
+
72
+ return data;
73
+ },
59
74
  get({ key }) {
60
75
  return this.values[key];
61
76
  },
62
77
  getDeleted() {
63
78
  for (const key of Object.keys(this.values)) {
64
79
  const item = this.values[key];
65
-
66
- if (item?.deleted) {
80
+ if (item?.deleted && item?.broken !== true) {
67
81
  return { key, data: item };
68
82
  }
69
83
  }
70
-
71
84
  return null;
72
85
  },
73
86
  claimDeleted({ key }) {
74
- if (!this.values[key]) return null;
75
- this.values[key].deleted = false;
76
- return this.values[key];
87
+ const data = this.values[key];
88
+ if (!data || data.broken === true) return null;
89
+ data.deleted = false;
90
+ return data;
77
91
  },
78
92
  getBySymbolPeriod({ symbol, period }) {
79
93
  for (let key of Object.keys(this.values)) {
@@ -125,6 +139,9 @@ module.exports = class Client {
125
139
  #connectPromise = null;
126
140
  #connectTimeoutMs = 20_000;
127
141
 
142
+ #chartQueuePaused = false;
143
+ #chartQueuePauseReason = null;
144
+
128
145
  #stopWatchdog() {
129
146
  if (!this.#watchdogTimer) return;
130
147
  clearInterval(this.#watchdogTimer);
@@ -169,8 +186,14 @@ module.exports = class Client {
169
186
  #intentionalClose = false;
170
187
  #watchdogTimer = null;
171
188
  #lastMessageAt = 0;
189
+ #chartInitialLimitMax = 1500;
172
190
 
173
191
  constructor(config = {}) {
192
+ const initialLimitMax = Number(process.env.chart_initial_limit_max ?? 1500);
193
+ if (!Number.isSafeInteger(initialLimitMax) || initialLimitMax <= 0 || initialLimitMax > 40000) {
194
+ throw new RangeError('chart_initial_limit_max must be a positive safe integer no greater than 40000');
195
+ }
196
+ this.#chartInitialLimitMax = initialLimitMax;
174
197
  const cfg = config && typeof config === 'object' ? config : {};
175
198
  this.#configuredMaxChartSessions = utils.positive(cfg.max, this.#configuredMaxChartSessions);
176
199
  this.#maxChartSessions = this.#configuredMaxChartSessions;
@@ -202,6 +225,8 @@ module.exports = class Client {
202
225
  this.#symbols.values = {};
203
226
  this.#symbols.max = 1;
204
227
  this.#maxChartSessions = this.#configuredMaxChartSessions;
228
+ this.#chartQueuePaused = false;
229
+ this.#chartQueuePauseReason = null;
205
230
  this.#chartFrameMax = 0;
206
231
  this.#chartQueue = [];
207
232
  this.#queuedChartKeys.clear();
@@ -235,8 +260,9 @@ module.exports = class Client {
235
260
  const identity = this.#validateChartIdentity({ symbol, period });
236
261
  if (!identity.ok) return identity;
237
262
 
263
+ if (typeof limit !== 'number' && typeof limit !== 'string') return { ok: false, reason: 'invalid_limit' };
238
264
  const normalizedLimit = Number(limit);
239
- if (!Number.isInteger(normalizedLimit) || normalizedLimit <= 0 || normalizedLimit > 1500) return { ok: false, reason: 'invalid_limit' };
265
+ if (!Number.isSafeInteger(normalizedLimit) || normalizedLimit <= 0 || normalizedLimit > this.#chartInitialLimitMax) return { ok: false, reason: 'invalid_limit' };
240
266
 
241
267
  return { ...identity, limit: normalizedLimit };
242
268
  }
@@ -293,12 +319,11 @@ module.exports = class Client {
293
319
  }
294
320
 
295
321
  #scheduleQueue(delay = 0) {
296
- if (!this.#isOpen() || this.#queueTimer) return;
297
- this.#queueTimer = setTimeout(() => {
322
+ if (this.#chartQueuePaused || !this.#isOpen() || this.#queueTimer) return;
323
+ this.#queueTimer = setTimeout(() => {
298
324
  this.#queueTimer = null;
299
325
  void this.#processChartQueue();
300
326
  }, Math.max(0, delay));
301
-
302
327
  this.#queueTimer.unref?.();
303
328
  }
304
329
 
@@ -395,9 +420,9 @@ module.exports = class Client {
395
420
  }
396
421
 
397
422
  async #processChartQueue() {
398
- if (this.#queueProcessing || !this.#isOpen()) return;
399
- const generation = this.#connectGeneration;
400
- this.#queueProcessing = true;
423
+ if (this.#chartQueuePaused || this.#queueProcessing || !this.#isOpen()) return;
424
+ const generation = this.#connectGeneration;
425
+ this.#queueProcessing = true;
401
426
  try {
402
427
  const wait = this.#queueBlockedUntil - Date.now();
403
428
  if (wait > 0) {
@@ -406,7 +431,7 @@ module.exports = class Client {
406
431
  }
407
432
 
408
433
  while (this.#chartQueue.length > 0) {
409
- if (generation !== this.#connectGeneration || !this.#isOpen()) return;
434
+ if (this.#chartQueuePaused || generation !== this.#connectGeneration || !this.#isOpen()) return;
410
435
  const active = this.#countActiveCharts();
411
436
  if (active >= this.#maxChartSessions) {
412
437
  this.#logQueueState('queue_paused_capacity');
@@ -436,11 +461,32 @@ module.exports = class Client {
436
461
  } finally {
437
462
  if (generation === this.#connectGeneration) {
438
463
  this.#queueProcessing = false;
439
- if (this.#chartQueue.length > 0 && this.#isOpen()) this.#scheduleQueue(this.#queueRetryMs);
464
+ if (!this.#chartQueuePaused && this.#chartQueue.length > 0 && this.#isOpen()) this.#scheduleQueue(this.#queueRetryMs);
440
465
  }
441
466
  }
442
467
  }
443
468
 
469
+ pauseChartQueue({ reason = 'manual', extra = {} } = {}) {
470
+ this.#chartQueuePaused = true;
471
+ this.#chartQueuePauseReason = reason;
472
+ this.#stopQueueTimer();
473
+ this.#logQueueState('queue_paused', { reason, ...extra });
474
+ return this.getDebugState();
475
+ }
476
+
477
+ resumeChartQueue({ reason = 'manual', extra = {} } = {}) {
478
+ const previousReason = this.#chartQueuePauseReason;
479
+ this.#chartQueuePaused = false;
480
+ this.#chartQueuePauseReason = null;
481
+ this.#logQueueState('queue_resumed', { reason, previousReason, ...extra });
482
+ if (this.#chartQueue.length > 0) this.#scheduleQueue(0);
483
+ return this.getDebugState();
484
+ }
485
+
486
+ logQueueState(context = '', extra = {}) {
487
+ this.#logQueueState(context, extra);
488
+ }
489
+
444
490
  async close() {
445
491
  this.#intentionalClose = true;
446
492
  this.#connectGeneration++;
@@ -513,7 +559,10 @@ module.exports = class Client {
513
559
  tokenLen: this.#token ? this.#token.length : 0,
514
560
  sessionid: Boolean(this.#sessionid),
515
561
  sessionidsign: Boolean(this.#sessionidsign),
562
+ chartQueuePaused: this.#chartQueuePaused,
563
+ chartQueuePauseReason: this.#chartQueuePauseReason,
516
564
  chartsCount: charts.length,
565
+ brokenChartSessions: charts.filter((chart) => chart?.broken === true).length,
517
566
  activeChartSessions: charts.filter((chart) => chart && chart.deleted !== true).length,
518
567
  deletedChartSessions: charts.filter((chart) => chart?.deleted === true).length,
519
568
  quotesCount: Object.keys(this.#quotes.values).length,
@@ -939,11 +988,7 @@ module.exports = class Client {
939
988
  return { status: 'not_connected' };
940
989
  }
941
990
 
942
- this.#charts.set({
943
- key,
944
- data,
945
- });
946
-
991
+ this.#charts.set({ key, data });
947
992
  return { status: 'update', symbol: validation.symbol, period: validation.period, limit: validation.limit };
948
993
  }
949
994
 
@@ -1077,7 +1122,8 @@ module.exports = class Client {
1077
1122
  const period = parseInt(session.period);
1078
1123
  const message = detail.slice(2).map((value) => typeof value === 'string' ? value : JSON.stringify(value)).join(' ') || 'TradingView series error';
1079
1124
 
1080
- this.#charts.delete({ key: chartSessionKey });
1125
+ this.#charts.break({ key: chartSessionKey });
1126
+ this.#logQueueState('series_error_session_quarantined', { session: chartSessionKey, frame: errorFrame, symbol, period });
1081
1127
  this.#stopQueueTimer();
1082
1128
  this.#scheduleQueue(0);
1083
1129
  this.#emit('error', { type: 'chart_error', code: 'series_error', symbol, period, message, detail, retryable: false });
@@ -204,7 +204,6 @@ class TradingView {
204
204
  'lp_time',
205
205
  'rtc',
206
206
  'rtc_time',
207
- 'trade',
208
207
  'bid',
209
208
  'bid_size',
210
209
  'ask',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "market-data-tradingview-ws",
3
- "version": "2.1.2",
3
+ "version": "2.1.4",
4
4
  "description": "marketData from Tradingview ws",
5
5
  "main": "index.js",
6
6
  "scripts": {