nodejs-insta-private-api-mqt 1.3.92 → 1.3.93

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.
@@ -61,7 +61,7 @@ class MQTToTClient extends mqtts_1.MqttClient {
61
61
  // Register listeners (errors, disconnect, pingresps, etc.)
62
62
  this.registerListeners();
63
63
  this.requirePayload = options.requirePayload;
64
- this._keepaliveMs = (typeof options.keepaliveMs === 'number') ? options.keepaliveMs : (8 * 60 * 1000);
64
+ this._keepaliveMs = (typeof options.keepaliveMs === 'number') ? options.keepaliveMs : (45 * 1000);
65
65
  this._consecutivePingFailures = 0;
66
66
  this._startKeepalive();
67
67
  }
@@ -69,7 +69,7 @@ class MQTToTClient extends mqtts_1.MqttClient {
69
69
  try {
70
70
  if (this._keepaliveTimer)
71
71
  clearInterval(this._keepaliveTimer);
72
- const jitter = Math.floor(Math.random() * 30000);
72
+ const jitter = Math.floor(Math.random() * 5000);
73
73
  this._keepaliveTimer = setInterval(() => {
74
74
  try {
75
75
  if (typeof this.ping === 'function') {
@@ -147,7 +147,7 @@ class MQTToTClient extends mqtts_1.MqttClient {
147
147
  return;
148
148
  }
149
149
  let delay = 3000 + Math.floor(Math.random() * 2000);
150
- const maxAttempts = 12;
150
+ const maxAttempts = 999;
151
151
  for (let attempt = 0; attempt < maxAttempts; attempt++) {
152
152
  try {
153
153
  this.mqttotDebug(`Reconnect attempt #${attempt + 1} (delay ${delay}ms)`);
@@ -30,7 +30,7 @@ class ErrorHandler {
30
30
  constructor(client) {
31
31
  this.errorDebug = (0, shared_1.debugChannel)('realtime', 'errors');
32
32
  this.errorCount = 0;
33
- this.maxRetries = 15;
33
+ this.maxRetries = 999;
34
34
  this.client = client;
35
35
  this.errorHistory = [];
36
36
  this.rateLimitUntil = 0;
@@ -1057,11 +1057,12 @@ class RealtimeClient extends eventemitter3_1.EventEmitter {
1057
1057
  this.on('iris', updateLast);
1058
1058
  const KEEPALIVE_FOREGROUND_MS = (this.initOptions && this.initOptions.keepaliveForegroundMs) ? this.initOptions.keepaliveForegroundMs : 45000; // 45s keepalive pulse
1059
1059
  const MESSAGE_SYNC_REFRESH_MS = (this.initOptions && this.initOptions.messageSyncRefreshMs) ? this.initOptions.messageSyncRefreshMs : 300000;
1060
- const TRAFFIC_INACTIVITY_MS = (this.initOptions && this.initOptions.trafficInactivityMs) ? this.initOptions.trafficInactivityMs : 180000; // 3 min default for faster recovery
1060
+ const TRAFFIC_INACTIVITY_MS = (this.initOptions && this.initOptions.trafficInactivityMs) ? this.initOptions.trafficInactivityMs : 120000; // 2 min default for faster recovery
1061
1061
  const HEARTBEAT_MS = (this.initOptions && this.initOptions.heartbeatMs) ? this.initOptions.heartbeatMs : 90000; // 90s mobile-like heartbeat
1062
1062
  try {
1063
1063
  if (this._foregroundTimer)
1064
1064
  clearInterval(this._foregroundTimer);
1065
+ this._foregroundConsecutiveFailures = 0;
1065
1066
  this._foregroundTimer = setInterval(async () => {
1066
1067
  try {
1067
1068
  if (!this.commands)
@@ -1071,10 +1072,18 @@ class RealtimeClient extends eventemitter3_1.EventEmitter {
1071
1072
  data: { foreground: true }
1072
1073
  });
1073
1074
  this._lastMessageAt = Date.now();
1075
+ this._foregroundConsecutiveFailures = 0;
1074
1076
  this.realtimeDebug('[KEEPALIVE] Foreground pulse sent.');
1075
1077
  }
1076
1078
  catch (e) {
1077
- this.realtimeDebug('[KEEPALIVE] Foreground pulse failed:', e?.message || e);
1079
+ this._foregroundConsecutiveFailures = (this._foregroundConsecutiveFailures || 0) + 1;
1080
+ this.realtimeDebug(`[KEEPALIVE] Foreground pulse failed (${this._foregroundConsecutiveFailures}):`, e?.message || e);
1081
+ if (this._foregroundConsecutiveFailures >= 3 && !this._reconnectInProgress && !this.safeDisconnect) {
1082
+ this.realtimeDebug('[KEEPALIVE] 3 consecutive foreground failures - connection likely dead, triggering reconnect');
1083
+ this._mqttConnected = false;
1084
+ this._foregroundConsecutiveFailures = 0;
1085
+ try { await this._attemptReconnectSafely(e); } catch(re) {}
1086
+ }
1078
1087
  }
1079
1088
  }, KEEPALIVE_FOREGROUND_MS + Math.floor(Math.random() * 5000));
1080
1089
  }
@@ -1113,6 +1122,7 @@ class RealtimeClient extends eventemitter3_1.EventEmitter {
1113
1122
  if (this._mqtt && typeof this._mqtt.ping === 'function') {
1114
1123
  await this._mqtt.ping();
1115
1124
  this._lastMessageAt = Date.now();
1125
+ this._lastServerTrafficAt = Date.now();
1116
1126
  this.realtimeDebug('[WATCHDOG] Ping succeeded, connection is alive.');
1117
1127
  return;
1118
1128
  }
@@ -1140,10 +1150,16 @@ class RealtimeClient extends eventemitter3_1.EventEmitter {
1140
1150
  try {
1141
1151
  await this._mqtt.ping();
1142
1152
  this._lastMessageAt = Date.now();
1153
+ this._lastServerTrafficAt = Date.now();
1143
1154
  this.realtimeDebug('[HEARTBEAT] mqtt.ping() ok.');
1144
1155
  }
1145
1156
  catch (e) {
1146
1157
  this.realtimeDebug('[HEARTBEAT] mqtt.ping failed:', e?.message || e);
1158
+ if (!this._reconnectInProgress && !this.safeDisconnect) {
1159
+ this.realtimeDebug('[HEARTBEAT] Ping failed - triggering reconnect');
1160
+ this._mqttConnected = false;
1161
+ try { await this._attemptReconnectSafely(e); } catch(re) {}
1162
+ }
1147
1163
  }
1148
1164
  }
1149
1165
  }
@@ -1155,6 +1171,33 @@ class RealtimeClient extends eventemitter3_1.EventEmitter {
1155
1171
  catch (e) {
1156
1172
  this.realtimeDebug('[HEARTBEAT] Could not start heartbeat timer:', e?.message || e);
1157
1173
  }
1174
+ try {
1175
+ if (this._reconnectSafetyNet)
1176
+ clearInterval(this._reconnectSafetyNet);
1177
+ this._reconnectSafetyNet = setInterval(async () => {
1178
+ try {
1179
+ if (this._reconnectInProgress && this._reconnectStartedAt) {
1180
+ const stuckMs = Date.now() - this._reconnectStartedAt;
1181
+ if (stuckMs > 10 * 60 * 1000) {
1182
+ this.realtimeDebug('[SAFETY_NET] _reconnectInProgress stuck for >10min — force resetting and retrying');
1183
+ this._reconnectInProgress = false;
1184
+ this._reconnectStartedAt = null;
1185
+ try { await this._attemptReconnectSafely(); } catch(e) {}
1186
+ }
1187
+ }
1188
+ if (!this._mqttConnected && !this._reconnectInProgress && !this.safeDisconnect && !this._connectInProgress) {
1189
+ this.realtimeDebug('[SAFETY_NET] Not connected and no reconnect in progress — triggering reconnect');
1190
+ try { await this._attemptReconnectSafely(); } catch(e) {}
1191
+ }
1192
+ }
1193
+ catch (e) {
1194
+ this.realtimeDebug('[SAFETY_NET] fault:', e?.message || e);
1195
+ }
1196
+ }, 2 * 60 * 1000);
1197
+ }
1198
+ catch (e) {
1199
+ this.realtimeDebug('[SAFETY_NET] Could not start safety net timer:', e?.message || e);
1200
+ }
1158
1201
  }
1159
1202
  catch (e) {
1160
1203
  this.realtimeDebug('[WATCHDOG] initialization error:', e?.message || e);
@@ -1558,18 +1601,33 @@ class RealtimeClient extends eventemitter3_1.EventEmitter {
1558
1601
  */
1559
1602
  async _attemptReconnectSafely(lastError) {
1560
1603
  if (this._reconnectInProgress) {
1561
- this.realtimeDebug('[RECONNECT] Skipped reconnect already in progress');
1562
- return;
1604
+ const stuckMs = Date.now() - (this._reconnectStartedAt || Date.now());
1605
+ if (stuckMs > 5 * 60 * 1000) {
1606
+ this.realtimeDebug('[RECONNECT] _reconnectInProgress stuck for >5min — resetting flag');
1607
+ this._reconnectInProgress = false;
1608
+ } else {
1609
+ this.realtimeDebug('[RECONNECT] Skipped — reconnect already in progress');
1610
+ return;
1611
+ }
1563
1612
  }
1564
1613
  if (this._mqttConnected) {
1565
- this.realtimeDebug('[RECONNECT] Skipped — MQTT is already connected');
1566
- return;
1614
+ try {
1615
+ if (this._mqtt && typeof this._mqtt.ping === 'function') {
1616
+ await this._mqtt.ping();
1617
+ this.realtimeDebug('[RECONNECT] Skipped — MQTT is already connected and ping succeeded');
1618
+ return;
1619
+ }
1620
+ } catch(e) {
1621
+ this.realtimeDebug('[RECONNECT] MQTT marked connected but ping failed — proceeding with reconnect');
1622
+ this._mqttConnected = false;
1623
+ }
1567
1624
  }
1568
1625
  if (this.safeDisconnect) {
1569
1626
  this.realtimeDebug('[RECONNECT] Skipped — safe disconnect active');
1570
1627
  return;
1571
1628
  }
1572
1629
  this._reconnectInProgress = true;
1630
+ this._reconnectStartedAt = Date.now();
1573
1631
  if (this._reconnectTimeoutId) {
1574
1632
  clearTimeout(this._reconnectTimeoutId);
1575
1633
  this._reconnectTimeoutId = null;
@@ -1581,7 +1639,7 @@ class RealtimeClient extends eventemitter3_1.EventEmitter {
1581
1639
  }
1582
1640
  try {
1583
1641
  let attempt = 0;
1584
- const maxAttempts = 20;
1642
+ const maxAttempts = 999;
1585
1643
  let lastErrorType = lastError ? (this.errorHandler ? this.errorHandler.classifyError(lastError) : 'unknown') : 'unknown';
1586
1644
  while (attempt < maxAttempts) {
1587
1645
  attempt++;
@@ -1960,6 +2018,11 @@ class RealtimeClient extends eventemitter3_1.EventEmitter {
1960
2018
  clearInterval(this._activeKeepaliveTimer);
1961
2019
  this._activeKeepaliveTimer = null;
1962
2020
  }
2021
+ // Clear reconnect safety net
2022
+ if (this._reconnectSafetyNet) {
2023
+ clearInterval(this._reconnectSafetyNet);
2024
+ this._reconnectSafetyNet = null;
2025
+ }
1963
2026
  // clear credential refresh timer
1964
2027
  if (this._mqttAuthRefreshTimer) {
1965
2028
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodejs-insta-private-api-mqt",
3
- "version": "1.3.92",
3
+ "version": "1.3.93",
4
4
  "description": "Complete Instagram MQTT protocol with full-featured REALTIME and REST API — all in one project.",
5
5
 
6
6
  "main": "dist/dist/index.js",