dsh-config-manager 0.6.0 → 0.7.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.
Files changed (49) hide show
  1. package/lib/client.d.ts +5 -0
  2. package/lib/client.js +605 -566
  3. package/lib/core/local-plugin-host.d.ts +9 -0
  4. package/lib/core/local-plugin-host.js +174 -1
  5. package/lib/core/messages.d.ts +3 -0
  6. package/lib/core/messages.js +8 -0
  7. package/lib/core/run-registry.d.ts +10 -0
  8. package/lib/core/run-registry.js +17 -0
  9. package/lib/index.d.ts +3 -2
  10. package/lib/index.js +8 -6
  11. package/lib/routes/import.js +6 -1
  12. package/lib/routes/route-support.d.ts +2 -1
  13. package/lib/routes/route-support.js +8 -3
  14. package/lib/routes/sync.js +5 -2
  15. package/lib/sync/autosync-config.d.ts +9 -1
  16. package/lib/sync/autosync-config.js +10 -2
  17. package/lib/sync/autosync-scheduler.js +18 -6
  18. package/lib/sync/git/git-transport.d.ts +31 -1
  19. package/lib/sync/git/git-transport.js +119 -23
  20. package/lib/sync/sync-config.d.ts +34 -3
  21. package/lib/sync/sync-config.js +38 -5
  22. package/lib/ui/sync-settings-view.d.ts +4 -0
  23. package/lib/ui/sync-settings-view.js +3 -0
  24. package/package.json +1 -1
  25. package/src/client/run-store.test.ts +1 -1
  26. package/src/client/run-store.ts +5 -0
  27. package/src/client/sync/ChannelConfigDialog.tsx +15 -1
  28. package/src/client/sync/SyncPanel.tsx +6 -0
  29. package/src/client/sync/sync-api.ts +2 -0
  30. package/src/client/sync/sync-locales.ts +6 -0
  31. package/src/core/local-plugin-host.test.ts +31 -2
  32. package/src/core/local-plugin-host.ts +175 -2
  33. package/src/core/messages.ts +8 -0
  34. package/src/core/run-registry-runs.test.ts +26 -1
  35. package/src/core/run-registry.ts +18 -0
  36. package/src/index.sync.test.ts +27 -0
  37. package/src/index.ts +10 -8
  38. package/src/routes/import.ts +6 -1
  39. package/src/routes/route-support.ts +7 -3
  40. package/src/routes/sync.ts +5 -2
  41. package/src/sync/autosync-config.ts +10 -2
  42. package/src/sync/autosync-scheduler.test.ts +61 -1
  43. package/src/sync/autosync-scheduler.ts +17 -5
  44. package/src/sync/git/git-transport.test.ts +117 -0
  45. package/src/sync/git/git-transport.ts +131 -23
  46. package/src/sync/sync-config.test.ts +59 -1
  47. package/src/sync/sync-config.ts +51 -5
  48. package/src/ui/sync-settings-view.test.ts +1 -0
  49. package/src/ui/sync-settings-view.ts +7 -0
@@ -26,7 +26,7 @@ import {
26
26
  SYNC_CONFIG_FILE, SYNC_CONFIG_SCHEMA_VERSION, SYNC_CONFIG_SUPPORTED_VERSIONS, CLOUD_SYNC_CONFIG_FILE,
27
27
  validateWebDavUrl, validateRepoUrl, type SyncConfig, type SyncConfigIssue,
28
28
  SYNC_CHANNELS, channelOf, channelMap, isSyncTransportType, parseSyncChannel, clearSyncChannel,
29
- S3_COMPAT_PROVIDERS, CLOUD_SYNC_PROVIDERS, cloudSecretRef,
29
+ S3_COMPAT_PROVIDERS, CLOUD_SYNC_PROVIDERS, cloudSecretRef, normalizeGitSubdir,
30
30
  } from './sync-config.ts';
31
31
  // t88 跨侧常量守卫:共享零依赖常量 + 客户端镜像(见文件末尾的 t88 用例)
32
32
  import { S3_COMPAT_PROVIDERS as SHARED_S3_COMPAT_PROVIDERS, cloudSecretRefName } from '../utils/shared-constants.ts';
