fca-nx 6.7.1 → 6.7.2

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/index.d.ts ADDED
@@ -0,0 +1,120 @@
1
+ import { EventEmitter } from "events";
2
+
3
+ export type Callback<T = unknown> = (error: Error | null, value?: T) => void;
4
+
5
+ export interface LoginData {
6
+ appState?: unknown;
7
+ Cookie?: string;
8
+ email?: string;
9
+ password?: string;
10
+ [key: string]: unknown;
11
+ }
12
+
13
+ export interface LoginOptions {
14
+ [key: string]: unknown;
15
+ }
16
+
17
+ export interface FcaApi {
18
+ [method: string]: any;
19
+ getAppState?: () => unknown;
20
+ listenMqtt?: (...args: any[]) => any;
21
+ sendMessage?: (...args: any[]) => any;
22
+ }
23
+
24
+ export interface Namespace {
25
+ [method: string]: any;
26
+ }
27
+
28
+ export interface FcaClient {
29
+ raw: FcaApi;
30
+ messages: Namespace;
31
+ threads: Namespace;
32
+ users: Namespace;
33
+ account: Namespace;
34
+ realtime: Namespace;
35
+ http: Namespace;
36
+ scheduler: Namespace;
37
+ }
38
+
39
+ export interface MessengerBotOptions {
40
+ commandPrefix?: string;
41
+ enableComposer?: boolean;
42
+ stopOnSignals?: boolean;
43
+ maxEventListeners?: number;
44
+ }
45
+
46
+ export interface LaunchOptions {
47
+ stopOnSignals?: boolean;
48
+ }
49
+
50
+ export type Middleware = (context: MessengerContext, next: () => Promise<void>) => unknown;
51
+ export type ErrorHandler = (error: unknown, context: MessengerContext) => unknown;
52
+
53
+ export class MessengerContext {
54
+ constructor(bot: MessengerBot, event: any);
55
+ bot: MessengerBot;
56
+ event: any;
57
+ readonly threadID: string | undefined;
58
+ readonly senderID: string | undefined;
59
+ readonly messageID: string | undefined;
60
+ readonly body: string | undefined;
61
+ readonly message: any;
62
+ readonly text: string;
63
+ reply(payload: any, callback?: Callback): any;
64
+ replyAsync(payload: any): Promise<any>;
65
+ }
66
+
67
+ export class MessengerBot extends EventEmitter {
68
+ constructor(api: FcaApi, options?: MessengerBotOptions);
69
+ static connect(credentials: LoginData, options?: LoginOptions & MessengerBotOptions): Promise<MessengerBot>;
70
+ readonly api: FcaApi;
71
+ readonly client: FcaClient;
72
+ commandPrefix: string;
73
+ use(middleware: Middleware): this;
74
+ command(name: string, handler: (context: MessengerContext) => unknown): this;
75
+ hears(trigger: string | RegExp, handler: (context: MessengerContext) => unknown): this;
76
+ catch(handler: ErrorHandler): this;
77
+ startListening(): this;
78
+ launch(options?: LaunchOptions): Promise<this>;
79
+ stop(): Promise<void>;
80
+ }
81
+
82
+ export interface ThreadInfoSyncOptions {
83
+ refreshParticipants?: boolean;
84
+ invalidateOnUnknownEvent?: boolean;
85
+ logger?: (...args: any[]) => void;
86
+ }
87
+
88
+ export function login(
89
+ loginData: LoginData,
90
+ options?: LoginOptions,
91
+ callback?: Callback<FcaApi>
92
+ ): Promise<FcaApi> | void;
93
+
94
+ export function createFcaClient(api: FcaApi): FcaClient;
95
+ export function createMessengerBot(
96
+ credentials: LoginData,
97
+ options?: LoginOptions & MessengerBotOptions
98
+ ): Promise<MessengerBot>;
99
+ export function attachThreadInfoRealtimeSync(
100
+ api: FcaApi,
101
+ options?: ThreadInfoSyncOptions
102
+ ): () => void;
103
+ export function applyThreadInfoRealtimeEvent(
104
+ api: FcaApi,
105
+ event: any,
106
+ options?: ThreadInfoSyncOptions
107
+ ): Promise<unknown>;
108
+
109
+ declare const exportedLogin: typeof login & {
110
+ default: typeof login;
111
+ login: typeof login;
112
+ createFcaClient: typeof createFcaClient;
113
+ MessengerBot: typeof MessengerBot;
114
+ createMessengerBot: typeof createMessengerBot;
115
+ MessengerContext: typeof MessengerContext;
116
+ attachThreadInfoRealtimeSync: typeof attachThreadInfoRealtimeSync;
117
+ applyThreadInfoRealtimeEvent: typeof applyThreadInfoRealtimeEvent;
118
+ };
119
+
120
+ export = exportedLogin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fca-nx",
3
- "version": "6.7.1",
3
+ "version": "6.7.2",
4
4
  "description": "Facebook Messenger API for Node.js — Fast, Secure, and Feature-Rich",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -17,9 +17,7 @@
