@opengis/fastify-table 1.2.95 → 1.2.97
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/README.md +86 -86
- package/package.json +2 -2
- package/server/migrations/cls.sql +1 -0
- package/server/plugins/cron/funcs/addCron.js +130 -130
- package/server/plugins/cron/index.js +6 -6
- package/server/plugins/crud/funcs/getOpt.js +13 -13
- package/server/plugins/crud/funcs/setOpt.js +21 -21
- package/server/plugins/crud/funcs/setToken.js +44 -44
- package/server/plugins/crud/funcs/utils/getFolder.js +11 -11
- package/server/plugins/crud/index.js +23 -23
- package/server/plugins/hook/index.js +8 -8
- package/server/plugins/logger/errorStatus.js +19 -19
- package/server/plugins/logger/index.js +26 -26
- package/server/plugins/migration/index.js +7 -7
- package/server/plugins/policy/sqlInjection.js +33 -33
- package/server/plugins/redis/client.js +8 -8
- package/server/plugins/redis/funcs/redisClients.js +3 -3
- package/server/plugins/redis/index.js +17 -17
- package/server/plugins/table/funcs/getFilterSQL/util/getCustomQuery.js +13 -13
- package/server/plugins/table/funcs/getFilterSQL/util/getTableSql.js +34 -34
- package/server/plugins/table/funcs/getTemplates.js +19 -19
- package/server/plugins/table/funcs/gisIRColumn.js +82 -82
- package/server/plugins/table/funcs/loadTemplate.js +1 -1
- package/server/plugins/table/funcs/loadTemplatePath.js +1 -1
- package/server/plugins/table/funcs/userTemplateDir.js +1 -1
- package/server/plugins/table/index.js +13 -13
- package/server/plugins/util/index.js +7 -7
- package/server/routes/cron/index.js +14 -14
- package/server/routes/logger/controllers/logger.file.js +92 -92
- package/server/routes/logger/controllers/utils/checkUserAccess.js +19 -19
- package/server/routes/logger/controllers/utils/getRootDir.js +26 -26
- package/server/routes/logger/index.js +17 -17
- package/server/routes/properties/controllers/properties.add.js +55 -55
- package/server/routes/properties/controllers/properties.get.js +17 -17
- package/server/routes/properties/index.js +16 -16
- package/server/routes/table/controllers/form.js +42 -42
- package/server/routes/table/controllers/search.js +74 -74
- package/server/routes/table/index.js +29 -29
- package/server/routes/table/schema.js +64 -64
- package/server/routes/util/controllers/status.monitor.js +8 -8
- package/server/routes/util/index.js +12 -12
package/README.md
CHANGED
|
@@ -1,87 +1,87 @@
|
|
|
1
|
-
# fastify-table
|
|
2
|
-
|
|
3
|
-
<!--  -->
|
|
4
|
-
|
|
5
|
-
[](https://www.npmjs.com/package/@opengis/fastify-table)
|
|
6
|
-
[]()
|
|
7
|
-
[]()
|
|
8
|
-
[](http://standardjs.com/)
|
|
9
|
-
|
|
10
|
-
This is a backend toolkit for database management and more.
|
|
11
|
-
|
|
12
|
-
### Why fastify-table?
|
|
13
|
-
|
|
14
|
-
- **Logger** - Monitoring user activity and system status.
|
|
15
|
-
- **Redis cache** - Optimizes web page rendering for lightning-fast performance.
|
|
16
|
-
- **Secure** - Ensures protection against XSS attacks in queries.
|
|
17
|
-
- **SQL Migration** - Automatically adds all necessary tables to your project's database.
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
### Features
|
|
21
|
-
|
|
22
|
-
- CRUD
|
|
23
|
-
- cron
|
|
24
|
-
- Logger
|
|
25
|
-
- Redis
|
|
26
|
-
- pg
|
|
27
|
-
|
|
28
|
-
---
|
|
29
|
-
|
|
30
|
-
### Install
|
|
31
|
-
|
|
32
|
-
```bash
|
|
33
|
-
npm i @opengis/fastify-table
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
### Usage
|
|
37
|
-
|
|
38
|
-
```js
|
|
39
|
-
fastify.register(import('@opengis/fastify-table'), config);
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
---
|
|
43
|
-
|
|
44
|
-
### Documenation
|
|
45
|
-
|
|
46
|
-
For a detailed understanding `fastify-table`, its features, and how to use them, refer to our [Documentation](https://apidocs.softpro.ua/fastify-table/).
|
|
47
|
-
|
|
48
|
-
---
|
|
49
|
-
|
|
50
|
-
### Technology stack
|
|
51
|
-
|
|
52
|
-
<a href="https://fastify.dev/" target="_blank">
|
|
53
|
-
<img src="https://img.shields.io/badge/Fastify-323330?style=for-the-badge&logo=fastify" /></a>
|
|
54
|
-
|
|
55
|
-
<a href="https://www.postgresql.org/" target="_blank">
|
|
56
|
-
<img src="https://img.shields.io/badge/PostgreSQL-323330?style=for-the-badge&logo=postgresql&logoColor=white" /></a>
|
|
57
|
-
|
|
58
|
-
<a href="https://redis.io/" target="_blank">
|
|
59
|
-
<img src="https://img.shields.io/badge/redis-323330.svg?&style=for-the-badge&logo=redis&logoColor=" /></a>
|
|
60
|
-
|
|
61
|
-
<a href="https://www.npmjs.com/package/pino" target="_blank">
|
|
62
|
-
<img src="https://img.shields.io/badge/pino-323330.svg?&style=for-the-badge&logo=pino&logoColor=" /></a>
|
|
63
|
-
|
|
64
|
-
---
|
|
65
|
-
|
|
66
|
-
### Contribute
|
|
67
|
-
|
|
68
|
-
Feel free to contact us through our website [SOFTPRO.UA](https://softpro.ua) or email <info@softpro.ua>
|
|
69
|
-
|
|
70
|
-
- Report bugs
|
|
71
|
-
- Share your ideas
|
|
72
|
-
- Ask questions
|
|
73
|
-
|
|
74
|
-
### Follow Us
|
|
75
|
-
|
|
76
|
-
[Official site](https://softpro.ua)
|
|
77
|
-
|
|
78
|
-
<p>
|
|
79
|
-
<a href="https://www.instagram.com/gissoftpro/" target="_blank"><img src="https://cdn.softpro.ua/data/npm/instagram.png" alt="Softpro Instagram" title="oftpro Instagram"></a>
|
|
80
|
-
<a href="https://www.facebook.com/gissoftpro" target="_blank"><img src="https://cdn.softpro.ua/data/npm/facebook.png" alt="Softpro Facebook" title="Softpro Facebook"></a>
|
|
81
|
-
<a href="https://t.me/softprogis" target="_blank"><img src="https://cdn.softpro.ua/data/npm/telegram.png" alt="Softpro Telegram" title="Softpro Telegram"></a>
|
|
82
|
-
<a href="https://www.linkedin.com/in/softpro-ukraine-a8876b282/recent-activity/all/" target="_blank"><img src="https://cdn.softpro.ua/data/npm/social/linkedin.png" alt="Softpro Linkedin" title="Softpro LinkedIn"></a>
|
|
83
|
-
</p>
|
|
84
|
-
|
|
85
|
-
### License
|
|
86
|
-
|
|
1
|
+
# fastify-table
|
|
2
|
+
|
|
3
|
+
<!--  -->
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@opengis/fastify-table)
|
|
6
|
+
[]()
|
|
7
|
+
[]()
|
|
8
|
+
[](http://standardjs.com/)
|
|
9
|
+
|
|
10
|
+
This is a backend toolkit for database management and more.
|
|
11
|
+
|
|
12
|
+
### Why fastify-table?
|
|
13
|
+
|
|
14
|
+
- **Logger** - Monitoring user activity and system status.
|
|
15
|
+
- **Redis cache** - Optimizes web page rendering for lightning-fast performance.
|
|
16
|
+
- **Secure** - Ensures protection against XSS attacks in queries.
|
|
17
|
+
- **SQL Migration** - Automatically adds all necessary tables to your project's database.
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Features
|
|
21
|
+
|
|
22
|
+
- CRUD
|
|
23
|
+
- cron
|
|
24
|
+
- Logger
|
|
25
|
+
- Redis
|
|
26
|
+
- pg
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
### Install
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
npm i @opengis/fastify-table
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Usage
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
fastify.register(import('@opengis/fastify-table'), config);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
### Documenation
|
|
45
|
+
|
|
46
|
+
For a detailed understanding `fastify-table`, its features, and how to use them, refer to our [Documentation](https://apidocs.softpro.ua/fastify-table/).
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
### Technology stack
|
|
51
|
+
|
|
52
|
+
<a href="https://fastify.dev/" target="_blank">
|
|
53
|
+
<img src="https://img.shields.io/badge/Fastify-323330?style=for-the-badge&logo=fastify" /></a>
|
|
54
|
+
|
|
55
|
+
<a href="https://www.postgresql.org/" target="_blank">
|
|
56
|
+
<img src="https://img.shields.io/badge/PostgreSQL-323330?style=for-the-badge&logo=postgresql&logoColor=white" /></a>
|
|
57
|
+
|
|
58
|
+
<a href="https://redis.io/" target="_blank">
|
|
59
|
+
<img src="https://img.shields.io/badge/redis-323330.svg?&style=for-the-badge&logo=redis&logoColor=" /></a>
|
|
60
|
+
|
|
61
|
+
<a href="https://www.npmjs.com/package/pino" target="_blank">
|
|
62
|
+
<img src="https://img.shields.io/badge/pino-323330.svg?&style=for-the-badge&logo=pino&logoColor=" /></a>
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
### Contribute
|
|
67
|
+
|
|
68
|
+
Feel free to contact us through our website [SOFTPRO.UA](https://softpro.ua) or email <info@softpro.ua>
|
|
69
|
+
|
|
70
|
+
- Report bugs
|
|
71
|
+
- Share your ideas
|
|
72
|
+
- Ask questions
|
|
73
|
+
|
|
74
|
+
### Follow Us
|
|
75
|
+
|
|
76
|
+
[Official site](https://softpro.ua)
|
|
77
|
+
|
|
78
|
+
<p>
|
|
79
|
+
<a href="https://www.instagram.com/gissoftpro/" target="_blank"><img src="https://cdn.softpro.ua/data/npm/instagram.png" alt="Softpro Instagram" title="oftpro Instagram"></a>
|
|
80
|
+
<a href="https://www.facebook.com/gissoftpro" target="_blank"><img src="https://cdn.softpro.ua/data/npm/facebook.png" alt="Softpro Facebook" title="Softpro Facebook"></a>
|
|
81
|
+
<a href="https://t.me/softprogis" target="_blank"><img src="https://cdn.softpro.ua/data/npm/telegram.png" alt="Softpro Telegram" title="Softpro Telegram"></a>
|
|
82
|
+
<a href="https://www.linkedin.com/in/softpro-ukraine-a8876b282/recent-activity/all/" target="_blank"><img src="https://cdn.softpro.ua/data/npm/social/linkedin.png" alt="Softpro Linkedin" title="Softpro LinkedIn"></a>
|
|
83
|
+
</p>
|
|
84
|
+
|
|
85
|
+
### License
|
|
86
|
+
|
|
87
87
|
Copyright © SOFTPRO. All rights reserved.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opengis/fastify-table",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.97",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "core-plugins",
|
|
6
6
|
"keywords": [
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"@fastify/http-proxy": "9.5.0",
|
|
32
32
|
"@fastify/sensible": "^5.0.0",
|
|
33
33
|
"@fastify/url-data": "5.4.0",
|
|
34
|
-
"@opengis/fastify-hb": "1.7.
|
|
34
|
+
"@opengis/fastify-hb": "^1.7.1",
|
|
35
35
|
"fastify": "^4.26.1",
|
|
36
36
|
"fastify-plugin": "^4.0.0",
|
|
37
37
|
"ioredis": "5.3.2",
|
|
@@ -25,6 +25,7 @@ ALTER TABLE admin.cls ALTER COLUMN uid SET NOT NULL;
|
|
|
25
25
|
ALTER TABLE admin.cls ALTER COLUMN uid SET DEFAULT '1'::text;
|
|
26
26
|
ALTER TABLE admin.cls ADD COLUMN IF NOT EXISTS icon text;
|
|
27
27
|
ALTER TABLE admin.cls ADD COLUMN IF NOT EXISTS color text;
|
|
28
|
+
ALTER TABLE admin.cls ADD COLUMN IF NOT EXISTS hash text;
|
|
28
29
|
|
|
29
30
|
ALTER TABLE admin.cls ADD CONSTRAINT admin_cls_pkey PRIMARY KEY (clsid);
|
|
30
31
|
ALTER TABLE admin.cls ADD CONSTRAINT admin_cls_unique UNIQUE (code, parent);
|
|
@@ -1,130 +1,130 @@
|
|
|
1
|
-
import { createHash } from 'crypto';
|
|
2
|
-
|
|
3
|
-
import config from '../../../../config.js';
|
|
4
|
-
import getRedis from '../../redis/funcs/getRedis.js';
|
|
5
|
-
import getPG from '../../pg/funcs/getPG.js';
|
|
6
|
-
import logger from '../../logger/getLogger.js';
|
|
7
|
-
import cronList from '../cronList.js';
|
|
8
|
-
|
|
9
|
-
const md5 = (string) => createHash('md5').update(string).digest('hex');
|
|
10
|
-
|
|
11
|
-
const rclient = getRedis();
|
|
12
|
-
async function verifyUnique(name) {
|
|
13
|
-
const cronId = config.port || 3000 + md5(name);
|
|
14
|
-
// one per node check
|
|
15
|
-
const key = `cron:unique:${cronId}`;
|
|
16
|
-
const unique = await rclient.setnx(key, 1);
|
|
17
|
-
const ttl = await rclient.ttl(key);
|
|
18
|
-
if (!unique && ttl !== -1) {
|
|
19
|
-
return false;
|
|
20
|
-
}
|
|
21
|
-
await rclient.expire(key, 20);
|
|
22
|
-
return true;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const intervalStringMs = {
|
|
26
|
-
everyMin: 1000 * 60,
|
|
27
|
-
tenMin: 1000 * 60 * 10,
|
|
28
|
-
everyHour: 1000 * 60 * 60,
|
|
29
|
-
isHalfday: 1000 * 60 * 60 * 12,
|
|
30
|
-
dailyHour: 1000 * 60 * 60 * 24,
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
const interval2ms = {
|
|
34
|
-
string: (interval) => {
|
|
35
|
-
const date = new Date();
|
|
36
|
-
const intervarSplit = interval.match(/^(\*{2}|(\*)?(\d{1,2})):(\*(\d)|(\d{2}))/);
|
|
37
|
-
if (!intervarSplit) {
|
|
38
|
-
throw new Error(`interval ${interval} not suported`);
|
|
39
|
-
}
|
|
40
|
-
const [, , isHalfday, dailyHour, , tenMin, HourlyMin] = intervarSplit;
|
|
41
|
-
const intervalMs = (isHalfday && intervalStringMs.isHalfday)
|
|
42
|
-
|| (dailyHour && intervalStringMs.dailyHour)
|
|
43
|
-
|| (tenMin && intervalStringMs.tenMin)
|
|
44
|
-
|| intervalStringMs.everyHour;
|
|
45
|
-
const offsetDay = ((+dailyHour || 0) * 60 + (+tenMin || +HourlyMin)) * 60 * 1000;
|
|
46
|
-
const offsetCur = (date - date.getTimezoneOffset() * 1000 * 60) % intervalMs;
|
|
47
|
-
const waitMs = (offsetDay - offsetCur + intervalMs) % intervalMs;
|
|
48
|
-
return [waitMs, intervalMs];
|
|
49
|
-
},
|
|
50
|
-
number: (interval) => {
|
|
51
|
-
const date = new Date();
|
|
52
|
-
const intervalMs = interval * 1000;
|
|
53
|
-
const dateWithTZ = date - date.getTimezoneOffset() * 1000 * 60;
|
|
54
|
-
const offsetCur = dateWithTZ % intervalMs;
|
|
55
|
-
// start every cron within 1 hour
|
|
56
|
-
const sixtyMinutesStartMs = 3600000;
|
|
57
|
-
const waitMs = (intervalMs - offsetCur) % sixtyMinutesStartMs;
|
|
58
|
-
return [waitMs, intervalMs];
|
|
59
|
-
},
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
async function runCron({
|
|
63
|
-
func, name,
|
|
64
|
-
}) {
|
|
65
|
-
const pg = getPG();
|
|
66
|
-
|
|
67
|
-
const unique = await verifyUnique(name);
|
|
68
|
-
|
|
69
|
-
if (!unique) return;
|
|
70
|
-
const db = pg.options.database;
|
|
71
|
-
|
|
72
|
-
try {
|
|
73
|
-
// console.log(`cron:${name}`);
|
|
74
|
-
const data = await func({ pg });
|
|
75
|
-
const subdir = !!data ? 'cron' : 'cron/null';
|
|
76
|
-
logger.file(subdir, {
|
|
77
|
-
db, name, result: data,
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
catch (err) {
|
|
81
|
-
logger.file('cron', {
|
|
82
|
-
db, name, error: err.toString(),
|
|
83
|
-
});
|
|
84
|
-
logger.error(err);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* interval:
|
|
90
|
-
* - 02:54 - every day
|
|
91
|
-
* - 2:03 - every day
|
|
92
|
-
* - *1:43 - 2 times a day
|
|
93
|
-
* - *12:03 - 2 times a day
|
|
94
|
-
* - **:54 - every hour
|
|
95
|
-
* - **:*3 - every 10 minutes
|
|
96
|
-
* - 60 - every minute
|
|
97
|
-
* - 10 * 60 - every 10 minutes
|
|
98
|
-
*/
|
|
99
|
-
|
|
100
|
-
export default async function addCron(func, interval) {
|
|
101
|
-
const { time = {}, disabled = [] } = config.cron || {};
|
|
102
|
-
|
|
103
|
-
const name = func.name || func.toString().split('/').at(-1).split('\'')[0];
|
|
104
|
-
|
|
105
|
-
// if (!config.isServer) return;
|
|
106
|
-
|
|
107
|
-
if (disabled.includes(name)) {
|
|
108
|
-
logger.file('cron', { name, message: 'cron disabled' });
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
cronList[name] = func;
|
|
113
|
-
|
|
114
|
-
const userInterval = time[name] || interval;
|
|
115
|
-
const [waitMs, intervalMs] = interval2ms[typeof interval](userInterval);
|
|
116
|
-
|
|
117
|
-
if (intervalMs < 1000) {
|
|
118
|
-
logger.file('cron', { name, error: `interval ${interval} to small` });
|
|
119
|
-
return;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// setTimeout to w8 for the time to start
|
|
123
|
-
setTimeout(() => {
|
|
124
|
-
runCron({ func, name });
|
|
125
|
-
// interval
|
|
126
|
-
setInterval(() => {
|
|
127
|
-
runCron({ func, name });
|
|
128
|
-
}, intervalMs);
|
|
129
|
-
}, waitMs);
|
|
130
|
-
}
|
|
1
|
+
import { createHash } from 'crypto';
|
|
2
|
+
|
|
3
|
+
import config from '../../../../config.js';
|
|
4
|
+
import getRedis from '../../redis/funcs/getRedis.js';
|
|
5
|
+
import getPG from '../../pg/funcs/getPG.js';
|
|
6
|
+
import logger from '../../logger/getLogger.js';
|
|
7
|
+
import cronList from '../cronList.js';
|
|
8
|
+
|
|
9
|
+
const md5 = (string) => createHash('md5').update(string).digest('hex');
|
|
10
|
+
|
|
11
|
+
const rclient = getRedis();
|
|
12
|
+
async function verifyUnique(name) {
|
|
13
|
+
const cronId = config.port || 3000 + md5(name);
|
|
14
|
+
// one per node check
|
|
15
|
+
const key = `cron:unique:${cronId}`;
|
|
16
|
+
const unique = await rclient.setnx(key, 1);
|
|
17
|
+
const ttl = await rclient.ttl(key);
|
|
18
|
+
if (!unique && ttl !== -1) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
await rclient.expire(key, 20);
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const intervalStringMs = {
|
|
26
|
+
everyMin: 1000 * 60,
|
|
27
|
+
tenMin: 1000 * 60 * 10,
|
|
28
|
+
everyHour: 1000 * 60 * 60,
|
|
29
|
+
isHalfday: 1000 * 60 * 60 * 12,
|
|
30
|
+
dailyHour: 1000 * 60 * 60 * 24,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const interval2ms = {
|
|
34
|
+
string: (interval) => {
|
|
35
|
+
const date = new Date();
|
|
36
|
+
const intervarSplit = interval.match(/^(\*{2}|(\*)?(\d{1,2})):(\*(\d)|(\d{2}))/);
|
|
37
|
+
if (!intervarSplit) {
|
|
38
|
+
throw new Error(`interval ${interval} not suported`);
|
|
39
|
+
}
|
|
40
|
+
const [, , isHalfday, dailyHour, , tenMin, HourlyMin] = intervarSplit;
|
|
41
|
+
const intervalMs = (isHalfday && intervalStringMs.isHalfday)
|
|
42
|
+
|| (dailyHour && intervalStringMs.dailyHour)
|
|
43
|
+
|| (tenMin && intervalStringMs.tenMin)
|
|
44
|
+
|| intervalStringMs.everyHour;
|
|
45
|
+
const offsetDay = ((+dailyHour || 0) * 60 + (+tenMin || +HourlyMin)) * 60 * 1000;
|
|
46
|
+
const offsetCur = (date - date.getTimezoneOffset() * 1000 * 60) % intervalMs;
|
|
47
|
+
const waitMs = (offsetDay - offsetCur + intervalMs) % intervalMs;
|
|
48
|
+
return [waitMs, intervalMs];
|
|
49
|
+
},
|
|
50
|
+
number: (interval) => {
|
|
51
|
+
const date = new Date();
|
|
52
|
+
const intervalMs = interval * 1000;
|
|
53
|
+
const dateWithTZ = date - date.getTimezoneOffset() * 1000 * 60;
|
|
54
|
+
const offsetCur = dateWithTZ % intervalMs;
|
|
55
|
+
// start every cron within 1 hour
|
|
56
|
+
const sixtyMinutesStartMs = 3600000;
|
|
57
|
+
const waitMs = (intervalMs - offsetCur) % sixtyMinutesStartMs;
|
|
58
|
+
return [waitMs, intervalMs];
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
async function runCron({
|
|
63
|
+
func, name,
|
|
64
|
+
}) {
|
|
65
|
+
const pg = getPG();
|
|
66
|
+
|
|
67
|
+
const unique = await verifyUnique(name);
|
|
68
|
+
|
|
69
|
+
if (!unique) return;
|
|
70
|
+
const db = pg.options.database;
|
|
71
|
+
|
|
72
|
+
try {
|
|
73
|
+
// console.log(`cron:${name}`);
|
|
74
|
+
const data = await func({ pg });
|
|
75
|
+
const subdir = !!data ? 'cron' : 'cron/null';
|
|
76
|
+
logger.file(subdir, {
|
|
77
|
+
db, name, result: data,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
catch (err) {
|
|
81
|
+
logger.file('cron', {
|
|
82
|
+
db, name, error: err.toString(),
|
|
83
|
+
});
|
|
84
|
+
logger.error(err);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* interval:
|
|
90
|
+
* - 02:54 - every day
|
|
91
|
+
* - 2:03 - every day
|
|
92
|
+
* - *1:43 - 2 times a day
|
|
93
|
+
* - *12:03 - 2 times a day
|
|
94
|
+
* - **:54 - every hour
|
|
95
|
+
* - **:*3 - every 10 minutes
|
|
96
|
+
* - 60 - every minute
|
|
97
|
+
* - 10 * 60 - every 10 minutes
|
|
98
|
+
*/
|
|
99
|
+
|
|
100
|
+
export default async function addCron(func, interval) {
|
|
101
|
+
const { time = {}, disabled = [] } = config.cron || {};
|
|
102
|
+
|
|
103
|
+
const name = func.name || func.toString().split('/').at(-1).split('\'')[0];
|
|
104
|
+
|
|
105
|
+
// if (!config.isServer) return;
|
|
106
|
+
|
|
107
|
+
if (disabled.includes(name)) {
|
|
108
|
+
logger.file('cron', { name, message: 'cron disabled' });
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
cronList[name] = func;
|
|
113
|
+
|
|
114
|
+
const userInterval = time[name] || interval;
|
|
115
|
+
const [waitMs, intervalMs] = interval2ms[typeof interval](userInterval);
|
|
116
|
+
|
|
117
|
+
if (intervalMs < 1000) {
|
|
118
|
+
logger.file('cron', { name, error: `interval ${interval} to small` });
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// setTimeout to w8 for the time to start
|
|
123
|
+
setTimeout(() => {
|
|
124
|
+
runCron({ func, name });
|
|
125
|
+
// interval
|
|
126
|
+
setInterval(() => {
|
|
127
|
+
runCron({ func, name });
|
|
128
|
+
}, intervalMs);
|
|
129
|
+
}, waitMs);
|
|
130
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import addCron from './funcs/addCron.js';
|
|
2
|
-
|
|
3
|
-
async function plugin(fastify) {
|
|
4
|
-
// fastify.decorate('addCron', addCron);
|
|
5
|
-
}
|
|
6
|
-
export default plugin;
|
|
1
|
+
import addCron from './funcs/addCron.js';
|
|
2
|
+
|
|
3
|
+
async function plugin(fastify) {
|
|
4
|
+
// fastify.decorate('addCron', addCron);
|
|
5
|
+
}
|
|
6
|
+
export default plugin;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import getRedis from '../../redis/funcs/getRedis.js';
|
|
2
|
-
|
|
3
|
-
// import { getRedis } from '../../../../utils.js';
|
|
4
|
-
|
|
5
|
-
export default async function getOpt(token, uid = 0) {
|
|
6
|
-
const rclient = getRedis({ db: 0 });
|
|
7
|
-
|
|
8
|
-
const key = `opt:${uid}:${token}`;
|
|
9
|
-
// console.log(key);
|
|
10
|
-
const data = await rclient.get(key);
|
|
11
|
-
if (!data) return null;
|
|
12
|
-
return JSON.parse(data);
|
|
13
|
-
}
|
|
1
|
+
import getRedis from '../../redis/funcs/getRedis.js';
|
|
2
|
+
|
|
3
|
+
// import { getRedis } from '../../../../utils.js';
|
|
4
|
+
|
|
5
|
+
export default async function getOpt(token, uid = 0) {
|
|
6
|
+
const rclient = getRedis({ db: 0 });
|
|
7
|
+
|
|
8
|
+
const key = `opt:${uid}:${token}`;
|
|
9
|
+
// console.log(key);
|
|
10
|
+
const data = await rclient.get(key);
|
|
11
|
+
if (!data) return null;
|
|
12
|
+
return JSON.parse(data);
|
|
13
|
+
}
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import { createHash, randomUUID } from 'crypto';
|
|
2
|
-
|
|
3
|
-
const random = randomUUID();
|
|
4
|
-
|
|
5
|
-
import getRedis from '../../redis/funcs/getRedis.js';
|
|
6
|
-
|
|
7
|
-
// import { getRedis } from '../../../../utils.js';
|
|
8
|
-
|
|
9
|
-
function md5(string) {
|
|
10
|
-
return createHash('md5').update(string).digest('hex');
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export default function setOpt(params, uid = 0) {
|
|
14
|
-
const token = Buffer.from(md5(typeof params === 'object' ? JSON.stringify(params) : params) + random, 'hex').toString('base64').replace(/[+-=]+/g, '');
|
|
15
|
-
// const token = md5(params);
|
|
16
|
-
const key = `opt:${uid}:${token}`;
|
|
17
|
-
|
|
18
|
-
const rclient = getRedis({ db: 0 });
|
|
19
|
-
rclient.set(key, JSON.stringify(params), 'EX', 60 * 60);
|
|
20
|
-
return token;
|
|
21
|
-
}
|
|
1
|
+
import { createHash, randomUUID } from 'crypto';
|
|
2
|
+
|
|
3
|
+
const random = randomUUID();
|
|
4
|
+
|
|
5
|
+
import getRedis from '../../redis/funcs/getRedis.js';
|
|
6
|
+
|
|
7
|
+
// import { getRedis } from '../../../../utils.js';
|
|
8
|
+
|
|
9
|
+
function md5(string) {
|
|
10
|
+
return createHash('md5').update(string).digest('hex');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export default function setOpt(params, uid = 0) {
|
|
14
|
+
const token = Buffer.from(md5(typeof params === 'object' ? JSON.stringify(params) : params) + random, 'hex').toString('base64').replace(/[+-=]+/g, '');
|
|
15
|
+
// const token = md5(params);
|
|
16
|
+
const key = `opt:${uid}:${token}`;
|
|
17
|
+
|
|
18
|
+
const rclient = getRedis({ db: 0 });
|
|
19
|
+
rclient.set(key, JSON.stringify(params), 'EX', 60 * 60);
|
|
20
|
+
return token;
|
|
21
|
+
}
|
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
import { createHash, randomUUID } from 'crypto';
|
|
2
|
-
|
|
3
|
-
import config from '../../../../config.js';
|
|
4
|
-
|
|
5
|
-
import getRedis from '../../redis/funcs/getRedis.js';
|
|
6
|
-
|
|
7
|
-
const rclient = getRedis({ db: 0 });
|
|
8
|
-
|
|
9
|
-
// import { config, getRedis } from '../../../../utils.js';
|
|
10
|
-
|
|
11
|
-
const generateCodes = (ids, userToken) => {
|
|
12
|
-
const token = userToken || randomUUID();
|
|
13
|
-
const notNullIds = ids.filter((el) => el);
|
|
14
|
-
const obj = {};
|
|
15
|
-
const codes = notNullIds.reduce((acc, id) => {
|
|
16
|
-
const newToken = createHash('sha1').update(token + id).digest('base64url').replace(/-/g, '');
|
|
17
|
-
acc[newToken] = id; obj[id] = newToken;
|
|
18
|
-
return acc;
|
|
19
|
-
}, {});
|
|
20
|
-
return { codes, obj };
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
function setToken({
|
|
24
|
-
ids: idsOrigin, uid, array,
|
|
25
|
-
}) {
|
|
26
|
-
// const rclient5 = getRedis({ db: 0, funcs });
|
|
27
|
-
|
|
28
|
-
if (!uid) return { user: 'empty' };
|
|
29
|
-
if (!Object.keys(idsOrigin).length) return { ids: 'empty' };
|
|
30
|
-
|
|
31
|
-
const ids = idsOrigin.map((el) => (typeof el === 'object' ? JSON.stringify(el) : el));
|
|
32
|
-
|
|
33
|
-
// TODO generate salt
|
|
34
|
-
const { codes, obj } = generateCodes(ids, uid);
|
|
35
|
-
|
|
36
|
-
if (!Object.keys(codes).length) return { ids: 'empty' };
|
|
37
|
-
|
|
38
|
-
rclient.hmset(`${config.pg.database}:token:edit:${uid}`, codes);
|
|
39
|
-
// console.log(`${config.pg.database}:token:edit:${uid}`, idsOrigin, Object.values(obj));
|
|
40
|
-
// TODO дополнительно писать в hset token -> uid
|
|
41
|
-
return array ? Object.values(obj) : obj;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export default setToken;
|
|
1
|
+
import { createHash, randomUUID } from 'crypto';
|
|
2
|
+
|
|
3
|
+
import config from '../../../../config.js';
|
|
4
|
+
|
|
5
|
+
import getRedis from '../../redis/funcs/getRedis.js';
|
|
6
|
+
|
|
7
|
+
const rclient = getRedis({ db: 0 });
|
|
8
|
+
|
|
9
|
+
// import { config, getRedis } from '../../../../utils.js';
|
|
10
|
+
|
|
11
|
+
const generateCodes = (ids, userToken) => {
|
|
12
|
+
const token = userToken || randomUUID();
|
|
13
|
+
const notNullIds = ids.filter((el) => el);
|
|
14
|
+
const obj = {};
|
|
15
|
+
const codes = notNullIds.reduce((acc, id) => {
|
|
16
|
+
const newToken = createHash('sha1').update(token + id).digest('base64url').replace(/-/g, '');
|
|
17
|
+
acc[newToken] = id; obj[id] = newToken;
|
|
18
|
+
return acc;
|
|
19
|
+
}, {});
|
|
20
|
+
return { codes, obj };
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
function setToken({
|
|
24
|
+
ids: idsOrigin, uid, array,
|
|
25
|
+
}) {
|
|
26
|
+
// const rclient5 = getRedis({ db: 0, funcs });
|
|
27
|
+
|
|
28
|
+
if (!uid) return { user: 'empty' };
|
|
29
|
+
if (!Object.keys(idsOrigin).length) return { ids: 'empty' };
|
|
30
|
+
|
|
31
|
+
const ids = idsOrigin.map((el) => (typeof el === 'object' ? JSON.stringify(el) : el));
|
|
32
|
+
|
|
33
|
+
// TODO generate salt
|
|
34
|
+
const { codes, obj } = generateCodes(ids, uid);
|
|
35
|
+
|
|
36
|
+
if (!Object.keys(codes).length) return { ids: 'empty' };
|
|
37
|
+
|
|
38
|
+
rclient.hmset(`${config.pg.database}:token:edit:${uid}`, codes);
|
|
39
|
+
// console.log(`${config.pg.database}:token:edit:${uid}`, idsOrigin, Object.values(obj));
|
|
40
|
+
// TODO дополнительно писать в hset token -> uid
|
|
41
|
+
return array ? Object.values(obj) : obj;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export default setToken;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
|
-
|
|
3
|
-
import config from '../../../../../config.js';
|
|
4
|
-
|
|
5
|
-
export default function getFolder(req, type = 'server') {
|
|
6
|
-
if (!['server', 'local'].includes(type)) throw new Error('params type is invalid');
|
|
7
|
-
const types = { local: req.root || config.root, server: req.mapServerRoot || config.mapServerRoot };
|
|
8
|
-
const dbname = req.pg?.options?.database || req.pg?.database || config.pg?.database; // request / config params / default config params
|
|
9
|
-
const filepath = path.posix.join(types[type] || `/data/local/${dbname || ''}`, req.folder || config.folder || '');
|
|
10
|
-
return filepath;
|
|
11
|
-
}
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
|
|
3
|
+
import config from '../../../../../config.js';
|
|
4
|
+
|
|
5
|
+
export default function getFolder(req, type = 'server') {
|
|
6
|
+
if (!['server', 'local'].includes(type)) throw new Error('params type is invalid');
|
|
7
|
+
const types = { local: req.root || config.root, server: req.mapServerRoot || config.mapServerRoot };
|
|
8
|
+
const dbname = req.pg?.options?.database || req.pg?.database || config.pg?.database; // request / config params / default config params
|
|
9
|
+
const filepath = path.posix.join(types[type] || `/data/local/${dbname || ''}`, req.folder || config.folder || '');
|
|
10
|
+
return filepath;
|
|
11
|
+
}
|