@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
|
@@ -0,0 +1,44 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
+
};
|
|
11
|
+
var KewaGuardModule_1;
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.KewaGuardModule = void 0;
|
|
14
|
+
const common_1 = require("@nestjs/common");
|
|
15
|
+
const ioredis_1 = __importDefault(require("ioredis"));
|
|
16
|
+
const constants_1 = require("./constants");
|
|
17
|
+
const kewa_guard_service_1 = require("./kewa-guard.service");
|
|
18
|
+
let KewaGuardModule = KewaGuardModule_1 = class KewaGuardModule {
|
|
19
|
+
static register(options) {
|
|
20
|
+
return {
|
|
21
|
+
module: KewaGuardModule_1,
|
|
22
|
+
providers: [
|
|
23
|
+
{
|
|
24
|
+
provide: constants_1.KEWA_REDIS_CLIENT,
|
|
25
|
+
useFactory: () => {
|
|
26
|
+
return new ioredis_1.default({
|
|
27
|
+
host: options.host,
|
|
28
|
+
port: options.port,
|
|
29
|
+
password: options.password,
|
|
30
|
+
});
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
kewa_guard_service_1.KewaGuardService,
|
|
34
|
+
],
|
|
35
|
+
exports: [kewa_guard_service_1.KewaGuardService],
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
exports.KewaGuardModule = KewaGuardModule;
|
|
40
|
+
exports.KewaGuardModule = KewaGuardModule = KewaGuardModule_1 = __decorate([
|
|
41
|
+
(0, common_1.Global)(),
|
|
42
|
+
(0, common_1.Module)({})
|
|
43
|
+
], KewaGuardModule);
|
|
44
|
+
//# sourceMappingURL=kewa-guard.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kewa-guard.module.js","sourceRoot":"","sources":["../../../libs/kewa-guard/src/kewa-guard.module.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,2CAA+D;AAC/D,sDAA4B;AAC5B,2CAAgD;AAChD,6DAAwD;AAKjD,IAAM,eAAe,uBAArB,MAAM,eAAe;IAC1B,MAAM,CAAC,QAAQ,CAAC,OAAyB;QACvC,OAAO;YACL,MAAM,EAAE,iBAAe;YACvB,SAAS,EAAE;gBACT;oBACE,OAAO,EAAE,6BAAiB;oBAC1B,UAAU,EAAE,GAAG,EAAE;wBACf,OAAO,IAAI,iBAAK,CAAC;4BACf,IAAI,EAAE,OAAO,CAAC,IAAI;4BAClB,IAAI,EAAE,OAAO,CAAC,IAAI;4BAClB,QAAQ,EAAE,OAAO,CAAC,QAAQ;yBAC3B,CAAC,CAAC;oBACL,CAAC;iBACF;gBACD,qCAAgB;aACjB;YACD,OAAO,EAAE,CAAC,qCAAgB,CAAC;SAC5B,CAAC;IACJ,CAAC;CACF,CAAA;AApBY,0CAAe;0BAAf,eAAe;IAF3B,IAAA,eAAM,GAAE;IACR,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,eAAe,CAoB3B"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { OnModuleDestroy } from '@nestjs/common';
|
|
2
|
+
import Redis from 'ioredis';
|
|
3
|
+
interface RateLimitResult {
|
|
4
|
+
allowed: boolean;
|
|
5
|
+
remaining: number;
|
|
6
|
+
resetAt: number;
|
|
7
|
+
}
|
|
8
|
+
export declare class KewaGuardService implements OnModuleDestroy {
|
|
9
|
+
private readonly redis;
|
|
10
|
+
constructor(redis: Redis);
|
|
11
|
+
onModuleDestroy(): void;
|
|
12
|
+
checkLimit(key: string, limit: number, ttlSeconds: number): Promise<RateLimitResult>;
|
|
13
|
+
ping(): Promise<"PONG">;
|
|
14
|
+
getClient(): Redis;
|
|
15
|
+
}
|
|
16
|
+
export {};
|
|
17
|
+
//# sourceMappingURL=kewa-guard.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kewa-guard.service.d.ts","sourceRoot":"","sources":["../../../libs/kewa-guard/src/kewa-guard.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACrE,OAAO,KAAK,MAAM,SAAS,CAAC;AAG5B,UAAU,eAAe;IACvB,OAAO,EAAE,OAAO,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,qBACa,gBAAiB,YAAW,eAAe;IACf,OAAO,CAAC,QAAQ,CAAC,KAAK;gBAAL,KAAK,EAAE,KAAK;IAEpE,eAAe;IAIT,UAAU,CACd,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,MAAM,EACb,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,eAAe,CAAC;IAkDrB,IAAI;IAIV,SAAS;CAGV"}
|
|
@@ -0,0 +1,81 @@
|
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
15
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
16
|
+
};
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.KewaGuardService = void 0;
|
|
19
|
+
const common_1 = require("@nestjs/common");
|
|
20
|
+
const ioredis_1 = __importDefault(require("ioredis"));
|
|
21
|
+
const constants_1 = require("./constants");
|
|
22
|
+
let KewaGuardService = class KewaGuardService {
|
|
23
|
+
redis;
|
|
24
|
+
constructor(redis) {
|
|
25
|
+
this.redis = redis;
|
|
26
|
+
}
|
|
27
|
+
onModuleDestroy() {
|
|
28
|
+
this.redis.disconnect();
|
|
29
|
+
}
|
|
30
|
+
async checkLimit(key, limit, ttlSeconds) {
|
|
31
|
+
const now = Date.now();
|
|
32
|
+
const windowStart = now - ttlSeconds * 1000;
|
|
33
|
+
const luaScript = `
|
|
34
|
+
local key = KEYS[1]
|
|
35
|
+
local limit = tonumber(ARGV[1])
|
|
36
|
+
local now = tonumber(ARGV[2])
|
|
37
|
+
local windowStart = tonumber(ARGV[3])
|
|
38
|
+
local ttl = tonumber(ARGV[4])
|
|
39
|
+
|
|
40
|
+
redis.call('ZREMRANGEBYSCORE', key, 0, windowStart)
|
|
41
|
+
|
|
42
|
+
local currentCount = redis.call('ZCARD', key)
|
|
43
|
+
|
|
44
|
+
local allowed = 1
|
|
45
|
+
local remaining = 0
|
|
46
|
+
|
|
47
|
+
if currentCount >= limit then
|
|
48
|
+
allowed = 0
|
|
49
|
+
remaining = 0
|
|
50
|
+
else
|
|
51
|
+
redis.call('ZADD', key, now, now)
|
|
52
|
+
redis.call('EXPIRE', key, ttl)
|
|
53
|
+
allowed = 1
|
|
54
|
+
remaining = limit - (currentCount + 1)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
local resetAt = math.floor((now + (ttl * 1000)) / 1000)
|
|
58
|
+
|
|
59
|
+
return { allowed, remaining, resetAt }
|
|
60
|
+
`;
|
|
61
|
+
const result = (await this.redis.eval(luaScript, 1, key, limit, now, windowStart, ttlSeconds));
|
|
62
|
+
return {
|
|
63
|
+
allowed: result[0] === 1,
|
|
64
|
+
remaining: result[1],
|
|
65
|
+
resetAt: result[2],
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
async ping() {
|
|
69
|
+
return await this.redis.ping();
|
|
70
|
+
}
|
|
71
|
+
getClient() {
|
|
72
|
+
return this.redis;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
exports.KewaGuardService = KewaGuardService;
|
|
76
|
+
exports.KewaGuardService = KewaGuardService = __decorate([
|
|
77
|
+
(0, common_1.Injectable)(),
|
|
78
|
+
__param(0, (0, common_1.Inject)(constants_1.KEWA_REDIS_CLIENT)),
|
|
79
|
+
__metadata("design:paramtypes", [ioredis_1.default])
|
|
80
|
+
], KewaGuardService);
|
|
81
|
+
//# sourceMappingURL=kewa-guard.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kewa-guard.service.js","sourceRoot":"","sources":["../../../libs/kewa-guard/src/kewa-guard.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,2CAAqE;AACrE,sDAA4B;AAC5B,2CAAgD;AASzC,IAAM,gBAAgB,GAAtB,MAAM,gBAAgB;IAC6B;IAAxD,YAAwD,KAAY;QAAZ,UAAK,GAAL,KAAK,CAAO;IAAG,CAAC;IAExE,eAAe;QACb,IAAI,CAAC,KAAK,CAAC,UAAU,EAAE,CAAC;IAC1B,CAAC;IAED,KAAK,CAAC,UAAU,CACd,GAAW,EACX,KAAa,EACb,UAAkB;QAElB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACvB,MAAM,WAAW,GAAG,GAAG,GAAG,UAAU,GAAG,IAAI,CAAC;QAE5C,MAAM,SAAS,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;KA2BjB,CAAC;QAEF,MAAM,MAAM,GAAG,CAAC,MAAM,IAAI,CAAC,KAAK,CAAC,IAAI,CACnC,SAAS,EACT,CAAC,EACD,GAAG,EACH,KAAK,EACL,GAAG,EACH,WAAW,EACX,UAAU,CACX,CAA6B,CAAC;QAE/B,OAAO;YACL,OAAO,EAAE,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC;YACxB,SAAS,EAAE,MAAM,CAAC,CAAC,CAAC;YACpB,OAAO,EAAE,MAAM,CAAC,CAAC,CAAC;SACnB,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,IAAI;QACR,OAAO,MAAM,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;IACjC,CAAC;IAED,SAAS;QACP,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;CACF,CAAA;AApEY,4CAAgB;2BAAhB,gBAAgB;IAD5B,IAAA,mBAAU,GAAE;IAEE,WAAA,IAAA,eAAM,EAAC,6BAAiB,CAAC,CAAA;qCAAyB,iBAAK;GADzD,gBAAgB,CAoE5B"}
|
package/package.json
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kewacode/guard",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "Distributed Rate Limiter for NestJS using Redis",
|
|
5
5
|
"author": "João Bertan",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"types": "./index.d.ts",
|
|
8
|
+
"main": "./index.js",
|
|
8
9
|
"peerDependencies": {
|
|
9
10
|
"@nestjs/common": "^10.0.0 || ^11.0.0",
|
|
10
11
|
"@nestjs/core": "^10.0.0 || ^11.0.0",
|
|
11
|
-
"ioredis": "5.0.0",
|
|
12
|
+
"ioredis": "^5.0.0",
|
|
12
13
|
"rxjs": "^7.0.0"
|
|
13
14
|
},
|
|
14
15
|
"repository": {
|