@nest-boot/graphql-rate-limit 7.0.0-beta.4

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 (42) hide show
  1. package/LICENSE +21 -0
  2. package/dist/complexities/connection.complexity.d.ts +2 -0
  3. package/dist/complexities/connection.complexity.js +9 -0
  4. package/dist/complexities/connection.complexity.js.map +1 -0
  5. package/dist/complexities/index.d.ts +1 -0
  6. package/dist/complexities/index.js +18 -0
  7. package/dist/complexities/index.js.map +1 -0
  8. package/dist/decorators/complexity.decorator.d.ts +5 -0
  9. package/dist/decorators/complexity.decorator.js +8 -0
  10. package/dist/decorators/complexity.decorator.js.map +1 -0
  11. package/dist/decorators/index.d.ts +1 -0
  12. package/dist/decorators/index.js +18 -0
  13. package/dist/decorators/index.js.map +1 -0
  14. package/dist/graphql-rate-limit.module-definition.d.ts +3 -0
  15. package/dist/graphql-rate-limit.module-definition.js +11 -0
  16. package/dist/graphql-rate-limit.module-definition.js.map +1 -0
  17. package/dist/graphql-rate-limit.module.d.ts +3 -0
  18. package/dist/graphql-rate-limit.module.js +56 -0
  19. package/dist/graphql-rate-limit.module.js.map +1 -0
  20. package/dist/graphql-rate-limit.plugin.d.ts +13 -0
  21. package/dist/graphql-rate-limit.plugin.js +144 -0
  22. package/dist/graphql-rate-limit.plugin.js.map +1 -0
  23. package/dist/graphql-rate-limit.storage.d.ts +10 -0
  24. package/dist/graphql-rate-limit.storage.js +103 -0
  25. package/dist/graphql-rate-limit.storage.js.map +1 -0
  26. package/dist/index.d.ts +5 -0
  27. package/dist/index.js +24 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/interfaces/cost-response.interface.d.ts +11 -0
  30. package/dist/interfaces/cost-response.interface.js +3 -0
  31. package/dist/interfaces/cost-response.interface.js.map +1 -0
  32. package/dist/interfaces/graphql-rate-limit-module-options.interface.d.ts +2 -0
  33. package/dist/interfaces/graphql-rate-limit-module-options.interface.js +3 -0
  34. package/dist/interfaces/graphql-rate-limit-module-options.interface.js.map +1 -0
  35. package/dist/interfaces/graphql-rate-limit-options.interface.d.ts +11 -0
  36. package/dist/interfaces/graphql-rate-limit-options.interface.js +3 -0
  37. package/dist/interfaces/graphql-rate-limit-options.interface.js.map +1 -0
  38. package/dist/interfaces/index.d.ts +3 -0
  39. package/dist/interfaces/index.js +20 -0
  40. package/dist/interfaces/index.js.map +1 -0
  41. package/dist/tsconfig.build.tsbuildinfo +1 -0
  42. package/package.json +77 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 nest-boot
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,2 @@
1
+ import { type Complexity } from "@nestjs/graphql";
2
+ export declare const connectionComplexity: Complexity;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.connectionComplexity = void 0;
4
+ const connectionComplexity = (options) => {
5
+ return (Number(options.args.first ?? options.args.last ?? 1) *
6
+ options.childComplexity);
7
+ };
8
+ exports.connectionComplexity = connectionComplexity;
9
+ //# sourceMappingURL=connection.complexity.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connection.complexity.js","sourceRoot":"","sources":["../../src/complexities/connection.complexity.ts"],"names":[],"mappings":";;;AAEO,MAAM,oBAAoB,GAAe,CAC9C,OAAgC,EAChC,EAAE;IACF,OAAO,CACL,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,CAAC;QACpD,OAAO,CAAC,eAAe,CACxB,CAAC;AACJ,CAAC,CAAC;AAPW,QAAA,oBAAoB,wBAO/B"}
@@ -0,0 +1 @@
1
+ export * from "./connection.complexity";
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./connection.complexity"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/complexities/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0DAAwC"}
@@ -0,0 +1,5 @@
1
+ export interface ComplexityOptions {
2
+ value?: number;
3
+ multipliers?: string[];
4
+ }
5
+ export declare function Complexity(options: ComplexityOptions): MethodDecorator & PropertyDecorator & ClassDecorator;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Complexity = Complexity;
4
+ const graphql_1 = require("@nestjs/graphql");
5
+ function Complexity(options) {
6
+ return (0, graphql_1.Directive)(`@complexity(value: ${String(options.value ?? 1)}, multipliers: ${JSON.stringify(options.multipliers ?? [])})`);
7
+ }
8
+ //# sourceMappingURL=complexity.decorator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"complexity.decorator.js","sourceRoot":"","sources":["../../src/decorators/complexity.decorator.ts"],"names":[],"mappings":";;AAOA,gCAQC;AAfD,6CAA4C;AAO5C,SAAgB,UAAU,CACxB,OAA0B;IAE1B,OAAO,IAAA,mBAAS,EACd,sBAAsB,MAAM,CAAC,OAAO,CAAC,KAAK,IAAI,CAAC,CAAC,kBAAkB,IAAI,CAAC,SAAS,CAC9E,OAAO,CAAC,WAAW,IAAI,EAAE,CAC1B,GAAG,CACL,CAAC;AACJ,CAAC"}
@@ -0,0 +1 @@
1
+ export * from "./complexity.decorator";
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./complexity.decorator"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/decorators/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yDAAuC"}
@@ -0,0 +1,3 @@
1
+ export declare const REDIS_COMMAND = "GRAPHQL_RATE_LIMIT";
2
+ export declare const OPTIONS_TOKEN: unique symbol;
3
+ export declare const ConfigurableModuleClass: import("@nestjs/common").ConfigurableModuleCls<Partial<import("./interfaces").GraphQLRateLimitOptions>, "forRoot", "create", {}>, MODULE_OPTIONS_TOKEN: string | symbol, OPTIONS_TYPE: Partial<import("./interfaces").GraphQLRateLimitOptions> & Partial<{}>, ASYNC_OPTIONS_TYPE: import("@nestjs/common").ConfigurableModuleAsyncOptions<Partial<import("./interfaces").GraphQLRateLimitOptions>, "create"> & Partial<{}>;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ var _a;
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.ASYNC_OPTIONS_TYPE = exports.OPTIONS_TYPE = exports.MODULE_OPTIONS_TOKEN = exports.ConfigurableModuleClass = exports.OPTIONS_TOKEN = exports.REDIS_COMMAND = void 0;
5
+ const common_1 = require("@nestjs/common");
6
+ exports.REDIS_COMMAND = "GRAPHQL_RATE_LIMIT";
7
+ exports.OPTIONS_TOKEN = Symbol("GraphQLRateLimitOptions");
8
+ _a = new common_1.ConfigurableModuleBuilder()
9
+ .setClassMethodName("forRoot")
10
+ .build(), exports.ConfigurableModuleClass = _a.ConfigurableModuleClass, exports.MODULE_OPTIONS_TOKEN = _a.MODULE_OPTIONS_TOKEN, exports.OPTIONS_TYPE = _a.OPTIONS_TYPE, exports.ASYNC_OPTIONS_TYPE = _a.ASYNC_OPTIONS_TYPE;
11
+ //# sourceMappingURL=graphql-rate-limit.module-definition.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graphql-rate-limit.module-definition.js","sourceRoot":"","sources":["../src/graphql-rate-limit.module-definition.ts"],"names":[],"mappings":";;;;AAAA,2CAA2D;AAI9C,QAAA,aAAa,GAAG,oBAAoB,CAAC;AAErC,QAAA,aAAa,GAAG,MAAM,CAAC,yBAAyB,CAAC,CAAC;AAElD,KAKT,IAAI,kCAAyB,EAAiC;KAC/D,kBAAkB,CAAC,SAAS,CAAC;KAC7B,KAAK,EAAE,EANR,+BAAuB,+BACvB,4BAAoB,4BACpB,oBAAY,oBACZ,0BAAkB,yBAGT"}
@@ -0,0 +1,3 @@
1
+ import { ConfigurableModuleClass } from "./graphql-rate-limit.module-definition";
2
+ export declare class GraphQLRateLimitModule extends ConfigurableModuleClass {
3
+ }
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
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
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.GraphQLRateLimitModule = void 0;
10
+ const redis_1 = require("@nest-boot/redis");
11
+ const common_1 = require("@nestjs/common");
12
+ const graphql_rate_limit_module_definition_1 = require("./graphql-rate-limit.module-definition");
13
+ const graphql_rate_limit_plugin_1 = require("./graphql-rate-limit.plugin");
14
+ const graphql_rate_limit_storage_1 = require("./graphql-rate-limit.storage");
15
+ let GraphQLRateLimitModule = class GraphQLRateLimitModule extends graphql_rate_limit_module_definition_1.ConfigurableModuleClass {
16
+ };
17
+ exports.GraphQLRateLimitModule = GraphQLRateLimitModule;
18
+ exports.GraphQLRateLimitModule = GraphQLRateLimitModule = __decorate([
19
+ (0, common_1.Global)(),
20
+ (0, common_1.Module)({
21
+ imports: [
22
+ redis_1.RedisModule.registerAsync({
23
+ inject: [graphql_rate_limit_module_definition_1.OPTIONS_TOKEN],
24
+ useFactory: (options) => options.connection ?? {},
25
+ }),
26
+ ],
27
+ providers: [
28
+ graphql_rate_limit_plugin_1.GraphQLRateLimitPlugin,
29
+ graphql_rate_limit_storage_1.GraphQLRateLimitStorage,
30
+ {
31
+ provide: graphql_rate_limit_module_definition_1.OPTIONS_TOKEN,
32
+ inject: [{ token: graphql_rate_limit_module_definition_1.MODULE_OPTIONS_TOKEN, optional: true }],
33
+ useFactory: (options = {}) => {
34
+ return {
35
+ maxComplexity: options.maxComplexity ?? 1000,
36
+ defaultComplexity: options.defaultComplexity ?? 0,
37
+ keyPrefix: options.keyPrefix ?? "graphql-rate-limit",
38
+ restoreRate: options.restoreRate ?? 50,
39
+ maximumAvailable: options.maximumAvailable ?? 1000,
40
+ getId: options.getId ??
41
+ ((args) => {
42
+ const req = args.contextValue.req;
43
+ const ip = req.ips.length ? req.ips[0] : req.ip;
44
+ if (typeof ip === "undefined") {
45
+ throw new Error("Unable to determine client IP address for rate limiting. Please ensure the Express 'trust proxy' setting is configured correctly or provide a custom 'getId' function in the module options.");
46
+ }
47
+ return ip;
48
+ }),
49
+ };
50
+ },
51
+ },
52
+ ],
53
+ exports: [graphql_rate_limit_module_definition_1.OPTIONS_TOKEN],
54
+ })
55
+ ], GraphQLRateLimitModule);
56
+ //# sourceMappingURL=graphql-rate-limit.module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graphql-rate-limit.module.js","sourceRoot":"","sources":["../src/graphql-rate-limit.module.ts"],"names":[],"mappings":";;;;;;;;;AACA,4CAA+C;AAC/C,2CAAgD;AAGhD,iGAIgD;AAChD,2EAAqE;AACrE,6EAAuE;AAkDhE,IAAM,sBAAsB,GAA5B,MAAM,sBAAuB,SAAQ,8DAAuB;CAAG,CAAA;AAAzD,wDAAsB;iCAAtB,sBAAsB;IA5ClC,IAA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zB,CAAC;GACW,sBAAsB,CAAmC"}
@@ -0,0 +1,13 @@
1
+ import { type ApolloServerPlugin, type BaseContext, type GraphQLRequestListener } from "@apollo/server";
2
+ import { GraphQLSchemaHost } from "@nestjs/graphql";
3
+ import { GraphQLRateLimitStorage } from "./graphql-rate-limit.storage";
4
+ export declare class GraphQLRateLimitPlugin implements ApolloServerPlugin {
5
+ private readonly storage;
6
+ private readonly gqlSchemaHost;
7
+ private readonly complexityEstimators;
8
+ private readonly maxComplexity;
9
+ private readonly defaultComplexity;
10
+ private readonly rateLimitGetId;
11
+ constructor(storage: GraphQLRateLimitStorage, gqlSchemaHost: GraphQLSchemaHost);
12
+ requestDidStart(): Promise<GraphQLRequestListener<BaseContext>>;
13
+ }
@@ -0,0 +1,144 @@
1
+ "use strict";
2
+ /* eslint-disable @typescript-eslint/require-await */
3
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
4
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
5
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
6
+ 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;
7
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
8
+ };
9
+ var __metadata = (this && this.__metadata) || function (k, v) {
10
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.GraphQLRateLimitPlugin = void 0;
14
+ const apollo_1 = require("@nestjs/apollo");
15
+ const common_1 = require("@nestjs/common");
16
+ const graphql_1 = require("@nestjs/graphql");
17
+ const graphql_2 = require("graphql");
18
+ const graphql_query_complexity_1 = require("graphql-query-complexity");
19
+ const graphql_rate_limit_storage_1 = require("./graphql-rate-limit.storage");
20
+ // https://shopify.engineering/rate-limiting-graphql-apis-calculating-query-complexity
21
+ function shopifyEstimator(args, type) {
22
+ type = type ?? args.field.type;
23
+ if (type instanceof graphql_2.GraphQLNonNull || type instanceof graphql_2.GraphQLList) {
24
+ return shopifyEstimator(args, type.ofType);
25
+ }
26
+ // GraphQL 的 Connection 表示的是一对多的关系,Connection 的消耗是两点加上要返回的对象数量。
27
+ if (type instanceof graphql_2.GraphQLObjectType && type.name.endsWith("Connection")) {
28
+ return 2 + args.childComplexity * (args.args.first ?? args.args.last ?? 0);
29
+ }
30
+ // Object 是查询的基本单位,一般代码一个单次的 server 端操作,可以是一次数据库查询,也可以一次内部服务的访问。
31
+ // Interface 和 Union 和 Object 类似,只不过是能返回不同类型的 object,所以算一点。
32
+ if (type instanceof graphql_2.GraphQLObjectType ||
33
+ type instanceof graphql_2.GraphQLInterfaceType ||
34
+ type instanceof graphql_2.GraphQLUnionType) {
35
+ return 1 + args.childComplexity;
36
+ }
37
+ // Scalar 和 Enum 是 Object 本身的一部分,在 Object 里我们已经算过消耗了。
38
+ // Scalar 和 Enum 其实就是 Object 上的某个字段,一个 Object 上多返回几个字段消耗是比较少的。
39
+ if (type instanceof graphql_2.GraphQLScalarType || type instanceof graphql_2.GraphQLEnumType) {
40
+ return 0;
41
+ }
42
+ }
43
+ let GraphQLRateLimitPlugin = class GraphQLRateLimitPlugin {
44
+ constructor(storage, gqlSchemaHost) {
45
+ this.storage = storage;
46
+ this.gqlSchemaHost = gqlSchemaHost;
47
+ this.complexityEstimators = [];
48
+ this.maxComplexity = 1000;
49
+ this.defaultComplexity = 0;
50
+ this.rateLimitGetId = (args) => {
51
+ const req = args.contextValue.req;
52
+ return req.ips.length ? req.ips[0] : req.ip;
53
+ };
54
+ this.complexityEstimators = [
55
+ (0, graphql_query_complexity_1.directiveEstimator)(),
56
+ (0, graphql_query_complexity_1.fieldExtensionsEstimator)(),
57
+ shopifyEstimator,
58
+ (0, graphql_query_complexity_1.simpleEstimator)({ defaultComplexity: this.defaultComplexity }),
59
+ ];
60
+ }
61
+ async requestDidStart() {
62
+ const { schema } = this.gqlSchemaHost;
63
+ const cost = {
64
+ requestedQueryCost: 0,
65
+ actualQueryCost: 0,
66
+ };
67
+ return {
68
+ didResolveOperation: async (args) => {
69
+ const { request, document } = args;
70
+ cost.requestedQueryCost = (0, graphql_query_complexity_1.getComplexity)({
71
+ schema,
72
+ operationName: request.operationName,
73
+ query: document,
74
+ variables: request.variables,
75
+ estimators: this.complexityEstimators,
76
+ });
77
+ if (cost.requestedQueryCost >= this.maxComplexity) {
78
+ throw new common_1.HttpException(`Query is too complex: ${String(cost.requestedQueryCost)}. Maximum allowed complexity: ${String(this.maxComplexity)}`, 429);
79
+ }
80
+ const { blocked, currentlyAvailable, maximumAvailable, restoreRate } = await this.storage.subPoint(args, cost.requestedQueryCost);
81
+ cost.throttleStatus = {
82
+ blocked,
83
+ maximumAvailable,
84
+ currentlyAvailable,
85
+ restoreRate,
86
+ };
87
+ if (blocked) {
88
+ throw new common_1.HttpException(`Too Many Requests`, 429);
89
+ }
90
+ },
91
+ executionDidStart: async () => {
92
+ return {
93
+ willResolveField: ({ info }) => {
94
+ const parentTypeFields = info.parentType.getFields();
95
+ const field = parentTypeFields[info.fieldName];
96
+ const estimatorArgs = {
97
+ childComplexity: 0,
98
+ args: {},
99
+ field,
100
+ node: info.fieldNodes[0],
101
+ type: info.parentType,
102
+ };
103
+ return (error) => {
104
+ if (error === null) {
105
+ // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
106
+ let fieldCost;
107
+ for (const complexityEstimator of this.complexityEstimators) {
108
+ fieldCost = complexityEstimator(estimatorArgs);
109
+ if (typeof fieldCost === "number" && !isNaN(fieldCost)) {
110
+ cost.actualQueryCost += fieldCost;
111
+ break;
112
+ }
113
+ }
114
+ }
115
+ };
116
+ },
117
+ };
118
+ },
119
+ willSendResponse: async (request) => {
120
+ if (request.response.body.kind === "single" &&
121
+ "data" in request.response.body.singleResult) {
122
+ const { blocked, currentlyAvailable, maximumAvailable, restoreRate } = await this.storage.addPoint(request, cost.requestedQueryCost - cost.actualQueryCost);
123
+ cost.throttleStatus = {
124
+ blocked,
125
+ maximumAvailable,
126
+ currentlyAvailable,
127
+ restoreRate,
128
+ };
129
+ request.response.body.singleResult.extensions = {
130
+ ...request.response.body.singleResult.extensions,
131
+ cost,
132
+ };
133
+ }
134
+ },
135
+ };
136
+ }
137
+ };
138
+ exports.GraphQLRateLimitPlugin = GraphQLRateLimitPlugin;
139
+ exports.GraphQLRateLimitPlugin = GraphQLRateLimitPlugin = __decorate([
140
+ (0, apollo_1.Plugin)(),
141
+ __metadata("design:paramtypes", [graphql_rate_limit_storage_1.GraphQLRateLimitStorage,
142
+ graphql_1.GraphQLSchemaHost])
143
+ ], GraphQLRateLimitPlugin);
144
+ //# sourceMappingURL=graphql-rate-limit.plugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graphql-rate-limit.plugin.js","sourceRoot":"","sources":["../src/graphql-rate-limit.plugin.ts"],"names":[],"mappings":";AAAA,qDAAqD;;;;;;;;;;;;AAQrD,2CAAwC;AACxC,2CAA+C;AAC/C,6CAAoD;AAEpD,qCASiB;AACjB,uEAOkC;AAElC,6EAAuE;AAGvE,sFAAsF;AACtF,SAAS,gBAAgB,CACvB,IAA6B,EAC7B,IAAkB;IAElB,IAAI,GAAG,IAAI,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC;IAE/B,IAAI,IAAI,YAAY,wBAAc,IAAI,IAAI,YAAY,qBAAW,EAAE,CAAC;QAClE,OAAO,gBAAgB,CAAC,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;IAC7C,CAAC;IAED,+DAA+D;IAC/D,IAAI,IAAI,YAAY,2BAAiB,IAAI,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,YAAY,CAAC,EAAE,CAAC;QAC1E,OAAO,CAAC,GAAG,IAAI,CAAC,eAAe,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,IAAI,IAAI,CA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YAAY,CAAC,UAAU,GAAG;wBAC9C,GAAG,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,YAAY,CAAC,UAAU;wBAChD,IAAI;qBACL,CAAC;gBACJ,CAAC;YACH,CAAC;SACF,CAAC;IACJ,CAAC;CACF,CAAA;AA5HY,wDAAsB;iCAAtB,sBAAsB;IADlC,IAAA,eAAM,GAAE;qCAeqB,oDAAuB;QACjB,2BAAiB;GAfxC,sBAAsB,CA4HlC"}
@@ -0,0 +1,10 @@
1
+ import { BaseContext, GraphQLRequestContext } from "@apollo/server";
2
+ import { Redis } from "@nest-boot/redis";
3
+ import { CostThrottleStatus, GraphQLRateLimitOptions } from "./interfaces";
4
+ export declare class GraphQLRateLimitStorage {
5
+ private readonly redis;
6
+ private readonly options;
7
+ constructor(redis: Redis, options: GraphQLRateLimitOptions);
8
+ addPoint(args: GraphQLRequestContext<BaseContext>, point: number): Promise<CostThrottleStatus>;
9
+ subPoint(args: GraphQLRequestContext<BaseContext>, point: number): Promise<CostThrottleStatus>;
10
+ }
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
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
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
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 __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.GraphQLRateLimitStorage = void 0;
16
+ const redis_1 = require("@nest-boot/redis");
17
+ const common_1 = require("@nestjs/common");
18
+ const graphql_rate_limit_module_definition_1 = require("./graphql-rate-limit.module-definition");
19
+ let GraphQLRateLimitStorage = class GraphQLRateLimitStorage {
20
+ constructor(redis, options) {
21
+ this.redis = redis;
22
+ this.options = options;
23
+ this.redis.defineCommand(graphql_rate_limit_module_definition_1.REDIS_COMMAND, {
24
+ numberOfKeys: 5,
25
+ lua: /* lua */ `
26
+ -- 获取当前时间戳
27
+ local currentTimestamp = redis.call("TIME")[1]
28
+
29
+ -- 获取参数
30
+ local bucketKeyPrefix = KEYS[1]
31
+ local maximumAvailable = tonumber(KEYS[2])
32
+ local restoreRate = tonumber(KEYS[3])
33
+ local id = KEYS[4]
34
+ local complexity = tonumber(KEYS[5])
35
+
36
+ -- 定义存储桶的键
37
+ local bucketKey = bucketKeyPrefix .. ":" .. id
38
+
39
+ local keyExpireSeconds = math.ceil(maximumAvailable / restoreRate)
40
+
41
+ -- 获取存储桶中当前的令牌数量,如果不存在,则设置为最大可用令牌数
42
+ local currentlyAvailable = redis.call("HGET", bucketKey, "currentlyAvailable")
43
+ if not currentlyAvailable then
44
+ currentlyAvailable = maximumAvailable
45
+ end
46
+
47
+ -- 如果存储桶为空,设置上次更新时间戳为当前时间戳
48
+ local updatedTimestamp = redis.call("HGET", bucketKey, "updatedTimestamp")
49
+ if not updatedTimestamp then
50
+ updatedTimestamp = currentTimestamp
51
+ end
52
+
53
+ -- 更新上次更新的时间戳
54
+ redis.call("HSET", bucketKey, "updatedTimestamp", currentTimestamp)
55
+
56
+ -- 更新存储桶的过期时间
57
+ redis.call("EXPIRE", bucketKey, keyExpireSeconds)
58
+
59
+ -- 计算自上次更新以来要恢复的令牌数量,并恢复存储桶中的令牌数量
60
+ local intervalSeconds = currentTimestamp - updatedTimestamp;
61
+ if intervalSeconds > 0 then
62
+ currentlyAvailable = math.min((restoreRate * intervalSeconds) + currentlyAvailable, maximumAvailable);
63
+ redis.call("HSET", bucketKey, "currentlyAvailable", currentlyAvailable)
64
+ end
65
+
66
+ -- 检查是否有足够的令牌供扣减,如果有,则扣减并返回剩余令牌数
67
+ local newCurrentlyAvailable = currentlyAvailable - complexity
68
+ if newCurrentlyAvailable >= 0 then
69
+ currentlyAvailable = newCurrentlyAvailable
70
+ redis.call("HSET", bucketKey, "currentlyAvailable", currentlyAvailable)
71
+ return { false, currentlyAvailable };
72
+ end
73
+
74
+ return { true, currentlyAvailable }
75
+ `,
76
+ });
77
+ }
78
+ async addPoint(args, point) {
79
+ const [blocked, currentlyAvailable] = (await this.redis[graphql_rate_limit_module_definition_1.REDIS_COMMAND](this.options.keyPrefix, this.options.maximumAvailable, this.options.restoreRate, this.options.getId(args), -point));
80
+ return {
81
+ blocked,
82
+ currentlyAvailable,
83
+ maximumAvailable: this.options.maximumAvailable,
84
+ restoreRate: this.options.restoreRate,
85
+ };
86
+ }
87
+ async subPoint(args, point) {
88
+ const [blocked, currentlyAvailable] = (await this.redis[graphql_rate_limit_module_definition_1.REDIS_COMMAND](this.options.keyPrefix, this.options.maximumAvailable, this.options.restoreRate, this.options.getId(args), point));
89
+ return {
90
+ blocked,
91
+ currentlyAvailable,
92
+ maximumAvailable: this.options.maximumAvailable,
93
+ restoreRate: this.options.restoreRate,
94
+ };
95
+ }
96
+ };
97
+ exports.GraphQLRateLimitStorage = GraphQLRateLimitStorage;
98
+ exports.GraphQLRateLimitStorage = GraphQLRateLimitStorage = __decorate([
99
+ (0, common_1.Injectable)(),
100
+ __param(1, (0, common_1.Inject)(graphql_rate_limit_module_definition_1.OPTIONS_TOKEN)),
101
+ __metadata("design:paramtypes", [redis_1.Redis, Object])
102
+ ], GraphQLRateLimitStorage);
103
+ //# sourceMappingURL=graphql-rate-limit.storage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graphql-rate-limit.storage.js","sourceRoot":"","sources":["../src/graphql-rate-limit.storage.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,4CA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C;QAExB,OAAO;YACL,OAAO;YACP,kBAAkB;YAClB,gBAAgB,EAAE,IAAI,CAAC,OAAO,CAAC,gBAAgB;YAC/C,WAAW,EAAE,IAAI,CAAC,OAAO,CAAC,WAAW;SACtC,CAAC;IACJ,CAAC;CACF,CAAA;AAzGY,0DAAuB;kCAAvB,uBAAuB;IADnC,IAAA,mBAAU,GAAE;IAIR,WAAA,IAAA,eAAM,EAAC,oDAAa,CAAC,CAAA;qCADE,aAAK;GAFpB,uBAAuB,CAyGnC"}
@@ -0,0 +1,5 @@
1
+ export * from "./complexities";
2
+ export * from "./decorators";
3
+ export * from "./graphql-rate-limit.module";
4
+ export { MODULE_OPTIONS_TOKEN as GRAPHQL_COMPLEXITY_MODULE_OPTIONS_TOKEN } from "./graphql-rate-limit.module-definition";
5
+ export * from "./interfaces";
package/dist/index.js ADDED
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.GRAPHQL_COMPLEXITY_MODULE_OPTIONS_TOKEN = void 0;
18
+ __exportStar(require("./complexities"), exports);
19
+ __exportStar(require("./decorators"), exports);
20
+ __exportStar(require("./graphql-rate-limit.module"), exports);
21
+ var graphql_rate_limit_module_definition_1 = require("./graphql-rate-limit.module-definition");
22
+ Object.defineProperty(exports, "GRAPHQL_COMPLEXITY_MODULE_OPTIONS_TOKEN", { enumerable: true, get: function () { return graphql_rate_limit_module_definition_1.MODULE_OPTIONS_TOKEN; } });
23
+ __exportStar(require("./interfaces"), exports);
24
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,iDAA+B;AAC/B,+CAA6B;AAC7B,8DAA4C;AAC5C,+FAAyH;AAAhH,+JAAA,oBAAoB,OAA2C;AACxE,+CAA6B"}
@@ -0,0 +1,11 @@
1
+ export interface CostThrottleStatus {
2
+ blocked: boolean;
3
+ maximumAvailable: number;
4
+ currentlyAvailable: number;
5
+ restoreRate: number;
6
+ }
7
+ export interface CostResponse {
8
+ requestedQueryCost: number;
9
+ actualQueryCost: number;
10
+ throttleStatus?: CostThrottleStatus;
11
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=cost-response.interface.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cost-response.interface.js","sourceRoot":"","sources":["../../src/interfaces/cost-response.interface.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ import { GraphQLRateLimitOptions } from "./graphql-rate-limit-options.interface";
2
+ export type GraphQLRateLimitModuleOptions = Partial<GraphQLRateLimitOptions>;
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=graphql-rate-limit-module-options.interface.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graphql-rate-limit-module-options.interface.js","sourceRoot":"","sources":["../../src/interfaces/graphql-rate-limit-module-options.interface.ts"],"names":[],"mappings":""}
@@ -0,0 +1,11 @@
1
+ import { BaseContext, GraphQLRequestContext } from "@apollo/server";
2
+ import { RedisOptions } from "@nest-boot/redis";
3
+ export interface GraphQLRateLimitOptions {
4
+ connection?: RedisOptions;
5
+ maxComplexity: number;
6
+ defaultComplexity: number;
7
+ keyPrefix: string;
8
+ restoreRate: number;
9
+ maximumAvailable: number;
10
+ getId: (args: GraphQLRequestContext<BaseContext>) => string;
11
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=graphql-rate-limit-options.interface.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graphql-rate-limit-options.interface.js","sourceRoot":"","sources":["../../src/interfaces/graphql-rate-limit-options.interface.ts"],"names":[],"mappings":""}
@@ -0,0 +1,3 @@
1
+ export * from "./cost-response.interface";
2
+ export * from "./graphql-rate-limit-module-options.interface";
3
+ export * from "./graphql-rate-limit-options.interface";
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./cost-response.interface"), exports);
18
+ __exportStar(require("./graphql-rate-limit-module-options.interface"), exports);
19
+ __exportStar(require("./graphql-rate-limit-options.interface"), exports);
20
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/interfaces/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4DAA0C;AAC1C,gFAA8D;AAC9D,yEAAuD"}