17
17
  "module/",
18
18
  "func/",
19
19
  "src/",
20
- "DOCS.md",
21
- "README.md",
22
- "src/app/"
20
+ "README.md"
23
21
  ],
24
22
  "scripts": {
25
23
  "test": "mocha",
@@ -84,19 +82,25 @@
84
82
  "mime": "^3.0.0",
85
83
  "mqtt": "^5.10.1",
86
84
  "protobufjs": "^8.4.0",
87
- "request": "^2.88.2",
85
+ "form-data": "^4.0.4",
88
86
  "sequelize": "^6.37.8",
89
- "sqlite3": "^5.1.7",
87
+ "sqlite3": "^6.0.1",
90
88
  "totp-generator": "^1.0.0",
91
89
  "tough-cookie": "^4.1.4",
92
90
  "ws": "^8.18.0"
93
91
  },
94
92
  "devDependencies": {
95
- "eslint": "^8.50.0",
96
- "mocha": "^10.2.0"
93
+ "eslint": "^8.57.1",
94
+ "mocha": "^10.8.2"
97
95
  },
98
96
  "publishConfig": {
99
97
  "access": "public",
100
98
  "registry": "https://registry.npmjs.org/"
99
+ },
100
+ "pnpm": {
101
+ "onlyBuiltDependencies": [
102
+ "@signalapp/libsignal-client",
103
+ "sqlite3"
104
+ ]
101
105
  }
102
106
  }
@@ -1,6 +1,5 @@
1
1
  "use strict";
2
2
 
3
- const log = require("../../../func/logAdapter");
4
3
  const { getFrom } = require("../../utils/constants");
5
4
  const { get } = require("../../utils/request")
6
5
  const { getType } = require("../../utils/format");
@@ -12,7 +11,7 @@ module.exports = function (defaultFuncs, api, ctx) {
12
11
  }
13
12
  if (!obj) obj = {};
14
13
  if (getType(obj) !== "Object") {
15
- throw new CustomError("The first parameter must be an object or a callback function");
14
+ throw new TypeError("The first parameter must be an object or a callback function");
16
15
  }
17
16
  let resolveFunc, rejectFunc;
