@opengis/fastify-table 1.3.65 → 1.3.66

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "1.3.65",
3
+ "version": "1.3.66",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -122,7 +122,7 @@ export default async function loggerSystem(req) {
122
122
  cpu: cpuInfo[0].model,
123
123
  ram: formatMemoryUsage(totalMemory),
124
124
  db: dbVerion,
125
- redis: rclient.server_info?.redis_version,
125
+ redis: rclient?.server_info?.redis_version,
126
126
  node: process.version,
127
127
  },
128
128
  top: topProcess,
@@ -0,0 +1,87 @@
1
+ import path from 'node:path';
2
+ import { readdirSync, readFileSync } from 'node:fs';
3
+ import yaml from 'js-yaml';
4
+
5
+ import getTemplatePath from './getTemplatePath.js';
6
+ import loadTemplate from './loadTemplate.js';
7
+ import mdToHTML from '../../md/funcs/mdToHTML.js';
8
+
9
+ function readFileData(file) {
10
+ const data = readFileSync(file, 'utf-8');
11
+ const ext = file.substring(file.lastIndexOf('.') + 1);
12
+
13
+ if (ext === 'yml') {
14
+ return yaml.load(data);
15
+ }
16
+ if (ext === 'json') {
17
+ return JSON.parse(data);
18
+ }
19
+ if (ext === 'md') {
20
+ const match = data.match(/^---\r?\n([\s\S]*?)---\r?\n([\s\S]*)$/);
21
+
22
+ const parseLinesRecursively = (lines, index = 0, result = {}) => (index >= lines.length
23
+ ? result
24
+ : (([key, ...rest]) => (key && rest.length
25
+ ? parseLinesRecursively(lines, index + 1, { ...result, [key.trim()]: rest.join(':').trim() })
26
+ : parseLinesRecursively(lines, index + 1, result)))((lines[index] || '').split(':')));
27
+
28
+ const header = parseLinesRecursively(match[1]?.split?.(/\r?\n/) || []);
29
+ const body = match[2]?.replace(/\r\n/g, '')?.startsWith('```html') ? match[2]?.match?.(/```html\r?\n([\s\S]*?)```/)?.[1] : match[2];
30
+
31
+ return { ...header, html: mdToHTML(body) };
32
+ }
33
+ return data;
34
+ }
35
+
36
+ const isProduction = process.env.NODE_ENV === 'production';
37
+
38
+ function getTemplateData(template) {
39
+ // dir template: dashboard, card
40
+ if (template[0][3]) {
41
+ const files = readdirSync(template[0][1]);
42
+ const data = files.map(async el => readFileData(path.join(template[0][1], el)));
43
+ return files.map((el, i) => [el, data[i]]);
44
+ }
45
+
46
+ // one file template: table, form
47
+ if (template.length === 1) {
48
+ const data = readFileData(template[0][1]);
49
+ return data;
50
+ }
51
+
52
+ // multi file template: select, etc
53
+ if (template.length > 1) {
54
+ const data = template.map(el => readFileData(el[1]));
55
+ if (Array.isArray(data[0])) {
56
+ return data[0];
57
+ }
58
+ const result = {};
59
+ template.forEach((el, i) => {
60
+ Object.assign(result, typeof data[i] === 'object' ? data[i] : { [el[2]]: data[i] });
61
+ });
62
+ return result;
63
+ }
64
+ return null;
65
+ }
66
+ export default function getTemplateSync(type, name) {
67
+ if (!type) return null;
68
+ if (!name) return null;
69
+
70
+ const key = `${type}:${name}`;
71
+ if (name === 'cache' && !isProduction) return loadTemplate; // all cache debug
72
+ if (loadTemplate[key] && isProduction && false) return loadTemplate[key]; // from cache
73
+
74
+ // type one or multi
75
+ const templateList = Array.isArray(type)
76
+ ? type.map(el => getTemplatePath(el)).filter(list => list?.filter(el => el[0] === name).length)[0] || []
77
+ : getTemplatePath(type);
78
+
79
+ // find template
80
+ const template = templateList?.filter(el => el[0] === name);
81
+ if (name === 'list' && !isProduction) return templateList; // all template debug
82
+
83
+ if (!template.length) return null; // not found
84
+
85
+ loadTemplate[key] = getTemplateData(template);
86
+ return loadTemplate[key];
87
+ }
package/utils.js CHANGED
@@ -19,6 +19,7 @@ import redisClients from './server/plugins/redis/funcs/redisClients.js';
19
19
 
20
20
  // template
21
21
  import getTemplate from './server/plugins/table/funcs/getTemplate.js';
22
+ import getTemplateSync from './server/plugins/table/funcs/getTemplateSync.js';
22
23
  import getTemplates from './server/plugins/table/funcs/getTemplates.js';
23
24
  import getTemplatePath from './server/plugins/table/funcs/getTemplatePath.js';
24
25
  import addTemplateDir from './server/plugins/table/funcs/addTemplateDir.js';
@@ -104,6 +105,7 @@ export {
104
105
 
105
106
  // template
106
107
  getTemplate,
108
+ getTemplateSync,
107
109
  getTemplates,
108
110
  getTemplatePath,
109
111
  addTemplateDir,