not-node 6.3.23 → 6.3.25
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/package.json +1 -1
- package/src/exceptions/form.js +16 -3
- package/src/exceptions/http.js +8 -1
- package/src/form/form.js +25 -15
- package/src/init/lib/rateLimiter.js +6 -2
package/package.json
CHANGED
package/src/exceptions/form.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
const notRequestError = require("not-error/src/request.error.node.cjs");
|
|
2
|
+
const { HttpExceptionTooManyRequests } = require("./http");
|
|
2
3
|
class FormExceptionExtractorForFieldIsUndefined extends notRequestError {
|
|
3
4
|
constructor(fieldName) {
|
|
4
5
|
super("not-node:form_exception_field_extractor_is_undefined", {
|
|
5
|
-
fieldName,
|
|
6
|
+
params: { fieldName },
|
|
6
7
|
});
|
|
7
8
|
}
|
|
8
9
|
}
|
|
@@ -13,11 +14,23 @@ module.exports.FormExceptionExtractorForFieldIsUndefined =
|
|
|
13
14
|
class FormExceptionTransformerForFieldIsUndefined extends notRequestError {
|
|
14
15
|
constructor(fieldName, instruction) {
|
|
15
16
|
super("not-node:form_exception_field_transformer_is_undefined", {
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
params: {
|
|
18
|
+
fieldName,
|
|
19
|
+
instruction,
|
|
20
|
+
},
|
|
18
21
|
});
|
|
19
22
|
}
|
|
20
23
|
}
|
|
21
24
|
|
|
22
25
|
module.exports.FormExceptionTransformerForFieldIsUndefined =
|
|
23
26
|
FormExceptionTransformerForFieldIsUndefined;
|
|
27
|
+
|
|
28
|
+
class FormExceptionTooManyRequests extends HttpExceptionTooManyRequests {
|
|
29
|
+
constructor(formData) {
|
|
30
|
+
super({
|
|
31
|
+
ip: formData.identity.ip,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
module.exports.FormExceptionTooManyRequests = FormExceptionTooManyRequests;
|
package/src/exceptions/http.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const
|
|
1
|
+
const notRequestError = require("not-error/src/request.error.node.cjs");
|
|
2
2
|
|
|
3
3
|
//https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses
|
|
4
4
|
|
|
@@ -41,3 +41,10 @@ class HttpExceptionNotFound extends notRequestError {
|
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
module.exports.HttpExceptionNotFound = HttpExceptionNotFound;
|
|
44
|
+
|
|
45
|
+
class HttpExceptionTooManyRequests extends notRequestError {
|
|
46
|
+
constructor(params) {
|
|
47
|
+
super("Too many requests", { code: 429, ...params });
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
module.exports.HttpExceptionTooManyRequests = HttpExceptionTooManyRequests;
|
package/src/form/form.js
CHANGED
|
@@ -11,6 +11,7 @@ const notValidationError = require("not-error/src/validation.error.node.cjs");
|
|
|
11
11
|
const InitRateLimiter = require("../init/lib/rateLimiter");
|
|
12
12
|
|
|
13
13
|
const notError = require("not-error/src/error.node.cjs");
|
|
14
|
+
const { FormExceptionTooManyRequests } = require("../exceptions/form.js");
|
|
14
15
|
|
|
15
16
|
const {
|
|
16
17
|
FormExceptionExtractorForFieldIsUndefined,
|
|
@@ -54,8 +55,9 @@ class Form {
|
|
|
54
55
|
};
|
|
55
56
|
|
|
56
57
|
#rateLimiter = null;
|
|
57
|
-
#rateLimiterIdGetter =
|
|
58
|
-
#rateLimiterException =
|
|
58
|
+
#rateLimiterIdGetter = (data) => data.identity.sid;
|
|
59
|
+
#rateLimiterException = FormExceptionTooManyRequests;
|
|
60
|
+
#rateLimiterClientName = InitRateLimiter.DEFAULT_CLIENT;
|
|
59
61
|
|
|
60
62
|
constructor({
|
|
61
63
|
FIELDS,
|
|
@@ -470,28 +472,36 @@ class Form {
|
|
|
470
472
|
|
|
471
473
|
#createRateLimiter(rate) {
|
|
472
474
|
if (rate) {
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
rate.
|
|
478
|
-
|
|
475
|
+
if (typeof rate.idGetter === "function") {
|
|
476
|
+
this.#rateLimiterIdGetter = rate.idGetter;
|
|
477
|
+
}
|
|
478
|
+
if (rate.exception) {
|
|
479
|
+
this.#rateLimiterException = rate.exception;
|
|
480
|
+
}
|
|
481
|
+
if (rate.client && typeof rate.client === "string") {
|
|
482
|
+
this.#rateLimiterClientName = rate.client;
|
|
483
|
+
}
|
|
484
|
+
if (
|
|
485
|
+
rate.options &&
|
|
486
|
+
typeof rate.options == "object" &&
|
|
487
|
+
typeof rate.client === "string"
|
|
488
|
+
) {
|
|
489
|
+
this.#rateLimiter = InitRateLimiter.initCustom(
|
|
490
|
+
rate.options,
|
|
491
|
+
this.#rateLimiterClientName
|
|
492
|
+
);
|
|
493
|
+
}
|
|
479
494
|
}
|
|
480
495
|
}
|
|
481
496
|
|
|
482
497
|
async #checkRate(envs) {
|
|
483
498
|
try {
|
|
484
499
|
this.#rateLimiter &&
|
|
485
|
-
typeof this.#rateLimiterIdGetter === "function" &&
|
|
486
500
|
(await this.#rateLimiter.consume(
|
|
487
501
|
this.#rateLimiterIdGetter(envs)
|
|
488
502
|
));
|
|
489
|
-
} catch (
|
|
490
|
-
|
|
491
|
-
throw new this.#rateLimiterException(envs);
|
|
492
|
-
} else {
|
|
493
|
-
throw e;
|
|
494
|
-
}
|
|
503
|
+
} catch (_) {
|
|
504
|
+
throw new this.#rateLimiterException(envs);
|
|
495
505
|
}
|
|
496
506
|
}
|
|
497
507
|
}
|
|
@@ -12,13 +12,17 @@ const DEFAULT_OPTIONS = {
|
|
|
12
12
|
const DEFAULT_CLIENT = "ioredis";
|
|
13
13
|
|
|
14
14
|
module.exports = class InitRateLimiter {
|
|
15
|
+
static get DEFAULT_CLIENT() {
|
|
16
|
+
return DEFAULT_CLIENT;
|
|
17
|
+
}
|
|
18
|
+
|
|
15
19
|
static createMiddleware({ rateLimiter }) {
|
|
16
20
|
return (req, res, next) => {
|
|
17
21
|
rateLimiter
|
|
18
22
|
.consume(req.ip)
|
|
19
23
|
.then(() => next())
|
|
20
24
|
.catch(() => {
|
|
21
|
-
log
|
|
25
|
+
log?.error("Too many requests by " + req.ip);
|
|
22
26
|
res.status(429).send("Too Many Requests");
|
|
23
27
|
});
|
|
24
28
|
};
|
|
@@ -59,7 +63,7 @@ module.exports = class InitRateLimiter {
|
|
|
59
63
|
* Returns redis client name in "db.*" of notEnv
|
|
60
64
|
*
|
|
61
65
|
* @static
|
|
62
|
-
* @param {object}
|
|
66
|
+
* @param {object} params={ config }
|
|
63
67
|
* @return {string}
|
|
64
68
|
*/
|
|
65
69
|
static getClientName({ config }) {
|