-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
http.ts
121 lines (113 loc) · 3.08 KB
/
http.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
import type { RequestOptions } from "https";
import https from "https";
import http from "http";
// @ts-expect-error -- no types
import tunnel from "tunnel-agent";
const TIMEOUT = 60000;
/**
* GET Method using http modules.
*/
export default function get(
url: string,
options?: RequestOptions,
): Promise<string> {
return get0(url, options, 0);
}
/** Implementation of HTTP GET method */
function get0(
url: string,
options: RequestOptions | undefined,
redirectCount: number,
): Promise<string> {
const client = url.startsWith("https") ? https : http;
const parsedOptions = parseUrlAndOptions(url, options || {});
return new Promise((resolve, reject) => {
let result = "";
const req = client.get(parsedOptions, (res) => {
res.on("data", (chunk) => {
result += chunk;
});
res.on("end", () => {
if (
res.statusCode &&
res.statusCode >= 300 &&
res.statusCode < 400 &&
redirectCount < 3 // max redirect
) {
const location = res.headers.location!;
try {
const redirectUrl = new URL(location, url).toString();
resolve(get0(redirectUrl, options, redirectCount + 1));
} catch (e) {
reject(e);
}
return;
}
resolve(result);
});
});
req.on("error", (e) => {
reject(e);
});
req.setTimeout(TIMEOUT, function handleRequestTimeout() {
if (req.destroy) {
req.destroy();
} else {
req.abort();
}
reject(new Error(`Timeout of ${TIMEOUT}ms exceeded`));
});
});
}
/** Parse URL and options */
function parseUrlAndOptions(urlStr: string, baseOptions: RequestOptions) {
const url = new URL(urlStr);
const hostname =
typeof url.hostname === "string" && url.hostname.startsWith("[")
? url.hostname.slice(1, -1)
: url.hostname;
const options: RequestOptions = {
agent: false,
...baseOptions,
protocol: url.protocol,
hostname,
path: `${url.pathname || ""}${url.search || ""}`,
};
if (url.port !== "") {
options.port = Number(url.port);
}
if (url.username || url.password) {
options.auth = `${url.username}:${url.password}`;
}
const PROXY_ENV = [
"https_proxy",
"HTTPS_PROXY",
"http_proxy",
"HTTP_PROXY",
"npm_config_https_proxy",
"npm_config_http_proxy",
];
const proxyStr: string =
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- ignore
(options as any)?.proxy ||
// eslint-disable-next-line no-process-env -- ignore
PROXY_ENV.map((k) => process.env[k]).find((v) => v);
if (proxyStr) {
const proxyUrl = new URL(proxyStr);
options.agent = tunnel[
`http${url.protocol === "https:" ? "s" : ""}OverHttp${
proxyUrl.protocol === "https:" ? "s" : ""
}`
]({
proxy: {
host: proxyUrl.hostname,
port: Number(proxyUrl.port),
proxyAuth:
proxyUrl.username || proxyUrl.password
? `${proxyUrl.username}:${proxyUrl.password}`
: undefined,
},
});
}
return options;
}