@dongdev/fca-unofficial 3.0.25 → 3.0.28

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.
Files changed (35) hide show
  1. package/.gitattributes +2 -0
  2. package/CHANGELOG.md +196 -190
  3. package/DOCS.md +3 -6
  4. package/Fca_Database/database.sqlite +0 -0
  5. package/LICENSE-MIT +1 -1
  6. package/README.md +1 -1
  7. package/index.d.ts +745 -746
  8. package/module/config.js +29 -33
  9. package/module/login.js +133 -136
  10. package/module/loginHelper.js +1240 -1048
  11. package/module/options.js +44 -45
  12. package/package.json +81 -82
  13. package/src/api/messaging/changeAdminStatus.js +56 -56
  14. package/src/api/messaging/changeGroupImage.js +2 -1
  15. package/src/api/messaging/changeThreadEmoji.js +47 -47
  16. package/src/api/messaging/createPoll.js +25 -25
  17. package/src/api/messaging/deleteMessage.js +110 -30
  18. package/src/api/messaging/forwardAttachment.js +28 -28
  19. package/src/api/messaging/removeUserFromGroup.js +28 -73
  20. package/src/api/messaging/sendMessage.js +15 -17
  21. package/src/api/messaging/sendTypingIndicator.js +23 -23
  22. package/src/api/messaging/setMessageReaction.js +57 -60
  23. package/src/api/messaging/setTitle.js +47 -47
  24. package/src/api/messaging/uploadAttachment.js +471 -73
  25. package/src/api/socket/core/connectMqtt.js +250 -250
  26. package/src/api/socket/core/emitAuth.js +1 -1
  27. package/src/api/socket/core/getSeqID.js +322 -40
  28. package/src/api/socket/core/parseDelta.js +368 -377
  29. package/src/api/socket/listenMqtt.js +371 -360
  30. package/src/utils/client.js +2 -312
  31. package/src/utils/cookies.js +68 -0
  32. package/src/utils/format.js +117 -90
  33. package/src/utils/loginParser.js +347 -0
  34. package/src/utils/messageFormat.js +1173 -0
  35. package/src/api/socket/core/markDelivery.js +0 -12
@@ -62,30 +62,30 @@ module.exports = function (defaultFuncs, api, ctx) {
62
62
  });
63
63
  return returnPromise;
64
64
  };
