@dongdev/fca-unofficial 3.0.30 → 4.0.0

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 (104) hide show
  1. package/LICENSE +191 -0
  2. package/README.md +224 -406
  3. package/dist/index.d.mts +1241 -0
  4. package/dist/index.d.ts +1241 -0
  5. package/dist/index.js +27749 -0
  6. package/dist/index.mjs +27713 -0
  7. package/docs/ARCHITECTURE.md +467 -0
  8. package/docs/DOCS.md +686 -0
  9. package/fca-config.example.json +33 -0
  10. package/package.json +33 -22
  11. package/test/fca.test.cjs +533 -0
  12. package/CHANGELOG.md +0 -293
  13. package/DOCS.md +0 -2712
  14. package/func/checkUpdate.js +0 -222
  15. package/func/logAdapter.js +0 -33
  16. package/func/logger.js +0 -48
  17. package/index.d.ts +0 -751
  18. package/index.js +0 -8
  19. package/module/config.js +0 -40
  20. package/module/login.js +0 -133
  21. package/module/loginHelper.js +0 -1296
  22. package/module/options.js +0 -44
  23. package/src/api/action/addExternalModule.js +0 -25
  24. package/src/api/action/changeAvatar.js +0 -137
  25. package/src/api/action/changeBio.js +0 -75
  26. package/src/api/action/enableAutoSaveAppState.js +0 -73
  27. package/src/api/action/getCurrentUserID.js +0 -7
  28. package/src/api/action/handleFriendRequest.js +0 -57
  29. package/src/api/action/logout.js +0 -76
  30. package/src/api/action/refreshFb_dtsg.js +0 -48
  31. package/src/api/action/setPostReaction.js +0 -106
  32. package/src/api/action/unfriend.js +0 -54
  33. package/src/api/http/httpGet.js +0 -46
  34. package/src/api/http/httpPost.js +0 -52
  35. package/src/api/http/postFormData.js +0 -47
  36. package/src/api/messaging/addUserToGroup.js +0 -68
  37. package/src/api/messaging/changeAdminStatus.js +0 -126
  38. package/src/api/messaging/changeArchivedStatus.js +0 -55
  39. package/src/api/messaging/changeBlockedStatus.js +0 -48
  40. package/src/api/messaging/changeGroupImage.js +0 -91
  41. package/src/api/messaging/changeNickname.js +0 -70
  42. package/src/api/messaging/changeThreadColor.js +0 -79
  43. package/src/api/messaging/changeThreadEmoji.js +0 -111
  44. package/src/api/messaging/createNewGroup.js +0 -88
  45. package/src/api/messaging/createPoll.js +0 -46
  46. package/src/api/messaging/createThemeAI.js +0 -98
  47. package/src/api/messaging/deleteMessage.js +0 -136
  48. package/src/api/messaging/deleteThread.js +0 -56
  49. package/src/api/messaging/editMessage.js +0 -68
  50. package/src/api/messaging/forwardAttachment.js +0 -57
  51. package/src/api/messaging/getEmojiUrl.js +0 -29
  52. package/src/api/messaging/getFriendsList.js +0 -82
  53. package/src/api/messaging/getMessage.js +0 -829
  54. package/src/api/messaging/getThemePictures.js +0 -62
  55. package/src/api/messaging/handleMessageRequest.js +0 -65
  56. package/src/api/messaging/markAsDelivered.js +0 -57
  57. package/src/api/messaging/markAsRead.js +0 -88
  58. package/src/api/messaging/markAsReadAll.js +0 -49
  59. package/src/api/messaging/markAsSeen.js +0 -61
  60. package/src/api/messaging/muteThread.js +0 -50
  61. package/src/api/messaging/removeUserFromGroup.js +0 -62
  62. package/src/api/messaging/resolvePhotoUrl.js +0 -43
  63. package/src/api/messaging/scheduler.js +0 -264
  64. package/src/api/messaging/searchForThread.js +0 -52
  65. package/src/api/messaging/sendMessage.js +0 -270
  66. package/src/api/messaging/sendTypingIndicator.js +0 -74
  67. package/src/api/messaging/setMessageReaction.js +0 -91
  68. package/src/api/messaging/setTitle.js +0 -124
  69. package/src/api/messaging/shareContact.js +0 -49
  70. package/src/api/messaging/threadColors.js +0 -128
  71. package/src/api/messaging/unsendMessage.js +0 -81
  72. package/src/api/messaging/uploadAttachment.js +0 -492
  73. package/src/api/socket/core/connectMqtt.js +0 -258
  74. package/src/api/socket/core/emitAuth.js +0 -103
  75. package/src/api/socket/core/getSeqID.js +0 -320
  76. package/src/api/socket/core/getTaskResponseData.js +0 -25
  77. package/src/api/socket/core/parseDelta.js +0 -377
  78. package/src/api/socket/detail/buildStream.js +0 -215
  79. package/src/api/socket/detail/constants.js +0 -28
  80. package/src/api/socket/listenMqtt.js +0 -377
  81. package/src/api/socket/middleware/index.js +0 -216
  82. package/src/api/threads/getThreadHistory.js +0 -664
  83. package/src/api/threads/getThreadInfo.js +0 -295
  84. package/src/api/threads/getThreadList.js +0 -293
  85. package/src/api/threads/getThreadPictures.js +0 -78
  86. package/src/api/users/getUserID.js +0 -65
  87. package/src/api/users/getUserInfo.js +0 -399
  88. package/src/api/users/getUserInfoV2.js +0 -134
  89. package/src/core/sendReqMqtt.js +0 -96
  90. package/src/database/models/index.js +0 -87
  91. package/src/database/models/thread.js +0 -50
  92. package/src/database/models/user.js +0 -46
  93. package/src/database/threadData.js +0 -98
  94. package/src/database/userData.js +0 -89
  95. package/src/remote/remoteClient.js +0 -123
  96. package/src/utils/broadcast.js +0 -51
  97. package/src/utils/client.js +0 -10
  98. package/src/utils/constants.js +0 -23
  99. package/src/utils/cookies.js +0 -68
  100. package/src/utils/format.js +0 -1174
  101. package/src/utils/headers.js +0 -115
  102. package/src/utils/loginParser.js +0 -365
  103. package/src/utils/messageFormat.js +0 -1173
  104. package/src/utils/request.js +0 -332
@@ -1,492 +0,0 @@
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");
11
- const log = require("../../../func/logAdapter");
12
-
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;
18
-
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
- }
31
-
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"
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
- }
207
- }
208
- throw new Error("Unrecognized input");
209
- }
210
-
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) {
367
- resolveFunc = resolve;
368
- rejectFunc = reject;
369
- });
370
- if (!callback) {
371
- callback = function (err, data) {
372
- if (err) return rejectFunc(err);
373
- resolveFunc(data);
374
- };
375
- }
376
-
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");
402
-
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);
471
- }
472
- })().catch(err => {
473
- log.error("[uploadAttachment] Unhandled promise rejection: " + (err.message || err));
474
- rejectFunc(err);
475
- });
476
-
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 : []);
491
- };
492
- };