2026-11-03 14:36:21 +00:00
|
|
|
import { isIP } from "node:net";
|
2026-11-03 13:52:28 +00:00
|
|
|
import { TextDecoder } from "node:util";
|
2026-11-03 14:36:21 +00:00
|
|
|
import WebSocket, { RawData } from "ws";
|
2026-11-03 13:52:28 +00:00
|
|
|
import {
|
|
|
|
|
ActorSnapshot,
|
2026-11-03 14:36:21 +00:00
|
|
|
decodeNotification,
|
2026-11-03 13:52:28 +00:00
|
|
|
decodeResponse,
|
|
|
|
|
isActorRef,
|
2026-11-03 14:36:21 +00:00
|
|
|
MAX_MESSAGE_BYTES,
|
2026-11-03 13:52:28 +00:00
|
|
|
PROTOCOL_VERSION,
|
|
|
|
|
ProtocolError,
|
|
|
|
|
ReadCommand,
|
|
|
|
|
ReadReply,
|
2026-11-03 14:36:21 +00:00
|
|
|
WEBSOCKET_SUBPROTOCOL,
|
2026-11-03 13:52:28 +00:00
|
|
|
} from "./protocol";
|
|
|
|
|
|
|
|
|
|
export function parseEndpoint(endpoint: string): { host: string; port: number } {
|
|
|
|
|
const match = /^(?:\[([^\]]+)\]|([^:]+)):(\d+)$/.exec(endpoint);
|
|
|
|
|
if (!match) throw new Error("Use a numeric loopback address, e.g. 127.0.0.1:3737 or [::1]:3737");
|
|
|
|
|
const host = match[1] ?? match[2];
|
|
|
|
|
if (
|
|
|
|
|
host === undefined ||
|
|
|
|
|
!((isIP(host) === 4 && host.startsWith("127.")) || (isIP(host) === 6 && host === "::1"))
|
|
|
|
|
) {
|
|
|
|
|
throw new Error("Only numeric IPv4 loopback or [::1] endpoints are supported");
|
|
|
|
|
}
|
|
|
|
|
const port = Number(match[3]);
|
2026-11-03 14:36:21 +00:00
|
|
|
if (!Number.isInteger(port) || port < 1 || port > 65535)
|
|
|
|
|
throw new Error("Invalid WebSocket port");
|
2026-11-03 13:52:28 +00:00
|
|
|
return { host, port };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface Pending {
|
|
|
|
|
requestId: string;
|
|
|
|
|
command: ReadCommand;
|
|
|
|
|
resolve: (reply: ReadReply) => void;
|
|
|
|
|
reject: (error: Error) => void;
|
|
|
|
|
timer: NodeJS.Timeout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class ImageClient {
|
|
|
|
|
private pending?: Pending;
|
|
|
|
|
private queue: Promise<unknown> = Promise.resolve();
|
|
|
|
|
private sequence = 0;
|
2026-11-03 14:36:21 +00:00
|
|
|
private revision = 0n;
|
2026-11-03 13:52:28 +00:00
|
|
|
private closed = false;
|
2026-11-03 14:36:21 +00:00
|
|
|
private ready = false;
|
2026-11-03 13:52:28 +00:00
|
|
|
|
|
|
|
|
private constructor(
|
2026-11-03 14:36:21 +00:00
|
|
|
private readonly socket: WebSocket,
|
2026-11-03 13:52:28 +00:00
|
|
|
private readonly onDisconnect: (error?: Error) => void,
|
2026-11-03 14:36:21 +00:00
|
|
|
private readonly onImageChanged: (revision: bigint) => void,
|
2026-11-03 13:52:28 +00:00
|
|
|
private readonly requestTimeout: number,
|
|
|
|
|
) {
|
2026-11-03 14:36:21 +00:00
|
|
|
socket.on("message", (data, binary) => this.receive(data, binary));
|
2026-11-03 13:52:28 +00:00
|
|
|
socket.on("error", (error) => this.close(error));
|
2026-11-03 14:36:21 +00:00
|
|
|
socket.on("close", () => this.close(new Error("Image WebSocket closed")));
|
2026-11-03 13:52:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async connect(
|
|
|
|
|
endpoint: string,
|
|
|
|
|
onDisconnect: (error?: Error) => void,
|
2026-11-03 14:36:21 +00:00
|
|
|
onImageChanged: (revision: bigint) => void = () => {},
|
2026-11-03 13:52:28 +00:00
|
|
|
timeouts = { connect: 5000, request: 30000 },
|
|
|
|
|
): Promise<ImageClient> {
|
2026-11-03 14:36:21 +00:00
|
|
|
parseEndpoint(endpoint);
|
|
|
|
|
const socket = new WebSocket(`ws://${endpoint}/`, WEBSOCKET_SUBPROTOCOL, {
|
|
|
|
|
handshakeTimeout: timeouts.connect,
|
|
|
|
|
maxPayload: MAX_MESSAGE_BYTES,
|
|
|
|
|
perMessageDeflate: false,
|
|
|
|
|
followRedirects: false,
|
2026-11-03 13:52:28 +00:00
|
|
|
});
|
2026-11-03 14:36:21 +00:00
|
|
|
const client = new ImageClient(socket, onDisconnect, onImageChanged, timeouts.request);
|
|
|
|
|
try {
|
|
|
|
|
await new Promise<void>((resolve, reject) => {
|
|
|
|
|
const fail = (error: Error) => {
|
|
|
|
|
socket.off("open", opened);
|
|
|
|
|
reject(error);
|
|
|
|
|
};
|
|
|
|
|
const opened = () => {
|
|
|
|
|
socket.off("error", fail);
|
|
|
|
|
if (socket.protocol !== WEBSOCKET_SUBPROTOCOL) {
|
|
|
|
|
reject(new Error("Server did not negotiate runa-debug"));
|
|
|
|
|
} else {
|
|
|
|
|
client.ready = true;
|
|
|
|
|
resolve();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
socket.once("error", fail);
|
|
|
|
|
socket.once("open", opened);
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
client.close();
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
return client;
|
2026-11-03 13:52:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async listActors(): Promise<ActorSnapshot[]> {
|
|
|
|
|
const reply = await this.request({ operation: "list_actors" });
|
|
|
|
|
if (reply.kind !== "actors") throw new Error("Expected actor listing");
|
|
|
|
|
return reply.data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async inspectActor(actorRef: string): Promise<ActorSnapshot> {
|
|
|
|
|
if (!isActorRef(actorRef)) throw new Error("Invalid actor reference");
|
|
|
|
|
const reply = await this.request({
|
|
|
|
|
operation: "inspect_actor",
|
|
|
|
|
parameters: { actor_ref: actorRef },
|
|
|
|
|
});
|
|
|
|
|
if (reply.kind !== "actor") throw new Error("Expected actor snapshot");
|
|
|
|
|
return reply.data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private request(command: ReadCommand): Promise<ReadReply> {
|
|
|
|
|
const request = this.queue.then(
|
|
|
|
|
() =>
|
|
|
|
|
new Promise<ReadReply>((resolve, reject) => {
|
|
|
|
|
if (this.closed) {
|
|
|
|
|
reject(new Error("Not connected to an image"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const requestId = `runa-vscode-${++this.sequence}`;
|
2026-11-03 14:36:21 +00:00
|
|
|
const message = JSON.stringify({
|
2026-11-03 13:52:28 +00:00
|
|
|
version: PROTOCOL_VERSION,
|
|
|
|
|
request_id: requestId,
|
|
|
|
|
command,
|
|
|
|
|
});
|
|
|
|
|
const timer = setTimeout(
|
|
|
|
|
() => this.close(new Error("Image request timed out; connection closed without retry")),
|
|
|
|
|
this.requestTimeout,
|
|
|
|
|
);
|
|
|
|
|
this.pending = { requestId, command, resolve, reject, timer };
|
2026-11-03 14:36:21 +00:00
|
|
|
this.socket.send(message, (error) => {
|
2026-11-03 13:52:28 +00:00
|
|
|
if (error) this.close(error);
|
|
|
|
|
});
|
|
|
|
|
}),
|
|
|
|
|
);
|
2026-11-03 14:36:21 +00:00
|
|
|
// Each caller retains its own rejection, without poisoning subsequent requests.
|
2026-11-03 13:52:28 +00:00
|
|
|
this.queue = request.then(
|
|
|
|
|
() => undefined,
|
|
|
|
|
() => undefined,
|
|
|
|
|
);
|
|
|
|
|
return request;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-03 14:36:21 +00:00
|
|
|
private receive(data: RawData, binary: boolean): void {
|
2026-11-03 13:52:28 +00:00
|
|
|
if (this.closed) return;
|
2026-11-03 14:36:21 +00:00
|
|
|
try {
|
|
|
|
|
if (binary) throw new Error("Only WebSocket text protocol messages are accepted");
|
|
|
|
|
const bytes = Array.isArray(data)
|
|
|
|
|
? Buffer.concat(data)
|
|
|
|
|
: data instanceof ArrayBuffer
|
|
|
|
|
? Buffer.from(data)
|
|
|
|
|
: data;
|
|
|
|
|
if (bytes.length > MAX_MESSAGE_BYTES) throw new Error("Image response exceeds 1 MiB");
|
|
|
|
|
const text = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
|
|
|
const revision = decodeNotification(text);
|
|
|
|
|
if (revision !== undefined) {
|
|
|
|
|
if (revision <= this.revision)
|
|
|
|
|
throw new Error("Non-increasing image notification revision");
|
|
|
|
|
this.revision = revision;
|
|
|
|
|
this.onImageChanged(revision);
|
2026-11-03 13:52:28 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const pending = this.pending;
|
2026-11-03 14:36:21 +00:00
|
|
|
if (!pending) throw new Error("Unexpected unsolicited image response");
|
2026-11-03 13:52:28 +00:00
|
|
|
try {
|
2026-11-03 14:36:21 +00:00
|
|
|
pending.resolve(decodeResponse(text, pending.requestId, pending.command));
|
2026-11-03 13:52:28 +00:00
|
|
|
} catch (error) {
|
2026-11-03 14:36:21 +00:00
|
|
|
if (!(error instanceof ProtocolError)) throw error;
|
|
|
|
|
pending.reject(error);
|
2026-11-03 13:52:28 +00:00
|
|
|
}
|
2026-11-03 14:36:21 +00:00
|
|
|
this.pending = undefined;
|
|
|
|
|
clearTimeout(pending.timer);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
this.close(error instanceof Error ? error : new Error(String(error)));
|
2026-11-03 13:52:28 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
close(error?: Error): void {
|
|
|
|
|
if (this.closed) return;
|
|
|
|
|
this.closed = true;
|
2026-11-03 14:36:21 +00:00
|
|
|
this.socket.terminate();
|
2026-11-03 13:52:28 +00:00
|
|
|
if (this.pending) {
|
|
|
|
|
clearTimeout(this.pending.timer);
|
|
|
|
|
this.pending.reject(error ?? new Error("Disconnected from image"));
|
|
|
|
|
this.pending = undefined;
|
|
|
|
|
}
|
2026-11-03 14:36:21 +00:00
|
|
|
if (this.ready) this.onDisconnect(error);
|
2026-11-03 13:52:28 +00:00
|
|
|
}
|
|
|
|
|
}
|