@raevon/n8n-nodes-whatsapp 1.0.9 → 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
  });
@@ -246,20 +248,21 @@ async function ensureConnected(cfg) {
246
248
  message: 'No WhatsApp session found. Run the WhatsApp Connect node first to scan QR code.',
247
249
  });
248
250
  }
249
- // Check if current socket is actually alive
251
+ // Check if current socket is alive — reuse if possible
250
252
  if (socketInstance && socketStatus === 'connected') {
251
253
  try {
252
- if (socketInstance.user) {
253
- console.log('[WhatsApp] Reusing existing connection');
254
- return socketInstance;
255
- }
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;
256
257
  }
257
258
  catch {
258
- console.log('[WhatsApp] Existing socket is dead, reconnecting...');
259
+ // Socket is dead, create new one
260
+ socketInstance = null;
261
+ socketStatus = 'stopped';
259
262
  }
260
263
  }
261
- // Session exists connect using saved credentials
262
- console.log('[WhatsApp] Connecting from saved session...');
264
+ // Create fresh connection from saved session
265
+ console.log('[WhatsApp] Creating fresh connection from saved session...');
263
266
  const sock = await initSocket(antiBanCfg, cfg.sessionPath);
264
267
  // Wait for connection to open (up to 20s)
265
268
  const connected = await new Promise((resolve) => {
@@ -280,6 +283,11 @@ async function ensureConnected(cfg) {
280
283
  message: `WhatsApp connection failed (status: ${socketStatus}). Try running Connect node again.`,
281
284
  });
282
285
  }
286
+ // Wait for credentials to be saved
287
+ if (credsSavedPromise) {
288
+ await credsSavedPromise;
289
+ credsSavedPromise = null;
290
+ }
283
291
  return sock;
284
292
  }
285
293
  // Non-blocking version for Connect node — starts socket, returns QR URL if needed, doesn't wait
@@ -317,6 +325,11 @@ async function connectOrGetQr(cfg) {
317
325
  }, 500);
318
326
  setTimeout(() => { clearInterval(check); resolve(); }, 15000);
319
327
  });
328
+ // Wait for credentials to be fully saved to disk before returning
329
+ if (credsSavedPromise) {
330
+ await credsSavedPromise;
331
+ credsSavedPromise = null;
332
+ }
320
333
  if (socketStatus === 'connected') {
321
334
  return { connected: true, message: 'Connected successfully' };
322
335
  }
@@ -133,10 +133,12 @@ class WhatsAppSend {
133
133
  const returnData = [];
134
134
  const credentials = await this.getCredentials('whatsappApi');
135
135
  const cfg = await (0, WhatsAppApiHelper_1.getWhatsAppCredentials)(credentials);
136
+ console.log(`[WhatsApp Send] Starting execution, recipient check: ${cfg.checkRecipientExists}`);
136
137
  for (let i = 0; i < items.length; i++) {
137
138
  try {
138
139
  const operation = this.getNodeParameter('operation', i);
139
140
  const recipient = this.getNodeParameter('recipient', i);
141
+ console.log(`[WhatsApp Send] Operation: ${operation}, To: ${recipient}`);
140
142
  let content = {};
141
143
  switch (operation) {
142
144
  case 'sendText':
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@raevon/n8n-nodes-whatsapp",
3
- "version": "1.0.9",
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",