import * as https from "https" ; import * as HttpsProxyAgent from "https-proxy-agent" ; import q = require ( "q" ) ; import { IncomingMessage } from "http" ; function httpsGet ( url ) { const deferal = q . defer < IncomingMessage > ( ) ; const options = { } ; var proxy = process . env . HTTPS_PROXY || process . env . https_proxy || process . env . HTTP_PROXY || process . env . http_proxy ; if ( proxy !== null && proxy !== undefined ) { options . agent = new HttpsProxyAgent ( proxy ) ; } https . get ( url , options , ( response ) => { deferal . resolve ( response ) ; } ) . on ( "error" , ( err ) => { deferal . reject ( err ) ; } ) ; return deferal . promise ; } export async function downloadFrom ( url , logRedirect ? ) { let response = await httpsGet ( url ) ; while ( ( response . statusCode >= 301 && response . statusCode <= 303 ) || response . statusCode == 307 ) { const location = response . headers [ "location" ] as string ; logRedirect && logRedirect ( location ) ; response = await httpsGet ( location ) ; } return response ; }