@nestjs/throttler 4.2.1 → 5.0.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/README.md +204 -159
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/throttler-module-options.interface.d.ts +12 -4
- package/dist/throttler-storage.interface.d.ts +0 -2
- package/dist/throttler-storage.interface.js.map +1 -1
- package/dist/throttler.decorator.d.ts +8 -2
- package/dist/throttler.decorator.js +16 -12
- package/dist/throttler.decorator.js.map +1 -1
- package/dist/throttler.guard.d.ts +12 -5
- package/dist/throttler.guard.interface.d.ts +7 -0
- package/dist/throttler.guard.interface.js +3 -0
- package/dist/throttler.guard.interface.js.map +1 -0
- package/dist/throttler.guard.js +77 -30
- package/dist/throttler.guard.js.map +1 -1
- package/dist/throttler.module.js +3 -2
- package/dist/throttler.module.js.map +1 -1
- package/dist/throttler.providers.d.ts +2 -1
- package/dist/throttler.providers.js +3 -1
- package/dist/throttler.providers.js.map +1 -1
- package/dist/throttler.service.js +3 -2
- package/dist/throttler.service.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/utilities.d.ts +5 -0
- package/dist/utilities.js +14 -0
- package/dist/utilities.js.map +1 -0
- package/package.json +33 -32
package/dist/throttler.guard.js
CHANGED
|
@@ -20,7 +20,7 @@ const throttler_storage_interface_1 = require("./throttler-storage.interface");
|
|
|
20
20
|
const throttler_constants_1 = require("./throttler.constants");
|
|
21
21
|
const throttler_decorator_1 = require("./throttler.decorator");
|
|
22
22
|
const throttler_exception_1 = require("./throttler.exception");
|
|
23
|
-
let ThrottlerGuard =
|
|
23
|
+
let ThrottlerGuard = class ThrottlerGuard {
|
|
24
24
|
constructor(options, storageService, reflector) {
|
|
25
25
|
this.options = options;
|
|
26
26
|
this.storageService = storageService;
|
|
@@ -28,62 +28,109 @@ let ThrottlerGuard = exports.ThrottlerGuard = class ThrottlerGuard {
|
|
|
28
28
|
this.headerPrefix = 'X-RateLimit';
|
|
29
29
|
this.errorMessage = throttler_exception_1.throttlerMessage;
|
|
30
30
|
}
|
|
31
|
+
async onModuleInit() {
|
|
32
|
+
this.throttlers = (Array.isArray(this.options) ? this.options : this.options.throttlers)
|
|
33
|
+
.sort((first, second) => {
|
|
34
|
+
if (typeof first.ttl === 'function') {
|
|
35
|
+
return 1;
|
|
36
|
+
}
|
|
37
|
+
if (typeof second.ttl === 'function') {
|
|
38
|
+
return 0;
|
|
39
|
+
}
|
|
40
|
+
return first.ttl - second.ttl;
|
|
41
|
+
})
|
|
42
|
+
.map((opt) => { var _a; return (Object.assign(Object.assign({}, opt), { name: (_a = opt.name) !== null && _a !== void 0 ? _a : 'default' })); });
|
|
43
|
+
if (Array.isArray(this.options)) {
|
|
44
|
+
this.commonOptions = {};
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
this.commonOptions = {
|
|
48
|
+
skipIf: this.options.skipIf,
|
|
49
|
+
ignoreUserAgents: this.options.ignoreUserAgents,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
}
|
|
31
53
|
async canActivate(context) {
|
|
32
|
-
var _a, _b;
|
|
33
54
|
const handler = context.getHandler();
|
|
34
55
|
const classRef = context.getClass();
|
|
35
|
-
if (this.
|
|
36
|
-
((_b = (_a = this.options).skipIf) === null || _b === void 0 ? void 0 : _b.call(_a, context))) {
|
|
56
|
+
if (await this.shouldSkip(context)) {
|
|
37
57
|
return true;
|
|
38
58
|
}
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
59
|
+
const continues = [];
|
|
60
|
+
for (const namedThrottler of this.throttlers) {
|
|
61
|
+
const skip = this.reflector.getAllAndOverride(throttler_constants_1.THROTTLER_SKIP + namedThrottler.name, [
|
|
62
|
+
handler,
|
|
63
|
+
classRef,
|
|
64
|
+
]);
|
|
65
|
+
const skipIf = namedThrottler.skipIf || this.commonOptions.skipIf;
|
|
66
|
+
if (skip || (skipIf === null || skipIf === void 0 ? void 0 : skipIf(context))) {
|
|
67
|
+
continues.push(true);
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
const routeOrClassLimit = this.reflector.getAllAndOverride(throttler_constants_1.THROTTLER_LIMIT + namedThrottler.name, [handler, classRef]);
|
|
71
|
+
const routeOrClassTtl = this.reflector.getAllAndOverride(throttler_constants_1.THROTTLER_TTL + namedThrottler.name, [handler, classRef]);
|
|
72
|
+
const limit = await this.resolveValue(context, routeOrClassLimit || namedThrottler.limit);
|
|
73
|
+
const ttl = await this.resolveValue(context, routeOrClassTtl || namedThrottler.ttl);
|
|
74
|
+
continues.push(await this.handleRequest(context, limit, ttl, namedThrottler));
|
|
75
|
+
}
|
|
76
|
+
return continues.every((cont) => cont);
|
|
77
|
+
}
|
|
78
|
+
async shouldSkip(_context) {
|
|
79
|
+
return false;
|
|
50
80
|
}
|
|
51
|
-
async handleRequest(context, limit, ttl) {
|
|
81
|
+
async handleRequest(context, limit, ttl, throttler) {
|
|
82
|
+
var _a;
|
|
52
83
|
const { req, res } = this.getRequestResponse(context);
|
|
53
|
-
|
|
54
|
-
|
|
84
|
+
const ignoreUserAgents = (_a = throttler.ignoreUserAgents) !== null && _a !== void 0 ? _a : this.commonOptions.ignoreUserAgents;
|
|
85
|
+
if (Array.isArray(ignoreUserAgents)) {
|
|
86
|
+
for (const pattern of ignoreUserAgents) {
|
|
55
87
|
if (pattern.test(req.headers['user-agent'])) {
|
|
56
88
|
return true;
|
|
57
89
|
}
|
|
58
90
|
}
|
|
59
91
|
}
|
|
60
|
-
const tracker = this.getTracker(req);
|
|
61
|
-
const key = this.generateKey(context, tracker);
|
|
92
|
+
const tracker = await this.getTracker(req);
|
|
93
|
+
const key = this.generateKey(context, tracker, throttler.name);
|
|
62
94
|
const { totalHits, timeToExpire } = await this.storageService.increment(key, ttl);
|
|
95
|
+
const getThrottlerSuffix = (name) => (name === 'default' ? '' : `-${name}`);
|
|
63
96
|
if (totalHits > limit) {
|
|
64
|
-
res.header(
|
|
65
|
-
this.throwThrottlingException(context
|
|
97
|
+
res.header(`Retry-After${getThrottlerSuffix(throttler.name)}`, timeToExpire);
|
|
98
|
+
await this.throwThrottlingException(context, {
|
|
99
|
+
limit,
|
|
100
|
+
ttl,
|
|
101
|
+
key,
|
|
102
|
+
tracker,
|
|
103
|
+
totalHits,
|
|
104
|
+
timeToExpire,
|
|
105
|
+
});
|
|
66
106
|
}
|
|
67
|
-
res.header(`${this.headerPrefix}-Limit`, limit);
|
|
68
|
-
res.header(`${this.headerPrefix}-Remaining`, Math.max(0, limit - totalHits));
|
|
69
|
-
res.header(`${this.headerPrefix}-Reset`, timeToExpire);
|
|
107
|
+
res.header(`${this.headerPrefix}-Limit${getThrottlerSuffix(throttler.name)}`, limit);
|
|
108
|
+
res.header(`${this.headerPrefix}-Remaining${getThrottlerSuffix(throttler.name)}`, Math.max(0, limit - totalHits));
|
|
109
|
+
res.header(`${this.headerPrefix}-Reset${getThrottlerSuffix(throttler.name)}`, timeToExpire);
|
|
70
110
|
return true;
|
|
71
111
|
}
|
|
72
|
-
getTracker(req) {
|
|
112
|
+
async getTracker(req) {
|
|
73
113
|
return req.ip;
|
|
74
114
|
}
|
|
75
115
|
getRequestResponse(context) {
|
|
76
116
|
const http = context.switchToHttp();
|
|
77
117
|
return { req: http.getRequest(), res: http.getResponse() };
|
|
78
118
|
}
|
|
79
|
-
generateKey(context, suffix) {
|
|
80
|
-
const prefix = `${context.getClass().name}-${context.getHandler().name}`;
|
|
119
|
+
generateKey(context, suffix, name) {
|
|
120
|
+
const prefix = `${context.getClass().name}-${context.getHandler().name}-${name}`;
|
|
81
121
|
return md5(`${prefix}-${suffix}`);
|
|
82
122
|
}
|
|
83
|
-
throwThrottlingException(context) {
|
|
84
|
-
throw new throttler_exception_1.ThrottlerException(this.
|
|
123
|
+
async throwThrottlingException(context, throttlerLimitDetail) {
|
|
124
|
+
throw new throttler_exception_1.ThrottlerException(await this.getErrorMessage(context, throttlerLimitDetail));
|
|
125
|
+
}
|
|
126
|
+
async getErrorMessage(_context, _throttlerLimitDetail) {
|
|
127
|
+
return this.errorMessage;
|
|
128
|
+
}
|
|
129
|
+
async resolveValue(context, resolvableValue) {
|
|
130
|
+
return typeof resolvableValue === 'function' ? resolvableValue(context) : resolvableValue;
|
|
85
131
|
}
|
|
86
132
|
};
|
|
133
|
+
exports.ThrottlerGuard = ThrottlerGuard;
|
|
87
134
|
exports.ThrottlerGuard = ThrottlerGuard = __decorate([
|
|
88
135
|
(0, common_1.Injectable)(),
|
|
89
136
|
__param(0, (0, throttler_decorator_1.InjectThrottlerOptions)()),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"throttler.guard.js","sourceRoot":"","sources":["../src/throttler.guard.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAA2E;AAC3E,uCAAyC;AACzC,2BAA2B;
|
|
1
|
+
{"version":3,"file":"throttler.guard.js","sourceRoot":"","sources":["../src/throttler.guard.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAA2E;AAC3E,uCAAyC;AACzC,2BAA2B;AAM3B,+EAAiE;AACjE,+DAAuF;AACvF,+DAAuF;AACvF,+DAA6E;AAOtE,IAAM,cAAc,GAApB,MAAM,cAAc;IAKzB,YAC4B,OAAkD,EAClD,cAAmD,EAC1D,SAAoB;QAFM,YAAO,GAAP,OAAO,CAAwB;QAC/B,mBAAc,GAAd,cAAc,CAAkB;QAC1D,cAAS,GAAT,SAAS,CAAW;QAP/B,iBAAY,GAAG,aAAa,CAAC;QAC7B,iBAAY,GAAG,sCAAgB,CAAC;IAOvC,CAAC;IAEJ,KAAK,CAAC,YAAY;QAChB,IAAI,CAAC,UAAU,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC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eAAe,KAAK,UAAU,CAAC,CAAC,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,eAAe,CAAC;IAC5F,CAAC;CACF,CAAA;AAtLY,wCAAc;yBAAd,cAAc;IAD1B,IAAA,mBAAU,GAAE;IAOR,WAAA,IAAA,4CAAsB,GAAE,CAAA;IACxB,WAAA,IAAA,4CAAsB,GAAE,CAAA;qDACK,gBAAS;GAR9B,cAAc,CAsL1B"}
|
package/dist/throttler.module.js
CHANGED
|
@@ -11,8 +11,8 @@ exports.ThrottlerModule = void 0;
|
|
|
11
11
|
const common_1 = require("@nestjs/common");
|
|
12
12
|
const throttler_constants_1 = require("./throttler.constants");
|
|
13
13
|
const throttler_providers_1 = require("./throttler.providers");
|
|
14
|
-
let ThrottlerModule =
|
|
15
|
-
static forRoot(options =
|
|
14
|
+
let ThrottlerModule = ThrottlerModule_1 = class ThrottlerModule {
|
|
15
|
+
static forRoot(options = []) {
|
|
16
16
|
const providers = [...(0, throttler_providers_1.createThrottlerProviders)(options), throttler_providers_1.ThrottlerStorageProvider];
|
|
17
17
|
return {
|
|
18
18
|
module: ThrottlerModule_1,
|
|
@@ -56,6 +56,7 @@ let ThrottlerModule = exports.ThrottlerModule = ThrottlerModule_1 = class Thrott
|
|
|
56
56
|
};
|
|
57
57
|
}
|
|
58
58
|
};
|
|
59
|
+
exports.ThrottlerModule = ThrottlerModule;
|
|
59
60
|
exports.ThrottlerModule = ThrottlerModule = ThrottlerModule_1 = __decorate([
|
|
60
61
|
(0, common_1.Global)(),
|
|
61
62
|
(0, common_1.Module)({})
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"throttler.module.js","sourceRoot":"","sources":["../src/throttler.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAAyE;AAMzE,+DAA0D;AAC1D,+DAA2F;AAOpF,IAAM,eAAe,
|
|
1
|
+
{"version":3,"file":"throttler.module.js","sourceRoot":"","sources":["../src/throttler.module.ts"],"names":[],"mapp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AE,KAAK,EAAE,cAAuC,EAAE,EAAE,CAC5D,MAAM,cAAc,CAAC,sBAAsB,EAAE;YAC/C,MAAM,EAAE,CAAC,OAAO,CAAC,WAAW,IAAI,OAAO,CAAC,QAAQ,CAAC;SAClD,CAAC;IACJ,CAAC;CACF,CAAA;AAtDY,0CAAe;0BAAf,eAAe;IAF3B,IAAA,eAAM,GAAE;IACR,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,eAAe,CAsD3B"}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { Provider } from '@nestjs/common';
|
|
2
2
|
import { ThrottlerModuleOptions } from './throttler-module-options.interface';
|
|
3
|
+
import { ThrottlerStorage } from './throttler-storage.interface';
|
|
3
4
|
export declare function createThrottlerProviders(options: ThrottlerModuleOptions): Provider[];
|
|
4
5
|
export declare const ThrottlerStorageProvider: {
|
|
5
6
|
provide: symbol;
|
|
6
|
-
useFactory: (options: ThrottlerModuleOptions) =>
|
|
7
|
+
useFactory: (options: ThrottlerModuleOptions) => ThrottlerStorage;
|
|
7
8
|
inject: string[];
|
|
8
9
|
};
|
|
9
10
|
export declare const getOptionsToken: () => string;
|
|
@@ -16,7 +16,9 @@ exports.createThrottlerProviders = createThrottlerProviders;
|
|
|
16
16
|
exports.ThrottlerStorageProvider = {
|
|
17
17
|
provide: throttler_storage_interface_1.ThrottlerStorage,
|
|
18
18
|
useFactory: (options) => {
|
|
19
|
-
return options
|
|
19
|
+
return !Array.isArray(options) && options.storage
|
|
20
|
+
? options.storage
|
|
21
|
+
: new throttler_service_1.ThrottlerStorageService();
|
|
20
22
|
},
|
|
21
23
|
inject: [throttler_constants_1.THROTTLER_OPTIONS],
|
|
22
24
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"throttler.providers.js","sourceRoot":"","sources":["../src/throttler.providers.ts"],"names":[],"mappings":";;;AAEA,+EAAiE;AACjE,+DAA0D;AAC1D,2DAA8D;AAE9D,SAAgB,wBAAwB,CAAC,OAA+B;IACtE,OAAO;QACL;YACE,OAAO,EAAE,uCAAiB;YAC1B,QAAQ,EAAE,OAAO;SAClB;KACF,CAAC;AACJ,CAAC;AAPD,4DAOC;AAEY,QAAA,wBAAwB,GAAG;IACtC,OAAO,EAAE,8CAAgB;IACzB,UAAU,EAAE,CAAC,OAA+B,EAAE,EAAE;QAC9C,OAAO,
|
|
1
|
+
{"version":3,"file":"throttler.providers.js","sourceRoot":"","sources":["../src/throttler.providers.ts"],"names":[],"mappings":";;;AAEA,+EAAiE;AACjE,+DAA0D;AAC1D,2DAA8D;AAE9D,SAAgB,wBAAwB,CAAC,OAA+B;IACtE,OAAO;QACL;YACE,OAAO,EAAE,uCAAiB;YAC1B,QAAQ,EAAE,OAAO;SAClB;KACF,CAAC;AACJ,CAAC;AAPD,4DAOC;AAEY,QAAA,wBAAwB,GAAG;IACtC,OAAO,EAAE,8CAAgB;IACzB,UAAU,EAAE,CAAC,OAA+B,EAAE,EAAE;QAC9C,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,OAAO;YAC/C,CAAC,CAAC,OAAO,CAAC,OAAO;YACjB,CAAC,CAAC,IAAI,2CAAuB,EAAE,CAAC;IACpC,CAAC;IACD,MAAM,EAAE,CAAC,uCAAiB,CAAC;CAC5B,CAAC;AAMK,MAAM,eAAe,GAAG,GAAG,EAAE,CAAC,uCAAiB,CAAC;AAA1C,QAAA,eAAe,mBAA2B;AAMhD,MAAM,eAAe,GAAG,GAAG,EAAE,CAAC,8CAAgB,CAAC;AAAzC,QAAA,eAAe,mBAA0B"}
|
|
@@ -8,7 +8,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.ThrottlerStorageService = void 0;
|
|
10
10
|
const common_1 = require("@nestjs/common");
|
|
11
|
-
let ThrottlerStorageService =
|
|
11
|
+
let ThrottlerStorageService = class ThrottlerStorageService {
|
|
12
12
|
constructor() {
|
|
13
13
|
this._storage = {};
|
|
14
14
|
this.timeoutIds = [];
|
|
@@ -28,7 +28,7 @@ let ThrottlerStorageService = exports.ThrottlerStorageService = class ThrottlerS
|
|
|
28
28
|
this.timeoutIds.push(timeoutId);
|
|
29
29
|
}
|
|
30
30
|
async increment(key, ttl) {
|
|
31
|
-
const ttlMilliseconds = ttl
|
|
31
|
+
const ttlMilliseconds = ttl;
|
|
32
32
|
if (!this.storage[key]) {
|
|
33
33
|
this.storage[key] = { totalHits: 0, expiresAt: Date.now() + ttlMilliseconds };
|
|
34
34
|
}
|
|
@@ -48,6 +48,7 @@ let ThrottlerStorageService = exports.ThrottlerStorageService = class ThrottlerS
|
|
|
48
48
|
this.timeoutIds.forEach(clearTimeout);
|
|
49
49
|
}
|
|
50
50
|
};
|
|
51
|
+
exports.ThrottlerStorageService = ThrottlerStorageService;
|
|
51
52
|
exports.ThrottlerStorageService = ThrottlerStorageService = __decorate([
|
|
52
53
|
(0, common_1.Injectable)()
|
|
53
54
|
], ThrottlerStorageService);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"throttler.service.js","sourceRoot":"","sources":["../src/throttler.service.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAAmE;AAS5D,IAAM,uBAAuB,
|
|
1
|
+
{"version":3,"file":"throttler.service.js","sourceRoot":"","sources":["../src/throttler.service.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAAmE;AAS5D,IAAM,uBAAuB,GAA7B,MAAM,uBAAuB;IAA7B;QACG,aAAQ,GAA4C,EAAE,CAAC;QACvD,eAAU,GAAqB,EAAE,CAAC;IAmD5C,CAAC;IAjDC,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAKO,iBAAiB,CAAC,GAAW;QACnC,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,SAAS,GAAG,IAAI,CAA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