65
- function setTitleMqtt(newTitle, threadID, callback) {
66
- if (!ctx.mqttClient) {
67
- throw new Error("Not connected to MQTT");
68
- }
69
- if (typeof ctx.wsReqNumber !== "number") ctx.wsReqNumber = 0;
70
- const reqID = ++ctx.wsReqNumber;
71
- var resolveFunc = function () { };
72
- var rejectFunc = function () { };
73
- var returnPromise = new Promise(function (resolve, reject) {
74
- resolveFunc = resolve;
75
- rejectFunc = reject;
76
- });
77
- const done = (err, data) => {
78
- if (err) {
79
- if (callback) callback(err);
80
- return rejectFunc(err);
81
- }
82
- if (callback) callback(null, data);
83
- resolveFunc(data);
84
- };
85
- var form = JSON.stringify({
86
- "app_id": "2220391788200892",
87
- "payload": JSON.stringify({
88
- epoch_id: generateOfflineThreadingID(),
65
+ function setTitleMqtt(newTitle, threadID, callback) {
66
+ if (!ctx.mqttClient) {
67
+ throw new Error("Not connected to MQTT");
68
+ }
69
+ if (typeof ctx.wsReqNumber !== "number") ctx.wsReqNumber = 0;
70
+ const reqID = ++ctx.wsReqNumber;
71
+ var resolveFunc = function () { };
72
+ var rejectFunc = function () { };
73
+ var returnPromise = new Promise(function (resolve, reject) {
74
+ resolveFunc = resolve;
75
+ rejectFunc = reject;
76
+ });
77
+ const done = (err, data) => {
78
+ if (err) {
79
+ if (callback) callback(err);
80
+ return rejectFunc(err);
81
+ }
82
+ if (callback) callback(null, data);
83
+ resolveFunc(data);
84
+ };
85
+ var form = JSON.stringify({
86
+ "app_id": "2220391788200892",
87
+ "payload": JSON.stringify({
88
+ epoch_id: generateOfflineThreadingID(),
89
89
  tasks: [
90
90
  {
91
91
  failure_count: null,
@@ -99,26 +99,26 @@ module.exports = function (defaultFuncs, api, ctx) {
99
99
  task_id: Math.random() * 1001 << 0
100
100
  }
101
101
  ],
102
- version_id: '8798795233522156'
103
- }),
104
- "request_id": reqID,
105
- "type": 3
106
- });
107
- ctx.mqttClient.publish("/ls_req", form, { qos: 1, retain: false }, (err) => {
108
- if (err) return done(err);
109
- done(null, { success: true });
110
- });
111
- return returnPromise;
112
- };
113
- return function setTitle(newTitle, threadID, callback) {
114
- if (ctx.mqttClient) {
115
- try {
116
- return setTitleMqtt(newTitle, threadID, callback);
117
- } catch (e) {
118
- return setTitleNoMqtt(newTitle, threadID, callback);
119
- }
120
- } else {
121
- return setTitleNoMqtt(newTitle, threadID, callback);
122
- }
123
- };
124
- };
102
+ version_id: '8798795233522156'
103
+ }),
104
+ "request_id": reqID,
105
+ "type": 3
106
+ });
107
+ ctx.mqttClient.publish("/ls_req", form, { qos: 1, retain: false }, (err) => {
108
+ if (err) return done(err);
109
+ done(null, { success: true });
110
+ });
111
+ return returnPromise;
112
+ };
113
+ return function setTitle(newTitle, threadID, callback) {
114
+ if (ctx.mqttClient) {
115
+ try {
116
+ return setTitleMqtt(newTitle, threadID, callback);
117
+ } catch (e) {
118
+ return setTitleNoMqtt(newTitle, threadID, callback);
119
+ }
120
+ } else {
121
+ return setTitleNoMqtt(newTitle, threadID, callback);
122
+ }
123
+ };
124
+ };
@@ -1,94 +1,492 @@
1
1
  "use strict";
2
+
3
+ const axios = require("axios");
4
+ const { wrapper } = require("axios-cookiejar-support");
5
+ const { CookieJar } = require("tough-cookie");
6
+ const FormData = require("form-data");
7
+ const fs = require("fs");
8
+ const path = require("path");
9
+ const stream = require("stream");
10
+ const { URL } = require("url");
2
11
  const log = require("npmlog");
