@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.
- package/LICENSE.md +6 -0
- package/__assets__/package-og.svg +1 -1
- package/dist/index.d.mts +269 -3
- package/dist/index.d.ts +269 -3
- package/dist/index.mjs +5 -5
- package/dist/packem_shared/{RateLimitError-YOUfRzXc.mjs → RateLimitError-CiLy3DsZ.mjs} +6 -3
- package/dist/packem_shared/{RateLimiter-CeElVjB0.mjs → RateLimiter-rDCxu_Nx.mjs} +16 -17
- package/dist/packem_shared/{availableAt-D5GyJtxT.mjs → availableAt-DQcuVfSA.mjs} +46 -49
- package/dist/packem_shared/{dbRateLimit-C4nMF5cN.mjs → dbRateLimit-WTU-e0r6.mjs} +2 -2
- package/dist/packem_shared/{rateLimit-uQxVMZfh.mjs → rateLimit-BBdG9GFo.mjs} +10 -12
- package/package.json +5 -2
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { rateLimit } from './rateLimit-
|
|
2
|
-
import { RateLimiter } from './RateLimiter-
|
|
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
|
|
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
|
|
33
|
-
|
|
34
|
-
|
|
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.
|
|
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
|
}
|