not-node 6.5.17 → 6.5.20

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/.husky/pre-commit CHANGED
@@ -1,4 +1 @@
1
- #!/usr/bin/env sh
2
- . "$(dirname -- "$0")/_/husky.sh"
3
-
4
1
  npm test
package/index.js CHANGED
@@ -37,6 +37,8 @@ module.exports.Enrich = require("./src/model/enrich");
37
37
  module.exports.Routine = require("./src/model/routine");
38
38
  /** Common functions */
39
39
  module.exports.Common = require("./src/common");
40
+ /** Task Runner */
41
+ module.exports.TaskRunner = require("./src/task.runner.js");
40
42
  /** Fields library manager */
41
43
  module.exports.Fields = require("./src/fields");
42
44
  /** Application generic helpers */
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "not-node",
3
- "version": "6.5.17",
3
+ "version": "6.5.20",
4
4
  "description": "node complimentary part for client side notFramework.",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "test": "mocha --require mocha-suppress-logs --reporter spec --timeout 12000",
7
+ "test": "mocha --exit --require mocha-suppress-logs --reporter spec --timeout 12000",
8
8
  "lint": "eslint ./src --fix",
9
9
  "pretest": "eslint ./src",
10
10
  "docs": "jsdoc -c jsdoc.json",
@@ -24,7 +24,7 @@ function getOwnerId(data, ownerFieldName = CONST.DOCUMENT_OWNER_FIELD_NAME) {
24
24
  /**
25
25
  * Check if data is belongs to user
26
26
  * @param {Object} data object
27
- * @param {import('mongoose').Schema.Types.ObjectId} user_id possible owner
27
+ * @param {import('mongoose').Schema.Types.ObjectId|string} user_id possible owner
28
28
  * @return {boolean} true - belongs, false - not belongs
29
29
  **/
30
30
 
package/src/domain.js CHANGED
@@ -59,7 +59,6 @@ class notDomain extends EventEmitter {
59
59
  constructor(options) {
60
60
  super();
61
61
  this.#options = options;
62
-
63
62
  return this;
64
63
  }
65
64
 
@@ -138,8 +137,8 @@ class notDomain extends EventEmitter {
138
137
 
139
138
  /**
140
139
  * Returns route
141
- * @param {string} name 'moduleName//routeName//functionName' ('not-user//user//add')
142
- * @return {function} route
140
+ * @param {string} name 'moduleName//routeName//functionName' ('not-user//user//add')
141
+ * @return {function} route
143
142
  **/
144
143
  getRoute(name) {
145
144
  if (name.indexOf("//") > 0) {
@@ -25,14 +25,14 @@ module.exports = ({ getLogic, before, after }) => {
25
25
  );
26
26
  }
27
27
  if (this.rootAsUser) {
28
- return await getLogic().createOwn(prepared);
28
+ return await getLogic(prepared).createOwn(prepared);
29
29
  } else {
30
- return await getLogic().create(prepared);
30
+ return await getLogic(prepared).create(prepared);
31
31
  }
32
32
  }
33
33
 
34
34
  static async create(req, res, next, prepared) {
35
- return await getLogic().createOwn(prepared);
35
+ return await getLogic(prepared).createOwn(prepared);
36
36
  }
37
37
 
38
38
  static async _get(req, res, next, prepared) {
@@ -42,14 +42,14 @@ module.exports = ({ getLogic, before, after }) => {
42
42
  );
43
43
  }
44
44
  if (this.rootAsUser) {
45
- return await getLogic().getOwn(prepared);
45
+ return await getLogic(prepared).getOwn(prepared);
46
46
  } else {
47
- return await getLogic().get(prepared);
47
+ return await getLogic(prepared).get(prepared);
48
48
  }
49
49
  }
50
50
 
51
51
  static async get(req, res, next, prepared) {
52
- return await getLogic().getOwn(prepared);
52
+ return await getLogic(prepared).getOwn(prepared);
53
53
  }
54
54
 
55
55
  static async _getByID(req, res, next, prepared) {
@@ -59,14 +59,14 @@ module.exports = ({ getLogic, before, after }) => {
59
59
  );
60
60
  }
61
61
  if (this.rootAsUser) {
62
- return await getLogic().getByIDOwn(prepared);
62
+ return await getLogic(prepared).getByIDOwn(prepared);
63
63
  } else {
64
- return await getLogic().getByID(prepared);
64
+ return await getLogic(prepared).getByID(prepared);
65
65
  }
66
66
  }
67
67
 
68
68
  static async getByID(req, res, next, prepared) {
69
- return await getLogic().getByIDOwn(prepared);
69
+ return await getLogic(prepared).getByIDOwn(prepared);
70
70
  }
71
71
 
72
72
  static async _getRaw(req, res, next, prepared) {
@@ -76,14 +76,14 @@ module.exports = ({ getLogic, before, after }) => {
76
76
  );
77
77
  }
78
78
  if (this.rootAsUser) {
79
- return await getLogic().getOwnRaw(prepared);
79
+ return await getLogic(prepared).getOwnRaw(prepared);
80
80
  } else {
81
- return await getLogic().getRaw(prepared);
81
+ return await getLogic(prepared).getRaw(prepared);
82
82
  }
83
83
  }
