mirror of
https://github.com/harish2704/npm-offline-registry.git
synced 2026-09-10 14:31:09 +00:00
Compare commits
3
Commits
0.0.7
..
7a774fee6a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7a774fee6a | ||
|
|
2afb9b6877 | ||
|
|
8d2102aacd |
@@ -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`
|
||||||
|
```
|
||||||
|
|||||||
@@ -81,6 +81,30 @@ app.get( '/:package/-/:tarball', function( req, res, next ){
|
|||||||
.catch( next );
|
.catch( next );
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.get( '/:scope/:package/-/:tarball', function( req, res, next ){
|
||||||
|
var scopeName = req.params.scope;
|
||||||
|
var packageName = req.params.package;
|
||||||
|
var version = req.params.tarball.match( new RegExp( packageName + '-(.*).tgz') )[1];
|
||||||
|
var packagePath = [ NPM_PATH , scopeName + "/" + packageName, version, 'package.tgz'].join( '/' );
|
||||||
|
|
||||||
|
fileExists( packagePath )
|
||||||
|
.tap( function( isExists ){
|
||||||
|
if( !isExists ){
|
||||||
|
if ( !ENABLE_NPM_FAILOVER ) {
|
||||||
|
res._log.cacheHit = '!!!';
|
||||||
|
return Promise.reject( { status: 404 });
|
||||||
|
}
|
||||||
|
res._log.cacheHit = '---';
|
||||||
|
return fetchAndCacheTarball( packageName, version, packagePath, scopeName );
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then( function( ){
|
||||||
|
res._log.cacheFile = packagePath;
|
||||||
|
return res.sendFile( packagePath );
|
||||||
|
})
|
||||||
|
.catch( next );
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
// catch 404 and forward to error handler
|
// catch 404 and forward to error handler
|
||||||
app.use(function(req, res, next) {
|
app.use(function(req, res, next) {
|
||||||
|
|||||||
@@ -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
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "npm-offline-registry",
|
"name": "npm-offline-registry",
|
||||||
"version": "0.0.7",
|
"version": "0.0.9",
|
||||||
"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": {
|
||||||
|
|||||||
@@ -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://';
|
||||||
@@ -76,10 +85,13 @@ exports.fetchAndCacheMetadata = function ( packageName, cacheFile ){
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.fetchAndCacheTarball = function ( packageName, version, tarballPath ){
|
exports.fetchAndCacheTarball = function ( packageName, version, tarballPath, scopeName ){
|
||||||
packageName = encodePackageName( packageName );
|
packageName = encodePackageName( packageName );
|
||||||
|
|
||||||
var tarballUrl = 'http://' + REGISTRY_NAME + '/' + packageName + '/-/' + packageName + '-' + version + '.tgz';
|
var tarballUrl = 'http://' + REGISTRY_NAME + '/' + packageName + '/-/' + packageName + '-' + version + '.tgz';
|
||||||
|
if (scopeName) {
|
||||||
|
tarballUrl = 'http://' + REGISTRY_NAME + '/' + scopeName + '/' + packageName + '/-/' + packageName + '-' + version + '.tgz';
|
||||||
|
}
|
||||||
var packageTarballDir = path.dirname( tarballPath );
|
var packageTarballDir = path.dirname( tarballPath );
|
||||||
|
|
||||||
return exec( fetchAndCacheTarballCmd, {
|
return exec( fetchAndCacheTarballCmd, {
|
||||||
|
|||||||
Reference in New Issue
Block a user