Add counter for "browser" fallback usage

Helps with #63
This commit is contained in:
MICHAEL JACKSON 2017-11-15 07:30:05 -05:00
parent 3f2192f7d4
commit a9497166fe
2 changed files with 25 additions and 0 deletions

View File

@ -5,6 +5,7 @@ const createPackageURL = require('../utils/createPackageURL')
const createSearch = require('./utils/createSearch')
const getPackageInfo = require('./utils/getPackageInfo')
const getPackage = require('./utils/getPackage')
const incrementCounter = require('./utils/incrementCounter')
function getBasename(file) {
return path.basename(file, path.extname(file))
@ -118,6 +119,15 @@ function fetchFile(req, res, next) {
} else if (typeof req.packageConfig.browser === 'string') {
// Fall back to the "browser" field if declared (only support strings).
filename = req.packageConfig.browser
// Count which packages + versions are actually using this fallback
// so we can warn them when we deprecate this functionality.
// See https://github.com/unpkg/unpkg/issues/63
incrementCounter(
'package-json-browser-fallback',
req.packageSpec,
1
)
} else {
// Fall back to "main" or / (same as npm).
filename = req.packageConfig.main || '/'

View File

@ -0,0 +1,15 @@
const db = require('../../RedisClient')
function incrementCounter(counter, key, by) {
return new Promise((resolve, reject) => {
db.hincrby(counter, key, by, (error, value) => {
if (error) {
reject(error)
} else {
resolve(value)
}
})
})
}
module.exports = incrementCounter