@upstash/ratelimit 0.0.0-ci.b753fcb35cfe266add114c8197acdee3e4a5f74e-20241021134827 → 0.0.0-ci.b7f9ea418a9003054c47927b12f4b106261fb65b-20251023085337
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/index.d.mts +10 -20
- package/dist/index.d.ts +10 -20
- package/dist/index.js +5 -11
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +5 -11
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Aggregate } from '@upstash/core-analytics';
|
|
2
|
-
import { Redis as Redis$
|
|
2
|
+
import { Redis as Redis$2 } from '@upstash/redis';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* EphemeralCache is used to block certain identifiers right away in case they have already exceeded the ratelimit.
|
|
@@ -12,13 +12,13 @@ type EphemeralCache = {
|
|
|
12
12
|
blockUntil: (identifier: string, reset: number) => void;
|
|
13
13
|
set: (key: string, value: number) => void;
|
|
14
14
|
get: (key: string) => number | null;
|
|
15
|
-
incr: (key: string) => number;
|
|
15
|
+
incr: (key: string, incrementAmount?: number) => number;
|
|
16
16
|
pop: (key: string) => void;
|
|
17
17
|
empty: () => void;
|
|
18
18
|
size: () => number;
|
|
19
19
|
};
|
|
20
20
|
type RegionContext = {
|
|
21
|
-
redis: Redis;
|
|
21
|
+
redis: Redis$1;
|
|
22
22
|
cache?: EphemeralCache;
|
|
23
23
|
};
|
|
24
24
|
type MultiRegionContext = {
|
|
@@ -98,18 +98,7 @@ type LimitOptions = {
|
|
|
98
98
|
userAgent?: string;
|
|
99
99
|
country?: string;
|
|
100
100
|
};
|
|
101
|
-
|
|
102
|
-
* This is all we need from the redis sdk.
|
|
103
|
-
*/
|
|
104
|
-
type Redis = {
|
|
105
|
-
sadd: Redis$1["sadd"];
|
|
106
|
-
hset: Redis$1["hset"];
|
|
107
|
-
eval: Redis$1["eval"];
|
|
108
|
-
evalsha: Redis$1["evalsha"];
|
|
109
|
-
scriptLoad: Redis$1["scriptLoad"];
|
|
110
|
-
smismember: Redis$1["smismember"];
|
|
111
|
-
multi: Redis$1["multi"];
|
|
112
|
-
};
|
|
101
|
+
type Redis$1 = Redis$2;
|
|
113
102
|
|
|
114
103
|
type Geo = {
|
|
115
104
|
country?: string;
|
|
@@ -129,7 +118,7 @@ type Event = Geo & {
|
|
|
129
118
|
success: EventSuccess;
|
|
130
119
|
};
|
|
131
120
|
type AnalyticsConfig = {
|
|
132
|
-
redis: Redis;
|
|
121
|
+
redis: Redis$1;
|
|
133
122
|
prefix?: string;
|
|
134
123
|
};
|
|
135
124
|
/**
|
|
@@ -256,7 +245,7 @@ declare abstract class Ratelimit<TContext extends Context> {
|
|
|
256
245
|
protected readonly ctx: TContext;
|
|
257
246
|
protected readonly prefix: string;
|
|
258
247
|
protected readonly timeout: number;
|
|
259
|
-
protected readonly primaryRedis: Redis;
|
|
248
|
+
protected readonly primaryRedis: Redis$1;
|
|
260
249
|
protected readonly analytics?: Analytics;
|
|
261
250
|
protected readonly enableProtection: boolean;
|
|
262
251
|
protected readonly denyListThreshold: number;
|
|
@@ -381,7 +370,7 @@ type MultiRegionRatelimitConfig = {
|
|
|
381
370
|
* Instances of `@upstash/redis`
|
|
382
371
|
* @see https://github.com/upstash/upstash-redis#quick-start
|
|
383
372
|
*/
|
|
384
|
-
redis: Redis[];
|
|
373
|
+
redis: Redis$1[];
|
|
385
374
|
/**
|
|
386
375
|
* The ratelimiter function to use.
|
|
387
376
|
*
|
|
@@ -508,6 +497,7 @@ declare class MultiRegionRatelimit extends Ratelimit<MultiRegionContext> {
|
|
|
508
497
|
window: Duration): Algorithm<MultiRegionContext>;
|
|
509
498
|
}
|
|
510
499
|
|
|
500
|
+
type Redis = Pick<Redis$1, "evalsha" | "get" | "set">;
|
|
511
501
|
type RegionRatelimitConfig = {
|
|
512
502
|
/**
|
|
513
503
|
* Instance of `@upstash/redis`
|
|
@@ -740,7 +730,7 @@ declare class ThresholdError extends Error {
|
|
|
740
730
|
* passed, ttl is infferred from current time.
|
|
741
731
|
* @returns list of ips which are not in the deny list
|
|
742
732
|
*/
|
|
743
|
-
declare const updateIpDenyList: (redis: Redis, prefix: string, threshold: number, ttl?: number) => Promise<unknown[]>;
|
|
733
|
+
declare const updateIpDenyList: (redis: Redis$1, prefix: string, threshold: number, ttl?: number) => Promise<unknown[]>;
|
|
744
734
|
/**
|
|
745
735
|
* Disables the ip deny list by removing the ip deny list from the all
|
|
746
736
|
* set and removing the ip deny list. Also sets the status key to disabled
|
|
@@ -750,7 +740,7 @@ declare const updateIpDenyList: (redis: Redis, prefix: string, threshold: number
|
|
|
750
740
|
* @param prefix ratelimit prefix
|
|
751
741
|
* @returns
|
|
752
742
|
*/
|
|
753
|
-
declare const disableIpDenyList: (redis: Redis, prefix: string) => Promise<unknown[]>;
|
|
743
|
+
declare const disableIpDenyList: (redis: Redis$1, prefix: string) => Promise<unknown[]>;
|
|
754
744
|
|
|
755
745
|
type ipDenyList_ThresholdError = ThresholdError;
|
|
756
746
|
declare const ipDenyList_ThresholdError: typeof ThresholdError;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Aggregate } from '@upstash/core-analytics';
|
|
2
|
-
import { Redis as Redis$
|
|
2
|
+
import { Redis as Redis$2 } from '@upstash/redis';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* EphemeralCache is used to block certain identifiers right away in case they have already exceeded the ratelimit.
|
|
@@ -12,13 +12,13 @@ type EphemeralCache = {
|
|
|
12
12
|
blockUntil: (identifier: string, reset: number) => void;
|
|
13
13
|
set: (key: string, value: number) => void;
|
|
14
14
|
get: (key: string) => number | null;
|
|
15
|
-
incr: (key: string) => number;
|
|
15
|
+
incr: (key: string, incrementAmount?: number) => number;
|
|
16
16
|
pop: (key: string) => void;
|
|
17
17
|
empty: () => void;
|
|
18
18
|
size: () => number;
|
|
19
19
|
};
|
|
20
20
|
type RegionContext = {
|
|
21
|
-
redis: Redis;
|
|
21
|
+
redis: Redis$1;
|
|
22
22
|
cache?: EphemeralCache;
|
|
23
23
|
};
|
|
24
24
|
type MultiRegionContext = {
|
|
@@ -98,18 +98,7 @@ type LimitOptions = {
|
|
|
98
98
|
userAgent?: string;
|
|
99
99
|
country?: string;
|
|
100
100
|
};
|
|
101
|
-
|
|
102
|
-
* This is all we need from the redis sdk.
|
|
103
|
-
*/
|
|
104
|
-
type Redis = {
|
|
105
|
-
sadd: Redis$1["sadd"];
|
|
106
|
-
hset: Redis$1["hset"];
|
|
107
|
-
eval: Redis$1["eval"];
|
|
108
|
-
evalsha: Redis$1["evalsha"];
|
|
109
|
-
scriptLoad: Redis$1["scriptLoad"];
|
|
110
|
-
smismember: Redis$1["smismember"];
|
|
111
|
-
multi: Redis$1["multi"];
|
|
112
|
-
};
|
|
101
|
+
type Redis$1 = Redis$2;
|
|
113
102
|
|
|
114
103
|
type Geo = {
|
|
115
104
|
country?: string;
|
|
@@ -129,7 +118,7 @@ type Event = Geo & {
|
|
|
129
118
|
success: EventSuccess;
|
|
130
119
|
};
|
|
131
120
|
type AnalyticsConfig = {
|
|
132
|
-
redis: Redis;
|
|
121
|
+
redis: Redis$1;
|
|
133
122
|
prefix?: string;
|
|
134
123
|
};
|
|
135
124
|
/**
|
|
@@ -256,7 +245,7 @@ declare abstract class Ratelimit<TContext extends Context> {
|
|
|
256
245
|
protected readonly ctx: TContext;
|
|
257
246
|
protected readonly prefix: string;
|
|
258
247
|
protected readonly timeout: number;
|
|
259
|
-
protected readonly primaryRedis: Redis;
|
|
248
|
+
protected readonly primaryRedis: Redis$1;
|
|
260
249
|
protected readonly analytics?: Analytics;
|
|
261
250
|
protected readonly enableProtection: boolean;
|
|
262
251
|
protected readonly denyListThreshold: number;
|
|
@@ -381,7 +370,7 @@ type MultiRegionRatelimitConfig = {
|
|
|
381
370
|
* Instances of `@upstash/redis`
|
|
382
371
|
* @see https://github.com/upstash/upstash-redis#quick-start
|
|
383
372
|
*/
|
|
384
|
-
redis: Redis[];
|
|
373
|
+
redis: Redis$1[];
|
|
385
374
|
/**
|
|
386
375
|
* The ratelimiter function to use.
|
|
387
376
|
*
|
|
@@ -508,6 +497,7 @@ declare class MultiRegionRatelimit extends Ratelimit<MultiRegionContext> {
|
|
|
508
497
|
window: Duration): Algorithm<MultiRegionContext>;
|
|
509
498
|
}
|
|
510
499
|
|
|
500
|
+
type Redis = Pick<Redis$1, "evalsha" | "get" | "set">;
|
|
511
501
|
type RegionRatelimitConfig = {
|
|
512
502
|
/**
|
|
513
503
|
* Instance of `@upstash/redis`
|
|
@@ -740,7 +730,7 @@ declare class ThresholdError extends Error {
|
|
|
740
730
|
* passed, ttl is infferred from current time.
|
|
741
731
|
* @returns list of ips which are not in the deny list
|
|
742
732
|
*/
|
|
743
|
-
declare const updateIpDenyList: (redis: Redis, prefix: string, threshold: number, ttl?: number) => Promise<unknown[]>;
|
|
733
|
+
declare const updateIpDenyList: (redis: Redis$1, prefix: string, threshold: number, ttl?: number) => Promise<unknown[]>;
|
|
744
734
|
/**
|
|
745
735
|
* Disables the ip deny list by removing the ip deny list from the all
|
|
746
736
|
* set and removing the ip deny list. Also sets the status key to disabled
|
|
@@ -750,7 +740,7 @@ declare const updateIpDenyList: (redis: Redis, prefix: string, threshold: number
|
|
|
750
740
|
* @param prefix ratelimit prefix
|
|
751
741
|
* @returns
|
|
752
742
|
*/
|
|
753
|
-
declare const disableIpDenyList: (redis: Redis, prefix: string) => Promise<unknown[]>;
|
|
743
|
+
declare const disableIpDenyList: (redis: Redis$1, prefix: string) => Promise<unknown[]>;
|
|
754
744
|
|
|
755
745
|
type ipDenyList_ThresholdError = ThresholdError;
|
|
756
746
|
declare const ipDenyList_ThresholdError: typeof ThresholdError;
|
package/dist/index.js
CHANGED
|
@@ -115,9 +115,9 @@ var Cache = class {
|
|
|
115
115
|
get(key) {
|
|
116
116
|
return this.cache.get(key) || null;
|
|
117
117
|
}
|
|
118
|
-
incr(key) {
|
|
118
|
+
incr(key, incrementAmount = 1) {
|
|
119
119
|
let value = this.cache.get(key) ?? 0;
|
|
120
|
-
value +=
|
|
120
|
+
value += incrementAmount;
|
|
121
121
|
this.cache.set(key, value);
|
|
122
122
|
return value;
|
|
123
123
|
}
|
|
@@ -168,13 +168,7 @@ var safeEval = async (ctx, script, keys, args) => {
|
|
|
168
168
|
return await ctx.redis.evalsha(script.hash, keys, args);
|
|
169
169
|
} catch (error) {
|
|
170
170
|
if (`${error}`.includes("NOSCRIPT")) {
|
|
171
|
-
|
|
172
|
-
if (hash !== script.hash) {
|
|
173
|
-
console.warn(
|
|
174
|
-
"Upstash Ratelimit: Expected hash and the hash received from Redis are different. Ratelimit will work as usual but performance will be reduced."
|
|
175
|
-
);
|
|
176
|
-
}
|
|
177
|
-
return await ctx.redis.evalsha(hash, keys, args);
|
|
171
|
+
return await ctx.redis.eval(script.script, keys, args);
|
|
178
172
|
}
|
|
179
173
|
throw error;
|
|
180
174
|
}
|
|
@@ -925,7 +919,7 @@ var Ratelimit = class {
|
|
|
925
919
|
*/
|
|
926
920
|
getDefinedMembers = (identifier, req) => {
|
|
927
921
|
const members = [identifier, req?.ip, req?.userAgent, req?.country];
|
|
928
|
-
return members.filter(
|
|
922
|
+
return members.filter(Boolean);
|
|
929
923
|
};
|
|
930
924
|
};
|
|
931
925
|
|
|
@@ -1580,7 +1574,7 @@ var RegionRatelimit = class extends Ratelimit {
|
|
|
1580
1574
|
const incrementBy = rate ? Math.max(1, rate) : 1;
|
|
1581
1575
|
const hit = typeof ctx.cache.get(key) === "number";
|
|
1582
1576
|
if (hit) {
|
|
1583
|
-
const cachedTokensAfterUpdate = ctx.cache.incr(key);
|
|
1577
|
+
const cachedTokensAfterUpdate = ctx.cache.incr(key, incrementBy);
|
|
1584
1578
|
const success = cachedTokensAfterUpdate < tokens;
|
|
1585
1579
|
const pending = success ? safeEval(
|
|
1586
1580
|
ctx,
|