hono-rate-limiter 0.2.1-rc.0 → 0.2.2

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 ADDED
@@ -0,0 +1,67 @@
1
+ <h1 align="center"> <code>🔥hono-rate-limiter🔥</code> </h1>
2
+
3
+ <div align="center">
4
+
5
+ [![tests](https://img.shields.io/github/actions/workflow/status/rhinobase/hono-rate-limiter/test.yaml)](https://github.com/rhinobase/hono-rate-limiter/actions/workflows/test.yaml)
6
+ [![npm version](https://img.shields.io/npm/v/hono-rate-limiter.svg)](https://npmjs.org/package/hono-rate-limiter "View this project on NPM")
7
+ [![npm downloads](https://img.shields.io/npm/dm/hono-rate-limiter)](https://www.npmjs.com/package/hono-rate-limiter)
8
+ [![license](https://img.shields.io/npm/l/hono-rate-limiter)](LICENSE)
9
+
10
+ </div>
11
+
12
+ Rate limiting middleware for [Hono](https://hono.dev/). Use to
13
+ limit repeated requests to public APIs and/or endpoints such as password reset.
14
+
15
+ > [!WARNING]
16
+ > The `keyGenerator` function is currently under construction and needs to be defined for `hono-rate-limiter` to work properly in your environment. Please ensure that you define the `keyGenerator` function according to the documentation before using the library.
17
+
18
+ ##Usage
19
+
20
+ ```ts
21
+ import { rateLimiter } from "hono-rate-limiter";
22
+
23
+ const limiter = rateLimiter({
24
+ windowMs: 15 * 60 * 1000, // 15 minutes
25
+ limit: 100, // Limit each IP to 100 requests per `window` (here, per 15 minutes).
26
+ standardHeaders: "draft-6", // draft-6: `RateLimit-*` headers; draft-7: combined `RateLimit` header
27
+ keyGenerator: (c) => "<unique_key>", // Method to generate custom identifiers for clients.
28
+ // store: ... , // Redis, MemoryStore, etc. See below.
29
+ });
30
+
31
+ // Apply the rate limiting middleware to all requests.
32
+ app.use(limiter);
33
+ ```
34
+
35
+ ## Data Stores
36
+
37
+ `hono-rate-limit` supports external data stores to synchronize hit counts across multiple processes and servers.
38
+
39
+ By default, `MemoryStore` is used. This one does not synchronize its state across instances. It’s simple to deploy, and often sufficient for basic abuse prevention, but will be inconnsistent across reboots or in deployments with multiple process or servers.
40
+
41
+ Deployments requiring more consistently enforced rate limits should use an external store.
42
+
43
+ Here is a list of stores:
44
+
45
+ | Name | Description |
46
+ | ------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
47
+ | MemoryStore | (default) Simple in-memory option. Does not share state when the app has multiple processes or servers. |
48
+ | [`@hono-rate-limiter/redis`](https://www.npm.im/@hono-rate-limiter/redis) | A [Redis](https://redis.io/)-backed store, used with [`@vercel/kv`](https://www.npmjs.com/package/@vercel/kv) and [`@upstash/redis`](https://www.npmjs.com/package/@upstash/redis) |
49
+ | [`rate-limit-redis`](https://npm.im/rate-limit-redis) | A [Redis](https://redis.io/)-backed store, more suitable for large or demanding deployments. |
50
+ | [`rate-limit-postresql`](https://www.npm.im/@acpr/rate-limit-postgresql) | A [PostgreSQL](https://www.postgresql.org/)-backed store. |
51
+ | [`rate-limit-memecached`](https://npmjs.org/package/rate-limit-memcached) | A [Memcached](https://memcached.org/)-backed store. |
52
+ | [`cluster-memory-store`](https://npm.im/@express-rate-limit/cluster-memory-store) | A memory-store wrapper that shares state across all processes on a single server via the [node:cluster](https://nodejs.org/api/cluster.html) module. Does not share state across multiple servers. |
53
+ | [`precise-memory-rate-limit`](https://www.npm.im/precise-memory-rate-limit) | A memory store similar to the built-in one, except that it stores a distinct timestamp for each key. |
54
+ | [`typeorm-rate-limit-store`](https://www.npmjs.com/package/typeorm-rate-limit-store) | Supports a variety of databases via [TypeORM](https://typeorm.io/): MySQL, MariaDB, CockroachDB, SQLite, Microsoft SQL Server, Oracle, SAP Hana, and more. |
55
+ | [`@rlimit/storage`](https://www.npmjs.com/package/@rlimit/storage) | A distributed rlimit store, ideal for multi-regional deployments. |
56
+
57
+ Take a look at this [guide](https://express-rate-limit.mintlify.app/guides/creating-a-store) if you wish to create your own store.
58
+
59
+ ## Contributing
60
+
61
+ We would love to have more contributors involved!
62
+
63
+ To get started, please read our [Contributing Guide](https://github.com/rhinobase/hono-rate-limiter/blob/main/CONTRIBUTING.md).
64
+
65
+ ## Credits
66
+
67
+ The `hono-rate-limiter` project is heavily inspired by [express-rate-limit](https://github.com/express-rate-limit/express-rate-limit)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono-rate-limiter",
3
- "version": "0.2.1-rc.0",
3
+ "version": "0.2.2",
4
4
  "license": "MIT",
5
5
  "keywords": [
6
6
  "hono",
@@ -11,14 +11,14 @@
11
11
  "rate-limiter",
12
12
  "honojs"
13
13
  ],
14
- "homepage": "https://github.com/rhinobase/hono-rate-limiter",
14
+ "homepage": "https://hono-rate-limiter.vercel.app",
15
15
  "publishConfig": {
16
16
  "access": "public"
17
17
  },
18
18
  "repository": {
19
19
  "type": "git",
20
20
  "url": "git+https://github.com/rhinobase/hono-rate-limiter.git",
21
- "directory": "packages/ui"
21
+ "directory": "packages/core"
22
22
  },
23
23
  "bugs": {
24
24
  "url": "https://github.com/rhinobase/hono-rate-limiter/issues"
package/src/types.d.ts CHANGED
@@ -10,6 +10,13 @@ export type ClientRateLimitInfo = {
10
10
  totalHits: number;
11
11
  resetTime?: Date;
12
12
  };
13
+ /**
14
+ * Promisify<T> is a utility type that represents a value of type T or a Promise<T>.
15
+ * This type is useful for converting synchronous functions to asynchronous functions.
16
+ * @example
17
+ * type getResult = Promisify<number>; // getResult can be number or Promise<number>
18
+ * type getUser = Promisify<User>; // getUser can be User or Promise<User>
19
+ */
13
20
  export type Promisify<T> = T | Promise<T>;
14
21
  /**
15
22
  * The rate limit related information for each client included in the
@@ -148,7 +155,7 @@ export type Store = {
148
155
  *
149
156
  * @returns {ClientRateLimitInfo} - The number of hits and reset time for that client.
150
157
  */
151
- get?: (key: string) => Promise<ClientRateLimitInfo | undefined> | ClientRateLimitInfo | undefined;
158
+ get?: (key: string) => Promisify<ClientRateLimitInfo | undefined>;
152
159
  /**
153
160
  * Method to increment a client's hit counter.
154
161
  *
@@ -156,27 +163,27 @@ export type Store = {
156
163
  *
157
164
  * @returns {IncrementResponse | undefined} - The number of hits and reset time for that client.
158
165
  */
159
- increment: (key: string) => Promise<IncrementResponse> | IncrementResponse;
166
+ increment: (key: string) => Promisify<IncrementResponse>;
160
167
  /**
161
168
  * Method to decrement a client's hit counter.
162
169
  *
163
170
  * @param key {string} - The identifier for a client.
164
171
  */
165
- decrement: (key: string) => Promise<void> | void;
172
+ decrement: (key: string) => Promisify<void>;
166
173
  /**
167
174
  * Method to reset a client's hit counter.
168
175
  *
169
176
  * @param key {string} - The identifier for a client.
170
177
  */
171
- resetKey: (key: string) => Promise<void> | void;
178
+ resetKey: (key: string) => Promisify<void>;
172
179
  /**
173
180
  * Method to reset everyone's hit counter.
174
181
  */
175
- resetAll?: () => Promise<void> | void;
182
+ resetAll?: () => Promisify<void>;
176
183
  /**
177
184
  * Method to shutdown the store, stop timers, and release all resources.
178
185
  */
179
- shutdown?: () => Promise<void> | void;
186
+ shutdown?: () => Promisify<void>;
180
187
  /**
181
188
  * Flag to indicate that keys incremented in one instance of this store can
182
189
  * not affect other instances. Typically false if a database is used, true for