@upstash/ratelimit 0.1.5-next.1 → 0.1.5
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/esm/multi.js +2 -2
- package/esm/ratelimit.js +3 -3
- package/esm/single.js +1 -1
- package/package.json +2 -2
- package/script/multi.js +2 -2
- package/script/ratelimit.js +3 -3
- package/script/single.js +1 -1
- package/types/cache.d.ts +2 -2
- package/types/multi.d.ts +1 -1
- package/types/ratelimit.d.ts +1 -1
- package/types/single.d.ts +1 -1
- package/types/types.d.ts +5 -5
package/esm/multi.js
CHANGED
|
@@ -26,8 +26,8 @@ export class MultiRegionRatelimit extends Ratelimit {
|
|
|
26
26
|
limiter: config.limiter,
|
|
27
27
|
ctx: {
|
|
28
28
|
redis: config.redis,
|
|
29
|
-
cache: config.
|
|
30
|
-
? new Cache(config.
|
|
29
|
+
cache: config.ephemeralCache
|
|
30
|
+
? new Cache(config.ephemeralCache)
|
|
31
31
|
: undefined,
|
|
32
32
|
},
|
|
33
33
|
});
|
package/esm/ratelimit.js
CHANGED
|
@@ -126,10 +126,10 @@ export class Ratelimit {
|
|
|
126
126
|
this.ctx = config.ctx;
|
|
127
127
|
this.limiter = config.limiter;
|
|
128
128
|
this.prefix = config.prefix ?? "@upstash/ratelimit";
|
|
129
|
-
if (config.
|
|
130
|
-
this.ctx.cache = new Cache(config.
|
|
129
|
+
if (config.ephemeralCache instanceof Map) {
|
|
130
|
+
this.ctx.cache = new Cache(config.ephemeralCache);
|
|
131
131
|
}
|
|
132
|
-
else if (typeof config.
|
|
132
|
+
else if (typeof config.ephemeralCache === "undefined") {
|
|
133
133
|
this.ctx.cache = new Cache(new Map());
|
|
134
134
|
}
|
|
135
135
|
}
|
package/esm/single.js
CHANGED
package/package.json
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
"main": "./script/mod.js",
|
|
4
4
|
"types": "./types/mod.d.ts",
|
|
5
5
|
"name": "@upstash/ratelimit",
|
|
6
|
+
"version": "v0.1.5",
|
|
6
7
|
"description": "A serverless ratelimiter built on top of Upstash REST API.",
|
|
7
8
|
"repository": {
|
|
8
9
|
"type": "git",
|
|
@@ -46,6 +47,5 @@
|
|
|
46
47
|
"require": "./script/mod.js",
|
|
47
48
|
"types": "./types/mod.d.ts"
|
|
48
49
|
}
|
|
49
|
-
}
|
|
50
|
-
"version": "0.1.5-next.1"
|
|
50
|
+
}
|
|
51
51
|
}
|
package/script/multi.js
CHANGED
|
@@ -29,8 +29,8 @@ class MultiRegionRatelimit extends ratelimit_js_1.Ratelimit {
|
|
|
29
29
|
limiter: config.limiter,
|
|
30
30
|
ctx: {
|
|
31
31
|
redis: config.redis,
|
|
32
|
-
cache: config.
|
|
33
|
-
? new cache_js_1.Cache(config.
|
|
32
|
+
cache: config.ephemeralCache
|
|
33
|
+
? new cache_js_1.Cache(config.ephemeralCache)
|
|
34
34
|
: undefined,
|
|
35
35
|
},
|
|
36
36
|
});
|
package/script/ratelimit.js
CHANGED
|
@@ -129,10 +129,10 @@ class Ratelimit {
|
|
|
129
129
|
this.ctx = config.ctx;
|
|
130
130
|
this.limiter = config.limiter;
|
|
131
131
|
this.prefix = config.prefix ?? "@upstash/ratelimit";
|
|
132
|
-
if (config.
|
|
133
|
-
this.ctx.cache = new cache_js_1.Cache(config.
|
|
132
|
+
if (config.ephemeralCache instanceof Map) {
|
|
133
|
+
this.ctx.cache = new cache_js_1.Cache(config.ephemeralCache);
|
|
134
134
|
}
|
|
135
|
-
else if (typeof config.
|
|
135
|
+
else if (typeof config.ephemeralCache === "undefined") {
|
|
136
136
|
this.ctx.cache = new cache_js_1.Cache(new Map());
|
|
137
137
|
}
|
|
138
138
|
}
|
package/script/single.js
CHANGED
package/types/cache.d.ts
CHANGED
package/types/multi.d.ts
CHANGED
|
@@ -38,7 +38,7 @@ export declare type MultiRegionRatelimitConfig = {
|
|
|
38
38
|
* If left undefined, a map is created automatically, but it can only work
|
|
39
39
|
* if the map or th ratelimit instance is created outside your serverless function handler.
|
|
40
40
|
*/
|
|
41
|
-
|
|
41
|
+
ephemeralCache?: Map<string, number> | false;
|
|
42
42
|
};
|
|
43
43
|
/**
|
|
44
44
|
* Ratelimiter using serverless redis from https://upstash.com/
|
package/types/ratelimit.d.ts
CHANGED
|
@@ -34,7 +34,7 @@ export declare type RatelimitConfig<TContext> = {
|
|
|
34
34
|
* If left undefined, a map is created automatically, but it can only work
|
|
35
35
|
* if the map or the ratelimit instance is created outside your serverless function handler.
|
|
36
36
|
*/
|
|
37
|
-
|
|
37
|
+
ephemeralCache?: Map<string, number> | false;
|
|
38
38
|
};
|
|
39
39
|
/**
|
|
40
40
|
* Ratelimiter using serverless redis from https://upstash.com/
|
package/types/single.d.ts
CHANGED
|
@@ -41,7 +41,7 @@ export declare type RegionRatelimitConfig = {
|
|
|
41
41
|
* If left undefined, a map is created automatically, but it can only work
|
|
42
42
|
* if the map or the ratelimit instance is created outside your serverless function handler.
|
|
43
43
|
*/
|
|
44
|
-
|
|
44
|
+
ephemeralCache?: Map<string, number> | false;
|
|
45
45
|
};
|
|
46
46
|
/**
|
|
47
47
|
* Ratelimiter using serverless redis from https://upstash.com/
|
package/types/types.d.ts
CHANGED
|
@@ -3,9 +3,9 @@ export interface Redis {
|
|
|
3
3
|
sadd: (key: string, ...members: string[]) => Promise<number>;
|
|
4
4
|
}
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* EphemeralCache is used to block certain identifiers right away in case they have already exceedd the ratelimit.
|
|
7
7
|
*/
|
|
8
|
-
export interface
|
|
8
|
+
export interface EphemeralCache {
|
|
9
9
|
isBlocked: (identifier: string) => {
|
|
10
10
|
blocked: boolean;
|
|
11
11
|
reset: number;
|
|
@@ -14,11 +14,11 @@ export interface EphermeralCache {
|
|
|
14
14
|
}
|
|
15
15
|
export declare type RegionContext = {
|
|
16
16
|
redis: Redis;
|
|
17
|
-
cache?:
|
|
17
|
+
cache?: EphemeralCache;
|
|
18
18
|
};
|
|
19
19
|
export declare type MultiRegionContext = {
|
|
20
20
|
redis: Redis[];
|
|
21
|
-
cache?:
|
|
21
|
+
cache?: EphemeralCache;
|
|
22
22
|
};
|
|
23
23
|
export declare type Context = RegionContext | MultiRegionContext;
|
|
24
24
|
export declare type RatelimitResponse = {
|
|
@@ -63,5 +63,5 @@ export declare type RatelimitResponse = {
|
|
|
63
63
|
pending: Promise<unknown>;
|
|
64
64
|
};
|
|
65
65
|
export declare type Algorithm<TContext> = (ctx: TContext, identifier: string, opts?: {
|
|
66
|
-
cache?:
|
|
66
|
+
cache?: EphemeralCache;
|
|
67
67
|
}) => Promise<RatelimitResponse>;
|