@clov-std/elysia-ratelimit 3.0.0 → 3.1.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/dist/index.js +13 -3
- package/dist/rate-limit.d.ts +9 -14
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -13,6 +13,9 @@ var rateLimitResponseSchema = t.Object({
|
|
|
13
13
|
title: t.String({
|
|
14
14
|
description: "Short, human-readable summary of the error."
|
|
15
15
|
}),
|
|
16
|
+
status: t.Literal(429, {
|
|
17
|
+
description: "HTTP status code, repeated in the body per RFC 9457."
|
|
18
|
+
}),
|
|
16
19
|
detail: t.Optional(t.String({
|
|
17
20
|
description: "Explanation specific to this occurrence."
|
|
18
21
|
}))
|
|
@@ -22,7 +25,8 @@ class RateLimitException extends Exception {
|
|
|
22
25
|
status = 429;
|
|
23
26
|
response = {
|
|
24
27
|
type: RATE_LIMIT_ERROR_KEYS.RATE_LIMIT_EXCEEDED,
|
|
25
|
-
title: "Too Many Requests"
|
|
28
|
+
title: "Too Many Requests",
|
|
29
|
+
status: 429
|
|
26
30
|
};
|
|
27
31
|
constructor(cause) {
|
|
28
32
|
super("Too Many Requests", {
|
|
@@ -41,7 +45,11 @@ var extractClientIp = (request, server) => {
|
|
|
41
45
|
return request.headers.get("x-real-ip") ?? server?.requestIP(request)?.address ?? "127.0.0.1";
|
|
42
46
|
};
|
|
43
47
|
var rateLimitPlugin = (store = new MemoryStore) => new Elysia().macro({
|
|
44
|
-
rateLimit: ({
|
|
48
|
+
rateLimit: ({
|
|
49
|
+
limit,
|
|
50
|
+
window,
|
|
51
|
+
keyGenerator
|
|
52
|
+
}) => ({
|
|
45
53
|
response: { 429: rateLimitResponseSchema },
|
|
46
54
|
transform: async ({ set, request, server }) => {
|
|
47
55
|
const route = `${request.method}:${new URL(request.url).pathname}`;
|
|
@@ -56,8 +64,10 @@ var rateLimitPlugin = (store = new MemoryStore) => new Elysia().macro({
|
|
|
56
64
|
set.headers["X-RateLimit-Limit"] = limit.toString();
|
|
57
65
|
set.headers["X-RateLimit-Remaining"] = remaining.toString();
|
|
58
66
|
set.headers["X-RateLimit-Reset"] = reset.toString();
|
|
59
|
-
if (count > limit)
|
|
67
|
+
if (count > limit) {
|
|
68
|
+
set.headers["content-type"] = "application/problem+json";
|
|
60
69
|
throw new RateLimitException({ limit, window, reset });
|
|
70
|
+
}
|
|
61
71
|
}
|
|
62
72
|
})
|
|
63
73
|
});
|
package/dist/rate-limit.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { Exception } from '@clov-std/error';
|
|
|
2
2
|
import { type KvStore } from '@clov-std/kv-store';
|
|
3
3
|
import type { Server } from 'bun';
|
|
4
4
|
import { Elysia, type HTTPHeaders } from 'elysia';
|
|
5
|
+
import type { TLiteral, TObject, TOptional, TString } from 'typebox/type';
|
|
5
6
|
export declare const RATE_LIMIT_ERROR_KEYS: {
|
|
6
7
|
readonly RATE_LIMIT_EXCEEDED: 'elysia-ratelimit.exceeded';
|
|
7
8
|
};
|
|
@@ -15,25 +16,18 @@ export interface RateLimitExceeded {
|
|
|
15
16
|
readonly reset: number;
|
|
16
17
|
}
|
|
17
18
|
/**
|
|
18
|
-
* Shape of the `429` body.
|
|
19
|
-
*
|
|
20
|
-
* `type` is a plain string, not a literal: an application error handler is free to answer
|
|
21
|
-
* with its own key (a localized catalog entry, for instance). `detail` is optional for the
|
|
22
|
-
* same reason, the plugin never sets it.
|
|
19
|
+
* Shape of the `429` body, following RFC 9457.
|
|
23
20
|
*/
|
|
24
|
-
declare const rateLimitResponseSchema:
|
|
25
|
-
type:
|
|
26
|
-
title:
|
|
27
|
-
|
|
21
|
+
declare const rateLimitResponseSchema: TObject<{
|
|
22
|
+
type: TString;
|
|
23
|
+
title: TString;
|
|
24
|
+
status: TLiteral<429>;
|
|
25
|
+
detail: TOptional<TString>;
|
|
28
26
|
}>;
|
|
29
27
|
/**
|
|
30
28
|
* Thrown when a caller exceeds its allowance.
|
|
31
29
|
*
|
|
32
|
-
*
|
|
33
|
-
* installs no error handler still answers a real `429` with the documented body. They are
|
|
34
|
-
* only a default: any `.error()` handler registered by the application runs first and wins,
|
|
35
|
-
* which is what lets an API localize this error from its own catalog. Match on
|
|
36
|
-
* {@link Exception.key}, not on the message.
|
|
30
|
+
* @see {@link rateLimitResponseSchema}
|
|
37
31
|
*/
|
|
38
32
|
export declare class RateLimitException extends Exception<RateLimitExceeded> {
|
|
39
33
|
/** HTTP status used by Elysia's error fallback. */
|
|
@@ -42,6 +36,7 @@ export declare class RateLimitException extends Exception<RateLimitExceeded> {
|
|
|
42
36
|
readonly response: {
|
|
43
37
|
readonly type: string;
|
|
44
38
|
readonly title: string;
|
|
39
|
+
readonly status: 429;
|
|
45
40
|
};
|
|
46
41
|
/**
|
|
47
42
|
* Creates a new rate limit exception.
|