@twinsearth/dsh-fang 3.1.21 → 3.1.22
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/LICENSES/LICENSES/cordis-MIT.txt +39 -0
- package/client.js +197 -3
- package/defaults/defaults/umbrella-rules.yaml +113 -0
- package/package.json +2 -2
- package/sdk/sdk/README.md +65 -0
- package/sdk/sdk/examples/status-badge/README.md +16 -0
- package/sdk/sdk/examples/status-badge/client.js +60 -0
- package/sdk/sdk/examples/status-badge/cordis.patch.yml +7 -0
- package/sdk/sdk/examples/status-badge/index.js +12 -0
- package/sdk/sdk/examples/status-badge/package.json +24 -0
- package/sdk/sdk/templates/plugin/README.md +36 -0
- package/sdk/sdk/templates/plugin/client.js +35 -0
- package/sdk/sdk/templates/plugin/cordis.patch.yml +8 -0
- package/sdk/sdk/templates/plugin/icon.svg +5 -0
- package/sdk/sdk/templates/plugin/index.js +23 -0
- package/sdk/sdk/templates/plugin/locale/en.json +6 -0
- package/sdk/sdk/templates/plugin/package.json +36 -0
- package/src/kernel/types.mjs +1 -1
- package/src/src/compose.mjs +67 -0
- package/src/src/contract/client.js +86 -0
- package/src/src/contract/constants.js +79 -0
- package/src/src/contract/envelope.js +83 -0
- package/src/src/contract/index.js +19 -0
- package/src/src/contract/negotiate.js +106 -0
- package/src/src/contract/server.js +77 -0
- package/src/src/contract/transports.js +301 -0
- package/src/src/index.mjs +11 -0
- package/src/src/kernel/core.mjs +100 -0
- package/src/src/kernel/extensions.mjs +61 -0
- package/src/src/kernel/fsx.mjs +349 -0
- package/src/src/kernel/index.mjs +21 -0
- package/src/src/kernel/ledger.mjs +94 -0
- package/src/src/kernel/paths.mjs +182 -0
- package/src/src/kernel/role.mjs +68 -0
- package/src/src/kernel/rounds.mjs +172 -0
- package/src/src/kernel/schema.mjs +5 -0
- package/src/src/kernel/types.mjs +264 -0
- package/src/src/kernel/vendor/cosmokit.mjs +421 -0
- package/src/src/kernel/vendor/schemastery.mjs +638 -0
- package/src/src/lib/adapter-client.mjs +383 -0
- package/src/src/lib/adapter-protocol.mjs +243 -0
- package/src/src/lib/ai-secret-store.mjs +202 -0
- package/src/src/lib/boot-guard.mjs +182 -0
- package/src/src/lib/branch-store.mjs +84 -0
- package/src/src/lib/branch.mjs +74 -0
- package/src/src/lib/bridge-client.mjs +133 -0
- package/src/src/lib/broker-adapter.mjs +67 -0
- package/src/src/lib/broker.mjs +45 -0
- package/src/src/lib/business-producers.mjs +109 -0
- package/src/src/lib/command-cache.mjs +209 -0
- package/src/src/lib/compat.mjs +170 -0
- package/src/src/lib/composer-journal.mjs +128 -0
- package/src/src/lib/container/index.mjs +806 -0
- package/src/src/lib/decision-events.mjs +129 -0
- package/src/src/lib/decision-ingest-client.mjs +41 -0
- package/src/src/lib/decision-outbox.mjs +129 -0
- package/src/src/lib/default-essence.mjs +61 -0
- package/src/src/lib/desktop-launch.mjs +138 -0
- package/src/src/lib/essence.mjs +1211 -0
- package/src/src/lib/fork-client.mjs +410 -0
- package/src/src/lib/fork-config-lock.mjs +153 -0
- package/src/src/lib/fork-seed.mjs +304 -0
- package/src/src/lib/fork-supervision-settings.mjs +58 -0
- package/src/src/lib/fork-supervisor.mjs +122 -0
- package/src/src/lib/fs-guard.mjs +28 -0
- package/src/src/lib/http-auth.mjs +162 -0
- package/src/src/lib/idle-audit.mjs +178 -0
- package/src/src/lib/interaction-poke.mjs +61 -0
- package/src/src/lib/mirror-etag.mjs +92 -0
- package/src/src/lib/mirror-official-source.mjs +93 -0
- package/src/src/lib/mirror.mjs +478 -0
- package/src/src/lib/mother-link.mjs +156 -0
- package/src/src/lib/mutex.mjs +31 -0
- package/src/src/lib/permissions.mjs +115 -0
- package/src/src/lib/pnpm-store.mjs +189 -0
- package/src/src/lib/preferences.mjs +212 -0
- package/src/src/lib/producer-contract.mjs +162 -0
- package/src/src/lib/reflow.mjs +41 -0
- package/src/src/lib/remote.mjs +408 -0
- package/src/src/lib/restart-contract.mjs +650 -0
- package/src/src/lib/roots.mjs +30 -0
- package/src/src/lib/rule-registry.mjs +1101 -0
- package/src/src/lib/runtime-capture.mjs +307 -0
- package/src/src/lib/semaphore.mjs +34 -0
- package/src/src/lib/session-bundle.mjs +138 -0
- package/src/src/lib/session-format.mjs +658 -0
- package/src/src/lib/sse-parser.mjs +102 -0
- package/src/src/lib/ssrf-guard.mjs +170 -0
- package/src/src/lib/status-store.mjs +257 -0
- package/src/src/lib/task-handle.mjs +622 -0
- package/src/src/lib/ui-actions.mjs +55 -0
- package/src/src/lib/umbrella.mjs +1661 -0
- package/src/src/lib/upgrade-coordinator.mjs +107 -0
- package/src/src/lib/upgrade-status.mjs +43 -0
- package/src/src/plugins/adapter.mjs +1468 -0
- package/src/src/plugins/approver.mjs +1210 -0
- package/src/src/plugins/auditor.mjs +330 -0
- package/src/src/plugins/buffer-keeper.mjs +522 -0
- package/src/src/plugins/decision-center.mjs +2265 -0
- package/src/src/plugins/forker.mjs +1594 -0
- package/src/src/plugins/healer.mjs +664 -0
- package/src/src/plugins/hot-reloader.mjs +274 -0
- package/src/src/plugins/imports.mjs +247 -0
- package/src/src/plugins/orchestrator.mjs +3806 -0
- package/src/src/plugins/restart-guard.mjs +948 -0
- package/src/src/plugins/router.mjs +714 -0
- package/src/src/plugins/scorer.mjs +624 -0
- package/src/src/plugins/session-bridge.mjs +166 -0
- package/src/src/plugins/session-catalog.mjs +771 -0
- package/src/src/plugins/session-router.mjs +125 -0
- package/src/src/plugins/session-sync.mjs +171 -0
- package/src/src/plugins/umbrella.mjs +419 -0
- package/tools/recover-queue.mjs +133 -0
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// Copyright (c) 2026 dsh-Fang contributors — see LICENSE-CORE.
|
|
3
|
+
// dsh-Fang v2.11.0 — 母体镜像管理(零依赖)。
|
|
4
|
+
//
|
|
5
|
+
// 目录(文件夹 1):
|
|
6
|
+
// <mirrorRoot>/
|
|
7
|
+
// active/ 正在运行的镜像内容(唯一可被升级/回退替换的层)
|
|
8
|
+
// backup/<id>/ 自动备份(按 backupIntervalHours 滚动)
|
|
9
|
+
// restore/<id>/ 手动恢复点
|
|
10
|
+
// backups.json 备份索引(id/label/createdAt/sizeBytes/version/kind/bucket)
|
|
11
|
+
// meta.json version/installedAt/backupIntervalHours/lastBackupAt/nextBackupAt
|
|
12
|
+
// mirror-locked.json 锁定插件清单(由 EssenceManager 读取;可选)
|
|
13
|
+
//
|
|
14
|
+
// 语义:
|
|
15
|
+
// * 备份 = 复制 active/ 到 backup|restore/<id>/,索引与 meta 同步;
|
|
16
|
+
// * 恢复 = 先自动备份当前 active/,再用快照替换 active/,最后更新 meta.version;
|
|
17
|
+
// * 升级 = 官方源取最新版 → 自动备份 → 下载到 staging → 原子替换 → 失败自动回退;
|
|
18
|
+
// 未配置官方源时 upgrade 显式返回 official-source-unavailable(绝不假装成功)。
|
|
19
|
+
|
|
20
|
+
import fs from 'node:fs'
|
|
21
|
+
import path from 'node:path'
|
|
22
|
+
|
|
23
|
+
import {
|
|
24
|
+
ensureDir,
|
|
25
|
+
exists,
|
|
26
|
+
readJson,
|
|
27
|
+
writeJsonAtomic,
|
|
28
|
+
sha256,
|
|
29
|
+
copyTree,
|
|
30
|
+
removeTree,
|
|
31
|
+
listFiles,
|
|
32
|
+
} from '../kernel/index.mjs'
|
|
33
|
+
|
|
34
|
+
export const MIRROR_VERSION = '1.0.0'
|
|
35
|
+
|
|
36
|
+
export const BACKUP_INTERVAL_HOURS = Object.freeze([6, 12, 24])
|
|
37
|
+
export const DEFAULT_BACKUP_INTERVAL_HOURS = 24
|
|
38
|
+
export const BACKUP_BUCKET = Object.freeze({ AUTO: 'backup', MANUAL: 'restore' })
|
|
39
|
+
export const UPGRADE_STAGE = Object.freeze({
|
|
40
|
+
CHECKING: 'checking', BACKING_UP: 'backing-up', DOWNLOADING: 'downloading',
|
|
41
|
+
SWAPPING: 'swapping', FINALIZING: 'finalizing', DONE: 'done', FAILED: 'failed',
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
const HOUR_MS = 3_600_000
|
|
45
|
+
const BACKUP_ID_RE = /^\d{10,16}-[a-z0-9]{4,12}$/
|
|
46
|
+
|
|
47
|
+
/** @param {unknown} value */
|
|
48
|
+
function normalizeIntervalHours(value) {
|
|
49
|
+
const hours = Number(value)
|
|
50
|
+
return BACKUP_INTERVAL_HOURS.includes(hours) ? hours : DEFAULT_BACKUP_INTERVAL_HOURS
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** @param {number} bytes */
|
|
54
|
+
function directorySize(bytes) {
|
|
55
|
+
return Number.isFinite(bytes) && bytes > 0 ? bytes : 0
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 计算目录大小(递归)。
|
|
60
|
+
* @param {string} dir
|
|
61
|
+
* @returns {number}
|
|
62
|
+
*/
|
|
63
|
+
function dirSize(dir) {
|
|
64
|
+
if (!exists(dir)) return 0
|
|
65
|
+
let total = 0
|
|
66
|
+
for (const relative of listFiles(dir, { max: 200000 })) {
|
|
67
|
+
try { total += fs.statSync(path.join(dir, relative)).size } catch { /* 文件在遍历中被删:忽略 */ }
|
|
68
|
+
}
|
|
69
|
+
return total
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 递归复制目录(不跟随符号链接;目标必须不存在或为空)。
|
|
74
|
+
* @param {string} from @param {string} to
|
|
75
|
+
*/
|
|
76
|
+
function copyDir(from, to) {
|
|
77
|
+
if (!exists(from)) throw new Error(`mirror: source not found: ${from}`)
|
|
78
|
+
ensureDir(to)
|
|
79
|
+
copyTree(from, to)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export class MirrorManager {
|
|
83
|
+
/**
|
|
84
|
+
* @param {{mirrorRoot: string, officialSource?: {getLatestVersion:Function, download:Function}|null,
|
|
85
|
+
* logger?: {info?:Function, warn?:Function}, now?: () => number,
|
|
86
|
+
* usageProvider?: () => {activeEssenceCount?: number, activeSessionCount?: number}}} options
|
|
87
|
+
*/
|
|
88
|
+
constructor(options) {
|
|
89
|
+
this.mirrorRoot = String(options.mirrorRoot ?? '')
|
|
90
|
+
if (this.mirrorRoot.length === 0) throw new Error('MirrorManager: mirrorRoot required')
|
|
91
|
+
this.officialSource = options.officialSource ?? null
|
|
92
|
+
this.logger = options.logger ?? {}
|
|
93
|
+
this.now = typeof options.now === 'function' ? options.now : () => Date.now()
|
|
94
|
+
this.usageProvider = typeof options.usageProvider === 'function' ? options.usageProvider : null
|
|
95
|
+
// v3.0.9:升级成功后的副作用钩子(由协调器注入;异步触发,不阻塞 upgrade 返回)
|
|
96
|
+
this.postUpgradeHook = typeof options.postUpgradeHook === 'function' ? options.postUpgradeHook : null
|
|
97
|
+
// v3.0.3:客户端升级后的兼容性门禁;失败时保持子插件关闭(默认策略)
|
|
98
|
+
this.compatGate = typeof options.compatGate === 'function' ? options.compatGate : null
|
|
99
|
+
this.onCompatResult = typeof options.onCompatResult === 'function' ? options.onCompatResult : null
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
activeDir() { return path.join(this.mirrorRoot, 'active') }
|
|
103
|
+
backupDir() { return path.join(this.mirrorRoot, 'backup') }
|
|
104
|
+
restoreDir() { return path.join(this.mirrorRoot, 'restore') }
|
|
105
|
+
metaPath() { return path.join(this.mirrorRoot, 'meta.json') }
|
|
106
|
+
indexPath() { return path.join(this.mirrorRoot, 'backups.json') }
|
|
107
|
+
|
|
108
|
+
/** 初始化目录与 meta(幂等;不覆盖已有 meta)。 */
|
|
109
|
+
ensureLayout() {
|
|
110
|
+
ensureDir(this.activeDir())
|
|
111
|
+
ensureDir(this.backupDir())
|
|
112
|
+
ensureDir(this.restoreDir())
|
|
113
|
+
if (!exists(this.metaPath())) {
|
|
114
|
+
this.writeMeta({
|
|
115
|
+
version: this.detectActiveVersion(),
|
|
116
|
+
installedAt: this.now(),
|
|
117
|
+
backupIntervalHours: DEFAULT_BACKUP_INTERVAL_HOURS,
|
|
118
|
+
lastBackupAt: null,
|
|
119
|
+
nextBackupAt: null,
|
|
120
|
+
})
|
|
121
|
+
}
|
|
122
|
+
if (!exists(this.indexPath())) writeJsonAtomic(this.indexPath(), [])
|
|
123
|
+
return this.getStatus()
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** @returns {{version:string, installedAt:number, backupIntervalHours:number, lastBackupAt:number|null, nextBackupAt:number|null}} */
|
|
127
|
+
readMeta() {
|
|
128
|
+
const raw = readJson(this.metaPath(), {})
|
|
129
|
+
return {
|
|
130
|
+
version: String(raw.version ?? 'unknown'),
|
|
131
|
+
installedAt: Number(raw.installedAt) || 0,
|
|
132
|
+
backupIntervalHours: normalizeIntervalHours(raw.backupIntervalHours),
|
|
133
|
+
lastBackupAt: raw.lastBackupAt === null || raw.lastBackupAt === undefined ? null : Number(raw.lastBackupAt),
|
|
134
|
+
nextBackupAt: raw.nextBackupAt === null || raw.nextBackupAt === undefined ? null : Number(raw.nextBackupAt),
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** @param {ReturnType<MirrorManager['readMeta']>} meta */
|
|
139
|
+
writeMeta(meta) {
|
|
140
|
+
ensureDir(this.mirrorRoot)
|
|
141
|
+
writeJsonAtomic(this.metaPath(), meta)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** @returns {Array<{id:string,label:string|null,createdAt:number,sizeBytes:number,version:string,kind:string,bucket:string}>} */
|
|
145
|
+
listBackups() {
|
|
146
|
+
const list = readJson(this.indexPath(), [])
|
|
147
|
+
if (!Array.isArray(list)) return []
|
|
148
|
+
return list
|
|
149
|
+
.filter(entry => entry !== null && typeof entry === 'object')
|
|
150
|
+
.map(entry => ({
|
|
151
|
+
id: String(entry.id ?? ''),
|
|
152
|
+
label: entry.label === undefined ? null : entry.label,
|
|
153
|
+
createdAt: Number(entry.createdAt) || 0,
|
|
154
|
+
sizeBytes: directorySize(Number(entry.sizeBytes) || 0),
|
|
155
|
+
version: String(entry.version ?? 'unknown'),
|
|
156
|
+
kind: entry.kind === 'manual' ? 'manual' : 'auto',
|
|
157
|
+
bucket: entry.bucket === BACKUP_BUCKET.MANUAL ? BACKUP_BUCKET.MANUAL : BACKUP_BUCKET.AUTO,
|
|
158
|
+
}))
|
|
159
|
+
.filter(entry => entry.id.length > 0)
|
|
160
|
+
.sort((a, b) => b.createdAt - a.createdAt)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** 当前镜像版本(无法判断时 unknown)。 */
|
|
164
|
+
detectActiveVersion() {
|
|
165
|
+
const version = readJson(path.join(this.activeDir(), 'version.json'), null)
|
|
166
|
+
if (version !== null && typeof version === 'object' && typeof version.version === 'string') return version.version
|
|
167
|
+
try { return String(readJson(this.metaPath(), {}).version ?? 'unknown') } catch { return 'unknown' }
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* 完整状态(供 UI / 路由)。
|
|
172
|
+
* @returns {Record<string, unknown>}
|
|
173
|
+
*/
|
|
174
|
+
getStatus() {
|
|
175
|
+
this.ensureLayoutWithoutRecursion()
|
|
176
|
+
const meta = this.readMeta()
|
|
177
|
+
let latest = null
|
|
178
|
+
if (this.officialSource !== null && typeof this.officialSource.getLatestVersion === 'function') {
|
|
179
|
+
try { latest = String(this.officialSource.getLatestVersion() ?? null) } catch { latest = null }
|
|
180
|
+
}
|
|
181
|
+
const usage = this.usageProvider === null ? {} : (this.usageProvider() ?? {})
|
|
182
|
+
return {
|
|
183
|
+
ok: true,
|
|
184
|
+
meta,
|
|
185
|
+
currentVersion: meta.version,
|
|
186
|
+
officialLatest: latest,
|
|
187
|
+
updateAvailable: latest !== null && latest !== meta.version,
|
|
188
|
+
backups: this.listBackups(),
|
|
189
|
+
activeEssenceCount: Number(usage.activeEssenceCount ?? 0) || 0,
|
|
190
|
+
activeSessionCount: Number(usage.activeSessionCount ?? 0) || 0,
|
|
191
|
+
diskUsageBytes: dirSize(this.mirrorRoot),
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** getStatus 内部调用;只补齐目录,避免递归。 */
|
|
196
|
+
ensureLayoutWithoutRecursion() {
|
|
197
|
+
ensureDir(this.backupDir())
|
|
198
|
+
ensureDir(this.restoreDir())
|
|
199
|
+
if (!exists(this.indexPath())) writeJsonAtomic(this.indexPath(), [])
|
|
200
|
+
if (!exists(this.metaPath())) {
|
|
201
|
+
this.writeMeta({
|
|
202
|
+
version: this.detectActiveVersion(), installedAt: this.now(),
|
|
203
|
+
backupIntervalHours: DEFAULT_BACKUP_INTERVAL_HOURS, lastBackupAt: null, nextBackupAt: null,
|
|
204
|
+
})
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* 备份当前 active 到 backup|restore/<id>/。
|
|
210
|
+
* @param {{label?: string, bucket?: 'backup'|'restore'}} [options]
|
|
211
|
+
* @returns {Record<string, unknown>} 备份条目
|
|
212
|
+
*/
|
|
213
|
+
backup(options = {}) {
|
|
214
|
+
this.ensureLayout()
|
|
215
|
+
const bucket = options.bucket === BACKUP_BUCKET.MANUAL || options.bucket === 'restore'
|
|
216
|
+
? BACKUP_BUCKET.MANUAL
|
|
217
|
+
: BACKUP_BUCKET.AUTO
|
|
218
|
+
const meta = this.readMeta()
|
|
219
|
+
const createdAt = this.now()
|
|
220
|
+
const id = `${String(createdAt).padStart(13, '0')}-${sha256(`${createdAt}:${this.mirrorRoot}:${Math.random()}`).slice(0, 8)}`
|
|
221
|
+
const target = path.join(bucket === BACKUP_BUCKET.MANUAL ? this.restoreDir() : this.backupDir(), id)
|
|
222
|
+
if (exists(target)) throw new Error(`mirror: backup id collision: ${id}`)
|
|
223
|
+
copyDir(this.activeDir(), target)
|
|
224
|
+
const entry = {
|
|
225
|
+
id,
|
|
226
|
+
label: typeof options.label === 'string' && options.label.trim().length > 0 ? options.label.trim() : null,
|
|
227
|
+
createdAt,
|
|
228
|
+
sizeBytes: dirSize(target),
|
|
229
|
+
version: meta.version,
|
|
230
|
+
kind: bucket === BACKUP_BUCKET.MANUAL ? 'manual' : 'auto',
|
|
231
|
+
bucket,
|
|
232
|
+
}
|
|
233
|
+
const index = this.listBackups()
|
|
234
|
+
index.unshift(entry)
|
|
235
|
+
writeJsonAtomic(this.indexPath(), index)
|
|
236
|
+
this.writeMeta({
|
|
237
|
+
...meta,
|
|
238
|
+
lastBackupAt: createdAt,
|
|
239
|
+
nextBackupAt: createdAt + meta.backupIntervalHours * HOUR_MS,
|
|
240
|
+
})
|
|
241
|
+
this.logger?.info?.(`[mirror] backup ${bucket} ${id} (${entry.sizeBytes} bytes)`)
|
|
242
|
+
return entry
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* 恢复某个备份:默认先自动备份当前 active,再替换,更新 meta.version。
|
|
247
|
+
* @param {string} backupId
|
|
248
|
+
* @returns {{ok:boolean, from?:string, to?:string, preBackupId?:string, reason?:string}}
|
|
249
|
+
*/
|
|
250
|
+
restore(backupId) {
|
|
251
|
+
return this.restoreSnapshot(backupId, { preBackup: true })
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* 恢复实现;升级失败时的内部回滚不再额外备份(升级前备份已存在)。
|
|
256
|
+
* @param {string} backupId
|
|
257
|
+
* @param {{preBackup?: boolean}} [options]
|
|
258
|
+
* @returns {{ok:boolean, from?:string, to?:string, preBackupId?:string, reason?:string}}
|
|
259
|
+
*/
|
|
260
|
+
restoreSnapshot(backupId, options = {}) {
|
|
261
|
+
const preBackup = options.preBackup !== false
|
|
262
|
+
this.ensureLayout()
|
|
263
|
+
const id = this.assertBackupId(backupId)
|
|
264
|
+
const entry = this.listBackups().find(item => item.id === id)
|
|
265
|
+
if (entry === undefined) return { ok: false, reason: 'backup-not-found' }
|
|
266
|
+
const source = path.join(entry.bucket === BACKUP_BUCKET.MANUAL ? this.restoreDir() : this.backupDir(), id)
|
|
267
|
+
if (!exists(source)) return { ok: false, reason: 'backup-content-missing', backupId: id }
|
|
268
|
+
const meta = this.readMeta()
|
|
269
|
+
const pre = preBackup ? this.backup({ label: `pre-restore-${id}`, bucket: BACKUP_BUCKET.AUTO }) : null
|
|
270
|
+
try {
|
|
271
|
+
this.swapActive(source)
|
|
272
|
+
this.writeMeta({ ...meta, version: entry.version, installedAt: this.now(), lastBackupAt: this.now() })
|
|
273
|
+
this.logger?.info?.(`[mirror] restored ${id} (${meta.version} → ${entry.version})`)
|
|
274
|
+
return {
|
|
275
|
+
ok: true, from: meta.version, to: entry.version,
|
|
276
|
+
...(pre === null ? {} : { preBackupId: pre.id }),
|
|
277
|
+
}
|
|
278
|
+
} catch (error) {
|
|
279
|
+
return {
|
|
280
|
+
ok: false, reason: String(error instanceof Error ? error.message : error),
|
|
281
|
+
backupId: id, ...(pre === null ? {} : { preBackupId: pre.id }),
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** @param {string} backupId @returns {{ok:boolean, removed?:boolean, reason?:string}} */
|
|
287
|
+
deleteBackup(backupId) {
|
|
288
|
+
const id = this.assertBackupId(backupId)
|
|
289
|
+
const entry = this.listBackups().find(item => item.id === id)
|
|
290
|
+
if (entry === undefined) return { ok: false, reason: 'backup-not-found' }
|
|
291
|
+
removeTree(path.join(entry.bucket === BACKUP_BUCKET.MANUAL ? this.restoreDir() : this.backupDir(), id))
|
|
292
|
+
writeJsonAtomic(this.indexPath(), this.listBackups().filter(item => item.id !== id))
|
|
293
|
+
return { ok: true, removed: true }
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/** @param {number} hours 6 | 12 | 24 */
|
|
297
|
+
setBackupInterval(hours) {
|
|
298
|
+
const next = Number(hours)
|
|
299
|
+
if (!BACKUP_INTERVAL_HOURS.includes(next)) {
|
|
300
|
+
return { ok: false, reason: `invalid interval: ${hours} (allowed: ${BACKUP_INTERVAL_HOURS.join(',')})` }
|
|
301
|
+
}
|
|
302
|
+
this.ensureLayout()
|
|
303
|
+
const meta = this.readMeta()
|
|
304
|
+
const last = meta.lastBackupAt ?? this.now()
|
|
305
|
+
this.writeMeta({ ...meta, backupIntervalHours: next, nextBackupAt: last + next * HOUR_MS })
|
|
306
|
+
return { ok: true, backupIntervalHours: next }
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** 到期自动备份(由 orchestrator 的定时器调用;now 可注入便于测试)。 */
|
|
310
|
+
maybeAutoBackup(now = this.now()) {
|
|
311
|
+
this.ensureLayoutWithoutRecursion()
|
|
312
|
+
const meta = this.readMeta()
|
|
313
|
+
if (meta.nextBackupAt === null) {
|
|
314
|
+
const last = meta.lastBackupAt ?? now
|
|
315
|
+
this.writeMeta({ ...meta, nextBackupAt: last + meta.backupIntervalHours * HOUR_MS })
|
|
316
|
+
return { ok: true, backedUp: false, reason: 'scheduled-first' }
|
|
317
|
+
}
|
|
318
|
+
if (now < meta.nextBackupAt) return { ok: true, backedUp: false, reason: 'not-due' }
|
|
319
|
+
try {
|
|
320
|
+
const entry = this.backup({ bucket: BACKUP_BUCKET.AUTO })
|
|
321
|
+
return { ok: true, backedUp: true, entry }
|
|
322
|
+
} catch (error) {
|
|
323
|
+
this.logger?.warn?.(`[mirror] auto backup failed: ${error instanceof Error ? error.message : error}`)
|
|
324
|
+
return { ok: false, backedUp: false, reason: String(error instanceof Error ? error.message : error) }
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/** 检查更新(仅查询官方源,不下发下载)。 */
|
|
329
|
+
checkUpdate() {
|
|
330
|
+
this.ensureLayoutWithoutRecursion()
|
|
331
|
+
const current = this.readMeta().version
|
|
332
|
+
if (this.officialSource === null) return { ok: false, current, latest: null, available: false, reason: 'official-source-unavailable' }
|
|
333
|
+
const settle = (latest) => ({ ok: true, current, latest: String(latest ?? null), available: latest !== null && latest !== current })
|
|
334
|
+
try {
|
|
335
|
+
const latest = this.officialSource.getLatestVersion()
|
|
336
|
+
if (latest && typeof latest.then === 'function') {
|
|
337
|
+
return latest.then(settle).catch((error) => ({
|
|
338
|
+
ok: false, current, latest: null, available: false,
|
|
339
|
+
reason: String(error instanceof Error ? error.message : error),
|
|
340
|
+
}))
|
|
341
|
+
}
|
|
342
|
+
return settle(latest)
|
|
343
|
+
} catch (error) {
|
|
344
|
+
return { ok: false, current, latest: null, available: false, reason: String(error instanceof Error ? error.message : error) }
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* 升级:备份 → 下载 staging → 原子替换 → 写 meta;失败自动回退。
|
|
350
|
+
* @param {{autoBackup?: boolean, autoRollback?: boolean, onProgress?: (stage: string) => void}} [options]
|
|
351
|
+
* @returns {Promise<Record<string, unknown>>}
|
|
352
|
+
*/
|
|
353
|
+
async upgrade(options = {}) {
|
|
354
|
+
const autoBackup = options.autoBackup !== false
|
|
355
|
+
const autoRollback = options.autoRollback !== false
|
|
356
|
+
const progress = typeof options.onProgress === 'function' ? options.onProgress : null
|
|
357
|
+
const stage = (name) => { try { progress?.(name) } catch { /* 观察者异常不影响升级 */ } }
|
|
358
|
+
|
|
359
|
+
this.ensureLayout()
|
|
360
|
+
const from = this.readMeta().version
|
|
361
|
+
if (this.officialSource === null || typeof this.officialSource.download !== 'function') {
|
|
362
|
+
return { ok: false, from, to: from, backupId: null, rolledBack: false, reason: 'official-source-unavailable' }
|
|
363
|
+
}
|
|
364
|
+
stage(UPGRADE_STAGE.CHECKING)
|
|
365
|
+
let latest
|
|
366
|
+
try { latest = String((await Promise.resolve(this.officialSource.getLatestVersion())) ?? '') } catch (error) {
|
|
367
|
+
return { ok: false, from, to: from, backupId: null, rolledBack: false, reason: `fetch-latest-failed: ${error instanceof Error ? error.message : error}` }
|
|
368
|
+
}
|
|
369
|
+
if (latest.length === 0) return { ok: false, from, to: from, backupId: null, rolledBack: false, reason: 'latest-version-unknown' }
|
|
370
|
+
if (latest === from) return { ok: true, from, to: from, backupId: null, rolledBack: false, reason: 'already-latest' }
|
|
371
|
+
|
|
372
|
+
let backupId = null
|
|
373
|
+
if (autoBackup) {
|
|
374
|
+
stage(UPGRADE_STAGE.BACKING_UP)
|
|
375
|
+
backupId = this.backup({ label: `pre-upgrade-${latest}`, bucket: BACKUP_BUCKET.AUTO }).id
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
const staging = path.join(this.mirrorRoot, `.staging-${this.now()}-${sha256(`${latest}:${Math.random()}`).slice(0, 6)}`)
|
|
379
|
+
try {
|
|
380
|
+
stage(UPGRADE_STAGE.DOWNLOADING)
|
|
381
|
+
await Promise.resolve(this.officialSource.download(latest, staging))
|
|
382
|
+
stage(UPGRADE_STAGE.SWAPPING)
|
|
383
|
+
this.swapActive(staging)
|
|
384
|
+
stage(UPGRADE_STAGE.FINALIZING)
|
|
385
|
+
this.writeMeta({
|
|
386
|
+
...this.readMeta(), version: latest, installedAt: this.now(),
|
|
387
|
+
lastBackupAt: backupId === null ? this.readMeta().lastBackupAt : this.now(),
|
|
388
|
+
})
|
|
389
|
+
writeJsonAtomic(path.join(this.activeDir(), 'version.json'), { version: latest })
|
|
390
|
+
// v3.0.3:客户端升级后先跑 compat 门禁;未通过则保持子插件关闭并跳过重算
|
|
391
|
+
let compat = null
|
|
392
|
+
if (this.compatGate !== null) {
|
|
393
|
+
try {
|
|
394
|
+
compat = await this.compatGate({ from, to: latest, backupId })
|
|
395
|
+
} catch (error) {
|
|
396
|
+
compat = {
|
|
397
|
+
ok: false,
|
|
398
|
+
checkedAt: Date.now(),
|
|
399
|
+
findings: [{ code: 'compat-gate-threw', severity: 'error', message: String(error instanceof Error ? error.message : error) }],
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
try { this.onCompatResult?.(compat, { from, to: latest }) } catch { /* 观察者异常隔离 */ }
|
|
403
|
+
if (compat === null || compat.ok !== true) {
|
|
404
|
+
stage(UPGRADE_STAGE.FAILED)
|
|
405
|
+
this.logger?.warn?.('[mirror] compat gate failed; sub-plugins stay disabled', {
|
|
406
|
+
findings: Array.isArray(compat?.findings) ? compat.findings.length : 0,
|
|
407
|
+
})
|
|
408
|
+
return {
|
|
409
|
+
ok: true, from, to: latest, backupId, rolledBack: false,
|
|
410
|
+
reason: 'compat-gate-failed', compat, postUpgradeTriggered: false,
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
stage(UPGRADE_STAGE.DONE)
|
|
415
|
+
this.logger?.info?.(`[mirror] upgraded ${from} → ${latest}`)
|
|
416
|
+
const result = { ok: true, from, to: latest, backupId, rolledBack: false }
|
|
417
|
+
if (compat !== null) result.compat = compat
|
|
418
|
+
if (this.postUpgradeHook !== null) {
|
|
419
|
+
result.postUpgradeTriggered = true
|
|
420
|
+
void this.runPostUpgrade({ from, to: latest, backupId })
|
|
421
|
+
}
|
|
422
|
+
return result
|
|
423
|
+
} catch (error) {
|
|
424
|
+
const reason = String(error instanceof Error ? error.message : error)
|
|
425
|
+
this.logger?.warn?.(`[mirror] upgrade failed: ${reason}`)
|
|
426
|
+
if (autoRollback && backupId !== null) {
|
|
427
|
+
const rollback = this.restoreSnapshot(backupId, { preBackup: false })
|
|
428
|
+
stage(UPGRADE_STAGE.FAILED)
|
|
429
|
+
return {
|
|
430
|
+
ok: false, from, to: from, backupId, rolledBack: rollback.ok === true,
|
|
431
|
+
reason: rollback.ok === true ? reason : `${reason}; rollback failed: ${rollback.reason}`,
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
stage(UPGRADE_STAGE.FAILED)
|
|
435
|
+
return { ok: false, from, to: from, backupId, rolledBack: false, reason }
|
|
436
|
+
} finally {
|
|
437
|
+
if (exists(staging)) removeTree(staging)
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* 触发升级后副作用(fire-and-forget):钩子失败只记 warn,不影响升级结果。
|
|
443
|
+
* @param {{from: string, to: string, backupId: string|null}} input
|
|
444
|
+
*/
|
|
445
|
+
async runPostUpgrade(input) {
|
|
446
|
+
try {
|
|
447
|
+
this.logger?.info?.('[mirror] triggering post-upgrade hook', input)
|
|
448
|
+
await this.postUpgradeHook(input)
|
|
449
|
+
this.logger?.info?.('[mirror] post-upgrade hook completed', input)
|
|
450
|
+
} catch (error) {
|
|
451
|
+
this.logger?.warn?.('[mirror] post-upgrade hook failed', {
|
|
452
|
+
error: String(error instanceof Error ? error.message : error), ...input,
|
|
453
|
+
})
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/** @param {unknown} value @returns {string} */
|
|
458
|
+
assertBackupId(value) {
|
|
459
|
+
const id = String(value ?? '')
|
|
460
|
+
if (!BACKUP_ID_RE.test(id)) throw new Error(`invalid backup id: ${id}`)
|
|
461
|
+
return id
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/** 用 source 目录原子替换 active/(先拷到临时目录,再删旧、改名)。 */
|
|
465
|
+
swapActive(source) {
|
|
466
|
+
const stagingCopy = path.join(this.mirrorRoot, `.swap-${this.now()}-${sha256(`${source}:${Math.random()}`).slice(0, 6)}`)
|
|
467
|
+
try {
|
|
468
|
+
ensureDir(stagingCopy)
|
|
469
|
+
copyTree(source, stagingCopy)
|
|
470
|
+
if (exists(this.activeDir())) removeTree(this.activeDir())
|
|
471
|
+
fs.renameSync(stagingCopy, this.activeDir())
|
|
472
|
+
} finally {
|
|
473
|
+
if (exists(stagingCopy)) removeTree(stagingCopy)
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
export const _internals = Object.freeze({ dirSize, copyDir, normalizeIntervalHours, BACKUP_ID_RE })
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// Copyright (c) 2026 dsh-Fang contributors — see LICENSE-CORE.
|
|
3
|
+
// fang-link(v2.9.0)— 母体连接描述 + 连通性探测(纯逻辑,供 orchestrator 服务与 HTTP 路由复用)。
|
|
4
|
+
//
|
|
5
|
+
// 边界:
|
|
6
|
+
// * 只描述、不持有连接状态:分身的 token 由 Forker 在 spawn 时注入,fang 不落盘、不回显;
|
|
7
|
+
// * 探测是只读 GET `${target}${prefix}/api/status`,带超时、不抛异常,失败以结构化错误返回;
|
|
8
|
+
// * 目标只接受 http(s) origin,拒绝凭据/查询串/非 http 协议(避免把 token 写进 URL)。
|
|
9
|
+
|
|
10
|
+
import fs from 'node:fs'
|
|
11
|
+
import path from 'node:path'
|
|
12
|
+
|
|
13
|
+
export const MOTHER_LINK_PATHS = Object.freeze({
|
|
14
|
+
DESCRIBE: '/api/mother-link',
|
|
15
|
+
PROBE: '/api/mother-link/probe',
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
export const DEFAULT_LINK_TIMEOUT_MS = 3000
|
|
19
|
+
export const MAX_LINK_TIMEOUT_MS = 10000
|
|
20
|
+
export const MIN_LINK_TIMEOUT_MS = 200
|
|
21
|
+
|
|
22
|
+
/** @param {unknown} raw @returns {string} */
|
|
23
|
+
export function normalizePrefix(raw) {
|
|
24
|
+
const text = String(raw ?? '/fang').trim().replace(/\/+$/, '')
|
|
25
|
+
if (text.length === 0) return '/fang'
|
|
26
|
+
return text.startsWith('/') ? text : `/${text}`
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 校验并归一化母体地址(只保留 origin,丢弃 path/query/hash)。
|
|
31
|
+
* @param {unknown} raw
|
|
32
|
+
* @returns {{ok:true, origin:string, host:string} | {ok:false, error:string}}
|
|
33
|
+
*/
|
|
34
|
+
export function normalizeLinkOrigin(raw) {
|
|
35
|
+
const text = String(raw ?? '').trim()
|
|
36
|
+
if (text.length === 0) return { ok: false, error: 'target-empty' }
|
|
37
|
+
if (text.length > 512) return { ok: false, error: 'target-too-long' }
|
|
38
|
+
let url
|
|
39
|
+
try {
|
|
40
|
+
url = new URL(text)
|
|
41
|
+
} catch {
|
|
42
|
+
return { ok: false, error: 'target-invalid-url' }
|
|
43
|
+
}
|
|
44
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:') return { ok: false, error: 'target-scheme' }
|
|
45
|
+
if (url.username.length > 0 || url.password.length > 0) return { ok: false, error: 'target-credentials' }
|
|
46
|
+
if (url.search.length > 0 || url.hash.length > 0) return { ok: false, error: 'target-query' }
|
|
47
|
+
return { ok: true, origin: `${url.protocol}//${url.host}`, host: url.host }
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 母体侧读取 profile 的“共享配置摘要”(只读、失败降级为 null。
|
|
52
|
+
* @param {{profileDir?: string, env?: Record<string, string|undefined>}} [options]
|
|
53
|
+
* @returns {{layer:'profile', profile:string|null, profileDir:string|null, bundles:number|null, name?:string}}
|
|
54
|
+
*/
|
|
55
|
+
export function readSharedProfileSummary(options = {}) {
|
|
56
|
+
const env = options.env ?? process.env
|
|
57
|
+
const profile = typeof env.DSH_PROFILE === 'string' && env.DSH_PROFILE.length > 0 ? env.DSH_PROFILE : null
|
|
58
|
+
const profileDir = String(options.profileDir ?? '').trim()
|
|
59
|
+
const fallback = { layer: 'profile', profile, profileDir: profileDir.length > 0 ? profileDir : null, bundles: null }
|
|
60
|
+
if (profileDir.length === 0) return fallback
|
|
61
|
+
try {
|
|
62
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(profileDir, 'package.json'), 'utf8'))
|
|
63
|
+
const bundles = Array.isArray(pkg?.dsh?.profile?.bundles) ? pkg.dsh.profile.bundles.length : null
|
|
64
|
+
return {
|
|
65
|
+
...fallback,
|
|
66
|
+
bundles,
|
|
67
|
+
name: typeof pkg?.name === 'string' ? pkg.name : undefined,
|
|
68
|
+
}
|
|
69
|
+
} catch {
|
|
70
|
+
return fallback
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 生成母体连接描述(纯函数)。
|
|
76
|
+
* @param {{role?: string, version?: string, prefix?: string, profileDir?: string, sharedConfig?: object, now?: () => number}} options
|
|
77
|
+
* @returns {Record<string, unknown>}
|
|
78
|
+
*/
|
|
79
|
+
export function describeMotherLink(options = {}) {
|
|
80
|
+
const now = options.now ?? Date.now
|
|
81
|
+
const prefix = normalizePrefix(options.prefix ?? '/fang')
|
|
82
|
+
const sharedConfig = options.sharedConfig ?? readSharedProfileSummary({ profileDir: options.profileDir })
|
|
83
|
+
return {
|
|
84
|
+
ok: true,
|
|
85
|
+
role: typeof options.role === 'string' ? options.role : 'mother',
|
|
86
|
+
version: typeof options.version === 'string' ? options.version : '',
|
|
87
|
+
at: now(),
|
|
88
|
+
prefix,
|
|
89
|
+
motherApiBase: `${prefix}/api`,
|
|
90
|
+
adapterBase: `${prefix}/adapter`,
|
|
91
|
+
probe: {
|
|
92
|
+
method: 'POST',
|
|
93
|
+
path: `${prefix}${MOTHER_LINK_PATHS.PROBE}`,
|
|
94
|
+
body: { target: 'http(s)://<mother-host>', timeoutMs: DEFAULT_LINK_TIMEOUT_MS },
|
|
95
|
+
},
|
|
96
|
+
tokenPolicy: 'fork adapter token is injected by Forker at spawn time; fang never persists or echoes it',
|
|
97
|
+
sharedConfig,
|
|
98
|
+
forkInstructions: [
|
|
99
|
+
'打开母体 web 门户(/fang/panel)或瓮城面板的「母体连接」卡片',
|
|
100
|
+
'分身只需母体地址;令牌由 Forker 注入 DSH_ADAPTER_TOKEN,不需要手工填写',
|
|
101
|
+
'连通性测试调用 POST /fang/api/mother-link/probe,不会写入任何配置',
|
|
102
|
+
],
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 探测母体 `/api/status`(一次 GET,超时上限 10s;任何失败都返回结构化错误,不抛)。
|
|
108
|
+
* @param {{target?: unknown, prefix?: string, timeoutMs?: number, timeoutMsLimit?: number, fetchImpl?: typeof fetch, now?: () => number}} options
|
|
109
|
+
* @returns {Promise<Record<string, unknown>>}
|
|
110
|
+
*/
|
|
111
|
+
export async function probeLink(options = {}) {
|
|
112
|
+
const now = options.now ?? Date.now
|
|
113
|
+
const normalized = normalizeLinkOrigin(options.target)
|
|
114
|
+
if (!normalized.ok) {
|
|
115
|
+
return { ok: false, target: String(options.target ?? ''), error: normalized.error, latencyMs: 0, checkedAt: now() }
|
|
116
|
+
}
|
|
117
|
+
const rawTimeout = Number.isFinite(options.timeoutMs) ? Math.trunc(options.timeoutMs) : DEFAULT_LINK_TIMEOUT_MS
|
|
118
|
+
const timeoutMs = Math.min(Math.max(rawTimeout, MIN_LINK_TIMEOUT_MS), MAX_LINK_TIMEOUT_MS)
|
|
119
|
+
const prefix = normalizePrefix(options.prefix ?? '/fang')
|
|
120
|
+
const fetchImpl = options.fetchImpl ?? globalThis.fetch
|
|
121
|
+
if (typeof fetchImpl !== 'function') {
|
|
122
|
+
return { ok: false, target: normalized.origin, error: 'fetch-unavailable', latencyMs: 0, checkedAt: now() }
|
|
123
|
+
}
|
|
124
|
+
const started = now()
|
|
125
|
+
try {
|
|
126
|
+
const response = await fetchImpl(`${normalized.origin}${prefix}/api/status`, {
|
|
127
|
+
method: 'GET',
|
|
128
|
+
headers: { accept: 'application/json' },
|
|
129
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
130
|
+
})
|
|
131
|
+
const latencyMs = Math.max(0, now() - started)
|
|
132
|
+
let payload = null
|
|
133
|
+
try {
|
|
134
|
+
payload = await response.json()
|
|
135
|
+
} catch {
|
|
136
|
+
payload = null
|
|
137
|
+
}
|
|
138
|
+
const version = typeof payload?.version === 'string' ? payload.version : null
|
|
139
|
+
if (!response.ok) {
|
|
140
|
+
return { ok: false, target: normalized.origin, httpStatus: response.status, latencyMs, version, error: 'status-http-error', checkedAt: now() }
|
|
141
|
+
}
|
|
142
|
+
return { ok: true, target: normalized.origin, httpStatus: response.status, latencyMs, version, checkedAt: now() }
|
|
143
|
+
} catch (error) {
|
|
144
|
+
const name = error instanceof Error ? error.name : ''
|
|
145
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
146
|
+
const timedOut = name === 'TimeoutError' || name === 'AbortError'
|
|
147
|
+
return {
|
|
148
|
+
ok: false,
|
|
149
|
+
target: normalized.origin,
|
|
150
|
+
error: timedOut ? 'timeout' : 'network-error',
|
|
151
|
+
message: message.slice(0, 200),
|
|
152
|
+
latencyMs: Math.max(0, now() - started),
|
|
153
|
+
checkedAt: now(),
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// dsh-Fang v2.12.0 — per-key 串行锁(零依赖)。
|
|
3
|
+
// 用途:同一原身的 ensureFresh/重算串行化;不同 key 互不阻塞。
|
|
4
|
+
// 失败不阻断链:then(fn, fn) 保证前一个任务 reject 后下一个仍执行。
|
|
5
|
+
|
|
6
|
+
export class KeyedMutex {
|
|
7
|
+
constructor() {
|
|
8
|
+
/** @type {Map<string, Promise<unknown>>} */
|
|
9
|
+
this.chains = new Map()
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @template T
|
|
14
|
+
* @param {string} key
|
|
15
|
+
* @param {() => (T|Promise<T>)} fn
|
|
16
|
+
* @returns {Promise<T>}
|
|
17
|
+
*/
|
|
18
|
+
run(key, fn) {
|
|
19
|
+
const previous = this.chains.get(key) ?? Promise.resolve()
|
|
20
|
+
const next = previous.then(fn, fn).finally(() => {
|
|
21
|
+
if (this.chains.get(key) === next) this.chains.delete(key)
|
|
22
|
+
})
|
|
23
|
+
this.chains.set(key, next)
|
|
24
|
+
return next
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** 当前是否还有排队/执行中的 key(诊断用)。 */
|
|
28
|
+
size() {
|
|
29
|
+
return this.chains.size
|
|
30
|
+
}
|
|
31
|
+
}
|