@nest-boot/graphql-rate-limit 7.0.4 → 7.2.0
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/graphql-rate-limit.module.d.ts +21 -2
- package/dist/graphql-rate-limit.module.js +41 -8
- package/dist/graphql-rate-limit.module.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/utils/load-config-from-env.util.d.ts +16 -0
- package/dist/utils/load-config-from-env.util.js +47 -0
- package/dist/utils/load-config-from-env.util.js.map +1 -0
- package/package.json +10 -11
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { type DynamicModule } from "@nestjs/common";
|
|
1
|
+
import { type DynamicModule, type OnApplicationShutdown } from "@nestjs/common";
|
|
2
|
+
import Redis from "ioredis";
|
|
2
3
|
import { ASYNC_OPTIONS_TYPE, ConfigurableModuleClass, OPTIONS_TYPE } from "./graphql-rate-limit.module-definition";
|
|
3
4
|
/**
|
|
4
5
|
* GraphQL rate limiting module using Redis-backed leaky bucket algorithm.
|
|
@@ -6,8 +7,18 @@ import { ASYNC_OPTIONS_TYPE, ConfigurableModuleClass, OPTIONS_TYPE } from "./gra
|
|
|
6
7
|
* @remarks
|
|
7
8
|
* Provides query complexity analysis and rate limiting for GraphQL operations.
|
|
8
9
|
* Uses Redis for distributed rate limit state and supports custom ID extraction.
|
|
10
|
+
*
|
|
11
|
+
* The module automatically loads Redis connection from environment variables if not provided:
|
|
12
|
+
* - `REDIS_URL`: Full Redis connection URL (e.g., `redis://user:pass@host:6379/0`)
|
|
13
|
+
* - `REDIS_HOST`: Redis server hostname
|
|
14
|
+
* - `REDIS_PORT`: Redis server port
|
|
15
|
+
* - `REDIS_DB` or `REDIS_DATABASE`: Redis database number
|
|
16
|
+
* - `REDIS_USER` or `REDIS_USERNAME`: Redis username
|
|
17
|
+
* - `REDIS_PASS` or `REDIS_PASSWORD`: Redis password
|
|
18
|
+
* - `REDIS_TLS`: Enable TLS connection
|
|
9
19
|
*/
|
|
10
|
-
export declare class GraphQLRateLimitModule extends ConfigurableModuleClass {
|
|
20
|
+
export declare class GraphQLRateLimitModule extends ConfigurableModuleClass implements OnApplicationShutdown {
|
|
21
|
+
private readonly redis;
|
|
11
22
|
/**
|
|
12
23
|
* Registers the GraphQLRateLimitModule with the given options.
|
|
13
24
|
* @param options - Configuration options including rate limit thresholds and Redis connection
|
|
@@ -20,4 +31,12 @@ export declare class GraphQLRateLimitModule extends ConfigurableModuleClass {
|
|
|
20
31
|
* @returns Dynamic module configuration
|
|
21
32
|
*/
|
|
22
33
|
static forRootAsync(options: typeof ASYNC_OPTIONS_TYPE): DynamicModule;
|
|
34
|
+
/** Creates a new GraphQLRateLimitModule instance.
|
|
35
|
+
* @param redis - The ioredis client instance
|
|
36
|
+
*/
|
|
37
|
+
constructor(redis: Redis);
|
|
38
|
+
/**
|
|
39
|
+
* Gracefully closes the Redis connection when the application shuts down.
|
|
40
|
+
*/
|
|
41
|
+
onApplicationShutdown(): Promise<void>;
|
|
23
42
|
}
|
|
@@ -5,19 +5,35 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
5
5
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
6
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
7
|
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
8
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
15
|
exports.GraphQLRateLimitModule = void 0;
|
|
10
|
-
const redis_1 = require("@nest-boot/redis");
|
|
11
16
|
const common_1 = require("@nestjs/common");
|
|
17
|
+
const ioredis_1 = __importDefault(require("ioredis"));
|
|
12
18
|
const graphql_rate_limit_module_definition_1 = require("./graphql-rate-limit.module-definition");
|
|
13
19
|
const graphql_rate_limit_plugin_1 = require("./graphql-rate-limit.plugin");
|
|
14
20
|
const graphql_rate_limit_storage_1 = require("./graphql-rate-limit.storage");
|
|
21
|
+
const load_config_from_env_util_1 = require("./utils/load-config-from-env.util");
|
|
15
22
|
/**
|
|
16
23
|
* GraphQL rate limiting module using Redis-backed leaky bucket algorithm.
|
|
17
24
|
*
|
|
18
25
|
* @remarks
|
|
19
26
|
* Provides query complexity analysis and rate limiting for GraphQL operations.
|
|
20
27
|
* Uses Redis for distributed rate limit state and supports custom ID extraction.
|
|
28
|
+
*
|
|
29
|
+
* The module automatically loads Redis connection from environment variables if not provided:
|
|
30
|
+
* - `REDIS_URL`: Full Redis connection URL (e.g., `redis://user:pass@host:6379/0`)
|
|
31
|
+
* - `REDIS_HOST`: Redis server hostname
|
|
32
|
+
* - `REDIS_PORT`: Redis server port
|
|
33
|
+
* - `REDIS_DB` or `REDIS_DATABASE`: Redis database number
|
|
34
|
+
* - `REDIS_USER` or `REDIS_USERNAME`: Redis username
|
|
35
|
+
* - `REDIS_PASS` or `REDIS_PASSWORD`: Redis password
|
|
36
|
+
* - `REDIS_TLS`: Enable TLS connection
|
|
21
37
|
*/
|
|
22
38
|
let GraphQLRateLimitModule = class GraphQLRateLimitModule extends graphql_rate_limit_module_definition_1.ConfigurableModuleClass {
|
|
23
39
|
/**
|
|
@@ -36,25 +52,41 @@ let GraphQLRateLimitModule = class GraphQLRateLimitModule extends graphql_rate_l
|
|
|
36
52
|
static forRootAsync(options) {
|
|
37
53
|
return super.forRootAsync(options);
|
|
38
54
|
}
|
|
55
|
+
/** Creates a new GraphQLRateLimitModule instance.
|
|
56
|
+
* @param redis - The ioredis client instance
|
|
57
|
+
*/
|
|
58
|
+
constructor(redis) {
|
|
59
|
+
super();
|
|
60
|
+
this.redis = redis;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Gracefully closes the Redis connection when the application shuts down.
|
|
64
|
+
*/
|
|
65
|
+
async onApplicationShutdown() {
|
|
66
|
+
await this.redis.quit();
|
|
67
|
+
}
|
|
39
68
|
};
|
|
40
69
|
exports.GraphQLRateLimitModule = GraphQLRateLimitModule;
|
|
41
70
|
exports.GraphQLRateLimitModule = GraphQLRateLimitModule = __decorate([
|
|
42
71
|
(0, common_1.Global)(),
|
|
43
72
|
(0, common_1.Module)({
|
|
44
|
-
imports: [
|
|
45
|
-
redis_1.RedisModule.registerAsync({
|
|
46
|
-
inject: [graphql_rate_limit_module_definition_1.OPTIONS_TOKEN],
|
|
47
|
-
useFactory: (options) => options.connection ?? {},
|
|
48
|
-
}),
|
|
49
|
-
],
|
|
50
73
|
providers: [
|
|
51
74
|
graphql_rate_limit_plugin_1.GraphQLRateLimitPlugin,
|
|
52
75
|
graphql_rate_limit_storage_1.GraphQLRateLimitStorage,
|
|
76
|
+
{
|
|
77
|
+
provide: ioredis_1.default,
|
|
78
|
+
inject: [graphql_rate_limit_module_definition_1.OPTIONS_TOKEN],
|
|
79
|
+
useFactory: (options) => new ioredis_1.default({
|
|
80
|
+
...(0, load_config_from_env_util_1.loadConfigFromEnv)(),
|
|
81
|
+
...options.connection,
|
|
82
|
+
}),
|
|
83
|
+
},
|
|
53
84
|
{
|
|
54
85
|
provide: graphql_rate_limit_module_definition_1.OPTIONS_TOKEN,
|
|
55
86
|
inject: [{ token: graphql_rate_limit_module_definition_1.MODULE_OPTIONS_TOKEN, optional: true }],
|
|
56
87
|
useFactory: (options) => {
|
|
57
88
|
return {
|
|
89
|
+
connection: options?.connection,
|
|
58
90
|
maxComplexity: options?.maxComplexity ?? 1000,
|
|
59
91
|
defaultComplexity: options?.defaultComplexity ?? 0,
|
|
60
92
|
keyPrefix: options?.keyPrefix ?? "graphql-rate-limit",
|
|
@@ -74,6 +106,7 @@ exports.GraphQLRateLimitModule = GraphQLRateLimitModule = __decorate([
|
|
|
74
106
|
},
|
|
75
107
|
],
|
|
76
108
|
exports: [graphql_rate_limit_module_definition_1.OPTIONS_TOKEN],
|
|
77
|
-
})
|
|
109
|
+
}),
|
|
110
|
+
__metadata("design:paramtypes", [ioredis_1.default])
|
|
78
111
|
], GraphQLRateLimitModule);
|
|
79
112
|
//# sourceMappingURL=graphql-rate-limit.module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graphql-rate-limit.module.js","sourceRoot":"","sources":["../src/graphql-rate-limit.module.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"graphql-rate-limit.module.js","sourceRoot":"","sources":["../src/graphql-rate-limit.module.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,2CAKwB;AAExB,sDAA4B;AAE5B,iGAMgD;AAChD,2EAAqE;AACrE,6EAAuE;AAKvE,iFAAsE;AAEtE;;;;;;;;;;;;;;;GAeG;AAgDI,IAAM,sBAAsB,GAA5B,MAAM,sBACX,SAAQ,8DAAuB;IAG/B;;;;OAIG;IACH,MAAM,CAAU,OAAO,CAAC,OAA4B;QAClD,OAAO,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAChC,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAU,YAAY,CAC1B,OAAkC;QAElC,OAAO,KAAK,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC;IACrC,CAAC;IAED;;OAEG;IACH,YAA6B,KAAY;QACvC,KAAK,EAAE,CAAC;QADmB,UAAK,GAAL,KAAK,CAAO;IAEzC,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,qBAAqB;QACzB,MAAM,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;IAC1B,CAAC;CACF,CAAA;AArCY,wDAAsB;iCAAtB,sBAAsB;IA/ClC,IAAA,eAAM,GAAE;IACR,IAAA,eAAM,EAAC;QACN,SAAS,EAAE;YACT,kDAAsB;YACtB,oDAAuB;YACvB;gBACE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