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

Is mongodb supoorted? #28

Open
cookie-ag opened this issue Oct 7, 2016 · 4 comments
Open

Is mongodb supoorted? #28

cookie-ag opened this issue Oct 7, 2016 · 4 comments

Comments

@cookie-ag
Copy link

I tired to change the client to mongo and it failed. Has anyone used it?

Appreciate help. Thanks

@v-kat
Copy link

v-kat commented Oct 7, 2016

Modify the db calls in the module and/or submit a PR for a wrapper for calls...

@cookie-ag
Copy link
Author

@IroG I would try to do this by end of the month. I think it would be useful for many people.

@MrSpark2591
Copy link

I have changed db calls to mongo collection queries and that works great for me.
If we want this integrated in same package i can submit PR with detailed document and changes.
But i am worried about that it would not be backward compatible. I need help to figure out about backward compatibility .

module.exports = function(app, collection) {
  collection.ensureIndex({ 'reset': 1 }, { 'expireAfterSeconds': 0 })
  return function(opts) {
    var middleware = function(req, res, next) {
      if (opts.whitelist && opts.whitelist(req)) return next()
      opts.lookup = Array.isArray(opts.lookup) ? opts.lookup : [opts.lookup]
      opts.onRateLimited = typeof opts.onRateLimited === 'function' ? opts.onRateLimited : function(req, res, next) {
        res.status(429).send({ 'msg': 'Rate limit exceeded' })
      }
      var lookups = opts.lookup.map(function(item) {
        return item + ':' + item.split('.').reduce(function(prev, cur) {
          return prev[cur]
        }, req)
      }).join(':')
      var path = opts.path || req.path
      var method = (opts.method || req.method).toLowerCase()
      var key = 'ratelimit:' + path + ':' + method + ':' + lookups
      collection.findOne({ key: key }, function(err, limit) {
        if (err && opts.ignoreErrors) return next()
        var now = Date.now()
        limit = limit ? limit : {
          total: opts.total,
          remaining: opts.total,
          reset: new Date(now + opts.expire)
        }

        if (now > limit.reset) {
          limit.reset = new Date(now + opts.expire)
          limit.remaining = opts.total
        }

        // do not allow negative remaining
        limit.remaining = Math.max(Number(limit.remaining) - 1, -1)
        collection.update({ key: key }, { $set: { total: limit.total, remaining: limit.remaining, reset: limit.reset } }, { 'upsert': true }, function(e) {
          if (!opts.skipHeaders) {
            res.set('X-RateLimit-Limit', limit.total)
            res.set('X-RateLimit-Reset', Math.ceil(limit.reset / 1000)) // UTC epoch seconds
            res.set('X-RateLimit-Remaining', Math.max(limit.remaining, 0))
          }

          if (limit.remaining >= 0) return next()

          var after = (limit.reset - Date.now()) / 1000
          if (!opts.skipHeaders) res.set('Retry-After', after)

          opts.onRateLimited(req, res, next)
        })

      })
    }
    if (typeof(opts.lookup) === 'function') {
      var callableLookup = opts.lookup;
      middleware = function(middleware, req, res, next) {
        return callableLookup(req, res, opts, function() {
          return middleware(req, res, next)
        })
      }.bind(this, middleware)
    }
    if (opts.method && opts.path) app[opts.method](opts.path, middleware)
    return middleware
  }
};

@MrSpark2591
Copy link

I have created pull request for supporting it.
Here is the PR

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

3 participants