@opengis/fastify-table 1.3.19 → 1.3.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.
Files changed (39) hide show
  1. package/README.md +86 -86
  2. package/package.json +1 -1
  3. package/server/plugins/cron/funcs/addCron.js +52 -52
  4. package/server/plugins/cron/index.js +74 -74
  5. package/server/plugins/crud/funcs/getOpt.js +13 -13
  6. package/server/plugins/crud/funcs/setOpt.js +21 -21
  7. package/server/plugins/crud/funcs/setToken.js +44 -44
  8. package/server/plugins/crud/funcs/utils/getFolder.js +11 -11
  9. package/server/plugins/crud/index.js +23 -23
  10. package/server/plugins/hook/index.js +8 -8
  11. package/server/plugins/logger/errorStatus.js +19 -19
  12. package/server/plugins/logger/index.js +26 -26
  13. package/server/plugins/migration/index.js +7 -7
  14. package/server/plugins/policy/sqlInjection.js +33 -33
  15. package/server/plugins/redis/client.js +8 -8
  16. package/server/plugins/redis/funcs/redisClients.js +3 -3
  17. package/server/plugins/redis/index.js +17 -17
  18. package/server/plugins/table/funcs/getFilterSQL/util/getCustomQuery.js +13 -13
  19. package/server/plugins/table/funcs/getFilterSQL/util/getTableSql.js +34 -34
  20. package/server/plugins/table/funcs/getTemplates.js +19 -19
  21. package/server/plugins/table/funcs/gisIRColumn.js +82 -82
  22. package/server/plugins/table/funcs/loadTemplate.js +1 -1
  23. package/server/plugins/table/funcs/loadTemplatePath.js +1 -1
  24. package/server/plugins/table/funcs/userTemplateDir.js +1 -1
  25. package/server/plugins/table/index.js +13 -13
  26. package/server/plugins/util/index.js +7 -7
  27. package/server/routes/cron/index.js +14 -14
  28. package/server/routes/logger/controllers/logger.file.js +92 -92
  29. package/server/routes/logger/controllers/utils/checkUserAccess.js +19 -19
  30. package/server/routes/logger/controllers/utils/getRootDir.js +26 -26
  31. package/server/routes/logger/index.js +17 -17
  32. package/server/routes/properties/controllers/properties.add.js +55 -55
  33. package/server/routes/properties/controllers/properties.get.js +17 -17
  34. package/server/routes/properties/index.js +16 -16
  35. package/server/routes/table/controllers/form.js +42 -42
  36. package/server/routes/table/controllers/search.js +74 -74
  37. package/server/routes/table/schema.js +64 -64
  38. package/server/routes/util/controllers/status.monitor.js +8 -8
  39. package/server/routes/util/index.js +1 -1
