@nestjs/throttler 2.0.0 → 2.0.1
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/CHANGELOG.md +109 -0
- package/README.md +31 -3
- package/dist/index.js +27 -46
- package/dist/index.js.map +1 -1
- package/dist/throttler-module-options.interface.js +3 -3
- package/dist/throttler-storage.interface.js +3 -3
- package/dist/throttler.constants.js +4 -8
- package/dist/throttler.decorator.js +27 -31
- package/dist/throttler.decorator.js.map +1 -1
- package/dist/throttler.exception.js +7 -7
- package/dist/throttler.guard.js +85 -110
- package/dist/throttler.guard.js.map +1 -1
- package/dist/throttler.module.js +58 -77
- package/dist/throttler.module.js.map +1 -1
- package/dist/throttler.providers.js +18 -22
- package/dist/throttler.service.js +37 -47
- package/dist/throttler.service.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +47 -47
package/dist/throttler.module.js
CHANGED
|
@@ -1,83 +1,64 @@
|
|
|
1
|
-
|
|
2
|
-
var __decorate =
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var
|
|
6
|
-
r =
|
|
7
|
-
c < 3
|
|
8
|
-
? target
|
|
9
|
-
: desc === null
|
|
10
|
-
? (desc = Object.getOwnPropertyDescriptor(target, key))
|
|
11
|
-
: desc,
|
|
12
|
-
d;
|
|
13
|
-
if (typeof Reflect === 'object' && typeof Reflect.decorate === 'function')
|
|
14
|
-
r = Reflect.decorate(decorators, target, key, desc);
|
|
15
|
-
else
|
|
16
|
-
for (var i = decorators.length - 1; i >= 0; i--)
|
|
17
|
-
if ((d = decorators[i]))
|
|
18
|
-
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
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;
|
|
19
6
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
20
|
-
|
|
7
|
+
};
|
|
21
8
|
var ThrottlerModule_1;
|
|
22
|
-
Object.defineProperty(exports,
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
10
|
exports.ThrottlerModule = void 0;
|
|
24
|
-
const common_1 = require(
|
|
25
|
-
const throttler_constants_1 = require(
|
|
26
|
-
const throttler_providers_1 = require(
|
|
27
|
-
let ThrottlerModule =
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
providers,
|
|
36
|
-
exports: providers,
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
static forRootAsync(options) {
|
|
40
|
-
const providers = [
|
|
41
|
-
...this.createAsyncProviders(options),
|
|
42
|
-
throttler_providers_1.ThrottlerStorageProvider,
|
|
43
|
-
];
|
|
44
|
-
return {
|
|
45
|
-
module: ThrottlerModule_1,
|
|
46
|
-
imports: options.imports || [],
|
|
47
|
-
providers,
|
|
48
|
-
exports: providers,
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
static createAsyncProviders(options) {
|
|
52
|
-
if (options.useExisting || options.useFactory) {
|
|
53
|
-
return [this.createAsyncOptionsProvider(options)];
|
|
11
|
+
const common_1 = require("@nestjs/common");
|
|
12
|
+
const throttler_constants_1 = require("./throttler.constants");
|
|
13
|
+
const throttler_providers_1 = require("./throttler.providers");
|
|
14
|
+
let ThrottlerModule = ThrottlerModule_1 = class ThrottlerModule {
|
|
15
|
+
static forRoot(options = {}) {
|
|
16
|
+
const providers = [...(0, throttler_providers_1.createThrottlerProviders)(options), throttler_providers_1.ThrottlerStorageProvider];
|
|
17
|
+
return {
|
|
18
|
+
module: ThrottlerModule_1,
|
|
19
|
+
providers,
|
|
20
|
+
exports: providers,
|
|
21
|
+
};
|
|
54
22
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
static createAsyncOptionsProvider(options) {
|
|
64
|
-
if (options.useFactory) {
|
|
65
|
-
return {
|
|
66
|
-
provide: throttler_constants_1.THROTTLER_OPTIONS,
|
|
67
|
-
useFactory: options.useFactory,
|
|
68
|
-
inject: options.inject || [],
|
|
69
|
-
};
|
|
23
|
+
static forRootAsync(options) {
|
|
24
|
+
const providers = [...this.createAsyncProviders(options), throttler_providers_1.ThrottlerStorageProvider];
|
|
25
|
+
return {
|
|
26
|
+
module: ThrottlerModule_1,
|
|
27
|
+
imports: options.imports || [],
|
|
28
|
+
providers,
|
|
29
|
+
exports: providers,
|
|
30
|
+
};
|
|
70
31
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
32
|
+
static createAsyncProviders(options) {
|
|
33
|
+
if (options.useExisting || options.useFactory) {
|
|
34
|
+
return [this.createAsyncOptionsProvider(options)];
|
|
35
|
+
}
|
|
36
|
+
return [
|
|
37
|
+
this.createAsyncOptionsProvider(options),
|
|
38
|
+
{
|
|
39
|
+
provide: options.useClass,
|
|
40
|
+
useClass: options.useClass,
|
|
41
|
+
},
|
|
42
|
+
];
|
|
43
|
+
}
|
|
44
|
+
static createAsyncOptionsProvider(options) {
|
|
45
|
+
if (options.useFactory) {
|
|
46
|
+
return {
|
|
47
|
+
provide: throttler_constants_1.THROTTLER_OPTIONS,
|
|
48
|
+
useFactory: options.useFactory,
|
|
49
|
+
inject: options.inject || [],
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
provide: throttler_constants_1.THROTTLER_OPTIONS,
|
|
54
|
+
useFactory: async (optionsFactory) => await optionsFactory.createThrottlerOptions(),
|
|
55
|
+
inject: [options.useExisting || options.useClass],
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
ThrottlerModule = ThrottlerModule_1 = __decorate([
|
|
60
|
+
(0, common_1.Global)(),
|
|
61
|
+
(0, common_1.Module)({})
|
|
62
|
+
], ThrottlerModule);
|
|
82
63
|
exports.ThrottlerModule = ThrottlerModule;
|
|
83
|
-
//# sourceMappingURL=throttler.module.js.map
|
|
64
|
+
//# 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;AAO3F,IAAa,eAAe,uBAA5B,MAAa,eAAe;IAI1B,MAAM,CAAC,OAAO,CAAC,UAAkC,EAAE;QACjD,MAAM,SAAS,GAAG,CAAC,GAAG,8CAAwB,
|
|
1
|
+
{"version":3,"file":"throttler.module.js","sourceRoot":"","sources":["../src/throttler.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAAyE;AAMzE,+DAA0D;AAC1D,+DAA2F;AAO3F,IAAa,eAAe,uBAA5B,MAAa,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,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void 0;
|
|
8
|
-
const throttler_storage_interface_1 = require('./throttler-storage.interface');
|
|
9
|
-
const throttler_constants_1 = require('./throttler.constants');
|
|
10
|
-
const throttler_service_1 = require('./throttler.service');
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getStorageToken = exports.getOptionsToken = exports.ThrottlerStorageProvider = exports.createThrottlerProviders = void 0;
|
|
4
|
+
const throttler_storage_interface_1 = require("./throttler-storage.interface");
|
|
5
|
+
const throttler_constants_1 = require("./throttler.constants");
|
|
6
|
+
const throttler_service_1 = require("./throttler.service");
|
|
11
7
|
function createThrottlerProviders(options) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
8
|
+
return [
|
|
9
|
+
{
|
|
10
|
+
provide: throttler_constants_1.THROTTLER_OPTIONS,
|
|
11
|
+
useValue: options,
|
|
12
|
+
},
|
|
13
|
+
];
|
|
18
14
|
}
|
|
19
15
|
exports.createThrottlerProviders = createThrottlerProviders;
|
|
20
16
|
exports.ThrottlerStorageProvider = {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
17
|
+
provide: throttler_storage_interface_1.ThrottlerStorage,
|
|
18
|
+
useFactory: (options) => {
|
|
19
|
+
return options.storage ? options.storage : new throttler_service_1.ThrottlerStorageService();
|
|
20
|
+
},
|
|
21
|
+
inject: [throttler_constants_1.THROTTLER_OPTIONS],
|
|
26
22
|
};
|
|
27
23
|
const getOptionsToken = () => throttler_constants_1.THROTTLER_OPTIONS;
|
|
28
24
|
exports.getOptionsToken = getOptionsToken;
|
|
29
25
|
const getStorageToken = () => throttler_storage_interface_1.ThrottlerStorage;
|
|
30
26
|
exports.getStorageToken = getStorageToken;
|
|
31
|
-
//# sourceMappingURL=throttler.providers.js.map
|
|
27
|
+
//# sourceMappingURL=throttler.providers.js.map
|
|
@@ -1,53 +1,43 @@
|
|
|
1
|
-
|
|
2
|
-
var __decorate =
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var
|
|
6
|
-
r =
|
|
7
|
-
c < 3
|
|
8
|
-
? target
|
|
9
|
-
: desc === null
|
|
10
|
-
? (desc = Object.getOwnPropertyDescriptor(target, key))
|
|
11
|
-
: desc,
|
|
12
|
-
d;
|
|
13
|
-
if (typeof Reflect === 'object' && typeof Reflect.decorate === 'function')
|
|
14
|
-
r = Reflect.decorate(decorators, target, key, desc);
|
|
15
|
-
else
|
|
16
|
-
for (var i = decorators.length - 1; i >= 0; i--)
|
|
17
|
-
if ((d = decorators[i]))
|
|
18
|
-
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
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;
|
|
19
6
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
20
|
-
|
|
21
|
-
Object.defineProperty(exports,
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
9
|
exports.ThrottlerStorageService = void 0;
|
|
23
|
-
const common_1 = require(
|
|
10
|
+
const common_1 = require("@nestjs/common");
|
|
24
11
|
let ThrottlerStorageService = class ThrottlerStorageService {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
12
|
+
constructor() {
|
|
13
|
+
this._storage = {};
|
|
14
|
+
this.timeoutIds = [];
|
|
15
|
+
}
|
|
16
|
+
get storage() {
|
|
17
|
+
return this._storage;
|
|
18
|
+
}
|
|
19
|
+
async getRecord(key) {
|
|
20
|
+
return this.storage[key] || [];
|
|
21
|
+
}
|
|
22
|
+
async addRecord(key, ttl) {
|
|
23
|
+
const ttlMilliseconds = ttl * 1000;
|
|
24
|
+
if (!this.storage[key]) {
|
|
25
|
+
this.storage[key] = [];
|
|
26
|
+
}
|
|
27
|
+
this.storage[key].push(Date.now() + ttlMilliseconds);
|
|
28
|
+
const timeoutId = setTimeout(() => {
|
|
29
|
+
this.storage[key].shift();
|
|
30
|
+
clearTimeout(timeoutId);
|
|
31
|
+
this.timeoutIds = this.timeoutIds.filter((id) => id != timeoutId);
|
|
32
|
+
}, ttlMilliseconds);
|
|
33
|
+
this.timeoutIds.push(timeoutId);
|
|
34
|
+
}
|
|
35
|
+
onApplicationShutdown() {
|
|
36
|
+
this.timeoutIds.forEach(clearTimeout);
|
|
39
37
|
}
|
|
40
|
-
this.storage[key].push(Date.now() + ttlMilliseconds);
|
|
41
|
-
const timeoutId = setTimeout(() => {
|
|
42
|
-
this.storage[key].shift();
|
|
43
|
-
clearTimeout(timeoutId);
|
|
44
|
-
}, ttlMilliseconds);
|
|
45
|
-
this.timeoutIds.push(timeoutId);
|
|
46
|
-
}
|
|
47
|
-
onApplicationShutdown() {
|
|
48
|
-
this.timeoutIds.forEach(clearTimeout);
|
|
49
|
-
}
|
|
50
38
|
};
|
|
51
|
-
ThrottlerStorageService = __decorate([
|
|
39
|
+
ThrottlerStorageService = __decorate([
|
|
40
|
+
(0, common_1.Injectable)()
|
|
41
|
+
], ThrottlerStorageService);
|
|
52
42
|
exports.ThrottlerStorageService = ThrottlerStorageService;
|
|
53
|
-
//# sourceMappingURL=throttler.service.js.map
|
|
43
|
+
//# sourceMappingURL=throttler.service.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"throttler.service.js","sourceRoot":"","sources":["../src/throttler.service.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAAmE;AAInE,IAAa,uBAAuB,GAApC,MAAa,uBAAuB;IAApC;QACU,aAAQ,GAA6B,EAAE,CAAC;QACxC,eAAU,GAAqB,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"throttler.service.js","sourceRoot":"","sources":["../src/throttler.service.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAAmE;AAInE,IAAa,uBAAuB,GAApC,MAAa,uBAAuB;IAApC;QACU,aAAQ,GAA6B,EAAE,CAAC;QACxC,eAAU,GAAqB,EAAE,CAAC;IA6B5C,CAAC;IA3BC,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED,KAAK,CAAC,SAAS,CAAC,GAAW;QACzB,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC;IACjC,CAAC;IAED,KAAK,CAAC,SAAS,CAAC,GAAW,EAAE,GAAW;QACtC,MAAM,eAAe,GAAG,GAAG,GAAG,IAAI,CAAC;QACnC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;YACtB,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC;SACxB;QAED,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,eAAe,CAAC,CAAC;QAErD,MAAM,SAAS,GAAG,UAAU,CAAC,GAAG,EAAE;YAChC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC;YAC1B,YAAY,CAAC,SAAS,CAAC,CAAC;YACxB,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,IAAI,SAAS,CAAC,CAAC;QACpE,CAAC,EAA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