2022-09-26 07:57:35 +02:00
|
|
|
import * as https from "https";
|
2022-09-06 05:58:16 +02:00
|
|
|
|
2023-05-20 01:38:26 +02:00
|
|
|
const methods = ["get", "put", "post", "delete", "head"];
|
2022-09-29 23:46:07 +02:00
|
|
|
const redirectCodes = new Set([301, 302, 307, 308]);
|
2022-09-26 07:57:35 +02:00
|
|
|
const headersToClone = ["statusCode", "statusMessage", "url", "headers", "method", "aborted", "complete", "rawHeaders", "end"];
|
2022-09-06 05:58:16 +02:00
|
|
|
|
2022-09-29 23:46:07 +02:00
|
|
|
const makeRequest = (url, options, callback, setReq) => {
|
2022-09-26 07:57:35 +02:00
|
|
|
const req = https.request(url, Object.assign({method: "GET"}, options), res => {
|
2022-09-29 23:46:07 +02:00
|
|
|
if (redirectCodes.has(res.statusCode) && res.headers.location) {
|
|
|
|
const final = new URL(res.headers.location);
|
2022-09-06 05:58:16 +02:00
|
|
|
|
2022-09-29 23:46:07 +02:00
|
|
|
for (const [key, value] of new URL(url).searchParams.entries()) {
|
|
|
|
final.searchParams.set(key, value);
|
|
|
|
}
|
2022-09-26 07:57:35 +02:00
|
|
|
|
2022-09-29 23:46:07 +02:00
|
|
|
return makeRequest(final.toString(), options, callback, setReq);
|
2022-09-06 05:58:16 +02:00
|
|
|
}
|
|
|
|
|
2022-09-29 23:46:07 +02:00
|
|
|
const chunks = [];
|
|
|
|
let error = null;
|
|
|
|
|
|
|
|
setReq(res, req);
|
|
|
|
|
2022-09-26 07:57:35 +02:00
|
|
|
res.addListener("error", err => {error = err;});
|
2022-09-06 05:58:16 +02:00
|
|
|
|
2022-09-26 07:57:35 +02:00
|
|
|
res.addListener("data", chunk => {
|
|
|
|
chunks.push(chunk);
|
|
|
|
});
|
2022-09-06 05:58:16 +02:00
|
|
|
|
2022-09-26 07:57:35 +02:00
|
|
|
res.addListener("end", () => {
|
|
|
|
const headers = Object.fromEntries(headersToClone.map(h => [h, res[h]]));
|
2022-09-06 05:58:16 +02:00
|
|
|
|
2022-09-26 07:57:35 +02:00
|
|
|
callback(error, headers, Buffer.concat(chunks));
|
|
|
|
req.end();
|
|
|
|
});
|
2022-09-06 05:58:16 +02:00
|
|
|
});
|
2023-04-12 23:20:20 +02:00
|
|
|
|
|
|
|
if (options.formData) {
|
|
|
|
// Make sure to close the socket.
|
|
|
|
try {req.write(options.formData);}
|
|
|
|
finally {req.end();}
|
2024-02-22 06:55:46 +01:00
|
|
|
}
|
|
|
|
else {
|
2023-04-12 23:20:20 +02:00
|
|
|
req.end();
|
|
|
|
}
|
|
|
|
|
2022-10-14 05:42:05 +02:00
|
|
|
};
|
2022-09-29 23:46:07 +02:00
|
|
|
|
|
|
|
const request = function (url, options, callback) {
|
2022-10-14 05:42:05 +02:00
|
|
|
let responseObject = null;
|
2022-09-29 23:46:07 +02:00
|
|
|
let reqObject = null;
|
|
|
|
let pipe = null;
|
|
|
|
|
|
|
|
makeRequest(url, options, callback, (req, res) => {
|
|
|
|
reqObject = req;
|
|
|
|
responseObject = res;
|
|
|
|
|
|
|
|
if (pipe) {
|
|
|
|
res.pipe(pipe);
|
|
|
|
}
|
|
|
|
});
|
2022-09-06 05:58:16 +02:00
|
|
|
|
2022-09-26 07:57:35 +02:00
|
|
|
return {
|
2022-09-29 23:46:07 +02:00
|
|
|
end() {reqObject?.end();},
|
2022-09-26 07:57:35 +02:00
|
|
|
pipe(fsStream) {
|
|
|
|
if (!responseObject) {
|
|
|
|
pipe = fsStream;
|
2022-10-14 05:42:05 +02:00
|
|
|
}
|
|
|
|
else {
|
2022-09-26 07:57:35 +02:00
|
|
|
responseObject.pipe(fsStream);
|
|
|
|
}
|
2022-09-06 05:58:16 +02:00
|
|
|
}
|
2022-09-26 07:57:35 +02:00
|
|
|
};
|
2022-09-06 05:58:16 +02:00
|
|
|
};
|
|
|
|
|
2022-09-26 07:57:35 +02:00
|
|
|
export default Object.assign({request},
|
|
|
|
Object.fromEntries(methods.map(method => [
|
|
|
|
method,
|
|
|
|
function () {
|
|
|
|
arguments[1] ??= {};
|
2022-09-06 05:58:16 +02:00
|
|
|
|
2022-09-26 07:57:35 +02:00
|
|
|
arguments[1].method ??= method.toUpperCase();
|
2022-09-06 05:58:16 +02:00
|
|
|
|
2022-09-26 07:57:35 +02:00
|
|
|
return Reflect.apply(request, this, arguments);
|
|
|
|
}
|
|
|
|
]))
|
|
|
|
);
|