@wrcb/cb-common 1.0.134 → 1.0.135
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/build/middlewares/apiLimiter.js +47 -16
- package/package.json +1 -1
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
13
|
};
|
|
@@ -14,23 +23,45 @@ function createRateApiLimiter(options) {
|
|
|
14
23
|
host: redisHost,
|
|
15
24
|
port: redisPort
|
|
16
25
|
});
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
// Verificando a versão do RedisStore
|
|
27
|
+
// Para versões mais recentes (>= 3.x)
|
|
28
|
+
if (typeof rate_limit_redis_1.default === 'function') {
|
|
29
|
+
return (0, express_rate_limit_1.default)({
|
|
30
|
+
windowMs: windowMinutes * 60 * 1000,
|
|
31
|
+
max,
|
|
32
|
+
standardHeaders: true,
|
|
33
|
+
legacyHeaders: false,
|
|
34
|
+
store: new rate_limit_redis_1.default({
|
|
35
|
+
prefix,
|
|
36
|
+
client: redisClient,
|
|
37
|
+
// Se necessário para versões específicas:
|
|
38
|
+
// @ts-ignore
|
|
39
|
+
sendCommand: (...args) => __awaiter(this, void 0, void 0, function* () {
|
|
40
|
+
const [command, ...params] = args;
|
|
41
|
+
return redisClient.sendCommand(new ioredis_1.default.Command(command, params, { replyEncoding: 'utf8' }));
|
|
42
|
+
})
|
|
43
|
+
}),
|
|
44
|
+
handler: function handleRateLimitExceeded(req, res, next) {
|
|
45
|
+
next(new tooManyRequestsError_1.TooManyRequestsError(errorMessage));
|
|
28
46
|
}
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
// Para versões mais antigas
|
|
50
|
+
else {
|
|
51
|
+
return (0, express_rate_limit_1.default)({
|
|
52
|
+
windowMs: windowMinutes * 60 * 1000,
|
|
53
|
+
max,
|
|
54
|
+
standardHeaders: true,
|
|
55
|
+
legacyHeaders: false,
|
|
56
|
+
store: new rate_limit_redis_1.default({
|
|
57
|
+
prefix,
|
|
58
|
+
client: redisClient
|
|
59
|
+
}),
|
|
60
|
+
handler: function handleRateLimitExceeded(req, res, next) {
|
|
61
|
+
next(new tooManyRequestsError_1.TooManyRequestsError(errorMessage));
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
34
65
|
}
|
|
35
66
|
// store: new RedisStore({
|
|
36
67
|
// prefix,
|