Skip to content

Commit

Permalink
Follow redirects in downloadBinaryFile
Browse files Browse the repository at this point in the history
  • Loading branch information
numtel committed Oct 12, 2024
1 parent c4e7fb2 commit 94101ff
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
*.swp
node_modules

.yarn/*
!.yarn/patches
!.yarn/plugins
Expand Down
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import https from 'node:https';
import {mkdirSync, createWriteStream, createReadStream} from 'node:fs';
import {isAbsolute, resolve, sep} from 'node:path';
import {exec} from 'node:child_process';
import nodeUrl from 'node:url';

import {S3Client, PutObjectCommand, GetObjectCommand, HeadObjectCommand, DeleteObjectsCommand} from '@aws-sdk/client-s3';
import {Upload} from '@aws-sdk/lib-storage';
Expand Down Expand Up @@ -71,8 +72,14 @@ export function downloadBinaryFile(url, outputPath) {
rejectUnauthorized: !url.startsWith('https://localhost:') // This allows self-signed certificates
}),
}, response => {
// Check if the request was successful
if (response.statusCode !== 200) {
// Check if the status code is a redirect (301, 302, 307, 308)
if (response.statusCode >= 300 && response.statusCode < 400 && response.headers.location) {
// Handle redirect by making a new request to the "location" header
const redirectUrl = nodeUrl.resolve(url, response.headers.location);
console.log(`Redirecting to: ${redirectUrl}`);
resolve(downloadBinaryFile(redirectUrl, outputPath));
return;
} else if (response.statusCode !== 200) {
reject(new Error(`Failed to download file: Status code ${response.statusCode}`));
return;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "circuitscan-pipeline-runner",
"version": "0.0.7",
"version": "0.0.8",
"main": "index.js",
"type": "module",
"dependencies": {
Expand Down

0 comments on commit 94101ff

Please sign in to comment.