@jellyfungus/hono-rate-limiter 0.2.0 → 0.4.0

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/dist/index.d.cts CHANGED
@@ -26,9 +26,42 @@ type StoreResult = {
26
26
  reset: number;
27
27
  };
28
28
  /**
29
- * Header format versions
29
+ * Quota unit for IETF standard headers.
30
+ * @see https://datatracker.ietf.org/doc/draft-ietf-httpapi-ratelimit-headers/
30
31
  */
31
- type HeadersFormat = "draft-6" | "draft-7" | false;
32
+ type QuotaUnit = "requests" | "content-bytes" | "concurrent-requests";
33
+ /**
34
+ * Header format options.
35
+ *
36
+ * ## "legacy" (default)
37
+ * Common X-RateLimit-* headers used by GitHub, Twitter, and most APIs:
38
+ * - `X-RateLimit-Limit`: max requests in window
39
+ * - `X-RateLimit-Remaining`: remaining requests
40
+ * - `X-RateLimit-Reset`: Unix timestamp (seconds) when window resets
41
+ *
42
+ * ## "draft-6"
43
+ * IETF draft-06 format with individual RateLimit-* headers:
44
+ * - `RateLimit-Policy`: policy description (e.g., `100;w=60`)
45
+ * - `RateLimit-Limit`: max requests
46
+ * - `RateLimit-Remaining`: remaining requests
47
+ * - `RateLimit-Reset`: seconds until reset
48
+ *
49
+ * ## "draft-7"
50
+ * IETF draft-07 format with combined RateLimit header:
51
+ * - `RateLimit-Policy`: policy description
52
+ * - `RateLimit`: combined (e.g., `limit=100, remaining=50, reset=30`)
53
+ *
54
+ * ## "standard"
55
+ * Current IETF draft-08+ format with structured field values (RFC 9651):
56
+ * - `RateLimit-Policy`: `"name";q=100;w=60`
57
+ * - `RateLimit`: `"name";r=50;t=30`
58
+ *
59
+ * ## false
60
+ * Disable all rate limit headers.
61
+ *
62
+ * @see https://datatracker.ietf.org/doc/draft-ietf-httpapi-ratelimit-headers/
63
+ */
64
+ type HeadersFormat = "legacy" | "draft-6" | "draft-7" | "standard" | false;
32
65
  /**
33
66
  * Rate limit algorithm
34
67
  */
