@lunora/ratelimit 0.0.0 → 1.0.0-alpha.1
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 +105 -0
- package/README.md +128 -9
- package/__assets__/package-og.svg +14 -0
- package/dist/index.d.mts +143 -0
- package/dist/index.d.ts +143 -0
- package/dist/index.mjs +11 -0
- package/dist/packem_shared/RateLimitError-YOUfRzXc.mjs +18 -0
- package/dist/packem_shared/RateLimiter-CeElVjB0.mjs +122 -0
- package/dist/packem_shared/availableAt-D5GyJtxT.mjs +117 -0
- package/dist/packem_shared/createDbStore-L1kD1g1n.mjs +103 -0
- package/dist/packem_shared/dbRateLimit-C4nMF5cN.mjs +7 -0
- package/dist/packem_shared/rateLimit-uQxVMZfh.mjs +42 -0
- package/dist/packem_shared/ratelimitPlugin-D5_-KV1T.mjs +12 -0
- package/package.json +37 -17
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
const STATUS_BY_REASON = {
|
|
2
|
+
deny: { code: "FORBIDDEN", status: 403 },
|
|
3
|
+
rate: { code: "TOO_MANY_REQUESTS", status: 429 }
|
|
4
|
+
};
|
|
5
|
+
const defaultMessage = (name, reason, retryAfter) => {
|
|
6
|
+
if (reason === "deny") {
|
|
7
|
+
return `request denied for "${name}"`;
|
|
8
|
+
}
|
|
9
|
+
return retryAfter === void 0 ? `rate limit "${name}" exceeded` : `rate limit "${name}" exceeded; retry after ${String(retryAfter)}ms`;
|
|
10
|
+
};
|
|
11
|
+
const rateLimit = (limiter, name, options = {}) => async ({ ctx, next }) => {
|
|
12
|
+
let status;
|
|
13
|
+
try {
|
|
14
|
+
const resolved = typeof limiter === "function" ? await limiter(ctx) : limiter;
|
|
15
|
+
status = await resolved.limit(name, { count: options.count, key: options.key?.(ctx) });
|
|
16
|
+
} catch (error) {
|
|
17
|
+
console.error(`@lunora/ratelimit: rateLimit("${name}") threw; ${options.failOpen ? "failing open" : "failing closed"}`, error);
|
|
18
|
+
if (options.failOpen) {
|
|
19
|
+
return next();
|
|
20
|
+
}
|
|
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
|
+
});
|
|
27
|
+
}
|
|
28
|
+
if (!status.ok) {
|
|
29
|
+
const reason = status.reason ?? "rate";
|
|
30
|
+
const mapped = STATUS_BY_REASON[reason];
|
|
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
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
return next();
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export { rateLimit };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const ratelimitPlugin = (limiter) => {
|
|
2
|
+
return {
|
|
3
|
+
key: "ratelimit",
|
|
4
|
+
middleware: async ({ ctx, next }) => {
|
|
5
|
+
const resolved = typeof limiter === "function" ? await limiter(ctx) : limiter;
|
|
6
|
+
const existingApi = ctx.api ?? {};
|
|
7
|
+
return next({ ctx: { api: { ...existingApi, ratelimit: resolved } } });
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export { ratelimitPlugin };
|
package/package.json
CHANGED
|
@@ -1,31 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lunora/ratelimit",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "1.0.0-alpha.1",
|
|
4
4
|
"description": "Rate limiting: token-bucket / fixed-window / sliding-window algorithms, deny list, sharding, pluggable stores, and procedure middleware",
|
|
5
|
-
"
|
|
5
|
+
"keywords": [
|
|
6
|
+
"cloudflare",
|
|
7
|
+
"durable-objects",
|
|
8
|
+
"lunora",
|
|
9
|
+
"middleware",
|
|
10
|
+
"rate-limit",
|
|
11
|
+
"sliding-window",
|
|
12
|
+
"token-bucket",
|
|
13
|
+
"workers"
|
|
14
|
+
],
|
|
6
15
|
"homepage": "https://lunora.sh",
|
|
16
|
+
"bugs": "https://github.com/anolilab/lunora/issues",
|
|
17
|
+
"license": "FSL-1.1-Apache-2.0",
|
|
18
|
+
"author": {
|
|
19
|
+
"name": "Daniel Bannert",
|
|
20
|
+
"email": "d.bannert@anolilab.de"
|
|
21
|
+
},
|
|
7
22
|
"repository": {
|
|
8
23
|
"type": "git",
|
|
9
24
|
"url": "git+https://github.com/anolilab/lunora.git",
|
|
10
25
|
"directory": "packages/ratelimit"
|
|
11
26
|
},
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
"
|
|
17
|
-
"cloudflare",
|
|
18
|
-
"workers",
|
|
19
|
-
"durable-objects",
|
|
20
|
-
"rate-limit",
|
|
21
|
-
"token-bucket",
|
|
22
|
-
"sliding-window",
|
|
23
|
-
"middleware"
|
|
27
|
+
"files": [
|
|
28
|
+
"dist",
|
|
29
|
+
"__assets__",
|
|
30
|
+
"README.md",
|
|
31
|
+
"LICENSE.md"
|
|
24
32
|
],
|
|
33
|
+
"type": "module",
|
|
34
|
+
"sideEffects": false,
|
|
35
|
+
"main": "./dist/index.mjs",
|
|
36
|
+
"module": "./dist/index.mjs",
|
|
37
|
+
"types": "./dist/index.d.ts",
|
|
38
|
+
"exports": {
|
|
39
|
+
".": {
|
|
40
|
+
"types": "./dist/index.d.ts",
|
|
41
|
+
"import": "./dist/index.mjs"
|
|
42
|
+
},
|
|
43
|
+
"./package.json": "./package.json"
|
|
44
|
+
},
|
|
25
45
|
"publishConfig": {
|
|
26
46
|
"access": "public"
|
|
27
47
|
},
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
|
|
48
|
+
"engines": {
|
|
49
|
+
"node": "^22.15.0 || >=24.11.0"
|
|
50
|
+
}
|
|
31
51
|
}
|