var express = require('express');
var config = require( __dirname + '/./config' );
var utils = require( __dirname + '/./utils');

var NPM_PATH = config.NPM_PATH;
var REGISTRY_NAME = config.REGISTRY_NAME;
var ENABLE_NPM_FAILOVER = config.ENABLE_NPM_FAILOVER;


var fetchAndCacheMetadata = utils.fetchAndCacheMetadata;
var fetchAndCacheTarball = utils.fetchAndCacheTarball;
var patchData = utils.patchData;
var fileExists = utils.fileExists;
var readFile = utils.readFile;

var app = express();
app.use( function(req, res, next ){
  res._log = {
    method: req.method,
    path: req.path,
    cacheHit: 'Hit',
    cacheFile: '',
  };

  res.on( 'finish', function(){
    var log = this._log;
    console.log( log.cacheHit, this.statusCode, log.method, log.path, '    =>   ', log.cacheFile  );
  });
  // setTimeout( next, 1000 );
  next();
});

app.get( '/:package', function( req, res, next ){
  var packageName = req.params.package;
  var cacheFile = [ NPM_PATH, REGISTRY_NAME, packageName, '.cache.json' ].join( '/' );

  return fileExists( cacheFile )
    .then( function( isExists ){
      if( !isExists ){
        if ( !ENABLE_NPM_FAILOVER ) {
          return false;
        }
        res._log.cacheHit = '---';
        return fetchAndCacheMetadata( packageName, cacheFile );
      }
    })
    .then( function( isExists ){
      if ( !isExists ) {
        return false;
      }
      res._log.cacheFile = cacheFile;
      return readFile( cacheFile, 'utf-8' );
    })
    .then( function( cacheData ){
      cacheData = JSON.parse( cacheData );
      if ( cacheData ) {
        patchData( cacheData );
      } else {
        res.status( 404 );
        cacheData = {};
      }
      return res.send( cacheData );
    })
    .catch( next );
});

app.get( '/:package/-/:tarball', function( req, res, next ){
  var packageName = req.params.package;
  var version = req.params.tarball.match( new RegExp( packageName + '-(.*).tgz') )[1];
  var packagePath = [ NPM_PATH , packageName, version, 'package.tgz'].join( '/' );

  fileExists( packagePath )
    .then( function( isExists ){
      if( !isExists ){
        if ( !ENABLE_NPM_FAILOVER ) {
          return false;
        }
        res._log.cacheHit = '---';
        return fetchAndCacheTarball( packageName, version, packagePath );
      }
    })
    .then( function( isExists ){
      res._log.cacheFile = packagePath;
      if ( isExists ) {
        return res.sendFile( packagePath );
      } else {
        res.status( 404 );
        return res.end();
      }
    })
    .catch( next );
});


// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

app.use(function(err, req, res, next) {
  console.log( err.stack );
  res.status(err.status || 500);
  res.send({
    message: err.message,
    error: err
  });
  if( next ) { next(); }
});



module.exports = app;

@@ -1307,32 +1307,34 @@
 ( function( 
-isExists
+cacheState
  )%7B%0A      if
@@ -1337,24 +1337,26 @@
   if ( !
-isExists
+cacheState
  ) %7B%0A   
@@ -2317,32 +2317,34 @@
 ( function( 
-isExists
+cacheState
  )%7B%0A      re
@@ -2386,24 +2386,26 @@
    if ( 
-isExists
+cacheState
  ) %7B%0A   

Rename second isState flags in both chains to cacheState, so it's clear what they represent in the next callback