2018-07-16 17:17:00 +00:00
|
|
|
const cache = require("./cache");
|
2018-07-15 00:41:58 +00:00
|
|
|
const fetchNpmPackageInfo = require("./fetchNpmPackageInfo");
|
2017-08-11 03:43:20 +00:00
|
|
|
|
2018-09-04 14:39:45 +00:00
|
|
|
const notFound = 0;
|
2017-08-11 03:43:20 +00:00
|
|
|
|
2018-07-16 19:05:02 +00:00
|
|
|
function getNpmPackageInfo(packageName) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const key = `npmPackageInfo-${packageName}`;
|
2018-07-16 17:17:00 +00:00
|
|
|
|
2018-07-16 19:05:02 +00:00
|
|
|
cache.get(key, (error, value) => {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
} else if (value != null) {
|
|
|
|
resolve(value === notFound ? null : JSON.parse(value));
|
|
|
|
} else {
|
|
|
|
fetchNpmPackageInfo(packageName).then(value => {
|
2018-05-23 01:16:10 +00:00
|
|
|
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.
|
2018-07-16 19:05:02 +00:00
|
|
|
cache.setex(key, 300, notFound);
|
2018-10-19 00:41:10 +00:00
|
|
|
resolve(null);
|
2018-05-23 01:16:10 +00:00
|
|
|
} else {
|
2018-07-16 17:17:00 +00:00
|
|
|
// Cache valid package info for 1 minute. In the worst case,
|
|
|
|
// new versions won't be available for 1 minute.
|
2018-10-19 00:41:10 +00:00
|
|
|
cache.setex(key, 60, JSON.stringify(value));
|
|
|
|
resolve(value);
|
2018-05-23 01:16:10 +00:00
|
|
|
}
|
2018-07-16 19:05:02 +00:00
|
|
|
}, reject);
|
2018-05-21 22:44:00 +00:00
|
|
|
}
|
|
|
|
});
|
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;
|