lupine.api 1.1.58 → 1.1.59
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 +3 -3
- package/admin/admin-about.tsx +12 -16
- package/admin/admin-config.tsx +47 -44
- package/admin/admin-css.tsx +3 -3
- package/admin/admin-db.tsx +75 -75
- package/admin/admin-frame-helper.tsx +364 -364
- package/admin/admin-frame.tsx +164 -164
- package/admin/admin-index.tsx +65 -65
- package/admin/admin-login.tsx +111 -111
- package/admin/admin-menu-edit.tsx +637 -637
- package/admin/admin-menu-list.tsx +87 -87
- package/admin/admin-page-edit.tsx +564 -564
- package/admin/admin-page-list.tsx +83 -83
- package/admin/admin-performance.tsx +28 -28
- package/admin/admin-release.tsx +427 -426
- package/admin/admin-resources.tsx +382 -382
- package/admin/admin-shell.tsx +89 -89
- package/admin/admin-table-data.tsx +146 -146
- package/admin/admin-table-list.tsx +230 -230
- package/admin/admin-test-animations.tsx +395 -395
- package/admin/admin-test-component.tsx +823 -808
- package/admin/admin-test-edit.tsx +319 -319
- package/admin/admin-test-themes.tsx +56 -56
- package/admin/admin-tokens.tsx +338 -338
- package/admin/design/admin-design.tsx +174 -174
- package/admin/design/block-grid.tsx +36 -36
- package/admin/design/block-grid1.tsx +21 -21
- package/admin/design/block-paragraph.tsx +19 -19
- package/admin/design/block-title.tsx +19 -19
- package/admin/design/design-block-box.tsx +140 -140
- package/admin/design/drag-data.tsx +24 -24
- package/admin/index.ts +9 -9
- package/admin/package.json +15 -15
- package/admin/tsconfig.json +127 -127
- package/dev/copy-folder.js +32 -32
- package/dev/cp-index-html.js +69 -69
- package/dev/file-utils.js +12 -12
- package/dev/index.js +18 -19
- package/dev/package.json +12 -12
- package/dev/plugin-ifelse.js +168 -168
- package/dev/plugin-ifelse.test.js +37 -37
- package/dev/run-cmd.js +14 -14
- package/dev/send-request.js +12 -12
- package/package.json +55 -55
- package/src/admin-api/admin-api-helper.ts +210 -205
- package/src/admin-api/admin-api.ts +65 -65
- package/src/admin-api/admin-auth.ts +152 -146
- package/src/admin-api/admin-config.ts +94 -84
- package/src/admin-api/admin-csv.ts +94 -94
- package/src/admin-api/admin-db.ts +269 -269
- package/src/admin-api/admin-menu.ts +135 -135
- package/src/admin-api/admin-page.ts +135 -135
- package/src/admin-api/admin-performance.ts +128 -128
- package/src/admin-api/admin-release.ts +703 -700
- package/src/admin-api/admin-resources.ts +318 -318
- package/src/admin-api/admin-token-helper.ts +82 -79
- package/src/admin-api/admin-tokens.ts +90 -90
- package/src/admin-api/index.ts +2 -2
- package/src/admin-api/web-config-api.ts +19 -19
- package/src/api/api-cache.ts +103 -103
- package/src/api/api-helper.ts +44 -44
- package/src/api/api-module.ts +67 -60
- package/src/api/api-router.ts +177 -177
- package/src/api/api-shared-storage.ts +64 -64
- package/src/api/async-storage.ts +5 -5
- package/src/api/debug-service.ts +56 -56
- package/src/api/encode-html.ts +27 -27
- package/src/api/handle-status.ts +75 -75
- package/src/api/index.ts +15 -16
- package/src/api/mini-web-socket.ts +270 -270
- package/src/api/server-content-type.ts +82 -82
- package/src/api/server-render.ts +235 -215
- package/src/api/shell-service.ts +74 -74
- package/src/api/simple-storage.ts +80 -80
- package/src/api/static-server.ts +128 -125
- package/src/api/to-client-delivery.ts +26 -26
- package/src/app/app-cache.ts +55 -55
- package/src/app/app-helper.ts +62 -62
- package/src/app/app-message.ts +109 -109
- package/src/app/app-shared-storage.ts +363 -363
- package/src/app/app-start.ts +136 -136
- package/src/app/cleanup-exit.ts +16 -16
- package/src/app/host-to-path.ts +38 -38
- package/src/app/index.ts +11 -11
- package/src/app/process-dev-requests.ts +130 -130
- package/src/app/web-listener.ts +294 -294
- package/src/app/web-processor.ts +47 -42
- package/src/app/web-server.ts +100 -100
- package/src/common-js/web-env.js +104 -104
- package/src/index.ts +7 -7
- package/src/lang/api-lang-en.ts +26 -26
- package/src/lang/api-lang-zh-cn.ts +27 -27
- package/src/lang/index.ts +2 -2
- package/src/lang/lang-helper.ts +76 -76
- package/src/lang/lang-props.ts +6 -6
- package/src/lib/db/db-helper.ts +23 -23
- package/src/lib/db/db-mysql.ts +249 -250
- package/src/lib/db/db-sqlite.ts +101 -101
- package/src/lib/db/db.spec.ts +28 -28
- package/src/lib/db/db.ts +325 -325
- package/src/lib/db/index.ts +5 -5
- package/src/lib/index.ts +3 -3
- package/src/lib/logger.spec.ts +214 -214
- package/src/lib/logger.ts +281 -281
- package/src/lib/runtime-require.ts +37 -37
- package/src/lib/utils/cookie-util.ts +34 -34
- package/src/lib/utils/crypto.ts +58 -58
- package/src/lib/utils/date-utils.ts +317 -317
- package/src/lib/utils/deep-merge.ts +37 -37
- package/src/lib/utils/delay.ts +12 -12
- package/src/lib/utils/file-setting.ts +55 -55
- package/src/lib/utils/format-bytes.ts +11 -11
- package/src/lib/utils/fs-utils.ts +158 -158
- package/src/lib/utils/get-env.ts +27 -27
- package/src/lib/utils/index.ts +12 -12
- package/src/lib/utils/is-type.ts +48 -48
- package/src/lib/utils/load-env.ts +14 -14
- package/src/lib/utils/pad.ts +6 -6
- package/src/models/api-base.ts +5 -5
- package/src/models/api-module-props.ts +10 -11
- package/src/models/api-router-props.ts +26 -26
- package/src/models/app-cache-props.ts +33 -33
- package/src/models/app-data-props.ts +10 -10
- package/src/models/app-helper-props.ts +6 -6
- package/src/models/app-shared-storage-props.ts +38 -38
- package/src/models/app-start-props.ts +18 -18
- package/src/models/async-storage-props.ts +13 -13
- package/src/models/db-config.ts +30 -30
- package/src/models/host-to-path-props.ts +12 -12
- package/src/models/index.ts +16 -16
- package/src/models/json-object.ts +8 -8
- package/src/models/locals-props.ts +36 -36
- package/src/models/logger-props.ts +84 -84
- package/src/models/simple-storage-props.ts +13 -14
- package/src/models/to-client-delivery-props.ts +6 -6
- package/tsconfig.json +115 -115
- 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 || '')
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
+
}
|
package/src/admin-api/index.ts
CHANGED
|
@@ -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
|
+
};
|
package/src/api/api-cache.ts
CHANGED
|
@@ -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();
|