nsekit 0.3.13 → 0.3.14
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.
|
@@ -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;
|
|
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;AAmBlF;;;;;;;0DAO0D;AAC1D,eAAO,MAAM,kBAAkB,QAAS,CAAC;AAEzC;0EAC0E;AAC1E,eAAO,MAAM,gBAAgB,QAAS,CAAC;AAIvC,qBAAa,cAAc;IACzB,OAAO,CAAC,EAAE,CAAmB;IAC7B,OAAO,CAAC,KAAK,CAAqC;IAClD,OAAO,CAAC,aAAa,CAAmC;IACxD,OAAO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AAmB;IAO3B,OAAO,CAAC,oBAAoB;IAW5B,OAAO,CAAC,mBAAmB;IAO3B;;;;;;;;;OASG;IACH,wBAAwB,IAAI,OAAO;CAkBpC"}
|
package/dist/index.js
CHANGED
|
@@ -21541,6 +21541,8 @@ function nextSubId3() {
|
|
|
21541
21541
|
subIdCounter3 += 1;
|
|
21542
21542
|
return `fsub_${subIdCounter3}`;
|
|
21543
21543
|
}
|
|
21544
|
+
var SILENCE_TIMEOUT_MS = 90000;
|
|
21545
|
+
var SILENCE_CHECK_MS = 15000;
|
|
21544
21546
|
|
|
21545
21547
|
class FinvasiaSocket {
|
|
21546
21548
|
ws = null;
|
|
@@ -21556,6 +21558,8 @@ class FinvasiaSocket {
|
|
|
21556
21558
|
reconnectTimer = null;
|
|
21557
21559
|
onAuthFailed;
|
|
21558
21560
|
egressIp;
|
|
21561
|
+
lastMessageAt = 0;
|
|
21562
|
+
silenceTimer = null;
|
|
21559
21563
|
constructor(userId, accessToken, onAuthFailed, authMethod = "totp", egressIp) {
|
|
21560
21564
|
this.userId = userId;
|
|
21561
21565
|
this.accessToken = accessToken;
|
|
@@ -21575,6 +21579,8 @@ class FinvasiaSocket {
|
|
|
21575
21579
|
return;
|
|
21576
21580
|
}
|
|
21577
21581
|
this.ws.on("open", () => {
|
|
21582
|
+
this.lastMessageAt = Date.now();
|
|
21583
|
+
this.startSilenceWatchdog();
|
|
21578
21584
|
console.log(`${TAG} WS open, sending auth for uid=${this.userId} (method=${this.authMethod})`);
|
|
21579
21585
|
const authMsg = this.authMethod === "oauth" ? JSON.stringify({
|
|
21580
21586
|
t: "a",
|
|
@@ -21592,6 +21598,7 @@ class FinvasiaSocket {
|
|
|
21592
21598
|
this.ws.send(authMsg);
|
|
21593
21599
|
});
|
|
21594
21600
|
this.ws.on("message", (raw) => {
|
|
21601
|
+
this.lastMessageAt = Date.now();
|
|
21595
21602
|
try {
|
|
21596
21603
|
const text = raw.toString();
|
|
21597
21604
|
console.log(`${TAG} WS msg: ${text.slice(0, 200)}`);
|
|
@@ -21600,6 +21607,7 @@ class FinvasiaSocket {
|
|
|
21600
21607
|
} catch {}
|
|
21601
21608
|
});
|
|
21602
21609
|
this.ws.on("close", (code, reason) => {
|
|
21610
|
+
this.stopSilenceWatchdog();
|
|
21603
21611
|
console.error(`${TAG} WS closed: code=${code}, reason=${reason.toString()}`);
|
|
21604
21612
|
this.state = "DISCONNECTED";
|
|
21605
21613
|
if (code === 1002 || code === 1008 || code === 4001) {
|
|
@@ -21616,6 +21624,7 @@ class FinvasiaSocket {
|
|
|
21616
21624
|
}
|
|
21617
21625
|
disconnect() {
|
|
21618
21626
|
this.clearReconnectTimer();
|
|
21627
|
+
this.stopSilenceWatchdog();
|
|
21619
21628
|
this.reconnectAttempts = this.maxReconnects;
|
|
21620
21629
|
if (this.ws) {
|
|
21621
21630
|
this.ws.close();
|
|
@@ -21771,6 +21780,31 @@ class FinvasiaSocket {
|
|
|
21771
21780
|
this.reconnectTimer = null;
|
|
21772
21781
|
}
|
|
21773
21782
|
}
|
|
21783
|
+
startSilenceWatchdog() {
|
|
21784
|
+
this.stopSilenceWatchdog();
|
|
21785
|
+
this.silenceTimer = setInterval(() => this.checkSilenceAndReconnect(), SILENCE_CHECK_MS);
|
|
21786
|
+
this.silenceTimer.unref?.();
|
|
21787
|
+
}
|
|
21788
|
+
stopSilenceWatchdog() {
|
|
21789
|
+
if (this.silenceTimer) {
|
|
21790
|
+
clearInterval(this.silenceTimer);
|
|
21791
|
+
this.silenceTimer = null;
|
|
21792
|
+
}
|
|
21793
|
+
}
|
|
21794
|
+
checkSilenceAndReconnect() {
|
|
21795
|
+
if (this.state !== "CONNECTED")
|
|
21796
|
+
return false;
|
|
21797
|
+
if (!this.ws)
|
|
21798
|
+
return false;
|
|
21799
|
+
const idleMs = Date.now() - this.lastMessageAt;
|
|
21800
|
+
if (idleMs < SILENCE_TIMEOUT_MS)
|
|
21801
|
+
return false;
|
|
21802
|
+
console.warn(`${TAG} No message for ${Math.round(idleMs / 1000)}s \u2014 forcing reconnect (silence watchdog)`);
|
|
21803
|
+
try {
|
|
21804
|
+
this.ws.close(4000, "silence-timeout");
|
|
21805
|
+
} catch {}
|
|
21806
|
+
return true;
|
|
21807
|
+
}
|
|
21774
21808
|
}
|
|
21775
21809
|
|
|
21776
21810
|
// src/brokers/finvasia/finvasia-instruments.ts
|