dsh-skill-harbor 0.5.4 → 0.6.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.
@@ -1,90 +1,90 @@
1
- /**
2
- * 仓库源模型(findings:ref + commitSha 双存,id 由 hash 推导)。
3
- *
4
- * 关键设计:
5
- * - `ref` 是用户可读的(`main` / `v1.2.0`),`commitSha` 是钉死的实际提交。
6
- * 只存 ref 会在上游推进后悄悄改变内容(浮动 ref 事故),只存 sha 又没法更新。
7
- * - `id` 由内容 hash 推导而非随机 UUID:状态文件被篡改时 id 重算不出来,
8
- * 加载即失败 —— 这正是我们想要的(宁可拒绝,不要静默接受被改过的状态)。
9
- */
10
- import { createHash } from 'node:crypto'
11
-
12
- /** 一个仓库源的规范化形状。 */
13
- export function makeSource({ owner, repo, ref, commitSha }) {
14
- const normalized = {
15
- owner: String(owner ?? '').trim(),
16
- repo: String(repo ?? '').trim(),
17
- ref: String(ref ?? 'HEAD').trim(),
18
- commitSha: String(commitSha ?? '').trim().toLowerCase(),
19
- }
20
- return { ...normalized, id: deriveId(normalized) }
21
- }
22
-
23
- /** id = hash(owner/repo@ref#sha) 的前 16 位。改任一字段 id 就变。 */
24
- export function deriveId({ owner, repo, ref, commitSha }) {
25
- const material = `${owner}/${repo}@${ref}#${commitSha}`
26
- return createHash('sha256').update(material).digest('hex').slice(0, 16)
27
- }
28
-
29
- /** sha 形状校验:完整 40 位或短 7-40 位十六进制。 */
30
- export function isCommitSha(value) {
31
- return typeof value === 'string' && /^[0-9a-f]{7,40}$/i.test(value)
32
- }
33
-
34
- /** 校验并解析一个持久化的源记录;id 不匹配即拒绝(篡改检测)。 */
35
- export function loadSource(record) {
36
- if (record === null || typeof record !== 'object') {
37
- return { ok: false, reason: 'not-an-object' }
38
- }
39
- const { owner, repo, ref, commitSha, id } = record
40
- if (typeof owner !== 'string' || owner === '') return { ok: false, reason: 'missing-owner' }
41
- if (typeof repo !== 'string' || repo === '') return { ok: false, reason: 'missing-repo' }
42
- if (!isCommitSha(commitSha)) return { ok: false, reason: 'invalid-sha' }
43
-
44
- const expected = deriveId({ owner, repo, ref, commitSha })
45
- if (id !== expected) {
46
- return { ok: false, reason: 'id-mismatch', expected, actual: id }
47
- }
48
- return { ok: true, source: { owner, repo, ref, commitSha, id: expected } }
49
- }
50
-
51
- /** 序列化一组源(稳定排序,便于 diff)。 */
52
- export function serializeSources(sources) {
53
- return JSON.stringify(
54
- [...sources].sort((a, b) => a.id.localeCompare(b.id)),
55
- null,
56
- 2,
57
- )
58
- }
59
-
60
- /**
61
- * 比较「已安装的钉死 sha」与「远端最新 sha」。
62
- * 两者不同即视为有更新 —— 但**不自动同步**,由用户决定。
63
- */
64
- export function checkUpdate(installedSha, remoteSha) {
65
- if (!isCommitSha(installedSha) || !isCommitSha(remoteSha)) {
66
- return { ok: false, reason: 'invalid-sha', updateAvailable: null }
67
- }
68
- const installed = installedSha.toLowerCase()
69
- const remote = remoteSha.toLowerCase()
70
- return {
71
- ok: true,
72
- updateAvailable: installed !== remote,
73
- installedSha: installed,
74
- remoteSha: remote,
75
- }
76
- }
77
-
78
- /** 同步:对齐到钉住的目标 sha,而不是跟随 ref 漂移。 */
79
- export function applySync(source, remoteSha) {
80
- if (!isCommitSha(remoteSha)) {
81
- return { ok: false, reason: 'invalid-sha' }
82
- }
83
- const next = makeSource({
84
- owner: source.owner,
85
- repo: source.repo,
86
- ref: source.ref,
87
- commitSha: remoteSha,
88
- })
89
- return { ok: true, source: next, changed: next.id !== source.id }
90
- }
1
+ /**
2
+ * 仓库源模型(findings:ref + commitSha 双存,id 由 hash 推导)。
3
+ *
4
+ * 关键设计:
5
+ * - `ref` 是用户可读的(`main` / `v1.2.0`),`commitSha` 是钉死的实际提交。
6
+ * 只存 ref 会在上游推进后悄悄改变内容(浮动 ref 事故),只存 sha 又没法更新。
7
+ * - `id` 由内容 hash 推导而非随机 UUID:状态文件被篡改时 id 重算不出来,
8
+ * 加载即失败 —— 这正是我们想要的(宁可拒绝,不要静默接受被改过的状态)。
9
+ */
10
+ import { createHash } from 'node:crypto'
11
+
12
+ /** 一个仓库源的规范化形状。 */
13
+ export function makeSource({ owner, repo, ref, commitSha }) {
14
+ const normalized = {
15
+ owner: String(owner ?? '').trim(),
16
+ repo: String(repo ?? '').trim(),
17
+ ref: String(ref ?? 'HEAD').trim(),
18
+ commitSha: String(commitSha ?? '').trim().toLowerCase(),
19
+ }
20
+ return { ...normalized, id: deriveId(normalized) }
21
+ }
22
+
23
+ /** id = hash(owner/repo@ref#sha) 的前 16 位。改任一字段 id 就变。 */
24
+ export function deriveId({ owner, repo, ref, commitSha }) {
25
+ const material = `${owner}/${repo}@${ref}#${commitSha}`
26
+ return createHash('sha256').update(material).digest('hex').slice(0, 16)
27
+ }
28
+
29
+ /** sha 形状校验:完整 40 位或短 7-40 位十六进制。 */
30
+ export function isCommitSha(value) {
31
+ return typeof value === 'string' && /^[0-9a-f]{7,40}$/i.test(value)
32
+ }
33
+
34
+ /** 校验并解析一个持久化的源记录;id 不匹配即拒绝(篡改检测)。 */
35
+ export function loadSource(record) {
36
+ if (record === null || typeof record !== 'object') {
37
+ return { ok: false, reason: 'not-an-object' }
38
+ }
39
+ const { owner, repo, ref, commitSha, id } = record
40
+ if (typeof owner !== 'string' || owner === '') return { ok: false, reason: 'missing-owner' }
41
+ if (typeof repo !== 'string' || repo === '') return { ok: false, reason: 'missing-repo' }
42
+ if (!isCommitSha(commitSha)) return { ok: false, reason: 'invalid-sha' }
43
+
44
+ const expected = deriveId({ owner, repo, ref, commitSha })
45
+ if (id !== expected) {
46
+ return { ok: false, reason: 'id-mismatch', expected, actual: id }
47
+ }
48
+ return { ok: true, source: { owner, repo, ref, commitSha, id: expected } }
49
+ }
50
+
51
+ /** 序列化一组源(稳定排序,便于 diff)。 */
52
+ export function serializeSources(sources) {
53
+ return JSON.stringify(
54
+ [...sources].sort((a, b) => a.id.localeCompare(b.id)),
55
+ null,
56
+ 2,
57
+ )
58
+ }
59
+
60
+ /**
61
+ * 比较「已安装的钉死 sha」与「远端最新 sha」。
62
+ * 两者不同即视为有更新 —— 但**不自动同步**,由用户决定。
63
+ */
64
+ export function checkUpdate(installedSha, remoteSha) {
65
+ if (!isCommitSha(installedSha) || !isCommitSha(remoteSha)) {
66
+ return { ok: false, reason: 'invalid-sha', updateAvailable: null }
67
+ }
68
+ const installed = installedSha.toLowerCase()
69
+ const remote = remoteSha.toLowerCase()
70
+ return {
71
+ ok: true,
72
+ updateAvailable: installed !== remote,
73
+ installedSha: installed,
74
+ remoteSha: remote,
75
+ }
76
+ }
77
+
78
+ /** 同步:对齐到钉住的目标 sha,而不是跟随 ref 漂移。 */
79
+ export function applySync(source, remoteSha) {
80
+ if (!isCommitSha(remoteSha)) {
81
+ return { ok: false, reason: 'invalid-sha' }
82
+ }
83
+ const next = makeSource({
84
+ owner: source.owner,
85
+ repo: source.repo,
86
+ ref: source.ref,
87
+ commitSha: remoteSha,
88
+ })
89
+ return { ok: true, source: next, changed: next.id !== source.id }
90
+ }
@@ -1,66 +1,66 @@
1
- /**
2
- * 市场源持久化。
3
- *
4
- * 落在 `~/.dsh/skill-harbor/sources.json`(与前面所有路径同一套 base)。
5
- * 读取时对每条记录做 `loadSource()` 校验:id 与内容 hash 不符即整条丢弃,
6
- * 并回报丢弃原因 —— 宁可不显示,也不显示一条被篡改过的记录。
7
- */
8
- import { existsSync, mkdirSync, readFileSync, writeFileSync, renameSync } from 'node:fs'
9
- import { dirname, join } from 'node:path'
10
-
11
- import { loadSource, serializeSources } from '../market/source.js'
12
-
13
- /** 状态文件路径。 */
14
- export function sourcesFile(config) {
15
- const base = config.dshHome ?? join(process.env.USERPROFILE ?? process.env.HOME ?? '.', '.dsh')
16
- return join(base, 'skill-harbor', 'sources.json')
17
- }
18
-
19
- /**
20
- * 读取全部源。返回对象同时带 `rejected`,便于 UI 提示「有条目被丢弃」。
21
- */
22
- export function readSources(config) {
23
- const file = sourcesFile(config)
24
- if (!existsSync(file)) return { sources: [], rejected: [] }
25
-
26
- let parsed
27
- try {
28
- parsed = JSON.parse(readFileSync(file, 'utf8'))
29
- } catch (error) {
30
- return { sources: [], rejected: [{ reason: 'unreadable', message: String(error?.message ?? error) }] }
31
- }
32
- if (!Array.isArray(parsed)) {
33
- return { sources: [], rejected: [{ reason: 'not-an-array' }] }
34
- }
35
-
36
- const sources = []
37
- const rejected = []
38
- for (const record of parsed) {
39
- const result = loadSource(record)
40
- if (result.ok) sources.push(result.source)
41
- else rejected.push({ reason: result.reason, record })
42
- }
43
- return { sources, rejected }
44
- }
45
-
46
- /** 写入(原子 rename,避免半截文件)。 */
47
- export function writeSources(config, sources) {
48
- const file = sourcesFile(config)
49
- mkdirSync(dirname(file), { recursive: true })
50
- const temp = `${file}.tmp-${process.pid}`
51
- writeFileSync(temp, serializeSources(sources), 'utf8')
52
- renameSync(temp, file)
53
- return file
54
- }
55
-
56
- /**
57
- * 保存一个源:同 id 覆盖,否则追加。
58
- * 若读取时发现有被丢弃的条目,**不写回**它们(丢弃是永久的,避免把坏数据带下去)。
59
- */
60
- export function saveSource(config, source) {
61
- const { sources } = readSources(config)
62
- const next = sources.filter(item => item.id !== source.id)
63
- next.push(source)
64
- writeSources(config, next)
65
- return source
66
- }
1
+ /**
2
+ * 市场源持久化。
3
+ *
4
+ * 落在 `~/.dsh/skill-harbor/sources.json`(与前面所有路径同一套 base)。
5
+ * 读取时对每条记录做 `loadSource()` 校验:id 与内容 hash 不符即整条丢弃,
6
+ * 并回报丢弃原因 —— 宁可不显示,也不显示一条被篡改过的记录。
7
+ */
8
+ import { existsSync, mkdirSync, readFileSync, writeFileSync, renameSync } from 'node:fs'
9
+ import { dirname, join } from 'node:path'
10
+
11
+ import { loadSource, serializeSources } from '../market/source.js'
12
+
13
+ /** 状态文件路径。 */
14
+ export function sourcesFile(config) {
15
+ const base = config.dshHome ?? join(process.env.USERPROFILE ?? process.env.HOME ?? '.', '.dsh')
16
+ return join(base, 'skill-harbor', 'sources.json')
17
+ }
18
+
19
+ /**
20
+ * 读取全部源。返回对象同时带 `rejected`,便于 UI 提示「有条目被丢弃」。
21
+ */
22
+ export function readSources(config) {
23
+ const file = sourcesFile(config)
24
+ if (!existsSync(file)) return { sources: [], rejected: [] }
25
+
26
+ let parsed
27
+ try {
28
+ parsed = JSON.parse(readFileSync(file, 'utf8'))
29
+ } catch (error) {
30
+ return { sources: [], rejected: [{ reason: 'unreadable', message: String(error?.message ?? error) }] }
31
+ }
32
+ if (!Array.isArray(parsed)) {
33
+ return { sources: [], rejected: [{ reason: 'not-an-array' }] }
34
+ }
35
+
36
+ const sources = []
37
+ const rejected = []
38
+ for (const record of parsed) {
39
+ const result = loadSource(record)
40
+ if (result.ok) sources.push(result.source)
41
+ else rejected.push({ reason: result.reason, record })
42
+ }
43
+ return { sources, rejected }
44
+ }
45
+
46
+ /** 写入(原子 rename,避免半截文件)。 */
47
+ export function writeSources(config, sources) {
48
+ const file = sourcesFile(config)
49
+ mkdirSync(dirname(file), { recursive: true })
50
+ const temp = `${file}.tmp-${process.pid}`
51
+ writeFileSync(temp, serializeSources(sources), 'utf8')
52
+ renameSync(temp, file)
53
+ return file
54
+ }
55
+
56
+ /**
57
+ * 保存一个源:同 id 覆盖,否则追加。
58
+ * 若读取时发现有被丢弃的条目,**不写回**它们(丢弃是永久的,避免把坏数据带下去)。
59
+ */
60
+ export function saveSource(config, source) {
61
+ const { sources } = readSources(config)
62
+ const next = sources.filter(item => item.id !== source.id)
63
+ next.push(source)
64
+ writeSources(config, next)
65
+ return source
66
+ }