lupine.api 1.1.58 → 1.1.60

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 (137) hide show
  1. package/README.md +3 -3
  2. package/admin/admin-about.tsx +12 -16
  3. package/admin/admin-config.tsx +47 -44
  4. package/admin/admin-css.tsx +3 -3
  5. package/admin/admin-db.tsx +75 -75
  6. package/admin/admin-frame-helper.tsx +364 -364
  7. package/admin/admin-frame.tsx +164 -164
  8. package/admin/admin-index.tsx +65 -65
  9. package/admin/admin-login.tsx +111 -111
  10. package/admin/admin-menu-edit.tsx +637 -637
  11. package/admin/admin-menu-list.tsx +87 -87
  12. package/admin/admin-page-edit.tsx +564 -564
  13. package/admin/admin-page-list.tsx +83 -83
  14. package/admin/admin-performance.tsx +28 -28
  15. package/admin/admin-release.tsx +427 -426
  16. package/admin/admin-resources.tsx +382 -382
  17. package/admin/admin-shell.tsx +89 -89
  18. package/admin/admin-table-data.tsx +146 -146
  19. package/admin/admin-table-list.tsx +230 -230
  20. package/admin/admin-test-animations.tsx +395 -395
  21. package/admin/admin-test-component.tsx +823 -808
  22. package/admin/admin-test-edit.tsx +319 -319
  23. package/admin/admin-test-themes.tsx +56 -56
  24. package/admin/admin-tokens.tsx +338 -338
  25. package/admin/design/admin-design.tsx +174 -174
  26. package/admin/design/block-grid.tsx +36 -36
  27. package/admin/design/block-grid1.tsx +21 -21
  28. package/admin/design/block-paragraph.tsx +19 -19
  29. package/admin/design/block-title.tsx +19 -19
  30. package/admin/design/design-block-box.tsx +140 -140
  31. package/admin/design/drag-data.tsx +24 -24
  32. package/admin/index.ts +9 -9
  33. package/admin/package.json +15 -15
  34. package/admin/tsconfig.json +127 -127
  35. package/dev/copy-folder.js +32 -32
  36. package/dev/cp-index-html.js +69 -69
  37. package/dev/file-utils.js +12 -12
  38. package/dev/index.js +18 -19
  39. package/dev/package.json +12 -12
  40. package/dev/plugin-ifelse.js +168 -168
  41. package/dev/plugin-ifelse.test.js +37 -37
  42. package/dev/run-cmd.js +14 -14
  43. package/dev/send-request.js +12 -12
  44. package/package.json +55 -55
  45. package/src/admin-api/admin-api-helper.ts +210 -205
  46. package/src/admin-api/admin-api.ts +65 -65
  47. package/src/admin-api/admin-auth.ts +152 -146
  48. package/src/admin-api/admin-config.ts +94 -84
  49. package/src/admin-api/admin-csv.ts +94 -94
  50. package/src/admin-api/admin-db.ts +269 -269
  51. package/src/admin-api/admin-menu.ts +135 -135
  52. package/src/admin-api/admin-page.ts +135 -135
  53. package/src/admin-api/admin-performance.ts +128 -128
  54. package/src/admin-api/admin-release.ts +706 -700
  55. package/src/admin-api/admin-resources.ts +318 -318
  56. package/src/admin-api/admin-token-helper.ts +82 -79
  57. package/src/admin-api/admin-tokens.ts +90 -90
  58. package/src/admin-api/index.ts +2 -2
  59. package/src/admin-api/web-config-api.ts +19 -19
  60. package/src/api/api-cache.ts +103 -103
  61. package/src/api/api-helper.ts +44 -44
  62. package/src/api/api-module.ts +67 -60
  63. package/src/api/api-router.ts +177 -177
  64. package/src/api/api-shared-storage.ts +64 -64
  65. package/src/api/async-storage.ts +5 -5
  66. package/src/api/debug-service.ts +56 -56
  67. package/src/api/encode-html.ts +27 -27
  68. package/src/api/handle-status.ts +75 -75
  69. package/src/api/index.ts +15 -16
  70. package/src/api/mini-web-socket.ts +270 -270
  71. package/src/api/server-content-type.ts +82 -82
  72. package/src/api/server-render.ts +235 -215
  73. package/src/api/shell-service.ts +74 -74
  74. package/src/api/simple-storage.ts +80 -80
  75. package/src/api/static-server.ts +128 -125
  76. package/src/api/to-client-delivery.ts +26 -26
  77. package/src/app/app-cache.ts +55 -55
  78. package/src/app/app-helper.ts +62 -62
  79. package/src/app/app-message.ts +109 -109
  80. package/src/app/app-shared-storage.ts +363 -363
  81. package/src/app/app-start.ts +136 -136
  82. package/src/app/cleanup-exit.ts +16 -16
  83. package/src/app/host-to-path.ts +38 -38
  84. package/src/app/index.ts +11 -11
  85. package/src/app/process-dev-requests.ts +130 -130
  86. package/src/app/web-listener.ts +294 -294
  87. package/src/app/web-processor.ts +47 -42
  88. package/src/app/web-server.ts +100 -100
  89. package/src/common-js/web-env.js +104 -104
  90. package/src/index.ts +7 -7
  91. package/src/lang/api-lang-en.ts +26 -26
  92. package/src/lang/api-lang-zh-cn.ts +27 -27
  93. package/src/lang/index.ts +2 -2
  94. package/src/lang/lang-helper.ts +76 -76
  95. package/src/lang/lang-props.ts +6 -6
  96. package/src/lib/db/db-helper.ts +23 -23
  97. package/src/lib/db/db-mysql.ts +249 -250
  98. package/src/lib/db/db-sqlite.ts +101 -101
  99. package/src/lib/db/db.spec.ts +28 -28
  100. package/src/lib/db/db.ts +325 -325
  101. package/src/lib/db/index.ts +5 -5
  102. package/src/lib/index.ts +3 -3
  103. package/src/lib/logger.spec.ts +214 -214
  104. package/src/lib/logger.ts +281 -281
  105. package/src/lib/runtime-require.ts +37 -37
  106. package/src/lib/utils/cookie-util.ts +34 -34
  107. package/src/lib/utils/crypto.ts +58 -58
  108. package/src/lib/utils/date-utils.ts +317 -317
  109. package/src/lib/utils/deep-merge.ts +37 -37
  110. package/src/lib/utils/delay.ts +12 -12
  111. package/src/lib/utils/file-setting.ts +55 -55
  112. package/src/lib/utils/format-bytes.ts +11 -11
  113. package/src/lib/utils/fs-utils.ts +158 -158
  114. package/src/lib/utils/get-env.ts +27 -27
  115. package/src/lib/utils/index.ts +12 -12
  116. package/src/lib/utils/is-type.ts +48 -48
  117. package/src/lib/utils/load-env.ts +14 -14
  118. package/src/lib/utils/pad.ts +6 -6
  119. package/src/models/api-base.ts +5 -5
  120. package/src/models/api-module-props.ts +10 -11
  121. package/src/models/api-router-props.ts +26 -26
  122. package/src/models/app-cache-props.ts +33 -33
  123. package/src/models/app-data-props.ts +10 -10
  124. package/src/models/app-helper-props.ts +6 -6
  125. package/src/models/app-shared-storage-props.ts +38 -38
  126. package/src/models/app-start-props.ts +18 -18
  127. package/src/models/async-storage-props.ts +13 -13
  128. package/src/models/db-config.ts +30 -30
  129. package/src/models/host-to-path-props.ts +12 -12
  130. package/src/models/index.ts +16 -16
  131. package/src/models/json-object.ts +8 -8
  132. package/src/models/locals-props.ts +36 -36
  133. package/src/models/logger-props.ts +84 -84
  134. package/src/models/simple-storage-props.ts +13 -14
  135. package/src/models/to-client-delivery-props.ts +6 -6
  136. package/tsconfig.json +115 -115
  137. package/dev/plugin-gen-versions.js +0 -20
