mirror of
https://github.com/harish2704/npm-offline-registry.git
synced 2026-09-10 06:21:09 +00:00
Small cleanup
This commit is contained in:
@@ -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');
|
||||||
|
|
||||||
@@ -35,31 +36,23 @@ app.get( '/:package', function( req, res, next ){
|
|||||||
var cacheFile = [ NPM_PATH, REGISTRY_NAME, packageName, '.cache.json' ].join( '/' );
|
var cacheFile = [ NPM_PATH, REGISTRY_NAME, packageName, '.cache.json' ].join( '/' );
|
||||||
|
|
||||||
return fileExists( cacheFile )
|
return fileExists( cacheFile )
|
||||||
.then( function( isExists ){
|
.tap( function( isExists ){
|
||||||
if( !isExists ){
|
if( !isExists ){
|
||||||
if ( !ENABLE_NPM_FAILOVER ) {
|
if ( !ENABLE_NPM_FAILOVER ) {
|
||||||
return false;
|
return Promise.reject( { status:404, message: 'Package not found' });
|
||||||
}
|
}
|
||||||
res._log.cacheHit = '---';
|
res._log.cacheHit = '---';
|
||||||
return fetchAndCacheMetadata( packageName, cacheFile );
|
return fetchAndCacheMetadata( packageName, cacheFile );
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then( function( cacheState ){
|
.then( function( ){
|
||||||
if ( !cacheState ) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
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 );
|
||||||
if ( cacheData ) {
|
patchData( cachedData );
|
||||||
patchData( cacheData );
|
return res.send( cachedData );
|
||||||
} else {
|
|
||||||
res.status( 404 );
|
|
||||||
cacheData = {};
|
|
||||||
}
|
|
||||||
return res.send( cacheData );
|
|
||||||
})
|
})
|
||||||
.catch( next );
|
.catch( next );
|
||||||
});
|
});
|
||||||
@@ -70,23 +63,18 @@ app.get( '/:package/-/:tarball', function( req, res, next ){
|
|||||||
var packagePath = [ NPM_PATH , packageName, version, 'package.tgz'].join( '/' );
|
var packagePath = [ NPM_PATH , packageName, version, 'package.tgz'].join( '/' );
|
||||||
|
|
||||||
fileExists( packagePath )
|
fileExists( packagePath )
|
||||||
.then( function( isExists ){
|
.tap( function( isExists ){
|
||||||
if( !isExists ){
|
if( !isExists ){
|
||||||
if ( !ENABLE_NPM_FAILOVER ) {
|
if ( !ENABLE_NPM_FAILOVER ) {
|
||||||
return false;
|
return Promise.reject( { status: 404, message: '' });
|
||||||
}
|
}
|
||||||
res._log.cacheHit = '---';
|
res._log.cacheHit = '---';
|
||||||
return fetchAndCacheTarball( packageName, version, packagePath );
|
return fetchAndCacheTarball( packageName, version, packagePath );
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then( function( cacheState ){
|
.then( function( ){
|
||||||
res._log.cacheFile = packagePath;
|
res._log.cacheFile = packagePath;
|
||||||
if ( cacheState ) {
|
return res.sendFile( packagePath );
|
||||||
return res.sendFile( packagePath );
|
|
||||||
} else {
|
|
||||||
res.status( 404 );
|
|
||||||
return res.end();
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.catch( next );
|
.catch( next );
|
||||||
});
|
});
|
||||||
@@ -104,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(); }
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user