nodejs-insta-private-api-mqt 1.3.70 → 1.3.71

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.
@@ -65,15 +65,23 @@ class MQTToTClient extends mqtts_1.MqttClient {
65
65
  this.registerListeners();
66
66
  this.requirePayload = options.requirePayload;
67
67
 
68
- this._keepaliveMs = (typeof options.keepaliveMs === 'number') ? options.keepaliveMs : (8 * 60 * 1000);
68
+ this._keepaliveMs = (typeof options.keepaliveMs === 'number') ? options.keepaliveMs : (30 * 1000); // default ~30s (mobile-like) to stay well within CONNECT keepAlive=60
69
69
  this._consecutivePingFailures = 0;
70
+
71
+ // Stall detection (mobile-like): if we don't receive any traffic for too long,
72
+ // force a reconnect. This helps recover from half-open sockets.
73
+ this._lastRxTs = Date.now();
74
+ this._stallMs = (typeof options.stallMs === 'number') ? options.stallMs : (180 * 1000); // 3 min default
75
+ this._stallCheckMs = (typeof options.stallCheckMs === 'number') ? options.stallCheckMs : (30 * 1000); // check every 30s
76
+ this._startStallWatchdog();
77
+
70
78
  this._startKeepalive();
71
79
  }
72
80
 
