import * as assert from "assert" ; import * as http from "http" ; import * as https from "https" ; export function httpGet ( url ) { assert ( url , "Cannot make a HTTP request for a null, undefined, or empty url." ) ; if ( ! url . startsWith ( "http" ) ) { url = `http://${ url }` ; } return new Promise < string > ( ( resolve , reject ) : void => { try { let request ; function callback ( response ) { if ( typeof response . statusCode === "number" && 300 <= response . statusCode && response . statusCode < 400 && response . headers . location ) { resolve ( httpGet ( response . headers . location . toString ( ) ) ) ; } else if ( typeof response . statusCode === "number" && 200 <= response . statusCode && response . statusCode < 400 ) { let responseContent = "" ; let encoding ; response . on ( "data" , ( dataChunk ) => { const buffer = dataChunk instanceof Buffer ? dataChunk : new Buffer ( dataChunk ) ; let byteOrderMarkLength = 0 ; if ( ! encoding ) { if ( dataChunk [ 0 ] === 0xFF && dataChunk [ 1 ] === 0xFE ) { byteOrderMarkLength = 2 ; encoding = "utf16le" ; } else { if ( dataChunk [ 0 ] === 0xEF && dataChunk [ 1 ] === 0xBB && dataChunk [ 2 ] === 0xBF ) { byteOrderMarkLength = 3 ; } encoding = "utf8" ; } } responseContent += buffer . slice ( byteOrderMarkLength ) . toString ( encoding ) ; } ) . on ( "end" , ( ) => { resolve ( responseContent ) ; } ) ; } else { reject ( { method : response . method , requestUrl : url , statusCode : response . statusCode , statusMessage : response . statusMessage } ) ; } } if ( url . startsWith ( "https" ) ) { request = https . get ( url , callback ) ; } else if ( url . startsWith ( "http" ) ) { request = http . get ( url , callback ) ; } else { reject ( `Unsupported url schema: '${ url }` ) ; return ; } request . on ( "error" , ( e ) => { reject ( e ) ; } ) ; } catch ( err ) { reject ( err ) ; } } ) ; }