Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | import { isJsonRpcFailure } from '@metamask/utils';
import { deleteDaemonState } from '@ocap/nodejs/daemon';
import { readFile, rm } from 'node:fs/promises';
import { homedir } from 'node:os';
import { join, resolve } from 'node:path';
import { pathToFileURL } from 'node:url';
import { pingDaemon, sendCommand } from './daemon-client.ts';
import { ensureDaemon } from './daemon-spawn.ts';
const home = homedir();
/**
* Replace the home directory prefix with `~` for display.
*
* @param path - An absolute path.
* @returns The path with the home prefix replaced.
*/
function tildefy(path: string): string {
return path.startsWith(home) ? `~${path.slice(home.length)}` : path;
}
/**
* Check if a value looks like a cluster config (has bootstrap + vats).
*
* @param value - The value to check.
* @returns True if the value has bootstrap and vats fields.
*/
function isClusterConfigLike(
value: unknown,
): value is { vats: Record<string, { bundleSpec?: string }> } {
return (
typeof value === 'object' &&
value !== null &&
'bootstrap' in value &&
'vats' in value &&
typeof (value as { vats: unknown }).vats === 'object'
);
}
/**
* Resolve relative bundleSpec paths in a cluster config to file:// URLs.
*
* @param config - The cluster config object.
* @param config.vats - The vat configurations with optional bundleSpec paths.
* @returns The config with resolved bundleSpec URLs.
*/
function resolveBundleSpecs(config: {
vats: Record<string, { bundleSpec?: string }>;
}): unknown {
const resolvedVats: Record<string, unknown> = {};
for (const [vatName, vatConfig] of Object.entries(config.vats)) {
const spec = vatConfig.bundleSpec;
if (spec && !spec.includes('://')) {
resolvedVats[vatName] = {
...vatConfig,
bundleSpec: pathToFileURL(resolve(spec)).href,
};
} else {
resolvedVats[vatName] = vatConfig;
}
}
return { ...config, vats: resolvedVats };
}
/**
* Stop the daemon via a `shutdown` RPC call. Falls back to PID + SIGTERM if
* the socket is unresponsive, and escalates to SIGKILL if SIGTERM is ignored.
*
* @param socketPath - The daemon socket path.
* @returns True if the daemon was stopped (or was not running), false if it
* failed to stop within the timeout.
*/
export async function stopDaemon(socketPath: string): Promise<boolean> {
const pidPath = join(homedir(), '.ocap', 'daemon.pid');
const pid = await readPidFile(pidPath);
const processAlive = pid !== undefined && isProcessAlive(pid);
const socketResponsive = await pingDaemon(socketPath);
if (!socketResponsive && !processAlive) {
if (pid !== undefined) {
await rm(pidPath, { force: true });
}
process.stderr.write('Daemon is not running.\n');
return true;
}
process.stderr.write('Stopping daemon...\n');
let stopped = false;
// Strategy 1: Graceful socket-based shutdown.
if (socketResponsive) {
try {
await sendCommand({ socketPath, method: 'shutdown' });
} catch {
// Socket became unresponsive.
}
stopped = await waitFor(async () => !(await pingDaemon(socketPath)), 5_000);
}
// Strategy 2: SIGTERM.
if (!stopped && pid !== undefined) {
try {
process.kill(pid, 'SIGTERM');
} catch {
stopped = true;
}
if (!stopped) {
stopped = await waitFor(() => !isProcessAlive(pid), 5_000);
}
}
// Strategy 3: SIGKILL.
if (!stopped && pid !== undefined) {
try {
process.kill(pid, 'SIGKILL');
} catch {
stopped = true;
}
if (!stopped) {
stopped = await waitFor(() => !isProcessAlive(pid), 2_000);
}
}
if (stopped) {
await rm(pidPath, { force: true });
process.stderr.write('Daemon stopped.\n');
} else {
process.stderr.write('Daemon did not stop within timeout.\n');
}
return stopped;
}
/**
* Ensure the daemon is running and print its socket path.
*
* @param socketPath - The daemon socket path.
*/
export async function handleDaemonStart(socketPath: string): Promise<void> {
await ensureDaemon(socketPath);
process.stderr.write(`Daemon running. Socket: ${tildefy(socketPath)}\n`);
}
/**
* Stop the daemon (if running) and delete all state.
*
* @param socketPath - The daemon socket path.
*/
export async function handleDaemonBegone(socketPath: string): Promise<void> {
const stopped = await stopDaemon(socketPath);
if (!stopped) {
process.stderr.write(
'Refusing to delete state while the daemon is still running.\n',
);
process.exitCode = 1;
return;
}
await deleteDaemonState({ socketPath });
process.stderr.write('All daemon state deleted.\n');
}
/**
* Send an RPC method call to the daemon.
*
* @param args - Positional arguments: [method, params-json].
* @param socketPath - The daemon socket path.
* @param options - Additional options.
* @param options.timeoutMs - Read timeout in milliseconds.
*/
export async function handleDaemonExec(
args: string[],
socketPath: string,
{ timeoutMs }: { timeoutMs?: number } = {},
): Promise<void> {
const method = args[0] ?? 'getStatus';
const rawParams = args[1];
// For launchSubcluster: resolve relative bundleSpec paths to file:// URLs.
let params: Record<string, unknown> | undefined;
if (rawParams !== undefined) {
try {
const parsed = JSON.parse(rawParams) as Record<string, unknown>;
const { config } = parsed as { config?: unknown };
if (method === 'launchSubcluster' && isClusterConfigLike(config)) {
params = {
...parsed,
config: resolveBundleSpecs(config),
};
} else {
params = parsed;
}
} catch {
process.stderr.write('Error: params-json must be valid JSON.\n');
process.exitCode = 1;
return;
}
}
const response = await sendCommand({
socketPath,
method,
params,
...(timeoutMs === undefined ? {} : { timeoutMs }),
});
if (isJsonRpcFailure(response)) {
process.stderr.write(
`Error: ${response.error.message} (code ${String(response.error.code)})\n`,
);
process.exitCode = 1;
return;
}
const isTTY = process.stdout.isTTY ?? false;
if (isTTY) {
process.stdout.write(`${JSON.stringify(response.result, null, 2)}\n`);
} else {
process.stdout.write(`${JSON.stringify(response.result)}\n`);
}
}
/**
* Read a PID from a file.
*
* @param pidPath - The PID file path.
* @returns The PID, or undefined if the file is missing or invalid.
*/
async function readPidFile(pidPath: string): Promise<number | undefined> {
try {
const pid = Number(await readFile(pidPath, 'utf-8'));
return pid > 0 && !Number.isNaN(pid) ? pid : undefined;
} catch {
return undefined;
}
}
/**
* Check whether a process is alive by sending signal 0.
*
* @param pid - The process ID to check.
* @returns True if the process exists.
*/
function isProcessAlive(pid: number): boolean {
try {
process.kill(pid, 0);
return true;
} catch {
return false;
}
}
/**
* Poll until a condition is met or the timeout elapses.
*
* @param check - A function that returns true when the condition is met.
* @param timeoutMs - Maximum time to wait in milliseconds.
* @returns True if the condition was met, false on timeout.
*/
async function waitFor(
check: () => boolean | Promise<boolean>,
timeoutMs: number,
): Promise<boolean> {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
if (await check()) {
return true;
}
await new Promise((resolveTimeout) => setTimeout(resolveTimeout, 250));
}
return await check();
}
|