market-data-tradingview-ws 2.1.0 → 2.1.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/lib/client.js CHANGED
@@ -77,7 +77,6 @@ module.exports = class Client {
77
77
  },
78
78
  getBySymbolPeriod({ symbol, period }) {
79
79
  for (let key of Object.keys(this.values)) {
80
- // console.warn(symbol, period, key, this.values[key], this.values[key].symbol === symbol, this.values[key].period === period);
81
80
  if (this.values[key].symbol === symbol && parseInt(this.values[key].period) === parseInt(period) && !this.values[key].deleted) {
82
81
  return key;
83
82
  }
@@ -319,19 +318,13 @@ module.exports = class Client {
319
318
  if (!socket || socket.readyState !== WebSocket.OPEN) return false;
320
319
 
321
320
  if (socket.bufferedAmount > this.#maxBufferedAmount) {
322
- this.#emit('error', {
323
- type: 'ws_backpressure',
324
- bufferedAmount: socket.bufferedAmount,
325
- });
321
+ this.#emit('error', { type: 'ws_backpressure', bufferedAmount: socket.bufferedAmount });
326
322
  return false;
327
323
  }
328
324
 
329
325
  socket.send(data, (error) => {
330
326
  if (!error) return;
331
- this.#emit('error', {
332
- type: 'ws_send_error',
333
- message: error.message,
334
- });
327
+ this.#emit('error', { type: 'ws_send_error', message: error.message });
335
328
  });
336
329
 
337
330
  return true;
@@ -545,7 +538,7 @@ module.exports = class Client {
545
538
  });
546
539
  }
547
540
 
548
- async connect({ url, options = {}, result, sessionid, sessionidsign, tvecuid, imageurl, proxy }) {
541
+ async connect({ url, options = {}, result, sessionid, sessionidsign, tvecuid, imageurl, proxy, token }) {
549
542
  if (this.#isOpen()) return 'open';
550
543
  if (this.#connectPromise) return this.#connectPromise;
551
544
  if (typeof result !== 'function') throw new TypeError('result callback must be a function');
@@ -555,7 +548,7 @@ module.exports = class Client {
555
548
  const controller = new AbortController();
556
549
  this.#connectAbortController = controller;
557
550
 
558
- this.#connectPromise = this.#connectInternal({ url, options, result, sessionid, sessionidsign, tvecuid, imageurl, proxy, generation, signal: controller.signal });
551
+ this.#connectPromise = this.#connectInternal({ url, options, result, sessionid, sessionidsign, tvecuid, imageurl, proxy, token, generation, signal: controller.signal });
559
552
 
560
553
  try {
561
554
  return await this.#connectPromise;
@@ -565,18 +558,24 @@ module.exports = class Client {
565
558
  }
566
559
  }
567
560
 
568
- async #connectInternal({ url, options, result, sessionid, sessionidsign, tvecuid, imageurl, proxy, generation, signal }) {
561
+ async #connectInternal({ url, options, result, sessionid, sessionidsign, tvecuid, imageurl, proxy, token, generation, signal }) {
569
562
  this.#result = result;
570
563
  this.#proxy = proxy || null;
571
564
 
572
- this.#token = await this.getToken({ sessionId: sessionid, sessionIdSign: sessionidsign, tvEcuid: tvecuid, imageUrl: imageurl, proxy, signal });
573
-
565
+ if (typeof token === 'string' && token.length > 10) {
566
+ this.#token = token;
567
+ } else {
568
+ this.#token = await this.getToken({ sessionId: sessionid, sessionIdSign: sessionidsign, tvEcuid: tvecuid, imageUrl: imageurl, proxy, signal });
569
+ }
570
+
574
571
  this.#assertConnectGeneration(generation);
575
572
 
576
573
  this.#sessionid = sessionid;
577
574
  this.#sessionidsign = sessionidsign;
578
575
 
579
- if (!this.#token || this.#token.length <= 10) throw new Error('Invalid TradingView auth token');
576
+ if (!this.#token || this.#token.length <= 10) {
577
+ throw new Error('Invalid TradingView auth token');
578
+ }
580
579
 
581
580
  this.#resetRemoteState();
582
581
 
@@ -764,9 +763,12 @@ module.exports = class Client {
764
763
  const normalized = symbol.trim();
765
764
  const key = this.#quotes.getBySymbol({ symbol: normalized });
766
765
  if (!key) return 'unlisted';
767
- this.#quotes.delete({ key, symbol: normalized });
766
+
768
767
  const sent = this.#send(tv.quoteRemoveSymbols({ session: key, symbol: normalized }));
769
- return sent ? 'delete' : 'not_connected';
768
+ if (!sent) return 'not_connected';
769
+
770
+ this.#quotes.delete({ key, symbol: normalized });
771
+ return 'delete';
770
772
  }
771
773
 
772
774
  #reuseDeletedChartSession({ key, symbol, period, limit }) {
@@ -256,18 +256,16 @@ class TradingView {
256
256
  });
257
257
  }
258
258
 
259
+ quoteSymbol({ symbol }) {
260
+ return symbol.split(':')[0] === 'BINANCE' ? symbol : '={"adjustment":"splits","currency-id":"USD","symbol":"' + symbol + '"}';
261
+ }
262
+
259
263
  quoteAddSymbol({ session, symbol }) {
260
- const symbolString =
261
- symbol.split(':')[0] === 'BINANCE' ? symbol : '={"adjustment":"splits","currency-id":"USD","symbol":"' + symbol + '"}';
262
- // console.log('quoteAddSymbol', session, symbol, symbolString);
263
- // return this.formatWSPacket({ packet: { m: 'quote_add_symbols', p: [session, symbol] } });
264
- return this.formatWSPacket({
265
- packet: { m: 'quote_add_symbols', p: [session, symbolString] },
266
- });
264
+ return this.formatWSPacket({ packet: { m: 'quote_add_symbols', p: [session, this.quoteSymbol({ symbol })] }});
267
265
  }
268
266
 
269
267
  quoteRemoveSymbols({ session, symbol }) {
270
- return this.formatWSPacket({ packet: { m: 'quote_remove_symbols', p: [session, symbol] } });
268
+ return this.formatWSPacket({ packet: { m: 'quote_remove_symbols', p: [session, this.quoteSymbol({ symbol })] }});
271
269
  }
272
270
 
273
271
  quoteRemoveSeries({ session }) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "market-data-tradingview-ws",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
4
4
  "description": "marketData from Tradingview ws",
5
5
  "main": "index.js",
6
6
  "scripts": {