dsh-xdpool 2.7.4 → 2.7.5

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/CHANGELOG.md CHANGED
@@ -1,6 +1,44 @@
1
1
  # Changelog
2
2
  本项目的所有重要变更都会记录在此文件。
3
3
  版本号遵循 [语义化版本](https://semver.org/lang/zh-CN/)。
4
+ ## 2.7.5 — 一键安装更新
5
+
6
+ > 2.7.4 加了「检查更新」,但查到了只会告诉你「去插件设置里更新」。现在有安装按钮。
7
+
8
+ ### 点一下就能装
9
+
10
+ 「更新与备份」页在检查到新版本时多一个按钮:
11
+
12
+ ```
13
+ 有新版本 2.7.6
14
+ [ 安装 2.7.6 ] 下载到本机并写入插件配置,重启 DSH 后生效
15
+ ```
16
+
17
+ 点下去:从 npm 下载 tarball → 校验 gzip 文件头 → 写入 profile 的 `package.json` →
18
+ 提示「已下载,重启后生效」。
19
+
20
+ **为什么需要重启**:已加载的模块没法原地替换。不这么说就是骗人,用户会在下次崩溃时才发现。
21
+
22
+ ### 宿主没有安装 API,所以改的是配置
23
+
24
+ 插件无法「调用一个安装接口」。宿主在启动时解析 profile `package.json` 里的依赖描述,
25
+ 所以更新 = 改描述 + 重启。这就是所有自更新插件实际在做的事。
26
+
27
+ 写成 `file:` 而不是版本号:指定版本号会让宿主每次启动都去 registry 拉取,在代理或慢网络下
28
+ 的失败形态是「插件加载不出来」。钉住刚刚验证过能下载的 tarball,更新和它替代的安装一样本地。
29
+
30
+ ### 两个安全设计
31
+
32
+ **版本号不由前端传。** `POST /update/install` 忽略 body,版本由路由自己问 registry ——
33
+ 否则这个会写 `package.json` 的接口就能被诱导装任意版本。
34
+
35
+ **重复点击不重复写。** 配置已指向同一文件时直接返回成功,不重写。否则第二次点击会让宿主
36
+ 再跑一遍安装,中间要是重启,用户会看到一个没完成的更新。
37
+
38
+ ### 检查到最新
39
+
40
+ 显示「已是最新版本(N 分钟前检查)」—— 这条 2.7.4 就有,保留。
41
+
4
42
  ## 2.7.4 — 远程可用、只列已绑定渠道、检查更新
5
43
 
6
44
  ### 远程访问时所有按钮 403(#164)
package/lib/client.js CHANGED
@@ -114,6 +114,8 @@ window.__ModuleLoader__.load({
114
114
  const POOL_AUTOMATION_RUN_PATH = "/plugins/dsh-xdpool/automation/run";
115
115
  /** Is there a newer build of this plugin? */
116
116
  const POOL_UPDATE_CHECK_PATH = "/plugins/dsh-xdpool/update";
117
+ /** Install the newest build. The host applies it on the next start. */
118
+ const POOL_UPDATE_INSTALL_PATH = "/plugins/dsh-xdpool/update/install";
117
119
  /** Set or clear one account's reserved-credit floor. */
118
120
  const POOL_CREDIT_RESERVE_PATH = "/plugins/dsh-xdpool/accounts/credit-reserve";
119
121
  /**
@@ -1949,6 +1951,8 @@ window.__ModuleLoader__.load({
1949
1951
  .dsm-xdpool-update-head .dsm-xdpool-btn{margin-left:auto}
1950
1952
  .dsm-xdpool-update-current{margin:0;font-size:11.5px;line-height:17px;color:var(--dsw-alias-state-success-primary,#22a06b)}
1951
1953
  .dsm-xdpool-update-newer{margin:0;font-size:11.5px;line-height:17px;color:var(--dsw-alias-state-warning-primary,#d99a2b)}
1954
+ .dsm-xdpool-update-acts{display:flex;align-items:center;gap:10px;flex-wrap:wrap}
1955
+ .dsm-xdpool-update-hint{font-size:10.5px;line-height:15px;color:var(--dsw-alias-label-tertiary,#8a90a0)}
1952
1956
  .dsm-xdpool-update-error{margin:0;font-size:11.5px;line-height:17px;color:var(--dsw-alias-state-error-primary,#e5534b)}
1953
1957
  `;
1954
1958
  //#endregion
@@ -14136,6 +14140,9 @@ window.__ModuleLoader__.load({
14136
14140
  function UpdatePanel({ t }) {
14137
14141
  const [status, setStatus] = (0, react.useState)(void 0);
14138
14142
  const [busy, setBusy] = (0, react.useState)(false);
14143
+ const [installing, setInstalling] = (0, react.useState)(false);
14144
+ /** What the install attempt said, kept apart from the check result. */
14145
+ const [installed, setInstalled] = (0, react.useState)(void 0);
14139
14146
  const mounted = (0, react.useRef)(true);
14140
14147
  (0, react.useEffect)(() => {
14141
14148
  mounted.current = true;
@@ -14162,6 +14169,48 @@ window.__ModuleLoader__.load({
14162
14169
  if (mounted.current) setBusy(false);
14163
14170
  }
14164
14171
  }, []);
14172
+ /**
14173
+ * Install the newest build.
14174
+ *
14175
+ * The version is not sent: the route asks the registry itself, so the panel cannot be talked
14176
+ * into installing something it did not just see.
14177
+ */
14178
+ const install = (0, react.useCallback)(async () => {
14179
+ setInstalling(true);
14180
+ setInstalled(void 0);
14181
+ try {
14182
+ const body = await (await fetch(POOL_UPDATE_INSTALL_PATH, {
14183
+ method: "POST",
14184
+ credentials: "same-origin"
14185
+ })).json().catch(() => void 0);
14186
+ if (!mounted.current) return;
14187
+ if (body?.ok === true && body.restartRequired === true) {
14188
+ setInstalled({
14189
+ ok: true,
14190
+ text: t?.("update.installed", { version: body.version ?? "" }) ?? `已下载 ${body.version ?? ""},重启 DSH 后生效`
14191
+ });
14192
+ return;
14193
+ }
14194
+ if (body?.ok === true) {
14195
+ setInstalled({
14196
+ ok: true,
14197
+ text: t?.("update.upToDate") ?? "已是最新版本"
14198
+ });
14199
+ return;
14200
+ }
14201
+ setInstalled({
14202
+ ok: false,
14203
+ text: body?.error ?? t?.("update.installFailed") ?? "安装失败"
14204
+ });
14205
+ } catch (cause) {
14206
+ if (mounted.current) setInstalled({
14207
+ ok: false,
14208
+ text: cause instanceof Error ? cause.message : String(cause)
14209
+ });
14210
+ } finally {
14211
+ if (mounted.current) setInstalling(false);
14212
+ }
14213
+ }, [t]);
14165
14214
  (0, react.useEffect)(() => {
14166
14215
  check(false);
14167
14216
  }, [check]);
@@ -14215,17 +14264,31 @@ window.__ModuleLoader__.load({
14215
14264
  status.error
14216
14265
  ]
14217
14266
  }),
14218
- status?.error !== void 0 || status === void 0 ? null : newer ? /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("p", {
14267
+ status?.error !== void 0 || status === void 0 ? null : newer ? /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("p", {
14219
14268
  className: "dsm-xdpool-update-newer",
14220
- children: [
14221
- t?.("update.available", { version: status.latest ?? "" }) ?? `有新版本 ${status.latest ?? ""}`,
14222
- published === void 0 ? "" : `(发布于 ${published})`,
14223
- ". ",
14224
- t?.("update.howTo") ?? "在 DSH 的插件设置里把它更新到该版本即可;数据与账号不受影响。"
14225
- ]
14226
- }) : /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("p", {
14269
+ children: [t?.("update.available", { version: status.latest ?? "" }) ?? `有新版本 ${status.latest ?? ""}`, published === void 0 ? "" : `(发布于 ${published})`]
14270
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
14271
+ className: "dsm-xdpool-update-acts",
14272
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
14273
+ type: "button",
14274
+ className: "dsm-xdpool-btn",
14275
+ "data-tone": "primary",
14276
+ disabled: installing,
14277
+ onClick: () => {
14278
+ install();
14279
+ },
14280
+ children: installing ? t?.("update.installing") ?? "安装中…" : t?.("update.install", { version: status.latest ?? "" }) ?? `安装 ${status.latest ?? ""}`
14281
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
14282
+ className: "dsm-xdpool-update-hint",
14283
+ children: t?.("update.installHint") ?? "下载到本机并写入插件配置,重启 DSH 后生效;数据和账号不受影响。"
14284
+ })]
14285
+ })] }) : /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("p", {
14227
14286
  className: "dsm-xdpool-update-current",
14228
14287
  children: [t?.("update.upToDate") ?? "已是最新版本", checked === void 0 ? "" : `(${checked}检查)`]
14288
+ }),
14289
+ installed === void 0 ? null : /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
14290
+ className: installed.ok ? "dsm-xdpool-update-current" : "dsm-xdpool-update-error",
14291
+ children: installed.text
14229
14292
  })