@@ -919,3 +919,61 @@ test('validateRepoUrl(RV1-F7):带 scheme 的 userinfo —— ssh 的 token
919
919
  assert.ok(half !== null);
920
920
  assert.ok(!half.includes('ghp_halfsecret'), '无法解析的地址同样不得回显凭据: ' + half);
921
921
  });
922
+
923
+ test('issue #131:normalizeGitSubdir —— 中文与嵌套放行,绝对路径/../反斜杠/空段/.git 段拒绝', () => {
924
+ const ok = (raw: unknown): string => {
925
+ const r = normalizeGitSubdir(raw);
926
+ assert.equal(r.ok, true, '应放行: ' + String(raw));
927
+ return r.ok ? r.value : '';
928
+ };
929
+ const bad = (raw: unknown): void => {
930
+ assert.equal(normalizeGitSubdir(raw).ok, false, '应拒绝: ' + String(raw));
931
+ };
932
+ assert.equal(ok(undefined), '', '缺省 = 仓库根');
933
+ assert.equal(ok(''), '', '空串 = 仓库根');
934
+ assert.equal(ok(' '), '', '纯空白 = 仓库根');
935
+ assert.equal(ok(' DSH备份 '), 'DSH备份', '去首尾空白,保留 UTF-8');
936
+ assert.equal(ok('tools/dsh'), 'tools/dsh', '允许嵌套');
937
+ for (const raw of ['/abs', 'a/', '/', 'a//b', '..', 'a/../b', './a', 'a/.', '.git', 'a/.git/b', 'a/.GIT', 'a\\b', 'a\u0000b', 123, true]) bad(raw);
938
+ });
939
+
940
+ test('issue #131:git 子目录前缀往返 —— 写入保留字段,空/缺省按仓库根读回,旧 v2 文件不受影响', async () => {
941
+ const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'dsh-sync-cfg-subdir-'));
942
+ try {
943
+ const cfg: SyncConfig = {
944
+ schemaVersion: 2,
945
+ transport: 'git',
946
+ git: { repoUrl: 'git@github.com:foo/bar.git', subdir: 'DSH备份' },
947
+ };
948
+ await writeSyncConfig(dir, cfg);
949
+ assert.deepEqual(await readSyncConfig(dir), cfg, '子目录前缀必须原样往返');
950
+ const raw = JSON.parse(await fs.readFile(path.join(dir, SYNC_CONFIG_FILE), 'utf8'));
951
+ assert.equal(raw.git.subdir, 'DSH备份');
952
+ assert.equal(raw.schemaVersion, SYNC_CONFIG_SCHEMA_VERSION, '带新字段的文件按当前版本写');
953
+
954
+ // 旧 v2 文件(无 subdir)→ 读成仓库根布局,不因新字段缺失而丢配置
955
+ await fs.writeFile(path.join(dir, SYNC_CONFIG_FILE), JSON.stringify({
956
+ schemaVersion: 2,
957
+ transport: 'git',
958
+ git: { repoUrl: 'git@github.com:foo/bar.git' },
959
+ }));
960
+ assert.deepEqual(await readSyncConfig(dir), {
961
+ schemaVersion: 2,
962
+ transport: 'git',
963
+ git: { repoUrl: 'git@github.com:foo/bar.git' },
964
+ });
965
+
966
+ // 非法 subdir(历史文件被人改坏)→ 丢弃该字段而不是整份配置报错
967
+ await fs.writeFile(path.join(dir, SYNC_CONFIG_FILE), JSON.stringify({
968
+ schemaVersion: 2,
969
+ transport: 'git',
970
+ git: { repoUrl: 'git@github.com:foo/bar.git', subdir: '../escape' },
971
+ }));
972
+ assert.deepEqual(await readSyncConfig(dir), {
973
+ schemaVersion: 2,
974
+ transport: 'git',
975
+ git: { repoUrl: 'git@github.com:foo/bar.git' },
976
+ }, '非法前缀按仓库根处理(不拿坏值去拼路径)');
977
+ } finally { await fs.rm(dir, { recursive: true, force: true }); }
978
+ });
979
+
@@ -87,14 +87,26 @@ export function channelMap<T>(make: (channel: SyncTransportType) => T): Record<S
87
87
  return out;
