dsh-mobile-plugin 0.5.112 → 0.5.113

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.
@@ -10,6 +10,10 @@
10
10
  *
11
11
  * 写入侧(POST /host-settings/mutate)与桌面端同一口径:只走宿主的 settings.mutate,用按路径
12
12
  * 寻址的 op(set / unset)+ expectedRevision 做乐观并发,绝不整段 replace。
13
+ *
14
+ * 权限档那一项(permission)只有 defaultPreset 是 volatile,describe 下发时会丢掉档位目录
15
+ * presets;手机那行「默认权限预设」要靠这份目录列候选,所以转发时从宿主的 permissionPresets
16
+ * 服务把目录补回去(见 augmentPermissionPresets)。
13
17
  */
14
18
 
15
19
  const { sendJson, readJsonBody } = require('../shared/http-util');
@@ -262,6 +266,57 @@ function normalizeProvider(item) {
262
266
  return { provider, displayName, active: item.active === true };
263
267
  }
264
268
 
269
+ /**
270
+ * 给 permission 命名空间补回档位目录。
271
+ *
272
+ * DSH 的 permission 项只有 defaultPreset 是 volatile 字段:settings.describe 下发时只投影
273
+ * volatile 字段,presets 这张「档名 → { sandbox, approval }」映射不在其中,手机端于是解析不出
274
+ * 候选档,「默认权限预设」那行整行不画。这里从同进程的 permissionPresets.catalog() 拿目录补回
275
+ * value.presets(形状与桌面端一致),App 的 mapPointerTarget / mapPointerChoices 就能认出来。
276
+ *
277
+ * 只取 defaultOptions(配置表里的档),不含 auto 这种 per-call 的临时档 —— volatile 的
278
+ * defaultPreset 只会指向配置表里的档。拿不到服务或目录为空就原样返回,不改动别的命名空间。
279
+ *
280
+ * @param {object} ctx 插件上下文(用 ctx.get 取同进程宿主服务)
281
+ * @param {Array} namespaces 已经 normalizeNamespace 过的命名空间数组(原地补 value)
282
+ */
283
+ function augmentPermissionPresets(ctx, namespaces) {
284
+ const permission = (Array.isArray(namespaces) ? namespaces : [])
285
+ .find((item) => item && item.ns === 'permission');
286
+ if (!permission) return;
287
+ const presets = typeof ctx?.get === 'function' ? ctx.get('permissionPresets') : null;
288
+ if (!presets || typeof presets.catalog !== 'function') return;
289
+
290
+ let catalog;
291
+ try {
292
+ catalog = presets.catalog();
293
+ } catch {
294
+ return;
295
+ }
296
+ const configured = catalog && Array.isArray(catalog.defaultOptions) ? catalog.defaultOptions : [];
297
+ if (configured.length === 0) return;
298
+
299
+ const table = {};
300
+ for (const option of configured) {
301
+ const value = option && typeof option.value === 'string' ? option.value : '';
302
+ if (!value) continue;
303
+ // 值只要有「对象」形状就能过 App 的 mapPointerTarget;保留真实 name/description 供日后读数。
304
+ table[value] = {
305
+ ...(typeof option.name === 'string' ? { name: option.name } : {}),
306
+ ...(typeof option.description === 'string' ? { description: option.description } : {}),
307
+ };
308
+ }
309
+ if (Object.keys(table).length === 0) return;
310
+
311
+ const base = permission.value && typeof permission.value === 'object' && !Array.isArray(permission.value)
312
+ ? permission.value
313
+ : {};
314
+ permission.value = {
315
+ ...base,
316
+ presets: { ...(base.presets && typeof base.presets === 'object' ? base.presets : {}), ...table },
317
+ };
318
+ }
319
+
265
320
  /** GET /api/mobile/v1/host-settings —— 宿主设置命名空间(只读、已脱敏)。 */
266
321
  async function handleHostSettings(ctx, req, res) {
267
322
  // 宿主的 settings.describe 不接受取消信号(apiproxy 的 invoke 也只传 request),故只传信封。
@@ -277,6 +332,7 @@ async function handleHostSettings(ctx, req, res) {
277
332
  .map(normalizeNamespace)
278
333
  .filter(Boolean)
279
334
  .slice(0, HOST_SETTINGS_MAX_NAMESPACES);
335
+ augmentPermissionPresets(ctx, namespaces);
280
336
 
281
337
  let webProviders;
282
338
  try {
@@ -470,6 +526,7 @@ module.exports = {
470
526
  boundValue,
471
527
  credentialRefOf,
472
528
  normalizeNamespace,
529
+ augmentPermissionPresets,
473
530
  normalizeProvider,
474
531
  normalizeOps,
475
532
  };
@@ -0,0 +1,26 @@
1
+ // 给手机下发整包时按平台选打包命令:Windows 没有系统 zip,改用自带的 tar。
2
+ 'use strict';
3
+
4
+ const { execFileSync } = require('node:child_process');
5
+
6
+ /**
7
+ * 按平台给出把目录打成 zip 的命令。
8
+ *
9
+ * 宿主不引第三方压缩库:mac/Linux 用系统 zip,Windows 用系统自带的 bsdtar —— `tar -a`
10
+ * 按输出文件名推断格式,`.zip` 就是 zip。Windows 上直接调 zip 会 `spawnSync zip ENOENT`,
11
+ * 装第三方插件自带的扩展时整包那步就失败了。
12
+ *
13
+ * `platform` 单独收一个参数,是为了让守卫在 mac 上也能钉住 Windows 那条命令。
14
+ */
15
+ function packCommand(platform, outFile) {
16
+ if (platform === 'win32') return { command: 'tar', args: ['-a', '-cf', outFile, '.'] };
17
+ return { command: 'zip', args: ['-r', '-q', '-X', outFile, '.'] };
18
+ }
19
+
20
+ /** 从 directory 里打成 outFile 这份 zip;包内是相对路径,不带目录那一层。 */
21
+ function packDirectory(directory, outFile) {
22
+ const { command, args } = packCommand(process.platform, outFile);
23
+ execFileSync(command, args, { cwd: directory, stdio: 'pipe' });
24
+ }
25
+
26
+ module.exports = { packCommand, packDirectory };