@adea-ai/voice 0.0.0-stage → 0.2.0
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/README.md +51 -2
- package/dist/cli/helpers-cli.d.ts +2 -0
- package/dist/cli/helpers-cli.js +102 -0
- package/dist/contract/contract.d.ts +138 -0
- package/dist/contract/errors.d.ts +26 -0
- package/dist/contract/index.d.ts +9 -0
- package/dist/contract/index.js +54 -0
- package/dist/contract/session.d.ts +58 -0
- package/dist/contract/wire.d.ts +188 -0
- package/dist/desktop/desktop-voice.d.ts +152 -0
- package/dist/desktop/helper-lock.d.ts +83 -0
- package/dist/desktop/helper-process.d.ts +92 -0
- package/dist/desktop/index.d.ts +7 -0
- package/dist/desktop/index.js +1042 -0
- package/dist/index-9kerqn9r.js +150 -0
- package/dist/index-acm1yvvv.js +290 -0
- package/dist/index-r4hz8gk0.js +10 -0
- package/dist/index-rehs788p.js +42 -0
- package/dist/index-s270cw30.js +295 -0
- package/dist/index-tvz9s9nz.js +77 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +86 -0
- package/dist/settings/index.d.ts +5 -0
- package/dist/settings/index.js +34 -0
- package/dist/settings/settings.d.ts +163 -0
- package/dist/web/index.d.ts +15 -0
- package/dist/web/index.js +1287 -0
- package/dist/web/stt/speech-recognition.d.ts +105 -0
- package/dist/web/tts/phonon.d.ts +139 -0
- package/dist/web/tts/player.d.ts +115 -0
- package/dist/web/tts/system-voices.d.ts +122 -0
- package/dist/web/turns.d.ts +101 -0
- package/package.json +68 -4
- package/src/cli/helpers-cli.ts +119 -0
- package/src/contract/contract.ts +365 -0
- package/src/contract/errors.ts +98 -0
- package/src/contract/index.ts +9 -0
- package/src/contract/session.ts +65 -0
- package/src/contract/wire.ts +263 -0
- package/src/desktop/desktop-voice.ts +933 -0
- package/src/desktop/helper-lock.ts +375 -0
- package/src/desktop/helper-process.ts +401 -0
- package/src/desktop/index.ts +16 -0
- package/src/index.ts +7 -0
- package/src/settings/index.ts +5 -0
- package/src/settings/settings.ts +478 -0
- package/src/web/index.ts +15 -0
- package/src/web/stt/speech-recognition.ts +583 -0
- package/src/web/tts/phonon.ts +386 -0
- package/src/web/tts/player.ts +271 -0
- package/src/web/tts/system-voices.ts +401 -0
- package/src/web/turns.ts +224 -0
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pinning the desktop helpers in an app: one lock file, staged and verified at build time.
|
|
3
|
+
*
|
|
4
|
+
* Voice publishes each helper as a versioned release artifact (a `.tar.gz` holding the signed
|
|
5
|
+
* executable and, for the local-model helpers, its `Frameworks/`), with its sha256, at
|
|
6
|
+
* `https://adea.dev/releases/voice/<helper>/<version>/`. An app commits a lock that names the
|
|
7
|
+
* version and the digests; its build stages the helper from the lock and its release check fails
|
|
8
|
+
* if the packaged helper is missing or is not the pinned one. Nothing here falls back to another
|
|
9
|
+
* build or compiles speech code.
|
|
10
|
+
*
|
|
11
|
+
* Lock shape (`voice-helpers.lock.json`):
|
|
12
|
+
*
|
|
13
|
+
* ```json
|
|
14
|
+
* {
|
|
15
|
+
* "schema": 1,
|
|
16
|
+
* "version": "0.2.0",
|
|
17
|
+
* "base": "https://adea.dev/releases/voice/",
|
|
18
|
+
* "helpers": {
|
|
19
|
+
* "adea-voice-helper": {
|
|
20
|
+
* "macos-arm64": {
|
|
21
|
+
* "file": "adea-voice-helper-0.2.0-macos-arm64.tar.gz",
|
|
22
|
+
* "sha256": "<archive sha256>",
|
|
23
|
+
* "executableSha256": "<sha256 of the adea-voice-helper executable>",
|
|
24
|
+
* "bytes": 812345
|
|
25
|
+
* }
|
|
26
|
+
* }
|
|
27
|
+
* }
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
import { createHash } from "node:crypto";
|
|
32
|
+
import { spawnSync } from "node:child_process";
|
|
33
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync, writeFileSync, chmodSync, statSync } from "node:fs";
|
|
34
|
+
import { dirname, join, resolve } from "node:path";
|
|
35
|
+
import { tmpdir } from "node:os";
|
|
36
|
+
import { CONTRACT_MAJOR, WIRE_VERSION, parseWireLine } from "../contract/wire";
|
|
37
|
+
|
|
38
|
+
export const HELPER_LOCK_SCHEMA = 1;
|
|
39
|
+
export const HELPER_NAMES = ["adea-voice-helper", "adea-transcribe-helper", "adea-tts-helper"] as const;
|
|
40
|
+
export type HelperName = (typeof HELPER_NAMES)[number];
|
|
41
|
+
export const HELPER_TARGETS = ["macos-arm64", "macos-x64", "linux-x64", "linux-arm64", "windows-x64"] as const;
|
|
42
|
+
export type HelperTarget = (typeof HELPER_TARGETS)[number];
|
|
43
|
+
|
|
44
|
+
/** A pinned artifact can never be larger than this; a bigger body is refused while streaming. */
|
|
45
|
+
export const MAX_HELPER_ARCHIVE_BYTES = 256 * 1024 * 1024;
|
|
46
|
+
|
|
47
|
+
export interface HelperArtifactPin {
|
|
48
|
+
readonly file: string;
|
|
49
|
+
readonly sha256: string;
|
|
50
|
+
readonly executableSha256: string;
|
|
51
|
+
readonly bytes?: number;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface HelperLock {
|
|
55
|
+
readonly schema: 1;
|
|
56
|
+
readonly version: string;
|
|
57
|
+
readonly base: string;
|
|
58
|
+
readonly helpers: Readonly<Partial<Record<HelperName, Readonly<Partial<Record<HelperTarget, HelperArtifactPin>>>>>>;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** What a staged directory records about itself, written next to the helpers. */
|
|
62
|
+
export interface StagedHelperRecord {
|
|
63
|
+
readonly schema: 1;
|
|
64
|
+
readonly version: string;
|
|
65
|
+
readonly target: HelperTarget;
|
|
66
|
+
readonly helpers: Readonly<Partial<Record<HelperName, { readonly sha256: string; readonly executableSha256: string }>>>;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export const STAGED_RECORD_FILE = "voice-helpers.json";
|
|
70
|
+
|
|
71
|
+
const SHA256 = /^[0-9a-f]{64}$/;
|
|
72
|
+
const SEMVER = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/;
|
|
73
|
+
const FILE_NAME = /^[A-Za-z0-9._-]+\.tar\.gz$/;
|
|
74
|
+
|
|
75
|
+
export class HelperLockError extends Error {
|
|
76
|
+
constructor(message: string) {
|
|
77
|
+
super(message);
|
|
78
|
+
this.name = "HelperLockError";
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Validates a parsed lock. Every problem is a `HelperLockError`: a build must stop on it. */
|
|
83
|
+
export function validateHelperLock(value: unknown): HelperLock {
|
|
84
|
+
const fail = (message: string): never => {
|
|
85
|
+
throw new HelperLockError(`voice helper lock: ${message}`);
|
|
86
|
+
};
|
|
87
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) fail("must be a JSON object");
|
|
88
|
+
const lock = value as Record<string, unknown>;
|
|
89
|
+
if (lock.schema !== HELPER_LOCK_SCHEMA) fail(`schema must be ${HELPER_LOCK_SCHEMA}`);
|
|
90
|
+
if (typeof lock.version !== "string" || !SEMVER.test(lock.version)) fail("version must be a semver");
|
|
91
|
+
if (typeof lock.base !== "string" || !/^https:\/\/[^\s]+\/$/.test(lock.base)) fail("base must be an https URL ending in /");
|
|
92
|
+
if (lock.helpers === null || typeof lock.helpers !== "object") fail("helpers must be an object");
|
|
93
|
+
for (const [name, targets] of Object.entries(lock.helpers as Record<string, unknown>)) {
|
|
94
|
+
if (!(HELPER_NAMES as readonly string[]).includes(name)) fail(`unknown helper ${name}`);
|
|
95
|
+
if (targets === null || typeof targets !== "object") fail(`${name} must map targets to pins`);
|
|
96
|
+
for (const [target, pin] of Object.entries(targets as Record<string, unknown>)) {
|
|
97
|
+
if (!(HELPER_TARGETS as readonly string[]).includes(target)) fail(`${name}: unknown target ${target}`);
|
|
98
|
+
const entry = pin as Record<string, unknown>;
|
|
99
|
+
if (typeof entry?.file !== "string" || !FILE_NAME.test(entry.file)) fail(`${name}/${target}: file must be a .tar.gz name`);
|
|
100
|
+
if (typeof entry.sha256 !== "string" || !SHA256.test(entry.sha256)) fail(`${name}/${target}: sha256 must be 64 hex`);
|
|
101
|
+
if (typeof entry.executableSha256 !== "string" || !SHA256.test(entry.executableSha256)) {
|
|
102
|
+
fail(`${name}/${target}: executableSha256 must be 64 hex`);
|
|
103
|
+
}
|
|
104
|
+
if (entry.bytes !== undefined && (typeof entry.bytes !== "number" || entry.bytes <= 0 || entry.bytes > MAX_HELPER_ARCHIVE_BYTES)) {
|
|
105
|
+
fail(`${name}/${target}: bytes out of range`);
|
|
106
|
+
}
|
|
107
|
+
if (!(entry.file as string).includes(lock.version as string)) fail(`${name}/${target}: file does not carry version ${String(lock.version)}`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return value as HelperLock;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function readHelperLock(path: string): HelperLock {
|
|
114
|
+
let text: string;
|
|
115
|
+
try {
|
|
116
|
+
text = readFileSync(path, "utf8");
|
|
117
|
+
} catch (error) {
|
|
118
|
+
throw new HelperLockError(`voice helper lock: cannot read ${path}: ${String(error)}`);
|
|
119
|
+
}
|
|
120
|
+
let parsed: unknown;
|
|
121
|
+
try {
|
|
122
|
+
parsed = JSON.parse(text);
|
|
123
|
+
} catch (error) {
|
|
124
|
+
throw new HelperLockError(`voice helper lock: ${path} is not JSON: ${String(error)}`);
|
|
125
|
+
}
|
|
126
|
+
return validateHelperLock(parsed);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function helperPin(lock: HelperLock, helper: HelperName, target: HelperTarget): HelperArtifactPin | undefined {
|
|
130
|
+
return lock.helpers[helper]?.[target];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function helperArtifactUrl(lock: HelperLock, helper: HelperName, target: HelperTarget): string {
|
|
134
|
+
const pin = helperPin(lock, helper, target);
|
|
135
|
+
if (!pin) throw new HelperLockError(`voice helper lock pins no ${helper} for ${target}`);
|
|
136
|
+
return `${lock.base}${helper}/${lock.version}/${pin.file}`;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** The lock target for this machine (or `undefined` where no helper is published). */
|
|
140
|
+
export function currentHelperTarget(platform: string = process.platform, arch: string = process.arch): HelperTarget | undefined {
|
|
141
|
+
const os = platform === "darwin" ? "macos" : platform === "win32" ? "windows" : platform === "linux" ? "linux" : undefined;
|
|
142
|
+
const cpu = arch === "arm64" ? "arm64" : arch === "x64" ? "x64" : undefined;
|
|
143
|
+
if (!os || !cpu) return undefined;
|
|
144
|
+
const target = `${os}-${cpu}`;
|
|
145
|
+
return (HELPER_TARGETS as readonly string[]).includes(target) ? (target as HelperTarget) : undefined;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function sha256File(path: string): string {
|
|
149
|
+
return createHash("sha256").update(readFileSync(path)).digest("hex");
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export interface StageHelpersOptions {
|
|
153
|
+
readonly lock: HelperLock;
|
|
154
|
+
readonly target: HelperTarget;
|
|
155
|
+
/** The directory the app bundles (each helper lands at `<outDir>/<helper>`). */
|
|
156
|
+
readonly outDir: string;
|
|
157
|
+
/** Which helpers must be staged; a pin missing for one is an error. */
|
|
158
|
+
readonly helpers: readonly HelperName[];
|
|
159
|
+
/** A cache of verified archives, keyed by sha256 (default: none). */
|
|
160
|
+
readonly cacheDir?: string;
|
|
161
|
+
/**
|
|
162
|
+
* A directory holding locally built helpers (`scripts/package-helper.sh` output) to use instead
|
|
163
|
+
* of downloading, for developing the helper itself. Its binaries are still checked for a
|
|
164
|
+
* compatible handshake, and the staged record says `local` so a release check refuses it.
|
|
165
|
+
*/
|
|
166
|
+
readonly localDir?: string;
|
|
167
|
+
readonly fetch?: typeof fetch;
|
|
168
|
+
/** Run `--version` on the staged executable (default: when the target matches this machine). */
|
|
169
|
+
readonly handshake?: boolean;
|
|
170
|
+
readonly log?: (message: string) => void;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Downloads, verifies and unpacks the pinned helpers into `outDir`, then records what was staged.
|
|
175
|
+
* Reuses an already staged helper whose digests match. Throws `HelperLockError` on any mismatch;
|
|
176
|
+
* a partially staged directory is removed so a build never packages it.
|
|
177
|
+
*/
|
|
178
|
+
export async function stageVoiceHelpers(options: StageHelpersOptions): Promise<StagedHelperRecord> {
|
|
179
|
+
const log = options.log ?? (() => undefined);
|
|
180
|
+
const outDir = resolve(options.outDir);
|
|
181
|
+
const handshake = options.handshake ?? options.target === currentHelperTarget();
|
|
182
|
+
const existing = readStagedRecord(outDir);
|
|
183
|
+
const record: { schema: 1; version: string; target: HelperTarget; helpers: Record<string, { sha256: string; executableSha256: string }> } = {
|
|
184
|
+
schema: 1,
|
|
185
|
+
version: options.localDir ? `local+${options.lock.version}` : options.lock.version,
|
|
186
|
+
target: options.target,
|
|
187
|
+
helpers: {},
|
|
188
|
+
};
|
|
189
|
+
mkdirSync(outDir, { recursive: true });
|
|
190
|
+
try {
|
|
191
|
+
for (const helper of options.helpers) {
|
|
192
|
+
const executable = join(outDir, helper);
|
|
193
|
+
if (options.localDir) {
|
|
194
|
+
stageLocal(options.localDir, helper, outDir);
|
|
195
|
+
record.helpers[helper] = { sha256: "local", executableSha256: sha256File(executable) };
|
|
196
|
+
} else {
|
|
197
|
+
const pin = helperPin(options.lock, helper, options.target);
|
|
198
|
+
if (!pin) throw new HelperLockError(`voice helper lock pins no ${helper} for ${options.target}`);
|
|
199
|
+
const reuse =
|
|
200
|
+
existing?.version === options.lock.version &&
|
|
201
|
+
existing.helpers[helper]?.sha256 === pin.sha256 &&
|
|
202
|
+
existsSync(executable) &&
|
|
203
|
+
sha256File(executable) === pin.executableSha256;
|
|
204
|
+
if (reuse) {
|
|
205
|
+
log(`${helper} ${options.lock.version} already staged`);
|
|
206
|
+
} else {
|
|
207
|
+
const archive = await obtainArchive(options, helper, pin, log);
|
|
208
|
+
try {
|
|
209
|
+
extractArchive(archive, outDir, helper);
|
|
210
|
+
} finally {
|
|
211
|
+
// An uncached download lives in its own scratch directory; do not leak it.
|
|
212
|
+
if (!options.cacheDir) rmSync(dirname(archive), { recursive: true, force: true });
|
|
213
|
+
}
|
|
214
|
+
const actual = existsSync(executable) ? sha256File(executable) : "missing";
|
|
215
|
+
if (actual !== pin.executableSha256) {
|
|
216
|
+
throw new HelperLockError(`${helper}: executable sha256 ${actual} is not the pinned ${pin.executableSha256}`);
|
|
217
|
+
}
|
|
218
|
+
chmodSync(executable, 0o755);
|
|
219
|
+
log(`staged ${helper} ${options.lock.version} (${options.target})`);
|
|
220
|
+
}
|
|
221
|
+
record.helpers[helper] = { sha256: pin.sha256, executableSha256: pin.executableSha256 };
|
|
222
|
+
}
|
|
223
|
+
if (handshake) assertHandshake(executable);
|
|
224
|
+
}
|
|
225
|
+
} catch (error) {
|
|
226
|
+
rmSync(outDir, { recursive: true, force: true });
|
|
227
|
+
throw error;
|
|
228
|
+
}
|
|
229
|
+
writeFileSync(join(outDir, STAGED_RECORD_FILE), `${JSON.stringify(record, null, 2)}\n`);
|
|
230
|
+
return record;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export interface VerifyStagedOptions {
|
|
234
|
+
readonly lock: HelperLock;
|
|
235
|
+
readonly target: HelperTarget;
|
|
236
|
+
readonly dir: string;
|
|
237
|
+
readonly helpers: readonly HelperName[];
|
|
238
|
+
/** Run `--version` (only possible on a matching machine). */
|
|
239
|
+
readonly handshake?: boolean;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* The release gate: the packaged directory holds exactly the pinned helpers. Throws
|
|
244
|
+
* `HelperLockError` naming the first difference; a local (unpinned) staging is refused.
|
|
245
|
+
*/
|
|
246
|
+
export function verifyStagedVoiceHelpers(options: VerifyStagedOptions): void {
|
|
247
|
+
const dir = resolve(options.dir);
|
|
248
|
+
const record = readStagedRecord(dir);
|
|
249
|
+
if (!record) throw new HelperLockError(`no ${STAGED_RECORD_FILE} in ${dir}: the voice helpers were not staged from the lock`);
|
|
250
|
+
if (record.version !== options.lock.version) {
|
|
251
|
+
throw new HelperLockError(`staged voice helpers are ${record.version}; the lock pins ${options.lock.version}`);
|
|
252
|
+
}
|
|
253
|
+
if (record.target !== options.target) throw new HelperLockError(`staged voice helpers are for ${record.target}, not ${options.target}`);
|
|
254
|
+
for (const helper of options.helpers) {
|
|
255
|
+
const pin = helperPin(options.lock, helper, options.target);
|
|
256
|
+
if (!pin) throw new HelperLockError(`voice helper lock pins no ${helper} for ${options.target}`);
|
|
257
|
+
const executable = join(dir, helper);
|
|
258
|
+
if (!existsSync(executable)) throw new HelperLockError(`${helper} is missing from ${dir}`);
|
|
259
|
+
const actual = sha256File(executable);
|
|
260
|
+
if (actual !== pin.executableSha256) {
|
|
261
|
+
throw new HelperLockError(`${helper} in ${dir} has sha256 ${actual}, not the pinned ${pin.executableSha256}`);
|
|
262
|
+
}
|
|
263
|
+
if (options.handshake ?? options.target === currentHelperTarget()) assertHandshake(executable);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export function readStagedRecord(dir: string): StagedHelperRecord | undefined {
|
|
268
|
+
const path = join(dir, STAGED_RECORD_FILE);
|
|
269
|
+
if (!existsSync(path)) return undefined;
|
|
270
|
+
try {
|
|
271
|
+
return JSON.parse(readFileSync(path, "utf8")) as StagedHelperRecord;
|
|
272
|
+
} catch {
|
|
273
|
+
return undefined;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** Runs `<helper> --version` and refuses an incompatible or silent helper. */
|
|
278
|
+
export function assertHandshake(executable: string): void {
|
|
279
|
+
const run = spawnSync(executable, ["--version"], { encoding: "utf8", timeout: 10_000 });
|
|
280
|
+
if (run.error || run.status !== 0) {
|
|
281
|
+
throw new HelperLockError(`${executable} --version failed: ${run.error?.message ?? `exit ${String(run.status)}`}`);
|
|
282
|
+
}
|
|
283
|
+
const version = run.stdout
|
|
284
|
+
.split("\n")
|
|
285
|
+
.map(parseWireLine)
|
|
286
|
+
.find((event) => event?.event === "version");
|
|
287
|
+
if (!version || version.event !== "version") throw new HelperLockError(`${executable} printed no version handshake`);
|
|
288
|
+
if (version.wire !== WIRE_VERSION || version.contractMajor !== CONTRACT_MAJOR) {
|
|
289
|
+
throw new HelperLockError(
|
|
290
|
+
`${executable} speaks wire ${version.wire} / contract ${version.contract}; need wire ${WIRE_VERSION} / contract ${CONTRACT_MAJOR}`,
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
async function obtainArchive(
|
|
296
|
+
options: StageHelpersOptions,
|
|
297
|
+
helper: HelperName,
|
|
298
|
+
pin: HelperArtifactPin,
|
|
299
|
+
log: (message: string) => void,
|
|
300
|
+
): Promise<string> {
|
|
301
|
+
const cached = options.cacheDir ? join(options.cacheDir, `${pin.sha256}.tar.gz`) : undefined;
|
|
302
|
+
if (cached && existsSync(cached) && sha256File(cached) === pin.sha256) {
|
|
303
|
+
log(`${helper}: using cached ${pin.file}`);
|
|
304
|
+
return cached;
|
|
305
|
+
}
|
|
306
|
+
const url = helperArtifactUrl(options.lock, helper, options.target);
|
|
307
|
+
log(`${helper}: downloading ${url}`);
|
|
308
|
+
const response = await (options.fetch ?? fetch)(url, { redirect: "follow" });
|
|
309
|
+
if (!response.ok || !response.body) throw new HelperLockError(`${helper}: GET ${url} answered ${response.status}`);
|
|
310
|
+
const limit = pin.bytes ?? MAX_HELPER_ARCHIVE_BYTES;
|
|
311
|
+
const hash = createHash("sha256");
|
|
312
|
+
const chunks: Uint8Array[] = [];
|
|
313
|
+
let total = 0;
|
|
314
|
+
const reader = response.body.getReader();
|
|
315
|
+
for (;;) {
|
|
316
|
+
const { value, done } = await reader.read();
|
|
317
|
+
if (done) break;
|
|
318
|
+
total += value.byteLength;
|
|
319
|
+
if (total > limit) {
|
|
320
|
+
await reader.cancel();
|
|
321
|
+
throw new HelperLockError(`${helper}: ${url} is larger than the pinned ${limit} bytes`);
|
|
322
|
+
}
|
|
323
|
+
hash.update(value);
|
|
324
|
+
chunks.push(value);
|
|
325
|
+
}
|
|
326
|
+
const digest = hash.digest("hex");
|
|
327
|
+
if (digest !== pin.sha256) throw new HelperLockError(`${helper}: ${pin.file} has sha256 ${digest}, not the pinned ${pin.sha256}`);
|
|
328
|
+
const target = cached ?? join(mkdtempSync(join(tmpdir(), "adea-voice-helper-")), pin.file);
|
|
329
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
330
|
+
const partial = `${target}.partial`;
|
|
331
|
+
writeFileSync(partial, Buffer.concat(chunks));
|
|
332
|
+
renameSync(partial, target);
|
|
333
|
+
return target;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** Unpacks into a scratch directory, then moves only the expected entries into `outDir`. */
|
|
337
|
+
function extractArchive(archive: string, outDir: string, helper: HelperName): void {
|
|
338
|
+
const scratch = mkdtempSync(join(tmpdir(), "adea-voice-extract-"));
|
|
339
|
+
try {
|
|
340
|
+
const listing = spawnSync("tar", ["-tzf", archive], { encoding: "utf8" });
|
|
341
|
+
if (listing.status !== 0) throw new HelperLockError(`${helper}: cannot read ${archive}`);
|
|
342
|
+
for (const entry of listing.stdout.split("\n").filter(Boolean)) {
|
|
343
|
+
// No absolute paths, no parent traversal: the archive is pinned, but stay defensive.
|
|
344
|
+
if (entry.startsWith("/") || entry.split("/").includes("..")) throw new HelperLockError(`${helper}: unsafe archive entry ${entry}`);
|
|
345
|
+
}
|
|
346
|
+
const unpack = spawnSync("tar", ["-xzf", archive, "-C", scratch], { encoding: "utf8" });
|
|
347
|
+
if (unpack.status !== 0) throw new HelperLockError(`${helper}: cannot unpack ${archive}: ${unpack.stderr}`);
|
|
348
|
+
const executable = join(scratch, helper);
|
|
349
|
+
if (!existsSync(executable) || !statSync(executable).isFile()) throw new HelperLockError(`${helper}: the archive holds no ${helper}`);
|
|
350
|
+
rmSync(join(outDir, helper), { force: true });
|
|
351
|
+
renameSync(executable, join(outDir, helper));
|
|
352
|
+
const frameworks = join(scratch, "Frameworks");
|
|
353
|
+
if (existsSync(frameworks)) {
|
|
354
|
+
const destination = join(outDir, "Frameworks");
|
|
355
|
+
mkdirSync(destination, { recursive: true });
|
|
356
|
+
for (const entry of spawnSync("ls", ["-1", frameworks], { encoding: "utf8" }).stdout.split("\n").filter(Boolean)) {
|
|
357
|
+
rmSync(join(destination, entry), { recursive: true, force: true });
|
|
358
|
+
renameSync(join(frameworks, entry), join(destination, entry));
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
} finally {
|
|
362
|
+
rmSync(scratch, { recursive: true, force: true });
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function stageLocal(localDir: string, helper: HelperName, outDir: string): void {
|
|
367
|
+
const source = join(resolve(localDir), helper);
|
|
368
|
+
if (!existsSync(source)) throw new HelperLockError(`${helper} is not in the local helper directory ${localDir}`);
|
|
369
|
+
const destination = join(outDir, helper);
|
|
370
|
+
rmSync(destination, { force: true });
|
|
371
|
+
writeFileSync(destination, readFileSync(source));
|
|
372
|
+
chmodSync(destination, 0o755);
|
|
373
|
+
const frameworks = join(resolve(localDir), "Frameworks");
|
|
374
|
+
if (existsSync(frameworks)) spawnSync("cp", ["-R", frameworks, outDir]);
|
|
375
|
+
}
|