express-limiter
express-limiter copied to clipboard
Invalid example
this example not works: // with a function for dynamic-ness limiter({ lookup: function(req, res, opts, next) { if (validApiKey(req.query.api_key)) { opts.lookup = 'query.api_key' opts.total = 100 } else { opts.lookup = 'connection.remoteAddress' opts.total = 10 } return next() } })
app.use('/api', limiter({ lookup: function(req, res, opts, next) { opts.lookup = 'connection.remoteAddress'; opts.total = 1000; return next(); }, })); Got TypeError: opts.lookup is not a function
Well, lookup is a property of the opts object. And application reassign that property to the string later in the code (opts.lookup = 'connection.remoteAddress';). I suggest, as a quick patch, to cache an initial function (opts.lookup) and reuse it in line 53 (I'm using v1.6.0)
if (typeof(opts.lookup) === 'function') {
const _cachedLookup = opts.lookup; // cache provided function
middleware = function (middleware, req, res, next) {
return _cachedLookup(req, res, opts, function () { // reuse cached function
return middleware(req, res, next)
})
}.bind(this, middleware)
}
Hi @ded Could you cut new release version as current version v1.6.0 (that available through NPM) is missing fix (made by @vamonte ) for described issue? Thank you.