apim-pull.js
function fetchFromCache(options, opts, cb) {
  // look for existing cached resource
  if (indirFiles.length > 0) {
    var etag;
    var regexp = '^' + opts.prefix +
                 '([A-Za-z0-9]+={0,2})' + // base64 encoded
                 opts.suffix + '$';
    var regex = new RegExp(regexp);
    var i;
    for (i = 0; i < indirFiles.length; i++) {
      var file = regex.exec(indirFiles[i]);
      if (file) {
        etag = new Buffer(file[1], 'base64').toString();
        break;
      }
    }

    if (etag) {
      try {
        var headOpts = JSON.parse(JSON.stringify(options)); // clone options
        headOpts.headers = { 'If-None-Match': etag };
        request.head(headOpts, function(err, res, body) {
          if (err) {
            logger.error(err);
            cb();
            // not fatal; continue
          } else if (res.statusCode === 304) {
            var filename = path.join(opts.outdir, indirFiles[i]);
            fs.copy(path.join(opts.indir, indirFiles[i]),
                    filename,
                    { preserveTimestamps: true },
                    function(err) {
                      if (err) {
                        throw (err);
                      }
                      var body = decryptData(fs.readFileSync(filename));
                      logger.info('Using cached copy of %s', indirFiles[i]);
                      var result = {
                        file: filename,
                        contents: body };
                      cb(null, result);
                    });
          } else {
            cb();
          }
        });
      } catch (e) {
        logger.error(e);
        cb();
        // not fatal; continue
      }
    } else {
      cb();
    }
  } else {
    cb();
  }
}
function fetchFromCache(options, opts, cb) {
  // look for existing cached resource
  if (indirFiles.length > 0) {
    var etag;
    var regexp = '^' + opts.prefix +
                 '([A-Za-z0-9]+={0,2})' + // base64 encoded
                 opts.suffix + '$';
    var regex = new RegExp(regexp);
    var i;
    for (i = 0; i < indirFiles.length; i++) {
      var file = regex.exec(indirFiles[i]);
      if (file) {
        etag = new Buffer(file[1], 'base64').toString();
        break;
      }
    }

    if (etag) {
      try {
        var headOpts = JSON.parse(JSON.stringify(options)); // clone options
        headOpts.headers = { 'If-None-Match': etag };
        request.head(headOpts, function(err, res, body) {
          if (err) {
            logger.error(err);
            cb();
            // not fatal; continue
          } else if (res.statusCode === 304) {
            var filename = path.join(opts.outdir, indirFiles[i]);
            fs.copy(path.join(opts.indir, indirFiles[i]),
                    filename,
                    { preserveTimestamps: true },
                    function(err) {
                      if (err) {
                        throw (err);
                      }
                      var body = decryptData(fs.readFileSync(filename));
                      logger.info('Using cached copy of %s', indirFiles[i]);
                      var result = {
                        file: filename,
                        contents: body };
                      cb(null, result);
                    });
          } else {
            cb();
          }
        });
      } catch (e) {
        logger.error(e);
        cb();
        // not fatal; continue
      }
    } else {
      cb();
    }
  } else {
    cb();
  }
}
function fetchFromCache ( options , opts , cb ) { // look for existing cached resource if ( indirFiles . length > 0 ) { var etag ; var regexp = '^' + opts . prefix + '([A-Za-z0-9]+={0,2})' + // base64 encoded opts . suffix + '$' ; var regex = new RegExp ( regexp ) ; var i ; for ( i = 0 ; i < indirFiles . length ; i ++ ) { var file = regex . exec ( indirFiles [ i ] ) ; if ( file ) { etag = new Buffer ( file [ 1 ] , 'base64' ) . toString ( ) ; break ; } } if ( etag ) { try { var headOpts = JSON . parse ( JSON . stringify ( options ) ) ; // clone options headOpts . headers = { 'If-None-Match' : etag } ; request . head ( headOpts , function ( err , res , body ) { if ( err ) { logger . error ( err ) ; cb ( ) ; // not fatal; continue } else if ( res . statusCode === 304 ) { var filename = path . join ( opts . outdir , indirFiles [ i ] ) ; fs . copy ( path . join ( opts . indir , indirFiles [ i ] ) , filename , { preserveTimestamps : true } , function ( err ) { if ( err ) { throw ( err ) ; } var body = decryptData ( fs . readFileSync ( filename ) ) ; logger . info ( 'Using cached copy of %s' , indirFiles [ i ] ) ; var result = { file : filename , contents : body } ; cb ( null , result ) ; } ) ; } else { cb ( ) ; } } ) ; } catch ( e ) { logger . error ( e ) ; cb ( ) ; // not fatal; continue } } else { cb ( ) ; } } else { cb ( ) ; } }
Uses cached configuration if still fresh
Uses cached configuration if still fresh