@dongdev/fca-unofficial 3.0.11 → 3.0.12

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/CHANGELOG.md CHANGED
@@ -161,3 +161,6 @@ Too lazy to write changelog, sorry! (will write changelog in the next release, t
161
161
 
162
162
  ## v3.0.10 - 2025-12-05
163
163
  - Hotfix / auto bump
164
+
165
+ ## v3.0.11 - 2025-12-05
166
+ - Hotfix / auto bump
@@ -284,14 +284,31 @@ async function setJarCookies(j, appstate) {
284
284
  const cookieName = c.name || c.key;
285
285
  const cookieValue = c.value;
286
286
  if (!cookieName || cookieValue === undefined) continue;
287
- const dom = (c.domain || ".facebook.com").replace(/^\./, "");
288
- const path = c.path || "/";
289
- const base1 = `https://${dom}${path}`;
290
- const base2 = `https://www.${dom}${path}`;
291
- const domain = c.domain || ".facebook.com";
292
- const str = `${cookieName}=${cookieValue}; Domain=${domain}; Path=${path};`;
293
- tasks.push(j.setCookie(str, base1));
294
- tasks.push(j.setCookie(str, base2));
287
+
288
+ const cookieDomain = c.domain || ".facebook.com";
289
+ const cookiePath = c.path || "/";
290
+ const dom = cookieDomain.replace(/^\./, "");
291
+
292
+ // Format expires if provided
293
+ let expiresStr = "";
294
+ if (c.expires) {
295
+ const expiresDate = typeof c.expires === "number" ? new Date(c.expires) : new Date(c.expires);
296
+ expiresStr = `; expires=${expiresDate.toUTCString()}`;
297
+ }
298
+
299
+ // Build cookie string
300
+ const str = `${cookieName}=${cookieValue}${expiresStr}; Domain=${cookieDomain}; Path=${cookiePath};`;
301
+
302
+ // Set cookie for both http and https, with and without www
303
+ const base1 = `http://${dom}${cookiePath}`;
304
+ const base2 = `https://${dom}${cookiePath}`;
305
+ const base3 = `http://www.${dom}${cookiePath}`;
306
+ const base4 = `https://www.${dom}${cookiePath}`;
307
+
308
+ tasks.push(j.setCookie(str, base1).catch(() => { }));
309
+ tasks.push(j.setCookie(str, base2).catch(() => { }));
310
+ tasks.push(j.setCookie(str, base3).catch(() => { }));
311
+ tasks.push(j.setCookie(str, base4).catch(() => { }));
295
312
  }
296
313
  await Promise.all(tasks);
297
314
  }
@@ -577,23 +594,47 @@ function loginHelper(appState, Cookie, email, password, globalOptions, callback)
577
594
  (async () => {
578
595
  try {
579
596
  if (appState) {
580
- if (typeof appState === "string") {
597
+ // Check and convert cookie to appState format
598
+ if (Array.isArray(appState) && appState.some(c => c.name)) {
599
+ // Convert name to key if needed
600
+ appState = appState.map(c => {
601
+ if (c.name && !c.key) {
602
+ c.key = c.name;
603
+ delete c.name;
604
+ }
605
+ return c;
606
+ });
607
+ } else if (typeof appState === "string") {
608
+ // Try to parse as JSON first
581
609
  let parsed = appState;
582
610
  try {
583
611
  parsed = JSON.parse(appState);
584
612
  } catch { }
613
+
585
614
  if (Array.isArray(parsed)) {
586
- // Use setJarCookies to properly handle individual cookie domains/paths
587
- await setJarCookies(jar, parsed);
588
- } else if (typeof parsed === "string") {
589
- const pairs = normalizeCookieHeaderString(parsed);
590
- if (!pairs.length) throw new Error("Empty appState cookie header");
591
- setJarFromPairs(jar, pairs, domain);
615
+ // Already parsed as array, use it
616
+ appState = parsed;
592
617
  } else {
593
- throw new Error("Invalid appState format");
618
+ // Parse string cookie format (key=value; key2=value2)
619
+ const arrayAppState = [];
620
+ appState.split(';').forEach(c => {
621
+ const [key, value] = c.split('=');
622
+ if (key && value) {
623
+ arrayAppState.push({
624
+ key: key.trim(),
625
+ value: value.trim(),
626
+ domain: ".facebook.com",
627
+ path: "/",
628
+ expires: new Date().getTime() + 1000 * 60 * 60 * 24 * 365
629
+ });
630
+ }
631
+ });
632
+ appState = arrayAppState;
594
633
  }
595
- } else if (Array.isArray(appState)) {
596
- // Use setJarCookies to properly handle individual cookie domains/paths
634
+ }
635
+
636
+ // Set cookies into jar with individual domain/path
637
+ if (Array.isArray(appState)) {
597
638
  await setJarCookies(jar, appState);
598
639
  } else {
599
640
  throw new Error("Invalid appState format");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dongdev/fca-unofficial",
3
- "version": "3.0.11",
3
+ "version": "3.0.12",
4
4
  "description": "Unofficial Facebook Chat API for Node.js - Interact with Facebook Messenger programmatically",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",