2018-02-18 02:00:56 +00:00
|
|
|
require("isomorphic-fetch");
|
|
|
|
const createCache = require("./createCache");
|
|
|
|
const createMutex = require("./createMutex");
|
2017-08-11 03:43:20 +00:00
|
|
|
|
2018-02-18 02:00:56 +00:00
|
|
|
const RegistryURL =
|
|
|
|
process.env.NPM_REGISTRY_URL || "https://registry.npmjs.org";
|
2017-08-11 03:43:20 +00:00
|
|
|
|
2018-02-18 02:00:56 +00:00
|
|
|
const PackageInfoCache = createCache("packageInfo");
|
2017-08-17 05:47:24 +00:00
|
|
|
|
2017-08-11 03:43:20 +00:00
|
|
|
function fetchPackageInfo(packageName) {
|
2018-02-18 02:00:56 +00:00
|
|
|
console.log(`info: Fetching package info for ${packageName}`);
|
2017-08-11 03:43:20 +00:00
|
|
|
|
2018-02-18 02:00:56 +00:00
|
|
|
let encodedPackageName;
|
2017-11-25 21:25:01 +00:00
|
|
|
if (packageName.charAt(0) === "@") {
|
2018-02-18 02:00:56 +00:00
|
|
|
encodedPackageName = `@${encodeURIComponent(packageName.substring(1))}`;
|
2017-08-11 03:43:20 +00:00
|
|
|
} else {
|
2018-02-18 02:00:56 +00:00
|
|
|
encodedPackageName = encodeURIComponent(packageName);
|
2017-08-11 03:43:20 +00:00
|
|
|
}
|
|
|
|
|
2018-02-18 02:00:56 +00:00
|
|
|
const url = `${RegistryURL}/${encodedPackageName}`;
|
2017-08-11 03:43:20 +00:00
|
|
|
|
|
|
|
return fetch(url, {
|
2017-08-31 22:42:48 +00:00
|
|
|
headers: {
|
2017-11-25 21:25:01 +00:00
|
|
|
Accept: "application/json"
|
2017-08-31 22:42:48 +00:00
|
|
|
}
|
2017-11-08 18:14:46 +00:00
|
|
|
}).then(res => {
|
2018-02-18 02:00:56 +00:00
|
|
|
return res.status === 404 ? null : res.json();
|
|
|
|
});
|
2017-08-11 03:43:20 +00:00
|
|
|
}
|
|
|
|
|
2018-02-18 02:00:56 +00:00
|
|
|
const PackageNotFound = "PackageNotFound";
|
2017-08-11 03:43:20 +00:00
|
|
|
|
|
|
|
// This mutex prevents multiple concurrent requests to
|
|
|
|
// the registry for the same package info.
|
2017-11-08 18:14:46 +00:00
|
|
|
const fetchMutex = createMutex((packageName, callback) => {
|
2017-11-08 16:57:15 +00:00
|
|
|
fetchPackageInfo(packageName).then(
|
|
|
|
function(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.
|
|
|
|
PackageInfoCache.set(packageName, PackageNotFound, 300, function() {
|
2018-02-18 02:00:56 +00:00
|
|
|
callback(null, value);
|
|
|
|
});
|
2017-11-08 16:57:15 +00:00
|
|
|
} else {
|
|
|
|
// Cache valid package info for 1 minute.
|
|
|
|
PackageInfoCache.set(packageName, value, 60, function() {
|
2018-02-18 02:00:56 +00:00
|
|
|
callback(null, value);
|
|
|
|
});
|
2017-11-08 16:57:15 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
function(error) {
|
|
|
|
// Do not cache errors.
|
|
|
|
PackageInfoCache.del(packageName, function() {
|
2018-02-18 02:00:56 +00:00
|
|
|
callback(error);
|
|
|
|
});
|
2017-08-11 03:43:20 +00:00
|
|
|
}
|
2018-02-18 02:00:56 +00:00
|
|
|
);
|
|
|
|
});
|
2017-08-11 03:43:20 +00:00
|
|
|
|
|
|
|
function getPackageInfo(packageName, callback) {
|
2017-11-08 16:57:15 +00:00
|
|
|
PackageInfoCache.get(packageName, function(error, value) {
|
2017-08-17 05:47:24 +00:00
|
|
|
if (error || value != null) {
|
2018-02-18 02:00:56 +00:00
|
|
|
callback(error, value === PackageNotFound ? null : value);
|
2017-08-11 03:43:20 +00:00
|
|
|
} else {
|
2018-02-18 02:00:56 +00:00
|
|
|
fetchMutex(packageName, packageName, callback);
|
2017-08-11 03:43:20 +00:00
|
|
|
}
|
2018-02-18 02:00:56 +00:00
|
|
|
});
|
2017-08-11 03:43:20 +00:00
|
|
|
}
|
|
|
|
|
2018-02-18 02:00:56 +00:00
|
|
|
module.exports = getPackageInfo;
|