@kne/fastify-app-manager 0.1.40 → 0.1.41
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/libs/services/menu.js +8 -1
- package/libs/utils/menuManifest.js +3 -19
- package/libs/utils/remoteModuleConfig.js +48 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -703,13 +703,13 @@ manifest 规则:
|
|
|
703
703
|
| 字段 | 说明 |
|
|
704
704
|
|------|------|
|
|
705
705
|
| `integration` | `remote`(远程组件,默认当存在 `remote` 时)/ `iframe` / `blank`;缺省且无 `remote` 为 `iframe` |
|
|
706
|
-
| `remote.name` | 联邦容器名,**必须等于应用 name
|
|
707
|
-
| `remote.version` | linked
|
|
706
|
+
| `remote.name` | 联邦容器名,**必须等于应用 name**;`apiHost` 一律为 `/app/{name}`(接口经网关同源转发并换令牌,linked 应用再转到其 `apiBaseUrl`) |
|
|
707
|
+
| `remote.url` / `remote.version` | **清单里不用声明**(声明了也会被忽略)。托管应用:`url` 取 `/app/{name}`,`version` 取当前部署版本;linked 应用:拉取菜单快照时读取其前端站点(`baseUrl`)的 `GET /remote-module-config.json`,取其 `url`(http(s) 绝对地址)与 `version`(联邦容器名 `{name}_{version}`)。线上由 `@kne/fastify-app-runtime` 提供(内容来自构建产物),本地 `craco start` 由 `@kne/modules-dev` 提供。接口不存在或内容不合法时,快照为 error 并给出原因 |
|
|
708
708
|
| `menus` | 最多两级,同级 `key` 唯一(`[a-z0-9-]`);叶子菜单 remote 需 `component`,iframe / blank 需 `path`;有 `children` 的节点不能带 `component` / `path` |
|
|
709
709
|
| `permission` | 子应用权限树内的相对码;返回时附加 `permissionCode = {应用 name}:{permission}`,缺省表示开通应用即可见 |
|
|
710
710
|
| `menus[].icon` | 可选,原样透传给宿主。约定:含 `/` 或 `.` 视为图片地址(相对地址按 `remote.url` / `baseUrl` 解析,推荐单色 SVG,宿主用 mask 按菜单激活态着色,跨域时需 CORS 放行);否则视为 iconfont type;子菜单未声明时沿用父级,均未声明用宿主默认图标 |
|
|
711
711
|
|
|
712
|
-
`version`
|
|
712
|
+
`version` 为当前部署版本号(linked 应用为其 `/remote-module-config.json` 的 `version`),宿主据此判断是否需要重载 `remoteEntry.js`。
|
|
713
713
|
|
|
714
714
|
##### GET `{prefix}/app/center/display-settings`
|
|
715
715
|
|
package/libs/services/menu.js
CHANGED
|
@@ -2,6 +2,7 @@ const fp = require('fastify-plugin');
|
|
|
2
2
|
const { isLinked, linkedBaseUrl, linkedApiBaseUrl, KIND_LINKED, KIND_MANAGED } = require('../utils/appKind');
|
|
3
3
|
const { isServed, SERVED_STATUSES } = require('../utils/appAvailability');
|
|
4
4
|
const { normalizeMenuManifest, flattenLeafMenus } = require('../utils/menuManifest');
|
|
5
|
+
const { fetchRemoteModuleConfig } = require('../utils/remoteModuleConfig');
|
|
5
6
|
const { hasLabels, parseLabelI18n, mergeLabelI18n, appDefaultLabels } = require('../utils/labelI18n');
|
|
6
7
|
|
|
7
8
|
/** 拉取失败或没有菜单声明的应用,间隔多久再尝试一次懒拉取 */
|
|
@@ -75,11 +76,17 @@ module.exports = fp(async (fastify, options) => {
|
|
|
75
76
|
if (raw == null) {
|
|
76
77
|
return await saveSnapshot(app, { status: 'none', fetchedAt, manifest: null });
|
|
77
78
|
}
|
|
78
|
-
const { ok, manifest, errors } = normalizeMenuManifest(raw, { appName: app.name
|
|
79
|
+
const { ok, manifest, errors } = normalizeMenuManifest(raw, { appName: app.name });
|
|
79
80
|
if (!ok) {
|
|
80
81
|
fastify.log.warn({ app: app.name, errors }, 'app menus manifest invalid');
|
|
81
82
|
return await saveSnapshot(app, { status: 'error', fetchedAt, error: errors.join(';'), manifest: previous?.manifest || null });
|
|
82
83
|
}
|
|
84
|
+
if (isLinked(app) && manifest.integration === 'remote') {
|
|
85
|
+
// 关联应用没有部署记录:远程组件地址与版本(联邦容器名 {name}_{version})读前端站点(baseUrl)的 /remote-module-config.json;
|
|
86
|
+
// 线上由 fastify-app-runtime 提供,本地 craco start 由 @kne/modules-dev 提供
|
|
87
|
+
const { url, version } = await fetchRemoteModuleConfig({ baseUrl: linkedBaseUrl(app), timeoutMs: FETCH_TIMEOUT_MS });
|
|
88
|
+
manifest.remote = Object.assign({}, manifest.remote, { url, version });
|
|
89
|
+
}
|
|
83
90
|
return await saveSnapshot(app, { status: 'ok', fetchedAt, manifest });
|
|
84
91
|
} catch (e) {
|
|
85
92
|
fastify.log.warn({ err: e, app: app.name }, 'fetch app menus failed');
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* 规范见《应用菜单集成规范》:
|
|
5
5
|
* - integration:remote(远程组件,首选)/ iframe / blank(新窗口)
|
|
6
6
|
* - menus 最多两级;叶子菜单 remote 需要 component,iframe / blank 需要 path
|
|
7
|
-
* - remote.name 必须等于应用 slug
|
|
7
|
+
* - remote.name 必须等于应用 slug;remote 的地址与版本不在清单里声明:托管应用取部署记录,linked 应用读其 /remote-module-config.json
|
|
8
8
|
* - labelI18n(可选):子应用声明自己在各语言下的名称,如 { "zh-CN": "订单", "en-US": "Orders" };应用中心与租户可覆盖
|
|
9
9
|
*/
|
|
10
10
|
|
|
@@ -30,10 +30,10 @@ const normalizePath = value => {
|
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
32
|
* @param {unknown} raw 子应用返回的 manifest 原文
|
|
33
|
-
* @param {{ appName: string
|
|
33
|
+
* @param {{ appName: string }} context
|
|
34
34
|
* @returns {{ ok: boolean, manifest: object|null, errors: string[] }}
|
|
35
35
|
*/
|
|
36
|
-
const normalizeMenuManifest = (raw, { appName
|
|
36
|
+
const normalizeMenuManifest = (raw, { appName } = {}) => {
|
|
37
37
|
const errors = [];
|
|
38
38
|
if (!isPlainObject(raw)) {
|
|
39
39
|
return { ok: false, manifest: null, errors: ['manifest 必须是对象'] };
|
|
@@ -62,22 +62,6 @@ const normalizeMenuManifest = (raw, { appName, kind = 'managed' } = {}) => {
|
|
|
62
62
|
name,
|
|
63
63
|
preset: text(raw.remote?.preset) || DEFAULT_PRESET
|
|
64
64
|
};
|
|
65
|
-
if (kind === 'linked') {
|
|
66
|
-
const url = text(raw.remote?.url).replace(/\/+$/, '');
|
|
67
|
-
if (!/^https?:\/\//i.test(url)) {
|
|
68
|
-
errors.push('linked 应用的 remote.url 必须是 http(s) 绝对地址');
|
|
69
|
-
}
|
|
70
|
-
remote.url = url;
|
|
71
|
-
// 关联应用没有部署版本记录:联邦容器名带版本后缀({name}_{version}),宿主据此拼容器名,必须声明与构建一致的版本
|
|
72
|
-
const version = text(raw.remote?.version);
|
|
73
|
-
if (!version) {
|
|
74
|
-
errors.push('linked 应用必须提供 remote.version(与构建时 package.json 的 version 一致,用于定位联邦容器名)');
|
|
75
|
-
} else if (!/^[A-Za-z0-9._+-]+$/.test(version)) {
|
|
76
|
-
errors.push('remote.version 只能包含字母、数字与 . _ + -');
|
|
77
|
-
} else {
|
|
78
|
-
remote.version = version;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
65
|
}
|
|
82
66
|
|
|
83
67
|
let labelI18n = {};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 读取应用前端站点提供的 /remote-module-config.json(线上由 @kne/fastify-app-runtime 内置提供,内容来自构建产物 remote-module-config*.json;
|
|
3
|
+
* 本地 craco start 由 @kne/modules-dev 提供)。关联应用的远程组件地址与版本以它为准,不再要求应用在菜单清单里重复声明。
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const DEFAULT_TIMEOUT_MS = 5000;
|
|
7
|
+
const VERSION_RE = /^[A-Za-z0-9._+-]+$/;
|
|
8
|
+
|
|
9
|
+
const text = value => (value == null ? '' : String(value).trim());
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param {{ baseUrl: string, timeoutMs?: number, fetchImpl?: typeof fetch }} params baseUrl 为应用前端站点根地址
|
|
13
|
+
* @returns {Promise<{ url: string, version: string }>} url 为远程组件 remoteEntry.js 所在目录的绝对地址
|
|
14
|
+
*/
|
|
15
|
+
const fetchRemoteModuleConfig = async ({ baseUrl, timeoutMs = DEFAULT_TIMEOUT_MS, fetchImpl = fetch } = {}) => {
|
|
16
|
+
const base = text(baseUrl).replace(/\/+$/, '');
|
|
17
|
+
if (!base) {
|
|
18
|
+
throw new Error('缺少应用地址,无法读取 /remote-module-config.json');
|
|
19
|
+
}
|
|
20
|
+
const endpoint = `${base}/remote-module-config.json`;
|
|
21
|
+
const response = await fetchImpl(endpoint, { signal: AbortSignal.timeout(timeoutMs) });
|
|
22
|
+
if (response.status === 404) {
|
|
23
|
+
throw new Error(`${endpoint} 不存在:应用需使用 @kne/fastify-app-runtime,且构建产物包含 remote-module-config*.json`);
|
|
24
|
+
}
|
|
25
|
+
if (!response.ok) {
|
|
26
|
+
throw new Error(`${endpoint} 返回 HTTP ${response.status}`);
|
|
27
|
+
}
|
|
28
|
+
let config;
|
|
29
|
+
try {
|
|
30
|
+
config = await response.json();
|
|
31
|
+
} catch (e) {
|
|
32
|
+
throw new Error(`${endpoint} 返回的不是 JSON`);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const url = text(config?.url)
|
|
36
|
+
.replace(/\/+$/, '')
|
|
37
|
+
.replace(/\/remoteEntry\.js$/i, '');
|
|
38
|
+
if (!/^https?:\/\//i.test(url)) {
|
|
39
|
+
throw new Error(`${endpoint} 的 url 必须是 http(s) 绝对地址,当前为 ${url || '空'}`);
|
|
40
|
+
}
|
|
41
|
+
const version = text(config?.version);
|
|
42
|
+
if (!version || !VERSION_RE.test(version)) {
|
|
43
|
+
throw new Error(`${endpoint} 的 version 为空或包含不允许的字符:${version || '空'}`);
|
|
44
|
+
}
|
|
45
|
+
return { url, version };
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
module.exports = { fetchRemoteModuleConfig };
|