cortico-world-extra-actions 0.3.20 → 0.3.22
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/package.json +1 -1
- package/src/online.js +64 -0
- package/src/tar.js +54 -0
- package/src/world.js +27 -2
- package/tools/test_download.mjs +3 -2
- package/tools/_tmp-download/data/shenshen-library/actions/core/cheer-hop/action.json +0 -1
- package/tools/_tmp-download/data/shenshen-library/actions/core/cheer-hop/anim.webp +0 -1
- package/tools/_tmp-download/data/shenshen-library/actions/eat/eat-dumplings/action.json +0 -1
- package/tools/_tmp-download/data/shenshen-library/actions/eat/eat-dumplings/anim.webp +0 -1
package/package.json
CHANGED
package/src/online.js
CHANGED
|
@@ -11,11 +11,39 @@
|
|
|
11
11
|
*/
|
|
12
12
|
import { existsSync, mkdirSync, readdirSync, rmSync } from 'node:fs';
|
|
13
13
|
import { isAbsolute, join, normalize, sep } from 'node:path';
|
|
14
|
+
import { createHash } from 'node:crypto';
|
|
14
15
|
import { extractZip, listZip } from './zip.js';
|
|
16
|
+
import { extractTarGz } from './tar.js';
|
|
15
17
|
|
|
16
18
|
/** 维护者把 Gitee 仓库的 raw/下载前缀填这里(末尾不要带斜杠)。 */
|
|
17
19
|
export const DEFAULT_REMOTE = '';
|
|
18
20
|
|
|
21
|
+
/** 动作库分片:主源 Gitee(国内直连快),sha256 用于完整性校验。 */
|
|
22
|
+
export const LIB_PARTS = [
|
|
23
|
+
{ name: 'cortico-world-shenshen-lib-1', version: '1.0.0', modules: 15, bytes: 51758812, sha256: 'db10bf40ae3078d087966b048fd1ab9704b16ffd99179e80799e75ddf2efc1ca',
|
|
24
|
+
url: 'https://gitee.com/Daniele-WOLF-Holo/shenshen-actions/releases/download/library/lib-1.tgz' },
|
|
25
|
+
{ name: 'cortico-world-shenshen-lib-2', version: '1.0.0', modules: 15, bytes: 53693012, sha256: '541bb2ce6ee0ccc53aa9437213ec15e7b981ee0edb04b33037d941c235b6fcff',
|
|
26
|
+
url: 'https://gitee.com/Daniele-WOLF-Holo/shenshen-actions/releases/download/library/lib-2.tgz' },
|
|
27
|
+
{ name: 'cortico-world-shenshen-lib-3', version: '1.0.0', modules: 15, bytes: 53643090, sha256: 'b4a908bf0c4fb778829e13ca230801d3110935104d6b5b2e692131372e946182',
|
|
28
|
+
url: 'https://gitee.com/Daniele-WOLF-Holo/shenshen-actions/releases/download/library/lib-3.tgz' },
|
|
29
|
+
{ name: 'cortico-world-shenshen-lib-4', version: '1.0.0', modules: 13, bytes: 53889958, sha256: 'c528c70954cb20afb57568c85ada3b6af8ca274a059bde9c2cba8774e37f65fe',
|
|
30
|
+
url: 'https://gitee.com/Daniele-WOLF-Holo/shenshen-actions/releases/download/library/lib-4.tgz' },
|
|
31
|
+
{ name: 'cortico-world-shenshen-lib-5', version: '1.0.0', modules: 13, bytes: 52249938, sha256: '575b60e97fe87198dc9794f3848b6cfd422238df081810ac7169c804cfa80212',
|
|
32
|
+
url: 'https://gitee.com/Daniele-WOLF-Holo/shenshen-actions/releases/download/library/lib-5.tgz' },
|
|
33
|
+
{ name: 'cortico-world-shenshen-lib-6', version: '1.0.0', modules: 14, bytes: 51269171, sha256: '19b06067b724555e36633ba63f0696631287278447e78d4a0606509de4b03f64',
|
|
34
|
+
url: 'https://gitee.com/Daniele-WOLF-Holo/shenshen-actions/releases/download/library/lib-6.tgz' },
|
|
35
|
+
{ name: 'cortico-world-shenshen-lib-7', version: '1.0.0', modules: 14, bytes: 51556774, sha256: 'a533d38e5504156539bbbdcf4bf231d6162f9033a83ab82317cd5c72be934a6e',
|
|
36
|
+
url: 'https://gitee.com/Daniele-WOLF-Holo/shenshen-actions/releases/download/library/lib-7.tgz' },
|
|
37
|
+
{ name: 'cortico-world-shenshen-lib-8', version: '1.0.0', modules: 6, bytes: 20030747, sha256: 'ac8fdf8c0927369226ca09f646f2a8a40c167a954260670463c64727713d0a81',
|
|
38
|
+
url: 'https://gitee.com/Daniele-WOLF-Holo/shenshen-actions/releases/download/library/lib-8.tgz' },
|
|
39
|
+
];
|
|
40
|
+
|
|
41
|
+
/** 每片的候选地址:Gitee 优先,官方 npm 兜底(npm 分片可能会被撤,404 会立刻跳过)。 */
|
|
42
|
+
export const partTarballs = (p) => [
|
|
43
|
+
p.url,
|
|
44
|
+
`https://registry.npmjs.org/${p.name}/-/${p.name}-${p.version}.tgz`,
|
|
45
|
+
].filter(Boolean);
|
|
46
|
+
|
|
19
47
|
const MAX_LIBRARY_BYTES = 800 * 1024 * 1024; // 完整库上限
|
|
20
48
|
const MAX_ACTION_BYTES = 32 * 1024 * 1024; // 单个动作上限
|
|
21
49
|
/** 没配 remoteBase 时的兜底:GitHub Release 上的完整库(国内可能慢,但不至于完全没有)。 */
|
|
@@ -119,3 +147,39 @@ export const libraryUrl = (base) => `${String(base).replace(/\/+$/, '')}/library
|
|
|
119
147
|
export const actionUrl = (base, category, id) => `${String(base).replace(/\/+$/, '')}/actions/${category}/${id}.zip`;
|
|
120
148
|
/** 扁平布局(GitHub Release 的资产做不到子目录):<base>/<id>.zip */
|
|
121
149
|
export const flatActionUrl = (base, id) => `${String(base).replace(/\/+$/, '')}/${id}.zip`;
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* 分片安装:逐个下载 npm 包 tarball → 校验 sha256 → 解压进动作库。
|
|
154
|
+
* 一片失败不影响已装好的;返回装了哪些模块。
|
|
155
|
+
*/
|
|
156
|
+
export async function installLibraryParts({ parts = LIB_PARTS, dest, log, onProgress }) {
|
|
157
|
+
mkdirSync(dest, { recursive: true });
|
|
158
|
+
const before = new Set(summarize(dest));
|
|
159
|
+
let bytes = 0;
|
|
160
|
+
let failed = 0;
|
|
161
|
+
for (let i = 0; i < parts.length; i++) {
|
|
162
|
+
const p = parts[i];
|
|
163
|
+
let buf = null;
|
|
164
|
+
let lastErr = null;
|
|
165
|
+
for (const url of partTarballs(p)) {
|
|
166
|
+
try { buf = await fetchBytes(url, MAX_LIBRARY_BYTES); break; } catch (e) { lastErr = e; }
|
|
167
|
+
}
|
|
168
|
+
if (!buf) { failed++; log?.(`第 ${i + 1} 片下载失败:${lastErr?.message ?? '未知错误'}`); continue; }
|
|
169
|
+
bytes += buf.length;
|
|
170
|
+
if (p.sha256) {
|
|
171
|
+
const got = createHash('sha256').update(buf).digest('hex');
|
|
172
|
+
if (got !== p.sha256) { failed++; log?.(`第 ${i + 1} 片校验不通过,已跳过`); continue; }
|
|
173
|
+
}
|
|
174
|
+
const r = extractTarGz(buf, {
|
|
175
|
+
dest,
|
|
176
|
+
mapPath: (rel) => (rel.startsWith('package/') ? rel.slice(8) : rel),
|
|
177
|
+
onFile: (rel) => WANT.test(rel),
|
|
178
|
+
});
|
|
179
|
+
log?.(`第 ${i + 1}/${parts.length} 片:${r.files.length} 个文件`);
|
|
180
|
+
onProgress?.(i + 1, parts.length);
|
|
181
|
+
}
|
|
182
|
+
const mods = summarize(dest);
|
|
183
|
+
const added = mods.filter((m) => !before.has(m));
|
|
184
|
+
return { ok: failed === 0, path: dest, modules: mods.length, added: added.length, failed, bytes };
|
|
185
|
+
}
|
package/src/tar.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 最小 tar.gz 读取器(只用 node:zlib)。
|
|
3
|
+
*
|
|
4
|
+
* npm 包的 tarball 就是 tar.gz —— 动作库按 npm 包分发时得能解它。
|
|
5
|
+
* 安全约束与 zip 那边一致:不安全路径整条丢掉,只允许 onFile 认的文件落盘。
|
|
6
|
+
*/
|
|
7
|
+
import { mkdirSync, writeFileSync } from 'node:fs';
|
|
8
|
+
import { dirname, join, normalize, sep } from 'node:path';
|
|
9
|
+
import { gunzipSync } from 'node:zlib';
|
|
10
|
+
import { safeZipPath } from './zip.js';
|
|
11
|
+
|
|
12
|
+
const MAX_ENTRY_BYTES = 128 * 1024 * 1024;
|
|
13
|
+
|
|
14
|
+
const str = (buf, from, to) => buf.toString('utf8', from, to).replace(/\0[\s\S]*$/, '');
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param {Buffer} buf .tgz
|
|
18
|
+
* @param {{ dest: string, onFile?: (rel: string) => boolean, mapPath?: (rel: string) => string|null }} o
|
|
19
|
+
*/
|
|
20
|
+
export function extractTarGz(buf, { dest, onFile = () => true, mapPath = null }) {
|
|
21
|
+
const tar = gunzipSync(buf);
|
|
22
|
+
const files = [];
|
|
23
|
+
let skipped = 0;
|
|
24
|
+
let off = 0;
|
|
25
|
+
const root = normalize(dest + sep);
|
|
26
|
+
|
|
27
|
+
while (off + 512 <= tar.length) {
|
|
28
|
+
const h = tar.subarray(off, off + 512);
|
|
29
|
+
if (h.every((b) => b === 0)) break; // 结束块
|
|
30
|
+
const name = str(h, 0, 100);
|
|
31
|
+
const prefix = str(h, 345, 500);
|
|
32
|
+
const sizeStr = str(h, 124, 136).trim();
|
|
33
|
+
const size = sizeStr ? parseInt(sizeStr, 8) : 0;
|
|
34
|
+
const type = h[156] === 0 ? 0x30 : h[156];
|
|
35
|
+
off += 512;
|
|
36
|
+
|
|
37
|
+
if (type === 0x30 && Number.isFinite(size) && size >= 0) {
|
|
38
|
+
let rel = safeZipPath(prefix ? `${prefix}/${name}` : name);
|
|
39
|
+
if (rel) {
|
|
40
|
+
if (mapPath) rel = mapPath(rel);
|
|
41
|
+
if (rel && onFile(rel)) {
|
|
42
|
+
if (size > MAX_ENTRY_BYTES) throw new Error(`条目太大:${rel}`);
|
|
43
|
+
const full = join(dest, ...rel.split('/'));
|
|
44
|
+
if (!normalize(full).startsWith(root)) throw new Error(`越界:${rel}`);
|
|
45
|
+
mkdirSync(dirname(full), { recursive: true });
|
|
46
|
+
writeFileSync(full, tar.subarray(off, off + size));
|
|
47
|
+
files.push(rel);
|
|
48
|
+
} else skipped++;
|
|
49
|
+
} else skipped++;
|
|
50
|
+
}
|
|
51
|
+
off += Math.ceil(size / 512) * 512;
|
|
52
|
+
}
|
|
53
|
+
return { files, skipped };
|
|
54
|
+
}
|
package/src/world.js
CHANGED
|
@@ -14,7 +14,7 @@ import { fileURLToPath } from 'node:url';
|
|
|
14
14
|
import { EXTRA_DIR, clear, inspect, isInside, packLooksSupported, prunePack, readExtra, removeOne, save } from './writer.js';
|
|
15
15
|
import { addModules, rebuildIndex, scanLibrary } from './importer.js';
|
|
16
16
|
import { ensureStarter, installStarterLibrary, libraryHealth, restorePack, safePackName } from './starter.js';
|
|
17
|
-
import { FALLBACK_ACTION_BASE, FALLBACK_LIBRARY, actionUrl, flatActionUrl, installAction, installLibrary, libraryUrl } from './online.js';
|
|
17
|
+
import { FALLBACK_ACTION_BASE, FALLBACK_LIBRARY, LIB_PARTS, actionUrl, flatActionUrl, installAction, installLibrary, installLibraryParts, libraryUrl } from './online.js';
|
|
18
18
|
import { petFigures, petState } from './pet.js';
|
|
19
19
|
|
|
20
20
|
const ID = 'extra-actions';
|
|
@@ -535,10 +535,35 @@ export class ExtraActionsWorld {
|
|
|
535
535
|
/** 一键:下载完整动作库 → 解压进动作库 → 设为当前库。 */
|
|
536
536
|
async downloadLibrary() {
|
|
537
537
|
const info = this.downloadInfo();
|
|
538
|
+
const existing = this.libraryDir();
|
|
539
|
+
// 没配自家源时:走 npm 分片(国内走 npmmirror,比 GitHub 稳)
|
|
540
|
+
if (!info.base && LIB_PARTS.length) {
|
|
541
|
+
const dest = existing && existsSync(existing) ? existing : join(this.dataDir, 'shenshen-library');
|
|
542
|
+
const r = await installLibraryParts({
|
|
543
|
+
dest,
|
|
544
|
+
log: (m) => this.host?.log?.info?.(m),
|
|
545
|
+
onProgress: (i, n) => this.host?.log?.info?.(`动作库 ${i}/${n} 片`),
|
|
546
|
+
});
|
|
547
|
+
// 分片有失败(镜像没同步等)时就再用整包兜一次,别让用户停在半截
|
|
548
|
+
if (!r.ok || r.modules < 20) {
|
|
549
|
+
this.host?.log?.warn?.(`分片装了 ${r.modules} 段(失败 ${r.failed} 片),改用整包兜底`);
|
|
550
|
+
try {
|
|
551
|
+
const z = await installLibrary({ url: FALLBACK_LIBRARY, libraryDir: r.path, dataDir: this.dataDir, log: (m) => this.host?.log?.info?.(m) });
|
|
552
|
+
this.saveSettings({ libraryDir: z.path });
|
|
553
|
+
this.bustCatalog();
|
|
554
|
+
return { ok: true, ...z, libraryDir: z.path, viaZip: true };
|
|
555
|
+
} catch (e) {
|
|
556
|
+
this.host?.log?.warn?.('整包兜底也失败了:' + String(e?.message ?? e));
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
this.saveSettings({ libraryDir: r.path });
|
|
560
|
+
this.bustCatalog();
|
|
561
|
+
return { ok: r.ok, ...r, libraryDir: r.path };
|
|
562
|
+
}
|
|
538
563
|
if (!info.libraryUrl) throw new Error('还没配置下载源');
|
|
539
564
|
const r = await installLibrary({
|
|
540
565
|
url: info.libraryUrl,
|
|
541
|
-
libraryDir:
|
|
566
|
+
libraryDir: existing,
|
|
542
567
|
dataDir: this.dataDir,
|
|
543
568
|
log: (m) => this.host?.log?.info?.(m),
|
|
544
569
|
});
|
package/tools/test_download.mjs
CHANGED
|
@@ -53,7 +53,7 @@ const oneZip = buildZip([
|
|
|
53
53
|
['action.json', JSON.stringify({ id: 'fireworks', category: 'festival', word: 'fireworks', zh: '放烟花', say: '放烟花', clip: 'actions/festival/fireworks', frames: 1, fps: 24, seconds: 1, anim: true })],
|
|
54
54
|
['anim.webp', 'RIFFxxxxWEBPVP8 '],
|
|
55
55
|
]);
|
|
56
|
-
const routes = new Map([['/lib/library.zip', libZip], ['/lib/actions/festival/fireworks.zip', oneZip]]);
|
|
56
|
+
const routes = new Map([['/lib/library.zip', libZip], ['/lib/actions/festival/fireworks.zip', oneZip], ['/lib/fireworks.zip', oneZip]]);
|
|
57
57
|
const server = createServer((req, res) => {
|
|
58
58
|
const b = routes.get(req.url);
|
|
59
59
|
if (!b) { res.writeHead(404).end(); return; }
|
|
@@ -67,7 +67,7 @@ rmSync(root, { recursive: true, force: true });
|
|
|
67
67
|
const dataDir = join(root, 'data');
|
|
68
68
|
mkdirSync(dataDir, { recursive: true });
|
|
69
69
|
|
|
70
|
-
const cfg = { remoteBase: base };
|
|
70
|
+
const cfg = { remoteBase: base, remoteActionBase: base }; // 单段走扁平布局 <base>/<id>.zip
|
|
71
71
|
// 宿主的 persist 会写回同一个 cfg,这里照做,否则 libraryDir() 读不到
|
|
72
72
|
const settings = cfg;
|
|
73
73
|
const ctx = {
|
|
@@ -87,6 +87,7 @@ const check = (fn, msg) => { try { fn(); console.log('ok ' + msg); } catch (e)
|
|
|
87
87
|
let st = await w.invoke('picker', 'state');
|
|
88
88
|
check(() => assert.equal(st.download.perAction, true), 'remoteBase makes per-action downloads available');
|
|
89
89
|
check(() => assert.equal(st.download.libraryUrl, base + '/library.zip'), 'library url = <base>/library.zip');
|
|
90
|
+
check(() => assert.equal(st.download.flat, true), 'per-action uses the flat layout when remoteActionBase is set');
|
|
90
91
|
|
|
91
92
|
/* 2. 一键下载完整库 */
|
|
92
93
|
const r = await w.invoke('picker', 'download-library');
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"id":"cheer-hop","category":"core","word":"cheer-hop","zh":"cheer-hop","say":"cheer-hop","clip":"actions/core/cheer-hop","frames":1,"fps":24,"seconds":1,"anim":true}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
RIFFxxxxWEBPVP8
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"id":"eat-dumplings","category":"eat","word":"eat-dumplings","zh":"eat-dumplings","say":"eat-dumplings","clip":"actions/eat/eat-dumplings","frames":1,"fps":24,"seconds":1,"anim":true}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
RIFFxxxxWEBPVP8
|