@kewacode/guard 1.0.2 → 1.0.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.
- package/constants.d.ts +2 -0
- package/constants.d.ts.map +1 -0
- package/constants.js +5 -0
- package/constants.js.map +1 -0
- package/decorators/kewa-rate-limit.decorator.d.ts +7 -0
- package/decorators/kewa-rate-limit.decorator.d.ts.map +1 -0
- package/decorators/kewa-rate-limit.decorator.js +8 -0
- package/decorators/kewa-rate-limit.decorator.js.map +1 -0
- package/guards/kewa-rate-limit.guard.d.ts +10 -0
- package/guards/kewa-rate-limit.guard.d.ts.map +1 -0
- package/guards/kewa-rate-limit.guard.js +54 -0
- package/guards/kewa-rate-limit.guard.js.map +1 -0
- package/index.d.ts +6 -0
- package/index.d.ts.map +1 -0
- package/index.js +21 -441
- package/index.js.map +1 -0
- package/interfaces/kewa-options.interface.d.ts +6 -0
- package/interfaces/kewa-options.interface.d.ts.map +1 -0
- package/interfaces/kewa-options.interface.js +3 -0
- package/interfaces/kewa-options.interface.js.map +1 -0
- package/kewa-guard.module.d.ts +6 -0
- package/kewa-guard.module.d.ts.map +1 -0
- package/kewa-guard.module.js +44 -0
- package/kewa-guard.module.js.map +1 -0
- package/kewa-guard.service.d.ts +17 -0
- package/kewa-guard.service.d.ts.map +1 -0
- package/kewa-guard.service.js +81 -0
- package/kewa-guard.service.js.map +1 -0
- package/package.json +3 -2
- package/tsconfig.lib.tsbuildinfo +1 -0
package/constants.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../libs/kewa-guard/src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,iBAAiB,iBAAiB,CAAC"}
|
package/constants.js
ADDED
package/constants.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../libs/kewa-guard/src/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,iBAAiB,GAAG,cAAc,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const KEWA_RATE_LIMIT_KEY = "kewa_rate_limit_key";
|
|
2
|
+
export interface KewaRateLimitOptions {
|
|
3
|
+
limit: number;
|
|
4
|
+
ttl: number;
|
|
5
|
+
}
|
|
6
|
+
export declare const KewaRateLimit: (options: KewaRateLimitOptions) => import("@nestjs/common").CustomDecorator<string>;
|
|
7
|
+
//# sourceMappingURL=kewa-rate-limit.decorator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kewa-rate-limit.decorator.d.ts","sourceRoot":"","sources":["../../../../libs/kewa-guard/src/decorators/kewa-rate-limit.decorator.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,mBAAmB,wBAAwB,CAAC;AAEzD,MAAM,WAAW,oBAAoB;IACnC,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;CACb;AAED,eAAO,MAAM,aAAa,GAAI,SAAS,oBAAoB,qDAChB,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KewaRateLimit = exports.KEWA_RATE_LIMIT_KEY = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
exports.KEWA_RATE_LIMIT_KEY = 'kewa_rate_limit_key';
|
|
6
|
+
const KewaRateLimit = (options) => (0, common_1.SetMetadata)(exports.KEWA_RATE_LIMIT_KEY, options);
|
|
7
|
+
exports.KewaRateLimit = KewaRateLimit;
|
|
8
|
+
//# sourceMappingURL=kewa-rate-limit.decorator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kewa-rate-limit.decorator.js","sourceRoot":"","sources":["../../../../libs/kewa-guard/src/decorators/kewa-rate-limit.decorator.ts"],"names":[],"mappings":";;;AAAA,2CAA6C;AAEhC,QAAA,mBAAmB,GAAG,qBAAqB,CAAC;AAOlD,MAAM,aAAa,GAAG,CAAC,OAA6B,EAAE,EAAE,CAC7D,IAAA,oBAAW,EAAC,2BAAmB,EAAE,OAAO,CAAC,CAAC;AAD/B,QAAA,aAAa,iBACkB"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { CanActivate, ExecutionContext } from '@nestjs/common';
|
|
2
|
+
import { Reflector } from '@nestjs/core';
|
|
3
|
+
import { KewaGuardService } from '../kewa-guard.service';
|
|
4
|
+
export declare class KewaRateLimitGuard implements CanActivate {
|
|
5
|
+
private reflector;
|
|
6
|
+
private kewaGuardService;
|
|
7
|
+
constructor(reflector: Reflector, kewaGuardService: KewaGuardService);
|
|
8
|
+
canActivate(context: ExecutionContext): Promise<boolean>;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=kewa-rate-limit.guard.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kewa-rate-limit.guard.d.ts","sourceRoot":"","sources":["../../../../libs/kewa-guard/src/guards/kewa-rate-limit.guard.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,gBAAgB,EAIjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAMzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD,qBACa,kBAAmB,YAAW,WAAW;IAElD,OAAO,CAAC,SAAS;IACjB,OAAO,CAAC,gBAAgB;gBADhB,SAAS,EAAE,SAAS,EACpB,gBAAgB,EAAE,gBAAgB;IAGtC,WAAW,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC;CAuC/D"}
|
|
@@ -0,0 +1,54 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.KewaRateLimitGuard = void 0;
|
|
13
|
+
const common_1 = require("@nestjs/common");
|
|
14
|
+
const core_1 = require("@nestjs/core");
|
|
15
|
+
const kewa_rate_limit_decorator_1 = require("../decorators/kewa-rate-limit.decorator");
|
|
16
|
+
const kewa_guard_service_1 = require("../kewa-guard.service");
|
|
17
|
+
let KewaRateLimitGuard = class KewaRateLimitGuard {
|
|
18
|
+
reflector;
|
|
19
|
+
kewaGuardService;
|
|
20
|
+
constructor(reflector, kewaGuardService) {
|
|
21
|
+
this.reflector = reflector;
|
|
22
|
+
this.kewaGuardService = kewaGuardService;
|
|
23
|
+
}
|
|
24
|
+
async canActivate(context) {
|
|
25
|
+
const options = this.reflector.get(kewa_rate_limit_decorator_1.KEWA_RATE_LIMIT_KEY, context.getHandler());
|
|
26
|
+
if (!options) {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
const { limit, ttl } = options;
|
|
30
|
+
const request = context.switchToHttp().getRequest();
|
|
31
|
+
const response = context.switchToHttp().getResponse();
|
|
32
|
+
const ip = request.ip || request.connection.remoteAddress;
|
|
33
|
+
const key = `rate_limit:${ip}:${context.getClass().name}.${context.getHandler().name}`;
|
|
34
|
+
const { allowed, remaining, resetAt } = await this.kewaGuardService.checkLimit(key, limit, ttl);
|
|
35
|
+
response.header('X-RateLimit-Limit', limit.toString());
|
|
36
|
+
response.header('X-RateLimit-Remaining', remaining.toString());
|
|
37
|
+
response.header('X-RateLimit-Reset', resetAt.toString());
|
|
38
|
+
if (!allowed) {
|
|
39
|
+
throw new common_1.HttpException({
|
|
40
|
+
statusCode: common_1.HttpStatus.TOO_MANY_REQUESTS,
|
|
41
|
+
error: 'Too Many Requests',
|
|
42
|
+
message: `Você excedeu o limite de ${limit} requisições em ${ttl} segundos.`,
|
|
43
|
+
}, common_1.HttpStatus.TOO_MANY_REQUESTS);
|
|
44
|
+
}
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
exports.KewaRateLimitGuard = KewaRateLimitGuard;
|
|
49
|
+
exports.KewaRateLimitGuard = KewaRateLimitGuard = __decorate([
|
|
50
|
+
(0, common_1.Injectable)(),
|
|
51
|
+
__metadata("design:paramtypes", [core_1.Reflector,
|
|
52
|
+
kewa_guard_service_1.KewaGuardService])
|
|
53
|
+
], KewaRateLimitGuard);
|
|
54
|
+
//# sourceMappingURL=kewa-rate-limit.guard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kewa-rate-limit.guard.js","sourceRoot":"","sources":["../../../../libs/kewa-guard/src/guards/kewa-rate-limit.guard.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAMwB;AACxB,uCAAyC;AAEzC,uFAGiD;AACjD,8DAAyD;AAGlD,IAAM,kBAAkB,GAAxB,MAAM,kBAAkB;IAEnB;IACA;IAFV,YACU,SAAoB,EACpB,gBAAkC;QADlC,cAAS,GAAT,SAAS,CAAW;QACpB,qBAAgB,GAAhB,gBAAgB,CAAkB;IACzC,CAAC;IAEJ,KAAK,CAAC,WAAW,CAAC,OAAyB;QACzC,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAChC,+CAAmB,EACnB,OAAO,CAAC,UAAU,EAAE,CACrB,CAAC;QAEF,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,OAAO,IAAI,CAAC;QACd,CAAC;QAED,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC;QAE/B,MAAM,OAAO,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC,UAAU,EAAW,CAAC;QAC7D,MAAM,QAAQ,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC,WAAW,EAAY,CAAC;QAEhE,MAAM,EAAE,GAAG,OAAO,CAAC,EAAE,IAAI,OAAO,CAAC,UAAU,CAAC,aAAa,CAAC;QAE1D,MAAM,GAAG,GAAG,cAAc,EAAE,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC,IAAI,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC,IAAI,EAAE,CAAC;QAEvF,MAAM,EAAE,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,GACnC,MAAM,IAAI,CAAC,gBAAgB,CAAC,UAAU,CAAC,GAAG,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC;QAE1D,QAAQ,CAAC,MAAM,CAAC,mBAAmB,EAAE,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC;QACvD,QAAQ,CAAC,MAAM,CAAC,uBAAuB,EAAE,SAAS,CAAC,QAAQ,EAAE,CAAC,CAAC;QAC/D,QAAQ,CAAC,MAAM,CAAC,mBAAmB,EAAE,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC;QAEzD,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,MAAM,IAAI,sBAAa,CACrB;gBACE,UAAU,EAAE,mBAAU,CAAC,iBAAiB;gBACxC,KAAK,EAAE,mBAAmB;gBAC1B,OAAO,EAAE,4BAA4B,KAAK,mBAAmB,GAAG,YAAY;aAC7E,EACD,mBAAU,CAAC,iBAAiB,CAC7B,CAAC;QACJ,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF,CAAA;AA7CY,gDAAkB;6BAAlB,kBAAkB;IAD9B,IAAA,mBAAU,GAAE;qCAGU,gBAAS;QACF,qCAAgB;GAHjC,kBAAkB,CA6C9B"}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * from './kewa-guard.module';
|
|
2
|
+
export * from './kewa-guard.service';
|
|
3
|
+
export * from './interfaces/kewa-options.interface';
|
|
4
|
+
export * from './guards/kewa-rate-limit.guard';
|
|
5
|
+
export * from './decorators/kewa-rate-limit.decorator';
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
package/index.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../libs/kewa-guard/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,qCAAqC,CAAC;AACpD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,wCAAwC,CAAC"}
|
package/index.js
CHANGED
|
@@ -1,442 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var __createBinding =
|
|
8
|
-
(this && this.__createBinding) ||
|
|
9
|
-
(Object.create
|
|
10
|
-
? function (o, m, k, k2) {
|
|
11
|
-
if (k2 === undefined) k2 = k;
|
|
12
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
13
|
-
if (
|
|
14
|
-
!desc ||
|
|
15
|
-
('get' in desc
|
|
16
|
-
? !m.__esModule
|
|
17
|
-
: desc.writable || desc.configurable)
|
|
18
|
-
) {
|
|
19
|
-
desc = {
|
|
20
|
-
enumerable: true,
|
|
21
|
-
get: function () {
|
|
22
|
-
return m[k];
|
|
23
|
-
},
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
Object.defineProperty(o, k2, desc);
|
|
27
|
-
}
|
|
28
|
-
: function (o, m, k, k2) {
|
|
29
|
-
if (k2 === undefined) k2 = k;
|
|
30
|
-
o[k2] = m[k];
|
|
31
|
-
});
|
|
32
|
-
var __exportStar =
|
|
33
|
-
(this && this.__exportStar) ||
|
|
34
|
-
function (m, exports) {
|
|
35
|
-
for (var p in m)
|
|
36
|
-
if (
|
|
37
|
-
p !== 'default' &&
|
|
38
|
-
!Object.prototype.hasOwnProperty.call(exports, p)
|
|
39
|
-
)
|
|
40
|
-
__createBinding(exports, m, p);
|
|
41
|
-
};
|
|
42
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
43
|
-
__exportStar(__webpack_require__(1), exports);
|
|
44
|
-
__exportStar(__webpack_require__(5), exports);
|
|
45
|
-
__exportStar(__webpack_require__(6), exports);
|
|
46
|
-
__exportStar(__webpack_require__(7), exports);
|
|
47
|
-
__exportStar(__webpack_require__(9), exports);
|
|
48
|
-
|
|
49
|
-
/***/
|
|
50
|
-
},
|
|
51
|
-
/* 1 */
|
|
52
|
-
/***/ function (__unused_webpack_module, exports, __webpack_require__) {
|
|
53
|
-
var __decorate =
|
|
54
|
-
(this && this.__decorate) ||
|
|
55
|
-
function (decorators, target, key, desc) {
|
|
56
|
-
var c = arguments.length,
|
|
57
|
-
r =
|
|
58
|
-
c < 3
|
|
59
|
-
? target
|
|
60
|
-
: desc === null
|
|
61
|
-
? (desc = Object.getOwnPropertyDescriptor(target, key))
|
|
62
|
-
: desc,
|
|
63
|
-
d;
|
|
64
|
-
if (
|
|
65
|
-
typeof Reflect === 'object' &&
|
|
66
|
-
typeof Reflect.decorate === 'function'
|
|
67
|
-
)
|
|
68
|
-
r = Reflect.decorate(decorators, target, key, desc);
|
|
69
|
-
else
|
|
70
|
-
for (var i = decorators.length - 1; i >= 0; i--)
|
|
71
|
-
if ((d = decorators[i]))
|
|
72
|
-
r =
|
|
73
|
-
(c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) ||
|
|
74
|
-
r;
|
|
75
|
-
return (c > 3 && r && Object.defineProperty(target, key, r), r);
|
|
76
|
-
};
|
|
77
|
-
var __importDefault =
|
|
78
|
-
(this && this.__importDefault) ||
|
|
79
|
-
function (mod) {
|
|
80
|
-
return mod && mod.__esModule ? mod : { default: mod };
|
|
81
|
-
};
|
|
82
|
-
var KewaGuardModule_1;
|
|
83
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
84
|
-
exports.KewaGuardModule = void 0;
|
|
85
|
-
const common_1 = __webpack_require__(2);
|
|
86
|
-
const ioredis_1 = __importDefault(__webpack_require__(3));
|
|
87
|
-
const constants_1 = __webpack_require__(4);
|
|
88
|
-
const kewa_guard_service_1 = __webpack_require__(5);
|
|
89
|
-
let KewaGuardModule = (KewaGuardModule_1 = class KewaGuardModule {
|
|
90
|
-
static register(options) {
|
|
91
|
-
return {
|
|
92
|
-
module: KewaGuardModule_1,
|
|
93
|
-
providers: [
|
|
94
|
-
{
|
|
95
|
-
provide: constants_1.KEWA_REDIS_CLIENT,
|
|
96
|
-
useFactory: () => {
|
|
97
|
-
return new ioredis_1.default({
|
|
98
|
-
host: options.host,
|
|
99
|
-
port: options.port,
|
|
100
|
-
password: options.password,
|
|
101
|
-
});
|
|
102
|
-
},
|
|
103
|
-
},
|
|
104
|
-
kewa_guard_service_1.KewaGuardService,
|
|
105
|
-
],
|
|
106
|
-
exports: [kewa_guard_service_1.KewaGuardService],
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
exports.KewaGuardModule = KewaGuardModule;
|
|
111
|
-
exports.KewaGuardModule =
|
|
112
|
-
KewaGuardModule =
|
|
113
|
-
KewaGuardModule_1 =
|
|
114
|
-
__decorate(
|
|
115
|
-
[(0, common_1.Global)(), (0, common_1.Module)({})],
|
|
116
|
-
KewaGuardModule,
|
|
117
|
-
);
|
|
118
|
-
|
|
119
|
-
/***/
|
|
120
|
-
},
|
|
121
|
-
/* 2 */
|
|
122
|
-
/***/ (module) => {
|
|
123
|
-
module.exports = require('@nestjs/common');
|
|
124
|
-
|
|
125
|
-
/***/
|
|
126
|
-
},
|
|
127
|
-
/* 3 */
|
|
128
|
-
/***/ (module) => {
|
|
129
|
-
module.exports = require('ioredis');
|
|
130
|
-
|
|
131
|
-
/***/
|
|
132
|
-
},
|
|
133
|
-
/* 4 */
|
|
134
|
-
/***/ (__unused_webpack_module, exports) => {
|
|
135
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
136
|
-
exports.KEWA_REDIS_CLIENT = void 0;
|
|
137
|
-
exports.KEWA_REDIS_CLIENT = 'REDIS_CLIENT';
|
|
138
|
-
|
|
139
|
-
/***/
|
|
140
|
-
},
|
|
141
|
-
/* 5 */
|
|
142
|
-
/***/ function (__unused_webpack_module, exports, __webpack_require__) {
|
|
143
|
-
var __decorate =
|
|
144
|
-
(this && this.__decorate) ||
|
|
145
|
-
function (decorators, target, key, desc) {
|
|
146
|
-
var c = arguments.length,
|
|
147
|
-
r =
|
|
148
|
-
c < 3
|
|
149
|
-
? target
|
|
150
|
-
: desc === null
|
|
151
|
-
? (desc = Object.getOwnPropertyDescriptor(target, key))
|
|
152
|
-
: desc,
|
|
153
|
-
d;
|
|
154
|
-
if (
|
|
155
|
-
typeof Reflect === 'object' &&
|
|
156
|
-
typeof Reflect.decorate === 'function'
|
|
157
|
-
)
|
|
158
|
-
r = Reflect.decorate(decorators, target, key, desc);
|
|
159
|
-
else
|
|
160
|
-
for (var i = decorators.length - 1; i >= 0; i--)
|
|
161
|
-
if ((d = decorators[i]))
|
|
162
|
-
r =
|
|
163
|
-
(c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) ||
|
|
164
|
-
r;
|
|
165
|
-
return (c > 3 && r && Object.defineProperty(target, key, r), r);
|
|
166
|
-
};
|
|
167
|
-
var __metadata =
|
|
168
|
-
(this && this.__metadata) ||
|
|
169
|
-
function (k, v) {
|
|
170
|
-
if (
|
|
171
|
-
typeof Reflect === 'object' &&
|
|
172
|
-
typeof Reflect.metadata === 'function'
|
|
173
|
-
)
|
|
174
|
-
return Reflect.metadata(k, v);
|
|
175
|
-
};
|
|
176
|
-
var __param =
|
|
177
|
-
(this && this.__param) ||
|
|
178
|
-
function (paramIndex, decorator) {
|
|
179
|
-
return function (target, key) {
|
|
180
|
-
decorator(target, key, paramIndex);
|
|
181
|
-
};
|
|
182
|
-
};
|
|
183
|
-
var __importDefault =
|
|
184
|
-
(this && this.__importDefault) ||
|
|
185
|
-
function (mod) {
|
|
186
|
-
return mod && mod.__esModule ? mod : { default: mod };
|
|
187
|
-
};
|
|
188
|
-
var _a;
|
|
189
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
190
|
-
exports.KewaGuardService = void 0;
|
|
191
|
-
const common_1 = __webpack_require__(2);
|
|
192
|
-
const ioredis_1 = __importDefault(__webpack_require__(3));
|
|
193
|
-
const constants_1 = __webpack_require__(4);
|
|
194
|
-
let KewaGuardService = class KewaGuardService {
|
|
195
|
-
redis;
|
|
196
|
-
constructor(redis) {
|
|
197
|
-
this.redis = redis;
|
|
198
|
-
}
|
|
199
|
-
onModuleDestroy() {
|
|
200
|
-
this.redis.disconnect();
|
|
201
|
-
}
|
|
202
|
-
async checkLimit(key, limit, ttlSeconds) {
|
|
203
|
-
const now = Date.now();
|
|
204
|
-
const windowStart = now - ttlSeconds * 1000;
|
|
205
|
-
const luaScript = `
|
|
206
|
-
local key = KEYS[1]
|
|
207
|
-
local limit = tonumber(ARGV[1])
|
|
208
|
-
local now = tonumber(ARGV[2])
|
|
209
|
-
local windowStart = tonumber(ARGV[3])
|
|
210
|
-
local ttl = tonumber(ARGV[4])
|
|
211
|
-
|
|
212
|
-
redis.call('ZREMRANGEBYSCORE', key, 0, windowStart)
|
|
213
|
-
|
|
214
|
-
local currentCount = redis.call('ZCARD', key)
|
|
215
|
-
|
|
216
|
-
local allowed = 1
|
|
217
|
-
local remaining = 0
|
|
218
|
-
|
|
219
|
-
if currentCount >= limit then
|
|
220
|
-
allowed = 0
|
|
221
|
-
remaining = 0
|
|
222
|
-
else
|
|
223
|
-
redis.call('ZADD', key, now, now)
|
|
224
|
-
redis.call('EXPIRE', key, ttl)
|
|
225
|
-
allowed = 1
|
|
226
|
-
remaining = limit - (currentCount + 1)
|
|
227
|
-
end
|
|
228
|
-
|
|
229
|
-
local resetAt = math.floor((now + (ttl * 1000)) / 1000)
|
|
230
|
-
|
|
231
|
-
return { allowed, remaining, resetAt }
|
|
232
|
-
`;
|
|
233
|
-
const result = await this.redis.eval(
|
|
234
|
-
luaScript,
|
|
235
|
-
1,
|
|
236
|
-
key,
|
|
237
|
-
limit,
|
|
238
|
-
now,
|
|
239
|
-
windowStart,
|
|
240
|
-
ttlSeconds,
|
|
241
|
-
);
|
|
242
|
-
return {
|
|
243
|
-
allowed: result[0] === 1,
|
|
244
|
-
remaining: result[1],
|
|
245
|
-
resetAt: result[2],
|
|
246
|
-
};
|
|
247
|
-
}
|
|
248
|
-
async ping() {
|
|
249
|
-
return await this.redis.ping();
|
|
250
|
-
}
|
|
251
|
-
getClient() {
|
|
252
|
-
return this.redis;
|
|
253
|
-
}
|
|
254
|
-
};
|
|
255
|
-
exports.KewaGuardService = KewaGuardService;
|
|
256
|
-
exports.KewaGuardService = KewaGuardService = __decorate(
|
|
257
|
-
[
|
|
258
|
-
(0, common_1.Injectable)(),
|
|
259
|
-
__param(0, (0, common_1.Inject)(constants_1.KEWA_REDIS_CLIENT)),
|
|
260
|
-
__metadata('design:paramtypes', [
|
|
261
|
-
typeof (_a =
|
|
262
|
-
typeof ioredis_1.default !== 'undefined' && ioredis_1.default) ===
|
|
263
|
-
'function'
|
|
264
|
-
? _a
|
|
265
|
-
: Object,
|
|
266
|
-
]),
|
|
267
|
-
],
|
|
268
|
-
KewaGuardService,
|
|
269
|
-
);
|
|
270
|
-
|
|
271
|
-
/***/
|
|
272
|
-
},
|
|
273
|
-
/* 6 */
|
|
274
|
-
/***/ (__unused_webpack_module, exports) => {
|
|
275
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
276
|
-
|
|
277
|
-
/***/
|
|
278
|
-
},
|
|
279
|
-
/* 7 */
|
|
280
|
-
/***/ function (__unused_webpack_module, exports, __webpack_require__) {
|
|
281
|
-
var __decorate =
|
|
282
|
-
(this && this.__decorate) ||
|
|
283
|
-
function (decorators, target, key, desc) {
|
|
284
|
-
var c = arguments.length,
|
|
285
|
-
r =
|
|
286
|
-
c < 3
|
|
287
|
-
? target
|
|
288
|
-
: desc === null
|
|
289
|
-
? (desc = Object.getOwnPropertyDescriptor(target, key))
|
|
290
|
-
: desc,
|
|
291
|
-
d;
|
|
292
|
-
if (
|
|
293
|
-
typeof Reflect === 'object' &&
|
|
294
|
-
typeof Reflect.decorate === 'function'
|
|
295
|
-
)
|
|
296
|
-
r = Reflect.decorate(decorators, target, key, desc);
|
|
297
|
-
else
|
|
298
|
-
for (var i = decorators.length - 1; i >= 0; i--)
|
|
299
|
-
if ((d = decorators[i]))
|
|
300
|
-
r =
|
|
301
|
-
(c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) ||
|
|
302
|
-
r;
|
|
303
|
-
return (c > 3 && r && Object.defineProperty(target, key, r), r);
|
|
304
|
-
};
|
|
305
|
-
var __metadata =
|
|
306
|
-
(this && this.__metadata) ||
|
|
307
|
-
function (k, v) {
|
|
308
|
-
if (
|
|
309
|
-
typeof Reflect === 'object' &&
|
|
310
|
-
typeof Reflect.metadata === 'function'
|
|
311
|
-
)
|
|
312
|
-
return Reflect.metadata(k, v);
|
|
313
|
-
};
|
|
314
|
-
var _a, _b;
|
|
315
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
316
|
-
exports.KewaRateLimitGuard = void 0;
|
|
317
|
-
const common_1 = __webpack_require__(2);
|
|
318
|
-
const core_1 = __webpack_require__(8);
|
|
319
|
-
const kewa_rate_limit_decorator_1 = __webpack_require__(9);
|
|
320
|
-
const kewa_guard_service_1 = __webpack_require__(5);
|
|
321
|
-
let KewaRateLimitGuard = class KewaRateLimitGuard {
|
|
322
|
-
reflector;
|
|
323
|
-
kewaGuardService;
|
|
324
|
-
constructor(reflector, kewaGuardService) {
|
|
325
|
-
this.reflector = reflector;
|
|
326
|
-
this.kewaGuardService = kewaGuardService;
|
|
327
|
-
}
|
|
328
|
-
async canActivate(context) {
|
|
329
|
-
const options = this.reflector.get(
|
|
330
|
-
kewa_rate_limit_decorator_1.KEWA_RATE_LIMIT_KEY,
|
|
331
|
-
context.getHandler(),
|
|
332
|
-
);
|
|
333
|
-
if (!options) {
|
|
334
|
-
return true;
|
|
335
|
-
}
|
|
336
|
-
const { limit, ttl } = options;
|
|
337
|
-
const request = context.switchToHttp().getRequest();
|
|
338
|
-
const response = context.switchToHttp().getResponse();
|
|
339
|
-
const ip = request.ip || request.connection.remoteAddress;
|
|
340
|
-
const key = `rate_limit:${ip}:${context.getClass().name}.${context.getHandler().name}`;
|
|
341
|
-
const { allowed, remaining, resetAt } =
|
|
342
|
-
await this.kewaGuardService.checkLimit(key, limit, ttl);
|
|
343
|
-
response.header('X-RateLimit-Limit', limit.toString());
|
|
344
|
-
response.header('X-RateLimit-Remaining', remaining.toString());
|
|
345
|
-
response.header('X-RateLimit-Reset', resetAt.toString());
|
|
346
|
-
if (!allowed) {
|
|
347
|
-
throw new common_1.HttpException(
|
|
348
|
-
{
|
|
349
|
-
statusCode: common_1.HttpStatus.TOO_MANY_REQUESTS,
|
|
350
|
-
error: 'Too Many Requests',
|
|
351
|
-
message: `Você excedeu o limite de ${limit} requisições em ${ttl} segundos.`,
|
|
352
|
-
},
|
|
353
|
-
common_1.HttpStatus.TOO_MANY_REQUESTS,
|
|
354
|
-
);
|
|
355
|
-
}
|
|
356
|
-
return true;
|
|
357
|
-
}
|
|
358
|
-
};
|
|
359
|
-
exports.KewaRateLimitGuard = KewaRateLimitGuard;
|
|
360
|
-
exports.KewaRateLimitGuard = KewaRateLimitGuard = __decorate(
|
|
361
|
-
[
|
|
362
|
-
(0, common_1.Injectable)(),
|
|
363
|
-
__metadata('design:paramtypes', [
|
|
364
|
-
typeof (_a =
|
|
365
|
-
typeof core_1.Reflector !== 'undefined' && core_1.Reflector) ===
|
|
366
|
-
'function'
|
|
367
|
-
? _a
|
|
368
|
-
: Object,
|
|
369
|
-
typeof (_b =
|
|
370
|
-
typeof kewa_guard_service_1.KewaGuardService !== 'undefined' &&
|
|
371
|
-
kewa_guard_service_1.KewaGuardService) === 'function'
|
|
372
|
-
? _b
|
|
373
|
-
: Object,
|
|
374
|
-
]),
|
|
375
|
-
],
|
|
376
|
-
KewaRateLimitGuard,
|
|
377
|
-
);
|
|
378
|
-
|
|
379
|
-
/***/
|
|
380
|
-
},
|
|
381
|
-
/* 8 */
|
|
382
|
-
/***/ (module) => {
|
|
383
|
-
module.exports = require('@nestjs/core');
|
|
384
|
-
|
|
385
|
-
/***/
|
|
386
|
-
},
|
|
387
|
-
/* 9 */
|
|
388
|
-
/***/ (__unused_webpack_module, exports, __webpack_require__) => {
|
|
389
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
390
|
-
exports.KewaRateLimit = exports.KEWA_RATE_LIMIT_KEY = void 0;
|
|
391
|
-
const common_1 = __webpack_require__(2);
|
|
392
|
-
exports.KEWA_RATE_LIMIT_KEY = 'kewa_rate_limit_key';
|
|
393
|
-
const KewaRateLimit = (options) =>
|
|
394
|
-
(0, common_1.SetMetadata)(exports.KEWA_RATE_LIMIT_KEY, options);
|
|
395
|
-
exports.KewaRateLimit = KewaRateLimit;
|
|
396
|
-
|
|
397
|
-
/***/
|
|
398
|
-
},
|
|
399
|
-
/******/
|
|
400
|
-
];
|
|
401
|
-
/************************************************************************/
|
|
402
|
-
/******/ // The module cache
|
|
403
|
-
/******/ var __webpack_module_cache__ = {};
|
|
404
|
-
/******/
|
|
405
|
-
/******/ // The require function
|
|
406
|
-
/******/ function __webpack_require__(moduleId) {
|
|
407
|
-
/******/ // Check if module is in cache
|
|
408
|
-
/******/ var cachedModule = __webpack_module_cache__[moduleId];
|
|
409
|
-
/******/ if (cachedModule !== undefined) {
|
|
410
|
-
/******/ return cachedModule.exports;
|
|
411
|
-
/******/
|
|
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]; } };
|
|
412
7
|
}
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
/******/
|
|
429
|
-
/******/ // Return the exports of the module
|
|
430
|
-
/******/ return module.exports;
|
|
431
|
-
/******/
|
|
432
|
-
}
|
|
433
|
-
/******/
|
|
434
|
-
/************************************************************************/
|
|
435
|
-
/******/
|
|
436
|
-
/******/ // startup
|
|
437
|
-
/******/ // Load entry module and return exports
|
|
438
|
-
/******/ // This entry module is referenced by other modules so it can't be inlined
|
|
439
|
-
/******/ var __webpack_exports__ = __webpack_require__(0);
|
|
440
|
-
/******/
|
|
441
|
-
/******/
|
|
442
|
-
})();
|
|
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("./kewa-guard.module"), exports);
|
|
18
|
+
__exportStar(require("./kewa-guard.service"), exports);
|
|
19
|
+
__exportStar(require("./interfaces/kewa-options.interface"), exports);
|
|
20
|
+
__exportStar(require("./guards/kewa-rate-limit.guard"), exports);
|
|
21
|
+
__exportStar(require("./decorators/kewa-rate-limit.decorator"), exports);
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../libs/kewa-guard/src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,sDAAoC;AACpC,uDAAqC;AACrC,sEAAoD;AACpD,iEAA+C;AAC/C,yEAAuD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kewa-options.interface.d.ts","sourceRoot":"","sources":["../../../../libs/kewa-guard/src/interfaces/kewa-options.interface.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kewa-options.interface.js","sourceRoot":"","sources":["../../../../libs/kewa-guard/src/interfaces/kewa-options.interface.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { DynamicModule } from '@nestjs/common';
|
|
2
|
+
import { KewaGuardOptions } from './interfaces/kewa-options.interface';
|
|
3
|
+
export declare class KewaGuardModule {
|
|
4
|
+
static register(options: KewaGuardOptions): DynamicModule;
|
|
5
|
+
}
|
|
6
|
+
//# sourceMappingURL=kewa-guard.module.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kewa-guard.module.d.ts","sourceRoot":"","sources":["../../../libs/kewa-guard/src/kewa-guard.module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAkB,MAAM,gBAAgB,CAAC;AAI/D,OAAO,EAAE,gBAAgB,EAAE,MAAM,qCAAqC,CAAC;AAEvE,qBAEa,eAAe;IAC1B,MAAM,CAAC,QAAQ,CAAC,OAAO,EAAE,gBAAgB,GAAG,aAAa;CAmB1D"}
|