@sigil-dev/plugin-ratelimit 0.9.29 → 0.9.30

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 (3) hide show
  1. package/cache.ts +50 -0
  2. package/index.ts +11 -3
  3. package/package.json +10 -3
package/cache.ts ADDED
@@ -0,0 +1,50 @@
1
+ import type { RateLimitBackend } from "./index";
2
+
3
+ /**
4
+ * Counts in the app's cache (@sigil-dev/cache): with cache(redis()) or
5
+ * cache(table()) every worker shares the count, with nothing else to set up.
6
+ *
7
+ * Windows are aligned to the clock, so where a window ends needs no second
8
+ * read: one atomic increment per request.
9
+ */
10
+ export function cacheBackend(prefix = "ratelimit:"): RateLimitBackend {
11
+ type Counter = {
12
+ increment(key: string, window: number): Promise<number>;
13
+ delete(key: string): Promise<boolean>;
14
+ };
15
+ let found: Counter | undefined;
16
+ const counter = async (): Promise<Counter> => {
17
+ if (found) return found;
18
+ let mod: { useCache(): Counter };
19
+ try {
20
+ mod = (await import("@sigil-dev/cache")) as never;
21
+ } catch {
22
+ throw new Error(
23
+ 'plugin-ratelimit: backend "cache" needs @sigil-dev/cache. Run `sigil add cache`.',
24
+ );
25
+ }
26
+ found = mod.useCache();
27
+ return found;
28
+ };
29
+ // The window each key last counted in, so reset() finds its bucket.
30
+ const windows = new Map<string, number>();
31
+ const bucketKey = (key: string, windowMs: number, now: number) =>
32
+ `${prefix}${key}:${Math.floor(now / windowMs)}`;
33
+
34
+ return {
35
+ async increment(key, windowMs) {
36
+ const now = Date.now();
37
+ windows.set(key, windowMs);
38
+ const count = await (await counter()).increment(
39
+ bucketKey(key, windowMs, now),
40
+ windowMs,
41
+ );
42
+ return { count, resetAt: (Math.floor(now / windowMs) + 1) * windowMs };
43
+ },
44
+ async reset(key) {
45
+ const windowMs = windows.get(key);
46
+ if (windowMs === undefined) return;
47
+ await (await counter()).delete(bucketKey(key, windowMs, Date.now()));
48
+ },
49
+ };
50
+ }
package/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { clientAddress, type GrimoirePlugin } from "@sigil-dev/grimoire";
2
2
  import type { Server } from "bun";
3
+ import { cacheBackend } from "./cache";
3
4
  import { MemoryStore } from "./memory";
4
5
 
5
6
  export interface RateLimitBackend {
@@ -47,10 +48,12 @@ export interface RateLimitOptions {
47
48
  /**
48
49
  * Storage backend.
49
50
  * - "memory" — in-process Map. Fast, zero deps. NOT safe for multi-worker.
50
- * - redis(opts) — Bun-native Redis client. Safe for multi-worker.
51
+ * - "cache" — the app's cache (@sigil-dev/cache): shared by every worker
52
+ * when that is cache(redis()) or cache(table()).
53
+ * - redis(opts) — its own Redis connection. Prefer "cache".
51
54
  * @default "memory"
52
55
  */
53
- backend?: "memory" | RateLimitBackend;
56
+ backend?: "memory" | "cache" | RateLimitBackend;
54
57
 
55
58
  /**
56
59
  * Per-route overrides. Keys are exact URL pathnames.
@@ -132,7 +135,11 @@ export function rateLimit(opts: RateLimitOptions = {}): GrimoirePlugin {
132
135
  } = opts;
133
136
 
134
137
  const store: RateLimitBackend =
135
- backend === "memory" ? new MemoryStore() : backend;
138
+ backend === "memory"
139
+ ? new MemoryStore()
140
+ : backend === "cache"
141
+ ? cacheBackend()
142
+ : backend;
136
143
 
137
144
  let server: Server<unknown> | null = null;
138
145
 
@@ -211,4 +218,5 @@ export function rateLimit(opts: RateLimitOptions = {}): GrimoirePlugin {
211
218
  };
212
219
  }
213
220
 
221
+ export { cacheBackend } from "./cache";
214
222
  export { redis } from "./redis";
package/package.json CHANGED
@@ -11,6 +11,7 @@
11
11
  "module": "index.ts",
12
12
  "type": "module",
13
13
  "files": [
14
+ "cache.ts",
14
15
  "index.ts",
15
16
  "memory.ts",
16
17
  "redis.ts",
@@ -18,13 +19,19 @@
18
19
  ],
19
20
  "private": false,
20
21
  "dependencies": {
21
- "@sigil-dev/grimoire": "0.9.29"
22
+ "@sigil-dev/grimoire": "0.9.30"
22
23
  },
23
24
  "devDependencies": {
24
25
  "@types/bun": "latest"
25
26
  },
26
27
  "peerDependencies": {
27
- "typescript": "^5"
28
+ "typescript": "^5",
29
+ "@sigil-dev/cache": "0.9.30"
28
30
  },
29
- "version": "0.9.29"
31
+ "version": "0.9.30",
32
+ "peerDependenciesMeta": {
33
+ "@sigil-dev/cache": {
34
+ "optional": true
35
+ }
36
+ }
30
37
  }