2018-12-17 17:38:05 +00:00
|
|
|
const cache = require('./cache');
|
|
|
|
const fetchNpmPackageInfo = require('./fetchNpmPackageInfo');
|
2017-08-11 03:43:20 +00:00
|
|
|
|
2018-12-17 23:57:44 +00:00
|
|
|
const notFound = '';
|
2017-08-11 03:43:20 +00:00
|
|
|
|
2018-12-17 22:02:59 +00:00
|
|
|
function cleanPackageInfo(packageInfo) {
|
|
|
|
return {
|
|
|
|
versions: Object.keys(packageInfo.versions).reduce((memo, key) => {
|
2018-12-20 23:09:06 +00:00
|
|
|
memo[key] = packageInfo.versions[key];
|
2018-12-17 22:02:59 +00:00
|
|
|
return memo;
|
|
|
|
}, {}),
|
|
|
|
'dist-tags': packageInfo['dist-tags']
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-07-16 19:05:02 +00:00
|
|
|
function getNpmPackageInfo(packageName) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const key = `npmPackageInfo-${packageName}`;
|
2018-12-17 23:57:44 +00:00
|
|
|
const value = cache.get(key);
|
2018-07-16 17:17:00 +00:00
|
|
|
|
2018-12-17 23:57:44 +00:00
|
|
|
if (value != null) {
|
|
|
|
resolve(value === notFound ? null : JSON.parse(value));
|
|
|
|
} else {
|
|
|
|
fetchNpmPackageInfo(packageName).then(value => {
|
|
|
|
if (value == null) {
|
|
|
|
// Cache 404s for 5 minutes. This prevents us from making
|
|
|
|
// unnecessary requests to the registry for bad package names.
|
|
|
|
// In the worst case, a brand new package's info will be
|
|
|
|
// available within 5 minutes.
|
|
|
|
cache.setex(key, 300, notFound);
|
|
|
|
resolve(null);
|
|
|
|
} else {
|
|
|
|
value = cleanPackageInfo(value);
|
2018-12-17 22:02:59 +00:00
|
|
|
|
2018-12-17 23:57:44 +00:00
|
|
|
// Cache valid package info for 1 minute. In the worst case,
|
|
|
|
// new versions won't be available for 1 minute.
|
|
|
|
cache.setex(key, 60, JSON.stringify(value));
|
|
|
|
resolve(value);
|
|
|
|
}
|
|
|
|
}, reject);
|
|
|
|
}
|
2018-02-18 02:00:56 +00:00
|
|
|
});
|
2017-08-11 03:43:20 +00:00
|
|
|
}
|
|
|
|
|
2018-07-15 00:41:58 +00:00
|
|
|
module.exports = getNpmPackageInfo;
|