async-ratelimiter
Advanced tools
Comparing version 1.1.3 to 1.1.4
@@ -5,2 +5,7 @@ # Change Log | ||
<a name="1.1.4"></a> | ||
## [1.1.4](https://github.com/microlinkhq/async-ratelimiter/compare/v1.1.3...v1.1.4) (2019-03-27) | ||
<a name="1.1.3"></a> | ||
@@ -7,0 +12,0 @@ ## 1.1.3 (2018-12-17) |
@@ -5,3 +5,3 @@ { | ||
"homepage": "https://documentup.com/microlinkhq/async-ratelimiter", | ||
"version": "1.1.3", | ||
"version": "1.1.4", | ||
"main": "src/index.js", | ||
@@ -17,2 +17,3 @@ "author": { | ||
"Ayan Yenbekbay <ayan.yenb@gmail.com>", | ||
"Nico Kaiser <nico@kaiser.me>", | ||
"amanda <amandalucis@gmail.com>" | ||
@@ -19,0 +20,0 @@ ], |
@@ -122,3 +122,9 @@ # async-ratelimiter | ||
##### decrease | ||
Type: `boolean` | ||
When set to `false`, the remaining number of calls is not decreased. This is useful for just reading the remaining calls without actually decreasing them. | ||
## License | ||
@@ -125,0 +131,0 @@ |
@@ -17,3 +17,8 @@ 'use strict' | ||
async get ({ id = this.id, max = this.max, duration = this.duration } = {}) { | ||
async get ({ | ||
id = this.id, | ||
max = this.max, | ||
duration = this.duration, | ||
decrease = true | ||
} = {}) { | ||
assert(id, 'id required') | ||
@@ -28,15 +33,14 @@ assert(max, 'max required') | ||
const res = await db | ||
.multi() | ||
.zremrangebyscore([key, 0, start]) | ||
.zcard([key]) | ||
.zadd([key, now, now]) | ||
.zrange([key, 0, 0]) | ||
.zrange([key, -max, -max]) | ||
.pexpire([key, duration]) | ||
.exec() | ||
const pipeline = db.multi() | ||
pipeline.zremrangebyscore([key, 0, start]) | ||
pipeline.zcard([key]) | ||
if (decrease) pipeline.zadd([key, now, now]) | ||
pipeline.zrange([key, 0, 0]) | ||
pipeline.zrange([key, -max, -max]) | ||
pipeline.pexpire([key, duration]) | ||
const res = await pipeline.exec() | ||
const count = parseInt(res[1][1]) | ||
const oldest = parseInt(res[3][1]) | ||
const oldestInRange = parseInt(res[4][1]) | ||
const oldest = parseInt(res[decrease ? 3 : 2][1]) | ||
const oldestInRange = parseInt(res[decrease ? 4 : 3][1]) | ||
const resetMicro = | ||
@@ -43,0 +47,0 @@ (Number.isNaN(oldestInRange) ? oldest : oldestInRange) + duration * 1000 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12740
79
135