18
17
  const returnPromise = new Promise((resolve, reject) => {
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
 
3
3
  const { generateOfflineThreadingID } = require("../../utils/format");
4
+ const { parseAndCheckLogin } = require("../../utils/utils");
4
5
  const log = require("../../../func/logAdapter");
5
6
 
6
7
  module.exports = function (defaultFuncs, api, ctx) {
@@ -18,9 +18,15 @@
18
18
 
19
19
  const path = require("path");
20
20
  const logger = require("../../../utils/logger");
21
+ const requestHttp = require("../../../utils/request");
21
22
 
22
23
  function loadFBClient() {
23
24
  try {
25
+ // [Fixed by xalman] see comment above loadFBClient - Node < 22 has no
26
+ // global WebSocket, but fb-e2ee.cjs unconditionally does `new WebSocket(...)`.
27
+ if (typeof global.WebSocket === "undefined") {
28
+ global.WebSocket = require("ws");
29
+ }
24
30
  const vendorPath = path.join(__dirname, "vendor", "fb-e2ee.cjs");
25
31
  return require(vendorPath).FBClient;
26
32
  } catch (err) {
@@ -114,10 +120,7 @@ class E2EEBridge {
114
120
  if (!merged.__user) merged.__user = _ctx.userID;
115
121
  const res = _defaultFuncs
116
122
  ? await _defaultFuncs.post(url, _ctx.jar, merged)
117
- : await new Promise(function (resolve, reject) {
118
- require("request")({ method: "POST", url: url, jar: _ctx.jar, form: merged, gzip: true },
119
- function (err, r) { if (err) return reject(err); resolve(r); });
120
- });
123
+ : await requestHttp.post(url, _ctx.jar, merged, {}, _ctx);
121
124
  const body = res && (res.data !== undefined ? res.data : res.body);
122
125
  if (body == null) return body;
123
126
  if (typeof body === "string") return body;
@@ -2,6 +2,7 @@
2
2
 
3
3
  const log = require("../../../func/logAdapter");
4
4
  const { formatID } = require("../../utils/format");
5
+ const { parseAndCheckLogin } = require("../../utils/utils");
5
6
  function formatData(data) {
6
7
  return {
7
8
  userID: formatID(data.uid.toString()),
@@ -2,16 +2,12 @@
2
2
 
3
3
  var url = require("url");
4
4
  var stream = require("stream");
5
- var bluebird = require("bluebird");
6
5
  var querystring = require("querystring");
7
- var request = bluebird.promisify(require("request").defaults({ jar: true }));
6
+ var CookieJar = require("tough-cookie").CookieJar;
7
+ var requestHttp = require("./request");
8
8
 
9
9
  function setProxy(proxyUrl) {
10
- if (typeof proxyUrl === "undefined") {
11
- request = bluebird.promisify(require("request").defaults({ jar: true }));
12
- } else {
13
- request = bluebird.promisify(require("request").defaults({ jar: true, proxy: proxyUrl }));
14
- }
10
+ requestHttp.setProxy(proxyUrl);
15
11
  }
16
12
 
17
13
  function sanitizeHeaderValue(value) {
@@ -103,53 +99,31 @@ function get(reqUrl, jar, qs, options, ctx) {
103
99
  if (qs.hasOwnProperty(prop) && getType(qs[prop]) === "Object") qs[prop] = JSON.stringify(qs[prop]);
104
100
  }
105
101
  }
106
- var op = {
107
- headers: getHeaders(reqUrl, options, ctx),
108
- timeout: 60000,
109
- qs: qs,
110
- url: reqUrl,
111
- method: "GET",
112
- jar: jar,
113
- gzip: true
114
- };
115
- return request(op);
102
+ return requestHttp.get(reqUrl, jar, qs, options, ctx);
116
103
  }
117
104
 
118
105
  function post(reqUrl, jar, form, options, ctx, customHeader) {
119
- var op = {
120
- headers: getHeaders(reqUrl, options, ctx, customHeader),
121
- timeout: 60000,
122
- url: reqUrl,
123
- method: "POST",
124
- form: form,
125
- jar: jar,
126
- gzip: true
127
- };
128
- return request(op);
106
+ return requestHttp.post(reqUrl, jar, form, options, ctx, customHeader);
129
107
  }
130
108
 
131
109
  function postFormData(reqUrl, jar, form, qs, options, ctx) {
132
- var headers = getHeaders(reqUrl, options, ctx);
133
- headers["Content-Type"] = "multipart/form-data";
134
- var op = {
135
- headers: headers,
136
- timeout: 60000,
137
- url: reqUrl,
138
- method: "POST",
139
- formData: form,
140
- qs: qs,
141
- jar: jar,
142
- gzip: true
143
- };
144
- return request(op);
110
+ return requestHttp.postFormData(reqUrl, jar, form, qs, options, ctx);
145
111
  }
146
112
 
147
113
  function getJar() {
148
- return require("request").jar();
114
+ return new CookieJar();
149
115
  }
150
116
 
151
117
  function getAppState(jar) {
152
- return jar.getCookies("https://www.facebook.com").map(c => ({
118
+ if (!jar || typeof jar.getCookies !== "function") return [];
119
+ var cookies;
120
+ if (typeof jar.getCookiesSync === "function") {
121
+ cookies = jar.getCookiesSync("https://www.facebook.com");
122
+ } else {
123
+ cookies = jar.getCookies("https://www.facebook.com");
124
+ }
125
+ if (!Array.isArray(cookies)) return [];
126
+ return cookies.map(c => ({
153
127
  key: c.key,
154
128
  value: c.value,
155
129
  domain: c.domain,
@@ -166,7 +140,13 @@ function saveCookies(jar) {
166
140
  if (cookies) {
167
141
  if (!Array.isArray(cookies)) cookies = [cookies];
168
142
  cookies.forEach(c => {
169
- try { jar.setCookie(c, "https://www.facebook.com"); } catch (_) { }
143
+ try {
144
+ if (typeof jar.setCookieSync === "function") {
145
+ jar.setCookieSync(c, "https://www.facebook.com");
146
+ } else if (typeof jar.setCookie === "function") {
147
+ jar.setCookie(c, "https://www.facebook.com");
148
+ }
149
+ } catch (_) { }
170
150
  });
171
151
  }
172
152
  return res;