dsh-profile-config-sync 0.6.45 → 0.6.47
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/README.en.md +10 -7
- package/README.md +17 -14
- package/client.js +32 -117
- package/docs/CHANGELOG.md +6 -4
- package/index.js +384 -150
- package/package.json +3 -2
- package/release-notes.json +6 -0
- package/runtime/install-plugin.ps1 +60 -1
- package/runtime/post-exit.ps1 +80 -1
package/index.js
CHANGED
|
@@ -20,6 +20,7 @@ import { AsyncLocalStorage } from 'node:async_hooks'
|
|
|
20
20
|
import { spawn } from 'node:child_process'
|
|
21
21
|
import { createHash } from 'node:crypto'
|
|
22
22
|
import { createRequire } from 'node:module'
|
|
23
|
+
import { gunzipSync } from 'node:zlib'
|
|
23
24
|
import { closeSync, existsSync, mkdirSync, mkdtempSync, openSync, readdirSync, readFileSync, readSync, rmSync, statSync, unlinkSync, writeFileSync, chmodSync } from 'node:fs'
|
|
24
25
|
import { writeFileAtomicSync } from './runtime/atomic-write.mjs'
|
|
25
26
|
import { homedir, hostname, tmpdir } from 'node:os'
|
|
@@ -100,32 +101,54 @@ const PLUGIN_VERSION = (() => {
|
|
|
100
101
|
return String(pkg.version || '')
|
|
101
102
|
} catch { return '' }
|
|
102
103
|
})()
|
|
103
|
-
//
|
|
104
|
-
//
|
|
104
|
+
// ── 0.6.47 起:更新检查只查 npm,不看 GitHub ──
|
|
105
|
+
// 用户明确要求「插件更新只检查 npm 是否有更新,不检查 GitHub」。
|
|
105
106
|
//
|
|
106
|
-
//
|
|
107
|
-
//
|
|
108
|
-
//
|
|
107
|
+
// 版本判定:npm registry 的 dist-tags.latest。
|
|
108
|
+
// 下载地址与校验值:该版本的单版本文档里的 dist.tarball / dist.integrity / dist.shasum。
|
|
109
|
+
// 发布说明:npm tarball 里的 package/release-notes.json(已加进 package.json 的 files 字段)。
|
|
109
110
|
//
|
|
110
|
-
//
|
|
111
|
-
//
|
|
112
|
-
//
|
|
113
|
-
//
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
]
|
|
124
|
-
const UPDATE_MANIFEST_URL = RAW_MANIFEST_URL
|
|
125
|
-
// 30 秒:慢链路 10 秒根本不够(raw 通的时候也要几百毫秒到十几秒不等)
|
|
111
|
+
// 为什么这么换(不是"顺手改一下",是踩到过坑):
|
|
112
|
+
// GitHub release 资产与 npm tarball **不是同一个字节流** —— 实测 npm pack 产物与 registry 上的
|
|
113
|
+
// tarball 逐字节一致(sha256 相同),但与 pnpm pack 的资产不同,差在打包元数据。
|
|
114
|
+
// 于是旧实现「清单里记 pnpm 的 sha256、下载走 npm 兜底」在校验那一步必然失败,
|
|
115
|
+
// 那条「npm 兜底下载」的路一直有坑。改成单一 npm 源后,校验改用 registry 自己的
|
|
116
|
+
// dist.integrity / dist.shasum,权威值与被校验物同源,不会再出现这种错配。
|
|
117
|
+
//
|
|
118
|
+
// npm 作为唯一更新源(0.6.47 起)
|
|
119
|
+
const NPM_PACKAGE_NAME = 'dsh-profile-config-sync'
|
|
120
|
+
// registry / dist-tags / 版本文档的 URL 一律由 npmUrls(registry) 现算(见下方 effectiveNpmRegistry),
|
|
121
|
+
// 不在模块顶层写死 —— 否则夹具没法用本地服务器当 registry。
|
|
122
|
+
|
|
123
|
+
// 30 秒:慢链路 10 秒根本不够
|
|
126
124
|
const UPDATE_FETCH_TIMEOUT = 30 * 1000
|
|
127
125
|
const UPDATE_CHECK_TTL = 30 * 60 * 1000
|
|
128
126
|
const GH_TOKEN_ENV_CANDIDATES = ['DSH_CONFIG_SYNC_GH_TOKEN', 'GITHUB_TOKEN', 'GH_TOKEN']
|
|
127
|
+
// 0.6.47:npm registry 可被设置或环境变量覆盖。
|
|
128
|
+
// 用途有两个:① 国内用户换 npmmirror 之类镜像(仍然只查 npm,只是换了个 registry);
|
|
129
|
+
// ② 审计夹具用本地 HTTP 服务器当 registry,不必真的连公网。
|
|
130
|
+
// 环境变量优先,便于夹具在进程启动前就定好;设置里改了下次检查生效。
|
|
131
|
+
const NPM_REGISTRY_ENV = 'DSH_CONFIG_SYNC_NPM_REGISTRY'
|
|
132
|
+
const DEFAULT_NPM_REGISTRY = 'https://registry.npmjs.org'
|
|
133
|
+
|
|
134
|
+
function effectiveNpmRegistry(settings = {}) {
|
|
135
|
+
const fromSettings = String((settings && settings.updateRegistry) || '').trim()
|
|
136
|
+
if (fromSettings) return fromSettings.replace(/\/+$/, '')
|
|
137
|
+
const fromEnv = String(process.env[NPM_REGISTRY_ENV] || '').trim()
|
|
138
|
+
if (fromEnv) return fromEnv.replace(/\/+$/, '')
|
|
139
|
+
return DEFAULT_NPM_REGISTRY
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function npmUrls(registry) {
|
|
143
|
+
const base = String(registry || DEFAULT_NPM_REGISTRY).replace(/\/+$/, '')
|
|
144
|
+
return {
|
|
145
|
+
registry: base,
|
|
146
|
+
distTags: `${base}/${NPM_PACKAGE_NAME}/-/package/${NPM_PACKAGE_NAME}/dist-tags`,
|
|
147
|
+
versionDoc: (v) => `${base}/${NPM_PACKAGE_NAME}/${encodeURIComponent(v)}`,
|
|
148
|
+
latestDoc: `${base}/${NPM_PACKAGE_NAME}/latest`,
|
|
149
|
+
tarball: (v) => `${base}/${NPM_PACKAGE_NAME}/-/${NPM_PACKAGE_NAME}-${v}.tgz`,
|
|
150
|
+
}
|
|
151
|
+
}
|
|
129
152
|
|
|
130
153
|
const winEnvCache = new Map()
|
|
131
154
|
function clearWinEnvCache() { winEnvCache.clear() }
|
|
@@ -2873,78 +2896,91 @@ function compareVersion(a, b) {
|
|
|
2873
2896
|
}
|
|
2874
2897
|
|
|
2875
2898
|
/**
|
|
2876
|
-
* 0.3.89
|
|
2877
|
-
*
|
|
2878
|
-
*
|
|
2879
|
-
*
|
|
2899
|
+
* 0.3.89 起是「npm 双源」;0.6.47 起 npm 成为**唯一**更新源。
|
|
2900
|
+
*
|
|
2901
|
+
* 旧注释说「npm 包与 GitHub release 的 tgz 是同一份发布产物(实测 sha256 完全一致)」——
|
|
2902
|
+
* 这句话是错的,实测:npm pack 产物与 registry 上的 tarball 逐字节一致(sha256 相同),
|
|
2903
|
+
* 但与 pnpm pack 的资产**不是同一份**(打包元数据不同,sha256 不同)。
|
|
2904
|
+
* 于是旧实现「清单记 pnpm 的 sha256、下载走 npm 兜底」在校验那一步必然失败。
|
|
2905
|
+
* 0.6.47 改成单一 npm 源后,校验值取 registry 自己的 dist.integrity / dist.shasum,
|
|
2906
|
+
* 权威值与被校验物同源,这个错配从根上没了。
|
|
2880
2907
|
*/
|
|
2881
|
-
function npmTgzUrls(version) {
|
|
2908
|
+
function npmTgzUrls(version, registry = DEFAULT_NPM_REGISTRY) {
|
|
2882
2909
|
const v = String(version || '').replace(/[^0-9A-Za-z.+\-]/g, '')
|
|
2883
2910
|
if (!v) return []
|
|
2884
|
-
|
|
2885
|
-
return [
|
|
2886
|
-
`https://registry.npmmirror.com/dsh-profile-config-sync/-/${f}`,
|
|
2887
|
-
`https://registry.npmjs.org/dsh-profile-config-sync/-/${f}`,
|
|
2888
|
-
]
|
|
2911
|
+
return [npmUrls(registry).tarball(v)]
|
|
2889
2912
|
}
|
|
2890
|
-
// 兼容旧调用:返回首选 npm
|
|
2891
|
-
function npmTgzUrl(version) {
|
|
2892
|
-
const list = npmTgzUrls(version)
|
|
2913
|
+
// 兼容旧调用:返回首选 npm 地址
|
|
2914
|
+
function npmTgzUrl(version, registry = DEFAULT_NPM_REGISTRY) {
|
|
2915
|
+
const list = npmTgzUrls(version, registry)
|
|
2893
2916
|
return list.length ? list[0] : ''
|
|
2894
2917
|
}
|
|
2895
2918
|
|
|
2896
2919
|
/**
|
|
2897
|
-
*
|
|
2898
|
-
*
|
|
2899
|
-
* —— GitHub release 整段不通时(国内常见),仍能走 npm 把包下下来。
|
|
2920
|
+
* 0.6.47:下载地址只认 npm 单版本文档里给的 dist.tarball。
|
|
2921
|
+
* 没有它才退回按版本号拼 registry 地址(正常不会走到)。
|
|
2900
2922
|
*/
|
|
2901
|
-
function resolveTgzCandidates(manifest,
|
|
2923
|
+
function resolveTgzCandidates(manifest, registry = DEFAULT_NPM_REGISTRY) {
|
|
2902
2924
|
const tgz = String((manifest && manifest.tgz) || '')
|
|
2903
|
-
const npmList = npmTgzUrls(manifest && manifest.version)
|
|
2904
|
-
const withNpm = (list) => {
|
|
2905
|
-
const out = list.slice()
|
|
2906
|
-
for (const u of npmList) if (u && !out.includes(u)) out.push(u)
|
|
2907
|
-
return out
|
|
2908
|
-
}
|
|
2909
|
-
// 清单没给 tgz 时,也留 npm 这两条路(有版本就能拼出 tarball)
|
|
2925
|
+
const npmList = npmTgzUrls(manifest && manifest.version, registry)
|
|
2910
2926
|
if (!tgz) return npmList.slice()
|
|
2911
|
-
|
|
2912
|
-
|
|
2927
|
+
if (/^https?:\/\//i.test(tgz)) return [tgz]
|
|
2928
|
+
const base = String(registry || DEFAULT_NPM_REGISTRY).replace(/\/+$/, '')
|
|
2913
2929
|
const rel = tgz.replace(/^\.?\//, '')
|
|
2914
|
-
|
|
2915
|
-
const actual = String(manifestUrl || '').replace(/[^/]*$/, '')
|
|
2916
|
-
if (actual) bases.push(actual)
|
|
2917
|
-
for (const cand of UPDATE_MANIFEST_MIRRORS) {
|
|
2918
|
-
const base = cand.replace(/[^/]*$/, '')
|
|
2919
|
-
if (base && !bases.includes(base)) bases.push(base)
|
|
2920
|
-
}
|
|
2921
|
-
return withNpm(bases.map((base) => base + rel))
|
|
2930
|
+
return [`${base}/${rel}`]
|
|
2922
2931
|
}
|
|
2923
2932
|
|
|
2924
|
-
/**
|
|
2925
|
-
function resolveTgzUrl(manifest,
|
|
2926
|
-
const list = resolveTgzCandidates(manifest,
|
|
2933
|
+
/** 下载地址:优先用 registry 给的绝对地址,没有才按版本号拼。 */
|
|
2934
|
+
function resolveTgzUrl(manifest, registry = DEFAULT_NPM_REGISTRY) {
|
|
2935
|
+
const list = resolveTgzCandidates(manifest, registry)
|
|
2927
2936
|
if (list.length) return list[0]
|
|
2928
2937
|
// 没给 tgz 时保留原来的兜底行为(返回空串,调用方自己报错)
|
|
2929
2938
|
return ''
|
|
2930
2939
|
}
|
|
2931
2940
|
|
|
2932
|
-
|
|
2941
|
+
/**
|
|
2942
|
+
* 0.6.47:读 npm 的 dist-tags(几十字节),拿到 latest 指向的版本号。
|
|
2943
|
+
* 用 /-/package/<name>/dist-tags 而不是完整 packument —— 后者含全部历史版本,
|
|
2944
|
+
* 面板每次打开都拉一遍纯属浪费。
|
|
2945
|
+
*/
|
|
2946
|
+
async function fetchNpmLatestVersion(registry = DEFAULT_NPM_REGISTRY, timeoutMs = UPDATE_FETCH_TIMEOUT) {
|
|
2933
2947
|
if (typeof fetch !== 'function') throw new Error(t('当前运行环境没有 fetch,无法检查更新', 'fetch is unavailable in this runtime — cannot check for updates'))
|
|
2934
2948
|
const seconds = Math.round(timeoutMs / 1000)
|
|
2935
2949
|
const ctrl = typeof AbortController === 'function' ? new AbortController() : null
|
|
2936
2950
|
let timedOut = false
|
|
2937
2951
|
const timer = ctrl ? setTimeout(() => { timedOut = true; try { ctrl.abort() } catch { } }, timeoutMs) : null
|
|
2938
2952
|
try {
|
|
2939
|
-
const res = await fetch(
|
|
2940
|
-
if (!res.ok) {
|
|
2941
|
-
|
|
2942
|
-
|
|
2953
|
+
const res = await fetch(npmUrls(registry).distTags, { cache: 'no-store', signal: ctrl ? ctrl.signal : undefined })
|
|
2954
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}`)
|
|
2955
|
+
const json = await res.json()
|
|
2956
|
+
const latest = json && json.latest
|
|
2957
|
+
if (!latest || typeof latest !== 'string') throw new Error(t('npm 的 dist-tags 里没有 latest', 'npm dist-tags has no "latest"'))
|
|
2958
|
+
return String(latest)
|
|
2959
|
+
} catch (error) {
|
|
2960
|
+
if (timedOut) throw new Error(t(`超时(${seconds} 秒内没有响应)`, `timed out (no response in ${seconds}s)`))
|
|
2961
|
+
throw error
|
|
2962
|
+
} finally {
|
|
2963
|
+
if (timer) clearTimeout(timer)
|
|
2964
|
+
}
|
|
2965
|
+
}
|
|
2966
|
+
|
|
2967
|
+
/**
|
|
2968
|
+
* 0.6.47:读 npm 上某个版本的文档(2~3 KB),取 dist.tarball / dist.integrity / dist.shasum。
|
|
2969
|
+
* 校验值一律取 registry 自己给的 —— 权威值与被校验物同源,不会出现「清单记 A 的哈希、下载 B 的包」。
|
|
2970
|
+
*/
|
|
2971
|
+
async function fetchNpmVersionDoc(version, registry = DEFAULT_NPM_REGISTRY, timeoutMs = UPDATE_FETCH_TIMEOUT) {
|
|
2972
|
+
if (typeof fetch !== 'function') throw new Error(t('当前运行环境没有 fetch,无法检查更新', 'fetch is unavailable in this runtime — cannot check for updates'))
|
|
2973
|
+
const seconds = Math.round(timeoutMs / 1000)
|
|
2974
|
+
const ctrl = typeof AbortController === 'function' ? new AbortController() : null
|
|
2975
|
+
let timedOut = false
|
|
2976
|
+
const timer = ctrl ? setTimeout(() => { timedOut = true; try { ctrl.abort() } catch { } }, timeoutMs) : null
|
|
2977
|
+
try {
|
|
2978
|
+
const res = await fetch(npmUrls(registry).versionDoc(version), { cache: 'no-store', signal: ctrl ? ctrl.signal : undefined })
|
|
2979
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}`)
|
|
2943
2980
|
const json = await res.json()
|
|
2944
|
-
if (!json || !json.version) throw new Error(t('
|
|
2981
|
+
if (!json || !json.version) throw new Error(t('npm 上没有这个版本的文档', 'npm has no document for that version'))
|
|
2945
2982
|
return json
|
|
2946
2983
|
} catch (error) {
|
|
2947
|
-
// abort 的原话是 "This operation was aborted",看不出是谁的问题,换成能看懂的话
|
|
2948
2984
|
if (timedOut) throw new Error(t(`超时(${seconds} 秒内没有响应)`, `timed out (no response in ${seconds}s)`))
|
|
2949
2985
|
throw error
|
|
2950
2986
|
} finally {
|
|
@@ -2952,6 +2988,33 @@ async function fetchManifest(url, timeoutMs = UPDATE_FETCH_TIMEOUT, headers = {}
|
|
|
2952
2988
|
}
|
|
2953
2989
|
}
|
|
2954
2990
|
|
|
2991
|
+
/**
|
|
2992
|
+
* 0.6.47:从 npm tarball 里取包内的 release-notes.json(发布说明)。
|
|
2993
|
+
* npm tarball 是 gzip 的 tar,只解到内存 —— 只取 release-notes.json 一个文件,
|
|
2994
|
+
* tar 里条目按顺序出现,release-notes.json 通常排在 package.json 之后。
|
|
2995
|
+
*/
|
|
2996
|
+
function extractReleaseNotesFromTarball(buf) {
|
|
2997
|
+
try {
|
|
2998
|
+
const tar = gunzipSync(buf)
|
|
2999
|
+
// tar 头 512 字节一块;文件名在 0..100,size 在 124..156(八进制字符串)
|
|
3000
|
+
for (let off = 0; off + 512 <= tar.length; off += 512) {
|
|
3001
|
+
const nameRaw = tar.slice(off, off + 100)
|
|
3002
|
+
const zero = nameRaw.indexOf(0)
|
|
3003
|
+
const name = zero >= 0 ? nameRaw.slice(0, zero).toString('utf8') : nameRaw.toString('utf8')
|
|
3004
|
+
if (!name) continue
|
|
3005
|
+
const sizeRaw = tar.slice(off + 124, off + 156).toString('ascii').replace(/\0.*$/, '').trim()
|
|
3006
|
+
const size = parseInt(sizeRaw, 8) || 0
|
|
3007
|
+
const body = off + 512
|
|
3008
|
+
const base = name.replace(/^\.\//, '')
|
|
3009
|
+
if (base === 'package/release-notes.json' || base === 'release-notes.json') {
|
|
3010
|
+
if (size > 0 && body + size <= tar.length) return JSON.parse(tar.slice(body, body + size).toString('utf8'))
|
|
3011
|
+
}
|
|
3012
|
+
off += Math.ceil(size / 512) * 512
|
|
3013
|
+
}
|
|
3014
|
+
} catch { /* 解不出来就当没有说明文字 */ }
|
|
3015
|
+
return null
|
|
3016
|
+
}
|
|
3017
|
+
|
|
2955
3018
|
/** 仅向直连官方域传令牌(绝不把令牌发给第三方反代 gh-proxy / ghfast)。 */
|
|
2956
3019
|
function authHeadersForUrl(url, token) {
|
|
2957
3020
|
try {
|
|
@@ -2994,39 +3057,13 @@ async function resolveUpdateToken(settings, customUrl = '') {
|
|
|
2994
3057
|
}
|
|
2995
3058
|
|
|
2996
3059
|
/**
|
|
2997
|
-
*
|
|
2998
|
-
*
|
|
2999
|
-
*
|
|
3060
|
+
* 0.6.47:旧的「清单」读取路径整体退休。
|
|
3061
|
+
*
|
|
3062
|
+
* 原来是「读 GitHub 仓库根的 version.json」(raw / API / gh-proxy / ghfast 依次试),
|
|
3063
|
+
* 现在更新检查只查 npm,GitHub 不再参与 —— fetchManifestAuto / fetchManifest 连同
|
|
3064
|
+
* UPDATE_MANIFEST_MIRRORS 一起删掉,避免留下「看起来还在用」的分支。
|
|
3065
|
+
* 保留 withAccessToken / authHeadersForUrl:下载环节还可能碰到 Gitee 私有库与 GitHub 资产的旧配置。
|
|
3000
3066
|
*/
|
|
3001
|
-
async function fetchManifestAuto(targetUrl, token = '', timeoutMs = UPDATE_FETCH_TIMEOUT) {
|
|
3002
|
-
const custom = String(targetUrl || '').trim()
|
|
3003
|
-
const candidates = custom
|
|
3004
|
-
? [custom]
|
|
3005
|
-
: (token
|
|
3006
|
-
? [API_MANIFEST_URL, ...UPDATE_MANIFEST_MIRRORS.filter((u) => u !== API_MANIFEST_URL)]
|
|
3007
|
-
: UPDATE_MANIFEST_MIRRORS.slice())
|
|
3008
|
-
const tried = []
|
|
3009
|
-
for (const cand of candidates) {
|
|
3010
|
-
try {
|
|
3011
|
-
const headers = authHeadersForUrl(cand, token)
|
|
3012
|
-
// 有令牌且走直连 GitHub 域时改用 Authorization Header,绝不把令牌拼进第三方反代 URL
|
|
3013
|
-
const reqUrl = Object.keys(headers).length ? cand : withAccessToken(cand, token)
|
|
3014
|
-
const manifest = await fetchManifest(reqUrl, timeoutMs, headers)
|
|
3015
|
-
tried.push({ url: cand, ok: true })
|
|
3016
|
-
return { manifest, url: cand, tried }
|
|
3017
|
-
} catch (error) {
|
|
3018
|
-
const msg = String((error && error.message) || error)
|
|
3019
|
-
let host = cand
|
|
3020
|
-
try { host = new URL(cand).host } catch { }
|
|
3021
|
-
tried.push({ url: cand, host, ok: false, error: msg })
|
|
3022
|
-
if (custom) break
|
|
3023
|
-
}
|
|
3024
|
-
}
|
|
3025
|
-
const detail = tried.map((x) => `${x.host || x.url}:${x.ok ? 'ok' : x.error}`).join(';')
|
|
3026
|
-
const error = new Error(t(`所有更新源都读不到(${detail})`, `No update source could be reached (${detail})`))
|
|
3027
|
-
error.sources = tried
|
|
3028
|
-
throw error
|
|
3029
|
-
}
|
|
3030
3067
|
|
|
3031
3068
|
/**
|
|
3032
3069
|
* 私有仓库的 raw 链接匿名读会 403,必须换成 Gitee API 的 raw 端点再带 access_token。
|
|
@@ -3049,17 +3086,18 @@ function withAccessToken(url, token) {
|
|
|
3049
3086
|
return target + sep + 'access_token=' + encodeURIComponent(token)
|
|
3050
3087
|
}
|
|
3051
3088
|
|
|
3052
|
-
async function checkUpdate({ force = false,
|
|
3053
|
-
const
|
|
3089
|
+
async function checkUpdate({ force = false, registry = '', settings = {} } = {}) {
|
|
3090
|
+
const reg = String(registry || effectiveNpmRegistry(settings))
|
|
3054
3091
|
const now = Date.now()
|
|
3055
|
-
if (!force && updateState.requestUrl ===
|
|
3092
|
+
if (!force && updateState.requestUrl === reg && updateState.checkedAt && now - updateState.checkedAt < UPDATE_CHECK_TTL && (updateState.manifest || updateState.error)) {
|
|
3056
3093
|
return updateState
|
|
3057
3094
|
}
|
|
3058
3095
|
updateState.checking = true
|
|
3059
|
-
updateState.requestUrl =
|
|
3060
|
-
updateState.token =
|
|
3096
|
+
updateState.requestUrl = reg
|
|
3097
|
+
updateState.token = ''
|
|
3061
3098
|
try {
|
|
3062
|
-
|
|
3099
|
+
// 0.6.47:只查 npm。GitHub release 不再参与更新检查。
|
|
3100
|
+
const got = await fetchNpmLatest(reg)
|
|
3063
3101
|
updateState.manifest = got.manifest
|
|
3064
3102
|
updateState.url = got.url
|
|
3065
3103
|
updateState.sources = got.tried
|
|
@@ -3075,25 +3113,153 @@ async function checkUpdate({ force = false, url = '', token = '' } = {}) {
|
|
|
3075
3113
|
return updateState
|
|
3076
3114
|
}
|
|
3077
3115
|
|
|
3116
|
+
/**
|
|
3117
|
+
* 0.6.47:更新检查的唯一实现 —— 读 npm。
|
|
3118
|
+
*
|
|
3119
|
+
* 流程:
|
|
3120
|
+
* 1) dist-tags 拿 latest 的版本号(几十字节);
|
|
3121
|
+
* 2) 该版本的单版本文档拿 dist.tarball / dist.integrity / dist.shasum(2~3 KB);
|
|
3122
|
+
* 3) 发布说明从 npm tarball 里的 package/release-notes.json 读(不解包到磁盘,只在内存里解 gzip+tar)。
|
|
3123
|
+
*
|
|
3124
|
+
* 为什么第 3 步要下整包:npm 的单版本文档里没有发布说明(那是 GitHub release 的内容,
|
|
3125
|
+
* 而用户明确要求更新检查不查 GitHub)。tarball 400 多 KB,只在有新版本时才读一次。
|
|
3126
|
+
*/
|
|
3127
|
+
async function fetchNpmLatest(registry = DEFAULT_NPM_REGISTRY) {
|
|
3128
|
+
const u = npmUrls(registry)
|
|
3129
|
+
const tried = []
|
|
3130
|
+
const record = (url, ok, error) => {
|
|
3131
|
+
let host = url
|
|
3132
|
+
try { host = new URL(url).host } catch { }
|
|
3133
|
+
tried.push({ url, host, ok, ...(error ? { error } : {}) })
|
|
3134
|
+
}
|
|
3135
|
+
let latestVersion = ''
|
|
3136
|
+
try {
|
|
3137
|
+
latestVersion = await fetchNpmLatestVersion(registry)
|
|
3138
|
+
record(u.distTags, true)
|
|
3139
|
+
} catch (error) {
|
|
3140
|
+
record(u.distTags, false, String((error && error.message) || error))
|
|
3141
|
+
const detail = tried.map((x) => `${x.host}:${x.error}`).join(';')
|
|
3142
|
+
const err = new Error(t(`读不到 npm 的版本信息(${detail})`, `Cannot read the version list from npm (${detail})`))
|
|
3143
|
+
err.sources = tried
|
|
3144
|
+
throw err
|
|
3145
|
+
}
|
|
3146
|
+
if (compareVersion(latestVersion, PLUGIN_VERSION) <= 0) {
|
|
3147
|
+
// 本机已经是 npm 上的最新版:不必再拉版本文档,省一次请求。
|
|
3148
|
+
// 但下载地址要能给出来 —— 面板会显示它,也便于手工核对。
|
|
3149
|
+
return {
|
|
3150
|
+
manifest: {
|
|
3151
|
+
version: latestVersion, notes: '', notesEn: '', releasedAt: '',
|
|
3152
|
+
tgz: u.tarball(latestVersion), sha256: '', integrity: '', sha1: '', source: 'npm',
|
|
3153
|
+
},
|
|
3154
|
+
url: u.distTags,
|
|
3155
|
+
tried,
|
|
3156
|
+
}
|
|
3157
|
+
}
|
|
3158
|
+
let doc = null
|
|
3159
|
+
try {
|
|
3160
|
+
doc = await fetchNpmVersionDoc(latestVersion, registry)
|
|
3161
|
+
record(u.versionDoc(latestVersion), true)
|
|
3162
|
+
} catch (error) {
|
|
3163
|
+
record(u.versionDoc(latestVersion), false, String((error && error.message) || error))
|
|
3164
|
+
const detail = tried.map((x) => `${x.host}:${x.error}`).join(';')
|
|
3165
|
+
const err = new Error(t(`读不到 npm 上 ${latestVersion} 的包信息(${detail})`, `Cannot read the package info for ${latestVersion} from npm (${detail})`))
|
|
3166
|
+
err.sources = tried
|
|
3167
|
+
throw err
|
|
3168
|
+
}
|
|
3169
|
+
const dist = (doc && doc.dist) || {}
|
|
3170
|
+
const tarball = String(dist.tarball || '')
|
|
3171
|
+
const manifest = {
|
|
3172
|
+
version: String(doc.version || latestVersion),
|
|
3173
|
+
notes: '',
|
|
3174
|
+
notesEn: '',
|
|
3175
|
+
releasedAt: '',
|
|
3176
|
+
tgz: tarball,
|
|
3177
|
+
// npm 给的权威校验值:integrity 是 sha512(SRI),shasum 是 sha1。
|
|
3178
|
+
// 两者都取,下载时优先用 integrity(更强),退回 shasum。
|
|
3179
|
+
integrity: String(dist.integrity || ''),
|
|
3180
|
+
sha1: String(dist.shasum || ''),
|
|
3181
|
+
sha256: '',
|
|
3182
|
+
source: 'npm',
|
|
3183
|
+
}
|
|
3184
|
+
// 发布说明:下 tarball 从里面取 release-notes.json。
|
|
3185
|
+
// 缓存按「版本+integrity」记:同一次会话里反复点「检查更新」不该反复下 400KB。
|
|
3186
|
+
if (tarball) {
|
|
3187
|
+
const cacheKey = `${manifest.version}|${manifest.integrity}`
|
|
3188
|
+
const cached = notesCache.get(cacheKey)
|
|
3189
|
+
if (cached) {
|
|
3190
|
+
manifest.notes = cached.notes
|
|
3191
|
+
manifest.notesEn = cached.notesEn
|
|
3192
|
+
manifest.releasedAt = cached.releasedAt
|
|
3193
|
+
manifest.pkgSha256 = cached.pkgSha256
|
|
3194
|
+
} else {
|
|
3195
|
+
try {
|
|
3196
|
+
const buf = await fetchTarballBuffer(tarball)
|
|
3197
|
+
const inner = extractReleaseNotesFromTarball(buf)
|
|
3198
|
+
if (inner && typeof inner === 'object') {
|
|
3199
|
+
manifest.notes = String(inner.notes || '')
|
|
3200
|
+
manifest.notesEn = String(inner.notesEn || '')
|
|
3201
|
+
manifest.releasedAt = String(inner.releasedAt || '')
|
|
3202
|
+
// 包内 version.json 的 sha256 是打包时那份 tgz 的哈希,和 npm tarball 不是同一份,
|
|
3203
|
+
// 所以**不用**它做校验(校验用 registry 的 integrity/shasum)。
|
|
3204
|
+
manifest.pkgSha256 = String(inner.sha256 || '')
|
|
3205
|
+
}
|
|
3206
|
+
notesCache.set(cacheKey, {
|
|
3207
|
+
notes: manifest.notes, notesEn: manifest.notesEn,
|
|
3208
|
+
releasedAt: manifest.releasedAt, pkgSha256: manifest.pkgSha256,
|
|
3209
|
+
})
|
|
3210
|
+
} catch (error) {
|
|
3211
|
+
record(tarball, false, String((error && error.message) || error))
|
|
3212
|
+
// 取不到说明文字不算失败:版本号与下载地址已经拿到了
|
|
3213
|
+
}
|
|
3214
|
+
}
|
|
3215
|
+
}
|
|
3216
|
+
return { manifest, url: u.versionDoc(manifest.version), tried }
|
|
3217
|
+
}
|
|
3218
|
+
|
|
3219
|
+
/** 下载 npm tarball 到内存(只用于取包内 release-notes.json)。 */
|
|
3220
|
+
async function fetchTarballBuffer(url, timeoutMs = 120 * 1000) {
|
|
3221
|
+
if (typeof fetch !== 'function') throw new Error(t('当前运行环境没有 fetch', 'fetch is unavailable in this runtime'))
|
|
3222
|
+
const ctrl = typeof AbortController === 'function' ? new AbortController() : null
|
|
3223
|
+
const timer = ctrl ? setTimeout(() => { try { ctrl.abort() } catch { } }, timeoutMs) : null
|
|
3224
|
+
try {
|
|
3225
|
+
const res = await fetch(url, { cache: 'no-store', signal: ctrl ? ctrl.signal : undefined })
|
|
3226
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}`)
|
|
3227
|
+
const buf = Buffer.from(await res.arrayBuffer())
|
|
3228
|
+
if (!buf.length) throw new Error(t('下载到的包是空的', 'the downloaded package is empty'))
|
|
3229
|
+
return buf
|
|
3230
|
+
} finally {
|
|
3231
|
+
if (timer) clearTimeout(timer)
|
|
3232
|
+
}
|
|
3233
|
+
}
|
|
3234
|
+
|
|
3235
|
+
// 发布说明缓存:key = 版本|integrity。同一次会话里反复点「检查更新」不该反复下 400KB 的包。
|
|
3236
|
+
const notesCache = new Map()
|
|
3237
|
+
|
|
3078
3238
|
function updatePayload(state, settings = {}, resolvedToken = null) {
|
|
3079
3239
|
const remote = state.manifest
|
|
3080
|
-
const custom = String(settings.updateManifestUrl || '')
|
|
3081
|
-
const resolvedUpd = resolvedToken || resolveTokenSpec(settings.updateToken, custom ? [] : GH_TOKEN_ENV_CANDIDATES)
|
|
3082
3240
|
const installed = state.liveInstall || null
|
|
3241
|
+
const reg = effectiveNpmRegistry(settings)
|
|
3242
|
+
const u = npmUrls(reg)
|
|
3083
3243
|
// 装好了但进程还在跑旧代码 = 需要重启;重启后 PLUGIN_VERSION 追上来,这条自然消失
|
|
3084
3244
|
const restartRequired = Boolean(installed && compareVersion(installed.version, PLUGIN_VERSION) > 0)
|
|
3085
3245
|
return {
|
|
3086
3246
|
ok: !state.error,
|
|
3087
3247
|
local: { version: PLUGIN_VERSION || t('未知', 'unknown'), dir: PLUGIN_DIR },
|
|
3088
|
-
//
|
|
3248
|
+
// 更新相关的配置集中在这里。0.6.47 起更新源固定是 npm:
|
|
3249
|
+
// 清单地址/令牌这两项对 npm 都不再适用(npm 公开包不需要令牌,也没有可换的清单地址)。
|
|
3250
|
+
// registry 可被设置或环境变量覆盖(换镜像 / 夹具用本地服务器),但仍然是「只查 npm」。
|
|
3089
3251
|
config: {
|
|
3090
|
-
|
|
3091
|
-
|
|
3092
|
-
|
|
3093
|
-
|
|
3094
|
-
|
|
3095
|
-
|
|
3096
|
-
|
|
3252
|
+
source: 'npm',
|
|
3253
|
+
packageName: NPM_PACKAGE_NAME,
|
|
3254
|
+
registry: reg,
|
|
3255
|
+
registryIsDefault: reg === DEFAULT_NPM_REGISTRY,
|
|
3256
|
+
manifestUrl: u.distTags,
|
|
3257
|
+
manifestUrlIsDefault: true,
|
|
3258
|
+
manifestSources: [u.distTags],
|
|
3259
|
+
tokenSet: false,
|
|
3260
|
+
tokenSaved: false,
|
|
3261
|
+
tokenSource: resolvedToken && resolvedToken.source ? resolvedToken.source : '',
|
|
3262
|
+
tokenEnv: resolvedToken && resolvedToken.envName ? resolvedToken.envName : '',
|
|
3097
3263
|
},
|
|
3098
3264
|
remote: remote ? {
|
|
3099
3265
|
version: String(remote.version || ''),
|
|
@@ -3101,11 +3267,14 @@ function updatePayload(state, settings = {}, resolvedToken = null) {
|
|
|
3101
3267
|
// 清单里的说明是中英两份(notes / notesEn),由客户端按自己的语言挑
|
|
3102
3268
|
notesEn: String(remote.notesEn || ''),
|
|
3103
3269
|
releasedAt: String(remote.releasedAt || ''),
|
|
3104
|
-
tgz: resolveTgzUrl(remote,
|
|
3105
|
-
//
|
|
3106
|
-
npmTgz:
|
|
3107
|
-
npmTgzUrls:
|
|
3270
|
+
tgz: resolveTgzUrl(remote, reg),
|
|
3271
|
+
// npm 下载地址(单一来源,与被校验物同源)
|
|
3272
|
+
npmTgz: String(remote.tgz || ''),
|
|
3273
|
+
npmTgzUrls: remote.tgz ? [String(remote.tgz)] : [],
|
|
3108
3274
|
sha256: String(remote.sha256 || ''),
|
|
3275
|
+
// registry 给的权威校验值
|
|
3276
|
+
integrity: String(remote.integrity || ''),
|
|
3277
|
+
sha1: String(remote.sha1 || ''),
|
|
3109
3278
|
} : null,
|
|
3110
3279
|
hasUpdate: remote ? compareVersion(remote.version, PLUGIN_VERSION) > 0 : false,
|
|
3111
3280
|
checking: state.checking,
|
|
@@ -3122,6 +3291,11 @@ function updatePayload(state, settings = {}, resolvedToken = null) {
|
|
|
3122
3291
|
file: String(state.prepared.file || ''),
|
|
3123
3292
|
size: Number(state.prepared.size) || 0,
|
|
3124
3293
|
sha256: String(state.prepared.sha256 || ''),
|
|
3294
|
+
// 0.6.47:记下这次是按哪个校验值过的 —— 面板/日志能看出「校验到底做了没有」
|
|
3295
|
+
integrity: String(state.prepared.integrity || ''),
|
|
3296
|
+
sha1: String(state.prepared.sha1 || ''),
|
|
3297
|
+
verifiedBy: String(state.prepared.verifiedBy || ''),
|
|
3298
|
+
verifiedAlgo: String(state.prepared.verifiedAlgo || ''),
|
|
3125
3299
|
at: Number(state.prepared.at) || 0,
|
|
3126
3300
|
ready: compareVersion(state.prepared.version, PLUGIN_VERSION) > 0,
|
|
3127
3301
|
}
|
|
@@ -3170,12 +3344,53 @@ async function fetchGithubReleaseAsset(url, token, timeoutMs = 120 * 1000) {
|
|
|
3170
3344
|
return Buffer.from(await assetRes.arrayBuffer())
|
|
3171
3345
|
}
|
|
3172
3346
|
|
|
3173
|
-
|
|
3347
|
+
/**
|
|
3348
|
+
* 0.6.47:校验下载到的包。
|
|
3349
|
+
*
|
|
3350
|
+
* 权威校验值取自 npm registry 自己的 dist.integrity(SRI,通常 sha512)/ dist.shasum(sha1),
|
|
3351
|
+
* 与被校验物同源。旧实现用 version.json 里的 sha256 —— 那是**打包时那份 tgz**的哈希,
|
|
3352
|
+
* 而 npm 上的 tarball 与它不是同一份字节流(npm pack 与 registry 上的 tarball 逐字节一致,
|
|
3353
|
+
* 但与 pnpm pack 的资产不同),所以旧校验对 npm 下载必然失败。
|
|
3354
|
+
*
|
|
3355
|
+
* 三种校验值都收:integrity 优先,其次 shasum(sha1),最后才是 sha256(兼容旧路径)。
|
|
3356
|
+
* 一个都没有时**不校验**(放行)—— 但要如实记下来,不能假装校验过。
|
|
3357
|
+
*/
|
|
3358
|
+
function verifyDownloadedPackage(buf, { integrity = '', sha1 = '', sha256 = '' } = {}) {
|
|
3359
|
+
const sri = String(integrity || '').trim()
|
|
3360
|
+
if (sri) {
|
|
3361
|
+
// SRI 形如 "sha512-<base64>",可能有多条(空格分隔),任一条对上即可
|
|
3362
|
+
const parts = sri.split(/\s+/).filter(Boolean)
|
|
3363
|
+
for (const part of parts) {
|
|
3364
|
+
const dash = part.indexOf('-')
|
|
3365
|
+
if (dash <= 0) continue
|
|
3366
|
+
const algo = part.slice(0, dash).toLowerCase()
|
|
3367
|
+
const b64 = part.slice(dash + 1)
|
|
3368
|
+
if (!/^sha(?:256|384|512)$/.test(algo)) continue
|
|
3369
|
+
const digest = createHash(algo).update(buf).digest('base64')
|
|
3370
|
+
if (digest === b64) return { ok: true, algo, verified: 'integrity' }
|
|
3371
|
+
}
|
|
3372
|
+
return { ok: false, error: t('包的 integrity 校验不通过', 'integrity verification failed') }
|
|
3373
|
+
}
|
|
3374
|
+
const want1 = String(sha1 || '').trim().toLowerCase()
|
|
3375
|
+
if (want1) {
|
|
3376
|
+
const got = createHash('sha1').update(buf).digest('hex')
|
|
3377
|
+
if (got !== want1) return { ok: false, error: t(`sha1 不一致(拿到 ${got})`, `sha1 mismatch (got ${got})`) }
|
|
3378
|
+
return { ok: true, algo: 'sha1', verified: 'shasum' }
|
|
3379
|
+
}
|
|
3380
|
+
const want256 = String(sha256 || '').trim().toLowerCase()
|
|
3381
|
+
if (want256) {
|
|
3382
|
+
const got = createHash('sha256').update(buf).digest('hex')
|
|
3383
|
+
if (got !== want256) return { ok: false, error: t(`sha256 不一致(拿到 ${got})`, `sha256 mismatch (got ${got})`) }
|
|
3384
|
+
return { ok: true, algo: 'sha256', verified: 'sha256' }
|
|
3385
|
+
}
|
|
3386
|
+
return { ok: true, algo: '', verified: 'none' }
|
|
3387
|
+
}
|
|
3388
|
+
|
|
3389
|
+
async function downloadPackage({ urls, sha256, integrity = '', sha1 = '', version, dshHome, token = '' }) {
|
|
3174
3390
|
if (typeof fetch !== 'function') return { ok: false, error: t('当前运行环境没有 fetch,无法下载更新包', 'fetch is unavailable in this runtime — cannot download the update package') }
|
|
3175
3391
|
const dir = join(dshHome, 'update-staging')
|
|
3176
3392
|
try { mkdirSync(dir, { recursive: true }) } catch { }
|
|
3177
3393
|
const file = join(dir, `dsh-profile-config-sync-${version}.tgz`)
|
|
3178
|
-
const want = String(sha256 || '').toLowerCase()
|
|
3179
3394
|
const errors = []
|
|
3180
3395
|
for (const rawUrl of urls) {
|
|
3181
3396
|
const url = String(rawUrl || '').trim()
|
|
@@ -3200,10 +3415,11 @@ async function downloadPackage({ urls, sha256, version, dshHome, token = '' }) {
|
|
|
3200
3415
|
buf = Buffer.from(await res.arrayBuffer())
|
|
3201
3416
|
}
|
|
3202
3417
|
if (!buf.length) throw new Error(t('下载到的文件是空的', 'the downloaded file is empty'))
|
|
3418
|
+
const verdict = verifyDownloadedPackage(buf, { integrity, sha1, sha256 })
|
|
3419
|
+
if (!verdict.ok) throw new Error(String(verdict.error))
|
|
3203
3420
|
const hash = createHash('sha256').update(buf).digest('hex')
|
|
3204
|
-
if (want && hash !== want) throw new Error(t(`sha256 不一致(拿到 ${hash})`, `sha256 mismatch (got ${hash})`))
|
|
3205
3421
|
writeFileSync(file, buf)
|
|
3206
|
-
return { ok: true, file, size: buf.length, sha256: hash, url }
|
|
3422
|
+
return { ok: true, file, size: buf.length, sha256: hash, url, verifiedBy: verdict.verified, verifiedAlgo: verdict.algo }
|
|
3207
3423
|
} catch (error) {
|
|
3208
3424
|
let host = url
|
|
3209
3425
|
try { host = new URL(url).host } catch { }
|
|
@@ -3226,7 +3442,7 @@ async function downloadPackage({ urls, sha256, version, dshHome, token = '' }) {
|
|
|
3226
3442
|
* 备份旧目录(<DshHome>\backups\plugin-<旧版本>-<时间戳>)、覆盖,失败就把备份拷回去。
|
|
3227
3443
|
* 返回 { ok, version, backup, log, error }
|
|
3228
3444
|
*/
|
|
3229
|
-
function runInstallPlugin({ version, sha256, urls, dshHome }) {
|
|
3445
|
+
function runInstallPlugin({ version, sha256, integrity = '', sha1 = '', urls, dshHome }) {
|
|
3230
3446
|
const script = join(PLUGIN_DIR, 'runtime', 'install-plugin.ps1')
|
|
3231
3447
|
if (!isFile(script)) return Promise.resolve({ ok: false, error: t('插件目录里没有 runtime\\install-plugin.ps1', 'runtime\\install-plugin.ps1 is missing from the plugin directory') })
|
|
3232
3448
|
const sysRoot = process.env.SystemRoot || 'C:\\Windows'
|
|
@@ -3236,6 +3452,8 @@ function runInstallPlugin({ version, sha256, urls, dshHome }) {
|
|
|
3236
3452
|
'-PluginDir', PLUGIN_DIR,
|
|
3237
3453
|
'-Version', String(version || ''),
|
|
3238
3454
|
'-Sha256', String(sha256 || ''),
|
|
3455
|
+
...(integrity ? ['-Integrity', String(integrity)] : []),
|
|
3456
|
+
...(sha1 ? ['-Sha1', String(sha1)] : []),
|
|
3239
3457
|
'-TgzUrls', urls.join('|'),
|
|
3240
3458
|
'-DshHome', dshHome,
|
|
3241
3459
|
]
|
|
@@ -3264,37 +3482,33 @@ function runInstallPlugin({ version, sha256, urls, dshHome }) {
|
|
|
3264
3482
|
async function handleUpdate(req, res, config) {
|
|
3265
3483
|
const dshHome = config?.dshHome || process.env.DSH_HOME || join(homedir(), '.dsh')
|
|
3266
3484
|
const settings = readSettings(dshHome)
|
|
3267
|
-
//
|
|
3268
|
-
//
|
|
3269
|
-
|
|
3485
|
+
// 0.6.47:更新源固定是 npm,没有可配置的清单地址,也不需要令牌。
|
|
3486
|
+
// 旧设置里的 updateManifestUrl / updateToken 保留在配置文件里不删(用户自己填的东西不该被悄悄抹掉),
|
|
3487
|
+
// 但更新检查不再读它们 —— 面板上那两栏也一并撤掉。
|
|
3270
3488
|
try {
|
|
3271
3489
|
if (req.method === 'GET') {
|
|
3272
|
-
|
|
3273
|
-
sendJson(res, 200, updatePayload(await checkUpdate({ url: manifestUrl, token: resolvedUpd.token }), settings, resolvedUpd))
|
|
3490
|
+
sendJson(res, 200, updatePayload(await checkUpdate({ settings }), settings, null))
|
|
3274
3491
|
return
|
|
3275
3492
|
}
|
|
3276
3493
|
if (req.method !== 'POST') { sendJson(res, 405, { ok: false, error: 'GET or POST only' }); return }
|
|
3277
3494
|
if (!originAllowed(req)) { sendJson(res, 403, { ok: false, error: 'cross-origin request rejected' }); return }
|
|
3278
|
-
// 先读 body
|
|
3495
|
+
// 先读 body 再解析:读 body 之前不能有 await,否则请求流可能已经被读空/结束,
|
|
3279
3496
|
// 事件监听挂晚了就永远等不到 'end'(实测:把令牌解析提到前面会让所有 POST 一直挂住)。
|
|
3280
3497
|
const raw = await readBody(req)
|
|
3281
3498
|
const body = raw.trim() ? JSON.parse(raw) : {}
|
|
3282
3499
|
const action = String(body.action || 'check')
|
|
3283
|
-
const resolvedUpd = await resolveUpdateToken(settings, manifestUrl)
|
|
3284
|
-
const updateToken = resolvedUpd.token
|
|
3285
3500
|
|
|
3286
3501
|
if (action === 'check') {
|
|
3287
3502
|
clearWinEnvCache()
|
|
3288
|
-
const
|
|
3289
|
-
|
|
3290
|
-
sendJson(res, 200, { ok: !state.error, action, ...updatePayload(state, settings, refreshedUpd) })
|
|
3503
|
+
const state = await checkUpdate({ force: true, settings })
|
|
3504
|
+
sendJson(res, 200, { ok: !state.error, action, ...updatePayload(state, settings, null) })
|
|
3291
3505
|
return
|
|
3292
3506
|
}
|
|
3293
3507
|
|
|
3294
3508
|
// 只下载:把包落到 <DshHome>\update-staging\,校验 sha256,**不动插件目录**。
|
|
3295
3509
|
// 面板点「下载更新」走这条 —— 页面不会因为插件被改写而重置。
|
|
3296
3510
|
if (action === 'prepare') {
|
|
3297
|
-
const state = await checkUpdate({ force: true,
|
|
3511
|
+
const state = await checkUpdate({ force: true, settings })
|
|
3298
3512
|
const remote = state.manifest
|
|
3299
3513
|
if (!remote) throw new Error(t(`读不到更新清单:${state.error || '未知原因'}`, `Cannot read the update manifest: ${state.error || 'unknown reason'}`))
|
|
3300
3514
|
if (compareVersion(remote.version, PLUGIN_VERSION) <= 0) {
|
|
@@ -3303,12 +3517,19 @@ async function handleUpdate(req, res, config) {
|
|
|
3303
3517
|
// 同一个版本已经下好就不重复下(DSH 一直开着时用户可能点第二次)
|
|
3304
3518
|
const prev = updateState.prepared
|
|
3305
3519
|
if (prev && prev.version === String(remote.version) && isFile(prev.file)) {
|
|
3306
|
-
sendJson(res, 200, { ok: true, action, prepared: prev, ...updatePayload(state, settings,
|
|
3520
|
+
sendJson(res, 200, { ok: true, action, prepared: prev, ...updatePayload(state, settings, null) })
|
|
3307
3521
|
return
|
|
3308
3522
|
}
|
|
3309
|
-
const urls = resolveTgzCandidates(remote,
|
|
3310
|
-
if (!urls.length) throw new Error(t('
|
|
3311
|
-
const got = await downloadPackage({
|
|
3523
|
+
const urls = resolveTgzCandidates(remote, effectiveNpmRegistry(settings))
|
|
3524
|
+
if (!urls.length) throw new Error(t('npm 上没有给下载地址', 'npm gave no download URL'))
|
|
3525
|
+
const got = await downloadPackage({
|
|
3526
|
+
urls,
|
|
3527
|
+
integrity: String(remote.integrity || ''),
|
|
3528
|
+
sha1: String(remote.sha1 || ''),
|
|
3529
|
+
sha256: String(remote.sha256 || ''),
|
|
3530
|
+
version: String(remote.version),
|
|
3531
|
+
dshHome,
|
|
3532
|
+
})
|
|
3312
3533
|
if (!got.ok) throw new Error(String(got.error || t('下载失败', 'download failed')))
|
|
3313
3534
|
updateState.prepared = {
|
|
3314
3535
|
version: String(remote.version),
|
|
@@ -3317,8 +3538,12 @@ async function handleUpdate(req, res, config) {
|
|
|
3317
3538
|
size: got.size,
|
|
3318
3539
|
at: Date.now(),
|
|
3319
3540
|
url: got.url,
|
|
3541
|
+
verifiedBy: got.verifiedBy || '',
|
|
3542
|
+
verifiedAlgo: got.verifiedAlgo || '',
|
|
3543
|
+
integrity: String(remote.integrity || ''),
|
|
3544
|
+
sha1: String(remote.sha1 || ''),
|
|
3320
3545
|
}
|
|
3321
|
-
sendJson(res, 200, { ok: true, action, prepared: updateState.prepared, ...updatePayload(state, settings,
|
|
3546
|
+
sendJson(res, 200, { ok: true, action, prepared: updateState.prepared, ...updatePayload(state, settings, null) })
|
|
3322
3547
|
return
|
|
3323
3548
|
}
|
|
3324
3549
|
|
|
@@ -3347,28 +3572,36 @@ async function handleUpdate(req, res, config) {
|
|
|
3347
3572
|
'-UpdateTgz', prepared.file,
|
|
3348
3573
|
'-UpdateVersion', String(prepared.version),
|
|
3349
3574
|
'-UpdateSha256', String(prepared.sha256 || ''),
|
|
3575
|
+
...(prepared.integrity ? ['-UpdateIntegrity', String(prepared.integrity)] : []),
|
|
3350
3576
|
],
|
|
3351
3577
|
})
|
|
3352
3578
|
if (!started.ok) throw new Error(started.error)
|
|
3353
3579
|
autoState.pending = { mode: 'update', at: Date.now(), version: String(prepared.version), restart: true, watcherPid: started.pid, installFrom: prepared.file }
|
|
3354
3580
|
const closed = closeDsh()
|
|
3355
3581
|
autoState.pending.closeRequested = closed
|
|
3356
|
-
sendJson(res, 200, { ok: true, action, started: true, pending: autoState.pending, closeRequested: closed, ...updatePayload(await checkUpdate({ force: false,
|
|
3582
|
+
sendJson(res, 200, { ok: true, action, started: true, pending: autoState.pending, closeRequested: closed, ...updatePayload(await checkUpdate({ force: false, settings }), settings, null) })
|
|
3357
3583
|
return
|
|
3358
3584
|
}
|
|
3359
3585
|
|
|
3360
3586
|
// 运行中直接装(不退出 DSH,**界面已不再使用**,留给 CLI/兼容):下载 → 校验 sha256 → 备份旧版 → 覆盖插件目录
|
|
3361
3587
|
if (action === 'install-live') {
|
|
3362
|
-
const state = await checkUpdate({ force: true,
|
|
3588
|
+
const state = await checkUpdate({ force: true, settings })
|
|
3363
3589
|
const remote = state.manifest
|
|
3364
3590
|
if (!remote) throw new Error(t(`读不到更新清单:${state.error || '未知原因'}`, `Cannot read the update manifest: ${state.error || 'unknown reason'}`))
|
|
3365
3591
|
if (compareVersion(remote.version, PLUGIN_VERSION) <= 0) {
|
|
3366
3592
|
throw new Error(t(`已经是最新版本(${PLUGIN_VERSION}),无需更新`, `Already on the latest version (${PLUGIN_VERSION}) — no update needed`))
|
|
3367
3593
|
}
|
|
3368
|
-
const urls = resolveTgzCandidates(remote,
|
|
3369
|
-
if (!urls.length) throw new Error(t('
|
|
3594
|
+
const urls = resolveTgzCandidates(remote, effectiveNpmRegistry(settings))
|
|
3595
|
+
if (!urls.length) throw new Error(t('npm 上没有给下载地址', 'npm gave no download URL'))
|
|
3370
3596
|
// 包只有一百多 KB,同步做完再回包:前端把按钮置灰等这一下就够了
|
|
3371
|
-
const report = await runInstallPlugin({
|
|
3597
|
+
const report = await runInstallPlugin({
|
|
3598
|
+
version: String(remote.version),
|
|
3599
|
+
sha256: String(remote.sha256 || ''),
|
|
3600
|
+
integrity: String(remote.integrity || ''),
|
|
3601
|
+
sha1: String(remote.sha1 || ''),
|
|
3602
|
+
urls,
|
|
3603
|
+
dshHome,
|
|
3604
|
+
})
|
|
3372
3605
|
if (!report.ok) throw new Error(String(report.error || t('安装失败', 'install failed')))
|
|
3373
3606
|
updateState.liveInstall = {
|
|
3374
3607
|
version: String(remote.version),
|
|
@@ -3378,7 +3611,7 @@ async function handleUpdate(req, res, config) {
|
|
|
3378
3611
|
}
|
|
3379
3612
|
// 注意 hasUpdate 仍是 true:进程里跑的还是旧代码,重启后 PLUGIN_VERSION 才追上来。
|
|
3380
3613
|
// 面板靠 restartRequired 换成「已装好,重启生效」
|
|
3381
|
-
sendJson(res, 200, { ok: true, action, installed: updateState.liveInstall, ...updatePayload(state, settings,
|
|
3614
|
+
sendJson(res, 200, { ok: true, action, installed: updateState.liveInstall, ...updatePayload(state, settings, null) })
|
|
3382
3615
|
return
|
|
3383
3616
|
}
|
|
3384
3617
|
|
|
@@ -3396,14 +3629,14 @@ async function handleUpdate(req, res, config) {
|
|
|
3396
3629
|
}
|
|
3397
3630
|
|
|
3398
3631
|
if (action === 'apply') {
|
|
3399
|
-
const state = await checkUpdate({ force: true,
|
|
3632
|
+
const state = await checkUpdate({ force: true, settings })
|
|
3400
3633
|
const remote = state.manifest
|
|
3401
3634
|
if (!remote) throw new Error(t(`读不到更新清单:${state.error || '未知原因'}`, `Cannot read the update manifest: ${state.error || 'unknown reason'}`))
|
|
3402
3635
|
if (compareVersion(remote.version, PLUGIN_VERSION) <= 0) {
|
|
3403
3636
|
throw new Error(t(`已经是最新版本(${PLUGIN_VERSION}),无需更新`, `Already on the latest version (${PLUGIN_VERSION}) — no update needed`))
|
|
3404
3637
|
}
|
|
3405
|
-
const tgz = resolveTgzUrl(remote,
|
|
3406
|
-
if (!tgz) throw new Error(t('
|
|
3638
|
+
const tgz = resolveTgzUrl(remote, effectiveNpmRegistry(settings))
|
|
3639
|
+
if (!tgz) throw new Error(t('npm 上没有给下载地址', 'npm gave no download URL'))
|
|
3407
3640
|
const notes = String((langStore.getStore() === 'en' ? (remote.notesEn || remote.notes) : remote.notes) || '').trim()
|
|
3408
3641
|
const yes = await askConfirm(
|
|
3409
3642
|
t('更新「配置备份与同步」插件', 'Update the Config Backup & Sync plugin'),
|
|
@@ -3416,7 +3649,7 @@ async function handleUpdate(req, res, config) {
|
|
|
3416
3649
|
+ '\n\nSteps: exit DSH → download and verify the package → back up the old version → install over it → relaunch automatically.',
|
|
3417
3650
|
),
|
|
3418
3651
|
)
|
|
3419
|
-
if (!yes) { sendJson(res, 200, { ok: true, action, cancelled: true, ...updatePayload(state, settings,
|
|
3652
|
+
if (!yes) { sendJson(res, 200, { ok: true, action, cancelled: true, ...updatePayload(state, settings, null) }); return }
|
|
3420
3653
|
|
|
3421
3654
|
const repo = resolveRepo(config?.repoPath, dshHome).repo || ''
|
|
3422
3655
|
const appRoot = resolveAppRoot(config?.appRoot) || ''
|
|
@@ -3432,7 +3665,8 @@ async function handleUpdate(req, res, config) {
|
|
|
3432
3665
|
'-UpdateVersion', String(remote.version),
|
|
3433
3666
|
'-UpdateTgz', tgz,
|
|
3434
3667
|
'-UpdateSha256', String(remote.sha256 || ''),
|
|
3435
|
-
...(
|
|
3668
|
+
...(remote.integrity ? ['-UpdateIntegrity', String(remote.integrity)] : []),
|
|
3669
|
+
...(remote.sha1 ? ['-UpdateSha1', String(remote.sha1)] : []),
|
|
3436
3670
|
],
|
|
3437
3671
|
})
|
|
3438
3672
|
if (!started.ok) throw new Error(started.error)
|