nsekit 0.3.13 → 0.3.15

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.
@@ -1 +1 @@
1
- {"version":3,"file":"finvasia-auth.d.ts","sourceRoot":"","sources":["../../../src/brokers/finvasia/finvasia-auth.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACzE,OAAO,EAAW,KAAK,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAE3D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAIzD;iEACiE;AACjE,MAAM,WAAW,sBAAsB;IACrC;;8BAE0B;IAC1B,UAAU,CAAC,EAAE,UAAU,CAAC;CACzB;AA+DD;;;;;;;;GAQG;AACH,wBAAsB,YAAY,CAChC,KAAK,EAAE,iBAAiB,EACxB,IAAI,CAAC,EAAE,sBAAsB,GAC5B,OAAO,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,CA2E9B;AAED;;;GAGG;AACH,wBAAsB,cAAc,CAAC,QAAQ,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,CAExF;AAID;;;;;;;;;;;;GAYG;AACH,wBAAgB,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAMpD;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,gBAAgB,CACpC,QAAQ,EAAE,MAAM,EAChB,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,MAAM,EAChB,GAAG,EAAE,MAAM,EACX,IAAI,CAAC,EAAE,sBAAsB,GAC5B,OAAO,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,CA4E9B;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,CACnC,WAAW,EAAE,MAAM,EACnB,GAAG,EAAE,MAAM,EACX,YAAY,CAAC,EAAE,MAAM,GAC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CAUb;AAED,uDAAuD;AACvD,wBAAgB,cAAc,CAAC,OAAO,EAAE,WAAW,GAAG,IAAI,GAAG,OAAO,CAGnE"}
@@ -5,6 +5,18 @@
5
5
  */
6
6
  import type { WSConnectionState } from '../../types/common';
7
7
  import type { TickCallback, Subscription } from '../../types/broker';
8
+ /** Force-close the socket if no message is received for this long.
9
+ * Finvasia sends `{t:'h'}` heartbeats every ~30-60s, so a 90s gap
10
+ * means the server has silently stopped delivering frames — TCP alive,
11
+ * data pipe dead. Observed in prod (Sharkfin FA96383 2026-08-25 and
12
+ * again 2026-09-08): no close event fires, ticks just stop, and the
13
+ * client sits forever unless someone restarts the container.
14
+ * Force-close routes through the existing `on('close')` handler which
15
+ * runs `attemptReconnect()` with exponential backoff. */
16
+ export declare const SILENCE_TIMEOUT_MS = 90000;
17
+ /** How often the silence watchdog wakes up to check. Should be well
18
+ * under SILENCE_TIMEOUT_MS so recovery is prompt without being noisy. */
19
+ export declare const SILENCE_CHECK_MS = 15000;
8
20
  export declare class FinvasiaSocket {
9
21
  private ws;
10
22
  private state;
@@ -19,6 +31,8 @@ export declare class FinvasiaSocket {
19
31
  private reconnectTimer;
20
32
  private readonly onAuthFailed?;
21
33
  private readonly egressIp;
34
+ private lastMessageAt;
35
+ private silenceTimer;
22
36
  constructor(userId: string, accessToken: string, onAuthFailed?: () => void, authMethod?: 'totp' | 'oauth', egressIp?: string);
23
37
  /** Connect to the Shoonya WebSocket endpoint. */
24
38
  connect(): void;
@@ -46,5 +60,18 @@ export declare class FinvasiaSocket {
46
60
  private dispatchTick;
47
61
  private attemptReconnect;
48
62
  private clearReconnectTimer;
63
+ private startSilenceWatchdog;
64
+ private stopSilenceWatchdog;
65
+ /**
66
+ * Silence watchdog: if we're CONNECTED but no message has arrived for
67
+ * SILENCE_TIMEOUT_MS, force-close the socket. The existing `on('close')`
68
+ * handler then runs `attemptReconnect()` with exponential backoff.
69
+ *
70
+ * Public so tests can trigger it deterministically without waiting on
71
+ * a real interval. Returns true if silence was detected and a
72
+ * force-close was issued, false otherwise (recent traffic, not
73
+ * CONNECTED, or no socket to close).
74
+ */
75
+ checkSilenceAndReconnect(): boolean;
49
76
  }
50
77
  //# sourceMappingURL=finvasia-socket.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"finvasia-socket.d.ts","sourceRoot":"","sources":["../../../src/brokers/finvasia/finvasia-socket.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAC5D,OAAO,KAAK,EAAe,YAAY,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAqBlF,qBAAa,cAAc;IACzB,OAAO,CAAC,EAAE,CAAmB;IAC7B,OAAO,CAAC,KAAK,CAAqC;IAClD,OAAO,CAAC,aAAa,CAAmC;IACxD,OAAO,CAAC,aAAa,CAA6B;IAClD,OAAO,CAAC,gBAAgB,CAAqB;IAE7C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,UAAU,CAAmB;IACrC,OAAO,CAAC,iBAAiB,CAAK;IAC9B,OAAO,CAAC,aAAa,CAAK;IAC1B,OAAO,CAAC,cAAc,CAA8C;IACpE,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAa;IAC3C,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAqB;gBAG5C,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,MAAM,EACnB,YAAY,CAAC,EAAE,MAAM,IAAI,EACzB,UAAU,GAAE,MAAM,GAAG,OAAgB,EACrC,QAAQ,CAAC,EAAE,MAAM;IASnB,iDAAiD;IACjD,OAAO,IAAI,IAAI;IA2Ff,6BAA6B;IAC7B,UAAU,IAAI,IAAI;IAWlB;;;OAGG;IACH,SAAS,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI;IAYlC;;;OAGG;IACH,SAAS,CAAC,MAAM,EAAE,MAA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AAC,QAAQ,CAAC,YAAY,CAAC,CAAa;IAC3C,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAqB;IAG9C,OAAO,CAAC,aAAa,CAAK;IAC1B,OAAO,CAAC,YAAY,CAA+C;gBAGjE,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,MAAM,EACnB,YAAY,CAAC,EAAE,MAAM,IAAI,EACzB,UAAU,GAAE,MAAM,GAAG,OAAgB,EACrC,QAAQ,CAAC,EAAE,MAAM;IASnB,iDAAiD;IACjD,OAAO,IAAI,IAAI;IAyGf,6BAA6B;IAC7B,UAAU,IAAI,IAAI;IAYlB;;;OAGG;IACH,SAAS,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI;IAYlC;;;OAGG;IACH,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,QAAQ,EAAE,YAAY,EAAE,SAAS,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,YAAY;IA2BlG,0DAA0D;IAC1D,WAAW,CAAC,GAAG,EAAE,YAAY,GAAG,IAAI;IAuBpC,gCAAgC;IAChC,kBAAkB,IAAI,iBAAiB;IAIvC,oDAAoD;IACpD,YAAY,CAAC,GAAG,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAAI;IAe5C,OAAO,CAAC,aAAa;IAsDrB,OAAO,CAAC,aAAa;IAWrB,OAAO,CAAC,eAAe;IAUvB,OAAO,CAAC,YAAY;IAYpB,OAAO,CAAC,gBAAgB;IAYxB,OAAO,CAAC,mBAAmB;IAO3B,OAAO,CAAC,oBAAoB;IAW5B,OAAO,CAAC,mBAAmB;IAO3B;;;;;;;;;OASG;IACH,wBAAwB,IAAI,OAAO;CAkBpC"}
package/dist/index.js CHANGED
@@ -21238,6 +21238,23 @@ async function authenticate2(creds, opts) {
21238
21238
  async function refreshSession2(_session) {
21239
21239
  return Err(new Error("Finvasia/Shoonya does not support session refresh. Re-authenticate with TOTP."));
21240
21240
  }
21241
+ async function fetchOAuthAccountName(accessToken, uid, opts) {
21242
+ try {
21243
+ const response = await fetch(`${SHOONYA_API_BASE3}/UserDetails`, {
21244
+ method: "POST",
21245
+ headers: {
21246
+ "Content-Type": "application/x-www-form-urlencoded",
21247
+ Authorization: `Bearer ${accessToken}`
21248
+ },
21249
+ body: `jData=${JSON.stringify({ uid })}`,
21250
+ dispatcher: opts?.dispatcher
21251
+ });
21252
+ const json = JSON.parse(await response.text());
21253
+ return json.stat === "Ok" && typeof json.uname === "string" ? json.uname : undefined;
21254
+ } catch {
21255
+ return;
21256
+ }
21257
+ }
21241
21258
  function getOAuthURL(clientId) {
21242
21259
  const uid = clientId.endsWith("_U") ? clientId.slice(0, -2) : clientId;
21243
21260
  return `${SHOONYA_OAUTH_BASE}?api_key=${encodeURIComponent(clientId)}&route_to=${encodeURIComponent(uid)}`;
@@ -21290,17 +21307,22 @@ async function exchangeAuthCode(authCode, secretCode, clientId, uid, opts) {
21290
21307
  }
21291
21308
  return Err(new Error(friendly));
21292
21309
  }
21310
+ const resolvedUserId = String(json.USERID ?? json.uid ?? uid);
21311
+ let accountName = typeof json.uname === "string" ? json.uname : undefined;
21312
+ if (!accountName) {
21313
+ accountName = await fetchOAuthAccountName(String(json.access_token), resolvedUserId, opts);
21314
+ }
21293
21315
  const session = {
21294
21316
  accessToken: String(json.access_token),
21295
21317
  refreshToken: json.refresh_token ? String(json.refresh_token) : undefined,
21296
21318
  expiresAt: getEndOfDay2(),
21297
21319
  ttlSeconds: secondsUntilEndOfDay2(),
21298
21320
  brokerId: "finvasia",
21299
- userId: String(json.USERID ?? json.uid ?? uid),
21300
- accountName: typeof json.uname === "string" ? json.uname : undefined,
21321
+ userId: resolvedUserId,
21322
+ accountName,
21301
21323
  metadata: {
21302
21324
  actid: json.actid,
21303
- uname: json.uname,
21325
+ uname: accountName ?? json.uname,
21304
21326
  brkname: json.brkname,
21305
21327
  lastAccess: json.lastaccesstime,
21306
21328
  authMethod: "oauth"
@@ -21541,6 +21563,8 @@ function nextSubId3() {
21541
21563
  subIdCounter3 += 1;
21542
21564
  return `fsub_${subIdCounter3}`;
21543
21565
  }
21566
+ var SILENCE_TIMEOUT_MS = 90000;
21567
+ var SILENCE_CHECK_MS = 15000;
21544
21568
 
21545
21569
  class FinvasiaSocket {
21546
21570
  ws = null;
@@ -21556,6 +21580,8 @@ class FinvasiaSocket {
21556
21580
  reconnectTimer = null;
21557
21581
  onAuthFailed;
21558
21582
  egressIp;
21583
+ lastMessageAt = 0;
21584
+ silenceTimer = null;
21559
21585
  constructor(userId, accessToken, onAuthFailed, authMethod = "totp", egressIp) {
21560
21586
  this.userId = userId;
21561
21587
  this.accessToken = accessToken;
@@ -21575,6 +21601,8 @@ class FinvasiaSocket {
21575
21601
  return;
21576
21602
  }
21577
21603
  this.ws.on("open", () => {
21604
+ this.lastMessageAt = Date.now();
21605
+ this.startSilenceWatchdog();
21578
21606
  console.log(`${TAG} WS open, sending auth for uid=${this.userId} (method=${this.authMethod})`);
21579
21607
  const authMsg = this.authMethod === "oauth" ? JSON.stringify({
21580
21608
  t: "a",
@@ -21592,6 +21620,7 @@ class FinvasiaSocket {
21592
21620
  this.ws.send(authMsg);
21593
21621
  });
21594
21622
  this.ws.on("message", (raw) => {
21623
+ this.lastMessageAt = Date.now();
21595
21624
  try {
21596
21625
  const text = raw.toString();
21597
21626
  console.log(`${TAG} WS msg: ${text.slice(0, 200)}`);
@@ -21600,6 +21629,7 @@ class FinvasiaSocket {
21600
21629
  } catch {}
21601
21630
  });
21602
21631
  this.ws.on("close", (code, reason) => {
21632
+ this.stopSilenceWatchdog();
21603
21633
  console.error(`${TAG} WS closed: code=${code}, reason=${reason.toString()}`);
21604
21634
  this.state = "DISCONNECTED";
21605
21635
  if (code === 1002 || code === 1008 || code === 4001) {
@@ -21616,6 +21646,7 @@ class FinvasiaSocket {
21616
21646
  }
21617
21647
  disconnect() {
21618
21648
  this.clearReconnectTimer();
21649
+ this.stopSilenceWatchdog();
21619
21650
  this.reconnectAttempts = this.maxReconnects;
21620
21651
  if (this.ws) {
21621
21652
  this.ws.close();
@@ -21771,6 +21802,31 @@ class FinvasiaSocket {
21771
21802
  this.reconnectTimer = null;
21772
21803
  }
21773
21804
  }
21805
+ startSilenceWatchdog() {
21806
+ this.stopSilenceWatchdog();
21807
+ this.silenceTimer = setInterval(() => this.checkSilenceAndReconnect(), SILENCE_CHECK_MS);
21808
+ this.silenceTimer.unref?.();
21809
+ }
21810
+ stopSilenceWatchdog() {
21811
+ if (this.silenceTimer) {
21812
+ clearInterval(this.silenceTimer);
21813
+ this.silenceTimer = null;
21814
+ }
21815
+ }
21816
+ checkSilenceAndReconnect() {
21817
+ if (this.state !== "CONNECTED")
21818
+ return false;
21819
+ if (!this.ws)
21820
+ return false;
21821
+ const idleMs = Date.now() - this.lastMessageAt;
21822
+ if (idleMs < SILENCE_TIMEOUT_MS)
21823
+ return false;
21824
+ console.warn(`${TAG} No message for ${Math.round(idleMs / 1000)}s \u2014 forcing reconnect (silence watchdog)`);
21825
+ try {
21826
+ this.ws.close(4000, "silence-timeout");
21827
+ } catch {}
21828
+ return true;
21829
+ }
21774
21830
  }
21775
21831
 
21776
21832
  // src/brokers/finvasia/finvasia-instruments.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nsekit",
3
- "version": "0.3.13",
3
+ "version": "0.3.15",
4
4
  "description": "Unified broker abstraction for Indian stock markets - CCXT-like interface for NSE/BSE brokers",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",