not-node 6.3.23 → 6.3.24
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 +21 -15
- package/src/init/lib/rateLimiter.js +2 -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,8 @@ class Form {
|
|
|
54
55
|
};
|
|
55
56
|
|
|
56
57
|
#rateLimiter = null;
|
|
57
|
-
#rateLimiterIdGetter =
|
|
58
|
-
#rateLimiterException =
|
|
58
|
+
#rateLimiterIdGetter = (data) => data.identity.sid;
|
|
59
|
+
#rateLimiterException = FormExceptionTooManyRequests;
|
|
59
60
|
|
|
60
61
|
constructor({
|
|
61
62
|
FIELDS,
|
|
@@ -470,28 +471,33 @@ class Form {
|
|
|
470
471
|
|
|
471
472
|
#createRateLimiter(rate) {
|
|
472
473
|
if (rate) {
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
rate.
|
|
478
|
-
|
|
474
|
+
if (typeof rate.idGetter === "function") {
|
|
475
|
+
this.#rateLimiterIdGetter = rate.idGetter;
|
|
476
|
+
}
|
|
477
|
+
if (rate.exception) {
|
|
478
|
+
this.#rateLimiterException = rate.exception;
|
|
479
|
+
}
|
|
480
|
+
if (
|
|
481
|
+
rate.options &&
|
|
482
|
+
typeof rate.options == "object" &&
|
|
483
|
+
typeof rate.client === "string"
|
|
484
|
+
) {
|
|
485
|
+
this.#rateLimiter = InitRateLimiter.initCustom(
|
|
486
|
+
rate.options,
|
|
487
|
+
rate.client
|
|
488
|
+
);
|
|
489
|
+
}
|
|
479
490
|
}
|
|
480
491
|
}
|
|
481
492
|
|
|
482
493
|
async #checkRate(envs) {
|
|
483
494
|
try {
|
|
484
495
|
this.#rateLimiter &&
|
|
485
|
-
typeof this.#rateLimiterIdGetter === "function" &&
|
|
486
496
|
(await this.#rateLimiter.consume(
|
|
487
497
|
this.#rateLimiterIdGetter(envs)
|
|
488
498
|
));
|
|
489
|
-
} catch (
|
|
490
|
-
|
|
491
|
-
throw new this.#rateLimiterException(envs);
|
|
492
|
-
} else {
|
|
493
|
-
throw e;
|
|
494
|
-
}
|
|
499
|
+
} catch (_) {
|
|
500
|
+
throw new this.#rateLimiterException(envs);
|
|
495
501
|
}
|
|
496
502
|
}
|
|
497
503
|
}
|
|
@@ -18,7 +18,7 @@ module.exports = class InitRateLimiter {
|
|
|
18
18
|
.consume(req.ip)
|
|
19
19
|
.then(() => next())
|
|
20
20
|
.catch(() => {
|
|
21
|
-
log
|
|
21
|
+
log?.error("Too many requests by " + req.ip);
|
|
22
22
|
res.status(429).send("Too Many Requests");
|
|
23
23
|
});
|
|
24
24
|
};
|
|
@@ -59,7 +59,7 @@ module.exports = class InitRateLimiter {
|
|
|
59
59
|
* Returns redis client name in "db.*" of notEnv
|
|
60
60
|
*
|
|
61
61
|
* @static
|
|
62
|
-
* @param {object}
|
|
62
|
+
* @param {object} params={ config }
|
|
63
63
|
* @return {string}
|
|
64
64
|
*/
|
|
65
65
|
static getClientName({ config }) {
|