-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapp.js
130 lines (112 loc) · 3.64 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var express = require('express');
var Promise = require('bluebird');
var config = require( __dirname + '/./config' );
var utils = require( __dirname + '/./utils');
var NPM_PATH = config.NPM_PATH;
var REGISTRY_NAME = config.REGISTRY_NAME;
var ENABLE_NPM_FAILOVER = config.ENABLE_NPM_FAILOVER;
var fetchAndCacheMetadata = utils.fetchAndCacheMetadata;
var fetchAndCacheTarball = utils.fetchAndCacheTarball;
var patchData = utils.patchData;
var fileExists = utils.fileExists;
var readFile = utils.readFile;
var app = express();
app.use( function(req, res, next ){
res._log = {
method: req.method,
path: req.path,
cacheHit: 'Hit',
cacheFile: '',
};
res.on( 'finish', function(){
var log = this._log;
console.log( log.cacheHit, this.statusCode, log.method, log.path, ' => ', log.cacheFile );
});
// setTimeout( next, 1000 );
next();
});
app.get( '/:package', function( req, res, next ){
var packageName = req.params.package;
var cacheFile = [ NPM_PATH, REGISTRY_NAME, packageName, '.cache.json' ].join( '/' );
return fileExists( cacheFile )
.tap( function( isExists ){
if( !isExists ){
if ( !ENABLE_NPM_FAILOVER ) {
res._log.cacheHit = '!!!';
return Promise.reject( { status: 404 });
}
res._log.cacheHit = '---';
return fetchAndCacheMetadata( packageName, cacheFile );
}
})
.then( function( ){
res._log.cacheFile = cacheFile;
return readFile( cacheFile, 'utf-8' );
})
.then( function( cachedData ){
cachedData = JSON.parse( cachedData );
patchData( cachedData );
return res.send( cachedData );
})
.catch( next );
});
app.get( '/:package/-/:tarball', function( req, res, next ){
var packageName = req.params.package;
var version = req.params.tarball.match( new RegExp( packageName + '-(.*).tgz') )[1];
var packagePath = [ NPM_PATH , 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 );
}
})
.then( function( ){
res._log.cacheFile = packagePath;
return res.sendFile( packagePath );
})
.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
app.use(function(req, res, next) {
next({ status: 404 });
});
// error handlers
app.use(function(err, req, res, next) {
var message = err.message || err;
err.stack && console.log( err.stack );
res.status(err.status || 500);
// NPM registry returns empty objects for unknown packages
if( err.status == 404 ){
message = {};
}
res.send( message );
if( next ) { next(); }
});
module.exports = app;