73
81
  _startKeepalive() {
74
82
  try {
75
83
  if (this._keepaliveTimer) clearInterval(this._keepaliveTimer);
76
- const jitter = Math.floor(Math.random() * 30000);
84
+ const jitter = Math.floor(Math.random() * 3000);
77
85
  this._keepaliveTimer = setInterval(() => {
78
86
  try {
79
87
  if (typeof this.ping === 'function') {
@@ -104,7 +112,41 @@ class MQTToTClient extends mqtts_1.MqttClient {
104
112
  }
105
113
  }
106
114
 
107
- /**
115
+
116
+ _startStallWatchdog() {
117
+ try {
118
+ if (this._stallTimer) clearInterval(this._stallTimer);
119
+ this._stallTimer = setInterval(() => {
120
+ try {
121
+ // only check if we appear connected
122
+ const now = Date.now();
123
+ const last = this._lastRxTs || 0;
124
+ if (last && (now - last) > this._stallMs) {
125
+ this.mqttotDebug(`Stall detected: no inbound traffic for ${Math.round((now - last) / 1000)}s (threshold ${Math.round(this._stallMs/1000)}s). Forcing reconnect.`);
126
+ this._lastRxTs = now;
127
+ this.emit('disconnect', 'stall_timeout');
128
+ }
129
+ } catch (e) {
130
+ this.mqttotDebug(`Stall watchdog error: ${e?.message || e}`);
131
+ }
132
+ }, this._stallCheckMs);
133
+ } catch (e) {
134
+ this.mqttotDebug(`Stall watchdog setup error: ${e?.message || e}`);
135
+ }
136
+ }
137
+
138
+ _stopStallWatchdog() {
139
+ try {
140
+ if (this._stallTimer) {
141
+ clearInterval(this._stallTimer);
142
+ this._stallTimer = null;
143
+ }
144
+ } catch (e) {
145
+ // ignore
146
+ }
147
+ }
148
+
149
+ /**
108
150
  * Stop keepalive timer (call on explicit close/disconnect)
109
151
  */
110
152
  _stopKeepalive() {
@@ -137,9 +179,14 @@ class MQTToTClient extends mqtts_1.MqttClient {
137
179
  // Attach diagnostics
138
180
  this.on('error', printErrorOrWarning('Error'));
139
181
  this.on('warning', printErrorOrWarning('Warning'));
182
+ this.on('connect', () => { try { this._lastRxTs = Date.now(); } catch (e) {} });
183
+
184
+ // Update last-receive timestamp for stall detection
185
+ this.on('message', () => { try { this._lastRxTs = Date.now(); } catch (e) {} });
140
186
 
141
187
  // Listen to ping responses if the library emits them
142
188
  this.on('pingresp', () => {
189
+ try { this._lastRxTs = Date.now(); } catch (e) {}
143
190
  this.mqttotDebug('Received PINGRESP (keepalive ok)');
144
191
  });
145
192
 
@@ -147,6 +194,7 @@ class MQTToTClient extends mqtts_1.MqttClient {
147
194
  try {
148
195
  this.mqttotDebug(`Disconnected. Reason: ${reason}`);
149
196
  this._stopKeepalive();
197
+ this._stopStallWatchdog();
150
198
 
151
199
  if (this._options && this._options.autoReconnect === false) {
152
200
  this.mqttotDebug('autoReconnect disabled; will not attempt reconnect.');
@@ -164,6 +212,8 @@ class MQTToTClient extends mqtts_1.MqttClient {
164
212
  await this.connect(this._options);
165
213
  this.mqttotDebug('Reconnected successfully');
166
214
  this._consecutivePingFailures = 0;
215
+ this._lastRxTs = Date.now();
216
+ this._startStallWatchdog();
167
217
  this._startKeepalive();
168
218
  return;
169
219
  } catch (err) {
@@ -276,6 +326,7 @@ class MQTToTClient extends mqtts_1.MqttClient {
276
326
  async gracefulClose() {
277
327
  try {
278
328
  this._stopKeepalive();
329
+ this._stopStallWatchdog();
279
330
  if (typeof super.close === 'function') {
280
331
  // some libs provide close() or end()
281
332
  await super.close();
@@ -786,7 +786,7 @@ class RealtimeClient extends eventemitter3_1.EventEmitter {
786
786
  this.connection = new mqttot_1.MQTToTConnection({
787
787
  clientIdentifier: deviceId.substring(0, 20),
788
788
  clientInfo: {
789
- userId: BigInt(Number(this.ig.state.cookieUserId || this.ig.state.userId || 0)),
789
+ userId: BigInt(String(this.ig.state.cookieUserId || this.ig.state.userId || '0')), // keep full precision (avoid Number() > 2^53),
790
790
  userAgent,
791
791
  clientCapabilities: 439,
792
792
  endpointCapabilities: 128,
@@ -946,10 +946,10 @@ class RealtimeClient extends eventemitter3_1.EventEmitter {
946
946
  this.on('message', updateLast);
947
947
  this.on('iris', updateLast);
948
948
 
949
- const KEEPALIVE_FOREGROUND_MS = (this.initOptions && this.initOptions.keepaliveForegroundMs) ? this.initOptions.keepaliveForegroundMs : 60000;
949
+ const KEEPALIVE_FOREGROUND_MS = (this.initOptions && this.initOptions.keepaliveForegroundMs) ? this.initOptions.keepaliveForegroundMs : 45000; // 45s keepalive pulse
950
950
  const MESSAGE_SYNC_REFRESH_MS = (this.initOptions && this.initOptions.messageSyncRefreshMs) ? this.initOptions.messageSyncRefreshMs : 300000;
951
- const TRAFFIC_INACTIVITY_MS = (this.initOptions && this.initOptions.trafficInactivityMs) ? this.initOptions.trafficInactivityMs : 300000;
952
- const HEARTBEAT_MS = (this.initOptions && this.initOptions.heartbeatMs) ? this.initOptions.heartbeatMs : 240000;
951
+ const TRAFFIC_INACTIVITY_MS = (this.initOptions && this.initOptions.trafficInactivityMs) ? this.initOptions.trafficInactivityMs : 180000; // 3 min default for faster recovery
952
+ const HEARTBEAT_MS = (this.initOptions && this.initOptions.heartbeatMs) ? this.initOptions.heartbeatMs : 90000; // 90s mobile-like heartbeat
953
953
 
954
954
  try {
955
955
  if (this._foregroundTimer) clearInterval(this._foregroundTimer);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodejs-insta-private-api-mqt",
3
- "version": "1.3.70",
3
+ "version": "1.3.71",
4
4
  "description": "Complete Instagram MQTT protocol with FULL Featured REALTIME And api Rest All in one project .",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {