@dongdev/fca-unofficial 3.0.4 → 3.0.6

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
@@ -140,3 +140,9 @@ Too lazy to write changelog, sorry! (will write changelog in the next release, t
140
140
 
141
141
  ## v3.0.3 - 2025-11-27
142
142
  - Hotfix / auto bump
143
+
144
+ ## v3.0.4 - 2025-11-27
145
+ - Hotfix / auto bump
146
+
147
+ ## v3.0.5 - 2025-11-27
148
+ - Hotfix / auto bump
@@ -2,8 +2,15 @@ const logger = require("./logger");
2
2
  const fs = require("fs");
3
3
  const path = require("path");
4
4
  const { exec } = require("child_process");
5
+ const https = require("https");
5
6
  const pkgName = "@dongdev/fca-unofficial";
6
7
 
8
+ let axios = null;
9
+ try {
10
+ axios = require("axios");
11
+ } catch (e) {
12
+ }
13
+
7
14
  const TEMP_DIR = path.join(process.cwd(), "temp");
8
15
  const LOCK_FILE = path.join(TEMP_DIR, ".fca-update-lock.json");
9
16
  const RESTART_COOLDOWN_MS = 10 * 60 * 1000;
@@ -47,13 +54,103 @@ async function getInstalledVersionByNpm() {
47
54
  try {
48
55
  const { stdout } = await execPromise(`npm ls ${pkgName} --json --depth=0`);
49
56
  const json = JSON.parse(stdout || "{}");
50
- const v = json?.dependencies?.[pkgName]?.version;
51
- return v || null;
57
+ const v = (json && json.dependencies && json.dependencies[pkgName] && json.dependencies[pkgName].version) || null;
58
+ return v;
52
59
  } catch {
53
60
  return null;
54
61
  }
55
62
  }
56
63
 
64
+ async function getLatestVersionFromNpmRegistry() {
65
+ const url = `https://registry.npmjs.org/${pkgName}/latest`;
66
+
67
+ if (axios) {
68
+ try {
69
+ const response = await axios.get(url, {
70
+ timeout: 10000,
71
+ headers: {
72
+ 'User-Agent': 'fca-unofficial-updater',
73
+ 'Accept': 'application/json'
74
+ }
75
+ });
76
+ if (response && response.data && response.data.version) {
77
+ return response.data.version;
78
+ }
79
+ throw new Error("Invalid response from npm registry");
80
+ } catch (error) {
81
+ if (error.code === 'ECONNABORTED' || error.code === 'ETIMEDOUT') {
82
+ throw new Error("Request timeout");
83
+ }
84
+ throw error;
85
+ }
86
+ }
87
+ return new Promise((resolve, reject) => {
88
+ const timeout = setTimeout(() => {
89
+ reject(new Error("Request timeout"));
90
+ }, 10000);
91
+
92
+ https.get(url, {
93
+ headers: {
94
+ 'User-Agent': 'fca-unofficial-updater',
95
+ 'Accept': 'application/json'
96
+ }
97
+ }, (res) => {
98
+ let data = "";
99
+ res.on("data", (chunk) => { data += chunk; });
100
+ res.on("end", () => {
101
+ clearTimeout(timeout);
102
+ try {
103
+ const json = JSON.parse(data);
104
+ if (json && json.version) {
105
+ resolve(json.version);
106
+ } else {
107
+ reject(new Error("Invalid response from npm registry"));
108
+ }
109
+ } catch (e) {
110
+ reject(e);
111
+ }
112
+ });
113
+ }).on("error", (err) => {
114
+ clearTimeout(timeout);
115
+ reject(err);
116
+ });
117
+ });
118
+ }
119
+
120
+ async function getLatestVersion() {
121
+ const nodeVersion = process.version;
122
+ const nodeMajor = parseInt(nodeVersion.split('.')[0].substring(1), 10);
123
+
124
+ const shouldUseNpmCommand = nodeMajor >= 12 && nodeMajor < 20;
125
+
126
+ if (shouldUseNpmCommand) {
127
+ try {
128
+ const { stdout } = await execPromise(`npm view ${pkgName} version`);
129
+ const version = stdout.trim();
130
+ if (version && version.length > 0) {
131
+ return version;
132
+ }
133
+ } catch (npmError) {
134
+ const errorMsg = npmError.error && npmError.error.message ? npmError.error.message : String(npmError);
135
+ if (errorMsg.includes("npm") && errorMsg.includes("not to run")) {
136
+ logger("npm version incompatible, using registry API instead", "warn");
137
+ } else {
138
+ logger("npm view failed, using registry API instead", "warn");
139
+ }
140
+ }
141
+ } else {
142
+ logger("Using npm registry API (bypassing npm command)", "info");
143
+ }
144
+ try {
145
+ const version = await getLatestVersionFromNpmRegistry();
146
+ return version;
147
+ } catch (httpError) {
148
+ const errorMsg = httpError && httpError.message ? httpError.message : String(httpError);
149
+ logger(`Failed to get latest version: ${errorMsg}`, "error");
150
+ throw new Error("Cannot check for updates: npm command failed and registry API unavailable");
151
+ }
152
+ }
153
+
57
154
  async function _checkAndUpdateVersionImpl() {
58
155
  const lock = readLock();
59
156
  if (lock && Date.now() - (lock.ts || 0) < RESTART_COOLDOWN_MS) {
@@ -62,7 +159,13 @@ async function _checkAndUpdateVersionImpl() {
62
159
  }
63
160
 
64
161
  logger("Checking version...", "info");
65
- const latest = (await execPromise(`npm view ${pkgName} version`)).stdout.trim();
162
+ let latest;
163
+ try {
164
+ latest = await getLatestVersion();
165
+ } catch (error) {
166
+ logger(`Cannot check for updates: ${error.message || error}. Skipping version check.`, "warn");
167
+ return;
168
+ }
66
169
 
67
170
  let installed = getInstalledVersion();
68
171
  if (!installed) installed = await getInstalledVersionByNpm();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dongdev/fca-unofficial",
3
- "version": "3.0.4",
3
+ "version": "3.0.6",
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",
@@ -34,7 +34,28 @@ function getHeaders(url, options, ctx, customHeader) {
34
34
  "Cache-Control": "no-cache"
35
35
  };
36
36
  if (ctx?.region) headers["X-MSGR-Region"] = ctx.region;
37
- if (customHeader && typeof customHeader === "object") Object.assign(headers, customHeader);
37
+ if (customHeader && typeof customHeader === "object") {
38
+ // Filter customHeader to only include valid HTTP header values (strings, numbers, booleans)
39
+ // Exclude functions, objects, arrays, and other non-serializable values
40
+ for (const [key, value] of Object.entries(customHeader)) {
41
+ // Skip null, undefined, functions, objects, and arrays
42
+ if (value === null || value === undefined || typeof value === "function") {
43
+ continue;
44
+ }
45
+ if (typeof value === "object") {
46
+ // Arrays are objects in JavaScript, so check for arrays explicitly
47
+ if (Array.isArray(value)) {
48
+ continue;
49
+ }
50
+ // Skip plain objects (but allow null which is already handled above)
51
+ continue;
52
+ }
53
+ // Only allow strings, numbers, and booleans - convert to string
54
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
55
+ headers[key] = String(value);
56
+ }
57
+ }
58
+ }
38
59
  return headers;
39
60
  }
40
61