84
84
 
85
85
  static async getRaw(req, res, next, prepared) {
86
- return await getLogic().getOwnRaw(prepared);
86
+ return await getLogic(prepared).getOwnRaw(prepared);
87
87
  }
88
88
 
89
89
  static async _update(req, res, next, prepared) {
@@ -93,14 +93,14 @@ module.exports = ({ getLogic, before, after }) => {
93
93
  );
94
94
  }
95
95
  if (this.rootAsUser) {
96
- return await getLogic().updateOwn(prepared);
96
+ return await getLogic(prepared).updateOwn(prepared);
97
97
  } else {
98
- return await getLogic().update(prepared);
98
+ return await getLogic(prepared).update(prepared);
99
99
  }
100
100
  }
101
101
 
102
102
  static async update(req, res, next, prepared) {
103
- return await getLogic().updateOwn(prepared);
103
+ return await getLogic(prepared).updateOwn(prepared);
104
104
  }
105
105
 
106
106
  static async _listAll(req, res, next, prepared) {
@@ -110,14 +110,14 @@ module.exports = ({ getLogic, before, after }) => {
110
110
  );
111
111
  }
112
112
  if (this.rootAsUser) {
113
- return await getLogic().listAllOwn(prepared);
113
+ return await getLogic(prepared).listAllOwn(prepared);
114
114
  } else {
115
- return await getLogic().listAll(prepared);
115
+ return await getLogic(prepared).listAll(prepared);
116
116
  }
117
117
  }
118
118
 
119
119
  static async listAll(req, res, next, prepared) {
120
- return await getLogic().listAllOwn(prepared);
120
+ return await getLogic(prepared).listAllOwn(prepared);
121
121
  }
122
122
 
123
123
  static async _listAndCount(req, res, next, prepared) {
@@ -127,14 +127,14 @@ module.exports = ({ getLogic, before, after }) => {
127
127
  );
128
128
  }
129
129
  if (this.rootAsUser) {
130
- return await getLogic().listAndCountOwn(prepared);
130
+ return await getLogic(prepared).listAndCountOwn(prepared);
131
131
  } else {
132
- return await getLogic().listAndCount(prepared);
132
+ return await getLogic(prepared).listAndCount(prepared);
133
133
  }
134
134
  }
135
135
 
136
136
  static async listAndCount(req, res, next, prepared) {
137
- return await getLogic().listAndCountOwn(prepared);
137
+ return await getLogic(prepared).listAndCountOwn(prepared);
138
138
  }
139
139
 
140
140
  static async _list(req, res, next, prepared) {
@@ -144,14 +144,14 @@ module.exports = ({ getLogic, before, after }) => {
144
144
  );
145
145
  }
146
146
  if (this.rootAsUser) {
147
- return await getLogic().listOwn(prepared);
147
+ return await getLogic(prepared).listOwn(prepared);
148
148
  } else {
149
- return await getLogic().list(prepared);
149
+ return await getLogic(prepared).list(prepared);
150
150
  }
151
151
  }
152
152
 
153
153
  static async list(req, res, next, prepared) {
154
- return await getLogic().listOwn(prepared);
154
+ return await getLogic(prepared).listOwn(prepared);
155
155
  }
156
156
 
157
157
  static async _count(req, res, next, prepared) {
@@ -161,14 +161,14 @@ module.exports = ({ getLogic, before, after }) => {
161
161
  );
162
162
  }
163
163
  if (this.rootAsUser) {
164
- return await getLogic().countOwn(prepared);
164
+ return await getLogic(prepared).countOwn(prepared);
165
165
  } else {
166
- return await getLogic().count(prepared);
166
+ return await getLogic(prepared).count(prepared);
167
167
  }
