not-node 6.3.22 → 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.22",
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
@@ -1,7 +1,5 @@
1
1
  const validator = require("validator");
2
-
3
2
  const FormFabric = require("./fabric");
4
-
5
3
  const { createSchemaFromFields } = require("../fields");
6
4
 
7
5
  const { objHas, isFunc, firstLetterToUpper } = require("../common");
@@ -10,7 +8,10 @@ const ValidationBuilder = require("not-validation").Builder;
10
8
  const ValidationSession = require("not-validation").Session;
11
9
 
12
10
  const notValidationError = require("not-error/src/validation.error.node.cjs");
11
+ const InitRateLimiter = require("../init/lib/rateLimiter");
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,
@@ -53,6 +54,10 @@ class Form {
53
54
  ...DEFAULT_TRANSFORMERS,
54
55
  };
55
56
 
57
+ #rateLimiter = null;
58
+ #rateLimiterIdGetter = (data) => data.identity.sid;
59
+ #rateLimiterException = FormExceptionTooManyRequests;
60
+
56
61
  constructor({
57
62
  FIELDS,
58
63
  FORM_NAME,
@@ -62,6 +67,7 @@ class Form {
62
67
  EXTRACTORS = {},
63
68
  ENV_EXTRACTORS = {},
64
69
  TRANSFORMERS = {},
70
+ rate = null,
65
71
  }) {
66
72
  this.#FORM_NAME = FORM_NAME;
67
73
  this.#MODEL_NAME = MODEL_NAME;
@@ -72,6 +78,7 @@ class Form {
72
78
  this.#addExtractors(EXTRACTORS);
73
79
  this.#addEnvExtractors(ENV_EXTRACTORS);
74
80
  this.#addTransformers(TRANSFORMERS);
81
+ this.#createRateLimiter(rate);
75
82
  }
76
83
 
77
84
  /**
@@ -109,6 +116,7 @@ class Form {
109
116
  **/
110
117
  async run(req) {
111
118
  let data = await this.extract(req);
119
+ this.#checkRate(data);
112
120
  await this.#_validate(data);
113
121
  return data;
114
122
  }
@@ -460,6 +468,38 @@ class Form {
460
468
  this.#TRANSFORMERS = { ...this.#TRANSFORMERS, ...transformers };
461
469
  }
462
470
  }
471
+
472
+ #createRateLimiter(rate) {
473
+ if (rate) {
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
+ }
490
+ }
491
+ }
492
+
493
+ async #checkRate(envs) {
494
+ try {
495
+ this.#rateLimiter &&
496
+ (await this.#rateLimiter.consume(
497
+ this.#rateLimiterIdGetter(envs)
498
+ ));
499
+ } catch (_) {
500
+ throw new this.#rateLimiterException(envs);
501
+ }
502
+ }
463
503
  }
464
504
 
465
505
  module.exports = Form;
@@ -9,8 +9,8 @@ module.exports = ({ MODULE_NAME, MODEL_NAME, actionName }) => {
9
9
  actionName
10
10
  )}Form`;
11
11
  return class extends Form {
12
- constructor({ app }) {
13
- super({ MODULE_NAME, MODEL_NAME, FIELDS, FORM_NAME, app });
12
+ constructor(params) {
13
+ super({ ...params, MODULE_NAME, MODEL_NAME, FIELDS, FORM_NAME });
14
14
  }
15
15
 
16
16
  async extract(req) {
@@ -13,8 +13,8 @@ module.exports = ({ MODULE_NAME, MODEL_NAME, actionName }) => {
13
13
  actionName
14
14
  )}Form`;
15
15
  return class extends Form {
16
- constructor({ app }) {
17
- super({ MODULE_NAME, MODEL_NAME, FIELDS, FORM_NAME, app });
16
+ constructor(params) {
17
+ super({ ...params, MODULE_NAME, MODEL_NAME, FIELDS, FORM_NAME });
18
18
  }
19
19
 
20
20
  async extract(req) {
@@ -13,8 +13,8 @@ module.exports = ({ MODULE_NAME, MODEL_NAME, actionName }) => {
13
13
  actionName
14
14
  )}Form`;
15
15
  return class extends Form {
16
- constructor({ app }) {
17
- super({ MODULE_NAME, MODEL_NAME, FIELDS, FORM_NAME, app });
16
+ constructor(params) {
17
+ super({ ...params, MODULE_NAME, MODEL_NAME, FIELDS, FORM_NAME });
18
18
  }
19
19
 
20
20
  async extract(req) {
@@ -23,8 +23,14 @@ const FactoryFormList = ({ MODULE_NAME, MODEL_NAME, actionName }) => {
23
23
  )}Form`;
24
24
 
25
25
  return class extends Form {
26
- constructor({ app }) {
27
- super({ FIELDS, FORM_NAME, app, MODULE_NAME, MODEL_NAME });
26
+ constructor(params) {
27
+ super({
28
+ ...params,
29
+ FIELDS,
30
+ FORM_NAME,
31
+ MODULE_NAME,
32
+ MODEL_NAME,
33
+ });
28
34
  }
29
35
 
30
36
  /**
@@ -23,8 +23,14 @@ const FactoryFormListAndCount = ({ MODULE_NAME, MODEL_NAME, actionName }) => {
23
23
  )}Form`;
24
24
 
25
25
  return class extends Form {
26
- constructor({ app }) {
27
- super({ FIELDS, FORM_NAME, app, MODULE_NAME, MODEL_NAME });
26
+ constructor(params) {
27
+ super({
28
+ ...params,
29
+ FIELDS,
30
+ FORM_NAME,
31
+ MODULE_NAME,
32
+ MODEL_NAME,
33
+ });
28
34
  }
29
35
 
30
36
  /**
@@ -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
  };
@@ -28,7 +28,6 @@ module.exports = class InitRateLimiter {
28
28
  await emit("rateLimiter.pre", { config, master });
29
29
  const rateLimiter = InitRateLimiter.createRateLimiter({
30
30
  config,
31
- master,
32
31
  });
33
32
  const middleware = InitRateLimiter.createMiddleware({ rateLimiter });
34
33
  master.getServer().use(middleware);
@@ -60,7 +59,7 @@ module.exports = class InitRateLimiter {
60
59
  * Returns redis client name in "db.*" of notEnv
61
60
  *
62
61
  * @static
63
- * @param {object} { config }
62
+ * @param {object} params={ config }
64
63
  * @return {string}
65
64
  */
66
65
  static getClientName({ config }) {