diff --git a/resources/scripts/download.js b/resources/scripts/download.js new file mode 100644 index 00000000..cc97f1df --- /dev/null +++ b/resources/scripts/download.js @@ -0,0 +1,50 @@ +const { ProxyAgent } = require('undici') +const { SocksProxyAgent } = require('socks-proxy-agent') +const https = require('https') +const fs = require('fs') +const { pipeline } = require('stream/promises') + +/** + * Downloads a file from a URL with redirect handling + * @param {string} url The URL to download from + * @param {string} destinationPath The path to save the file to + * @returns {Promise} Promise that resolves when download is complete + */ +async function downloadWithRedirects(url, destinationPath) { + const proxyUrl = process.env.HTTPS_PROXY || process.env.HTTP_PROXY + if (proxyUrl.startsWith('socks')) { + const proxyAgent = new SocksProxyAgent(proxyUrl) + return new Promise((resolve, reject) => { + const request = (url) => { + https.get(url, { agent: proxyAgent }, (response) => { + if (response.statusCode == 301 || response.statusCode == 302) { + request(response.headers.location) + return + } + if (response.statusCode !== 200) { + reject(new Error(`Download failed: ${response.statusCode} ${response.statusMessage}`)) + return + } + const file = fs.createWriteStream(destinationPath) + response.pipe(file) + file.on('finish', () => resolve()) + }).on('error', (err) => { + reject(err) + }) + } + request(url) + }) + } else { + const proxyAgent = new ProxyAgent(proxyUrl) + const response = await fetch(url, { + dispatcher: proxyAgent + }) + if (!response.ok) { + throw new Error(`Download failed: ${response.status} ${response.statusText}`) + } + const file = fs.createWriteStream(destinationPath) + await pipeline(response.body, file) + } +} + +module.exports = { downloadWithRedirects } diff --git a/resources/scripts/install-bun.js b/resources/scripts/install-bun.js index 6087c191..24612847 100644 --- a/resources/scripts/install-bun.js +++ b/resources/scripts/install-bun.js @@ -2,8 +2,8 @@ const fs = require('fs') const path = require('path') const os = require('os') const { execSync } = require('child_process') -const https = require('https') const AdmZip = require('adm-zip') +const { downloadWithRedirects } = require('./download') // Base URL for downloading bun binaries const BUN_RELEASE_BASE_URL = 'https://github.com/oven-sh/bun/releases/download' @@ -24,41 +24,6 @@ const BUN_PACKAGES = { 'linux-musl-arm64': 'bun-linux-aarch64-musl.zip' } -/** - * Downloads a file from a URL with redirect handling - * @param {string} url The URL to download from - * @param {string} destinationPath The path to save the file to - * @returns {Promise} - */ -async function downloadWithRedirects(url, destinationPath) { - return new Promise((resolve, reject) => { - const file = fs.createWriteStream(destinationPath) - const request = (url) => { - https - .get(url, (response) => { - if (response.statusCode === 302 || response.statusCode === 301) { - // Handle redirect - request(response.headers.location) - return - } - - if (response.statusCode !== 200) { - reject(new Error(`Failed to download: ${response.statusCode}`)) - return - } - - response.pipe(file) - file.on('finish', () => { - file.close(resolve) - }) - }) - .on('error', reject) - } - - request(url) - }) -} - /** * Downloads and extracts the bun binary for the specified platform and architecture * @param {string} platform Platform to download for (e.g., 'darwin', 'win32', 'linux') diff --git a/resources/scripts/install-uv.js b/resources/scripts/install-uv.js index 773ff24c..0b3dfe3a 100644 --- a/resources/scripts/install-uv.js +++ b/resources/scripts/install-uv.js @@ -2,9 +2,9 @@ const fs = require('fs') const path = require('path') const os = require('os') const { execSync } = require('child_process') -const https = require('https') const tar = require('tar') const AdmZip = require('adm-zip') +const { downloadWithRedirects } = require('./download') // Base URL for downloading uv binaries const UV_RELEASE_BASE_URL = 'https://github.com/astral-sh/uv/releases/download' @@ -32,41 +32,6 @@ const UV_PACKAGES = { 'linux-musl-armv7l': 'uv-armv7-unknown-linux-musleabihf.tar.gz' } -/** - * Downloads a file from a URL with redirect handling - * @param {string} url The URL to download from - * @param {string} destinationPath The path to save the file to - * @returns {Promise} - */ -async function downloadWithRedirects(url, destinationPath) { - return new Promise((resolve, reject) => { - const file = fs.createWriteStream(destinationPath) - const request = (url) => { - https - .get(url, (response) => { - if (response.statusCode === 302 || response.statusCode === 301) { - // Handle redirect - request(response.headers.location) - return - } - - if (response.statusCode !== 200) { - reject(new Error(`Failed to download: ${response.statusCode}`)) - return - } - - response.pipe(file) - file.on('finish', () => { - file.close(resolve) - }) - }) - .on('error', reject) - } - - request(url) - }) -} - /** * Downloads and extracts the uv binary for the specified platform and architecture * @param {string} platform Platform to download for (e.g., 'darwin', 'win32', 'linux')