@raevon/n8n-nodes-whatsapp 1.0.10 → 1.0.11

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.
@@ -88,6 +88,7 @@ let reconnectTimer = null;
88
88
  let qrResolve = null;
89
89
  let latestQr = null;
90
90
  let generation = 0; // #10: Generation counter — prevents stale handlers on reconnect
91
+ let credsSavedPromise = null; // Track credential save completion
91
92
  function todayStartIso() {
92
93
  return new Date().toISOString().slice(0, 10) + 'T00:00:00.000Z';
93
94
  }
@@ -161,7 +162,8 @@ async function initSocket(cfg, authPath) {
161
162
  sock.ev.on('creds.update', () => {
162
163
  if (gen !== generation)
163
164
  return; // #10: Ignore stale connection events
164
- saveCreds().catch((err) => {
165
+ // Track credential save completion — ensures session is persisted before execution ends
166
+ credsSavedPromise = saveCreds().catch((err) => {
165
167
  console.error('[WhatsApp] Failed to save credentials:', err.message);
166
168
  });
167
169
  });
@@ -241,27 +243,26 @@ async function ensureConnected(cfg) {
241
243
  // Check if session exists on disk
242
244
  const resolvedPath = expandHome(cfg.sessionPath);
243
245
  const hasSession = node_fs_1.default.existsSync(resolvedPath) && node_fs_1.default.readdirSync(resolvedPath).length > 0;
244
- console.log(`[WhatsApp] Session path: ${resolvedPath}, exists: ${hasSession}`);
245
246
  if (!hasSession) {
246
247
  throw new n8n_workflow_1.NodeApiError({}, {
247
248
  message: 'No WhatsApp session found. Run the WhatsApp Connect node first to scan QR code.',
248
249
  });
249
250
  }
250
- // Check if current socket is actually alive
251
- console.log(`[WhatsApp] Current status: ${socketStatus}, socket exists: ${!!socketInstance}`);
251
+ // Check if current socket is alive — reuse if possible
252
252
  if (socketInstance && socketStatus === 'connected') {
253
253
  try {
254
- if (socketInstance.user) {
255
- console.log('[WhatsApp] Reusing existing connection');
256
- return socketInstance;
257
- }
254
+ // Test if socket is actually responding
255
+ await socketInstance.query({ tag: 'iq', attrs: { id: 'ping', to: 's.whatsapp.net', type: 'get', xmlns: 'w:p' } });
256
+ return socketInstance;
258
257
  }
259
258
  catch {
260
- console.log('[WhatsApp] Existing socket is dead, reconnecting...');
259
+ // Socket is dead, create new one
260
+ socketInstance = null;
261
+ socketStatus = 'stopped';
261
262
  }
262
263
  }
263
- // Session exists connect using saved credentials
264
- console.log('[WhatsApp] Connecting from saved session...');
264
+ // Create fresh connection from saved session
265
+ console.log('[WhatsApp] Creating fresh connection from saved session...');
265
266
  const sock = await initSocket(antiBanCfg, cfg.sessionPath);
266
267
  // Wait for connection to open (up to 20s)
267
268
  const connected = await new Promise((resolve) => {
@@ -282,6 +283,11 @@ async function ensureConnected(cfg) {
282
283
  message: `WhatsApp connection failed (status: ${socketStatus}). Try running Connect node again.`,
283
284
  });
284
285
  }
286
+ // Wait for credentials to be saved
287
+ if (credsSavedPromise) {
288
+ await credsSavedPromise;
289
+ credsSavedPromise = null;
290
+ }
285
291
  return sock;
286
292
  }
287
293
  // Non-blocking version for Connect node — starts socket, returns QR URL if needed, doesn't wait
@@ -319,6 +325,11 @@ async function connectOrGetQr(cfg) {
319
325
  }, 500);
320
326
  setTimeout(() => { clearInterval(check); resolve(); }, 15000);
321
327
  });
328
+ // Wait for credentials to be fully saved to disk before returning
329
+ if (credsSavedPromise) {
330
+ await credsSavedPromise;
331
+ credsSavedPromise = null;
332
+ }
322
333
  if (socketStatus === 'connected') {
323
334
  return { connected: true, message: 'Connected successfully' };
324
335
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@raevon/n8n-nodes-whatsapp",
3
- "version": "1.0.10",
3
+ "version": "1.0.11",
4
4
  "description": "n8n community node for WhatsApp — send and receive messages with anti-ban protection via the Baileys library",
5
5
  "keywords": [
6
6
  "n8n-community-node-package",