@upstash/ratelimit 0.0.0-ci.e36c7a4668d76e92b9a8650ce5d3967a8d0a7e36-20241006173732
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/LICENSE +21 -0
- package/README.md +106 -0
- package/dist/index.d.mts +770 -0
- package/dist/index.d.ts +770 -0
- package/dist/index.js +1665 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1641 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Upstash, Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# Upstash Rate Limit
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@upstash/ratelimit)
|
|
4
|
+
[](https://github.com/upstash/ratelimit/actions/workflows/tests.yaml)
|
|
5
|
+
|
|
6
|
+
> [!NOTE]
|
|
7
|
+
> **This project is in GA Stage.**
|
|
8
|
+
> The Upstash Professional Support fully covers this project. It receives regular updates, and bug fixes. The Upstash team is committed to maintaining and improving its functionality.
|
|
9
|
+
|
|
10
|
+
It is the only connectionless (HTTP based) rate limiting library and designed
|
|
11
|
+
for:
|
|
12
|
+
|
|
13
|
+
- Serverless functions (AWS Lambda, Vercel ....)
|
|
14
|
+
- Cloudflare Workers & Pages
|
|
15
|
+
- Vercel Edge
|
|
16
|
+
- Fastly Compute@Edge
|
|
17
|
+
- Next.js, Jamstack ...
|
|
18
|
+
- Client side web/mobile applications
|
|
19
|
+
- WebAssembly
|
|
20
|
+
- and other environments where HTTP is preferred over TCP.
|
|
21
|
+
|
|
22
|
+
## Quick Start
|
|
23
|
+
|
|
24
|
+
### Install
|
|
25
|
+
|
|
26
|
+
#### npm
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm install @upstash/ratelimit
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
#### Deno
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { Ratelimit } from "https://cdn.skypack.dev/@upstash/ratelimit@latest";
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Create database
|
|
39
|
+
|
|
40
|
+
Create a new redis database on [upstash](https://console.upstash.com/). See [here](https://github.com/upstash/upstash-redis#quick-start) for documentation on how to create a redis instance.
|
|
41
|
+
|
|
42
|
+
### Basic Usage
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import { Ratelimit } from "@upstash/ratelimit"; // for deno: see above
|
|
46
|
+
import { Redis } from "@upstash/redis"; // see below for cloudflare and fastly adapters
|
|
47
|
+
|
|
48
|
+
// Create a new ratelimiter, that allows 10 requests per 10 seconds
|
|
49
|
+
const ratelimit = new Ratelimit({
|
|
50
|
+
redis: Redis.fromEnv(),
|
|
51
|
+
limiter: Ratelimit.slidingWindow(10, "10 s"),
|
|
52
|
+
analytics: true,
|
|
53
|
+
/**
|
|
54
|
+
* Optional prefix for the keys used in redis. This is useful if you want to share a redis
|
|
55
|
+
* instance with other applications and want to avoid key collisions. The default prefix is
|
|
56
|
+
* "@upstash/ratelimit"
|
|
57
|
+
*/
|
|
58
|
+
prefix: "@upstash/ratelimit",
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// Use a constant string to limit all requests with a single ratelimit
|
|
62
|
+
// Or use a userID, apiKey or ip address for individual limits.
|
|
63
|
+
const identifier = "api";
|
|
64
|
+
const { success } = await ratelimit.limit(identifier);
|
|
65
|
+
|
|
66
|
+
if (!success) {
|
|
67
|
+
return "Unable to process at this time";
|
|
68
|
+
}
|
|
69
|
+
doExpensiveCalculation();
|
|
70
|
+
return "Here you go!";
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
For more information on getting started, you can refer to [our documentation](https://upstash.com/docs/oss/sdks/ts/ratelimit/gettingstarted).
|
|
74
|
+
|
|
75
|
+
[Here's a complete nextjs example](https://github.com/upstash/ratelimit/tree/main/examples/nextjs)
|
|
76
|
+
|
|
77
|
+
## Documentation
|
|
78
|
+
|
|
79
|
+
See [the documentation](https://upstash.com/docs/redis/sdks/ratelimit-ts/overview) for more information details about this package.
|
|
80
|
+
|
|
81
|
+
## Contributing
|
|
82
|
+
|
|
83
|
+
### Database
|
|
84
|
+
|
|
85
|
+
Create a new redis database on [upstash](https://console.upstash.com/) and copy
|
|
86
|
+
the url and token.
|
|
87
|
+
|
|
88
|
+
### Running tests
|
|
89
|
+
|
|
90
|
+
To run the tests, you will need to set some environment variables. Here is a list of
|
|
91
|
+
variables to set:
|
|
92
|
+
- `UPSTASH_REDIS_REST_URL`
|
|
93
|
+
- `UPSTASH_REDIS_REST_TOKEN`
|
|
94
|
+
- `US1_UPSTASH_REDIS_REST_URL`
|
|
95
|
+
- `US1_UPSTASH_REDIS_REST_TOKEN`
|
|
96
|
+
- `APN_UPSTASH_REDIS_REST_URL`
|
|
97
|
+
- `APN_UPSTASH_REDIS_REST_TOKEN`
|
|
98
|
+
- `EU2_UPSTASH_REDIS_REST_URL`
|
|
99
|
+
- `EU2_UPSTASH_REDIS_REST_TOKEN`
|
|
100
|
+
|
|
101
|
+
You can create a single Upstash Redis and use its URL and token for all four above.
|
|
102
|
+
|
|
103
|
+
Once you set the environment variables, simply run:
|
|
104
|
+
```sh
|
|
105
|
+
pnpm test
|
|
106
|
+
```
|