lib/request.js
function download(uri, dest, progressCallback) {
    "use strict";
    progressCallback = progressCallback || function() {};
    var parsedUrl = url.parse(uri, false, true);
    var makeRequest = parsedUrl.protocol === 'https:' ? https.request.bind(https) : http.request.bind(http);
    var serverPort = parsedUrl.port ? parsedUrl.port : parsedUrl.protocol === 'https:' ? 443 : 80;
    var agent = parsedUrl.protocol === 'https:' ? httpsAgent : httpAgent;
    var file = fs.createWriteStream(dest);

    var options = {
        host: parsedUrl.host,
        path: parsedUrl.path,
        agent: agent,
        port: serverPort,
        method: 'GET'
    };

    return new Promise(function (resolve, reject) {

        var req = makeRequest(options, function (response) {
            var size = response.headers['content-length'];
            var progress = 0;

            response.on('data', function(chunk) {
                progress += chunk.length;
                progressCallback(size, progress);
            });

            response.pipe(file);
            file.on('finish', function() {
                resolve({
                    status: response.statusCode
                });
            });
        });

        req.on('error', function(error) {
            file.end();
            rimraf.sync(dest);
            reject(error);
        });

        req.end();
    });
}
function download(uri, dest, progressCallback) {
    "use strict";
    progressCallback = progressCallback || function() {};
    var parsedUrl = url.parse(uri, false, true);
    var makeRequest = parsedUrl.protocol === 'https:' ? https.request.bind(https) : http.request.bind(http);
    var serverPort = parsedUrl.port ? parsedUrl.port : parsedUrl.protocol === 'https:' ? 443 : 80;
    var agent = parsedUrl.protocol === 'https:' ? httpsAgent : httpAgent;
    var file = fs.createWriteStream(dest);

    var options = {
        host: parsedUrl.host,
        path: parsedUrl.path,
        agent: agent,
        port: serverPort,
        method: 'GET'
    };

    return new Promise(function (resolve, reject) {

        var req = makeRequest(options, function (response) {
            var size = response.headers['content-length'];
            var progress = 0;

            response.on('data', function(chunk) {
                progress += chunk.length;
                progressCallback(size, progress);
            });

            response.pipe(file);
            file.on('finish', function() {
                resolve({
                    status: response.statusCode
                });
            });
        });

        req.on('error', function(error) {
            file.end();
            rimraf.sync(dest);
            reject(error);
        });

        req.end();
    });
}
function download ( uri , dest , progressCallback ) { "use strict" ; progressCallback = progressCallback || function ( ) { } ; var parsedUrl = url . parse ( uri , false , true ) ; var makeRequest = parsedUrl . protocol === 'https:' ? https . request . bind ( https ) : http . request . bind ( http ) ; var serverPort = parsedUrl . port ? parsedUrl . port : parsedUrl . protocol === 'https:' ? 443 : 80 ; var agent = parsedUrl . protocol === 'https:' ? httpsAgent : httpAgent ; var file = fs . createWriteStream ( dest ) ; var options = { host : parsedUrl . host , path : parsedUrl . path , agent : agent , port : serverPort , method : 'GET' } ; return new Promise ( function ( resolve , reject ) { var req = makeRequest ( options , function ( response ) { var size = response . headers [ 'content-length' ] ; var progress = 0 ; response . on ( 'data' , function ( chunk ) { progress += chunk . length ; progressCallback ( size , progress ) ; } ) ; response . pipe ( file ) ; file . on ( 'finish' , function ( ) { resolve ( { status : response . statusCode } ) ; } ) ; } ) ; req . on ( 'error' , function ( error ) { file . end ( ) ; rimraf . sync ( dest ) ; reject ( error ) ; } ) ; req . end ( ) ; } ) ; }
Downloads a url to a file.

@param uri {string} the uri to download
@param dest {string}
@param progressCallback {function} receives progress updates
@returns {Promise.<{}|Error>} the status code or an error
Downloads a url to a file .