not-node 4.0.11 → 4.0.16

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/index.js CHANGED
@@ -30,7 +30,9 @@ module.exports.Common = require('./src/common');
30
30
  /** Fields library manager */
31
31
  module.exports.Fields = require('./src/fields');
32
32
  /** Form validation template **/
33
- module.exports.Form = require('./src/form');
33
+ module.exports.Form = require('./src/form').Form;
34
+ /** Form validation template fabric **/
35
+ module.exports.FormFabric = require('./src/form').FormFabric;
34
36
  /** Application initialization procedures */
35
37
  module.exports.Init = require('./src/init').Init;
36
38
  /** Application object */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "not-node",
3
- "version": "4.0.11",
3
+ "version": "4.0.16",
4
4
  "description": "node complimentary part for client side notFramework.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -63,6 +63,7 @@
63
63
  "not-path": "*",
64
64
  "rate-limiter-flexible": "^2.3.6",
65
65
  "redis": "^4.0.0",
66
+ "rfdc": "^1.3.0",
66
67
  "rmdir": "^1.2.0",
67
68
  "serve-static": "*",
68
69
  "simple-git": "*",
@@ -73,7 +74,7 @@
73
74
  "babel-eslint": "^10.1.0",
74
75
  "chai": "*",
75
76
  "chai-as-promised": "*",
76
- "eslint": "^8.3.0",
77
+ "eslint": "^7.0.0",
77
78
  "eslint-plugin-node": "^11.1.0",
78
79
  "eslint-plugin-promise": "^5.2.0",
79
80
  "eslint-plugin-sonarjs": "^0.11.0",
package/src/common.js CHANGED
@@ -92,6 +92,21 @@ module.exports.copyObj = (obj) => {
92
92
  return JSON.parse(JSON.stringify(obj));
93
93
  };
94
94
 
95
+ /**
96
+ * Copies object to secure it from changes
97
+ * @param {object} obj original object
98
+ * @return {object} copy of object
99
+ **/
100
+ module.exports.partCopyObj = (obj, list) => {
101
+ let partObj = Object.keys(obj).reduce((prev, curr)=>{
102
+ if(list.includes(curr)){
103
+ prev[curr] = obj[curr];
104
+ }
105
+ return prev;
106
+ }, {});
107
+ return JSON.parse(JSON.stringify(partObj));
108
+ };
109
+
95
110
 
