not-node 6.3.21 → 6.3.23

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.21",
3
+ "version": "6.3.23",
4
4
  "description": "node complimentary part for client side notFramework.",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/env.js CHANGED
@@ -22,7 +22,7 @@ module.exports = class notEnv {
22
22
  * Setting application environment variable
23
23
  * @param {string} key name of var
24
24
  * @param {object} val value
25
- * @return {notDomain} chainable
25
+ * @return {notEnv} chainable
26
26
  */
27
27
  static setEnv(key, val) {
28
28
  ENVS[key] = val;
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,6 +8,8 @@ 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
14
 
15
15
  const {
@@ -53,6 +53,10 @@ class Form {
53
53
  ...DEFAULT_TRANSFORMERS,
54
54
  };
55
55
 
56
+ #rateLimiter = null;
57
+ #rateLimiterIdGetter = null;
58
+ #rateLimiterException = null;
59
+
56
60
  constructor({
57
61
  FIELDS,
58
62
  FORM_NAME,
@@ -62,6 +66,7 @@ class Form {
62
66
  EXTRACTORS = {},
63
67
  ENV_EXTRACTORS = {},
64
68
  TRANSFORMERS = {},
69
+ rate = null,
65
70
  }) {
66
71
  this.#FORM_NAME = FORM_NAME;
67
72
  this.#MODEL_NAME = MODEL_NAME;
@@ -72,6 +77,7 @@ class Form {
72
77
  this.#addExtractors(EXTRACTORS);
73
78
  this.#addEnvExtractors(ENV_EXTRACTORS);
74
79
  this.#addTransformers(TRANSFORMERS);
80
+ this.#createRateLimiter(rate);
75
81
  }
76
82
 
77
83
  /**
@@ -109,6 +115,7 @@ class Form {
109
115
  **/
110
116
  async run(req) {
111
117
  let data = await this.extract(req);
118
+ this.#checkRate(data);
112
119
  await this.#_validate(data);
113
120
  return data;
114
121
  }
@@ -460,6 +467,33 @@ class Form {
460
467
  this.#TRANSFORMERS = { ...this.#TRANSFORMERS, ...transformers };
461
468
  }
462
469
  }
470
+
471
+ #createRateLimiter(rate) {
472
+ if (rate) {
473
+ this.#rateLimiterIdGetter = rate.idGetter;
474
+ this.#rateLimiterException = rate.exception;
475
+ this.#rateLimiter = InitRateLimiter.initCustom(
476
+ rate.options,
477
+ rate.client
478
+ );
479
+ }
480
+ }
481
+
482
+ async #checkRate(envs) {
483
+ try {
484
+ this.#rateLimiter &&
485
+ typeof this.#rateLimiterIdGetter === "function" &&
486
+ (await this.#rateLimiter.consume(
487
+ this.#rateLimiterIdGetter(envs)
488
+ ));
489
+ } catch (e) {
490
+ if (this.#rateLimiterException) {
491
+ throw new this.#rateLimiterException(envs);
492
+ } else {
493
+ throw e;
494
+ }
495
+ }
496
+ }
463
497
  }
464
498
 
465
499
  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
  /**
@@ -1,5 +1,7 @@
1
1
  const log = require("not-log")(module, "RateLimiter");
2
2
  const { partCopyObj } = require("../../common");
3
+ const notEnv = require("../../env");
4
+ const { RateLimiterRedis } = require("rate-limiter-flexible");
3
5
 
4
6
  const DEFAULT_OPTIONS = {
5
7
  keyPrefix: "rateLimiterMiddleware",
@@ -26,7 +28,6 @@ module.exports = class InitRateLimiter {
26
28
  await emit("rateLimiter.pre", { config, master });
27
29
  const rateLimiter = InitRateLimiter.createRateLimiter({
28
30
  config,
29
- master,
30
31
  });
31
32
  const middleware = InitRateLimiter.createMiddleware({ rateLimiter });
32
33
  master.getServer().use(middleware);
@@ -44,15 +45,43 @@ module.exports = class InitRateLimiter {
44
45
  };
45
46
  }
46
47
 
47
- static createRateLimiter({ master, config }) {
48
- const { RateLimiterRedis } = require("rate-limiter-flexible");
49
- const storeClient = config.get(
50
- "modules.rateLimiter.client",
51
- DEFAULT_CLIENT
48
+ static createRateLimiter({ config }) {
49
+ const storeClient = InitRateLimiter.getClient(
50
+ InitRateLimiter.getClientName({ config })
52
51
  );
53
52
  return new RateLimiterRedis({
54
- storeClient: master.getEnv(`db.${storeClient}`),
55
- ...InitRateLimiter.getOptions({ master, config }),
53
+ storeClient,
54
+ ...InitRateLimiter.getOptions({ config }),
55
+ });
56
+ }
57
+
58
+ /**
59
+ * Returns redis client name in "db.*" of notEnv
60
+ *
61
+ * @static
62
+ * @param {object} { config }
63
+ * @return {string}
64
+ */
65
+ static getClientName({ config }) {
66
+ return config.get("modules.rateLimiter.client", DEFAULT_CLIENT);
67
+ }
68
+
69
+ static getClient(storeClient) {
70
+ return notEnv.getEnv(`db.${storeClient}`);
71
+ }
72
+
73
+ static initCustom(
74
+ options = {
75
+ keyPrefix: "rateLimiterCustom",
76
+ points: 20,
77
+ duration: 1,
78
+ },
79
+ storeName = DEFAULT_CLIENT
80
+ ) {
81
+ const storeClient = InitRateLimiter.getClient(storeName);
82
+ return new RateLimiterRedis({
83
+ storeClient,
84
+ ...options,
56
85
  });
57
86
  }
58
87
  };
@@ -71,7 +71,7 @@ class notRoute {
71
71
 
72
72
  /**
73
73
  * Select rule from available or return null
74
- * @param {import('../types').ExtendedExpressRequest} req Express Request Object
74
+ * @param {import('../types').notNodeExpressRequest} req Express Request Object
75
75
  * @return {import('../types').notRouteRule | null} rule or null
76
76
  */
77
77
  selectRule(req) {
@@ -85,7 +85,7 @@ class notRoute {
85
85
  /**
86
86
  *
87
87
  *
88
- * @param {import('../types').ExtendedExpressRequest} req
88
+ * @param {import('../types').notNodeExpressRequest} req
89
89
  * @param {import('../types').notRouteData} notRouteData
90
90
  * @memberof notRoute
91
91
  */
@@ -112,7 +112,7 @@ class notRoute {
112
112
 
113
113
  /**
114
114
  * Executes route action if such exist
115
- * @param {import('../types').ExtendedExpressRequest} req Express Request Object
115
+ * @param {import('../types').notNodeExpressRequest} req Express Request Object
116
116
  * @param {import('express').Response} res Express Response Object
117
117
  * @param {function} next
118
118
  * @return {object} result of execution or HttpError