@kne/fastify-app-manager 0.1.36 → 0.1.38
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 +2 -1
- package/index.js +2 -0
- package/libs/controllers/app.js +1 -1
- package/libs/services/app.js +9 -2
- package/libs/utils/remoteIcon.js +99 -39
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -207,7 +207,8 @@ await fastify.listen({ port: 3000 });
|
|
|
207
207
|
| `getOpenApiToken` | function | 否 | `null` | `async app => accessToken`,签发读取子应用 `/open-api.json` 的服务令牌;未配置时 `GET .../app/open-api` 返回 400。接 OIDC 时传 `appOidc.getOpenApiToken` |
|
|
208
208
|
| `issueUserAccessToken` | function | 否 | `null` | `async ({ app, userId, tenantId }) => accessToken`,以登录用户身份签发子应用令牌;未配置时 `POST .../app/open-api/token` 返回 400。接 OIDC 时传 `appOidc.issueUserAccessToken` |
|
|
209
209
|
| `resolveVersionFile` | function | 否 | `null` | `async ({ fileId, name, version, request }) => Buffer`,版本上传传 `fileId`(而非 multipart 文件)时调用,返回 zip 内容;未配置时传 `fileId` 返回 400。版本记录保存 `fileId`;这类版本只在当前部署时常驻磁盘,部署非当前版本或启动时当前版本目录缺失会再次调用以重建 |
|
|
210
|
-
| `saveIconFile` | function | 否 | `null` | `async ({ buffer, filename, mimetype }) => fileId`,凭 installKey 关联外部应用(`POST .../app/link/import`)且未传 `icon`
|
|
210
|
+
| `saveIconFile` | function | 否 | `null` | `async ({ buffer, filename, mimetype }) => fileId`,凭 installKey 关联外部应用(`POST .../app/link/import`)且未传 `icon` 时,平台从远程应用站点收集 favicon / apple-touch-icon / manifest 图标,按真实像素取最大的一张,经此回调存入宿主文件系统并返回文件 id,写入应用 `icon`。未配置则不抓取;抓取或保存失败只记告警,不阻断关联 |
|
|
211
|
+
| `filterCenterApps` | function | 否 | `null` | `async ({ apps, request }) => apps`,登录用户的应用中心列表(`GET .../app/center/list`)返回前调用,宿主可据 `request` 裁剪(如按租户已开通权限隐藏被关闭的应用),返回保留的应用数组;返回非数组则不裁剪。公开列表(`center/public-list`)无登录态,不调用 |
|
|
211
212
|
| `defaultAppDb` | object | 否 | `appsRoot/_shared/apps-data.sqlite` | 托管应用默认库(须与宿主主库分离) |
|
|
212
213
|
| `dbQueryMaxRows` | number | 否 | `500` | 只读 query SQL 最大返回行数 |
|
|
213
214
|
| `pm2Defaults` | object | 否 | 见下表 | 全局 PM2 默认;可被应用 `pm2Config` 覆盖 |
|
package/index.js
CHANGED
|
@@ -71,6 +71,8 @@ module.exports = fp(
|
|
|
71
71
|
onPermissionsRemoved: null,
|
|
72
72
|
/** async () => string[],导入时校验 appName 不与本平台权限顶层码冲突 */
|
|
73
73
|
getPermissionRootCodes: null,
|
|
74
|
+
/** async ({ apps, request }) => apps,登录用户应用中心列表的裁剪钩子(如按租户已开通权限隐藏应用);返回保留的应用数组 */
|
|
75
|
+
filterCenterApps: null,
|
|
74
76
|
linkSyncIntervalMs: 5 * 60 * 1000,
|
|
75
77
|
linkProxyTimeoutMs: 15000,
|
|
76
78
|
/** async ({ fileId, name, version, request }) => Buffer,版本上传传 fileId 时读取宿主文件 */
|
package/libs/controllers/app.js
CHANGED
|
@@ -132,7 +132,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
132
132
|
response: success('运行中的应用,按创建时间倒序', pageResult('应用', centerApp))
|
|
133
133
|
}
|
|
134
134
|
},
|
|
135
|
-
async
|
|
135
|
+
async request => services.app.centerList({ request })
|
|
136
136
|
);
|
|
137
137
|
|
|
138
138
|
fastify.get(
|
package/libs/services/app.js
CHANGED
|
@@ -328,9 +328,9 @@ module.exports = fp(async (fastify, options) => {
|
|
|
328
328
|
};
|
|
329
329
|
};
|
|
330
330
|
|
|
331
|
-
const centerList = async ({ publicOnly = false } = {}) => {
|
|
331
|
+
const centerList = async ({ publicOnly = false, request } = {}) => {
|
|
332
332
|
const rows = await models.app.findAll({ where: { status: 'running' }, order: [['createdAt', 'DESC']] });
|
|
333
|
-
|
|
333
|
+
let pageData = rows
|
|
334
334
|
.filter(app => !publicOnly || isAppPublic(app))
|
|
335
335
|
.map(app => {
|
|
336
336
|
const active = deployProgress.active(app.name);
|
|
@@ -352,6 +352,13 @@ module.exports = fp(async (fastify, options) => {
|
|
|
352
352
|
deployment: active ? { source: active.source } : null
|
|
353
353
|
};
|
|
354
354
|
});
|
|
355
|
+
// 登录用户的应用中心可由宿主按租户/权限裁剪(公开列表无登录态,不裁剪)
|
|
356
|
+
if (!publicOnly && request && typeof options.filterCenterApps === 'function') {
|
|
357
|
+
const filtered = await options.filterCenterApps({ apps: pageData, request });
|
|
358
|
+
if (Array.isArray(filtered)) {
|
|
359
|
+
pageData = filtered;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
355
362
|
return { pageData, totalCount: pageData.length };
|
|
356
363
|
};
|
|
357
364
|
|
package/libs/utils/remoteIcon.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
const MAX_ICON_BYTES = 1024 * 1024;
|
|
2
|
-
const MAX_CANDIDATES =
|
|
2
|
+
const MAX_CANDIDATES = 8;
|
|
3
|
+
// SVG 没有固有像素尺寸,按一个中等值参与比较:有更大的位图时用位图,否则矢量图也比一张很小的位图好
|
|
4
|
+
const SVG_NOMINAL_SIZE = 256;
|
|
3
5
|
|
|
4
6
|
const MIME_EXT = {
|
|
5
7
|
'image/png': 'png',
|
|
@@ -25,11 +27,10 @@ const sizeOf = sizes => {
|
|
|
25
27
|
return values.length ? Math.max(...values) : 0;
|
|
26
28
|
};
|
|
27
29
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
/** 从首页 HTML 解析 <link rel=icon|apple-touch-icon|manifest>,已按优先级排序 */
|
|
30
|
+
/** 从首页 HTML 解析 <link rel=icon|apple-touch-icon|manifest>,返回候选 [{ url, size }] 与 manifest 地址 */
|
|
31
31
|
const parseHtmlIcons = (html, pageUrl) => {
|
|
32
|
-
const
|
|
32
|
+
const touch = [];
|
|
33
|
+
const normal = [];
|
|
33
34
|
let manifestUrl = null;
|
|
34
35
|
for (const tag of String(html || '').match(/<link\b[^>]*>/gi) || []) {
|
|
35
36
|
const rel = String(attrOf(tag, 'rel') || '').toLowerCase();
|
|
@@ -46,29 +47,80 @@ const parseHtmlIcons = (html, pageUrl) => {
|
|
|
46
47
|
if (/\bmanifest\b/.test(rel)) {
|
|
47
48
|
manifestUrl = url;
|
|
48
49
|
} else if (/\bapple-touch-icon(-precomposed)?\b/.test(rel)) {
|
|
49
|
-
|
|
50
|
+
touch.push({ url, size: sizeOf(attrOf(tag, 'sizes')) });
|
|
50
51
|
} else if (/\bicon\b/.test(rel)) {
|
|
51
|
-
|
|
52
|
+
normal.push({ url, size: sizeOf(attrOf(tag, 'sizes')) });
|
|
52
53
|
}
|
|
53
54
|
}
|
|
54
|
-
|
|
55
|
-
const normal = icons.filter(item => !item.touch).sort(bySizeDesc);
|
|
56
|
-
return { icons: [...touch, ...normal].map(item => item.url), manifestUrl };
|
|
55
|
+
return { icons: [...touch, ...normal], manifestUrl };
|
|
57
56
|
};
|
|
58
57
|
|
|
58
|
+
/** manifest icons → 候选 [{ url, size, maskable }];maskable 图带安全边距,同尺寸时排在普通图之后 */
|
|
59
59
|
const parseManifestIcons = (manifest, manifestUrl) =>
|
|
60
60
|
(Array.isArray(manifest?.icons) ? manifest.icons : [])
|
|
61
61
|
.filter(item => item && item.src)
|
|
62
62
|
.map(item => {
|
|
63
63
|
try {
|
|
64
|
-
|
|
64
|
+
const purposes = String(item.purpose || 'any')
|
|
65
|
+
.toLowerCase()
|
|
66
|
+
.split(/\s+/);
|
|
67
|
+
return { url: new URL(item.src, manifestUrl).href, size: sizeOf(item.sizes), maskable: purposes.includes('maskable') && !purposes.includes('any') };
|
|
65
68
|
} catch (e) {
|
|
66
69
|
return null;
|
|
67
70
|
}
|
|
68
71
|
})
|
|
69
|
-
.filter(Boolean)
|
|
70
|
-
|
|
71
|
-
|
|
72
|
+
.filter(Boolean);
|
|
73
|
+
|
|
74
|
+
/** 读图片头得到真实像素最大边;认不出或没有固有尺寸时返回 0 */
|
|
75
|
+
const imageSize = (buffer, mimetype) => {
|
|
76
|
+
const b = buffer;
|
|
77
|
+
try {
|
|
78
|
+
if (b.length >= 24 && b.readUInt32BE(0) === 0x89504e47 && b.toString('ascii', 12, 16) === 'IHDR') {
|
|
79
|
+
return Math.max(b.readUInt32BE(16), b.readUInt32BE(20));
|
|
80
|
+
}
|
|
81
|
+
if (b.length >= 6 && b.readUInt16LE(0) === 0 && b.readUInt16LE(2) === 1) {
|
|
82
|
+
let max = 0;
|
|
83
|
+
for (let i = 0; i < b.readUInt16LE(4) && 6 + i * 16 + 2 <= b.length; i += 1) {
|
|
84
|
+
max = Math.max(max, b[6 + i * 16] || 256, b[7 + i * 16] || 256);
|
|
85
|
+
}
|
|
86
|
+
return max;
|
|
87
|
+
}
|
|
88
|
+
if (b.length >= 10 && b.toString('ascii', 0, 4) === 'GIF8') {
|
|
89
|
+
return Math.max(b.readUInt16LE(6), b.readUInt16LE(8));
|
|
90
|
+
}
|
|
91
|
+
if (b.length >= 4 && b[0] === 0xff && b[1] === 0xd8) {
|
|
92
|
+
let offset = 2;
|
|
93
|
+
while (offset + 9 < b.length) {
|
|
94
|
+
if (b[offset] !== 0xff) {
|
|
95
|
+
offset += 1;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
const marker = b[offset + 1];
|
|
99
|
+
if (marker >= 0xc0 && marker <= 0xcf && ![0xc4, 0xc8, 0xcc].includes(marker)) {
|
|
100
|
+
return Math.max(b.readUInt16BE(offset + 5), b.readUInt16BE(offset + 7));
|
|
101
|
+
}
|
|
102
|
+
offset += 2 + b.readUInt16BE(offset + 2);
|
|
103
|
+
}
|
|
104
|
+
return 0;
|
|
105
|
+
}
|
|
106
|
+
if (b.length >= 30 && b.toString('ascii', 0, 4) === 'RIFF' && b.toString('ascii', 8, 12) === 'WEBP') {
|
|
107
|
+
const type = b.toString('ascii', 12, 16);
|
|
108
|
+
if (type === 'VP8X') {
|
|
109
|
+
return Math.max(1 + b.readUIntLE(24, 3), 1 + b.readUIntLE(27, 3));
|
|
110
|
+
}
|
|
111
|
+
if (type === 'VP8L') {
|
|
112
|
+
const bits = b.readUInt32LE(21);
|
|
113
|
+
return Math.max((bits & 0x3fff) + 1, ((bits >> 14) & 0x3fff) + 1);
|
|
114
|
+
}
|
|
115
|
+
if (type === 'VP8 ') {
|
|
116
|
+
return Math.max(b.readUInt16LE(26) & 0x3fff, b.readUInt16LE(28) & 0x3fff);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
} catch (e) {
|
|
120
|
+
return 0;
|
|
121
|
+
}
|
|
122
|
+
return mimetype === 'image/svg+xml' ? SVG_NOMINAL_SIZE : 0;
|
|
123
|
+
};
|
|
72
124
|
|
|
73
125
|
const filenameOf = (url, mimetype) => {
|
|
74
126
|
const base = decodeURIComponent(new URL(url).pathname.split('/').pop() || '');
|
|
@@ -79,7 +131,8 @@ const filenameOf = (url, mimetype) => {
|
|
|
79
131
|
};
|
|
80
132
|
|
|
81
133
|
/**
|
|
82
|
-
* 从远程应用前端站点获取图标:首页 <link rel=icon/apple-touch-icon
|
|
134
|
+
* 从远程应用前端站点获取图标:首页 <link rel=icon/apple-touch-icon>、web manifest icons、/favicon.ico 全部作为候选,
|
|
135
|
+
* 逐个下载后读真实像素尺寸,取最大的一张(站点没声明 sizes 也能比出大小);尺寸相同按 touch > icon > manifest > favicon.ico,普通图优先于 maskable。
|
|
83
136
|
* 只接受 image/* 且不超过 1MB 的响应(SPA 对未知路径回落 HTML 的会被丢弃)。
|
|
84
137
|
* 失败返回 null,不抛错。
|
|
85
138
|
*/
|
|
@@ -112,31 +165,38 @@ const discoverRemoteIcon = async ({ baseUrl, timeoutMs = 5000, fetchImpl = fetch
|
|
|
112
165
|
} catch (e) {
|
|
113
166
|
// 首页取不到,退回 /favicon.ico
|
|
114
167
|
}
|
|
115
|
-
candidates.push(`${base}/favicon.ico
|
|
168
|
+
candidates.push({ url: `${base}/favicon.ico`, size: 0 });
|
|
116
169
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
.
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
170
|
+
const unique = [...new Map(candidates.map(item => [item.url, item])).values()].slice(0, MAX_CANDIDATES);
|
|
171
|
+
const fetched = await Promise.all(
|
|
172
|
+
unique.map(async (item, order) => {
|
|
173
|
+
try {
|
|
174
|
+
const response = await request(item.url, 'image/*');
|
|
175
|
+
if (!response) {
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
const mimetype = String(response.headers.get('content-type') || '')
|
|
179
|
+
.split(';')[0]
|
|
180
|
+
.trim()
|
|
181
|
+
.toLowerCase();
|
|
182
|
+
if (!mimetype.startsWith('image/')) {
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
const buffer = Buffer.from(await response.arrayBuffer());
|
|
186
|
+
if (!buffer.length || buffer.length > MAX_ICON_BYTES) {
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
// 真实尺寸优先;读不出时退回站点声明的 sizes
|
|
190
|
+
const pixels = imageSize(buffer, mimetype) || item.size;
|
|
191
|
+
return { buffer, mimetype, filename: filenameOf(item.url, mimetype), pixels, maskable: !!item.maskable, order };
|
|
192
|
+
} catch (e) {
|
|
193
|
+
return null;
|
|
133
194
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
}
|
|
139
|
-
return null;
|
|
195
|
+
})
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
const best = fetched.filter(Boolean).sort((a, b) => b.pixels - a.pixels || Number(a.maskable) - Number(b.maskable) || a.order - b.order)[0];
|
|
199
|
+
return best ? { buffer: best.buffer, mimetype: best.mimetype, filename: best.filename } : null;
|
|
140
200
|
};
|
|
141
201
|
|
|
142
|
-
module.exports = { discoverRemoteIcon, parseHtmlIcons, parseManifestIcons };
|
|
202
|
+
module.exports = { discoverRemoteIcon, parseHtmlIcons, parseManifestIcons, imageSize };
|