app-manager-edge-worker 1.1.0 → 1.1.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "app-manager-edge-worker",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "Distributed Scraper Edge Worker for Apps Manager. Run on any local machine, VPS, or Termux Android device to serve browser and HTTP scraping jobs.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -112,7 +112,7 @@ export class WhatsAppWorkerHandler {
112
112
  * Can be invoked for pairing (requestQr=true) or on-demand send.
113
113
  */
114
114
  async initialize(requestQr = false) {
115
- if (this.sock && this.status === "CONNECTED") {
115
+ if (this.sock && this.status === "CONNECTED" && !requestQr) {
116
116
  this._resetIdleTimer();
117
117
  return this.sock;
118
118
  }
@@ -126,19 +126,31 @@ export class WhatsAppWorkerHandler {
126
126
  }
127
127
  }
128
128
 
129
+ if (requestQr) {
130
+ // Clean slate for fresh pairing request
131
+ if (this.sock) {
132
+ try {
133
+ this.sock.end(undefined);
134
+ } catch (_) {}
135
+ this.sock = null;
136
+ }
137
+ this.clearAuth();
138
+ }
139
+
129
140
  this.isInitializing = true;
130
141
  this.status = requestQr ? "PAIRING" : "STANDBY";
131
142
 
132
143
  try {
133
144
  const { state, saveCreds } = await useMultiFileAuthState(this.authDir);
134
- const { version } = await fetchLatestBaileysVersion().catch(() => ({
135
- version: [2, 3000, 1015901307],
136
- }));
145
+ let version = undefined;
146
+ try {
147
+ const fetched = await fetchLatestBaileysVersion();
148
+ if (fetched?.version) version = fetched.version;
149
+ } catch (_) {}
137
150
 
138
151
  const logger = pino({ level: "silent" });
139
152
 
140
- this.sock = makeWASocket({
141
- version,
153
+ const socketConfig = {
142
154
  auth: state,
143
155
  logger,
144
156
  printQRInTerminal: false,
@@ -146,7 +158,14 @@ export class WhatsAppWorkerHandler {
146
158
  syncFullHistory: false,
147
159
  markOnlineOnConnect: false, // Stealth Presence: Does NOT broadcast green online dot
148
160
  generateHighQualityLinkPreview: true,
149
- });
161
+ defaultQueryTimeoutMs: 60000,
162
+ connectTimeoutMs: 60000,
163
+ keepAliveIntervalMs: 25000,
164
+ getMessage: async () => undefined,
165
+ };
166
+ if (version) socketConfig.version = version;
167
+
168
+ this.sock = makeWASocket(socketConfig);
150
169
 
151
170
  this.sock.ev.on("creds.update", saveCreds);
152
171
 
@@ -173,6 +192,10 @@ export class WhatsAppWorkerHandler {
173
192
  try {
174
193
  this.currentQrDataUrl = await qrcode.toDataURL(qr);
175
194
  this.onQrGenerated({ qrDataUrl: this.currentQrDataUrl });
195
+ this.onStatusChange({
196
+ status: "PAIRING",
197
+ qrCodeDataUrl: this.currentQrDataUrl,
198
+ });
176
199
  } catch (err) {
177
200
  console.error("[WhatsApp Worker] Failed to render QR Data URL:", err);
178
201
  }
@@ -193,6 +216,8 @@ export class WhatsAppWorkerHandler {
193
216
 
194
217
  this._resetIdleTimer();
195
218
 
219
+ console.log(`[WhatsApp Worker] Companion Device paired successfully! User: ${this.user.name || "WhatsApp User"} (+${this.user.phone})`);
220
+
196
221
  this.onStatusChange({
197
222
  status: "CONNECTED",
198
223
  phone: this.user.phone,
@@ -203,12 +228,15 @@ export class WhatsAppWorkerHandler {
203
228
 
204
229
  if (connection === "close") {
205
230
  this.isInitializing = false;
206
- const statusCode = (lastDisconnect?.error)?.output?.statusCode;
231
+ const error = lastDisconnect?.error;
232
+ const statusCode = error?.output?.statusCode;
207
233
  const isLoggedOut =
208
234
  statusCode === DisconnectReason.loggedOut ||
209
235
  statusCode === 401 ||
210
236
  statusCode === DisconnectReason.badSession;
211
237
 
238
+ console.log(`[WhatsApp Worker] Connection closed. StatusCode: ${statusCode}. LoggedOut: ${isLoggedOut}`);
239
+
212
240
  if (isLoggedOut) {
213
241
  // Mobile App or Meta unlinked this companion device
214
242
  this.status = "DISCONNECTED";
@@ -228,8 +256,21 @@ export class WhatsAppWorkerHandler {
228
256
  reason: "LOGGED_OUT_BY_PHONE",
229
257
  });
230
258
  } else {
231
- // Temporary disconnect / network blip
232
- this.status = "STANDBY";
259
+ // Post-scan restart (code 515 restartRequired) or temporary network glitch:
260
+ // WhatsApp protocol REQUIRES immediate socket reconnection to finalize the multi-device key exchange!
261
+ if (this.sock) {
262
+ try {
263
+ this.sock.end(undefined);
264
+ } catch (_) {}
265
+ this.sock = null;
266
+ }
267
+
268
+ console.log("[WhatsApp Worker] Reconnecting to finalize credentials and establish active session...");
269
+ setTimeout(() => {
270
+ this.initialize(false).catch((err) => {
271
+ console.error("[WhatsApp Worker] Reconnection error:", err.message);
272
+ });
273
+ }, 1000);
233
274
  }
234
275
  }
235
276
  });