Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support redirections #341

Merged
merged 15 commits into from
Dec 29, 2023
8 changes: 7 additions & 1 deletion sources/httpUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@ export async function fetchUrlStream(url: string, options: RequestOptions = {})
const proxyAgent = new ProxyAgent();

return new Promise<IncomingMessage>((resolve, reject) => {
const request = https.get(url, {...options, agent: proxyAgent}, response => {
const creatRequest = (url: string) => https.get(url, {...options, agent: proxyAgent}, response => {
const statusCode = response.statusCode;

if (statusCode === 301 || statusCode === 302)
guibwl marked this conversation as resolved.
Show resolved Hide resolved
return creatRequest(response.headers.location);

if (statusCode != null && statusCode >= 200 && statusCode < 300)
return resolve(response);

return reject(new Error(`Server answered with HTTP ${statusCode} when performing the request to ${url}; for troubleshooting help, see https://github.com/nodejs/corepack#troubleshooting`));
});

const request = creatRequest(url);

request.on(`error`, err => {
reject(new Error(`Error when performing the request to ${url}; for troubleshooting help, see https://github.com/nodejs/corepack#troubleshooting`));
});
Expand Down