96
111
  /**
97
112
  * Test argument type to be 'function'
@@ -1,3 +1,4 @@
1
+ const clone = require('rfdc')();
1
2
  const fs = require('fs');
2
3
  const path = require('path');
3
4
  const {objHas, tryFile} = require('../common');
@@ -82,7 +83,7 @@ module.exports.initField = (field, resultOnly = true, type = 'ui') => {
82
83
  destName = srcName = field;
83
84
  }
84
85
  let proto = (objHas(FIELDS, srcName) && objHas(FIELDS[srcName], type)) ? FIELDS[srcName][type]:{};
85
- let result = Object.assign({}, proto, mutation);
86
+ let result = Object.assign({}, clone(proto), mutation);
86
87
  if (resultOnly) {
87
88
  return result;
88
89
  } else {
@@ -0,0 +1,28 @@
1
+ const Form = require('./form');
2
+
3
+ module.exports = class FormFabric {
4
+ static create({
5
+ FIELDS,
6
+ MODULE_NAME,
7
+ FORM_NAME,
8
+ extractor
9
+ }) {
10
+ return class extends Form {
11
+ constructor() {
12
+ super({
13
+ FIELDS,
14
+ FORM_NAME: `${MODULE_NAME}:${FORM_NAME}`
15
+ });
16
+ }
17
+
18
+ /**
19
+ * Extracts data
20
+ * @param {ExpressRequest} req expressjs request object
21
+ * @return {Object} forma data
22
+ **/
23
+ extract(req) {
24
+ return extractor(req);
25
+ }
26
+ };
27
+ }
28
+ };
@@ -0,0 +1,105 @@
1
+ //DB related validation tools
2
+ const mongoose = require('mongoose');
3
+ const Schema = mongoose.Schema;
4
+ //not-node
5
+ const initFields = require('../fields').initFields;
6
+
7
+ const FormFabric = require('./fabric');
8
+
9
+ const {
10
+ byFieldsValidators
11
+ } = require('../model/enrich');
12
+
13
+ const {
14
+ notValidationError,
15
+ notError
16
+ } = require('not-error');
17
+
18
+
19
+ /**
20
+ * Generic form validation class
21
+ **/
22
+ class Form {
23
+ constructor({
24
+ FIELDS,
25
+ FORM_NAME
26
+ }) {
27
+ this.FORM_NAME = FORM_NAME;
28
+ this.PROTO_FIELDS = FIELDS;
29
+ if (mongoose.modelNames().indexOf(FORM_NAME)===-1){
30
+ this.SCHEMA = byFieldsValidators(initFields(this.PROTO_FIELDS, 'model'));
31
+ this.MODEL = mongoose.model(FORM_NAME, Schema(this.SCHEMA));
32
+ }else{
33
+ this.MODEL = mongoose.connection.model(FORM_NAME);
34
+ this.SCHEMA = this.MODEL.schema;
35
+ }
36
+ }
37
+
38
+ getFields(){
39
+ return Object.keys(this.SCHEMA);
40
+ }
41
+
42
+ /**
43
+ * Extract data from ExpressRequest object and validates it
44
+ * returns it or throws
45
+ * @param {ExpressRequest} req expressjs request object
46
+ * @return {Promise<Object>} form data
47
+ * @throws {notValidationError}
48
+ **/
49
+ async run(req) {
50
+ let data = await this.extract(req);
51
+ await this._validate(data);
52
+ return data;
53
+ }
54
+
55
+ /**
56
+ * Extracts data, should be overriden
57
+ * @param {ExpressRequest} req expressjs request object
58
+ * @return {Object} forma data
59
+ **/
60
+ async extract( /*req*/ ) {
61
+ return {};
62
+ }
63
+
64
+ /**
65
+ * Validates form data or throws
66
+ * @param {Object} data form data
67
+ * @return {Object}
68
+ * @throws {notValidationError}
69
+ **/
70
+ async _validate(data) {
71
+ try {
72
+ await this.validate(data);
73
+ } catch (e) {
74
+ let fields = {};
75
+ if (e instanceof mongoose.Error.ValidationError) {
76
+ Object.keys(e.errors).forEach(name => {
77
+ fields[name] = [e.errors[name].message];
78
+ });
79
+ throw new notValidationError(e.message, fields, e, data);
80
+ } else if (e instanceof notValidationError){
81
+ throw e;
82
+ }else {
83
+ throw new notError(
84
+ 'core:form_validation_error', {
85
+ FORM_NAME: this.FORM_NAME,
86
+ PROTO_FIELDS: this.PROTO_FIELDS,
87
+ FORM_FIELDS: this.getFields(),
88
+ data
89
+ },
90
+ e
91
+ );
92
+ }
93
+ }
94
+ }
95
+
96
+ async validate(data){
97
+ await this.MODEL.validate(data, this.getFields());
98
+ }
99
+
100
+ static fabric(){
101
+ return FormFabric;
102
+ }
103
+ }
104
+
105
+ module.exports = Form;
package/src/form/index.js CHANGED
@@ -1,82 +1,4 @@
1
- //DB related validation tools
2
- const mongoose = require('mongoose');
3
- const Schema = mongoose.Schema;
4
- //not-node
5
- const initFields = require('../fields').initFields;
6
- const {
7
- byFieldsValidators
8
- } = require('../model/enrich');
9
- const {
10
- notValidationError,
11
- notError
12
- } = require('not-error');
13
-
14
- /**
15
- * Generic form validation class
16
- **/
17
- module.exports = class Form {
18
- constructor({
19
- FIELDS,
20
- FORM_NAME
21
- }) {
22
- this.FORM_NAME = FORM_NAME;
23
- this.FIELDS = FIELDS;
24
- this.SCHEMA = byFieldsValidators(initFields(FIELDS, 'model'));
25
- if (mongoose.modelNames().indexOf(FORM_NAME)===-1){
26
- this.MODEL = mongoose.model(FORM_NAME, Schema(this.SCHEMA));
27
- }else{
28
- this.MODEL = mongoose.connection.model(FORM_NAME);
29
- }
30
- }
31
-
32
- /**
33
- * Extract data from ExpressRequest object and validates it
34
- * returns it or throws
35
- * @param {ExpressRequest} req expressjs request object
36
- * @return {Promise<Object>} form data
37
- * @throws {notValidationError}
38
- **/
39
- async run(req) {
40
- let data = await this.extract(req);
41
- await this.validate(data);
42
- return data;
43
- }
44
-
45
- /**
46
- * Extracts data, should be overriden
47
- * @param {ExpressRequest} req expressjs request object
48
- * @return {Object} forma data
49
- **/
50
- async extract( /*req*/ ) {
51
- return {};
52
- }
53
-
54
- /**
55
- * Validates form data or throws
56
- * @param {Object} data form data
57
- * @return {Object}
58
- * @throws {notValidationError}
59
- **/
60
- async validate(data) {
61
- try {
62
- await this.MODEL.validate(data, this.FIELDS);
63
- } catch (e) {
64
- let fields = {};
65
- if (e instanceof mongoose.Error.ValidationError) {
66
- Object.keys(e.errors).forEach(name => {
67
- fields[name] = [e.errors[name].message];
68
- });
69
- throw new notValidationError(e.message, fields, e, data);
70
- } else {
71
- throw new notError(
72
- 'core:form_validation_error', {
73
- FORM_NAME: this.FORM_NAME,
74
- FIELDS: this.FIELDS,
75
- data
76
- },
77
- e
78
- );
79
- }
80
- }
81
- }
1
+ module.exports = {
2
+ Form: require('./form'),
3
+ FormFabric: require('./fabric'),
82
4
  };
@@ -1,5 +1,6 @@
1
1
  const emit = require('./additional').run;
2
2
  const log = require('not-log')(module, 'RateLimiter');
3
+ const {partCopyObj} = require('../common');
3
4
 
4
5
  const DEFAULT_OPTIONS = {
5
6
  keyPrefix: 'rateLimiterMiddleware',
@@ -7,6 +8,8 @@ const DEFAULT_OPTIONS = {
7
8
  duration: 1
8
9
  };
9
10
 
11
+ const DEFAULT_CLIENT = 'ioredis';
12
+
10
13
  module.exports = class InitRateLimiter{
11
14
 
12
15
  static createMiddleware({rateLimiter}){
@@ -29,18 +32,20 @@ module.exports = class InitRateLimiter{
29
32
  }
30
33
 
31
34
 
32
- getOptions({config}){
35
+ static getOptions({config}){
36
+ const opts = partCopyObj(config.get('modules.rateLimiter', {}), Object.keys(DEFAULT_OPTIONS));
33
37
  return {
34
38
  ...DEFAULT_OPTIONS,
35
- ...config.get('modules.rateLimiter', {})
39
+ ...opts
36
40
  };
37
41
  }
38
42
 
39
43
  static createRateLimiter({master, config}){
40
44
  const {RateLimiterRedis} = require('rate-limiter-flexible');
45
+ const storeClient = config.get('modules.rateLimiter.client', DEFAULT_CLIENT);
41
46
  return new RateLimiterRedis({
42
- storeClient: master.getEnv('db.redis'),
43
- ...this.getOptions({master, config})
47
+ storeClient: master.getEnv(`db.${storeClient}`),
48
+ ...InitRateLimiter.getOptions({master, config})
44
49
  });
45
50
  }
46
51
  };
@@ -1,17 +1,20 @@
1
1
  const log = require('not-log')(module, 'not-node//init');
2
2
  const ADDS = require('../additional');
3
3
 
4
+ const DEFAULT_CLIENT = 'ioredis';
5
+
4
6
  module.exports = class InitSessionsRedis{
5
7
  async run({config, options, master}) {
6
8
  log.info('Setting up user sessions handler(redis)...');
7
9
  await ADDS.run('sessions.pre', {config, options, master});
8
10
  const expressSession = require('express-session');
9
- const redisClient = master.getEnv('db.redis');
11
+ const storeClient = config.get('session.client', DEFAULT_CLIENT);
12
+ const redisClient = master.getEnv(`db.${storeClient}`);
10
13
  const redisStore = require('connect-redis')(expressSession);
11
14
  master.getServer().use(expressSession({
12
- secret: config.get('session:secret'),
13
- key: config.get('session:key'),
14
- cookie: config.get('session:cookie'),
15
+ secret: config.get('session.secret'),
16
+ key: config.get('session.key'),
17
+ cookie: config.get('session.cookie'),
15
18
  resave: false,
16
19
  saveUninitialized: true,
17
20
  store: new redisStore({
@@ -1,7 +1,7 @@
1
1
  /** @module Model/Enrich */
2
2
 
3
3
  const Schema = require('mongoose').Schema,
4
- firstLetterToLower = require('../common').firstLetterToLower,
4
+ {firstLetterToLower,isFunc} = require('../common'),
5
5
  buildValidator = require('./buildValidator');
6
6
 
7
7
  class ModelEnricher{
@@ -49,7 +49,7 @@ class ModelEnricher{
49
49
  mongooseSchema.statics.__incModel = modelName;
50
50
  if(options && options.filter){
51
51
  mongooseSchema.statics.__incFilter = options.filter;
52
- }
52
+ }
53
53
  return mongooseSchema;
54
54
  }
55
55
 
@@ -61,7 +61,7 @@ class ModelEnricher{
61
61
  static byFieldsValidators (mongooseSchema) {
62
62
  if (mongooseSchema) {
63
63
  for (let fieldName in mongooseSchema) {
64
- if (Object.prototype.hasOwnProperty.call(mongooseSchema[fieldName], 'validate')) {
64
+ if (Object.prototype.hasOwnProperty.call(mongooseSchema[fieldName], 'validate') && mongooseSchema[fieldName].validate.length && !isFunc(mongooseSchema[fieldName].validate[0])) {
65
65
  mongooseSchema[fieldName].validate = buildValidator(mongooseSchema[fieldName].validate);
66
66
  }
67
67
  }