fetch-filecache.js
function hasExpired(headers, refresh) {
    if (!headers) {
      log('response is not in cache');
      return true;
    }
    if (refresh === 'force') {
      log('response in cache but refresh requested');
      return true;
    }
    if (refresh === 'never') {
      log('response in cache and considered to be always valid');
      return false;
    }

    let received = new Date(
      headers.received || headers.date || 'Jan 1, 1970, 00:00:00.000 GMT');
    received = received.getTime();
    if (refresh === 'once') {
      if (received < launchTime) {
        log('response in cache but one refresh requested');
        return true;
      }
      else {
        log('response in cache and already refreshed once')
        return false;
      }
    }

    let now = Date.now();
    if (Number.isInteger(refresh)) {
      if (received + refresh * 1000 < now) {
        log('response in cache is older than requested duration');
        return true;
      }
      else {
        log('response in cache is fresh enough for requested duration');
        return false;
      }
    }

    // Apply HTTP expiration rules otherwise
    if (headers.expires) {
      try {
        let expires = (new Date(headers.expires)).getTime();
        if (expires < now) {
          log('response in cache has expired');
          return true;
        }
        else {
          log('response in cache is still valid');
          return false;
        }
      }
      catch (err) {}
    }

    if (headers['cache-control']) {
      try {
        let tokens = headers['cache-control'].split(',')
          .map(token => token.split('='));
        for (token of tokens) {
          let param = token[0].trim();
          if (param === 'no-cache') {
            log('response in cache but no-cache directive');
            return true;
          }
          else if (param === 'max-age') {
            let maxAge = parseInt(token[1], 10);
            if (received + maxAge * 1000 < now) {
              log('response in cache has expired');
              return true;
            }
            else {
              log('response in cache is still valid');
              return false;
            }
          }
        }
      }
      catch (err) {}
    }

    // Cannot tell? Let's refresh the cache
    log('response in cache has expired (no explicit directive)');
    return true;
  }
function hasExpired(headers, refresh) {
    if (!headers) {
      log('response is not in cache');
      return true;
    }
    if (refresh === 'force') {
      log('response in cache but refresh requested');
      return true;
    }
    if (refresh === 'never') {
      log('response in cache and considered to be always valid');
      return false;
    }

    let received = new Date(
      headers.received || headers.date || 'Jan 1, 1970, 00:00:00.000 GMT');
    received = received.getTime();
    if (refresh === 'once') {
      if (received < launchTime) {
        log('response in cache but one refresh requested');
        return true;
      }
      else {
        log('response in cache and already refreshed once')
        return false;
      }
    }

    let now = Date.now();
    if (Number.isInteger(refresh)) {
      if (received + refresh * 1000 < now) {
        log('response in cache is older than requested duration');
        return true;
      }
      else {
        log('response in cache is fresh enough for requested duration');
        return false;
      }
    }

    // Apply HTTP expiration rules otherwise
    if (headers.expires) {
      try {
        let expires = (new Date(headers.expires)).getTime();
        if (expires < now) {
          log('response in cache has expired');
          return true;
        }
        else {
          log('response in cache is still valid');
          return false;
        }
      }
      catch (err) {}
    }

    if (headers['cache-control']) {
      try {
        let tokens = headers['cache-control'].split(',')
          .map(token => token.split('='));
        for (token of tokens) {
          let param = token[0].trim();
          if (param === 'no-cache') {
            log('response in cache but no-cache directive');
            return true;
          }
          else if (param === 'max-age') {
            let maxAge = parseInt(token[1], 10);
            if (received + maxAge * 1000 < now) {
              log('response in cache has expired');
              return true;
            }
            else {
              log('response in cache is still valid');
              return false;
            }
          }
        }
      }
      catch (err) {}
    }

    // Cannot tell? Let's refresh the cache
    log('response in cache has expired (no explicit directive)');
    return true;
  }
function hasExpired ( headers , refresh ) { if ( ! headers ) { log ( 'response is not in cache' ) ; return true ; } if ( refresh === 'force' ) { log ( 'response in cache but refresh requested' ) ; return true ; } if ( refresh === 'never' ) { log ( 'response in cache and considered to be always valid' ) ; return false ; } let received = new Date ( headers . received || headers . date || 'Jan 1, 1970, 00:00:00.000 GMT' ) ; received = received . getTime ( ) ; if ( refresh === 'once' ) { if ( received < launchTime ) { log ( 'response in cache but one refresh requested' ) ; return true ; } else { log ( 'response in cache and already refreshed once' ) return false ; } } let now = Date . now ( ) ; if ( Number . isInteger ( refresh ) ) { if ( received + refresh * 1000 < now ) { log ( 'response in cache is older than requested duration' ) ; return true ; } else { log ( 'response in cache is fresh enough for requested duration' ) ; return false ; } } // Apply HTTP expiration rules otherwise if ( headers . expires ) { try { let expires = ( new Date ( headers . expires ) ) . getTime ( ) ; if ( expires < now ) { log ( 'response in cache has expired' ) ; return true ; } else { log ( 'response in cache is still valid' ) ; return false ; } } catch ( err ) { } } if ( headers [ 'cache-control' ] ) { try { let tokens = headers [ 'cache-control' ] . split ( ',' ) . map ( token => token . split ( '=' ) ) ; for ( token of tokens ) { let param = token [ 0 ] . trim ( ) ; if ( param === 'no-cache' ) { log ( 'response in cache but no-cache directive' ) ; return true ; } else if ( param === 'max-age' ) { let maxAge = parseInt ( token [ 1 ] , 10 ) ; if ( received + maxAge * 1000 < now ) { log ( 'response in cache has expired' ) ; return true ; } else { log ( 'response in cache is still valid' ) ; return false ; } } } } catch ( err ) { } } // Cannot tell? Let's refresh the cache log ( 'response in cache has expired (no explicit directive)' ) ; return true ; }
Look at HTTP headers, current time and refresh strategy to determine whether
cached content has expired

@function
@param {Object} headers HTTP headers received last time
@param {String|Integer} refresh Refresh strategy
@return {Boolean} true if cached content has expired (or does not exist),
false when it can still be returned.
Look at HTTP headers current time and refresh strategy to determine whether cached content has expired