dsh-config-manager 0.4.3 → 0.5.0
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.md +10 -0
- package/README.zh-CN.md +10 -0
- package/lib/adapters/mcp.d.ts +9 -0
- package/lib/adapters/mcp.js +37 -0
- package/lib/adapters/plugins.js +10 -2
- package/lib/adapters/pnpm-workspace.js +1 -1
- package/lib/adapters/self.d.ts +2 -0
- package/lib/adapters/self.js +18 -2
- package/lib/adapters/workspaces.js +1 -1
- package/lib/cli/restore-resume.js +1 -1
- package/lib/cli/web/page.js +2 -2
- package/lib/client.d.ts +14 -41
- package/lib/client.js +794 -716
- package/lib/core/backup.d.ts +11 -0
- package/lib/core/backup.js +27 -0
- package/lib/core/env-lock-lifecycle.d.ts +17 -3
- package/lib/core/env-lock-lifecycle.js +38 -0
- package/lib/core/external-failure.d.ts +9 -0
- package/lib/core/external-failure.js +50 -0
- package/lib/core/local-plugin-host.d.ts +9 -7
- package/lib/core/local-plugin-host.js +109 -15
- package/lib/core/local-plugin-pack.d.ts +20 -0
- package/lib/core/local-plugin-pack.js +18 -3
- package/lib/core/mcp-connector-store.d.ts +13 -0
- package/lib/core/mcp-connector-store.js +16 -1
- package/lib/core/messages-core.d.ts +45 -0
- package/lib/core/messages-core.js +92 -0
- package/lib/core/messages.d.ts +1 -0
- package/lib/core/messages.js +6 -0
- package/lib/core/model-tool-plans.d.ts +66 -0
- package/lib/core/model-tool-plans.js +128 -0
- package/lib/core/model-tools.d.ts +96 -8
- package/lib/core/model-tools.js +568 -31
- package/lib/core/phase3-host.js +10 -2
- package/lib/core/plugin-data-registry.d.ts +1 -1
- package/lib/core/plugin-data-registry.js +1 -1
- package/lib/core/restore-plan-fingerprint.d.ts +2 -0
- package/lib/core/restore-plan-fingerprint.js +18 -0
- package/lib/core/restore-resume.d.ts +1 -1
- package/lib/core/restore-resume.js +1 -1
- package/lib/core/restore.d.ts +5 -0
- package/lib/core/restore.js +4 -1
- package/lib/core/types.d.ts +12 -0
- package/lib/foreign/claude-sessions.d.ts +1 -1
- package/lib/foreign/claude-sessions.js +1 -1
- package/lib/foreign/registry.js +1 -1
- package/lib/foreign/types.d.ts +1 -1
- package/lib/index.d.ts +15 -15
- package/lib/index.js +60 -67
- package/lib/market/git-file-writer.js +1 -1
- package/lib/market/github-repos.js +1 -1
- package/lib/market/market-config.js +1 -1
- package/lib/market/messages-market.js +1 -1
- package/lib/market/my-repo.js +1 -1
- package/lib/market/prepare.js +1 -1
- package/lib/market/reader.js +2 -2
- package/lib/market/repo-url.d.ts +1 -1
- package/lib/market/security.js +2 -2
- package/lib/market/star-cache.d.ts +1 -1
- package/lib/market/types.d.ts +1 -1
- package/lib/market/types.js +1 -1
- package/lib/market/url.js +1 -1
- package/lib/routes/backup.js +17 -9
- package/lib/routes/index.d.ts +1 -1
- package/lib/routes/kit.d.ts +2 -2
- package/lib/routes/market.d.ts +1 -1
- package/lib/routes/market.js +3 -60
- package/lib/routes/model-tools-deps.d.ts +41 -0
- package/lib/routes/model-tools-deps.js +18 -0
- package/lib/routes/recovery.js +5 -3
- package/lib/routes/route-support.d.ts +18 -26
- package/lib/routes/route-support.js +19 -32
- package/lib/routes/snapshots.d.ts +1 -1
- package/lib/routes/snapshots.js +4 -40
- package/lib/routes/sync.js +40 -28
- package/lib/schema/index.d.ts +2 -2
- package/lib/schema/index.js +2 -2
- package/lib/schema/section-registry.d.ts +5 -3
- package/lib/schema/section-registry.js +7 -3
- package/lib/security/secret-scanner.js +1 -1
- package/lib/sync/apply-bundle.d.ts +50 -0
- package/lib/sync/apply-bundle.js +60 -0
- package/lib/sync/autosync-scheduler.d.ts +9 -0
- package/lib/sync/autosync-scheduler.js +118 -89
- package/lib/sync/backup-scheduler.js +5 -4
- package/lib/sync/blob-store.d.ts +2 -2
- package/lib/sync/git/git-transport.d.ts +12 -0
- package/lib/sync/git/git-transport.js +50 -2
- package/lib/sync/merge.d.ts +2 -0
- package/lib/sync/merge.js +28 -0
- package/lib/sync/messages.d.ts +4 -0
- package/lib/sync/messages.js +9 -2
- package/lib/sync/s3/s3-transport.d.ts +1 -1
- package/lib/sync/s3/s3-transport.js +1 -1
- package/lib/sync/sync-engine.d.ts +4 -4
- package/lib/sync/sync-engine.js +25 -26
- package/lib/sync/sync-scope-boot.d.ts +34 -0
- package/lib/sync/sync-scope-boot.js +71 -0
- package/lib/sync/sync-scope.d.ts +103 -0
- package/lib/sync/sync-scope.js +229 -0
- package/lib/sync/sync-selection.d.ts +17 -4
- package/lib/sync/sync-selection.js +40 -14
- package/lib/sync/transport.d.ts +4 -1
- package/lib/sync/transport.js +6 -0
- package/lib/sync/webdav/webdav-transport.js +1 -1
- package/lib/sync/workspace-merge.d.ts +36 -0
- package/lib/sync/workspace-merge.js +152 -0
- package/lib/ui/backup-schedule.d.ts +13 -2
- package/lib/ui/backup-schedule.js +3 -0
- package/lib/ui/snapshots-view.d.ts +16 -0
- package/lib/ui/snapshots-view.js +21 -0
- package/lib/utils/env-lock.d.ts +96 -0
- package/lib/utils/env-lock.js +332 -111
- package/lib/utils/messages-utils.d.ts +6 -0
- package/lib/utils/messages-utils.js +17 -1
- package/lib/utils/patch-yaml.js +1 -1
- package/lib/utils/zip.js +1 -1
- package/package.json +3 -2
- package/src/adapters/mcp.test.ts +28 -0
- package/src/adapters/mcp.ts +33 -0
- package/src/adapters/plugins.test.ts +49 -0
- package/src/adapters/plugins.ts +9 -1
- package/src/adapters/pnpm-workspace.ts +1 -1
- package/src/adapters/self.test.ts +39 -1
- package/src/adapters/self.ts +20 -2
- package/src/adapters/workspaces.ts +1 -1
- package/src/cli/restore-resume.ts +1 -1
- package/src/cli/web/page.ts +2 -2
- package/src/client/about/AboutPanel.tsx +1 -1
- package/src/client/about/about-view.test.ts +1 -1
- package/src/client/about/about-view.ts +1 -1
- package/src/client/api.ts +1 -6
- package/src/client/chat/chat-entry-view.test.ts +1 -1
- package/src/client/chat/chat-slots.ts +1 -1
- package/src/client/chat/rollback-diff-tab.ts +1 -1
- package/src/client/client-types.ts +1 -1
- package/src/client/common/routes.ts +1 -5
- package/src/client/css-scale-guard.test.ts +1 -1
- package/src/client/home/HomePanel.tsx +3 -0
- package/src/client/library/ArtifactRow.tsx +14 -2
- package/src/client/library/LibraryPanel.tsx +44 -0
- package/src/client/locales/overview.ts +10 -2
- package/src/client/locales/sessions.ts +3 -0
- package/src/client/market/MyConfigsView.tsx +1 -1
- package/src/client/market/market-api.ts +2 -14
- package/src/client/market/market-locales.ts +1 -1
- package/src/client/market/market-view.ts +3 -3
- package/src/client/market/my-configs-api.ts +1 -1
- package/src/client/market/my-configs-view.ts +1 -1
- package/src/client/recovery/recovery-locales.test.ts +1 -1
- package/src/client/run-store.ts +1 -1
- package/src/client/sync/SyncSectionPickerDialog.tsx +0 -5
- package/src/client/sync/history-model.test.ts +28 -7
- package/src/client/sync/history-model.ts +6 -3
- package/src/client/sync/sync-locales.ts +10 -4
- package/src/client/sync/sync-view.ts +1 -1
- package/src/core/analyzer-cache.test.ts +1 -1
- package/src/core/backup.test.ts +45 -1
- package/src/core/backup.ts +25 -0
- package/src/core/disk-usage.test.ts +2 -0
- package/src/core/env-lock-lifecycle.ts +33 -3
- package/src/core/external-failure.test.ts +60 -0
- package/src/core/external-failure.ts +54 -0
- package/src/core/integrity-extra-entries.test.ts +1 -1
- package/src/core/local-plugin-host.test.ts +134 -0
- package/src/core/local-plugin-host.ts +122 -17
- package/src/core/local-plugin-pack.ts +39 -3
- package/src/core/mcp-connector-store.ts +26 -1
- package/src/core/messages-core.ts +94 -0
- package/src/core/messages.ts +6 -0
- package/src/core/model-tool-plans.test.ts +111 -0
- package/src/core/model-tool-plans.ts +170 -0
- package/src/core/model-tools.test.ts +212 -12
- package/src/core/model-tools.ts +710 -38
- package/src/core/phase3-host.ts +10 -2
- package/src/core/phase3-production-integration.test.ts +43 -0
- package/src/core/plugin-data-registry.ts +1 -1
- package/src/core/restore-plan-fingerprint.ts +19 -0
- package/src/core/restore-resume.ts +1 -1
- package/src/core/restore.ts +9 -1
- package/src/core/types.ts +12 -0
- package/src/foreign/claude-sessions.ts +3 -3
- package/src/foreign/registry.ts +1 -1
- package/src/foreign/types.ts +1 -1
- package/src/index.ts +91 -98
- package/src/market/git-file-writer.ts +1 -1
- package/src/market/github-repos.test.ts +1 -1
- package/src/market/github-repos.ts +1 -1
- package/src/market/market-config.ts +1 -1
- package/src/market/messages-market.ts +1 -1
- package/src/market/my-repo.test.ts +1 -1
- package/src/market/my-repo.ts +1 -1
- package/src/market/prepare.test.ts +1 -1
- package/src/market/prepare.ts +1 -1
- package/src/market/reader.ts +2 -2
- package/src/market/repo-url.ts +1 -1
- package/src/market/security.ts +2 -2
- package/src/market/star-cache.ts +1 -1
- package/src/market/types.ts +1 -1
- package/src/market/url.test.ts +1 -1
- package/src/market/url.ts +1 -1
- package/src/profiles/dsh-profile-copy-rollback.test.ts +1 -1
- package/src/routes/backup.ts +16 -9
- package/src/routes/index.ts +1 -1
- package/src/routes/kit.ts +2 -2
- package/src/routes/market.ts +2 -59
- package/src/routes/model-tools-deps.ts +56 -0
- package/src/routes/recovery.ts +5 -3
- package/src/routes/route-support.ts +49 -36
- package/src/routes/snapshots.ts +4 -40
- package/src/routes/sync.ts +39 -27
- package/src/schema/index.ts +2 -2
- package/src/schema/manifest-schema.test.ts +2 -2
- package/src/schema/registry.test.ts +1 -1
- package/src/schema/section-registry.ts +9 -4
- package/src/security/redaction.test.ts +1 -1
- package/src/security/secret-scanner.test.ts +2 -2
- package/src/security/secret-scanner.ts +1 -1
- package/src/sync/apply-bundle.ts +104 -0
- package/src/sync/autosync-scheduler.test.ts +180 -6
- package/src/sync/autosync-scheduler.ts +132 -92
- package/src/sync/backup-scheduler.test.ts +11 -4
- package/src/sync/backup-scheduler.ts +5 -4
- package/src/sync/blob-store.ts +2 -2
- package/src/sync/git/git-transport.test.ts +62 -0
- package/src/sync/git/git-transport.ts +54 -3
- package/src/sync/merge.ts +29 -0
- package/src/sync/messages.ts +9 -2
- package/src/sync/s3/s3-transport.ts +1 -1
- package/src/sync/sync-engine.test.ts +238 -2
- package/src/sync/sync-engine.ts +25 -26
- package/src/sync/sync-preview-push.test.ts +2 -1
- package/src/sync/sync-scope-boot.ts +87 -0
- package/src/sync/sync-scope.test.ts +154 -0
- package/src/sync/sync-scope.ts +256 -0
- package/src/sync/sync-selection.test.ts +76 -2
- package/src/sync/sync-selection.ts +40 -13
- package/src/sync/transport.test.ts +21 -1
- package/src/sync/transport.ts +12 -0
- package/src/sync/webdav/webdav-delete-index-unreadable.test.ts +2 -2
- package/src/sync/webdav/webdav-transport.ts +1 -1
- package/src/sync/workspace-merge.test.ts +110 -0
- package/src/sync/workspace-merge.ts +180 -0
- package/src/ui/backup-schedule.test.ts +5 -0
- package/src/ui/backup-schedule.ts +21 -2
- package/src/ui/session-issue-labels.ts +1 -1
- package/src/ui/snapshots-view.test.ts +32 -0
- package/src/ui/snapshots-view.ts +34 -0
- package/src/utils/env-lock.test.ts +242 -9
- package/src/utils/env-lock.ts +247 -13
- package/src/utils/messages-utils.ts +17 -1
- package/src/utils/patch-yaml.ts +1 -1
- package/src/utils/zip.ts +1 -1
- package/src/client/CLIENT_DEPENDENCIES.md +0 -94
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 同步作用域的启动接线(per-profile sync scope)—— 从 `src/index.ts` 下沉到这里,
|
|
3
|
+
* 让宿主装配只留一行调用(体量棘轮不允许继续堆在 index.ts 里)。
|
|
4
|
+
*
|
|
5
|
+
* 两件事,都属于「档案作用域」的运行期胶水,本身没有状态:
|
|
6
|
+
* 1. `bootSyncScope`:非默认档案首次启动时从历史同步根继承配置与凭据值(best-effort);
|
|
7
|
+
* 2. `makeSyncCredentialViews`:密码槽位的「是否已保存」视图(只回布尔,值永不返回)。
|
|
8
|
+
*
|
|
9
|
+
* 依赖只经参数传入(凭据端口 / 日志回调),本模块不 import DSH 运行时。
|
|
10
|
+
*/
|
|
11
|
+
import { SYNC_CHANNELS } from '../utils/shared-constants.ts'
|
|
12
|
+
import type { SyncTransportType } from './sync-config.ts'
|
|
13
|
+
import { adoptLegacySyncScope, syncPasswordRef, syncScopeLegacyCredentialRefs, type SyncScope } from './sync-scope.ts'
|
|
14
|
+
import { engineMsg } from './messages.ts'
|
|
15
|
+
|
|
16
|
+
/** 只用到三个方法的凭据端口(与 DSH CredentialProvider 同形)。 */
|
|
17
|
+
export interface SyncScopeCredentialPort {
|
|
18
|
+
describe(ref: string): Promise<{ configured: boolean }>
|
|
19
|
+
resolve(ref: string): Promise<{ value?: unknown } | undefined>
|
|
20
|
+
set(ref: string, value: string): Promise<void>
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** 同步密码「是否已保存」视图(只回布尔;密码值永不进任何响应)。 */
|
|
24
|
+
export interface SyncPasswordViews {
|
|
25
|
+
syncPasswordConfigured: (ref: string) => Promise<boolean>
|
|
26
|
+
syncCredentialsByChannelView: () => Promise<Record<SyncTransportType, { encryptPasswordConfigured: boolean; decryptPasswordConfigured: boolean }>>
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 启动期继承(**异步 + best-effort**):失败只记日志,绝不影响插件挂载 ——
|
|
31
|
+
* 最坏结果只是该档案「同步未配置」,用户重填一次即恢复。
|
|
32
|
+
*
|
|
33
|
+
* 幂等由作用域目录本身决定(见 `adoptLegacySyncScope`):目录已存在就不再继承。
|
|
34
|
+
*/
|
|
35
|
+
export function bootSyncScope(
|
|
36
|
+
scope: SyncScope,
|
|
37
|
+
credentials: SyncScopeCredentialPort,
|
|
38
|
+
warn: (line: string) => void,
|
|
39
|
+
): void {
|
|
40
|
+
if (scope.isDefault) return
|
|
41
|
+
void (async (): Promise<void> => {
|
|
42
|
+
try {
|
|
43
|
+
const adopted = await adoptLegacySyncScope(scope, warn)
|
|
44
|
+
if (adopted.length === 0) return
|
|
45
|
+
for (const pair of syncScopeLegacyCredentialRefs(scope)) {
|
|
46
|
+
// 先看新槽位是否已配置:用户自己填过的值绝不覆盖
|
|
47
|
+
if ((await credentials.describe(pair.to)).configured) continue
|
|
48
|
+
const legacy = await credentials.resolve(pair.from)
|
|
49
|
+
const value = legacy?.value
|
|
50
|
+
if (typeof value !== 'string' || value === '') continue
|
|
51
|
+
// 值只在进程内搬一次:读旧槽位 → 写新槽位,不落盘、不进日志
|
|
52
|
+
await credentials.set(pair.to, value)
|
|
53
|
+
}
|
|
54
|
+
} catch (error) {
|
|
55
|
+
warn(engineMsg('sync.scopeBootFailed', { reason: String(error) }))
|
|
56
|
+
}
|
|
57
|
+
})()
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 密码槽位视图工厂。
|
|
62
|
+
*
|
|
63
|
+
* @param credentials 凭据端口(只用来 describe,永不 resolve 出值)
|
|
64
|
+
* @param profile 本档案名(槽位名按档案加后缀)
|
|
65
|
+
*/
|
|
66
|
+
export function makeSyncCredentialViews(
|
|
67
|
+
credentials: SyncScopeCredentialPort,
|
|
68
|
+
profile: string,
|
|
69
|
+
): SyncPasswordViews {
|
|
70
|
+
const syncPasswordConfigured = async (ref: string): Promise<boolean> => {
|
|
71
|
+
try {
|
|
72
|
+
return (await credentials.describe(ref)).configured
|
|
73
|
+
} catch {
|
|
74
|
+
return false
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const syncCredentialsByChannelView = async (): Promise<Record<SyncTransportType, { encryptPasswordConfigured: boolean; decryptPasswordConfigured: boolean }>> => {
|
|
78
|
+
const describe = async (channel: SyncTransportType): Promise<{ encryptPasswordConfigured: boolean; decryptPasswordConfigured: boolean }> => ({
|
|
79
|
+
encryptPasswordConfigured: await syncPasswordConfigured(syncPasswordRef('ENCRYPT', channel, profile)),
|
|
80
|
+
decryptPasswordConfigured: await syncPasswordConfigured(syncPasswordRef('DECRYPT', channel, profile)),
|
|
81
|
+
})
|
|
82
|
+
// t32/B7:通道集合来自唯一枚举(保持 Promise.all 并发语义)
|
|
83
|
+
const pairs = await Promise.all(SYNC_CHANNELS.map(async (channel) => [channel, await describe(channel)] as const))
|
|
84
|
+
return Object.fromEntries(pairs) as Record<SyncTransportType, { encryptPasswordConfigured: boolean; decryptPasswordConfigured: boolean }>
|
|
85
|
+
}
|
|
86
|
+
return { syncPasswordConfigured, syncCredentialsByChannelView }
|
|
87
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 同步作用域(src/sync/sync-scope.ts)单测。
|
|
3
|
+
*
|
|
4
|
+
* 钉住的判据:
|
|
5
|
+
* 1. 默认档案(web)路径与槽位名**逐字不变** —— 这是「老用户零迁移」的全部依据;
|
|
6
|
+
* 2. 其它档案的目录与槽位后缀互相一致(同一份档案名只有一个作用域);
|
|
7
|
+
* 3. 非法档案名回落默认档案 —— 绝不拼出 \`..\` 之类的越界目录段;
|
|
8
|
+
* 4. 采纳(adopt)只搬「怎么同步」的配置,且幂等(目录一存在就跳过)。
|
|
9
|
+
*/
|
|
10
|
+
import test from 'node:test'
|
|
11
|
+
import assert from 'node:assert/strict'
|
|
12
|
+
import { mkdtemp, mkdir, readFile, rm, writeFile } from 'node:fs/promises'
|
|
13
|
+
import { existsSync } from 'node:fs'
|
|
14
|
+
import { tmpdir } from 'node:os'
|
|
15
|
+
import { join } from 'node:path'
|
|
16
|
+
import { CLOUD_SECRET_REFS, CLOUD_SYNC_PROVIDERS, SYNC_CHANNELS } from '../utils/shared-constants.ts'
|
|
17
|
+
import {
|
|
18
|
+
SYNC_CREDENTIAL_REF,
|
|
19
|
+
SYNC_DEFAULT_PROFILE,
|
|
20
|
+
SYNC_SCOPE_ADOPT_FILES,
|
|
21
|
+
SYNC_WEBDAV_CREDENTIAL_REF,
|
|
22
|
+
adoptLegacySyncScope,
|
|
23
|
+
normalizeSyncProfile,
|
|
24
|
+
scopedSyncRef,
|
|
25
|
+
syncPasswordRef,
|
|
26
|
+
syncScopeFor,
|
|
27
|
+
syncScopeLegacyCredentialRefs,
|
|
28
|
+
syncScopeSuffix,
|
|
29
|
+
} from './sync-scope.ts'
|
|
30
|
+
|
|
31
|
+
async function tempDataDir(): Promise<string> {
|
|
32
|
+
return await mkdtemp(join(tmpdir(), 'dcm-sync-scope-'))
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
test('默认档案:作用域 = 历史 sync/,槽位名不加后缀(零迁移)', () => {
|
|
36
|
+
const dataDir = join(tmpdir(), 'home', 'dsh-config-manager')
|
|
37
|
+
for (const raw of [SYNC_DEFAULT_PROFILE, undefined, '', ' ']) {
|
|
38
|
+
const scope = syncScopeFor(dataDir, raw)
|
|
39
|
+
assert.equal(scope.profile, 'web')
|
|
40
|
+
assert.equal(scope.isDefault, true)
|
|
41
|
+
assert.equal(scope.rootDir, join(dataDir, 'sync'))
|
|
42
|
+
assert.equal(scope.dir, join(dataDir, 'sync'))
|
|
43
|
+
assert.equal(scope.tokenRef, SYNC_CREDENTIAL_REF)
|
|
44
|
+
assert.equal(scope.webdavPasswordRef, SYNC_WEBDAV_CREDENTIAL_REF)
|
|
45
|
+
assert.equal(scope.scopeRef('DSH_CONFIG_MANAGER_S3_SECRET_KEY'), 'DSH_CONFIG_MANAGER_S3_SECRET_KEY')
|
|
46
|
+
assert.equal(scope.passwordRef('ENCRYPT', 'git'), syncPasswordRef('ENCRYPT', 'git'))
|
|
47
|
+
}
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
test('其它档案:目录 = sync-profiles/<name>,槽位追加 __<PROFILE>', () => {
|
|
51
|
+
const dataDir = join(tmpdir(), 'home', 'dsh-config-manager')
|
|
52
|
+
const scope = syncScopeFor(dataDir, 'Test1')
|
|
53
|
+
assert.equal(scope.profile, 'Test1')
|
|
54
|
+
assert.equal(scope.isDefault, false)
|
|
55
|
+
// 共享根不变:定时备份配置仍在历史 sync/ 下
|
|
56
|
+
assert.equal(scope.rootDir, join(dataDir, 'sync'))
|
|
57
|
+
assert.equal(scope.dir, join(dataDir, 'sync-profiles', 'Test1'))
|
|
58
|
+
assert.equal(scope.tokenRef, SYNC_CREDENTIAL_REF + '__TEST1')
|
|
59
|
+
assert.equal(scope.webdavPasswordRef, SYNC_WEBDAV_CREDENTIAL_REF + '__TEST1')
|
|
60
|
+
assert.equal(scope.scopeRef('FOO'), 'FOO__TEST1')
|
|
61
|
+
assert.equal(scope.passwordRef('DECRYPT', 'webdav'), 'DSH_CONFIG_MANAGER_SYNC_DECRYPT_PASSWORD_WEBDAV__TEST1')
|
|
62
|
+
// 槽位名必须满足 credentials 的 POSIX env-var 形态(否则写入被拒)
|
|
63
|
+
for (const ref of [scope.tokenRef, scope.webdavPasswordRef, scope.passwordRef('ENCRYPT', 's3')]) {
|
|
64
|
+
assert.match(ref, /^[A-Z_][A-Z0-9_]*$/)
|
|
65
|
+
}
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
test('档案名归一:非法名 / 空名 / 带路径分隔符一律回落默认档案', () => {
|
|
69
|
+
assert.equal(normalizeSyncProfile('desktop'), 'desktop')
|
|
70
|
+
assert.equal(normalizeSyncProfile(' my-prof_1.2 '), 'my-prof_1.2')
|
|
71
|
+
for (const bad of ['..', '../etc', 'a/b', 'a\\b', '-lead', '.hidden', '', 123, null, undefined]) {
|
|
72
|
+
assert.equal(normalizeSyncProfile(bad), SYNC_DEFAULT_PROFILE)
|
|
73
|
+
}
|
|
74
|
+
// 越界名不会拼出上级目录:仍是默认作用域
|
|
75
|
+
assert.equal(syncScopeFor('/data', '..').dir, join('/data', 'sync'))
|
|
76
|
+
assert.equal(syncScopeSuffix('..'), '')
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
test('后缀只由归档名决定:大小写不同 = 不同档案;非字母数字折成下划线', () => {
|
|
80
|
+
assert.equal(syncScopeSuffix('web'), '')
|
|
81
|
+
assert.equal(syncScopeSuffix('Web'), '__WEB')
|
|
82
|
+
assert.equal(syncScopeSuffix('a-b.c_d'), '__A_B_C_D')
|
|
83
|
+
assert.equal(scopedSyncRef('X', 'web'), 'X')
|
|
84
|
+
assert.equal(scopedSyncRef('X', 'a-b'), 'X__A_B')
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
test('legacy 槽位表:默认档案为空;其它档案覆盖 token / WebDAV / 各通道加解密 / 云端密钥', () => {
|
|
88
|
+
assert.deepEqual(syncScopeLegacyCredentialRefs(syncScopeFor('/data', 'web')), [])
|
|
89
|
+
const pairs = syncScopeLegacyCredentialRefs(syncScopeFor('/data', 'desktop'))
|
|
90
|
+
const from = pairs.map((p) => p.from)
|
|
91
|
+
assert.ok(from.includes(SYNC_CREDENTIAL_REF))
|
|
92
|
+
assert.ok(from.includes(SYNC_WEBDAV_CREDENTIAL_REF))
|
|
93
|
+
for (const channel of SYNC_CHANNELS) {
|
|
94
|
+
assert.ok(from.includes('DSH_CONFIG_MANAGER_SYNC_ENCRYPT_PASSWORD_' + channel.toUpperCase()))
|
|
95
|
+
assert.ok(from.includes('DSH_CONFIG_MANAGER_SYNC_DECRYPT_PASSWORD_' + channel.toUpperCase()))
|
|
96
|
+
}
|
|
97
|
+
for (const provider of CLOUD_SYNC_PROVIDERS) {
|
|
98
|
+
assert.ok(from.includes(CLOUD_SECRET_REFS[provider]))
|
|
99
|
+
}
|
|
100
|
+
// 每条都指向同一档案的带后缀槽位,且没有自映射(自映射 = 白搬一次)
|
|
101
|
+
for (const pair of pairs) {
|
|
102
|
+
assert.notEqual(pair.from, pair.to)
|
|
103
|
+
assert.ok(pair.to.endsWith('__DESKTOP'))
|
|
104
|
+
}
|
|
105
|
+
assert.equal(new Set(from).size, from.length)
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
test('采纳历史根:默认档案不动;首次只搬白名单配置;第二次幂等', async () => {
|
|
109
|
+
const dataDir = await tempDataDir()
|
|
110
|
+
try {
|
|
111
|
+
const rootDir = join(dataDir, 'sync')
|
|
112
|
+
await mkdir(join(rootDir, 'work'), { recursive: true })
|
|
113
|
+
await writeFile(join(rootDir, 'sync-config.json'), '{"transport":"git"}', 'utf8')
|
|
114
|
+
await writeFile(join(rootDir, 'sync-selection.json'), '{"git":["settings"]}', 'utf8')
|
|
115
|
+
// 不该被继承的三类:工作副本 / 同步流水 / 运行态
|
|
116
|
+
await writeFile(join(rootDir, 'sync-state.json'), '{"remote":1}', 'utf8')
|
|
117
|
+
await writeFile(join(rootDir, 'sync-history.json'), '[]', 'utf8')
|
|
118
|
+
await writeFile(join(rootDir, 'work', 'HEAD'), 'ref: x', 'utf8')
|
|
119
|
+
|
|
120
|
+
// 默认档案:连目录都不碰(它的作用域就是历史根)
|
|
121
|
+
assert.deepEqual(await adoptLegacySyncScope(syncScopeFor(dataDir, 'web')), [])
|
|
122
|
+
assert.equal(existsSync(join(rootDir, 'sync-profiles')), false)
|
|
123
|
+
|
|
124
|
+
const scope = syncScopeFor(dataDir, 'desktop')
|
|
125
|
+
const copied = await adoptLegacySyncScope(scope)
|
|
126
|
+
assert.deepEqual(copied.sort(), ['sync-config.json', 'sync-selection.json'])
|
|
127
|
+
assert.equal(await readFile(join(scope.dir, 'sync-config.json'), 'utf8'), '{"transport":"git"}')
|
|
128
|
+
for (const skipped of ['sync-state.json', 'sync-history.json']) {
|
|
129
|
+
assert.equal(existsSync(join(scope.dir, skipped)), false)
|
|
130
|
+
}
|
|
131
|
+
assert.equal(existsSync(join(scope.dir, 'work')), false)
|
|
132
|
+
|
|
133
|
+
// 幂等:目录已存在 → 空表,且不覆盖用户后来改过的内容
|
|
134
|
+
await writeFile(join(scope.dir, 'sync-config.json'), '{"transport":"webdav"}', 'utf8')
|
|
135
|
+
assert.deepEqual(await adoptLegacySyncScope(scope), [])
|
|
136
|
+
assert.equal(await readFile(join(scope.dir, 'sync-config.json'), 'utf8'), '{"transport":"webdav"}')
|
|
137
|
+
} finally {
|
|
138
|
+
await rm(dataDir, { recursive: true, force: true })
|
|
139
|
+
}
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
test('采纳历史根:历史根不存在时建目录但不搬文件(不因缺文件抛错)', async () => {
|
|
143
|
+
const dataDir = await tempDataDir()
|
|
144
|
+
try {
|
|
145
|
+
const scope = syncScopeFor(dataDir, 'open-design')
|
|
146
|
+
assert.deepEqual(await adoptLegacySyncScope(scope), [])
|
|
147
|
+
assert.equal(existsSync(scope.dir), true)
|
|
148
|
+
// 白名单常量与实现同源:每个名字都应被尝试(此处只校验表本身非空且全是 basename)
|
|
149
|
+
assert.ok(SYNC_SCOPE_ADOPT_FILES.length > 0)
|
|
150
|
+
for (const name of SYNC_SCOPE_ADOPT_FILES) assert.ok(!name.includes('/'))
|
|
151
|
+
} finally {
|
|
152
|
+
await rm(dataDir, { recursive: true, force: true })
|
|
153
|
+
}
|
|
154
|
+
})
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 同步作用域(per-profile sync scope):哪个档案(profile)同步到哪个仓库、用哪份凭据。
|
|
3
|
+
*
|
|
4
|
+
* ## 问题
|
|
5
|
+
* `$DSH_HOME/dsh-config-manager` 是全机共用的一份插件数据根 —— 全局写锁
|
|
6
|
+
* (`createHostMutationLock`)、SAFE MODE 落盘态(`bootStateDir`)、崩溃可续跑事务日志
|
|
7
|
+
* (`JournalStore`)都长在这棵树上,拆开 = 拆锁 / 拆 SAFE MODE / 拆事务日志。
|
|
8
|
+
* 但「同步到哪个仓库 / 用哪个 GitHub token / 勾了哪些分区 / 自动同步开没开」是**某个档案
|
|
9
|
+
* 自己的决定**:共用一份时,在 web 里配的仓库会连 desktop 一起推;两个档案还共用同一份
|
|
10
|
+
* `sync/work/` git 工作副本与同一份祖先指针,会把对方的写操作判成冲突。
|
|
11
|
+
*
|
|
12
|
+
* ## 结论:只分「同步作用域」,不分数据根
|
|
13
|
+
* 1. `syncRootDir = <dataDir>/sync` —— 历史位置;它同时是**默认档案**(`web`)的作用域,
|
|
14
|
+
* 所以只有一个档案的老用户升级后路径逐字节不变(零迁移)。其下 `backup-schedule.json`
|
|
15
|
+
* 仍是**全机一份**的定时备份配置(定时备份是宿主行为,不是某个档案的同步态);
|
|
16
|
+
* 2. `syncDir(profile) = profile === 'web' ? syncRootDir : <dataDir>/sync-profiles/<profile>`;
|
|
17
|
+
* 同步引擎的全部状态(sync-config / cloud / state / selection / history / autosync /
|
|
18
|
+
* ui-prefs / work / incoming / snapshots)跟着它走;
|
|
19
|
+
* 3. 凭据:默认档案沿用历史 ref 名(跨版本、跨机器、既有用户已配置的槽位都不动),
|
|
20
|
+
* 其它档案追加 `__<PROFILE>` 后缀。值仍在 DSH credentials 里,文件里永不出现 token。
|
|
21
|
+
*
|
|
22
|
+
* ## 已知限制
|
|
23
|
+
* - 分作用域不等于分权限:两个档案指向同一个仓库时仍会互相覆盖 —— 那是用户的配置选择;
|
|
24
|
+
* - `config.dataDir` 可配置 ≠ 隔离手段:那会拆掉全局锁 / SAFE MODE / 事务日志,
|
|
25
|
+
* 因此本模块**不**按 dataDir 分档。
|
|
26
|
+
*/
|
|
27
|
+
import { copyFile, mkdir, stat } from 'node:fs/promises'
|
|
28
|
+
import { join } from 'node:path'
|
|
29
|
+
import { CLOUD_SECRET_REFS, CLOUD_SYNC_PROVIDERS, SYNC_CHANNELS } from '../utils/shared-constants.ts'
|
|
30
|
+
import { isENOENT } from '../utils/guards.ts'
|
|
31
|
+
// 导入即注册本域追加目录(zh/en 两本);宿主日志文案不走字面量
|
|
32
|
+
import { engineMsg } from './messages.ts'
|
|
33
|
+
|
|
34
|
+
/** 默认档案名:只有它使用历史根 `<dataDir>/sync`(零迁移) */
|
|
35
|
+
export const SYNC_DEFAULT_PROFILE = 'web'
|
|
36
|
+
|
|
37
|
+
/** 非默认档案的作用域父目录(在 dataDir 下与历史 `sync/` 平级) */
|
|
38
|
+
export const SYNC_SCOPES_DIR_NAME = 'sync-profiles'
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 同步 token 的 DSH credentials 引用名(POSIX env-var 形态,满足 CredentialRef 品牌要求)。
|
|
42
|
+
* token 只经 credentialRef 读写(写入由请求体触发,读取在每次 git 网络操作时 resolve),
|
|
43
|
+
* 永不进 repoUrl / argv / commit / 同步文件 / 日志。
|
|
44
|
+
*/
|
|
45
|
+
export const SYNC_CREDENTIAL_REF = 'DSH_CONFIG_MANAGER_SYNC_TOKEN'
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* WebDAV 通道口令的独立 DSH credentials 引用(与 git token 槽位分离)。
|
|
49
|
+
* 口令只经 credentialRef 读写,永不进 URL / 请求头 / 日志。
|
|
50
|
+
*/
|
|
51
|
+
export const SYNC_WEBDAV_CREDENTIAL_REF = 'DSH_CONFIG_MANAGER_SYNC_WEBDAV_PASSWORD'
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 同步快照「加密密码 / 解密密码」的 DSH credentials 引用名前缀(按通道各自独立)。
|
|
55
|
+
*
|
|
56
|
+
* 为什么放 DSH credentials:加密/解密密码跨会话必须可用(否则每次推送都要重新输入),
|
|
57
|
+
* 但**绝不允许**写进 sync-selection.json 一类同步文件(那等于把密码推上远端)。
|
|
58
|
+
* 引用名形如 DSH_CONFIG_MANAGER_SYNC_ENCRYPT_PASSWORD_GIT。
|
|
59
|
+
*/
|
|
60
|
+
export const SYNC_ENCRYPT_PASSWORD_REF_PREFIX = 'DSH_CONFIG_MANAGER_SYNC_ENCRYPT_PASSWORD'
|
|
61
|
+
export const SYNC_DECRYPT_PASSWORD_REF_PREFIX = 'DSH_CONFIG_MANAGER_SYNC_DECRYPT_PASSWORD'
|
|
62
|
+
|
|
63
|
+
/** 作用域后缀分隔符(`__` 在 env-var 形态里合法,且不可能与历史槽位名撞车) */
|
|
64
|
+
const SCOPE_SUFFIX_SEP = '__'
|
|
65
|
+
|
|
66
|
+
/** 档案名的合法形态:既做目录段,也做凭据后缀(拒绝空、'..'、分隔符与空白) */
|
|
67
|
+
const PROFILE_NAME_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 归一档案名:非法 / 缺省一律回落默认档案。
|
|
71
|
+
*
|
|
72
|
+
* 为什么回落而不是抛错:档案名来自宿主(`host.profile`);一个坏名字不该让插件在
|
|
73
|
+
* 启动期炸掉,写出历史根(默认作用域)也仍是一致的状态,最多是「没分档」。
|
|
74
|
+
*/
|
|
75
|
+
export function normalizeSyncProfile(raw: unknown): string {
|
|
76
|
+
if (typeof raw !== 'string') return SYNC_DEFAULT_PROFILE
|
|
77
|
+
const name = raw.trim()
|
|
78
|
+
if (!PROFILE_NAME_RE.test(name)) return SYNC_DEFAULT_PROFILE
|
|
79
|
+
return name
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** 档案名 → 凭据后缀(`Test1` → `__TEST1`;默认档案 → 空串,保持历史槽位名) */
|
|
83
|
+
export function syncScopeSuffix(profile: unknown): string {
|
|
84
|
+
const name = normalizeSyncProfile(profile)
|
|
85
|
+
if (name === SYNC_DEFAULT_PROFILE) return ''
|
|
86
|
+
return SCOPE_SUFFIX_SEP + name.toUpperCase().replace(/[^A-Z0-9]/g, '_')
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 把「历史(不带后缀)」凭据槽位名映射到某档案。
|
|
91
|
+
*
|
|
92
|
+
* 只接受已经算好的历史名(调用点仍是 `SYNC_CREDENTIAL_REF` / `cloudSecretRef(x)` 那些常量),
|
|
93
|
+
* 因此全仓槽位名仍然一处声明,不会出现第二套命名规则。
|
|
94
|
+
*/
|
|
95
|
+
export function scopedSyncRef(ref: string, profile: unknown): string {
|
|
96
|
+
const suffix = syncScopeSuffix(profile)
|
|
97
|
+
return suffix === '' ? ref : ref + suffix
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** ('ENCRYPT'|'DECRYPT', 'git'|'webdav'|...) → 该通道的凭据引用名(可按档案加后缀)。 */
|
|
101
|
+
export function syncPasswordRef(kind: 'ENCRYPT' | 'DECRYPT', channel: string, profile?: unknown): string {
|
|
102
|
+
const prefix = kind === 'ENCRYPT' ? SYNC_ENCRYPT_PASSWORD_REF_PREFIX : SYNC_DECRYPT_PASSWORD_REF_PREFIX
|
|
103
|
+
return scopedSyncRef(prefix + '_' + channel.toUpperCase(), profile)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** 一个档案的同步作用域(由 `syncScopeFor` 产出,纯数据 + 派生槽位名)。 */
|
|
107
|
+
export interface SyncScope {
|
|
108
|
+
/** 归一后的档案名(非法名回落默认档案) */
|
|
109
|
+
profile: string
|
|
110
|
+
/** 是否默认档案(= 历史根,路径零迁移) */
|
|
111
|
+
isDefault: boolean
|
|
112
|
+
/** 全机一份的同步根:只用来读 `backup-schedule.json`,**不要**用它读同步配置 */
|
|
113
|
+
rootDir: string
|
|
114
|
+
/** 本档案的同步作用域目录:同步引擎的全部状态都在这里 */
|
|
115
|
+
dir: string
|
|
116
|
+
/** 本档案的 git / GitHub token 槽位 */
|
|
117
|
+
tokenRef: string
|
|
118
|
+
/** 本档案的 WebDAV 口令槽位 */
|
|
119
|
+
webdavPasswordRef: string
|
|
120
|
+
/** 历史槽位名 → 本档案槽位名(云端点密钥等按需映射) */
|
|
121
|
+
scopeRef(ref: string): string
|
|
122
|
+
/** 本档案的加密 / 解密密码槽位 */
|
|
123
|
+
passwordRef(kind: 'ENCRYPT' | 'DECRYPT', channel: string): string
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* 由插件数据根与档案名算出同步作用域。
|
|
128
|
+
*
|
|
129
|
+
* @param dataDir 插件数据根(`$DSH_HOME/dsh-config-manager` 或 `config.dataDir`)
|
|
130
|
+
* @param profile 宿主给的档案名(`host.profile`);缺省 / 非法 = 默认档案
|
|
131
|
+
*/
|
|
132
|
+
export function syncScopeFor(dataDir: string, profile?: unknown): SyncScope {
|
|
133
|
+
const name = normalizeSyncProfile(profile)
|
|
134
|
+
const isDefault = name === SYNC_DEFAULT_PROFILE
|
|
135
|
+
const rootDir = join(dataDir, 'sync')
|
|
136
|
+
const dir = isDefault ? rootDir : join(dataDir, SYNC_SCOPES_DIR_NAME, name)
|
|
137
|
+
return {
|
|
138
|
+
profile: name,
|
|
139
|
+
isDefault,
|
|
140
|
+
rootDir,
|
|
141
|
+
dir,
|
|
142
|
+
tokenRef: scopedSyncRef(SYNC_CREDENTIAL_REF, name),
|
|
143
|
+
webdavPasswordRef: scopedSyncRef(SYNC_WEBDAV_CREDENTIAL_REF, name),
|
|
144
|
+
scopeRef: (ref: string) => scopedSyncRef(ref, name),
|
|
145
|
+
passwordRef: (kind, channel) => syncPasswordRef(kind, channel, name),
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* 只有作用域目录、没有档案作用域的装配面(测试夹具 / 手工装配)→ 历史单作用域语义。
|
|
151
|
+
*
|
|
152
|
+
* 语义是**等价**而非猜测:历史布局里「作用域目录」就是默认档案的作用域(rootDir === dir),
|
|
153
|
+
* 槽位名也就是历史槽位名。因此这里给出的是「默认档案」这一确定的解释,不碰档案判定。
|
|
154
|
+
* 生产路径一律走 `syncScopeFor(dataDir, profile)`;类型上 `RoutesEnv.syncScope` 是必填的,
|
|
155
|
+
* 漏传只可能出现在 `as never` 之类的未类型化夹具里。
|
|
156
|
+
*/
|
|
157
|
+
export function syncScopeOrDefault(syncDir: string, scope?: SyncScope): SyncScope {
|
|
158
|
+
if (scope !== undefined) return scope
|
|
159
|
+
return {
|
|
160
|
+
profile: SYNC_DEFAULT_PROFILE,
|
|
161
|
+
isDefault: true,
|
|
162
|
+
rootDir: syncDir,
|
|
163
|
+
dir: syncDir,
|
|
164
|
+
tokenRef: SYNC_CREDENTIAL_REF,
|
|
165
|
+
webdavPasswordRef: SYNC_WEBDAV_CREDENTIAL_REF,
|
|
166
|
+
scopeRef: (ref: string) => ref,
|
|
167
|
+
passwordRef: (kind, channel) => syncPasswordRef(kind, channel),
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* 首次启用某档案的作用域时,从历史根继承的文件。
|
|
173
|
+
*
|
|
174
|
+
* 继承哪些:同步目标与「怎么同步」的配置(sync-config / cloud / selection / autosync / ui-prefs)
|
|
175
|
+
* —— 这些档案在此前就是共用历史根的配置,不继承 = 升级后同步「凭空未配置」。
|
|
176
|
+
*
|
|
177
|
+
* **不继承**:`work/`(git 工作副本,按新目标重克隆即可;复制一份反而会指向旧 origin)、
|
|
178
|
+
* `incoming/`、`snapshots/`(远端内容缓存)、`sync-state.json` 与 `sync-history.json`
|
|
179
|
+
* (那是本机同步流水;复制等于伪造「我在另一档案里同步过」)。
|
|
180
|
+
*/
|
|
181
|
+
export const SYNC_SCOPE_ADOPT_FILES = [
|
|
182
|
+
'sync-config.json',
|
|
183
|
+
'sync-cloud-config.json',
|
|
184
|
+
'sync-selection.json',
|
|
185
|
+
'sync-autosync.json',
|
|
186
|
+
'ui-prefs.json',
|
|
187
|
+
] as const
|
|
188
|
+
|
|
189
|
+
/** 历史槽位名 → 新档案槽位名(采纳时按「值只在进程内搬一次」复制,绝不落盘)。 */
|
|
190
|
+
export function syncScopeLegacyCredentialRefs(scope: SyncScope): Array<{ from: string; to: string }> {
|
|
191
|
+
if (scope.isDefault) return []
|
|
192
|
+
const pairs: Array<{ from: string; to: string }> = [
|
|
193
|
+
{ from: SYNC_CREDENTIAL_REF, to: scope.tokenRef },
|
|
194
|
+
{ from: SYNC_WEBDAV_CREDENTIAL_REF, to: scope.webdavPasswordRef },
|
|
195
|
+
]
|
|
196
|
+
for (const channel of SYNC_CHANNELS) {
|
|
197
|
+
for (const kind of ['ENCRYPT', 'DECRYPT'] as const) {
|
|
198
|
+
const base = (kind === 'ENCRYPT' ? SYNC_ENCRYPT_PASSWORD_REF_PREFIX : SYNC_DECRYPT_PASSWORD_REF_PREFIX) + '_' + channel.toUpperCase()
|
|
199
|
+
pairs.push({ from: base, to: scope.scopeRef(base) })
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
for (const provider of CLOUD_SYNC_PROVIDERS) {
|
|
203
|
+
// 云端点密钥 base 名仍由 CLOUD_SECRET_REFS 派生(唯一声明处),此处只加后缀
|
|
204
|
+
const base = CLOUD_SECRET_REFS[provider]
|
|
205
|
+
pairs.push({ from: base, to: scope.scopeRef(base) })
|
|
206
|
+
}
|
|
207
|
+
return pairs
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
async function pathExists(target: string): Promise<boolean> {
|
|
211
|
+
try {
|
|
212
|
+
await stat(target)
|
|
213
|
+
return true
|
|
214
|
+
} catch (error) {
|
|
215
|
+
if (isENOENT(error)) return false
|
|
216
|
+
throw error
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* 采纳历史根:非默认档案首次启动、自己的作用域目录还不存在时,把历史配置复制一份过来。
|
|
222
|
+
*
|
|
223
|
+
* 幂等:目录一存在就跳过(采纳前先 `mkdir`,因此「空目录」也是已采纳的标记,
|
|
224
|
+
* 不需要额外的 marker 文件)。任何单个文件复制失败都只记日志、不影响其它文件 ——
|
|
225
|
+
* 采纳是尽力而为的兼容动作,绝不能让插件启动失败。
|
|
226
|
+
*
|
|
227
|
+
* @returns 实际复制过来的文件名(未采纳 / 已采纳时为空数组)
|
|
228
|
+
*/
|
|
229
|
+
export async function adoptLegacySyncScope(
|
|
230
|
+
scope: SyncScope,
|
|
231
|
+
log?: (message: string) => void,
|
|
232
|
+
): Promise<string[]> {
|
|
233
|
+
if (scope.isDefault) return []
|
|
234
|
+
if (await pathExists(scope.dir)) return []
|
|
235
|
+
const copied: string[] = []
|
|
236
|
+
try {
|
|
237
|
+
await mkdir(scope.dir, { recursive: true })
|
|
238
|
+
} catch (error) {
|
|
239
|
+
log?.(engineMsg('sync.scopeDirCreateFailed', { dir: scope.dir, reason: String(error) }))
|
|
240
|
+
return copied
|
|
241
|
+
}
|
|
242
|
+
for (const name of SYNC_SCOPE_ADOPT_FILES) {
|
|
243
|
+
const from = join(scope.rootDir, name)
|
|
244
|
+
if (!(await pathExists(from))) continue
|
|
245
|
+
try {
|
|
246
|
+
await copyFile(from, join(scope.dir, name))
|
|
247
|
+
copied.push(name)
|
|
248
|
+
} catch (error) {
|
|
249
|
+
log?.(engineMsg('sync.scopeAdoptFailed', { file: name, reason: String(error) }))
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
if (copied.length > 0) {
|
|
253
|
+
log?.(engineMsg('sync.scopeAdopted', { profile: scope.profile, files: copied.join(', ') }))
|
|
254
|
+
}
|
|
255
|
+
return copied
|
|
256
|
+
}
|
|
@@ -12,7 +12,7 @@ import path from 'node:path';
|
|
|
12
12
|
import {
|
|
13
13
|
AUTO_SYNC_OPT_IN_SECTIONS, defaultSyncSelection, effectiveSections, normalizeSessionsInclude, normalizeSessionsLimit, OPT_IN_SYNC_SECTIONS,
|
|
14
14
|
readSyncSelection, writeSyncSelection,
|
|
15
|
-
readAllSyncSelections, DEFAULT_SYNC_SESSIONS_LIMIT, SYNC_SELECTION_FILE, SYNC_SELECTION_SCHEMA_VERSION,
|
|
15
|
+
readAllSyncSelections, readAllSyncSelectionsSync, DEFAULT_SYNC_SESSIONS_LIMIT, SYNC_SELECTION_FILE, SYNC_SELECTION_SCHEMA_VERSION,
|
|
16
16
|
} from './sync-selection.ts';
|
|
17
17
|
import { OPT_IN_SYNC_SECTION_IDS } from '../schema/section-registry.ts';
|
|
18
18
|
import type { SectionId } from '../schema/types.ts';
|
|
@@ -146,6 +146,80 @@ test('v1 迁移:顶层单通道字段 → git 通道(webdav 缺省)', asyn
|
|
|
146
146
|
} finally { await fs.rm(dir, { recursive: true, force: true }); }
|
|
147
147
|
});
|
|
148
148
|
|
|
149
|
+
/* ---------------- C6:同步读取(apply 期种入缓存) ---------------- */
|
|
150
|
+
|
|
151
|
+
test('C6:readAllSyncSelectionsSync 与异步读取逐字段一致(v2 / v1 / 损坏 / 缺失)', async () => {
|
|
152
|
+
const cases: [string, string][] = [
|
|
153
|
+
['v2', JSON.stringify({
|
|
154
|
+
schemaVersion: 2,
|
|
155
|
+
channels: {
|
|
156
|
+
git: { mode: 'advanced', sections: ['settings', 'mcp'], sessionsLimit: 3, sessionsInclude: ['s-1'], encrypt: true, includeSecrets: true },
|
|
157
|
+
webdav: { mode: 'default' },
|
|
158
|
+
},
|
|
159
|
+
})],
|
|
160
|
+
['v1', JSON.stringify({ schemaVersion: 1, mode: 'advanced', sections: ['skills'], encrypt: true })],
|
|
161
|
+
['损坏', '{ not json'],
|
|
162
|
+
['不支持 schema', JSON.stringify({ schemaVersion: 99, channels: {} })],
|
|
163
|
+
];
|
|
164
|
+
for (const [label, raw] of cases) {
|
|
165
|
+
const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'dsh-sync-selection-sync-'));
|
|
166
|
+
try {
|
|
167
|
+
await fs.writeFile(path.join(dir, SYNC_SELECTION_FILE), raw, 'utf8');
|
|
168
|
+
const synced = readAllSyncSelectionsSync(dir);
|
|
169
|
+
const awaited = await readAllSyncSelections(dir);
|
|
170
|
+
assert.deepEqual(synced, awaited, `${label}: 同步/异步必须是同一份解析结果`);
|
|
171
|
+
assert.deepEqual(effectiveSections(synced.git), effectiveSections(awaited.git), `${label}: 生效范围必须一致`);
|
|
172
|
+
} finally { await fs.rm(dir, { recursive: true, force: true }); }
|
|
173
|
+
}
|
|
174
|
+
// 文件不存在:同步版也回缺省(不抛错、不返回 undefined)
|
|
175
|
+
const empty = await fs.mkdtemp(path.join(os.tmpdir(), 'dsh-sync-selection-sync-none-'));
|
|
176
|
+
try {
|
|
177
|
+
assert.deepEqual(readAllSyncSelectionsSync(empty), await readAllSyncSelections(empty), '缺失文件两侧都回缺省');
|
|
178
|
+
assert.equal(effectiveSections(readAllSyncSelectionsSync(empty).git), undefined, '缺省 = 全部 portable');
|
|
179
|
+
} finally { await fs.rm(empty, { recursive: true, force: true }); }
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test('C6 接线守卫:apply() 必须同步种入 selectionCache(异步 fire-and-forget 会留启动窗口)', async () => {
|
|
183
|
+
const src = await fs.readFile(path.join(import.meta.dirname, '..', 'index.ts'), 'utf8');
|
|
184
|
+
assert.match(src, /const seededSelections = readAllSyncSelectionsSync\(syncDir\)/, 'apply() 必须同步读取分区选择');
|
|
185
|
+
assert.match(src, /for \(const channel of SYNC_CHANNELS\) selectionCache\[channel\] = seededSelections\[channel\]/, '同步结果必须种进全部通道');
|
|
186
|
+
assert.doesNotMatch(src, /void readAllSyncSelections\(syncDir\)\.then\(/, '不得再用 fire-and-forget 异步种入(窗口内引擎会兜底 defaultSyncSelection)');
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test('C6 守卫:路由组只能在请求处理函数内构造同步引擎(装配期构造会冻结分区选择)', async () => {
|
|
190
|
+
// 用缩进链判「最近的更外层块」:处理函数体缩进 > endpoint( 行 > 工厂函数行。
|
|
191
|
+
// 装配期构造(挂在 export function xxxRoutes(env) 直接体内)会让引擎拿到建路由那一刻的勾选,
|
|
192
|
+
// 之后保存分区选择也不生效 —— 这正是 C6 的形状,只是发生在装配期而不是启动期。
|
|
193
|
+
const dir = path.join(import.meta.dirname, '..', 'routes');
|
|
194
|
+
const files = (await fs.readdir(dir)).filter((f) => f.endsWith('.ts') && !f.endsWith('.test.ts'));
|
|
195
|
+
const offenders: string[] = [];
|
|
196
|
+
let calls = 0;
|
|
197
|
+
for (const file of files) {
|
|
198
|
+
const lines = (await fs.readFile(path.join(dir, file), 'utf8')).split('\n');
|
|
199
|
+
lines.forEach((line, i) => {
|
|
200
|
+
if (!/\bmakeSyncEngine\(/.test(line)) return;
|
|
201
|
+
calls++;
|
|
202
|
+
let limit = line.length - line.trimStart().length;
|
|
203
|
+
for (let j = i - 1; j >= 0; j--) {
|
|
204
|
+
const text = lines[j] ?? '';
|
|
205
|
+
const trimmed = text.trim();
|
|
206
|
+
if (trimmed === '' || trimmed.startsWith('//') || trimmed.startsWith('*') || trimmed.startsWith('/*')) continue;
|
|
207
|
+
const indent = text.length - text.trimStart().length;
|
|
208
|
+
if (indent >= limit) continue;
|
|
209
|
+
if (/\bendpoint\(/.test(text)) return;
|
|
210
|
+
if (indent === 0) {
|
|
211
|
+
offenders.push(file + ':' + (i + 1));
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
limit = indent;
|
|
215
|
+
}
|
|
216
|
+
offenders.push(file + ':' + (i + 1) + '(未找到 endpoint 边界)');
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
assert.deepEqual(offenders, [], '同步引擎必须在 endpoint 处理函数内构造: ' + offenders.join(', '));
|
|
220
|
+
assert.ok(calls >= 10, '至少要扫到路由组里的引擎构造点(防扫描失效): ' + calls);
|
|
221
|
+
});
|
|
222
|
+
|
|
149
223
|
/* ---------------- sessions(可选分区)与「最新 N 个」上限 ---------------- */
|
|
150
224
|
|
|
151
225
|
test('sessionsLimit: 缺省 5、写盘读回一致、非法值回退缺省、超大钳制', async () => {
|
|
@@ -230,7 +304,7 @@ test('normalizeSessionsLimit: 只有非负整数被接受', () => {
|
|
|
230
304
|
|
|
231
305
|
test('OPT_IN_SYNC_SECTIONS: 只含需要用户显式勾选的可选分区,凭据类分区永不入选', () => {
|
|
232
306
|
// 单一事实源 = 分区注册表(issue #78:此前是本文件手写的 ['sessions'],与注册表字段两份字面量、零护栏)
|
|
233
|
-
assert.deepEqual([...OPT_IN_SYNC_SECTIONS], ['mcp', 'sessions']);
|
|
307
|
+
assert.deepEqual([...OPT_IN_SYNC_SECTIONS], ['mcp', 'workspaces', 'sessions']);
|
|
234
308
|
assert.deepEqual([...OPT_IN_SYNC_SECTIONS], [...OPT_IN_SYNC_SECTION_IDS], '必须与注册表派生视图逐项一致');
|
|
235
309
|
for (const forbidden of ['credentialsStatus', 'secrets', 'settings'] as const) {
|
|
236
310
|
assert.ok(!OPT_IN_SYNC_SECTIONS.includes(forbidden), '可选分区白名单不得含 ' + forbidden);
|