dsh-skill-harbor 0.3.0 → 0.3.2
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/dsh.plugin.json +1 -1
- package/lib/client/api.js +79 -0
- package/lib/client/labels.js +6 -0
- package/lib/client/locales.js +688 -0
- package/lib/client/panel.js +338 -0
- package/lib/client.js +13 -4
- package/lib/config.js +42 -0
- package/lib/index.js +14 -6
- package/lib/market/install.js +169 -0
- package/lib/market/zip.js +201 -0
- package/lib/routes/index.js +68 -0
- package/package.json +1 -1
- package/src/client/api.js +79 -0
- package/src/client/labels.js +6 -0
- package/src/client/locales.js +688 -0
- package/src/client/panel.js +338 -0
- package/src/client.js +13 -4
- package/src/config.js +42 -0
- package/src/index.js +14 -6
- package/src/market/install.js +169 -0
- package/src/market/zip.js +201 -0
- package/src/routes/index.js +68 -0
|
@@ -0,0 +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
|
+
}
|
package/lib/routes/index.js
CHANGED
|
@@ -15,6 +15,7 @@ import { FILE_STATE, isWritable } from '../collect/observe.js'
|
|
|
15
15
|
import { extractSkillMeta } from '../collect/frontmatter.js'
|
|
16
16
|
import { auditSkillText } from '../audit/rules.js'
|
|
17
17
|
import { budgetReport } from '../budget/announcement.js'
|
|
18
|
+
import { parseSkillsFromArchive, decidePlacement, installSkill } from '../market/install.js'
|
|
18
19
|
|
|
19
20
|
/** 关闭/开启模型的 frontmatter 键。 */
|
|
20
21
|
const DISABLE_KEY = 'disable-model-invocation'
|
|
@@ -302,6 +303,73 @@ export function registerRoutes(ctx, config = {}, deps = {}) {
|
|
|
302
303
|
sendJson(res, 200, { takenAt: snap.takenAt, ...budgetReport(snap.observations) })
|
|
303
304
|
})
|
|
304
305
|
|
|
306
|
+
// ---- 写:导入归档 ----
|
|
307
|
+
register('/import', ['POST'], async (req, res, body) => {
|
|
308
|
+
const snap = await snapshot()
|
|
309
|
+
const archiveBase64 = body?.archive
|
|
310
|
+
const targetSource = body?.targetSource ?? 'user-dsh'
|
|
311
|
+
|
|
312
|
+
if (typeof archiveBase64 !== 'string' || archiveBase64 === '') {
|
|
313
|
+
sendJson(res, 400, { error: 'archive-required' })
|
|
314
|
+
return
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
let buffer
|
|
318
|
+
try {
|
|
319
|
+
buffer = Buffer.from(archiveBase64, 'base64')
|
|
320
|
+
} catch {
|
|
321
|
+
sendJson(res, 400, { error: 'invalid-base64' })
|
|
322
|
+
return
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const targetRoot = snap.roots.find(root => root.source === targetSource)
|
|
326
|
+
if (targetRoot === undefined) {
|
|
327
|
+
sendJson(res, 400, {
|
|
328
|
+
error: 'unknown-target-source',
|
|
329
|
+
available: snap.roots.map(root => root.source),
|
|
330
|
+
})
|
|
331
|
+
return
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
let parsed
|
|
335
|
+
try {
|
|
336
|
+
parsed = parseSkillsFromArchive(buffer)
|
|
337
|
+
} catch (error) {
|
|
338
|
+
const code = error?.code ?? 'archive-invalid'
|
|
339
|
+
const status = code === 'archive-too-large' || code === 'too-many-entries' || code === 'total-too-large' ? 413
|
|
340
|
+
: code === 'encrypted-entry' ? 422
|
|
341
|
+
: 400
|
|
342
|
+
sendJson(res, status, { error: code, message: String(error?.message ?? error) })
|
|
343
|
+
return
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const results = []
|
|
347
|
+
for (const skill of parsed.skills) {
|
|
348
|
+
const placement = decidePlacement({
|
|
349
|
+
skillName: skill.name,
|
|
350
|
+
targetRoot,
|
|
351
|
+
allRoots: snap.roots,
|
|
352
|
+
observations: snap.observations,
|
|
353
|
+
})
|
|
354
|
+
if (!placement.ok) {
|
|
355
|
+
results.push({ name: skill.name, installed: false, reason: placement.reason, message: placement.message })
|
|
356
|
+
continue
|
|
357
|
+
}
|
|
358
|
+
const outcome = installSkill({ skill, targetDir: placement.targetDir })
|
|
359
|
+
results.push({ name: skill.name, ...outcome })
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const installedCount = results.filter(r => r.installed).length
|
|
363
|
+
if (installedCount > 0) invalidate()
|
|
364
|
+
|
|
365
|
+
sendJson(res, installedCount > 0 ? 200 : 422, {
|
|
366
|
+
ok: installedCount > 0,
|
|
367
|
+
skippedSymlinks: parsed.skippedSymlinks,
|
|
368
|
+
totalBytes: parsed.totalBytes,
|
|
369
|
+
results,
|
|
370
|
+
})
|
|
371
|
+
})
|
|
372
|
+
|
|
305
373
|
return () => {
|
|
306
374
|
for (const dispose of disposers) {
|
|
307
375
|
try {
|
package/package.json
CHANGED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 客户端唯一 fetch 出口。
|
|
3
|
+
*
|
|
4
|
+
* 必须按 `document.baseURI` 解析 —— 反代带路径前缀(如 `/app/dsh/`)部署时,
|
|
5
|
+
* 写死 `/api/...` 会打到站点根而不是应用根,全部 404。
|
|
6
|
+
*/
|
|
7
|
+
import { LOCALES } from './locales.js'
|
|
8
|
+
|
|
9
|
+
export const API_PREFIX = '/dsh-skill-harbor/api'
|
|
10
|
+
export const CSRF_HEADER = 'x-dsh-skill-harbor'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 相对路径 → 绝对 URL,锚定到 document.baseURI。
|
|
14
|
+
* 非 `/` 开头或无 document 环境时原样返回。
|
|
15
|
+
*/
|
|
16
|
+
export function resolveApiUrl(path) {
|
|
17
|
+
if (typeof path !== 'string') return String(path)
|
|
18
|
+
try {
|
|
19
|
+
if (path.startsWith('/') && typeof document !== 'undefined' && document.baseURI) {
|
|
20
|
+
return new URL(path.slice(1), document.baseURI).href
|
|
21
|
+
}
|
|
22
|
+
} catch {
|
|
23
|
+
/* 落到原样返回 */
|
|
24
|
+
}
|
|
25
|
+
return path
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function request(path, options = {}) {
|
|
29
|
+
const url = resolveApiUrl(`${API_PREFIX}${path}`)
|
|
30
|
+
const method = options.method ?? 'GET'
|
|
31
|
+
|
|
32
|
+
const headers = { ...options.headers }
|
|
33
|
+
if (method !== 'GET' && method !== 'HEAD') {
|
|
34
|
+
// 写方法必须带自定义头;跨站表单设不了这个头
|
|
35
|
+
headers[CSRF_HEADER] = '1'
|
|
36
|
+
if (options.body !== undefined) headers['content-type'] = 'application/json'
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const response = await fetch(url, {
|
|
40
|
+
method,
|
|
41
|
+
headers,
|
|
42
|
+
credentials: 'same-origin',
|
|
43
|
+
...options.body === undefined ? {} : { body: JSON.stringify(options.body) },
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
let payload = null
|
|
47
|
+
try {
|
|
48
|
+
payload = await response.json()
|
|
49
|
+
} catch {
|
|
50
|
+
payload = null
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (!response.ok) {
|
|
54
|
+
const error = new Error(payload?.message ?? payload?.error ?? `HTTP ${response.status}`)
|
|
55
|
+
error.status = response.status
|
|
56
|
+
error.payload = payload
|
|
57
|
+
throw error
|
|
58
|
+
}
|
|
59
|
+
return payload
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export const api = {
|
|
63
|
+
list: () => request('/list'),
|
|
64
|
+
read: path => request(`/read?path=${encodeURIComponent(path)}`),
|
|
65
|
+
audit: () => request('/audit'),
|
|
66
|
+
budget: () => request('/budget'),
|
|
67
|
+
setEnabled: (path, enabled) => request('/set-enabled', { method: 'POST', body: { path, enabled } }),
|
|
68
|
+
update: (path, content) => request('/update', { method: 'POST', body: { path, content } }),
|
|
69
|
+
remove: path => request('/delete', { method: 'POST', body: { path } }),
|
|
70
|
+
importArchive: (archive, targetSource) =>
|
|
71
|
+
request('/import', { method: 'POST', body: { archive, targetSource } }),
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** 取某语言的字典,缺省回落 zh。 */
|
|
75
|
+
export function dict(lang) {
|
|
76
|
+
return LOCALES[lang] ?? LOCALES.zh
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export { request }
|