@nestjs/throttler 3.1.0 → 4.1.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 +7 -7
- package/dist/throttler-storage-options.interface.d.ts +5 -0
- package/dist/throttler-storage-options.interface.js +5 -0
- package/dist/throttler-storage-options.interface.js.map +1 -0
- package/dist/throttler-storage-record.interface.d.ts +5 -0
- package/dist/throttler-storage-record.interface.js +5 -0
- package/dist/throttler-storage-record.interface.js.map +1 -0
- package/dist/throttler-storage.interface.d.ts +4 -3
- package/dist/throttler-storage.interface.js.map +1 -1
- package/dist/throttler.decorator.d.ts +2 -2
- package/dist/throttler.guard.js +7 -10
- package/dist/throttler.guard.js.map +1 -1
- package/dist/throttler.module.js +2 -3
- package/dist/throttler.module.js.map +1 -1
- package/dist/throttler.service.d.ts +6 -3
- package/dist/throttler.service.js +23 -12
- package/dist/throttler.service.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +64 -66
- package/CHANGELOG.md +0 -123
package/README.md
CHANGED
|
@@ -31,7 +31,6 @@ For an overview of the community storage providers, see [Community Storage Provi
|
|
|
31
31
|
|
|
32
32
|
This package comes with a couple of goodies that should be mentioned, first is the `ThrottlerModule`.
|
|
33
33
|
|
|
34
|
-
|
|
35
34
|
## Installation
|
|
36
35
|
|
|
37
36
|
```bash
|
|
@@ -42,6 +41,8 @@ $ npm i --save @nestjs/throttler
|
|
|
42
41
|
|
|
43
42
|
`@nestjs/throttler@^1` is compatible with Nest v7 while `@nestjs/throttler@^2` is compatible with Nest v7 and Nest v8, but it is suggested to be used with only v8 in case of breaking changes against v7 that are unseen.
|
|
44
43
|
|
|
44
|
+
For NestJS v10, please use version 4.1.0 or above
|
|
45
|
+
|
|
45
46
|
## Table of Contents
|
|
46
47
|
|
|
47
48
|
- [Description](#description)
|
|
@@ -216,8 +217,8 @@ The interface looks like this:
|
|
|
216
217
|
|
|
217
218
|
```ts
|
|
218
219
|
export interface ThrottlerStorage {
|
|
219
|
-
|
|
220
|
-
|
|
220
|
+
storage: Record<string, ThrottlerStorageOptions>;
|
|
221
|
+
increment(key: string, ttl: number): Promise<ThrottlerStorageRecord>;
|
|
221
222
|
}
|
|
222
223
|
```
|
|
223
224
|
|
|
@@ -259,13 +260,12 @@ export class WsThrottlerGuard extends ThrottlerGuard {
|
|
|
259
260
|
.filter((obj) => obj)
|
|
260
261
|
.shift().remoteAddress;
|
|
261
262
|
const key = this.generateKey(context, ip);
|
|
262
|
-
const
|
|
263
|
+
const { totalHits } = await this.storageService.increment(key, ttl);
|
|
263
264
|
|
|
264
|
-
if (
|
|
265
|
+
if (totalHits > limit) {
|
|
265
266
|
throw new ThrottlerException();
|
|
266
267
|
}
|
|
267
268
|
|
|
268
|
-
await this.storageService.addRecord(key, ttl);
|
|
269
269
|
return true;
|
|
270
270
|
}
|
|
271
271
|
}
|
|
@@ -306,4 +306,4 @@ Feel free to submit a PR with your custom storage provider being added to this l
|
|
|
306
306
|
|
|
307
307
|
## License
|
|
308
308
|
|
|
309
|
-
Nest is [MIT licensed](LICENSE).
|
|
309
|
+
Nest is [MIT licensed](LICENSE).
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"throttler-storage-options.interface.js","sourceRoot":"","sources":["../src/throttler-storage-options.interface.ts"],"names":[],"mappings":";;;AAYa,QAAA,uBAAuB,GAAG,MAAM,CAAC,yBAAyB,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"throttler-storage-record.interface.js","sourceRoot":"","sources":["../src/throttler-storage-record.interface.ts"],"names":[],"mappings":";;;AAYa,QAAA,sBAAsB,GAAG,MAAM,CAAC,wBAAwB,CAAC,CAAC"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { ThrottlerStorageOptions } from './throttler-storage-options.interface';
|
|
2
|
+
import { ThrottlerStorageRecord } from './throttler-storage-record.interface';
|
|
1
3
|
export interface ThrottlerStorage {
|
|
2
|
-
storage: Record<string,
|
|
3
|
-
|
|
4
|
-
addRecord(key: string, ttl: number): Promise<void>;
|
|
4
|
+
storage: Record<string, ThrottlerStorageOptions>;
|
|
5
|
+
increment(key: string, ttl: number): Promise<ThrottlerStorageRecord>;
|
|
5
6
|
}
|
|
6
7
|
export declare const ThrottlerStorage: unique symbol;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"throttler-storage.interface.js","sourceRoot":"","sources":["../src/throttler-storage.interface.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"throttler-storage.interface.js","sourceRoot":"","sources":["../src/throttler-storage.interface.ts"],"names":[],"mappings":";;;AAiBa,QAAA,gBAAgB,GAAG,MAAM,CAAC,kBAAkB,CAAC,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export declare const Throttle: (limit?: number, ttl?: number) => MethodDecorator & ClassDecorator;
|
|
2
2
|
export declare const SkipThrottle: (skip?: boolean) => MethodDecorator & ClassDecorator;
|
|
3
|
-
export declare const InjectThrottlerOptions: () =>
|
|
4
|
-
export declare const InjectThrottlerStorage: () =>
|
|
3
|
+
export declare const InjectThrottlerOptions: () => PropertyDecorator & ParameterDecorator;
|
|
4
|
+
export declare const InjectThrottlerStorage: () => PropertyDecorator & ParameterDecorator;
|
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 = class ThrottlerGuard {
|
|
23
|
+
let ThrottlerGuard = exports.ThrottlerGuard = class ThrottlerGuard {
|
|
24
24
|
constructor(options, storageService, reflector) {
|
|
25
25
|
this.options = options;
|
|
26
26
|
this.storageService = storageService;
|
|
@@ -59,16 +59,14 @@ let ThrottlerGuard = class ThrottlerGuard {
|
|
|
59
59
|
}
|
|
60
60
|
const tracker = this.getTracker(req);
|
|
61
61
|
const key = this.generateKey(context, tracker);
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
res.header('Retry-After', nearestExpiryTime);
|
|
62
|
+
const { totalHits, timeToExpire } = await this.storageService.increment(key, ttl);
|
|
63
|
+
if (totalHits > limit) {
|
|
64
|
+
res.header('Retry-After', timeToExpire);
|
|
66
65
|
this.throwThrottlingException(context);
|
|
67
66
|
}
|
|
68
67
|
res.header(`${this.headerPrefix}-Limit`, limit);
|
|
69
|
-
res.header(`${this.headerPrefix}-Remaining`, Math.max(0, limit -
|
|
70
|
-
res.header(`${this.headerPrefix}-Reset`,
|
|
71
|
-
await this.storageService.addRecord(key, ttl);
|
|
68
|
+
res.header(`${this.headerPrefix}-Remaining`, Math.max(0, limit - totalHits));
|
|
69
|
+
res.header(`${this.headerPrefix}-Reset`, timeToExpire);
|
|
72
70
|
return true;
|
|
73
71
|
}
|
|
74
72
|
getTracker(req) {
|
|
@@ -86,11 +84,10 @@ let ThrottlerGuard = class ThrottlerGuard {
|
|
|
86
84
|
throw new throttler_exception_1.ThrottlerException(this.errorMessage);
|
|
87
85
|
}
|
|
88
86
|
};
|
|
89
|
-
ThrottlerGuard = __decorate([
|
|
87
|
+
exports.ThrottlerGuard = ThrottlerGuard = __decorate([
|
|
90
88
|
(0, common_1.Injectable)(),
|
|
91
89
|
__param(0, (0, throttler_decorator_1.InjectThrottlerOptions)()),
|
|
92
90
|
__param(1, (0, throttler_decorator_1.InjectThrottlerStorage)()),
|
|
93
91
|
__metadata("design:paramtypes", [Object, Object, core_1.Reflector])
|
|
94
92
|
], ThrottlerGuard);
|
|
95
|
-
exports.ThrottlerGuard = ThrottlerGuard;
|
|
96
93
|
//# sourceMappingURL=throttler.guard.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"throttler.guard.js","sourceRoot":"","sources":["../src/throttler.guard.ts"],"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C,GAAG,CAAC,CAAC,EAAE,KAAK,GAAG,SAAS,CAAC,CAAC,CAAC;QAC7E,GAAG,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,YAAY,QAAQ,EAAE,YAAY,CAAC,CAAC;QAEvD,OAAO,IAAI,CAAC;IACd,CAAC;IAES,UAAU,CAAC,GAAwB;QAC3C,OAAO,GAAG,CAAC,EAAE,CAAC;IAChB,CAAC;IAES,kBAAkB,CAAC,OAAyB;QAIpD,MAAM,IAAI,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC;QACpC,OAAO,EAAE,GAAG,EAAE,IAAI,CAAC,UAAU,EAAE,EAAE,GAAG,EAAE,IAAI,CAAC,WAAW,EAAE,EAAE,CAAC;IAC7D,CAAC;IAMS,WAAW,CAAC,OAAyB,EAAE,MAAc;QAC7D,MAAM,MAAM,GAAG,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC,IAAI,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC,IAAI,EAAE,CAAC;QACzE,OAAO,GAAG,CAAC,GAAG,MAAM,IAAI,MAAM,EAAE,CAAC,CAAC;IACpC,CAAC;IAUS,wBAAwB,CAAC,OAAyB;QAC1D,MAAM,IAAI,wCAAkB,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IAClD,CAAC;CACF,CAAA;yBAnHY,cAAc;IAD1B,IAAA,mBAAU,GAAE;IAKR,WAAA,IAAA,4CAAsB,GAAE,CAAA;IACxB,WAAA,IAAA,4CAAsB,GAAE,CAAA;qDACK,gBAAS;GAN9B,cAAc,CAmH1B"}
|
package/dist/throttler.module.js
CHANGED
|
@@ -11,7 +11,7 @@ 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 = ThrottlerModule_1 = class ThrottlerModule {
|
|
14
|
+
let ThrottlerModule = exports.ThrottlerModule = ThrottlerModule_1 = class ThrottlerModule {
|
|
15
15
|
static forRoot(options = {}) {
|
|
16
16
|
const providers = [...(0, throttler_providers_1.createThrottlerProviders)(options), throttler_providers_1.ThrottlerStorageProvider];
|
|
17
17
|
return {
|
|
@@ -56,9 +56,8 @@ let ThrottlerModule = ThrottlerModule_1 = class ThrottlerModule {
|
|
|
56
56
|
};
|
|
57
57
|
}
|
|
58
58
|
};
|
|
59
|
-
ThrottlerModule = ThrottlerModule_1 = __decorate([
|
|
59
|
+
exports.ThrottlerModule = ThrottlerModule = ThrottlerModule_1 = __decorate([
|
|
60
60
|
(0, common_1.Global)(),
|
|
61
61
|
(0, common_1.Module)({})
|
|
62
62
|
], ThrottlerModule);
|
|
63
|
-
exports.ThrottlerModule = ThrottlerModule;
|
|
64
63
|
//# sourceMappingURL=throttler.module.js.map
|
|
@@ -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":[],"mappings":";;;;;;;;;;AAAA,2CAAyE;AAMzE,+DAA0D;AAC1D,+DAA2F;AAOpF,IAAM,eAAe,iDAArB,MAAM,eAAe;IAI1B,MAAM,CAAC,OAAO,CAAC,UAAkC,EAAE;QACjD,MAAM,SAAS,GAAG,CAAC,GAAG,IAAA,8CAAwB,EAAC,OAAO,CAAC,EAAE,8CAAwB,CAAC,CAAC;QACnF,OAAO;YACL,MAAM,EAAE,iBAAe;YACvB,SAAS;YACT,OAAO,EAAE,SAAS;SACnB,CAAC;IACJ,CAAC;IAKD,MAAM,CAAC,YAAY,CAAC,OAA8B;QAChD,MAAM,SAAS,GAAG,CAAC,GAAG,IAAI,CAAC,oBAAoB,CAAC,OAAO,CAAC,EAAE,8CAAwB,CAAC,CAAC;QACpF,OAAO;YACL,MAAM,EAAE,iBAAe;YACvB,OAAO,EAAE,OAAO,CAAC,OAAO,IAAI,EAAE;YAC9B,SAAS;YACT,OAAO,EAAE,SAAS;SACnB,CAAC;IACJ,CAAC;IAEO,MAAM,CAAC,oBAAoB,CAAC,OAA8B;QAChE,IAAI,OAAO,CAAC,WAAW,IAAI,OAAO,CAAC,UAAU,EAAE;YAC7C,OAAO,CAAC,IAAI,CAAC,0BAA0B,CAAC,OAAO,CAAC,CAAC,CAAC;SACnD;QACD,OAAO;YACL,IAAI,CAAC,0BAA0B,CAAC,OAAO,CAAC;YACxC;gBACE,OAAO,EAAE,OAAO,CAAC,QAAQ;gBACzB,QAAQ,EAAE,OAAO,CAAC,QAAQ;aAC3B;SACF,CAAC;IACJ,CAAC;IAEO,MAAM,CAAC,0BAA0B,CAAC,OAA8B;QACtE,IAAI,OAAO,CAAC,UAAU,EAAE;YACtB,OAAO;gBACL,OAAO,EAAE,uCAAiB;gBAC1B,UAAU,EAAE,OAAO,CAAC,UAAU;gBAC9B,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,EAAE;aAC7B,CAAC;SACH;QACD,OAAO;YACL,OAAO,EAAE,uCAAiB;YAC1B,UAAU,EAAE,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;0BAtDY,eAAe;IAF3B,IAAA,eAAM,GAAE;IACR,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,eAAe,CAsD3B"}
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { OnApplicationShutdown } from '@nestjs/common';
|
|
2
|
+
import { ThrottlerStorageOptions } from './throttler-storage-options.interface';
|
|
3
|
+
import { ThrottlerStorageRecord } from './throttler-storage-record.interface';
|
|
2
4
|
import { ThrottlerStorage } from './throttler-storage.interface';
|
|
3
5
|
export declare class ThrottlerStorageService implements ThrottlerStorage, OnApplicationShutdown {
|
|
4
6
|
private _storage;
|
|
5
7
|
private timeoutIds;
|
|
6
|
-
get storage(): Record<string,
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
get storage(): Record<string, ThrottlerStorageOptions>;
|
|
9
|
+
private getExpirationTime;
|
|
10
|
+
private setExpirationTime;
|
|
11
|
+
increment(key: string, ttl: number): Promise<ThrottlerStorageRecord>;
|
|
9
12
|
onApplicationShutdown(): void;
|
|
10
13
|
}
|
|
@@ -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 = class ThrottlerStorageService {
|
|
11
|
+
let ThrottlerStorageService = exports.ThrottlerStorageService = class ThrottlerStorageService {
|
|
12
12
|
constructor() {
|
|
13
13
|
this._storage = {};
|
|
14
14
|
this.timeoutIds = [];
|
|
@@ -16,28 +16,39 @@ let ThrottlerStorageService = class ThrottlerStorageService {
|
|
|
16
16
|
get storage() {
|
|
17
17
|
return this._storage;
|
|
18
18
|
}
|
|
19
|
-
|
|
20
|
-
return this.storage[key]
|
|
19
|
+
getExpirationTime(key) {
|
|
20
|
+
return Math.floor((this.storage[key].expiresAt - Date.now()) / 1000);
|
|
21
21
|
}
|
|
22
|
-
|
|
23
|
-
const ttlMilliseconds = ttl * 1000;
|
|
24
|
-
if (!this.storage[key]) {
|
|
25
|
-
this.storage[key] = [];
|
|
26
|
-
}
|
|
27
|
-
this.storage[key].push(Date.now() + ttlMilliseconds);
|
|
22
|
+
setExpirationTime(key, ttlMilliseconds) {
|
|
28
23
|
const timeoutId = setTimeout(() => {
|
|
29
|
-
this.storage[key].
|
|
24
|
+
this.storage[key].totalHits--;
|
|
30
25
|
clearTimeout(timeoutId);
|
|
31
26
|
this.timeoutIds = this.timeoutIds.filter((id) => id != timeoutId);
|
|
32
27
|
}, ttlMilliseconds);
|
|
33
28
|
this.timeoutIds.push(timeoutId);
|
|
34
29
|
}
|
|
30
|
+
async increment(key, ttl) {
|
|
31
|
+
const ttlMilliseconds = ttl * 1000;
|
|
32
|
+
if (!this.storage[key]) {
|
|
33
|
+
this.storage[key] = { totalHits: 0, expiresAt: Date.now() + ttlMilliseconds };
|
|
34
|
+
}
|
|
35
|
+
let timeToExpire = this.getExpirationTime(key);
|
|
36
|
+
if (timeToExpire <= 0) {
|
|
37
|
+
this.storage[key].expiresAt = Date.now() + ttlMilliseconds;
|
|
38
|
+
timeToExpire = this.getExpirationTime(key);
|
|
39
|
+
}
|
|
40
|
+
this.storage[key].totalHits++;
|
|
41
|
+
this.setExpirationTime(key, ttlMilliseconds);
|
|
42
|
+
return {
|
|
43
|
+
totalHits: this.storage[key].totalHits,
|
|
44
|
+
timeToExpire,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
35
47
|
onApplicationShutdown() {
|
|
36
48
|
this.timeoutIds.forEach(clearTimeout);
|
|
37
49
|
}
|
|
38
50
|
};
|
|
39
|
-
ThrottlerStorageService = __decorate([
|
|
51
|
+
exports.ThrottlerStorageService = ThrottlerStorageService = __decorate([
|
|
40
52
|
(0, common_1.Injectable)()
|
|
41
53
|
], ThrottlerStorageService);
|
|
42
|
-
exports.ThrottlerStorageService = ThrottlerStorageService;
|
|
43
54
|
//# sourceMappingURL=throttler.service.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"throttler.service.js","sourceRoot":"","sources":["../src/throttler.service.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAAmE;
|
|
1
|
+
{"version":3,"file":"throttler.service.js","sourceRoot":"","sources":["../src/throttler.service.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAAmE;AAS5D,IAAM,uBAAuB,qCAA7B,MAAM,uBAAuB;IAA7B;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