Experimental port to Firebase hosting

This commit is contained in:
Michael Jackson
2019-01-05 16:50:05 -08:00
parent e4d6df255e
commit 31e7d3865a
300 changed files with 129300 additions and 5817 deletions

View File

@ -1,19 +1,21 @@
const cache = require('./cache');
const fetchNpmPackageInfo = require('./fetchNpmPackageInfo');
import LRUCache from 'lru-cache';
import fetchNpmPackageInfo from './fetchNpmPackageInfo';
const maxMegabytes = 40; // Cap the cache at 40 MB
const maxLength = maxMegabytes * 1024 * 1024;
const oneSecond = 1000;
const oneMinute = 60 * oneSecond;
const cache = new LRUCache({
max: maxLength,
maxAge: oneMinute,
length: Buffer.byteLength
});
const notFound = '';
function cleanPackageInfo(packageInfo) {
return {
versions: Object.keys(packageInfo.versions).reduce((memo, key) => {
memo[key] = packageInfo.versions[key];
return memo;
}, {}),
'dist-tags': packageInfo['dist-tags']
};
}
function getNpmPackageInfo(packageName) {
export default function getNpmPackageInfo(packageName) {
return new Promise((resolve, reject) => {
const key = `npmPackageInfo-${packageName}`;
const value = cache.get(key);
@ -21,25 +23,21 @@ function getNpmPackageInfo(packageName) {
if (value != null) {
resolve(value === notFound ? null : JSON.parse(value));
} else {
fetchNpmPackageInfo(packageName).then(value => {
if (value == null) {
fetchNpmPackageInfo(packageName).then(info => {
if (info == 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);
cache.set(key, notFound, oneMinute * 5);
resolve(null);
} else {
value = cleanPackageInfo(value);
// 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);
cache.set(key, JSON.stringify(info), oneMinute);
resolve(info);
}
}, reject);
}
});
}
module.exports = getNpmPackageInfo;