@kne/fastify-app-manager 0.1.18 → 0.1.19

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 CHANGED
@@ -198,6 +198,7 @@ await fastify.listen({ port: 3000 });
198
198
  | `portMax` | number | 否 | `7999` | 端口池上限(含) |
199
199
  | `pathPrefix` | string | 否 | `'/app'` | 路径访问前缀(无尾斜杠亦可) |
200
200
  | `passthroughEnvKeys` | array | 否 | `[]` | 允许注入子进程的宿主 env 键名 |
201
+ | `pwaIconPrefix` | string | 否 | `'/api/v1/static/file-id'` | PWA manifest 图标地址前缀(应用 `icon` 为文件 id 时拼接) |
201
202
  | `secretEnvKeyPattern` | RegExp | 否 | `/(SECRET\|PASSWORD\|TOKEN\|KEY\|PRIVATE)/i` | 按键名正则脱敏;与显式 `secretEnvKeys` 取并集 |
202
203
  | `systemEnvKeys` | array | 否 | `[]` | 宿主维护的系统变量键名,与内置 `DB_TABLE_PREFIX` 合并:接口不返回,create / save / saveEnv 不可新增、修改或删除 |
203
204
  | `resolveSystemEnv` | function | 否 | `null` | `async ({ app, version, serverDir }) => env \| null`,每次启动进程(deploy / start / restart / 恢复)前调用;返回值覆盖应用变量注入子进程,值为 `null` / `undefined` 的键忽略,属于 `systemEnvKeys` 的键持久化到 `app.env` |
