hola-server 1.0.11 → 3.0.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.
Files changed (204) hide show
  1. package/README.md +382 -1
  2. package/dist/config/index.d.ts +46 -0
  3. package/dist/config/index.d.ts.map +1 -0
  4. package/dist/config/index.js +55 -0
  5. package/dist/config/index.js.map +1 -0
  6. package/dist/core/array.d.ts +27 -0
  7. package/dist/core/array.d.ts.map +1 -0
  8. package/dist/core/array.js +66 -0
  9. package/dist/core/array.js.map +1 -0
  10. package/dist/core/bash.d.ts +51 -0
  11. package/dist/core/bash.d.ts.map +1 -0
  12. package/dist/core/bash.js +161 -0
  13. package/dist/core/bash.js.map +1 -0
  14. package/dist/core/chart.d.ts +11 -0
  15. package/dist/core/chart.d.ts.map +1 -0
  16. package/dist/core/chart.js +35 -0
  17. package/dist/core/chart.js.map +1 -0
  18. package/dist/core/date.d.ts +11 -0
  19. package/dist/core/date.d.ts.map +1 -0
  20. package/dist/core/date.js +18 -0
  21. package/dist/core/date.js.map +1 -0
  22. package/dist/core/encrypt.d.ts +18 -0
  23. package/dist/core/encrypt.d.ts.map +1 -0
  24. package/dist/core/encrypt.js +50 -0
  25. package/dist/core/encrypt.js.map +1 -0
  26. package/dist/core/file.d.ts +22 -0
  27. package/dist/core/file.d.ts.map +1 -0
  28. package/dist/core/file.js +21 -0
  29. package/dist/core/file.js.map +1 -0
  30. package/dist/core/lhs.d.ts +17 -0
  31. package/dist/core/lhs.d.ts.map +1 -0
  32. package/dist/core/lhs.js +30 -0
  33. package/dist/core/lhs.js.map +1 -0
  34. package/dist/core/meta.d.ts +200 -0
  35. package/dist/core/meta.d.ts.map +1 -0
  36. package/dist/core/meta.js +336 -0
  37. package/dist/core/meta.js.map +1 -0
  38. package/dist/core/number.d.ts +37 -0
  39. package/dist/core/number.d.ts.map +1 -0
  40. package/dist/core/number.js +99 -0
  41. package/dist/core/number.js.map +1 -0
  42. package/dist/core/obj.d.ts +9 -0
  43. package/dist/core/obj.d.ts.map +1 -0
  44. package/dist/core/obj.js +15 -0
  45. package/dist/core/obj.js.map +1 -0
  46. package/dist/core/random.d.ts +7 -0
  47. package/dist/core/random.d.ts.map +1 -0
  48. package/dist/core/random.js +7 -0
  49. package/dist/core/random.js.map +1 -0
  50. package/dist/core/role.d.ts +42 -0
  51. package/dist/core/role.d.ts.map +1 -0
  52. package/dist/core/role.js +81 -0
  53. package/dist/core/role.js.map +1 -0
  54. package/dist/core/thread.d.ts +7 -0
  55. package/dist/core/thread.d.ts.map +1 -0
  56. package/dist/core/thread.js +7 -0
  57. package/dist/core/thread.js.map +1 -0
  58. package/dist/core/type.d.ts +46 -0
  59. package/dist/core/type.d.ts.map +1 -0
  60. package/dist/core/type.js +281 -0
  61. package/dist/core/type.js.map +1 -0
  62. package/dist/core/url.d.ts +20 -0
  63. package/dist/core/url.d.ts.map +1 -0
  64. package/dist/core/url.js +24 -0
  65. package/dist/core/url.js.map +1 -0
  66. package/dist/core/validate.d.ts +11 -0
  67. package/dist/core/validate.d.ts.map +1 -0
  68. package/dist/core/validate.js +19 -0
  69. package/dist/core/validate.js.map +1 -0
  70. package/dist/db/db.d.ts +72 -0
  71. package/dist/db/db.d.ts.map +1 -0
  72. package/dist/db/db.js +225 -0
  73. package/dist/db/db.js.map +1 -0
  74. package/dist/db/entity.d.ts +77 -0
  75. package/dist/db/entity.d.ts.map +1 -0
  76. package/dist/db/entity.js +671 -0
  77. package/dist/db/entity.js.map +1 -0
  78. package/dist/db/gridfs.d.ts +29 -0
  79. package/dist/db/gridfs.d.ts.map +1 -0
  80. package/dist/db/gridfs.js +125 -0
  81. package/dist/db/gridfs.js.map +1 -0
  82. package/dist/db/index.d.ts +8 -0
  83. package/dist/db/index.d.ts.map +1 -0
  84. package/dist/db/index.js +8 -0
  85. package/dist/db/index.js.map +1 -0
  86. package/dist/errors/auth.d.ts +15 -0
  87. package/dist/errors/auth.d.ts.map +1 -0
  88. package/dist/errors/auth.js +21 -0
  89. package/dist/errors/auth.js.map +1 -0
  90. package/dist/errors/http.d.ts +15 -0
  91. package/dist/errors/http.d.ts.map +1 -0
  92. package/dist/errors/http.js +21 -0
  93. package/dist/errors/http.js.map +1 -0
  94. package/dist/errors/index.d.ts +18 -0
  95. package/dist/errors/index.d.ts.map +1 -0
  96. package/dist/errors/index.js +18 -0
  97. package/dist/errors/index.js.map +1 -0
  98. package/dist/errors/validation.d.ts +11 -0
  99. package/dist/errors/validation.d.ts.map +1 -0
  100. package/dist/errors/validation.js +15 -0
  101. package/dist/errors/validation.js.map +1 -0
  102. package/dist/http/code.d.ts +21 -0
  103. package/dist/http/code.d.ts.map +1 -0
  104. package/dist/http/code.js +27 -0
  105. package/dist/http/code.js.map +1 -0
  106. package/dist/index.d.ts +57 -0
  107. package/dist/index.d.ts.map +1 -0
  108. package/dist/index.js +61 -0
  109. package/dist/index.js.map +1 -0
  110. package/dist/meta/index.d.ts +9 -0
  111. package/dist/meta/index.d.ts.map +1 -0
  112. package/dist/meta/index.js +11 -0
  113. package/dist/meta/index.js.map +1 -0
  114. package/dist/meta/router.d.ts +26 -0
  115. package/dist/meta/router.d.ts.map +1 -0
  116. package/dist/meta/router.js +258 -0
  117. package/dist/meta/router.js.map +1 -0
  118. package/dist/meta/schema.d.ts +41 -0
  119. package/dist/meta/schema.d.ts.map +1 -0
  120. package/dist/meta/schema.js +69 -0
  121. package/dist/meta/schema.js.map +1 -0
  122. package/dist/plugins/auth.d.ts +248 -0
  123. package/dist/plugins/auth.d.ts.map +1 -0
  124. package/dist/plugins/auth.js +121 -0
  125. package/dist/plugins/auth.js.map +1 -0
  126. package/dist/plugins/body.d.ts +47 -0
  127. package/dist/plugins/body.d.ts.map +1 -0
  128. package/dist/plugins/body.js +36 -0
  129. package/dist/plugins/body.js.map +1 -0
  130. package/dist/plugins/cors.d.ts +62 -0
  131. package/dist/plugins/cors.d.ts.map +1 -0
  132. package/dist/plugins/cors.js +17 -0
  133. package/dist/plugins/cors.js.map +1 -0
  134. package/dist/plugins/error.d.ts +51 -0
  135. package/dist/plugins/error.d.ts.map +1 -0
  136. package/dist/plugins/error.js +51 -0
  137. package/dist/plugins/error.js.map +1 -0
  138. package/dist/plugins/index.d.ts +9 -0
  139. package/dist/plugins/index.d.ts.map +1 -0
  140. package/dist/plugins/index.js +9 -0
  141. package/dist/plugins/index.js.map +1 -0
  142. package/dist/setting.d.ts +66 -0
  143. package/dist/setting.d.ts.map +1 -0
  144. package/dist/setting.js +27 -0
  145. package/dist/setting.js.map +1 -0
  146. package/dist/tool/gen_i18n.d.ts +10 -0
  147. package/dist/tool/gen_i18n.d.ts.map +1 -0
  148. package/dist/tool/gen_i18n.js +51 -0
  149. package/dist/tool/gen_i18n.js.map +1 -0
  150. package/dist/tool/vector_store.d.ts +72 -0
  151. package/dist/tool/vector_store.d.ts.map +1 -0
  152. package/dist/tool/vector_store.js +203 -0
  153. package/dist/tool/vector_store.js.map +1 -0
  154. package/package.json +38 -23
  155. package/core/array.js +0 -187
  156. package/core/bash.js +0 -345
  157. package/core/chart.js +0 -36
  158. package/core/cron.js +0 -10
  159. package/core/date.js +0 -55
  160. package/core/encrypt.js +0 -16
  161. package/core/file.js +0 -38
  162. package/core/lhs.js +0 -27
  163. package/core/meta.js +0 -359
  164. package/core/msg.js +0 -11
  165. package/core/number.js +0 -179
  166. package/core/obj.js +0 -22
  167. package/core/random.js +0 -9
  168. package/core/role.js +0 -116
  169. package/core/thread.js +0 -3
  170. package/core/type.js +0 -329
  171. package/core/url.js +0 -22
  172. package/core/validate.js +0 -32
  173. package/db/db.js +0 -376
  174. package/db/entity.js +0 -1252
  175. package/db/gridfs.js +0 -221
  176. package/http/code.js +0 -18
  177. package/http/context.js +0 -17
  178. package/http/cors.js +0 -15
  179. package/http/error.js +0 -21
  180. package/http/express.js +0 -75
  181. package/http/params.js +0 -57
  182. package/http/router.js +0 -72
  183. package/http/session.js +0 -50
  184. package/index.js +0 -36
  185. package/router/clone.js +0 -73
  186. package/router/create.js +0 -59
  187. package/router/delete.js +0 -53
  188. package/router/read.js +0 -177
  189. package/router/update.js +0 -107
  190. package/setting.js +0 -51
  191. package/test/core/date.js +0 -37
  192. package/test/core/encrypt.js +0 -14
  193. package/test/core/meta.js +0 -594
  194. package/test/core/number.js +0 -17
  195. package/test/crud/router.js +0 -99
  196. package/test/db/db.js +0 -72
  197. package/test/entity/create.js +0 -442
  198. package/test/entity/delete.js +0 -480
  199. package/test/entity/read.js +0 -285
  200. package/test/entity/update.js +0 -252
  201. package/test/router/role.js +0 -15
  202. package/test/router/user.js +0 -17
  203. package/tool/gen_i18n.js +0 -30
  204. package/tool/test.json +0 -25
