Prettify everything

This commit is contained in:
MICHAEL JACKSON
2018-02-17 18:00:56 -08:00
parent d6f2bc089a
commit 2e1f09e913
58 changed files with 1061 additions and 932 deletions

View File

@ -1,33 +1,34 @@
require("isomorphic-fetch")
const createCache = require("./createCache")
const createMutex = require("./createMutex")
require("isomorphic-fetch");
const createCache = require("./createCache");
const createMutex = require("./createMutex");
const RegistryURL = process.env.NPM_REGISTRY_URL || "https://registry.npmjs.org"
const RegistryURL =
process.env.NPM_REGISTRY_URL || "https://registry.npmjs.org";
const PackageInfoCache = createCache("packageInfo")
const PackageInfoCache = createCache("packageInfo");
function fetchPackageInfo(packageName) {
console.log(`info: Fetching package info for ${packageName}`)
console.log(`info: Fetching package info for ${packageName}`);
let encodedPackageName
let encodedPackageName;
if (packageName.charAt(0) === "@") {
encodedPackageName = `@${encodeURIComponent(packageName.substring(1))}`
encodedPackageName = `@${encodeURIComponent(packageName.substring(1))}`;
} else {
encodedPackageName = encodeURIComponent(packageName)
encodedPackageName = encodeURIComponent(packageName);
}
const url = `${RegistryURL}/${encodedPackageName}`
const url = `${RegistryURL}/${encodedPackageName}`;
return fetch(url, {
headers: {
Accept: "application/json"
}
}).then(res => {
return res.status === 404 ? null : res.json()
})
return res.status === 404 ? null : res.json();
});
}
const PackageNotFound = "PackageNotFound"
const PackageNotFound = "PackageNotFound";
// This mutex prevents multiple concurrent requests to
// the registry for the same package info.
@ -40,32 +41,32 @@ const fetchMutex = createMutex((packageName, callback) => {
// In the worst case, a brand new package's info will be
// available within 5 minutes.
PackageInfoCache.set(packageName, PackageNotFound, 300, function() {
callback(null, value)
})
callback(null, value);
});
} else {
// Cache valid package info for 1 minute.
PackageInfoCache.set(packageName, value, 60, function() {
callback(null, value)
})
callback(null, value);
});
}
},
function(error) {
// Do not cache errors.
PackageInfoCache.del(packageName, function() {
callback(error)
})
callback(error);
});
}
)
})
);
});
function getPackageInfo(packageName, callback) {
PackageInfoCache.get(packageName, function(error, value) {
if (error || value != null) {
callback(error, value === PackageNotFound ? null : value)
callback(error, value === PackageNotFound ? null : value);
} else {
fetchMutex(packageName, packageName, callback)
fetchMutex(packageName, packageName, callback);
}
})
});
}
module.exports = getPackageInfo
module.exports = getPackageInfo;