@@ -110,9 +143,28 @@ type RateLimitOptions<E extends Env = Env> = {
110
143
  handler?: (c: Context<E>, info: RateLimitInfo) => Response | Promise<Response>;
111
144
  /**
112
145
  * HTTP header format to use.
113
- * @default 'draft-6'
146
+ *
147
+ * - "legacy": X-RateLimit-* headers (GitHub/Twitter style, default)
148
+ * - "draft-6": IETF draft-06 individual headers
149
+ * - "draft-7": IETF draft-07 combined header
150
+ * - "standard": IETF draft-08+ structured fields (current spec)
151
+ * - false: Disable headers
152
+ *
153
+ * @default 'legacy'
114
154
  */
115
155
  headers?: HeadersFormat;
156
+ /**
157
+ * Policy identifier for IETF headers (draft-6+).
158
+ * Used in RateLimit and RateLimit-Policy headers.
159
+ * @default 'default'
160
+ */
161
+ identifier?: string;
162
+ /**
163
+ * Quota unit for IETF standard headers.
164
+ * Only included in "standard" format when not "requests".
165
+ * @default 'requests'
166
+ */
167
+ quotaUnit?: QuotaUnit;
116
168
  /**
117
169
  * Skip rate limiting for certain requests.
118
170
  */
@@ -131,6 +183,16 @@ type RateLimitOptions<E extends Env = Env> = {
131
183
  * Callback when a request is rate limited.
132
184
  */
133
185
  onRateLimited?: (c: Context<E>, info: RateLimitInfo) => void | Promise<void>;
186
+ /**
187
+ * Behavior when store operations fail.
188
+ *
189
+ * - 'allow': Allow the request through (fail-open, default)
190
+ * - 'deny': Block the request with 500 error (fail-closed)
191
+ * - Function: Custom handler returning true to allow, false to deny
192
+ *
193
+ * @default 'allow'
194
+ */
195
+ onStoreError?: "allow" | "deny" | ((error: Error, c: Context<E>) => boolean | Promise<boolean>);
134
196
  };
135
197
  /**
136
198
  * Cloudflare Rate Limiting binding interface
@@ -185,6 +247,21 @@ declare class MemoryStore implements RateLimitStore {
185
247
  resetAll(): void;
186
248
  shutdown(): void;
187
249
  }
250
+ /**
251
+ * Shutdown the default memory store.
252
+ * Call this during graceful shutdown to clean up timers.
253
+ *
254
+ * @example
255
+ * ```ts
256
+ * import { shutdownDefaultStore } from '@jellyfungus/hono-rate-limiter'
257
+ *
258
+ * process.on('SIGTERM', () => {
259
+ * shutdownDefaultStore()
260
+ * process.exit(0)
261
+ * })
262
+ * ```
263
+ */
264
+ declare function shutdownDefaultStore(): void;
188
265
  declare function getClientIP(c: Context): string;
189
266
  /**
190
267
  * Rate Limit Middleware for Hono.
@@ -232,4 +309,4 @@ declare const rateLimiter: <E extends Env = Env>(options?: RateLimitOptions<E>)
232
309
  */
233
310
  declare const cloudflareRateLimiter: <E extends Env = Env>(options: CloudflareRateLimitOptions<E>) => MiddlewareHandler<E>;
234
311
 
235
- export { type Algorithm, type CloudflareRateLimitOptions, type HeadersFormat, MemoryStore, type RateLimitBinding, type RateLimitInfo, type RateLimitOptions, type RateLimitStore, type RateLimitStoreAccess, type StoreResult, cloudflareRateLimiter, getClientIP, rateLimiter };
312
+ export { type Algorithm, type CloudflareRateLimitOptions, type HeadersFormat, MemoryStore, type QuotaUnit, type RateLimitBinding, type RateLimitInfo, type RateLimitOptions, type RateLimitStore, type RateLimitStoreAccess, type StoreResult, cloudflareRateLimiter, getClientIP, rateLimiter, shutdownDefaultStore };
package/dist/index.d.ts CHANGED
@@ -26,9 +26,42 @@ type StoreResult = {
26
26
  reset: number;
27
27
  };
28
28
  /**
29
- * Header format versions
29
+ * Quota unit for IETF standard headers.
30
+ * @see https://datatracker.ietf.org/doc/draft-ietf-httpapi-ratelimit-headers/
30
31
  */
31
- type HeadersFormat = "draft-6" | "draft-7" | false;
32
+ type QuotaUnit = "requests" | "content-bytes" | "concurrent-requests";
33
+ /**
34
+ * Header format options.
35
+ *
36
+ * ## "legacy" (default)
37
+ * Common X-RateLimit-* headers used by GitHub, Twitter, and most APIs:
38
+ * - `X-RateLimit-Limit`: max requests in window
39
+ * - `X-RateLimit-Remaining`: remaining requests
40
+ * - `X-RateLimit-Reset`: Unix timestamp (seconds) when window resets
41
+ *
42
+ * ## "draft-6"
43
+ * IETF draft-06 format with individual RateLimit-* headers:
44
+ * - `RateLimit-Policy`: policy description (e.g., `100;w=60`)
45
+ * - `RateLimit-Limit`: max requests
46
+ * - `RateLimit-Remaining`: remaining requests
47
+ * - `RateLimit-Reset`: seconds until reset
48
+ *
49
+ * ## "draft-7"
50
+ * IETF draft-07 format with combined RateLimit header:
51
+ * - `RateLimit-Policy`: policy description
52
+ * - `RateLimit`: combined (e.g., `limit=100, remaining=50, reset=30`)
53
+ *
54
+ * ## "standard"
55
+ * Current IETF draft-08+ format with structured field values (RFC 9651):
56
+ * - `RateLimit-Policy`: `"name";q=100;w=60`
57
+ * - `RateLimit`: `"name";r=50;t=30`
58
+ *
59
+ * ## false
60
+ * Disable all rate limit headers.
61
+ *
62
+ * @see https://datatracker.ietf.org/doc/draft-ietf-httpapi-ratelimit-headers/
63
+ */
64
+ type HeadersFormat = "legacy" | "draft-6" | "draft-7" | "standard" | false;
32
65
  /**
33
66
  * Rate limit algorithm
34
67
  */
@@ -110,9 +143,28 @@ type RateLimitOptions<E extends Env = Env> = {
110
143
  handler?: (c: Context<E>, info: RateLimitInfo) => Response | Promise<Response>;
111
144
  /**
112
145
  * HTTP header format to use.
113
- * @default 'draft-6'
146
+ *
147
+ * - "legacy": X-RateLimit-* headers (GitHub/Twitter style, default)
148
+ * - "draft-6": IETF draft-06 individual headers
149
+ * - "draft-7": IETF draft-07 combined header
150
+ * - "standard": IETF draft-08+ structured fields (current spec)
151
+ * - false: Disable headers
152
+ *
153
+ * @default 'legacy'
114
154
  */
115
155
  headers?: HeadersFormat;
156
+ /**
157
+ * Policy identifier for IETF headers (draft-6+).
158
+ * Used in RateLimit and RateLimit-Policy headers.
159
+ * @default 'default'
160
+ */
161
+ identifier?: string;
162
+ /**
163
+ * Quota unit for IETF standard headers.
164
+ * Only included in "standard" format when not "requests".
165
+ * @default 'requests'
166
+ */
167
+ quotaUnit?: QuotaUnit;
116
168
  /**
117
169
  * Skip rate limiting for certain requests.
118
170
  */
@@ -131,6 +183,16 @@ type RateLimitOptions<E extends Env = Env> = {
131
183
  * Callback when a request is rate limited.
132
184
  */
133
185
  onRateLimited?: (c: Context<E>, info: RateLimitInfo) => void | Promise<void>;
186
+ /**
187
+ * Behavior when store operations fail.
188
+ *
189
+ * - 'allow': Allow the request through (fail-open, default)
190
+ * - 'deny': Block the request with 500 error (fail-closed)
191
+ * - Function: Custom handler returning true to allow, false to deny
192
+ *
193
+ * @default 'allow'
194
+ */
195
+ onStoreError?: "allow" | "deny" | ((error: Error, c: Context<E>) => boolean | Promise<boolean>);
134
196
  };
135
197
  /**
136
198
  * Cloudflare Rate Limiting binding interface
@@ -185,6 +247,21 @@ declare class MemoryStore implements RateLimitStore {
185
247
  resetAll(): void;
186
248
  shutdown(): void;
187
249
  }
250
+ /**
251
+ * Shutdown the default memory store.
252
+ * Call this during graceful shutdown to clean up timers.
253
+ *
254
+ * @example
255
+ * ```ts
256
+ * import { shutdownDefaultStore } from '@jellyfungus/hono-rate-limiter'
257
+ *
258
+ * process.on('SIGTERM', () => {
259
+ * shutdownDefaultStore()
260
+ * process.exit(0)
261
+ * })
262
+ * ```
263
+ */
264
+ declare function shutdownDefaultStore(): void;
188
265
  declare function getClientIP(c: Context): string;
189
266
  /**
190
267
  * Rate Limit Middleware for Hono.
@@ -232,4 +309,4 @@ declare const rateLimiter: <E extends Env = Env>(options?: RateLimitOptions<E>)
232
309
  */
233
310
  declare const cloudflareRateLimiter: <E extends Env = Env>(options: CloudflareRateLimitOptions<E>) => MiddlewareHandler<E>;
234
311
 
235
- export { type Algorithm, type CloudflareRateLimitOptions, type HeadersFormat, MemoryStore, type RateLimitBinding, type RateLimitInfo, type RateLimitOptions, type RateLimitStore, type RateLimitStoreAccess, type StoreResult, cloudflareRateLimiter, getClientIP, rateLimiter };
312
+ export { type Algorithm, type CloudflareRateLimitOptions, type HeadersFormat, MemoryStore, type QuotaUnit, type RateLimitBinding, type RateLimitInfo, type RateLimitOptions, type RateLimitStore, type RateLimitStoreAccess, type StoreResult, cloudflareRateLimiter, getClientIP, rateLimiter, shutdownDefaultStore };
package/dist/index.js CHANGED
@@ -55,21 +55,47 @@ var MemoryStore = class {
55
55
  }
56
56
  };
57
57
  var defaultStore;
58
- function setHeaders(c, info, format, windowMs) {
58
+ function shutdownDefaultStore() {
59
+ if (defaultStore) {
60
+ defaultStore.shutdown();
61
+ defaultStore = void 0;
62
+ }
63
+ }
64
+ function setHeaders(c, info, format, windowMs, identifier, quotaUnit) {
59
65
  if (format === false) {
60
66
  return;
61
67
  }
62
68
  const windowSeconds = Math.ceil(windowMs / 1e3);
63
69
  const resetSeconds = Math.max(0, Math.ceil((info.reset - Date.now()) / 1e3));
64
- c.header("RateLimit-Policy", `${info.limit};w=${windowSeconds}`);
70
+ const safeId = sanitizeIdentifier(identifier);
65
71
  switch (format) {
72
+ case "standard":
73
+ {
74
+ let policy = `"${safeId}";q=${info.limit};w=${windowSeconds}`;
75
+ if (quotaUnit !== "requests") {
76
+ policy += `;qu="${quotaUnit}"`;
77
+ }
78
+ c.header("RateLimit-Policy", policy);
79
+ c.header(
80
+ "RateLimit",
81
+ `"${safeId}";r=${info.remaining};t=${resetSeconds}`
82
+ );
83
+ }
84
+ break;
66
85
  case "draft-7":
86
+ c.header("RateLimit-Policy", `${info.limit};w=${windowSeconds}`);
67
87
  c.header(
68
88
  "RateLimit",
69
89
  `limit=${info.limit}, remaining=${info.remaining}, reset=${resetSeconds}`
70
90
  );
71
91
  break;
72
92
  case "draft-6":
93
+ c.header("RateLimit-Policy", `${info.limit};w=${windowSeconds}`);
94
+ c.header("RateLimit-Limit", String(info.limit));
95
+ c.header("RateLimit-Remaining", String(info.remaining));
96
+ c.header("RateLimit-Reset", String(resetSeconds));
97
+ break;
98
+ case "legacy":
73
99
  default:
74
100
  c.header("X-RateLimit-Limit", String(info.limit));
75
101
  c.header("X-RateLimit-Remaining", String(info.remaining));
@@ -77,6 +103,16 @@ function setHeaders(c, info, format, windowMs) {
77
103
  break;
78
104
  }
79
105
  }
106
+ function sanitizeIdentifier(id) {
107
+ if (!id || typeof id !== "string") {
108
+ return "default";
109
+ }
110
+ const sanitized = id.replace(/[^a-zA-Z0-9_\-.:*/]/g, "-");
111
+ if (!sanitized || !/^[a-zA-Z]/.test(sanitized)) {
112
+ return "default";
113
+ }
114
+ return sanitized;
115
+ }
80
116
  function getClientIP(c) {
81
117
  const cfIP = c.req.header("cf-connecting-ip");
82
118
  if (cfIP) {
@@ -92,14 +128,59 @@ function getClientIP(c) {
92
128
  }
93
129
  return "unknown";
94
130
  }
95
- function createDefaultResponse(info) {
96
- const retryAfter = Math.max(0, Math.ceil((info.reset - Date.now()) / 1e3));
131
+ function buildRateLimitHeaders(info, format, windowMs, identifier, quotaUnit) {
132
+ const headers = {
133
+ "Content-Type": "text/plain",
134
+ "Retry-After": String(
135
+ Math.max(0, Math.ceil((info.reset - Date.now()) / 1e3))
136
+ )
137
+ };
138
+ if (format === false) {
139
+ return headers;
140
+ }
141
+ const windowSeconds = Math.ceil(windowMs / 1e3);
142
+ const resetSeconds = Math.max(0, Math.ceil((info.reset - Date.now()) / 1e3));
143
+ const safeId = sanitizeIdentifier(identifier);
144
+ switch (format) {
145
+ case "standard": {
146
+ let policy = `"${safeId}";q=${info.limit};w=${windowSeconds}`;
147
+ if (quotaUnit !== "requests") {
148
+ policy += `;qu="${quotaUnit}"`;
149
+ }
150
+ headers["RateLimit-Policy"] = policy;
151
+ headers["RateLimit"] = `"${safeId}";r=${info.remaining};t=${resetSeconds}`;
152
+ break;
153
+ }
154
+ case "draft-7":
155
+ headers["RateLimit-Policy"] = `${info.limit};w=${windowSeconds}`;
156
+ headers["RateLimit"] = `limit=${info.limit}, remaining=${info.remaining}, reset=${resetSeconds}`;
157
+ break;
158
+ case "draft-6":
159
+ headers["RateLimit-Policy"] = `${info.limit};w=${windowSeconds}`;
160
+ headers["RateLimit-Limit"] = String(info.limit);
161
+ headers["RateLimit-Remaining"] = String(info.remaining);
162
+ headers["RateLimit-Reset"] = String(resetSeconds);
163
+ break;
164
+ case "legacy":
165
+ default:
166
+ headers["X-RateLimit-Limit"] = String(info.limit);
167
+ headers["X-RateLimit-Remaining"] = String(info.remaining);
168
+ headers["X-RateLimit-Reset"] = String(Math.ceil(info.reset / 1e3));
169
+ break;
170
+ }
171
+ return headers;
172
+ }
173
+ function createDefaultResponse(info, format, windowMs, identifier, quotaUnit) {
174
+ const headers = buildRateLimitHeaders(
175
+ info,
176
+ format,
177
+ windowMs,
178
+ identifier,
179
+ quotaUnit
180
+ );
97
181
  return new Response("Rate limit exceeded", {
98
182
  status: 429,
99
- headers: {
100
- "Content-Type": "text/plain",
101
- "Retry-After": String(retryAfter)
102
- }
183
+ headers
103
184
  });
104
185
  }
105
186
  async function checkSlidingWindow(store, key, limit, windowMs) {
@@ -145,19 +226,44 @@ var rateLimiter = (options) => {
145
226
  store: void 0,
146
227
  keyGenerator: getClientIP,
147
228
  handler: void 0,
148
- headers: "draft-6",
229
+ headers: "legacy",
230
+ identifier: "default",
231
+ quotaUnit: "requests",
149
232
  skip: void 0,
150
233
  skipSuccessfulRequests: false,
151
234
  skipFailedRequests: false,
152
235
  onRateLimited: void 0,
236
+ onStoreError: "allow",
153
237
  ...options
154
238
  };
155
239
  const store = opts.store ?? (defaultStore ??= new MemoryStore());
156
- let initialized = false;
240
+ let initPromise = null;
241
+ async function handleStoreError(error, c) {
242
+ if (typeof opts.onStoreError === "function") {
243
+ return opts.onStoreError(error, c);
244
+ }
245
+ return opts.onStoreError === "allow";
246
+ }
157
247
  return async function rateLimiter2(c, next) {
158
- if (!initialized && store.init) {
159
- await store.init(opts.windowMs);
160
- initialized = true;
248
+ if (!initPromise && store.init) {
249
+ const result = store.init(opts.windowMs);
250
+ initPromise = result instanceof Promise ? result : Promise.resolve();
251
+ }
252
+ if (initPromise) {
253
+ try {
254
+ await initPromise;
255
+ } catch (error) {
256
+ const shouldAllow = await handleStoreError(
257
+ error instanceof Error ? error : new Error(String(error)),
258
+ c
259
+ );
260
+ if (shouldAllow) {
261
+ return next();
262
+ }
263
+ return new Response("Rate limiter initialization failed", {
264
+ status: 500
265
+ });
266
+ }
161
267
  }
162
268
  if (opts.skip) {
163
269
  const shouldSkip = await opts.skip(c);
@@ -167,13 +273,35 @@ var rateLimiter = (options) => {
167
273
  }
168
274
  const key = await opts.keyGenerator(c);
169
275
  const limit = typeof opts.limit === "function" ? await opts.limit(c) : opts.limit;
170
- const { allowed, info } = opts.algorithm === "sliding-window" ? await checkSlidingWindow(store, key, limit, opts.windowMs) : await checkFixedWindow(store, key, limit, opts.windowMs);
276
+ let allowed;
277
+ let info;
278
+ try {
279
+ const result = opts.algorithm === "sliding-window" ? await checkSlidingWindow(store, key, limit, opts.windowMs) : await checkFixedWindow(store, key, limit, opts.windowMs);
280
+ allowed = result.allowed;
281
+ info = result.info;
282
+ } catch (error) {
283
+ const shouldAllow = await handleStoreError(
284
+ error instanceof Error ? error : new Error(String(error)),
285
+ c
286
+ );
287
+ if (shouldAllow) {
288
+ return next();
289
+ }
290
+ return new Response("Rate limiter error", { status: 500 });
291
+ }
171
292
  c.set("rateLimit", info);
172
293
  c.set("rateLimitStore", {
173
294
  getKey: store.get?.bind(store) ?? (() => void 0),
174
295
  resetKey: store.resetKey.bind(store)
175
296
  });
176
- setHeaders(c, info, opts.headers, opts.windowMs);
297
+ setHeaders(
298
+ c,
299
+ info,
300
+ opts.headers,
301
+ opts.windowMs,
302
+ opts.identifier,
303
+ opts.quotaUnit
304
+ );
177
305
  if (!allowed) {
178
306
  if (opts.onRateLimited) {
179
307
  await opts.onRateLimited(c, info);
@@ -181,7 +309,13 @@ var rateLimiter = (options) => {
181
309
  if (opts.handler) {
182
310
  return opts.handler(c, info);
183
311
  }
184
- return createDefaultResponse(info);
312
+ return createDefaultResponse(
313
+ info,
314
+ opts.headers,
315
+ opts.windowMs,
316
+ opts.identifier,
317
+ opts.quotaUnit
318
+ );
185
319
  }
186
320
  await next();
187
321
  if (opts.skipSuccessfulRequests || opts.skipFailedRequests) {
@@ -190,7 +324,10 @@ var rateLimiter = (options) => {
190
324
  if (shouldDecrement && store.decrement) {
191
325
  const windowStart = Math.floor(Date.now() / opts.windowMs) * opts.windowMs;
192
326
  const windowKey = `${key}:${windowStart}`;
193
- await store.decrement(windowKey);
327
+ try {
328
+ await store.decrement(windowKey);
329
+ } catch {
330
+ }
194
331
  }
195
332
  }
196
333
  };
@@ -223,6 +360,7 @@ export {
223
360
  MemoryStore,
224
361
  cloudflareRateLimiter,
225
362
  getClientIP,
226
- rateLimiter
363
+ rateLimiter,
364
+ shutdownDefaultStore
227
365
  };
228
366
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * @module\n * Rate Limit Middleware for Hono.\n */\n\nimport type { Context, Env, MiddlewareHandler } from \"hono\";\n\n// ============================================================================\n// Types\n// ============================================================================\n\n/**\n * Rate limit information for a single request\n */\nexport type RateLimitInfo = {\n /** Maximum requests allowed in window */\n limit: number;\n /** Remaining requests in current window */\n remaining: number;\n /** Unix timestamp (ms) when window resets */\n reset: number;\n};\n\n/**\n * Result from store increment operation\n */\nexport type StoreResult = {\n /** Current request count in window */\n count: number;\n /** When the window resets (Unix timestamp ms) */\n reset: number;\n};\n\n/**\n * Header format versions\n */\nexport type HeadersFormat =\n | \"draft-6\" // X-RateLimit-* headers\n | \"draft-7\" // RateLimit-* without structured fields\n | false; // Disable headers\n\n/**\n * Rate limit algorithm\n */\nexport type Algorithm = \"fixed-window\" | \"sliding-window\";\n\n/**\n * Store interface for rate limit state\n */\nexport type RateLimitStore = {\n /**\n * Initialize store. Called once before first use.\n */\n init?: (windowMs: number) => void | Promise<void>;\n\n /**\n * Increment counter for key and return current state.\n */\n increment: (key: string) => StoreResult | Promise<StoreResult>;\n\n /**\n * Decrement counter for key.\n */\n decrement?: (key: string) => void | Promise<void>;\n\n /**\n * Reset a specific key.\n */\n resetKey: (key: string) => void | Promise<void>;\n\n /**\n * Reset all keys.\n */\n resetAll?: () => void | Promise<void>;\n\n /**\n * Get current state for key.\n */\n get?: (\n key: string,\n ) => StoreResult | Promise<StoreResult | undefined> | undefined;\n\n /**\n * Graceful shutdown.\n */\n shutdown?: () => void | Promise<void>;\n};\n\n/**\n * Store access interface exposed in context\n */\nexport type RateLimitStoreAccess = {\n /** Get rate limit info for a key */\n getKey: (\n key: string,\n ) => StoreResult | Promise<StoreResult | undefined> | undefined;\n /** Reset rate limit for a key */\n resetKey: (key: string) => void | Promise<void>;\n};\n\n/**\n * Options for rate limit middleware\n */\nexport type RateLimitOptions<E extends Env = Env> = {\n /**\n * Maximum requests allowed in the time window.\n * @default 60\n */\n limit?: number | ((c: Context<E>) => number | Promise<number>);\n\n /**\n * Time window in milliseconds.\n * @default 60000 (1 minute)\n */\n windowMs?: number;\n\n /**\n * Rate limiting algorithm.\n * @default 'sliding-window'\n */\n algorithm?: Algorithm;\n\n /**\n * Storage backend for rate limit state.\n * @default MemoryStore\n */\n store?: RateLimitStore;\n\n /**\n * Generate unique key for each client.\n * @default IP address from headers\n */\n keyGenerator?: (c: Context<E>) => string | Promise<string>;\n\n /**\n * Handler called when rate limit is exceeded.\n */\n handler?: (\n c: Context<E>,\n info: RateLimitInfo,\n ) => Response | Promise<Response>;\n\n /**\n * HTTP header format to use.\n * @default 'draft-6'\n */\n headers?: HeadersFormat;\n\n /**\n * Skip rate limiting for certain requests.\n */\n skip?: (c: Context<E>) => boolean | Promise<boolean>;\n\n /**\n * Don't count successful (2xx) requests against limit.\n * @default false\n */\n skipSuccessfulRequests?: boolean;\n\n /**\n * Don't count failed (4xx, 5xx) requests against limit.\n * @default false\n */\n skipFailedRequests?: boolean;\n\n /**\n * Callback when a request is rate limited.\n */\n onRateLimited?: (c: Context<E>, info: RateLimitInfo) => void | Promise<void>;\n};\n\n/**\n * Cloudflare Rate Limiting binding interface\n */\nexport type RateLimitBinding = {\n limit: (options: { key: string }) => Promise<{ success: boolean }>;\n};\n\n/**\n * Options for Cloudflare Rate Limiting binding\n */\nexport type CloudflareRateLimitOptions<E extends Env = Env> = {\n /**\n * Cloudflare Rate Limiting binding from env\n */\n binding: RateLimitBinding | ((c: Context<E>) => RateLimitBinding);\n\n /**\n * Generate unique key for each client.\n */\n keyGenerator: (c: Context<E>) => string | Promise<string>;\n\n /**\n * Handler called when rate limit is exceeded.\n */\n handler?: (c: Context<E>) => Response | Promise<Response>;\n\n /**\n * Skip rate limiting for certain requests.\n */\n skip?: (c: Context<E>) => boolean | Promise<boolean>;\n};\n\n// ============================================================================\n// Context Variable Type Extension\n// ============================================================================\n\ndeclare module \"hono\" {\n interface ContextVariableMap {\n rateLimit?: RateLimitInfo;\n rateLimitStore?: RateLimitStoreAccess;\n }\n}\n\n// ============================================================================\n// Memory Store\n// ============================================================================\n\ntype MemoryEntry = {\n count: number;\n reset: number;\n};\n\n/**\n * In-memory store for rate limiting.\n * Suitable for single-instance deployments.\n */\nexport class MemoryStore implements RateLimitStore {\n private entries = new Map<string, MemoryEntry>();\n private windowMs = 60_000;\n private cleanupTimer?: ReturnType<typeof setInterval>;\n\n init(windowMs: number): void {\n this.windowMs = windowMs;\n\n // Cleanup expired entries every minute\n this.cleanupTimer = setInterval(() => {\n const now = Date.now();\n for (const [key, entry] of this.entries) {\n if (entry.reset <= now) {\n this.entries.delete(key);\n }\n }\n }, 60_000);\n\n // Don't keep process alive for cleanup\n if (typeof this.cleanupTimer.unref === \"function\") {\n this.cleanupTimer.unref();\n }\n }\n\n increment(key: string): StoreResult {\n const now = Date.now();\n const existing = this.entries.get(key);\n\n if (!existing || existing.reset <= now) {\n // New window\n const reset = now + this.windowMs;\n this.entries.set(key, { count: 1, reset });\n return { count: 1, reset };\n }\n\n // Increment existing\n existing.count++;\n return { count: existing.count, reset: existing.reset };\n }\n\n get(key: string): StoreResult | undefined {\n const entry = this.entries.get(key);\n if (!entry || entry.reset <= Date.now()) {\n return undefined;\n }\n return { count: entry.count, reset: entry.reset };\n }\n\n decrement(key: string): void {\n const entry = this.entries.get(key);\n if (entry && entry.count > 0) {\n entry.count--;\n }\n }\n\n resetKey(key: string): void {\n this.entries.delete(key);\n }\n\n resetAll(): void {\n this.entries.clear();\n }\n\n shutdown(): void {\n if (this.cleanupTimer) {\n clearInterval(this.cleanupTimer);\n }\n this.entries.clear();\n }\n}\n\n// Singleton default store\nlet defaultStore: MemoryStore | undefined;\n\n// ============================================================================\n// Header Generation\n// ============================================================================\n\nfunction setHeaders(\n c: Context,\n info: RateLimitInfo,\n format: HeadersFormat,\n windowMs: number,\n): void {\n if (format === false) {\n return;\n }\n\n const windowSeconds = Math.ceil(windowMs / 1000);\n const resetSeconds = Math.max(0, Math.ceil((info.reset - Date.now()) / 1000));\n\n // RateLimit-Policy header (IETF compliant)\n c.header(\"RateLimit-Policy\", `${info.limit};w=${windowSeconds}`);\n\n switch (format) {\n case \"draft-7\":\n c.header(\n \"RateLimit\",\n `limit=${info.limit}, remaining=${info.remaining}, reset=${resetSeconds}`,\n );\n break;\n\n case \"draft-6\":\n default:\n c.header(\"X-RateLimit-Limit\", String(info.limit));\n c.header(\"X-RateLimit-Remaining\", String(info.remaining));\n c.header(\"X-RateLimit-Reset\", String(Math.ceil(info.reset / 1000)));\n break;\n }\n}\n\n// ============================================================================\n// Default Key Generator\n// ============================================================================\n\nfunction getClientIP(c: Context): string {\n // Platform-specific headers (most reliable)\n const cfIP = c.req.header(\"cf-connecting-ip\");\n if (cfIP) {\n return cfIP;\n }\n\n const xRealIP = c.req.header(\"x-real-ip\");\n if (xRealIP) {\n return xRealIP;\n }\n\n // X-Forwarded-For - take first IP\n const xff = c.req.header(\"x-forwarded-for\");\n if (xff) {\n return xff.split(\",\")[0].trim();\n }\n\n return \"unknown\";\n}\n\n// ============================================================================\n// Default Handler\n// ============================================================================\n\nfunction createDefaultResponse(info: RateLimitInfo): Response {\n const retryAfter = Math.max(0, Math.ceil((info.reset - Date.now()) / 1000));\n\n return new Response(\"Rate limit exceeded\", {\n status: 429,\n headers: {\n \"Content-Type\": \"text/plain\",\n \"Retry-After\": String(retryAfter),\n },\n });\n}\n\n// ============================================================================\n// Sliding Window Algorithm\n// ============================================================================\n\nasync function checkSlidingWindow(\n store: RateLimitStore,\n key: string,\n limit: number,\n windowMs: number,\n): Promise<{ allowed: boolean; info: RateLimitInfo }> {\n const now = Date.now();\n const currentWindowStart = Math.floor(now / windowMs) * windowMs;\n const previousWindowStart = currentWindowStart - windowMs;\n\n const previousKey = `${key}:${previousWindowStart}`;\n const currentKey = `${key}:${currentWindowStart}`;\n\n // Increment current window\n const current = await store.increment(currentKey);\n\n // Get previous window (may not exist)\n let previousCount = 0;\n if (store.get) {\n const prev = await store.get(previousKey);\n previousCount = prev?.count ?? 0;\n }\n\n // Cloudflare's weighted formula\n const elapsedMs = now - currentWindowStart;\n const weight = (windowMs - elapsedMs) / windowMs;\n const estimatedCount = Math.floor(previousCount * weight) + current.count;\n\n const remaining = Math.max(0, limit - estimatedCount);\n const allowed = estimatedCount <= limit;\n const reset = currentWindowStart + windowMs;\n\n return {\n allowed,\n info: { limit, remaining, reset },\n };\n}\n\n// ============================================================================\n// Fixed Window Algorithm\n// ============================================================================\n\nasync function checkFixedWindow(\n store: RateLimitStore,\n key: string,\n limit: number,\n windowMs: number,\n): Promise<{ allowed: boolean; info: RateLimitInfo }> {\n const now = Date.now();\n const windowStart = Math.floor(now / windowMs) * windowMs;\n const windowKey = `${key}:${windowStart}`;\n\n const { count, reset } = await store.increment(windowKey);\n\n const remaining = Math.max(0, limit - count);\n const allowed = count <= limit;\n\n return {\n allowed,\n info: { limit, remaining, reset },\n };\n}\n\n// ============================================================================\n// Main Middleware\n// ============================================================================\n\n/**\n * Rate Limit Middleware for Hono.\n *\n * @param {RateLimitOptions} [options] - Configuration options\n * @returns {MiddlewareHandler} Middleware handler\n *\n * @example\n * ```ts\n * import { Hono } from 'hono'\n * import { rateLimiter } from '@jellyfungus/hono-rate-limiter'\n *\n * const app = new Hono()\n *\n * // Basic usage - 60 requests per minute\n * app.use(rateLimiter())\n *\n * // Custom configuration\n * app.use('/api/*', rateLimiter({\n * limit: 100,\n * windowMs: 60 * 1000,\n * }))\n * ```\n */\nexport const rateLimiter = <E extends Env = Env>(\n options?: RateLimitOptions<E>,\n): MiddlewareHandler<E> => {\n // Merge with defaults\n const opts = {\n limit: 60 as number | ((c: Context<E>) => number | Promise<number>),\n windowMs: 60_000,\n algorithm: \"sliding-window\" as Algorithm,\n store: undefined as RateLimitStore | undefined,\n keyGenerator: getClientIP as (c: Context<E>) => string | Promise<string>,\n handler: undefined as\n | ((c: Context<E>, info: RateLimitInfo) => Response | Promise<Response>)\n | undefined,\n headers: \"draft-6\" as HeadersFormat,\n skip: undefined as\n | ((c: Context<E>) => boolean | Promise<boolean>)\n | undefined,\n skipSuccessfulRequests: false,\n skipFailedRequests: false,\n onRateLimited: undefined as\n | ((c: Context<E>, info: RateLimitInfo) => void | Promise<void>)\n | undefined,\n ...options,\n };\n\n // Use default store if none provided\n const store = opts.store ?? (defaultStore ??= new MemoryStore());\n\n // Track initialization\n let initialized = false;\n\n return async function rateLimiter(c, next) {\n // Initialize store on first request\n if (!initialized && store.init) {\n await store.init(opts.windowMs);\n initialized = true;\n }\n\n // Check if should skip\n if (opts.skip) {\n const shouldSkip = await opts.skip(c);\n if (shouldSkip) {\n return next();\n }\n }\n\n // Generate key\n const key = await opts.keyGenerator(c);\n\n // Get limit (may be dynamic)\n const limit =\n typeof opts.limit === \"function\" ? await opts.limit(c) : opts.limit;\n\n // Check rate limit\n const { allowed, info } =\n opts.algorithm === \"sliding-window\"\n ? await checkSlidingWindow(store, key, limit, opts.windowMs)\n : await checkFixedWindow(store, key, limit, opts.windowMs);\n\n // Set context variable for downstream middleware\n c.set(\"rateLimit\", info);\n\n // Expose store access in context\n c.set(\"rateLimitStore\", {\n getKey: store.get?.bind(store) ?? (() => undefined),\n resetKey: store.resetKey.bind(store),\n });\n\n // Set headers\n setHeaders(c, info, opts.headers, opts.windowMs);\n\n // Handle rate limited\n if (!allowed) {\n // Fire callback\n if (opts.onRateLimited) {\n await opts.onRateLimited(c, info);\n }\n\n // Custom handler or default\n if (opts.handler) {\n return opts.handler(c, info);\n }\n return createDefaultResponse(info);\n }\n\n // Continue\n await next();\n\n // Handle skip options after response\n if (opts.skipSuccessfulRequests || opts.skipFailedRequests) {\n const status = c.res.status;\n const shouldDecrement =\n (opts.skipSuccessfulRequests && status >= 200 && status < 300) ||\n (opts.skipFailedRequests && status >= 400);\n\n if (shouldDecrement && store.decrement) {\n const windowStart =\n Math.floor(Date.now() / opts.windowMs) * opts.windowMs;\n const windowKey = `${key}:${windowStart}`;\n await store.decrement(windowKey);\n }\n }\n };\n};\n\n// ============================================================================\n// Cloudflare Rate Limiting Binding Middleware\n// ============================================================================\n\n/**\n * Rate limiter using Cloudflare's built-in Rate Limiting binding.\n *\n * This uses Cloudflare's globally distributed rate limiting infrastructure,\n * which is ideal for high-traffic applications.\n *\n * @example\n * ```ts\n * import { cloudflareRateLimiter } from '@jellyfungus/hono-rate-limiter'\n *\n * type Bindings = { RATE_LIMITER: RateLimitBinding }\n *\n * const app = new Hono<{ Bindings: Bindings }>()\n *\n * app.use(cloudflareRateLimiter({\n * binding: (c) => c.env.RATE_LIMITER,\n * keyGenerator: (c) => c.req.header('cf-connecting-ip') ?? 'unknown',\n * }))\n * ```\n */\nexport const cloudflareRateLimiter = <E extends Env = Env>(\n options: CloudflareRateLimitOptions<E>,\n): MiddlewareHandler<E> => {\n const { binding, keyGenerator, handler, skip } = options;\n\n return async function cloudflareRateLimiter(c, next) {\n // Check if should skip\n if (skip) {\n const shouldSkip = await skip(c);\n if (shouldSkip) {\n return next();\n }\n }\n\n // Get binding (may be dynamic)\n const rateLimitBinding =\n typeof binding === \"function\" ? binding(c) : binding;\n\n // Generate key\n const key = await keyGenerator(c);\n\n // Check rate limit\n const { success } = await rateLimitBinding.limit({ key });\n\n if (!success) {\n if (handler) {\n return handler(c);\n }\n return new Response(\"Rate limit exceeded\", {\n status: 429,\n headers: { \"Content-Type\": \"text/plain\" },\n });\n }\n\n return next();\n };\n};\n\n// ============================================================================\n// Exports\n// ============================================================================\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index.ts"],"sourcesContent":["/**\n * @module\n * Rate Limit Middleware for Hono.\n */\n\nimport type { Context, Env, MiddlewareHandler } from \"hono\";\n\n// ============================================================================\n// Types\n// ============================================================================\n\n/**\n * Rate limit information for a single request\n */\nexport type RateLimitInfo = {\n /** Maximum requests allowed in window */\n limit: number;\n /** Remaining requests in current window */\n remaining: number;\n /** Unix timestamp (ms) when window resets */\n reset: number;\n};\n\n/**\n * Result from store increment operation\n */\nexport type StoreResult = {\n /** Current request count in window */\n count: number;\n /** When the window resets (Unix timestamp ms) */\n reset: number;\n};\n\n/**\n * Quota unit for IETF standard headers.\n * @see https://datatracker.ietf.org/doc/draft-ietf-httpapi-ratelimit-headers/\n */\nexport type QuotaUnit = \"requests\" | \"content-bytes\" | \"concurrent-requests\";\n\n/**\n * Header format options.\n *\n * ## \"legacy\" (default)\n * Common X-RateLimit-* headers used by GitHub, Twitter, and most APIs:\n * - `X-RateLimit-Limit`: max requests in window\n * - `X-RateLimit-Remaining`: remaining requests\n * - `X-RateLimit-Reset`: Unix timestamp (seconds) when window resets\n *\n * ## \"draft-6\"\n * IETF draft-06 format with individual RateLimit-* headers:\n * - `RateLimit-Policy`: policy description (e.g., `100;w=60`)\n * - `RateLimit-Limit`: max requests\n * - `RateLimit-Remaining`: remaining requests\n * - `RateLimit-Reset`: seconds until reset\n *\n * ## \"draft-7\"\n * IETF draft-07 format with combined RateLimit header:\n * - `RateLimit-Policy`: policy description\n * - `RateLimit`: combined (e.g., `limit=100, remaining=50, reset=30`)\n *\n * ## \"standard\"\n * Current IETF draft-08+ format with structured field values (RFC 9651):\n * - `RateLimit-Policy`: `\"name\";q=100;w=60`\n * - `RateLimit`: `\"name\";r=50;t=30`\n *\n * ## false\n * Disable all rate limit headers.\n *\n * @see https://datatracker.ietf.org/doc/draft-ietf-httpapi-ratelimit-headers/\n */\nexport type HeadersFormat =\n | \"legacy\" // X-RateLimit-* headers (GitHub/Twitter style)\n | \"draft-6\" // IETF draft-06: individual RateLimit-* headers\n | \"draft-7\" // IETF draft-07: combined RateLimit header\n | \"standard\" // IETF draft-08+: structured field format (current)\n | false; // Disable headers\n\n/**\n * Rate limit algorithm\n */\nexport type Algorithm = \"fixed-window\" | \"sliding-window\";\n\n/**\n * Store interface for rate limit state\n */\nexport type RateLimitStore = {\n /**\n * Initialize store. Called once before first use.\n */\n init?: (windowMs: number) => void | Promise<void>;\n\n /**\n * Increment counter for key and return current state.\n */\n increment: (key: string) => StoreResult | Promise<StoreResult>;\n\n /**\n * Decrement counter for key.\n */\n decrement?: (key: string) => void | Promise<void>;\n\n /**\n * Reset a specific key.\n */\n resetKey: (key: string) => void | Promise<void>;\n\n /**\n * Reset all keys.\n */\n resetAll?: () => void | Promise<void>;\n\n /**\n * Get current state for key.\n */\n get?: (\n key: string,\n ) => StoreResult | Promise<StoreResult | undefined> | undefined;\n\n /**\n * Graceful shutdown.\n */\n shutdown?: () => void | Promise<void>;\n};\n\n/**\n * Store access interface exposed in context\n */\nexport type RateLimitStoreAccess = {\n /** Get rate limit info for a key */\n getKey: (\n key: string,\n ) => StoreResult | Promise<StoreResult | undefined> | undefined;\n /** Reset rate limit for a key */\n resetKey: (key: string) => void | Promise<void>;\n};\n\n/**\n * Options for rate limit middleware\n */\nexport type RateLimitOptions<E extends Env = Env> = {\n /**\n * Maximum requests allowed in the time window.\n * @default 60\n */\n limit?: number | ((c: Context<E>) => number | Promise<number>);\n\n /**\n * Time window in milliseconds.\n * @default 60000 (1 minute)\n */\n windowMs?: number;\n\n /**\n * Rate limiting algorithm.\n * @default 'sliding-window'\n */\n algorithm?: Algorithm;\n\n /**\n * Storage backend for rate limit state.\n * @default MemoryStore\n */\n store?: RateLimitStore;\n\n /**\n * Generate unique key for each client.\n * @default IP address from headers\n */\n keyGenerator?: (c: Context<E>) => string | Promise<string>;\n\n /**\n * Handler called when rate limit is exceeded.\n */\n handler?: (\n c: Context<E>,\n info: RateLimitInfo,\n ) => Response | Promise<Response>;\n\n /**\n * HTTP header format to use.\n *\n * - \"legacy\": X-RateLimit-* headers (GitHub/Twitter style, default)\n * - \"draft-6\": IETF draft-06 individual headers\n * - \"draft-7\": IETF draft-07 combined header\n * - \"standard\": IETF draft-08+ structured fields (current spec)\n * - false: Disable headers\n *\n * @default 'legacy'\n */\n headers?: HeadersFormat;\n\n /**\n * Policy identifier for IETF headers (draft-6+).\n * Used in RateLimit and RateLimit-Policy headers.\n * @default 'default'\n */\n identifier?: string;\n\n /**\n * Quota unit for IETF standard headers.\n * Only included in \"standard\" format when not \"requests\".\n * @default 'requests'\n */\n quotaUnit?: QuotaUnit;\n\n /**\n * Skip rate limiting for certain requests.\n */\n skip?: (c: Context<E>) => boolean | Promise<boolean>;\n\n /**\n * Don't count successful (2xx) requests against limit.\n * @default false\n */\n skipSuccessfulRequests?: boolean;\n\n /**\n * Don't count failed (4xx, 5xx) requests against limit.\n * @default false\n */\n skipFailedRequests?: boolean;\n\n /**\n * Callback when a request is rate limited.\n */\n onRateLimited?: (c: Context<E>, info: RateLimitInfo) => void | Promise<void>;\n\n /**\n * Behavior when store operations fail.\n *\n * - 'allow': Allow the request through (fail-open, default)\n * - 'deny': Block the request with 500 error (fail-closed)\n * - Function: Custom handler returning true to allow, false to deny\n *\n * @default 'allow'\n */\n onStoreError?:\n | \"allow\"\n | \"deny\"\n | ((error: Error, c: Context<E>) => boolean | Promise<boolean>);\n};\n\n/**\n * Cloudflare Rate Limiting binding interface\n */\nexport type RateLimitBinding = {\n limit: (options: { key: string }) => Promise<{ success: boolean }>;\n};\n\n/**\n * Options for Cloudflare Rate Limiting binding\n */\nexport type CloudflareRateLimitOptions<E extends Env = Env> = {\n /**\n * Cloudflare Rate Limiting binding from env\n */\n binding: RateLimitBinding | ((c: Context<E>) => RateLimitBinding);\n\n /**\n * Generate unique key for each client.\n */\n keyGenerator: (c: Context<E>) => string | Promise<string>;\n\n /**\n * Handler called when rate limit is exceeded.\n */\n handler?: (c: Context<E>) => Response | Promise<Response>;\n\n /**\n * Skip rate limiting for certain requests.\n */\n skip?: (c: Context<E>) => boolean | Promise<boolean>;\n};\n\n// ============================================================================\n// Context Variable Type Extension\n// ============================================================================\n\ndeclare module \"hono\" {\n interface ContextVariableMap {\n rateLimit?: RateLimitInfo;\n rateLimitStore?: RateLimitStoreAccess;\n }\n}\n\n// ============================================================================\n// Memory Store\n// ============================================================================\n\ntype MemoryEntry = {\n count: number;\n reset: number;\n};\n\n/**\n * In-memory store for rate limiting.\n * Suitable for single-instance deployments.\n */\nexport class MemoryStore implements RateLimitStore {\n private entries = new Map<string, MemoryEntry>();\n private windowMs = 60_000;\n private cleanupTimer?: ReturnType<typeof setInterval>;\n\n init(windowMs: number): void {\n this.windowMs = windowMs;\n\n // Cleanup expired entries every minute\n this.cleanupTimer = setInterval(() => {\n const now = Date.now();\n for (const [key, entry] of this.entries) {\n if (entry.reset <= now) {\n this.entries.delete(key);\n }\n }\n }, 60_000);\n\n // Don't keep process alive for cleanup\n if (typeof this.cleanupTimer.unref === \"function\") {\n this.cleanupTimer.unref();\n }\n }\n\n increment(key: string): StoreResult {\n const now = Date.now();\n const existing = this.entries.get(key);\n\n if (!existing || existing.reset <= now) {\n // New window\n const reset = now + this.windowMs;\n this.entries.set(key, { count: 1, reset });\n return { count: 1, reset };\n }\n\n // Increment existing\n existing.count++;\n return { count: existing.count, reset: existing.reset };\n }\n\n get(key: string): StoreResult | undefined {\n const entry = this.entries.get(key);\n if (!entry || entry.reset <= Date.now()) {\n return undefined;\n }\n return { count: entry.count, reset: entry.reset };\n }\n\n decrement(key: string): void {\n const entry = this.entries.get(key);\n if (entry && entry.count > 0) {\n entry.count--;\n }\n }\n\n resetKey(key: string): void {\n this.entries.delete(key);\n }\n\n resetAll(): void {\n this.entries.clear();\n }\n\n shutdown(): void {\n if (this.cleanupTimer) {\n clearInterval(this.cleanupTimer);\n }\n this.entries.clear();\n }\n}\n\n// Singleton default store\nlet defaultStore: MemoryStore | undefined;\n\n/**\n * Shutdown the default memory store.\n * Call this during graceful shutdown to clean up timers.\n *\n * @example\n * ```ts\n * import { shutdownDefaultStore } from '@jellyfungus/hono-rate-limiter'\n *\n * process.on('SIGTERM', () => {\n * shutdownDefaultStore()\n * process.exit(0)\n * })\n * ```\n */\nexport function shutdownDefaultStore(): void {\n if (defaultStore) {\n defaultStore.shutdown();\n defaultStore = undefined;\n }\n}\n\n// ============================================================================\n// Header Generation\n// ============================================================================\n\n/**\n * Set rate limit response headers based on the configured format.\n *\n * @see https://datatracker.ietf.org/doc/draft-ietf-httpapi-ratelimit-headers/\n */\nfunction setHeaders(\n c: Context,\n info: RateLimitInfo,\n format: HeadersFormat,\n windowMs: number,\n identifier: string,\n quotaUnit: QuotaUnit,\n): void {\n if (format === false) {\n return;\n }\n\n const windowSeconds = Math.ceil(windowMs / 1000);\n const resetSeconds = Math.max(0, Math.ceil((info.reset - Date.now()) / 1000));\n const safeId = sanitizeIdentifier(identifier);\n\n switch (format) {\n case \"standard\":\n // IETF draft-08+ (current): Structured field values per RFC 9651\n // RateLimit-Policy: describes the quota policy\n // Format: \"name\";q=<quota>;w=<window>[;qu=\"<unit>\"]\n {\n let policy = `\"${safeId}\";q=${info.limit};w=${windowSeconds}`;\n if (quotaUnit !== \"requests\") {\n policy += `;qu=\"${quotaUnit}\"`;\n }\n c.header(\"RateLimit-Policy\", policy);\n // RateLimit: describes current service limits\n // Format: \"name\";r=<remaining>;t=<reset>\n c.header(\n \"RateLimit\",\n `\"${safeId}\";r=${info.remaining};t=${resetSeconds}`,\n );\n }\n break;\n\n case \"draft-7\":\n // IETF draft-07: Combined RateLimit header with comma-separated values\n c.header(\"RateLimit-Policy\", `${info.limit};w=${windowSeconds}`);\n c.header(\n \"RateLimit\",\n `limit=${info.limit}, remaining=${info.remaining}, reset=${resetSeconds}`,\n );\n break;\n\n case \"draft-6\":\n // IETF draft-06: Individual RateLimit-* headers\n c.header(\"RateLimit-Policy\", `${info.limit};w=${windowSeconds}`);\n c.header(\"RateLimit-Limit\", String(info.limit));\n c.header(\"RateLimit-Remaining\", String(info.remaining));\n c.header(\"RateLimit-Reset\", String(resetSeconds));\n break;\n\n case \"legacy\":\n default:\n // Common X-RateLimit-* headers (GitHub, Twitter, most APIs)\n // Uses Unix timestamp for reset (seconds since epoch)\n c.header(\"X-RateLimit-Limit\", String(info.limit));\n c.header(\"X-RateLimit-Remaining\", String(info.remaining));\n c.header(\"X-RateLimit-Reset\", String(Math.ceil(info.reset / 1000)));\n break;\n }\n}\n\n// ============================================================================\n// Identifier Sanitization\n// ============================================================================\n\n/**\n * Sanitize identifier for RFC 9651 structured field compliance.\n * Identifiers are used in the RateLimit and RateLimit-Policy headers.\n */\nfunction sanitizeIdentifier(id: string): string {\n if (!id || typeof id !== \"string\") {\n return \"default\";\n }\n // RFC 9651 tokens: Only allow alphanumeric, underscore, hyphen, dot, colon, asterisk, slash\n // Must start with a letter\n const sanitized = id.replace(/[^a-zA-Z0-9_\\-.:*/]/g, \"-\");\n if (!sanitized || !/^[a-zA-Z]/.test(sanitized)) {\n return \"default\";\n }\n return sanitized;\n}\n\n// ============================================================================\n// Default Key Generator\n// ============================================================================\n\nfunction getClientIP(c: Context): string {\n // Platform-specific headers (most reliable)\n const cfIP = c.req.header(\"cf-connecting-ip\");\n if (cfIP) {\n return cfIP;\n }\n\n const xRealIP = c.req.header(\"x-real-ip\");\n if (xRealIP) {\n return xRealIP;\n }\n\n // X-Forwarded-For - take first IP\n const xff = c.req.header(\"x-forwarded-for\");\n if (xff) {\n return xff.split(\",\")[0].trim();\n }\n\n return \"unknown\";\n}\n\n// ============================================================================\n// Default Handler\n// ============================================================================\n\n/**\n * Build headers object for rate limit responses.\n * This ensures headers are included in both custom and default responses.\n */\nfunction buildRateLimitHeaders(\n info: RateLimitInfo,\n format: HeadersFormat,\n windowMs: number,\n identifier: string,\n quotaUnit: QuotaUnit,\n): Record<string, string> {\n const headers: Record<string, string> = {\n \"Content-Type\": \"text/plain\",\n \"Retry-After\": String(\n Math.max(0, Math.ceil((info.reset - Date.now()) / 1000)),\n ),\n };\n\n if (format === false) {\n return headers;\n }\n\n const windowSeconds = Math.ceil(windowMs / 1000);\n const resetSeconds = Math.max(0, Math.ceil((info.reset - Date.now()) / 1000));\n const safeId = sanitizeIdentifier(identifier);\n\n switch (format) {\n case \"standard\": {\n let policy = `\"${safeId}\";q=${info.limit};w=${windowSeconds}`;\n if (quotaUnit !== \"requests\") {\n policy += `;qu=\"${quotaUnit}\"`;\n }\n headers[\"RateLimit-Policy\"] = policy;\n headers[\"RateLimit\"] =\n `\"${safeId}\";r=${info.remaining};t=${resetSeconds}`;\n break;\n }\n case \"draft-7\":\n headers[\"RateLimit-Policy\"] = `${info.limit};w=${windowSeconds}`;\n headers[\"RateLimit\"] =\n `limit=${info.limit}, remaining=${info.remaining}, reset=${resetSeconds}`;\n break;\n case \"draft-6\":\n headers[\"RateLimit-Policy\"] = `${info.limit};w=${windowSeconds}`;\n headers[\"RateLimit-Limit\"] = String(info.limit);\n headers[\"RateLimit-Remaining\"] = String(info.remaining);\n headers[\"RateLimit-Reset\"] = String(resetSeconds);\n break;\n case \"legacy\":\n default:\n headers[\"X-RateLimit-Limit\"] = String(info.limit);\n headers[\"X-RateLimit-Remaining\"] = String(info.remaining);\n headers[\"X-RateLimit-Reset\"] = String(Math.ceil(info.reset / 1000));\n break;\n }\n\n return headers;\n}\n\nfunction createDefaultResponse(\n info: RateLimitInfo,\n format: HeadersFormat,\n windowMs: number,\n identifier: string,\n quotaUnit: QuotaUnit,\n): Response {\n const headers = buildRateLimitHeaders(\n info,\n format,\n windowMs,\n identifier,\n quotaUnit,\n );\n\n return new Response(\"Rate limit exceeded\", {\n status: 429,\n headers,\n });\n}\n\n// ============================================================================\n// Sliding Window Algorithm\n// ============================================================================\n\nasync function checkSlidingWindow(\n store: RateLimitStore,\n key: string,\n limit: number,\n windowMs: number,\n): Promise<{ allowed: boolean; info: RateLimitInfo }> {\n const now = Date.now();\n const currentWindowStart = Math.floor(now / windowMs) * windowMs;\n const previousWindowStart = currentWindowStart - windowMs;\n\n const previousKey = `${key}:${previousWindowStart}`;\n const currentKey = `${key}:${currentWindowStart}`;\n\n // Increment current window\n const current = await store.increment(currentKey);\n\n // Get previous window (may not exist)\n let previousCount = 0;\n if (store.get) {\n const prev = await store.get(previousKey);\n previousCount = prev?.count ?? 0;\n }\n\n // Cloudflare's weighted formula\n const elapsedMs = now - currentWindowStart;\n const weight = (windowMs - elapsedMs) / windowMs;\n const estimatedCount = Math.floor(previousCount * weight) + current.count;\n\n const remaining = Math.max(0, limit - estimatedCount);\n const allowed = estimatedCount <= limit;\n const reset = currentWindowStart + windowMs;\n\n return {\n allowed,\n info: { limit, remaining, reset },\n };\n}\n\n// ============================================================================\n// Fixed Window Algorithm\n// ============================================================================\n\nasync function checkFixedWindow(\n store: RateLimitStore,\n key: string,\n limit: number,\n windowMs: number,\n): Promise<{ allowed: boolean; info: RateLimitInfo }> {\n const now = Date.now();\n const windowStart = Math.floor(now / windowMs) * windowMs;\n const windowKey = `${key}:${windowStart}`;\n\n const { count, reset } = await store.increment(windowKey);\n\n const remaining = Math.max(0, limit - count);\n const allowed = count <= limit;\n\n return {\n allowed,\n info: { limit, remaining, reset },\n };\n}\n\n// ============================================================================\n// Main Middleware\n// ============================================================================\n\n/**\n * Rate Limit Middleware for Hono.\n *\n * @param {RateLimitOptions} [options] - Configuration options\n * @returns {MiddlewareHandler} Middleware handler\n *\n * @example\n * ```ts\n * import { Hono } from 'hono'\n * import { rateLimiter } from '@jellyfungus/hono-rate-limiter'\n *\n * const app = new Hono()\n *\n * // Basic usage - 60 requests per minute\n * app.use(rateLimiter())\n *\n * // Custom configuration\n * app.use('/api/*', rateLimiter({\n * limit: 100,\n * windowMs: 60 * 1000,\n * }))\n * ```\n */\nexport const rateLimiter = <E extends Env = Env>(\n options?: RateLimitOptions<E>,\n): MiddlewareHandler<E> => {\n // Merge with defaults\n const opts = {\n limit: 60 as number | ((c: Context<E>) => number | Promise<number>),\n windowMs: 60_000,\n algorithm: \"sliding-window\" as Algorithm,\n store: undefined as RateLimitStore | undefined,\n keyGenerator: getClientIP as (c: Context<E>) => string | Promise<string>,\n handler: undefined as\n | ((c: Context<E>, info: RateLimitInfo) => Response | Promise<Response>)\n | undefined,\n headers: \"legacy\" as HeadersFormat,\n identifier: \"default\",\n quotaUnit: \"requests\" as QuotaUnit,\n skip: undefined as\n | ((c: Context<E>) => boolean | Promise<boolean>)\n | undefined,\n skipSuccessfulRequests: false,\n skipFailedRequests: false,\n onRateLimited: undefined as\n | ((c: Context<E>, info: RateLimitInfo) => void | Promise<void>)\n | undefined,\n onStoreError: \"allow\" as\n | \"allow\"\n | \"deny\"\n | ((error: Error, c: Context<E>) => boolean | Promise<boolean>),\n ...options,\n };\n\n // Use default store if none provided\n const store = opts.store ?? (defaultStore ??= new MemoryStore());\n\n // Track initialization\n let initPromise: Promise<void> | null = null;\n\n /**\n * Handle store errors based on configuration.\n * @returns true to allow request, false to deny\n */\n async function handleStoreError(\n error: Error,\n c: Context<E>,\n ): Promise<boolean> {\n if (typeof opts.onStoreError === \"function\") {\n return opts.onStoreError(error, c);\n }\n // Default: fail-open (allow request through)\n return opts.onStoreError === \"allow\";\n }\n\n return async function rateLimiter(c, next) {\n // Initialize store on first request (with proper locking)\n if (!initPromise && store.init) {\n const result = store.init(opts.windowMs);\n // Handle both sync and async init\n initPromise = result instanceof Promise ? result : Promise.resolve();\n }\n if (initPromise) {\n try {\n await initPromise;\n } catch (error) {\n const shouldAllow = await handleStoreError(\n error instanceof Error ? error : new Error(String(error)),\n c,\n );\n if (shouldAllow) {\n return next();\n }\n return new Response(\"Rate limiter initialization failed\", {\n status: 500,\n });\n }\n }\n\n // Check if should skip\n if (opts.skip) {\n const shouldSkip = await opts.skip(c);\n if (shouldSkip) {\n return next();\n }\n }\n\n // Generate key\n const key = await opts.keyGenerator(c);\n\n // Get limit (may be dynamic)\n const limit =\n typeof opts.limit === \"function\" ? await opts.limit(c) : opts.limit;\n\n // Check rate limit with error handling\n let allowed: boolean;\n let info: RateLimitInfo;\n\n try {\n const result =\n opts.algorithm === \"sliding-window\"\n ? await checkSlidingWindow(store, key, limit, opts.windowMs)\n : await checkFixedWindow(store, key, limit, opts.windowMs);\n allowed = result.allowed;\n info = result.info;\n } catch (error) {\n const shouldAllow = await handleStoreError(\n error instanceof Error ? error : new Error(String(error)),\n c,\n );\n if (shouldAllow) {\n return next();\n }\n return new Response(\"Rate limiter error\", { status: 500 });\n }\n\n // Set context variable for downstream middleware\n c.set(\"rateLimit\", info);\n\n // Expose store access in context\n c.set(\"rateLimitStore\", {\n getKey: store.get?.bind(store) ?? (() => undefined),\n resetKey: store.resetKey.bind(store),\n });\n\n // Set headers\n setHeaders(\n c,\n info,\n opts.headers,\n opts.windowMs,\n opts.identifier,\n opts.quotaUnit,\n );\n\n // Handle rate limited\n if (!allowed) {\n // Fire callback\n if (opts.onRateLimited) {\n await opts.onRateLimited(c, info);\n }\n\n // Custom handler or default\n if (opts.handler) {\n return opts.handler(c, info);\n }\n return createDefaultResponse(\n info,\n opts.headers,\n opts.windowMs,\n opts.identifier,\n opts.quotaUnit,\n );\n }\n\n // Continue\n await next();\n\n // Handle skip options after response\n if (opts.skipSuccessfulRequests || opts.skipFailedRequests) {\n const status = c.res.status;\n const shouldDecrement =\n (opts.skipSuccessfulRequests && status >= 200 && status < 300) ||\n (opts.skipFailedRequests && status >= 400);\n\n if (shouldDecrement && store.decrement) {\n const windowStart =\n Math.floor(Date.now() / opts.windowMs) * opts.windowMs;\n const windowKey = `${key}:${windowStart}`;\n try {\n await store.decrement(windowKey);\n } catch {\n // Ignore decrement errors - request already processed\n }\n }\n }\n };\n};\n\n// ============================================================================\n// Cloudflare Rate Limiting Binding Middleware\n// ============================================================================\n\n/**\n * Rate limiter using Cloudflare's built-in Rate Limiting binding.\n *\n * This uses Cloudflare's globally distributed rate limiting infrastructure,\n * which is ideal for high-traffic applications.\n *\n * @example\n * ```ts\n * import { cloudflareRateLimiter } from '@jellyfungus/hono-rate-limiter'\n *\n * type Bindings = { RATE_LIMITER: RateLimitBinding }\n *\n * const app = new Hono<{ Bindings: Bindings }>()\n *\n * app.use(cloudflareRateLimiter({\n * binding: (c) => c.env.RATE_LIMITER,\n * keyGenerator: (c) => c.req.header('cf-connecting-ip') ?? 'unknown',\n * }))\n * ```\n */\nexport const cloudflareRateLimiter = <E extends Env = Env>(\n options: CloudflareRateLimitOptions<E>,\n): MiddlewareHandler<E> => {\n const { binding, keyGenerator, handler, skip } = options;\n\n return async function cloudflareRateLimiter(c, next) {\n // Check if should skip\n if (skip) {\n const shouldSkip = await skip(c);\n if (shouldSkip) {\n return next();\n }\n }\n\n // Get binding (may be dynamic)\n const rateLimitBinding =\n typeof binding === \"function\" ? binding(c) : binding;\n\n // Generate key\n const key = await keyGenerator(c);\n\n // Check rate limit\n const { success } = await rateLimitBinding.limit({ key });\n\n if (!success) {\n if (handler) {\n return handler(c);\n }\n return new Response(\"Rate limit exceeded\", {\n status: 429,\n headers: { \"Content-Type\": \"text/plain\" },\n });\n }\n\n return next();\n };\n};\n\n// ============================================================================\n// Exports\n// ============================================================================\n\nexport { getClientIP };\n"],"mappings":";AA0SO,IAAM,cAAN,MAA4C;AAAA,EACzC,UAAU,oBAAI,IAAyB;AAAA,EACvC,WAAW;AAAA,EACX;AAAA,EAER,KAAK,UAAwB;AAC3B,SAAK,WAAW;AAGhB,SAAK,eAAe,YAAY,MAAM;AACpC,YAAM,MAAM,KAAK,IAAI;AACrB,iBAAW,CAAC,KAAK,KAAK,KAAK,KAAK,SAAS;AACvC,YAAI,MAAM,SAAS,KAAK;AACtB,eAAK,QAAQ,OAAO,GAAG;AAAA,QACzB;AAAA,MACF;AAAA,IACF,GAAG,GAAM;AAGT,QAAI,OAAO,KAAK,aAAa,UAAU,YAAY;AACjD,WAAK,aAAa,MAAM;AAAA,IAC1B;AAAA,EACF;AAAA,EAEA,UAAU,KAA0B;AAClC,UAAM,MAAM,KAAK,IAAI;AACrB,UAAM,WAAW,KAAK,QAAQ,IAAI,GAAG;AAErC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