index.js
function download(url) {
  if (!validator.isURL(url)) return Promise.reject(error(400, 'Invalid URL: ' + url));
  // download in progress
  if (progress[sha]) return progress[sha];
  var sha = hash(url);
  return progress[sha] = cache.access(sha).then(function (filename) {
    if (filename) return filename;

    return request(url)
    .redirects(3)
    .agent(false)
    .then(function (response) {
      assert(response.status < 400, response.status, 'Got status code ' + response.status + ' from ' + url);
      assert(response.status === 200, 'Got status code ' + response.status + ' from ' + url);

      debug('Downloading %s -> %s', url, sha);

      return cache.copy(sha, response.response);
    });
  }).then(function (filename) {
    delete progress[sha];
    return filename;
  }, /* istanbul ignore next */ function (err) {
    delete progress[sha];
    throw err;
  });
}
function download(url) {
  if (!validator.isURL(url)) return Promise.reject(error(400, 'Invalid URL: ' + url));
  // download in progress
  if (progress[sha]) return progress[sha];
  var sha = hash(url);
  return progress[sha] = cache.access(sha).then(function (filename) {
    if (filename) return filename;

    return request(url)
    .redirects(3)
    .agent(false)
    .then(function (response) {
      assert(response.status < 400, response.status, 'Got status code ' + response.status + ' from ' + url);
      assert(response.status === 200, 'Got status code ' + response.status + ' from ' + url);

      debug('Downloading %s -> %s', url, sha);

      return cache.copy(sha, response.response);
    });
  }).then(function (filename) {
    delete progress[sha];
    return filename;
  }, /* istanbul ignore next */ function (err) {
    delete progress[sha];
    throw err;
  });
}
function download ( url ) { if ( ! validator . isURL ( url ) ) return Promise . reject ( error ( 400 , 'Invalid URL: ' + url ) ) ; // download in progress if ( progress [ sha ] ) return progress [ sha ] ; var sha = hash ( url ) ; return progress [ sha ] = cache . access ( sha ) . then ( function ( filename ) { if ( filename ) return filename ; return request ( url ) . redirects ( 3 ) . agent ( false ) . then ( function ( response ) { assert ( response . status < 400 , response . status , 'Got status code ' + response . status + ' from ' + url ) ; assert ( response . status === 200 , 'Got status code ' + response . status + ' from ' + url ) ; debug ( 'Downloading %s -> %s' , url , sha ) ; return cache . copy ( sha , response . response ) ; } ) ; } ) . then ( function ( filename ) { delete progress [ sha ] ; return filename ; } , /* istanbul ignore next */ function ( err ) { delete progress [ sha ] ; throw err ; } ) ; }
If the locally exists locally, use that.
Otherwise, download it and return the filename.
We cache by the sha of the URL.
If the locally exists locally use that . Otherwise download it and return the filename . We cache by the sha of the URL .