@raevon/n8n-nodes-whatsapp 1.0.8 → 1.0.9

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.
@@ -184,16 +184,20 @@ async function initSocket(cfg, authPath) {
184
184
  socketStatus = 'connected';
185
185
  reconnectAttempts = 0;
186
186
  latestQr = null;
187
+ console.log('[WhatsApp] Connected successfully');
187
188
  return;
188
189
  }
189
190
  if (connection === 'close') {
190
191
  const code = (_b = (_a = lastDisconnect === null || lastDisconnect === void 0 ? void 0 : lastDisconnect.error) === null || _a === void 0 ? void 0 : _a.output) === null || _b === void 0 ? void 0 : _b.statusCode;
192
+ const reason = baileys_1.DisconnectReason[code] || 'Unknown';
193
+ console.log(`[WhatsApp] Connection closed: ${reason} (code: ${code})`);
191
194
  if (code === baileys_1.DisconnectReason.loggedOut) {
192
195
  socketStatus = 'logged_out';
193
196
  socketInstance = null;
194
197
  ++generation; // #10: Invalidate all handlers from this session
195
198
  }
196
199
  else {
200
+ socketStatus = 'disconnected';
197
201
  socketInstance = null;
198
202
  scheduleReconnect(cfg);
199
203
  }
@@ -218,17 +222,6 @@ async function getWhatsAppCredentials(credentials) {
218
222
  };
219
223
  }
220
224
  async function ensureConnected(cfg) {
221
- // Always try to use existing socket first, but verify it's actually alive
222
- if (socketInstance && socketStatus === 'connected') {
223
- try {
224
- // Quick check — if socket user is set, it's alive
225
- if (socketInstance.user)
226
- return socketInstance;
227
- }
228
- catch {
229
- // Socket is dead, reconnect below
230
- }
231
- }
232
225
  const antiBanCfg = {
233
226
  messageDelayMinMs: cfg.messageDelayMinMs,
234
227
  messageDelayMaxMs: cfg.messageDelayMaxMs,
@@ -245,7 +238,7 @@ async function ensureConnected(cfg) {
245
238
  if (!queue) {
246
239
  queue = new p_queue_1.default({ concurrency: 1 });
247
240
  }
248
- // Check if session already exists on disk
241
+ // Check if session exists on disk
249
242
  const resolvedPath = expandHome(cfg.sessionPath);
250
243
  const hasSession = node_fs_1.default.existsSync(resolvedPath) && node_fs_1.default.readdirSync(resolvedPath).length > 0;
251
244
  if (!hasSession) {
@@ -253,19 +246,36 @@ async function ensureConnected(cfg) {
253
246
  message: 'No WhatsApp session found. Run the WhatsApp Connect node first to scan QR code.',
254
247
  });
255
248
  }
256
- // Session exists connect using saved credentials (no QR needed)
249
+ // Check if current socket is actually alive
250
+ if (socketInstance && socketStatus === 'connected') {
251
+ try {
252
+ if (socketInstance.user) {
253
+ console.log('[WhatsApp] Reusing existing connection');
254
+ return socketInstance;
255
+ }
256
+ }
257
+ catch {
258
+ console.log('[WhatsApp] Existing socket is dead, reconnecting...');
259
+ }
260
+ }
261
+ // Session exists — connect using saved credentials
262
+ console.log('[WhatsApp] Connecting from saved session...');
257
263
  const sock = await initSocket(antiBanCfg, cfg.sessionPath);
258
- // Wait for connection to open (up to 15s)
259
- await new Promise((resolve) => {
264
+ // Wait for connection to open (up to 20s)
265
+ const connected = await new Promise((resolve) => {
260
266
  const check = setInterval(() => {
261
- if (socketStatus === 'connected' || socketStatus === 'error' || socketStatus === 'logged_out') {
267
+ if (socketStatus === 'connected') {
268
+ clearInterval(check);
269
+ resolve(true);
270
+ }
271
+ if (socketStatus === 'logged_out' || socketStatus === 'error') {
262
272
  clearInterval(check);
263
- resolve();
273
+ resolve(false);
264
274
  }
265
275
  }, 500);
266
- setTimeout(() => { clearInterval(check); resolve(); }, 15000);
276
+ setTimeout(() => { clearInterval(check); resolve(false); }, 20000);
267
277
  });
268
- if (socketStatus !== 'connected') {
278
+ if (!connected) {
269
279
  throw new n8n_workflow_1.NodeApiError({}, {
270
280
  message: `WhatsApp connection failed (status: ${socketStatus}). Try running Connect node again.`,
271
281
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@raevon/n8n-nodes-whatsapp",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
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",