14230
14293
  ]
14231
14294
  });
@@ -14658,7 +14721,7 @@ window.__ModuleLoader__.load({
14658
14721
  *
14659
14722
  * @module dsh-xdpool/version
14660
14723
  */
14661
- const POOL_VERSION = "2.7.4";
14724
+ const POOL_VERSION = "2.7.5";
14662
14725
  //#endregion
14663
14726
  //#region src/client/PoolCard.tsx
14664
14727
  /**
@@ -17772,6 +17835,11 @@ window.__ModuleLoader__.load({
17772
17835
  * @module dsh-xdpool/client/locales
17773
17836
  */
17774
17837
  const en = {
17838
+ "update.install": "Install {version}",
17839
+ "update.installing": "Installing…",
17840
+ "update.installHint": "Downloads to this machine and updates the plugin config; takes effect after a DSH restart. Your data and accounts are unaffected.",
17841
+ "update.installed": "Downloaded {version} — restart DSH to apply",
17842
+ "update.installFailed": "Install failed",
17775
17843
  "update.title": "Check for updates",
17776
17844
  "update.current": "current",
17777
17845
  "update.latest": "latest",
@@ -18399,6 +18467,11 @@ window.__ModuleLoader__.load({
18399
18467
  "channelpool.note": "Note"
18400
18468
  };
18401
18469
  const zh = {
18470
+ "update.install": "安装 {version}",
18471
+ "update.installing": "安装中…",
18472
+ "update.installHint": "下载到本机并写入插件配置,重启 DSH 后生效;数据和账号不受影响。",
18473
+ "update.installed": "已下载 {version},重启 DSH 后生效",
18474
+ "update.installFailed": "安装失败",
18402
18475
  "update.title": "检查更新",
18403
18476
  "update.current": "当前",
18404
18477
  "update.latest": "最新",
package/lib/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { createRequire } from "node:module";
2
2
  import { execFile, execFileSync, spawn, spawnSync } from "node:child_process";
3
- import { appendFileSync, cpSync, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, statSync, truncateSync, writeFileSync } from "node:fs";
3
+ import { appendFileSync, cpSync, createWriteStream, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, truncateSync, writeFileSync } from "node:fs";
4
4
  import { homedir, hostname, tmpdir } from "node:os";
5
5
  import { basename, dirname, join, resolve } from "node:path";
6
6
  import crypto$1, { createDecipheriv, createHash, randomBytes, randomUUID, timingSafeEqual } from "node:crypto";
@@ -2583,6 +2583,8 @@ const POOL_ACCOUNT_IGNORE_PATH = "/plugins/dsh-xdpool/accounts/ignored";
2583
2583
  const POOL_AUTOMATION_RUN_PATH = "/plugins/dsh-xdpool/automation/run";
2584
2584
  /** Is there a newer build of this plugin? */
2585
2585
  const POOL_UPDATE_CHECK_PATH = "/plugins/dsh-xdpool/update";
2586
+ /** Install the newest build. The host applies it on the next start. */
2587
+ const POOL_UPDATE_INSTALL_PATH = "/plugins/dsh-xdpool/update/install";
2586
2588
  /** Set or clear one account's reserved-credit floor. */
2587
2589
  const POOL_CREDIT_RESERVE_PATH = "/plugins/dsh-xdpool/accounts/credit-reserve";
2588
2590
  /**
@@ -19732,7 +19734,7 @@ function parseBody(body) {
19732
19734
  *
19733
19735
  * @module dsh-xdpool/version
19734
19736
  */
19735
- const POOL_VERSION = "2.7.4";
19737
+ const POOL_VERSION = "2.7.5";
19736
19738
  //#endregion
19737
19739
  //#region src/update-check.ts
19738
19740
  /**
@@ -19761,9 +19763,9 @@ const POOL_VERSION = "2.7.4";
19761
19763
  * @module dsh-xdpool/update-check
19762
19764
  */
19763
19765
  /** The package name this plugin publishes under. */
19764
- const PACKAGE = "dsh-xdpool";
19766
+ const PACKAGE$1 = "dsh-xdpool";
19765
19767
  /** The registries tried, in order. npm first: it is the canonical one. */
19766
- const REGISTRIES = [`https://registry.npmjs.org/${PACKAGE}/latest`, `https://registry.npmjs.org/${PACKAGE}`];
19768
+ const REGISTRIES = [`https://registry.npmjs.org/${PACKAGE$1}/latest`, `https://registry.npmjs.org/${PACKAGE$1}`];
19767
19769
  /** How long an answer is reused. */
19768
19770
  const TTL_MS = 6e5;
19769
19771
  let cached;
@@ -21656,6 +21658,164 @@ const PROVIDER_NOTES_ZH = Object.freeze({
21656
21658
  "zenmux": "max_tokens 实际按 max_completion_tokens 发送"
21657
21659
  });
21658
21660
  //#endregion
21661
+ //#region src/update-install.ts
21662
+ /**
21663
+ * Installing a newer build of this plugin.
21664
+ *
21665
+ * HOW THE HOST UPDATES A PLUGIN
21666
+ *
21667
+ * There is no install API to call. A profile's `package.json` names each plugin by a spec, and
21668
+ * the host resolves that spec when it starts — so "update" means "change the spec, then restart".
21669
+ * That is the whole mechanism, and it is what every plugin that self-updates is really doing.
21670
+ *
21671
+ * WHAT THIS DOES
21672
+ *
21673
+ * A newer version is downloaded from the npm registry to a local file, and the profile's
21674
+ * dependency is pointed at it with a `file:` spec. On the next start the host installs from that
21675
+ * file. Nothing is written outside the profile's own directory, and nothing is changed if the
21676
+ * download fails — a half-applied update is worse than an unapplied one.
21677
+ *
21678
+ * WHY `file:` AND NOT THE PUBLISHED VERSION
21679
+ *
21680
+ * Naming the version (`"dsh-xdpool": "2.7.5"`) makes the host fetch from the registry on every
21681
+ * start, which fails on a machine whose registry is proxied or slow — and the failure mode is a
21682
+ * plugin that does not load. Pinning the tarball that was just verified to download keeps the
21683
+ * update as local as the install it replaces.
21684
+ *
21685
+ * A RESTART IS REQUIRED, AND SAYS SO
21686
+ *
21687
+ * Modules already loaded cannot be swapped in place. The panel reports `restartRequired` and the
21688
+ * user restarts when convenient — claiming the update took effect immediately would be a lie the
21689
+ * user discovers on the next crash.
21690
+ *
21691
+ * @module dsh-xdpool/update-install
21692
+ */
21693
+ /** The package this plugin publishes under. */
21694
+ const PACKAGE = "dsh-xdpool";
21695
+ /** The profile directory whose `package.json` names this plugin. */
21696
+ function profileDir() {
21697
+ const candidates = [
21698
+ process.env["DSH_PROFILE_DIR"],
21699
+ process.env["DSH_HOME"] === void 0 ? void 0 : join(process.env["DSH_HOME"], "profiles", "desktop"),
21700
+ join(homedir(), ".dsh", "profiles", "desktop")
21701
+ ];
21702
+ for (const candidate of candidates) {
21703
+ if (candidate === void 0) continue;
21704
+ if (existsSync(join(candidate, "package.json"))) return candidate;
21705
+ }
21706
+ }
21707
+ /**
21708
+ * Download a version and point the profile at it.
21709
+ *
21710
+ * The order matters: download first, verify it is a gzip tarball, and only then touch
21711
+ * `package.json`. A failed download must leave a working install alone.
21712
+ */
21713
+ async function installVersion(version) {
21714
+ const profile = profileDir();
21715
+ if (profile === void 0) return {
21716
+ ok: false,
21717
+ restartRequired: false,
21718
+ error: "找不到插件所在的 profile 目录,无法自动更新;请在 DSH 的插件设置里更新"
21719
+ };
21720
+ const target = join(tmpdir(), `${PACKAGE}-${version}.tgz`);
21721
+ try {
21722
+ const response = await fetch(`https://registry.npmjs.org/${PACKAGE}/-/${PACKAGE}-${version}.tgz`, { signal: AbortSignal.timeout(12e4) });
21723
+ if (!response.ok || response.body === null) return {
21724
+ ok: false,
21725
+ restartRequired: false,
21726
+ error: `下载失败:HTTP ${String(response.status)}`
21727
+ };
21728
+ mkdirSync(dirname(target), { recursive: true });
21729
+ const partial = `${target}.part`;
21730
+ const sink = createWriteStream(partial);
21731
+ for await (const chunk of response.body) sink.write(chunk);
21732
+ await new Promise((resolve, reject) => {
21733
+ sink.end(() => {
21734
+ resolve();
21735
+ });
21736
+ sink.on("error", reject);
21737
+ });
21738
+ const head = readFileSync(partial).subarray(0, 2);
21739
+ if (head[0] !== 31 || head[1] !== 139) {
21740
+ rmSync(partial, { force: true });
21741
+ return {
21742
+ ok: false,
21743
+ restartRequired: false,
21744
+ error: "下载到的不是压缩包(可能被代理拦截)"
21745
+ };
21746
+ }
21747
+ renameSync(partial, target);
21748
+ } catch (error) {
21749
+ return {
21750
+ ok: false,
21751
+ restartRequired: false,
21752
+ error: error instanceof Error ? error.message : String(error)
21753
+ };
21754
+ }
21755
+ try {
21756
+ const pkgPath = join(profile, "package.json");
21757
+ const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
21758
+ let changed = false;
21759
+ for (const key of [
21760
+ "dependencies",
21761
+ "devDependencies",
21762
+ "optionalDependencies"
21763
+ ]) {
21764
+ const block = pkg[key];
21765
+ if (block === null || typeof block !== "object") continue;
21766
+ const record = block;
21767
+ if (!Object.hasOwn(record, PACKAGE)) continue;
21768
+ record[PACKAGE] = `file:${target.replaceAll("\\", "/")}`;
21769
+ changed = true;
21770
+ }
21771
+ if (!changed) return {
21772
+ ok: false,
21773
+ restartRequired: false,
21774
+ path: target,
21775
+ error: `profile 的 package.json 里没有 ${PACKAGE},无法自动更新`
21776
+ };
21777
+ writeFileSync(pkgPath, `${JSON.stringify(pkg, null, 2)}\n`, "utf8");
21778
+ return {
21779
+ ok: true,
21780
+ version,
21781
+ path: target,
21782
+ restartRequired: true
21783
+ };
21784
+ } catch (error) {
21785
+ return {
21786
+ ok: false,
21787
+ restartRequired: false,
21788
+ path: target,
21789
+ error: error instanceof Error ? error.message : String(error)
21790
+ };
21791
+ }
21792
+ }
21793
+ /**
21794
+ * Check, then install if there is something newer.
21795
+ *
21796
+ * Kept together so the route cannot install a version the check never saw — the alternative is a
21797
+ * caller passing an arbitrary version string to an endpoint that writes `package.json`.
21798
+ */
21799
+ async function updateToLatest() {
21800
+ const status = await checkForUpdate({ force: true });
21801
+ if (status.error !== void 0) return {
21802
+ ok: false,
21803
+ restartRequired: false,
21804
+ error: status.error
21805
+ };
21806
+ if (status.latest === void 0) return {
21807
+ ok: false,
21808
+ restartRequired: false,
21809
+ error: "没有读到最新版本号"
21810
+ };
21811
+ if (!status.newer) return {
21812
+ ok: true,
21813
+ version: status.current,
21814
+ restartRequired: false
21815
+ };
21816
+ return await installVersion(status.latest);
21817
+ }
21818
+ //#endregion
21659
21819
  //#region src/web-status.ts
21660
21820
  /** Redact token-like content before it crosses to the browser. */
21661
21821
  /**
@@ -22819,7 +22979,32 @@ function registerPoolStatusRoute(ctx, deps) {
22819
22979
  }
22820
22980
  }
22821
22981
  });
22982
+ /**
22983
+ * Install the newest build.
22984
+ *
22985
+ * `POST` only, and the BODY is ignored: the version comes from the same check the panel
22986
+ * shows, never from the caller. An endpoint that writes `package.json` must not take a
22987
+ * version string on trust.
22988
+ */
22989
+ const disposeInstall = ctx.webServer.register({
22990
+ kind: "exact",
22991
+ path: POOL_UPDATE_INSTALL_PATH,
22992
+ handler: async (req, res) => {
22993
+ if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
22994
+ if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
22995
+ try {
22996
+ return json(res, 200, await updateToLatest());
22997
+ } catch (error) {
22998
+ return json(res, 200, {
22999
+ ok: false,
23000
+ restartRequired: false,
23001
+ error: safeMessage(error)
23002
+ });
23003
+ }
23004
+ }
23005
+ });
22822
23006
  return () => {
23007
+ disposeInstall();
22823
23008
  disposeUpdate();
22824
23009
  disposeRelay();
22825
23010
  disposeCombosSave();
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "dsh-xdpool",
3
3
  "displayName": "DSH XD Pool",
4
4
  "description": "One DSH model pool for every free quota you have: merge all locally signed-in WorkBuddy accounts with auto-failover, sign in to 13 vendors by QR/web, aggregate 76 vendors' free tiers, plus a usage dashboard, model routing, a local OpenAI-compatible gateway and a points-automation suite.",
5
- "version": "2.7.4",
5
+ "version": "2.7.5",
6
6
  "license": "MIT",
7
7
  "author": "XDTrees",
8
8
  "homepage": "https://github.com/XDTrees/dsh-xdpool#readme",