lib/plugins/HttpPlugin.js
function readFileAsync (args) {
    var file = args.file;
    var config = args.config;
    var next = args.next;
    var url = parseUrl(file.url);
    var transport;

    switch (url.protocol) {
      case 'http:':
        transport = followRedirects.http;
        break;
      case 'https:':
        transport = followRedirects.https;
        break;
      default:
        // It's not an HTTP or HTTPS URL, so let some other plugin handle it
        return next();
    }

    var httpConfig = {
      hostname: url.hostname,
      port: url.port,
      path: url.pathname + url.search,
      headers: Object.assign({}, defaultHeaders, config.http.headers),
      timeout: config.http.timeout,
      followRedirects: !!config.http.maxRedirects,
      maxRedirects: config.http.maxRedirects,
    };

    var req = transport.get(httpConfig);

    req.on('error', next);

    req.on('timeout', function handleTimeout () {
      req.abort();
    });

    req.once('response', function handleResponse (res) {
      var responseChunks = [];
      var chunksAreStrings = false;

      decompressResponse(res);
      setHttpMetadata(file, res);

      res.on('error', next);

      res.on('data', function handleResponseData (chunk) {
        // Keep track of whether ANY of the chunks are strings (as opposed to Buffers)
        if (typeof chunk === 'string') {
          chunksAreStrings = true;
        }

        responseChunks.push(chunk);
      });

      res.on('end', function handleResponseEnd () {
        try {
          var response;

          if (chunksAreStrings) {
            // Return the response as a string
            response = responseChunks.join('');
          }
          else {
            // Return the response as a Buffer (Uint8Array)
            response = Buffer.concat(responseChunks);
          }

          next(null, response);
        }
        catch (err) {
          next(err);
        }
      });
    });
  }
function readFileAsync (args) {
    var file = args.file;
    var config = args.config;
    var next = args.next;
    var url = parseUrl(file.url);
    var transport;

    switch (url.protocol) {
      case 'http:':
        transport = followRedirects.http;
        break;
      case 'https:':
        transport = followRedirects.https;
        break;
      default:
        // It's not an HTTP or HTTPS URL, so let some other plugin handle it
        return next();
    }

    var httpConfig = {
      hostname: url.hostname,
      port: url.port,
      path: url.pathname + url.search,
      headers: Object.assign({}, defaultHeaders, config.http.headers),
      timeout: config.http.timeout,
      followRedirects: !!config.http.maxRedirects,
      maxRedirects: config.http.maxRedirects,
    };

    var req = transport.get(httpConfig);

    req.on('error', next);

    req.on('timeout', function handleTimeout () {
      req.abort();
    });

    req.once('response', function handleResponse (res) {
      var responseChunks = [];
      var chunksAreStrings = false;

      decompressResponse(res);
      setHttpMetadata(file, res);

      res.on('error', next);

      res.on('data', function handleResponseData (chunk) {
        // Keep track of whether ANY of the chunks are strings (as opposed to Buffers)
        if (typeof chunk === 'string') {
          chunksAreStrings = true;
        }

        responseChunks.push(chunk);
      });

      res.on('end', function handleResponseEnd () {
        try {
          var response;

          if (chunksAreStrings) {
            // Return the response as a string
            response = responseChunks.join('');
          }
          else {
            // Return the response as a Buffer (Uint8Array)
            response = Buffer.concat(responseChunks);
          }

          next(null, response);
        }
        catch (err) {
          next(err);
        }
      });
    });
  }
function readFileAsync ( args ) { var file = args . file ; var config = args . config ; var next = args . next ; var url = parseUrl ( file . url ) ; var transport ; switch ( url . protocol ) { case 'http:' : transport = followRedirects . http ; break ; case 'https:' : transport = followRedirects . https ; break ; default : // It's not an HTTP or HTTPS URL, so let some other plugin handle it return next ( ) ; } var httpConfig = { hostname : url . hostname , port : url . port , path : url . pathname + url . search , headers : Object . assign ( { } , defaultHeaders , config . http . headers ) , timeout : config . http . timeout , followRedirects : ! ! config . http . maxRedirects , maxRedirects : config . http . maxRedirects , } ; var req = transport . get ( httpConfig ) ; req . on ( 'error' , next ) ; req . on ( 'timeout' , function handleTimeout ( ) { req . abort ( ) ; } ) ; req . once ( 'response' , function handleResponse ( res ) { var responseChunks = [ ] ; var chunksAreStrings = false ; decompressResponse ( res ) ; setHttpMetadata ( file , res ) ; res . on ( 'error' , next ) ; res . on ( 'data' , function handleResponseData ( chunk ) { // Keep track of whether ANY of the chunks are strings (as opposed to Buffers) if ( typeof chunk === 'string' ) { chunksAreStrings = true ; } responseChunks . push ( chunk ) ; } ) ; res . on ( 'end' , function handleResponseEnd ( ) { try { var response ; if ( chunksAreStrings ) { // Return the response as a string response = responseChunks . join ( '' ) ; } else { // Return the response as a Buffer (Uint8Array) response = Buffer . concat ( responseChunks ) ; } next ( null , response ) ; } catch ( err ) { next ( err ) ; } } ) ; } ) ; }
Asynchronously downlaods a file from an HTTP or HTTPS URL.

@param {File} args.file - The {@link File} to read
@param {function} args.next - Calls the next plugin, if the file is not a local filesystem file
Asynchronously downlaods a file from an HTTP or HTTPS URL .