node-ainzfb-new 1.6.2431-test → 1.6.2511-test

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 (2) hide show
  1. package/package.json +3 -2
  2. package/utils.js +111 -67
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-ainzfb-new",
3
- "version": "1.6.2431-test",
3
+ "version": "1.6.2511-test",
4
4
  "description": "A Facebook chat API that doesn't rely on XMPP. Will NOT be deprecated after April 30th 2015.",
5
5
  "scripts": {
6
6
  "test": "mocha",
@@ -35,7 +35,8 @@
35
35
  "https-proxy-agent": "latest",
36
36
  "is-hexcolor": "^1.0.0",
37
37
  "lodash": "",
38
- "mqtt": "^4.3.7",
38
+ "mqtt": "^3.0.0",
39
+ "node-superfetch": "^0.2.3",
39
40
  "npmlog": "^1.2.0",
40
41
  "path": "latest",
41
42
  "pretty-ms": "latest",
package/utils.js CHANGED
@@ -1019,75 +1019,119 @@ function makeDefaults(html, userID, ctx) {
1019
1019
  }
1020
1020
 
1021
1021
  function parseAndCheckLogin(ctx, defaultFuncs, retryCount) {
1022
- if (retryCount == undefined) retryCount = 0;
1023
- return function(data) {
1024
- return bluebird.try(function() {
1025
- log.verbose("parseAndCheckLogin", data.body);
1026
- if (data.statusCode >= 500 && data.statusCode < 600) {
1027
- if (retryCount >= 5) {
1028
- throw {
1029
- error: "Request retry failed. Check the `res` and `statusCode` property on this error.",
1030
- statusCode: data.statusCode,
1031
- res: data.body
1032
- };
1033
- }
1034
- retryCount++;
1035
- var retryTime = Math.floor(Math.random() * 5000);
1036
- log.warn("parseAndCheckLogin", "Got status code " + data.statusCode + " - " + retryCount + ". attempt to retry in " + retryTime + " milliseconds...");
1037
- var url = data.request.uri.protocol + "//" + data.request.uri.hostname + data.request.uri.pathname;
1038
- if (data.request.headers["Content-Type"].split(";")[0] === "multipart/form-data") {
1039
- return bluebird.delay(retryTime).then(() => defaultFuncs.postFormData(url, ctx.jar, data.request.formData, {}))
1040
- .then(parseAndCheckLogin(ctx, defaultFuncs, retryCount));
1041
- } else {
1042
- return bluebird.delay(retryTime).then(() => defaultFuncs.post(url, ctx.jar, data.request.formData))
1043
- .then(parseAndCheckLogin(ctx, defaultFuncs, retryCount));
1044
- }
1045
- }
1046
- if (data.statusCode !== 200) throw new Error("parseAndCheckLogin got status code: " + data.statusCode + ". Bailing out of trying to parse response.");
1047
-
1048
- var res = null;
1049
- try {
1050
- res = JSON.parse(makeParsable(data.body));
1051
- } catch (e) {
1052
- throw {
1053
- error: "JSON.parse error. Check the `detail` property on this error.",
1054
- detail: e,
1055
- res: data.body
1056
- };
1057
- }
1058
-
1059
- // In some cases the response contains only a redirect URL which should be followed
1060
- if (res.redirect && data.request.method === "GET") return defaultFuncs.get(res.redirect, ctx.jar).then(parseAndCheckLogin(ctx, defaultFuncs));
1061
-
1062
- // TODO: handle multiple cookies?
1063
- if (res.jsmods && res.jsmods.require && Array.isArray(res.jsmods.require[0]) && res.jsmods.require[0][0] === "Cookie") {
1064
- res.jsmods.require[0][3][0] = res.jsmods.require[0][3][0].replace("_js_", "");
1065
- var cookie = formatCookie(res.jsmods.require[0][3], "facebook");
1066
- var cookie2 = formatCookie(res.jsmods.require[0][3], "messenger");
1067
- ctx.jar.setCookie(cookie, "https://www.facebook.com");
1068
- ctx.jar.setCookie(cookie2, "https://www.messenger.com");
1069
- }
1070
-
1071
- // On every request we check if we got a DTSG and we mutate the context so that we use the latest
1072
- // one for the next requests.
1073
- if (res.jsmods && Array.isArray(res.jsmods.require)) {
1074
- var arr = res.jsmods.require;
1075
- for (var i in arr) {
1076
- if (arr[i][0] === "DTSG" && arr[i][1] === "setToken") {
1077
- ctx.fb_dtsg = arr[i][3][0];
1078
-
1079
- // Update ttstamp since that depends on fb_dtsg
1080
- ctx.ttstamp = "2";
1081
- for (var j = 0; j < ctx.fb_dtsg.length; j++) ctx.ttstamp += ctx.fb_dtsg.charCodeAt(j);
1082
- }
1083
- }
1022
+ if (retryCount == undefined) {
1023
+ retryCount = 0;
1024
+ }
1025
+ return function (data) {
1026
+ return bluebird.try(function () {
1027
+
1028
+ if (data.statusCode >= 500 && data.statusCode < 600) {
1029
+ if (retryCount >= 5) {
1030
+ throw {
1031
+ error:
1032
+ "Request retry failed. Check the `res` and `statusCode` property on this error.",
1033
+ statusCode: data.statusCode,
1034
+ res: data.body
1035
+ };
1036
+ }
1037
+ retryCount++;
1038
+ var retryTime = Math.floor(Math.random() * 5000);
1039
+ var url =
1040
+ data.request.uri.protocol +
1041
+ "//" +
1042
+ data.request.uri.hostname +
1043
+ data.request.uri.pathname;
1044
+ if (
1045
+ data.request.headers["Content-Type"].split(";")[0] ===
1046
+ "multipart/form-data"
1047
+ ) {
1048
+ return bluebird
1049
+ .delay(retryTime)
1050
+ .then(function () {
1051
+ return defaultFuncs.postFormData(
1052
+ url,
1053
+ ctx.jar,
1054
+ data.request.formData,
1055
+ {}
1056
+ );
1057
+ })
1058
+ .then(parseAndCheckLogin(ctx, defaultFuncs, retryCount));
1059
+ } else {
1060
+ return bluebird
1061
+ .delay(retryTime)
1062
+ .then(function () {
1063
+ return defaultFuncs.post(url, ctx.jar, data.request.formData);
1064
+ })
1065
+ .then(parseAndCheckLogin(ctx, defaultFuncs, retryCount));
1066
+ }
1067
+ }
1068
+ if (data.statusCode !== 200)
1069
+ throw new Error(
1070
+ "parseAndCheckLogin got status code: " +
1071
+ data.statusCode +
1072
+ ". Bailing out of trying to parse response."
1073
+ );
1074
+
1075
+ var res = null;
1076
+ try {
1077
+ res = JSON.parse(makeParsable(data.body));
1078
+ } catch (e) {
1079
+ throw {
1080
+ error: "JSON.parse error. Check the `detail` property on this error.",
1081
+ detail: e,
1082
+ res: data.body
1083
+ };
1084
+ }
1085
+
1086
+ // In some cases the response contains only a redirect URL which should be followed
1087
+ if (res.redirect && data.request.method === "GET") {
1088
+ return defaultFuncs
1089
+ .get(res.redirect, ctx.jar)
1090
+ .then(parseAndCheckLogin(ctx, defaultFuncs));
1091
+ }
1092
+
1093
+ // TODO: handle multiple cookies?
1094
+ if (
1095
+ res.jsmods &&
1096
+ res.jsmods.require &&
1097
+ Array.isArray(res.jsmods.require[0]) &&
1098
+ res.jsmods.require[0][0] === "Cookie"
1099
+ ) {
1100
+ res.jsmods.require[0][3][0] = res.jsmods.require[0][3][0].replace(
1101
+ "_js_",
1102
+ ""
1103
+ );
1104
+ var cookie = formatCookie(res.jsmods.require[0][3], "facebook");
1105
+ var cookie2 = formatCookie(res.jsmods.require[0][3], "messenger");
1106
+ ctx.jar.setCookie(cookie, "https://www.facebook.com");
1107
+ ctx.jar.setCookie(cookie2, "https://www.messenger.com");
1108
+ }
1109
+
1110
+ // On every request we check if we got a DTSG and we mutate the context so that we use the latest
1111
+ // one for the next requests.
1112
+ if (res.jsmods && Array.isArray(res.jsmods.require)) {
1113
+ var arr = res.jsmods.require;
1114
+ for (var i in arr) {
1115
+ if (arr[i][0] === "DTSG" && arr[i][1] === "setToken") {
1116
+ ctx.fb_dtsg = arr[i][3][0];
1117
+
1118
+ // Update ttstamp since that depends on fb_dtsg
1119
+ ctx.ttstamp = "2";
1120
+ for (var j = 0; j < ctx.fb_dtsg.length; j++) {
1121
+ ctx.ttstamp += ctx.fb_dtsg.charCodeAt(j);
1122
+ }
1084
1123
  }
1085
-
1086
- if (res.error === 1357001) throw { error: "Chưa Đăng Nhập Được - Appstate Đã Bị Lỗi" };
1087
- return res;
1088
- });
1124
+ }
1125
+ }
1126
+
1127
+ if (res.error === 1357001) {
1128
+ throw { error: "Not logged in." };
1129
+ }
1130
+ return res;
1131
+ });
1089
1132
  };
1090
- }
1133
+ }
1134
+
1091
1135
 
1092
1136
  function saveCookies(jar) {
1093
1137
  return function(res) {