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.
- package/package.json +3 -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.
|
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": "^
|
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)
|
1023
|
-
|
1024
|
-
|
1025
|
-
|
1026
|
-
|
1027
|
-
|
1028
|
-
|
1029
|
-
|
1030
|
-
|
1031
|
-
|
1032
|
-
|
1033
|
-
|
1034
|
-
|
1035
|
-
|
1036
|
-
|
1037
|
-
|
1038
|
-
|
1039
|
-
|
1040
|
-
|
1041
|
-
|
1042
|
-
|
1043
|
-
|
1044
|
-
|
1045
|
-
|
1046
|
-
|
1047
|
-
|
1048
|
-
|
1049
|
-
|
1050
|
-
|
1051
|
-
|
1052
|
-
|
1053
|
-
|
1054
|
-
|
1055
|
-
|
1056
|
-
|
1057
|
-
|
1058
|
-
|
1059
|
-
|
1060
|
-
|
1061
|
-
|
1062
|
-
|
1063
|
-
|
1064
|
-
|
1065
|
-
|
1066
|
-
|
1067
|
-
|
1068
|
-
|
1069
|
-
|
1070
|
-
|
1071
|
-
|
1072
|
-
|
1073
|
-
|
1074
|
-
|
1075
|
-
|
1076
|
-
|
1077
|
-
|
1078
|
-
|
1079
|
-
|
1080
|
-
|
1081
|
-
|
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
|
-
|
1087
|
-
|
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) {
|