not-node 6.2.27 → 6.3.0

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.2.27",
3
+ "version": "6.3.0",
4
4
  "description": "node complimentary part for client side notFramework.",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/common.js CHANGED
@@ -366,3 +366,61 @@ const getValueFromEnv = (
366
366
  }
367
367
  };
368
368
  module.exports.getValueFromEnv = getValueFromEnv;
369
+
370
+ /**
371
+ * Provides shallow object signature checks
372
+ *
373
+ * @param {object} obj object to check
374
+ * @param {object} sign signature object {fieldName: someValueOfTargetType}
375
+ * @param {boolean} [strict=true] if you need exact properties as in signature, when false - properties not described in signature are ok
376
+ * @param {boolean} [typeStrict=true] compares types of properties in obj and signature
377
+ * @return {boolean} true if object structured as signature
378
+ */
379
+ const compareObjectSignatures = (
380
+ obj,
381
+ sign,
382
+ strict = true,
383
+ typeStrict = true
384
+ ) => {
385
+ const objKeys = Object.keys(obj);
386
+ const signKeys = Object.keys(sign);
387
+ const checkKey = (key) => {
388
+ if (objKeys.includes(key)) {
389
+ if (typeStrict) {
390
+ return typeof obj[key] === typeof sign[key];
391
+ } else {
392
+ return true;
393
+ }
394
+ } else {
395
+ return false;
396
+ }
397
+ };
398
+
399
+ if (strict) {
400
+ if (objKeys.length === signKeys.length) {
401
+ return signKeys.every(checkKey);
402
+ } else {
403
+ return false;
404
+ }
405
+ } else {
406
+ return signKeys.every(checkKey);
407
+ }
408
+ };
409
+
410
+ module.exports.compareObjectSignatures = compareObjectSignatures;
411
+
412
+ /**
413
+ * Returns first index of signature matching to object
414
+ *
415
+ * @param {object} obj object to match against signatures
416
+ * @param {Array<object>} signatures array of signature
417
+ * @param {boolean} [strict=true] check exact number of properties
418
+ * @param {boolean} [typeStrict=true] check exact properties types
419
+ * @return {number} -1 if nothing is found, from 0 to signatures.length-1 if some signature
420
+ */
421
+ const findSignature = (obj, signatures, strict = true, typeStrict = true) => {
422
+ return signatures.findIndex((sign) => {
423
+ return compareObjectSignatures(obj, sign, strict, typeStrict);
424
+ });
425
+ };
426
+ module.exports.findSignature = findSignature;
@@ -8,25 +8,26 @@ module.exports = class InitSecurity {
8
8
  let CSPDirectives = config.get("CSP");
9
9
  let result = {};
10
10
  Object.keys(CSPDirectives).forEach((nm) => {
11
- result[nm + "Src"] = CSPDirectives[nm].join(" ");
12
- if (["default", "connect"].includes(nm)) {
13
- result[nm + "Src"] += " " + corsLine;
11
+ result[nm] = [...CSPDirectives[nm]];
12
+ if (
13
+ Array.isArray(corsArr) &&
14
+ ["default-src", "connect-src"].includes(nm)
15
+ ) {
16
+ result[nm].push(...corsLine);
14
17
  }
15
18
  });
16
19
  return result;
17
20
  } catch (e) {
18
- Log.error(e);
21
+ Log && Log.error(e);
19
22
  return {};
20
23
  }
21
24
  }
22
25
 