@@ -1,79 +1,82 @@
1
- import { apiStorage } from '../api';
2
- import { CryptoUtils, Logger } from '../lib';
3
- import { adminApiHelper } from './admin-api-helper';
4
-
5
- export type TokenProps = {
6
- token: string;
7
- description: string;
8
- timestamp?: number;
9
- };
10
- export class AdminTokenHelper {
11
- private static instance: AdminTokenHelper;
12
- private logger = new Logger('admin-token-api');
13
-
14
- private constructor() {}
15
-
16
- public static getInstance(): AdminTokenHelper {
17
- if (!AdminTokenHelper.instance) {
18
- AdminTokenHelper.instance = new AdminTokenHelper();
19
- }
20
- return AdminTokenHelper.instance;
21
- }
22
-
23
- async list(search?: string): Promise<TokenProps[]> {
24
- const tokens = await apiStorage.getApi('access-tokens');
25
- const tokenJson = JSON.parse(tokens || '[]');
26
-
27
- const searchTexts = (search || '').trim().split(' ').filter(item => !!item);
28
- const results = tokenJson.filter((item: TokenProps) =>
29
- searchTexts.every((text) => item.description.toLowerCase().includes(text.toLowerCase()))
30
- );
31
-
32
- return results;
33
- }
34
- async add(tokenData: TokenProps) {
35
- const tokens = await apiStorage.getApi('access-tokens');
36
- const tokensJson = JSON.parse(tokens || '[]');
37
- tokenData.timestamp = new Date().getTime();
38
- tokensJson.push(tokenData);
39
- apiStorage.setApi('access-tokens', JSON.stringify(tokensJson));
40
- await apiStorage.save();
41
- }
42
-
43
- async update(tokenData: TokenProps) {
44
- const tokens = await apiStorage.getApi('access-tokens');
45
- const tokensJson = JSON.parse(tokens || '{}');
46
- const idx = tokensJson.findIndex((item: TokenProps) => item.token === tokenData.token);
47
- if (idx !== -1) {
48
- tokensJson[idx].description = tokenData.description;
49
- }
50
- apiStorage.setApi('access-tokens', JSON.stringify(tokensJson));
51
- apiStorage.save();
52
- }
53
-
54
- async remove(token: string) {
55
- const tokens = await apiStorage.getApi('access-tokens');
56
- const tokensJson = JSON.parse(tokens || '[]');
57
- const idx = tokensJson.findIndex((item: TokenProps) => item.token === token);
58
- if (idx !== -1) {
59
- tokensJson.splice(idx, 1);
60
- }
61
- apiStorage.setApi('access-tokens', JSON.stringify(tokensJson));
62
- apiStorage.save();
63
- }
64
-
65
- generate() {
66
- const salt = 'Lupine:' + CryptoUtils.uuid() + ':' + new Date().getTime().toString();
67
- return adminApiHelper.encryptJson(salt) as string;
68
- }
69
-
70
- async validateToken(token: string) {
71
- const tokens = await apiStorage.getApi('access-tokens');
72
- const tokensJson = JSON.parse(tokens || '[]');
73
- const idx = tokensJson.findIndex((item: TokenProps) => item.token === token);
74
- return idx !== -1;
75
- }
76
- }
77
-
78
- // add comment for tree shaking
79
- export const adminTokenHelper = /* @__PURE__ */ AdminTokenHelper.getInstance();
1
+ import { apiStorage } from '../api';
2
+ import { CryptoUtils, Logger } from '../lib';
3
+ import { adminApiHelper } from './admin-api-helper';
4
+
5
+ export type TokenProps = {
6
+ token: string;
7
+ description: string;
8
+ timestamp?: number;
9
+ };
10
+ export class AdminTokenHelper {
11
+ private static instance: AdminTokenHelper;
12
+ private logger = new Logger('admin-token-api');
13
+
14
+ private constructor() {}
15
+
16
+ public static getInstance(): AdminTokenHelper {
17
+ if (!AdminTokenHelper.instance) {
18
+ AdminTokenHelper.instance = new AdminTokenHelper();
19
+ }
20
+ return AdminTokenHelper.instance;
21
+ }
22
+
23
+ async list(search?: string): Promise<TokenProps[]> {
24
+ const tokens = await apiStorage.getApi('access-tokens');
25
+ const tokenJson = JSON.parse(tokens || '[]');
26
+
27
+ const searchTexts = (search || '')
28
+ .trim()
29
+ .split(' ')
30
+ .filter((item) => !!item);
31
+ const results = tokenJson.filter((item: TokenProps) =>
32
+ searchTexts.every((text) => item.description.toLowerCase().includes(text.toLowerCase()))
33
+ );
34
+
35
+ return results;
36
+ }
37
+ async add(tokenData: TokenProps) {
38
+ const tokens = await apiStorage.getApi('access-tokens');
39
+ const tokensJson = JSON.parse(tokens || '[]');
40
+ tokenData.timestamp = new Date().getTime();
41
+ tokensJson.push(tokenData);
42
+ apiStorage.setApi('access-tokens', JSON.stringify(tokensJson));
43
+ await apiStorage.save();
44
+ }
45
+
46
+ async update(tokenData: TokenProps) {
47
+ const tokens = await apiStorage.getApi('access-tokens');
48
+ const tokensJson = JSON.parse(tokens || '{}');
49
+ const idx = tokensJson.findIndex((item: TokenProps) => item.token === tokenData.token);
50
+ if (idx !== -1) {
51
+ tokensJson[idx].description = tokenData.description;
52
+ }
53
+ apiStorage.setApi('access-tokens', JSON.stringify(tokensJson));
54
+ apiStorage.save();
55
+ }
56
+
57
+ async remove(token: string) {
58
+ const tokens = await apiStorage.getApi('access-tokens');
59
+ const tokensJson = JSON.parse(tokens || '[]');
60
+ const idx = tokensJson.findIndex((item: TokenProps) => item.token === token);
61
+ if (idx !== -1) {
62
+ tokensJson.splice(idx, 1);
63
+ }
64
+ apiStorage.setApi('access-tokens', JSON.stringify(tokensJson));
65
+ apiStorage.save();
66
+ }
67
+
68
+ generate() {
69
+ const salt = 'Lupine:' + CryptoUtils.uuid() + ':' + new Date().getTime().toString();
70
+ return adminApiHelper.encryptJson(salt) as string;
71
+ }
72
+
73
+ async validateToken(token: string) {
74
+ const tokens = await apiStorage.getApi('access-tokens');
75
+ const tokensJson = JSON.parse(tokens || '[]');
76
+ const idx = tokensJson.findIndex((item: TokenProps) => item.token === token);
77
+ return idx !== -1;
78
+ }
79
+ }
80
+
81
+ // add comment for tree shaking
82
+ export const adminTokenHelper = /* @__PURE__ */ AdminTokenHelper.getInstance();
@@ -1,90 +1,90 @@
1
- import { ServerResponse } from 'http';
2
- import { IApiBase, Logger, ServerRequest, ApiRouter, ApiHelper, apiStorage } from 'lupine.api';
3
- import { adminTokenHelper } from './admin-token-helper';
4
-
5
- export class AdminTokens implements IApiBase {
6
- logger = new Logger('admin-page');
7
- protected router = new ApiRouter();
8
- adminUser: any;
9
-
10
- constructor() {
11
- this.mountDashboard();
12
- }
13
-
14
- public getRouter(): ApiRouter {
15
- return this.router;
16
- }
17
-
18
- protected mountDashboard() {
19
- this.router.use('/list', this.list.bind(this));
20
- this.router.use('/add', this.add.bind(this));
21
- this.router.use('/generate', this.generateToken.bind(this));
22
- this.router.use('/update', this.update.bind(this));
23
- this.router.use('/remove', this.remove.bind(this));
24
- }
25
-
26
- async list(req: ServerRequest, res: ServerResponse) {
27
- const pageLimit = await apiStorage.getWeb('pageLimit') || '15';
28
- const data = req.locals.json() as any;
29
- const search = data['q'];
30
- const list = await adminTokenHelper.list(search);
31
- const response = {
32
- status: 'ok',
33
- message: 'Token List.',
34
- result: list,
35
- pageLimit,
36
- };
37
- ApiHelper.sendJson(req, res, response);
38
- return true;
39
- }
40
-
41
- async add(req: ServerRequest, res: ServerResponse) {
42
- const data = req.locals.json() as any;
43
- const token = data['token'];
44
- const description = data['description'];
45
- token && (await adminTokenHelper.add({token, description}));
46
- const response = {
47
- status: 'ok',
48
- message: 'Added token.',
49
- };
50
- ApiHelper.sendJson(req, res, response);
51
- return true;
52
- }
53
-
54
- async generateToken(req: ServerRequest, res: ServerResponse) {
55
- const newToken = adminTokenHelper.generate();
56
- const response = {
57
- status: 'ok',
58
- message: 'Generated new token.',
59
- result: newToken,
60
- };
61
- ApiHelper.sendJson(req, res, response);
62
- return true;
63
- }
64
-
65
- async update(req: ServerRequest, res: ServerResponse) {
66
- const data = req.locals.json() as any;
67
- const token = data['token'];
68
- const description = data['description'];
69
- token && (await adminTokenHelper.update({token, description}));
70
- const response = {
71
- status: 'ok',
72
- message: 'Updated token.',
73
- };
74
- ApiHelper.sendJson(req, res, response);
75
- return true;
76
- }
77
-
78
- async remove(req: ServerRequest, res: ServerResponse) {
79
- const data = req.locals.json() as any;
80
- const token = data['token'];
81
- token && (await adminTokenHelper.remove(token));
82
-
83
- const response = {
84
- status: 'ok',
85
- message: 'Removed token.',
86
- };
87
- ApiHelper.sendJson(req, res, response);
88
- return true;
89
- }
90
- }
1
+ import { ServerResponse } from 'http';
2
+ import { IApiBase, Logger, ServerRequest, ApiRouter, ApiHelper, apiStorage } from 'lupine.api';
3
+ import { adminTokenHelper } from './admin-token-helper';
4
+
5
+ export class AdminTokens implements IApiBase {
6
+ logger = new Logger('admin-page');
7
+ protected router = new ApiRouter();
8
+ adminUser: any;
9
+
10
+ constructor() {
11
+ this.mountDashboard();
12
+ }
13
+
14
+ public getRouter(): ApiRouter {
15
+ return this.router;
16
+ }
17
+
18
+ protected mountDashboard() {
19
+ this.router.use('/list', this.list.bind(this));
20
+ this.router.use('/add', this.add.bind(this));
21
+ this.router.use('/generate', this.generateToken.bind(this));
22
+ this.router.use('/update', this.update.bind(this));
23
+ this.router.use('/remove', this.remove.bind(this));
24
+ }
25
+
26
+ async list(req: ServerRequest, res: ServerResponse) {
27
+ const pageLimit = (await apiStorage.getWeb('pageLimit')) || '15';
28
+ const data = req.locals.json() as any;
29
+ const search = data['q'];
30
+ const list = await adminTokenHelper.list(search);
31
+ const response = {
32
+ status: 'ok',
33
+ message: 'Token List.',
34
+ result: list,
35
+ pageLimit,
36
+ };
37
+ ApiHelper.sendJson(req, res, response);
38
+ return true;
39
+ }
40
+
41
+ async add(req: ServerRequest, res: ServerResponse) {
42
+ const data = req.locals.json() as any;
43
+ const token = data['token'];
44
+ const description = data['description'];
45
+ token && (await adminTokenHelper.add({ token, description }));
46
+ const response = {
47
+ status: 'ok',
48
+ message: 'Added token.',
49
+ };
50
+ ApiHelper.sendJson(req, res, response);
51
+ return true;
52
+ }
53
+
54
+ async generateToken(req: ServerRequest, res: ServerResponse) {
55
+ const newToken = adminTokenHelper.generate();
56
+ const response = {
57
+ status: 'ok',
58
+ message: 'Generated new token.',
59
+ result: newToken,
60
+ };
61
+ ApiHelper.sendJson(req, res, response);
62
+ return true;
63
+ }
64
+
65
+ async update(req: ServerRequest, res: ServerResponse) {
66
+ const data = req.locals.json() as any;
67
+ const token = data['token'];
68
+ const description = data['description'];
69
+ token && (await adminTokenHelper.update({ token, description }));
70
+ const response = {
71
+ status: 'ok',
72
+ message: 'Updated token.',
73
+ };
74
+ ApiHelper.sendJson(req, res, response);
75
+ return true;
76
+ }
77
+
78
+ async remove(req: ServerRequest, res: ServerResponse) {
79
+ const data = req.locals.json() as any;
80
+ const token = data['token'];
81
+ token && (await adminTokenHelper.remove(token));
82
+
83
+ const response = {
84
+ status: 'ok',
85
+ message: 'Removed token.',
86
+ };
87
+ ApiHelper.sendJson(req, res, response);
88
+ return true;
89
+ }
90
+ }
@@ -1,2 +1,2 @@
1
- export * from './admin-api';
2
- export * from './admin-api-helper';
1
+ export * from './admin-api';
2
+ export * from './admin-api-helper';
@@ -1,19 +1,19 @@
1
- import { ServerResponse } from 'http';
2
- import { Logger } from '../lib';
3
- import { ServerRequest } from '../models';
4
- import { langHelper } from '../lang';
5
- import { ApiHelper, apiStorage } from '../api';
6
-
7
- // only used by mobile app. For web, it's injected in the html by server-side render
8
- const logger = new Logger('web-cfg-api');
9
- export const readWebConfig = async (req: ServerRequest, res: ServerResponse) => {
10
- logger.info('readWebConfig');
11
-
12
- const response = {
13
- status: 'ok',
14
- result: await apiStorage.getWebAll(),
15
- message: langHelper.getLang('shared:operation_success'),
16
- };
17
- ApiHelper.sendJson(req, res, response);
18
- return true;
19
- };
1
+ import { ServerResponse } from 'http';
2
+ import { Logger } from '../lib';
3
+ import { ServerRequest } from '../models';
4
+ import { langHelper } from '../lang';
5
+ import { ApiHelper, apiStorage } from '../api';
6
+
7
+ // only used by mobile app. For web, it's injected in the html by server-side render
8
+ const logger = new Logger('web-cfg-api');
9
+ export const readWebConfig = async (req: ServerRequest, res: ServerResponse) => {
10
+ logger.info('readWebConfig');
11
+
12
+ const response = {
13
+ status: 'ok',
14
+ result: await apiStorage.getWebAll(),
15
+ message: langHelper.getLang('shared:operation_success'),
16
+ };
17
+ ApiHelper.sendJson(req, res, response);
18
+ return true;
19
+ };
@@ -1,103 +1,103 @@
1
- /**
2
- * A simple settings/config class for storing key/value pairs in memory
3
- */
4
- import { Db } from '../lib';
5
- import { asyncLocalStorage } from './async-storage';
6
- import { AsyncStorageProps, HostToPathProps } from '../models';
7
-
8
- // This data doesn't need to be shared to other workers
9
- export const getTemplateCache = () => {
10
- let cachedHtml = apiCache.get(apiCache.KEYS.TEMPLATE);
11
- if (!cachedHtml) {
12
- cachedHtml = {};
13
- apiCache.set(apiCache.KEYS.TEMPLATE, cachedHtml);
14
- }
15
- return cachedHtml;
16
- };
17
-
18
- enum ApiCacheKeys {
19
- TEMPLATE = 'TEMPLATE',
20
- DB = 'DB',
21
- APP_DATA = 'APP_DATA',
22
- // APP_CACHE = 'APP_CACHE',
23
- }
24
-
25
- // For cross clusters sharing, use AppSharedStorage
26
- // ApiCache doesn't share cross clusters
27
- // Since apis and app are independent, so ApiCache in apis and app are different instances.
28
- // ApiCache is not shared cross app and apis, so `set` is only supposed to be called when app starts, or
29
- // `set/get` is only used inside one api only
30
- export class ApiCache {
31
- private static instance: ApiCache;
32
- KEYS = ApiCacheKeys;
33
-
34
- private cacheMap: { [key: string]: any } = {};
35
-
36
- private constructor() {}
37
-
38
- public static getInstance(): ApiCache {
39
- if (!ApiCache.instance) {
40
- ApiCache.instance = new ApiCache();
41
- }
42
- return ApiCache.instance;
43
- }
44
-
45
- clear() {
46
- Object.keys(this.cacheMap).forEach((key) => {
47
- delete this.cacheMap[key];
48
- });
49
- }
50
-
51
- get(key: string) {
52
- return this.cacheMap[key];
53
- }
54
-
55
- set(key: string, value: any) {
56
- if (typeof value === 'undefined') {
57
- delete this.cacheMap[key];
58
- } else {
59
- this.cacheMap[key] = value;
60
- }
61
- }
62
-
63
- // get scope variables inside of asyncLocalStorage.run
64
- getAsyncStore(): AsyncStorageProps {
65
- const store = asyncLocalStorage.getStore();
66
- if (!store) {
67
- throw new Error('This function should be called inside of asyncLocalStorage.run');
68
- }
69
-
70
- return store;
71
- }
72
-
73
- // get scope variables inside of asyncLocalStorage.run
74
- getAppData() {
75
- return this.get(ApiCacheKeys.APP_DATA) as HostToPathProps;
76
- }
77
-
78
- // get scope variables inside of asyncLocalStorage.run
79
- getAppName() {
80
- return this.getAsyncStore().appName;
81
- }
82
-
83
- // get scope variables inside of asyncLocalStorage.run
84
- getDb() {
85
- return this.get(ApiCacheKeys.DB) as Db;
86
- }
87
-
88
- // get scope variables inside of asyncLocalStorage.run
89
- getUuid() {
90
- return this.getAsyncStore().uuid;
91
- }
92
-
93
- // get scope variables inside of asyncLocalStorage.run
94
- getLang() {
95
- return this.getAsyncStore().lang;
96
- }
97
-
98
- clearTemplateCache() {
99
- this.set(ApiCacheKeys.TEMPLATE, undefined);
100
- }
101
- }
102
-
103
- export const apiCache = /* @__PURE__ */ ApiCache.getInstance();
1
+ /**
2
+ * A simple settings/config class for storing key/value pairs in memory
3
+ */
4
+ import { Db } from '../lib';
5
+ import { asyncLocalStorage } from './async-storage';
6
+ import { AsyncStorageProps, HostToPathProps } from '../models';
7
+
8
+ // This data doesn't need to be shared to other workers
9
+ export const getTemplateCache = () => {
10
+ let cachedHtml = apiCache.get(apiCache.KEYS.TEMPLATE);
11
+ if (!cachedHtml) {
12
+ cachedHtml = {};
13
+ apiCache.set(apiCache.KEYS.TEMPLATE, cachedHtml);
14
+ }
15
+ return cachedHtml;
16
+ };
17
+
18
+ enum ApiCacheKeys {
19
+ TEMPLATE = 'TEMPLATE',
20
+ DB = 'DB',
21
+ APP_DATA = 'APP_DATA',
22
+ // APP_CACHE = 'APP_CACHE',
23
+ }
24
+
25
+ // For cross clusters sharing, use AppSharedStorage
26
+ // ApiCache doesn't share cross clusters
27
+ // Since apis and app are independent, so ApiCache in apis and app are different instances.
28
+ // ApiCache is not shared cross app and apis, so `set` is only supposed to be called when app starts, or
29
+ // `set/get` is only used inside one api only
30
+ export class ApiCache {
31
+ private static instance: ApiCache;
32
+ KEYS = ApiCacheKeys;
33
+
34
+ private cacheMap: { [key: string]: any } = {};
35
+
36
+ private constructor() {}
37
+
38
+ public static getInstance(): ApiCache {
39
+ if (!ApiCache.instance) {
40
+ ApiCache.instance = new ApiCache();
41
+ }
42
+ return ApiCache.instance;
43
+ }
44
+
45
+ clear() {
46
+ Object.keys(this.cacheMap).forEach((key) => {
47
+ delete this.cacheMap[key];
48
+ });
49
+ }
50
+
51
+ get(key: string) {
52
+ return this.cacheMap[key];
53
+ }
54
+
55
+ set(key: string, value: any) {
56
+ if (typeof value === 'undefined') {
57
+ delete this.cacheMap[key];
58
+ } else {
59
+ this.cacheMap[key] = value;
60
+ }
61
+ }
62
+
63
+ // get scope variables inside of asyncLocalStorage.run
64
+ getAsyncStore(): AsyncStorageProps {
65
+ const store = asyncLocalStorage.getStore();
66
+ if (!store) {
67
+ throw new Error('This function should be called inside of asyncLocalStorage.run');
68
+ }
69
+
70
+ return store;
71
+ }
72
+
73
+ // get scope variables inside of asyncLocalStorage.run
74
+ getAppData() {
75
+ return this.get(ApiCacheKeys.APP_DATA) as HostToPathProps;
76
+ }
77
+
78
+ // get scope variables inside of asyncLocalStorage.run
79
+ getAppName() {
80
+ return this.getAsyncStore().appName;
81
+ }
82
+
83
+ // get scope variables inside of asyncLocalStorage.run
84
+ getDb() {
85
+ return this.get(ApiCacheKeys.DB) as Db;
86
+ }
87
+
88
+ // get scope variables inside of asyncLocalStorage.run
89
+ getUuid() {
90
+ return this.getAsyncStore().uuid;
91
+ }
92
+
93
+ // get scope variables inside of asyncLocalStorage.run
94
+ getLang() {
95
+ return this.getAsyncStore().lang;
96
+ }
97
+
98
+ clearTemplateCache() {
99
+ this.set(ApiCacheKeys.TEMPLATE, undefined);
100
+ }
101
+ }
102
+
103
+ export const apiCache = /* @__PURE__ */ ApiCache.getInstance();