diff --git a/.gitignore b/.gitignore index 870eb6a..379a89b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +*.swp +node_modules + .yarn/* !.yarn/patches !.yarn/plugins diff --git a/index.js b/index.js index 862b080..ed00a01 100644 --- a/index.js +++ b/index.js @@ -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'; @@ -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; } diff --git a/package.json b/package.json index a31d3b5..ec11fb7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "circuitscan-pipeline-runner", - "version": "0.0.7", + "version": "0.0.8", "main": "index.js", "type": "module", "dependencies": {