@upstash/ratelimit 2.2.0 → 2.2.1

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/src/types.ts ADDED
@@ -0,0 +1,132 @@
1
+ import type { Redis as RedisCore } from "@upstash/redis";
2
+ import type { Geo } from "./analytics";
3
+
4
+ /**
5
+ * EphemeralCache is used to block certain identifiers right away in case they have already exceeded the ratelimit.
6
+ */
7
+ export type EphemeralCache = {
8
+ isBlocked: (identifier: string) => { blocked: boolean; reset: number };
9
+ blockUntil: (identifier: string, reset: number) => void;
10
+
11
+ set: (key: string, value: number) => void;
12
+ get: (key: string) => number | null;
13
+
14
+ incr: (key: string, incrementAmount?: number) => number;
15
+
16
+ pop: (key: string) => void;
17
+ empty: () => void;
18
+
19
+ size: () => number;
20
+ }
21
+
22
+ export type RegionContext = {
23
+ redis: Redis;
24
+ cache?: EphemeralCache,
25
+ /**
26
+ * If enabled, the ratelimiter will check for dynamic limits in Redis
27
+ * using MGET before applying the regular limit.
28
+ */
29
+ dynamicLimits?: boolean;
30
+ /**
31
+ * The prefix used for Redis keys
32
+ */
33
+ prefix: string;
34
+ };
35
+ export type MultiRegionContext = { regionContexts: Omit<RegionContext[], "cache">; cache?: EphemeralCache };
36
+
37
+ export type RatelimitResponseType = "timeout" | "cacheBlock" | "denyList"
38
+
39
+ export type Context = RegionContext | MultiRegionContext;
40
+ export type RatelimitResponse = {
41
+ /**
42
+ * Whether the request may pass(true) or exceeded the limit(false)
43
+ */
44
+ success: boolean;
45
+ /**
46
+ * Maximum number of requests allowed within a window.
47
+ */
48
+ limit: number;
49
+ /**
50
+ * How many requests the user has left within the current window.
51
+ */
52
+ remaining: number;
53
+ /**
54
+ * Unix timestamp in milliseconds when the limits are reset.
55
+ */
56
+ reset: number;
57
+
58
+ /**
59
+ * For the MultiRegion setup we do some synchronizing in the background, after returning the current limit.
60
+ * Or when analytics is enabled, we send the analytics asynchronously after returning the limit.
61
+ * In most case you can simply ignore this.
62
+ *
63
+ * On Vercel Edge or Cloudflare workers, you need to explicitly handle the pending Promise like this:
64
+ *
65
+ * ```ts
66
+ * const { pending } = await ratelimit.limit("id")
67
+ * context.waitUntil(pending)
68
+ * ```
69
+ *
70
+ * See `waitUntil` documentation in
71
+ * [Cloudflare](https://developers.cloudflare.com/workers/runtime-apis/handlers/fetch/#contextwaituntil)
72
+ * and [Vercel](https://vercel.com/docs/functions/edge-middleware/middleware-api#waituntil)
73
+ * for more details.
74
+ * ```
75
+ */
76
+ pending: Promise<unknown>;
77
+
78
+ /**
79
+ * Reason behind the result in `success` field.
80
+ * - Is set to "timeout" when request times out
81
+ * - Is set to "cacheBlock" when an identifier is blocked through cache without calling redis because it was
82
+ * rate limited previously.
83
+ * - Is set to "denyList" when identifier or one of ip/user-agent/country parameters is in deny list. To enable
84
+ * deny list, see `enableProtection` parameter. To edit the deny list, see the Upstash Ratelimit Dashboard
85
+ * at https://console.upstash.com/ratelimit.
86
+ * - Is set to undefined if rate limit check had to use Redis. This happens in cases when `success` field in
87
+ * the response is true. It can also happen the first time sucecss is false.
88
+ */
89
+ reason?: RatelimitResponseType;
90
+
91
+ /**
92
+ * The value which was in the deny list if reason: "denyList"
93
+ */
94
+ deniedValue?: DeniedValue
95
+ };
96
+
97
+ export type Algorithm<TContext> = () => {
98
+ limit: (
99
+ ctx: TContext,
100
+ identifier: string,
101
+ rate?: number,
102
+ opts?: {
103
+ cache?: EphemeralCache;
104
+ },
105
+ ) => Promise<RatelimitResponse>;
106
+ getRemaining: (ctx: TContext, identifier: string) => Promise<{
107
+ remaining: number,
108
+ reset: number,
109
+ limit: number
110
+ }>;
111
+ resetTokens: (ctx: TContext, identifier: string) => Promise<void>;
112
+ };
113
+
114
+ export type IsDenied = 0 | 1;
115
+
116
+ export type DeniedValue = string | undefined;
117
+ export type DenyListResponse = { deniedValue: DeniedValue, invalidIpDenyList: boolean }
118
+
119
+ export const DenyListExtension = "denyList" as const
120
+ export const IpDenyListKey = "ipDenyList" as const
121
+ export const IpDenyListStatusKey = "ipDenyListStatus" as const
122
+
123
+ export type LimitPayload = [RatelimitResponse, DenyListResponse];
124
+ export type LimitOptions = {
125
+ geo?: Geo,
126
+ rate?: number,
127
+ ip?: string,
128
+ userAgent?: string,
129
+ country?: string
130
+ }
131
+
132
+ export type Redis = RedisCore
package/src/version.ts ADDED
@@ -0,0 +1 @@
1
+ export const VERSION = "2.2.1";