168
168
  }
169
169
 
170
170
  static async count(req, res, next, prepared) {
171
- return await getLogic().countOwn(prepared);
171
+ return await getLogic(prepared).countOwn(prepared);
172
172
  }
173
173
 
174
174
  static async _delete(req, res, next, prepared) {
@@ -178,14 +178,14 @@ module.exports = ({ getLogic, before, after }) => {
178
178
  );
179
179
  }
180
180
  if (this.rootAsUser) {
181
- return await getLogic().deleteOwn(prepared);
181
+ return await getLogic(prepared).deleteOwn(prepared);
182
182
  } else {
183
- return await getLogic().delete(prepared);
183
+ return await getLogic(prepared).delete(prepared);
184
184
  }
185
185
  }
186
186
 
187
187
  static async delete(req, res, next, prepared) {
188
- return await getLogic().deleteOwn(prepared);
188
+ return await getLogic(prepared).deleteOwn(prepared);
189
189
  }
190
190
  }
191
191
 
@@ -0,0 +1,95 @@
1
+ const MIN_TASK_CHECK_INTERVAL = 3600;
2
+
3
+ class TaskRunner {
4
+ static #logger = console;
5
+ static #runEvery = 3600;
6
+ static #checkEvery = 3500; //every hour
7
+ static #int = setInterval(TaskRunner.#check, TaskRunner.#checkEvery * 1000);
8
+ static #tasks = [];
9
+
10
+ static setLogger(logger) {
11
+ TaskRunner.#logger = logger;
12
+ }
13
+
14
+ static #create(id, task, tag) {
15
+ return {
16
+ id,
17
+ task,
18
+ tag,
19
+ count: 0,
20
+ lastRunnedAt: -1,
21
+ };
22
+ }
23
+
24
+ static add(task, tag) {
25
+ const id = Math.random();
26
+ if (!tag) {
27
+ tag = `task#${TaskRunner.#tasks.length + 1}`;
28
+ }
29
+ TaskRunner.#tasks.push(TaskRunner.#create(id, task, tag));
30
+ }
31
+
32
+ static remove(id) {
33
+ const task = TaskRunner.#tasks.find((item) => item.id === id);
34
+ if (task) {
35
+ TaskRunner.#tasks.splice(TaskRunner.#tasks.indexOf(task), 1);
36
+ return true;
37
+ }
38
+ return false;
39
+ }
40
+
41
+ static clear() {
42
+ this.#tasks = [];
43
+ }
44
+
45
+ static async #check() {
46
+ try {
47
+ const runners = TaskRunner.#tasks
48
+ .filter(TaskRunner.#taskShouldBeRunned)
49
+ .map(TaskRunner.#taskToPromise);
50
+ await Promise.allSettled(runners);
51
+ } catch (e) {
52
+ TaskRunner.#logger.error("Task Set Failed", new Date(), e);
53
+ }
54
+ }
55
+
56
+ static async #taskToPromise(taskItem) {
57
+ const { tag } = taskItem;
58
+ try {
59
+ await taskItem.task();
60
+ taskItem.lastRunnedAt = Date.now();
61
+ taskItem.count += 1;
62
+ } catch (e) {
63
+ TaskRunner.#logger.error("Task Failed", tag, new Date(), e);
64
+ }
65
+ }
66
+
67
+ static #taskShouldBeRunned(taskItem) {
68
+ if (taskItem.count === 0) {
69
+ return true;
70
+ }
71
+ return taskItem.lastRunnedAt < Date.now() - TaskRunner.#runEvery;
72
+ }
73
+
74
+ static stop() {
75
+ clearInterval(TaskRunner.#int);
76
+ }
77
+
78
+ static start() {
79
+ TaskRunner.#int = setInterval(
80
+ TaskRunner.#check,
81
+ TaskRunner.#checkEvery * 1000
82
+ );
83
+ }
84
+
85
+ static setInterval(int = MIN_TASK_CHECK_INTERVAL) {
86
+ if (isNaN(int) || int < MIN_TASK_CHECK_INTERVAL) {
87
+ return false;
88
+ }
89
+ TaskRunner.#checkEvery = int;
90
+ TaskRunner.stop();
91
+ TaskRunner.start();
92
+ }
93
+ }
94
+
95
+ module.exports = TaskRunner;
package/.eslintignore DELETED
@@ -1,4 +0,0 @@
1
- /node_modules/**
2
- src/rollup.js
3
- src/repos.js
4
- src/lib.js