-
Notifications
You must be signed in to change notification settings - Fork 0
/
steam_community_fetch.ts
115 lines (103 loc) · 2.82 KB
/
steam_community_fetch.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import {
CookieJar,
wrapFetchWithCookieJar,
wrapFetchWithHeaders,
} from "../deps.ts";
import { DEFAULT_USERAGENT } from "./fetch_utils.ts";
import type { SteamCommunity } from "./steam_community.ts";
import { throwIfHasError } from "./steam_error.ts";
export function getFetchAndCookieJar(community: SteamCommunity) {
const cookieJar = new CookieJar();
const wrappedWithCookiesFetch = wrapFetchWithCookieJar({
cookieJar,
});
const wrappedWithHeaders = wrapFetchWithHeaders({
fetch: wrappedWithCookiesFetch,
userAgent: DEFAULT_USERAGENT,
validator: async function communityResponseValidator(response: Response) {
const responseText = await response.clone().text(); // allow for subsequent reads
checkHTTPError(community, response, responseText);
checkCommunityError(community, response, responseText);
checkTradeError(responseText);
},
});
return {
fetch: wrappedWithHeaders,
cookieJar,
};
}
function checkHTTPError(
community: SteamCommunity,
response: Response,
responseText: string | undefined,
) {
let err;
if (
response.status >= 300 && response.status <= 399 &&
(response.headers.get("location") || "").indexOf("/login") !== -1
) {
err = new Error("Not Logged In");
community._notifySessionExpired(err);
throw err;
}
if (
response.status === 403 && responseText &&
responseText.match(
/<div id="parental_notice_instructions">Enter your PIN below to exit Family View.<\/div>/,
)
) {
err = new Error("Family View Restricted");
community._notifyFamilyViewRestricted(err);
throw err;
}
}
function checkCommunityError(
community: SteamCommunity,
response: Response,
responseText: string | undefined,
) {
if (
typeof responseText === "string" && /<h1>Sorry!<\/h1>/.test(responseText)
) {
const match = responseText.match(/<h3>(.+)<\/h3>/);
throw new Error(match ? match[1] : "Unknown error occurred");
}
if (
typeof responseText === "string" &&
responseText.match(/g_steamID = false;/) &&
responseText.match(/<h1>Sign In<\/h1>/)
) {
const err = new Error("Not Logged In");
community._notifySessionExpired(err);
throw err;
}
if (response.status === 401) {
const err = new Error("Not Logged In");
community._notifySessionExpired(err);
throw err;
}
let jsonParsed;
try {
jsonParsed = JSON.parse(responseText!);
} catch {
// silent
}
if (jsonParsed) {
throwIfHasError(jsonParsed);
}
if (response.status > 403) {
throw new Error("HTTP error " + response.status);
}
}
function checkTradeError(
responseText: string | undefined,
) {
if (typeof responseText === "string") {
const match = responseText.match(
/<div id="error_msg">\s*([^<]+)\s*<\/div>/,
);
if (match) {
throw new Error(match[1].trim());
}
}
}