@upstash/ratelimit 0.1.4-rc.1 → 0.1.4
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/README.md +24 -24
- package/esm/multi.js +3 -1
- package/esm/single.js +0 -2
- package/package.json +1 -1
- package/script/multi.js +3 -1
- package/script/single.js +0 -2
- package/types/single.d.ts +0 -2
package/README.md
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
# Upstash
|
|
2
|
-
|
|
3
|
-
An HTTP/REST based Redis client built on top of Upstash REST API.
|
|
4
|
-
[Upstash REST API](https://docs.upstash.com/features/restapi).
|
|
1
|
+
# Upstash RateLimit
|
|
5
2
|
|
|
6
3
|
[](https://github.com/upstash/ratelimit/actions/workflows/tests.yaml)
|
|
7
4
|

|
|
8
5
|
|
|
9
|
-
It is the only connectionless (HTTP based)
|
|
6
|
+
It is the only connectionless (HTTP based) rate limiting library and designed
|
|
7
|
+
for:
|
|
10
8
|
|
|
11
9
|
- Serverless functions (AWS Lambda ...)
|
|
12
10
|
- Cloudflare Workers
|
|
@@ -18,6 +16,7 @@ It is the only connectionless (HTTP based) ratelimiter and designed for:
|
|
|
18
16
|
|
|
19
17
|
<!-- toc -->
|
|
20
18
|
|
|
19
|
+
- [Docs](#docs)
|
|
21
20
|
- [Quick Start](#quick-start)
|
|
22
21
|
- [Install](#install)
|
|
23
22
|
- [npm](#npm)
|
|
@@ -25,7 +24,8 @@ It is the only connectionless (HTTP based) ratelimiter and designed for:
|
|
|
25
24
|
- [Create database](#create-database)
|
|
26
25
|
- [Use it](#use-it)
|
|
27
26
|
- [Block until ready](#block-until-ready)
|
|
28
|
-
- [
|
|
27
|
+
- [Ephemeral Cache](#ephemeral-cache)
|
|
28
|
+
- [MultiRegion replicated ratelimiting](#multiregion-replicated-ratelimiting)
|
|
29
29
|
- [Usage](#usage)
|
|
30
30
|
- [Asynchronous synchronization between databases](#asynchronous-synchronization-between-databases)
|
|
31
31
|
- [Example](#example)
|
|
@@ -49,6 +49,10 @@ It is the only connectionless (HTTP based) ratelimiter and designed for:
|
|
|
49
49
|
|
|
50
50
|
<!-- tocstop -->
|
|
51
51
|
|
|
52
|
+
## Docs
|
|
53
|
+
|
|
54
|
+
[doc.deno.land](https://doc.deno.land/https://deno.land/x/upstash_ratelimit/src/mod.ts)
|
|
55
|
+
|
|
52
56
|
## Quick Start
|
|
53
57
|
|
|
54
58
|
### Install
|
|
@@ -180,11 +184,10 @@ return "Here you go!";
|
|
|
180
184
|
### Ephemeral Cache
|
|
181
185
|
|
|
182
186
|
For extreme load or denial of service attacks, it might be too expensive to call
|
|
183
|
-
redis for every incoming request, just to find out
|
|
184
|
-
|
|
187
|
+
redis for every incoming request, just to find out it should be blocked because
|
|
188
|
+
they have exceeded the limit.
|
|
185
189
|
|
|
186
|
-
You can use an ephemeral in memory cache by passing the `ephemeralCache`
|
|
187
|
-
options:
|
|
190
|
+
You can use an ephemeral in memory cache by passing the `ephemeralCache` option:
|
|
188
191
|
|
|
189
192
|
```ts
|
|
190
193
|
const cache = new Map(); // must be outside of your serverless function handler
|
|
@@ -197,21 +200,18 @@ const ratelimit = new Ratelimit({
|
|
|
197
200
|
});
|
|
198
201
|
```
|
|
199
202
|
|
|
200
|
-
If enabled, the ratelimiter will keep a global cache of identifiers and
|
|
201
|
-
|
|
202
|
-
is only possible if you create the
|
|
203
|
-
function. While the function is still hot, the
|
|
204
|
-
without having to request data from redis, thus
|
|
205
|
-
|
|
206
|
-
Whenever an identifier has exceeded its limit, the ratelimiter will add it to an
|
|
207
|
-
internal list together with its reset timestamp. If the same identifier makes a
|
|
208
|
-
new request before it is reset, we can immediately reject it.
|
|
203
|
+
If enabled, the ratelimiter will keep a global cache of identifiers and their
|
|
204
|
+
reset timestamps, that have exhausted their ratelimit. In serverless
|
|
205
|
+
environments this is only possible if you create the cache or ratelimiter
|
|
206
|
+
instance outside of your handler function. While the function is still hot, the
|
|
207
|
+
ratelimiter can block requests without having to request data from redis, thus
|
|
208
|
+
saving time and money.
|
|
209
209
|
|
|
210
|
-
##
|
|
210
|
+
## MultiRegion replicated ratelimiting
|
|
211
211
|
|
|
212
|
-
Using a single redis instance has the downside of providing low latencies
|
|
213
|
-
part of your userbase closest to the deployed db. That's why we also
|
|
214
|
-
`MultiRegionRatelimit` which replicates the state across multiple redis
|
|
212
|
+
Using a single redis instance has the downside of providing low latencies only
|
|
213
|
+
to the part of your userbase closest to the deployed db. That's why we also
|
|
214
|
+
built `MultiRegionRatelimit` which replicates the state across multiple redis
|
|
215
215
|
databases as well as offering lower latencies to more of your users.
|
|
216
216
|
|
|
217
217
|
`MultiRegionRatelimit` does this by checking the current limit in the closest db
|
|
@@ -278,7 +278,7 @@ context.waitUntil(pending);
|
|
|
278
278
|
### Example
|
|
279
279
|
|
|
280
280
|
Let's assume you have customers in the US and Europe. In this case you can
|
|
281
|
-
create 2 regional redis databases on [
|
|
281
|
+
create 2 regional redis databases on [Upstash](https://console.upstash.com) and
|
|
282
282
|
your users will enjoy the latency of whichever db is closest to them.
|
|
283
283
|
|
|
284
284
|
## Ratelimiting algorithms
|
package/esm/multi.js
CHANGED
|
@@ -26,7 +26,9 @@ export class MultiRegionRatelimit extends Ratelimit {
|
|
|
26
26
|
limiter: config.limiter,
|
|
27
27
|
ctx: {
|
|
28
28
|
redis: config.redis,
|
|
29
|
-
cache: config.ephermeralCache
|
|
29
|
+
cache: config.ephermeralCache
|
|
30
|
+
? new Cache(config.ephermeralCache)
|
|
31
|
+
: undefined,
|
|
30
32
|
},
|
|
31
33
|
});
|
|
32
34
|
}
|
package/esm/single.js
CHANGED
|
@@ -201,8 +201,6 @@ export class RegionRatelimit extends Ratelimit {
|
|
|
201
201
|
* rate.
|
|
202
202
|
* - Allows to set a higher initial burst limit by setting `maxTokens` higher
|
|
203
203
|
* than `refillRate`
|
|
204
|
-
*
|
|
205
|
-
* **Usage of Upstash Redis requests:**
|
|
206
204
|
*/
|
|
207
205
|
static tokenBucket(
|
|
208
206
|
/**
|
package/package.json
CHANGED
package/script/multi.js
CHANGED
|
@@ -29,7 +29,9 @@ class MultiRegionRatelimit extends ratelimit_js_1.Ratelimit {
|
|
|
29
29
|
limiter: config.limiter,
|
|
30
30
|
ctx: {
|
|
31
31
|
redis: config.redis,
|
|
32
|
-
cache: config.ephermeralCache
|
|
32
|
+
cache: config.ephermeralCache
|
|
33
|
+
? new cache_js_1.Cache(config.ephermeralCache)
|
|
34
|
+
: undefined,
|
|
33
35
|
},
|
|
34
36
|
});
|
|
35
37
|
}
|
package/script/single.js
CHANGED
|
@@ -204,8 +204,6 @@ class RegionRatelimit extends ratelimit_js_1.Ratelimit {
|
|
|
204
204
|
* rate.
|
|
205
205
|
* - Allows to set a higher initial burst limit by setting `maxTokens` higher
|
|
206
206
|
* than `refillRate`
|
|
207
|
-
*
|
|
208
|
-
* **Usage of Upstash Redis requests:**
|
|
209
207
|
*/
|
|
210
208
|
static tokenBucket(
|
|
211
209
|
/**
|
package/types/single.d.ts
CHANGED
|
@@ -127,8 +127,6 @@ export declare class RegionRatelimit extends Ratelimit<RegionContext> {
|
|
|
127
127
|
* rate.
|
|
128
128
|
* - Allows to set a higher initial burst limit by setting `maxTokens` higher
|
|
129
129
|
* than `refillRate`
|
|
130
|
-
*
|
|
131
|
-
* **Usage of Upstash Redis requests:**
|
|
132
130
|
*/
|
|
133
131
|
static tokenBucket(
|
|
134
132
|
/**
|