From a1362f49049a32fcc7d38b8479ae90655a384922 Mon Sep 17 00:00:00 2001 From: Ronan Jouchet Date: Mon, 4 Jun 2018 10:06:52 -0400 Subject: [PATCH] Add resetMs (milliseconds) result object --- Readme.md | 3 ++- index.js | 1 + test/index.js | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 346cca9..5af9cb7 100644 --- a/Readme.md +++ b/Readme.md @@ -60,7 +60,8 @@ limit.get(function(err, limit){ ## Result Object - `total` - `max` value - `remaining` - number of calls left in current `duration` without decreasing current `get` - - `reset` - time since epoch in seconds that the rate limiting period will end (or already ended) + - `reset` - time since epoch in seconds at which the rate limiting period will end (or already ended) + - `resetMs` - time since epoch in milliseconds at which the rate limiting period will end (or already ended) ## Options diff --git a/index.js b/index.js index f512664..d85e42c 100644 --- a/index.js +++ b/index.js @@ -77,6 +77,7 @@ Limiter.prototype.get = function (fn) { fn(null, { remaining: count < max ? max - count : 0, reset: Math.floor((oldest + duration * 1000) / 1000000), + resetMs: Math.floor((oldest + duration * 1000) / 1000), total: max }); }); diff --git a/test/index.js b/test/index.js index ca6a232..26d2fcc 100644 --- a/test/index.js +++ b/test/index.js @@ -70,6 +70,23 @@ var Limiter = require('..'), }); }); + describe('.resetMs', function() { + it('should represent the next reset time in UTC epoch milliseconds', function(done) { + var limit = new Limiter({ + max: 5, + duration: 60000, + id: 'something', + db: db + }); + limit.get(function(err, res) { + var left = res.resetMs - Date.now(); + Number.isInteger(left).should.be.true; + left.should.be.within(0, 60000); + done(); + }); + }); + }); + describe('when the limit is exceeded', function() { it('should retain .remaining at 0', function(done) { var limit = new Limiter({