dsh-mobile-ios 0.2.14 → 0.2.16

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/lib/client.js CHANGED
@@ -913,29 +913,54 @@ window.__ModuleLoader__.load({
913
913
  return;
914
914
  }
915
915
  if (compareVersions(latest, current) > 0) {
916
- // 一键更新有两条路,面板不再把「复制包名」当主路径:
917
- // ① 宿主给了 pluginManager.installBundle → 交给 DSH 安装器;
918
- // ② 没给(老宿主 / 自定义协议页面)→ 网关自己取 npm tarball 覆盖安装。
919
- // 以前 ② 缺失就只剩复制包名,而这类页面里剪贴板本身也不可用,
920
- // 结果就是「既不能一键更新、也复制不了」的死角。
921
- const canAuto = !!(pluginManager && typeof pluginManager.installBundle === 'function');
916
+ // 更新能力分三档,面板按宿主实际给了什么决定按钮,绝不摆一个按了没反应的按钮:
917
+ // ① updateBundle —— 宿主插件管理器自己的升级入口(与「插件」页那个
918
+ // 「更新到 vX」完全同一条代码路径:先移除旧包再装新包并热加载)。
919
+ // 已安装的包只能走这条,installBundle 会被宿主判为「该插件已安装」。
920
+ // ② installBundle —— 老宿主没有 updateBundle 时的等效能力。
921
+ // ③ 都没有 —— 只给图文指引:去「插件」页点右上角刷新,交给宿主安装。
922
+ const hostUpdate = !!(pluginManager && typeof pluginManager.updateBundle === 'function');
923
+ const hostInstall = !!(pluginManager && typeof pluginManager.installBundle === 'function');
924
+ const canAuto = hostUpdate || hostInstall;
922
925
  showUpdateModal({
923
926
  title: '新版本更新',
924
- banner: { text: `⬆️ 发现新版本 v${latest},可一键更新`, tone: 'info' },
927
+ banner: { text: `⬆️ 发现新版本 v${latest}`, tone: 'info' },
925
928
  lines: [
926
929
  { label: '当前版本', value: `v${current}` },
927
930
  { label: '最新版本', value: `v${latest}`, highlight: true },
928
931
  ],
929
932
  note: canAuto
930
- ? '点击「立即更新」将由 DSH 安装器一步完成,更新中请勿关闭窗口。是否更新由你决定。'
931
- : '点击「立即更新」由本机网关直接下载并安装,无需复制包名;完成后重启 DSH 生效。',
932
- actions: [
933
- { label: '稍后再说', onClick: closeUpdateModal },
934
- {
935
- label: `立即更新到 v${latest}`, primary: true,
936
- onClick: () => performUpdate(latest),
937
- },
933
+ ? '点「立即更新」由 DSH 插件管理器安装并切换到新版本,完成后重启 DSH 生效。也可以到「插件」页点右上角「刷新」手动更新。'
934
+ : '本面板没拿到 DSH 插件管理器接口,请按下面步骤在「插件」页更新,由 DSH 自带的插件更新机制安装。',
935
+ // 没有宿主接口时,把「点哪里」写清楚:只报「可更新」不给路径等于没帮上忙。
936
+ steps: canAuto ? undefined : [
937
+ '在 DSH 左侧点「插件」',
938
+ '点右上角「刷新」',
939
+ `在列表里点 dsh-mobile-ios 的「更新到 v${latest}」`,
940
+ '重启 DSH 桌面端,让新版本生效',
938
941
  ],
942
+ actions: canAuto
943
+ ? [
944
+ { label: '稍后再说', onClick: closeUpdateModal },
945
+ { label: `立即更新到 v${latest}`, primary: true, onClick: () => performUpdate(latest) },
946
+ ]
947
+ : [
948
+ {
949
+ label: '复制包名',
950
+ onClick: async (btn) => {
951
+ const ok = await copyToClipboard(PLUGIN_NAME);
952
+ if (ok) {
953
+ btn.textContent = '已复制';
954
+ showModalToast(`已复制 ${PLUGIN_NAME},可在「插件 → 添加插件」粘贴`, 'success');
955
+ } else {
956
+ btn.textContent = '请手动复制';
957
+ showModalToast('本机剪贴板不可用,请在下方框内全选复制', 'error');
958
+ offerManualCopy(PLUGIN_NAME);
959
+ }
960
+ },
961
+ },
962
+ { label: '知道了', primary: true, onClick: closeUpdateModal },
963
+ ],
939
964
  });
940
965
  } else {
941
966
  showUpdateModal({
@@ -1028,7 +1053,8 @@ window.__ModuleLoader__.load({
1028
1053
  updateInFlight = true;
1029
1054
  const current = summary?.version || '未知';
1030
1055
  const spec = `${PLUGIN_NAME}@${latest}`;
1031
- const canHost = !!(pluginManager && typeof pluginManager.installBundle === 'function');
1056
+ const canHost = !!(pluginManager
1057
+ && (typeof pluginManager.updateBundle === 'function' || typeof pluginManager.installBundle === 'function'));
1032
1058
  // 先持久化「更新中」,确保 HMR 卸载本模块后新实例能恢复出结果反馈。
1033
1059
  writeUpdateState({ status: 'updating', target: String(latest), at: Date.now() });
1034
1060
  // 状态:更新中。无取消/关闭动作外的操作按钮,防重复点击。
@@ -1040,14 +1066,33 @@ window.__ModuleLoader__.load({
1040
1066
  { label: '目标版本', value: `v${latest}`, highlight: true },
1041
1067
  ],
1042
1068
  note: canHost
1043
- ? '正在通过 DSH 安装器更新,可能需要 1–3 分钟。请勿重复点击或关闭窗口。'
1069
+ ? '正在通过 DSH 插件管理器更新(先卸旧版再装新版),约 1–3 分钟。请勿重复点击或关闭窗口。'
1044
1070
  : '正在经本机网关下载并安装(含完整性校验),请勿重复点击或关闭窗口。',
1045
1071
  actions: [],
1046
1072
  });
1047
1073
  try {
1048
1074
  let restart = false;
1049
- if (canHost) {
1050
- // ① 宿主安装器:最常见的 Desktop 路径
1075
+ const canHostUpdate = !!(pluginManager && typeof pluginManager.updateBundle === 'function');
1076
+ if (canHostUpdate) {
1077
+ // ⓪ 宿主插件管理器的升级入口:与「插件」页「更新到 vX」同一条路径
1078
+ // (内部先 removeBundle 再 installBundle,并触发 HMR 热切换)。
1079
+ // 已安装的包只能走这条 —— installBundle 会被判「该插件已安装」。
1080
+ let requestId = globalThis.crypto?.randomUUID?.();
1081
+ if (typeof requestId !== 'string') throw new Error('无法生成更新请求 ID');
1082
+ const settled = await withTimeout(
1083
+ pluginManager.updateBundle(String(PLUGIN_NAME), String(latest), { requestId }),
1084
+ UPDATE_TIMEOUT_MS, '更新',
1085
+ );
1086
+ const out = unwrapRemote(settled) || {};
1087
+ const result = out.result || {};
1088
+ // 宿主用 application:'failed' + error 表达拒绝/失败,不能当成功吞掉
1089
+ if (result.application === 'failed' || result.error) {
1090
+ const why = result.error?.diagnostic || result.error?.code || out.step || '宿主未说明原因';
1091
+ throw new Error(`DSH 插件管理器更新失败:${why}`);
1092
+ }
1093
+ restart = true;
1094
+ } else if (typeof pluginManager?.installBundle === 'function') {
1095
+ // ① 老宿主安装器:最常见的 Desktop 路径
1051
1096
  let requestId;
1052
1097
  requestId = globalThis.crypto?.randomUUID?.();
1053
1098
  if (typeof requestId !== 'string') throw new Error('无法生成更新请求 ID');
@@ -1068,7 +1113,7 @@ window.__ModuleLoader__.load({
1068
1113
  const value = unwrapRemote(result);
1069
1114
  restart = !!(value && (value.restartRequired || value.restart));
1070
1115
  } else {
1071
- // ② 网关自更新:宿主没给 installBundle 时的等效能力。
1116
+ // ② 网关自更新:宿主没给任何插件管理接口时的等效能力。
1072
1117
  // 网关从 npm 取 tarball、按 dist.shasum 校验、备份旧目录后覆盖安装。
1073
1118
  const res = await fetchJson('/mobile/v1/plugin/self-update', {
1074
1119
  method: 'POST',
@@ -1091,16 +1136,23 @@ window.__ModuleLoader__.load({
1091
1136
  actions: [{ label: '知道了', primary: true, onClick: closeUpdateModal }],
1092
1137
  });
1093
1138
  } catch (err) {
1094
- // 状态:失败(保留重试入口;重试会重新走一遍锁与状态机)
1139
+ // 状态:失败。不能只报错就完 —— 顺手把「插件页刷新」这条路指出来,
1140
+ // 宿主自己的安装器通常能成功(它走的是 pnpm + 本机 registry 镜像)。
1095
1141
  clearUpdateState();
1096
1142
  showUpdateModal({
1097
1143
  title: '更新失败',
1098
- banner: { text: '❌ 更新未成功', tone: 'error' },
1144
+ banner: { text: '❌ 面板内更新未成功', tone: 'error' },
1099
1145
  lines: [{ label: '目标版本', value: `v${latest}` }],
1100
- note: `安装失败:${err?.body?.error?.message || err?.message || String(err)}。可重试,也可以复制包名到「插件 → 添加插件」手动安装。`,
1146
+ note: `失败原因:${err?.body?.error?.message || err?.message || String(err)}。可重试;更稳的办法是到「插件」页刷新,由 DSH 自己安装。`,
1147
+ steps: [
1148
+ '在 DSH 左侧点「插件」',
1149
+ '点右上角「刷新」',
1150
+ `在列表里点 dsh-mobile-ios 的「更新到 v${latest}」`,
1151
+ '重启 DSH 桌面端,让新版本生效',
1152
+ ],
1101
1153
  actions: [
1102
1154
  { label: '关闭', onClick: closeUpdateModal },
1103
- { label: '重试更新', primary: true, onClick: () => performUpdate(latest) },
1155
+ { label: '重试更新', onClick: () => performUpdate(latest) },
1104
1156
  {
1105
1157
  label: '复制包名',
1106
1158
  onClick: async (btn) => {
@@ -1115,6 +1167,7 @@ window.__ModuleLoader__.load({
1115
1167
  }
1116
1168
  },
1117
1169
  },
1170
+ { label: '知道了', primary: true, onClick: closeUpdateModal },
1118
1171
  ],
1119
1172
  });
1120
1173
  } finally {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-mobile-ios",
3
- "version": "0.2.14",
3
+ "version": "0.2.16",
4
4
  "type": "module",
5
5
  "description": "DSH iOS移动伴侣:iPhone/iPad 扫码连接 DSH 桌面端,局域网直连 + Cloudflare Tunnel 固定域名远程两种通道",
6
6
  "main": "entries/plugin.mjs",
@@ -55,6 +55,104 @@ function sha1(buf) {
55
55
  return createHash('sha1').update(buf).digest('hex');
56
56
  }
57
57
 
58
+ /** 锁文件里我们这一个包的条目重写:importers / packages / snapshots 三处都要对齐。 */
59
+ function rewriteLock(text, { from, to, specifier, integrity }) {
60
+ const lines = text.split('\n');
61
+ const headPattern = new RegExp(`^(\\s{2})${PLUGIN_NAME}@([^:\\s]+):(.*)$`);
62
+ const depPattern = new RegExp(`^(\\s+)${PLUGIN_NAME}:\\s*$`);
63
+ for (let i = 0; i < lines.length; i += 1) {
64
+ // ① importers 下的 specifier / version
65
+ const dep = lines[i].match(depPattern);
66
+ if (dep) {
67
+ const baseIndent = dep[1].length;
68
+ for (let j = i + 1; j < lines.length; j += 1) {
69
+ if (!lines[j].trim()) continue;
70
+ const indent = lines[j].length - lines[j].trimStart().length;
71
+ if (indent <= baseIndent) break;
72
+ const kv = lines[j].match(/^(\s*)(specifier|version):\s*(.+)$/);
73
+ if (!kv) continue;
74
+ if (kv[2] === 'specifier' && specifier) lines[j] = `${kv[1]}specifier: ${specifier}`;
75
+ // version 可能带 peer 后缀,只替换版本前缀
76
+ if (kv[2] === 'version' && kv[3].trim().split('(')[0] === from) {
77
+ lines[j] = `${kv[1]}version: ${to}${kv[3].trim().slice(from.length)}`;
78
+ }
79
+ }
80
+ }
81
+ // ② packages / snapshots 的条目头,紧随其后的 resolution.integrity 一并改
82
+ const head = lines[i].match(headPattern);
83
+ if (head && head[2] === from) {
84
+ lines[i] = `${head[1]}${PLUGIN_NAME}@${to}:${head[3]}`;
85
+ for (let j = i + 1; j < lines.length; j += 1) {
86
+ if (!lines[j].trim()) continue;
87
+ if (!/^\s{4}/.test(lines[j])) break;
88
+ if (integrity && /^\s*resolution:/.test(lines[j])) {
89
+ lines[j] = lines[j].replace(/integrity:\s*[^,}]+/, `integrity: ${integrity}`);
90
+ }
91
+ }
92
+ }
93
+ }
94
+ return lines.join('\n');
95
+ }
96
+
97
+ /**
98
+ * 自更新后同步宿主配置档里的「版本钉」。
99
+ *
100
+ * 为什么必须做:安装目录是宿主包管理器(pnpm)托管的,它的 package.json
101
+ * dependencies 与 pnpm-lock.yaml 都钉着具体版本。只替换 node_modules 里的文件,
102
+ * 下一次 `pnpm install`(插件开关、配置档修复都会触发)就按旧钉重装,自更新被静默
103
+ * 回滚——在 macOS 上表现为「刚修好的自动安装又坏了」。所以这里把 specifier 与
104
+ * 锁文件三处一起改到新版本,并用下载到的 tarball 现算 integrity,避免锁文件校验不过。
105
+ *
106
+ * 只在「确实装在 node_modules 里」时动手:开发态直接跑仓库目录,上层不是配置档。
107
+ */
108
+ export function syncProfilePin({ pluginRoot, version, tarball, log = () => {} }) {
109
+ if (path.basename(pluginRoot) !== PLUGIN_NAME) return { synced: false, reason: 'not-plugin-root', path: null };
110
+ const modulesDir = path.dirname(pluginRoot);
111
+ if (path.basename(modulesDir) !== 'node_modules') return { synced: false, reason: 'not-in-node-modules', path: null };
112
+ const profileRoot = path.dirname(modulesDir);
113
+ const manifestPath = path.join(profileRoot, 'package.json');
114
+ if (!fs.existsSync(manifestPath)) return { synced: false, reason: 'no-profile-manifest', path: null };
115
+
116
+ let manifest = null;
117
+ try {
118
+ manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
119
+ } catch {
120
+ return { synced: false, reason: 'bad-profile-manifest', path: null };
121
+ }
122
+ const deps = manifest?.dependencies;
123
+ if (!deps || !Object.prototype.hasOwnProperty.call(deps, PLUGIN_NAME)) {
124
+ return { synced: false, reason: 'not-a-dependency', path: null };
125
+ }
126
+
127
+ const old = String(deps[PLUGIN_NAME]);
128
+ // 保留用户/宿主原本的区间写法(^ 或 ~),只换版本号
129
+ const next = /^[\^~]/.test(old) ? `${old[0]}${version}` : version;
130
+ let changed = false;
131
+ if (old !== next) {
132
+ deps[PLUGIN_NAME] = next;
133
+ fs.writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`);
134
+ changed = true;
135
+ }
136
+
137
+ const lockPath = path.join(profileRoot, 'pnpm-lock.yaml');
138
+ if (fs.existsSync(lockPath)) {
139
+ const integrity = `sha512-${createHash('sha512').update(tarball).digest('base64')}`;
140
+ const before = fs.readFileSync(lockPath, 'utf8');
141
+ const after = rewriteLock(before, {
142
+ from: old.replace(/^[\^~]/, ''),
143
+ to: version,
144
+ specifier: next,
145
+ integrity,
146
+ });
147
+ if (after !== before) {
148
+ fs.writeFileSync(lockPath, after);
149
+ log(`配置档锁文件已同步 ${PLUGIN_NAME}@${version}`);
150
+ changed = true;
151
+ }
152
+ }
153
+ return { synced: changed, reason: changed ? 'ok' : 'already-current', path: manifestPath };
154
+ }
155
+
58
156
  /** 取 registry 元数据:目标版本(缺省 latest)的 tarball 地址与 shasum。 */
59
157
  async function loadMetadata(registry, { name = PLUGIN_NAME, target = null, fetchImpl }) {
60
158
  const res = await fetchImpl(`${registry}/${name}`, {
@@ -153,8 +251,21 @@ export async function selfUpdatePlugin({
153
251
  const now = readVersion(pluginRoot);
154
252
  if (now !== meta.version) throw new Error(`覆盖后版本仍为 ${now},更新未生效`);
155
253
 
254
+ // 同步配置档版本钉:不同步的话,下次宿主 install 会按旧钉把自更新悄悄回滚。
255
+ // 这一步失败不影响本次更新(磁盘上已是新版),只记录原因。
256
+ let pin = { synced: false, reason: 'skipped' };
257
+ try {
258
+ pin = syncProfilePin({ pluginRoot, version: meta.version, tarball: buf, log });
259
+ if (pin.synced) log(`配置档版本钉同步到 ${meta.version}`);
260
+ } catch (err) {
261
+ log(`配置档版本钉同步失败(${err?.message || err}):下次宿主安装可能装回旧版`);
262
+ }
263
+
156
264
  log(`plugin 已更新 ${from || '?'} → ${meta.version}`);
157
- return { ok: true, from, to: meta.version, changed: true, root: pluginRoot, backup, restartRequired: true };
265
+ return {
266
+ ok: true, from, to: meta.version, changed: true, root: pluginRoot, backup,
267
+ restartRequired: true, pinSynced: pin.synced,
268
+ };
158
269
  } finally {
159
270
  try { fs.rmSync(tmpRoot, { recursive: true, force: true }); } catch { /* 临时目录清理失败不影响结果 */ }
160
271
  }