halt-rate 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/express.d.mts +1 -1
- package/dist/adapters/express.d.ts +1 -1
- package/dist/adapters/fastify.d.mts +1 -1
- package/dist/adapters/fastify.d.ts +1 -1
- package/dist/adapters/graphql.d.mts +1 -1
- package/dist/adapters/graphql.d.ts +1 -1
- package/dist/adapters/hono.d.mts +1 -1
- package/dist/adapters/hono.d.ts +1 -1
- package/dist/adapters/next.d.mts +1 -1
- package/dist/adapters/next.d.ts +1 -1
- package/dist/adapters/next.js +6 -2
- package/dist/adapters/next.js.map +1 -1
- package/dist/adapters/next.mjs +6 -2
- package/dist/adapters/next.mjs.map +1 -1
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +6 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +6 -2
- package/dist/index.mjs.map +1 -1
- package/dist/{limiter-DqVVE0Kl.d.mts → limiter-C_Yf7DTg.d.mts} +2 -0
- package/dist/{limiter-DqVVE0Kl.d.ts → limiter-C_Yf7DTg.d.ts} +2 -0
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -28,6 +28,7 @@ function normalizePolicy(policy) {
|
|
|
28
28
|
algorithm: policy.algorithm ?? "token_bucket" /* TOKEN_BUCKET */,
|
|
29
29
|
keyStrategy: policy.keyStrategy ?? "ip" /* IP */,
|
|
30
30
|
burst: policy.burst ?? Math.floor(policy.limit * 1.2),
|
|
31
|
+
slidingPrecision: policy.slidingPrecision ?? 10,
|
|
31
32
|
cost: policy.cost ?? 1,
|
|
32
33
|
blockDuration: policy.blockDuration ?? void 0,
|
|
33
34
|
keyExtractor: policy.keyExtractor ?? void 0,
|
|
@@ -43,6 +44,9 @@ function normalizePolicy(policy) {
|
|
|
43
44
|
if (normalized.cost <= 0) {
|
|
44
45
|
throw new Error("cost must be positive");
|
|
45
46
|
}
|
|
47
|
+
if (!Number.isInteger(normalized.slidingPrecision) || normalized.slidingPrecision <= 0) {
|
|
48
|
+
throw new Error("slidingPrecision must be a positive integer");
|
|
49
|
+
}
|
|
46
50
|
if (normalized.burst < normalized.limit) {
|
|
47
51
|
throw new Error("burst must be >= limit");
|
|
48
52
|
}
|
|
@@ -484,7 +488,7 @@ var RateLimiter = class {
|
|
|
484
488
|
return decision2;
|
|
485
489
|
}
|
|
486
490
|
const store = this.store;
|
|
487
|
-
const cacheKey = `${policy.name}|${policy.algorithm}|${policy.limit}|${policy.window}|${policy.burst}`;
|
|
491
|
+
const cacheKey = `${policy.name}|${policy.algorithm}|${policy.limit}|${policy.window}|${policy.burst}|${policy.slidingPrecision}`;
|
|
488
492
|
let algorithm = this.algorithmCache.get(cacheKey);
|
|
489
493
|
if (!algorithm) {
|
|
490
494
|
if (policy.algorithm === "token_bucket" /* TOKEN_BUCKET */) {
|
|
@@ -492,7 +496,7 @@ var RateLimiter = class {
|
|
|
492
496
|
} else if (policy.algorithm === "fixed_window" /* FIXED_WINDOW */) {
|
|
493
497
|
algorithm = new FixedWindow(policy.limit, policy.window);
|
|
494
498
|
} else if (policy.algorithm === "sliding_window" /* SLIDING_WINDOW */) {
|
|
495
|
-
algorithm = new SlidingWindow(policy.limit, policy.window);
|
|
499
|
+
algorithm = new SlidingWindow(policy.limit, policy.window, policy.slidingPrecision);
|
|
496
500
|
} else if (policy.algorithm === "leaky_bucket" /* LEAKY_BUCKET */) {
|
|
497
501
|
const leakRate = policy.limit / policy.window;
|
|
498
502
|
algorithm = new LeakyBucket(policy.burst, leakRate, policy.window);
|