Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid example #30

Open
smff opened this issue Nov 14, 2016 · 2 comments
Open

Invalid example #30

smff opened this issue Nov 14, 2016 · 2 comments

Comments

@smff
Copy link

smff commented Nov 14, 2016

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

@westpole
Copy link

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants