dsh-mobile-ios 0.2.4 → 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/assets/brand/plugin-icon.png +0 -0
- package/lib/client.js +230 -16
- package/package.json +1 -1
|
Binary file
|
package/lib/client.js
CHANGED
|
@@ -160,11 +160,12 @@ window.__ModuleLoader__.load({
|
|
|
160
160
|
padding: 14px; }
|
|
161
161
|
.dsh-mios-panel h3 { margin: 0 0 10px; font-size: 14px; display: flex; align-items: center; gap: 8px; }
|
|
162
162
|
.dsh-mios-upd { margin-left: auto; background: none; border: 1px solid var(--border); border-radius: 7px;
|
|
163
|
-
color: var(--muted); font:
|
|
163
|
+
color: var(--muted); font-family: inherit; font-size: 11px; font-weight: 500; line-height: 1.4;
|
|
164
|
+
cursor: pointer; padding: 3px 8px; }
|
|
164
165
|
.dsh-mios-upd:hover { color: var(--fg); border-color: var(--border-hover); }
|
|
165
166
|
.dsh-mios-modal-mask { --modal-bg:#1c2230; --modal-fg:#e8eaf0; --modal-muted:#8b93a3;
|
|
166
167
|
--modal-border:#3a4560; --modal-btn:rgba(139,147,163,.12); --modal-accent:#3fb27f; --modal-shadow:rgba(0,0,0,.6);
|
|
167
|
-
position:fixed; inset:0; z-index:
|
|
168
|
+
position:fixed; inset:0; z-index:2147483000; background:var(--modal-shadow);
|
|
168
169
|
display:flex; align-items:center; justify-content:center; padding:20px;
|
|
169
170
|
font-family:-apple-system, "PingFang SC", sans-serif; font-size:13px; line-height:1.6; }
|
|
170
171
|
.dsh-mios-modal-mask.light { --modal-bg:#ffffff; --modal-fg:#1f2430; --modal-muted:#6b7280;
|
|
@@ -173,6 +174,12 @@ window.__ModuleLoader__.load({
|
|
|
173
174
|
color:var(--modal-fg); border:1px solid var(--modal-border); border-radius:14px; padding:18px;
|
|
174
175
|
box-shadow:0 18px 60px var(--modal-shadow); }
|
|
175
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; }
|
|
176
183
|
.dsh-mios-modal .modal-x { margin-left:auto; background:none; border:none; color:var(--modal-muted);
|
|
177
184
|
font-size:16px; cursor:pointer; line-height:1; padding:2px 4px; }
|
|
178
185
|
.dsh-mios-modal .modal-line { display:flex; justify-content:space-between; gap:10px;
|
|
@@ -256,6 +263,10 @@ window.__ModuleLoader__.load({
|
|
|
256
263
|
const stripUrl = (u) => String(u || '').replace(/^https?:\/\//, '');
|
|
257
264
|
const stripLocal = (h) => String(h || '').replace(/\.local$/i, '');
|
|
258
265
|
|
|
266
|
+
// 宿主插件管理服务(remote.pluginManager)。注入模块经 apply 拿到;
|
|
267
|
+
// 非 Desktop 或老宿主拿不到时保持 null,更新入口降级为「复制包名」手动安装。
|
|
268
|
+
let pluginManager = null;
|
|
269
|
+
let updateInFlight = false; // 一步更新的并发锁:更新中禁止再次触发
|
|
259
270
|
let panelEl = null;
|
|
260
271
|
let tunnelBusy = false;
|
|
261
272
|
const bannerBusy = { lan: false, remote: false };
|
|
@@ -587,7 +598,13 @@ window.__ModuleLoader__.load({
|
|
|
587
598
|
const updBtn = document.createElement('button');
|
|
588
599
|
updBtn.className = 'dsh-mios-upd';
|
|
589
600
|
updBtn.textContent = '插件更新';
|
|
590
|
-
updBtn.addEventListener('click', () =>
|
|
601
|
+
updBtn.addEventListener('click', () => {
|
|
602
|
+
// 点击务必先有可见反馈再联网,避免「无反应」错觉(缓存旧模块时旧逻辑写已移除的内联元素)。
|
|
603
|
+
try { checkPluginUpdate(); } catch (e) {
|
|
604
|
+
console.error('[dsh-mobile-ios] checkPluginUpdate failed:', e);
|
|
605
|
+
showUpdateModal({ title: '插件更新', lines: [], note: `打开更新检查失败:${e?.message || e}`, actions: [{ label: '知道了', primary: true, onClick: closeUpdateModal }] });
|
|
606
|
+
}
|
|
607
|
+
});
|
|
591
608
|
const close = document.createElement('button');
|
|
592
609
|
close.className = 'dsh-mios-close';
|
|
593
610
|
close.textContent = '✕';
|
|
@@ -677,7 +694,7 @@ window.__ModuleLoader__.load({
|
|
|
677
694
|
updateModal?.remove();
|
|
678
695
|
updateModal = null;
|
|
679
696
|
}
|
|
680
|
-
function showUpdateModal({ title, lines, note, actions }) {
|
|
697
|
+
function showUpdateModal({ title, banner, lines, note, actions }) {
|
|
681
698
|
closeUpdateModal();
|
|
682
699
|
const mask = document.createElement('div');
|
|
683
700
|
mask.className = 'dsh-mios-modal-mask';
|
|
@@ -695,6 +712,14 @@ window.__ModuleLoader__.load({
|
|
|
695
712
|
h.append(x);
|
|
696
713
|
box.append(h);
|
|
697
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
|
+
|
|
698
723
|
for (const line of lines || []) {
|
|
699
724
|
const row = document.createElement('div');
|
|
700
725
|
row.className = 'modal-line';
|
|
@@ -781,23 +806,35 @@ window.__ModuleLoader__.load({
|
|
|
781
806
|
return;
|
|
782
807
|
}
|
|
783
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
|
+
}
|
|
784
827
|
showUpdateModal({
|
|
785
|
-
title: '
|
|
828
|
+
title: '新版本更新',
|
|
829
|
+
banner: { text: `⬆️ 发现新版本 v${latest},可一键更新`, tone: 'info' },
|
|
786
830
|
lines: [
|
|
787
831
|
{ label: '当前版本', value: `v${current}` },
|
|
788
832
|
{ label: '最新版本', value: `v${latest}`, highlight: true },
|
|
789
833
|
],
|
|
790
|
-
note:
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
label: `复制 ${PLUGIN_NAME}@${latest}`, primary: true,
|
|
795
|
-
onClick: async (btn) => {
|
|
796
|
-
const ok = await copyToClipboard(`${PLUGIN_NAME}@${latest}`);
|
|
797
|
-
btn.textContent = ok ? '已复制,去插件页粘贴安装' : '复制失败,请手动复制包名';
|
|
798
|
-
},
|
|
799
|
-
},
|
|
800
|
-
],
|
|
834
|
+
note: canAuto
|
|
835
|
+
? '点击「立即更新」将由 DSH 安装器一步完成,更新中请勿关闭窗口。是否更新由你决定。'
|
|
836
|
+
: '当前宿主未提供一步更新能力,请复制包名到 DSH「插件 → 添加插件」粘贴安装。',
|
|
837
|
+
actions,
|
|
801
838
|
});
|
|
802
839
|
} else {
|
|
803
840
|
showUpdateModal({
|
|
@@ -812,6 +849,164 @@ window.__ModuleLoader__.load({
|
|
|
812
849
|
}
|
|
813
850
|
}
|
|
814
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
|
+
|
|
815
1010
|
function compareVersions(a, b) {
|
|
816
1011
|
const parse = (s) => String(s || '').replace(/^v/i, '').split('-')[0].split('.').map((n) => parseInt(n, 10) || 0);
|
|
817
1012
|
const [pa, pb] = [parse(a), parse(b)];
|
|
@@ -928,6 +1123,18 @@ window.__ModuleLoader__.load({
|
|
|
928
1123
|
function apply(ctx) {
|
|
929
1124
|
installStyle();
|
|
930
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
|
+
|
|
931
1138
|
// 轮询保持 summary 新鲜(banner 状态点 + 面板数据共用)
|
|
932
1139
|
probeUntilUp();
|
|
933
1140
|
const pollTimer = setInterval(probe, 15000);
|
|
@@ -966,12 +1173,19 @@ window.__ModuleLoader__.load({
|
|
|
966
1173
|
};
|
|
967
1174
|
}
|
|
968
1175
|
|
|
1176
|
+
// 注意:宿主 inject 数组是「硬依赖」,缺失会让整个模块挂起(pending)。
|
|
1177
|
+
// remote/pluginManager 属可选能力,只声明 slots;安装服务在 apply 内安全获取。
|
|
969
1178
|
const inject = ['slots'];
|
|
970
1179
|
exports.apply = apply;
|
|
971
1180
|
exports.inject = inject;
|
|
972
1181
|
exports.__test = {
|
|
973
1182
|
PORT_CANDIDATES, STATUS_OK, STATUS_DOWN,
|
|
974
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,
|
|
975
1189
|
};
|
|
976
1190
|
|
|
977
1191
|
return module.exports;
|