@lunora/ratelimit 1.0.0-alpha.13 → 1.0.0-alpha.131

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 CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  </a>
8
8
 
9
- <h3 align="center">Rate limiting: token-bucket / fixed-window / sliding-window algorithms, deny list, sharding, pluggable stores, and procedure middleware</h3>
9
+ <h3 align="center">Rate limiting: token-bucket / fixed-window / sliding-window algorithms, deny list, pluggable stores, and procedure middleware</h3>
10
10
 
11
11
  <!-- END_PACKAGE_OG_IMAGE_PLACEHOLDER -->
12
12
 
@@ -34,7 +34,7 @@
34
34
 
35
35
  ---
36
36
 
37
- Rate limiting for Lunora: token-bucket, fixed-window, and sliding-window algorithms, a deny list, optional sharding for hot limits, pluggable stores (in-memory, SQL, or the Lunora ORM), and procedure middleware that rides the `.use()` chain.
37
+ Rate limiting for Lunora: token-bucket, fixed-window, and sliding-window algorithms, a deny list, pluggable stores (in-memory, SQL, or the Lunora ORM), and procedure middleware that rides the `.use()` chain.
38
38
 
39
39
  Part of the [Lunora](https://github.com/anolilab/lunora) framework — a type-safe, real-time backend on Cloudflare Workers + Durable Objects with a Vite-first DX.
40
40
 
@@ -59,37 +59,68 @@ to a procedure's `.use(...)` chain. The procedure builders (`mutation`, …) com
59
59
  from your app's generated `_generated/server` module, not from a package.
60
60
 
61
61
  ```ts
62
- import { RateLimiter, rateLimit } from "@lunora/ratelimit";
62
+ import { dbRateLimit } from "@lunora/ratelimit";
63
63
 
64
64
  import { mutation } from "./_generated/server";
65
65
 
66
- const limiter = new RateLimiter({
67
- config: {
68
- login: { kind: "fixed window", period: 60_000, rate: 5 },
69
- send: { kind: "token bucket", period: 1_000, rate: 10 },
70
- },
71
- });
72
-
66
+ const config = {
67
+ login: { kind: "fixed window", period: 60_000, rate: 5 },
68
+ send: { kind: "token bucket", period: 1_000, rate: 10 },
69
+ };
70
+
71
+ // `dbRateLimit` builds the limiter per call, backed by a Lunora table via
72
+ // `ctx.db`, so the count is durable. A `RateLimiter` built with no explicit
73
+ // `store` falls back to `createMemoryStore` (a per-instance in-memory Map) —
74
+ // fine for a limit that only ever needs to hold within one Durable Object
75
+ // instance, but a limiter like `login` needs a durable store or it stops
76
+ // enforcing the configured rate the moment the DO instance is sharded,
77
+ // replicated, or recreated.
73
78
  // As procedure middleware — throws a structural LunoraError (429/403) on rejection.
74
- export const send = mutation.use(rateLimit(limiter, "send", { key: (ctx) => ctx.auth.userId })).mutation(async ({ ctx }) => {
79
+ // `key` must resolve to a string — a resolver returning `undefined` throws
80
+ // rather than silently sharing one bucket across every keyless caller.
81
+ export const send = mutation.use(dbRateLimit(config, "send", { key: (ctx) => ctx.auth.userId ?? "anonymous" })).mutation(async ({ ctx }) => {
75
82
  // …
76
83
  });
77
84
  ```
78
85
 
79
- Or call the limiter directly:
86
+ Or call the limiter directly, inside a mutation/action so `ctx.db` is available.
87
+ A login limiter belongs in an **action**: a mutation's `ctx.db` writes ride its
88
+ storage transaction, so a handler that throws (a wrong password) rolls the
89
+ consumed unit back and every failed attempt is free. An action's writes commit
90
+ on their own, so the charge stays whether or not the handler throws.
80
91
 
81
92
  ```ts
82
- const status = await limiter.limit("send", { key: userId });
93
+ import { RateLimiter, RateLimitError, createDbStore } from "@lunora/ratelimit";
94
+
95
+ import { action } from "./_generated/server";
83
96
 
84
- if (!status.ok) {
85
- // status.retryAfter is milliseconds until the request would succeed.
86
- // status.reason is "rate" or "deny".
87
- }
97
+ export const login = action.action(async ({ ctx, args }) => {
98
+ const limiter = new RateLimiter({ config, store: createDbStore({ db: ctx.db }) });
99
+ const status = await limiter.limit("login", { key: args.email });
100
+
101
+ if (!status.ok) {
102
+ // Stop here — `limit` returns a failing status by default rather than
103
+ // throwing, so falling through authenticates the rejected attempt.
104
+ // status.retryAfter is milliseconds until the request would succeed.
105
+ // status.reason is "rate" or "deny".
106
+ throw new RateLimitError(status);
107
+ }
108
+
109
+ await authenticate(args);
110
+ });
88
111
  ```
89
112
 
90
113
  For durable per-DO state inside a procedure, back the limiter with a Lunora
91
114
  table via `dbRateLimit(config, name, options)`, or supply a `store`
92
- (`createMemoryStore` / `createSqlStore` / `createDbStore`).
115
+ (`createSqlStore` / `createDbStore`). `createMemoryStore` stays the default —
116
+ correct inside a single Durable Object — but constructing a `RateLimiter` with
117
+ no explicit `store` now warns once so that choice is visible instead of
118
+ silent.
119
+
120
+ A key that is an IPv6 address (for example `ctx.ip`) is bucketed by its /64
121
+ prefix, and an IPv4-mapped address by the IPv4 address it wraps. An IPv6 client
122
+ usually controls a whole /64, so a per-address bucket would let it pick a fresh
123
+ address for every request. A deny-list IPv6 entry bans its /64.
93
124
 
94
125
  > This README covers the basics. For the full API, options, and guides, see the **[documentation](https://lunora.sh/docs)**.
95
126
 
package/dist/index.d.mts CHANGED
@@ -19,18 +19,6 @@ interface RateLimitConfig {
19
19
  period: number;
20
20
  /** Tokens granted per `period`. */
21
21
  rate: number;
22
- /**
23
- * Split a hot limit across N independent sub-buckets to avoid a single
24
- * contended key/Durable Object. Each shard enforces `rate / shards` (and
25
- * `capacity / shards`); a request is routed to a shard via a deterministic
26
- * hash of `(name, key)`, so the same key always lands on the same shard and
27
- * a single key's effective throughput is exactly `rate / shards`. Aggregate
28
- * throughput across many distinct keys approaches `rate` as keys spread
29
- * uniformly across shards. Reserve it for high-volume limits where
30
- * contention bites; leave unset (one bucket) otherwise. Must be a positive
31
- * integer — `1` is equivalent to unset.
32
- */
33
- shards?: number;
34
22
  /**
35
23
  * Phase offset in epoch milliseconds for windowed algorithms — windows
36
24
  * align to `start + n * period`. Ignored by token buckets. Defaults to `0`.
@@ -92,6 +80,18 @@ interface RateLimitStore {
92
80
  delete: (storageKey: string) => Promise<void> | void;
93
81
  get: (storageKey: string) => Promise<RateLimitValue | undefined> | RateLimitValue | undefined;
94
82
  set: (storageKey: string, value: RateLimitValue) => Promise<void> | void;
83
+ /**
84
+ * Optional conditional write: persist `value` only while the stored value is
85
+ * still `prior` (what `get` returned for this consume, `undefined` for no
86
+ * row), and resolve `false` when it no longer is. When present the limiter
87
+ * writes through it instead of `set`, and a `false` makes it read and decide
88
+ * again rather than admit off a stale read.
89
+ *
90
+ * A store whose `get` and write cannot be interleaved by another consume
91
+ * (memory, SQL: synchronous) needs none. One whose calls can be
92
+ * (`createDbStore`: every call is awaited I/O) must have it.
93
+ */
94
+ setIf?: (storageKey: string, value: RateLimitValue, prior: RateLimitValue | undefined) => Promise<boolean>;
95
95
  }
96
96
  /** Inputs to {@link evaluate}. */
97
97
  interface EvaluateOptions {
@@ -135,11 +135,15 @@ interface RateLimiterOptions<Names extends string> {
135
135
  denyList?: Iterable<string>;
136
136
  /**
137
137
  * Optional key normalizer applied to every incoming `args.key` (the
138
- * deny-list check, the storage key, and downstream shard selection all see
139
- * the normalized form). Use for case-folding, trimming, or canonicalizing
140
- * IPs/emails so equivalent inputs share a single bucket. The deny-list
138
+ * deny-list check and the storage key both see the normalized form). Use for case-folding, trimming, or canonicalizing
139
+ * emails so equivalent inputs share a single bucket. The deny-list
141
140
  * itself is consulted as-is; normalize the deny-list entries up front to
142
141
  * match.
142
+ *
143
+ * After it, a key that is an IPv6 address is always collapsed to its /64
144
+ * (`2001:db8:1:2::/64`) and an IPv4-mapped one to its IPv4 address, so a
145
+ * `ctx.ip`-keyed limit cannot be dodged by rotating addresses within one
146
+ * delegated prefix. A deny-list IPv6 entry therefore bans its whole /64.
143
147
  */
144
148
  normalize?: (key: string) => string;
145
149
  /** Clock injection for tests. Defaults to `Date.now`. */
@@ -166,9 +170,7 @@ declare class RateLimiter<Names extends string = string> {
166
170
  * Read the current config and the units admittable right now for a
167
171
  * `(name, key)` pair. The value is projected forward to the current clock
168
172
  * (token-bucket refill, fixed-window rollover, sliding-window decay), not
169
- * the last persisted figure. For a sharded limit it reads only the single
170
- * shard `limit()`/`run()` would route this key to — the sibling shards are
171
- * never touched by this key, so summing them would over-report.
173
+ * the last persisted figure.
172
174
  */
173
175
  getValue(name: Names, args?: {
174
176
  key?: string;
@@ -183,6 +185,17 @@ declare class RateLimiter<Names extends string = string> {
183
185
  reset(name: Names, args?: {
184
186
  key?: string;
185
187
  }): Promise<void>;
188
+ /**
189
+ * Read, evaluate and write one consume. A store that reports the write lost
190
+ * (`setIf` resolved `false`: another consume changed the value since this one
191
+ * read it) gets a fresh read and a fresh decision, so the admission always
192
+ * rests on the value the write replaced. Bounded: each lost round means some
193
+ * other consume was admitted (a refusal writes nothing), and past
194
+ * {@link MAX_DECIDE_ATTEMPTS} the call is refused with `retryAfter: 0` rather
195
+ * than admitted.
196
+ */
197
+ private decide;
198
+ private normalizeKey;
186
199
  private resolve;
187
200
  private run;
188
201
  }
@@ -202,7 +215,15 @@ interface RateLimitMiddlewareOptions<Context> {
202
215
  * — note that a failing limiter then permits every request through.
203
216
  */
204
217
  failOpen?: boolean;
205
- /** Sub-key derived from `ctx` (per-user/IP). Omit for a global limit. */
218
+ /**
219
+ * Sub-key derived from `ctx` (per-user/IP). Omit for a global limit.
220
+ *
221
+ * A resolver that returns `undefined` is a config bug, not a global limit:
222
+ * the middleware throws `INTERNAL` rather than silently pooling every
223
+ * keyless caller (e.g. every anonymous user) into one shared bucket. Fold
224
+ * the absent case yourself — `ctx.auth.userId ?? "anonymous"` — so the
225
+ * shared bucket is a visible choice.
226
+ */
206
227
  key?: (context: Context) => string | undefined;
207
228
  /** Override the error message thrown on rejection. */
208
229
  message?: string;
@@ -211,7 +232,7 @@ interface RateLimitMiddlewareOptions<Context> {
211
232
  * Procedure middleware that enforces a named rate limit before the handler
212
233
  * runs. Attach it with `.use()`. On rejection it throws a structural
213
234
  * `LunoraError` (`TOO_MANY_REQUESTS`/429, or `FORBIDDEN`/403 for deny-list
214
- * hits) carrying `retryAfter` in milliseconds — the runtime maps it to the
235
+ * hits) carrying `data.retryAfterMs` — the runtime maps it to the
215
236
  * matching RPC/HTTP status without any import of `@lunora/server` at runtime.
216
237
  *
217
238
  * **Failure policy:** if resolving or invoking the limiter throws for a genuine
@@ -250,33 +271,34 @@ interface SqlStoreOptions {
250
271
  * SQLite-backed store for durable, per-DO rate-limit state. Persists each
251
272
  * `(name, key)` pair as one row so limits survive hibernation and eviction.
252
273
  *
253
- * **Atomicity:** the store does not wrap individual operations in an explicit
254
- * SQL transaction. Inside a Durable Object the DO's input gate serializes
255
- * every RPC call against the storage, so the limiter's read-modify-write
256
- * sequence runs to completion without interleaving — this is the same
257
- * guarantee the surrounding `evaluate()` step depends on. **Outside a DO**
258
- * (e.g. driving `createSqlStore` from a long-lived `node:sqlite` connection in
259
- * tests or a custom host) the caller is responsible for serialization; the
260
- * SQL surface used here (`exec`) is not a substitute for transactional
261
- * isolation across concurrent invocations.
274
+ * **Atomicity:** `exec` is synchronous and the limiter does not yield between a
275
+ * synchronous store's read and its write, so one consume's read, decision and
276
+ * write run as a single JavaScript step. No other call on the same isolate can
277
+ * interleave, which is what makes concurrent consumes admit exactly the
278
+ * configured rate. Two isolates sharing one SQLite file (not a Durable Object
279
+ * case) are not covered; serialize those yourself.
262
280
  */
263
281
  declare const createSqlStore: (options: SqlStoreOptions) => RateLimitStore;
264
282
  /**
265
- * The slice of an index-range builder the store uses. Mirrors `@lunora/server`'s
266
- * `IndexRangeBuilder` field-for-field so the real `ctx.db` query builder is
267
- * assignable; only `eq` is exercised.
283
+ * The READ slice — everything the store needs to answer `RateLimiter.getValue`
284
+ * and `check`, which the docs describe as projecting the stored value forward to
285
+ * the current clock. A `QueryCtx`'s `ctx.db` is a reader and satisfies this.
286
+ *
287
+ * Split out because requiring the writer for a pure read meant "how many
288
+ * requests does this user have left" could not be answered from a query context
289
+ * at all — every remaining-quota display had to cast, and a cast that appears
290
+ * often enough stops carrying information. The distinction was already in the
291
+ * methods: `getValue`/`check` read, `limit`/`reset` write.
292
+ *
293
+ * `findFirst` rather than `query(table).withIndex(...)`: a `.global()` table's
294
+ * reader has no `withIndex`, so the indexed builder failed every read there.
295
+ * `findFirst` routes to whichever backend owns the table, and an equality on the
296
+ * key column still uses its index.
268
297
  */
269
- interface RateLimitDatabaseIndexRange {
270
- eq: (field: string, value: unknown) => RateLimitDatabaseIndexRange;
271
- gt: (field: string, value: unknown) => RateLimitDatabaseIndexRange;
272
- gte: (field: string, value: unknown) => RateLimitDatabaseIndexRange;
273
- lt: (field: string, value: unknown) => RateLimitDatabaseIndexRange;
274
- lte: (field: string, value: unknown) => RateLimitDatabaseIndexRange;
275
- }
276
- /** The slice of a `ctx.db` table query the store relies on. */
277
- interface RateLimitDatabaseQuery {
278
- first: () => Promise<Record<string, unknown> | null>;
279
- withIndex: (indexName: string, range: (q: RateLimitDatabaseIndexRange) => RateLimitDatabaseIndexRange) => RateLimitDatabaseQuery;
298
+ interface RateLimitDatabaseReader {
299
+ findFirst: (table: string, args?: {
300
+ where?: Record<string, unknown>;
301
+ }) => Promise<Record<string, unknown> | null>;
280
302
  }
281
303
  /**
282
304
  * The slice of the Lunora ORM writer (`ctx.db` on a mutation/action) the store
@@ -284,26 +306,39 @@ interface RateLimitDatabaseQuery {
284
306
  * directly — declared here (rather than imported) to keep `@lunora/ratelimit`
285
307
  * free of a runtime dependency on `@lunora/server`.
286
308
  */
287
- interface RateLimitDatabase {
309
+ interface RateLimitDatabase extends RateLimitDatabaseReader {
288
310
  delete: <T extends string>(id: Id<T>) => Promise<void>;
289
- insert: <T extends string>(table: T, document: Record<string, unknown>) => Promise<Id<T>>;
290
- patch: <T extends string>(id: Id<T>, patch: Record<string, unknown>) => Promise<void>;
291
- query: (table: string) => RateLimitDatabaseQuery;
311
+ insert: <T extends string>(table: T, document: Record<string, unknown>, options?: {
312
+ clientId?: string;
313
+ }) => Promise<Id<T>>;
314
+ patchIf: <T extends string>(table: T, id: Id<T>, args: {
315
+ patch: Record<string, unknown>;
316
+ where: Record<string, unknown>;
317
+ }) => Promise<{
318
+ applied: boolean;
319
+ doc: Record<string, unknown> | null;
320
+ }>;
292
321
  }
293
- interface DatabaseStoreOptions {
294
- /** The Lunora ORM writer — `ctx.db` inside a mutation or action. */
295
- db: RateLimitDatabase;
296
- /** Index that resolves a row by its key column. Defaults to `by_key`. */
297
- index?: string;
298
- /** Column storing the opaque key. Defaults to `key`. */
322
+ /** The table/column knobs shared by the read-only and read-write stores. */
323
+ interface DatabaseStoreLocation {
324
+ /** Column storing the opaque key. Defaults to `key`. Index it: every consume reads by it. */
299
325
  keyField?: string;
300
326
  /** Table holding one row per `(name, key)` pair. Defaults to `rateLimits`. */
301
327
  table?: string;
302
328
  }
329
+ interface DatabaseStoreOptions extends DatabaseStoreLocation {
330
+ /** The Lunora ORM writer — `ctx.db` inside a mutation or action. */
331
+ db: RateLimitDatabase;
332
+ }
333
+ interface ReadOnlyDatabaseStoreOptions extends DatabaseStoreLocation {
334
+ /** The Lunora ORM reader — `ctx.db` inside a query. */
335
+ db: RateLimitDatabaseReader;
336
+ }
303
337
  /**
304
- * Store backed by a Lunora table through `ctx.db`, for durable per-DO limits
305
- * inside a procedure (the procedure context exposes no raw SQL). Declare a
306
- * table with the key column and its index, e.g.
338
+ * Store backed by a Lunora table through `ctx.db`, for durable limits inside a
339
+ * procedure (the procedure context exposes no raw SQL). Works on a shard-local
340
+ * table and on a `.global()` one. Declare a table with the key column and its
341
+ * index, e.g.
307
342
  *
308
343
  * ```ts
309
344
  * rateLimits: defineTable({
@@ -314,10 +349,38 @@ interface DatabaseStoreOptions {
314
349
  * }).index("by_key", ["key"])
315
350
  * ```
316
351
  *
317
- * Each operation is a read-then-write; inside a mutation/action that pair runs
318
- * under the DO's input gate, so it is atomic against concurrent calls.
352
+ * **Atomic against concurrent consumes.** `setIf` writes only if the row still
353
+ * holds the value `get` returned: an existing row through `ctx.db.patchIf` with
354
+ * that value as its `where`, a new row through an `insert` under an id derived
355
+ * from the key, which a concurrent creator's insert collides with. A consume
356
+ * that loses either race gets `false` back and the limiter reads and decides
357
+ * again, so a limit of 1 admits one caller however many arrive together — on a
358
+ * `.global()` table too, where D1 calls do not hold the Durable Object's input
359
+ * gate.
360
+ *
361
+ * **Consumption commits with the procedure.** A mutation's `ctx.db` writes ride
362
+ * its storage transaction, so a handler that throws after `limit()` rolls the
363
+ * consumed unit back with everything else — inside a mutation this store counts
364
+ * successful calls, not attempts. To charge every attempt (a login limiter),
365
+ * consume from an action, where each write commits on its own, or return a
366
+ * failure value from the mutation instead of throwing. A `.global()` table's
367
+ * row lives in D1, outside the DO transaction, so there the unit stays spent.
319
368
  */
320
369
  declare const createDatabaseStore: (options: DatabaseStoreOptions) => RateLimitStore;
370
+ /**
371
+ * Read-only counterpart to {@link createDatabaseStore}, for a query context.
372
+ *
373
+ * `get` behaves identically — same table, index and key column — so
374
+ * `RateLimiter.getValue` / `check` report exactly what the writing store would.
375
+ * `set` and `delete` are the only difference: they throw rather than silently
376
+ * doing nothing, because a limiter that appears to consume budget and does not
377
+ * is worse than one that refuses.
378
+ *
379
+ * This mirrors the split Lunora already makes for `ctx.storage`, which is a
380
+ * `ReadOnlyStorage` in a query and a full `Storage` in an action — the
381
+ * capability difference is visible in the type instead of discovered at runtime.
382
+ */
383
+ declare const createReadOnlyDatabaseStore: (options: ReadOnlyDatabaseStoreOptions) => RateLimitStore;
321
384
  /**
322
385
  * DB-backed rate-limit middleware sugar. Collapses the common
323
386
  *
@@ -330,12 +393,17 @@ declare const createDatabaseStore: (options: DatabaseStoreOptions) => RateLimitS
330
393
  * on the DO the procedure runs on). Every query/mutation/action ctx exposes a
331
394
  * compatible `db`, so it slots straight into a `.use(...)` chain.
332
395
  *
333
- * Pass `options.store` to point at a non-default backing table/index/key column
334
- * (defaults: table `rateLimits`, index `by_key`, key column `key`); the rest of
396
+ * Pass `options.store` to point at a non-default backing table/key column
397
+ * (defaults: table `rateLimits`, key column `key`); the rest of
335
398
  * `options` (`key`, `count`, `failOpen`, `message`) is forwarded to
336
399
  * {@link rateLimit} unchanged. When `config` is precisely typed, `name`
337
400
  * autocompletes to its declared limit names.
338
401
  *
402
+ * On a mutation the consumed unit commits with the handler: a handler that
403
+ * throws rolls it back, so a failed call costs nothing. Attach it to an action
404
+ * (whose writes commit independently) when failed attempts must count — see
405
+ * {@link createDatabaseStore}.
406
+ *
339
407
  * Re-exported as `dbRateLimit` from the package root.
340
408
  *
341
409
  * ```ts
@@ -386,7 +454,7 @@ interface RatelimitApiContext<Context> {
386
454
  *
387
455
  * ```ts
388
456
  * const limiter = new RateLimiter({ config: { send: { kind: "token bucket", rate: 5, period: 60_000, capacity: 5 } } });
389
- * const c = initLunora.dataModel&lt;DataModel>().create();
457
+ * const c = initLunora.dataModel<DataModel>().create();
390
458
  * export const send = c.mutation
391
459
  * .use(ratelimitPlugin(limiter).middleware!)
392
460
  * .mutation(async ({ ctx, args }) => {
@@ -405,5 +473,50 @@ interface RatelimitApiContext<Context> {
405
473
  * `@lunora/server` a type-only dependency of `@lunora/ratelimit`.
406
474
  */
407
475
  declare const ratelimitPlugin: <Context = unknown>(limiter: LimiterResolver<Context>) => Plugin<Record<never, never>, Context, Context & RatelimitApiContext<Context>>;
476
+ /** A budget bound to one named limit — check before the call, record after it. */
477
+ interface TokenBudget {
478
+ /**
479
+ * Peek at the budget before spending. `ok: false` means it is exhausted:
480
+ * refuse the call, and `retryAfter` says when it refills. Consumes nothing.
481
+ */
482
+ check: (key: string) => Promise<RateLimitStatus>;
483
+ /**
484
+ * Charge the tokens a call actually used. Always call it, including when the
485
+ * call THREW — a failed generation that consumed input tokens still has to be
486
+ * paid for. `tokens` of `0` is a no-op, so a call that spent nothing costs
487
+ * nothing.
488
+ *
489
+ * The charge is a reservation, so it may take the bucket negative: the tokens
490
+ * are already spent, and refusing to record them would let a single oversized
491
+ * call escape the budget entirely.
492
+ */
493
+ record: (key: string, tokens: number) => Promise<RateLimitStatus>;
494
+ }
495
+ /**
496
+ * Bind a {@link TokenBudget} to one of a limiter's named limits.
497
+ *
498
+ * ```ts
499
+ * const budget = tokenBudget(limiter, "tokens");
500
+ * const allowed = await budget.check(userId);
501
+ *
502
+ * if (!allowed.ok) {
503
+ * throw new LunoraError("RATE_LIMITED", `token budget exhausted; retry in ${String(allowed.retryAfter)}ms`);
504
+ * }
505
+ *
506
+ * try {
507
+ * const { text, usage } = await generateText({ model: ctx.ai.model(), prompt });
508
+ *
509
+ * await budget.record(userId, usage?.totalTokens ?? 0);
510
+ *
511
+ * return text;
512
+ * } catch (error) {
513
+ * // The prompt was still sent — charge what is known, then rethrow.
514
+ * await budget.record(userId, estimatedInputTokens);
515
+ *
516
+ * throw error;
517
+ * }
518
+ * ```
519
+ */
520
+ declare const tokenBudget: <Names extends string>(limiter: RateLimiter<Names>, name: Names) => TokenBudget;
408
521
  declare const VERSION = "0.0.0";
409
- export { type DatabaseStoreOptions as DbStoreOptions, type EvaluateOptions, type EvaluateResult, type LimiterResolver, type RateLimitArgs, type RateLimitConfig, type RateLimitConfigMap, type RateLimitDatabase as RateLimitDb, type RateLimitDatabaseIndexRange as RateLimitDbIndexRange, type RateLimitDatabaseQuery as RateLimitDbQuery, RateLimitError, type RateLimitKind, type RateLimitMiddlewareOptions, type RateLimitReason, type RateLimitStatus, type RateLimitStore, type RateLimitValue, RateLimiter, type RateLimiterOptions, type RatelimitApiContext, type SqlLike, type SqlStoreOptions, VERSION, availableAt, createDatabaseStore as createDbStore, createMemoryStore, createSqlStore, databaseRateLimit as dbRateLimit, evaluate, rateLimit, ratelimitPlugin };
522
+ export { type DatabaseStoreOptions as DbStoreOptions, type EvaluateOptions, type EvaluateResult, type LimiterResolver, type RateLimitArgs, type RateLimitConfig, type RateLimitConfigMap, type RateLimitDatabase as RateLimitDb, type RateLimitDatabaseReader as RateLimitDbReader, RateLimitError, type RateLimitKind, type RateLimitMiddlewareOptions, type RateLimitReason, type RateLimitStatus, type RateLimitStore, type RateLimitValue, RateLimiter, type RateLimiterOptions, type RatelimitApiContext, type ReadOnlyDatabaseStoreOptions as ReadOnlyDbStoreOptions, type SqlLike, type SqlStoreOptions, type TokenBudget, VERSION, availableAt, createDatabaseStore as createDbStore, createMemoryStore, createReadOnlyDatabaseStore as createReadOnlyDbStore, createSqlStore, databaseRateLimit as dbRateLimit, evaluate, rateLimit, ratelimitPlugin, tokenBudget };
package/dist/index.d.ts CHANGED
@@ -19,18 +19,6 @@ interface RateLimitConfig {
19
19
  period: number;
20
20
  /** Tokens granted per `period`. */
21
21
  rate: number;
22
- /**
23
- * Split a hot limit across N independent sub-buckets to avoid a single
24
- * contended key/Durable Object. Each shard enforces `rate / shards` (and
25
- * `capacity / shards`); a request is routed to a shard via a deterministic
26
- * hash of `(name, key)`, so the same key always lands on the same shard and
27
- * a single key's effective throughput is exactly `rate / shards`. Aggregate
28
- * throughput across many distinct keys approaches `rate` as keys spread
29
- * uniformly across shards. Reserve it for high-volume limits where
30
- * contention bites; leave unset (one bucket) otherwise. Must be a positive
31
- * integer — `1` is equivalent to unset.
32
- */
33
- shards?: number;
34
22
  /**
35
23
  * Phase offset in epoch milliseconds for windowed algorithms — windows
36
24
  * align to `start + n * period`. Ignored by token buckets. Defaults to `0`.
@@ -92,6 +80,18 @@ interface RateLimitStore {
92
80
  delete: (storageKey: string) => Promise<void> | void;
93
81
  get: (storageKey: string) => Promise<RateLimitValue | undefined> | RateLimitValue | undefined;
94
82
  set: (storageKey: string, value: RateLimitValue) => Promise<void> | void;
83
+ /**
84
+ * Optional conditional write: persist `value` only while the stored value is
85
+ * still `prior` (what `get` returned for this consume, `undefined` for no
86
+ * row), and resolve `false` when it no longer is. When present the limiter
87
+ * writes through it instead of `set`, and a `false` makes it read and decide
88
+ * again rather than admit off a stale read.
89
+ *
90
+ * A store whose `get` and write cannot be interleaved by another consume
91
+ * (memory, SQL: synchronous) needs none. One whose calls can be
92
+ * (`createDbStore`: every call is awaited I/O) must have it.
93
+ */
94
+ setIf?: (storageKey: string, value: RateLimitValue, prior: RateLimitValue | undefined) => Promise<boolean>;
95
95
  }
96
96
  /** Inputs to {@link evaluate}. */
97
97
  interface EvaluateOptions {
@@ -135,11 +135,15 @@ interface RateLimiterOptions<Names extends string> {
135
135
  denyList?: Iterable<string>;
136
136
  /**
137
137
  * Optional key normalizer applied to every incoming `args.key` (the
138
- * deny-list check, the storage key, and downstream shard selection all see
139
- * the normalized form). Use for case-folding, trimming, or canonicalizing
140
- * IPs/emails so equivalent inputs share a single bucket. The deny-list
138
+ * deny-list check and the storage key both see the normalized form). Use for case-folding, trimming, or canonicalizing
139
+ * emails so equivalent inputs share a single bucket. The deny-list
141
140
  * itself is consulted as-is; normalize the deny-list entries up front to
142
141
  * match.
142
+ *
143
+ * After it, a key that is an IPv6 address is always collapsed to its /64
144
+ * (`2001:db8:1:2::/64`) and an IPv4-mapped one to its IPv4 address, so a
145
+ * `ctx.ip`-keyed limit cannot be dodged by rotating addresses within one
146
+ * delegated prefix. A deny-list IPv6 entry therefore bans its whole /64.
143
147
  */
144
148
  normalize?: (key: string) => string;
145
149
  /** Clock injection for tests. Defaults to `Date.now`. */
@@ -166,9 +170,7 @@ declare class RateLimiter<Names extends string = string> {
166
170
  * Read the current config and the units admittable right now for a
167
171
  * `(name, key)` pair. The value is projected forward to the current clock
168
172
  * (token-bucket refill, fixed-window rollover, sliding-window decay), not
169
- * the last persisted figure. For a sharded limit it reads only the single
170
- * shard `limit()`/`run()` would route this key to — the sibling shards are
171
- * never touched by this key, so summing them would over-report.
173
+ * the last persisted figure.
172
174
  */
173
175
  getValue(name: Names, args?: {
174
176
  key?: string;
@@ -183,6 +185,17 @@ declare class RateLimiter<Names extends string = string> {
183
185
  reset(name: Names, args?: {
184
186
  key?: string;
185
187
  }): Promise<void>;
188
+ /**
189
+ * Read, evaluate and write one consume. A store that reports the write lost
190
+ * (`setIf` resolved `false`: another consume changed the value since this one
191
+ * read it) gets a fresh read and a fresh decision, so the admission always
192
+ * rests on the value the write replaced. Bounded: each lost round means some
193
+ * other consume was admitted (a refusal writes nothing), and past
194
+ * {@link MAX_DECIDE_ATTEMPTS} the call is refused with `retryAfter: 0` rather
195
+ * than admitted.
196
+ */
197
+ private decide;
198
+ private normalizeKey;
186
199
  private resolve;
187
200
  private run;
188
201
  }
@@ -202,7 +215,15 @@ interface RateLimitMiddlewareOptions<Context> {
202
215
  * — note that a failing limiter then permits every request through.
203
216
  */
204
217
  failOpen?: boolean;
205
- /** Sub-key derived from `ctx` (per-user/IP). Omit for a global limit. */
218
+ /**
219
+ * Sub-key derived from `ctx` (per-user/IP). Omit for a global limit.
220
+ *
221
+ * A resolver that returns `undefined` is a config bug, not a global limit:
222
+ * the middleware throws `INTERNAL` rather than silently pooling every
223
+ * keyless caller (e.g. every anonymous user) into one shared bucket. Fold
224
+ * the absent case yourself — `ctx.auth.userId ?? "anonymous"` — so the
225
+ * shared bucket is a visible choice.
226
+ */
206
227
  key?: (context: Context) => string | undefined;
207
228
  /** Override the error message thrown on rejection. */
208
229
  message?: string;
@@ -211,7 +232,7 @@ interface RateLimitMiddlewareOptions<Context> {
211
232
  * Procedure middleware that enforces a named rate limit before the handler
212
233
  * runs. Attach it with `.use()`. On rejection it throws a structural
213
234
  * `LunoraError` (`TOO_MANY_REQUESTS`/429, or `FORBIDDEN`/403 for deny-list
214
- * hits) carrying `retryAfter` in milliseconds — the runtime maps it to the
235
+ * hits) carrying `data.retryAfterMs` — the runtime maps it to the
215
236
  * matching RPC/HTTP status without any import of `@lunora/server` at runtime.
216
237
  *
217
238
  * **Failure policy:** if resolving or invoking the limiter throws for a genuine
@@ -250,33 +271,34 @@ interface SqlStoreOptions {
250
271
  * SQLite-backed store for durable, per-DO rate-limit state. Persists each
251
272
  * `(name, key)` pair as one row so limits survive hibernation and eviction.
252
273
  *
253
- * **Atomicity:** the store does not wrap individual operations in an explicit
254
- * SQL transaction. Inside a Durable Object the DO's input gate serializes
255
- * every RPC call against the storage, so the limiter's read-modify-write
256
- * sequence runs to completion without interleaving — this is the same
257
- * guarantee the surrounding `evaluate()` step depends on. **Outside a DO**
258
- * (e.g. driving `createSqlStore` from a long-lived `node:sqlite` connection in
259
- * tests or a custom host) the caller is responsible for serialization; the
260
- * SQL surface used here (`exec`) is not a substitute for transactional
261
- * isolation across concurrent invocations.
274
+ * **Atomicity:** `exec` is synchronous and the limiter does not yield between a
275
+ * synchronous store's read and its write, so one consume's read, decision and
276
+ * write run as a single JavaScript step. No other call on the same isolate can
277
+ * interleave, which is what makes concurrent consumes admit exactly the
278
+ * configured rate. Two isolates sharing one SQLite file (not a Durable Object
279
+ * case) are not covered; serialize those yourself.
262
280
  */
263
281
  declare const createSqlStore: (options: SqlStoreOptions) => RateLimitStore;
264
282
  /**
265
- * The slice of an index-range builder the store uses. Mirrors `@lunora/server`'s
266
- * `IndexRangeBuilder` field-for-field so the real `ctx.db` query builder is
267
- * assignable; only `eq` is exercised.
283
+ * The READ slice — everything the store needs to answer `RateLimiter.getValue`
284
+ * and `check`, which the docs describe as projecting the stored value forward to
285
+ * the current clock. A `QueryCtx`'s `ctx.db` is a reader and satisfies this.
286
+ *
287
+ * Split out because requiring the writer for a pure read meant "how many
288
+ * requests does this user have left" could not be answered from a query context
289
+ * at all — every remaining-quota display had to cast, and a cast that appears
290
+ * often enough stops carrying information. The distinction was already in the
291
+ * methods: `getValue`/`check` read, `limit`/`reset` write.
292
+ *
293
+ * `findFirst` rather than `query(table).withIndex(...)`: a `.global()` table's
294
+ * reader has no `withIndex`, so the indexed builder failed every read there.
295
+ * `findFirst` routes to whichever backend owns the table, and an equality on the
296
+ * key column still uses its index.
268
297
  */
269
- interface RateLimitDatabaseIndexRange {
270
- eq: (field: string, value: unknown) => RateLimitDatabaseIndexRange;
271
- gt: (field: string, value: unknown) => RateLimitDatabaseIndexRange;
272
- gte: (field: string, value: unknown) => RateLimitDatabaseIndexRange;
273
- lt: (field: string, value: unknown) => RateLimitDatabaseIndexRange;
274
- lte: (field: string, value: unknown) => RateLimitDatabaseIndexRange;
275
- }
276
- /** The slice of a `ctx.db` table query the store relies on. */
277
- interface RateLimitDatabaseQuery {
278
- first: () => Promise<Record<string, unknown> | null>;
279
- withIndex: (indexName: string, range: (q: RateLimitDatabaseIndexRange) => RateLimitDatabaseIndexRange) => RateLimitDatabaseQuery;
298
+ interface RateLimitDatabaseReader {
299
+ findFirst: (table: string, args?: {
300
+ where?: Record<string, unknown>;
301
+ }) => Promise<Record<string, unknown> | null>;
280
302
  }
281
303
  /**
282
304
  * The slice of the Lunora ORM writer (`ctx.db` on a mutation/action) the store
@@ -284,26 +306,39 @@ interface RateLimitDatabaseQuery {
284
306
  * directly — declared here (rather than imported) to keep `@lunora/ratelimit`
285
307
  * free of a runtime dependency on `@lunora/server`.
286
308
  */
287
- interface RateLimitDatabase {
309
+ interface RateLimitDatabase extends RateLimitDatabaseReader {
288
310
  delete: <T extends string>(id: Id<T>) => Promise<void>;
289
- insert: <T extends string>(table: T, document: Record<string, unknown>) => Promise<Id<T>>;
290
- patch: <T extends string>(id: Id<T>, patch: Record<string, unknown>) => Promise<void>;
291
- query: (table: string) => RateLimitDatabaseQuery;
311
+ insert: <T extends string>(table: T, document: Record<string, unknown>, options?: {
312
+ clientId?: string;
313
+ }) => Promise<Id<T>>;
314
+ patchIf: <T extends string>(table: T, id: Id<T>, args: {
315
+ patch: Record<string, unknown>;
316
+ where: Record<string, unknown>;
317
+ }) => Promise<{
318
+ applied: boolean;
319
+ doc: Record<string, unknown> | null;
320
+ }>;
292
321
  }
293
- interface DatabaseStoreOptions {
294
- /** The Lunora ORM writer — `ctx.db` inside a mutation or action. */
295
- db: RateLimitDatabase;
296
- /** Index that resolves a row by its key column. Defaults to `by_key`. */
297
- index?: string;
298
- /** Column storing the opaque key. Defaults to `key`. */
322
+ /** The table/column knobs shared by the read-only and read-write stores. */
323
+ interface DatabaseStoreLocation {
324
+ /** Column storing the opaque key. Defaults to `key`. Index it: every consume reads by it. */
299
325
  keyField?: string;
300
326
  /** Table holding one row per `(name, key)` pair. Defaults to `rateLimits`. */
301
327
  table?: string;
302
328
  }
329
+ interface DatabaseStoreOptions extends DatabaseStoreLocation {
330
+ /** The Lunora ORM writer — `ctx.db` inside a mutation or action. */
331
+ db: RateLimitDatabase;
332
+ }
333
+ interface ReadOnlyDatabaseStoreOptions extends DatabaseStoreLocation {
334
+ /** The Lunora ORM reader — `ctx.db` inside a query. */
335
+ db: RateLimitDatabaseReader;
336
+ }
303
337
  /**
304
- * Store backed by a Lunora table through `ctx.db`, for durable per-DO limits
305
- * inside a procedure (the procedure context exposes no raw SQL). Declare a
306
- * table with the key column and its index, e.g.
338
+ * Store backed by a Lunora table through `ctx.db`, for durable limits inside a
339
+ * procedure (the procedure context exposes no raw SQL). Works on a shard-local
340
+ * table and on a `.global()` one. Declare a table with the key column and its
341
+ * index, e.g.
307
342
  *
308
343
  * ```ts
309
344
  * rateLimits: defineTable({
@@ -314,10 +349,38 @@ interface DatabaseStoreOptions {
314
349
  * }).index("by_key", ["key"])
315
350
  * ```
316
351
  *
317
- * Each operation is a read-then-write; inside a mutation/action that pair runs
318
- * under the DO's input gate, so it is atomic against concurrent calls.
352
+ * **Atomic against concurrent consumes.** `setIf` writes only if the row still
353
+ * holds the value `get` returned: an existing row through `ctx.db.patchIf` with
354
+ * that value as its `where`, a new row through an `insert` under an id derived
355
+ * from the key, which a concurrent creator's insert collides with. A consume
356
+ * that loses either race gets `false` back and the limiter reads and decides
357
+ * again, so a limit of 1 admits one caller however many arrive together — on a
358
+ * `.global()` table too, where D1 calls do not hold the Durable Object's input
359
+ * gate.
360
+ *
361
+ * **Consumption commits with the procedure.** A mutation's `ctx.db` writes ride
362
+ * its storage transaction, so a handler that throws after `limit()` rolls the
363
+ * consumed unit back with everything else — inside a mutation this store counts
364
+ * successful calls, not attempts. To charge every attempt (a login limiter),
365
+ * consume from an action, where each write commits on its own, or return a
366
+ * failure value from the mutation instead of throwing. A `.global()` table's
367
+ * row lives in D1, outside the DO transaction, so there the unit stays spent.
319
368
  */
320
369
  declare const createDatabaseStore: (options: DatabaseStoreOptions) => RateLimitStore;
370
+ /**
371
+ * Read-only counterpart to {@link createDatabaseStore}, for a query context.
372
+ *
373
+ * `get` behaves identically — same table, index and key column — so
374
+ * `RateLimiter.getValue` / `check` report exactly what the writing store would.
375
+ * `set` and `delete` are the only difference: they throw rather than silently
376
+ * doing nothing, because a limiter that appears to consume budget and does not
377
+ * is worse than one that refuses.
378
+ *
379
+ * This mirrors the split Lunora already makes for `ctx.storage`, which is a
380
+ * `ReadOnlyStorage` in a query and a full `Storage` in an action — the
381
+ * capability difference is visible in the type instead of discovered at runtime.
382
+ */
383
+ declare const createReadOnlyDatabaseStore: (options: ReadOnlyDatabaseStoreOptions) => RateLimitStore;
321
384
  /**
322
385
  * DB-backed rate-limit middleware sugar. Collapses the common
323
386
  *
@@ -330,12 +393,17 @@ declare const createDatabaseStore: (options: DatabaseStoreOptions) => RateLimitS
330
393
  * on the DO the procedure runs on). Every query/mutation/action ctx exposes a
331
394
  * compatible `db`, so it slots straight into a `.use(...)` chain.
332
395
  *
333
- * Pass `options.store` to point at a non-default backing table/index/key column
334
- * (defaults: table `rateLimits`, index `by_key`, key column `key`); the rest of
396
+ * Pass `options.store` to point at a non-default backing table/key column
397
+ * (defaults: table `rateLimits`, key column `key`); the rest of
335
398
  * `options` (`key`, `count`, `failOpen`, `message`) is forwarded to
336
399
  * {@link rateLimit} unchanged. When `config` is precisely typed, `name`
337
400
  * autocompletes to its declared limit names.
338
401
  *
402
+ * On a mutation the consumed unit commits with the handler: a handler that
403
+ * throws rolls it back, so a failed call costs nothing. Attach it to an action
404
+ * (whose writes commit independently) when failed attempts must count — see
405
+ * {@link createDatabaseStore}.
406
+ *
339
407
  * Re-exported as `dbRateLimit` from the package root.
340
408
  *
341
409
  * ```ts
@@ -386,7 +454,7 @@ interface RatelimitApiContext<Context> {
386
454
  *
387
455
  * ```ts
388
456
  * const limiter = new RateLimiter({ config: { send: { kind: "token bucket", rate: 5, period: 60_000, capacity: 5 } } });
389
- * const c = initLunora.dataModel&lt;DataModel>().create();
457
+ * const c = initLunora.dataModel<DataModel>().create();
390
458
  * export const send = c.mutation
391
459
  * .use(ratelimitPlugin(limiter).middleware!)
392
460
  * .mutation(async ({ ctx, args }) => {
@@ -405,5 +473,50 @@ interface RatelimitApiContext<Context> {
405
473
  * `@lunora/server` a type-only dependency of `@lunora/ratelimit`.
406
474
  */
407
475
  declare const ratelimitPlugin: <Context = unknown>(limiter: LimiterResolver<Context>) => Plugin<Record<never, never>, Context, Context & RatelimitApiContext<Context>>;
476
+ /** A budget bound to one named limit — check before the call, record after it. */
477
+ interface TokenBudget {
478
+ /**
479
+ * Peek at the budget before spending. `ok: false` means it is exhausted:
480
+ * refuse the call, and `retryAfter` says when it refills. Consumes nothing.
481
+ */
482
+ check: (key: string) => Promise<RateLimitStatus>;
483
+ /**
484
+ * Charge the tokens a call actually used. Always call it, including when the
485
+ * call THREW — a failed generation that consumed input tokens still has to be
486
+ * paid for. `tokens` of `0` is a no-op, so a call that spent nothing costs
487
+ * nothing.
488
+ *
489
+ * The charge is a reservation, so it may take the bucket negative: the tokens
490
+ * are already spent, and refusing to record them would let a single oversized
491
+ * call escape the budget entirely.
492
+ */
493
+ record: (key: string, tokens: number) => Promise<RateLimitStatus>;
494
+ }
495
+ /**
496
+ * Bind a {@link TokenBudget} to one of a limiter's named limits.
497
+ *
498
+ * ```ts
499
+ * const budget = tokenBudget(limiter, "tokens");
500
+ * const allowed = await budget.check(userId);
501
+ *
502
+ * if (!allowed.ok) {
503
+ * throw new LunoraError("RATE_LIMITED", `token budget exhausted; retry in ${String(allowed.retryAfter)}ms`);
504
+ * }
505
+ *
506
+ * try {
507
+ * const { text, usage } = await generateText({ model: ctx.ai.model(), prompt });
508
+ *
509
+ * await budget.record(userId, usage?.totalTokens ?? 0);
510
+ *
511
+ * return text;
512
+ * } catch (error) {
513
+ * // The prompt was still sent — charge what is known, then rethrow.
514
+ * await budget.record(userId, estimatedInputTokens);
515
+ *
516
+ * throw error;
517
+ * }
518
+ * ```
519
+ */
520
+ declare const tokenBudget: <Names extends string>(limiter: RateLimiter<Names>, name: Names) => TokenBudget;
408
521
  declare const VERSION = "0.0.0";
409
- export { type DatabaseStoreOptions as DbStoreOptions, type EvaluateOptions, type EvaluateResult, type LimiterResolver, type RateLimitArgs, type RateLimitConfig, type RateLimitConfigMap, type RateLimitDatabase as RateLimitDb, type RateLimitDatabaseIndexRange as RateLimitDbIndexRange, type RateLimitDatabaseQuery as RateLimitDbQuery, RateLimitError, type RateLimitKind, type RateLimitMiddlewareOptions, type RateLimitReason, type RateLimitStatus, type RateLimitStore, type RateLimitValue, RateLimiter, type RateLimiterOptions, type RatelimitApiContext, type SqlLike, type SqlStoreOptions, VERSION, availableAt, createDatabaseStore as createDbStore, createMemoryStore, createSqlStore, databaseRateLimit as dbRateLimit, evaluate, rateLimit, ratelimitPlugin };
522
+ export { type DatabaseStoreOptions as DbStoreOptions, type EvaluateOptions, type EvaluateResult, type LimiterResolver, type RateLimitArgs, type RateLimitConfig, type RateLimitConfigMap, type RateLimitDatabase as RateLimitDb, type RateLimitDatabaseReader as RateLimitDbReader, RateLimitError, type RateLimitKind, type RateLimitMiddlewareOptions, type RateLimitReason, type RateLimitStatus, type RateLimitStore, type RateLimitValue, RateLimiter, type RateLimiterOptions, type RatelimitApiContext, type ReadOnlyDatabaseStoreOptions as ReadOnlyDbStoreOptions, type SqlLike, type SqlStoreOptions, type TokenBudget, VERSION, availableAt, createDatabaseStore as createDbStore, createMemoryStore, createReadOnlyDatabaseStore as createReadOnlyDbStore, createSqlStore, databaseRateLimit as dbRateLimit, evaluate, rateLimit, ratelimitPlugin, tokenBudget };
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import{availableAt as o,evaluate as a}from"./packem_shared/availableAt-DITZ7jdT.mjs";import{default as i}from"./packem_shared/dbRateLimit-D7voUQX3.mjs";import{default as l}from"./packem_shared/RateLimitError-xJ2Iv-i7.mjs";import{rateLimit as x}from"./packem_shared/rateLimit-DLcwK-84.mjs";import{ratelimitPlugin as c}from"./packem_shared/ratelimitPlugin-D1QHZF9b.mjs";import{RateLimiter as L}from"./packem_shared/RateLimiter-BcVZuxFA.mjs";import{createDbStore as b,createMemoryStore as d,createSqlStore as s}from"./packem_shared/createDbStore-VpnhCmRx.mjs";const e="0.0.0";export{l as RateLimitError,L as RateLimiter,e as VERSION,o as availableAt,b as createDbStore,d as createMemoryStore,s as createSqlStore,i as dbRateLimit,a as evaluate,x as rateLimit,c as ratelimitPlugin};
1
+ import{availableAt as o,evaluate as a}from"./packem_shared/availableAt-BSKYrouo.mjs";import{default as i}from"./packem_shared/dbRateLimit-B1Jf_kBi.mjs";import{default as l}from"./packem_shared/RateLimitError-9Y5OLxpl.mjs";import{rateLimit as x}from"./packem_shared/rateLimit-CWZur6XE.mjs";import{ratelimitPlugin as c}from"./packem_shared/ratelimitPlugin-CFnDs9rj.mjs";import{RateLimiter as u}from"./packem_shared/RateLimiter-Cr80iRjQ.mjs";import{createDbStore as b,createMemoryStore as n,createReadOnlyDbStore as L,createSqlStore as s}from"./packem_shared/createDbStore-k6ol27eT.mjs";import{tokenBudget as v}from"./packem_shared/tokenBudget-BRlWn1im.mjs";const e="0.0.0";export{l as RateLimitError,u as RateLimiter,e as VERSION,o as availableAt,b as createDbStore,n as createMemoryStore,L as createReadOnlyDbStore,s as createSqlStore,i as dbRateLimit,a as evaluate,x as rateLimit,c as ratelimitPlugin,v as tokenBudget};
@@ -0,0 +1 @@
1
+ import{LunoraError as n}from"@lunora/errors";import{STATUS_BY_REASON as a}from"./rateLimit-CWZur6XE.mjs";const s=r=>r.reason==="deny"?"request denied (deny list)":Number.isFinite(r.retryAfter)?`rate limit exceeded; retry after ${String(Math.ceil(r.retryAfter))}ms`:"rate limit exceeded";class c extends n{reason;retryAfter;constructor(e,t){const{code:i,status:o}=a[e.reason??"rate"];super(i,t??s(e),{name:"RateLimitError",status:o}),this.reason=e.reason,this.retryAfter=e.retryAfter}}export{c as default};
@@ -0,0 +1 @@
1
+ import{LunoraError as h}from"@lunora/errors";import{availableAt as w,evaluate as p}from"./availableAt-BSKYrouo.mjs";import m from"./RateLimitError-9Y5OLxpl.mjs";import{createMemoryStore as y}from"./createDbStore-k6ol27eT.mjs";const v=/^[\d.:a-f]+$/iu,d=i=>{const t=i.indexOf("%"),e=t===-1?i:i.slice(0,t);if(!e.includes(":")||!v.test(e))return i;let r;try{r=new URL(`http://[${e}]/`).hostname.slice(1,-1)}catch{return i}const[c="",o]=r.split("::"),s=c===""?[]:c.split(":"),l=o===void 0||o===""?[]:o.split(":"),n=[...s,...Array.from({length:8-s.length-l.length},()=>"0"),...l].map(a=>Number.parseInt(a,16));if(n.slice(0,5).every(a=>a===0)&&n[5]===65535){const[a=0,f=0]=n.slice(6);return[a>>8,a&255,f>>8,f&255].join(".")}return`${n.slice(0,4).map(a=>a.toString(16)).join(":")}::/64`},u=(i,t)=>t===void 0?encodeURIComponent(i):`${encodeURIComponent(i)}:${encodeURIComponent(t)}`,b=16,N=i=>typeof i?.then=="function",g=i=>i?t=>d(i(t)):d,I="@lunora/ratelimit: new RateLimiter() was built with no explicit `store`, so it falls back to `createMemoryStore()` — an in-process Map. That Map is durable and correctly shared only for calls that land on the same Durable Object instance; it is NOT shared across `.shardBy(...)` shards or `.global()` replicas, and it resets when the DO instance is evicted/restarted. For a limit that must hold across any of those, pass a durable `store` (`createDbStore` or `createSqlStore`) instead. Pass `store: createMemoryStore()` explicitly once you've confirmed the in-memory default is correct here, to silence this warning.";class E{config;denyList;normalize;now;store;constructor(t){this.config=t.config,this.normalize=g(t.normalize),this.denyList=new Set([...t.denyList??[]].flatMap(e=>[e,this.normalize(e)])),this.now=t.now??Date.now,t.store===void 0&&console.warn(I),this.store=t.store??y();for(const[e,r]of Object.entries(this.config)){if(Object.hasOwn(r,"shards"))throw new h("INTERNAL",`rate limit "${e}": \`shards\` is no longer supported — remove it; the limit now enforces its full \`rate\` (lower \`rate\` if you raised it to offset the split)`);if(!Number.isFinite(r.period)||r.period<=0)throw new h("INTERNAL",`rate limit "${e}": period must be a positive number`);if(!Number.isFinite(r.rate)||r.rate<=0)throw new h("INTERNAL",`rate limit "${e}": rate must be a positive number`);if(r.capacity!==void 0&&(!Number.isFinite(r.capacity)||r.capacity<0))throw new h("INTERNAL",`rate limit "${e}": capacity must be a non-negative number`)}}async check(t,e={}){return this.run(t,e,!1)}async getValue(t,e={}){const r=this.resolve(t),c=this.now(),o=u(t,this.normalizeKey(e.key)),s=w(r,await this.store.get(o),c);return{config:r,ts:s.ts,value:s.value}}async limit(t,e={}){return this.run(t,e,!0)}async reset(t,e={}){this.resolve(t),await Promise.resolve(this.store.delete(u(t,this.normalizeKey(e.key))))}async decide(t,e,r){for(let c=0;c<b;c+=1){const o=this.store.get(e),s=N(o)?await o:o,{status:l,value:n}=p(t,s,{...r,now:this.now()});if(n===void 0)return l;if(this.store.setIf===void 0)return await this.store.set(e,n),l;if(await this.store.setIf(e,n,s))return l}return{ok:!1,reason:"rate",retryAfter:0}}normalizeKey(t){return t===void 0?void 0:this.normalize(t)}resolve(t){const e=this.config[t];if(!e)throw new h("INTERNAL",`rate limit "${t}" is not configured`);return e}async run(t,e,r){const c=this.resolve(t),o=this.normalizeKey(e.key);if(o!==void 0&&(this.denyList.has(o)||this.denyList.has(e.key))){const a={ok:!1,reason:"deny",retryAfter:Number.POSITIVE_INFINITY};if(e.throws)throw new m(a);return a}const s=e.count??1;if(!Number.isInteger(s)||s<=0)throw new h("INTERNAL",`rate limit "${t}": count must be a positive integer`);const l=u(t,o),n=await this.decide(c,l,{consume:r,count:s,reserve:e.reserve??!1});if(!n.ok&&e.throws)throw new m(n);return n}}export{E as RateLimiter};
@@ -0,0 +1 @@
1
+ import{LunoraError as b}from"@lunora/errors";const o=e=>e.capacity??e.rate,E=e=>e.kind==="sliding window"?e.rate:o(e),i=(e,r)=>{throw new b("INTERNAL",`@lunora/ratelimit: requested count ${String(e)} exceeds the limiter capacity ${String(r)}`)},M=(e,r,t)=>{const u=o(e),a=e.rate/e.period,s=r??{ts:t,value:u},c=Math.max(0,t-s.ts);return{available:Math.min(u,s.value+c*a),capacity:u,ratePerMs:a}},w=(e,r,t)=>{const u=e.start??0,a=u+Math.floor((t-u)/e.period)*e.period;if(!r)return{ts:a,value:o(e)};if(r.ts<a){let s=0,c=1;return(r.value<0||e.capacity!==void 0)&&(s=r.value,c=Math.max(1,Math.ceil((a-r.ts)/e.period))),{ts:a,value:Math.min(o(e),s+c*e.rate)}}return{ts:r.ts,value:r.value}},x=(e,r,t)=>{const u=e.start??0,a=u+Math.floor((t-u)/e.period)*e.period,s=t-a,c=(e.period-s)/e.period;let n=0,l=0;return r?.ts===a?(n=r.prev??0,l=r.value):r?.ts===a-e.period&&(n=r.value),{currentCount:l,elapsed:s,previousCount:n,weight:c,windowStart:a}},S=(e,r,t)=>{const{available:u,capacity:a,ratePerMs:s}=M(e,r,t.now);if(u>=t.count){const l={ts:t.now,value:u-t.count};return{status:{ok:!0,retryAfter:0},value:t.consume?l:void 0}}const c=t.count-u,n=Math.ceil(c/s);return t.consume&&t.reserve&&t.count<=a?{status:{ok:!0,retryAfter:n},value:{ts:t.now,value:u-t.count}}:(t.count>a&&i(t.count,a),{status:{ok:!1,reason:"rate",retryAfter:n},value:void 0})},y=(e,r,t,u,a,s)=>{const c=d=>w(e,r,t+d*e.period).value>=a;let l=u<0||e.capacity!==void 0?Math.max(1,Math.ceil((a-u)/e.rate)):1;for(let d=0;d<4&&l>1&&c(l-1);d+=1)l-=1;for(let d=0;d<4&&!c(l);d+=1)l+=1;return t+l*e.period-s},W=(e,r,t)=>{const u=o(e),a=w(e,r,t.now);if(a.value>=t.count){const s={ts:a.ts,value:a.value-t.count};return{status:{ok:!0,retryAfter:0},value:t.consume?s:void 0}}if(t.consume&&t.reserve&&t.count<=u){const s=a.value-t.count;return{status:{ok:!0,retryAfter:y(e,{ts:a.ts,value:s},a.ts,s,0,t.now)},value:{ts:a.ts,value:s}}}return t.count>u&&i(t.count,u),{status:{ok:!1,reason:"rate",retryAfter:y(e,r,a.ts,a.value,t.count,t.now)},value:void 0}},N=(e,r,t)=>{const u=e.rate,{period:a}=e,{currentCount:s,elapsed:c,previousCount:n,weight:l,windowStart:d}=x(e,r,t.now),m=n*l+s+t.count<=u,h=v=>{const k=u-v-t.count;if(n>0&&k>=0)return Math.ceil(a-c-k*a/n);const A=u-t.count,C=v>0?Math.max(0,a-A*a/v):0;return Math.ceil(a-c+C)};if(m||t.consume&&t.reserve&&t.count<=u){const v={prev:n,ts:d,value:s+t.count};return{status:{ok:!0,retryAfter:m?0:h(v.value)},value:t.consume?v:void 0}}return t.count>u&&i(t.count,u),{status:{ok:!1,reason:"rate",retryAfter:h(s)},value:void 0}},B=(e,r,t)=>{if(e.kind==="token bucket")return{ts:t,value:M(e,r,t).available};if(e.kind==="sliding window"){const{currentCount:u,previousCount:a,weight:s,windowStart:c}=x(e,r,t);return{ts:c,value:Math.max(0,e.rate-(a*s+u))}}return w(e,r,t)},L=(e,r,t)=>e.kind==="token bucket"?S(e,r,t):e.kind==="sliding window"?N(e,r,t):W(e,r,t);export{B as availableAt,E as enforcedCapacity,L as evaluate};
@@ -0,0 +1 @@
1
+ import{LunoraError as E}from"@lunora/errors";const m=()=>{const n=new Map;return{delete:t=>{n.delete(t)},get:t=>n.get(t),set:(t,c)=>{n.set(t,c)}}},u=(n,t,...c)=>n.exec.call(n,t,...c).toArray(),L=n=>{const{sql:t}=n,c=n.table??"_lunora_rate_limits";return u(t,`CREATE TABLE IF NOT EXISTS "${c}" (k TEXT PRIMARY KEY, value REAL NOT NULL, ts INTEGER NOT NULL, prev REAL)`),{delete:s=>{u(t,`DELETE FROM "${c}" WHERE k = ?`,s)},get:s=>{const l=u(t,`SELECT value, ts, prev FROM "${c}" WHERE k = ?`,s)[0];if(!l)return;const d={ts:l.ts,value:l.value};return l.prev!==null&&(d.prev=l.prev),d},set:(s,a)=>{u(t,`INSERT INTO "${c}" (k, value, ts, prev) VALUES (?, ?, ?, ?) ON CONFLICT(k) DO UPDATE SET value = excluded.value, ts = excluded.ts, prev = excluded.prev`,s,a.value,a.ts,a.prev??null)}}},b=n=>n.toString(16).padStart(2,"0"),S=async(n,t)=>{const s=[...new Uint8Array(await crypto.subtle.digest("SHA-256",new TextEncoder().encode(`${n}\0${t}`))).subarray(0,16)];s[6]=128+s[6]%16,s[8]=128+s[8]%64;const a=s.map(l=>b(l)).join("");return`${a.slice(0,8)}-${a.slice(8,12)}-${a.slice(12,16)}-${a.slice(16,20)}-${a.slice(20)}`},T=n=>n?.code==="CONFLICT",h=n=>{const{db:t}=n,c=n.table??"rateLimits",s=n.keyField??"key",a=new Map,l=async e=>{const r=await t.findFirst(c,{where:{[s]:e}});return a.set(e,r?r._id:void 0),r},d=async e=>(a.has(e)||await l(e),a.get(e)),v=(e,r)=>{const i={[s]:e,ts:r.ts,value:r.value};return r.prev!==void 0&&(i.prev=r.prev),i},w=async(e,r)=>{try{return a.set(e,await t.insert(c,r,{clientId:await S(c,e)})),!0}catch(i){if(T(i))return a.delete(e),!1;throw i}};return{delete:async e=>{const r=await d(e);r!==void 0&&await t.delete(r),a.delete(e)},get:async e=>{const r=await l(e);if(!r)return;const i={ts:r.ts,value:r.value};return r.prev!==null&&r.prev!==void 0&&(i.prev=r.prev),i},set:async(e,r)=>{const i=v(e,r);let o=await d(e);if(o===void 0){if(await w(e,i))return;await l(e),o=a.get(e)}o!==void 0&&await t.patchIf(c,o,{patch:i,where:{}})},setIf:async(e,r,i)=>{const o=v(e,r);if(i===void 0)return w(e,o);const f=await d(e);if(f===void 0)return!1;const{applied:p}=await t.patchIf(c,f,{patch:o,where:{prev:i.prev??null,ts:i.ts,value:i.value}});return p||a.delete(e),p}}},R=n=>{const t=s=>{throw new E("INTERNAL",`@lunora/ratelimit: \`${s}\` needs a writable \`ctx.db\`, but this store was created with \`createReadOnlyDbStore\` (a query context). Use \`createDbStore\` from a mutation or action; a query can only call \`getValue\`/\`check\`.`)},c=h({...n,db:{delete:()=>t("delete"),findFirst:n.db.findFirst.bind(n.db),insert:()=>t("insert"),patchIf:()=>t("patchIf")}});return{delete:()=>t("reset"),get:c.get,set:()=>t("limit"),setIf:()=>t("limit")}};export{h as createDbStore,m as createMemoryStore,R as createReadOnlyDbStore,L as createSqlStore};
@@ -0,0 +1 @@
1
+ import{rateLimit as o}from"./rateLimit-CWZur6XE.mjs";import{RateLimiter as m}from"./RateLimiter-Cr80iRjQ.mjs";import{createDbStore as i}from"./createDbStore-k6ol27eT.mjs";const f=(e,r,t={})=>o(a=>new m({config:e,store:i({db:a.db,...t.store})}),r,t);export{f as default};
@@ -0,0 +1 @@
1
+ import{isLunoraError as c,isInternalCode as l,LunoraError as n}from"@lunora/errors";import{tagPerDispatchMiddleware as f}from"@lunora/server";const v={deny:{code:"FORBIDDEN",status:403},rate:{code:"TOO_MANY_REQUESTS",status:429}},y=(r,t,e)=>t==="deny"?`request denied for "${r}"`:e===void 0?`rate limit "${r}" exceeded`:`rate limit "${r}" exceeded; retry after ${String(e)}ms`,w=(r,t,e)=>{if(!e)return;const a=e(t);if(a===void 0)throw new n("INTERNAL",`@lunora/ratelimit: rateLimit("${r}") key resolver returned undefined; return a fallback such as "anonymous" instead`);return a},h=(r,t,e={})=>f(async({ctx:a,next:d})=>{let i;try{i=await(typeof r=="function"?await r(a):r).limit(t,{count:e.count,key:w(t,a,e.key)})}catch(o){if(c(o)&&l(o.code))throw o;if(console.error(`@lunora/ratelimit: rateLimit("${t}") threw; ${e.failOpen?"failing open":"failing closed"}`,o),e.failOpen)return d();throw new n("SERVICE_UNAVAILABLE",`rate limiter unavailable for "${t}"`,{cause:o,status:503})}if(!i.ok){const o=i.reason??"rate",u=v[o],s=Number.isFinite(i.retryAfter)?Math.ceil(i.retryAfter):void 0;throw new n(u.code,e.message??y(t,o,s),{status:u.status,data:s===void 0?void 0:{retryAfterMs:s}})}return d()});export{v as STATUS_BY_REASON,h as rateLimit};
@@ -0,0 +1 @@
1
+ const r=t=>({key:"ratelimit",middleware:async({ctx:e,next:i})=>{const n=typeof t=="function"?await t(e):t,a=e.api??{};return i({ctx:{api:{...a,ratelimit:n}}})}});export{r as ratelimitPlugin};
@@ -0,0 +1 @@
1
+ import{enforcedCapacity as n}from"./availableAt-BSKYrouo.mjs";const h=(t,r)=>({check:async c=>t.check(r,{key:c}),record:async(c,a)=>{if(!Number.isFinite(a)||a<=0)return t.check(r,{key:c});const{config:e}=await t.getValue(r,{key:c}),o=Math.max(1,Math.floor(n(e)));return t.limit(r,{count:Math.min(Math.ceil(a),o),key:c,reserve:!0})}});export{h as tokenBudget};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@lunora/ratelimit",
3
- "version": "1.0.0-alpha.13",
4
- "description": "Rate limiting: token-bucket / fixed-window / sliding-window algorithms, deny list, sharding, pluggable stores, and procedure middleware",
3
+ "version": "1.0.0-alpha.131",
4
+ "description": "Rate limiting: token-bucket / fixed-window / sliding-window algorithms, deny list, pluggable stores, and procedure middleware",
5
5
  "keywords": [
6
6
  "cloudflare",
7
7
  "durable-objects",
@@ -46,7 +46,11 @@
46
46
  "access": "public"
47
47
  },
48
48
  "dependencies": {
49
- "@lunora/errors": "1.0.0-alpha.10"
49
+ "@lunora/errors": ">=1.0.0-alpha.51 <2.0.0-0"
50
+ },
51
+ "peerDependencies": {
52
+ "@lunora/server": ">=1.0.0-alpha.24 <2.0.0-0",
53
+ "@lunora/values": ">=1.0.0-alpha.7 <2.0.0-0"
50
54
  },
51
55
  "engines": {
52
56
  "node": "^22.15.0 || >=24.11.0"
@@ -1 +0,0 @@
1
- import{LunoraError as i}from"@lunora/errors";import{STATUS_BY_REASON as o}from"./rateLimit-DLcwK-84.mjs";const n=e=>e.reason==="deny"?"request denied (deny list)":Number.isFinite(e.retryAfter)?`rate limit exceeded; retry after ${String(Math.ceil(e.retryAfter))}ms`:"rate limit exceeded";class c extends i{reason;retryAfter;constructor(r,t){const{code:s,status:a}=o[r.reason??"rate"];super(s,t??n(r),{name:"RateLimitError",status:a}),this.reason=r.reason,this.retryAfter=r.retryAfter}}export{c as default};
@@ -1 +0,0 @@
1
- import{LunoraError as n}from"@lunora/errors";import{availableAt as p,evaluate as N}from"./availableAt-DITZ7jdT.mjs";import d from"./RateLimitError-xJ2Iv-i7.mjs";import{createMemoryStore as g}from"./createDbStore-VpnhCmRx.mjs";const v=(r,t)=>t===void 0?encodeURIComponent(r):`${encodeURIComponent(r)}:${encodeURIComponent(t)}`,b=(r,t)=>{let e=0;for(let i=0;i<r.length;i+=1)e=e*31+r.charCodeAt(i)|0;return Math.abs(e)%t},w=(r,t)=>t>1?{...r,capacity:(r.capacity??r.rate)/t,rate:r.rate/t}:r,I=(r,t,e)=>{const i=v(r,t);return e>1?Array.from({length:e},(o,s)=>`${i}#${String(s)}`):[i]},f=(r,t,e)=>{const i=v(r,t);return e>1?`${i}#${String(b(i,e))}`:i};class R{config;denyList;normalize;now;store;constructor(t){this.config=t.config,this.denyList=new Set(t.denyList),this.normalize=t.normalize??(e=>e),this.now=t.now??Date.now,this.store=t.store??g();for(const[e,i]of Object.entries(this.config)){if(i.shards!==void 0&&(!Number.isInteger(i.shards)||i.shards<1))throw new n("INTERNAL",`rate limit "${e}": shards must be a positive integer`);if(!Number.isFinite(i.period)||i.period<=0)throw new n("INTERNAL",`rate limit "${e}": period must be a positive number`);if(!Number.isFinite(i.rate)||i.rate<=0)throw new n("INTERNAL",`rate limit "${e}": rate must be a positive number`);if(i.capacity!==void 0&&(!Number.isFinite(i.capacity)||i.capacity<0))throw new n("INTERNAL",`rate limit "${e}": capacity must be a non-negative number`)}}async check(t,e={}){return this.run(t,e,!1)}async getValue(t,e={}){const i=this.resolve(t),o=i.shards??1,s=this.now(),a=e.key===void 0?void 0:this.normalize(e.key),m=f(t,a,o),c=p(w(i,o),await this.store.get(m),s);return{config:i,ts:c.ts,value:c.value}}async limit(t,e={}){return this.run(t,e,!0)}async reset(t,e={}){const i=this.resolve(t).shards??1,o=e.key===void 0?void 0:this.normalize(e.key);await Promise.all(I(t,o,i).map(s=>Promise.resolve(this.store.delete(s))))}resolve(t){const e=this.config[t];if(!e)throw new n("INTERNAL",`rate limit "${t}" is not configured`);return e}async run(t,e,i){const o=this.resolve(t),s=e.key===void 0?void 0:this.normalize(e.key);if(s!==void 0&&(this.denyList.has(s)||this.denyList.has(e.key))){const l={ok:!1,reason:"deny",retryAfter:Number.POSITIVE_INFINITY};if(e.throws)throw new d(l);return l}const a=e.count??1;if(!Number.isInteger(a)||a<=0)throw new n("INTERNAL",`rate limit "${t}": count must be a positive integer`);const m=o.shards??1,c=f(t,s,m),y=await this.store.get(c),{status:h,value:u}=N(w(o,m),y,{consume:i,count:a,now:this.now(),reserve:e.reserve??!1});if(u!==void 0&&await this.store.set(c,u),!h.ok&&e.throws)throw new d(h);return h}}export{R as RateLimiter};
@@ -1 +0,0 @@
1
- import{LunoraError as l}from"@lunora/errors";const v=r=>r.capacity??r.rate,p=(r,e,t)=>{const n=v(r),u=r.rate/r.period,o=e??{ts:t,value:n},a=Math.max(0,t-o.ts);return{available:Math.min(n,o.value+a*u),capacity:n,ratePerMs:u}},w=(r,e,t)=>{const n=r.start??0,u=n+Math.floor((t-n)/r.period)*r.period;if(!e||e.ts<u){let o=0;return e&&(e.value<0||r.capacity!==void 0)&&(o=e.value),{ts:u,value:Math.min(v(r),o+r.rate)}}return{ts:e.ts,value:e.value}},h=(r,e,t)=>{const n=r.start??0,u=n+Math.floor((t-n)/r.period)*r.period,o=t-u,a=(r.period-o)/r.period;let i=0,s=0;return e?.ts===u?(i=e.prev??0,s=e.value):e?.ts===u-r.period&&(i=e.value),{currentCount:s,elapsed:o,previousCount:i,weight:a,windowStart:u}},A=(r,e,t)=>{const{available:n,capacity:u,ratePerMs:o}=p(r,e,t.now);if(n>=t.count){const s={ts:t.now,value:n-t.count};return{status:{ok:!0,retryAfter:0},value:t.consume?s:void 0}}const a=t.count-n,i=Math.ceil(a/o);if(t.consume&&t.reserve&&t.count<=u)return{status:{ok:!0,retryAfter:i},value:{ts:t.now,value:n-t.count}};if(t.count>u)throw new l("INTERNAL",`@lunora/ratelimit: requested count ${String(t.count)} exceeds the limiter capacity ${String(u)}`);return{status:{ok:!1,reason:"rate",retryAfter:i},value:void 0}},M=(r,e,t)=>{const n=v(r),u=w(r,e,t.now);if(u.value>=t.count){const a={ts:u.ts,value:u.value-t.count};return{status:{ok:!0,retryAfter:0},value:t.consume?a:void 0}}const o=u.ts+r.period-t.now;if(t.consume&&t.reserve&&t.count<=n)return{status:{ok:!0,retryAfter:o},value:{ts:u.ts,value:u.value-t.count}};if(t.count>n)throw new l("INTERNAL",`@lunora/ratelimit: requested count ${String(t.count)} exceeds the limiter capacity ${String(n)}`);return{status:{ok:!1,reason:"rate",retryAfter:o},value:void 0}},g=(r,e,t)=>{const n=r.rate,{period:u}=r,{currentCount:o,elapsed:a,previousCount:i,weight:s,windowStart:m}=h(r,e,t.now),d=i*s+o+t.count<=n,f=()=>{const c=n-o-t.count;if(i>0&&c>=0)return Math.ceil(u-a-c*u/i);const k=n-t.count,y=o>0?Math.max(0,u-k*u/o):0;return Math.ceil(u-a+y)};if(d||t.consume&&t.reserve&&t.count<=n){const c={prev:i,ts:m,value:o+t.count};return{status:{ok:!0,retryAfter:d?0:f()},value:t.consume?c:void 0}}if(t.count>n)throw new l("INTERNAL",`@lunora/ratelimit: requested count ${String(t.count)} exceeds the limiter capacity ${String(n)}`);return{status:{ok:!1,reason:"rate",retryAfter:f()},value:void 0}},b=(r,e,t)=>{if(r.kind==="token bucket")return{ts:t,value:p(r,e,t).available};if(r.kind==="sliding window"){const{currentCount:n,previousCount:u,weight:o,windowStart:a}=h(r,e,t);return{ts:a,value:Math.max(0,r.rate-(u*o+n))}}return w(r,e,t)},x=(r,e,t)=>r.kind==="token bucket"?A(r,e,t):r.kind==="sliding window"?g(r,e,t):M(r,e,t);export{b as availableAt,x as evaluate};
@@ -1 +0,0 @@
1
- const d=()=>{const a=new Map;return{delete:t=>{a.delete(t)},get:t=>a.get(t),set:(t,l)=>{a.set(t,l)}}},n=(a,t,...l)=>a.exec.call(a,t,...l).toArray(),p=a=>{const{sql:t}=a,l=a.table??"_lunora_rate_limits";return n(t,`CREATE TABLE IF NOT EXISTS "${l}" (k TEXT PRIMARY KEY, value REAL NOT NULL, ts INTEGER NOT NULL, prev REAL)`),{delete:u=>{n(t,`DELETE FROM "${l}" WHERE k = ?`,u)},get:u=>{const s=n(t,`SELECT value, ts, prev FROM "${l}" WHERE k = ?`,u)[0];if(!s)return;const v={ts:s.ts,value:s.value};return s.prev!==null&&(v.prev=s.prev),v},set:(u,s)=>{n(t,`INSERT INTO "${l}" (k, value, ts, prev) VALUES (?, ?, ?, ?) ON CONFLICT(k) DO UPDATE SET value = excluded.value, ts = excluded.ts, prev = excluded.prev`,u,s.value,s.ts,s.prev??null)}}},T=a=>{const{db:t}=a,l=a.table??"rateLimits",u=a.index??"by_key",s=a.keyField??"key",v=new Map,o=async r=>{const e=await t.query(l).withIndex(u,i=>i.eq(s,r)).first();return v.set(r,e?e._id:void 0),e},E=async r=>(v.has(r)||await o(r),v.get(r));return{delete:async r=>{const e=await E(r);e!==void 0&&await t.delete(e),v.delete(r)},get:async r=>{const e=await o(r);if(!e)return;const i={ts:e.ts,value:e.value};return e.prev!==null&&e.prev!==void 0&&(i.prev=e.prev),i},set:async(r,e)=>{const i=await E(r),c={[s]:r,ts:e.ts,value:e.value};e.prev!==void 0&&(c.prev=e.prev),i===void 0?v.set(r,await t.insert(l,c)):await t.patch(i,c)}}};export{T as createDbStore,d as createMemoryStore,p as createSqlStore};
@@ -1 +0,0 @@
1
- import{rateLimit as a}from"./rateLimit-DLcwK-84.mjs";import{RateLimiter as i}from"./RateLimiter-BcVZuxFA.mjs";import{createDbStore as m}from"./createDbStore-VpnhCmRx.mjs";const b=(t,o,r={})=>a(e=>new i({config:t,store:m({db:e.db,...r.store})}),o,r);export{b as default};
@@ -1 +0,0 @@
1
- import{isLunoraError as f,isInternalCode as l,LunoraError as c}from"@lunora/errors";const u={deny:{code:"FORBIDDEN",status:403},rate:{code:"TOO_MANY_REQUESTS",status:429}},m=(r,a,e)=>a==="deny"?`request denied for "${r}"`:e===void 0?`rate limit "${r}" exceeded`:`rate limit "${r}" exceeded; retry after ${String(e)}ms`,A=(r,a,e={})=>async({ctx:n,next:s})=>{let i;try{i=await(typeof r=="function"?await r(n):r).limit(a,{count:e.count,key:e.key?.(n)})}catch(t){if(f(t)&&l(t.code))throw t;if(console.error(`@lunora/ratelimit: rateLimit("${a}") threw; ${e.failOpen?"failing open":"failing closed"}`,t),e.failOpen)return s();throw new c("SERVICE_UNAVAILABLE",`rate limiter unavailable for "${a}"`,{cause:t,status:503})}if(!i.ok){const t=i.reason??"rate",d=u[t],o=Number.isFinite(i.retryAfter)?Math.ceil(i.retryAfter):void 0;throw new c(d.code,e.message??m(a,t,o),{status:d.status,data:o===void 0?void 0:{retryAfter:o}})}return s()};export{u as STATUS_BY_REASON,A as rateLimit};
@@ -1 +0,0 @@
1
- const r=t=>({key:"ratelimit",middleware:async({ctx:i,next:a})=>{const n=typeof t=="function"?await t(i):t,e=i.api??{};return a({ctx:{api:{...e,ratelimit:n}}})}});export{r as ratelimitPlugin};