@dsh-plugin/dsh-pack-plugin 0.0.0-stage → 0.0.1
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 -0
- package/README.md +178 -3
- package/cordis.patch.yml +9 -0
- package/icons/folder-zip-line.svg +1 -0
- package/lib/ball.js +597 -0
- package/lib/client.js +2934 -0
- package/lib/host.js +51745 -0
- package/lib/migrate-helper.js +35346 -0
- package/lib/progress-main.cjs +79 -0
- package/lib/progress-window.ps1 +114 -0
- package/lib/progress.html +126 -0
- package/lib/publish-to-github/SKILL.md +132 -0
- package/package.json +77 -4
- package/src/ball-entry.js +5 -0
- package/src/ball-host.js +99 -0
- package/src/ball-settings.js +352 -0
- package/src/ball.js +418 -0
- package/src/channel.js +12 -0
- package/src/client-plugin.js +58 -0
- package/src/client-rpc.js +14 -0
- package/src/client.js +5 -0
- package/src/core/discovery.js +192 -0
- package/src/core/dspack.js +93 -0
- package/src/core/extension-files.js +102 -0
- package/src/core/extension-install.js +295 -0
- package/src/core/extension-patch.js +369 -0
- package/src/core/extension-skills.js +164 -0
- package/src/core/extension-validate.js +127 -0
- package/src/core/extensions.js +207 -0
- package/src/core/home-store.js +32 -0
- package/src/core/host.js +149 -0
- package/src/core/index.js +59 -0
- package/src/core/inspect.js +162 -0
- package/src/core/install.js +775 -0
- package/src/core/managed-install.js +84 -0
- package/src/core/manifest.js +717 -0
- package/src/core/market.js +268 -0
- package/src/core/mcp-composition.js +189 -0
- package/src/core/mcp-install.js +112 -0
- package/src/core/mcp-transport.js +116 -0
- package/src/core/pack.js +371 -0
- package/src/core/plugin-metadata.js +355 -0
- package/src/core/registry.js +316 -0
- package/src/core/repo.js +339 -0
- package/src/core/scan.js +78 -0
- package/src/core/security.js +101 -0
- package/src/core/special.js +50 -0
- package/src/core/tar.js +103 -0
- package/src/core/update-config.js +234 -0
- package/src/core/update.js +792 -0
- package/src/core/vendored.js +715 -0
- package/src/core/workspace.js +95 -0
- package/src/endpoints.js +455 -0
- package/src/ensure-manager.js +186 -0
- package/src/host-node.js +625 -0
- package/src/host.js +17 -0
- package/src/index.d.ts +7 -0
- package/src/index.js +39 -0
- package/src/junction.js +142 -0
- package/src/migrate-helper.js +225 -0
- package/src/migrate.js +56 -0
- package/src/packaged.js +9 -0
- package/src/profiles.js +106 -0
- package/src/progress-main.cjs +79 -0
- package/src/progress-window.ps1 +114 -0
- package/src/progress.html +126 -0
- package/src/progress.js +132 -0
- package/src/rpc.js +157 -0
- package/src/runtime.js +43 -0
- package/src/section-gate.js +50 -0
- package/src/settings-nav-icon.js +188 -0
- package/src/settings.js +1611 -0
- package/src/skills/index.js +90 -0
- package/src/skills/publish-to-github/SKILL.md +132 -0
- package/src/tasks.js +145 -0
- package/src/v6-dialogs.js +312 -0
package/src/core/repo.js
ADDED
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
// 导出「源仓库」(v6 源形态):把 Profile 物化为可二次开发 / 重打包的 git 仓库。
|
|
2
|
+
// 三档内容(opts.content,默认 readme):
|
|
3
|
+
// - manifest:仅清单(manifest.json)
|
|
4
|
+
// - readme :清单 + README.md
|
|
5
|
+
// - full :全套(机器文件 + overrides/ + .dspackignore)
|
|
6
|
+
// 仓库目录名 = <out>/<name>(不带版本号);release/ 始终产出 .dspack + .sha256(gitignore 不入库);
|
|
7
|
+
// 首次 git init + commit,之后每次导出增量 commit;同版本 release 冲突抛 ReleaseConflictError。
|
|
8
|
+
// 布局遵循 pack-structure v3:机器文件进根目录,其余用户文件进 overrides/。
|
|
9
|
+
import { resolveLocale } from './manifest.js';
|
|
10
|
+
import { prepareProfileArchive, writeProfileArchive } from './pack.js';
|
|
11
|
+
|
|
12
|
+
// 发布清单和内嵌图标可单独浏览。其余归档条目仅随完整源码发布。
|
|
13
|
+
function sourceEntries(entries, content) {
|
|
14
|
+
return Object.fromEntries(Object.entries(entries).filter(([path]) =>
|
|
15
|
+
path !== 'manifest.json' && (content === 'full' || path.startsWith('assets/plugins/')),
|
|
16
|
+
));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async function checkedSourcePath(host, root, relative) {
|
|
20
|
+
const parts = relative.split('/');
|
|
21
|
+
if (!parts.length || parts.some((part) => !part || part === '.' || part === '..' || /[\\:\x00-\x1f]|[. ]$/u.test(part) || part.toLowerCase() === '.git') || parts[0].toLowerCase() === 'release') {
|
|
22
|
+
throw new Error('源仓库文件路径无效');
|
|
23
|
+
}
|
|
24
|
+
let path = root;
|
|
25
|
+
for (const part of parts) {
|
|
26
|
+
if (await host.packEntryType(path) === 'symlink') throw new Error('源仓库路径包含链接:' + relative);
|
|
27
|
+
path = host.joinPath(path, part);
|
|
28
|
+
}
|
|
29
|
+
if (await host.packEntryType(path) === 'symlink') throw new Error('源仓库路径包含链接:' + relative);
|
|
30
|
+
return path;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// 只清理上次导出写入且内容未变的文件。手动修改过的旧资源须由作者决定是否继续发布。
|
|
34
|
+
async function planSourceFiles(host, root, inventoryFile, next) {
|
|
35
|
+
const saved = await host.readTextFile(inventoryFile);
|
|
36
|
+
const previous = saved ? JSON.parse(saved) : {};
|
|
37
|
+
const remove = [];
|
|
38
|
+
const hashes = Object.create(null);
|
|
39
|
+
for (const [rel, data] of Object.entries(next)) {
|
|
40
|
+
await checkedSourcePath(host, root, rel);
|
|
41
|
+
hashes[rel] = await host.sha256(data);
|
|
42
|
+
}
|
|
43
|
+
for (const [rel, hash] of Object.entries(previous)) {
|
|
44
|
+
if (Object.hasOwn(hashes, rel)) continue;
|
|
45
|
+
const path = await checkedSourcePath(host, root, rel);
|
|
46
|
+
const data = await host.readFile(path);
|
|
47
|
+
if (!data) continue;
|
|
48
|
+
if (await host.sha256(data) !== hash) throw new Error('上次发布的文件已被手动修改,请核对后再导出:' + rel);
|
|
49
|
+
remove.push(path);
|
|
50
|
+
}
|
|
51
|
+
return { hashes, remove };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** 合法内容档。 */
|
|
55
|
+
export const REPO_CONTENT_LEVELS = ['manifest', 'readme', 'full'];
|
|
56
|
+
|
|
57
|
+
/** 内容档 → 中文标签(GUI/CLI 展示)。 */
|
|
58
|
+
export const REPO_CONTENT_LABEL = {
|
|
59
|
+
manifest: '仅清单(manifest.json)',
|
|
60
|
+
readme: '清单 + README',
|
|
61
|
+
full: '全套文件(overrides/ + release/)',
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 导出源仓库。
|
|
66
|
+
* @param {Host} host
|
|
67
|
+
* @param {{name:string,dir:string}} profile
|
|
68
|
+
* @param {object} opts { out?, content?, replaceRelease?, displayName?, dshVersion?, ...其余透传 buildManifest/packProfile }
|
|
69
|
+
* - replaceRelease:undefined(冲突抛 ReleaseConflictError)/ true(覆盖)/ 'skip'(跳过 release,仍 commit 源码)
|
|
70
|
+
* @returns {Promise<{dir,manifest,content,written:string[],readme:string,ignored:string,
|
|
71
|
+
* release?:{dspack,sha256,sha256Value},git:{initialized,committed,reason},conflicted:boolean}>}
|
|
72
|
+
*/
|
|
73
|
+
export async function exportRepo(host, profile, opts = {}) {
|
|
74
|
+
const progress = (stage, detail) => {
|
|
75
|
+
if (typeof opts.onProgress === 'function') opts.onProgress(stage, detail);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const content = REPO_CONTENT_LEVELS.includes(opts.content) ? opts.content : 'readme';
|
|
79
|
+
progress('scan', `扫描 Profile「${profile.name}」`);
|
|
80
|
+
progress('manifest', '生成 manifest v6');
|
|
81
|
+
const archive = await prepareProfileArchive(host, profile, opts);
|
|
82
|
+
const { manifest, entries, scan } = archive;
|
|
83
|
+
|
|
84
|
+
const parent = opts.out ? host.resolvePath(opts.out) : host.cwd();
|
|
85
|
+
const repoDir = host.joinPath(parent, manifest.name); // 仓库名不带版本号
|
|
86
|
+
const releaseDir = host.joinPath(repoDir, 'release');
|
|
87
|
+
const releaseDspack = `${manifest.name}-${manifest.version}.dspack`;
|
|
88
|
+
const releasePath = host.joinPath(releaseDir, releaseDspack);
|
|
89
|
+
const releaseExists = (await host.stat(releasePath)) != null;
|
|
90
|
+
|
|
91
|
+
// 版本冲突:同版本 release 产物已存在且未明确「覆盖/跳过」→ 抛错交上层决定
|
|
92
|
+
if (releaseExists && opts.replaceRelease !== true && opts.replaceRelease !== 'skip') {
|
|
93
|
+
throw new ReleaseConflictError(releasePath, manifest.name, manifest.version);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// 先产 release,拿到 .dspack 校验和写进仓库根部的清单(清单在 .dspack 之外,不构成循环);
|
|
97
|
+
// .dspack 内部的 manifest(由 packProfile 生成)不带该字段。
|
|
98
|
+
for (const dir of [repoDir, releaseDir]) {
|
|
99
|
+
if (await host.packEntryType(dir) === 'symlink') throw new Error('源仓库输出目录不能是链接');
|
|
100
|
+
}
|
|
101
|
+
const inventoryFile = host.joinPath(releaseDir, '.source-files.json');
|
|
102
|
+
for (const generated of [inventoryFile, releasePath, releasePath + '.sha256']) {
|
|
103
|
+
if (await host.packEntryType(generated) === 'symlink') throw new Error('源仓库产物不能写入链接');
|
|
104
|
+
}
|
|
105
|
+
for (const file of ['manifest.json', 'README.md', '.gitignore', '.dspackignore']) {
|
|
106
|
+
await checkedSourcePath(host, repoDir, file);
|
|
107
|
+
}
|
|
108
|
+
const source = sourceEntries(entries, content);
|
|
109
|
+
const { hashes, remove } = await planSourceFiles(host, repoDir, inventoryFile, source);
|
|
110
|
+
let release = null;
|
|
111
|
+
if (!(opts.replaceRelease === 'skip' && releaseExists)) {
|
|
112
|
+
progress('pack', '打包 release .dspack');
|
|
113
|
+
const pack = await writeProfileArchive(host, archive, {
|
|
114
|
+
...opts,
|
|
115
|
+
onProgress: undefined, // release 打包不重入进度(避免时间线回退),由本函数统一报阶段
|
|
116
|
+
onOutput: undefined,
|
|
117
|
+
out: releaseDir,
|
|
118
|
+
force: opts.replaceRelease === true,
|
|
119
|
+
});
|
|
120
|
+
const shaName = `${releaseDspack}.sha256`;
|
|
121
|
+
await host.writeTextFile(host.joinPath(releaseDir, shaName), `${pack.sha256} ${releaseDspack}\n`);
|
|
122
|
+
release = { dspack: releaseDspack, sha256: shaName, sha256Value: pack.sha256 };
|
|
123
|
+
manifest.sha256 = pack.sha256; // 顶层 sha256 = release .dspack 校验和
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const written = [];
|
|
127
|
+
const writeText = async (name, body) => {
|
|
128
|
+
await host.writeTextFile(host.joinPath(repoDir, name), body);
|
|
129
|
+
written.push(name);
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
// 1) 清单(所有档;含 release sha256)
|
|
133
|
+
await writeText('manifest.json', `${JSON.stringify(manifest, null, 2)}\n`);
|
|
134
|
+
|
|
135
|
+
// 2) README(readme / full)
|
|
136
|
+
const readme = content === 'manifest' ? '' : renderReadme(manifest, scan);
|
|
137
|
+
if (content !== 'manifest') await writeText('README.md', readme);
|
|
138
|
+
|
|
139
|
+
// 3) 全套源码 + .dspackignore(full)
|
|
140
|
+
let ignored = '';
|
|
141
|
+
if (content === 'full') {
|
|
142
|
+
progress('collect', '收集源码文件');
|
|
143
|
+
ignored = renderDspackIgnore();
|
|
144
|
+
await writeText('.dspackignore', ignored);
|
|
145
|
+
}
|
|
146
|
+
for (const path of remove) await host.rm(path, { recursive: false, force: true });
|
|
147
|
+
for (const [dest, data] of Object.entries(source)) {
|
|
148
|
+
await host.writeFile(host.joinPath(repoDir, ...dest.split('/')), data);
|
|
149
|
+
written.push(dest);
|
|
150
|
+
}
|
|
151
|
+
await host.writeTextFile(inventoryFile, JSON.stringify(hashes));
|
|
152
|
+
|
|
153
|
+
// 4) .gitignore(所有档:release 产物不入版本管理)
|
|
154
|
+
await writeText('.gitignore', renderGitignore());
|
|
155
|
+
|
|
156
|
+
// 5) git init + add + commit(容错:无 git / 无变更时降级)
|
|
157
|
+
const git = await commitRepo(host, repoDir, manifest, opts.onOutput);
|
|
158
|
+
|
|
159
|
+
return {
|
|
160
|
+
dir: repoDir,
|
|
161
|
+
manifest,
|
|
162
|
+
content,
|
|
163
|
+
written,
|
|
164
|
+
readme,
|
|
165
|
+
ignored,
|
|
166
|
+
release,
|
|
167
|
+
git,
|
|
168
|
+
conflicted: releaseExists,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** 版本冲突错误:release/ 已存在同版本产物。 */
|
|
173
|
+
export class ReleaseConflictError extends Error {
|
|
174
|
+
constructor(file, name, version) {
|
|
175
|
+
super(`版本冲突:release/ 已存在 ${name}@${version}(${file})`);
|
|
176
|
+
this.name = 'ReleaseConflictError';
|
|
177
|
+
this.code = 'RELEASE_CONFLICT';
|
|
178
|
+
this.file = file;
|
|
179
|
+
this.packName = name;
|
|
180
|
+
this.packVersion = version;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** 生成 .gitignore(git 版本管理忽略规则:release 产物不入库)。 */
|
|
185
|
+
export function renderGitignore() {
|
|
186
|
+
return '# DSH PackForge 自动生成:release/ 产物不纳入版本管理\nrelease/\n';
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** git init + add + commit;无 git 或提交无变更时降级(不算失败)。 */
|
|
190
|
+
async function commitRepo(host, repoDir, manifest, onOutput) {
|
|
191
|
+
const message = `export:${manifest.name}@${manifest.version}`;
|
|
192
|
+
const runGit = async (args) => {
|
|
193
|
+
if (typeof host.git === 'function') {
|
|
194
|
+
const result = await host.git(args, {cwd:repoDir});
|
|
195
|
+
if (result.stdout) onOutput?.(result.stdout);
|
|
196
|
+
return result;
|
|
197
|
+
}
|
|
198
|
+
if (args.some(arg => /[^a-zA-Z0-9_.:@+\/-]/u.test(arg))) throw new Error('当前宿主不能安全传递 Git 参数');
|
|
199
|
+
return host.exec('git', args, {cwd:repoDir});
|
|
200
|
+
};
|
|
201
|
+
let gitOk = false;
|
|
202
|
+
try {
|
|
203
|
+
const r = await runGit(['--version']);
|
|
204
|
+
gitOk = r?.status === 0;
|
|
205
|
+
} catch { gitOk = false; }
|
|
206
|
+
if (!gitOk) return { initialized: false, committed: false, reason: 'git 不可用' };
|
|
207
|
+
|
|
208
|
+
const dotGit = host.joinPath(repoDir, '.git');
|
|
209
|
+
const existed = (await host.stat(dotGit)) != null;
|
|
210
|
+
if (!existed) await runGit(['init']);
|
|
211
|
+
await runGit(['add', '-A']);
|
|
212
|
+
const r = await runGit(['commit', '-m', message]);
|
|
213
|
+
const committed = r?.status === 0;
|
|
214
|
+
return { initialized: !existed, committed, reason: committed ? '' : '无变更或提交失败' };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* 生成 README.md(markdown)。描述来源为 manifest v5(displayName/description 支持 i18n map)。
|
|
219
|
+
* @param {object} m manifest v5
|
|
220
|
+
* @param {{files:Array,excluded:Array}} scan 扫描结果(用于文件清单段落)
|
|
221
|
+
*/
|
|
222
|
+
export function renderReadme(m, scan = { files: [], excluded: [] }) {
|
|
223
|
+
const disp = resolveLocale(m.displayName, 'zh-CN') || m.name;
|
|
224
|
+
const desc = resolveLocale(m.description, 'zh-CN');
|
|
225
|
+
const bundles = (m.bundles ?? []).map((b) => `- \`${b}\``).join('\n') || '(无)';
|
|
226
|
+
const deps = Object.entries(m.dependencies ?? {})
|
|
227
|
+
.map(([k, v]) => `- \`${k}\` @ \`${v}\``)
|
|
228
|
+
.join('\n') || '(无)';
|
|
229
|
+
|
|
230
|
+
return [
|
|
231
|
+
`# ${disp}`,
|
|
232
|
+
'',
|
|
233
|
+
desc || '(无描述)',
|
|
234
|
+
'',
|
|
235
|
+
`> 由 DSH PackForge 生成 · manifest v${m.manifestVersion} · type=${m.type || 'profile'}`,
|
|
236
|
+
'',
|
|
237
|
+
'## 元信息',
|
|
238
|
+
'',
|
|
239
|
+
'| 字段 | 值 |',
|
|
240
|
+
'| --- | --- |',
|
|
241
|
+
`| 整合包 | \`${m.name}\` v\`${m.version}\` |`,
|
|
242
|
+
`| DSH 版本 | ${m.dshVersion ? `\`${m.dshVersion}\`` : '未钉定(安装端兜底)'} |`,
|
|
243
|
+
`| 作者 | ${m.author || '—'} |`,
|
|
244
|
+
`| 层栈 | ${(m.bundles ?? []).length} 个 bundle |`,
|
|
245
|
+
`| 依赖 | ${Object.keys(m.dependencies ?? {}).length} 个 |`,
|
|
246
|
+
'',
|
|
247
|
+
'## 层栈(bundles)',
|
|
248
|
+
'',
|
|
249
|
+
bundles,
|
|
250
|
+
'',
|
|
251
|
+
'## 依赖(坐标 → 固定版本)',
|
|
252
|
+
'',
|
|
253
|
+
deps,
|
|
254
|
+
'',
|
|
255
|
+
'## 文件清单',
|
|
256
|
+
'',
|
|
257
|
+
`源 Profile 共 ${scan.files?.length ?? 0} 个文件(排除 ${scan.excluded?.length ?? 0} 个命中规则项)。`,
|
|
258
|
+
'打包时机器文件(`package.json` / `pnpm-workspace.yaml` / `pnpm-lock.yaml`)进根目录,其余进 `overrides/`。',
|
|
259
|
+
'',
|
|
260
|
+
'## 使用',
|
|
261
|
+
'',
|
|
262
|
+
'- 分发:重打包生成 `.dspack`(产物输出到 `release/`)',
|
|
263
|
+
`- 安装:\`dspack install release/${m.name}-${m.version}.dspack\``,
|
|
264
|
+
'',
|
|
265
|
+
].join('\n');
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* 生成 .dspackignore(gitignore 风格)——与引擎安全规则 security.js 对齐,
|
|
270
|
+
* 另追加仓库自身不入包项(.git/ / release/ / .dspackignore / README.md)。
|
|
271
|
+
*/
|
|
272
|
+
export function renderDspackIgnore() {
|
|
273
|
+
return [
|
|
274
|
+
'# DSH PackForge 打包忽略规则(自动生成;与引擎安全规则 security.js 对齐)',
|
|
275
|
+
'# 重打包为 .dspack 时,命中以下模式的文件/目录不会进入整合包。',
|
|
276
|
+
'',
|
|
277
|
+
'# --- 依赖与构建产物 ---',
|
|
278
|
+
'node_modules/',
|
|
279
|
+
'dist/',
|
|
280
|
+
'build/',
|
|
281
|
+
'coverage/',
|
|
282
|
+
'.cache/',
|
|
283
|
+
'.turbo/',
|
|
284
|
+
'.pnpm-store/',
|
|
285
|
+
'',
|
|
286
|
+
'# --- 自动生成的 DSH 配置(安装时由层栈 + patch 重新合成) ---',
|
|
287
|
+
'cordis.yml',
|
|
288
|
+
'',
|
|
289
|
+
'# --- 打包产物(防嵌套) ---',
|
|
290
|
+
'manifest.json',
|
|
291
|
+
'*.dspack',
|
|
292
|
+
'*.tgz',
|
|
293
|
+
'*.tar.gz',
|
|
294
|
+
'*.zip',
|
|
295
|
+
'',
|
|
296
|
+
'# --- 其它包管理器锁文件(DSH 固定使用 pnpm) ---',
|
|
297
|
+
'package-lock.json',
|
|
298
|
+
'yarn.lock',
|
|
299
|
+
'',
|
|
300
|
+
'# --- 日志 ---',
|
|
301
|
+
'npm-debug.log',
|
|
302
|
+
'pnpm-debug.log',
|
|
303
|
+
'',
|
|
304
|
+
'# --- 敏感文件 ---',
|
|
305
|
+
'.env',
|
|
306
|
+
'.env.*',
|
|
307
|
+
'.npmrc',
|
|
308
|
+
'.netrc',
|
|
309
|
+
'.yarnrc',
|
|
310
|
+
'.yarnrc.yml',
|
|
311
|
+
'.pypirc',
|
|
312
|
+
'.npmignore',
|
|
313
|
+
'*.key',
|
|
314
|
+
'*.pem',
|
|
315
|
+
'*.p12',
|
|
316
|
+
'*.pfx',
|
|
317
|
+
'*.crt',
|
|
318
|
+
'*.der',
|
|
319
|
+
'*.asc',
|
|
320
|
+
'*.credentials',
|
|
321
|
+
'credentials*.yml',
|
|
322
|
+
'id_rsa*',
|
|
323
|
+
'id_ed25519*',
|
|
324
|
+
'id_ecdsa*',
|
|
325
|
+
'id_ed448*',
|
|
326
|
+
'id_dsa*',
|
|
327
|
+
'secrets*.json',
|
|
328
|
+
'secrets*.yml',
|
|
329
|
+
'*api_key*',
|
|
330
|
+
'*token*',
|
|
331
|
+
'',
|
|
332
|
+
'# --- 仓库自身(不入包) ---',
|
|
333
|
+
'.git/',
|
|
334
|
+
'.dspackignore',
|
|
335
|
+
'release/',
|
|
336
|
+
'README.md',
|
|
337
|
+
'',
|
|
338
|
+
].join('\n');
|
|
339
|
+
}
|
package/src/core/scan.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { isExcluded } from './security.js';
|
|
2
|
+
import { HOME_ARTIFACTS } from './home-store.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 递归扫描 Profile 目录(主机无关版):
|
|
6
|
+
* - 跳过符号链接(防止逃出 Profile 目录 / 死循环)
|
|
7
|
+
* - 命中安全规则的文件/目录记入 excluded
|
|
8
|
+
* - 返回 { files: [{rel, abs, size}], excluded: [{rel, abs, reason}] }
|
|
9
|
+
* rel 一律用 '/' 分隔。
|
|
10
|
+
*
|
|
11
|
+
* @param {Host} host Host 实例
|
|
12
|
+
* @param {string} profileDir Profile 目录绝对路径
|
|
13
|
+
*/
|
|
14
|
+
export async function scanProfile(host, profileDir) {
|
|
15
|
+
const files = [];
|
|
16
|
+
const excluded = [];
|
|
17
|
+
await walk(host, profileDir, '', files, excluded);
|
|
18
|
+
files.sort((a, b) => a.rel.localeCompare(b.rel));
|
|
19
|
+
return { files, excluded };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 按 include 白名单(rel 路径数组或 Set)过滤扫描文件。
|
|
24
|
+
* undefined/null = 全量;[] / 空 Set = 空(显式一个都不选);否则取白名单命中项。
|
|
25
|
+
* 机器文件与 overrides 候选都走同一白名单(manifest.json 由打包侧单独写入,不在 files 里)。
|
|
26
|
+
*/
|
|
27
|
+
export function selectFiles(files, include) {
|
|
28
|
+
if (include === undefined || include === null) return files;
|
|
29
|
+
const set = include instanceof Set ? include : new Set(include);
|
|
30
|
+
return files.filter((f) => set.has(f.rel));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function walk(host, dir, rel, files, excluded) {
|
|
34
|
+
let entries;
|
|
35
|
+
try {
|
|
36
|
+
entries = await host.readdir(dir);
|
|
37
|
+
} catch {
|
|
38
|
+
return; // 无权限等:跳过该目录
|
|
39
|
+
}
|
|
40
|
+
if (!Array.isArray(entries)) return;
|
|
41
|
+
|
|
42
|
+
for (const entry of entries) {
|
|
43
|
+
if (!entry || typeof entry.name !== 'string') continue;
|
|
44
|
+
const relPath = rel ? `${rel}/${entry.name}` : entry.name;
|
|
45
|
+
|
|
46
|
+
if (entry.type === 'symlink') {
|
|
47
|
+
// home 根下的 skills / .agent-presets 是换指 junction:当目录继续遍历(rel 仍记 skills/...)。
|
|
48
|
+
// 其余 symlink(如 profiles/desktop)照旧跳过。
|
|
49
|
+
if (rel === '' && HOME_ARTIFACTS.has(entry.name)) {
|
|
50
|
+
await walk(host, entry.abs, relPath, files, excluded);
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
excluded.push({ rel: relPath, abs: entry.abs, reason: 'symlink' });
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (entry.type === 'dir') {
|
|
58
|
+
if (isExcluded(relPath)) {
|
|
59
|
+
excluded.push({ rel: relPath, abs: entry.abs, reason: 'deny' });
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
await walk(host, entry.abs, relPath, files, excluded);
|
|
63
|
+
} else if (entry.type === 'file') {
|
|
64
|
+
if (isExcluded(relPath)) {
|
|
65
|
+
excluded.push({ rel: relPath, abs: entry.abs, reason: 'deny' });
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
let size = 0;
|
|
69
|
+
try {
|
|
70
|
+
const st = await host.stat(entry.abs);
|
|
71
|
+
size = st && typeof st.size === 'number' ? st.size : 0;
|
|
72
|
+
} catch {
|
|
73
|
+
// 读不到大小(如权限问题):记为 0
|
|
74
|
+
}
|
|
75
|
+
files.push({ rel: relPath, abs: entry.abs, size });
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// 打包安全规则:决定哪些文件绝不能进入整合包(overrides/ 与根机器文件同样适用)。
|
|
2
|
+
// 规则分四类:精确名称(匹配任意路径段)、扩展名、文件名正则、相对路径正则。
|
|
3
|
+
// 移植自旧版 CLI 的 security 规则,并随 `.dspack` 格式追加嵌套压缩包排除。
|
|
4
|
+
|
|
5
|
+
/** 任何路径段命中即排除(依赖、构建产物、生成配置、敏感文件) */
|
|
6
|
+
const DENY_EXACT = new Set([
|
|
7
|
+
// 依赖与构建产物
|
|
8
|
+
'.git',
|
|
9
|
+
'node_modules',
|
|
10
|
+
'dist',
|
|
11
|
+
'build',
|
|
12
|
+
'coverage',
|
|
13
|
+
'.cache',
|
|
14
|
+
'.turbo',
|
|
15
|
+
'.pnpm-store',
|
|
16
|
+
// 自动生成的 DSH 配置文件(安装时由 bundles + patch 重新合成)
|
|
17
|
+
'cordis.yml',
|
|
18
|
+
// 打包产物(防嵌套)
|
|
19
|
+
'manifest.json',
|
|
20
|
+
// 其他包管理器的锁文件(DSH 使用 pnpm)
|
|
21
|
+
'package-lock.json',
|
|
22
|
+
'yarn.lock',
|
|
23
|
+
// 日志
|
|
24
|
+
'npm-debug.log',
|
|
25
|
+
'pnpm-debug.log',
|
|
26
|
+
// 敏感文件(精确名)
|
|
27
|
+
'.env',
|
|
28
|
+
'.npmrc',
|
|
29
|
+
'.netrc',
|
|
30
|
+
'.yarnrc',
|
|
31
|
+
'.yarnrc.yml',
|
|
32
|
+
'.pypirc',
|
|
33
|
+
'.npmignore',
|
|
34
|
+
// DSH_HOME 级:凭据 / 运行时状态 / 全局设置(dshhome 快照默认排除)
|
|
35
|
+
'.credentials.yaml',
|
|
36
|
+
'.anonymous-user-id',
|
|
37
|
+
'settings.yaml',
|
|
38
|
+
// 导出工作区配置快照(本工具写入,不进包)
|
|
39
|
+
'.dshpkcfg',
|
|
40
|
+
'.dspack-sources',
|
|
41
|
+
// home 级换指 slot 与运行时状态(skills/.agent-presets 真实数据 + 进度/迁移日志,不进包)
|
|
42
|
+
'.dsh-pack',
|
|
43
|
+
]);
|
|
44
|
+
|
|
45
|
+
/** 命中扩展名即排除(密钥/证书类) */
|
|
46
|
+
const DENY_EXT = new Set(['.key', '.pem', '.p12', '.pfx', '.crt', '.der', '.asc']);
|
|
47
|
+
|
|
48
|
+
/** 按文件名(basename)匹配的模式 */
|
|
49
|
+
const DENY_BASENAME = [
|
|
50
|
+
/^\.env(\..+)?$/, // .env / .env.local / .env.production ...
|
|
51
|
+
/(^|\.)credentials?\.ya?ml$/i, // credentials.yaml / .credentials.yml / my.credentials.yml
|
|
52
|
+
/\.(credential|credentials)$/i, // *.credential / *.credentials
|
|
53
|
+
/^id_(rsa|ed25519|ecdsa|ed448|dsa)(\.pub)?$/, // SSH 私钥
|
|
54
|
+
/^secrets?\.(json|ya?ml)$/i, // secrets.json / secret.yml
|
|
55
|
+
/^(api[-_]?key|apikey|token)s?([._-].*)?$/i, // api_key.txt / token.json / api.key
|
|
56
|
+
];
|
|
57
|
+
|
|
58
|
+
/** 按相对路径匹配的模式(禁止嵌套打包任何压缩包格式) */
|
|
59
|
+
const DENY_PATH = [/\.tgz$/, /\.tar\.gz$/, /\.zip$/, /\.dspack$/];
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 按相对路径前缀匹配(相对 $DSH_HOME 根)。这些是 home 级的运行时/基线目录,
|
|
63
|
+
* 只在 dshhome 快照语境下排除——用前缀而非任意段,避免误伤同名目录(如其它位置的 web/)。
|
|
64
|
+
*/
|
|
65
|
+
const DENY_PATH_PREFIX = [
|
|
66
|
+
'attachments/', // 附件(运行时数据)
|
|
67
|
+
'profiles/web/', // 安装基线 profile 模板(PROFILE_TEMPLATES)
|
|
68
|
+
'profiles/headless/',
|
|
69
|
+
'skills/.system/', // 安装内部系统技能(skipSystem)
|
|
70
|
+
];
|
|
71
|
+
|
|
72
|
+
function matchExt(name) {
|
|
73
|
+
const idx = name.lastIndexOf('.');
|
|
74
|
+
if (idx <= 0) return '';
|
|
75
|
+
return name.slice(idx).toLowerCase();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** 判断某相对路径('/' 分隔)是否必须排除。 */
|
|
79
|
+
export function isExcluded(relPath) {
|
|
80
|
+
const rel = relPath.replace(/\\/g, '/').toLowerCase();
|
|
81
|
+
const segments = rel.split('/');
|
|
82
|
+
const name = segments[segments.length - 1];
|
|
83
|
+
|
|
84
|
+
// 任一路径段命中精确名单(任意层级)
|
|
85
|
+
for (const seg of segments) {
|
|
86
|
+
if (DENY_EXACT.has(seg)) return true;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (DENY_EXT.has(matchExt(name))) return true;
|
|
90
|
+
|
|
91
|
+
for (const re of DENY_BASENAME) {
|
|
92
|
+
if (re.test(name)) return true;
|
|
93
|
+
}
|
|
94
|
+
for (const re of DENY_PATH) {
|
|
95
|
+
if (re.test(rel)) return true;
|
|
96
|
+
}
|
|
97
|
+
for (const p of DENY_PATH_PREFIX) {
|
|
98
|
+
if (rel.startsWith(p)) return true;
|
|
99
|
+
}
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// 特殊目录识别(整合包「关注 + 展示」用,不影响打包布局)。
|
|
2
|
+
// 依据 DSH 目录契约:
|
|
3
|
+
// - skill(dsh-skill-filesystem):skills/<name>.md(平铺)或 skills/<name>/SKILL.md(目录 bundle)
|
|
4
|
+
// - agent-preset(dsh-agent-presets):.agent-presets/<id>/agent.cordis.yml(组装文件;目录内还可有 preset.yml、自附 skill 资产)
|
|
5
|
+
// - icon:icons|icon/<…>.<图片扩展名> 或根 logo.<图片扩展名>(manifest.icon 指向的相对路径)
|
|
6
|
+
// 这些目录随扫描进 overrides/(skill / agent-preset / icon 都在 profile 根,安装后覆盖回 profile 根)。
|
|
7
|
+
|
|
8
|
+
const IMG_EXT = /\.(png|jpe?g|webp|ico|svg)$/i;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 从 scan.files 里识别特殊目录,返回 { skills, agentPresets, icons }。
|
|
12
|
+
* @param {{rel:string}[]} files scanProfile 的 files(rel 用 '/' 分隔)
|
|
13
|
+
*/
|
|
14
|
+
export function summarizeSpecial(files) {
|
|
15
|
+
const skills = [];
|
|
16
|
+
const agentPresets = [];
|
|
17
|
+
const icons = [];
|
|
18
|
+
|
|
19
|
+
for (const f of files ?? []) {
|
|
20
|
+
const rel = String(f.rel || '').replace(/\\/g, '/');
|
|
21
|
+
if (!rel) continue;
|
|
22
|
+
const seg = rel.split('/');
|
|
23
|
+
|
|
24
|
+
if (seg[0] === '.agent-presets') {
|
|
25
|
+
// 以 agent.cordis.yml 判定一个 preset 存在;目录内其它资产(自附 skill 等)不再单独归类
|
|
26
|
+
if (seg.length >= 3 && seg[2] === 'agent.cordis.yml') {
|
|
27
|
+
agentPresets.push({ id: seg[1], file: rel });
|
|
28
|
+
}
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (seg[0] === 'skills') {
|
|
33
|
+
if (seg.length === 2 && seg[1].endsWith('.md')) {
|
|
34
|
+
skills.push({ name: seg[1].slice(0, -3), file: rel });
|
|
35
|
+
} else if (seg.length >= 3 && seg[2] === 'SKILL.md') {
|
|
36
|
+
skills.push({ name: seg[1], file: rel });
|
|
37
|
+
}
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (IMG_EXT.test(rel)) {
|
|
42
|
+
// icons/xxx.png 或 icon/xxx.png(任意深度),或根 logo.png
|
|
43
|
+
if ((seg[0] === 'icons' || seg[0] === 'icon') || (seg.length === 1 && /^logo\./i.test(seg[0]))) {
|
|
44
|
+
icons.push(rel);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return { skills, agentPresets, icons };
|
|
50
|
+
}
|
package/src/core/tar.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// 极简 tar(ustar)解包/打包:解包服务于「从 NPM 拉取管理器」与 vendored 直挂;
|
|
2
|
+
// 打包服务于导出侧 vendored 重打包(死上游依赖按 npm tarball 规范从 node_modules 重建)。
|
|
3
|
+
//
|
|
4
|
+
// 只提取普通文件(typeflag '0' 或 NUL);目录('5')、符号链接('2')与 PAX/GNU 扩展头
|
|
5
|
+
// ('x'/'g'/'L')一律跳过。限制:不解析 PAX 扩展出来的超长文件名——我们自己的包路径都
|
|
6
|
+
// 很短(package/src/...),不会触发;若未来文件路径变长,这里需要补 PAX 处理。
|
|
7
|
+
import { gunzipSync, gzipSync } from 'fflate';
|
|
8
|
+
|
|
9
|
+
const decoder = new TextDecoder();
|
|
10
|
+
const encoder = new TextEncoder();
|
|
11
|
+
|
|
12
|
+
function readCString(buf, start, len) {
|
|
13
|
+
let end = start;
|
|
14
|
+
while (end < start + len && buf[end] !== 0) end++;
|
|
15
|
+
return decoder.decode(buf.subarray(start, end));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function readOctal(buf, start, len) {
|
|
19
|
+
const s = readCString(buf, start, len).trim();
|
|
20
|
+
return s ? parseInt(s, 8) || 0 : 0;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 把 npm tarball(gzip 压缩的 tar)解成 { 路径: Uint8Array }。
|
|
25
|
+
* @param {Uint8Array} tgz gzip+tar 字节
|
|
26
|
+
* @returns {Record<string, Uint8Array>}
|
|
27
|
+
*/
|
|
28
|
+
export function untar(tgz) {
|
|
29
|
+
const buf = gunzipSync(tgz);
|
|
30
|
+
const out = {};
|
|
31
|
+
let off = 0;
|
|
32
|
+
while (off + 512 <= buf.length) {
|
|
33
|
+
const block = buf.subarray(off, off + 512);
|
|
34
|
+
if (block[0] === 0) break; // 结束块(全零 padding)
|
|
35
|
+
|
|
36
|
+
const name = readCString(block, 0, 100);
|
|
37
|
+
const prefix = readCString(block, 345, 155);
|
|
38
|
+
const size = readOctal(block, 124, 12);
|
|
39
|
+
const typeflag = String.fromCharCode(block[156]);
|
|
40
|
+
const full = prefix ? `${prefix}/${name}` : name;
|
|
41
|
+
|
|
42
|
+
off += 512;
|
|
43
|
+
if ((typeflag === '0' || typeflag === '\0') && full) {
|
|
44
|
+
out[full] = buf.subarray(off, off + size);
|
|
45
|
+
}
|
|
46
|
+
off += Math.ceil(size / 512) * 512;
|
|
47
|
+
}
|
|
48
|
+
return out;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/* ---------------------------------------------------------------------------
|
|
52
|
+
* 打包(ustar):导出侧 vendored 重打包用(unpublished / local-modified 场景)。
|
|
53
|
+
* ------------------------------------------------------------------------- */
|
|
54
|
+
|
|
55
|
+
/** ustar 头校验和:chksum 字段(148..156)按 8 个空格计入后全体字节求和。 */
|
|
56
|
+
function headerChecksum(header) {
|
|
57
|
+
let sum = 0;
|
|
58
|
+
for (let i = 0; i < 512; i++) sum += header[i];
|
|
59
|
+
return sum;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** 造一个 ustar 文件条目(header + 数据 + 512 对齐;mode 0644、typeflag '0')。 */
|
|
63
|
+
function tarEntry(name, data) {
|
|
64
|
+
const header = new Uint8Array(512);
|
|
65
|
+
header.set(encoder.encode(String(name).slice(0, 99)), 0);
|
|
66
|
+
header.set(encoder.encode('0000644\0'), 100); // mode
|
|
67
|
+
header.set(encoder.encode('0000000\0'), 108); // uid
|
|
68
|
+
header.set(encoder.encode('0000000\0'), 116); // gid
|
|
69
|
+
header.set(encoder.encode(`${data.length.toString(8).padStart(11, '0')}\0`), 124); // size
|
|
70
|
+
header.set(encoder.encode('00000000000\0'), 136); // mtime
|
|
71
|
+
header.set(encoder.encode(' '.repeat(8)), 148); // chksum 占位(空格)
|
|
72
|
+
header[156] = 0x30; // typeflag '0'
|
|
73
|
+
const sum = headerChecksum(header);
|
|
74
|
+
header.set(encoder.encode(`${sum.toString(8).padStart(6, '0')}\0 `), 148);
|
|
75
|
+
const padded = new Uint8Array(Math.ceil(data.length / 512) * 512);
|
|
76
|
+
padded.set(data);
|
|
77
|
+
const out = new Uint8Array(512 + padded.length);
|
|
78
|
+
out.set(header, 0);
|
|
79
|
+
out.set(padded, 512);
|
|
80
|
+
return out;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 打 npm 风格 tarball(gzip + tar):路径以 `package/` 前缀,末尾两个零块。
|
|
85
|
+
* 与 untar 互逆;真 pnpm 安装时会校验 ustar 头(含 chksum),头字段按规范生成。
|
|
86
|
+
* @param {Record<string, Uint8Array|string>} files
|
|
87
|
+
* @returns {Uint8Array} gzip+tar 字节
|
|
88
|
+
*/
|
|
89
|
+
export function buildTarball(files) {
|
|
90
|
+
const parts = [];
|
|
91
|
+
for (const [name, content] of Object.entries(files ?? {})) {
|
|
92
|
+
const data = typeof content === 'string' ? encoder.encode(content) : content;
|
|
93
|
+
if (!data?.length) continue;
|
|
94
|
+
parts.push(tarEntry(name, data));
|
|
95
|
+
}
|
|
96
|
+
const end = new Uint8Array(1024);
|
|
97
|
+
const total = parts.reduce((s, p) => s + p.length, 0) + end.length;
|
|
98
|
+
const buf = new Uint8Array(total);
|
|
99
|
+
let off = 0;
|
|
100
|
+
for (const p of parts) { buf.set(p, off); off += p.length; }
|
|
101
|
+
buf.set(end, off);
|
|
102
|
+
return gzipSync(buf);
|
|
103
|
+
}
|