Remove min-download checking
This commit is contained in:
parent
180e942aa9
commit
2d03ed9be6
|
@ -1,17 +0,0 @@
|
|||
require('isomorphic-fetch')
|
||||
|
||||
const NPMAPIURL = 'https://api.npmjs.org'
|
||||
|
||||
function getJSON(path) {
|
||||
return fetch(`${NPMAPIURL}${path}`, {
|
||||
headers: {
|
||||
Accept: 'application/json'
|
||||
}
|
||||
}).then(function (res) {
|
||||
return res.status === 404 ? null : res.json()
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getJSON
|
||||
}
|
|
@ -1,53 +0,0 @@
|
|||
const NPMAPI = require('./NPMAPI')
|
||||
const createCache = require('./createCache')
|
||||
const createMutex = require('./createMutex')
|
||||
|
||||
const NPMDownloadsCache = createCache('npmDownloads')
|
||||
|
||||
function fetchDailyDownloads(packageName) {
|
||||
console.log(`info: Fetching downloads for ${packageName}`)
|
||||
|
||||
return NPMAPI.getJSON(`/downloads/point/last-week/${packageName}`).then(function (data) {
|
||||
return data && Math.round(data.downloads / 7)
|
||||
})
|
||||
}
|
||||
|
||||
const PackageNotFound = 'PackageNotFound'
|
||||
|
||||
const fetchMutex = createMutex(function (packageName, callback) {
|
||||
fetchDailyDownloads(packageName).then(function (value) {
|
||||
if (value == null) {
|
||||
// Cache 404s for 5 minutes. This prevents us from making
|
||||
// unnecessary requests to the NPM API for bad package names.
|
||||
// In the worst case, a brand new package's downloads will be
|
||||
// available within 5 minutes.
|
||||
NPMDownloadsCache.set(packageName, PackageNotFound, 300, function () {
|
||||
callback(null, value)
|
||||
})
|
||||
} else {
|
||||
// Cache downloads for 1 minute.
|
||||
NPMDownloadsCache.set(packageName, value, 60, function () {
|
||||
callback(null, value)
|
||||
})
|
||||
}
|
||||
}, function (error) {
|
||||
// Do not cache errors.
|
||||
NPMDownloadsCache.del(packageName, function () {
|
||||
callback(error)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
function getDailyDownloads(packageName, callback) {
|
||||
NPMDownloadsCache.get(packageName, function (error, value) {
|
||||
if (error || value != null) {
|
||||
callback(error, value === PackageNotFound ? null : value)
|
||||
} else {
|
||||
fetchMutex(packageName, packageName, callback)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getDaily: getDailyDownloads
|
||||
}
|
|
@ -8,7 +8,6 @@ const morgan = require('morgan')
|
|||
const { fetchStats } = require('./cloudflare')
|
||||
|
||||
const checkBlacklist = require('./middleware/checkBlacklist')
|
||||
const checkMinDailyDownloads = require('./middleware/checkMinDailyDownloads')
|
||||
const parsePackageURL = require('./middleware/parsePackageURL')
|
||||
const fetchFile = require('./middleware/fetchFile')
|
||||
const serveFile = require('./middleware/serveFile')
|
||||
|
@ -19,15 +18,6 @@ const serveMetadata = require('./middleware/serveMetadata')
|
|||
*/
|
||||
const PackageBlacklist = require('./PackageBlacklist').blacklist
|
||||
|
||||
/**
|
||||
* The minimum number of times a package must be downloaded on
|
||||
* average in order to be available on the CDN. We need to set this
|
||||
* sufficiently high to avoid serving packages that are only ever
|
||||
* downloaded by bots.
|
||||
* See https://twitter.com/seldo/status/892840020377075712
|
||||
*/
|
||||
const MinDailyDownloads = 50
|
||||
|
||||
morgan.token('fwd', function (req) {
|
||||
return req.get('x-forwarded-for').replace(/\s/g, '')
|
||||
})
|
||||
|
@ -58,8 +48,8 @@ function sendHomePage(publicDir) {
|
|||
}
|
||||
|
||||
function errorHandler(err, req, res, next) {
|
||||
res.status(500).type('text').send('Internal Server Error')
|
||||
console.error(err.stack)
|
||||
res.status(500).type('text').send('Internal Server Error')
|
||||
next(err)
|
||||
}
|
||||
|
||||
|
@ -87,19 +77,13 @@ function createServer() {
|
|||
app.use('/_meta',
|
||||
parsePackageURL,
|
||||
checkBlacklist(PackageBlacklist),
|
||||
// checkMinDailyDownloads(MinDailyDownloads),
|
||||
fetchFile,
|
||||
serveMetadata
|
||||
)
|
||||
|
||||
app.use('/_stats',
|
||||
parsePackageURL
|
||||
)
|
||||
|
||||
app.use('/',
|
||||
parsePackageURL,
|
||||
checkBlacklist(PackageBlacklist),
|
||||
// checkMinDailyDownloads(MinDailyDownloads),
|
||||
fetchFile,
|
||||
serveFile
|
||||
)
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
const NPMDownloads = require('../NPMDownloads')
|
||||
|
||||
function checkMinDailyDownloads(minDailyDownloads) {
|
||||
return function (req, res, next) {
|
||||
NPMDownloads.getDaily(req.packageName, function (error, downloads) {
|
||||
if (error) {
|
||||
console.error(error)
|
||||
next() // Keep going; this error isn't critical.
|
||||
} else if (downloads == null) {
|
||||
res.status(404).type('text').send(`Cannot find package "${req.packageName}"`)
|
||||
} else if (downloads >= minDailyDownloads) {
|
||||
next()
|
||||
} else {
|
||||
res.status(404).type('text').send(`Cannot serve requests for package "${req.packageName}" because it has been downloaded on average only ${downloads} time${downloads > 1 ? 's' : ''} per day this week`)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = checkMinDailyDownloads
|
Loading…
Reference in New Issue