Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/canvas/src/layout.ts

Raw
import { type ChildProcess, spawn } from "node:child_process";
import { access, mkdtemp, readdir, rm } from "node:fs/promises";
import { homedir, tmpdir } from "node:os";
import { delimiter, dirname, join } from "node:path";
import { requestedDocument } from "./document.ts";
import { type CanvasServer, startCanvasServer } from "./server.ts";
import type { CanvasDraw, CanvasElement, CanvasState } from "./store.ts";

const LIMIT = 1024 * 1024;
const STARTUP_MS = 15_000;
const EVAL_MS = 15_000;
const CLOSE_MS = 2_000;

type Diagnostics = {
	launches: number;
	requests: number;
	running: boolean;
	closed: boolean;
	executable?: string;
};
export type CanvasLayout = {
	generate(
		state: CanvasState,
		draw: CanvasDraw,
		signal?: AbortSignal,
	): Promise<CanvasElement[]>;
	close(): Promise<void>;
	diagnostics(): Diagnostics;
};

type Cdp = {
	call(
		method: string,
		params?: Record<string, unknown>,
	): Promise<Record<string, unknown>>;
	close(): void;
};
type Runtime = {
	browser: ChildProcess;
	profile: string;
	cdp?: Cdp;
	stopped: boolean;
	stopping?: Promise<void>;
};

