Skip to content

Commit 8d2102a

Browse files
committed
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.
1 parent a157250 commit 8d2102a

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# npm-offline-registry
21
Supper simple NPM registry server for offline NPM install
32

43
# Usage
@@ -56,3 +55,14 @@ Please check config.js. All config values can be over-written by `environment-va
5655
If you set the ``ENABLE_NPM_FAILOVER`` config value to ``false`` then npm-offlin-registry will not attempt to
5756
contact the upstream NPM registry for unknown packages and instead return a 404 response, meaning you can use
5857
it as an alternative to the NPM registry behind a firewall / isolated from the internet.
58+
59+
# Enabling strict mode
60+
61+
If config value `STRICT` is set to true, while npm checks for the available versions of a given packages,
62+
registry server will reply with list of cached versions.
63+
In this case, if the version is not previously cached, npm-offline-install will fail with, error message `version not available`
64+
65+
example
66+
```bash
67+
env STRICT=true npm-offline-registry`
68+
```

config.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ var defaultConfig = {
33
NPM_PATH : process.env.HOME + '/.npm',
44
REGISTRY_NAME : 'registry.npmjs.org',
55
PORT: 8234,
6-
ENABLE_NPM_FAILOVER: true
6+
ENABLE_NPM_FAILOVER: true,
7+
STRICT: false
78
};
89

910
var config = {};
@@ -21,5 +22,8 @@ if( config.ENABLE_NPM_FAILOVER == 'false' ){
2122
config.ENABLE_NPM_FAILOVER = false;
2223
}
2324

25+
if( config.STRICT == 'true' ){
26+
config.STRICT = true;
27+
}
2428
module.exports = config;
2529

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "npm-offline-registry",
3-
"version": "0.0.7",
3+
"version": "0.0.8",
44
"description": "Super simple loca npm registry server for offline usage",
55
"main": "bin/www",
66
"directories": {

utils.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,26 @@ exports.readFile = readFile;
2929

3030
exports.patchData = function ( data ){
3131
/* Get the list of versions which is available in local cache */
32-
var cachedVersions = fs.readdirSync( path.join( NPM_PATH, data.name ) );
32+
var cacheJsonFile, cacheJsonFileData = [];
33+
34+
if( config.STRICT ){
35+
cacheJsonFile = path.join( NPM_PATH, data.name );
36+
cacheJsonFileData = fs.existsSync( cacheJsonFile ) ? fs.readdirSync( cacheJsonFile ) : [];
37+
}
38+
3339
Object.keys(data.versions).forEach( function( v ){
3440
var val = data.versions[v];
3541

36-
/* Suppose our cache.json is at latest revision. It contains lot of versions which is not available in local cache.
37-
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 */
38-
/* TODO: If this feature causing problem, We can make this behaviour optional via some commandline-arguments/Env-variables */
39-
if( cachedVersions.indexOf(v) == -1 ){
40-
delete data.versions[v];
41-
return;
42+
43+
if( cacheJsonFileData.length && config.STRICT ){
44+
/* Suppose our cache.json is at latest revision. It contains lot of versions which is not available in local cache.
45+
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 */
46+
if( cacheJsonFileData.indexOf(v) == -1 ){
47+
delete data.versions[v];
48+
return;
49+
}
4250
}
51+
4352
var protocal = 'http://';
4453
if( val.dist.tarball.indexOf( 'https:' ) !== false ){
4554
protocal = 'https://';

0 commit comments

Comments
 (0)