express-limiter icon indicating copy to clipboard operation
express-limiter copied to clipboard

Invalid example

Open smff opened this issue 8 years ago • 2 comments

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

smff avatar Nov 14 '16 10:11 smff

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)
    }

westpole avatar Mar 29 '17 15:03 westpole

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.

westpole avatar Mar 30 '17 15:03 westpole