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.
- package/LICENSE +21 -21
- package/README.md +44 -44
- package/cordis.patch.yml +4 -4
- package/dsh.plugin.json +1 -1
- package/package.json +2 -1
- package/src/audit/normalize.js +77 -77
- package/src/audit/rules.js +167 -167
- package/src/budget/announcement.js +53 -53
- package/src/client/api.js +79 -79
- package/src/client/labels.js +6 -6
- package/src/client/locales.js +688 -688
- package/src/collect/frontmatter.js +169 -169
- package/src/collect/observe.js +143 -143
- package/src/collect/ranks.js +56 -56
- package/src/collect/roots.js +92 -92
- package/src/collect/shadow.js +68 -68
- package/src/collect/watcher.js +86 -86
- package/src/config.js +42 -42
- package/src/market/fetch.js +94 -94
- package/src/market/install.js +169 -169
- package/src/market/source.js +90 -90
- package/src/market/store.js +66 -66
- package/src/market/zip.js +201 -201
- package/src/provider/lifecycle.js +74 -74
- package/src/routes/guard.js +156 -156
- package/src/routes/safe-path.js +96 -96
- package/src/routes/store.js +72 -72
- package/src/settings.js +83 -83
package/src/market/zip.js
CHANGED
|
@@ -1,201 +1,201 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 零依赖 ZIP 读取器 —— 只做导入需要的部分。
|
|
3
|
-
*
|
|
4
|
-
* 不引 jszip/yauzl:导入路径是安全敏感面,第三方解析器的面比我们需要的宽得多
|
|
5
|
-
* (支持加密、zip64、跨卷…)。这里只实现「中央目录 → 本地头 → deflate 解压」,
|
|
6
|
-
* 遇到任何不认识的结构直接拒绝,而不是猜。
|
|
7
|
-
*
|
|
8
|
-
* 安全纪律(findings A5):
|
|
9
|
-
* - 符号链接**跳过而非跟随**
|
|
10
|
-
* - 条目数 / 单文件字节 / 总解压字节 三道上限在解压**过程中**实时检查
|
|
11
|
-
* (不能先全部解压再数——那就已经被解压炸弹打穿了)
|
|
12
|
-
*/
|
|
13
|
-
import { inflateRawSync } from 'node:zlib'
|
|
14
|
-
|
|
15
|
-
const SIG_EOCD = 0x06054b50
|
|
16
|
-
const SIG_CENTRAL = 0x02014b50
|
|
17
|
-
const SIG_LOCAL = 0x04034b50
|
|
18
|
-
|
|
19
|
-
const METHOD_STORE = 0
|
|
20
|
-
const METHOD_DEFLATE = 8
|
|
21
|
-
|
|
22
|
-
/** ZIP 外部属性里的 Unix 文件类型位。 */
|
|
23
|
-
const S_IFMT = 0o170000
|
|
24
|
-
const S_IFLNK = 0o120000
|
|
25
|
-
|
|
26
|
-
export const IMPORT_LIMITS = Object.freeze({
|
|
27
|
-
archiveBytes: 8 * 1024 * 1024,
|
|
28
|
-
entries: 512,
|
|
29
|
-
skillBytes: 256 * 1024,
|
|
30
|
-
totalBytes: 16 * 1024 * 1024,
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
export class ZipError extends Error {
|
|
34
|
-
constructor(code, message) {
|
|
35
|
-
super(message)
|
|
36
|
-
this.code = code
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** 找 EOCD(从尾部往前扫,最多 64KB 注释)。 */
|
|
41
|
-
function findEocd(buffer) {
|
|
42
|
-
const maxComment = 0xffff
|
|
43
|
-
const start = Math.max(0, buffer.length - maxComment - 22)
|
|
44
|
-
for (let i = buffer.length - 22; i >= start; i -= 1) {
|
|
45
|
-
if (buffer.readUInt32LE(i) === SIG_EOCD) return i
|
|
46
|
-
}
|
|
47
|
-
return -1
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* 读取 ZIP 的条目清单(不解压)。
|
|
52
|
-
* @returns {Array<{name:string, method:number, compressedSize:number, uncompressedSize:number, localOffset:number, isSymlink:boolean}>}
|
|
53
|
-
*/
|
|
54
|
-
export function listEntries(buffer) {
|
|
55
|
-
if (buffer.length > IMPORT_LIMITS.archiveBytes) {
|
|
56
|
-
throw new ZipError('archive-too-large', `归档 ${buffer.length}B 超过上限 ${IMPORT_LIMITS.archiveBytes}B`)
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const eocd = findEocd(buffer)
|
|
60
|
-
if (eocd === -1) throw new ZipError('not-a-zip', '找不到 EOCD 签名')
|
|
61
|
-
|
|
62
|
-
const entryCount = buffer.readUInt16LE(eocd + 10)
|
|
63
|
-
const centralOffset = buffer.readUInt32LE(eocd + 16)
|
|
64
|
-
|
|
65
|
-
if (entryCount > IMPORT_LIMITS.entries) {
|
|
66
|
-
throw new ZipError('too-many-entries', `条目数 ${entryCount} 超过上限 ${IMPORT_LIMITS.entries}`)
|
|
67
|
-
}
|
|
68
|
-
if (centralOffset >= buffer.length) {
|
|
69
|
-
throw new ZipError('bad-central-offset', '中央目录偏移越界')
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const entries = []
|
|
73
|
-
let cursor = centralOffset
|
|
74
|
-
|
|
75
|
-
for (let i = 0; i < entryCount; i += 1) {
|
|
76
|
-
if (cursor + 46 > buffer.length) throw new ZipError('truncated-central', '中央目录被截断')
|
|
77
|
-
if (buffer.readUInt32LE(cursor) !== SIG_CENTRAL) {
|
|
78
|
-
throw new ZipError('bad-central-signature', `第 ${i} 条中央目录签名不符`)
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const flags = buffer.readUInt16LE(cursor + 8)
|
|
82
|
-
const method = buffer.readUInt16LE(cursor + 10)
|
|
83
|
-
const compressedSize = buffer.readUInt32LE(cursor + 20)
|
|
84
|
-
const uncompressedSize = buffer.readUInt32LE(cursor + 24)
|
|
85
|
-
const nameLen = buffer.readUInt16LE(cursor + 28)
|
|
86
|
-
const extraLen = buffer.readUInt16LE(cursor + 30)
|
|
87
|
-
const commentLen = buffer.readUInt16LE(cursor + 32)
|
|
88
|
-
const externalAttr = buffer.readUInt32LE(cursor + 38)
|
|
89
|
-
const localOffset = buffer.readUInt32LE(cursor + 42)
|
|
90
|
-
|
|
91
|
-
if (cursor + 46 + nameLen > buffer.length) throw new ZipError('truncated-name', '条目名被截断')
|
|
92
|
-
const name = buffer.toString('utf8', cursor + 46, cursor + 46 + nameLen)
|
|
93
|
-
|
|
94
|
-
// 加密条目一律拒绝(我们不解密,也无法判断内容)
|
|
95
|
-
if ((flags & 0x1) !== 0) throw new ZipError('encrypted-entry', `条目被加密:${name}`)
|
|
96
|
-
|
|
97
|
-
const unixMode = (externalAttr >>> 16) & 0xffff
|
|
98
|
-
const isSymlink = (unixMode & S_IFMT) === S_IFLNK
|
|
99
|
-
|
|
100
|
-
entries.push({
|
|
101
|
-
name,
|
|
102
|
-
method,
|
|
103
|
-
compressedSize,
|
|
104
|
-
uncompressedSize,
|
|
105
|
-
localOffset,
|
|
106
|
-
isSymlink,
|
|
107
|
-
})
|
|
108
|
-
|
|
109
|
-
cursor += 46 + nameLen + extraLen + commentLen
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
return entries
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* 解压单个条目。上限在解压时实时校验。
|
|
117
|
-
* @returns {Buffer}
|
|
118
|
-
*/
|
|
119
|
-
export function readEntry(buffer, entry, limits = IMPORT_LIMITS) {
|
|
120
|
-
if (entry.localOffset + 30 > buffer.length) {
|
|
121
|
-
throw new ZipError('truncated-local', `本地头越界:${entry.name}`)
|
|
122
|
-
}
|
|
123
|
-
if (buffer.readUInt32LE(entry.localOffset) !== SIG_LOCAL) {
|
|
124
|
-
throw new ZipError('bad-local-signature', `本地头签名不符:${entry.name}`)
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
const nameLen = buffer.readUInt16LE(entry.localOffset + 26)
|
|
128
|
-
const extraLen = buffer.readUInt16LE(entry.localOffset + 28)
|
|
129
|
-
const dataStart = entry.localOffset + 30 + nameLen + extraLen
|
|
130
|
-
const dataEnd = dataStart + entry.compressedSize
|
|
131
|
-
|
|
132
|
-
if (dataEnd > buffer.length) {
|
|
133
|
-
throw new ZipError('truncated-data', `条目数据被截断:${entry.name}`)
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// 声明解压尺寸就超限 → 提前拒绝(不等真解压)
|
|
137
|
-
if (entry.uncompressedSize > limits.skillBytes) {
|
|
138
|
-
throw new ZipError(
|
|
139
|
-
'entry-too-large',
|
|
140
|
-
`${entry.name} 声明 ${entry.uncompressedSize}B,超过单文件上限 ${limits.skillBytes}B`,
|
|
141
|
-
)
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
const raw = buffer.subarray(dataStart, dataEnd)
|
|
145
|
-
|
|
146
|
-
if (entry.method === METHOD_STORE) return Buffer.from(raw)
|
|
147
|
-
if (entry.method !== METHOD_DEFLATE) {
|
|
148
|
-
throw new ZipError('unsupported-method', `${entry.name} 使用不支持的压缩方法 ${entry.method}`)
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// inflateRaw 带 maxOutputLength:解压炸弹在解压途中被打断,而不是先铺满内存
|
|
152
|
-
try {
|
|
153
|
-
return inflateRawSync(raw, { maxOutputLength: limits.skillBytes })
|
|
154
|
-
} catch (error) {
|
|
155
|
-
if (String(error?.code).includes('ERR_BUFFER_TOO_LARGE') || /maxOutputLength|too large/i.test(String(error?.message))) {
|
|
156
|
-
throw new ZipError('entry-too-large', `${entry.name} 解压后超过单文件上限 ${limits.skillBytes}B`)
|
|
157
|
-
}
|
|
158
|
-
throw new ZipError('inflate-failed', `${entry.name} 解压失败:${error?.message ?? error}`)
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* 一次性解出全部非符号链接的文本条目,累计总字节上限。
|
|
164
|
-
* @returns {{ files: Array<{name:string, content:string}>, skippedSymlinks: string[], totalBytes: number }}
|
|
165
|
-
*/
|
|
166
|
-
export function extractTextFiles(buffer, limits = IMPORT_LIMITS) {
|
|
167
|
-
const entries = listEntries(buffer)
|
|
168
|
-
const files = []
|
|
169
|
-
const skippedSymlinks = []
|
|
170
|
-
let totalBytes = 0
|
|
171
|
-
|
|
172
|
-
for (const entry of entries) {
|
|
173
|
-
if (entry.name.endsWith('/')) continue // 目录条目
|
|
174
|
-
if (entry.isSymlink) {
|
|
175
|
-
skippedSymlinks.push(entry.name)
|
|
176
|
-
continue
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
const content = readEntry(buffer, entry, limits)
|
|
180
|
-
totalBytes += content.length
|
|
181
|
-
if (totalBytes > limits.totalBytes) {
|
|
182
|
-
throw new ZipError(
|
|
183
|
-
'total-too-large',
|
|
184
|
-
`解压总字节 ${totalBytes}B 超过上限 ${limits.totalBytes}B`,
|
|
185
|
-
)
|
|
186
|
-
}
|
|
187
|
-
files.push({ name: entry.name, content: content.toString('utf8') })
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
return { files, skippedSymlinks, totalBytes }
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
/** 从条目路径里剥掉公共顶层目录(很多仓库打的 zip 都有一层)。 */
|
|
194
|
-
export function stripCommonPrefix(paths) {
|
|
195
|
-
if (paths.length === 0) return paths
|
|
196
|
-
const parts = paths.map(p => p.split('/'))
|
|
197
|
-
const first = parts[0][0]
|
|
198
|
-
if (first === '' || first === undefined) return paths
|
|
199
|
-
if (!parts.every(p => p.length > 1 && p[0] === first)) return paths
|
|
200
|
-
return paths.map(p => p.split('/').slice(1).join('/'))
|
|
201
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 零依赖 ZIP 读取器 —— 只做导入需要的部分。
|
|
3
|
+
*
|
|
4
|
+
* 不引 jszip/yauzl:导入路径是安全敏感面,第三方解析器的面比我们需要的宽得多
|
|
5
|
+
* (支持加密、zip64、跨卷…)。这里只实现「中央目录 → 本地头 → deflate 解压」,
|
|
6
|
+
* 遇到任何不认识的结构直接拒绝,而不是猜。
|
|
7
|
+
*
|
|
8
|
+
* 安全纪律(findings A5):
|
|
9
|
+
* - 符号链接**跳过而非跟随**
|
|
10
|
+
* - 条目数 / 单文件字节 / 总解压字节 三道上限在解压**过程中**实时检查
|
|
11
|
+
* (不能先全部解压再数——那就已经被解压炸弹打穿了)
|
|
12
|
+
*/
|
|
13
|
+
import { inflateRawSync } from 'node:zlib'
|
|
14
|
+
|
|
15
|
+
const SIG_EOCD = 0x06054b50
|
|
16
|
+
const SIG_CENTRAL = 0x02014b50
|
|
17
|
+
const SIG_LOCAL = 0x04034b50
|
|
18
|
+
|
|
19
|
+
const METHOD_STORE = 0
|
|
20
|
+
const METHOD_DEFLATE = 8
|
|
21
|
+
|
|
22
|
+
/** ZIP 外部属性里的 Unix 文件类型位。 */
|
|
23
|
+
const S_IFMT = 0o170000
|
|
24
|
+
const S_IFLNK = 0o120000
|
|
25
|
+
|
|
26
|
+
export const IMPORT_LIMITS = Object.freeze({
|
|
27
|
+
archiveBytes: 8 * 1024 * 1024,
|
|
28
|
+
entries: 512,
|
|
29
|
+
skillBytes: 256 * 1024,
|
|
30
|
+
totalBytes: 16 * 1024 * 1024,
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
export class ZipError extends Error {
|
|
34
|
+
constructor(code, message) {
|
|
35
|
+
super(message)
|
|
36
|
+
this.code = code
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** 找 EOCD(从尾部往前扫,最多 64KB 注释)。 */
|
|
41
|
+
function findEocd(buffer) {
|
|
42
|
+
const maxComment = 0xffff
|
|
43
|
+
const start = Math.max(0, buffer.length - maxComment - 22)
|
|
44
|
+
for (let i = buffer.length - 22; i >= start; i -= 1) {
|
|
45
|
+
if (buffer.readUInt32LE(i) === SIG_EOCD) return i
|
|
46
|
+
}
|
|
47
|
+
return -1
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 读取 ZIP 的条目清单(不解压)。
|
|
52
|
+
* @returns {Array<{name:string, method:number, compressedSize:number, uncompressedSize:number, localOffset:number, isSymlink:boolean}>}
|
|
53
|
+
*/
|
|
54
|
+
export function listEntries(buffer) {
|
|
55
|
+
if (buffer.length > IMPORT_LIMITS.archiveBytes) {
|
|
56
|
+
throw new ZipError('archive-too-large', `归档 ${buffer.length}B 超过上限 ${IMPORT_LIMITS.archiveBytes}B`)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const eocd = findEocd(buffer)
|
|
60
|
+
if (eocd === -1) throw new ZipError('not-a-zip', '找不到 EOCD 签名')
|
|
61
|
+
|
|
62
|
+
const entryCount = buffer.readUInt16LE(eocd + 10)
|
|
63
|
+
const centralOffset = buffer.readUInt32LE(eocd + 16)
|
|
64
|
+
|
|
65
|
+
if (entryCount > IMPORT_LIMITS.entries) {
|
|
66
|
+
throw new ZipError('too-many-entries', `条目数 ${entryCount} 超过上限 ${IMPORT_LIMITS.entries}`)
|
|
67
|
+
}
|
|
68
|
+
if (centralOffset >= buffer.length) {
|
|
69
|
+
throw new ZipError('bad-central-offset', '中央目录偏移越界')
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const entries = []
|
|
73
|
+
let cursor = centralOffset
|
|
74
|
+
|
|
75
|
+
for (let i = 0; i < entryCount; i += 1) {
|
|
76
|
+
if (cursor + 46 > buffer.length) throw new ZipError('truncated-central', '中央目录被截断')
|
|
77
|
+
if (buffer.readUInt32LE(cursor) !== SIG_CENTRAL) {
|
|
78
|
+
throw new ZipError('bad-central-signature', `第 ${i} 条中央目录签名不符`)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const flags = buffer.readUInt16LE(cursor + 8)
|
|
82
|
+
const method = buffer.readUInt16LE(cursor + 10)
|
|
83
|
+
const compressedSize = buffer.readUInt32LE(cursor + 20)
|
|
84
|
+
const uncompressedSize = buffer.readUInt32LE(cursor + 24)
|
|
85
|
+
const nameLen = buffer.readUInt16LE(cursor + 28)
|
|
86
|
+
const extraLen = buffer.readUInt16LE(cursor + 30)
|
|
87
|
+
const commentLen = buffer.readUInt16LE(cursor + 32)
|
|
88
|
+
const externalAttr = buffer.readUInt32LE(cursor + 38)
|
|
89
|
+
const localOffset = buffer.readUInt32LE(cursor + 42)
|
|
90
|
+
|
|
91
|
+
if (cursor + 46 + nameLen > buffer.length) throw new ZipError('truncated-name', '条目名被截断')
|
|
92
|
+
const name = buffer.toString('utf8', cursor + 46, cursor + 46 + nameLen)
|
|
93
|
+
|
|
94
|
+
// 加密条目一律拒绝(我们不解密,也无法判断内容)
|
|
95
|
+
if ((flags & 0x1) !== 0) throw new ZipError('encrypted-entry', `条目被加密:${name}`)
|
|
96
|
+
|
|
97
|
+
const unixMode = (externalAttr >>> 16) & 0xffff
|
|
98
|
+
const isSymlink = (unixMode & S_IFMT) === S_IFLNK
|
|
99
|
+
|
|
100
|
+
entries.push({
|
|
101
|
+
name,
|
|
102
|
+
method,
|
|
103
|
+
compressedSize,
|
|
104
|
+
uncompressedSize,
|
|
105
|
+
localOffset,
|
|
106
|
+
isSymlink,
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
cursor += 46 + nameLen + extraLen + commentLen
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return entries
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 解压单个条目。上限在解压时实时校验。
|
|
117
|
+
* @returns {Buffer}
|
|
118
|
+
*/
|
|
119
|
+
export function readEntry(buffer, entry, limits = IMPORT_LIMITS) {
|
|
120
|
+
if (entry.localOffset + 30 > buffer.length) {
|
|
121
|
+
throw new ZipError('truncated-local', `本地头越界:${entry.name}`)
|
|
122
|
+
}
|
|
123
|
+
if (buffer.readUInt32LE(entry.localOffset) !== SIG_LOCAL) {
|
|
124
|
+
throw new ZipError('bad-local-signature', `本地头签名不符:${entry.name}`)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const nameLen = buffer.readUInt16LE(entry.localOffset + 26)
|
|
128
|
+
const extraLen = buffer.readUInt16LE(entry.localOffset + 28)
|
|
129
|
+
const dataStart = entry.localOffset + 30 + nameLen + extraLen
|
|
130
|
+
const dataEnd = dataStart + entry.compressedSize
|
|
131
|
+
|
|
132
|
+
if (dataEnd > buffer.length) {
|
|
133
|
+
throw new ZipError('truncated-data', `条目数据被截断:${entry.name}`)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// 声明解压尺寸就超限 → 提前拒绝(不等真解压)
|
|
137
|
+
if (entry.uncompressedSize > limits.skillBytes) {
|
|
138
|
+
throw new ZipError(
|
|
139
|
+
'entry-too-large',
|
|
140
|
+
`${entry.name} 声明 ${entry.uncompressedSize}B,超过单文件上限 ${limits.skillBytes}B`,
|
|
141
|
+
)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const raw = buffer.subarray(dataStart, dataEnd)
|
|
145
|
+
|
|
146
|
+
if (entry.method === METHOD_STORE) return Buffer.from(raw)
|
|
147
|
+
if (entry.method !== METHOD_DEFLATE) {
|
|
148
|
+
throw new ZipError('unsupported-method', `${entry.name} 使用不支持的压缩方法 ${entry.method}`)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// inflateRaw 带 maxOutputLength:解压炸弹在解压途中被打断,而不是先铺满内存
|
|
152
|
+
try {
|
|
153
|
+
return inflateRawSync(raw, { maxOutputLength: limits.skillBytes })
|
|
154
|
+
} catch (error) {
|
|
155
|
+
if (String(error?.code).includes('ERR_BUFFER_TOO_LARGE') || /maxOutputLength|too large/i.test(String(error?.message))) {
|
|
156
|
+
throw new ZipError('entry-too-large', `${entry.name} 解压后超过单文件上限 ${limits.skillBytes}B`)
|
|
157
|
+
}
|
|
158
|
+
throw new ZipError('inflate-failed', `${entry.name} 解压失败:${error?.message ?? error}`)
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* 一次性解出全部非符号链接的文本条目,累计总字节上限。
|
|
164
|
+
* @returns {{ files: Array<{name:string, content:string}>, skippedSymlinks: string[], totalBytes: number }}
|
|
165
|
+
*/
|
|
166
|
+
export function extractTextFiles(buffer, limits = IMPORT_LIMITS) {
|
|
167
|
+
const entries = listEntries(buffer)
|
|
168
|
+
const files = []
|
|
169
|
+
const skippedSymlinks = []
|
|
170
|
+
let totalBytes = 0
|
|
171
|
+
|
|
172
|
+
for (const entry of entries) {
|
|
173
|
+
if (entry.name.endsWith('/')) continue // 目录条目
|
|
174
|
+
if (entry.isSymlink) {
|
|
175
|
+
skippedSymlinks.push(entry.name)
|
|
176
|
+
continue
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const content = readEntry(buffer, entry, limits)
|
|
180
|
+
totalBytes += content.length
|
|
181
|
+
if (totalBytes > limits.totalBytes) {
|
|
182
|
+
throw new ZipError(
|
|
183
|
+
'total-too-large',
|
|
184
|
+
`解压总字节 ${totalBytes}B 超过上限 ${limits.totalBytes}B`,
|
|
185
|
+
)
|
|
186
|
+
}
|
|
187
|
+
files.push({ name: entry.name, content: content.toString('utf8') })
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return { files, skippedSymlinks, totalBytes }
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** 从条目路径里剥掉公共顶层目录(很多仓库打的 zip 都有一层)。 */
|
|
194
|
+
export function stripCommonPrefix(paths) {
|
|
195
|
+
if (paths.length === 0) return paths
|
|
196
|
+
const parts = paths.map(p => p.split('/'))
|
|
197
|
+
const first = parts[0][0]
|
|
198
|
+
if (first === '' || first === undefined) return paths
|
|
199
|
+
if (!parts.every(p => p.length > 1 && p[0] === first)) return paths
|
|
200
|
+
return paths.map(p => p.split('/').slice(1).join('/'))
|
|
201
|
+
}
|
|
@@ -1,74 +1,74 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* provider 注册与生命周期。
|
|
3
|
-
*
|
|
4
|
-
* 两条硬约束(checklist Must Pass):
|
|
5
|
-
* 1. `registerProvider` 全部包在 `ctx.effect` 内 —— fiber 销毁时自动注销
|
|
6
|
-
* 2. `mounted()` 守卫覆盖全部异步续体 —— 防止 fiber 已销毁后仍有异步回调注册
|
|
7
|
-
* (这正是 #24 的形态:`already registered in this scope`)
|
|
8
|
-
*
|
|
9
|
-
* Phase 0 实测(findings):`registerProvider` 全线返回 `() => void`,可直接当 disposer;
|
|
10
|
-
* `ctx.fiber.uid` 在 dispose 时置 `null`,故用 `uid !== null` 判存活。
|
|
11
|
-
*/
|
|
12
|
-
import { collectRoots } from '../collect/roots.js'
|
|
13
|
-
import { observeSkills } from '../collect/observe.js'
|
|
14
|
-
import { applyShadowing } from '../collect/shadow.js'
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* 判断当前 fiber 是否仍挂载。
|
|
18
|
-
* Phase 0 实测:uid 类型是 `number | null`,**不是 optional**,
|
|
19
|
-
* 所以判 `!== null`;用 `=== undefined` 会永远 false(假死守卫)。
|
|
20
|
-
*/
|
|
21
|
-
export function isMounted(ctx) {
|
|
22
|
-
const fiber = ctx?.fiber
|
|
23
|
-
if (fiber === undefined || fiber === null) return true // 无 fiber 概念的环境放行
|
|
24
|
-
return fiber.uid !== null && fiber.uid !== undefined
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* 注册 skill provider。
|
|
29
|
-
* @param {object} ctx cordis context
|
|
30
|
-
* @param {object} config 插件配置(customSkillDirs / bundledSkillDir / dshHome / agentsHome)
|
|
31
|
-
* @param {object} [deps] 注入点,便于单测
|
|
32
|
-
*/
|
|
33
|
-
export function installProvider(ctx, config = {}, deps = {}) {
|
|
34
|
-
const { collect = observeSkills, rootsFactory = collectRoots } = deps
|
|
35
|
-
const logger = ctx?.logger ?? console
|
|
36
|
-
|
|
37
|
-
const buildProvider = () => ({
|
|
38
|
-
name: 'dsh-skill-harbor',
|
|
39
|
-
async list() {
|
|
40
|
-
// 异步续体守卫:await 之后 fiber 可能已销毁
|
|
41
|
-
if (!isMounted(ctx)) return []
|
|
42
|
-
try {
|
|
43
|
-
const roots = rootsFactory({
|
|
44
|
-
cwd: config.cwd ?? process.cwd(),
|
|
45
|
-
dshHome: config.dshHome,
|
|
46
|
-
agentsHome: config.agentsHome,
|
|
47
|
-
customSkillDirs: config.customSkillDirs ?? [],
|
|
48
|
-
bundledSkillDir: config.bundledSkillDir,
|
|
49
|
-
})
|
|
50
|
-
const registrySummaries = typeof ctx?.skills?.list === 'function' ? await ctx.skills.list() : []
|
|
51
|
-
if (!isMounted(ctx)) return []
|
|
52
|
-
const observations = collect({ roots, registrySummaries })
|
|
53
|
-
return applyShadowing(observations).filter(o => o.shadowedBy === undefined)
|
|
54
|
-
} catch (error) {
|
|
55
|
-
logger.warn?.('[dsh-skill-harbor] provider.list failed', error)
|
|
56
|
-
return []
|
|
57
|
-
}
|
|
58
|
-
},
|
|
59
|
-
})
|
|
60
|
-
|
|
61
|
-
// 全部注册包在 effect 内:fiber 销毁 → 自动 dispose
|
|
62
|
-
const dispose = ctx.effect(() => {
|
|
63
|
-
const providerDisposer = ctx.skills.registerProvider(buildProvider)
|
|
64
|
-
return () => {
|
|
65
|
-
try {
|
|
66
|
-
providerDisposer()
|
|
67
|
-
} catch (error) {
|
|
68
|
-
logger.warn?.('[dsh-skill-harbor] provider dispose failed', error)
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}, 'dsh-skill-harbor.registerProvider')
|
|
72
|
-
|
|
73
|
-
return dispose
|
|
74
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* provider 注册与生命周期。
|
|
3
|
+
*
|
|
4
|
+
* 两条硬约束(checklist Must Pass):
|
|
5
|
+
* 1. `registerProvider` 全部包在 `ctx.effect` 内 —— fiber 销毁时自动注销
|
|
6
|
+
* 2. `mounted()` 守卫覆盖全部异步续体 —— 防止 fiber 已销毁后仍有异步回调注册
|
|
7
|
+
* (这正是 #24 的形态:`already registered in this scope`)
|
|
8
|
+
*
|
|
9
|
+
* Phase 0 实测(findings):`registerProvider` 全线返回 `() => void`,可直接当 disposer;
|
|
10
|
+
* `ctx.fiber.uid` 在 dispose 时置 `null`,故用 `uid !== null` 判存活。
|
|
11
|
+
*/
|
|
12
|
+
import { collectRoots } from '../collect/roots.js'
|
|
13
|
+
import { observeSkills } from '../collect/observe.js'
|
|
14
|
+
import { applyShadowing } from '../collect/shadow.js'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 判断当前 fiber 是否仍挂载。
|
|
18
|
+
* Phase 0 实测:uid 类型是 `number | null`,**不是 optional**,
|
|
19
|
+
* 所以判 `!== null`;用 `=== undefined` 会永远 false(假死守卫)。
|
|
20
|
+
*/
|
|
21
|
+
export function isMounted(ctx) {
|
|
22
|
+
const fiber = ctx?.fiber
|
|
23
|
+
if (fiber === undefined || fiber === null) return true // 无 fiber 概念的环境放行
|
|
24
|
+
return fiber.uid !== null && fiber.uid !== undefined
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 注册 skill provider。
|
|
29
|
+
* @param {object} ctx cordis context
|
|
30
|
+
* @param {object} config 插件配置(customSkillDirs / bundledSkillDir / dshHome / agentsHome)
|
|
31
|
+
* @param {object} [deps] 注入点,便于单测
|
|
32
|
+
*/
|
|
33
|
+
export function installProvider(ctx, config = {}, deps = {}) {
|
|
34
|
+
const { collect = observeSkills, rootsFactory = collectRoots } = deps
|
|
35
|
+
const logger = ctx?.logger ?? console
|
|
36
|
+
|
|
37
|
+
const buildProvider = () => ({
|
|
38
|
+
name: 'dsh-skill-harbor',
|
|
39
|
+
async list() {
|
|
40
|
+
// 异步续体守卫:await 之后 fiber 可能已销毁
|
|
41
|
+
if (!isMounted(ctx)) return []
|
|
42
|
+
try {
|
|
43
|
+
const roots = rootsFactory({
|
|
44
|
+
cwd: config.cwd ?? process.cwd(),
|
|
45
|
+
dshHome: config.dshHome,
|
|
46
|
+
agentsHome: config.agentsHome,
|
|
47
|
+
customSkillDirs: config.customSkillDirs ?? [],
|
|
48
|
+
bundledSkillDir: config.bundledSkillDir,
|
|
49
|
+
})
|
|
50
|
+
const registrySummaries = typeof ctx?.skills?.list === 'function' ? await ctx.skills.list() : []
|
|
51
|
+
if (!isMounted(ctx)) return []
|
|
52
|
+
const observations = collect({ roots, registrySummaries })
|
|
53
|
+
return applyShadowing(observations).filter(o => o.shadowedBy === undefined)
|
|
54
|
+
} catch (error) {
|
|
55
|
+
logger.warn?.('[dsh-skill-harbor] provider.list failed', error)
|
|
56
|
+
return []
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
// 全部注册包在 effect 内:fiber 销毁 → 自动 dispose
|
|
62
|
+
const dispose = ctx.effect(() => {
|
|
63
|
+
const providerDisposer = ctx.skills.registerProvider(buildProvider)
|
|
64
|
+
return () => {
|
|
65
|
+
try {
|
|
66
|
+
providerDisposer()
|
|
67
|
+
} catch (error) {
|
|
68
|
+
logger.warn?.('[dsh-skill-harbor] provider dispose failed', error)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}, 'dsh-skill-harbor.registerProvider')
|
|
72
|
+
|
|
73
|
+
return dispose
|
|
74
|
+
}
|