Files
openbot/scripts/runtime-install.ts
T
NBandClaude Opus 5.5 e26b637ad0 Move shared runtime installer helpers to one module (#1458)
* Move shared runtime installer helpers to scripts/runtime-install.ts

The Claude, Codex, Grok, cua-driver and remote desktop installers had
copies of the same helpers. Put one copy of each in
scripts/runtime-install.ts:

- installValidatedTree (the remote desktop installer calls it for the
  platform/architecture directory)
- isCurrentInstallation, which now takes the verify function
- download (Grok and remote desktop)
- safeArchivePathParts, the common safety part of validateArchivePath
- escapeRegExp

Error messages, user agents and install paths do not change. The remote
desktop download now sets redirect: "follow" explicitly; that is the
fetch default, so the result is the same.

The cua-driver and pin-cua-driver downloads keep their own copies,
because their error messages (and the pin user agent) are different.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Keep remote-desktop-runtime-release.ts unchanged

The remote desktop workflow requires a recipeVersion increase when this
file changes. That increase changes the input digest and starts a new
native build and release. A refactor must not do that, so this file keeps
its own validateArchivePath.

Also run the cua-driver workflow when scripts/runtime-install.ts changes,
because install-cua-driver.ts now imports it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 17:07:29 +02:00

48 lines
1.9 KiB
TypeScript

import { cp, mkdir, rename, rm } from "node:fs/promises";
import { dirname, join } from "node:path";
export async function isCurrentInstallation(verify: () => Promise<void>): Promise<boolean> {
try {
await verify();
return true;
} catch {
return false;
}
}
export async function download(fetchImpl: typeof fetch, url: string, label: string): Promise<Buffer> {
const response = await fetchImpl(url, {
headers: { "User-Agent": "OpenBot-runtime-installer" },
redirect: "follow",
});
if (!response.ok) throw new Error(`${label} download failed with HTTP ${response.status}.`);
return Buffer.from(await response.arrayBuffer());
}
/** Splits a relative archive path into parts and rejects absolute, drive, `.` and `..` paths. */
export function safeArchivePathParts(name: string, label: string): string[] {
if (name.includes("\0") || name.includes("\\")) throw new Error(`Unsafe ${label} archive path: ${name}`);
const normalized = name.replace(/\/+$/u, "");
if (!normalized || normalized.startsWith("/") || /^[A-Za-z]:/u.test(normalized)) {
throw new Error(`Unsafe ${label} archive path: ${name}`);
}
const parts = normalized.split("/");
if (parts.some((part) => !part || part === "." || part === "..")) {
throw new Error(`Unsafe ${label} archive path: ${name}`);
}
return parts;
}
export async function installValidatedTree(source: string, destination: string): Promise<void> {
const temporaryTarget = join(dirname(destination), `.${destination.split(/[\\/]/u).at(-1)}.installing`);
await mkdir(dirname(destination), { recursive: true });
await rm(temporaryTarget, { recursive: true, force: true });
await cp(source, temporaryTarget, { recursive: true });
await rm(destination, { recursive: true, force: true });
await rename(temporaryTarget, destination);
}
export function escapeRegExp(value: string): string {
return value.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&");
}