88
88
  }
89
89
 
90
- /** 当前 sync-config.json schema 版本号(v3:双命名空间共存,切换通道不丢失另一通道配置)。 */
91
- export const SYNC_CONFIG_SCHEMA_VERSION = 3;
92
- /** 历史可读取版本:v1、v2、v3。 */
93
- export const SYNC_CONFIG_SUPPORTED_VERSIONS: readonly number[] = [1, 2, 3];
90
+ /**
91
+ * 当前 sync-config.json schema 版本号(v4:git 通道新增可选 `subdir` 子目录前缀,issue #131)。
92
+ *
93
+ * 为什么必须升版而不是只加一个可选字段:老版本插件读到带 `subdir` 的配置会**丢掉这个字段**
94
+ * 然后照旧往仓库根写快照 —— 在「多工具共用一个备份仓库」的现场,那等于把快照撒进别人的目录布局。
95
+ * 升版后老版本读到 v4 直接判为不支持(显示未配置),宁可让用户看见「要升级」,也不静默写错位置。
96
+ */
97
+ export const SYNC_CONFIG_SCHEMA_VERSION = 4;
98
+ /** 历史可读取版本:v1、v2、v3、v4。 */
99
+ export const SYNC_CONFIG_SUPPORTED_VERSIONS: readonly number[] = [1, 2, 3, 4];
94
100
 
95
101
  /** git 通道配置(不含任何凭据;git 可执行文件固定使用系统 PATH 中的 git) */
96
102
  export interface GitConfig {
97
103
  repoUrl: string;
104
+ /**
105
+ * 仓库内子目录前缀(issue #131,可缺省):快照布局落在 `<subdir>/snapshots`、
106
+ * `<subdir>/snapshots-encrypted`、`<subdir>/blobs`。缺省 = 仓库根(历史行为逐字不变)。
107
+ * 多工具共用同一个私有备份仓库时,每个工具各占一个子目录。
108
+ */
109
+ subdir?: string;
98
110
  }
99
111
 
100
112
  /** webdav 通道配置(不含 password;password 走 DSH credentials) */
@@ -149,7 +161,12 @@ export function isGistConfig(cfg: SyncConfig): cfg is Extract<SyncConfig, { tran
149
161
  /** 从 v1 扁平形态解析 git 配置;缺 repoUrl → null(gitBin 已废弃:始终使用系统 PATH 中的 git) */
150
162
  function parseV1Git(obj: Record<string, unknown>): GitConfig | null {
151
163
  if (typeof obj['repoUrl'] !== 'string' || obj['repoUrl'] === '') return null;
152
- return { repoUrl: obj['repoUrl'] };
164
+ // issue #131:子目录前缀必须在这里**显式搬运** —— 本函数只构造已知字段,漏搬就是静默丢掉用户配置。
165
+ // 读侧同口径校验:非法值当作「未设置」(落在仓库根),绝不把可疑路径继续交给 git 子进程。
166
+ const subdir = normalizeGitSubdir(obj['subdir']);
167
+ return subdir.ok && subdir.value !== ''
168
+ ? { repoUrl: obj['repoUrl'], subdir: subdir.value }
169
+ : { repoUrl: obj['repoUrl'] };
153
170
  }
154
171
 
155
172
  /** 从 v2 git 命名空间解析;缺有效 repoUrl → null */
@@ -917,6 +934,35 @@ export function validateCloudBucket(bucket: string): CloudConfigIssueCode | null
917
934
  return null;
918
935
  }
919
936
 
