-
Notifications
You must be signed in to change notification settings - Fork 878
/
options.ts
146 lines (134 loc) · 4.53 KB
/
options.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { GotUrl } from "got";
import { HttpProxyAgent, HttpsProxyAgent } from "hpagent";
import http2Wrapper from "http2-wrapper";
import { cleanObject, getType, isValidUrl } from "./lib/utils.js";
import { RequestConfig, RequestOptions } from "./types/crawler.js";
export const globalOnlyOptions = [
"maxConnections",
"rateLimit",
"priorityLevels",
"skipDuplicates",
"homogeneous",
"userAgents",
"silence",
];
export const crawlerOnlyOptions = [
"rateLimiterId",
"forceUTF8",
"jQuery",
"retryInterval",
"priority",
"proxy",
"retries",
"preRequest",
"callback",
"release",
"isJson",
"referer",
"rejectUnauthorized",
"userParams",
].concat(globalOnlyOptions);
export const deprecatedOptions = [
"uri",
"qs",
"strictSSL",
"incomingEncoding",
"gzip",
"jar",
"jsonReviver",
"jsonReplacer",
"skipEventRequest",
];
export const getCharset = (headers: Record<string, unknown>): null | string => {
let charset = null;
const contentType = headers["content-type"] as string;
if (contentType) {
const match = contentType.match(/charset=['"]?([\w.-]+)/i);
if (match) {
charset = match[1].trim().toLowerCase();
}
}
return charset;
};
export const getValidOptions = (options: RequestConfig): RequestOptions => {
const type = getType(options);
if (type === "string") {
try {
if (isValidUrl(options as string)) return { url: options } as RequestOptions;
options = JSON.parse(options as string);
return options as object;
} catch (_err) {
throw new TypeError(`Invalid options: ${JSON.stringify(options)}`);
}
} else if (type === "object") {
const prototype = Object.getPrototypeOf(options);
if (prototype === Object.prototype || prototype === null) return options as object;
}
throw new TypeError(`Invalid options: ${JSON.stringify(options)}`);
};
export const alignOptions = (options: RequestOptions): GotUrl => {
const gotOptions = {
...options,
url: options.url ?? options.uri,
searchParams: options.searchParams ?? options.qs,
decompress: options.decompress ?? options.gzip,
parseJson: options.parseJson ?? options.jsonReviver,
stringifyJson: options.stringifyJson ?? options.jsonReplacer,
cookieJar: options.cookieJar ?? options.jar,
timeout: { request: options.timeout },
} as any;
const sslConfig = options.rejectUnauthorized ?? options.strictSSL;
if (sslConfig !== undefined) {
if (gotOptions.https === undefined) {
gotOptions.https = { rejectUnauthorized: sslConfig };
}
else {
gotOptions.https.rejectUnauthorized = sslConfig;
}
}
const defaultagent = options["proxy"] ? {
https: new HttpsProxyAgent({ proxy: options["proxy"] }),
http: new HttpProxyAgent({ proxy: options["proxy"] }),
} : undefined;
// http2 proxy
if (options.http2 === true && options.proxy) {
const { proxies: Http2Proxies } = http2Wrapper;
const protocol = options.proxy.startsWith("https") ? "https" : "http";
const http2Agent =
protocol === "https"
? new Http2Proxies.Http2OverHttps({
proxyOptions: { url: options.proxy },
})
: new Http2Proxies.Http2OverHttp({
proxyOptions: { url: options.proxy },
});
gotOptions.agent = { http2: http2Agent };
} else {
gotOptions.agent = gotOptions.agent ?? (options.proxy ? defaultagent : undefined);
}
/**
* @deprecated The support of incomingEncoding will be removed in the next major version.
*/
if (options.encoding === undefined) options.encoding = options.incomingEncoding;
gotOptions.responseType = "buffer";
const invalidOptions = crawlerOnlyOptions.concat(deprecatedOptions);
invalidOptions.forEach(key => {
if (key in gotOptions) {
delete gotOptions[key];
}
});
const headers = gotOptions.headers;
cleanObject(gotOptions);
gotOptions.headers = headers;
if (!gotOptions.headers.referer) {
if (options.referer) {
gotOptions.headers.referer = options.referer;
}
else {
const domain = gotOptions.url.match(/^(\w+):\/\/([^/]+)/);
if (domain) gotOptions.headers.referer = domain[0];
}
}
gotOptions.retry = { limit: 0 };
return gotOptions;
};