@lunora/ratelimit 1.0.0-alpha.1 → 1.0.0-alpha.10

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.
@@ -1,5 +1,5 @@
1
- import { rateLimit } from './rateLimit-uQxVMZfh.mjs';
2
- import { RateLimiter } from './RateLimiter-CeElVjB0.mjs';
1
+ import { rateLimit } from './rateLimit-BBdG9GFo.mjs';
2
+ import { RateLimiter } from './RateLimiter-rDCxu_Nx.mjs';
3
3
  import { createDbStore as createDatabaseStore } from './createDbStore-L1kD1g1n.mjs';
4
4
 
5
5
  const databaseRateLimit = (config, name, options = {}) => rateLimit((context) => new RateLimiter({ config, store: createDatabaseStore({ db: context.db, ...options.store }) }), name, options);
@@ -1,3 +1,5 @@
1
+ import { isLunoraError, isInternalCode, LunoraError } from '@lunora/errors';
2
+
1
3
  const STATUS_BY_REASON = {
2
4
  deny: { code: "FORBIDDEN", status: 403 },
3
5
  rate: { code: "TOO_MANY_REQUESTS", status: 429 }
@@ -14,29 +16,25 @@ const rateLimit = (limiter, name, options = {}) => async ({ ctx, next }) => {
14
16
  const resolved = typeof limiter === "function" ? await limiter(ctx) : limiter;
15
17
  status = await resolved.limit(name, { count: options.count, key: options.key?.(ctx) });
16
18
  } catch (error) {
19
+ if (isLunoraError(error) && isInternalCode(error.code)) {
20
+ throw error;
21
+ }
17
22
  console.error(`@lunora/ratelimit: rateLimit("${name}") threw; ${options.failOpen ? "failing open" : "failing closed"}`, error);
18
23
  if (options.failOpen) {
19
24
  return next();
20
25
  }
21
- throw Object.assign(new Error(`rate limiter unavailable for "${name}"`), {
22
- cause: error,
23
- code: "SERVICE_UNAVAILABLE",
24
- name: "LunoraError",
25
- status: 503
26
- });
26
+ throw new LunoraError("SERVICE_UNAVAILABLE", `rate limiter unavailable for "${name}"`, { cause: error, status: 503 });
27
27
  }
28
28
  if (!status.ok) {
29
29
  const reason = status.reason ?? "rate";
30
30
  const mapped = STATUS_BY_REASON[reason];
31
31
  const retryAfter = Number.isFinite(status.retryAfter) ? Math.ceil(status.retryAfter) : void 0;
32
- throw Object.assign(new Error(options.message ?? defaultMessage(name, reason, retryAfter)), {
33
- code: mapped.code,
34
- name: "LunoraError",
35
- retryAfter,
36
- status: mapped.status
32
+ throw new LunoraError(mapped.code, options.message ?? defaultMessage(name, reason, retryAfter), {
33
+ status: mapped.status,
34
+ data: retryAfter === void 0 ? void 0 : { retryAfter }
37
35
  });
38
36
  }
39
37
  return next();
40
38
  };
41
39
 
42
- export { rateLimit };
40
+ export { STATUS_BY_REASON, rateLimit };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunora/ratelimit",
3
- "version": "1.0.0-alpha.1",
3
+ "version": "1.0.0-alpha.10",
4
4
  "description": "Rate limiting: token-bucket / fixed-window / sliding-window algorithms, deny list, sharding, pluggable stores, and procedure middleware",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -25,7 +25,7 @@
25
25
  "directory": "packages/ratelimit"
26
26
  },
27
27
  "files": [
28
- "dist",
28
+ "./dist",
29
29
  "__assets__",
30
30
  "README.md",
31
31
  "LICENSE.md"
@@ -45,6 +45,9 @@
45
45
  "publishConfig": {
46
46
  "access": "public"
47
47
  },
48
+ "dependencies": {
49
+ "@lunora/errors": "1.0.0-alpha.8"
50
+ },
48
51
  "engines": {
49
52
  "node": "^22.15.0 || >=24.11.0"
50
53
  }