2022-09-07 16:53:24 +03:00
|
|
|
/* eslint-disable prefer-promise-reject-errors */
|
2022-09-14 10:46:52 -07:00
|
|
|
/* eslint-disable no-param-reassign */
|
|
|
|
import { http, https } from "follow-redirects";
|
|
|
|
import { Cookie, CookieJar } from 'tough-cookie';
|
|
|
|
|
|
|
|
const cookieJar = new CookieJar();
|
|
|
|
|
2022-09-14 11:08:36 -07:00
|
|
|
function setCookieHeader(url, params) {
|
2022-09-14 10:46:52 -07:00
|
|
|
// add cookie header, if we have one in the jar
|
|
|
|
const existingCookie = cookieJar.getCookieStringSync(url.toString());
|
|
|
|
if (existingCookie) {
|
|
|
|
params.headers = params.headers ?? {};
|
|
|
|
params.headers.Cookie = existingCookie;
|
|
|
|
}
|
2022-09-14 11:08:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function addCookieHandler(url, params) {
|
|
|
|
setCookieHeader(url, params);
|
2022-09-14 10:46:52 -07:00
|
|
|
|
|
|
|
// handle cookies during redirects
|
|
|
|
params.beforeRedirect = (options, responseInfo) => {
|
|
|
|
const cookieHeader = responseInfo.headers['set-cookie'];
|
|
|
|
if (!cookieHeader || cookieHeader.length === 0) return;
|
|
|
|
|
|
|
|
let cookies = null;
|
|
|
|
if (cookieHeader instanceof Array) {
|
|
|
|
cookies = cookieHeader.map(Cookie.parse);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cookies = [Cookie.parse(cookieHeader)];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < cookies.length; i += 1) {
|
|
|
|
cookieJar.setCookieSync(cookies[i], options.href);
|
|
|
|
}
|
|
|
|
|
2022-09-14 11:08:36 -07:00
|
|
|
setCookieHeader(options.href, options);
|
2022-09-14 10:46:52 -07:00
|
|
|
};
|
|
|
|
}
|
2022-08-25 20:57:25 +03:00
|
|
|
|
2022-08-26 22:33:54 +03:00
|
|
|
export function httpsRequest(url, params) {
|
2022-09-07 16:53:24 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-09-14 10:46:52 -07:00
|
|
|
addCookieHandler(url, params);
|
2022-09-07 16:53:24 +03:00
|
|
|
const request = https.request(url, params, (response) => {
|
|
|
|
const data = [];
|
2022-08-25 20:57:25 +03:00
|
|
|
|
|
|
|
response.on("data", (chunk) => {
|
2022-08-27 12:38:32 +03:00
|
|
|
data.push(chunk);
|
2022-08-25 20:57:25 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
response.on("end", () => {
|
2022-09-12 19:35:47 -07:00
|
|
|
resolve([response.statusCode, response.headers["content-type"], Buffer.concat(data), response.headers]);
|
2022-08-25 20:57:25 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
request.on("error", (error) => {
|
|
|
|
reject([500, error]);
|
|
|
|
});
|
|
|
|
|
2022-09-12 19:35:47 -07:00
|
|
|
if (params.body) {
|
|
|
|
request.write(params.body);
|
|
|
|
}
|
|
|
|
|
2022-08-25 20:57:25 +03:00
|
|
|
request.end();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-26 22:33:54 +03:00
|
|
|
export function httpRequest(url, params) {
|
2022-09-07 16:53:24 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-09-14 10:46:52 -07:00
|
|
|
addCookieHandler(url, params);
|
2022-09-07 16:53:24 +03:00
|
|
|
const request = http.request(url, params, (response) => {
|
|
|
|
const data = [];
|
2022-08-25 20:57:25 +03:00
|
|
|
|
|
|
|
response.on("data", (chunk) => {
|
2022-08-27 12:38:32 +03:00
|
|
|
data.push(chunk);
|
2022-08-25 20:57:25 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
response.on("end", () => {
|
2022-09-12 19:35:47 -07:00
|
|
|
resolve([response.statusCode, response.headers["content-type"], Buffer.concat(data), response.headers]);
|
2022-08-25 20:57:25 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
request.on("error", (error) => {
|
|
|
|
reject([500, error]);
|
|
|
|
});
|
|
|
|
|
2022-09-12 19:35:47 -07:00
|
|
|
if (params.body) {
|
|
|
|
request.write(params.body);
|
|
|
|
}
|
|
|
|
|
2022-08-25 20:57:25 +03:00
|
|
|
request.end();
|
|
|
|
});
|
|
|
|
}
|
2022-09-04 21:58:42 +03:00
|
|
|
|
|
|
|
export function httpProxy(url, params = {}) {
|
|
|
|
const constructedUrl = new URL(url);
|
|
|
|
|
|
|
|
if (constructedUrl.protocol === "https:") {
|
|
|
|
const httpsAgent = new https.Agent({
|
|
|
|
rejectUnauthorized: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
return httpsRequest(constructedUrl, {
|
|
|
|
agent: httpsAgent,
|
|
|
|
...params,
|
|
|
|
});
|
|
|
|
}
|
2022-09-07 16:53:24 +03:00
|
|
|
return httpRequest(constructedUrl, params);
|
2022-09-04 21:58:42 +03:00
|
|
|
}
|