Merge branch '1stvamp-fix-offline-500'

This commit is contained in:
2016-05-06 17:19:37 +05:30
+15 -19
View File
@@ -1,4 +1,5 @@
var express = require('express'); var express = require('express');
var Promise = require('bluebird');
var config = require( __dirname + '/./config' ); var config = require( __dirname + '/./config' );
var utils = require( __dirname + '/./utils'); var utils = require( __dirname + '/./utils');
@@ -37,22 +38,21 @@ app.get( '/:package', function( req, res, next ){
return fileExists( cacheFile ) return fileExists( cacheFile )
.tap( function( isExists ){ .tap( function( isExists ){
if( !isExists ){ if( !isExists ){
if ( ENABLE_NPM_FAILOVER ) { if ( !ENABLE_NPM_FAILOVER ) {
res._log.cacheHit = '---'; return Promise.reject( { status:404, message: 'Package not found' });
return fetchAndCacheMetadata( packageName, cacheFile );
} else {
return res.status( 404 ).json( {} );
} }
res._log.cacheHit = '---';
return fetchAndCacheMetadata( packageName, cacheFile );
} }
}) })
.then( function( ){ .then( function( ){
res._log.cacheFile = cacheFile; res._log.cacheFile = cacheFile;
return readFile( cacheFile, 'utf-8' ); return readFile( cacheFile, 'utf-8' );
}) })
.then( function( cacheData ){ .then( function( cachedData ){
cacheData = JSON.parse( cacheData ); cachedData = JSON.parse( cachedData );
patchData( cacheData ); patchData( cachedData );
return res.send( cacheData ); return res.send( cachedData );
}) })
.catch( next ); .catch( next );
}); });
@@ -65,15 +65,14 @@ app.get( '/:package/-/:tarball', function( req, res, next ){
fileExists( packagePath ) fileExists( packagePath )
.tap( function( isExists ){ .tap( function( isExists ){
if( !isExists ){ if( !isExists ){
if ( ENABLE_NPM_FAILOVER ) { if ( !ENABLE_NPM_FAILOVER ) {
res._log.cacheHit = '---'; return Promise.reject( { status: 404, message: '' });
return fetchAndCacheTarball( packageName, version, packagePath );
} else {
return res.status( 404 ).json( {} );
} }
res._log.cacheHit = '---';
return fetchAndCacheTarball( packageName, version, packagePath );
} }
}) })
.then( function(){ .then( function( ){
res._log.cacheFile = packagePath; res._log.cacheFile = packagePath;
return res.sendFile( packagePath ); return res.sendFile( packagePath );
}) })
@@ -93,10 +92,7 @@ app.use(function(req, res, next) {
app.use(function(err, req, res, next) { app.use(function(err, req, res, next) {
console.log( err.stack ); console.log( err.stack );
res.status(err.status || 500); res.status(err.status || 500);
res.send({ res.send( err.message || err );
message: err.message,
error: err
});
if( next ) { next(); } if( next ) { next(); }
}); });