23
- async run({ master, config, options }) {
26
+ async run({ master, config }) {
24
27
  //adding protection
25
28
  const helmet = require("helmet");
26
29
  const CSPDirectives = this.getCSPDirectives({
27
- options,
28
30
  config,
29
- master,
30
31
  });
31
32
  master.getServer().use(
32
33
  helmet({
@@ -1,4 +1,4 @@
1
- const { notError } = require("not-error");
1
+ const notError = require("not-error/src/error.node");
2
2
 
3
3
  class VersioningExceptionSameOldData extends notError {
4
4
  constructor() {
@@ -1,6 +1,6 @@
1
1
  /** @module Model/Increment */
2
2
 
3
- const { updateResponseSuccess } = require("./utils.js");
3
+ const { updateResponseSuccess, insertResponseSuccess } = require("./utils.js");
4
4
  const {
5
5
  IncrementExceptionIDGeneratorRebaseFailed,
6
6
  IncrementExceptionIDGenerationFailed,
@@ -25,9 +25,9 @@ let schema = null;
25
25
 
26
26
  /**
27
27
  * Returns sub-list of fields which is not contained in object
28
- * @param {Array.string} fields list of fields
28
+ * @param {Array<string>} fields list of fields
29
29
  * @param {Object} data object to filter against
30
- * @return {Array.string} sub-list of fields not contained in object
30
+ * @return {Array<string>} sub-list of fields not contained in object
31
31
  **/
32
32
  function notContainedInData(fields, data) {
33
33
  let keys = Object.keys(data);
@@ -52,7 +52,7 @@ function formId(modelName, filterFields, data) {
52
52
  module.exports.formId = formId;
53
53
  /**
54
54
  * Some drivers versions work-arounds
55
- * @param {Mongoose.Model} thisModel counter model
55
+ * @param {import('mongoose').Model} thisModel counter model
56
56
  * @param {Object} which filter object of update request
57
57
  * @param {Object} cmd command object of update request
58
58
  * @param {Object} opts options of request
@@ -76,9 +76,9 @@ function newGetNext() {
76
76
  * sub-set of all documents, which is grouped by fields (filterFields) with
77
77
  * same value
78
78
  * @param {string} modelName
79
- * @param {Array.string} filterFields list of fild names, which is used for grouping
79
+ * @param {Array<string>} filterFields list of fild names, which is used for grouping
80
80
  * @param {Object} data item data
81
- * @return {Promise.Number}
81
+ * @return {Promise<Number>}
82
82
  **/
83
83
  return async function (modelName, filterFields, data) {
84
84
  let thisModel = this;
@@ -105,7 +105,7 @@ function newGetNext() {
105
105
  upsert: true,
106
106
  };
107
107
  const res = await secureUpdate(thisModel, which, cmd, opts);
108
- if (updateResponseSuccess(res)) {
108
+ if (updateResponseSuccess(res) || insertResponseSuccess(res, 1)) {
109
109
  const doc = await thisModel.findOne({ id });
110
110
  return doc.seq;
111
111
  } else {
@@ -116,12 +116,12 @@ function newGetNext() {
116
116
 
117
117
  module.exports.newGetNext = newGetNext;
118
118
 
119
- /**
120
- * Sets new current ID for model
121
- * @param {string} modelName name of target model
122
- * @param {number} ID desired new start ID for model
123
- **/
124
119
  function newRebase() {
120
+ /**
121
+ * Sets new current ID for model
122
+ * @param {string} modelName name of target model
123
+ * @param {number} ID desired new start ID for model
124
+ **/
125
125
  return async function (modelName, ID) {
126
126
  let thisModel = this;
127
127
  let which = {
@@ -73,11 +73,6 @@ module.exports = class ModelFabricate {
73
73
 
74
74
  static enrichByFields(targetModule, options) {
75
75
  if (targetModule.enrich) {
76
- const model_name = targetModule.thisModelName;
77
- console.log(
78
- `MODEL: ${model_name}`,
79
- JSON.stringify(options, null, 4)
80
- );
81
76
  if (targetModule.enrich.validators) {
82
77
  targetModule.thisSchema = enrich.byFieldsValidators(
83
78
  targetModule.thisSchema,
@@ -198,9 +193,7 @@ module.exports = class ModelFabricate {
198
193
  if (ModelFabricate.isIgnored(targetModule)) {
199
194
  return;
200
195
  }
201
-
202
196
  options = ModelFabricate.initOptions(options, targetModule);
203
-
204
197
  const schema = ModelFabricate.extendSchema(targetModule, options);
205
198
  if (schema) {
206
199
  targetModule.mongooseSchema = schema;
@@ -211,7 +204,7 @@ module.exports = class ModelFabricate {
211
204
  mongoose
212
205
  );
213
206
  } catch (error) {
214
- log.error(error);
207
+ log && log.error(error);
215
208
  }
216
209
  }
217
210
  }
@@ -1,3 +1,29 @@
1
+ const { findSignature } = require("../common");
2
+
3
+ const INSERT_SIGNATURE = {
4
+ acknowledged: true,
5
+ modifiedCount: 0,
6
+ upsertedId: {},
7
+ upsertedCount: 1,
8
+ matchedCount: 0,
9
+ };
10
+
11
+ const SIGNATURES = {
12
+ INSERT: [INSERT_SIGNATURE],
13
+ UPDATE: [],
14
+ DELETE: [],
15
+ };
16
+
17
+ function insertResponseSuccess(res, count = 1) {
18
+ const ind = findSignature(res, SIGNATURES.INSERT);
19
+ if (ind === -1) {
20
+ return false;
21
+ }
22
+ return SIGNATURES.INSERT[ind].upsertedCount === count;
23
+ }
24
+
25
+ module.exports.insertResponseSuccess = insertResponseSuccess;
26
+
1
27
  /**
2
28
  * checking result of modification queries to ensure that changes were made
3
29
  */
@@ -65,9 +65,9 @@ class ModelVersioning {
65
65
 
66
66
  /**
67
67
  * Compares latest version in __versions list in data with data
68
- * @param {MongooseModel} ModelConstructor model of data
68
+ * @param {import('mongoose').Model} ModelConstructor model of data
69
69
  * @param {Object} data data to save
70
- * @return {boolean} if data differs from latest version
70
+ * @return {Promise<boolean>} if data differs from latest version
71
71
  */
72
72
  static async isNew(ModelConstructor, data) {
73
73
  let latestId = ModelVersioning.getLatestVersionId(data);
@@ -84,10 +84,10 @@ class ModelVersioning {
84
84
 
85
85
  /**
86
86
  * Saves current version to versions archive, updates current version versioning tags
87
- * @param {ObjectId} id current version _id (just saved version)
87
+ * @param {import('mongoose').Types.ObjectId} id current version _id (just saved version)
88
88
  * @param {Object} data data to save
89
- * @param {MongooseModel} ModelConstructor model to use
90
- * @return {Promise<MongooseDocument>} current version with updated versioning tags
89
+ * @param {import('mongoose').Model} ModelConstructor model to use
90
+ * @return {Promise<import('mongoose').Document>} current version with updated versioning tags
91
91
  */
92
92
  static async saveVersion(id, data, ModelConstructor) {
93
93
  let preservedVersionNumber = ModelVersioning.extractVersionNumber(data),
@@ -112,10 +112,10 @@ class ModelVersioning {
112
112
 
113
113
  /**
114
114
  * Saves first version. Run AFTER doing .save() on document.
115
- * @param {ObjectId} id current version _id (just saved version)
115
+ * @param {import('mongoose').Types.ObjectId} id current version _id (just saved version)
116
116
  * @param {Object} data data to save
117
- * @param {MongooseModel} ModelConstructor model to use
118
- * @return {Promise<MongooseDocument>} current version with updated versions tags
117
+ * @param {import('mongoose').Model} ModelConstructor model to use
118
+ * @return {Promise<import('mongoose').Document>} current version with updated versions tags
119
119
  */
120
120
  static async saveFirstVersion(id, data, ModelConstructor) {
121
121
  //it's not latest version, it's archived copy
@@ -138,8 +138,8 @@ class ModelVersioning {
138
138
  * Save document
139
139
  * if document is new - creates document
140
140
  * if it's updated document - updates document and versioning history
141
- * @param {MongooseModel} doc document to save
142
- * @return {Promise<MongooseDocument>} current version with updated versions tags
141
+ * @param {import('mongoose').Model} doc document to save
142
+ * @return {Promise<import('mongoose').Document>} current version with updated versions tags
143
143
  */
144
144
  static saveDiff(ModelConstructor, doc) {
145
145
  let data = toObject(doc),