not-node 4.0.10 → 4.0.15
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/.eslintrc.json +2 -1
- package/index.js +3 -1
- package/package.json +11 -11
- package/src/common.js +15 -0
- package/src/form/fabric.js +28 -0
- package/src/form/form.js +93 -0
- package/src/form/index.js +3 -81
- package/src/init/rateLimiter.js +23 -8
- package/src/init/sessions/redis.js +7 -4
package/.eslintrc.json
CHANGED
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.
|
|
3
|
+
"version": "4.0.15",
|
|
4
4
|
"description": "node complimentary part for client side notFramework.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -53,36 +53,36 @@
|
|
|
53
53
|
"mongoose": "*",
|
|
54
54
|
"mongoose-validator": "*",
|
|
55
55
|
"nconf": "*",
|
|
56
|
-
"not-config": "^0.1.
|
|
57
|
-
"not-error": "^0.2.
|
|
56
|
+
"not-config": "^0.1.4",
|
|
57
|
+
"not-error": "^0.2.1",
|
|
58
58
|
"not-filter": "*",
|
|
59
59
|
"not-inform": "^0.0.27",
|
|
60
60
|
"not-locale": "*",
|
|
61
61
|
"not-log": "^0.0.20",
|
|
62
62
|
"not-monitor": "^0.0.13",
|
|
63
63
|
"not-path": "*",
|
|
64
|
-
"rate-limiter-flexible": "^2.3.
|
|
65
|
-
"redis": "^
|
|
64
|
+
"rate-limiter-flexible": "^2.3.6",
|
|
65
|
+
"redis": "^4.0.0",
|
|
66
66
|
"rmdir": "^1.2.0",
|
|
67
67
|
"serve-static": "*",
|
|
68
68
|
"simple-git": "*",
|
|
69
69
|
"yargs": "*"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
|
-
"@babel/eslint-parser": "^7.
|
|
72
|
+
"@babel/eslint-parser": "^7.16.3",
|
|
73
73
|
"babel-eslint": "^10.1.0",
|
|
74
74
|
"chai": "*",
|
|
75
75
|
"chai-as-promised": "*",
|
|
76
|
-
"eslint": "^
|
|
76
|
+
"eslint": "^8.3.0",
|
|
77
77
|
"eslint-plugin-node": "^11.1.0",
|
|
78
|
-
"eslint-plugin-promise": "^5.
|
|
79
|
-
"eslint-plugin-sonarjs": "^0.
|
|
78
|
+
"eslint-plugin-promise": "^5.2.0",
|
|
79
|
+
"eslint-plugin-sonarjs": "^0.11.0",
|
|
80
80
|
"ink-docstrap": "^1.3.2",
|
|
81
|
-
"ioredis": "^4.28.
|
|
81
|
+
"ioredis": "^4.28.2",
|
|
82
82
|
"jsdoc": "^3.6.7",
|
|
83
83
|
"mocha": "*",
|
|
84
84
|
"mocha-suppress-logs": "^0.3.1",
|
|
85
|
-
"mongodb-memory-server": "^
|
|
85
|
+
"mongodb-memory-server": "^8.0.4",
|
|
86
86
|
"npm-run-all": "^4.1.5",
|
|
87
87
|
"nyc": "^15.1.0",
|
|
88
88
|
"retire": "^3.0.3"
|
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'
|
|
@@ -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
|
+
};
|
package/src/form/form.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
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.FIELDS = FIELDS;
|
|
29
|
+
this.SCHEMA = byFieldsValidators(initFields(FIELDS, 'model'));
|
|
30
|
+
if (mongoose.modelNames().indexOf(FORM_NAME)===-1){
|
|
31
|
+
this.MODEL = mongoose.model(FORM_NAME, Schema(this.SCHEMA));
|
|
32
|
+
}else{
|
|
33
|
+
this.MODEL = mongoose.connection.model(FORM_NAME);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Extract data from ExpressRequest object and validates it
|
|
39
|
+
* returns it or throws
|
|
40
|
+
* @param {ExpressRequest} req expressjs request object
|
|
41
|
+
* @return {Promise<Object>} form data
|
|
42
|
+
* @throws {notValidationError}
|
|
43
|
+
**/
|
|
44
|
+
async run(req) {
|
|
45
|
+
let data = await this.extract(req);
|
|
46
|
+
await this.validate(data);
|
|
47
|
+
return data;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Extracts data, should be overriden
|
|
52
|
+
* @param {ExpressRequest} req expressjs request object
|
|
53
|
+
* @return {Object} forma data
|
|
54
|
+
**/
|
|
55
|
+
async extract( /*req*/ ) {
|
|
56
|
+
return {};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Validates form data or throws
|
|
61
|
+
* @param {Object} data form data
|
|
62
|
+
* @return {Object}
|
|
63
|
+
* @throws {notValidationError}
|
|
64
|
+
**/
|
|
65
|
+
async validate(data) {
|
|
66
|
+
try {
|
|
67
|
+
await this.MODEL.validate(data, this.FIELDS);
|
|
68
|
+
} catch (e) {
|
|
69
|
+
let fields = {};
|
|
70
|
+
if (e instanceof mongoose.Error.ValidationError) {
|
|
71
|
+
Object.keys(e.errors).forEach(name => {
|
|
72
|
+
fields[name] = [e.errors[name].message];
|
|
73
|
+
});
|
|
74
|
+
throw new notValidationError(e.message, fields, e, data);
|
|
75
|
+
} else {
|
|
76
|
+
throw new notError(
|
|
77
|
+
'core:form_validation_error', {
|
|
78
|
+
FORM_NAME: this.FORM_NAME,
|
|
79
|
+
FIELDS: this.FIELDS,
|
|
80
|
+
data
|
|
81
|
+
},
|
|
82
|
+
e
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
static fabric(){
|
|
89
|
+
return FormFabric;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
module.exports = Form;
|
package/src/form/index.js
CHANGED
|
@@ -1,82 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
};
|
package/src/init/rateLimiter.js
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
1
|
const emit = require('./additional').run;
|
|
2
2
|
const log = require('not-log')(module, 'RateLimiter');
|
|
3
|
+
const {partCopyObj} = require('../common');
|
|
4
|
+
|
|
5
|
+
const DEFAULT_OPTIONS = {
|
|
6
|
+
keyPrefix: 'rateLimiterMiddleware',
|
|
7
|
+
points: 20,
|
|
8
|
+
duration: 1
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const DEFAULT_CLIENT = 'ioredis';
|
|
3
12
|
|
|
4
13
|
module.exports = class InitRateLimiter{
|
|
5
14
|
|
|
6
15
|
static createMiddleware({rateLimiter}){
|
|
7
16
|
return (req, res, next) => {
|
|
8
17
|
rateLimiter.consume(req.ip)
|
|
9
|
-
.then(() =>
|
|
10
|
-
next();
|
|
11
|
-
})
|
|
18
|
+
.then(() => next())
|
|
12
19
|
.catch(() => {
|
|
13
20
|
log.error('Too many requests by ' + req.ip);
|
|
14
21
|
res.status(429).send('Too Many Requests');
|
|
@@ -24,13 +31,21 @@ module.exports = class InitRateLimiter{
|
|
|
24
31
|
await emit('rateLimiter.post', { config, master});
|
|
25
32
|
}
|
|
26
33
|
|
|
27
|
-
|
|
34
|
+
|
|
35
|
+
static getOptions({config}){
|
|
36
|
+
const opts = partCopyObj(config.get('modules.rateLimiter', {}), Object.keys(DEFAULT_OPTIONS));
|
|
37
|
+
return {
|
|
38
|
+
...DEFAULT_OPTIONS,
|
|
39
|
+
...opts
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
static createRateLimiter({master, config}){
|
|
28
44
|
const {RateLimiterRedis} = require('rate-limiter-flexible');
|
|
45
|
+
const storeClient = config.get('modules.rateLimiter.client', DEFAULT_CLIENT);
|
|
29
46
|
return new RateLimiterRedis({
|
|
30
|
-
storeClient: master.getEnv(
|
|
31
|
-
|
|
32
|
-
points: 100, // 10 requests
|
|
33
|
-
duration: 1, // per 1 second by IP
|
|
47
|
+
storeClient: master.getEnv(`db.${storeClient}`),
|
|
48
|
+
...InitRateLimiter.getOptions({master, config})
|
|
34
49
|
});
|
|
35
50
|
}
|
|
36
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
|
|
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
|
|
13
|
-
key: config.get('session
|
|
14
|
-
cookie: config.get('session
|
|
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({
|