@mrxsys/mrx-core 2.11.0-1-and-267-20250925 → 2.11.1-1-and-275-20251023

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.
Files changed (76) hide show
  1. package/dist/{chunk-fp8xg6kq.js → chunk-441xs5k1.js} +1 -1
  2. package/dist/{chunk-bdcsh435.js → chunk-4v86f7gp.js} +5 -5
  3. package/dist/{chunk-00s6t4tw.js → chunk-7t524zqh.js} +4 -4
  4. package/dist/{chunk-d1vp74n0.js → chunk-afd82epa.js} +2 -2
  5. package/dist/{chunk-v9kvw5x7.js → chunk-afyz8rg5.js} +2 -2
  6. package/dist/{chunk-6sfnrhfs.js → chunk-pjv1ekwr.js} +33 -1
  7. package/dist/chunk-rz5p4j3p.js +13 -0
  8. package/dist/{chunk-h9t48r9q.js → chunk-sqts8vyk.js} +16 -2
  9. package/dist/errors/enums/http-status-codes.d.ts +15 -1
  10. package/dist/errors/enums/index.d.ts +1 -1
  11. package/dist/errors/enums/index.js +3 -3
  12. package/dist/errors/http-error.d.ts +3 -3
  13. package/dist/errors/index.js +2 -2
  14. package/dist/errors/utils/index.js +2 -2
  15. package/dist/modules/database/enums/database-error-keys.d.ts +2 -2
  16. package/dist/modules/database/enums/index.js +1 -1
  17. package/dist/modules/database/index.js +5 -5
  18. package/dist/modules/elysia/cache/cache.d.ts +8 -14
  19. package/dist/modules/elysia/cache/index.js +66 -46
  20. package/dist/modules/elysia/cache/types/cache-item.d.ts +0 -1
  21. package/dist/modules/elysia/cache/types/cache-options.d.ts +2 -11
  22. package/dist/modules/elysia/crud/index.js +16 -16
  23. package/dist/modules/elysia/crud/operations/count.d.ts +1 -1
  24. package/dist/modules/elysia/crud/operations/delete.d.ts +1 -1
  25. package/dist/modules/elysia/crud/operations/deleteOne.d.ts +1 -1
  26. package/dist/modules/elysia/crud/operations/find.d.ts +1 -1
  27. package/dist/modules/elysia/crud/operations/findOne.d.ts +1 -1
  28. package/dist/modules/elysia/crud/operations/index.js +7 -7
  29. package/dist/modules/elysia/crud/operations/insert.d.ts +1 -1
  30. package/dist/modules/elysia/crud/operations/update.d.ts +1 -1
  31. package/dist/modules/elysia/crud/operations/updateOne.d.ts +1 -1
  32. package/dist/modules/elysia/db-resolver/db-resolver.d.ts +1 -1
  33. package/dist/modules/elysia/db-resolver/index.js +6 -6
  34. package/dist/modules/elysia/error/index.js +2 -2
  35. package/dist/modules/elysia/microservice/microservice.d.ts +12 -12
  36. package/dist/modules/elysia/rate-limit/index.js +53 -28
  37. package/dist/modules/elysia/rate-limit/rate-limit.d.ts +361 -32
  38. package/dist/modules/elysia/rate-limit/types/rate-limit-options.d.ts +0 -9
  39. package/dist/modules/jwt/index.js +2 -2
  40. package/dist/modules/kv-store/bun-redis/bun-redis-store.d.ts +25 -0
  41. package/dist/modules/kv-store/bun-redis/index.d.ts +1 -0
  42. package/dist/modules/kv-store/bun-redis/index.js +76 -0
  43. package/dist/modules/kv-store/ioredis/index.js +4 -10
  44. package/dist/modules/kv-store/ioredis/ioredis-store.d.ts +0 -82
  45. package/dist/modules/kv-store/memory/index.js +98 -4
  46. package/dist/modules/kv-store/memory/memory-store.d.ts +0 -84
  47. package/dist/modules/kv-store/types/kv-store.d.ts +2 -2
  48. package/dist/modules/logger/enums/index.js +1 -1
  49. package/dist/modules/logger/enums/logger-error-keys.d.ts +6 -3
  50. package/dist/modules/logger/events/logger-events.d.ts +12 -3
  51. package/dist/modules/logger/index.js +173 -81
  52. package/dist/modules/logger/logger.d.ts +115 -133
  53. package/dist/modules/logger/sinks/console-logger.d.ts +2 -16
  54. package/dist/modules/logger/sinks/devnull-logger.d.ts +8 -0
  55. package/dist/modules/logger/sinks/file-logger/enums/file-logger-error-keys.d.ts +3 -0
  56. package/dist/modules/logger/sinks/file-logger/file-logger.d.ts +25 -0
  57. package/dist/modules/logger/sinks/index.d.ts +4 -2
  58. package/dist/modules/logger/sinks/index.js +54 -31
  59. package/dist/modules/logger/types/index.d.ts +6 -6
  60. package/dist/modules/logger/types/log-levels.d.ts +1 -4
  61. package/dist/modules/logger/types/logger-options.d.ts +41 -0
  62. package/dist/modules/logger/types/logger-sink.d.ts +12 -3
  63. package/dist/modules/logger/types/sink-bodies-intersection.d.ts +2 -0
  64. package/dist/modules/logger/types/sink-body.d.ts +1 -1
  65. package/dist/modules/logger/types/sink-map.d.ts +1 -1
  66. package/dist/modules/repository/index.js +4 -4
  67. package/dist/modules/repository/types/query-options.d.ts +2 -2
  68. package/package.json +30 -25
  69. package/dist/chunk-b23dvm2d.js +0 -29
  70. package/dist/chunk-b96fm9ph.js +0 -10
  71. package/dist/chunk-e30paw8a.js +0 -101
  72. package/dist/modules/elysia/cache/utils/index.d.ts +0 -1
  73. package/dist/modules/elysia/cache/utils/index.js +0 -7
  74. package/dist/modules/logger/sinks/file-logger.d.ts +0 -25
  75. package/dist/modules/logger/types/bodies-intersection.d.ts +0 -2
  76. package/dist/modules/logger/types/log-stream-chunk.d.ts +0 -15
