dsh-mobile-ios 0.2.5 → 0.2.6
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 +220 -13
- package/package.json +1 -1
package/lib/client.js
CHANGED
|
@@ -174,6 +174,12 @@ window.__ModuleLoader__.load({
|
|
|
174
174
|
color:var(--modal-fg); border:1px solid var(--modal-border); border-radius:14px; padding:18px;
|
|
175
175
|
box-shadow:0 18px 60px var(--modal-shadow); }
|
|
176
176
|
.dsh-mios-modal h4 { margin:0 0 12px; font-size:15px; display:flex; align-items:center; gap:8px; }
|
|
177
|
+
.dsh-mios-modal .modal-banner { margin:0 0 12px; padding:9px 11px; border-radius:9px; font-size:12px;
|
|
178
|
+
font-weight:600; line-height:1.5; background:rgba(63,178,127,.12); color:var(--modal-accent);
|
|
179
|
+
border:1px solid var(--modal-accent); }
|
|
180
|
+
.dsh-mios-modal .modal-banner.success { background:rgba(63,178,127,.16); color:#2f9d70; }
|
|
181
|
+
.dsh-mios-modal .modal-banner.warn { background:rgba(224,160,60,.14); color:#d18a26; border-color:#d18a26; }
|
|
182
|
+
.dsh-mios-modal .modal-banner.error { background:rgba(217,79,61,.14); color:#d94f3d; border-color:#d94f3d; }
|
|
177
183
|
.dsh-mios-modal .modal-x { margin-left:auto; background:none; border:none; color:var(--modal-muted);
|
|
178
184
|
font-size:16px; cursor:pointer; line-height:1; padding:2px 4px; }
|
|
179
185
|
.dsh-mios-modal .modal-line { display:flex; justify-content:space-between; gap:10px;
|
|
@@ -257,6 +263,10 @@ window.__ModuleLoader__.load({
|
|
|
257
263
|
const stripUrl = (u) => String(u || '').replace(/^https?:\/\//, '');
|
|
258
264
|
const stripLocal = (h) => String(h || '').replace(/\.local$/i, '');
|
|
259
265
|
|
|
266
|
+
// 宿主插件管理服务(remote.pluginManager)。注入模块经 apply 拿到;
|
|
267
|
+
// 非 Desktop 或老宿主拿不到时保持 null,更新入口降级为「复制包名」手动安装。
|
|
268
|
+
let pluginManager = null;
|
|
269
|
+
let updateInFlight = false; // 一步更新的并发锁:更新中禁止再次触发
|
|
260
270
|
let panelEl = null;
|
|
261
271
|
let tunnelBusy = false;
|
|
262
272
|
const bannerBusy = { lan: false, remote: false };
|
|
@@ -684,7 +694,7 @@ window.__ModuleLoader__.load({
|
|
|
684
694
|
updateModal?.remove();
|
|
685
695
|
updateModal = null;
|
|
686
696
|
}
|
|
687
|
-
function showUpdateModal({ title, lines, note, actions }) {
|
|
697
|
+
function showUpdateModal({ title, banner, lines, note, actions }) {
|
|
688
698
|
closeUpdateModal();
|
|
689
699
|
const mask = document.createElement('div');
|
|
690
700
|
mask.className = 'dsh-mios-modal-mask';
|
|
@@ -702,6 +712,14 @@ window.__ModuleLoader__.load({
|
|
|
702
712
|
h.append(x);
|
|
703
713
|
box.append(h);
|
|
704
714
|
|
|
715
|
+
// banner:一步更新流程的状态横幅(发现新版 / 更新中 / 成功 / 失败)
|
|
716
|
+
if (banner) {
|
|
717
|
+
const b = document.createElement('div');
|
|
718
|
+
b.className = `modal-banner${banner.tone ? ' ' + banner.tone : ''}`;
|
|
719
|
+
b.textContent = banner.text;
|
|
720
|
+
box.append(b);
|
|
721
|
+
}
|
|
722
|
+
|
|
705
723
|
for (const line of lines || []) {
|
|
706
724
|
const row = document.createElement('div');
|
|
707
725
|
row.className = 'modal-line';
|
|
@@ -788,23 +806,35 @@ window.__ModuleLoader__.load({
|
|
|
788
806
|
return;
|
|
789
807
|
}
|
|
790
808
|
if (compareVersions(latest, current) > 0) {
|
|
809
|
+
// 一步更新:宿主提供 pluginManager 时直接安装,无需复制包名手动添加。
|
|
810
|
+
const canAuto = !!(pluginManager && typeof pluginManager.installBundle === 'function');
|
|
811
|
+
const actions = [{ label: '稍后再说', onClick: closeUpdateModal }];
|
|
812
|
+
if (canAuto) {
|
|
813
|
+
actions.push({
|
|
814
|
+
label: `立即更新到 v${latest}`, primary: true,
|
|
815
|
+
onClick: () => performUpdate(latest),
|
|
816
|
+
});
|
|
817
|
+
} else {
|
|
818
|
+
// 降级:拿不到宿主安装服务(老宿主 / 非 Desktop),保留复制包名手动安装。
|
|
819
|
+
actions.push({
|
|
820
|
+
label: `复制 ${PLUGIN_NAME}@${latest}`, primary: true,
|
|
821
|
+
onClick: async (btn) => {
|
|
822
|
+
const ok = await copyToClipboard(`${PLUGIN_NAME}@${latest}`);
|
|
823
|
+
btn.textContent = ok ? '已复制,去插件页粘贴安装' : '复制失败,请手动复制包名';
|
|
824
|
+
},
|
|
825
|
+
});
|
|
826
|
+
}
|
|
791
827
|
showUpdateModal({
|
|
792
|
-
title: '
|
|
828
|
+
title: '新版本更新',
|
|
829
|
+
banner: { text: `⬆️ 发现新版本 v${latest},可一键更新`, tone: 'info' },
|
|
793
830
|
lines: [
|
|
794
831
|
{ label: '当前版本', value: `v${current}` },
|
|
795
832
|
{ label: '最新版本', value: `v${latest}`, highlight: true },
|
|
796
833
|
],
|
|
797
|
-
note:
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
label: `复制 ${PLUGIN_NAME}@${latest}`, primary: true,
|
|
802
|
-
onClick: async (btn) => {
|
|
803
|
-
const ok = await copyToClipboard(`${PLUGIN_NAME}@${latest}`);
|
|
804
|
-
btn.textContent = ok ? '已复制,去插件页粘贴安装' : '复制失败,请手动复制包名';
|
|
805
|
-
},
|
|
806
|
-
},
|
|
807
|
-
],
|
|
834
|
+
note: canAuto
|
|
835
|
+
? '点击「立即更新」将由 DSH 安装器一步完成,更新中请勿关闭窗口。是否更新由你决定。'
|
|
836
|
+
: '当前宿主未提供一步更新能力,请复制包名到 DSH「插件 → 添加插件」粘贴安装。',
|
|
837
|
+
actions,
|
|
808
838
|
});
|
|
809
839
|
} else {
|
|
810
840
|
showUpdateModal({
|
|
@@ -819,6 +849,164 @@ window.__ModuleLoader__.load({
|
|
|
819
849
|
}
|
|
820
850
|
}
|
|
821
851
|
|
|
852
|
+
/** 一步更新:经宿主 pluginManager.installBundle 安装 npm spec,
|
|
853
|
+
* 状态机覆盖 更新中 / 成功 / 失败 / 超时对账 / 重复点击。 */
|
|
854
|
+
let UPDATE_TIMEOUT_MS = 180000; // pnpm 安装 + 可能的重启,给足 3 分钟
|
|
855
|
+
function unwrapRemote(result) {
|
|
856
|
+
if (result && typeof result === 'object' && Object.hasOwn(result, 'ok')) {
|
|
857
|
+
if (!result.ok) throw new Error(result.error?.code || result.error?.message || '宿主返回失败');
|
|
858
|
+
return result.value;
|
|
859
|
+
}
|
|
860
|
+
return result;
|
|
861
|
+
}
|
|
862
|
+
function withTimeout(promise, ms, label) {
|
|
863
|
+
return new Promise((resolve, reject) => {
|
|
864
|
+
let settled = false;
|
|
865
|
+
const timer = setTimeout(() => {
|
|
866
|
+
if (settled) return;
|
|
867
|
+
settled = true;
|
|
868
|
+
reject(new Error(`${label}超时(${Math.round(ms / 1000)} 秒)`));
|
|
869
|
+
}, ms);
|
|
870
|
+
Promise.resolve(promise).then(
|
|
871
|
+
(v) => { if (!settled) { settled = true; clearTimeout(timer); resolve(v); } },
|
|
872
|
+
(e) => { if (!settled) { settled = true; clearTimeout(timer); reject(e); } },
|
|
873
|
+
);
|
|
874
|
+
});
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
// ── 跨 HMR 重载的更新状态持久化 ─────────────────────────────
|
|
878
|
+
// 更新「自己」会触发宿主卸载本模块并加载新版,当前 JS 上下文连同弹窗一起销毁。
|
|
879
|
+
// 因此发起更新前把状态写入 localStorage,新模块 apply 时读取并给出结果反馈,
|
|
880
|
+
// 避免出现「点一下弹窗消失、什么都没发生」的体验。
|
|
881
|
+
const UPDATE_STATE_KEY = 'dsh-mios-update-state';
|
|
882
|
+
function readUpdateState() {
|
|
883
|
+
try {
|
|
884
|
+
const raw = globalThis.localStorage?.getItem(UPDATE_STATE_KEY);
|
|
885
|
+
return raw ? JSON.parse(raw) : null;
|
|
886
|
+
} catch { return null; }
|
|
887
|
+
}
|
|
888
|
+
function writeUpdateState(state) {
|
|
889
|
+
try { globalThis.localStorage?.setItem(UPDATE_STATE_KEY, JSON.stringify(state)); }
|
|
890
|
+
catch { /* 存储不可用时静默,功能降级但不崩 */ }
|
|
891
|
+
}
|
|
892
|
+
function clearUpdateState() {
|
|
893
|
+
try { globalThis.localStorage?.removeItem(UPDATE_STATE_KEY); }
|
|
894
|
+
catch { /* 同上 */ }
|
|
895
|
+
}
|
|
896
|
+
/** apply 时调用:若上次更新尚未收尾,依据当前版本推断结果并弹窗告知。 */
|
|
897
|
+
function restorePendingUpdate() {
|
|
898
|
+
const st = readUpdateState();
|
|
899
|
+
if (!st || st.status !== 'updating' || !st.target) return;
|
|
900
|
+
const current = String(summary?.version || '').trim();
|
|
901
|
+
const age = Date.now() - (st.at || 0);
|
|
902
|
+
if (current && compareVersions(current, st.target) >= 0) {
|
|
903
|
+
clearUpdateState();
|
|
904
|
+
// 延迟到面板初次渲染后再弹,避免被 apply 后续的初始化覆盖
|
|
905
|
+
setTimeout(() => showUpdateModal({
|
|
906
|
+
title: '更新完成',
|
|
907
|
+
banner: { text: `✅ 已更新到 v${st.target}`, tone: 'success' },
|
|
908
|
+
lines: [{ label: '当前版本', value: `v${current}` }],
|
|
909
|
+
note: '插件已更新并重新加载完成。',
|
|
910
|
+
actions: [{ label: '知道了', primary: true, onClick: closeUpdateModal }],
|
|
911
|
+
}), 600);
|
|
912
|
+
} else if (age > 5 * 60 * 1000) {
|
|
913
|
+
clearUpdateState();
|
|
914
|
+
setTimeout(() => showUpdateModal({
|
|
915
|
+
title: '更新结果未知',
|
|
916
|
+
banner: { text: '⚠️ 上一步更新未能确认', tone: 'warn' },
|
|
917
|
+
lines: [{ label: '目标版本', value: `v${st.target}` }, { label: '当前版本', value: current ? `v${current}` : '未知' }],
|
|
918
|
+
note: '更新过程被中断或耗时过长。请到「插件」页确认当前版本,必要时手动更新。',
|
|
919
|
+
actions: [{ label: '知道了', primary: true, onClick: closeUpdateModal }],
|
|
920
|
+
}), 600);
|
|
921
|
+
}
|
|
922
|
+
// 未超时且版本未变:保持 updating 状态,等下次 apply(轮询重载)再判断
|
|
923
|
+
}
|
|
924
|
+
async function performUpdate(latest) {
|
|
925
|
+
// 重复点击保护:更新中直接忽略再次触发
|
|
926
|
+
if (updateInFlight) return;
|
|
927
|
+
if (!pluginManager || typeof pluginManager.installBundle !== 'function') {
|
|
928
|
+
showUpdateModal({
|
|
929
|
+
title: '更新不可用',
|
|
930
|
+
banner: { text: '⚠️ 宿主未提供一步更新能力', tone: 'warn' },
|
|
931
|
+
lines: [],
|
|
932
|
+
note: '请复制包名,到 DSH「插件 → 添加插件」粘贴安装。',
|
|
933
|
+
actions: [
|
|
934
|
+
{ label: '知道了', onClick: closeUpdateModal },
|
|
935
|
+
{ label: `复制 ${PLUGIN_NAME}@${latest}`, primary: true, onClick: async (btn) => {
|
|
936
|
+
const ok = await copyToClipboard(`${PLUGIN_NAME}@${latest}`);
|
|
937
|
+
btn.textContent = ok ? '已复制' : '复制失败';
|
|
938
|
+
} },
|
|
939
|
+
],
|
|
940
|
+
});
|
|
941
|
+
return;
|
|
942
|
+
}
|
|
943
|
+
updateInFlight = true;
|
|
944
|
+
const current = summary?.version || '未知';
|
|
945
|
+
const spec = `${PLUGIN_NAME}@${latest}`;
|
|
946
|
+
// 先持久化「更新中」,确保 HMR 卸载本模块后新实例能恢复出结果反馈。
|
|
947
|
+
writeUpdateState({ status: 'updating', target: String(latest), at: Date.now() });
|
|
948
|
+
// 状态:更新中。无取消/关闭动作外的操作按钮,防重复点击。
|
|
949
|
+
showUpdateModal({
|
|
950
|
+
title: '正在更新',
|
|
951
|
+
banner: { text: `⏳ 正在更新到 v${latest},请稍候`, tone: 'info' },
|
|
952
|
+
lines: [
|
|
953
|
+
{ label: '当前版本', value: `v${current}` },
|
|
954
|
+
{ label: '目标版本', value: `v${latest}`, highlight: true },
|
|
955
|
+
],
|
|
956
|
+
note: '正在通过 DSH 安装器更新,可能需要 1–3 分钟。请勿重复点击或关闭窗口。',
|
|
957
|
+
actions: [],
|
|
958
|
+
});
|
|
959
|
+
let requestId;
|
|
960
|
+
try {
|
|
961
|
+
requestId = globalThis.crypto?.randomUUID?.();
|
|
962
|
+
if (typeof requestId !== 'string') throw new Error('无法生成更新请求 ID');
|
|
963
|
+
let result;
|
|
964
|
+
try {
|
|
965
|
+
result = await withTimeout(
|
|
966
|
+
pluginManager.installBundle(spec, { enabled: true, requestId }),
|
|
967
|
+
UPDATE_TIMEOUT_MS, '安装',
|
|
968
|
+
);
|
|
969
|
+
} catch (err) {
|
|
970
|
+
// 超时/结果未知:用 requestId 对账,绝不重复提交安装
|
|
971
|
+
if (typeof pluginManager.waitForInstall === 'function') {
|
|
972
|
+
const recovered = await withTimeout(pluginManager.waitForInstall(requestId), 60000, '结果对账');
|
|
973
|
+
if (!recovered) throw err;
|
|
974
|
+
result = recovered;
|
|
975
|
+
} else throw err;
|
|
976
|
+
}
|
|
977
|
+
const value = unwrapRemote(result);
|
|
978
|
+
const restart = !!(value && (value.restartRequired || value.restart));
|
|
979
|
+
clearUpdateState(); // 安装器已返回结果,交由本弹窗反馈,无需跨重载恢复
|
|
980
|
+
// 状态:成功
|
|
981
|
+
showUpdateModal({
|
|
982
|
+
title: '更新完成',
|
|
983
|
+
banner: { text: `✅ 已安装 v${latest}`, tone: 'success' },
|
|
984
|
+
lines: [
|
|
985
|
+
{ label: '目标版本', value: `v${latest}` },
|
|
986
|
+
],
|
|
987
|
+
note: restart
|
|
988
|
+
? '插件已更新,重启 DSH 桌面端后生效。'
|
|
989
|
+
: '插件已更新。若面板未刷新,重开面板或重启 DSH 即可。',
|
|
990
|
+
actions: [{ label: '知道了', primary: true, onClick: closeUpdateModal }],
|
|
991
|
+
});
|
|
992
|
+
} catch (err) {
|
|
993
|
+
// 状态:失败(保留重试入口;重试会重新走一遍锁与状态机)
|
|
994
|
+
clearUpdateState();
|
|
995
|
+
showUpdateModal({
|
|
996
|
+
title: '更新失败',
|
|
997
|
+
banner: { text: '❌ 更新未成功', tone: 'error' },
|
|
998
|
+
lines: [{ label: '目标版本', value: `v${latest}` }],
|
|
999
|
+
note: `安装失败:${err?.message || String(err)}。可重试,或复制包名到「插件 → 添加插件」手动安装。`,
|
|
1000
|
+
actions: [
|
|
1001
|
+
{ label: '关闭', onClick: closeUpdateModal },
|
|
1002
|
+
{ label: '重试更新', primary: true, onClick: () => performUpdate(latest) },
|
|
1003
|
+
],
|
|
1004
|
+
});
|
|
1005
|
+
} finally {
|
|
1006
|
+
updateInFlight = false;
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
|
|
822
1010
|
function compareVersions(a, b) {
|
|
823
1011
|
const parse = (s) => String(s || '').replace(/^v/i, '').split('-')[0].split('.').map((n) => parseInt(n, 10) || 0);
|
|
824
1012
|
const [pa, pb] = [parse(a), parse(b)];
|
|
@@ -935,6 +1123,18 @@ window.__ModuleLoader__.load({
|
|
|
935
1123
|
function apply(ctx) {
|
|
936
1124
|
installStyle();
|
|
937
1125
|
|
|
1126
|
+
// 注入宿主插件管理器:一步更新能力来源。可选能力——用安全读取而非硬依赖,
|
|
1127
|
+
// 拿不到时 pluginManager 保持 null,更新弹窗自动降级为「复制包名」手动安装。
|
|
1128
|
+
try {
|
|
1129
|
+
const remote = (typeof ctx.get === 'function' && ctx.get('remote'))
|
|
1130
|
+
|| ctx.remote || null;
|
|
1131
|
+
pluginManager = remote?.pluginManager || null;
|
|
1132
|
+
} catch { pluginManager = null; }
|
|
1133
|
+
|
|
1134
|
+
// 跨 HMR 重载恢复:一步更新触发插件自更新会卸载本模块,弹窗随之消失。
|
|
1135
|
+
// 发起前把状态写入 localStorage,重载后据此显示「更新完成/进行中」反馈。
|
|
1136
|
+
try { restorePendingUpdate(); } catch { /* 存储不可用时跳过,不影响主流程 */ }
|
|
1137
|
+
|
|
938
1138
|
// 轮询保持 summary 新鲜(banner 状态点 + 面板数据共用)
|
|
939
1139
|
probeUntilUp();
|
|
940
1140
|
const pollTimer = setInterval(probe, 15000);
|
|
@@ -973,12 +1173,19 @@ window.__ModuleLoader__.load({
|
|
|
973
1173
|
};
|
|
974
1174
|
}
|
|
975
1175
|
|
|
1176
|
+
// 注意:宿主 inject 数组是「硬依赖」,缺失会让整个模块挂起(pending)。
|
|
1177
|
+
// remote/pluginManager 属可选能力,只声明 slots;安装服务在 apply 内安全获取。
|
|
976
1178
|
const inject = ['slots'];
|
|
977
1179
|
exports.apply = apply;
|
|
978
1180
|
exports.inject = inject;
|
|
979
1181
|
exports.__test = {
|
|
980
1182
|
PORT_CANDIDATES, STATUS_OK, STATUS_DOWN,
|
|
981
1183
|
showUpdateModal, closeUpdateModal, copyToClipboard,
|
|
1184
|
+
performUpdate, checkPluginUpdate, compareVersions, unwrapRemote,
|
|
1185
|
+
setPluginManager: (m) => { pluginManager = m; },
|
|
1186
|
+
setUpdateTimeout: (ms) => { UPDATE_TIMEOUT_MS = ms; },
|
|
1187
|
+
isUpdateInFlight: () => updateInFlight,
|
|
1188
|
+
restorePendingUpdate, readUpdateState, writeUpdateState, clearUpdateState,
|
|
982
1189
|
};
|
|
983
1190
|
|
|
984
1191
|
return module.exports;
|