app-manager-edge-worker 1.1.2 → 1.1.4
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/README.md +8 -0
- package/bin/worker.js +16 -16
- package/package.json +5 -5
- package/src/banner.js +80 -80
- package/src/client.js +396 -351
- package/src/handlers/mailHandler.js +74 -74
- package/src/handlers/previewHandler.js +647 -647
- package/src/handlers/scrapeHandler.js +887 -887
- package/src/handlers/transformUtils.js +340 -340
- package/src/handlers/whatsappHandler.js +150 -4
- package/LICENSE.txt +0 -21
|
@@ -36,18 +36,116 @@ export class WhatsAppWorkerHandler {
|
|
|
36
36
|
this.IDLE_COOLDOWN_MS = options.idleCooldownMs || 10 * 60 * 1000; // 10 minutes
|
|
37
37
|
this.onStatusChange = options.onStatusChange || (() => {});
|
|
38
38
|
this.onQrGenerated = options.onQrGenerated || (() => {});
|
|
39
|
+
this.onSessionBatchUpdate = options.onSessionBatchUpdate || (() => {});
|
|
40
|
+
this.onClearSession = options.onClearSession || (() => {});
|
|
39
41
|
this.chatStore = new Map(); // JID -> Array of recent messages for reconciliation
|
|
40
42
|
this.isInitializing = false;
|
|
43
|
+
this.pendingWrites = new Map(); // keyId -> string data
|
|
44
|
+
this.pendingDeletes = new Set(); // keyId
|
|
45
|
+
this.syncDebounceTimer = null;
|
|
41
46
|
|
|
42
47
|
if (!fs.existsSync(this.authDir)) {
|
|
43
48
|
fs.mkdirSync(this.authDir, { recursive: true });
|
|
44
49
|
}
|
|
45
50
|
}
|
|
46
51
|
|
|
52
|
+
/**
|
|
53
|
+
* Queues a key write to be batched and synced to the central MongoDB server.
|
|
54
|
+
*/
|
|
55
|
+
queueSyncWrite(keyId, data) {
|
|
56
|
+
if (!keyId || typeof data !== "string") return;
|
|
57
|
+
this.pendingDeletes.delete(keyId);
|
|
58
|
+
this.pendingWrites.set(keyId, data);
|
|
59
|
+
this._scheduleBatchSync();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Queues a key removal to be batched and deleted from central MongoDB server.
|
|
64
|
+
*/
|
|
65
|
+
queueSyncDelete(keyId) {
|
|
66
|
+
if (!keyId) return;
|
|
67
|
+
this.pendingWrites.delete(keyId);
|
|
68
|
+
this.pendingDeletes.add(keyId);
|
|
69
|
+
this._scheduleBatchSync();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Debounces and dispatches batched key updates to the cluster WebSocket.
|
|
74
|
+
*/
|
|
75
|
+
_scheduleBatchSync() {
|
|
76
|
+
if (this.syncDebounceTimer) return;
|
|
77
|
+
this.syncDebounceTimer = setTimeout(() => {
|
|
78
|
+
this.syncDebounceTimer = null;
|
|
79
|
+
if (this.pendingWrites.size === 0 && this.pendingDeletes.size === 0) return;
|
|
80
|
+
|
|
81
|
+
const writes = [];
|
|
82
|
+
for (const [keyId, data] of this.pendingWrites.entries()) {
|
|
83
|
+
writes.push({ keyId, data });
|
|
84
|
+
}
|
|
85
|
+
const deletes = Array.from(this.pendingDeletes);
|
|
86
|
+
|
|
87
|
+
this.pendingWrites.clear();
|
|
88
|
+
this.pendingDeletes.clear();
|
|
89
|
+
|
|
90
|
+
this.onSessionBatchUpdate({ writes, deletes });
|
|
91
|
+
}, 150);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Hydrates local session disk cache with stored credentials from MongoDB.
|
|
96
|
+
*/
|
|
97
|
+
hydrateFromSessionFiles(files) {
|
|
98
|
+
if (!files || typeof files !== "object") return;
|
|
99
|
+
if (!fs.existsSync(this.authDir)) {
|
|
100
|
+
fs.mkdirSync(this.authDir, { recursive: true });
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
let hydratedCount = 0;
|
|
104
|
+
for (const [filename, content] of Object.entries(files)) {
|
|
105
|
+
if (!filename || typeof content !== "string") continue;
|
|
106
|
+
const safeName = filename.replace(/\//g, "__").replace(/:/g, "-");
|
|
107
|
+
const targetPath = path.join(this.authDir, safeName);
|
|
108
|
+
try {
|
|
109
|
+
fs.writeFileSync(targetPath, content, "utf-8");
|
|
110
|
+
hydratedCount++;
|
|
111
|
+
} catch (err) {
|
|
112
|
+
console.error(`[WhatsApp Worker] Failed to hydrate ${filename}:`, err.message);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
console.log(`[WhatsApp Worker] Successfully hydrated ${hydratedCount} session files from MongoDB.`);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Reads all local JSON credential files from disk for initial backup to MongoDB.
|
|
120
|
+
*/
|
|
121
|
+
exportAllSessionFiles() {
|
|
122
|
+
if (!fs.existsSync(this.authDir)) return {};
|
|
123
|
+
const result = {};
|
|
124
|
+
try {
|
|
125
|
+
const files = fs.readdirSync(this.authDir);
|
|
126
|
+
for (const file of files) {
|
|
127
|
+
if (file.endsWith(".json")) {
|
|
128
|
+
try {
|
|
129
|
+
const content = fs.readFileSync(path.join(this.authDir, file), "utf-8");
|
|
130
|
+
result[file] = content;
|
|
131
|
+
} catch (_) {}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
} catch (_) {}
|
|
135
|
+
return result;
|
|
136
|
+
}
|
|
137
|
+
|
|
47
138
|
/**
|
|
48
139
|
* Wipes local stored credentials.
|
|
49
140
|
*/
|
|
50
141
|
clearAuth() {
|
|
142
|
+
this.pendingWrites.clear();
|
|
143
|
+
this.pendingDeletes.clear();
|
|
144
|
+
if (this.syncDebounceTimer) {
|
|
145
|
+
clearTimeout(this.syncDebounceTimer);
|
|
146
|
+
this.syncDebounceTimer = null;
|
|
147
|
+
}
|
|
148
|
+
|
|
51
149
|
if (fs.existsSync(this.authDir)) {
|
|
52
150
|
try {
|
|
53
151
|
fs.rmSync(this.authDir, { recursive: true, force: true });
|
|
@@ -142,6 +240,44 @@ export class WhatsAppWorkerHandler {
|
|
|
142
240
|
|
|
143
241
|
try {
|
|
144
242
|
const { state, saveCreds } = await useMultiFileAuthState(this.authDir);
|
|
243
|
+
|
|
244
|
+
// Wrap saveCreds to mirror credentials to central MongoDB
|
|
245
|
+
const originalSaveCreds = saveCreds;
|
|
246
|
+
const wrappedSaveCreds = async () => {
|
|
247
|
+
await originalSaveCreds();
|
|
248
|
+
try {
|
|
249
|
+
const credsPath = path.join(this.authDir, "creds.json");
|
|
250
|
+
if (fs.existsSync(credsPath)) {
|
|
251
|
+
const credsStr = fs.readFileSync(credsPath, "utf-8");
|
|
252
|
+
this.queueSyncWrite("creds.json", credsStr);
|
|
253
|
+
}
|
|
254
|
+
} catch (_) {}
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
// Wrap state.keys.set to mirror key additions, updates and removals to central MongoDB
|
|
258
|
+
const originalKeysSet = state.keys.set;
|
|
259
|
+
state.keys.set = async (data) => {
|
|
260
|
+
await originalKeysSet(data);
|
|
261
|
+
for (const category in data) {
|
|
262
|
+
for (const id in data[category]) {
|
|
263
|
+
const value = data[category][id];
|
|
264
|
+
const file = `${category}-${id}.json`;
|
|
265
|
+
const safeName = file.replace(/\//g, "__").replace(/:/g, "-");
|
|
266
|
+
if (value) {
|
|
267
|
+
try {
|
|
268
|
+
const filePath = path.join(this.authDir, safeName);
|
|
269
|
+
if (fs.existsSync(filePath)) {
|
|
270
|
+
const content = fs.readFileSync(filePath, "utf-8");
|
|
271
|
+
this.queueSyncWrite(safeName, content);
|
|
272
|
+
}
|
|
273
|
+
} catch (_) {}
|
|
274
|
+
} else {
|
|
275
|
+
this.queueSyncDelete(safeName);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
|
|
145
281
|
let version = undefined;
|
|
146
282
|
try {
|
|
147
283
|
const fetched = await fetchLatestBaileysVersion();
|
|
@@ -167,7 +303,7 @@ export class WhatsAppWorkerHandler {
|
|
|
167
303
|
|
|
168
304
|
this.sock = makeWASocket(socketConfig);
|
|
169
305
|
|
|
170
|
-
this.sock.ev.on("creds.update",
|
|
306
|
+
this.sock.ev.on("creds.update", wrappedSaveCreds);
|
|
171
307
|
|
|
172
308
|
// Message Upsert listener for companion store sync & pre-send delivery reconciliation
|
|
173
309
|
this.sock.ev.on("messages.upsert", ({ messages }) => {
|
|
@@ -243,6 +379,7 @@ export class WhatsAppWorkerHandler {
|
|
|
243
379
|
this.user = null;
|
|
244
380
|
this.currentQrDataUrl = null;
|
|
245
381
|
this.clearAuth();
|
|
382
|
+
this.onClearSession();
|
|
246
383
|
|
|
247
384
|
if (this.sock) {
|
|
248
385
|
try {
|
|
@@ -422,15 +559,23 @@ export class WhatsAppWorkerHandler {
|
|
|
422
559
|
// 3. Stream PDF from authenticated URL (Zero buffer in central WebSocket)
|
|
423
560
|
const response = await axios.get(downloadUrl, {
|
|
424
561
|
responseType: "arraybuffer",
|
|
425
|
-
timeout:
|
|
562
|
+
timeout: 25000,
|
|
426
563
|
});
|
|
427
564
|
|
|
428
565
|
const buffer = Buffer.from(response.data);
|
|
566
|
+
if (!buffer || buffer.length === 0) {
|
|
567
|
+
throw new Error(`Failed to download document: received 0 bytes from ${downloadUrl}`);
|
|
568
|
+
}
|
|
429
569
|
|
|
430
570
|
// 4. SHA-256 Integrity Verification
|
|
571
|
+
const actualSha256 = crypto.createHash("sha256").update(buffer).digest("hex");
|
|
572
|
+
const serverSha256 = response.headers?.["x-sha256"] || response.headers?.["x-checksum-sha256"];
|
|
573
|
+
|
|
431
574
|
if (expectedSha256) {
|
|
432
|
-
const
|
|
433
|
-
|
|
575
|
+
const matchesExpected = actualSha256.toLowerCase() === expectedSha256.toLowerCase();
|
|
576
|
+
const matchesServerHeader = serverSha256 && actualSha256.toLowerCase() === String(serverSha256).toLowerCase();
|
|
577
|
+
|
|
578
|
+
if (!matchesExpected && !matchesServerHeader) {
|
|
434
579
|
throw new Error(
|
|
435
580
|
`Document integrity check failed. Expected SHA-256 ${expectedSha256}, got ${actualSha256}`
|
|
436
581
|
);
|
|
@@ -481,6 +626,7 @@ export class WhatsAppWorkerHandler {
|
|
|
481
626
|
}
|
|
482
627
|
|
|
483
628
|
this.clearAuth();
|
|
629
|
+
this.onClearSession();
|
|
484
630
|
this.status = "DISCONNECTED";
|
|
485
631
|
this.user = null;
|
|
486
632
|
this.currentQrDataUrl = null;
|
package/LICENSE.txt
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Chetan Mohite
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|