3
- const { parseAndCheckLogin } = require("../../utils/client");
4
- const { getType } = require("../../utils/format");
5
- const { isReadableStream } = require("../../utils/constants");
6
- module.exports = function(defaultFuncs, api, ctx) {
7
- function upload(attachments, callback) {
8
- callback = callback || function() {};
9
- const uploads = [];
10
-
11
- // create an array of promises
12
- for (let i = 0; i < attachments.length; i++) {
13
- if (!isReadableStream(attachments[i])) {
14
- throw {
15
- error:
16
- "Attachment should be a readable stream and not " +
17
- getType(attachments[i]) +
18
- "."
19
- };
20
- }
21
12
 
22
- const form = {
23
- farr: attachments[i]
24
- };
13
+ let http = null;
14
+ let cookieJar = new CookieJar();
15
+ let tokenCache = null;
16
+ let tokenCacheTime = 0;
17
+ const TOKEN_CACHE_TTL = 5 * 60 * 1000;
25
18
 
26
- uploads.push(
27
- defaultFuncs
28
- .postFormData(
29
- "https://www.facebook.com/ajax/mercury/upload.php",
30
- ctx.jar,
31
- form,
32
- {}
33
- )
34
- .then(parseAndCheckLogin(ctx, defaultFuncs))
35
- .then(function(resData) {
36
- if (resData.error) {
37
- throw resData;
38
- }
19
+ const DEFAULT_UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36";
20
+
21
+ function cleanJSON(x) {
22
+ if (typeof x !== "string") return x;
23
+ const s = x.replace(/^for\s*\(;;\);\s*/i, "");
24
+ try { return JSON.parse(s); } catch { return s; }
25
+ }
26
+
27
+ function pick(re, html, i = 1) {
28
+ const m = html && html.match(re);
29
+ return m ? m[i] : "";
30
+ }
39
31
 
40
- // We have to return the data unformatted unless we want to change it
41
- // back in sendMessage.
42
- return resData.payload.metadata[0];
43
- })
44
- );
45
- }
46
-
47
- // resolve all promises
48
- Promise.all(uploads)
49
- .then(function(resData) {
50
- callback(null, resData);
51
- })
52
- .catch(function(err) {
53
- log.error("uploadAttachment", err);
54
- return callback(err);
32
+ function getFrom(html, a, b) {
33
+ const i = html.indexOf(a);
34
+ if (i < 0) return;
35
+ const start = i + a.length;
36
+ const j = html.indexOf(b, start);
37
+ return j < 0 ? undefined : html.slice(start, j);
38
+ }
39
+
40
+ function respFinalUrl(res) {
41
+ return (res && (res.url || res.requestUrl)) || "";
42
+ }
43
+
44
+ function detectCheckpoint(res) {
45
+ const url = String(respFinalUrl(res) || "");
46
+ const body = typeof res?.body === "string" ? res.body : "";
47
+ const hit =
48
+ /\/checkpoint\//i.test(url) ||
49
+ /(?:href|action)\s*=\s*["']https?:\/\/[^"']*\/checkpoint\//i.test(body) ||
50
+ /"checkpoint"|checkpoint_title|checkpointMain|id="checkpoint"/i.test(body) ||
51
+ (/login\.php/i.test(url) && /checkpoint/i.test(body));
52
+ return { hit, url: url || (body.match(/https?:\/\/[^"']*\/checkpoint\/[^"'<>]*/i)?.[0] || "") };
53
+ }
54
+
55
+ function checkpointError(res) {
56
+ const d = detectCheckpoint(res);
57
+ if (!d.hit) return null;
58
+ const e = new Error("Checkpoint required");
59
+ e.code = "CHECKPOINT";
60
+ e.checkpoint = true;
61
+ e.url = d.url || "https://www.facebook.com/checkpoint/";
62
+ e.status = res?.statusCode || res?.status;
63
+ return e;
64
+ }
65
+
66
+ async function httpGet(pageUrl, ua, headers = {}) {
67
+ const host = new URL(pageUrl).hostname;
68
+ const referer = `https://${host}/`;
69
+ const baseHeaders = {
70
+ Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
71
+ "Accept-Language": "vi-VN,vi;q=0.9,en-US;q=0.8,en;q=0.7",
72
+ "Accept-Encoding": "gzip, deflate, br",
73
+ "Cache-Control": "max-age=0",
74
+ Connection: "keep-alive",
75
+ Host: host,
76
+ Origin: `https://${host}`,
77
+ Referer: referer,
78
+ "Sec-Ch-Prefers-Color-Scheme": "dark",
79
+ "Sec-Ch-Ua": '"Google Chrome";v="143", "Chromium";v="143", "Not A(Brand";v="24"',
80
+ "Sec-Ch-Ua-Full-Version-List": '"Google Chrome";v="143.0.7499.182", "Chromium";v="143.0.7499.182", "Not A(Brand";v="24.0.0.0"',
81
+ "Sec-Ch-Ua-Mobile": "?0",
82
+ "Sec-Ch-Ua-Model": '""',
83
+ "Sec-Ch-Ua-Platform": '"Windows"',
84
+ "Sec-Ch-Ua-Platform-Version": '"19.0.0"',
85
+ "Sec-Fetch-Dest": "document",
86
+ "Sec-Fetch-Mode": "navigate",
87
+ "Sec-Fetch-Site": "same-origin",
88
+ "Sec-Fetch-User": "?1",
89
+ "Upgrade-Insecure-Requests": "1",
90
+ "User-Agent": ua || DEFAULT_UA,
91
+ "x-fb-rlafr": "0"
92
+ };
93
+ const res = await http.get(pageUrl, {
94
+ headers: { ...baseHeaders, ...headers },
95
+ timeout: 30000
96
+ });
97
+ const cp = checkpointError(res);
98
+ if (cp) throw cp;
99
+ return typeof res.data === "string" ? res.data : String(res.data || "");
100
+ }
101
+
102
+ async function getTokens(ua, forceRefresh = false) {
103
+ const now = Date.now();
104
+ if (!forceRefresh && tokenCache && (now - tokenCacheTime) < TOKEN_CACHE_TTL) {
105
+ return tokenCache;
106
+ }
107
+ try {
108
+ const html = await httpGet("https://www.facebook.com/", ua, { Referer: "https://www.facebook.com/" });
109
+ const fb_dtsg = getFrom(html, '"DTSGInitData",[],{"token":"', '",') || html.match(/name="fb_dtsg"\s+value="([^"]+)"/)?.[1] || "";
110
+ const jazoest = getFrom(html, 'name="jazoest" value="', '"') || getFrom(html, "jazoest=", '",') || html.match(/name="jazoest"\s+value="([^"]+)"/)?.[1] || "";
111
+ const lsd = getFrom(html, '["LSD",[],{"token":"', '"}') || html.match(/name="lsd"\s+value="([^"]+)"/)?.[1] || "";
112
+ const spin_r = pick(/"__spin_r":(\d+)/, html) || "";
113
+ const spin_t = pick(/"__spin_t":(\d+)/, html) || "";
114
+ const rev = pick(/"__rev":(\d+)/, html) || "";
115
+
116
+ if (!fb_dtsg || !lsd) {
117
+ // Cố gắng fallback nếu regex fail, nhưng thường là do cookie die
118
+ if (!tokenCache) throw new Error("Failed to fetch fb_dtsg or LSD from Facebook");
119
+ }
120
+
121
+ tokenCache = { lsd, fb_dtsg, jazoest, spin_r, spin_t, rev };
122
+ tokenCacheTime = now;
123
+ return tokenCache;
124
+ } catch (e) {
125
+ if (tokenCache) {
126
+ log.warn("[uploadAttachment] Token fetch failed, using cached tokens: " + (e.message || e));
127
+ return tokenCache;
128
+ }
129
+ throw e;
130
+ }
131
+ }
132
+
133
+ function getType(obj) { return Object.prototype.toString.call(obj).slice(8, -1); }
134
+ function isReadableStream(obj) { return obj instanceof stream.Readable && (getType(obj._read) === "Function" || getType(obj._read) === "AsyncFunction") && getType(obj._readableState) === "Object"; }
135
+ function fromBuffer(buf) { return stream.Readable.from(buf); }
136
+ function parseDataUrl(s) { const m = /^data:([^;,]+)?(;base64)?,(.*)$/i.exec(s); if (!m) return null; const mime = m[1] || "application/octet-stream"; const isB64 = !!m[2]; const data = isB64 ? Buffer.from(m[3], "base64") : Buffer.from(decodeURIComponent(m[3]), "utf8"); return { mime, data }; }
137
+
138
+ function filenameFromUrl(u, headers) {
139
+ try {
140
+ const urlObj = new URL(u);
141
+ let filename = path.basename(urlObj.pathname) || `file-${Date.now()}`;
142
+ const cd = headers && (headers["content-disposition"] || headers["Content-Disposition"]);
143
+ if (cd) {
144
+ const m = /filename\*?=(?:UTF-8''|")?([^";\n]+)/i.exec(cd);
145
+ if (m) filename = decodeURIComponent(m[1].replace(/"/g, ""));
146
+ }
147
+ return filename;
148
+ } catch {
149
+ return `file-${Date.now()}`;
150
+ }
151
+ }
152
+
153
+ async function normalizeOne(input, ua) {
154
+ if (!input) throw new Error("Invalid input");
155
+ if (Buffer.isBuffer(input)) return { stream: fromBuffer(input), filename: `file-${Date.now()}.bin`, contentType: "application/octet-stream" };
156
+ if (typeof input === "string") {
157
+ if (/^https?:\/\//i.test(input)) {
158
+ const resp = await http.get(input, {
159
+ headers: {
160
+ "User-Agent": ua,
161
+ Accept: "*/*",
162
+ "Accept-Encoding": "gzip, deflate, br",
163
+ "Cache-Control": "no-cache"
164
+ },
165
+ timeout: 30000,
166
+ responseType: "stream"
55
167
  });
168
+ const s = resp.data;
169
+ const filename = filenameFromUrl(input, resp.headers);
170
+ return { stream: s, filename };
171
+ }
172
+ if (input.startsWith("data:")) {
173
+ const p = parseDataUrl(input);
174
+ if (!p) throw new Error("Bad data URL");
175
+ return { stream: fromBuffer(p.data), filename: `file-${Date.now()}`, contentType: p.mime };
176
+ }
177
+ if (fs.existsSync(input) && fs.statSync(input).isFile()) {
178
+ return { stream: fs.createReadStream(input), filename: path.basename(input) };
179
+ }
180
+ throw new Error(`Unsupported string input: ${input}`);
181
+ }
182
+ if (isReadableStream(input)) {
183
+ return { stream: input, filename: `file-${Date.now()}` };
184
+ }
185
+ if (typeof input === "object") {
186
+ if (input.buffer && Buffer.isBuffer(input.buffer)) {
187
+ const filename = input.filename || `file-${Date.now()}.bin`;
188
+ const contentType = input.contentType || "application/octet-stream";
189
+ return { stream: fromBuffer(input.buffer), filename, contentType };
190
+ }
191
+ if (input.data && Buffer.isBuffer(input.data)) {
192
+ const filename = input.filename || `file-${Date.now()}.bin`;
193
+ const contentType = input.contentType || "application/octet-stream";
194
+ return { stream: fromBuffer(input.data), filename, contentType };
195
+ }
196
+ if (input.stream && isReadableStream(input.stream)) {
197
+ const filename = input.filename || `file-${Date.now()}`;
198
+ const contentType = input.contentType;
199
+ return { stream: input.stream, filename, contentType };
200
+ }
201
+ if (input.url) {
202
+ return normalizeOne(String(input.url), ua);
203
+ }
204
+ if (input.path && fs.existsSync(input.path) && fs.statSync(input.path).isFile()) {
205
+ return { stream: fs.createReadStream(input.path), filename: input.filename || path.basename(input.path), contentType: input.contentType };
206
+ }
56
207
  }
208
+ throw new Error("Unrecognized input");
209
+ }
57
210
 
58
- return function uploadAttachment(attachments, callback) {
59
- if (
60
- !attachments &&
61
- !isReadableStream(attachments) &&
62
- !getType(attachments) === "Array" &&
63
- getType(attachments) === "Array" && !attachments.length
64
- )
65
- throw { error: "Please pass an attachment or an array of attachments." };
66
-
67
- let resolveFunc = function() {};
68
- let rejectFunc = function() {};
69
- const returnPromise = new Promise(function(resolve, reject) {
211
+ function mapAttachmentDetails(data) {
212
+ const out = [];
213
+ if (!data || typeof data !== "object") return out;
214
+
215
+ const stack = [data];
216
+ while (stack.length) {
217
+ const cur = stack.pop();
218
+ if (!cur || typeof cur !== "object") continue;
219
+ const id = cur.video_id || cur.image_id || cur.audio_id || cur.file_id || cur.fbid || cur.id || cur.upload_id || cur.gif_id;
220
+ const idKey =
221
+ cur.video_id ? "video_id" :
222
+ cur.image_id ? "image_id" :
223
+ cur.audio_id ? "audio_id" :
224
+ cur.file_id ? "file_id" :
225
+ cur.gif_id ? "gif_id" :
226
+ cur.fbid ? "fbid" :
227
+ id ? "id" : null;
228
+ const filename = cur.filename || cur.file_name || cur.name || cur.original_filename;
229
+ const filetype = cur.filetype || cur.mime_type || cur.type || cur.content_type;
230
+ let thumbnail = cur.thumbnail_src || cur.thumbnail_url || cur.preview_url || cur.thumbSrc || cur.thumb_url || cur.image_preview_url || cur.large_preview_url;
231
+ if (!thumbnail) {
232
+ const m = cur.media || cur.thumbnail || cur.thumb || cur.image_data || cur.video_data || cur.preview;
233
+ thumbnail = m?.thumbnail_src || m?.thumbnail_url || m?.src || m?.uri || m?.url;
234
+ }
235
+ if (idKey) {
236
+ const o = {};
237
+ o[idKey] = id;
238
+ if (filename) o.filename = filename;
239
+ if (filetype) o.filetype = filetype;
240
+ if (thumbnail) o.thumbnail_src = thumbnail;
241
+ out.push(o);
242
+ }
243
+ if (Array.isArray(cur)) {
244
+ for (const v of cur) stack.push(v);
245
+ } else {
246
+ for (const k of Object.keys(cur)) stack.push(cur[k]);
247
+ }
248
+ }
249
+
250
+ if (!out.length && data.payload && Array.isArray(data.payload.metadata)) {
251
+ return data.payload.metadata.slice();
252
+ }
253
+
254
+ return out;
255
+ }
256
+
257
+ function pLimit(n) {
258
+ let active = 0;
259
+ const queue = [];
260
+ const next = () => {
261
+ active--;
262
+ if (queue.length) queue.shift()();
263
+ };
264
+ return fn => new Promise((resolve, reject) => {
265
+ const run = () => {
266
+ active++;
267
+ fn().then(v => { resolve(v); next(); }).catch(e => { reject(e); next(); });
268
+ };
269
+ if (active < n) run(); else queue.push(run);
270
+ });
271
+ }
272
+
273
+ // Hàm upload core xử lý request
274
+ async function singleUpload(urlBase, file, ua, tokens, retries = 2) {
275
+ const form = new FormData();
276
+ // QUAN TRỌNG: Chỉ append file, KHÔNG append fb_dtsg vào body nữa
277
+ form.append("farr", file.stream, { filename: file.filename, contentType: file.contentType });
278
+
279
+ const headers = {
280
+ ...form.getHeaders(),
281
+ Accept: "*/*",
282
+ "Accept-Language": "vi,en-US;q=0.9,en;q=0.8,fr-FR;q=0.7,fr;q=0.6",
283
+ "Accept-Encoding": "gzip, deflate, br",
284
+ "User-Agent": ua,
285
+ "x-asbd-id": "359341",
286
+ "x-fb-lsd": tokens.lsd || "",
287
+ "x-fb-friendly-name": "MercuryUpload",
288
+ "x-fb-request-analytics-tags": JSON.stringify({
289
+ network_tags: {
290
+ product: "256002347743983",
291
+ purpose: "none",
292
+ request_category: "graphql",
293
+ retry_attempt: "0"
294
+ },
295
+ application_tags: "graphservice"
296
+ }),
297
+ "sec-ch-prefers-color-scheme": "dark",
298
+ "sec-ch-ua": '"Google Chrome";v="143", "Chromium";v="143", "Not A(Brand";v="24"',
299
+ "sec-ch-ua-mobile": "?0",
300
+ "sec-ch-ua-platform": '"Windows"',
301
+ "sec-fetch-dest": "empty",
302
+ "sec-fetch-mode": "cors",
303
+ "sec-fetch-site": "same-origin",
304
+ Origin: "https://www.facebook.com",
305
+ Referer: "https://www.facebook.com/",
306
+ "x-fb-rlafr": "0",
307
+ Connection: "keep-alive"
308
+ };
309
+
310
+ // Build URL với query string tokens
311
+ const finalUrl = new URL(urlBase);
312
+ finalUrl.searchParams.set("fb_dtsg", tokens.fb_dtsg);
313
+ finalUrl.searchParams.set("jazoest", tokens.jazoest);
314
+ finalUrl.searchParams.set("lsd", tokens.lsd);
315
+ finalUrl.searchParams.set("__aaid", "0");
316
+ finalUrl.searchParams.set("__ccg", "EXCELLENT");
317
+
318
+ for (let attempt = 0; attempt <= retries; attempt++) {
319
+ try {
320
+ const res = await http.post(finalUrl.toString(), form, {
321
+ headers,
322
+ timeout: 120000,
323
+ maxContentLength: Infinity,
324
+ maxBodyLength: Infinity
325
+ });
326
+ return res;
327
+ } catch (e) {
328
+ if (attempt === retries) throw e;
329
+ if (e.code === "ETIMEDOUT" || e.code === "ECONNRESET" || (e.response && e.response.status >= 500)) {
330
+ await new Promise(r => setTimeout(r, (attempt + 1) * 1000));
331
+ continue;
332
+ }
333
+ throw e;
334
+ }
335
+ }
336
+ }
337
+
338
+ module.exports = function (defaultFuncs, api, ctx) {
339
+ const ua = ctx?.options?.userAgent || DEFAULT_UA;
340
+ cookieJar = ctx.jar instanceof CookieJar ? ctx.jar : new CookieJar();
341
+
342
+ // Axios instance
343
+ http = wrapper(axios.create({
344
+ timeout: 60000,
345
+ headers: {
346
+ "User-Agent": ua,
347
+ "Accept-Language": "vi-VN,vi;q=0.9,en-US;q=0.8,en;q=0.7",
348
+ "Accept-Encoding": "gzip, deflate, br",
349
+ Connection: "keep-alive"
350
+ },
351
+ maxRedirects: 5,
352
+ validateStatus: () => true
353
+ }));
354
+ http.defaults.withCredentials = true;
355
+ http.defaults.jar = cookieJar;
356
+
357
+ async function uploadCore(link, opts, callback) {
358
+ if (typeof opts === "function") { callback = opts; opts = undefined; }
359
+ const options = {
360
+ concurrency: Math.max(1, Math.min(5, Number(opts?.concurrency || 3))),
361
+ mode: opts?.mode === "single" ? "single" : "parallel"
362
+ };
363
+
364
+ let resolveFunc = function () { };
365
+ let rejectFunc = function () { };
366
+ const returnPromise = new Promise(function (resolve, reject) {
70
367
  resolveFunc = resolve;
71
368
  rejectFunc = reject;
72
369
  });
73
-
74
370
  if (!callback) {
75
- callback = function(err, info) {
76
- if (err) {
77
- return rejectFunc(err);
78
- }
79
- resolveFunc(info);
371
+ callback = function (err, data) {
372
+ if (err) return rejectFunc(err);
373
+ resolveFunc(data);
80
374
  };
81
375
  }
82
376
 
83
- if (getType(attachments) !== "Array") attachments = [attachments];
377
+ (async () => {
378
+ try {
379
+ const inputsArr = Array.isArray(link) ? link : [link];
380
+ if (!inputsArr.length) {
381
+ const e = new Error("No files to upload");
382
+ callback(e);
383
+ return;
384
+ }
385
+
386
+ let tokens = await getTokens(ua);
387
+ const normAll = await Promise.all(inputsArr.map(x => normalizeOne(x, ua)));
388
+
389
+ // Base QS setup
390
+ const qs = [];
391
+ const userId = (ctx && (ctx.userID || ctx.userId)) ? String(ctx.userID || ctx.userId) : "";
392
+ if (userId) qs.push(`__user=${encodeURIComponent(userId)}`);
393
+ qs.push("__a=1");
394
+ qs.push("dpr=1");
395
+ const reqId = Math.floor(Math.random() * 36 ** 2).toString(36);
396
+ qs.push(`__req=${encodeURIComponent(reqId)}`);
397
+ if (tokens.spin_r) qs.push(`__spin_r=${encodeURIComponent(tokens.spin_r)}`);
398
+ if (tokens.spin_t) qs.push(`__spin_t=${encodeURIComponent(tokens.spin_t)}`);
399
+ if (tokens.rev) qs.push(`__rev=${encodeURIComponent(tokens.rev)}`);
400
+ qs.push("__spin_b=trunk");
401
+ qs.push("__comet_req=15");
84
402
 
85
- upload(attachments, (err, info) => {
86
- if (err) {
87
- return callback(err);
403
+ const baseUrl = `https://www.facebook.com/ajax/mercury/upload.php?${qs.join("&")}`;
404
+
405
+ if (options.mode === "single") {
406
+ const f = normAll[0];
407
+ const res = await singleUpload(baseUrl, f, ua, tokens);
408
+
409
+ const cp = checkpointError(res);
410
+ if (cp) { tokenCache = null; throw cp; }
411
+
412
+ const data = cleanJSON(res.data);
413
+ const ids = mapAttachmentDetails(data);
414
+
415
+ if (!ids.length) {
416
+ const e = new Error("UploadFb returned no metadata/ids");
417
+ e.code = "NO_METADATA";
418
+ e.status = res.status;
419
+ e.body = typeof data === "string" ? data.slice(0, 500) : data;
420
+ throw e;
421
+ }
422
+ log.info(`[uploadAttachment] success ${ids.length} item(s) status ${res.status}`);
423
+ callback(null, { status: res.status, ids, raw: data });
424
+ return;
425
+ }
426
+
427
+ // Parallel mode
428
+ const limit = pLimit(options.concurrency);
429
+ const tasks = normAll.map(f => () => singleUpload(baseUrl, f, ua, tokens));
430
+ const results = await Promise.all(tasks.map(t => limit(t)));
431
+
432
+ const ids = [];
433
+ const errors = [];
434
+ for (let i = 0; i < results.length; i++) {
435
+ const res = results[i];
436
+ try {
437
+ const cp = checkpointError(res);
438
+ if (cp) { tokenCache = null; throw cp; }
439
+
440
+ const data = cleanJSON(res.data);
441
+ const fileIds = mapAttachmentDetails(data);
442
+ if (!fileIds.length) {
443
+ log.warn(`[uploadAttachment] File ${i + 1} returned no metadata/ids`);
444
+ continue;
445
+ }
446
+ ids.push(...fileIds);
447
+ } catch (e) {
448
+ errors.push({ index: i, error: e });
449
+ log.error(`[uploadAttachment] Upload ${i + 1} failed: ${e.message || e}`);
450
+ }
451
+ }
452
+
453
+ if (ids.length === 0 && errors.length > 0) {
454
+ throw errors[0].error;
455
+ }
456
+
457
+ log.info(`[uploadAttachment] success ${ids.length}/${normAll.length} item(s)`);
458
+ callback(null, { status: 200, ids, raw: null, errors: errors.length > 0 ? errors : undefined });
459
+ } catch (e) {
460
+ if (e.code === "CHECKPOINT" || (e.response && [401, 403].includes(e.response.status))) {
461
+ tokenCache = null;
462
+ try {
463
+ await getTokens(ua, true);
464
+ log.info("[uploadAttachment] Tokens refreshed after error");
465
+ } catch (refreshErr) {
466
+ log.error("[uploadAttachment] Token refresh failed: " + (refreshErr.message || refreshErr));
467
+ }
468
+ }
469
+ log.error(`[uploadAttachment] error ${e.code || e.status || ""} ${e.message || e}`);
470
+ callback(e);
88
471
  }
89
- callback(null, info);
472
+ })().catch(err => {
473
+ log.error("[uploadAttachment] Unhandled promise rejection: " + (err.message || err));
474
+ rejectFunc(err);
90
475
  });
91
476
 
92
477
  return returnPromise;
478
+ }
479
+
480
+ return function uploadAttachment(attachments, callback) {
481
+ if (!attachments) throw { error: "Please pass an attachment or an array of attachments." };
482
+
483
+ if (typeof callback === "function") {
484
+ return uploadCore(attachments, { mode: "parallel" }, (err, result) => {
485
+ if (err) return callback(err);
486
+ callback(null, result && Array.isArray(result.ids) ? result.ids : []);
487
+ }).then(result => (result && Array.isArray(result.ids) ? result.ids : []));
488
+ }
489
+
490
+ return uploadCore(attachments, { mode: "parallel" }).then(result => result && Array.isArray(result.ids) ? result.ids : []);
93
491
  };
94
- };
492
+ };