@dongdev/fca-unofficial 0.0.5 → 0.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/utils.js CHANGED
@@ -1210,68 +1210,99 @@ function makeDefaults(html, userID, ctx) {
1210
1210
  };
1211
1211
  }
1212
1212
 
1213
- function parseAndCheckLogin(ctx, defaultFuncs, retryCount) {
1214
- if (retryCount == undefined) retryCount = 0;
1213
+ function parseAndCheckLogin(ctx, defaultFuncs, retryCount = 0, sourceCall) {
1214
+ if (sourceCall === undefined) {
1215
+ try {
1216
+ throw new Error();
1217
+ } catch (e) {
1218
+ sourceCall = e;
1219
+ }
1220
+ }
1215
1221
  return function (data) {
1216
- return bluebird.try(function () {
1222
+ return tryPromise(function () {
1217
1223
  log.verbose("parseAndCheckLogin", data.body);
1218
1224
  if (data.statusCode >= 500 && data.statusCode < 600) {
1219
1225
  if (retryCount >= 5) {
1220
1226
  throw {
1221
- error: "Request retry failed. Check the `res` and `statusCode` property on this error.",
1227
+ message: "Request retry failed. Check `res` and `statusCode`.",
1222
1228
  statusCode: data.statusCode,
1223
- res: data.body
1229
+ res: data.body,
1230
+ error: "Request retry failed.",
1231
+ sourceCall
1224
1232
  };
1225
1233
  }
1226
1234
  retryCount++;
1227
- var retryTime = Math.floor(Math.random() * 5000);
1228
- log.warn("parseAndCheckLogin", "Got status code " + data.statusCode + " - " + retryCount + ". attempt to retry in " + retryTime + " milliseconds...");
1229
- var url = data.request.uri.protocol + "//" + data.request.uri.hostname + data.request.uri.pathname;
1230
- if (data.request.headers["Content-Type"].split(";")[0] === "multipart/form-data") return bluebird.delay(retryTime).then(() => defaultFuncs.postFormData(url, ctx.jar, data.request.formData, {})).then(parseAndCheckLogin(ctx, defaultFuncs, retryCount));
1231
- else return bluebird.delay(retryTime).then(() => defaultFuncs.post(url, ctx.jar, data.request.formData)).then(parseAndCheckLogin(ctx, defaultFuncs, retryCount));
1235
+ const retryTime = Math.floor(Math.random() * 5000);
1236
+ log.warn(
1237
+ "parseAndCheckLogin",
1238
+ `Got status code ${data.statusCode} - Retrying in ${retryTime}ms...`
1239
+ );
1240
+ if (!data.request) throw new Error("Invalid request object");
1241
+ const url = `${data.request.uri.protocol}//${data.request.uri.hostname}${data.request.uri.pathname}`;
1242
+ const contentType = data.request.headers?.["content-type"]?.split(";")[0];
1243
+ return delay(retryTime)
1244
+ .then(() =>
1245
+ contentType === "multipart/form-data"
1246
+ ? defaultFuncs.postFormData(url, ctx.jar, data.request.formData, {})
1247
+ : defaultFuncs.post(url, ctx.jar, data.request.formData)
1248
+ )
1249
+ .then(parseAndCheckLogin(ctx, defaultFuncs, retryCount, sourceCall));
1232
1250
  }
1233
- if (data.statusCode !== 200) throw new Error("parseAndCheckLogin got status code: " + data.statusCode + ". Bailing out of trying to parse response.");
1234
-
1235
- var res = null;
1251
+ if (data.statusCode !== 200) {
1252
+ throw {
1253
+ message: `parseAndCheckLogin got status code: ${data.statusCode}.`,
1254
+ statusCode: data.statusCode,
1255
+ res: data.body,
1256
+ error: `parseAndCheckLogin got status code: ${data.statusCode}.`,
1257
+ sourceCall
1258
+ };
1259
+ }
1260
+ let res;
1236
1261
  try {
1237
1262
  res = JSON.parse(makeParsable(data.body));
1238
- }
1239
- catch (e) {
1263
+ } catch (e) {
1264
+ log.error("JSON parsing failed:", data.body);
1240
1265
  throw {
1241
- error: "JSON.parse error. Check the `detail` property on this error.",
1242
- detail: e,
1243
- res: data.body
1266
+ message: "Failed to parse JSON response.",
1267
+ detail: e.message,
1268
+ res: data.body,
1269
+ error: "JSON.parse error.",
1270
+ sourceCall
1244
1271
  };
1245
1272
  }
1246
-
1247
- // In some cases the response contains only a redirect URL which should be followed
1248
- if (res.redirect && data.request.method === "GET") return defaultFuncs.get(res.redirect, ctx.jar).then(parseAndCheckLogin(ctx, defaultFuncs));
1249
-
1250
- // TODO: handle multiple cookies?
1251
- if (res.jsmods && res.jsmods.require && Array.isArray(res.jsmods.require[0]) && res.jsmods.require[0][0] === "Cookie") {
1273
+ if (res.redirect && data.request.method === "GET") {
1274
+ return defaultFuncs
1275
+ .get(res.redirect, ctx.jar)
1276
+ .then(parseAndCheckLogin(ctx, defaultFuncs, undefined, sourceCall));
1277
+ }
1278
+ if (
1279
+ res.jsmods?.require &&
1280
+ Array.isArray(res.jsmods.require[0]) &&
1281
+ res.jsmods.require[0][0] === "Cookie"
1282
+ ) {
1252
1283
  res.jsmods.require[0][3][0] = res.jsmods.require[0][3][0].replace("_js_", "");
1253
- var cookie = formatCookie(res.jsmods.require[0][3], "facebook");
1254
- var cookie2 = formatCookie(res.jsmods.require[0][3], "messenger");
1284
+ const cookie = formatCookie(res.jsmods.require[0][3], "facebook");
1285
+ const cookie2 = formatCookie(res.jsmods.require[0][3], "messenger");
1255
1286
  ctx.jar.setCookie(cookie, "https://www.facebook.com");
1256
1287
  ctx.jar.setCookie(cookie2, "https://www.messenger.com");
1257
1288
  }
1258
-
1259
- // On every request we check if we got a DTSG and we mutate the context so that we use the latest
1260
- // one for the next requests.
1261
- if (res.jsmods && Array.isArray(res.jsmods.require)) {
1262
- var arr = res.jsmods.require;
1263
- for (var i in arr) {
1264
- if (arr[i][0] === "DTSG" && arr[i][1] === "setToken") {
1265
- ctx.fb_dtsg = arr[i][3][0];
1266
-
1267
- // Update ttstamp since that depends on fb_dtsg
1268
- ctx.ttstamp = "2";
1269
- for (var j = 0; j < ctx.fb_dtsg.length; j++) ctx.ttstamp += ctx.fb_dtsg.charCodeAt(j);
1289
+ if (res.jsmods?.require) {
1290
+ for (const arr of res.jsmods.require) {
1291
+ if (arr[0] === "DTSG" && arr[1] === "setToken") {
1292
+ ctx.fb_dtsg = arr[3][0];
1293
+ ctx.ttstamp = "2" + ctx.fb_dtsg.split("").map(c => c.charCodeAt(0)).join("");
1270
1294
  }
1271
1295
  }
1272
1296
  }
1273
-
1274
- if (res.error === 1357001) throw { error: "Not logged in." };
1297
+ if (res.error === 1357001) {
1298
+ throw {
1299
+ message: "Facebook blocked login. Please check your account.",
1300
+ error: "Not logged in.",
1301
+ res,
1302
+ statusCode: data.statusCode,
1303
+ sourceCall
1304
+ };
1305
+ }
1275
1306
  return res;
1276
1307
  });
1277
1308
  };