@upstash/ratelimit 0.1.3-rc.0 → 0.1.3
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 +58 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -99,29 +99,50 @@ return "Here you go!";
|
|
|
99
99
|
|
|
100
100
|
The `limit` method returns some more metadata that might be useful to you:
|
|
101
101
|
|
|
102
|
-
|
|
102
|
+
````ts
|
|
103
103
|
export type RatelimitResponse = {
|
|
104
104
|
/**
|
|
105
105
|
* Whether the request may pass(true) or exceeded the limit(false)
|
|
106
106
|
*/
|
|
107
107
|
success: boolean;
|
|
108
|
-
|
|
109
108
|
/**
|
|
110
109
|
* Maximum number of requests allowed within a window.
|
|
111
110
|
*/
|
|
112
111
|
limit: number;
|
|
113
|
-
|
|
114
112
|
/**
|
|
115
113
|
* How many requests the user has left within the current window.
|
|
116
114
|
*/
|
|
117
115
|
remaining: number;
|
|
118
|
-
|
|
119
116
|
/**
|
|
120
117
|
* Unix timestamp in milliseconds when the limits are reset.
|
|
121
118
|
*/
|
|
122
119
|
reset: number;
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* For the MultiRegion setup we do some synchronizing in the background, after returning the current limit.
|
|
123
|
+
* In most case you can simply ignore this.
|
|
124
|
+
*
|
|
125
|
+
* On Vercel Edge or Cloudflare workers, you need to explicitely handle the pending Promise like this:
|
|
126
|
+
*
|
|
127
|
+
* **Vercel Edge:**
|
|
128
|
+
* https://nextjs.org/docs/api-reference/next/server#nextfetchevent
|
|
129
|
+
*
|
|
130
|
+
* ```ts
|
|
131
|
+
* const { pending } = await ratelimit.limit("id")
|
|
132
|
+
* event.waitUntil(pending)
|
|
133
|
+
* ```
|
|
134
|
+
*
|
|
135
|
+
* **Cloudflare Worker:**
|
|
136
|
+
* https://developers.cloudflare.com/workers/runtime-apis/fetch-event/#syntax-module-worker
|
|
137
|
+
*
|
|
138
|
+
* ```ts
|
|
139
|
+
* const { pending } = await ratelimit.limit("id")
|
|
140
|
+
* context.waitUntil(pending)
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
pending: Promise<unknown>;
|
|
123
144
|
};
|
|
124
|
-
|
|
145
|
+
````
|
|
125
146
|
|
|
126
147
|
### Block until ready
|
|
127
148
|
|
|
@@ -181,9 +202,15 @@ import { Redis } from "@upstash/redis";
|
|
|
181
202
|
// Create a new ratelimiter, that allows 10 requests per 10 seconds
|
|
182
203
|
const ratelimit = new MultiRegionRatelimit({
|
|
183
204
|
redis: [
|
|
184
|
-
new Redis({
|
|
185
|
-
|
|
186
|
-
|
|
205
|
+
new Redis({
|
|
206
|
+
/* auth */
|
|
207
|
+
}),
|
|
208
|
+
new Redis({
|
|
209
|
+
/* auth */
|
|
210
|
+
}),
|
|
211
|
+
new Redis({
|
|
212
|
+
/* auth */
|
|
213
|
+
}),
|
|
187
214
|
],
|
|
188
215
|
limiter: Ratelimit.slidingWindow(10, "10 s"),
|
|
189
216
|
});
|
|
@@ -194,6 +221,29 @@ const identifier = "api";
|
|
|
194
221
|
const { success } = await ratelimit.limit(identifier);
|
|
195
222
|
```
|
|
196
223
|
|
|
224
|
+
### Asynchronous synchronization between databases
|
|
225
|
+
|
|
226
|
+
The MultiRegion setup will do some synchronization between databases after
|
|
227
|
+
returning the current limit. This can lead to problems on Cloudflare Workers and
|
|
228
|
+
therefore Vercel Edge functions, because dangling promises must be taken care
|
|
229
|
+
of:
|
|
230
|
+
|
|
231
|
+
**Vercel Edge:**
|
|
232
|
+
[docs](https://nextjs.org/docs/api-reference/next/server#nextfetchevent)
|
|
233
|
+
|
|
234
|
+
```ts
|
|
235
|
+
const { pending } = await ratelimit.limit("id");
|
|
236
|
+
event.waitUntil(pending);
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
**Cloudflare Worker:**
|
|
240
|
+
[docs](https://developers.cloudflare.com/workers/runtime-apis/fetch-event/#syntax-module-worker)
|
|
241
|
+
|
|
242
|
+
```ts
|
|
243
|
+
const { pending } = await ratelimit.limit("id");
|
|
244
|
+
context.waitUntil(pending);
|
|
245
|
+
```
|
|
246
|
+
|
|
197
247
|
### Example
|
|
198
248
|
|
|
199
249
|
Let's assume you have customers in the US and Europe. In this case you can
|
package/package.json
CHANGED