diff --git a/index.js b/index.js index d7cde10..74dc272 100644 --- a/index.js +++ b/index.js @@ -26,6 +26,8 @@ module.exports = function (app, db) { limit.remaining = opts.total } + var last = Number(limit.remaining) === 1; + // do not allow negative remaining limit.remaining = Math.max(Number(limit.remaining) - 1, 0) db.set(key, JSON.stringify(limit), 'PX', opts.expire, function (e) { @@ -34,7 +36,7 @@ module.exports = function (app, db) { res.set('X-RateLimit-Remaining', limit.remaining) } - if (limit.remaining) return next() + if (limit.remaining || last) return next() var after = (limit.reset - Date.now()) / 1000 diff --git a/tests/index.js b/tests/index.js index ee20ce4..86d040d 100644 --- a/tests/index.js +++ b/tests/index.js @@ -21,7 +21,7 @@ describe('rate-limiter', function () { }) it('should work', function (done) { - var map = [10, 9, 8, 7, 6, 5, 4, 3, 2] + var map = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] var clock = sinon.useFakeTimers() limiter({