@@ -566,6 +567,15 @@ data: {"progressId":"p-1","step":"ready","status":"done","detail":null,"message"
566
567
 
567
568
  公开应用中心列表,**无需登录**。返回结构同 `app/center/list`,仅包含运行中且 `isPublic` 不为 `false` 的应用。
568
569
 
570
+ ##### GET `{prefix}/app/center/webmanifest`
571
+
572
+ 公开应用的 PWA manifest,**无需登录**,`Content-Type: application/manifest+json`。由 `@kne/pwa-document` 的 `buildWebManifest` 生成。
573
+
574
+ | 参数 | 类型 | 必填 | 说明 |
575
+ |------|------|------|------|
576
+ | `name` | string | 是 | 应用名;不在公开列表中时 404 |
577
+ | `path` | string | 否 | 应用入口路径(主入口或 `entries[].path`);不匹配时回落到第一个入口 |
578
+
569
579
  ##### GET `{prefix}/app/detail`
570
580
 
571
581
  | 参数名 | 类型 | 必填 | 说明 |
@@ -777,6 +787,8 @@ data: {"name":"demo","ts":1791456608533,...}
777
787
  | `resolveAppLogFile({ name, file })` | 校验并返回 `{ path, fileName, size, compressed }`(下载用) |
778
788
  | `resolveAppLogFiles({ name, files })` | 批量校验(去重),返回 `{ name, files: [{ path, fileName, size, mtime, compressed }] }`(打包下载用) |
779
789
  | `removeLogFiles({ name, files })` | 删除归档,返回 `{ removed }` |
790
+ | `pwaManifest({ name, path? })` | 同 `GET .../app/center/webmanifest` |
791
+ | `resolvePwaEntry(urlPath, { shellEntries?, defaultIcon? })` | 按访问路径在公开应用中匹配 PWA 入口(`@kne/pwa-document` 的 `resolveEntry`),未命中时回落 `shellEntries`;宿主用于在 HTML 中注入 manifest |
780
792
  | `load({ name })` | 返回 `{ intervalMs, current, pageData }` |
781
793
  | `getLoadStore()` | 负载环形缓冲:`history(name)` / `latest(name)` / `emitter`(事件 `load:{name}`) |
782
794
  | `findByHost(host, { running? })` | 网关:按 domain 查找;默认只查 `running`,`running: false` 时不限状态 |
package/index.js CHANGED
@@ -51,6 +51,8 @@ module.exports = fp(
51
51
  /** async ({ fileId, name, version, request }) => Buffer,版本上传传 fileId 时读取宿主文件 */
52
52
  resolveVersionFile: null,
53
53
  secretEnvKeyPattern: /(SECRET|PASSWORD|TOKEN|KEY|PRIVATE)/i,
54
+ /** 应用图标为文件 id 时,PWA manifest 中图标地址的前缀 */
55
+ pwaIconPrefix: '/api/v1/static/file-id',
54
56
  sqlPath: 'sql',
55
57
  migrateBeforeStart: false,
56
58
  defaultAppDb: {
@@ -94,6 +94,28 @@ module.exports = fp(async (fastify, options) => {
94
94
  async () => services.app.centerList({ publicOnly: true })
95
95
  );
96
96
 
97
+ fastify.get(
98
+ `${options.prefix}/app/center/webmanifest`,
99
+ {
100
+ schema: {
101
+ summary: '应用中心:公开应用的 PWA manifest(无需登录)',
102
+ querystring: {
103
+ type: 'object',
104
+ required: ['name'],
105
+ properties: {
106
+ name: { type: 'string' },
107
+ path: { type: 'string' }
108
+ }
109
+ }
110
+ }
111
+ },
112
+ async (request, reply) => {
113
+ const manifest = await services.app.pwaManifest({ name: request.query.name, path: request.query.path });
114
+ reply.type('application/manifest+json; charset=utf-8').send(manifest);
115
+ return reply;
116
+ }
117
+ );
118
+
97
119
  fastify.post(
98
120
  `${options.prefix}/app/save-env`,
99
121
  {
@@ -27,6 +27,7 @@ const { toIsoInTz, convertLogText } = require('../utils/logTime');
27
27
  const { runSqlMigrations, MIGRATION_ACTIONS } = require('../utils/migrate');
28
28
  const { readSqlDir, writeSqlDir } = require('../utils/versionSql');
29
29
  const { createDeployProgress } = require('../utils/deployProgress');
30
+ const pwaDocument = require('@kne/pwa-document');
30
31
  const pm2Util = require('../utils/pm2');
31
32
 
32
33
  const NAME_RE = /^[a-z0-9]([a-z0-9-]{0,62}[a-z0-9])?$/;
@@ -267,6 +268,31 @@ module.exports = fp(async (fastify, options) => {
267
268
  return { pageData, totalCount: pageData.length };
268
269
  };
269
270
 
271
+ const pwaOptions = () => ({ manifestPrefix: options.prefix, staticPrefix: options.pwaIconPrefix });
272
+
273
+ /** 公开应用的 webmanifest;path 为应用入口地址,不传或不匹配时用应用根路径 */
274
+ const pwaManifest = async ({ name, path: surfacePath } = {}) => {
275
+ const { pageData } = await centerList({ publicOnly: true });
276
+ const app = pageData.find(item => item.name === name);
277
+ if (!app) {
278
+ httpError(404, `app ${name} not found`);
279
+ }
280
+ const surfaces = pwaDocument.appSurfaces(app);
281
+ const wanted = pwaDocument.normalizePath(surfacePath || app.pathUrl);
282
+ const surface = surfaces.find(item => pwaDocument.normalizePath(item.path) === wanted) || surfaces[0];
283
+ return pwaDocument.buildWebManifest(app, {
284
+ path: surface.path,
285
+ title: surface.title,
286
+ icon: pwaDocument.iconUrlFor(app.icon, pwaOptions())
287
+ });
288
+ };
289
+
290
+ /** 按请求路径匹配公开应用入口,shellEntries 为宿主自身页面;无匹配返回 null */
291
+ const resolvePwaEntry = async (urlPath, { shellEntries = [], defaultIcon } = {}) => {
292
+ const { pageData } = await centerList({ publicOnly: true });
293
+ return pwaDocument.resolveEntry(urlPath, pageData, Object.assign(pwaOptions(), { shellEntries, defaultIcon }));
294
+ };
295
+
270
296
  const detail = async ({ name }) => {
271
297
  const app = await getByName(name);
272
298
  return Object.assign(toPublicApp(app), { deployment: deployProgress.active(app.name), lastDeployment: await lastDeployment(app.name) });
@@ -1439,6 +1465,8 @@ module.exports = fp(async (fastify, options) => {
1439
1465
  saveEnv,
1440
1466
  list,
1441
1467
  centerList,
1468
+ pwaManifest,
1469
+ resolvePwaEntry,
1442
1470
  detail,
1443
1471
  uploadVersion,
1444
1472
  listVersions,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-app-manager",
3
- "version": "0.1.18",
3
+ "version": "0.1.19",
4
4
  "description": "Fastify 单机应用市场插件:版本上传、PM2 托管、路径/域名反代与日志 SSE",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -68,6 +68,7 @@
68
68
  "@fastify/multipart": "^10.1.1",
69
69
  "@fastify/reply-from": "^12.6.5",
70
70
  "@kne/ensure-slash": "^0.1.0",
71
+ "@kne/pwa-document": "^0.1.0",
71
72
  "adm-zip": "^0.6.0",
72
73
  "ajv": "^8.17.1",
73
74
  "fs-extra": "^11.4.0",