@upstash/ratelimit 0.4.5-canary.0 → 1.0.0-canary

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.
Files changed (94) hide show
  1. package/README.md +2 -2
  2. package/dist/index.d.mts +556 -0
  3. package/dist/index.d.ts +556 -0
  4. package/dist/index.js +832 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/index.mjs +803 -0
  7. package/dist/index.mjs.map +1 -0
  8. package/package.json +1 -22
  9. package/.github/actions/redis/action.yaml +0 -58
  10. package/.github/img/dashboard.png +0 -0
  11. package/.github/workflows/release.yml +0 -46
  12. package/.github/workflows/stale.yaml +0 -31
  13. package/.github/workflows/tests.yaml +0 -79
  14. package/biome.json +0 -37
  15. package/bun.lockb +0 -0
  16. package/cmd/set-version.js +0 -14
  17. package/examples/cloudflare-workers/package.json +0 -18
  18. package/examples/cloudflare-workers/src/index.ts +0 -35
  19. package/examples/cloudflare-workers/tsconfig.json +0 -105
  20. package/examples/cloudflare-workers/wrangler.toml +0 -3
  21. package/examples/nextjs/LICENSE +0 -21
  22. package/examples/nextjs/README.md +0 -17
  23. package/examples/nextjs/components/Breadcrumb.tsx +0 -67
  24. package/examples/nextjs/components/Header.tsx +0 -18
  25. package/examples/nextjs/components/ReadBlogPost.tsx +0 -9
  26. package/examples/nextjs/components/StarButton.tsx +0 -27
  27. package/examples/nextjs/middleware.ts +0 -35
  28. package/examples/nextjs/next-env.d.ts +0 -5
  29. package/examples/nextjs/package.json +0 -27
  30. package/examples/nextjs/pages/_app.tsx +0 -47
  31. package/examples/nextjs/pages/api/blocked.ts +0 -6
  32. package/examples/nextjs/pages/api/hello.ts +0 -5
  33. package/examples/nextjs/pages/index.tsx +0 -62
  34. package/examples/nextjs/postcss.config.js +0 -6
  35. package/examples/nextjs/public/favicon.ico +0 -0
  36. package/examples/nextjs/public/github.svg +0 -11
  37. package/examples/nextjs/public/upstash.svg +0 -27
  38. package/examples/nextjs/styles/globals.css +0 -76
  39. package/examples/nextjs/tailwind.config.js +0 -19
  40. package/examples/nextjs/tsconfig.json +0 -21
  41. package/examples/nextjs13/README.md +0 -38
  42. package/examples/nextjs13/app/favicon.ico +0 -0
  43. package/examples/nextjs13/app/globals.css +0 -107
  44. package/examples/nextjs13/app/layout.tsx +0 -18
  45. package/examples/nextjs13/app/page.module.css +0 -271
  46. package/examples/nextjs13/app/route.tsx +0 -14
  47. package/examples/nextjs13/next.config.js +0 -8
  48. package/examples/nextjs13/package.json +0 -22
  49. package/examples/nextjs13/public/next.svg +0 -1
  50. package/examples/nextjs13/public/thirteen.svg +0 -1
  51. package/examples/nextjs13/public/vercel.svg +0 -1
  52. package/examples/nextjs13/tsconfig.json +0 -28
  53. package/examples/remix/.env.example +0 -2
  54. package/examples/remix/.eslintrc.js +0 -4
  55. package/examples/remix/README.md +0 -59
  56. package/examples/remix/app/root.tsx +0 -25
  57. package/examples/remix/app/routes/index.tsx +0 -47
  58. package/examples/remix/package.json +0 -32
  59. package/examples/remix/public/favicon.ico +0 -0
  60. package/examples/remix/remix.config.js +0 -12
  61. package/examples/remix/remix.env.d.ts +0 -2
  62. package/examples/remix/server.js +0 -4
  63. package/examples/remix/tsconfig.json +0 -22
  64. package/examples/with-vercel-kv/README.md +0 -51
  65. package/examples/with-vercel-kv/app/favicon.ico +0 -0
  66. package/examples/with-vercel-kv/app/globals.css +0 -27
  67. package/examples/with-vercel-kv/app/layout.tsx +0 -21
  68. package/examples/with-vercel-kv/app/page.tsx +0 -71
  69. package/examples/with-vercel-kv/next.config.js +0 -8
  70. package/examples/with-vercel-kv/package.json +0 -25
  71. package/examples/with-vercel-kv/postcss.config.js +0 -6
  72. package/examples/with-vercel-kv/public/next.svg +0 -1
  73. package/examples/with-vercel-kv/public/vercel.svg +0 -1
  74. package/examples/with-vercel-kv/tailwind.config.js +0 -17
  75. package/examples/with-vercel-kv/tsconfig.json +0 -28
  76. package/src/analytics.test.ts +0 -23
  77. package/src/analytics.ts +0 -92
  78. package/src/blockUntilReady.test.ts +0 -56
  79. package/src/cache.test.ts +0 -41
  80. package/src/cache.ts +0 -43
  81. package/src/duration.test.ts +0 -23
  82. package/src/duration.ts +0 -30
  83. package/src/index.ts +0 -17
  84. package/src/multi.ts +0 -365
  85. package/src/ratelimit.test.ts +0 -155
  86. package/src/ratelimit.ts +0 -238
  87. package/src/single.ts +0 -487
  88. package/src/test_utils.ts +0 -65
  89. package/src/tools/seed.ts +0 -37
  90. package/src/types.ts +0 -78
  91. package/src/version.ts +0 -1
  92. package/tsconfig.json +0 -103
  93. package/tsup.config.js +0 -11
  94. package/turbo.json +0 -16