export function createCanvasLayout(serverUrl?: string): CanvasLayout {
	let runtime: Runtime | undefined;
	let starting: Promise<Runtime> | undefined;
	let pageServer: CanvasServer | undefined;
	let closing: Promise<void> | undefined;
	let busy = false;
	const lifetime = new AbortController();
	const info: Diagnostics = {
		launches: 0,
		requests: 0,
		running: false,
		closed: false,
	};

	async function generate(
		state: CanvasState,
		draw: CanvasDraw,
		signal?: AbortSignal,
	): Promise<CanvasElement[]> {
		if (info.closed) throw new Error("Canvas layout is closed.");
		throwIfAborted(signal);
		if (!draw.additions.length && !draw.relabels.length && !draw.mermaid)
			return requestedDocument(state, draw, []);
		const payload = JSON.stringify({ state, draw });
		if (payload.length > LIMIT)
			throw new Error("Canvas layout input exceeds limit.");
		if (busy) throw new Error("Another canvas layout request is running.");
		busy = true;
		const operationSignal = signal
			? AbortSignal.any([signal, lifetime.signal])
			: lifetime.signal;
		let active: Runtime | undefined;
		try {
			active = await connect(operationSignal);
			throwIfAborted(operationSignal);
			info.requests++;
			if (!active.cdp) throw new Error("Canvas layout is unavailable.");
			const result = await abortable(
				active.cdp.call("Runtime.evaluate", {
					expression: `(() => { const payload = ${payload.replace(/</g, "\\u003c")}; return globalThis.canvasGenerate(payload.state, payload.draw); })()`,
					awaitPromise: true,
					returnByValue: true,
				}),
				operationSignal,
			);
			const value = (result["result"] as { value?: unknown } | undefined)
				?.value;
			if (!Array.isArray(value) || JSON.stringify(value).length > LIMIT)
				throw new Error("Canvas layout returned invalid output.");
			return value as CanvasElement[];
		} catch (error) {
			if (active) await stop(active);
			throw error;
		} finally {
			busy = false;
		}
	}

	function connect(signal: AbortSignal): Promise<Runtime> {
		if (runtime) return Promise.resolve(runtime);
		starting ??= start(
			AbortSignal.any([signal, AbortSignal.timeout(STARTUP_MS)]),
		).finally(() => {
			starting = undefined;
		});
		return starting;
	}

	async function start(signal: AbortSignal): Promise<Runtime> {
		let active: Runtime | undefined;
		let createdProfile: string | undefined;
		try {
			const profile = await mkdtemp(join(tmpdir(), "pi-canvas-layout-"));
			createdProfile = profile;
			throwIfAborted(signal);
			if (info.closed) {
				await removeProfile(profile);
				throw new Error("Canvas layout is closed.");
			}
			const executable = await findChromium();
			info.executable = executable;
			throwIfAborted(signal);
			if (info.closed) {
				await removeProfile(profile);
				throw new Error("Canvas layout is closed.");
			}
			const browser = spawn(
				executable,
				[
					"--headless=new",
					"--remote-debugging-port=0",
					`--user-data-dir=${profile}`,
					"--no-first-run",
					"--disable-gpu",
					"about:blank",
				],
				{
					stdio: ["ignore", "ignore", "pipe"],
					env: {
						...process.env,
						PATH: [dirname(executable), process.env["PATH"]]
							.filter(Boolean)
							.join(delimiter),
					},
				},
			);
			active = { browser, profile, stopped: false };
			info.launches++;
			info.running = true;
			browser.once("exit", () => {
				active?.cdp?.close();
				if (runtime === active) runtime = undefined;
				if (info.closed || runtime === undefined) info.running = false;
				void removeProfile(profile).catch(() => undefined);
			});
			const endpoint = await abortable(startupEndpoint(active), signal);
			if (info.closed) throw new Error("Canvas layout is closed.");
			const base = await layoutUrl();
			throwIfAborted(signal);
			const target = new URL(endpoint);
			target.protocol = "http:";
			target.pathname = "/json/new";
			target.search = `?${encodeURIComponent(base)}`;
			const page = (await abortable(
				boundedFetch(target, STARTUP_MS),
				signal,
			)) as {
				webSocketDebuggerUrl?: unknown;
			};
			if (typeof page.webSocketDebuggerUrl !== "string")
				throw new Error("Chromium did not create a layout page.");
			active.cdp = await openCdp(page.webSocketDebuggerUrl, signal);
			for (let attempt = 0; attempt < 100; attempt++) {
				throwIfAborted(signal);
				const ready = await abortable(
					active.cdp.call("Runtime.evaluate", {
						expression: "typeof globalThis.canvasGenerate === 'function'",
						awaitPromise: true,
						returnByValue: true,
					}),
					signal,
				);
				if (
					(ready["result"] as { value?: unknown } | undefined)?.value === true
				) {
					if (info.closed) throw new Error("Canvas layout is closed.");
					runtime = active;
					return active;
				}
				await delay(100);
			}
			throw new Error("Canvas layout page did not load.");
		} catch (error) {
			if (active) await stop(active);
			else if (createdProfile) await removeProfile(createdProfile);
			throw error;
		}
	}

	async function layoutUrl(): Promise<string> {
		if (serverUrl) return new URL("/layout", serverUrl).href;
		pageServer ??= await startCanvasServer(
			() => ({ revision: 0, elements: [] }),
			() => undefined,
		);
		return new URL("/layout", pageServer.url).href;
	}

	return {
		generate,
		diagnostics: () => ({ ...info }),
		close(): Promise<void> {
			closing ??= (async () => {
				info.closed = true;
				lifetime.abort(new Error("Canvas layout is closed."));
				const active = runtime;
				if (active) await stop(active);
				await starting?.catch(() => undefined);
				if (runtime) await stop(runtime);
				await pageServer?.close();
				pageServer = undefined;
				info.running = false;
			})();
			return closing;
		},
	};

	function stop(active: Runtime): Promise<void> {
		active.stopping ??= (async () => {
			active.stopped = true;
			active.cdp?.close();
			if (runtime === active) runtime = undefined;
			if (
				active.browser.exitCode === null &&
				active.browser.signalCode === null
			) {
				active.browser.kill("SIGTERM");
				if (!(await exited(active.browser, CLOSE_MS))) {
					active.browser.kill("SIGKILL");
					await exited(active.browser, CLOSE_MS);
				}
			}
			await removeProfile(active.profile);
			if (runtime === undefined) info.running = false;
		})();
		return active.stopping;
	}
}