937
+ /**
938
+ * git 通道子目录前缀的归一化与校验(issue #131)。
939
+ *
940
+ * 为什么单独一个函数而不是复用 validateCloudPrefix:对象键前缀与仓库内目录名不是同一套约束 ——
941
+ * 目录名要允许 UTF-8(报告者现场就是 `DSH备份/` 这种中文目录)与多级嵌套,而对象键前缀按
942
+ * 存储桶语义只关心「不是绝对路径、没有 ..」。
943
+ *
944
+ * 接受:空串(= 仓库根,历史行为)、UTF-8 段、多级嵌套(`a/b`)。
945
+ * 拒绝:绝对路径(首部 /)、尾部 /、反斜杠、空段(含 `a//b`)、`.` / `..` 段、控制字符、
946
+ * `.git` 段(写进去会撞 git 自己的元数据目录名,任何大小写都算)。
947
+ */
948
+ export type GitSubdirCheck = { ok: true; value: string } | { ok: false; reason: 'gitSubdirInvalid' };
949
+
950
+ export function normalizeGitSubdir(raw: unknown): GitSubdirCheck {
951
+ // 缺省(字段不存在)与空串同义:仓库根。只有「显式写了但不是字符串」才算非法。
952
+ if (raw === undefined || raw === null) return { ok: true, value: '' };
953
+ if (typeof raw !== 'string') return { ok: false, reason: 'gitSubdirInvalid' };
954
+ const p = raw.trim();
955
+ if (p === '') return { ok: true, value: '' };
956
+ if (p.startsWith('/') || p.endsWith('/')) return { ok: false, reason: 'gitSubdirInvalid' };
957
+ if (p.includes('\\') || /[\u0000-\u001f\u007f]/.test(p)) return { ok: false, reason: 'gitSubdirInvalid' };
958
+ const segments = p.split('/');
959
+ for (const seg of segments) {
960
+ if (seg === '' || seg === '.' || seg === '..') return { ok: false, reason: 'gitSubdirInvalid' };
961
+ if (seg.toLowerCase() === '.git') return { ok: false, reason: 'gitSubdirInvalid' };
962
+ }
963
+ return { ok: true, value: segments.join('/') };
964
+ }
965
+
920
966
  /** 对象键前缀校验(可缺省 = 根前缀;不得以 / 开头、不得含 .. / 反斜杠 / 空段)。 */
921
967
  export function validateCloudPrefix(prefix: string): CloudConfigIssueCode | null {
922
968
  if (typeof prefix !== 'string') return 'cloud.prefixInvalid';
@@ -35,6 +35,7 @@ function form(overrides: Partial<SyncFormSnapshot> = {}): SyncFormSnapshot {
35
35
  return {
36
36
  channel: 'git',
37
37
  repoUrl: ' https://github.com/u/repo.git ',
38
+ gitSubdir: '',
38
39
  token: ' GIT-TOKEN ',
39
40
  webdavUrl: ' https://dav.example.com/dav ',
40
41
  webdavUsername: ' alice ',
@@ -41,6 +41,8 @@ export type SyncAutosyncInterval = '5m' | '15m' | '30m' | '60m' | '6h' | '12h' |
41
41
  export interface SyncFormSnapshot {
42
42
  channel: SyncChannelName
43
43
  repoUrl: string
44
+ /** 仓库内子目录前缀(空 = 仓库根;issue #131:多工具共用一个私有仓库时各自独立) */
45
+ gitSubdir: string
44
46
  /** 仅内存;成功后由组件清空(已写入 DSH credentials) */
45
47
  token: string
46
48
  webdavUrl: string
@@ -102,6 +104,8 @@ export interface SyncPushBody {
102
104
  */
103
105
  transport?: SyncTransportType
104
106
  repoUrl?: string
107
+ /** git:仓库内子目录前缀(issue #131;空 / 缺省 = 仓库根,旧配置读出来即空) */
108
+ subdir?: string
105
109
  token?: string
106
110
  url?: string
107
111
  username?: string
@@ -293,9 +297,12 @@ function channelAuthBody(form: SyncFormSnapshot, opts: { trimToken: boolean }):
293
297
  ...(token === '' ? {} : { secret: token }),
294
298
  }
295
299
  }
300
+ const subdir = form.gitSubdir.trim()
296
301
  return {
297
302
  transport: 'git',
298
303
  repoUrl: form.repoUrl.trim(),
304
+ // 空串不携带:宿主按「缺省 = 仓库根」解析,旧配置与旧宿主都能吃这份载荷
305
+ ...(subdir === '' ? {} : { subdir }),
299
306
  token: form.token.trim() !== '' ? (opts.trimToken ? form.token.trim() : form.token) : undefined,
300
307
  }
301
308
  }