Skip to content

Commit

Permalink
feat: support redirections
Browse files Browse the repository at this point in the history
  • Loading branch information
guibwl authored Dec 14, 2023
1 parent b7aa576 commit 9490fd2
Showing 1 changed file with 7 additions and 1 deletion.
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)
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

0 comments on commit 9490fd2

Please sign in to comment.