unpkg/server/utils/getPackageInfo.js

56 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-02-18 02:00:56 +00:00
const createCache = require("./createCache");
const createMutex = require("./createMutex");
2018-05-21 22:44:00 +00:00
const fetchPackageInfo = require("./fetchPackageInfo");
const cache = createCache("packageInfo");
const notFound = "PackageNotFound";
2017-11-08 18:14:46 +00:00
const fetchMutex = createMutex((packageName, callback) => {
cache.get(packageName, (error, value) => {
if (error) {
callback(error);
} else if (value != null) {
callback(null, value === notFound ? null : value);
} else {
fetchPackageInfo(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.set(packageName, notFound, 300, () => {
callback(null, value);
});
} else {
// Cache valid package info for 1 minute.
cache.set(packageName, value, 60, () => {
callback(null, value);
});
}
},
error => {
// Do not cache errors.
cache.del(packageName, () => {
callback(error);
});
}
);
}
});
2018-02-18 02:00:56 +00:00
});
2018-05-21 22:44:00 +00:00
function getPackageInfo(packageName) {
return new Promise((resolve, reject) => {
fetchMutex(packageName, (error, value) => {
2018-05-21 22:44:00 +00:00
if (error) {
reject(error);
} else {
resolve(value);
2018-05-21 22:44:00 +00:00
}
});
2018-02-18 02:00:56 +00:00
});
}
2018-02-18 02:00:56 +00:00
module.exports = getPackageInfo;