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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "not-node",
3
- "version": "6.3.23",
3
+ "version": "6.3.24",
4
4
  "description": "node complimentary part for client side notFramework.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -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
- fieldName,
17
- instruction,
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;
@@ -1,4 +1,4 @@
1
- const { notRequestError } = require("not-error");
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 = null;
58
- #rateLimiterException = null;
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
- this.#rateLimiterIdGetter = rate.idGetter;
474
- this.#rateLimiterException = rate.exception;
475
- this.#rateLimiter = InitRateLimiter.initCustom(
476
- rate.options,
477
- rate.client
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 (e) {
490
- if (this.#rateLimiterException) {
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.error("Too many requests by " + req.ip);
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} { config }
62
+ * @param {object} params={ config }
63
63
  * @return {string}
64
64
  */
65
65
  static getClientName({ config }) {