@@ -43,7 +43,7 @@ export declare const deleteOne: <const TDatabase extends Omit<MSSQLDatabaseOptio
43
43
  route: string;
44
44
  request: Request;
45
45
  store: {};
46
- status: <const Code extends number | keyof import("elysia").StatusMap, const T = Code extends 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 421 | 422 | 423 | 424 | 425 | 426 | 428 | 429 | 431 | 451 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511 | 100 | 102 | 207 | 208 | 101 | 103 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 300 | 301 | 302 | 303 | 304 | 307 | 308 | 420 ? {
46
+ status: <const Code extends number | keyof import("elysia").StatusMap, const T = Code extends 100 | 101 | 102 | 103 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 421 | 422 | 423 | 424 | 425 | 426 | 428 | 429 | 431 | 451 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511 | 300 | 301 | 302 | 303 | 304 | 307 | 308 | 420 ? {
47
47
  readonly 100: "Continue";
48
48
  readonly 101: "Switching Protocols";
49
49
  readonly 102: "Processing";
@@ -43,7 +43,7 @@ export declare const find: <const TDatabase extends Omit<MSSQLDatabaseOptions, "
43
43
  route: string;
44
44
  request: Request;
45
45
  store: {};
46
- status: <const Code extends number | keyof import("elysia").StatusMap, const T = Code extends 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 421 | 422 | 423 | 424 | 425 | 426 | 428 | 429 | 431 | 451 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511 | 100 | 102 | 207 | 208 | 101 | 103 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 300 | 301 | 302 | 303 | 304 | 307 | 308 | 420 ? {
46
+ status: <const Code extends number | keyof import("elysia").StatusMap, const T = Code extends 100 | 101 | 102 | 103 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 421 | 422 | 423 | 424 | 425 | 426 | 428 | 429 | 431 | 451 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511 | 300 | 301 | 302 | 303 | 304 | 307 | 308 | 420 ? {
47
47
  readonly 100: "Continue";
48
48
  readonly 101: "Switching Protocols";
49
49
  readonly 102: "Processing";
@@ -43,7 +43,7 @@ export declare const findOne: <const TDatabase extends Omit<MSSQLDatabaseOptions
43
43
  route: string;
44
44
  request: Request;
45
45
  store: {};
46
- status: <const Code extends number | keyof import("elysia").StatusMap, const T = Code extends 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 421 | 422 | 423 | 424 | 425 | 426 | 428 | 429 | 431 | 451 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511 | 100 | 102 | 207 | 208 | 101 | 103 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 300 | 301 | 302 | 303 | 304 | 307 | 308 | 420 ? {
46
+ status: <const Code extends number | keyof import("elysia").StatusMap, const T = Code extends 100 | 101 | 102 | 103 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 421 | 422 | 423 | 424 | 425 | 426 | 428 | 429 | 431 | 451 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511 | 300 | 301 | 302 | 303 | 304 | 307 | 308 | 420 ? {
47
47
  readonly 100: "Continue";
48
48
  readonly 101: "Switching Protocols";
49
49
  readonly 102: "Processing";
@@ -8,19 +8,19 @@ import {
8
8
  insert,
9
9
  update,
10
10
  updateOne
11
- } from "../../../../chunk-6sfnrhfs.js";
12
- import"../../../../chunk-fp8xg6kq.js";
11
+ } from "../../../../chunk-pjv1ekwr.js";
12
+ import"../../../../chunk-441xs5k1.js";
13
13
  import"../../../../chunk-fs3wm3p4.js";
14
14
  import"../../../../chunk-z0ct35ft.js";
15
15
  import"../../../../chunk-8tffnbpn.js";
16
16
  import"../../../../chunk-w7h898m3.js";
17
- import"../../../../chunk-d1vp74n0.js";
18
- import"../../../../chunk-v9kvw5x7.js";
17
+ import"../../../../chunk-afd82epa.js";
18
+ import"../../../../chunk-afyz8rg5.js";
19
19
  import"../../../../chunk-grfyngq0.js";
20
20
  import"../../../../chunk-mvrxngm7.js";
21
- import"../../../../chunk-bdcsh435.js";
22
- import"../../../../chunk-00s6t4tw.js";
23
- import"../../../../chunk-h9t48r9q.js";
21
+ import"../../../../chunk-4v86f7gp.js";
22
+ import"../../../../chunk-7t524zqh.js";
23
+ import"../../../../chunk-sqts8vyk.js";
24
24
  import"../../../../chunk-9cgzhc50.js";
25
25
  export {
26
26
  updateOne,
@@ -43,7 +43,7 @@ export declare const insert: <const TDatabase extends Omit<MSSQLDatabaseOptions,
43
43
  route: string;
44
44
  request: Request;
45
45
  store: {};
46
- status: <const Code extends number | keyof import("elysia").StatusMap, const T = Code extends 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 421 | 422 | 423 | 424 | 425 | 426 | 428 | 429 | 431 | 451 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511 | 100 | 102 | 207 | 208 | 101 | 103 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 300 | 301 | 302 | 303 | 304 | 307 | 308 | 420 ? {
46
+ status: <const Code extends number | keyof import("elysia").StatusMap, const T = Code extends 100 | 101 | 102 | 103 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 421 | 422 | 423 | 424 | 425 | 426 | 428 | 429 | 431 | 451 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511 | 300 | 301 | 302 | 303 | 304 | 307 | 308 | 420 ? {
47
47
  readonly 100: "Continue";
48
48
  readonly 101: "Switching Protocols";
49
49
  readonly 102: "Processing";
@@ -43,7 +43,7 @@ export declare const update: <const TDatabase extends Omit<MSSQLDatabaseOptions,
43
43
  route: string;
44
44
  request: Request;
45
45
  store: {};
46
- status: <const Code extends number | keyof import("elysia").StatusMap, const T = Code extends 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 421 | 422 | 423 | 424 | 425 | 426 | 428 | 429 | 431 | 451 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511 | 100 | 102 | 207 | 208 | 101 | 103 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 300 | 301 | 302 | 303 | 304 | 307 | 308 | 420 ? {
46
+ status: <const Code extends number | keyof import("elysia").StatusMap, const T = Code extends 100 | 101 | 102 | 103 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 421 | 422 | 423 | 424 | 425 | 426 | 428 | 429 | 431 | 451 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511 | 300 | 301 | 302 | 303 | 304 | 307 | 308 | 420 ? {
47
47
  readonly 100: "Continue";
48
48
  readonly 101: "Switching Protocols";
49
49
  readonly 102: "Processing";
@@ -43,7 +43,7 @@ export declare const updateOne: <const TDatabase extends Omit<MSSQLDatabaseOptio
43
43
  route: string;
44
44
  request: Request;
45
45
  store: {};
46
- status: <const Code extends number | keyof import("elysia").StatusMap, const T = Code extends 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 421 | 422 | 423 | 424 | 425 | 426 | 428 | 429 | 431 | 451 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511 | 100 | 102 | 207 | 208 | 101 | 103 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 300 | 301 | 302 | 303 | 304 | 307 | 308 | 420 ? {
46
+ status: <const Code extends number | keyof import("elysia").StatusMap, const T = Code extends 100 | 101 | 102 | 103 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 421 | 422 | 423 | 424 | 425 | 426 | 428 | 429 | 431 | 451 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511 | 300 | 301 | 302 | 303 | 304 | 307 | 308 | 420 ? {
47
47
  readonly 100: "Continue";
48
48
  readonly 101: "Switching Protocols";
49
49
  readonly 102: "Processing";
@@ -41,7 +41,7 @@ export declare const dbResolver: (prefixDatabaseName?: string) => Elysia<"", {
41
41
  route: string;
42
42
  request: Request;
43
43
  store: {};
44
- status: <const Code extends number | keyof import("elysia").StatusMap, const T = Code extends 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 421 | 422 | 423 | 424 | 425 | 426 | 428 | 429 | 431 | 451 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511 | 100 | 102 | 207 | 208 | 101 | 103 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 300 | 301 | 302 | 303 | 304 | 307 | 308 | 420 ? {
44
+ status: <const Code extends number | keyof import("elysia").StatusMap, const T = Code extends 100 | 101 | 102 | 103 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 421 | 422 | 423 | 424 | 425 | 426 | 428 | 429 | 431 | 451 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511 | 300 | 301 | 302 | 303 | 304 | 307 | 308 | 420 ? {
45
45
  readonly 100: "Continue";
46
46
  readonly 101: "Switching Protocols";
47
47
  readonly 102: "Processing";
@@ -1,17 +1,17 @@
1
1
  // @bun
2
2
  import {
3
3
  dbResolver
4
- } from "../../../chunk-fp8xg6kq.js";
4
+ } from "../../../chunk-441xs5k1.js";
5
5
  import"../../../chunk-fs3wm3p4.js";
6
6
  import"../../../chunk-z0ct35ft.js";
7
7
  import"../../../chunk-8tffnbpn.js";
8
- import"../../../chunk-d1vp74n0.js";
9
- import"../../../chunk-v9kvw5x7.js";
8
+ import"../../../chunk-afd82epa.js";
9
+ import"../../../chunk-afyz8rg5.js";
10
10
  import"../../../chunk-grfyngq0.js";
11
11
  import"../../../chunk-mvrxngm7.js";
12
- import"../../../chunk-bdcsh435.js";
13
- import"../../../chunk-00s6t4tw.js";
14
- import"../../../chunk-h9t48r9q.js";
12
+ import"../../../chunk-4v86f7gp.js";
13
+ import"../../../chunk-7t524zqh.js";
14
+ import"../../../chunk-sqts8vyk.js";
15
15
  import"../../../chunk-9cgzhc50.js";
16
16
  export {
17
17
  dbResolver
@@ -8,8 +8,8 @@ import {
8
8
  import"../../../chunk-jksb9ers.js";
9
9
  import {
10
10
  HttpError
11
- } from "../../../chunk-00s6t4tw.js";
12
- import"../../../chunk-h9t48r9q.js";
11
+ } from "../../../chunk-7t524zqh.js";
12
+ import"../../../chunk-sqts8vyk.js";
13
13
  import {
14
14
  BaseError
15
15
  } from "../../../chunk-9cgzhc50.js";
@@ -43,6 +43,9 @@ export declare const microservice: Elysia<"/microservice", {
43
43
  query: unknown;
44
44
  headers: unknown;
45
45
  response: {
46
+ 200: {
47
+ message: string;
48
+ };
46
49
  422: {
47
50
  type: "validation";
48
51
  on: string;
@@ -52,9 +55,6 @@ export declare const microservice: Elysia<"/microservice", {
52
55
  property?: string;
53
56
  expected?: string;
54
57
  };
55
- 200: {
56
- message: string;
57
- };
58
58
  };
59
59
  };
60
60
  };
@@ -68,15 +68,6 @@ export declare const microservice: Elysia<"/microservice", {
68
68
  query: unknown;
69
69
  headers: unknown;
70
70
  response: {
71
- 422: {
72
- type: "validation";
73
- on: string;
74
- summary?: string;
75
- message?: string;
76
- found?: unknown;
77
- property?: string;
78
- expected?: string;
79
- };
80
71
  200: {
81
72
  message: string;
82
73
  content: {
@@ -86,6 +77,15 @@ export declare const microservice: Elysia<"/microservice", {
86
77
  author: string;
87
78
  };
88
79
  };
80
+ 422: {
81
+ type: "validation";
82
+ on: string;
83
+ summary?: string;
84
+ message?: string;
85
+ found?: unknown;
86
+ property?: string;
87
+ expected?: string;
88
+ };
89
89
  };
90
90
  };
91
91
  };
@@ -4,50 +4,75 @@ import {
4
4
  } from "../../../chunk-dre2fgj0.js";
5
5
  import {
6
6
  MemoryStore
7
- } from "../../../chunk-e30paw8a.js";
7
+ } from "../../kv-store/memory/index.js";
8
8
  import"../../../chunk-xhhj1gvj.js";
9
9
  import {
10
10
  HttpError
11
- } from "../../../chunk-00s6t4tw.js";
12
- import"../../../chunk-h9t48r9q.js";
11
+ } from "../../../chunk-7t524zqh.js";
12
+ import"../../../chunk-sqts8vyk.js";
13
13
  import"../../../chunk-9cgzhc50.js";
14
14
 
15
15
  // source/modules/elysia/rate-limit/rate-limit.ts
16
16
  import { Elysia } from "elysia";
17
- var rateLimit = ({ store, limit, window }) => {
18
- const storeInstance = store === ":memory:" || !store ? new MemoryStore : store;
19
- return new Elysia({
20
- name: "rateLimit",
21
- seed: {
22
- store,
23
- limit,
24
- window
25
- }
26
- }).onRequest(async ({ set, request, server }) => {
27
- const ip = request.headers.get("x-forwarded-for") || request.headers.get("x-real-ip") || server?.requestIP(request)?.address || "127.0.0.1";
28
- const key = `ratelimit:${ip}`;
29
- const count = await storeInstance.get(key);
30
- let newCount;
31
- if (count === null) {
32
- await storeInstance.set(key, 1, window);
33
- newCount = 1;
17
+ var rateLimit = (store = new MemoryStore) => {
18
+ const restrictedRoutes = new Map;
19
+ const rateLimitCheck = async (key, limit, window, set) => {
20
+ if (set.headers["X-RateLimit-Limit"])
21
+ return;
22
+ let count = await store.get(key) ?? 0;
23
+ if (count === 0) {
24
+ await store.set(key, 1, window);
25
+ count = 1;
34
26
  } else {
35
- newCount = await storeInstance.increment(key);
27
+ count = await store.increment(key);
36
28
  }
37
- if (newCount > limit) {
29
+ const remaining = Math.max(0, limit - count);
30
+ const resetTime = await store.ttl(key);
31
+ set.headers = {
32
+ "X-RateLimit-Limit": limit.toString(),
33
+ "X-RateLimit-Remaining": remaining.toString(),
34
+ "X-RateLimit-Reset": resetTime.toString()
35
+ };
36
+ if (count > limit) {
38
37
  set.status = 429;
39
38
  throw new HttpError(RATE_LIMIT_ERROR_KEYS.RATE_LIMIT_EXCEEDED, "TOO_MANY_REQUESTS", {
40
39
  limit,
41
40
  window,
42
41
  remaining: 0,
43
- reset: await storeInstance.ttl(key)
42
+ reset: resetTime
44
43
  });
45
44
  }
46
- set.headers = {
47
- "X-RateLimit-Limit": limit.toString(),
48
- "X-RateLimit-Remaining": Math.max(0, limit - newCount).toString(),
49
- "X-RateLimit-Reset": (await storeInstance.ttl(key)).toString()
50
- };
45
+ };
46
+ return new Elysia({
47
+ name: "rateLimit",
48
+ seed: {
49
+ store
50
+ }
51
+ }).macro({
52
+ rateLimit: ({ limit, window }) => ({
53
+ transform: ({ request }) => {
54
+ const route = `${request.method}:${new URL(request.url).pathname}`;
55
+ if (!restrictedRoutes.has(route)) {
56
+ restrictedRoutes.set(route, { limit, window });
57
+ } else if (restrictedRoutes.has(route)) {
58
+ const existing = restrictedRoutes.get(route);
59
+ if (limit != existing.limit || window != existing.window)
60
+ restrictedRoutes.set(route, {
61
+ limit,
62
+ window
63
+ });
64
+ }
65
+ },
66
+ beforeHandle: async ({ set, request, server }) => {
67
+ const route = `${request.method}:${new URL(request.url).pathname}`;
68
+ if (restrictedRoutes.has(route)) {
69
+ const { limit: limit2, window: window2 } = restrictedRoutes.get(route);
70
+ const ip = request.headers.get("x-forwarded-for") || request.headers.get("x-real-ip") || server?.requestIP(request)?.address || "127.0.0.1";
71
+ const key = `ratelimit:${route}:${ip}`;
72
+ await rateLimitCheck(key, limit2, window2, set);
73
+ }
74
+ }
75
+ })
51
76
  }).as("global");
52
77
  };
53
78
  export {