@kewacode/guard 1.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.
Files changed (3) hide show
  1. package/LICENSE +7 -0
  2. package/index.js +229 -0
  3. package/package.json +18 -0
package/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2026 João Bertan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/index.js ADDED
@@ -0,0 +1,229 @@
1
+ /******/ (() => { // webpackBootstrap
2
+ /******/ "use strict";
3
+ /******/ var __webpack_modules__ = ([
4
+ /* 0 */
5
+ /***/ (function(__unused_webpack_module, exports, __webpack_require__) {
6
+
7
+
8
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
9
+ if (k2 === undefined) k2 = k;
10
+ var desc = Object.getOwnPropertyDescriptor(m, k);
11
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
12
+ desc = { enumerable: true, get: function() { return m[k]; } };
13
+ }
14
+ Object.defineProperty(o, k2, desc);
15
+ }) : (function(o, m, k, k2) {
16
+ if (k2 === undefined) k2 = k;
17
+ o[k2] = m[k];
18
+ }));
19
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
20
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
21
+ };
22
+ Object.defineProperty(exports, "__esModule", ({ value: true }));
23
+ __exportStar(__webpack_require__(1), exports);
24
+ __exportStar(__webpack_require__(5), exports);
25
+ __exportStar(__webpack_require__(6), exports);
26
+
27
+
28
+ /***/ }),
29
+ /* 1 */
30
+ /***/ (function(__unused_webpack_module, exports, __webpack_require__) {
31
+
32
+
33
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
34
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
35
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
36
+ 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;
37
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
38
+ };
39
+ var __importDefault = (this && this.__importDefault) || function (mod) {
40
+ return (mod && mod.__esModule) ? mod : { "default": mod };
41
+ };
42
+ var KewaGuardModule_1;
43
+ Object.defineProperty(exports, "__esModule", ({ value: true }));
44
+ exports.KewaGuardModule = void 0;
45
+ const common_1 = __webpack_require__(2);
46
+ const ioredis_1 = __importDefault(__webpack_require__(3));
47
+ const constants_1 = __webpack_require__(4);
48
+ const kewa_guard_service_1 = __webpack_require__(5);
49
+ let KewaGuardModule = KewaGuardModule_1 = class KewaGuardModule {
50
+ static register(options) {
51
+ return {
52
+ module: KewaGuardModule_1,
53
+ providers: [
54
+ {
55
+ provide: constants_1.KEWA_REDIS_CLIENT,
56
+ useFactory: () => {
57
+ return new ioredis_1.default({
58
+ host: options.host,
59
+ port: options.port,
60
+ password: options.password,
61
+ });
62
+ },
63
+ },
64
+ kewa_guard_service_1.KewaGuardService,
65
+ ],
66
+ exports: [kewa_guard_service_1.KewaGuardService],
67
+ };
68
+ }
69
+ };
70
+ exports.KewaGuardModule = KewaGuardModule;
71
+ exports.KewaGuardModule = KewaGuardModule = KewaGuardModule_1 = __decorate([
72
+ (0, common_1.Global)(),
73
+ (0, common_1.Module)({})
74
+ ], KewaGuardModule);
75
+
76
+
77
+ /***/ }),
78
+ /* 2 */
79
+ /***/ ((module) => {
80
+
81
+ module.exports = require("@nestjs/common");
82
+
83
+ /***/ }),
84
+ /* 3 */
85
+ /***/ ((module) => {
86
+
87
+ module.exports = require("ioredis");
88
+
89
+ /***/ }),
90
+ /* 4 */
91
+ /***/ ((__unused_webpack_module, exports) => {
92
+
93
+
94
+ Object.defineProperty(exports, "__esModule", ({ value: true }));
95
+ exports.KEWA_REDIS_CLIENT = void 0;
96
+ exports.KEWA_REDIS_CLIENT = 'REDIS_CLIENT';
97
+
98
+
99
+ /***/ }),
100
+ /* 5 */
101
+ /***/ (function(__unused_webpack_module, exports, __webpack_require__) {
102
+
103
+
104
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
105
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
106
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
107
+ 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;
108
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
109
+ };
110
+ var __metadata = (this && this.__metadata) || function (k, v) {
111
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
112
+ };
113
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
114
+ return function (target, key) { decorator(target, key, paramIndex); }
115
+ };
116
+ var __importDefault = (this && this.__importDefault) || function (mod) {
117
+ return (mod && mod.__esModule) ? mod : { "default": mod };
118
+ };
119
+ var _a;
120
+ Object.defineProperty(exports, "__esModule", ({ value: true }));
121
+ exports.KewaGuardService = void 0;
122
+ const common_1 = __webpack_require__(2);
123
+ const ioredis_1 = __importDefault(__webpack_require__(3));
124
+ const constants_1 = __webpack_require__(4);
125
+ let KewaGuardService = class KewaGuardService {
126
+ redis;
127
+ constructor(redis) {
128
+ this.redis = redis;
129
+ }
130
+ onModuleDestroy() {
131
+ this.redis.disconnect();
132
+ }
133
+ async checkLimit(key, limit, ttlSeconds) {
134
+ const now = Date.now();
135
+ const windowStart = now - ttlSeconds * 1000;
136
+ const luaScript = `
137
+ local key = KEYS[1]
138
+ local limit = tonumber(ARGV[1])
139
+ local now = tonumber(ARGV[2])
140
+ local windowStart = tonumber(ARGV[3])
141
+ local ttl = tonumber(ARGV[4])
142
+
143
+ redis.call('ZREMRANGEBYSCORE', key, 0, windowStart)
144
+
145
+ local currentCount = redis.call('ZCARD', key)
146
+
147
+ local allowed = 1
148
+ local remaining = 0
149
+
150
+ if currentCount >= limit then
151
+ allowed = 0
152
+ remaining = 0
153
+ else
154
+ redis.call('ZADD', key, now, now)
155
+ redis.call('EXPIRE', key, ttl)
156
+ allowed = 1
157
+ remaining = limit - (currentCount + 1)
158
+ end
159
+
160
+ local resetAt = math.floor((now + (ttl * 1000)) / 1000)
161
+
162
+ return { allowed, remaining, resetAt }
163
+ `;
164
+ const result = (await this.redis.eval(luaScript, 1, key, limit, now, windowStart, ttlSeconds));
165
+ return {
166
+ allowed: result[0] === 1,
167
+ remaining: result[1],
168
+ resetAt: result[2],
169
+ };
170
+ }
171
+ async ping() {
172
+ return await this.redis.ping();
173
+ }
174
+ getClient() {
175
+ return this.redis;
176
+ }
177
+ };
178
+ exports.KewaGuardService = KewaGuardService;
179
+ exports.KewaGuardService = KewaGuardService = __decorate([
180
+ (0, common_1.Injectable)(),
181
+ __param(0, (0, common_1.Inject)(constants_1.KEWA_REDIS_CLIENT)),
182
+ __metadata("design:paramtypes", [typeof (_a = typeof ioredis_1.default !== "undefined" && ioredis_1.default) === "function" ? _a : Object])
183
+ ], KewaGuardService);
184
+
185
+
186
+ /***/ }),
187
+ /* 6 */
188
+ /***/ ((__unused_webpack_module, exports) => {
189
+
190
+
191
+ Object.defineProperty(exports, "__esModule", ({ value: true }));
192
+
193
+
194
+ /***/ })
195
+ /******/ ]);
196
+ /************************************************************************/
197
+ /******/ // The module cache
198
+ /******/ var __webpack_module_cache__ = {};
199
+ /******/
200
+ /******/ // The require function
201
+ /******/ function __webpack_require__(moduleId) {
202
+ /******/ // Check if module is in cache
203
+ /******/ var cachedModule = __webpack_module_cache__[moduleId];
204
+ /******/ if (cachedModule !== undefined) {
205
+ /******/ return cachedModule.exports;
206
+ /******/ }
207
+ /******/ // Create a new module (and put it into the cache)
208
+ /******/ var module = __webpack_module_cache__[moduleId] = {
209
+ /******/ // no module.id needed
210
+ /******/ // no module.loaded needed
211
+ /******/ exports: {}
212
+ /******/ };
213
+ /******/
214
+ /******/ // Execute the module function
215
+ /******/ __webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__);
216
+ /******/
217
+ /******/ // Return the exports of the module
218
+ /******/ return module.exports;
219
+ /******/ }
220
+ /******/
221
+ /************************************************************************/
222
+ /******/
223
+ /******/ // startup
224
+ /******/ // Load entry module and return exports
225
+ /******/ // This entry module is referenced by other modules so it can't be inlined
226
+ /******/ var __webpack_exports__ = __webpack_require__(0);
227
+ /******/
228
+ /******/ })()
229
+ ;
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "@kewacode/guard",
3
+ "version": "1.0.0",
4
+ "description": "Distributed Rate Limiter for NestJS using Redis",
5
+ "author": "João Bertan",
6
+ "license": "MIT",
7
+ "peerDependencies": {
8
+ "@nestjs/common": "^10.0.0",
9
+ "@nestjs/core": "^10.0.0",
10
+ "ioredis": "5.0.0",
11
+ "rxjs": "^7.0.0"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/joaobertan/kewa-guard.git"
16
+ },
17
+ "dependencies": {}
18
+ }