tailcat-webui 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/NOTICE +23 -0
- package/README.en.md +104 -0
- package/README.md +93 -2
- package/bin/tailcat-webui.js +12 -0
- package/docs/CORE-API.md +343 -0
- package/docs/DESIGN.md +378 -0
- package/docs/screenshots/01-overview-light.png +0 -0
- package/docs/screenshots/02-overview-dark.png +0 -0
- package/docs/screenshots/03-ticket-light.png +0 -0
- package/docs/screenshots/04-ticket-qr-light.png +0 -0
- package/docs/screenshots/05-settings-light.png +0 -0
- package/docs/screenshots/06-diagnostics-light.png +0 -0
- package/docs/screenshots/07-mobile-overview.png +0 -0
- package/docs/screenshots/08-install-missing.png +0 -0
- package/package.json +56 -4
- package/src/cli.js +238 -0
- package/src/config.js +167 -0
- package/src/devices.js +163 -0
- package/src/errors.js +24 -0
- package/src/index.js +19 -0
- package/src/installer.js +907 -0
- package/src/manager.js +647 -0
- package/src/parse.js +207 -0
- package/src/paths.js +97 -0
- package/src/preferences.js +190 -0
- package/src/qr.js +37 -0
- package/src/relay.js +76 -0
- package/src/runtime/node.js +281 -0
- package/src/serve-targets.js +127 -0
- package/src/server.js +669 -0
- package/src/settings.js +262 -0
- package/src/supervisor.js +833 -0
- package/vendor/qrcode.mjs +2289 -0
- package/web/api.js +114 -0
- package/web/boot.js +29 -0
- package/web/components.js +504 -0
- package/web/dict.js +655 -0
- package/web/drawer-access.js +318 -0
- package/web/drawer-address.js +271 -0
- package/web/drawer-automation.js +223 -0
- package/web/drawer-relay.js +128 -0
- package/web/drawer-tunnel.js +309 -0
- package/web/drawer.js +302 -0
- package/web/format.js +252 -0
- package/web/icon.svg +11 -0
- package/web/index.html +29 -0
- package/web/main.js +471 -0
- package/web/room-diagnostics.js +426 -0
- package/web/room-overview.js +416 -0
- package/web/room-settings.js +71 -0
- package/web/store.js +168 -0
- package/web/styles.css +2741 -0
- package/web/ticket.js +212 -0
package/src/installer.js
ADDED
|
@@ -0,0 +1,907 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import fsp from 'node:fs/promises';
|
|
4
|
+
import os from 'node:os';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { Readable, Transform } from 'node:stream';
|
|
7
|
+
import { pipeline } from 'node:stream/promises';
|
|
8
|
+
import { MIN_TAILCAT_VERSION } from './config.js';
|
|
9
|
+
import { RemoteAccessError, errorMessage } from './errors.js';
|
|
10
|
+
import { defaultStateDir, findKnownTailcat } from './paths.js';
|
|
11
|
+
import { createNodeRuntime } from './runtime/node.js';
|
|
12
|
+
|
|
13
|
+
/** The upstream tailcat build this package knows how to install. */
|
|
14
|
+
export const TAILCAT_VERSION = '0.8.0';
|
|
15
|
+
export const TAILCAT_TAG = 'v0.8.0';
|
|
16
|
+
export const UPSTREAM_RELEASES = `https://github.com/tailscale/tailcat/releases/download/${TAILCAT_TAG}`;
|
|
17
|
+
export const MANUAL_INSTALL_URL = 'https://github.com/tailscale/tailcat#install';
|
|
18
|
+
|
|
19
|
+
const MAX_ARCHIVE_BYTES = 64 * 1024 * 1024;
|
|
20
|
+
const MAX_TEXT_BYTES = 512 * 1024;
|
|
21
|
+
const COMMAND_TIMEOUT_MS = 180_000;
|
|
22
|
+
const BREW_TIMEOUT_MS = 30 * 60_000;
|
|
23
|
+
const METADATA_FILE = 'tailcat.json';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* One installable target. `archive` downloads an upstream release archive;
|
|
27
|
+
* `brew` delegates to Homebrew because tailcat does not publish macOS static
|
|
28
|
+
* binaries. `unsupported` still carries the manual instructions.
|
|
29
|
+
* @typedef {{kind: 'archive', os: string, arch: string, asset: string, format: 'tar.gz'|'zip', binary: string, source: 'upstream', assetUrl: string, checksumsUrl: string}
|
|
30
|
+
* | {kind: 'brew', os: string, arch: string, binary: string, source: 'homebrew', formula: string, command: string}
|
|
31
|
+
* | {kind: 'unsupported', os: string, arch: string}} PlatformSpec
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Map Node's `process.platform`/`process.arch` to a tailcat distribution
|
|
36
|
+
* target. Kept pure so the interface and tests can describe the platform
|
|
37
|
+
* without starting a download.
|
|
38
|
+
* @param {string} [platform] - Node platform.
|
|
39
|
+
* @param {string} [arch] - Node architecture.
|
|
40
|
+
* @returns {PlatformSpec} install target.
|
|
41
|
+
*/
|
|
42
|
+
export function platformSpec(platform = process.platform, arch = process.arch) {
|
|
43
|
+
const key = `${platform}:${arch}`;
|
|
44
|
+
/** @type {Record<string, PlatformSpec>} */
|
|
45
|
+
const specs = {
|
|
46
|
+
'linux:x64': archiveSpec('linux', 'amd64', 'linux_amd64.tar.gz', 'tar.gz'),
|
|
47
|
+
'linux:arm64': archiveSpec('linux', 'arm64', 'linux_arm64.tar.gz', 'tar.gz'),
|
|
48
|
+
'linux:arm': archiveSpec('linux', 'armv7', 'linux_armv7.tar.gz', 'tar.gz'),
|
|
49
|
+
'win32:x64': archiveSpec('windows', 'amd64', 'windows_amd64.zip', 'zip'),
|
|
50
|
+
'win32:arm64': archiveSpec('windows', 'arm64', 'windows_arm64.zip', 'zip'),
|
|
51
|
+
'darwin:x64': {
|
|
52
|
+
kind: 'brew',
|
|
53
|
+
os: 'darwin',
|
|
54
|
+
arch: 'x64',
|
|
55
|
+
binary: 'tailcat',
|
|
56
|
+
source: 'homebrew',
|
|
57
|
+
formula: 'tailcat',
|
|
58
|
+
command: 'brew install tailcat',
|
|
59
|
+
},
|
|
60
|
+
'darwin:arm64': {
|
|
61
|
+
kind: 'brew',
|
|
62
|
+
os: 'darwin',
|
|
63
|
+
arch: 'arm64',
|
|
64
|
+
binary: 'tailcat',
|
|
65
|
+
source: 'homebrew',
|
|
66
|
+
formula: 'tailcat',
|
|
67
|
+
command: 'brew install tailcat',
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
return specs[key] ?? { kind: 'unsupported', os: platform, arch };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* @param {string} osName - archive OS.
|
|
75
|
+
* @param {string} arch - archive architecture label.
|
|
76
|
+
* @param {string} suffix - asset filename suffix.
|
|
77
|
+
* @param {'tar.gz'|'zip'} format - archive format.
|
|
78
|
+
* @returns {PlatformSpec} archive target.
|
|
79
|
+
*/
|
|
80
|
+
function archiveSpec(osName, arch, suffix, format) {
|
|
81
|
+
const asset = `tailcat_${TAILCAT_VERSION}_${suffix}`;
|
|
82
|
+
return {
|
|
83
|
+
kind: 'archive',
|
|
84
|
+
os: osName,
|
|
85
|
+
arch,
|
|
86
|
+
asset,
|
|
87
|
+
format,
|
|
88
|
+
binary: osName === 'windows' ? 'tailcat.exe' : 'tailcat',
|
|
89
|
+
source: 'upstream',
|
|
90
|
+
assetUrl: `${UPSTREAM_RELEASES}/${asset}`,
|
|
91
|
+
checksumsUrl: `${UPSTREAM_RELEASES}/checksums.txt`,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Parse a release checksum manifest (sha256 + whitespace + filename).
|
|
97
|
+
* @param {string} text - manifest contents.
|
|
98
|
+
* @returns {Map<string, string>} filename to lowercase digest.
|
|
99
|
+
*/
|
|
100
|
+
export function parseChecksums(text) {
|
|
101
|
+
const checksums = new Map();
|
|
102
|
+
for (const line of String(text).split(/\r?\n/)) {
|
|
103
|
+
const match = /^([0-9a-fA-F]{64})\s+\*?(.+?)\s*$/.exec(line.trim());
|
|
104
|
+
if (match) checksums.set(path.basename(match[2]), match[1].toLowerCase());
|
|
105
|
+
}
|
|
106
|
+
return checksums;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* @param {string} file - path to hash.
|
|
111
|
+
* @returns {Promise<string>} lowercase sha256.
|
|
112
|
+
*/
|
|
113
|
+
export async function sha256File(file) {
|
|
114
|
+
const hash = crypto.createHash('sha256');
|
|
115
|
+
for await (const chunk of fs.createReadStream(file)) hash.update(chunk);
|
|
116
|
+
return hash.digest('hex');
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Parse a semantic version out of `tailcat version` output.
|
|
121
|
+
* @param {string | null | undefined} value - version text.
|
|
122
|
+
* @returns {[number, number, number] | null} numeric triple.
|
|
123
|
+
*/
|
|
124
|
+
export function versionParts(value) {
|
|
125
|
+
const match = /(\d+)\.(\d+)\.(\d+)/.exec(String(value ?? ''));
|
|
126
|
+
return match ? [Number(match[1]), Number(match[2]), Number(match[3])] : null;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* @param {string | null | undefined} version - candidate.
|
|
131
|
+
* @param {string} minimum - required semantic version.
|
|
132
|
+
* @returns {boolean} whether candidate is at least minimum.
|
|
133
|
+
*/
|
|
134
|
+
export function versionAtLeast(version, minimum) {
|
|
135
|
+
const left = versionParts(version);
|
|
136
|
+
const right = versionParts(minimum);
|
|
137
|
+
if (!left || !right) return false;
|
|
138
|
+
for (let index = 0; index < 3; index += 1) {
|
|
139
|
+
if (left[index] !== right[index]) return left[index] > right[index];
|
|
140
|
+
}
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Install and update the managed tailcat executable without modifying PATH.
|
|
146
|
+
* Downloads are verified against the release checksum before extraction, and
|
|
147
|
+
* the archive is staged under the state directory before an atomic replace.
|
|
148
|
+
*
|
|
149
|
+
* Detection is deliberately separate from installation: a tailcat already
|
|
150
|
+
* installed by the operator stays in place and is used by the supervisor. The
|
|
151
|
+
* managed copy is only created after an explicit install action.
|
|
152
|
+
*/
|
|
153
|
+
export class TailcatInstaller {
|
|
154
|
+
/**
|
|
155
|
+
* @param {object} [options] - installer options.
|
|
156
|
+
* @param {any} [options.logger] - logger.
|
|
157
|
+
* @param {string} [options.releaseTag] - package release tag, used in the download user agent.
|
|
158
|
+
* @param {string} [options.requestedBinary] - configured binaryPath (defaults to `tailcat`).
|
|
159
|
+
* @param {typeof fetch} [options.fetchImpl] - fetch implementation.
|
|
160
|
+
* @param {(name: string, env: NodeJS.ProcessEnv) => Promise<string>} [options.resolveExecutable] - executable resolver override.
|
|
161
|
+
* @param {(argv: string[], options: {timeoutMs?: number, env?: NodeJS.ProcessEnv, signal?: AbortSignal}) => Promise<{stdout: string, stderr: string, exitCode: number | null}>} [options.runCommand] - command runner override.
|
|
162
|
+
* @param {string} [options.platform] - Node platform override.
|
|
163
|
+
* @param {string} [options.arch] - Node architecture override.
|
|
164
|
+
* @param {string} [options.installDir] - directory for the managed executable; defaults to `<state dir>/bin`.
|
|
165
|
+
* @param {boolean} [options.enabled] - whether managed installs are offered (default true).
|
|
166
|
+
* @param {{dataDir?: string, binaryDir?: string, binaryPath?: string, stagingRoot?: string}} [options.paths] - path overrides.
|
|
167
|
+
* @param {object} [options.runtime] - process adapter.
|
|
168
|
+
*/
|
|
169
|
+
constructor(options = {}) {
|
|
170
|
+
this.logger = options.logger ?? { info() {}, warn() {} };
|
|
171
|
+
this.runtime = options.runtime ?? createNodeRuntime();
|
|
172
|
+
this.releaseTag = options.releaseTag ?? 'v0.0.0';
|
|
173
|
+
this.requestedBinary = options.requestedBinary ?? 'tailcat';
|
|
174
|
+
this.fetchImpl = options.fetchImpl ?? globalThis.fetch;
|
|
175
|
+
this.platform = options.platform ?? process.platform;
|
|
176
|
+
this.arch = options.arch ?? process.arch;
|
|
177
|
+
this.spec = platformSpec(this.platform, this.arch);
|
|
178
|
+
this.enabled = options.enabled !== false;
|
|
179
|
+
|
|
180
|
+
const installDir = options.installDir ?? path.join(defaultStateDir(this.platform), 'bin');
|
|
181
|
+
const overrides = options.paths ?? {};
|
|
182
|
+
this.dataDir = overrides.dataDir ?? path.dirname(installDir);
|
|
183
|
+
this.binaryDir = overrides.binaryDir ?? installDir;
|
|
184
|
+
this.binaryPath =
|
|
185
|
+
overrides.binaryPath ?? path.join(this.binaryDir, this.platform === 'win32' ? 'tailcat.exe' : 'tailcat');
|
|
186
|
+
this.stagingRoot = overrides.stagingRoot ?? path.join(this.dataDir, '.install');
|
|
187
|
+
this.metadataPath = path.join(this.binaryDir, METADATA_FILE);
|
|
188
|
+
|
|
189
|
+
this.resolveExecutable =
|
|
190
|
+
options.resolveExecutable ?? ((name, env) => this.runtime.resolveExecutable(name, env));
|
|
191
|
+
this.runCommand = options.runCommand ?? ((argv, runOptions) => this.runInRuntime(argv, runOptions));
|
|
192
|
+
|
|
193
|
+
this.phase = 'idle';
|
|
194
|
+
this.busy = false;
|
|
195
|
+
this.progress = null;
|
|
196
|
+
this.lastError = null;
|
|
197
|
+
this.installedAt = null;
|
|
198
|
+
this.promise = null;
|
|
199
|
+
this.abort = null;
|
|
200
|
+
this.disposed = false;
|
|
201
|
+
this.system = null;
|
|
202
|
+
this.systemPromise = null;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** @returns {object} display-safe installer state. */
|
|
206
|
+
snapshot() {
|
|
207
|
+
const installed = this.readInstalled();
|
|
208
|
+
return {
|
|
209
|
+
supported: this.spec.kind !== 'unsupported',
|
|
210
|
+
method: this.spec.kind === 'unsupported' ? null : this.spec.kind,
|
|
211
|
+
platform: this.platform,
|
|
212
|
+
arch: this.arch,
|
|
213
|
+
version: TAILCAT_VERSION,
|
|
214
|
+
installed: installed !== null,
|
|
215
|
+
installedVersion: installed?.version ?? null,
|
|
216
|
+
installedAt: installed?.installedAt ?? null,
|
|
217
|
+
source: installed?.source ?? null,
|
|
218
|
+
binaryPath: this.binaryPath,
|
|
219
|
+
asset: this.spec.kind === 'archive' ? this.spec.asset : null,
|
|
220
|
+
command: this.spec.kind === 'brew' ? this.spec.command : null,
|
|
221
|
+
manualUrl: MANUAL_INSTALL_URL,
|
|
222
|
+
busy: this.busy,
|
|
223
|
+
phase: this.phase,
|
|
224
|
+
progress: this.progress ? { ...this.progress } : null,
|
|
225
|
+
error: this.lastError,
|
|
226
|
+
system: this.system ? { ...this.system } : null,
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** @returns {boolean} whether the managed executable is installed. */
|
|
231
|
+
hasInstalled() {
|
|
232
|
+
return this.readInstalled() !== null;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Find a tailcat installed independently (PATH, Homebrew, an explicit
|
|
237
|
+
* binaryPath, ...). This never downloads or writes anything.
|
|
238
|
+
* @returns {Promise<object>} cached detection result.
|
|
239
|
+
*/
|
|
240
|
+
detectSystem() {
|
|
241
|
+
if (this.systemPromise) return this.systemPromise;
|
|
242
|
+
this.system = { status: 'checking', path: null, version: null, checkedAt: null };
|
|
243
|
+
this.systemPromise = this.detectSystemInternal()
|
|
244
|
+
.catch((error) => {
|
|
245
|
+
this.system = {
|
|
246
|
+
status: 'missing',
|
|
247
|
+
path: null,
|
|
248
|
+
version: null,
|
|
249
|
+
checkedAt: new Date().toISOString(),
|
|
250
|
+
error: errorMessage(error),
|
|
251
|
+
};
|
|
252
|
+
return this.system;
|
|
253
|
+
})
|
|
254
|
+
.finally(() => {
|
|
255
|
+
this.systemPromise = null;
|
|
256
|
+
});
|
|
257
|
+
return this.systemPromise;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** @private */
|
|
261
|
+
async detectSystemInternal() {
|
|
262
|
+
let resolved = null;
|
|
263
|
+
try {
|
|
264
|
+
resolved = await this.resolveExecutable(this.requestedBinary, process.env);
|
|
265
|
+
} catch {
|
|
266
|
+
resolved = this.requestedBinary === 'tailcat' ? findKnownTailcat(this.platform) : null;
|
|
267
|
+
}
|
|
268
|
+
if (!resolved) {
|
|
269
|
+
this.system = { status: 'missing', path: null, version: null, checkedAt: new Date().toISOString() };
|
|
270
|
+
return this.system;
|
|
271
|
+
}
|
|
272
|
+
let version = null;
|
|
273
|
+
try {
|
|
274
|
+
const result = await this.runCommand([resolved, 'version'], { timeoutMs: 15_000 });
|
|
275
|
+
const match = /v?(\d+\.\d+\.\d+)/.exec(`${result.stdout}\n${result.stderr}`);
|
|
276
|
+
if (match) version = match[0].startsWith('v') ? match[0] : `v${match[0]}`;
|
|
277
|
+
} catch {
|
|
278
|
+
// A version probe is diagnostic only. A path that exists and runs as an
|
|
279
|
+
// executable is still more useful than treating it as missing.
|
|
280
|
+
}
|
|
281
|
+
this.system = {
|
|
282
|
+
status: 'found',
|
|
283
|
+
path: resolved,
|
|
284
|
+
version,
|
|
285
|
+
compatible: version ? versionAtLeast(version, MIN_TAILCAT_VERSION) : null,
|
|
286
|
+
checkedAt: new Date().toISOString(),
|
|
287
|
+
};
|
|
288
|
+
return this.system;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Download, verify, and install the managed executable.
|
|
293
|
+
* @param {{force?: boolean}} [options] - force reinstalls over an existing copy.
|
|
294
|
+
* @returns {Promise<object>} installer snapshot after completion.
|
|
295
|
+
*/
|
|
296
|
+
install(options = {}) {
|
|
297
|
+
if (this.disposed) {
|
|
298
|
+
return Promise.reject(new RemoteAccessError('disposed', 'The tunnel runtime is shutting down.', 503));
|
|
299
|
+
}
|
|
300
|
+
if (this.promise) return this.promise;
|
|
301
|
+
if (!options.force && this.readInstalled() !== null) return Promise.resolve(this.snapshot());
|
|
302
|
+
|
|
303
|
+
this.busy = true;
|
|
304
|
+
this.phase = 'starting';
|
|
305
|
+
this.progress = null;
|
|
306
|
+
this.lastError = null;
|
|
307
|
+
const controller = new AbortController();
|
|
308
|
+
this.abort = controller;
|
|
309
|
+
this.promise = this.installInternal(controller.signal)
|
|
310
|
+
.catch((error) => {
|
|
311
|
+
this.phase = 'error';
|
|
312
|
+
this.lastError = errorMessage(error);
|
|
313
|
+
throw error;
|
|
314
|
+
})
|
|
315
|
+
.finally(() => {
|
|
316
|
+
this.busy = false;
|
|
317
|
+
this.abort = null;
|
|
318
|
+
this.promise = null;
|
|
319
|
+
});
|
|
320
|
+
return this.promise;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** Stop an in-flight download when the runtime shuts down. */
|
|
324
|
+
dispose() {
|
|
325
|
+
this.disposed = true;
|
|
326
|
+
this.abort?.abort();
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/** @private */
|
|
330
|
+
async installInternal(signal) {
|
|
331
|
+
if (!this.enabled) {
|
|
332
|
+
throw new RemoteAccessError(
|
|
333
|
+
'tailcat-platform-unsupported',
|
|
334
|
+
`Managed tailcat installs are disabled for this deployment. Install tailcat manually from ${MANUAL_INSTALL_URL} and put it on PATH.`,
|
|
335
|
+
400,
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
if (this.spec.kind === 'unsupported') {
|
|
339
|
+
throw new RemoteAccessError(
|
|
340
|
+
'tailcat-platform-unsupported',
|
|
341
|
+
`Automatic tailcat install is not available for ${this.platform}/${this.arch}. Install it manually from ${MANUAL_INSTALL_URL}, then put it on PATH or set binaryPath.`,
|
|
342
|
+
400,
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
await fsp.mkdir(this.dataDir, { recursive: true, mode: 0o700 });
|
|
346
|
+
await fsp.mkdir(this.binaryDir, { recursive: true, mode: 0o700 });
|
|
347
|
+
await fsp.mkdir(this.stagingRoot, { recursive: true, mode: 0o700 });
|
|
348
|
+
const staging = await fsp.mkdtemp(path.join(this.stagingRoot, 'run-'));
|
|
349
|
+
try {
|
|
350
|
+
const result =
|
|
351
|
+
this.spec.kind === 'archive'
|
|
352
|
+
? await this.installArchive(staging, signal)
|
|
353
|
+
: await this.installBrew(staging, signal);
|
|
354
|
+
this.phase = 'done';
|
|
355
|
+
this.progress = null;
|
|
356
|
+
this.installedAt = result.installedAt;
|
|
357
|
+
this.system = null;
|
|
358
|
+
// The caller's action response is a snapshot too; report the finished
|
|
359
|
+
// state rather than the busy flag the promise is about to clear.
|
|
360
|
+
this.busy = false;
|
|
361
|
+
this.abort = null;
|
|
362
|
+
this.logger.info(`tailcat-webui: installed tailcat ${result.version ?? TAILCAT_VERSION} from ${result.source}`);
|
|
363
|
+
return this.snapshot();
|
|
364
|
+
} finally {
|
|
365
|
+
await fsp.rm(staging, { recursive: true, force: true }).catch(() => {});
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/** @private */
|
|
370
|
+
async installArchive(staging, signal) {
|
|
371
|
+
const spec = this.spec;
|
|
372
|
+
if (spec.kind !== 'archive') throw new Error('not an archive target');
|
|
373
|
+
this.phase = 'checksums';
|
|
374
|
+
const manifest = await this.fetchText(spec.checksumsUrl, signal);
|
|
375
|
+
const expected = parseChecksums(manifest).get(spec.asset);
|
|
376
|
+
if (!expected) {
|
|
377
|
+
throw new RemoteAccessError(
|
|
378
|
+
'tailcat-checksum-missing',
|
|
379
|
+
`The tailcat release checksums do not list ${spec.asset}. Refusing to install an unverified executable.`,
|
|
380
|
+
502,
|
|
381
|
+
);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
const archivePath = path.join(staging, spec.asset);
|
|
385
|
+
this.phase = 'downloading';
|
|
386
|
+
this.progress = { phase: 'downloading', received: 0, total: 0, percent: null };
|
|
387
|
+
await this.download(spec.assetUrl, archivePath, signal);
|
|
388
|
+
if (signal.aborted) throw installAborted();
|
|
389
|
+
|
|
390
|
+
this.phase = 'verifying';
|
|
391
|
+
this.progress = null;
|
|
392
|
+
const actual = await sha256File(archivePath);
|
|
393
|
+
if (actual !== expected) {
|
|
394
|
+
throw new RemoteAccessError(
|
|
395
|
+
'tailcat-checksum-mismatch',
|
|
396
|
+
`tailcat archive checksum mismatch: expected ${expected}, got ${actual}.`,
|
|
397
|
+
502,
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
const extracted = path.join(staging, 'extracted');
|
|
402
|
+
this.phase = 'extracting';
|
|
403
|
+
await fsp.mkdir(extracted, { recursive: true, mode: 0o700 });
|
|
404
|
+
await this.extractArchive(archivePath, extracted, spec);
|
|
405
|
+
const candidate = await findBinary(extracted, spec.binary);
|
|
406
|
+
if (!candidate) {
|
|
407
|
+
throw new RemoteAccessError('tailcat-archive-invalid', `The tailcat archive did not contain ${spec.binary}.`, 502);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
this.phase = 'installing';
|
|
411
|
+
const version = await this.probe(candidate);
|
|
412
|
+
this.ensureCompatible(version);
|
|
413
|
+
const installedAt = new Date().toISOString();
|
|
414
|
+
await this.replaceBinary(candidate);
|
|
415
|
+
await this.writeMetadata({
|
|
416
|
+
version: version ?? TAILCAT_VERSION,
|
|
417
|
+
source: 'upstream',
|
|
418
|
+
asset: spec.asset,
|
|
419
|
+
checksum: expected,
|
|
420
|
+
installedAt,
|
|
421
|
+
platform: this.platform,
|
|
422
|
+
arch: this.arch,
|
|
423
|
+
});
|
|
424
|
+
return { version: version ?? TAILCAT_VERSION, source: 'upstream', installedAt };
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/** @private */
|
|
428
|
+
async installBrew(staging, signal) {
|
|
429
|
+
if (this.spec.kind !== 'brew') throw new Error('not a Homebrew target');
|
|
430
|
+
const brew = await this.findBrew();
|
|
431
|
+
if (!brew) {
|
|
432
|
+
throw new RemoteAccessError(
|
|
433
|
+
'tailcat-brew-missing',
|
|
434
|
+
`Homebrew was not found. Install Homebrew (https://brew.sh), then retry, or install tailcat manually from ${MANUAL_INSTALL_URL} and set binaryPath.`,
|
|
435
|
+
503,
|
|
436
|
+
);
|
|
437
|
+
}
|
|
438
|
+
this.phase = 'homebrew';
|
|
439
|
+
this.progress = { phase: 'homebrew', received: 0, total: 0, percent: null };
|
|
440
|
+
try {
|
|
441
|
+
await this.runCommand([brew, 'update'], {
|
|
442
|
+
timeoutMs: 10 * 60_000,
|
|
443
|
+
env: { HOMEBREW_NO_INSTALL_CLEANUP: '1', NONINTERACTIVE: '1' },
|
|
444
|
+
signal,
|
|
445
|
+
});
|
|
446
|
+
} catch (error) {
|
|
447
|
+
if (signal.aborted) throw installAborted();
|
|
448
|
+
this.logger.warn(`tailcat-webui: brew update failed, continuing with the local formula: ${errorMessage(error)}`);
|
|
449
|
+
}
|
|
450
|
+
const installArgs = this.system?.compatible === false ? ['upgrade', 'tailcat'] : ['install', 'tailcat'];
|
|
451
|
+
try {
|
|
452
|
+
await this.runCommand([brew, ...installArgs], {
|
|
453
|
+
timeoutMs: BREW_TIMEOUT_MS,
|
|
454
|
+
env: { HOMEBREW_NO_INSTALL_CLEANUP: '1', NONINTERACTIVE: '1' },
|
|
455
|
+
signal,
|
|
456
|
+
});
|
|
457
|
+
} catch (error) {
|
|
458
|
+
if (installArgs[0] !== 'upgrade' || signal.aborted) throw error;
|
|
459
|
+
this.logger.warn(`tailcat-webui: brew upgrade failed, trying a fresh install: ${errorMessage(error)}`);
|
|
460
|
+
await this.runCommand([brew, 'install', 'tailcat'], {
|
|
461
|
+
timeoutMs: BREW_TIMEOUT_MS,
|
|
462
|
+
env: { HOMEBREW_NO_INSTALL_CLEANUP: '1', NONINTERACTIVE: '1' },
|
|
463
|
+
signal,
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
const prefix = await this.runCommand([brew, '--prefix', 'tailcat'], {
|
|
467
|
+
timeoutMs: COMMAND_TIMEOUT_MS,
|
|
468
|
+
signal,
|
|
469
|
+
}).catch(() => null);
|
|
470
|
+
const candidates = [
|
|
471
|
+
prefix?.stdout?.trim() ? path.join(prefix.stdout.trim(), 'bin', this.spec.binary) : null,
|
|
472
|
+
path.join(path.dirname(brew), this.spec.binary),
|
|
473
|
+
].filter((value) => typeof value === 'string');
|
|
474
|
+
const candidate = candidates.find((value) => isFile(value));
|
|
475
|
+
if (!candidate) {
|
|
476
|
+
throw new RemoteAccessError(
|
|
477
|
+
'tailcat-brew-missing',
|
|
478
|
+
'Homebrew finished but did not leave a tailcat executable in its prefix. Check `brew install tailcat`, or set binaryPath.',
|
|
479
|
+
502,
|
|
480
|
+
);
|
|
481
|
+
}
|
|
482
|
+
const staged = path.join(staging, this.spec.binary);
|
|
483
|
+
await fsp.copyFile(candidate, staged);
|
|
484
|
+
const version = await this.probe(staged);
|
|
485
|
+
this.ensureCompatible(version);
|
|
486
|
+
const installedAt = new Date().toISOString();
|
|
487
|
+
await this.replaceBinary(staged);
|
|
488
|
+
await this.writeMetadata({
|
|
489
|
+
version: version ?? TAILCAT_VERSION,
|
|
490
|
+
source: 'homebrew',
|
|
491
|
+
asset: null,
|
|
492
|
+
checksum: null,
|
|
493
|
+
installedAt,
|
|
494
|
+
platform: this.platform,
|
|
495
|
+
arch: this.arch,
|
|
496
|
+
});
|
|
497
|
+
return { version: version ?? TAILCAT_VERSION, source: 'homebrew', installedAt };
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/** @private */
|
|
501
|
+
async findBrew() {
|
|
502
|
+
// PATH first: a named `brew` is the user's own choice and works even when
|
|
503
|
+
// Homebrew lives somewhere unusual. The well-known locations are a fallback
|
|
504
|
+
// for GUIs whose PATH omits the shell's Homebrew entry.
|
|
505
|
+
try {
|
|
506
|
+
return await this.resolveExecutable('brew', process.env);
|
|
507
|
+
} catch {
|
|
508
|
+
// Fall through to the platform defaults.
|
|
509
|
+
}
|
|
510
|
+
const common = this.platform === 'darwin' ? ['/opt/homebrew/bin/brew', '/usr/local/bin/brew'] : [];
|
|
511
|
+
for (const candidate of common) {
|
|
512
|
+
if (isFile(candidate) && isExecutable(candidate)) return candidate;
|
|
513
|
+
}
|
|
514
|
+
return null;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/** @private */
|
|
518
|
+
async download(url, destination, signal) {
|
|
519
|
+
const response = await this.fetchImpl(url, {
|
|
520
|
+
redirect: 'follow',
|
|
521
|
+
signal,
|
|
522
|
+
headers: {
|
|
523
|
+
accept: 'application/octet-stream',
|
|
524
|
+
'user-agent': `tailcat-webui/${this.releaseTag.replace(/^v/, '')}`,
|
|
525
|
+
},
|
|
526
|
+
});
|
|
527
|
+
if (!response.ok || !response.body) {
|
|
528
|
+
throw new RemoteAccessError(
|
|
529
|
+
'tailcat-archive-invalid',
|
|
530
|
+
`Could not download ${url}: HTTP ${response.status}.`,
|
|
531
|
+
502,
|
|
532
|
+
);
|
|
533
|
+
}
|
|
534
|
+
const total = Number(response.headers.get('content-length')) || 0;
|
|
535
|
+
if (total > MAX_ARCHIVE_BYTES) {
|
|
536
|
+
throw new RemoteAccessError(
|
|
537
|
+
'tailcat-archive-invalid',
|
|
538
|
+
`The tailcat archive is larger than ${MAX_ARCHIVE_BYTES} bytes.`,
|
|
539
|
+
502,
|
|
540
|
+
);
|
|
541
|
+
}
|
|
542
|
+
let received = 0;
|
|
543
|
+
this.progress = { phase: 'downloading', received: 0, total, percent: total > 0 ? 0 : null };
|
|
544
|
+
const meter = new Transform({
|
|
545
|
+
transform: (chunk, _encoding, callback) => {
|
|
546
|
+
received += chunk.length;
|
|
547
|
+
if (received > MAX_ARCHIVE_BYTES) {
|
|
548
|
+
callback(new Error(`The tailcat archive is larger than ${MAX_ARCHIVE_BYTES} bytes.`));
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
this.progress = {
|
|
552
|
+
phase: 'downloading',
|
|
553
|
+
received,
|
|
554
|
+
total,
|
|
555
|
+
percent: total > 0 ? Math.min(100, Math.round((received / total) * 100)) : null,
|
|
556
|
+
};
|
|
557
|
+
callback(null, chunk);
|
|
558
|
+
},
|
|
559
|
+
});
|
|
560
|
+
try {
|
|
561
|
+
await pipeline(Readable.fromWeb(response.body), meter, fs.createWriteStream(destination, { mode: 0o600 }));
|
|
562
|
+
} catch (error) {
|
|
563
|
+
await fsp.rm(destination, { force: true }).catch(() => {});
|
|
564
|
+
if (signal.aborted) throw installAborted();
|
|
565
|
+
throw new RemoteAccessError('tailcat-archive-invalid', `Could not download ${url}: ${errorMessage(error)}`, 502);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
/** @private */
|
|
570
|
+
async fetchText(url, signal) {
|
|
571
|
+
let response;
|
|
572
|
+
try {
|
|
573
|
+
response = await this.fetchImpl(url, {
|
|
574
|
+
redirect: 'follow',
|
|
575
|
+
signal,
|
|
576
|
+
headers: { accept: 'text/plain', 'user-agent': `tailcat-webui/${this.releaseTag.replace(/^v/, '')}` },
|
|
577
|
+
});
|
|
578
|
+
} catch (error) {
|
|
579
|
+
if (signal.aborted) throw installAborted();
|
|
580
|
+
throw new RemoteAccessError(
|
|
581
|
+
'tailcat-checksum-missing',
|
|
582
|
+
`Could not download the tailcat release checksums from ${url}: ${errorMessage(error)}`,
|
|
583
|
+
502,
|
|
584
|
+
);
|
|
585
|
+
}
|
|
586
|
+
if (!response.ok) {
|
|
587
|
+
throw new RemoteAccessError(
|
|
588
|
+
'tailcat-checksum-missing',
|
|
589
|
+
`Could not download the tailcat release checksums from ${url}: HTTP ${response.status}.`,
|
|
590
|
+
502,
|
|
591
|
+
);
|
|
592
|
+
}
|
|
593
|
+
const length = Number(response.headers.get('content-length')) || 0;
|
|
594
|
+
if (length > MAX_TEXT_BYTES) {
|
|
595
|
+
throw new RemoteAccessError(
|
|
596
|
+
'tailcat-checksum-missing',
|
|
597
|
+
`The checksum manifest at ${url} is too large to trust.`,
|
|
598
|
+
502,
|
|
599
|
+
);
|
|
600
|
+
}
|
|
601
|
+
const text = await response.text();
|
|
602
|
+
if (text.length > MAX_TEXT_BYTES) {
|
|
603
|
+
throw new RemoteAccessError(
|
|
604
|
+
'tailcat-checksum-missing',
|
|
605
|
+
`The checksum manifest at ${url} is too large to trust.`,
|
|
606
|
+
502,
|
|
607
|
+
);
|
|
608
|
+
}
|
|
609
|
+
return text;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/** @private */
|
|
613
|
+
async extractArchive(archive, destination, spec) {
|
|
614
|
+
let tar = null;
|
|
615
|
+
try {
|
|
616
|
+
tar = await this.resolveExecutable('tar', process.env);
|
|
617
|
+
} catch {
|
|
618
|
+
if (spec.format !== 'zip' || this.platform !== 'win32') {
|
|
619
|
+
throw new RemoteAccessError(
|
|
620
|
+
'tailcat-archive-invalid',
|
|
621
|
+
'The system tar executable is unavailable; cannot extract the verified tailcat archive.',
|
|
622
|
+
502,
|
|
623
|
+
);
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
if (!tar) {
|
|
627
|
+
await this.extractZipWithPowerShell(archive, destination);
|
|
628
|
+
return;
|
|
629
|
+
}
|
|
630
|
+
const args = spec.format === 'zip' ? ['-xf', archive, '-C', destination] : ['-xzf', archive, '-C', destination];
|
|
631
|
+
try {
|
|
632
|
+
await this.runCommand([tar, ...args], { timeoutMs: COMMAND_TIMEOUT_MS });
|
|
633
|
+
if (await findBinary(destination, spec.binary)) return;
|
|
634
|
+
} catch {
|
|
635
|
+
if (spec.format !== 'zip' || this.platform !== 'win32') {
|
|
636
|
+
throw new RemoteAccessError(
|
|
637
|
+
'tailcat-archive-invalid',
|
|
638
|
+
`The tailcat archive could not be extracted: ${spec.asset}.`,
|
|
639
|
+
502,
|
|
640
|
+
);
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
if (spec.format !== 'zip' || this.platform !== 'win32') {
|
|
644
|
+
throw new RemoteAccessError('tailcat-archive-invalid', `The tailcat archive did not contain ${spec.binary}.`, 502);
|
|
645
|
+
}
|
|
646
|
+
await this.extractZipWithPowerShell(archive, destination);
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
/** @private */
|
|
650
|
+
async extractZipWithPowerShell(archive, destination) {
|
|
651
|
+
const powershell = await this.findWindowsPowerShell();
|
|
652
|
+
if (!powershell) {
|
|
653
|
+
throw new RemoteAccessError(
|
|
654
|
+
'tailcat-archive-invalid',
|
|
655
|
+
'The system tar and PowerShell are both unavailable; cannot extract the tailcat archive.',
|
|
656
|
+
502,
|
|
657
|
+
);
|
|
658
|
+
}
|
|
659
|
+
await this.runCommand(
|
|
660
|
+
[
|
|
661
|
+
powershell,
|
|
662
|
+
'-NonInteractive',
|
|
663
|
+
'-Command',
|
|
664
|
+
`Expand-Archive -LiteralPath ${quotePowerShell(archive)} -DestinationPath ${quotePowerShell(destination)} -Force`,
|
|
665
|
+
],
|
|
666
|
+
{ timeoutMs: COMMAND_TIMEOUT_MS },
|
|
667
|
+
);
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/** @private */
|
|
671
|
+
async findWindowsPowerShell() {
|
|
672
|
+
for (const name of ['powershell.exe', 'powershell']) {
|
|
673
|
+
try {
|
|
674
|
+
return await this.resolveExecutable(name, process.env);
|
|
675
|
+
} catch {
|
|
676
|
+
// Try the next spelling.
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
return null;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
/** @private */
|
|
683
|
+
async probe(binary) {
|
|
684
|
+
let result;
|
|
685
|
+
try {
|
|
686
|
+
result = await this.runCommand([binary, 'version'], { timeoutMs: 20_000 });
|
|
687
|
+
} catch (error) {
|
|
688
|
+
throw new RemoteAccessError(
|
|
689
|
+
'tailcat-command-failed',
|
|
690
|
+
`The installed tailcat could not run: ${errorMessage(error)}`,
|
|
691
|
+
502,
|
|
692
|
+
);
|
|
693
|
+
}
|
|
694
|
+
if (result.exitCode !== 0) {
|
|
695
|
+
const detail = lastLine(`${result.stderr}\n${result.stdout}`);
|
|
696
|
+
throw new RemoteAccessError(
|
|
697
|
+
'tailcat-command-failed',
|
|
698
|
+
`The installed tailcat exited with code ${result.exitCode}${detail ? `: ${detail}` : ''}.`,
|
|
699
|
+
502,
|
|
700
|
+
);
|
|
701
|
+
}
|
|
702
|
+
const match = /v?(\d+\.\d+\.\d+)/.exec(`${result.stdout}\n${result.stderr}`);
|
|
703
|
+
return match ? (match[0].startsWith('v') ? match[0] : `v${match[0]}`) : null;
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
/** @private */
|
|
707
|
+
ensureCompatible(version) {
|
|
708
|
+
if (version && !versionAtLeast(version, MIN_TAILCAT_VERSION)) {
|
|
709
|
+
throw new RemoteAccessError(
|
|
710
|
+
'tailcat-version-too-old',
|
|
711
|
+
`tailcat ${version} is too old; ${MIN_TAILCAT_VERSION} or newer is required. Update tailcat and retry.`,
|
|
712
|
+
503,
|
|
713
|
+
);
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
/** @private */
|
|
718
|
+
async replaceBinary(source) {
|
|
719
|
+
await fsp.chmod(source, 0o755).catch(() => {});
|
|
720
|
+
const target = this.binaryPath;
|
|
721
|
+
const backup = `${target}.previous`;
|
|
722
|
+
await fsp.mkdir(path.dirname(target), { recursive: true, mode: 0o700 });
|
|
723
|
+
await fsp.rm(backup, { force: true }).catch(() => {});
|
|
724
|
+
const hadTarget = isFile(target);
|
|
725
|
+
if (hadTarget) await fsp.rename(target, backup);
|
|
726
|
+
try {
|
|
727
|
+
await fsp.rename(source, target);
|
|
728
|
+
await fsp.chmod(target, 0o755).catch(() => {});
|
|
729
|
+
} catch (error) {
|
|
730
|
+
if (hadTarget) await fsp.rename(backup, target).catch(() => {});
|
|
731
|
+
throw new Error(`Could not install tailcat at ${target}: ${errorMessage(error)}`);
|
|
732
|
+
}
|
|
733
|
+
await fsp.rm(backup, { force: true }).catch(() => {});
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
/** @private */
|
|
737
|
+
async writeMetadata(metadata) {
|
|
738
|
+
const temporary = `${this.metadataPath}.tmp-${process.pid}`;
|
|
739
|
+
try {
|
|
740
|
+
await fsp.writeFile(temporary, `${JSON.stringify(metadata, null, 2)}\n`, { mode: 0o600 });
|
|
741
|
+
await fsp.rename(temporary, this.metadataPath);
|
|
742
|
+
} catch (error) {
|
|
743
|
+
await fsp.rm(temporary, { force: true }).catch(() => {});
|
|
744
|
+
this.logger.warn(`tailcat-webui: installed tailcat but could not save metadata: ${errorMessage(error)}`);
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
/** @private */
|
|
749
|
+
readInstalled() {
|
|
750
|
+
try {
|
|
751
|
+
if (!isRegularFile(this.binaryPath)) return null;
|
|
752
|
+
if (this.platform !== 'win32' && !isExecutable(this.binaryPath)) return null;
|
|
753
|
+
try {
|
|
754
|
+
const metadata = JSON.parse(fs.readFileSync(this.metadataPath, 'utf8'));
|
|
755
|
+
return {
|
|
756
|
+
version: metadata.version ?? null,
|
|
757
|
+
source: metadata.source ?? null,
|
|
758
|
+
installedAt: metadata.installedAt ?? null,
|
|
759
|
+
};
|
|
760
|
+
} catch {
|
|
761
|
+
return { version: null, source: null, installedAt: null };
|
|
762
|
+
}
|
|
763
|
+
} catch {
|
|
764
|
+
return null;
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
/** @private */
|
|
769
|
+
async runInRuntime(argv, options = {}) {
|
|
770
|
+
const timeoutMs = options.timeoutMs ?? COMMAND_TIMEOUT_MS;
|
|
771
|
+
const timeout = AbortSignal.timeout(timeoutMs);
|
|
772
|
+
const signal = options.signal ? AbortSignal.any([options.signal, timeout]) : timeout;
|
|
773
|
+
let handle;
|
|
774
|
+
try {
|
|
775
|
+
handle = this.runtime.spawn({
|
|
776
|
+
argv,
|
|
777
|
+
cwd: os.homedir(),
|
|
778
|
+
env: { ...baseEnv(), ...options.env },
|
|
779
|
+
stdio: {
|
|
780
|
+
stdin: 'ignore',
|
|
781
|
+
stdout: { maxBytes: 128 * 1024 },
|
|
782
|
+
stderr: { maxBytes: 128 * 1024 },
|
|
783
|
+
},
|
|
784
|
+
graceMs: 5_000,
|
|
785
|
+
signal,
|
|
786
|
+
});
|
|
787
|
+
} catch (error) {
|
|
788
|
+
throw new RemoteAccessError(
|
|
789
|
+
'tailcat-command-failed',
|
|
790
|
+
`Could not run ${path.basename(argv[0])}: ${errorMessage(error)}`,
|
|
791
|
+
502,
|
|
792
|
+
);
|
|
793
|
+
}
|
|
794
|
+
const outcome = await handle.done;
|
|
795
|
+
if (outcome && 'error' in outcome) {
|
|
796
|
+
throw new RemoteAccessError(
|
|
797
|
+
'tailcat-command-failed',
|
|
798
|
+
`${path.basename(argv[0])} failed: ${errorMessage(outcome.error)}`,
|
|
799
|
+
502,
|
|
800
|
+
);
|
|
801
|
+
}
|
|
802
|
+
const stdout = handle.collected?.stdout?.readFrom(0).text ?? '';
|
|
803
|
+
const stderr = handle.collected?.stderr?.readFrom(0).text ?? '';
|
|
804
|
+
if (outcome.exitCode !== 0) {
|
|
805
|
+
const detail = lastLine(stderr) ?? lastLine(stdout);
|
|
806
|
+
throw new RemoteAccessError(
|
|
807
|
+
'tailcat-command-failed',
|
|
808
|
+
`${path.basename(argv[0])} exited with code ${outcome.exitCode ?? 'null'}${detail ? `: ${detail}` : ''}`,
|
|
809
|
+
502,
|
|
810
|
+
);
|
|
811
|
+
}
|
|
812
|
+
return { stdout, stderr, exitCode: outcome.exitCode };
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
/**
|
|
817
|
+
* Build an aborted-install error with the documented code.
|
|
818
|
+
* @returns {RemoteAccessError} aborted error.
|
|
819
|
+
*/
|
|
820
|
+
function installAborted() {
|
|
821
|
+
return new RemoteAccessError('tailcat-install-aborted', 'The tailcat install was cancelled.', 503);
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
/**
|
|
825
|
+
* Environment for installer commands. It forwards PATH so Homebrew can find
|
|
826
|
+
* its own helpers, and keeps only the non-secret config locations plus HOME.
|
|
827
|
+
* @returns {NodeJS.ProcessEnv} child environment.
|
|
828
|
+
*/
|
|
829
|
+
function baseEnv() {
|
|
830
|
+
const env = { HOME: os.homedir() };
|
|
831
|
+
for (const name of [
|
|
832
|
+
'PATH',
|
|
833
|
+
'APPDATA',
|
|
834
|
+
'LOCALAPPDATA',
|
|
835
|
+
'SYSTEMROOT',
|
|
836
|
+
'TEMP',
|
|
837
|
+
'TMP',
|
|
838
|
+
'TMPDIR',
|
|
839
|
+
'XDG_CONFIG_HOME',
|
|
840
|
+
'XDG_CACHE_HOME',
|
|
841
|
+
'XDG_DATA_HOME',
|
|
842
|
+
]) {
|
|
843
|
+
const value = process.env[name];
|
|
844
|
+
if (value) env[name] = value;
|
|
845
|
+
}
|
|
846
|
+
return env;
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
/**
|
|
850
|
+
* Find a binary in a staging directory, allowing one archive wrapper folder.
|
|
851
|
+
* @param {string} root - extracted root.
|
|
852
|
+
* @param {string} name - expected basename.
|
|
853
|
+
* @returns {Promise<string | null>} absolute path.
|
|
854
|
+
*/
|
|
855
|
+
async function findBinary(root, name) {
|
|
856
|
+
const direct = path.join(root, name);
|
|
857
|
+
if (isRegularFile(direct)) return direct;
|
|
858
|
+
const entries = await fsp.readdir(root, { withFileTypes: true }).catch(() => []);
|
|
859
|
+
for (const entry of entries) {
|
|
860
|
+
if (!entry.isDirectory() || entry.isSymbolicLink()) continue;
|
|
861
|
+
const candidate = path.join(root, entry.name, name);
|
|
862
|
+
if (isRegularFile(candidate)) return candidate;
|
|
863
|
+
}
|
|
864
|
+
return null;
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
/** @param {string} file - path. @returns {boolean} regular file that is not a symlink. */
|
|
868
|
+
function isRegularFile(file) {
|
|
869
|
+
try {
|
|
870
|
+
return fs.lstatSync(file).isFile();
|
|
871
|
+
} catch {
|
|
872
|
+
return false;
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
/** @param {string} file - path. @returns {boolean} regular file. */
|
|
877
|
+
function isFile(file) {
|
|
878
|
+
try {
|
|
879
|
+
return fs.statSync(file).isFile();
|
|
880
|
+
} catch {
|
|
881
|
+
return false;
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
/** @param {string} file - path. @returns {boolean} executable file. */
|
|
886
|
+
function isExecutable(file) {
|
|
887
|
+
try {
|
|
888
|
+
fs.accessSync(file, fs.constants.X_OK);
|
|
889
|
+
return true;
|
|
890
|
+
} catch {
|
|
891
|
+
return false;
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
/** @param {string} value - PowerShell string. @returns {string} single-quoted literal. */
|
|
896
|
+
function quotePowerShell(value) {
|
|
897
|
+
return `'${String(value).replaceAll("'", "''")}'`;
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
/** @param {string} text - command output. @returns {string | null} last non-empty line. */
|
|
901
|
+
function lastLine(text) {
|
|
902
|
+
const lines = String(text)
|
|
903
|
+
.split(/\r?\n/)
|
|
904
|
+
.map((line) => line.trim())
|
|
905
|
+
.filter(Boolean);
|
|
906
|
+
return lines.length > 0 ? lines[lines.length - 1] : null;
|
|
907
|
+
}
|