From 8d2102aacdb5913819eed2bfc418a24053f1c9b1 Mon Sep 17 00:00:00 2001 From: "Harish.K" Date: Sun, 27 Nov 2016 17:52:45 +0530 Subject: [PATCH] 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. --- README.md | 12 +++++++++++- config.js | 6 +++++- package.json | 2 +- utils.js | 23 ++++++++++++++++------- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 6537718..3d07b96 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,3 @@ -# npm-offline-registry Supper simple NPM registry server for offline NPM install # 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 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. + +# 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` +``` diff --git a/config.js b/config.js index 496ca3d..5895935 100644 --- a/config.js +++ b/config.js @@ -3,7 +3,8 @@ var defaultConfig = { NPM_PATH : process.env.HOME + '/.npm', REGISTRY_NAME : 'registry.npmjs.org', PORT: 8234, - ENABLE_NPM_FAILOVER: true + ENABLE_NPM_FAILOVER: true, + STRICT: false }; var config = {}; @@ -21,5 +22,8 @@ if( config.ENABLE_NPM_FAILOVER == 'false' ){ config.ENABLE_NPM_FAILOVER = false; } +if( config.STRICT == 'true' ){ + config.STRICT = true; +} module.exports = config; diff --git a/package.json b/package.json index ace038a..33947d4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "npm-offline-registry", - "version": "0.0.7", + "version": "0.0.8", "description": "Super simple loca npm registry server for offline usage", "main": "bin/www", "directories": { diff --git a/utils.js b/utils.js index f5be964..9137248 100644 --- a/utils.js +++ b/utils.js @@ -29,17 +29,26 @@ exports.readFile = readFile; exports.patchData = function ( data ){ /* 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 ){ 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 */ - /* TODO: If this feature causing problem, We can make this behaviour optional via some commandline-arguments/Env-variables */ - if( cachedVersions.indexOf(v) == -1 ){ - delete data.versions[v]; - return; + + if( cacheJsonFileData.length && config.STRICT ){ + /* 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.indexOf(v) == -1 ){ + delete data.versions[v]; + return; + } } + var protocal = 'http://'; if( val.dist.tarball.indexOf( 'https:' ) !== false ){ protocal = 'https://';