@agentvault/secure-channel 0.6.16 → 0.6.18

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/dist/index.js CHANGED
@@ -45113,7 +45113,7 @@ var SecureChannel = class _SecureChannel extends EventEmitter {
45113
45113
  _reconnectAttempt = 0;
45114
45114
  _reconnectTimer = null;
45115
45115
  _pingTimer = null;
45116
- _pingTimeout = null;
45116
+ _lastServerMessage = 0;
45117
45117
  _pendingAcks = [];
45118
45118
  _ackTimer = null;
45119
45119
  _stopped = false;
@@ -45121,10 +45121,10 @@ var SecureChannel = class _SecureChannel extends EventEmitter {
45121
45121
  _httpServer = null;
45122
45122
  _pollFallbackTimer = null;
45123
45123
  _syncMessageIds = null;
45124
+ // Liveness detection: server sends app-level {"event":"ping"} every 30s.
45125
+ // We check every 30s; if no data received in 90s (3 missed pings), connection is dead.
45124
45126
  static PING_INTERVAL_MS = 3e4;
45125
- // Send ping every 30s
45126
- static PING_TIMEOUT_MS = 1e4;
45127
- // Treat as dead if no pong within 10s
45127
+ static SILENCE_TIMEOUT_MS = 9e4;
45128
45128
  static POLL_FALLBACK_INTERVAL_MS = 3e4;
45129
45129
  // 30s when messages found
45130
45130
  static POLL_FALLBACK_IDLE_MS = 6e4;
@@ -45579,6 +45579,10 @@ var SecureChannel = class _SecureChannel extends EventEmitter {
45579
45579
  }
45580
45580
  _connect() {
45581
45581
  if (this._stopped) return;
45582
+ if (this._reconnectTimer) {
45583
+ clearTimeout(this._reconnectTimer);
45584
+ this._reconnectTimer = null;
45585
+ }
45582
45586
  if (this._ws) {
45583
45587
  this._ws.removeAllListeners();
45584
45588
  try {
@@ -45601,6 +45605,7 @@ var SecureChannel = class _SecureChannel extends EventEmitter {
45601
45605
  this.emit("ready");
45602
45606
  });
45603
45607
  ws.on("message", async (raw) => {
45608
+ this._lastServerMessage = Date.now();
45604
45609
  try {
45605
45610
  const data = JSON.parse(raw.toString());
45606
45611
  if (data.event === "ping") {
@@ -46091,30 +46096,23 @@ ${messageText}`;
46091
46096
  }
46092
46097
  _startPing(ws) {
46093
46098
  this._stopPing();
46099
+ this._lastServerMessage = Date.now();
46094
46100
  this._pingTimer = setInterval(() => {
46095
46101
  if (ws.readyState !== WebSocket.OPEN) return;
46096
- this._pingTimeout = setTimeout(() => {
46097
- console.log("[SecureChannel] Ping timeout \u2014 reconnecting stale WebSocket");
46102
+ const silence = Date.now() - this._lastServerMessage;
46103
+ if (silence > _SecureChannel.SILENCE_TIMEOUT_MS) {
46104
+ console.log(
46105
+ `[SecureChannel] No server data for ${Math.round(silence / 1e3)}s \u2014 reconnecting stale WebSocket`
46106
+ );
46098
46107
  ws.terminate();
46099
- }, _SecureChannel.PING_TIMEOUT_MS);
46100
- ws.ping();
46101
- }, _SecureChannel.PING_INTERVAL_MS);
46102
- ws.on("pong", () => {
46103
- if (this._pingTimeout) {
46104
- clearTimeout(this._pingTimeout);
46105
- this._pingTimeout = null;
46106
46108
  }
46107
- });
46109
+ }, _SecureChannel.PING_INTERVAL_MS);
46108
46110
  }
46109
46111
  _stopPing() {
46110
46112
  if (this._pingTimer) {
46111
46113
  clearInterval(this._pingTimer);
46112
46114
  this._pingTimer = null;
46113
46115
  }
46114
- if (this._pingTimeout) {
46115
- clearTimeout(this._pingTimeout);
46116
- this._pingTimeout = null;
46117
- }
46118
46116
  }
46119
46117
  _scheduleReconnect() {
46120
46118
  if (this._stopped) return;
@@ -46124,7 +46122,9 @@ ${messageText}`;
46124
46122
  RECONNECT_MAX_MS
46125
46123
  );
46126
46124
  this._reconnectAttempt++;
46125
+ console.log(`[SecureChannel] Scheduling reconnect in ${delay}ms (attempt ${this._reconnectAttempt})`);
46127
46126
  this._reconnectTimer = setTimeout(() => {
46127
+ this._reconnectTimer = null;
46128
46128
  if (!this._stopped) {
46129
46129
  this._connect();
46130
46130
  }