@raevon/n8n-nodes-whatsapp 1.0.7 → 1.0.8
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.
|
@@ -123,8 +123,16 @@ async function scheduleReconnect(cfg) {
|
|
|
123
123
|
}, delay);
|
|
124
124
|
}
|
|
125
125
|
async function initSocket(cfg, authPath) {
|
|
126
|
-
if
|
|
127
|
-
|
|
126
|
+
// Always create fresh socket if current one is dead
|
|
127
|
+
if (socketInstance && socketStatus === 'connected') {
|
|
128
|
+
try {
|
|
129
|
+
if (socketInstance.user)
|
|
130
|
+
return socketInstance;
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
// Socket dead, create new one
|
|
134
|
+
}
|
|
135
|
+
}
|
|
128
136
|
const gen = ++generation; // #10: Snapshot generation for this connection attempt
|
|
129
137
|
const resolvedPath = expandHome(authPath);
|
|
130
138
|
if (!node_fs_1.default.existsSync(resolvedPath)) {
|
|
@@ -210,8 +218,17 @@ async function getWhatsAppCredentials(credentials) {
|
|
|
210
218
|
};
|
|
211
219
|
}
|
|
212
220
|
async function ensureConnected(cfg) {
|
|
213
|
-
|
|
214
|
-
|
|
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
|
+
}
|
|
215
232
|
const antiBanCfg = {
|
|
216
233
|
messageDelayMinMs: cfg.messageDelayMinMs,
|
|
217
234
|
messageDelayMaxMs: cfg.messageDelayMaxMs,
|
|
@@ -228,42 +245,29 @@ async function ensureConnected(cfg) {
|
|
|
228
245
|
if (!queue) {
|
|
229
246
|
queue = new p_queue_1.default({ concurrency: 1 });
|
|
230
247
|
}
|
|
231
|
-
// Check if session already exists
|
|
248
|
+
// Check if session already exists on disk
|
|
232
249
|
const resolvedPath = expandHome(cfg.sessionPath);
|
|
233
250
|
const hasSession = node_fs_1.default.existsSync(resolvedPath) && node_fs_1.default.readdirSync(resolvedPath).length > 0;
|
|
234
|
-
const sock = await initSocket(antiBanCfg, cfg.sessionPath);
|
|
235
|
-
// If no session, wait for QR with a reasonable timeout
|
|
236
251
|
if (!hasSession) {
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
// 90 second timeout — QR codes expire quickly, user needs to act fast
|
|
240
|
-
setTimeout(() => {
|
|
241
|
-
if (qrResolve) {
|
|
242
|
-
qrResolve(null);
|
|
243
|
-
qrResolve = null;
|
|
244
|
-
}
|
|
245
|
-
}, 90000);
|
|
252
|
+
throw new n8n_workflow_1.NodeApiError({}, {
|
|
253
|
+
message: 'No WhatsApp session found. Run the WhatsApp Connect node first to scan QR code.',
|
|
246
254
|
});
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
clearInterval(checkInterval);
|
|
264
|
-
reject(new Error('Connection failed after QR scan'));
|
|
265
|
-
}
|
|
266
|
-
}, 1000);
|
|
255
|
+
}
|
|
256
|
+
// Session exists — connect using saved credentials (no QR needed)
|
|
257
|
+
const sock = await initSocket(antiBanCfg, cfg.sessionPath);
|
|
258
|
+
// Wait for connection to open (up to 15s)
|
|
259
|
+
await new Promise((resolve) => {
|
|
260
|
+
const check = setInterval(() => {
|
|
261
|
+
if (socketStatus === 'connected' || socketStatus === 'error' || socketStatus === 'logged_out') {
|
|
262
|
+
clearInterval(check);
|
|
263
|
+
resolve();
|
|
264
|
+
}
|
|
265
|
+
}, 500);
|
|
266
|
+
setTimeout(() => { clearInterval(check); resolve(); }, 15000);
|
|
267
|
+
});
|
|
268
|
+
if (socketStatus !== 'connected') {
|
|
269
|
+
throw new n8n_workflow_1.NodeApiError({}, {
|
|
270
|
+
message: `WhatsApp connection failed (status: ${socketStatus}). Try running Connect node again.`,
|
|
267
271
|
});
|
|
268
272
|
}
|
|
269
273
|
return sock;
|
package/package.json
CHANGED