package/src/ratelimit.ts DELETED
@@ -1,238 +0,0 @@
1
- import { Analytics, Geo } from "./analytics";
2
- import { Cache } from "./cache";
3
- import type { Algorithm, Context, RatelimitResponse } from "./types";
4
-
5
- export class TimeoutError extends Error {
6
- constructor() {
7
- super("Timeout");
8
- this.name = "TimeoutError";
9
- }
10
- }
11
- export type RatelimitConfig<TContext> = {
12
- /**
13
- * The ratelimiter function to use.
14
- *
15
- * Choose one of the predefined ones or implement your own.
16
- * Available algorithms are exposed via static methods:
17
- * - Ratelimiter.fixedWindow
18
- * - Ratelimiter.slidingWindow
19
- * - Ratelimiter.tokenBucket
20
- */
21
-
22
- limiter: Algorithm<TContext>;
23
-
24
- ctx: TContext;
25
- /**
26
- * All keys in redis are prefixed with this.
27
- *
28
- * @default `@upstash/ratelimit`
29
- */
30
- prefix?: string;
31
-
32
- /**
33
- * If enabled, the ratelimiter will keep a global cache of identifiers, that have
34
- * exhausted their ratelimit. In serverless environments this is only possible if
35
- * you create the ratelimiter instance outside of your handler function. While the
36
- * function is still hot, the ratelimiter can block requests without having to
37
- * request data from redis, thus saving time and money.
38
- *
39
- * Whenever an identifier has exceeded its limit, the ratelimiter will add it to an
40
- * internal list together with its reset timestamp. If the same identifier makes a
41
- * new request before it is reset, we can immediately reject it.
42
- *
43
- * Set to `false` to disable.
44
- *
45
- * If left undefined, a map is created automatically, but it can only work
46
- * if the map or the ratelimit instance is created outside your serverless function handler.
47
- */
48
- ephemeralCache?: Map<string, number> | false;
49
-
50
- /**
51
- * If set, the ratelimiter will allow requests to pass after this many milliseconds.
52
- *
53
- * Use this if you want to allow requests in case of network problems
54
- *
55
- * @default 5000
56
- */
57
- timeout?: number;
58
-
59
- /**
60
- * If enabled, the ratelimiter will store analytics data in redis, which you can check out at
61
- * https://console.upstash.com/ratelimit
62
- *
63
- * @default false
64
- */
65
- analytics?: boolean;
66
- };
67
-
68
- /**
69
- * Ratelimiter using serverless redis from https://upstash.com/
70
- *
71
- * @example
72
- * ```ts
73
- * const { limit } = new Ratelimit({
74
- * redis: Redis.fromEnv(),
75
- * limiter: Ratelimit.slidingWindow(
76
- * 10, // Allow 10 requests per window of 30 minutes
77
- * "30 m", // interval of 30 minutes
78
- * ),
79
- * })
80
- *
81
- * ```
82
- */
83
- export abstract class Ratelimit<TContext extends Context> {
84
- protected readonly limiter: Algorithm<TContext>;
85
-
86
- protected readonly ctx: TContext;
87
-
88
- protected readonly prefix: string;
89
-
90
- protected readonly timeout: number;
91
- protected readonly analytics?: Analytics;
92
- constructor(config: RatelimitConfig<TContext>) {
93
- this.ctx = config.ctx;
94
- this.limiter = config.limiter;
95
- this.timeout = config.timeout ?? 5000;
96
- this.prefix = config.prefix ?? "@upstash/ratelimit";
97
- this.analytics = config.analytics
98
- ? new Analytics({
99
- redis: Array.isArray(this.ctx.redis) ? this.ctx.redis[0] : this.ctx.redis,
100
- prefix: this.prefix,
101
- })
102
- : undefined;
103
-
104
- if (config.ephemeralCache instanceof Map) {
105
- this.ctx.cache = new Cache(config.ephemeralCache);
106
- } else if (typeof config.ephemeralCache === "undefined") {
107
- this.ctx.cache = new Cache(new Map());
108
- }
109
- }
110
-
111
- /**
112
- * Determine if a request should pass or be rejected based on the identifier and previously chosen ratelimit.
113
- *
114
- * Use this if you want to reject all requests that you can not handle right now.
115
- *
116
- * @example
117
- * ```ts
118
- * const ratelimit = new Ratelimit({
119
- * redis: Redis.fromEnv(),
120
- * limiter: Ratelimit.slidingWindow(10, "10 s")
121
- * })
122
- *
123
- * const { success } = await ratelimit.limit(id)
124
- * if (!success){
125
- * return "Nope"
126
- * }
127
- * return "Yes"
128
- * ```
129
- */
130
- public limit = async (identifier: string, req?: { geo?: Geo }): Promise<RatelimitResponse> => {
131
- const key = [this.prefix, identifier].join(":");
132
- let timeoutId: any = null;
133
- try {
134
- const arr: Promise<RatelimitResponse>[] = [this.limiter(this.ctx, key)];
135
- if (this.timeout > 0) {
136
- arr.push(
137
- new Promise((resolve) => {
138
- timeoutId = setTimeout(() => {
139
- resolve({
140
- success: true,
141
- limit: 0,
142
- remaining: 0,
143
- reset: 0,
144
- pending: Promise.resolve(),
145
- });
146
- }, this.timeout);
147
- }),
148
- );
149
- }
150
-
151
- const res = await Promise.race(arr);
152
- if (this.analytics) {
153
- try {
154
- const geo = req ? this.analytics.extractGeo(req) : undefined;
155
- const analyticsP = this.analytics
156
- .record({
157
- identifier,
158
- time: Date.now(),
159
- success: res.success,
160
- ...geo,
161
- })
162
- .catch((err) => {
163
- console.warn("Failed to record analytics", err);
164
- });
165
- res.pending = Promise.all([res.pending, analyticsP]);
166
- } catch (err) {
167
- console.warn("Failed to record analytics", err);
168
- }
169
- }
170
- return res;
171
- } finally {
172
- if (timeoutId) {
173
- clearTimeout(timeoutId);
174
- }
175
- }
176
- };
177
-
178
- /**
179
- * Block until the request may pass or timeout is reached.
180
- *
181
- * This method returns a promise that resolves as soon as the request may be processed
182
- * or after the timeout has been reached.
183
- *
184
- * Use this if you want to delay the request until it is ready to get processed.
185
- *
186
- * @example
187
- * ```ts
188
- * const ratelimit = new Ratelimit({
189
- * redis: Redis.fromEnv(),
190
- * limiter: Ratelimit.slidingWindow(10, "10 s")
191
- * })
192
- *
193
- * const { success } = await ratelimit.blockUntilReady(id, 60_000)
194
- * if (!success){
195
- * return "Nope"
196
- * }
197
- * return "Yes"
198
- * ```
199
- */
200
- public blockUntilReady = async (
201
- /**
202
- * An identifier per user or api.
203
- * Choose a userID, or api token, or ip address.
204
- *
205
- * If you want to limit your api across all users, you can set a constant string.
206
- */
207
- identifier: string,
208
- /**
209
- * Maximum duration to wait in milliseconds.
210
- * After this time the request will be denied.
211
- */
212
- timeout: number,
213
- ): Promise<RatelimitResponse> => {
214
- if (timeout <= 0) {
215
- throw new Error("timeout must be positive");
216
- }
217
- let res: RatelimitResponse;
218
-
219
- const deadline = Date.now() + timeout;
220
- while (true) {
221
- res = await this.limit(identifier);
222
- if (res.success) {
223
- break;
224
- }
225
- if (res.reset === 0) {
226
- throw new Error("This should not happen");
227
- }
228
-
229
- const wait = Math.min(res.reset, deadline) - Date.now();
230
- await new Promise((r) => setTimeout(r, wait));
231
-
232
- if (Date.now() > deadline) {
233
- break;
234
- }
235
- }
236
- return res!;
237
- };
238
- }