cortico-world-agent-speed 0.2.0 → 0.2.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/CHANGELOG.md +64 -0
- package/LICENSE +0 -14
- package/README.md +29 -11
- package/THIRD_PARTY_NOTICES.md +69 -0
- package/activity.d.ts +15 -0
- package/activity.js +88 -3
- package/describe.json +806 -0
- package/dist/webui.css +2 -0
- package/dist/webui.css.map +7 -0
- package/dist/webui.js +2 -0
- package/dist/webui.js.map +7 -0
- package/host/app-store.cjs +68 -0
- package/host/cursor.cjs +67 -0
- package/host/electron-main.cjs +666 -0
- package/host/input-shape.cjs +63 -0
- package/host/mouse-lease.cjs +77 -0
- package/host/overlay.cjs +139 -0
- package/host/portrait.cjs +52 -0
- package/host/preload.cjs +60 -14
- package/host/renderer-gone.cjs +24 -0
- package/host/windows.cjs +251 -0
- package/package.json +27 -7
- package/scripts/build-webui.mjs +40 -0
- package/scripts/speech-bank/build.py +166 -0
- package/scripts/speech-bank/dicts.py +76 -0
- package/src/ENV_PROMPT.en.md +51 -0
- package/src/ENV_PROMPT.md +51 -0
- package/src/asr/address.ts +25 -0
- package/src/asr/hotkey.ts +335 -0
- package/src/asr/result.ts +51 -0
- package/src/asr/segmenter.ts +269 -0
- package/src/asr/sherpa.ts +280 -0
- package/src/asr/simplify.ts +38 -0
- package/src/asr/system-recognizer.ts +292 -0
- package/src/asr/system-sapi.ps1 +173 -0
- package/src/asr/transcription-api.ts +176 -0
- package/src/chat.ts +212 -0
- package/src/config.ts +481 -0
- package/src/cua/ENV_PROMPT.en.md +29 -0
- package/src/cua/ENV_PROMPT.md +29 -0
- package/src/cua/config.ts +88 -0
- package/src/cua/cua.ts +712 -0
- package/src/cua/engine/darwin.ts +410 -0
- package/src/cua/engine/display.ts +55 -0
- package/src/cua/engine/fail.ts +15 -0
- package/src/cua/engine/foreground.ts +8 -0
- package/src/cua/engine/image.ts +57 -0
- package/src/cua/engine/keys.ts +70 -0
- package/src/cua/engine/linux-keys.ts +29 -0
- package/src/cua/engine/linux.ts +343 -0
- package/src/cua/engine/mac-keys.ts +57 -0
- package/src/cua/engine/user-activity.ts +54 -0
- package/src/cua/engine/wait-idle.ts +31 -0
- package/src/cua/engine/win32.ts +299 -0
- package/src/cua/engine-child.ts +274 -0
- package/src/cua/engine-ipc.ts +140 -0
- package/src/cua/i18n/de.ts +53 -0
- package/src/cua/i18n/en.ts +63 -0
- package/src/cua/i18n/es-419.ts +53 -0
- package/src/cua/i18n/fr.ts +53 -0
- package/src/cua/i18n/index.ts +26 -0
- package/src/cua/i18n/it.ts +53 -0
- package/src/cua/i18n/ja.ts +53 -0
- package/src/cua/i18n/ko.ts +53 -0
- package/src/cua/i18n/pt-BR.ts +53 -0
- package/src/cua/i18n/ru.ts +59 -0
- package/src/cua/i18n/zh-Hant.ts +53 -0
- package/src/cua/i18n/zh.ts +59 -0
- package/src/cua/model-text.ts +252 -0
- package/src/cua/terminal.ts +31 -0
- package/src/cua/tools.ts +153 -0
- package/src/definition.ts +146 -0
- package/src/host-reason.ts +32 -0
- package/src/i18n/de.ts +409 -0
- package/src/i18n/en.ts +443 -0
- package/src/i18n/es-419.ts +409 -0
- package/src/i18n/fr.ts +409 -0
- package/src/i18n/index.ts +56 -0
- package/src/i18n/it.ts +409 -0
- package/src/i18n/ja.ts +409 -0
- package/src/i18n/ko.ts +409 -0
- package/src/i18n/pt-BR.ts +409 -0
- package/src/i18n/ru.ts +415 -0
- package/src/i18n/zh-Hant.ts +409 -0
- package/src/i18n/zh.ts +440 -0
- package/src/index.ts +15 -0
- package/src/model-text.ts +344 -0
- package/src/moves.ts +101 -0
- package/src/pack-import.ts +361 -0
- package/src/packs.ts +449 -0
- package/src/runtime/download.ts +29 -0
- package/src/runtime/store.ts +293 -0
- package/src/script.ts +202 -0
- package/src/self.ts +247 -0
- package/src/server.ts +479 -0
- package/src/speech/data/CMUDICT-LICENSE +33 -0
- package/src/speech/data/IPADIC-COPYING +71 -0
- package/src/speech/data/UNICODE-LICENSE +39 -0
- package/src/speech/data/en.tsv.gz +0 -0
- package/src/speech/data/ja-ipadic.tsv.gz +0 -0
- package/src/speech/data/ja-unihan.tsv.gz +0 -0
- package/src/speech/data/zh.json +1 -0
- package/src/speech/index.ts +204 -0
- package/src/speech/ipa.ts +83 -0
- package/src/speech/japanese.ts +105 -0
- package/src/speech/jukujikun.ts +22 -0
- package/src/speech/korean.ts +35 -0
- package/src/speech/lang.ts +151 -0
- package/src/speech/rules.ts +72 -0
- package/src/speech/syllable.ts +76 -0
- package/src/status.ts +164 -0
- package/src/tools.ts +110 -0
- package/src/webui/api-rows.css +2 -0
- package/src/webui/api-rows.ts +44 -0
- package/src/webui/client.ts +451 -0
- package/src/webui/dress-dialog.css +14 -0
- package/src/webui/dress-dialog.ts +117 -0
- package/src/webui/dress-panel.ts +160 -0
- package/src/webui/key-capture.ts +60 -0
- package/src/webui/shared/modal.css +13 -0
- package/src/webui/shared/modal.ts +77 -0
- package/src/webui/shared/problem.css +15 -0
- package/src/webui/shared/problem.ts +217 -0
- package/src/webui/shared/strings.ts +5 -0
- package/src/webui/style.css +50 -0
- package/src/window-host.ts +116 -0
- package/src/world.ts +2926 -0
- package/web/activity-motion.js +117 -0
- package/web/appearance.js +60 -0
- package/web/body-host.js +257 -0
- package/web/coo/coo.js +384 -0
- package/web/coo/figure.js +15 -0
- package/web/coo/figure.json +1224 -0
- package/web/dist/dress-dialog.css +1 -0
- package/web/dist/dress-dialog.js +1 -0
- package/web/drag-row.js +49 -0
- package/web/drag.js +149 -0
- package/web/dress.css +170 -0
- package/web/dress.html +37 -0
- package/web/dress.js +786 -0
- package/web/figure-frame.html +16 -0
- package/web/figure-frame.js +128 -0
- package/web/frame-loop.js +55 -0
- package/web/hints.js +77 -0
- package/web/i18n/de.js +170 -0
- package/web/i18n/en.js +177 -0
- package/web/i18n/es-419.js +170 -0
- package/web/i18n/fr.js +170 -0
- package/web/i18n/it.js +170 -0
- package/web/i18n/ja.js +170 -0
- package/web/i18n/ko.js +170 -0
- package/web/i18n/pt-BR.js +170 -0
- package/web/i18n/ru.js +170 -0
- package/web/i18n/zh-Hant.js +170 -0
- package/web/i18n/zh.js +175 -0
- package/web/i18n.js +54 -0
- package/{renderer/vendor → web}/kit/body.js +31 -58
- package/web/looks.js +70 -0
- package/web/mic-worklet.js +36 -0
- package/web/mic.js +117 -0
- package/web/motion-frames.js +80 -0
- package/web/motion-level.js +40 -0
- package/web/new-year-2027.svg +238 -0
- package/web/overlay.html +16 -0
- package/web/overlay.js +100 -0
- package/web/pet-app.js +3000 -0
- package/web/pet.css +642 -0
- package/web/pet.html +21 -0
- package/web/scale-slider.js +259 -0
- package/web/show-hide.js +49 -0
- package/web/sound.js +245 -0
- package/web/speech/zh.json +1 -0
- package/web/speech/zh.ogg +0 -0
- package/web/speech.js +197 -0
- package/web/stage.js +160 -0
- package/web/swap-motion.js +119 -0
- package/web/ui.js +195 -0
- package/host/launch.js +0 -187
- package/host/library.cjs +0 -638
- package/host/main.cjs +0 -389
- package/index.js +0 -93
- package/renderer/index.html +0 -22
- package/renderer/pet-sandbox.html +0 -11
- package/renderer/pet.css +0 -38
- package/renderer/pet.js +0 -251
- package/renderer/vendor/activity-motion.js +0 -101
- package/renderer/vendor/figure-frame.js +0 -115
- package/renderer/vendor/pack.css +0 -2
- /package/{renderer/vendor → web}/kit/rig.js +0 -0
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Installing figure packs from the dressing page (web/dress.js). The page sends a zip, or the files of the
|
|
3
|
+
* folders it found a pack in; they are unpacked into a staging directory, every pack there is read as an
|
|
4
|
+
* installed one would be (src/packs.ts), and the person picks which to install. Installing copies a pack to
|
|
5
|
+
* `<packDir>/<id>/`. Replacing existing packs or directories requires explicit approval of their ids;
|
|
6
|
+
* a built-in id is refused. Replacing or uninstalling deletes a pack's directory only when it is inside `packDir`
|
|
7
|
+
* (`removable`): built-in packs and those of the app's other pack roots stay.
|
|
8
|
+
*
|
|
9
|
+
* A pack is a directory holding `figure.json`, found at most `PACK_DEPTH` levels below what was picked (the
|
|
10
|
+
* zip's root, a picked or dropped folder: a zip from GitHub wraps its files in `<repo>-<branch>/`, and a
|
|
11
|
+
* repository may keep its pack in a folder of its own). The search does not go into a pack.
|
|
12
|
+
*/
|
|
13
|
+
import { randomUUID } from 'node:crypto';
|
|
14
|
+
import { existsSync } from 'node:fs';
|
|
15
|
+
import { cp, mkdir, mkdtemp, readdir, readFile, rm, stat, writeFile } from 'node:fs/promises';
|
|
16
|
+
import { tmpdir } from 'node:os';
|
|
17
|
+
import { extname, join, normalize, sep } from 'node:path';
|
|
18
|
+
import { unzipSync } from 'fflate';
|
|
19
|
+
import { MANIFEST_FILE, readManifest, type FigurePack, type Names } from './packs.ts';
|
|
20
|
+
import { petText, type PetText } from './i18n/index.ts';
|
|
21
|
+
|
|
22
|
+
export const PACK_DEPTH = 3;
|
|
23
|
+
/** An import bigger than this, as sent, gets a size warning on the page. The largest pack shipped, the whale, is 12 MB. */
|
|
24
|
+
export const IMPORT_LARGE = 128 * 1024 * 1024;
|
|
25
|
+
/** The page's own framing of a folder's files (`filesFromBundle`). */
|
|
26
|
+
export const BUNDLE_TYPE = 'application/x-figure-files';
|
|
27
|
+
/** A thumbnail bigger than this is left out of the list sent to the page, which shows the pack without one. */
|
|
28
|
+
const THUMB_MAX = 1024 * 1024;
|
|
29
|
+
const IMAGE: Record<string, string> = { '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.webp': 'image/webp' };
|
|
30
|
+
|
|
31
|
+
export interface ImportFile { path: string; bytes: Uint8Array }
|
|
32
|
+
|
|
33
|
+
/** A pack found in what was sent, as the page lists it for the person to pick. */
|
|
34
|
+
export interface StagedPack {
|
|
35
|
+
/** Where it was found, `/`-separated, '' for the root of what was sent. */
|
|
36
|
+
dir: string;
|
|
37
|
+
id: string;
|
|
38
|
+
name: Names;
|
|
39
|
+
version: string;
|
|
40
|
+
author: string | null;
|
|
41
|
+
credits: Array<{ role: string; name: string }>;
|
|
42
|
+
/** A data URL of its thumbnail. */
|
|
43
|
+
thumb: string | null;
|
|
44
|
+
axes: number;
|
|
45
|
+
words: number;
|
|
46
|
+
sounds: number;
|
|
47
|
+
/** Parts of it this version leaves out (src/packs.ts `readManifest`). */
|
|
48
|
+
skipped: string[];
|
|
49
|
+
/** The version of the installed pack it would replace. */
|
|
50
|
+
installed: string | null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface StageResult {
|
|
54
|
+
token: string;
|
|
55
|
+
packs: StagedPack[];
|
|
56
|
+
/** Directories with a `figure.json` that cannot be installed, and why. */
|
|
57
|
+
problems: Array<{ dir: string; reason: string }>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** A path inside what was sent: its segments, or null when it is empty, absolute, or climbs out. */
|
|
61
|
+
function segments(path: string): string[] | null {
|
|
62
|
+
const parts = path.split(/[\\/]/).filter((p) => p !== '' && p !== '.');
|
|
63
|
+
if (!parts.length || /^[a-z]:/i.test(path) || path.startsWith('/') || path.startsWith('\\')) return null;
|
|
64
|
+
return parts.some((p) => p === '..' || /[<>:"|?*\u0000-\u001f]/.test(p)) ? null : parts;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The files of a zip, its directories left out; a string says why it cannot be read. */
|
|
68
|
+
export function filesFromZip(bytes: Uint8Array, t: PetText['importing'] = petText().importing, expandedLimit?: number): ImportFile[] | string {
|
|
69
|
+
let entries: Record<string, Uint8Array>;
|
|
70
|
+
try {
|
|
71
|
+
let expanded = 0;
|
|
72
|
+
entries = unzipSync(bytes, { filter: (f) => {
|
|
73
|
+
if (f.name.endsWith('/')) return false;
|
|
74
|
+
if (expandedLimit !== undefined) {
|
|
75
|
+
expanded += Math.max(f.size, f.originalSize);
|
|
76
|
+
if (expanded > expandedLimit) throw new Error(`ZIP contents exceed ${expandedLimit} bytes`);
|
|
77
|
+
}
|
|
78
|
+
return true;
|
|
79
|
+
} });
|
|
80
|
+
} catch (err) {
|
|
81
|
+
return t.badZip((err as Error).message);
|
|
82
|
+
}
|
|
83
|
+
return Object.entries(entries).map(([path, data]) => ({ path, bytes: data }));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The files the page framed (`BUNDLE_TYPE`): for each, the byte length of its path as uint32 little-endian, the
|
|
88
|
+
* path in UTF-8, the byte length of its contents as uint32, the contents.
|
|
89
|
+
*/
|
|
90
|
+
export function filesFromBundle(bytes: Uint8Array, t: PetText['importing'] = petText().importing): ImportFile[] | string {
|
|
91
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
92
|
+
const text = new TextDecoder();
|
|
93
|
+
const files: ImportFile[] = [];
|
|
94
|
+
let at = 0;
|
|
95
|
+
while (at < bytes.length) {
|
|
96
|
+
if (at + 4 > bytes.length) return t.truncated;
|
|
97
|
+
const pathLen = view.getUint32(at, true); at += 4;
|
|
98
|
+
if (at + pathLen + 4 > bytes.length) return t.truncated;
|
|
99
|
+
const path = text.decode(bytes.subarray(at, at + pathLen)); at += pathLen;
|
|
100
|
+
const size = view.getUint32(at, true); at += 4;
|
|
101
|
+
if (at + size > bytes.length) return t.truncated;
|
|
102
|
+
files.push({ path, bytes: bytes.subarray(at, at + size) });
|
|
103
|
+
at += size;
|
|
104
|
+
}
|
|
105
|
+
return files;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The directories under `root` holding a figure.json, `depth` levels down at most, not looking inside one. */
|
|
109
|
+
async function findPacks(root: string, depth: number, rel = ''): Promise<string[]> {
|
|
110
|
+
const here = join(root, ...rel.split('/').filter(Boolean));
|
|
111
|
+
const entries = await readdir(here, { withFileTypes: true });
|
|
112
|
+
if (entries.some((e) => e.isFile() && e.name === MANIFEST_FILE)) return [rel];
|
|
113
|
+
if (depth === 0) return [];
|
|
114
|
+
const found: string[] = [];
|
|
115
|
+
for (const e of entries) if (e.isDirectory()) found.push(...await findPacks(root, depth - 1, rel ? `${rel}/${e.name}` : e.name));
|
|
116
|
+
return found;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async function thumbOf(dir: string, file: string | undefined): Promise<string | null> {
|
|
120
|
+
const type = file ? IMAGE[extname(file).toLowerCase()] : undefined;
|
|
121
|
+
if (!file || !type) return null;
|
|
122
|
+
try {
|
|
123
|
+
const full = join(dir, file);
|
|
124
|
+
if ((await stat(full)).size > THUMB_MAX) return null;
|
|
125
|
+
return `data:${type};base64,${(await readFile(full)).toString('base64')}`;
|
|
126
|
+
} catch {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Whether `dir` is a directory inside `root` (not `root` itself). */
|
|
132
|
+
const within = (root: string, dir: string) => normalize(dir).startsWith(normalize(root).replace(/[\\/]+$/, '') + sep);
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* One import at a time: staging a new one drops the one before. `packDir` is where packs are installed (one of
|
|
136
|
+
* the World's pack roots); `packs` the packs as the World scans them, built-in ones first.
|
|
137
|
+
*/
|
|
138
|
+
export class PackImporter {
|
|
139
|
+
private staged: { token: string; root: string; packs: StagedPack[] } | null = null;
|
|
140
|
+
|
|
141
|
+
/** `text`: the text table of the app language, for what goes wrong; Chinese when absent. */
|
|
142
|
+
constructor(private readonly opts: { packDir: () => string; packs: () => FigurePack[]; text?: () => PetText }) {}
|
|
143
|
+
|
|
144
|
+
private get t(): PetText {
|
|
145
|
+
return this.opts.text?.() ?? petText();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Unpacks `files` into a staging directory and reads the packs in it; `fromFolders`: each file's path starts with the folder picked. */
|
|
149
|
+
async stage(files: ImportFile[], fromFolders: boolean): Promise<StageResult | { error: string }> {
|
|
150
|
+
await this.cancel();
|
|
151
|
+
const root = await mkdtemp(join(tmpdir(), 'figure-import-'));
|
|
152
|
+
try {
|
|
153
|
+
for (const f of files) {
|
|
154
|
+
const parts = segments(f.path);
|
|
155
|
+
if (!parts) { await rm(root, { recursive: true, force: true }); return { error: this.t.importing.badPath(f.path) }; }
|
|
156
|
+
await mkdir(join(root, ...parts.slice(0, -1)), { recursive: true });
|
|
157
|
+
await writeFile(join(root, ...parts), f.bytes);
|
|
158
|
+
}
|
|
159
|
+
const dirs = await findPacks(root, PACK_DEPTH + (fromFolders ? 1 : 0));
|
|
160
|
+
const installed = this.opts.packs();
|
|
161
|
+
const packs: StagedPack[] = [];
|
|
162
|
+
const problems: StageResult['problems'] = [];
|
|
163
|
+
for (const dir of dirs) {
|
|
164
|
+
const full = join(root, ...dir.split('/').filter(Boolean));
|
|
165
|
+
const skipped: string[] = [];
|
|
166
|
+
const m = readManifest(full, false, skipped, this.t.packs);
|
|
167
|
+
if (typeof m === 'string') { problems.push({ dir, reason: m }); continue; }
|
|
168
|
+
const same = installed.find((p) => p.id === m.id);
|
|
169
|
+
if (same?.builtin) { problems.push({ dir, reason: this.t.importing.builtinId(m.id) }); continue; }
|
|
170
|
+
if (packs.some((p) => p.id === m.id)) { problems.push({ dir, reason: this.t.importing.duplicateId(m.id) }); continue; }
|
|
171
|
+
packs.push({
|
|
172
|
+
dir, id: m.id, name: m.name, version: m.version, author: m.author ?? null,
|
|
173
|
+
credits: (m.credits ?? []).filter((c) => typeof c?.role === 'string' && typeof c?.name === 'string').map(({ role, name }) => ({ role, name })),
|
|
174
|
+
thumb: await thumbOf(full, m.thumb), axes: m.axes.length, words: m.vocab.length, sounds: Object.keys(m.sounds).length,
|
|
175
|
+
skipped, installed: same ? same.manifest.version : null,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
if (!packs.length) await rm(root, { recursive: true, force: true });
|
|
179
|
+
else this.staged = { token: randomUUID(), root, packs };
|
|
180
|
+
return { token: this.staged?.token ?? '', packs, problems };
|
|
181
|
+
} catch (err) {
|
|
182
|
+
await rm(root, { recursive: true, force: true });
|
|
183
|
+
return { error: this.t.importing.unpackFailed((err as Error).message) };
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** A conflict leaves every pack untouched and the token valid; `replace` names the ids approved for replacement. */
|
|
188
|
+
async install(token: string, dirs: readonly string[], replace: readonly string[] = []): Promise<{ installed: string[] } | { conflicts: string[] } | { error: string }> {
|
|
189
|
+
const staged = this.staged;
|
|
190
|
+
if (!staged || staged.token !== token) return { error: this.t.importing.expired };
|
|
191
|
+
const chosen = staged.packs.filter((p) => dirs.includes(p.dir));
|
|
192
|
+
if (!chosen.length) return { error: this.t.importing.noneChosen };
|
|
193
|
+
const packDir = this.opts.packDir();
|
|
194
|
+
const installed = this.opts.packs();
|
|
195
|
+
const builtin = chosen.find((p) => installed.some((x) => x.id === p.id && x.builtin));
|
|
196
|
+
if (builtin) return { error: this.t.importing.builtinId(builtin.id) };
|
|
197
|
+
const conflicts = chosen.filter((p) => installed.some((x) => x.id === p.id) || existsSync(join(packDir, p.id))).map((p) => p.id);
|
|
198
|
+
if (conflicts.some((id) => !replace.includes(id))) return { conflicts };
|
|
199
|
+
// Consume the token before writing so concurrent installs cannot reuse it.
|
|
200
|
+
this.staged = null;
|
|
201
|
+
try {
|
|
202
|
+
await mkdir(packDir, { recursive: true });
|
|
203
|
+
for (const p of chosen) {
|
|
204
|
+
const target = join(packDir, p.id);
|
|
205
|
+
if (replace.includes(p.id)) {
|
|
206
|
+
// the pack it replaces may sit in a folder named otherwise; one in another pack root is left alone
|
|
207
|
+
const old = installed.find((x) => x.id === p.id && this.removable(x));
|
|
208
|
+
if (old) await rm(old.dir, { recursive: true, force: true });
|
|
209
|
+
await rm(target, { recursive: true, force: true });
|
|
210
|
+
}
|
|
211
|
+
await cp(join(staged.root, ...p.dir.split('/').filter(Boolean)), target, { recursive: true, force: false, errorOnExist: true });
|
|
212
|
+
}
|
|
213
|
+
return { installed: chosen.map((p) => p.id) };
|
|
214
|
+
} finally {
|
|
215
|
+
await rm(staged.root, { recursive: true, force: true });
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** Whether `pack` is an installed pack inside `packDir`, the only kind replacing or uninstalling deletes. */
|
|
220
|
+
removable(pack: FigurePack): boolean {
|
|
221
|
+
return !pack.builtin && within(this.opts.packDir(), pack.dir);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** Deletes the directory of the installed pack `id`; the next scan no longer lists it. */
|
|
225
|
+
async remove(id: string): Promise<{ removed: string } | { error: string }> {
|
|
226
|
+
const pack = this.opts.packs().find((p) => p.id === id);
|
|
227
|
+
if (!pack) return { error: this.t.removing.notInstalled(id) };
|
|
228
|
+
if (pack.builtin) return { error: this.t.removing.builtin(id) };
|
|
229
|
+
if (!this.removable(pack)) return { error: this.t.removing.elsewhere(id) };
|
|
230
|
+
try {
|
|
231
|
+
await rm(pack.dir, { recursive: true, force: true });
|
|
232
|
+
} catch (err) {
|
|
233
|
+
return { error: this.t.removing.failed((err as Error).message) };
|
|
234
|
+
}
|
|
235
|
+
return { removed: id };
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Drops the staged import, if any. */
|
|
239
|
+
async cancel(): Promise<void> {
|
|
240
|
+
const staged = this.staged;
|
|
241
|
+
this.staged = null;
|
|
242
|
+
if (staged) await rm(staged.root, { recursive: true, force: true });
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const ZIP_CHUNK_BYTES = 48 * 1024;
|
|
247
|
+
const ZIP_EXPANDED_MAX = 2 * IMPORT_LARGE;
|
|
248
|
+
const BASE64 = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/][AQgw]==|[A-Za-z0-9+/]{2}[AEIMQUYcgkosw048]=)?$/;
|
|
249
|
+
|
|
250
|
+
type ZipUpload = {
|
|
251
|
+
token: string;
|
|
252
|
+
bytes: Buffer | null;
|
|
253
|
+
received: number;
|
|
254
|
+
importer?: PackImporter;
|
|
255
|
+
stage?: StageResult;
|
|
256
|
+
pending?: Promise<unknown>;
|
|
257
|
+
cancelled?: boolean;
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
/** The settings invoke flow owns its staging slot; the standalone dress page cannot replace or cancel it. */
|
|
261
|
+
export class DressZipImport {
|
|
262
|
+
private session: ZipUpload | null = null;
|
|
263
|
+
|
|
264
|
+
constructor(private readonly createImporter: (text: PetText) => PackImporter) {}
|
|
265
|
+
|
|
266
|
+
async start(total: unknown, text: PetText): Promise<string> {
|
|
267
|
+
if (typeof total !== 'number' || !Number.isSafeInteger(total) || total < 22 || total > IMPORT_LARGE) {
|
|
268
|
+
throw new Error(`ZIP size must be between 22 and ${IMPORT_LARGE} bytes`);
|
|
269
|
+
}
|
|
270
|
+
if (this.session?.pending) throw new Error(text.importing.expired);
|
|
271
|
+
await this.cancel();
|
|
272
|
+
if (this.session) throw new Error(text.importing.expired);
|
|
273
|
+
const token = randomUUID();
|
|
274
|
+
this.session = { token, bytes: Buffer.allocUnsafe(total), received: 0 };
|
|
275
|
+
return token;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
chunk(token: unknown, base64: unknown, text: PetText): void {
|
|
279
|
+
const s = this.upload(token, text);
|
|
280
|
+
const { bytes } = s;
|
|
281
|
+
if (typeof base64 !== 'string' || !base64.length || base64.length > ZIP_CHUNK_BYTES / 3 * 4
|
|
282
|
+
|| base64.length % 4 !== 0 || !BASE64.test(base64)) {
|
|
283
|
+
throw new Error(text.importing.truncated);
|
|
284
|
+
}
|
|
285
|
+
const size = Buffer.byteLength(base64, 'base64');
|
|
286
|
+
if (size > bytes.length - s.received) throw new Error(text.importing.truncated);
|
|
287
|
+
s.received += bytes.write(base64, s.received, size, 'base64');
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
finish(token: unknown, text: PetText): Promise<{
|
|
291
|
+
token: string;
|
|
292
|
+
packs: Array<Pick<StagedPack, 'dir' | 'id' | 'name' | 'installed'>>;
|
|
293
|
+
problems: StageResult['problems'];
|
|
294
|
+
}> {
|
|
295
|
+
const s = this.upload(token, text);
|
|
296
|
+
if (s.received !== s.bytes.length) throw new Error(text.importing.truncated);
|
|
297
|
+
const files = filesFromZip(s.bytes, text.importing, ZIP_EXPANDED_MAX);
|
|
298
|
+
this.session = null;
|
|
299
|
+
const session: ZipUpload = { token: s.token, bytes: null, received: 0 };
|
|
300
|
+
if (typeof files === 'string') throw new Error(files);
|
|
301
|
+
this.session = session;
|
|
302
|
+
const importer = this.createImporter(text);
|
|
303
|
+
session.importer = importer;
|
|
304
|
+
const pending = (async () => {
|
|
305
|
+
try {
|
|
306
|
+
const stage = await importer.stage(files, false);
|
|
307
|
+
if (session.cancelled) throw new Error(text.importing.expired);
|
|
308
|
+
if ('error' in stage) throw new Error(stage.error);
|
|
309
|
+
if (stage.token) session.stage = stage;
|
|
310
|
+
const { token, packs, problems } = stage;
|
|
311
|
+
return { token, packs: packs.map(({ dir, id, name, installed }) => ({ dir, id, name, installed })), problems };
|
|
312
|
+
} finally {
|
|
313
|
+
delete session.pending;
|
|
314
|
+
if (!session.stage && !session.cancelled) this.session = null;
|
|
315
|
+
}
|
|
316
|
+
})();
|
|
317
|
+
session.pending = pending;
|
|
318
|
+
return pending;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
async install(token: unknown, dirs: unknown, replace: unknown, text: PetText): Promise<{ installed: string[] } | { conflicts: string[] }> {
|
|
322
|
+
const s = this.session;
|
|
323
|
+
if (!s?.importer || !s.stage || s.cancelled || s.pending || token !== s.stage.token) throw new Error(text.importing.expired);
|
|
324
|
+
const chosen = s.stage.packs.filter((p) => Array.isArray(dirs) && dirs.includes(p.dir));
|
|
325
|
+
if (!Array.isArray(dirs) || !chosen.length || dirs.length !== chosen.length
|
|
326
|
+
|| !Array.isArray(replace) || !replace.every((id) => chosen.some((p) => p.id === id))) {
|
|
327
|
+
throw new Error(text.importing.noneChosen);
|
|
328
|
+
}
|
|
329
|
+
const pending = s.importer.install(s.stage.token, dirs, replace);
|
|
330
|
+
s.pending = pending;
|
|
331
|
+
try {
|
|
332
|
+
const result = await pending;
|
|
333
|
+
if ('error' in result) throw new Error(result.error);
|
|
334
|
+
if ('installed' in result && !s.cancelled) this.session = null;
|
|
335
|
+
return result;
|
|
336
|
+
} finally {
|
|
337
|
+
delete s.pending;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** An omitted token is for World shutdown and resetting the panel slot, never a panel request. */
|
|
342
|
+
async cancel(token?: string): Promise<void> {
|
|
343
|
+
const s = this.session;
|
|
344
|
+
if (!s || (token !== undefined && token !== s.token && token !== s.stage?.token)) return;
|
|
345
|
+
s.cancelled = true;
|
|
346
|
+
s.bytes = null;
|
|
347
|
+
// Do not delete a staging directory while it is being built or copied into the install root.
|
|
348
|
+
await s.pending?.catch(() => {});
|
|
349
|
+
try {
|
|
350
|
+
await s.importer?.cancel();
|
|
351
|
+
} finally {
|
|
352
|
+
if (this.session === s) this.session = null;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
private upload(token: unknown, text: PetText): ZipUpload & { bytes: Buffer } {
|
|
357
|
+
const s = this.session;
|
|
358
|
+
if (!s?.bytes || s.cancelled || token !== s.token) throw new Error(text.importing.expired);
|
|
359
|
+
return s as ZipUpload & { bytes: Buffer };
|
|
360
|
+
}
|
|
361
|
+
}
|