@opengis/fastify-table 1.3.18 → 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 (42) hide show
  1. package/README.md +86 -86
  2. package/package.json +1 -1
  3. package/server/plugins/cron/funcs/addCron.js +52 -130
  4. package/server/plugins/cron/funcs/interval2ms.js +40 -0
  5. package/server/plugins/cron/funcs/runCron.js +23 -0
  6. package/server/plugins/cron/funcs/verifyUnique.js +22 -0
  7. package/server/plugins/cron/index.js +74 -6
  8. package/server/plugins/crud/funcs/getOpt.js +13 -13
  9. package/server/plugins/crud/funcs/setOpt.js +21 -21
  10. package/server/plugins/crud/funcs/setToken.js +44 -44
  11. package/server/plugins/crud/funcs/utils/getFolder.js +11 -11
  12. package/server/plugins/crud/index.js +23 -23
  13. package/server/plugins/hook/index.js +8 -8
  14. package/server/plugins/logger/errorStatus.js +19 -19
  15. package/server/plugins/logger/index.js +26 -26
  16. package/server/plugins/migration/index.js +7 -7
  17. package/server/plugins/policy/sqlInjection.js +33 -33
  18. package/server/plugins/redis/client.js +8 -8
  19. package/server/plugins/redis/funcs/redisClients.js +3 -3
  20. package/server/plugins/redis/index.js +17 -17
  21. package/server/plugins/table/funcs/getFilterSQL/util/getCustomQuery.js +13 -13
  22. package/server/plugins/table/funcs/getFilterSQL/util/getTableSql.js +34 -34
  23. package/server/plugins/table/funcs/getTemplates.js +19 -19
  24. package/server/plugins/table/funcs/gisIRColumn.js +82 -82
  25. package/server/plugins/table/funcs/loadTemplate.js +1 -1
  26. package/server/plugins/table/funcs/loadTemplatePath.js +1 -1
  27. package/server/plugins/table/funcs/userTemplateDir.js +1 -1
  28. package/server/plugins/table/index.js +13 -13
  29. package/server/plugins/util/index.js +7 -7
  30. package/server/routes/cron/index.js +14 -14
  31. package/server/routes/logger/controllers/logger.file.js +92 -92
  32. package/server/routes/logger/controllers/utils/checkUserAccess.js +19 -19
  33. package/server/routes/logger/controllers/utils/getRootDir.js +26 -26
  34. package/server/routes/logger/index.js +17 -17
  35. package/server/routes/properties/controllers/properties.add.js +55 -55
  36. package/server/routes/properties/controllers/properties.get.js +17 -17
  37. package/server/routes/properties/index.js +16 -16
  38. package/server/routes/table/controllers/form.js +42 -42
  39. package/server/routes/table/controllers/search.js +74 -74
  40. package/server/routes/table/schema.js +64 -64
  41. package/server/routes/util/controllers/status.monitor.js +8 -8
  42. 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.18",
3
+ "version": "1.3.20",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -1,130 +1,52 @@
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 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
+ }
@@ -0,0 +1,40 @@
1
+ const intervalStringMs = {
2
+ everyMin: 1000 * 60,
3
+ tenMin: 1000 * 60 * 10,
4
+ everyHour: 1000 * 60 * 60,
5
+ isHalfday: 1000 * 60 * 60 * 12,
6
+ dailyHour: 1000 * 60 * 60 * 24,
7
+ };
8
+
9
+ const interval2ms = {
10
+ string: (interval) => {
11
+ const date = new Date();
12
+ const intervarSplit = interval.match(/^(\*{2}|(\*)?(\d{1,2})):(\*(\d)|(\d{2}))/);
13
+
14
+ if (!intervarSplit) {
15
+ throw new Error(`interval ${interval} not suported`);
16
+ }
17
+
18
+ const [, , isHalfday, dailyHour, , tenMin, HourlyMin] = intervarSplit;
19
+ const intervalMs = (isHalfday && intervalStringMs.isHalfday)
20
+ || (dailyHour && intervalStringMs.dailyHour)
21
+ || (tenMin && intervalStringMs.tenMin)
22
+ || intervalStringMs.everyHour;
23
+ const offsetDay = ((+dailyHour || 0) * 60 + (+tenMin || +HourlyMin)) * 60 * 1000;
24
+ const offsetCur = (date - date.getTimezoneOffset() * 1000 * 60) % intervalMs;
25
+ const waitMs = (offsetDay - offsetCur + intervalMs) % intervalMs;
26
+ return [waitMs, intervalMs];
27
+ },
28
+ number: (interval) => {
29
+ const date = new Date();
30
+ const intervalMs = interval * 1000;
31
+ const dateWithTZ = date - date.getTimezoneOffset() * 1000 * 60;
32
+ const offsetCur = dateWithTZ % intervalMs;
33
+ // start every cron within 1 hour
34
+ const sixtyMinutesStartMs = 3600000;
35
+ const waitMs = (intervalMs - offsetCur) % sixtyMinutesStartMs;
36
+ return [waitMs, intervalMs];
37
+ },
38
+ };
39
+
40
+ export default interval2ms;
@@ -0,0 +1,23 @@
1
+ import logger from '../../logger/getLogger.js';
2
+ import pgClients from '../../pg/pgClients.js';
3
+
4
+ import verifyUnique from './verifyUnique.js';
5
+
6
+ export default async function runCron({
7
+ pg = pgClients.client, func, name,
8
+ }) {
9
+ const unique = await verifyUnique(name);
10
+ if (!unique) return;
11
+
12
+ const db = pg.options.database;
13
+
14
+ try {
15
+ const data = await func({ pg });
16
+ const subdir = !!data ? 'cron' : 'cron/null';
17
+ logger.file(subdir, { db, name, result: data });
18
+ }
19
+ catch (err) {
20
+ logger.file('cron', { db, name, error: err.toString() });
21
+ logger.error(err);
22
+ }
23
+ }
@@ -0,0 +1,22 @@
1
+ import { createHash } from 'node:crypto';
2
+
3
+ import config from '../../../../config.js';
4
+
5
+ import getRedis from '../../redis/funcs/getRedis.js';
6
+
7
+ const md5 = (string) => createHash('md5').update(string).digest('hex');
8
+
9
+ const rclient = getRedis();
10
+
11
+ export default async function verifyUnique(name) {
12
+ const cronId = config.port || 3000 + md5(name);
13
+ // one per node check
14
+ const key = `cron:unique:${cronId}`;
15
+ const unique = await rclient.setnx(key, 1);
16
+ const ttl = await rclient.ttl(key);
17
+ if (!unique && ttl !== -1) {
18
+ return false;
19
+ }
20
+ await rclient.expire(key, 20);
21
+ return true;
22
+ }
@@ -1,6 +1,74 @@
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 { 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
+ }