async function startupEndpoint(active: Runtime): Promise<string> {
	const stderr = active.browser.stderr;
	if (!stderr) throw new Error("Chromium startup failed.");
	return new Promise((resolve, reject) => {
		let text = "";
		let first = "";
		const finish = (fn: () => void) => {
			clearTimeout(timer);
			stderr.off("data", onData);
			active.browser.off("error", onError);
			active.browser.off("exit", onExit);
			fn();
		};
		const onData = (chunk: Buffer) => {
			const incoming = chunk.toString();
			first = (first + incoming).slice(0, 4096);
			text = (text + incoming).slice(-8192);
			const endpoint = text.match(/DevTools listening on (ws:\/\/[^\s]+)/)?.[1];
			if (endpoint) finish(() => resolve(endpoint));
		};
		const onError = () =>
			finish(() => reject(new Error("Chromium startup failed.")));
		const onExit = (code: number | null, signal: NodeJS.Signals | null) => {
			// about:blank is the only page before this endpoint exists.
			// Keep actionable launch evidence, never profile paths or URL tokens.
			const scrub = (value: string) => {
				const safe = value
					.replaceAll(active.profile, "[profile]")
					.replaceAll(homedir(), "[home]")
					.replace(/(?:https?|wss?):\/\/\S+/gu, "[url]")
					.replace(
						/\b(?:authorization|cookie|password|token|api[-_]?key)\s*[:=]\s*\S+/giu,
						"[credential]",
					)
					.replace(/\x1b\[[0-?]*[ -/]*[@-~]/gu, "");
				return safe.length <= 2048
					? safe
					: `${safe.slice(0, 1400)}\n[...truncated...]\n${safe.slice(-600)}`;
			};
			const diagnostic = {
				executable: scrub(active.browser.spawnfile),
				uid: process.getuid?.() ?? null,
				code,
				signal,
				stderr: scrub(
					first === text
						? text
						: `${first}\n[...tail...]\n${text.slice(-1024)}`,
				),
			};
			finish(() =>
				reject(
					new Error(
						`Chromium exited during startup. ${JSON.stringify(diagnostic)}`,
					),
				),
			);
		};
		const timer = setTimeout(
			() => finish(() => reject(new Error("Chromium startup timed out."))),
			STARTUP_MS,
		);
		stderr.on("data", onData);
		active.browser.once("error", onError);
		active.browser.once("exit", onExit);
	});
}

async function boundedFetch(url: URL, timeout: number): Promise<unknown> {
	const controller = new AbortController();
	const timer = setTimeout(() => controller.abort(), timeout);
	try {
		const response = await fetch(url, {
			method: "PUT",
			signal: controller.signal,
		});
		if (!response.ok) throw new Error("Chromium did not create a layout page.");
		return await response.json();
	} catch {
		throw new Error("Chromium layout page request failed.");
	} finally {
		clearTimeout(timer);
	}
}

async function exited(
	browser: ChildProcess,
	timeout: number,
): Promise<boolean> {
	if (browser.exitCode !== null) return true;
	return new Promise((resolve) => {
		const timer = setTimeout(() => {
			browser.off("exit", done);
			resolve(false);
		}, timeout);
		const done = () => {
			clearTimeout(timer);
			resolve(true);
		};
		browser.once("exit", done);
	});
}

async function removeProfile(path: string): Promise<void> {
	await rm(path, {
		recursive: true,
		force: true,
		maxRetries: 2,
		retryDelay: 100,
	});
}

export async function findChromium(): Promise<string> {
	const names =
		process.platform === "win32"
			? ["chrome.exe", "chromium.exe"]
			: [
					"chromium",
					"chromium-browser",
					"google-chrome",
					"google-chrome-stable",
				];
	const prefix = process.env["PREFIX"];
	const locations =
		process.platform === "darwin"
			? [
					"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
					"/Applications/Chromium.app/Contents/MacOS/Chromium",
					join(
						process.env["HOME"] ?? "",
						"Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
					),
				]
			: process.platform === "win32"
				? [
						"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
						"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",
						join(
							process.env["LOCALAPPDATA"] ?? "",
							"Google/Chrome/Application/chrome.exe",
						),
					]
				: [
						join(dirname(process.execPath), "chromium"),
						join(dirname(process.execPath), "chrome"),
						...(prefix
							? [
									join(prefix, "bin/chromium"),
									join(prefix, "bin/chromium-browser"),
								]
							: []),
					];
	for (const path of [
		...locations,
		...(await playwrightChromiumLocations()),
		...(process.env["PATH"]
			?.split(delimiter)
			.flatMap((dir) => names.map((name) => join(dir, name))) ?? []),
	])
		if (
			await access(path).then(
				() => true,
				() => false,
			)
		)
			return path;
	throw new Error(
		"Canvas shape additions and text/Mermaid layout need a local Chrome or Chromium installation.",
	);
}

async function playwrightChromiumLocations(): Promise<string[]> {
	const cache = process.env["PLAYWRIGHT_BROWSERS_PATH"];
	const executable = playwrightChromiumExecutable();
	if (!cache || !executable) return [];
	try {
		return (await readdir(cache, { withFileTypes: true }))
			.filter(
				(entry) => entry.isDirectory() && entry.name.startsWith("chromium-"),
			)
			.map((entry) => join(cache, entry.name, ...executable));
	} catch {
		return [];
	}
}

function playwrightChromiumExecutable(): string[] | undefined {
	if (process.platform === "win32") return ["chrome-win64", "chrome.exe"];
	if (process.arch !== "x64" && process.arch !== "arm64") return undefined;
	if (process.platform === "darwin")
		return [
			`chrome-mac-${process.arch}`,
			"Google Chrome for Testing.app",
			"Contents",
			"MacOS",
			"Google Chrome for Testing",
		];
	return [
		process.arch === "arm64" ? "chrome-linux-arm64" : "chrome-linux64",
		"chrome",
	];
}

export async function openCdp(url: string, signal: AbortSignal): Promise<Cdp> {
	throwIfAborted(signal);
	const socket = new WebSocket(url);
	await new Promise<void>((resolve, reject) => {
		const timer = setTimeout(
			() => finish(() => reject(new Error("CDP connection timed out."))),
			STARTUP_MS,
		);
		const finish = (fn: () => void) => {
			clearTimeout(timer);
			socket.removeEventListener("open", open);
			socket.removeEventListener("error", error);
			socket.removeEventListener("close", close);
			signal.removeEventListener("abort", abort);
			fn();
		};
		const abort = () =>
			finish(() => {
				socket.close();
				reject(signal.reason ?? new Error("Canvas layout cancelled."));
			});
		signal.addEventListener("abort", abort, { once: true });
		const open = () => finish(resolve);
		const error = () =>
			finish(() => reject(new Error("CDP connection failed.")));
		const close = () =>
			finish(() => reject(new Error("CDP connection closed.")));
		socket.addEventListener("open", open, { once: true });
		socket.addEventListener("error", error, { once: true });
		socket.addEventListener("close", close, { once: true });
	});
	let id = 0;
	let closed = false;
	const pending = new Map<
		number,
		{
			resolve(value: Record<string, unknown>): void;
			reject(reason: Error): void;
			timer: ReturnType<typeof setTimeout>;
		}
	>();
	const rejectAll = (reason: Error) => {
		for (const call of pending.values()) {
			clearTimeout(call.timer);
			call.reject(reason);
		}
		pending.clear();
	};
	socket.addEventListener("message", ({ data }) => {
		try {
			const text = String(data);
			if (text.length > LIMIT * 2) {
				rejectAll(new Error("Canvas layout response exceeds limit."));
				socket.close();
				return;
			}
			const message = JSON.parse(text) as {
				id?: number;
				result?: Record<string, unknown>;
				error?: unknown;
			};
			if (message.id === undefined) return;
			const call = pending.get(message.id);
			if (!call) return;
			pending.delete(message.id);
			clearTimeout(call.timer);
			message.error
				? call.reject(new Error("CDP evaluation failed."))
				: call.resolve(message.result ?? {});
		} catch {
			/* ignore malformed CDP messages */
		}
	});
	socket.addEventListener("close", () => {
		closed = true;
		rejectAll(new Error("Canvas layout closed."));
	});
	socket.addEventListener("error", () => {
		rejectAll(new Error("CDP connection failed."));
	});
	return {
		call(method, params = {}) {
			if (closed || socket.readyState !== WebSocket.OPEN)
				return Promise.reject(new Error("Canvas layout closed."));
			if (pending.size >= 16)
				return Promise.reject(new Error("Too many canvas layout requests."));
			const request = ++id;
			return new Promise((resolve, reject) => {
				const timer = setTimeout(() => {
					const call = pending.get(request);
					if (call) {
						pending.delete(request);
						call.reject(new Error("Canvas layout evaluation timed out."));
					}
				}, EVAL_MS);
				pending.set(request, { resolve, reject, timer });
				try {
					socket.send(JSON.stringify({ id: request, method, params }));
				} catch {
					clearTimeout(timer);
					pending.delete(request);
					reject(new Error("CDP connection failed."));
				}
			});
		},
		close() {
			if (!closed) socket.close();
			closed = true;
			rejectAll(new Error("Canvas layout closed."));
		},
	};
}

function delay(ms: number): Promise<void> {
	return new Promise((resolve) => setTimeout(resolve, ms));
}
function throwIfAborted(signal?: AbortSignal): void {
	if (signal?.aborted)
		throw signal.reason ?? new DOMException("Aborted", "AbortError");
}
function abortable<T>(promise: Promise<T>, signal?: AbortSignal): Promise<T> {
	if (!signal) return promise;
	throwIfAborted(signal);
	return new Promise<T>((resolve, reject) => {
		const abort = () =>
			finish(() =>
				reject(signal.reason ?? new DOMException("Aborted", "AbortError")),
			);
		const finish = (fn: () => void) => {
			signal.removeEventListener("abort", abort);
			fn();
		};
		promise.then(
			(value) => finish(() => resolve(value)),
			(error: unknown) => finish(() => reject(error)),
		);
		signal.addEventListener("abort", abort, { once: true });
	});
}