package/README.md CHANGED
@@ -1,87 +1,87 @@
1
- # fastify-table
2
-
3
- <!-- ![alt text](https://cdn.softpro.ua/data/npm/admin/opengis-admin.png) -->
4
-
5
- [![NPM version](https://img.shields.io/npm/v/@opengis/fastify-table?style=plain)](https://www.npmjs.com/package/@opengis/fastify-table)
6
- [![NPM last update](https://img.shields.io/npm/last-update/@opengis/fastify-table?style=plain)]()
7
- [![NPM downloads](https://img.shields.io/npm/dw/@opengis/fastify-table?style=plain)]()
8
- [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=plain)](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>&nbsp;&nbsp;
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>&nbsp;&nbsp;
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>&nbsp;&nbsp;
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>&nbsp;&nbsp;
83
- </p>
84
-
85
- ### License
86
-
1
+ # fastify-table
2
+
3
+ <!-- ![alt text](https://cdn.softpro.ua/data/npm/admin/opengis-admin.png) -->
4
+
5
+ [![NPM version](https://img.shields.io/npm/v/@opengis/fastify-table?style=plain)](https://www.npmjs.com/package/@opengis/fastify-table)
6
+ [![NPM last update](https://img.shields.io/npm/last-update/@opengis/fastify-table?style=plain)]()
7
+ [![NPM downloads](https://img.shields.io/npm/dw/@opengis/fastify-table?style=plain)]()
8
+ [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=plain)](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>&nbsp;&nbsp;
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>&nbsp;&nbsp;
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>&nbsp;&nbsp;
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>&nbsp;&nbsp;
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.3.19",
3
+ "version": "1.3.20",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -1,52 +1,52 @@
1
- import config from '../../../../config.js';
2
-
3
- import logger from '../../logger/getLogger.js';
4
- import pgClients from '../../pg/pgClients.js';
5
-
6
- import cronList from '../cronList.js';
7
- import runCron from './runCron.js';
8
- import interval2ms from './interval2ms.js';
9
-
10
- /**
11
- * interval:
12
- * - 02:54 - every day
13
- * - 2:03 - every day
14
- * - *1:43 - 2 times a day
15
- * - *12:03 - 2 times a day
16
- * - **:54 - every hour
17
- * - **:*3 - every 10 minutes
18
- * - 60 - every minute
19
- * - 10 * 60 - every 10 minutes
20
- */
21
-
22
- export default async function addCron(func, interval, pg = pgClients.client) {
23
- const { time = {}, disabled = [] } = config.cron || {};
24
-
25
- const name = func.name || func.toString().split('/').at(-1).split('\'')[0];
26
-
27
- // if (!config.isServer) return;
28
-
29
- if (disabled.includes(name)) {
30
- logger.file('cron', { name, message: 'cron disabled' });
31
- return;
32
- }
33
-
34
- cronList[name] = func;
35
-
36
- const userInterval = time[name] || interval;
37
- const [waitMs, intervalMs] = interval2ms[typeof interval](userInterval);
38
-
39
- if (intervalMs < 1000) {
40
- logger.file('cron', { name, error: `interval ${interval} too small` });
41
- return;
42
- }
43
-
44
- // setTimeout to w8 for the time to start
45
- setTimeout(() => {
46
- runCron({ pg, func, name });
47
- // interval
48
- setInterval(() => {
49
- runCron({ pg, func, name });
50
- }, intervalMs);
51
- }, waitMs);
52
- }
1
+ import config from '../../../../config.js';
2
+
3
+ import logger from '../../logger/getLogger.js';
4
+ import pgClients from '../../pg/pgClients.js';
5
+
6
+ import cronList from '../cronList.js';
7
+ import runCron from './runCron.js';
8
+ import interval2ms from './interval2ms.js';
9
+
10
+ /**
11
+ * interval:
12
+ * - 02:54 - every day
13
+ * - 2:03 - every day
14
+ * - *1:43 - 2 times a day
15
+ * - *12:03 - 2 times a day
16
+ * - **:54 - every hour
17
+ * - **:*3 - every 10 minutes
18
+ * - 60 - every minute
19
+ * - 10 * 60 - every 10 minutes
20
+ */
21
+
22
+ export default async function addCron(func, interval, pg = pgClients.client) {
23
+ const { time = {}, disabled = [] } = config.cron || {};
24
+
25
+ const name = func.name || func.toString().split('/').at(-1).split('\'')[0];
26
+
27
+ // if (!config.isServer) return;
28
+
29
+ if (disabled.includes(name)) {
30
+ logger.file('cron', { name, message: 'cron disabled' });
31
+ return;
32
+ }
33
+
34
+ cronList[name] = func;
35
+
36
+ const userInterval = time[name] || interval;
37
+ const [waitMs, intervalMs] = interval2ms[typeof interval](userInterval);
38
+
39
+ if (intervalMs < 1000) {
40
+ logger.file('cron', { name, error: `interval ${interval} too small` });
41
+ return;
42
+ }
43
+
44
+ // setTimeout to w8 for the time to start
45
+ setTimeout(() => {
46
+ runCron({ pg, func, name });
47
+ // interval
48
+ setInterval(() => {
49
+ runCron({ pg, func, name });
50
+ }, intervalMs);
51
+ }, waitMs);
52
+ }
@@ -1,74 +1,74 @@
1
- import { createHash } from 'node:crypto';
2
-
3
- import config from "../../../config.js";
4
-
5
- import getPG from "../pg/funcs/getPG.js";
6
- import pgClients from "../pg/pgClients.js";
7
- import getRedis from '../redis/funcs/getRedis.js';
8
- import logger from "../logger/getLogger.js";
9
- import interval2ms from "./funcs/interval2ms.js";
10
-
11
- const rclient = getRedis();
12
-
13
- async function runCron({
14
- pg = pgClients.client, query, name,
15
- }) {
16
- const db = pg.options.database;
17
-
18
- // verifyUnique
19
- const key = `cron:unique:${name}`;
20
- const unique = await rclient.setnx(key, 1);
21
- const ttl = await rclient.ttl(key);
22
-
23
- if (!unique && ttl !== -1) {
24
- // if (config.trace) console.log(name, db, query, 'skip unique');
25
- return;
26
- }
27
-
28
- await rclient.expire(key, 20);
29
-
30
- try {
31
- if (!pg.pk) await pg.init();
32
-
33
- if (config.trace) console.time(`${db}:${query}`);
34
- const { command, rows = [], rowCount } = await pg.query(query);
35
- if (config.trace) console.timeEnd(`${db}:${query}`);
36
-
37
- logger.file('cron', { db, name, result: { command, rows, rowCount } });
38
- }
39
- catch (err) {
40
- if (config.trace) console.error(name, err.toString());
41
- logger.file('cron/error', { db, name, error: err.toString() });
42
- logger.error(err);
43
- }
44
- }
45
-
46
- async function plugin(fastify) {
47
- if (config.cronList?.length) {
48
- config.cronList?.filter(el => el.query && !el.disabled)?.forEach?.((el, idx) => {
49
- const { interval, db, query } = el;
50
- const name = createHash('md5').update(`${config.port || 3000}:${db}:${query}`).digest('hex');
51
- const pg = getPG(db);
52
-
53
- if (config.trace) console.log('cron-list: init', db, idx);
54
-
55
- const [waitMs, intervalMs] = interval2ms[typeof interval](interval);
56
-
57
- if (intervalMs < 1000) {
58
- if (config.trace) console.error('cron-list: skip too small interval', db, idx);
59
- logger.file('cron', { name, error: `interval ${interval} too small` });
60
- return;
61
- }
62
-
63
- // setTimeout to w8 for the time to start
64
- setTimeout(() => {
65
- runCron({ pg, query, name });
66
- // interval
67
- setInterval(() => {
68
- runCron({ pg, query, name });
69
- }, intervalMs);
70
- }, waitMs);
71
- });
72
- }
73
- }
74
- export default plugin;
1
+ import { createHash } from 'node:crypto';
2
+
3
+ import config from "../../../config.js";
4
+
5
+ import getPG from "../pg/funcs/getPG.js";
6
+ import pgClients from "../pg/pgClients.js";
7
+ import getRedis from '../redis/funcs/getRedis.js';
8
+ import logger from "../logger/getLogger.js";
9
+ import interval2ms from "./funcs/interval2ms.js";
10
+
11
+ const rclient = getRedis();
12
+
13
+ async function runCron({
14
+ pg = pgClients.client, query, name,
15
+ }) {
16
+ const db = pg.options.database;
17
+
18
+ // verifyUnique
19
+ const key = `cron:unique:${name}`;
20
+ const unique = await rclient.setnx(key, 1);
21
+ const ttl = await rclient.ttl(key);
22
+
23
+ if (!unique && ttl !== -1) {
24
+ // if (config.trace) console.log(name, db, query, 'skip unique');
25
+ return;
26
+ }
27
+
28
+ await rclient.expire(key, 20);
29
+
30
+ try {
31
+ if (!pg.pk) await pg.init();
32
+
33
+ if (config.trace) console.time(`${db}:${query}`);
34
+ const { command, rows = [], rowCount } = await pg.query(query);
35
+ if (config.trace) console.timeEnd(`${db}:${query}`);
36
+
37
+ logger.file('cron', { db, name, result: { command, rows, rowCount } });
38
+ }
39
+ catch (err) {
40
+ if (config.trace) console.error(name, err.toString());
41
+ logger.file('cron/error', { db, name, error: err.toString() });
42
+ logger.error(err);
43
+ }
44
+ }
45
+
46
+ async function plugin(fastify) {
47
+ if (config.cronList?.length) {
48
+ config.cronList?.filter(el => el.query && !el.disabled)?.forEach?.((el, idx) => {
49
+ const { interval, db, query } = el;
50
+ const name = createHash('md5').update(`${config.port || 3000}:${db}:${query}`).digest('hex');
51
+ const pg = getPG(db);
52
+
53
+ if (config.trace) console.log('cron-list: init', db, idx);
54
+
55
+ const [waitMs, intervalMs] = interval2ms[typeof interval](interval);
56
+
57
+ if (intervalMs < 1000) {
58
+ if (config.trace) console.error('cron-list: skip too small interval', db, idx);
59
+ logger.file('cron', { name, error: `interval ${interval} too small` });
60
+ return;
61
+ }
62
+
63
+ // setTimeout to w8 for the time to start
64
+ setTimeout(() => {
65
+ runCron({ pg, query, name });
66
+ // interval
67
+ setInterval(() => {
68
+ runCron({ pg, query, name });
69
+ }, intervalMs);
70
+ }, waitMs);
71
+ });
72
+ }
73
+ }
74
+ 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
+ }