Make strict mode as a configurable option ( using env variable )

In strict mode, Only versions available in the cache will appear in the info response.
This commit is contained in:
2016-11-27 17:52:45 +05:30
parent a157250315
commit 8d2102aacd
4 changed files with 33 additions and 10 deletions
+11 -1
View File
@@ -1,4 +1,3 @@
# npm-offline-registry
Supper simple NPM registry server for offline NPM install Supper simple NPM registry server for offline NPM install
# Usage # Usage
@@ -56,3 +55,14 @@ Please check config.js. All config values can be over-written by `environment-va
If you set the ``ENABLE_NPM_FAILOVER`` config value to ``false`` then npm-offlin-registry will not attempt to If you set the ``ENABLE_NPM_FAILOVER`` config value to ``false`` then npm-offlin-registry will not attempt to
contact the upstream NPM registry for unknown packages and instead return a 404 response, meaning you can use contact the upstream NPM registry for unknown packages and instead return a 404 response, meaning you can use
it as an alternative to the NPM registry behind a firewall / isolated from the internet. it as an alternative to the NPM registry behind a firewall / isolated from the internet.
# Enabling strict mode
If config value `STRICT` is set to true, while npm checks for the available versions of a given packages,
registry server will reply with list of cached versions.
In this case, if the version is not previously cached, npm-offline-install will fail with, error message `version not available`
example
```bash
env STRICT=true npm-offline-registry`
```
+5 -1
View File
@@ -3,7 +3,8 @@ var defaultConfig = {
NPM_PATH : process.env.HOME + '/.npm', NPM_PATH : process.env.HOME + '/.npm',
REGISTRY_NAME : 'registry.npmjs.org', REGISTRY_NAME : 'registry.npmjs.org',
PORT: 8234, PORT: 8234,
ENABLE_NPM_FAILOVER: true ENABLE_NPM_FAILOVER: true,
STRICT: false
}; };
var config = {}; var config = {};
@@ -21,5 +22,8 @@ if( config.ENABLE_NPM_FAILOVER == 'false' ){
config.ENABLE_NPM_FAILOVER = false; config.ENABLE_NPM_FAILOVER = false;
} }
if( config.STRICT == 'true' ){
config.STRICT = true;
}
module.exports = config; module.exports = config;
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "npm-offline-registry", "name": "npm-offline-registry",
"version": "0.0.7", "version": "0.0.8",
"description": "Super simple loca npm registry server for offline usage", "description": "Super simple loca npm registry server for offline usage",
"main": "bin/www", "main": "bin/www",
"directories": { "directories": {
+16 -7
View File
@@ -29,17 +29,26 @@ exports.readFile = readFile;
exports.patchData = function ( data ){ exports.patchData = function ( data ){
/* Get the list of versions which is available in local cache */ /* Get the list of versions which is available in local cache */
var cachedVersions = fs.readdirSync( path.join( NPM_PATH, data.name ) ); var cacheJsonFile, cacheJsonFileData = [];
if( config.STRICT ){
cacheJsonFile = path.join( NPM_PATH, data.name );
cacheJsonFileData = fs.existsSync( cacheJsonFile ) ? fs.readdirSync( cacheJsonFile ) : [];
}
Object.keys(data.versions).forEach( function( v ){ Object.keys(data.versions).forEach( function( v ){
var val = data.versions[v]; var val = data.versions[v];
/* Suppose our cache.json is at latest revision. It contains lot of versions which is not available in local cache.
Then, Remove the versions which is not in local cache from the list. Otherwise npm will always choose higher versions whcih is not available in our cache */ if( cacheJsonFileData.length && config.STRICT ){
/* TODO: If this feature causing problem, We can make this behaviour optional via some commandline-arguments/Env-variables */ /* Suppose our cache.json is at latest revision. It contains lot of versions which is not available in local cache.
if( cachedVersions.indexOf(v) == -1 ){ Then, Remove the versions which is not in local cache from the list. Otherwise npm will always choose higher versions whcih is not available in our cache */
delete data.versions[v]; if( cacheJsonFileData.indexOf(v) == -1 ){
return; delete data.versions[v];
return;
}
} }
var protocal = 'http://'; var protocal = 'http://';
if( val.dist.tarball.indexOf( 'https:' ) !== false ){ if( val.dist.tarball.indexOf( 'https:' ) !== false ){
protocal = 'https://'; protocal = 'https://';