@emiyakatuz/crispasr-agent-transcriber 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/LICENSE +21 -0
- package/README.md +41 -0
- package/bin/crispasr-agent-transcriber.js +13 -0
- package/package.json +37 -0
- package/src/cli.js +174 -0
- package/src/constants.js +65 -0
- package/src/errors.js +16 -0
- package/src/installer.js +397 -0
- package/src/marketplace.js +100 -0
- package/src/release.js +122 -0
- package/src/system.js +63 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 crispasr-agent-transcriber contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# @emiyakatuz/crispasr-agent-transcriber
|
|
2
|
+
|
|
3
|
+
Cross-platform installer for the
|
|
4
|
+
[CrispASR Agent Transcriber](https://github.com/EmiyaKatuz/crispasr-agent-transcriber)
|
|
5
|
+
Codex plugin and MCP server.
|
|
6
|
+
|
|
7
|
+
```powershell
|
|
8
|
+
npx @emiyakatuz/crispasr-agent-transcriber@latest install
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
The installer downloads the matching GitHub Release, verifies its SHA-256
|
|
12
|
+
checksum, installs Python/MCP dependencies with `uv`, installs the best
|
|
13
|
+
available CrispASR binary (CUDA, then Vulkan, then CPU), and registers the
|
|
14
|
+
plugin in the Codex Personal marketplace.
|
|
15
|
+
|
|
16
|
+
Models are never downloaded automatically. The installer creates the plugin's
|
|
17
|
+
`models/` directory and prints the exact model filenames and official source
|
|
18
|
+
links before stopping.
|
|
19
|
+
|
|
20
|
+
## Commands
|
|
21
|
+
|
|
22
|
+
```powershell
|
|
23
|
+
npx @emiyakatuz/crispasr-agent-transcriber@latest install
|
|
24
|
+
npx @emiyakatuz/crispasr-agent-transcriber@latest update
|
|
25
|
+
npx @emiyakatuz/crispasr-agent-transcriber@latest doctor
|
|
26
|
+
npx @emiyakatuz/crispasr-agent-transcriber@latest --json doctor
|
|
27
|
+
npx @emiyakatuz/crispasr-agent-transcriber@latest uninstall
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
`uninstall` preserves local models, CrispASR binaries, and outputs. Add
|
|
31
|
+
`--purge-data` only when those files should also be removed.
|
|
32
|
+
|
|
33
|
+
## Prerequisites
|
|
34
|
+
|
|
35
|
+
- Node.js 20 or newer
|
|
36
|
+
- [uv](https://docs.astral.sh/uv/)
|
|
37
|
+
- [ffmpeg](https://ffmpeg.org/)
|
|
38
|
+
|
|
39
|
+
Media remains local. This installer downloads code and CrispASR binaries from
|
|
40
|
+
the project's documented GitHub sources, but never uploads media and never
|
|
41
|
+
downloads ASR models.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { main } from "../src/cli.js";
|
|
4
|
+
|
|
5
|
+
main(process.argv.slice(2)).then(
|
|
6
|
+
(code) => {
|
|
7
|
+
process.exitCode = code;
|
|
8
|
+
},
|
|
9
|
+
(error) => {
|
|
10
|
+
process.stderr.write(`crispasr-agent-transcriber: ${error.message}\n`);
|
|
11
|
+
process.exitCode = 1;
|
|
12
|
+
},
|
|
13
|
+
);
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@emiyakatuz/crispasr-agent-transcriber",
|
|
3
|
+
"version": "0.3.2",
|
|
4
|
+
"description": "Installer and updater for the local-only CrispASR Codex plugin and MCP server.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"crispasr-agent-transcriber": "./bin/crispasr-agent-transcriber.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"bin",
|
|
11
|
+
"src",
|
|
12
|
+
"README.md"
|
|
13
|
+
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"test": "node --test",
|
|
16
|
+
"pack:check": "npm pack --dry-run"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/EmiyaKatuz/crispasr-agent-transcriber.git"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://github.com/EmiyaKatuz/crispasr-agent-transcriber#readme",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/EmiyaKatuz/crispasr-agent-transcriber/issues"
|
|
25
|
+
},
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"engines": {
|
|
28
|
+
"node": ">=20"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public",
|
|
32
|
+
"provenance": true
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"adm-zip": "^0.5.16"
|
|
36
|
+
}
|
|
37
|
+
}
|
package/src/cli.js
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { PACKAGE_NAME, PACKAGE_VERSION } from "./constants.js";
|
|
2
|
+
import { InstallerError } from "./errors.js";
|
|
3
|
+
import { doctor, installPlugin, uninstallPlugin } from "./installer.js";
|
|
4
|
+
|
|
5
|
+
const HELP = `CrispASR Agent Transcriber installer
|
|
6
|
+
|
|
7
|
+
Usage:
|
|
8
|
+
crispasr-agent-transcriber [install] [options]
|
|
9
|
+
crispasr-agent-transcriber update [options]
|
|
10
|
+
crispasr-agent-transcriber doctor [options]
|
|
11
|
+
crispasr-agent-transcriber uninstall [options]
|
|
12
|
+
|
|
13
|
+
Commands:
|
|
14
|
+
install Install the Codex plugin, MCP dependencies, and CrispASR (default)
|
|
15
|
+
update Update plugin files and the GPU-preferred CrispASR binary
|
|
16
|
+
doctor Check prerequisites, plugin files, CrispASR, and local models
|
|
17
|
+
uninstall Remove managed plugin files and Codex registration
|
|
18
|
+
|
|
19
|
+
Options:
|
|
20
|
+
--target-dir PATH Plugin directory; use with --no-marketplace if custom
|
|
21
|
+
--marketplace-path PATH Personal Codex marketplace.json path
|
|
22
|
+
--release VERSION Install a matching GitHub Release version
|
|
23
|
+
--json Print stable JSON to stdout
|
|
24
|
+
--dry-run Show install/update actions without changing files
|
|
25
|
+
--skip-crispasr Do not install or update the CrispASR binary
|
|
26
|
+
--skip-deps Do not run uv sync
|
|
27
|
+
--no-marketplace Do not modify the Personal Codex marketplace
|
|
28
|
+
--purge-data With uninstall, also remove models, binaries, and outputs
|
|
29
|
+
-v, --version Print installer version
|
|
30
|
+
-h, --help Show this help
|
|
31
|
+
|
|
32
|
+
Models are never downloaded automatically. When they are missing, installation
|
|
33
|
+
stops after printing the exact filenames and source links.
|
|
34
|
+
`;
|
|
35
|
+
|
|
36
|
+
function parseArgs(argv) {
|
|
37
|
+
const options = {};
|
|
38
|
+
let command = null;
|
|
39
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
40
|
+
const value = argv[index];
|
|
41
|
+
if (["install", "update", "doctor", "uninstall"].includes(value) && !command) {
|
|
42
|
+
command = value;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (value === "--json") options.json = true;
|
|
46
|
+
else if (value === "--dry-run") options.dryRun = true;
|
|
47
|
+
else if (value === "--skip-crispasr") options.skipCrispASR = true;
|
|
48
|
+
else if (value === "--skip-deps") options.skipDependencies = true;
|
|
49
|
+
else if (value === "--no-marketplace") options.skipMarketplace = true;
|
|
50
|
+
else if (value === "--purge-data") options.purgeData = true;
|
|
51
|
+
else if (value === "--help" || value === "-h") options.help = true;
|
|
52
|
+
else if (value === "--version" || value === "-v") options.version = true;
|
|
53
|
+
else if (["--target-dir", "--marketplace-path", "--release"].includes(value)) {
|
|
54
|
+
const next = argv[index + 1];
|
|
55
|
+
if (!next || next.startsWith("--")) {
|
|
56
|
+
throw new InstallerError(`${value} requires a value.`, "invalid_arguments");
|
|
57
|
+
}
|
|
58
|
+
index += 1;
|
|
59
|
+
if (value === "--target-dir") options.targetDir = next;
|
|
60
|
+
if (value === "--marketplace-path") options.marketplacePath = next;
|
|
61
|
+
if (value === "--release") options.releaseVersion = next.replace(/^v/, "");
|
|
62
|
+
} else {
|
|
63
|
+
throw new InstallerError(`Unknown argument: ${value}`, "invalid_arguments");
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return { command: command || "install", options };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function printHumanResult(result) {
|
|
70
|
+
if (result.dryRun) {
|
|
71
|
+
process.stdout.write(`Dry run for ${result.command}:\n`);
|
|
72
|
+
for (const step of result.plan) process.stdout.write(`- ${step}\n`);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (result.command === "doctor") {
|
|
76
|
+
process.stdout.write(`Ready: ${result.ready ? "yes" : "no"}\n`);
|
|
77
|
+
for (const [name, check] of Object.entries(result.checks)) {
|
|
78
|
+
process.stdout.write(`${check.ok ? "[ok]" : "[missing]"} ${name}\n`);
|
|
79
|
+
}
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (result.command === "uninstall") {
|
|
83
|
+
process.stdout.write(
|
|
84
|
+
result.removed ? "Plugin registration and managed files removed.\n" : "Plugin was not installed.\n",
|
|
85
|
+
);
|
|
86
|
+
if (result.preserved) {
|
|
87
|
+
process.stdout.write("Preserved local data:\n");
|
|
88
|
+
for (const item of result.preserved) process.stdout.write(`- ${item}\n`);
|
|
89
|
+
}
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
process.stdout.write(`Plugin ${result.command} complete: ${result.targetDir}\n`);
|
|
94
|
+
if (result.ready) {
|
|
95
|
+
process.stdout.write("CrispASR and all required models are ready. Restart Codex.\n");
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
process.stdout.write("\nSetup paused: download these model files manually:\n");
|
|
99
|
+
for (const model of result.missingModels) {
|
|
100
|
+
process.stdout.write(`- ${model.filename}\n ${model.url}\n Save to: ${model.path}\n`);
|
|
101
|
+
}
|
|
102
|
+
process.stdout.write(
|
|
103
|
+
`\nAfter downloading, run: npx ${PACKAGE_NAME}@latest doctor\n`,
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function printError(error, jsonMode) {
|
|
108
|
+
const installerError =
|
|
109
|
+
error instanceof InstallerError
|
|
110
|
+
? error
|
|
111
|
+
: new InstallerError(error.message || String(error));
|
|
112
|
+
if (jsonMode) {
|
|
113
|
+
process.stdout.write(
|
|
114
|
+
`${JSON.stringify({ ok: false, error: installerError.toJSON() })}\n`,
|
|
115
|
+
);
|
|
116
|
+
} else {
|
|
117
|
+
process.stderr.write(`Error: ${installerError.message}\n`);
|
|
118
|
+
if (installerError.code === "missing_prerequisite") {
|
|
119
|
+
process.stderr.write(
|
|
120
|
+
"Install uv from https://docs.astral.sh/uv/ and ffmpeg from https://ffmpeg.org/, then retry.\n",
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export async function main(argv = []) {
|
|
127
|
+
let parsed;
|
|
128
|
+
try {
|
|
129
|
+
parsed = parseArgs(argv);
|
|
130
|
+
} catch (error) {
|
|
131
|
+
printError(error, argv.includes("--json"));
|
|
132
|
+
return 2;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (parsed.options.help) {
|
|
136
|
+
process.stdout.write(HELP);
|
|
137
|
+
return 0;
|
|
138
|
+
}
|
|
139
|
+
if (parsed.options.version) {
|
|
140
|
+
process.stdout.write(`${PACKAGE_VERSION}\n`);
|
|
141
|
+
return 0;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const progress = parsed.options.json
|
|
145
|
+
? (message) => process.stderr.write(`${message}\n`)
|
|
146
|
+
: (message) => process.stdout.write(`${message}\n`);
|
|
147
|
+
|
|
148
|
+
try {
|
|
149
|
+
let result;
|
|
150
|
+
if (parsed.command === "doctor") {
|
|
151
|
+
result = doctor(parsed.options);
|
|
152
|
+
} else if (parsed.command === "uninstall") {
|
|
153
|
+
result = uninstallPlugin(parsed.options);
|
|
154
|
+
} else {
|
|
155
|
+
result = await installPlugin({
|
|
156
|
+
...parsed.options,
|
|
157
|
+
update: parsed.command === "update",
|
|
158
|
+
captureCommands: parsed.options.json,
|
|
159
|
+
onProgress: progress,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
if (parsed.options.json) {
|
|
163
|
+
process.stdout.write(`${JSON.stringify({ ok: true, ...result })}\n`);
|
|
164
|
+
} else {
|
|
165
|
+
printHumanResult(result);
|
|
166
|
+
}
|
|
167
|
+
return 0;
|
|
168
|
+
} catch (error) {
|
|
169
|
+
printError(error, parsed.options.json);
|
|
170
|
+
return 1;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export { HELP, parseArgs };
|
package/src/constants.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
|
|
6
|
+
export const PLUGIN_NAME = "crispasr-agent-transcriber";
|
|
7
|
+
export const GITHUB_REPOSITORY = "EmiyaKatuz/crispasr-agent-transcriber";
|
|
8
|
+
|
|
9
|
+
const packageDir = path.dirname(fileURLToPath(import.meta.url));
|
|
10
|
+
const packageJson = JSON.parse(
|
|
11
|
+
fs.readFileSync(path.join(packageDir, "..", "package.json"), "utf8"),
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
export const PACKAGE_NAME = packageJson.name;
|
|
15
|
+
export const PACKAGE_VERSION = packageJson.version;
|
|
16
|
+
|
|
17
|
+
export const MODEL_SPECS = [
|
|
18
|
+
{
|
|
19
|
+
purpose: "English transcription",
|
|
20
|
+
filename: "cohere-transcribe.gguf",
|
|
21
|
+
url: "https://huggingface.co/cstr",
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
purpose: "Chinese transcription",
|
|
25
|
+
filename: "qwen3-asr-1.7b-q4_k.gguf",
|
|
26
|
+
url: "https://huggingface.co/cstr/qwen3-asr-1.7b-GGUF",
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
purpose: "English/Chinese language detection",
|
|
30
|
+
filename: "firered-lid-q2_k.gguf",
|
|
31
|
+
url: "https://huggingface.co/cstr/firered-lid-GGUF",
|
|
32
|
+
},
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
export const MANAGED_ENTRIES = [
|
|
36
|
+
".codex-plugin",
|
|
37
|
+
".mcp.json",
|
|
38
|
+
".venv",
|
|
39
|
+
"AGENTS.md",
|
|
40
|
+
"LICENSE",
|
|
41
|
+
"README.md",
|
|
42
|
+
"assets",
|
|
43
|
+
"docs",
|
|
44
|
+
"mcp_server",
|
|
45
|
+
"pyproject.toml",
|
|
46
|
+
"scripts",
|
|
47
|
+
"skills",
|
|
48
|
+
"src",
|
|
49
|
+
"uv.lock",
|
|
50
|
+
];
|
|
51
|
+
|
|
52
|
+
export function defaultTargetDir(homeDir = os.homedir()) {
|
|
53
|
+
return path.join(homeDir, "plugins", PLUGIN_NAME);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function defaultMarketplacePath(homeDir = os.homedir()) {
|
|
57
|
+
return path.join(homeDir, ".agents", "plugins", "marketplace.json");
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function defaultReleaseBase() {
|
|
61
|
+
return (
|
|
62
|
+
process.env.CRISPASR_INSTALLER_RELEASE_BASE ||
|
|
63
|
+
`https://github.com/${GITHUB_REPOSITORY}/releases/download`
|
|
64
|
+
);
|
|
65
|
+
}
|
package/src/errors.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export class InstallerError extends Error {
|
|
2
|
+
constructor(message, code = "installer_error", details = {}) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = "InstallerError";
|
|
5
|
+
this.code = code;
|
|
6
|
+
this.details = details;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
toJSON() {
|
|
10
|
+
return {
|
|
11
|
+
code: this.code,
|
|
12
|
+
message: this.message,
|
|
13
|
+
details: this.details,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
}
|
package/src/installer.js
ADDED
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
MANAGED_ENTRIES,
|
|
7
|
+
MODEL_SPECS,
|
|
8
|
+
PACKAGE_VERSION,
|
|
9
|
+
PLUGIN_NAME,
|
|
10
|
+
defaultMarketplacePath,
|
|
11
|
+
defaultReleaseBase,
|
|
12
|
+
defaultTargetDir,
|
|
13
|
+
} from "./constants.js";
|
|
14
|
+
import { InstallerError } from "./errors.js";
|
|
15
|
+
import {
|
|
16
|
+
hasMarketplaceEntry,
|
|
17
|
+
installMarketplaceEntry,
|
|
18
|
+
removeMarketplaceEntry,
|
|
19
|
+
} from "./marketplace.js";
|
|
20
|
+
import { extractZipSafely, fetchReleaseBundle } from "./release.js";
|
|
21
|
+
import { crispasrExecutableName, findCommand, runCommand } from "./system.js";
|
|
22
|
+
|
|
23
|
+
const MARKER_FILE = ".crispasr-installer.json";
|
|
24
|
+
const RELEASE_VERSION_RE = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/;
|
|
25
|
+
|
|
26
|
+
function resolvedPath(value) {
|
|
27
|
+
return path.resolve(value);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function assertSafeTarget(targetDir, homeDir = os.homedir()) {
|
|
31
|
+
const requested = resolvedPath(targetDir);
|
|
32
|
+
const target = fs.existsSync(requested) ? fs.realpathSync(requested) : requested;
|
|
33
|
+
const root = path.parse(target).root;
|
|
34
|
+
const home = resolvedPath(homeDir);
|
|
35
|
+
if (target === root || target === home) {
|
|
36
|
+
throw new InstallerError(
|
|
37
|
+
"Refusing to use a filesystem root or home directory as the plugin target.",
|
|
38
|
+
"unsafe_target",
|
|
39
|
+
{ target },
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
return target;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function readPluginManifest(targetDir) {
|
|
46
|
+
const manifestPath = path.join(targetDir, ".codex-plugin", "plugin.json");
|
|
47
|
+
if (!fs.existsSync(manifestPath)) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
return JSON.parse(fs.readFileSync(manifestPath, "utf8"));
|
|
52
|
+
} catch {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function readMarker(targetDir) {
|
|
58
|
+
const markerPath = path.join(targetDir, MARKER_FILE);
|
|
59
|
+
if (!fs.existsSync(markerPath)) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
try {
|
|
63
|
+
return JSON.parse(fs.readFileSync(markerPath, "utf8"));
|
|
64
|
+
} catch {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function targetIsRecognized(targetDir) {
|
|
70
|
+
const marker = readMarker(targetDir);
|
|
71
|
+
const manifest = readPluginManifest(targetDir);
|
|
72
|
+
return Boolean(
|
|
73
|
+
(marker && marker.plugin === PLUGIN_NAME) ||
|
|
74
|
+
(manifest && manifest.name === PLUGIN_NAME),
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function ensureInstallableTarget(targetDir) {
|
|
79
|
+
if (!fs.existsSync(targetDir)) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const entries = fs.readdirSync(targetDir);
|
|
83
|
+
if (entries.length === 0 || targetIsRecognized(targetDir)) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const dataOnly = entries.every((entry) =>
|
|
87
|
+
["bin", "models", "outputs", MARKER_FILE].includes(entry),
|
|
88
|
+
);
|
|
89
|
+
if (!dataOnly) {
|
|
90
|
+
throw new InstallerError(
|
|
91
|
+
"The target directory is not empty and is not a recognized CrispASR plugin installation.",
|
|
92
|
+
"target_not_managed",
|
|
93
|
+
{ target: targetDir },
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function removeManagedEntries(targetDir) {
|
|
99
|
+
for (const entry of MANAGED_ENTRIES) {
|
|
100
|
+
fs.rmSync(path.join(targetDir, entry), { recursive: true, force: true });
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function copyPluginFiles(sourceDir, targetDir) {
|
|
105
|
+
fs.mkdirSync(targetDir, { recursive: true });
|
|
106
|
+
for (const entry of fs.readdirSync(sourceDir)) {
|
|
107
|
+
fs.cpSync(path.join(sourceDir, entry), path.join(targetDir, entry), {
|
|
108
|
+
recursive: true,
|
|
109
|
+
force: true,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function writeMarker(targetDir, version, installed = true) {
|
|
115
|
+
const marker = {
|
|
116
|
+
plugin: PLUGIN_NAME,
|
|
117
|
+
version,
|
|
118
|
+
installed,
|
|
119
|
+
updatedAt: new Date().toISOString(),
|
|
120
|
+
};
|
|
121
|
+
fs.writeFileSync(
|
|
122
|
+
path.join(targetDir, MARKER_FILE),
|
|
123
|
+
`${JSON.stringify(marker, null, 2)}\n`,
|
|
124
|
+
"utf8",
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function locatePluginSource(extractDir, version) {
|
|
129
|
+
const sourceDir = path.join(extractDir, PLUGIN_NAME);
|
|
130
|
+
const manifest = readPluginManifest(sourceDir);
|
|
131
|
+
if (!manifest || manifest.name !== PLUGIN_NAME) {
|
|
132
|
+
throw new InstallerError(
|
|
133
|
+
"The release archive does not contain the expected Codex plugin.",
|
|
134
|
+
"invalid_release_bundle",
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
if (manifest.version !== version) {
|
|
138
|
+
throw new InstallerError(
|
|
139
|
+
"The release archive version does not match the npm installer version.",
|
|
140
|
+
"release_version_mismatch",
|
|
141
|
+
{ expected: version, actual: manifest.version },
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
return sourceDir;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function resolvePrerequisites(options) {
|
|
148
|
+
const commandFinder = options.commandFinder || findCommand;
|
|
149
|
+
const uv = options.uvPath || commandFinder("uv");
|
|
150
|
+
const ffmpeg = options.ffmpegPath || commandFinder("ffmpeg");
|
|
151
|
+
const missing = [];
|
|
152
|
+
if (!uv) missing.push("uv");
|
|
153
|
+
if (!ffmpeg) missing.push("ffmpeg");
|
|
154
|
+
if (missing.length > 0) {
|
|
155
|
+
throw new InstallerError(
|
|
156
|
+
`Missing required command${missing.length > 1 ? "s" : ""}: ${missing.join(", ")}.`,
|
|
157
|
+
"missing_prerequisite",
|
|
158
|
+
{ missing },
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
return { uv, ffmpeg };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export function inspectModels(targetDir) {
|
|
165
|
+
const modelsDir = path.join(targetDir, "models");
|
|
166
|
+
return MODEL_SPECS.map((model) => ({
|
|
167
|
+
...model,
|
|
168
|
+
path: path.join(modelsDir, model.filename),
|
|
169
|
+
installed: fs.existsSync(path.join(modelsDir, model.filename)),
|
|
170
|
+
}));
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function installPlan({ targetDir, version, marketplacePath, update }) {
|
|
174
|
+
return [
|
|
175
|
+
`Download and verify GitHub Release v${version}`,
|
|
176
|
+
`${update ? "Update" : "Install"} plugin files in ${targetDir}`,
|
|
177
|
+
"Install Python and MCP dependencies with uv",
|
|
178
|
+
`${update ? "Update" : "Install"} the GPU-preferred CrispASR binary`,
|
|
179
|
+
`Register the Codex Personal marketplace entry in ${marketplacePath}`,
|
|
180
|
+
"Check for the three manually supplied local model files",
|
|
181
|
+
];
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export async function installPlugin(options = {}) {
|
|
185
|
+
const homeDir = options.homeDir || os.homedir();
|
|
186
|
+
const defaultTarget = assertSafeTarget(defaultTargetDir(homeDir), homeDir);
|
|
187
|
+
const targetDir = assertSafeTarget(
|
|
188
|
+
options.targetDir || defaultTarget,
|
|
189
|
+
homeDir,
|
|
190
|
+
);
|
|
191
|
+
const marketplacePath =
|
|
192
|
+
options.marketplacePath || defaultMarketplacePath(homeDir);
|
|
193
|
+
const version = options.releaseVersion || PACKAGE_VERSION;
|
|
194
|
+
const releaseBase = options.releaseBase || defaultReleaseBase();
|
|
195
|
+
const update = Boolean(options.update);
|
|
196
|
+
const progress = options.onProgress || (() => {});
|
|
197
|
+
|
|
198
|
+
if (!RELEASE_VERSION_RE.test(version)) {
|
|
199
|
+
throw new InstallerError(
|
|
200
|
+
"Release versions must use semantic version syntax such as 0.3.2.",
|
|
201
|
+
"invalid_release_version",
|
|
202
|
+
{ version },
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (!options.skipMarketplace && targetDir !== defaultTarget) {
|
|
207
|
+
throw new InstallerError(
|
|
208
|
+
"A custom target directory requires --no-marketplace because the Personal marketplace path is fixed.",
|
|
209
|
+
"custom_target_marketplace",
|
|
210
|
+
{ target: targetDir, expected: defaultTarget },
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const plan = installPlan({ targetDir, version, marketplacePath, update });
|
|
215
|
+
if (options.dryRun) {
|
|
216
|
+
return { command: update ? "update" : "install", dryRun: true, targetDir, plan };
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const commands = resolvePrerequisites(options);
|
|
220
|
+
ensureInstallableTarget(targetDir);
|
|
221
|
+
|
|
222
|
+
const temporaryDir = fs.mkdtempSync(path.join(os.tmpdir(), "crispasr-installer-"));
|
|
223
|
+
try {
|
|
224
|
+
progress(`Downloading and verifying plugin release v${version}...`);
|
|
225
|
+
const bundle = await fetchReleaseBundle({
|
|
226
|
+
version,
|
|
227
|
+
releaseBase,
|
|
228
|
+
destinationDir: temporaryDir,
|
|
229
|
+
fetchImpl: options.fetchImpl,
|
|
230
|
+
});
|
|
231
|
+
const extractDir = path.join(temporaryDir, "extracted");
|
|
232
|
+
extractZipSafely(bundle.archivePath, extractDir);
|
|
233
|
+
const sourceDir = locatePluginSource(extractDir, version);
|
|
234
|
+
|
|
235
|
+
progress(`${update ? "Updating" : "Installing"} plugin files...`);
|
|
236
|
+
if (readMarker(targetDir)) {
|
|
237
|
+
removeManagedEntries(targetDir);
|
|
238
|
+
}
|
|
239
|
+
copyPluginFiles(sourceDir, targetDir);
|
|
240
|
+
fs.mkdirSync(path.join(targetDir, "models"), { recursive: true });
|
|
241
|
+
fs.mkdirSync(path.join(targetDir, "outputs"), { recursive: true });
|
|
242
|
+
writeMarker(targetDir, version, true);
|
|
243
|
+
|
|
244
|
+
const runner = options.commandRunner || runCommand;
|
|
245
|
+
if (!options.skipDependencies) {
|
|
246
|
+
progress("Installing Python and MCP dependencies...");
|
|
247
|
+
runner(commands.uv, ["sync", "--extra", "mcp"], {
|
|
248
|
+
cwd: targetDir,
|
|
249
|
+
capture: Boolean(options.captureCommands),
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const crispasrPath = path.join(
|
|
254
|
+
targetDir,
|
|
255
|
+
"bin",
|
|
256
|
+
crispasrExecutableName(options.platform),
|
|
257
|
+
);
|
|
258
|
+
if (!options.skipCrispASR && (update || !fs.existsSync(crispasrPath))) {
|
|
259
|
+
progress(`${update ? "Updating" : "Installing"} CrispASR...`);
|
|
260
|
+
runner(
|
|
261
|
+
commands.uv,
|
|
262
|
+
[
|
|
263
|
+
"run",
|
|
264
|
+
"python",
|
|
265
|
+
"scripts/transcribe.py",
|
|
266
|
+
update ? "--update-crispasr" : "--install-crispasr",
|
|
267
|
+
"--crispasr-bin-dir",
|
|
268
|
+
path.join(targetDir, "bin"),
|
|
269
|
+
],
|
|
270
|
+
{ cwd: targetDir, capture: Boolean(options.captureCommands) },
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (!options.skipMarketplace) {
|
|
275
|
+
progress("Registering the Codex Personal marketplace entry...");
|
|
276
|
+
installMarketplaceEntry(marketplacePath);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const models = inspectModels(targetDir);
|
|
280
|
+
const missingModels = models.filter((model) => !model.installed);
|
|
281
|
+
return {
|
|
282
|
+
command: update ? "update" : "install",
|
|
283
|
+
targetDir,
|
|
284
|
+
marketplacePath,
|
|
285
|
+
version,
|
|
286
|
+
archiveSha256: bundle.sha256,
|
|
287
|
+
ready: missingModels.length === 0,
|
|
288
|
+
models,
|
|
289
|
+
missingModels,
|
|
290
|
+
};
|
|
291
|
+
} finally {
|
|
292
|
+
fs.rmSync(temporaryDir, { recursive: true, force: true });
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export function doctor(options = {}) {
|
|
297
|
+
const homeDir = options.homeDir || os.homedir();
|
|
298
|
+
const targetDir = assertSafeTarget(
|
|
299
|
+
options.targetDir || defaultTargetDir(homeDir),
|
|
300
|
+
homeDir,
|
|
301
|
+
);
|
|
302
|
+
const marketplacePath =
|
|
303
|
+
options.marketplacePath || defaultMarketplacePath(homeDir);
|
|
304
|
+
const commandFinder = options.commandFinder || findCommand;
|
|
305
|
+
const manifest = readPluginManifest(targetDir);
|
|
306
|
+
const crispasrPath = path.join(
|
|
307
|
+
targetDir,
|
|
308
|
+
"bin",
|
|
309
|
+
crispasrExecutableName(options.platform),
|
|
310
|
+
);
|
|
311
|
+
const models = inspectModels(targetDir);
|
|
312
|
+
const checks = {
|
|
313
|
+
plugin: {
|
|
314
|
+
ok: manifest?.name === PLUGIN_NAME,
|
|
315
|
+
version: manifest?.version || null,
|
|
316
|
+
path: targetDir,
|
|
317
|
+
},
|
|
318
|
+
uv: { ok: Boolean(commandFinder("uv")), path: commandFinder("uv") },
|
|
319
|
+
ffmpeg: {
|
|
320
|
+
ok: Boolean(commandFinder("ffmpeg")),
|
|
321
|
+
path: commandFinder("ffmpeg"),
|
|
322
|
+
},
|
|
323
|
+
crispasr: { ok: fs.existsSync(crispasrPath), path: crispasrPath },
|
|
324
|
+
marketplace: {
|
|
325
|
+
ok: hasMarketplaceEntry(marketplacePath),
|
|
326
|
+
path: marketplacePath,
|
|
327
|
+
},
|
|
328
|
+
models: {
|
|
329
|
+
ok: models.every((model) => model.installed),
|
|
330
|
+
items: models,
|
|
331
|
+
},
|
|
332
|
+
};
|
|
333
|
+
return {
|
|
334
|
+
command: "doctor",
|
|
335
|
+
ready: Object.values(checks).every((check) => check.ok),
|
|
336
|
+
checks,
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
export function uninstallPlugin(options = {}) {
|
|
341
|
+
const homeDir = options.homeDir || os.homedir();
|
|
342
|
+
const defaultTarget = assertSafeTarget(defaultTargetDir(homeDir), homeDir);
|
|
343
|
+
const targetDir = assertSafeTarget(
|
|
344
|
+
options.targetDir || defaultTarget,
|
|
345
|
+
homeDir,
|
|
346
|
+
);
|
|
347
|
+
const marketplacePath =
|
|
348
|
+
options.marketplacePath || defaultMarketplacePath(homeDir);
|
|
349
|
+
|
|
350
|
+
if (!fs.existsSync(targetDir)) {
|
|
351
|
+
if (!options.skipMarketplace && targetDir === defaultTarget) {
|
|
352
|
+
removeMarketplaceEntry(marketplacePath);
|
|
353
|
+
}
|
|
354
|
+
return { command: "uninstall", removed: false, targetDir };
|
|
355
|
+
}
|
|
356
|
+
if (!targetIsRecognized(targetDir)) {
|
|
357
|
+
throw new InstallerError(
|
|
358
|
+
"Refusing to uninstall an unrecognized directory.",
|
|
359
|
+
"target_not_managed",
|
|
360
|
+
{ target: targetDir },
|
|
361
|
+
);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
if (!options.skipMarketplace && targetDir === defaultTarget) {
|
|
365
|
+
removeMarketplaceEntry(marketplacePath);
|
|
366
|
+
}
|
|
367
|
+
if (options.purgeData) {
|
|
368
|
+
if (!readMarker(targetDir)) {
|
|
369
|
+
throw new InstallerError(
|
|
370
|
+
"Refusing to purge data without an npm installer ownership marker.",
|
|
371
|
+
"purge_requires_marker",
|
|
372
|
+
{ target: targetDir },
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
fs.rmSync(targetDir, { recursive: true, force: true });
|
|
376
|
+
return {
|
|
377
|
+
command: "uninstall",
|
|
378
|
+
removed: true,
|
|
379
|
+
purgedData: true,
|
|
380
|
+
targetDir,
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
removeManagedEntries(targetDir);
|
|
385
|
+
writeMarker(targetDir, readMarker(targetDir)?.version || PACKAGE_VERSION, false);
|
|
386
|
+
return {
|
|
387
|
+
command: "uninstall",
|
|
388
|
+
removed: true,
|
|
389
|
+
purgedData: false,
|
|
390
|
+
preserved: [
|
|
391
|
+
path.join(targetDir, "models"),
|
|
392
|
+
path.join(targetDir, "bin"),
|
|
393
|
+
path.join(targetDir, "outputs"),
|
|
394
|
+
],
|
|
395
|
+
targetDir,
|
|
396
|
+
};
|
|
397
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
import { PLUGIN_NAME } from "./constants.js";
|
|
5
|
+
import { InstallerError } from "./errors.js";
|
|
6
|
+
|
|
7
|
+
export const MARKETPLACE_ENTRY = {
|
|
8
|
+
name: PLUGIN_NAME,
|
|
9
|
+
source: {
|
|
10
|
+
source: "local",
|
|
11
|
+
path: `./plugins/${PLUGIN_NAME}`,
|
|
12
|
+
},
|
|
13
|
+
policy: {
|
|
14
|
+
installation: "AVAILABLE",
|
|
15
|
+
authentication: "ON_INSTALL",
|
|
16
|
+
},
|
|
17
|
+
category: "Productivity",
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
function readMarketplace(filePath) {
|
|
21
|
+
if (!fs.existsSync(filePath)) {
|
|
22
|
+
return {
|
|
23
|
+
name: "personal",
|
|
24
|
+
interface: { displayName: "Personal" },
|
|
25
|
+
plugins: [],
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
const value = JSON.parse(fs.readFileSync(filePath, "utf8"));
|
|
30
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
31
|
+
throw new Error("root must be a JSON object");
|
|
32
|
+
}
|
|
33
|
+
if (value.plugins !== undefined && !Array.isArray(value.plugins)) {
|
|
34
|
+
throw new Error("plugins must be an array");
|
|
35
|
+
}
|
|
36
|
+
value.plugins ||= [];
|
|
37
|
+
return value;
|
|
38
|
+
} catch (error) {
|
|
39
|
+
throw new InstallerError(
|
|
40
|
+
`Cannot read the existing Codex marketplace file: ${error.message}`,
|
|
41
|
+
"invalid_marketplace",
|
|
42
|
+
{ path: filePath },
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function writeMarketplace(filePath, value) {
|
|
48
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
49
|
+
const temporary = `${filePath}.tmp-${process.pid}`;
|
|
50
|
+
const backup = `${filePath}.bak`;
|
|
51
|
+
fs.writeFileSync(temporary, `${JSON.stringify(value, null, 2)}\n`, "utf8");
|
|
52
|
+
if (fs.existsSync(filePath)) {
|
|
53
|
+
fs.copyFileSync(filePath, backup);
|
|
54
|
+
fs.rmSync(filePath);
|
|
55
|
+
}
|
|
56
|
+
fs.renameSync(temporary, filePath);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function installMarketplaceEntry(filePath) {
|
|
60
|
+
const marketplace = readMarketplace(filePath);
|
|
61
|
+
const existingIndex = marketplace.plugins.findIndex(
|
|
62
|
+
(entry) => entry && entry.name === PLUGIN_NAME,
|
|
63
|
+
);
|
|
64
|
+
if (existingIndex >= 0) {
|
|
65
|
+
marketplace.plugins[existingIndex] = MARKETPLACE_ENTRY;
|
|
66
|
+
} else {
|
|
67
|
+
marketplace.plugins.push(MARKETPLACE_ENTRY);
|
|
68
|
+
}
|
|
69
|
+
writeMarketplace(filePath, marketplace);
|
|
70
|
+
return filePath;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function removeMarketplaceEntry(filePath) {
|
|
74
|
+
if (!fs.existsSync(filePath)) {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
const marketplace = readMarketplace(filePath);
|
|
78
|
+
const originalLength = marketplace.plugins.length;
|
|
79
|
+
marketplace.plugins = marketplace.plugins.filter(
|
|
80
|
+
(entry) => !entry || entry.name !== PLUGIN_NAME,
|
|
81
|
+
);
|
|
82
|
+
if (marketplace.plugins.length === originalLength) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
writeMarketplace(filePath, marketplace);
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function hasMarketplaceEntry(filePath) {
|
|
90
|
+
if (!fs.existsSync(filePath)) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
try {
|
|
94
|
+
return readMarketplace(filePath).plugins.some(
|
|
95
|
+
(entry) => entry && entry.name === PLUGIN_NAME,
|
|
96
|
+
);
|
|
97
|
+
} catch {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
}
|
package/src/release.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { Readable } from "node:stream";
|
|
5
|
+
import { pipeline } from "node:stream/promises";
|
|
6
|
+
|
|
7
|
+
import AdmZip from "adm-zip";
|
|
8
|
+
|
|
9
|
+
import { InstallerError } from "./errors.js";
|
|
10
|
+
|
|
11
|
+
export function assertSafeReleaseBase(releaseBase) {
|
|
12
|
+
let parsed;
|
|
13
|
+
try {
|
|
14
|
+
parsed = new URL(releaseBase);
|
|
15
|
+
} catch {
|
|
16
|
+
throw new InstallerError("The release base URL is invalid.", "invalid_release_url");
|
|
17
|
+
}
|
|
18
|
+
const localHttp =
|
|
19
|
+
parsed.protocol === "http:" &&
|
|
20
|
+
["127.0.0.1", "localhost", "::1"].includes(parsed.hostname);
|
|
21
|
+
if (parsed.protocol !== "https:" && !localHttp) {
|
|
22
|
+
throw new InstallerError(
|
|
23
|
+
"Release downloads require HTTPS (except localhost test servers).",
|
|
24
|
+
"invalid_release_url",
|
|
25
|
+
{ url: releaseBase },
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
return parsed.toString().replace(/\/$/, "");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export async function downloadFile(url, destination, fetchImpl = globalThis.fetch) {
|
|
32
|
+
const response = await fetchImpl(url, {
|
|
33
|
+
headers: { "User-Agent": "crispasr-agent-transcriber-installer" },
|
|
34
|
+
redirect: "follow",
|
|
35
|
+
});
|
|
36
|
+
if (!response.ok || !response.body) {
|
|
37
|
+
throw new InstallerError(
|
|
38
|
+
`Download failed with HTTP ${response.status}.`,
|
|
39
|
+
"download_failed",
|
|
40
|
+
{ url, status: response.status },
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
await pipeline(Readable.fromWeb(response.body), fs.createWriteStream(destination));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function sha256File(filePath) {
|
|
47
|
+
const hash = crypto.createHash("sha256");
|
|
48
|
+
hash.update(fs.readFileSync(filePath));
|
|
49
|
+
return hash.digest("hex");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function expectedChecksum(checksumText, filename) {
|
|
53
|
+
for (const line of checksumText.split(/\r?\n/)) {
|
|
54
|
+
const match = line.trim().match(/^([0-9a-fA-F]{64})\s+\*?(.+)$/);
|
|
55
|
+
if (match && path.basename(match[2].trim()) === filename) {
|
|
56
|
+
return match[1].toLowerCase();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function assertSafeArchiveEntry(entryName) {
|
|
63
|
+
const normalized = entryName.replaceAll("\\", "/");
|
|
64
|
+
const parts = normalized.split("/").filter(Boolean);
|
|
65
|
+
if (
|
|
66
|
+
normalized.startsWith("/") ||
|
|
67
|
+
/^[a-zA-Z]:/.test(normalized) ||
|
|
68
|
+
parts.includes("..")
|
|
69
|
+
) {
|
|
70
|
+
throw new InstallerError(
|
|
71
|
+
"The plugin archive contains an unsafe path.",
|
|
72
|
+
"unsafe_archive",
|
|
73
|
+
{ entry: entryName },
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function extractZipSafely(archivePath, destination) {
|
|
79
|
+
const archive = new AdmZip(archivePath);
|
|
80
|
+
const entries = archive.getEntries();
|
|
81
|
+
for (const entry of entries) {
|
|
82
|
+
assertSafeArchiveEntry(entry.entryName);
|
|
83
|
+
}
|
|
84
|
+
fs.mkdirSync(destination, { recursive: true });
|
|
85
|
+
for (const entry of entries) {
|
|
86
|
+
archive.extractEntryTo(entry, destination, true, true);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export async function fetchReleaseBundle({
|
|
91
|
+
version,
|
|
92
|
+
releaseBase,
|
|
93
|
+
destinationDir,
|
|
94
|
+
fetchImpl = globalThis.fetch,
|
|
95
|
+
}) {
|
|
96
|
+
const filename = `crispasr-agent-transcriber-plugin-${version}.zip`;
|
|
97
|
+
const archivePath = path.join(destinationDir, filename);
|
|
98
|
+
const checksumPath = path.join(destinationDir, "SHA256SUMS");
|
|
99
|
+
const versionBase = `${assertSafeReleaseBase(releaseBase)}/v${version}`;
|
|
100
|
+
|
|
101
|
+
await downloadFile(`${versionBase}/SHA256SUMS`, checksumPath, fetchImpl);
|
|
102
|
+
await downloadFile(`${versionBase}/${filename}`, archivePath, fetchImpl);
|
|
103
|
+
|
|
104
|
+
const checksumText = fs.readFileSync(checksumPath, "utf8");
|
|
105
|
+
const expected = expectedChecksum(checksumText, filename);
|
|
106
|
+
if (!expected) {
|
|
107
|
+
throw new InstallerError(
|
|
108
|
+
"The release checksum file does not list the plugin archive.",
|
|
109
|
+
"checksum_missing",
|
|
110
|
+
{ filename },
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
const actual = sha256File(archivePath);
|
|
114
|
+
if (actual !== expected) {
|
|
115
|
+
throw new InstallerError(
|
|
116
|
+
"The downloaded plugin archive failed SHA-256 verification.",
|
|
117
|
+
"checksum_mismatch",
|
|
118
|
+
{ filename, expected, actual },
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
return { archivePath, filename, sha256: actual };
|
|
122
|
+
}
|
package/src/system.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { spawnSync } from "node:child_process";
|
|
4
|
+
|
|
5
|
+
import { InstallerError } from "./errors.js";
|
|
6
|
+
|
|
7
|
+
export function findCommand(name, env = process.env, platform = process.platform) {
|
|
8
|
+
const pathValue = env.PATH || env.Path || env.path || "";
|
|
9
|
+
const pathEntries = pathValue.split(path.delimiter).filter(Boolean);
|
|
10
|
+
const extensions =
|
|
11
|
+
platform === "win32"
|
|
12
|
+
? (env.PATHEXT || ".EXE;.CMD;.BAT;.COM").split(";")
|
|
13
|
+
: [""];
|
|
14
|
+
|
|
15
|
+
for (const directory of pathEntries) {
|
|
16
|
+
for (const extension of extensions) {
|
|
17
|
+
const candidate = path.join(directory, `${name}${extension}`);
|
|
18
|
+
try {
|
|
19
|
+
if (fs.statSync(candidate).isFile()) {
|
|
20
|
+
return candidate;
|
|
21
|
+
}
|
|
22
|
+
} catch {
|
|
23
|
+
// Continue searching PATH.
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function runCommand(command, args, options = {}) {
|
|
31
|
+
const result = spawnSync(command, args, {
|
|
32
|
+
cwd: options.cwd,
|
|
33
|
+
env: options.env || process.env,
|
|
34
|
+
encoding: "utf8",
|
|
35
|
+
shell: false,
|
|
36
|
+
timeout: options.timeoutMs || 10 * 60 * 1000,
|
|
37
|
+
stdio: options.capture ? "pipe" : "inherit",
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
if (result.error) {
|
|
41
|
+
throw new InstallerError(
|
|
42
|
+
`Failed to run ${path.basename(command)}: ${result.error.message}`,
|
|
43
|
+
"command_failed",
|
|
44
|
+
{ command: path.basename(command) },
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
if (result.status !== 0) {
|
|
48
|
+
throw new InstallerError(
|
|
49
|
+
`${path.basename(command)} exited with status ${result.status}.`,
|
|
50
|
+
"command_failed",
|
|
51
|
+
{
|
|
52
|
+
command: path.basename(command),
|
|
53
|
+
status: result.status,
|
|
54
|
+
stderr: options.capture ? (result.stderr || "").trim().slice(-2000) : undefined,
|
|
55
|
+
},
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function crispasrExecutableName(platform = process.platform) {
|
|
62
|
+
return platform === "win32" ? "crispasr.exe" : "crispasr";
|
|
63
|
+
}
|