package/core/array.js DELETED
@@ -1,187 +0,0 @@
1
- const { round_to_fixed2 } = require('./number');
2
-
3
- /**
4
- * Shuffle the array randomly
5
- *
6
- * @param {array to be shuffle} a
7
- */
8
- const shuffle = (a) => {
9
- let j, x, i;
10
- for (i = a.length; i; i--) {
11
- j = Math.floor(Math.random() * i);
12
- x = a[i - 1];
13
- a[i - 1] = a[j];
14
- a[j] = x;
15
- }
16
- }
17
-
18
- /**
19
- * Remove the object from array based on the attribute value of the object
20
- * @param {array of object} array
21
- * @param {the attribute name of the object} field
22
- * @param {the attribute value} value
23
- */
24
- const remove_element = (array, field, value) => {
25
- for (let i = array.length - 1; i >= 0; --i) {
26
- if (array[i][field] == value) {
27
- array.splice(i, 1);
28
- }
29
- }
30
- }
31
-
32
- /**
33
- * Pop n elements from the array
34
- * @param {array of elements} array
35
- * @param {n number of object to pop} n
36
- * @returns
37
- */
38
- const pop_n = (array, n) => {
39
- const subarray = [];
40
- while (subarray.length < n) {
41
- const ele = array.pop();
42
- if (ele) {
43
- subarray.push(ele);
44
- } else {
45
- break;
46
- }
47
- }
48
- if (subarray.length > 0) {
49
- return subarray;
50
- } else {
51
- return undefined;
52
- }
53
- }
54
-
55
- /**
56
- * Shift n elements from the array
57
- * @param {array of elements} array
58
- * @param {n number of object to pop} n
59
- * @returns
60
- */
61
- const shift_n = (array, n) => {
62
- const subarray = [];
63
- while (subarray.length < n) {
64
- const ele = array.shift();
65
- if (ele) {
66
- subarray.push(ele);
67
- } else {
68
- break;
69
- }
70
- }
71
- if (subarray.length > 0) {
72
- return subarray;
73
- } else {
74
- return undefined;
75
- }
76
- }
77
-
78
- /**
79
- * Calculate the sum value of the array of the number
80
- * @param {array of number} arr
81
- * @returns
82
- */
83
- const sum = function (arr) {
84
- return round_to_fixed2(arr.reduce((pre, cur) => pre + cur));
85
- };
86
-
87
- /**
88
- * Calculate the average value of the array of number
89
- * @param {array of number} arr
90
- * @returns
91
- */
92
- const avg = function (arr) {
93
- if (arr.length === 0) {
94
- return 0;
95
- } else {
96
- return round_to_fixed2(sum(arr) / arr.length);
97
- }
98
- };
99
-
100
- /**
101
- *
102
- * @param {array of object} arr
103
- * @param {key attr of element as key} key_attr
104
- * @param {value attr of element as value} value_attr
105
- * @returns
106
- */
107
- const map_array_to_obj = function (arr, key_attr, value_attr) {
108
- const obj = {};
109
- arr.forEach(element => {
110
- obj[element[key_attr]] = element[value_attr];
111
- });
112
- return obj;
113
- }
114
-
115
- /**
116
- * Sort array of element based on the attr value in desc order
117
- * @param {array to be sort} arr
118
- * @param {attribute used to do sorting} attr
119
- * @returns
120
- */
121
- const sort_desc = function (arr, attr) {
122
- arr.sort((a, b) => {
123
- return b[attr] - a[attr];
124
- });
125
- return arr;
126
- };
127
-
128
- /**
129
- * Sort array of element based on the attr value in asc order
130
- * @param {array to be sort} arr
131
- * @param {attribute used to do sorting} attr
132
- * @returns
133
- */
134
- const sort_asc = function (arr, attr) {
135
- arr.sort((a, b) => {
136
- return a[attr] - b[attr];
137
- });
138
- return arr;
139
- };
140
-
141
- /**
142
- * Use the keys sequence to sort the array
143
- * @param {array of object} arr
144
- * @param {the attr used to do sorting } attr
145
- * @param {the key values} keys
146
- * @returns
147
- */
148
- const sort_by_key_seq = function (arr, attr, keys) {
149
- arr.sort((a, b) => {
150
- return keys.indexOf(a[attr]) - keys.indexOf(b[attr]);
151
- });
152
- return arr;
153
- };
154
-
155
- /**
156
- * Create combination of two object, the size will be arr1.length * arr2.length
157
- * @param {array of object} arr1
158
- * @param {array of object} arr2
159
- * @returns
160
- */
161
- const combine = (arr1, arr2) => {
162
- const result = [];
163
- for (let i = 0; i < arr1.length; i++) {
164
- const obj1 = arr1[i];
165
- for (let j = 0; j < arr2.length; j++) {
166
- const obj2 = arr2[j];
167
- result.push({ ...obj1, ...obj2 });
168
- }
169
- }
170
- return result;
171
- }
172
-
173
- /**
174
- * Remove duplicate element and keep it unique
175
- * @param {array of elements} arr
176
- * @returns
177
- */
178
- const unique = (array) => {
179
- return array.filter((value, index) => {
180
- const _value = JSON.stringify(value);
181
- return index === array.findIndex(obj => {
182
- return JSON.stringify(obj) === _value;
183
- });
184
- });
185
- }
186
-
187
- module.exports = { shuffle, remove_element, pop_n, shift_n, sum, avg, combine, sort_desc, sort_asc, sort_by_key_seq, unique, map_array_to_obj }
package/core/bash.js DELETED
@@ -1,345 +0,0 @@
1
- const fs = require('fs');
2
- const { exec } = require("child_process");
3
- const { random_code } = require('./random');
4
- const { is_log_debug, is_log_error, log_debug, log_error } = require('../db/db');
5
-
6
- const LOG_BASH = "bash";
7
-
8
- const get_log_file = async () => {
9
- const home = await run_simple_local_cmd("echo ~");
10
- const log_dir = `${home}/.hola/ssh`;
11
- await run_local_cmd(`mkdir -p ${log_dir}`);
12
- return `${log_dir}/l_${random_code()}.log`;
13
- }
14
-
15
- /**
16
- * Run script and get stdout
17
- * @param {host info,contains user,ip and password} host
18
- * @param {commands to run} script
19
- * @returns
20
- */
21
- const run_script = async (host, script, log_extra) => {
22
- return new Promise((resolve) => {
23
- exec(`ssh ${host.auth} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p ${host.port} ${host.user}@${host.ip} /bin/bash <<'EOT'\n ${script} \nEOT\n`, { maxBuffer: 1024 * 150000 }, (error, stdout) => {
24
- if (error) {
25
- if (is_log_error()) {
26
- log_error(LOG_BASH, "error running on host:" + host.name + " the script:" + script + ",error:" + error, log_extra);
27
- }
28
- resolve({ stdout: stdout, err: "error running the script:" + script + ",error:" + error });
29
- } else {
30
- if (is_log_debug()) {
31
- log_debug(LOG_BASH, "executing on host:" + host.name + ", script:" + script + ",stdout:" + stdout, log_extra);
32
- }
33
- resolve({ stdout: stdout });
34
- }
35
- });
36
- });
37
- };
38
-
39
- /**
40
- * Run script and get stdout and this use file redirect to avoid progress bar issue
41
- * @param {host info,contains user,ip and password} host
42
- * @param {commands to run} script
43
- * @returns
44
- */
45
- const run_script_extra = async (host, script, log_extra) => {
46
- const log_file = await get_log_file();
47
-
48
- return new Promise((resolve) => {
49
- exec(`ssh ${host.auth} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p ${host.port} ${host.user}@${host.ip} /bin/bash <<'EOT' > ${log_file} \n ${script} \nEOT\n`, { maxBuffer: 1024 * 150000 }, (error, stdout) => {
50
- if (error) {
51
- if (is_log_error()) {
52
- log_error(LOG_BASH, "error running on host:" + host.name + " the script:" + script + ",error:" + error, log_extra);
53
- }
54
- resolve({ stdout: stdout, err: "error running the script:" + script + ",error:" + error });
55
- } else {
56
- const output = fs.readFileSync(log_file, { encoding: 'utf8', flag: 'r' });
57
- if (is_log_debug()) {
58
- log_debug(LOG_BASH, "executing on host:" + host.name + ", script:" + script + ",stdout:" + output, log_extra);
59
- }
60
- resolve({ stdout: output });
61
- fs.unlinkSync(log_file);
62
- }
63
- });
64
- });
65
- };
66
-
67
- const run_script_file = async (host, script_file, log_extra) => {
68
- return new Promise((resolve) => {
69
- exec(`ssh ${host.auth} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p ${host.port} ${host.user}@${host.ip} /bin/bash < ${script_file}`, (error, stdout) => {
70
- if (error) {
71
- if (is_log_error()) {
72
- log_error(LOG_BASH, "error running on host:" + host.name + " the script_file:" + script_file + ",error:" + error, log_extra);
73
- }
74
- resolve({ stdout: stdout, err: "error running the script:" + script_file + ",error:" + error });
75
- } else {
76
- if (is_log_debug()) {
77
- log_debug(LOG_BASH, "executing on host:" + host.name + ", script_file:" + script_file + ",stdout:" + stdout, log_extra);
78
- }
79
- resolve({ stdout: stdout });
80
- }
81
- });
82
- });
83
- };
84
-
85
- /**
86
- * Run command in local host
87
- * @param {command to execute} cmd
88
- * @returns
89
- */
90
- const run_local_cmd = async (cmd, log_extra) => {
91
- return new Promise((resolve) => {
92
- exec(cmd, { maxBuffer: 1024 * 15000000 }, (error, stdout) => {
93
- if (error) {
94
- if (is_log_error()) {
95
- log_error(LOG_BASH, "error running on local host with cmd:" + cmd + ",error:" + error, log_extra);
96
- }
97
- resolve({ stdout: stdout, err: "error running the cmd:" + cmd + ",error:" + error }, log_extra);
98
- } else {
99
- if (is_log_debug()) {
100
- log_debug(LOG_BASH, "executing on local host with cmd:" + cmd + ",stdout:" + stdout, log_extra);
101
- }
102
- resolve({ stdout: stdout });
103
- }
104
- });
105
- });
106
- };
107
-
108
- /**
109
- * Scp remote file to local file
110
- * @param {remote host} host
111
- * @param {remote file path} remote_file
112
- * @param {local file path} locale_file
113
- * @returns
114
- */
115
- const scp = async (host, remote_file, local_file, log_extra) => {
116
- return new Promise((resolve) => {
117
- exec(`scp ${host.auth} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -P ${host.port} -q ${host.user}@${host.ip}:${remote_file} ${local_file}`, (error, stdout) => {
118
- if (error) {
119
- if (is_log_error()) {
120
- log_error(LOG_BASH, "error scp on host:" + host.name + " remote:" + remote_file + ",local:" + local_file + ",error:" + error, log_extra);
121
- }
122
- resolve({ stdout: stdout, err: "error scp:" + remote_file + " to locale:" + local_file + ",err:" + error });
123
- } else {
124
- if (is_log_debug()) {
125
- log_debug(LOG_BASH, "executing scp on host:" + host.name + ", remote:" + remote_file + ",local:" + local_file + ",stdout:" + stdout, log_extra);
126
- }
127
- resolve({ stdout: stdout });
128
- }
129
- });
130
- });
131
- };
132
-
133
- /**
134
- * Scp local file to remote file
135
- * @param {remote host} host
136
- * @param {local file path} locale_file
137
- * @param {remote file path} remote_file
138
- * @returns
139
- */
140
- const scpr = async (host, local_file, remote_file, log_extra) => {
141
- return new Promise((resolve) => {
142
- exec(`scp ${host.auth} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -P ${host.port} -q ${local_file} ${host.user}@${host.ip}:${remote_file}`, (error, stdout) => {
143
- if (error) {
144
- if (is_log_error()) {
145
- log_error(LOG_BASH, "error scpr on host:" + host.name + " remote:" + remote_file + ",local:" + local_file + ",error:" + error, log_extra);
146
- }
147
- resolve({ stdout: stdout, err: "error scp:" + remote_file + " to locale:" + local_file + ",err:" + error });
148
- } else {
149
- if (is_log_debug()) {
150
- log_debug(LOG_BASH, "executing scpr on host:" + host.name + ", remote:" + remote_file + ",local:" + local_file + ",stdout:" + stdout, log_extra);
151
- }
152
- resolve({ stdout: stdout });
153
- }
154
- });
155
- });
156
- };
157
-
158
- /**
159
- *Just run simple command and get result directly
160
- * @param {host info,contains user,ip and password} host
161
- * @param {command to run} cmd
162
- * @returns
163
- */
164
- const run_simple_cmd = async (host, cmd, log_extra) => {
165
- const { err, stdout } = await run_script(host, cmd, log_extra);
166
- if (err) {
167
- return null;
168
- } else {
169
- return stdout.trim();
170
- }
171
- }
172
-
173
- /**
174
- *Just run simple command and get result directly
175
- * @param {command to run} cmd
176
- * @returns
177
- */
178
- const run_simple_local_cmd = async (cmd, log_extra) => {
179
- const { err, stdout } = await run_local_cmd(cmd, log_extra);
180
- if (err) {
181
- return null;
182
- } else {
183
- return stdout.trim();
184
- }
185
- }
186
-
187
- /**
188
- * Use regular expression to match the pattern to get some part of value
189
- Speed: Unknown
190
- Manufacturer: NO DIMM
191
- Serial Number: NO DIMM
192
- Asset Tag:
193
- Part Number: NO DIMM
194
- Rank: Unknown
195
- Configured Memory Speed: Unknown
196
- Minimum Voltage: 1.2 V
197
- Maximum Voltage: 1.2 V
198
- Configured Voltage: 1.2 V
199
-
200
- * @param {stdout of the run_cmd} stdout
201
- * @param {key to retrieve info} key
202
- * @returns
203
- */
204
- const get_info = (stdout, key, log_extra) => {
205
- const word_key = key.split(" ").join("\\s+");
206
- const regex = new RegExp(`\n\\s?${word_key}\\s?:(.*)\\s`, 'g');
207
- if (is_log_debug()) {
208
- log_debug(LOG_BASH, "get_info and regex:" + JSON.stringify(regex), log_extra);
209
- }
210
-
211
- const results = stdout.matchAll(regex);
212
-
213
- const matched = [];
214
- for (let result of results) {
215
- matched.push(result[1].trim());
216
- }
217
-
218
- if (is_log_debug()) {
219
- log_debug(LOG_BASH, "get_info and matched:" + JSON.stringify(matched), log_extra);
220
- }
221
- return matched;
222
- }
223
-
224
- /**
225
- * Get the lines of array
226
- * @param {array of lines} array
227
- * @param {array of line number to get,use range to define the lines} lines
228
- * @returns
229
- */
230
- const get_lines = (array, lines) => {
231
- const result = [];
232
- lines.forEach(line => {
233
- result.push(array[line]);
234
- });
235
- return result;
236
- }
237
-
238
- /**
239
- * Read all the lines to object such as lscpu to retrieve all the cpu info
240
- Core(s) per socket: 28
241
- Socket(s): 2
242
- NUMA node(s): 2
243
- Vendor ID: GenuineIntel
244
- CPU family: 6
245
- Model: 85
246
- Model name: Intel(R) Xeon(R) Gold 6258R CPU @ 2.70GHz
247
- Stepping: 7
248
- CPU MHz: 2965.749
249
- CPU max MHz: 4000.0000
250
- CPU min MHz: 1000.0000
251
- BogoMIPS: 5400.00
252
- Virtualization: VT-x
253
- L1d cache: 32K
254
- L1i cache: 32K
255
-
256
- * @param {stdout of the run_cmd} stdout
257
- * @param {delimiter to split} delimiter
258
- * @param {lines to retrieve} lines
259
- * @param {only retrieve the value that has the same property in config} config
260
- * @returns
261
- */
262
- const read_key_value_line = (stdout, delimiter = ":", lines, config, exclude_mode) => {
263
- const obj = {};
264
- if (!stdout) {
265
- return obj;
266
- }
267
-
268
- const contents = stdout.toString().split(/(?:\r\n|\r|\n)/g);
269
- const line_texts = lines ? get_lines(contents, lines) : contents;
270
- line_texts.forEach(content => {
271
- const key_value = content.split(delimiter);
272
- if (key_value.length == 2) {
273
- const key = key_value[0].trim();
274
- if (key && !config || (exclude_mode != true && config && config[key]) || exclude_mode == true && config && !config[`!${key}`]) {
275
- obj[key] = key_value[1].trim();
276
- }
277
- }
278
- });
279
- return obj;
280
- }
281
-
282
-
283
- /**
284
- * Use delimeter to split the line and retrieve the info
285
- *
286
- MODEL SIZE
287
- INTEL SSDSC2KB03 3.5T
288
- INTEL SSDPE2KE032T7 2.9T
289
-
290
- * @param {stdout of the run_cmd} stdout
291
- * @param {the attribute keys} keys
292
- * @param {the lines to ignore} ignore
293
- * @param {delimiter to split the line} delimiter
294
- * @returns
295
- */
296
- const read_obj_line = (stdout, keys, ignore = 1, delimiter = " ") => {
297
- const results = [];
298
- if (!stdout) {
299
- return results;
300
- }
301
-
302
- const contents = stdout.toString().split(/(?:\r\n|\r|\n)/g);
303
- const line_texts = contents.slice(ignore);
304
- line_texts.forEach(content => {
305
- const values = content.split(delimiter);
306
- if (values && values.length > 0 && values[0].trim().length > 0) {
307
- const obj = {};
308
- const attrs = values.filter(f => f.trim().length > 0);
309
- for (let i = 0; i < keys.length; i++) {
310
- const key = keys[i];
311
- const value = attrs[i];
312
- value && (obj[key] = value.trim());
313
- }
314
- results.push(obj);
315
- }
316
- });
317
- return results;
318
- }
319
-
320
- /**
321
- * Retrive attributes value and put it to obj
322
- * @param {host info,contains user,ip and password} host
323
- * @param {*} attrs {name:"attr name",cmd:"command to get the attr"}
324
- */
325
- const get_system_attributes = async (host, attrs, log_extra) => {
326
- const obj = {};
327
- for (let i = 0; i < attrs.length; i++) {
328
- const attr = attrs[i];
329
- const value = await run_simple_cmd(host, attr.cmd, log_extra);
330
- value && (obj[attr.name] = value);
331
- }
332
- return obj;
333
- }
334
-
335
- const stop_process = async (host, process_name, stop_cmd, using_full, log_extra) => {
336
- const grep_cmd = using_full == true ? `pgrep -f "${process_name}" | wc -l` : `pgrep ${process_name} | wc -l`;
337
- const { stdout } = await run_script(host, grep_cmd, log_extra);
338
- const has_process = stdout && parseInt(stdout) > 0;
339
- if (has_process) {
340
- await run_script(host, stop_cmd, log_extra);
341
- }
342
- return has_process;
343
- }
344
-
345
- module.exports = { stop_process, scp, scpr, run_script, run_script_extra, run_script_file, run_simple_cmd, run_local_cmd, run_simple_local_cmd, get_info, get_system_attributes, read_key_value_line, read_obj_line };
package/core/chart.js DELETED
@@ -1,36 +0,0 @@
1
- const { has_value } = require("./validate");
2
-
3
- const set_chart_header = (arr1, prefix) => {
4
- if (!arr1 || arr1.length < 1) {
5
- return;
6
- }
7
- const headers = arr1[0];
8
- if (!headers || headers.length < 1) {
9
- return;
10
- }
11
- for (let i = 1; i < headers.length; i++) {
12
- headers[i] = `${prefix}${headers[i]}`;
13
- }
14
- }
15
-
16
- const merge_chart_data = (arr1, arr2) => {
17
- if (!arr1 || arr1.length < 2 || !arr2 || arr2.length < 2) {
18
- return;
19
- }
20
-
21
- const max = Math.max(arr1.length, arr2.length);
22
- const arr1_cols = arr1[0].length;
23
- const arr2_cols = arr2[0].length;
24
- for (let i = 0; i < max; i++) {
25
- if (!has_value(arr1[i])) {
26
- arr1[i] = [...new Array(arr1_cols)].map(o => "");
27
- arr1[i][0] = arr2[i][0];
28
- }
29
- if (!has_value(arr2[i])) {
30
- arr2[i] = [...new Array(arr2_cols)].map(o => "");
31
- }
32
- arr1[i] = [...arr1[i], ...arr2[i].splice(1)];
33
- }
34
- }
35
-
36
- module.exports = { set_chart_header, merge_chart_data }
package/core/cron.js DELETED
@@ -1,10 +0,0 @@
1
- const node_cron = require('node-cron');
2
-
3
- const init_cron = async (crons, tasks) => {
4
- crons.forEach((cron, index) => {
5
- const schedule = node_cron.schedule(cron, () => { tasks[index](); }, { scheduled: true, timezone: "Asia/Shanghai" });
6
- schedule.start();
7
- });
8
- }
9
-
10
- module.exports = { init_cron };
package/core/date.js DELETED
@@ -1,55 +0,0 @@
1
- const date_format = require("dateformat");
2
- // import date_format from "dateformat";
3
-
4
- /**
5
- * Format the date object time part using HH:MM
6
- * @param {date object to be formatted} date
7
- * @returns
8
- */
9
- const format_time = (date) => {
10
- return date_format(date, "HH:MM");
11
- };
12
-
13
- /**
14
- * Format date object using mm/dd
15
- * @param {date object to be formatted} date
16
- * @returns
17
- */
18
- const simple_date = (date) => {
19
- return date_format(date, "mm/dd");
20
- };
21
-
22
- /**
23
- * Format date obj using yyyymmdd
24
- * @param {date object to be formatted} date
25
- * @returns
26
- */
27
- const format_date = (date) => {
28
- return date_format(date, "yyyymmdd");
29
- };
30
-
31
- /**
32
- * Format data obj using yyyymmdd hh:mm:ss
33
- * @param {date object to be formatted} date
34
- * @returns
35
- */
36
- const format_date_time = (date) => {
37
- return date_format(date, "yyyymmdd HH:MM:ss");
38
- };
39
-
40
- /**
41
- * Parse the date object using yyyymmdd format
42
- * @param {date str to be parsed} date
43
- * @returns
44
- */
45
- const parse_date = (date) => {
46
- const year = parseInt(date.substr(0, 4));
47
- const month = parseInt(date.substr(4, 2));
48
- const day = parseInt(date.substr(6, 2));
49
- const dateObj = new Date();
50
- dateObj.setFullYear(year, month - 1, day);
51
- dateObj.setHours(0, 0, 0, 0);
52
- return dateObj;
53
- };
54
-
55
- module.exports = { simple_date, format_date, format_time, format_date_time, parse_date };
package/core/encrypt.js DELETED
@@ -1,16 +0,0 @@
1
- const crypto = require('crypto');
2
- const { get_settings } = require('../setting');
3
-
4
- const md5 = (content) => {
5
- const md5 = crypto.createHash('md5');
6
- return md5.update(content).digest('hex');
7
- }
8
-
9
- const encrypt_pwd = (password) => {
10
- const crypto_key = get_settings().encrypt.key;
11
-
12
- const str = `BGT*&+${password}&76w${crypto_key}`;
13
- return md5(str);
14
- }
15
-
16
- module.exports = { encrypt_pwd };
package/core/file.js DELETED
@@ -1,38 +0,0 @@
1
- const fs = require('fs');
2
- const fs_promises = require('fs').promises;
3
-
4
- const unzipper = require('unzipper');
5
-
6
- const file_extension = file_name => file_name ? file_name.split('.').pop() : "";
7
- const file_prefix = file_name => file_name ? file_name.split('.')[0] : "";
8
-
9
- const read_from_zip_by_extension = async (path, extension) => {
10
- const directory = await unzipper.Open.file(path);
11
- return directory.files.filter(file => extension == file_extension(file.path));
12
- }
13
-
14
- const read_from_zip_by_prefix = async (path, prefix) => {
15
- const directory = await unzipper.Open.file(path);
16
- return directory.files.filter(file => file_prefix(file.path) == prefix);
17
- }
18
-
19
- /**
20
- * Check the file exist or not
21
- * @param {the file path} path
22
- * @returns
23
- */
24
- const is_file_exist = (path) => {
25
- try {
26
- fs.accessSync(path, fs.F_OK);
27
- } catch (e) {
28
- return false;
29
- }
30
- return true;
31
- }
32
-
33
- const get_file_size = async (path) => {
34
- const stats = await fs_promises.stat(path);
35
- return stats.size;
36
- }
37
-
38
- module.exports = { file_extension, file_prefix, read_from_zip_by_extension, read_from_zip_by_prefix, is_file_exist, get_file_size }